feat(littleblue): agent seed + persona + chat route
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
65
lib/api/routes/littleblue.js
Normal file
65
lib/api/routes/littleblue.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { validate } from '../validate.js';
|
||||
import { asyncWrap } from '../errors.js';
|
||||
import * as conversations from '../../db/repos/conversations.js';
|
||||
import * as messages from '../../db/repos/messages.js';
|
||||
import * as agents from '../../db/repos/agents.js';
|
||||
import { runAgentTurn } from '../../ai/agent/run_turn.js';
|
||||
import { personaFor } from '../../ai/personas/index.js';
|
||||
|
||||
const SLUG = 'little-blue';
|
||||
const BLUE_TOOLS = ['mcp__void__search', 'mcp__void__list_actions', 'mcp__void__propose_action'];
|
||||
|
||||
async function resolve() {
|
||||
const agent = await agents.getBySlug(SLUG);
|
||||
const convo = await conversations.findOrCreateGlobal(agent.id, { kind: 'user', id: null });
|
||||
return { agent, convo };
|
||||
}
|
||||
|
||||
export const router = Router();
|
||||
|
||||
router.get('/', asyncWrap(async (_req, res) => {
|
||||
const { agent, convo } = await resolve();
|
||||
res.json({
|
||||
conversation_id: convo.id,
|
||||
agent: { id: agent.id, slug: agent.slug, name: agent.name },
|
||||
messages: await messages.listByConversation(convo.id)
|
||||
});
|
||||
}));
|
||||
|
||||
router.post('/turn', validate({ body: z.object({ text: z.string().min(1) }) }), asyncWrap(async (req, res) => {
|
||||
const { agent, convo } = await resolve();
|
||||
const { text } = req.body;
|
||||
const resume = (await messages.listByConversation(convo.id)).length > 0;
|
||||
await messages.append(convo.id, { role: 'user', body: text });
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' });
|
||||
const send = (ev, d) => res.write(`event: ${ev}\ndata: ${JSON.stringify(d)}\n\n`);
|
||||
const claudeExe = req.app.locals.claudeExe || process.env.CLAUDE_EXE || 'claude';
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = await runAgentTurn({
|
||||
agent, persona: personaFor(agent.slug), registryName: 'blue', toolNames: BLUE_TOOLS,
|
||||
spaceId: null, view: null, sessionId: convo.id, resume, userText: text, claudeExe,
|
||||
home: process.env.VOID_CLAUDE_HOME || undefined,
|
||||
extraEnv: {
|
||||
VOID_API_URL: process.env.VOID_API_URL || 'http://127.0.0.1:3000',
|
||||
VOID_AGENT_TOKEN: process.env.LITTLEBLUE_TOKEN || ''
|
||||
},
|
||||
onEvent: (e) => {
|
||||
if (e.type === 'delta') send('delta', { type: 'delta', text: e.text });
|
||||
else if (e.type === 'tool') send('tool', { type: 'tool', tool: e.tool, status: e.status });
|
||||
else if (e.type === 'error') send('error', { type: 'error', message: e.message });
|
||||
}
|
||||
});
|
||||
} catch (e) { send('error', { message: String(e?.message || e) }); return res.end(); }
|
||||
|
||||
const a = await messages.append(convo.id, {
|
||||
role: 'assistant', body: result.text, agent_id: agent.id,
|
||||
metadata: { tool_trace: result.toolTrace, usage: result.usage }
|
||||
});
|
||||
send('done', { assistant_message_id: a.id, usage: result.usage });
|
||||
res.end();
|
||||
}));
|
||||
Reference in New Issue
Block a user