Rename repo-agent to conversation-layer-agent
This commit is contained in:
43
conversation-layer-agent/src/index.ts
Normal file
43
conversation-layer-agent/src/index.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import express from 'express';
|
||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const PORT = Number(process.env.PORT) || 8001;
|
||||
const API_KEY = process.env.GEMINI_API_KEY || '';
|
||||
|
||||
const genAI = API_KEY ? new GoogleGenerativeAI(API_KEY) : null;
|
||||
const model = genAI ? genAI.getGenerativeModel({ model: 'gemini-pro' }) : null;
|
||||
|
||||
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.
|
||||
- conversation-layer-agent: Answers questions about the repository.
|
||||
Run all services with docker-compose. Configure handler mappings in whatsapp-router/src/chatHandlers.ts.`;
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
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) {
|
||||
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();
|
||||
res.json({ reply });
|
||||
} catch (err: any) {
|
||||
console.error('Gemini error', err.message);
|
||||
res.status(500).json({ error: 'Failed to generate reply' });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`conversation-layer-agent listening on ${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user