import express from 'express'; import { genAI, getMcpTool } from './llm/gemini'; import { systemPrompt } from './systemPrompt'; // Import the repository info from a separate file import { iniciarProcesoCognitivo } from './cognition/index'; // Import the MCP function to start cognitive processes import type { Conversation, Msg, Participant } from './types'; // Import the Conversation type import dotenv from 'dotenv'; dotenv.config(); const PORT = Number(process.env.PORT) || 8001; const API_KEY = process.env.GEMINI_API_KEY || ''; console.log(`Using Gemini API key: ${API_KEY}`); /** * Descripción de alto nivel para que cualquier agente (humano o LLM) entienda y * trabaje con el repositorio sin perder tiempo buscando contexto. */ const app = express(); app.use(express.json()); app.post('/', async (req, res) => { const conversation = req.body?.conversation as Conversation | undefined; if (!conversation) return res.status(400).json({ error: 'Missing conversation' }); const lastMsg = conversation.messages[conversation.messages.length - 1]; const message = lastMsg?.text || ''; const context = conversation.messages .slice(-10) .map((m) => { const sender = conversation.participants.find((p) => p.id === m.from)?.name || m.from; const content = m.text || `[${m.type}]`; return `${sender}: ${content}`; }) .join('\n'); if (!genAI) { return res.json({ reply: systemPrompt }); } try { const contents = `${systemPrompt}\nConversation:\n${context}\n`; const result = await iniciarProcesoCognitivo({}) const reply = (result.text || '').trim(); res.json({ reply }); } catch (err: any) { console.error('Gemini error', err.message); res.status(500).json({ error: 'Failed to generate reply' }); } }); app.get('/', (req, res) => { res.send(`

Conversation Layer Agent

This service answers questions about the repository.

Send a POST request to / with a JSON body containing {"conversation": {...}}

Example: {"conversation": {"chatId": "123@c.us", "title": "Chat", "isGroup": false, "unreadCount": 0, "participants": [{"id": "123@c.us", "name": "Alice", "isMe": false}], "messages": [{"id": "m1", "from": "123@c.us", "to": "me@c.us", "ts": 0, "type": "chat", "text": "hello", "meta": {"ack":0,"hasReaction":false,"isQuoted":false}}]}}

It will respond with a JSON object containing {"reply": "the answer"}

Repository info: ${systemPrompt}

`); } ); app.listen(PORT, () => { console.log(`conversation-layer-agent listening on ${PORT}`); });