refactor: 聊天室 Agent 重构 — ToolLoopAgent + 论坛式布局
后端: - 删除 agent-stream.ts,新建 alfred-agent.ts (ToolLoopAgent 工厂) - 新建 get-current-time.ts 工具 (zod schema) - 重构 send.ts: createAgentUIStreamResponse + onFinish 可靠持久化 前端: - 删除 MessageBubble.tsx,新建 ToolCallCard.tsx (四态) - 重构 ChatPanel.tsx: 论坛式 Card 布局 + PartRenderer 分派 - 移除 @ant-design/x 依赖,改用 antd 组件 + streamdown 依赖: + zod + streamdown - @ant-design/x - @ant-design/x-markdown 测试: 306 pass, typecheck/lint 0 errors
This commit is contained in:
@@ -87,7 +87,7 @@ middleware.ts 提供 API 参数校验函数:
|
||||
|
||||
### Agent 流式调用
|
||||
|
||||
`src/server/ai/agent-stream.ts` 提供 `agentStream(options)` 函数,封装 Vercel AI SDK `streamText` 调用。接收数据库实例、消息数组和模型 DB ID,从 DB 查询模型与供应商信息后构建 Provider Registry,使用 `:` 作为 provider 和 modelId 的分隔符。默认使用 `stepCountIs(1)` 限制单步调用。返回 `StreamTextResult`,路由层通过 `result.toUIMessageStreamResponse()` 转为 SSE 响应。
|
||||
`src/server/ai/agents/alfred-agent.ts` 提供 `createAlfredAgent(model)` 工厂函数,返回 AI SDK `ToolLoopAgent` 实例。默认使用 `stepCountIs(20)` 限制最大迭代次数,配置 `getCurrentTime` 工具供 AI 调用。路由层通过 `createAgentUIStreamResponse()` 转为 SSE 响应,流结束通过 `onFinish` 回调可靠持久化 AI 回复(含完整 `parts`)。
|
||||
|
||||
### 供应商连通性测试
|
||||
|
||||
@@ -119,7 +119,7 @@ middleware.ts 提供 API 参数校验函数:
|
||||
| GET | `/api/projects/:id/conversations/:cid/messages` | 获取会话消息列表 |
|
||||
| POST | `/api/projects/:id/chat` | 发送消息并流式回复 |
|
||||
|
||||
聊天路由处理器位于 `src/server/routes/chat/`,遵循统一的 handler 模式。`send.ts` 处理发送消息:验证会话归属后保存用户消息到 DB,调用 `agentStream` 获取流式响应,返回 SSE UI 消息流,流结束后后台保存 AI 回复到 DB。
|
||||
聊天路由处理器位于 `src/server/routes/chat/`,遵循统一的 handler 模式。`send.ts` 处理发送消息:验证会话归属后只保存最后一条用户消息到 DB,通过 `createAlfredAgent` 创建 agent,调用 `createAgentUIStreamResponse` 返回 SSE UI 消息流,流结束通过 `onFinish` 回调保存 AI 回复到 DB。
|
||||
|
||||
## 类型规范
|
||||
|
||||
|
||||
Reference in New Issue
Block a user