# 任务 15:AI Agent 详细实现(第三部分) ## 一、前端组件实现 ### 1.1 消息项组件 ```vue ``` --- ### 1.2 工具调用展示组件 ```vue ``` --- ### 1.3 对话列表组件 ```vue ``` --- ## 二、数据库迁移 ### 2.1 Prisma Schema 更新 ```prisma // backend/prisma/schema.prisma // ... 其他模型 ... model Conversation { id Int @id @default(autoincrement()) userId String? topicId Int? reportId Int? articleId Int? title String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt messages ConversationMessage[] topic Topic? @relation(fields: [topicId], references: [id]) report Report? @relation(fields: [reportId], references: [id]) article Article? @relation(fields: [articleId], references: [id]) @@index([userId]) @@index([topicId]) @@index([reportId]) @@index([articleId]) @@map("conversations") } model ConversationMessage { id Int @id @default(autoincrement()) conversationId Int role String content String @db.Text toolCalls Json? createdAt DateTime @default(now()) conversation Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade) @@index([conversationId]) @@map("conversation_messages") } // 更新其他模型,添加关联 model Topic { // ... 现有字段 ... conversations Conversation[] } model Report { // ... 现有字段 ... conversations Conversation[] } model Article { // ... 现有字段 ... conversations Conversation[] } ``` ### 2.2 执行迁移 ```bash # 生成迁移文件 npx prisma migrate dev --name add_agent_tables # 生成 Prisma Client npx prisma generate ``` --- ## 三、路由配置 ### 3.1 后端路由注册 ```typescript // backend/src/app.ts import { agentRoutes } from './routes/agent.routes'; // ... 其他代码 ... // 注册 Agent 路由 fastify.register(agentRoutes, { prefix: '/api/agent' }); ``` ### 3.2 前端路由配置 ```typescript // frontend/src/router/index.ts import { createRouter, createWebHistory } from 'vue-router'; const routes = [ // ... 其他路由 ... { path: '/agent', name: 'Agent', children: [ { path: 'conversations', name: 'ConversationList', component: () => import('@/views/agent/ConversationList.vue'), meta: { title: '对话列表' }, }, { path: 'chat', name: 'AgentChat', component: () => import('@/views/agent/AgentChat.vue'), meta: { title: 'AI 对话' }, }, ], }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` --- ## 四、依赖安装 ### 4.1 后端依赖 ```bash cd backend # 安装 Vercel AI SDK npm install ai @ai-sdk/openai zod # 如果使用其他 LLM 提供商 npm install @ai-sdk/anthropic # Anthropic Claude npm install @ai-sdk/google # Google Gemini ``` ### 4.2 前端依赖 ```bash cd frontend # 安装 Markdown 渲染 npm install markdown-it @types/markdown-it # 安装代码高亮 npm install highlight.js # 安装 SSE 解析(如果需要) npm install eventsource-parser ``` --- ## 五、配置文件 ### 5.1 环境变量配置 ```env # backend/.env # LLM 默认配置 DEFAULT_LLM_PROVIDER=openai DEFAULT_LLM_MODEL=gpt-4 DEFAULT_LLM_API_KEY=sk-xxx DEFAULT_LLM_BASE_URL=https://api.openai.com/v1 # Agent 配置 AGENT_MAX_STEPS=5 AGENT_TIMEOUT=30000 AGENT_MAX_CONTEXT_LENGTH=8000 # 搜索 API(可选) GOOGLE_SEARCH_API_KEY=xxx GOOGLE_SEARCH_ENGINE_ID=xxx ``` ### 5.2 前端环境变量 ```env # frontend/.env VITE_API_BASE_URL=http://localhost:3000 VITE_AGENT_STREAM_TIMEOUT=60000 ``` --- ## 六、测试示例 ### 6.1 后端单元测试 ```typescript // backend/src/services/agent/__tests__/agent.service.test.ts import { describe, it, expect, beforeEach } from 'vitest'; import { AgentService } from '../agent.service'; describe('AgentService', () => { let agentService: AgentService; beforeEach(() => { agentService = new AgentService(); }); it('should create conversation', async () => { // TODO: 实现测试 }); it('should stream chat response', async () => { // TODO: 实现测试 }); it('should call tools correctly', async () => { // TODO: 实现测试 }); }); ``` ### 6.2 API 测试 ```bash # 测试流式对话 curl -X POST http://localhost:3000/api/agent/chat \ -H "Content-Type: application/json" \ -d '{ "topicId": 1, "message": "帮我搜索最新的 AI 新闻", "llmConfigId": 1, "userId": "test-user" }' ``` --- ## 七、下一步 继续创建部署和优化文档...