Merge pull request #11 from josedario87/codex/update-conversation-agent-for-gemini-sdk-and-mcp-server-inte
All checks were successful
Deploy conversation layer / deploy (push) Successful in 40s

lo agregamos para debugearlo aun es probable el rollback
This commit is contained in:
josedario87
2025-06-05 01:08:00 -06:00
committed by GitHub
6 changed files with 918 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
# ---------- Build stage ----------
FROM node:18-alpine AS builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
@@ -7,7 +7,7 @@ COPY . .
RUN npm run build
# ---------- Production stage ----------
FROM node:18-alpine
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,8 @@
},
"dependencies": {
"express": "^4.18.2",
"@google/generative-ai": "^0.5.0",
"@google/genai": "^1.4.0",
"@modelcontextprotocol/sdk": "^1.12.1",
"dotenv": "^16.5.0"
},
"devDependencies": {

View File

@@ -1,5 +1,7 @@
import express from 'express';
import { GoogleGenerativeAI } from '@google/generative-ai';
import { GoogleGenAI, mcpToTool } from '@google/genai';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import dotenv from 'dotenv';
dotenv.config();
@@ -8,9 +10,20 @@ const PORT = Number(process.env.PORT) || 8001;
const API_KEY = process.env.GEMINI_API_KEY || '';
console.log(`Using Gemini API key: ${API_KEY}`);
const genAI = API_KEY ? new GoogleGenerativeAI(API_KEY) : null;
const model = genAI ? genAI.getGenerativeModel({ model: 'gemini-pro' }) : null;
const genAI = API_KEY ? new GoogleGenAI({ apiKey: API_KEY }) : null;
const MCP_URL = process.env.MCP_URL || 'http://planilla.interno.com/mcp';
let mcpClient: Client | undefined;
let mcpTransport: StreamableHTTPClientTransport | undefined;
async function getMcpClient(): Promise<Client> {
if (!mcpClient) {
mcpClient = new Client({ name: 'planilla-client', version: '1.0.0' });
mcpTransport = new StreamableHTTPClientTransport(new URL(MCP_URL));
await mcpClient.connect(mcpTransport);
}
return mcpClient;
}
const repoInfo = `This repository contains a WhatsApp router, a simple chat UI and now a conversation-layer-agent service.
- whatsapp-router: Forwards WhatsApp messages to configured agents.
- chat-ui: Minimal web interface that also talks to an agent.
@@ -24,14 +37,24 @@ app.post('/', async (req, res) => {
const message = req.body?.message as string | undefined;
if (!message) return res.status(400).json({ error: 'Missing message' });
if (!model) {
if (!genAI) {
return res.json({ reply: repoInfo });
}
try {
const prompt = `Repo information: ${repoInfo}\nUser message: ${message}`;
const result = await model.generateContent(prompt);
const reply = result.response.text().trim();
const contents = `Repo information: ${repoInfo}\nUser message: ${message}`;
const config: any = {};
if (message.toLowerCase().includes('/planilla')) {
console.log('Using Model Context Protocol tools ', MCP_URL);
const client = await getMcpClient();
config.tools = [mcpToTool(client)];
}
const result = await genAI.models.generateContent({
model: 'gemini-2.0-flash',
contents,
config,
});
const reply = (result.text || '').trim();
res.json({ reply });
} catch (err: any) {
console.error('Gemini error', err.message);

View File

@@ -1,11 +1,12 @@
import { helloWorldAgent } from './helloAgent';
import { WhatsAppMessage } from './types';
import dotenv from 'dotenv';
dotenv.config();
export type Handler = string | ((msg: WhatsAppMessage | string) => Promise<string>);
export const chatHandlers: Record<string, Handler> = {
'50498554225@c.us': 'http://conversation-layer-agent:8001',
'repo-helper@c.us': 'http://conversation-layer-agent:8001',
'50498554225@c.us': process.env.CONVERSATION_AGENT_URL || 'http://conversation-layer-agent:8001',
// Add other mappings like:
// '50496210031@c.us': 'http://llm-agent:8000'
};

View File

@@ -156,7 +156,7 @@ app.post('/webhook', async (req: express.Request, res: express.Response) => {
if (!handler) throw new Error('No handler configured');
let reply: string;
if (typeof handler === 'string') {
const agentRes = await axios.post(handler, {message});
const agentRes = await axios.post(handler, {message: message.text});
reply = agentRes.data.reply || agentRes.data;
} else {
reply = await handler(message);