// kira-be/src/ai/agents/index.ts
async function runAgent(userId: string, threadId: string, message: string, groupId?: string) {
// ① 一次调用拿到所有记忆上下文
const memory = await fetch('http://kira-memory.internal:8080/inject', {
method: 'POST',
body: JSON.stringify({
entity_id: userId,
entity_type: 'user',
group_id: groupId,
query: message,
}),
}).then(r => r.json());
// ② 组装 system prompt
const systemPrompt = buildSystemPrompt({
baseInstructions,
userProfile: memory.profile, // 新皮层:永远在
groupProfile: memory.group_profile, // 群上下文
relevantMemories: memory.memories, // 海马体:proactive 检索
});
// ③ Agent 执行(还有 memory tools 可以主动调用)
const tools = [...existingTools, memoryAddTool, memorySearchTool];
const result = await callLLM({ systemPrompt, tools, message });
// ④ 异步触发记忆提取(不阻塞响应)
await nats.publish('tasks.memory.extract', {
entity_id: userId,
entity_type: 'user',
group_id: groupId, // 群上下文单独传递
thread_id: threadId,
messages: result.messages,
tool_calls: result.toolCalls,
});
return result;
}