feat(persona-agent): 光湖人格 Agent v0.1.0 — Grok Build 架构拆解+Git驱动人格体

核心架构(拆解自 xAI Grok Build 开源 Rust crate):
- PersonaAgent: Agent = Definition + ToolBridge + PromptContext(映射自 xai-grok-agent)
- 7个内置工具: read/create/update/delete/search/list/history(直接操作 Git 引擎)
- LLM 调用层: 可插拔 OpenAI-compatible API(支持 tool calling)
- 离线模式: 无 API Key 时降级为本地响应

API 端点:
- POST /api/agent/chat — Agent 对话(支持工具调用链)
- GET  /api/agent/status — Agent 状态
- GET  /api/agent/conversation — 对话历史
- POST /api/agent/clear — 清空对话

前端:
- AgentChat 组件: 对话气泡+工具调用展示+欢迎建议
- 三栏布局: 文档树 | 编辑器 | Agent 面板(可收起)

设计原则:
- Agent 是活的人格体,不是孤立的功能模块
- Agent 直接操作 Git(知识库引擎 = 人格体记忆空间)
- 所有 Git 依赖模块由人格体驱动
- 不搬 Rust 代码,拆设计模式用 TypeScript 重新实现
This commit is contained in:
冰朔 2026-08-08 07:38:09 +08:00
commit dcf8f7d7e9
5 changed files with 1233 additions and 0 deletions

View file

@ -8,6 +8,7 @@
import express from 'express';
import cors from 'cors';
import { GitEngine } from './git-engine.js';
import { PersonaAgent, createDefaultPersona } from './persona-agent.js';
import path from 'path';
import { fileURLToPath } from 'url';
@ -176,12 +177,78 @@ app.get('/api/search', async (req, res) => {
}
});
// ─── Agent人格体 ───
let personaAgent: PersonaAgent | null = null;
function getAgent(): PersonaAgent {
if (!personaAgent) {
personaAgent = createDefaultPersona(engine);
console.log('人格体已创建:', personaAgent.getDefinition().name);
}
return personaAgent;
}
// Agent 状态
app.get('/api/agent/status', (_req, res) => {
const agent = getAgent();
const def = agent.getDefinition();
res.json({
ok: true,
persona: {
id: def.id,
name: def.name,
role: def.role,
model: def.model,
permissionMode: def.permissionMode,
},
conversationLength: agent.getConversation().length,
engine: 'git',
repo: REPO_PATH,
});
});
// Agent 对话
app.post('/api/agent/chat', async (req, res) => {
try {
const { message } = req.body;
if (!message || typeof message !== 'string') {
return res.status(400).json({ ok: false, error: 'message 必填且为字符串' });
}
const agent = getAgent();
const reply = await agent.chat(message);
res.json({
ok: true,
reply,
conversationLength: agent.getConversation().length,
});
} catch (err: any) {
res.status(500).json({ ok: false, error: err.message });
}
});
// Agent 对话历史
app.get('/api/agent/conversation', (_req, res) => {
const agent = getAgent();
res.json({ ok: true, messages: agent.getConversation() });
});
// 清空对话
app.post('/api/agent/clear', (_req, res) => {
const agent = getAgent();
agent.clearConversation();
res.json({ ok: true, message: '对话已清空' });
});
// ─── 启动 ───
async function start() {
await engine.init();
// 初始化人格体
getAgent();
app.listen(PORT, () => {
console.log(`光湖知识库 API 已启动: http://localhost:${PORT}`);
console.log(`人格体已激活: ${personaAgent?.getDefinition().name}`);
console.log(`仓库路径: ${REPO_PATH}`);
});
}