feat: 工作台聊天室功能
This commit is contained in:
@@ -20,9 +20,12 @@ src/
|
||||
projects.ts 项目数据访问函数
|
||||
providers.ts 供应商数据访问函数
|
||||
models.ts 模型数据访问函数
|
||||
conversations.ts 会话数据访问函数
|
||||
index.ts 数据库模块导出
|
||||
ai/ AI 服务层
|
||||
types.ts AI 配置类型定义
|
||||
registry.ts AI Provider Registry 构建与连接测试
|
||||
agent-stream.ts AI Agent 流式调用
|
||||
dev.ts 开发模式启动入口
|
||||
main.ts 生产模式启动入口
|
||||
server.ts HTTP server 启动工厂(Bun.serve routes 声明式路由)
|
||||
@@ -34,6 +37,7 @@ src/
|
||||
routes/ API 路由处理器
|
||||
providers/ 供应商 CRUD 路由
|
||||
models/ 模型 CRUD 路由
|
||||
chat/ 聊天会话与消息路由
|
||||
shared/
|
||||
api.ts 前后端共享 TypeScript 类型定义
|
||||
app.ts 应用全局常量(name、title、subtitle、description)
|
||||
@@ -101,7 +105,7 @@ Request
|
||||
| `src/server/server.ts` | Bun HTTP server 和 routes 注册 |
|
||||
| `src/server/routes/` | API handler,按端点拆分 |
|
||||
| `src/server/db/` | SQLite 连接、schema、migration 和 data access |
|
||||
| `src/server/ai/` | AI Provider Registry 构建与连接测试 |
|
||||
| `src/server/ai/` | AI Provider Registry 构建与 Agent 流式调用 |
|
||||
| `src/server/config/` | 配置解析模块(types、variables、schema) |
|
||||
| `src/web/` | React 前端 |
|
||||
| `src/shared/api.ts` | 前后端共享 API 类型 |
|
||||
|
||||
@@ -62,7 +62,7 @@ middleware.ts 提供 API 参数校验函数:
|
||||
|
||||
### 数据访问
|
||||
|
||||
`src/server/db/projects.ts` 提供项目数据访问函数,`src/server/db/providers.ts` 提供供应商数据访问函数,`src/server/db/models.ts` 提供模型数据访问函数。输入输出使用 `src/shared/api.ts` 的类型。函数内部使用 Drizzle query builder 包装 `bun:sqlite` Database。
|
||||
`src/server/db/projects.ts` 提供项目数据访问函数,`src/server/db/providers.ts` 提供供应商数据访问函数,`src/server/db/models.ts` 提供模型数据访问函数,`src/server/db/conversations.ts` 提供会话和消息数据访问函数。输入输出使用 `src/shared/api.ts` 的类型。函数内部使用 Drizzle query builder 包装 `bun:sqlite` Database。
|
||||
|
||||
## AI 服务层
|
||||
|
||||
@@ -85,6 +85,10 @@ middleware.ts 提供 API 参数校验函数:
|
||||
|
||||
每次 AI 调用时从 DB 查询 providers,构建 registry 后通过 `registry.languageModel('providerId:modelId')` 获取模型实例。不使用缓存层。模型是否存在以及业务能力标签由调用方基于 models 表先行校验,registry 只负责将 providerId/modelId 映射到 AI SDK 模型实例。
|
||||
|
||||
### 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 响应。
|
||||
|
||||
### 供应商连通性测试
|
||||
|
||||
供应商连通性测试返回 `{ providerTestResponse: { ok, message } }`,前端根据 `ok` 展示成功或失败提示。
|
||||
@@ -102,6 +106,21 @@ middleware.ts 提供 API 参数校验函数:
|
||||
| `anthropic` | `createAnthropic({ apiKey, baseURL })` |
|
||||
| `openai-compatible` | `createOpenAICompatible({ name, apiKey, baseURL })` |
|
||||
|
||||
## 聊天 API
|
||||
|
||||
聊天 API 按项目维度组织会话和消息:
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
| ------ | ----------------------------------------------- | ------------------ |
|
||||
| GET | `/api/projects/:id/conversations` | 列出项目下所有会话 |
|
||||
| POST | `/api/projects/:id/conversations` | 创建新会话 |
|
||||
| GET | `/api/projects/:id/conversations/:cid` | 获取会话详情 |
|
||||
| DELETE | `/api/projects/:id/conversations/:cid` | 删除会话及其消息 |
|
||||
| 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/shared/api.ts 为唯一源头
|
||||
|
||||
@@ -6,14 +6,15 @@
|
||||
|
||||
## 技术栈
|
||||
|
||||
| 层面 | 技术 | 用途 |
|
||||
| ------ | --------------------------------------------------- | ------------------------ |
|
||||
| 框架 | React 19 | UI 组件开发 |
|
||||
| 构建 | Vite(开发)+ Bun compile(生产) | 开发服务 HMR 与生产构建 |
|
||||
| 语言 | TypeScript | 类型安全 |
|
||||
| UI 库 | Ant Design (antd) + @ant-design/icons | UI 组件与图标 |
|
||||
| 数据层 | TanStack Query (React Query) + React Query Devtools | 服务端状态管理与自动刷新 |
|
||||
| 路由 | React Router v7 (Declarative mode) | SPA 路由与页面导航 |
|
||||
| 层面 | 技术 | 用途 |
|
||||
| ------ | ----------------------------------------------------- | ------------------------ |
|
||||
| 框架 | React 19 | UI 组件开发 |
|
||||
| 构建 | Vite(开发)+ Bun compile(生产) | 开发服务 HMR 与生产构建 |
|
||||
| 语言 | TypeScript | 类型安全 |
|
||||
| UI 库 | Ant Design (antd) + @ant-design/icons + @ant-design/x | UI 组件、图标与聊天 UI |
|
||||
| 数据层 | TanStack Query (React Query) + React Query Devtools | 服务端状态管理与自动刷新 |
|
||||
| AI 层 | Vercel AI SDK (`@ai-sdk/react`) | 聊天状态管理与流式通信 |
|
||||
| 路由 | React Router v7 (Declarative mode) | SPA 路由与页面导航 |
|
||||
|
||||
不引入额外状态管理库。TanStack Query 承担服务端状态,组件内状态使用 useState。
|
||||
|
||||
@@ -123,7 +124,7 @@ token 和 CSS 变量规则:
|
||||
前端提供两个入口外壳,共享通用 Console Shell 组件:
|
||||
|
||||
- **Admin(管理台)**:`src/web/consoles/admin/AdminConsoleLayout.tsx`,菜单配置在 `menu.tsx`,路由 `/`、`/projects`、`/models`。
|
||||
- **Workbench(工作台)**:`src/web/consoles/workbench/WorkbenchProjectGate.tsx` → `WorkbenchConsoleLayout.tsx`,菜单配置和路由构造在 `routes.ts`,路由 `/workbench/:projectId`。
|
||||
- **Workbench(工作台)**:`src/web/consoles/workbench/WorkbenchProjectGate.tsx` → `WorkbenchConsoleLayout.tsx`,菜单配置和路由构造在 `routes.ts`,路由 `/workbench/:projectId`。默认菜单为"聊天室",使用 `ChatPage` 作为主页面。
|
||||
|
||||
通用 Console Shell(`src/web/components/ConsoleShell/ConsoleShell.tsx`)包含 Layout、Header、Sider、Content、主题切换、版本展示和侧边栏折叠状态,由 Admin 和 Workbench 复用。Header 显示品牌名、版本号和控制台标题(Admin 显示"管理台",Workbench 显示"工作台 · 项目名")。
|
||||
|
||||
@@ -135,6 +136,15 @@ Workbench 项目上下文通过 `ProjectContext` 提供,在 `WorkbenchProjectG
|
||||
|
||||
供应商表单必须支持未保存配置的连通性测试,新建供应商时 type 默认 `openai-compatible`,baseURL 不设默认值。连通性测试返回 `ok: false` 时应展示失败反馈,不得使用成功提示样式;`/models` 不支持或响应格式不兼容属于可忽略提醒,不得阻止保存。
|
||||
|
||||
### 聊天页面
|
||||
|
||||
Workbench 聊天页面位于 `src/web/consoles/workbench/pages/ChatPage.tsx`,组合 `ChatSidebar` 和 `ChatPanel` 两个子组件。
|
||||
|
||||
- **ChatSidebar**:使用 TanStack Query 管理会话列表,提供创建和删除会话操作。
|
||||
- **ChatPanel**:使用 Vercel AI SDK `useChat` hook(来自 `@ai-sdk/react`)管理聊天状态,通过 `DefaultChatTransport`(来自 `ai` 包)与后端 SSE 端点通信。使用 `@ant-design/x` 的 `Bubble.List` 和 `Sender` 组件渲染消息列表和输入框。`useChat` 返回的 `UIMessage` 使用 `parts` 数组存储内容,不包含 `content` 属性;需从 `parts` 中提取 `type: "text"` 的文本内容用于 Bubble 展示。
|
||||
- **MessageBubble**:简单的纯文本消息气泡组件(MVP)。
|
||||
- **use-conversations hook**:位于 `src/web/hooks/use-conversations.ts`,封装会话 CRUD 的 fetch 调用。
|
||||
|
||||
- 生产入口必须启用 `ErrorBoundary`,运行时渲染异常使用 antd `Result status="500"` 或等价组件展示。
|
||||
- `ReactQueryDevtools` 仅在 `import.meta.env.DEV` 条件下渲染,不进入生产渲染路径。
|
||||
- 主题切换统一通过 `ConfigProvider` 的 antd theme algorithm 控制,不使用硬编码主题色。
|
||||
|
||||
@@ -32,19 +32,19 @@ bun run dev config.yaml
|
||||
|
||||
## 功能介绍
|
||||
|
||||
| 功能 | 路径 | 说明 |
|
||||
| ---------- | ----------------------- | ---------------------------------------- |
|
||||
| 总览 | `/` | Admin 管理台总览,展示运行时元信息 |
|
||||
| 项目管理 | `/projects` | 创建、编辑、归档、恢复和永久删除项目 |
|
||||
| 模型管理 | `/models` | 配置 AI 供应商和模型,供后续 AI 功能使用 |
|
||||
| 工作台总览 | `/workbench/:projectId` | Workbench 工作台总览,按项目维度查看信息 |
|
||||
| 用户管理 | `/users` | 页面建设中 |
|
||||
| 系统设置 | `/settings` | 页面建设中 |
|
||||
| 功能 | 路径 | 说明 |
|
||||
| -------- | ----------------------- | ---------------------------------------- |
|
||||
| 总览 | `/` | Admin 管理台总览,展示运行时元信息 |
|
||||
| 项目管理 | `/projects` | 创建、编辑、归档、恢复和永久删除项目 |
|
||||
| 模型管理 | `/models` | 配置 AI 供应商和模型,供后续 AI 功能使用 |
|
||||
| 聊天室 | `/workbench/:projectId` | Workbench 工作台聊天室,与 AI 对话 |
|
||||
| 用户管理 | `/users` | 页面建设中 |
|
||||
| 系统设置 | `/settings` | 页面建设中 |
|
||||
|
||||
平台提供两个入口:
|
||||
|
||||
- **Admin(管理台)**:全局管理视角,包含总览和项目管理。默认入口,访问 `/` 即可进入。
|
||||
- **Workbench(工作台)**:项目维度视角,通过 `/workbench/:projectId` 进入指定项目的工作台。URL 可保存为浏览器书签,下次直接进入。仅 active 状态的项目可进入工作台,archived 项目不可访问。
|
||||
- **Workbench(工作台)**:项目维度视角,通过 `/workbench/:projectId` 进入指定项目的工作台。默认进入聊天室页面,可与已配置的 AI 模型进行对话。URL 可保存为浏览器书签,下次直接进入。仅 active 状态的项目可进入工作台,archived 项目不可访问。
|
||||
|
||||
从项目管理页面的 active 项目行可点击"工作台"跳转到对应项目的工作台。
|
||||
|
||||
@@ -55,4 +55,10 @@ bun run dev config.yaml
|
||||
- **供应商**:新增、编辑、删除 OpenAI、Anthropic 或 OpenAI 兼容供应商。新建供应商时类型默认是 `openai-compatible`,baseURL 和 API Key 由用户填写。
|
||||
- **模型**:为供应商新增模型,填写模型显示名称、实际调用用的 modelId、能力标签,以及可选的上下文长度和最大输出 token。
|
||||
|
||||
供应商表单提供“测试连接”操作:系统先测试 Base URL 是否可达,再尝试请求 `/models` 验证 API Key 和模型列表接口。若服务不支持 `/models`,页面会提示接口可达但可能不支持模型列表;该结果只作为提醒,不会阻止保存供应商或模型。删除供应商前必须先删除或迁移其关联模型,否则系统会拒绝删除以避免误删模型配置。
|
||||
供应商表单提供"测试连接"操作:系统先测试 Base URL 是否可达,再尝试请求 `/models` 验证 API Key 和模型列表接口。若服务不支持 `/models`,页面会提示接口可达但可能不支持模型列表;该结果只作为提醒,不会阻止保存供应商或模型。删除供应商前必须先删除或迁移其关联模型,否则系统会拒绝删除以避免误删模型配置。
|
||||
|
||||
## 聊天室
|
||||
|
||||
在 Workbench 工作台中,默认进入聊天室页面。左侧为会话列表,可新建和删除会话;右侧为聊天面板,输入消息后 AI 将流式回复。
|
||||
|
||||
使用聊天功能前,需先在 Admin 管理台的模型管理页面配置至少一个 AI 供应商和模型。新建会话时系统会自动选择第一个可用模型。
|
||||
|
||||
24
drizzle/0002_orange_black_knight.sql
Normal file
24
drizzle/0002_orange_black_knight.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
CREATE TABLE `conversations` (
|
||||
`created_at` text NOT NULL,
|
||||
`id` text PRIMARY KEY NOT NULL,
|
||||
`model_id` text NOT NULL,
|
||||
`project_id` text NOT NULL,
|
||||
`title` text DEFAULT '新会话' NOT NULL,
|
||||
`updated_at` text NOT NULL,
|
||||
FOREIGN KEY (`model_id`) REFERENCES `models`(`id`) ON UPDATE no action ON DELETE no action,
|
||||
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX `conversations_project_id_idx` ON `conversations` (`project_id`);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `messages` (
|
||||
`content` text DEFAULT '' NOT NULL,
|
||||
`conversation_id` text NOT NULL,
|
||||
`created_at` text NOT NULL,
|
||||
`id` text PRIMARY KEY NOT NULL,
|
||||
`parts` text,
|
||||
`role` text NOT NULL,
|
||||
FOREIGN KEY (`conversation_id`) REFERENCES `conversations`(`id`) ON UPDATE no action ON DELETE cascade
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX `messages_conversation_id_idx` ON `messages` (`conversation_id`);
|
||||
423
drizzle/meta/0002_snapshot.json
Normal file
423
drizzle/meta/0002_snapshot.json
Normal file
@@ -0,0 +1,423 @@
|
||||
{
|
||||
"version": "6",
|
||||
"dialect": "sqlite",
|
||||
"id": "da8963db-526e-46a1-a453-4027d5541db9",
|
||||
"prevId": "3ce8499a-1b73-4fbd-a2ec-0f872646136a",
|
||||
"tables": {
|
||||
"conversations": {
|
||||
"name": "conversations",
|
||||
"columns": {
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"model_id": {
|
||||
"name": "model_id",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"project_id": {
|
||||
"name": "project_id",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"title": {
|
||||
"name": "title",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "'新会话'"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"conversations_project_id_idx": {
|
||||
"name": "conversations_project_id_idx",
|
||||
"columns": ["project_id"],
|
||||
"isUnique": false
|
||||
}
|
||||
},
|
||||
"foreignKeys": {
|
||||
"conversations_model_id_models_id_fk": {
|
||||
"name": "conversations_model_id_models_id_fk",
|
||||
"tableFrom": "conversations",
|
||||
"tableTo": "models",
|
||||
"columnsFrom": ["model_id"],
|
||||
"columnsTo": ["id"],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
},
|
||||
"conversations_project_id_projects_id_fk": {
|
||||
"name": "conversations_project_id_projects_id_fk",
|
||||
"tableFrom": "conversations",
|
||||
"tableTo": "projects",
|
||||
"columnsFrom": ["project_id"],
|
||||
"columnsTo": ["id"],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
},
|
||||
"messages": {
|
||||
"name": "messages",
|
||||
"columns": {
|
||||
"content": {
|
||||
"name": "content",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "''"
|
||||
},
|
||||
"conversation_id": {
|
||||
"name": "conversation_id",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"parts": {
|
||||
"name": "parts",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"role": {
|
||||
"name": "role",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"messages_conversation_id_idx": {
|
||||
"name": "messages_conversation_id_idx",
|
||||
"columns": ["conversation_id"],
|
||||
"isUnique": false
|
||||
}
|
||||
},
|
||||
"foreignKeys": {
|
||||
"messages_conversation_id_conversations_id_fk": {
|
||||
"name": "messages_conversation_id_conversations_id_fk",
|
||||
"tableFrom": "messages",
|
||||
"tableTo": "conversations",
|
||||
"columnsFrom": ["conversation_id"],
|
||||
"columnsTo": ["id"],
|
||||
"onDelete": "cascade",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
},
|
||||
"models": {
|
||||
"name": "models",
|
||||
"columns": {
|
||||
"capabilities": {
|
||||
"name": "capabilities",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"context_length": {
|
||||
"name": "context_length",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"max_output_tokens": {
|
||||
"name": "max_output_tokens",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"model_id": {
|
||||
"name": "model_id",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"provider_id": {
|
||||
"name": "provider_id",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"models_provider_id_model_id_unique": {
|
||||
"name": "models_provider_id_model_id_unique",
|
||||
"columns": ["provider_id", "model_id"],
|
||||
"isUnique": true
|
||||
},
|
||||
"models_provider_id_idx": {
|
||||
"name": "models_provider_id_idx",
|
||||
"columns": ["provider_id"],
|
||||
"isUnique": false
|
||||
}
|
||||
},
|
||||
"foreignKeys": {
|
||||
"models_provider_id_providers_id_fk": {
|
||||
"name": "models_provider_id_providers_id_fk",
|
||||
"tableFrom": "models",
|
||||
"tableTo": "providers",
|
||||
"columnsFrom": ["provider_id"],
|
||||
"columnsTo": ["id"],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
},
|
||||
"projects": {
|
||||
"name": "projects",
|
||||
"columns": {
|
||||
"archived_at": {
|
||||
"name": "archived_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"description": {
|
||||
"name": "description",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "''"
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"status": {
|
||||
"name": "status",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "'active'"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"projects_name_unique": {
|
||||
"name": "projects_name_unique",
|
||||
"columns": ["name"],
|
||||
"isUnique": true
|
||||
}
|
||||
},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
},
|
||||
"providers": {
|
||||
"name": "providers",
|
||||
"columns": {
|
||||
"api_key": {
|
||||
"name": "api_key",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"base_url": {
|
||||
"name": "base_url",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"type": {
|
||||
"name": "type",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "'openai-compatible'"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"providers_name_unique": {
|
||||
"name": "providers_name_unique",
|
||||
"columns": ["name"],
|
||||
"isUnique": true
|
||||
}
|
||||
},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
},
|
||||
"schema_migrations": {
|
||||
"name": "schema_migrations",
|
||||
"columns": {
|
||||
"applied_at": {
|
||||
"name": "applied_at",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"checksum": {
|
||||
"name": "checksum",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"checkConstraints": {}
|
||||
}
|
||||
},
|
||||
"views": {},
|
||||
"enums": {},
|
||||
"_meta": {
|
||||
"schemas": {},
|
||||
"tables": {},
|
||||
"columns": {}
|
||||
},
|
||||
"internal": {
|
||||
"indexes": {}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,13 @@
|
||||
"when": 1780018783514,
|
||||
"tag": "0001_wooden_rocket_raccoon",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "6",
|
||||
"when": 1780162361636,
|
||||
"tag": "0002_orange_black_knight",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -56,7 +56,9 @@
|
||||
"@ai-sdk/anthropic": "^3.0.81",
|
||||
"@ai-sdk/openai": "^3.0.66",
|
||||
"@ai-sdk/openai-compatible": "^2.0.48",
|
||||
"@ai-sdk/react": "^3.0.195",
|
||||
"@ant-design/icons": "^6.2.3",
|
||||
"@ant-design/x": "^2.7.0",
|
||||
"@sinclair/typebox": "^0.34.49",
|
||||
"@tanstack/react-query": "^5.100.14",
|
||||
"ai": "^6.0.193",
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
{
|
||||
"version": 1,
|
||||
"skills": {
|
||||
"ai-sdk": {
|
||||
"source": "vercel/ai",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/use-ai-sdk/SKILL.md",
|
||||
"computedHash": "c99d2a95b3a5f8fad218f288503f9e724ba0f12bf4e8aaf2c792a9f2bc318ab6"
|
||||
},
|
||||
"ant-design": {
|
||||
"source": "ant-design/antd-skill",
|
||||
"sourceType": "github",
|
||||
|
||||
71
src/server/ai/agent-stream.ts
Normal file
71
src/server/ai/agent-stream.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import type { ModelMessage } from "ai";
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import { stepCountIs, streamText } from "ai";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
import { wrap } from "../db/connection";
|
||||
import { models, providers } from "../db/schema";
|
||||
import { buildProviderRegistry } from "./registry";
|
||||
|
||||
const SYSTEM_PROMPT = "你是 Alfred 的 AI 助手。你可以帮助用户回答问题、分析数据和完成各种任务。请用中文回复。";
|
||||
|
||||
export interface AgentStreamOptions {
|
||||
db: Database;
|
||||
messages: IncomingMessage[];
|
||||
modelDbId: string;
|
||||
}
|
||||
|
||||
export interface IncomingMessage {
|
||||
content?: string;
|
||||
id?: string;
|
||||
parts?: Array<{ text?: string; type: string }>;
|
||||
role?: string;
|
||||
}
|
||||
|
||||
export function agentStream(options: AgentStreamOptions) {
|
||||
const db = wrap(options.db);
|
||||
|
||||
const modelRow = db.select().from(models).where(eq(models.id, options.modelDbId)).get();
|
||||
if (!modelRow) throw new Error(`模型不存在: ${options.modelDbId}`);
|
||||
|
||||
const providerRow = db.select().from(providers).where(eq(providers.id, modelRow.providerId)).get();
|
||||
if (!providerRow) throw new Error(`供应商不存在: ${modelRow.providerId}`);
|
||||
|
||||
const registry = buildProviderRegistry(options.db);
|
||||
const model = registry.languageModel(`${providerRow.id}:${modelRow.modelId}`);
|
||||
|
||||
return streamText({
|
||||
messages: toCoreMessages(options.messages),
|
||||
model,
|
||||
stopWhen: stepCountIs(1),
|
||||
system: SYSTEM_PROMPT,
|
||||
});
|
||||
}
|
||||
|
||||
export function extractTextContent(msg: IncomingMessage): string {
|
||||
return (
|
||||
msg.content ??
|
||||
(Array.isArray(msg.parts)
|
||||
? msg.parts
|
||||
.filter((p) => p.type === "text" && typeof p.text === "string")
|
||||
.map((p) => p.text!)
|
||||
.join("")
|
||||
: "")
|
||||
);
|
||||
}
|
||||
|
||||
function toCoreMessages(messages: IncomingMessage[]): ModelMessage[] {
|
||||
return messages.map((msg) => {
|
||||
const content =
|
||||
msg.content ??
|
||||
(Array.isArray(msg.parts)
|
||||
? msg.parts
|
||||
.filter((p) => p.type === "text" && typeof p.text === "string")
|
||||
.map((p) => p.text!)
|
||||
.join("")
|
||||
: "");
|
||||
|
||||
return { content, role: msg.role as ModelMessage["role"] } as ModelMessage;
|
||||
});
|
||||
}
|
||||
178
src/server/db/conversations.ts
Normal file
178
src/server/db/conversations.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
import type { Conversation, Message } from "../../shared/api";
|
||||
|
||||
import { paginateQuery, wrap } from "./connection";
|
||||
import { conversations, messages, models } from "./schema";
|
||||
|
||||
export function createConversation(
|
||||
raw: Database,
|
||||
projectId: string,
|
||||
defaultModelId?: string,
|
||||
): { conversation: Conversation } | { error: string; status: number } {
|
||||
const db = wrap(raw);
|
||||
|
||||
let modelId = defaultModelId;
|
||||
if (!modelId) {
|
||||
const firstModel = db.select().from(models).limit(1).get();
|
||||
if (!firstModel) return { error: "没有可用的模型,请先配置模型", status: 400 };
|
||||
modelId = firstModel.id;
|
||||
} else {
|
||||
const model = db.select().from(models).where(eq(models.id, modelId)).get();
|
||||
if (!model) return { error: "模型不存在", status: 400 };
|
||||
}
|
||||
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
|
||||
db.insert(conversations)
|
||||
.values({
|
||||
createdAt: now,
|
||||
id,
|
||||
modelId,
|
||||
projectId,
|
||||
title: "新会话",
|
||||
updatedAt: now,
|
||||
})
|
||||
.run();
|
||||
|
||||
const row = db.select().from(conversations).where(eq(conversations.id, id)).get();
|
||||
return { conversation: toConversation(row!) };
|
||||
}
|
||||
|
||||
export function createMessage(
|
||||
raw: Database,
|
||||
data: {
|
||||
content: string;
|
||||
conversationId: string;
|
||||
parts?: string;
|
||||
role: "assistant" | "system" | "user";
|
||||
},
|
||||
): Message {
|
||||
const db = wrap(raw);
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
|
||||
db.insert(messages)
|
||||
.values({
|
||||
content: data.content,
|
||||
conversationId: data.conversationId,
|
||||
createdAt: now,
|
||||
id,
|
||||
parts: data.parts ?? null,
|
||||
role: data.role,
|
||||
})
|
||||
.run();
|
||||
|
||||
const row = db.select().from(messages).where(eq(messages.id, id)).get();
|
||||
return toMessage(row!);
|
||||
}
|
||||
|
||||
export function createMessages(
|
||||
raw: Database,
|
||||
data: Array<{
|
||||
content: string;
|
||||
conversationId: string;
|
||||
parts?: string;
|
||||
role: "assistant" | "system" | "user";
|
||||
}>,
|
||||
): Message[] {
|
||||
const db = wrap(raw);
|
||||
const now = new Date().toISOString();
|
||||
const results: Message[] = [];
|
||||
|
||||
for (const item of data) {
|
||||
const id = crypto.randomUUID();
|
||||
db.insert(messages)
|
||||
.values({
|
||||
content: item.content,
|
||||
conversationId: item.conversationId,
|
||||
createdAt: now,
|
||||
id,
|
||||
parts: item.parts ?? null,
|
||||
role: item.role,
|
||||
})
|
||||
.run();
|
||||
const row = db.select().from(messages).where(eq(messages.id, id)).get();
|
||||
results.push(toMessage(row!));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export function deleteConversation(raw: Database, id: string): { error: string; status: number } | { success: true } {
|
||||
const db = wrap(raw);
|
||||
const existing = db.select().from(conversations).where(eq(conversations.id, id)).get();
|
||||
if (!existing) return { error: "会话不存在", status: 404 };
|
||||
|
||||
db.delete(messages).where(eq(messages.conversationId, id)).run();
|
||||
db.delete(conversations).where(eq(conversations.id, id)).run();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export function getConversation(
|
||||
raw: Database,
|
||||
id: string,
|
||||
): { conversation: Conversation } | { error: string; status: number } {
|
||||
const db = wrap(raw);
|
||||
const row = db.select().from(conversations).where(eq(conversations.id, id)).get();
|
||||
if (!row) return { error: "会话不存在", status: 404 };
|
||||
return { conversation: toConversation(row) };
|
||||
}
|
||||
|
||||
export function listConversations(
|
||||
raw: Database,
|
||||
projectId: string,
|
||||
options: { page: number; pageSize: number },
|
||||
): { items: Conversation[]; page: number; pageSize: number; total: number } {
|
||||
return paginateQuery(raw, conversations, {
|
||||
conditions: [eq(conversations.projectId, projectId)],
|
||||
mapRow: toConversation,
|
||||
orderBy: () => desc(conversations.updatedAt),
|
||||
page: options.page,
|
||||
pageSize: options.pageSize,
|
||||
});
|
||||
}
|
||||
|
||||
export function listMessages(
|
||||
raw: Database,
|
||||
conversationId: string,
|
||||
options: { page: number; pageSize: number },
|
||||
): { items: Message[]; page: number; pageSize: number; total: number } {
|
||||
return paginateQuery(raw, messages, {
|
||||
conditions: [eq(messages.conversationId, conversationId)],
|
||||
mapRow: toMessage,
|
||||
orderBy: () => desc(messages.createdAt),
|
||||
page: options.page,
|
||||
pageSize: options.pageSize,
|
||||
});
|
||||
}
|
||||
|
||||
export function updateConversationTimestamp(raw: Database, id: string): void {
|
||||
const db = wrap(raw);
|
||||
db.update(conversations).set({ updatedAt: new Date().toISOString() }).where(eq(conversations.id, id)).run();
|
||||
}
|
||||
|
||||
function toConversation(row: typeof conversations.$inferSelect): Conversation {
|
||||
return {
|
||||
createdAt: row.createdAt,
|
||||
id: row.id,
|
||||
modelId: row.modelId,
|
||||
projectId: row.projectId,
|
||||
title: row.title,
|
||||
updatedAt: row.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
function toMessage(row: typeof messages.$inferSelect): Message {
|
||||
return {
|
||||
content: row.content,
|
||||
conversationId: row.conversationId,
|
||||
createdAt: row.createdAt,
|
||||
id: row.id,
|
||||
parts: row.parts,
|
||||
role: row.role,
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,14 @@
|
||||
export { createDatabase } from "./connection";
|
||||
export {
|
||||
createConversation,
|
||||
createMessage,
|
||||
createMessages,
|
||||
deleteConversation,
|
||||
getConversation,
|
||||
listConversations,
|
||||
listMessages,
|
||||
updateConversationTimestamp,
|
||||
} from "./conversations";
|
||||
export { loadMigrationsFromDir, type MigrationRecord } from "./load-migrations";
|
||||
export { runMigrations } from "./migrate";
|
||||
export { projects, schemaMigrations } from "./schema";
|
||||
export { conversations, messages, projects, schemaMigrations } from "./schema";
|
||||
|
||||
@@ -45,6 +45,38 @@ export const models = sqliteTable(
|
||||
],
|
||||
);
|
||||
|
||||
export const conversations = sqliteTable(
|
||||
"conversations",
|
||||
{
|
||||
createdAt: text("created_at").notNull(),
|
||||
id: text("id").primaryKey(),
|
||||
modelId: text("model_id")
|
||||
.notNull()
|
||||
.references(() => models.id),
|
||||
projectId: text("project_id")
|
||||
.notNull()
|
||||
.references(() => projects.id),
|
||||
title: text("title").notNull().default("新会话"),
|
||||
updatedAt: text("updated_at").notNull(),
|
||||
},
|
||||
(table) => [index("conversations_project_id_idx").on(table.projectId)],
|
||||
);
|
||||
|
||||
export const messages = sqliteTable(
|
||||
"messages",
|
||||
{
|
||||
content: text("content").notNull().default(""),
|
||||
conversationId: text("conversation_id")
|
||||
.notNull()
|
||||
.references(() => conversations.id, { onDelete: "cascade" }),
|
||||
createdAt: text("created_at").notNull(),
|
||||
id: text("id").primaryKey(),
|
||||
parts: text("parts"),
|
||||
role: text("role", { enum: ["assistant", "system", "user"] }).notNull(),
|
||||
},
|
||||
(table) => [index("messages_conversation_id_idx").on(table.conversationId)],
|
||||
);
|
||||
|
||||
export const schemaMigrations = sqliteTable("schema_migrations", {
|
||||
appliedAt: text("applied_at").notNull(),
|
||||
checksum: text("checksum").notNull(),
|
||||
|
||||
29
src/server/routes/chat/create.ts
Normal file
29
src/server/routes/chat/create.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { CreateConversationRequest, RuntimeMode } from "../../../shared/api";
|
||||
|
||||
import { createConversation } from "../../db/conversations";
|
||||
import { createApiError, jsonResponse } from "../../helpers";
|
||||
import { validateIdParam } from "../../middleware";
|
||||
|
||||
export async function handleCreateConversation(req: Request, db: Database, mode: RuntimeMode): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
const projectId = url.pathname.split("/")[3];
|
||||
|
||||
const validated = validateIdParam(projectId ?? "", mode);
|
||||
if (validated instanceof Response) return validated;
|
||||
|
||||
let body: CreateConversationRequest = {};
|
||||
try {
|
||||
body = (await req.json()) as CreateConversationRequest;
|
||||
} catch {
|
||||
// empty body is ok, defaults will be used
|
||||
}
|
||||
|
||||
const result = createConversation(db, validated.id, body.modelId);
|
||||
if ("error" in result) {
|
||||
return jsonResponse(createApiError(result.error, result.status), { mode, status: result.status });
|
||||
}
|
||||
|
||||
return jsonResponse({ conversation: result.conversation }, { mode, status: 201 });
|
||||
}
|
||||
35
src/server/routes/chat/delete.ts
Normal file
35
src/server/routes/chat/delete.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode } from "../../../shared/api";
|
||||
|
||||
import { deleteConversation, getConversation } from "../../db/conversations";
|
||||
import { createApiError, jsonResponse } from "../../helpers";
|
||||
import { validateIdParam } from "../../middleware";
|
||||
|
||||
export function handleDeleteConversation(req: Request, db: Database, mode: RuntimeMode): Response {
|
||||
const parts = new URL(req.url).pathname.split("/");
|
||||
const projectId = parts[3];
|
||||
const conversationId = parts[5];
|
||||
|
||||
const validatedProject = validateIdParam(projectId ?? "", mode);
|
||||
if (validatedProject instanceof Response) return validatedProject;
|
||||
|
||||
const validatedConv = validateIdParam(conversationId ?? "", mode);
|
||||
if (validatedConv instanceof Response) return validatedConv;
|
||||
|
||||
const convResult = getConversation(db, validatedConv.id);
|
||||
if ("error" in convResult) {
|
||||
return jsonResponse(createApiError(convResult.error, convResult.status), { mode, status: convResult.status });
|
||||
}
|
||||
|
||||
if (convResult.conversation.projectId !== validatedProject.id) {
|
||||
return jsonResponse(createApiError("会话不属于该项目", 403), { mode, status: 403 });
|
||||
}
|
||||
|
||||
const result = deleteConversation(db, validatedConv.id);
|
||||
if ("error" in result) {
|
||||
return jsonResponse(createApiError(result.error, result.status), { mode, status: result.status });
|
||||
}
|
||||
|
||||
return jsonResponse({ success: true }, { mode });
|
||||
}
|
||||
30
src/server/routes/chat/get.ts
Normal file
30
src/server/routes/chat/get.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode } from "../../../shared/api";
|
||||
|
||||
import { getConversation } from "../../db/conversations";
|
||||
import { createApiError, jsonResponse } from "../../helpers";
|
||||
import { validateIdParam } from "../../middleware";
|
||||
|
||||
export function handleGetConversation(req: Request, db: Database, mode: RuntimeMode): Response {
|
||||
const parts = new URL(req.url).pathname.split("/");
|
||||
const projectId = parts[3];
|
||||
const conversationId = parts[5];
|
||||
|
||||
const validatedProject = validateIdParam(projectId ?? "", mode);
|
||||
if (validatedProject instanceof Response) return validatedProject;
|
||||
|
||||
const validatedConv = validateIdParam(conversationId ?? "", mode);
|
||||
if (validatedConv instanceof Response) return validatedConv;
|
||||
|
||||
const result = getConversation(db, validatedConv.id);
|
||||
if ("error" in result) {
|
||||
return jsonResponse(createApiError(result.error, result.status), { mode, status: result.status });
|
||||
}
|
||||
|
||||
if (result.conversation.projectId !== validatedProject.id) {
|
||||
return jsonResponse(createApiError("会话不属于该项目", 403), { mode, status: 403 });
|
||||
}
|
||||
|
||||
return jsonResponse({ conversation: result.conversation }, { mode });
|
||||
}
|
||||
28
src/server/routes/chat/list.ts
Normal file
28
src/server/routes/chat/list.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode } from "../../../shared/api";
|
||||
|
||||
import { listConversations } from "../../db/conversations";
|
||||
import { jsonResponse } from "../../helpers";
|
||||
import { validateIdParam, validatePagination } from "../../middleware";
|
||||
|
||||
export function handleListConversations(req: Request, db: Database, mode: RuntimeMode): Response {
|
||||
const url = new URL(req.url);
|
||||
const projectId = url.pathname.split("/")[3];
|
||||
|
||||
const validated = validateIdParam(projectId ?? "", mode);
|
||||
if (validated instanceof Response) return validated;
|
||||
|
||||
const pageParam = url.searchParams.get("page");
|
||||
const pageSizeParam = url.searchParams.get("pageSize");
|
||||
|
||||
const pagination = validatePagination(pageParam, pageSizeParam, mode);
|
||||
if (pagination instanceof Response) return pagination;
|
||||
|
||||
const result = listConversations(db, validated.id, {
|
||||
page: pagination.page,
|
||||
pageSize: pagination.pageSize,
|
||||
});
|
||||
|
||||
return jsonResponse(result, { mode });
|
||||
}
|
||||
42
src/server/routes/chat/messages.ts
Normal file
42
src/server/routes/chat/messages.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode } from "../../../shared/api";
|
||||
|
||||
import { getConversation, listMessages } from "../../db/conversations";
|
||||
import { createApiError, jsonResponse } from "../../helpers";
|
||||
import { validateIdParam, validatePagination } from "../../middleware";
|
||||
|
||||
export function handleListMessages(req: Request, db: Database, mode: RuntimeMode): Response {
|
||||
const parts = new URL(req.url).pathname.split("/");
|
||||
const projectId = parts[3];
|
||||
const conversationId = parts[5];
|
||||
|
||||
const validatedProject = validateIdParam(projectId ?? "", mode);
|
||||
if (validatedProject instanceof Response) return validatedProject;
|
||||
|
||||
const validatedConv = validateIdParam(conversationId ?? "", mode);
|
||||
if (validatedConv instanceof Response) return validatedConv;
|
||||
|
||||
const convResult = getConversation(db, validatedConv.id);
|
||||
if ("error" in convResult) {
|
||||
return jsonResponse(createApiError(convResult.error, convResult.status), { mode, status: convResult.status });
|
||||
}
|
||||
|
||||
if (convResult.conversation.projectId !== validatedProject.id) {
|
||||
return jsonResponse(createApiError("会话不属于该项目", 403), { mode, status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const pageParam = url.searchParams.get("page");
|
||||
const pageSizeParam = url.searchParams.get("pageSize");
|
||||
|
||||
const pagination = validatePagination(pageParam, pageSizeParam, mode);
|
||||
if (pagination instanceof Response) return pagination;
|
||||
|
||||
const result = listMessages(db, validatedConv.id, {
|
||||
page: pagination.page,
|
||||
pageSize: pagination.pageSize,
|
||||
});
|
||||
|
||||
return jsonResponse(result, { mode });
|
||||
}
|
||||
91
src/server/routes/chat/send.ts
Normal file
91
src/server/routes/chat/send.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import type { RuntimeMode } from "../../../shared/api";
|
||||
import type { IncomingMessage } from "../../ai/agent-stream";
|
||||
|
||||
import { agentStream, extractTextContent } from "../../ai/agent-stream";
|
||||
import { createMessage, getConversation, updateConversationTimestamp } from "../../db/conversations";
|
||||
import { createApiError, jsonResponse } from "../../helpers";
|
||||
import { validateIdParam } from "../../middleware";
|
||||
|
||||
export async function handleSendChat(req: Request, db: Database, mode: RuntimeMode): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
const projectId = url.pathname.split("/")[3];
|
||||
|
||||
const validated = validateIdParam(projectId ?? "", mode);
|
||||
if (validated instanceof Response) return validated;
|
||||
|
||||
let body: { conversationId?: string; messages?: IncomingMessage[] };
|
||||
try {
|
||||
body = (await req.json()) as typeof body;
|
||||
} catch {
|
||||
return jsonResponse(createApiError("Invalid JSON body", 400), { mode, status: 400 });
|
||||
}
|
||||
|
||||
if (!body.conversationId || typeof body.conversationId !== "string") {
|
||||
return jsonResponse(createApiError("conversationId is required", 400), { mode, status: 400 });
|
||||
}
|
||||
|
||||
if (!Array.isArray(body.messages) || body.messages.length === 0) {
|
||||
return jsonResponse(createApiError("messages is required and must be a non-empty array", 400), {
|
||||
mode,
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const conversationResult = getConversation(db, body.conversationId);
|
||||
if ("error" in conversationResult) {
|
||||
return jsonResponse(createApiError(conversationResult.error, conversationResult.status), {
|
||||
mode,
|
||||
status: conversationResult.status,
|
||||
});
|
||||
}
|
||||
|
||||
const conversation = conversationResult.conversation;
|
||||
if (conversation.projectId !== validated.id) {
|
||||
return jsonResponse(createApiError("会话不属于该项目", 403), { mode, status: 403 });
|
||||
}
|
||||
|
||||
for (const msg of body.messages ?? []) {
|
||||
createMessage(db, {
|
||||
content: extractTextContent(msg),
|
||||
conversationId: conversation.id,
|
||||
role: (msg.role ?? "user") as "assistant" | "system" | "user",
|
||||
});
|
||||
}
|
||||
|
||||
updateConversationTimestamp(db, conversation.id);
|
||||
|
||||
try {
|
||||
const result = agentStream({
|
||||
db,
|
||||
messages: body.messages,
|
||||
modelDbId: conversation.modelId,
|
||||
});
|
||||
|
||||
const stream = result.toUIMessageStreamResponse();
|
||||
|
||||
const saveReply = async () => {
|
||||
try {
|
||||
const fullContent = await result.text;
|
||||
if (fullContent) {
|
||||
createMessage(db, {
|
||||
content: fullContent,
|
||||
conversationId: conversation.id,
|
||||
role: "assistant",
|
||||
});
|
||||
updateConversationTimestamp(db, conversation.id);
|
||||
}
|
||||
} catch {
|
||||
// stream ended without content, nothing to persist
|
||||
}
|
||||
};
|
||||
|
||||
void saveReply();
|
||||
|
||||
return stream;
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
return jsonResponse(createApiError(`AI 调用失败:${msg}`, 500), { mode, status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,62 @@ export function startServer(options: StartServerOptions) {
|
||||
logger,
|
||||
),
|
||||
},
|
||||
"/api/projects/:id/chat": {
|
||||
POST: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleSendChat } = await import("./routes/chat/send");
|
||||
return handleSendChat(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
},
|
||||
"/api/projects/:id/conversations": {
|
||||
GET: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleListConversations } = await import("./routes/chat/list");
|
||||
return handleListConversations(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
POST: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleCreateConversation } = await import("./routes/chat/create");
|
||||
return handleCreateConversation(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
},
|
||||
"/api/projects/:id/conversations/:cid": {
|
||||
DELETE: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleDeleteConversation } = await import("./routes/chat/delete");
|
||||
return handleDeleteConversation(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
GET: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleGetConversation } = await import("./routes/chat/get");
|
||||
return handleGetConversation(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
},
|
||||
"/api/projects/:id/conversations/:cid/messages": {
|
||||
GET: withErrorHandler(
|
||||
async (req) => {
|
||||
const { handleListMessages } = await import("./routes/chat/messages");
|
||||
return handleListMessages(req, db, mode);
|
||||
},
|
||||
mode,
|
||||
logger,
|
||||
),
|
||||
},
|
||||
"/api/projects/:id/restore": {
|
||||
POST: withErrorHandler(
|
||||
async (req) => {
|
||||
|
||||
@@ -3,6 +3,31 @@ export interface ApiErrorResponse {
|
||||
status: number;
|
||||
}
|
||||
|
||||
export interface Conversation {
|
||||
createdAt: string;
|
||||
id: string;
|
||||
modelId: string;
|
||||
projectId: string;
|
||||
title: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface ConversationListResponse {
|
||||
items: Conversation[];
|
||||
page: number;
|
||||
pageSize: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface ConversationResponse {
|
||||
conversation: Conversation;
|
||||
}
|
||||
|
||||
export interface CreateConversationRequest {
|
||||
modelId?: string;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export interface CreateModelRequest {
|
||||
capabilities: ModelCapability[];
|
||||
contextLength?: null | number;
|
||||
@@ -29,6 +54,22 @@ export interface CreateProviderRequest {
|
||||
type: ProviderType;
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
content: string;
|
||||
conversationId: string;
|
||||
createdAt: string;
|
||||
id: string;
|
||||
parts: null | string;
|
||||
role: "assistant" | "system" | "user";
|
||||
}
|
||||
|
||||
export interface MessageListResponse {
|
||||
items: Message[];
|
||||
page: number;
|
||||
pageSize: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface MetaResponse {
|
||||
ok: true;
|
||||
service: string;
|
||||
@@ -58,6 +99,11 @@ export type ModelCapability =
|
||||
| "video-generation"
|
||||
| "video-recognition";
|
||||
|
||||
export interface SendMessageRequest {
|
||||
conversationId: string;
|
||||
messages: Array<{ content: string; role: "assistant" | "system" | "user" }>;
|
||||
}
|
||||
|
||||
export const MODEL_CAPABILITIES: readonly ModelCapability[] = [
|
||||
"audio-generation",
|
||||
"audio-recognition",
|
||||
|
||||
132
src/web/consoles/workbench/components/chat/ChatPanel.tsx
Normal file
132
src/web/consoles/workbench/components/chat/ChatPanel.tsx
Normal file
@@ -0,0 +1,132 @@
|
||||
import type { BubbleItemType } from "@ant-design/x";
|
||||
|
||||
import { useChat } from "@ai-sdk/react";
|
||||
import { Bubble, Sender } from "@ant-design/x";
|
||||
import { DefaultChatTransport } from "ai";
|
||||
import { App, Empty, Spin } from "antd";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
import { fetchMessages } from "../../../../hooks/use-conversations";
|
||||
import { MessageBubble } from "./MessageBubble";
|
||||
|
||||
interface ChatPanelProps {
|
||||
conversationId: null | string;
|
||||
onConversationCreated: (id: string) => void;
|
||||
projectId: string;
|
||||
}
|
||||
|
||||
export function ChatPanel({ conversationId, projectId }: ChatPanelProps) {
|
||||
const { message } = App.useApp();
|
||||
const [input, setInput] = useState("");
|
||||
const [loadingHistory, setLoadingHistory] = useState(false);
|
||||
const fetchRef = useRef(fetchMessages);
|
||||
|
||||
const conversationIdRef = useRef(conversationId);
|
||||
useEffect(() => {
|
||||
conversationIdRef.current = conversationId;
|
||||
});
|
||||
|
||||
const { messages, sendMessage, setMessages, status } = useChat({
|
||||
onError: (err) => {
|
||||
void message.error(`发送失败:${err.message}`);
|
||||
},
|
||||
transport: new DefaultChatTransport({
|
||||
api: `/api/projects/${projectId}/chat`,
|
||||
}),
|
||||
});
|
||||
|
||||
const isLoading = status === "submitted" || status === "streaming";
|
||||
|
||||
useEffect(() => {
|
||||
if (!conversationId) {
|
||||
setMessages([]);
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
const load = async () => {
|
||||
setLoadingHistory(true);
|
||||
setMessages([]);
|
||||
try {
|
||||
const data = await fetchRef.current(projectId, conversationId);
|
||||
if (cancelled) return;
|
||||
const history = data.items
|
||||
.filter((m: { role: string }) => m.role === "user" || m.role === "assistant")
|
||||
.reverse()
|
||||
.map((m: { content: string; id: string; role: string }) => ({
|
||||
id: m.id,
|
||||
parts: [{ text: m.content, type: "text" as const }],
|
||||
role: m.role as "assistant" | "user",
|
||||
}));
|
||||
setMessages(history);
|
||||
} catch (err: unknown) {
|
||||
if (!cancelled) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
void message.error(`加载历史失败:${msg}`);
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setLoadingHistory(false);
|
||||
}
|
||||
};
|
||||
|
||||
void load();
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [conversationId, projectId, setMessages, message]);
|
||||
|
||||
const bubbleItems: BubbleItemType[] = messages.map((msg) => ({
|
||||
content: msg.parts
|
||||
.filter((p): p is { text: string; type: "text" } => p.type === "text")
|
||||
.map((p) => p.text)
|
||||
.join(""),
|
||||
key: msg.id,
|
||||
role: msg.role === "user" ? "user" : "ai",
|
||||
}));
|
||||
|
||||
const onSubmit = useCallback(
|
||||
(nextInput: string) => {
|
||||
if (!nextInput.trim()) return;
|
||||
setInput("");
|
||||
void sendMessage({ text: nextInput }, { body: { conversationId: conversationIdRef.current } });
|
||||
},
|
||||
[sendMessage],
|
||||
);
|
||||
|
||||
if (!conversationId) {
|
||||
return (
|
||||
<div className="app-chat-panel app-chat-panel-empty">
|
||||
<Empty description="选择或创建一个会话开始聊天" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app-chat-panel">
|
||||
{loadingHistory ? (
|
||||
<div className="app-chat-panel-loading">
|
||||
<Spin />
|
||||
</div>
|
||||
) : (
|
||||
<Bubble.List
|
||||
items={bubbleItems}
|
||||
role={{
|
||||
ai: {
|
||||
contentRender: (content: string) => <MessageBubble content={content} />,
|
||||
placement: "start",
|
||||
},
|
||||
user: {
|
||||
placement: "end",
|
||||
},
|
||||
}}
|
||||
style={{ flex: 1, overflow: "auto", padding: "16px" }}
|
||||
/>
|
||||
)}
|
||||
<div className="app-chat-panel-sender">
|
||||
<Sender loading={isLoading} onChange={setInput} onSubmit={onSubmit} placeholder="输入消息..." value={input} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
97
src/web/consoles/workbench/components/chat/ChatSidebar.tsx
Normal file
97
src/web/consoles/workbench/components/chat/ChatSidebar.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import { DeleteOutlined, PlusOutlined } from "@ant-design/icons";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { App, Button, Flex, Popconfirm, Spin, Typography } from "antd";
|
||||
|
||||
import type { Conversation } from "../../../../../shared/api";
|
||||
|
||||
import { createConversation, deleteConversation, fetchConversations } from "../../../../hooks/use-conversations";
|
||||
|
||||
interface ChatSidebarProps {
|
||||
activeId: null | string;
|
||||
onSelect: (id: null | string) => void;
|
||||
projectId: string;
|
||||
}
|
||||
|
||||
export function ChatSidebar({ activeId, onSelect, projectId }: ChatSidebarProps) {
|
||||
const queryClient = useQueryClient();
|
||||
const { message } = App.useApp();
|
||||
|
||||
const CONVERSATIONS_KEY = ["conversations", projectId] as const;
|
||||
|
||||
const { data, isLoading } = useQuery({
|
||||
queryFn: () => fetchConversations(projectId),
|
||||
queryKey: CONVERSATIONS_KEY,
|
||||
});
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: () => createConversation(projectId),
|
||||
onError: (err: Error) => {
|
||||
void message.error(`创建会话失败:${err.message}`);
|
||||
},
|
||||
onSuccess: (conversation: Conversation) => {
|
||||
void queryClient.invalidateQueries({ queryKey: CONVERSATIONS_KEY });
|
||||
onSelect(conversation.id);
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (id: string) => deleteConversation(projectId, id),
|
||||
onError: (err: Error) => {
|
||||
void message.error(`删除会话失败:${err.message}`);
|
||||
},
|
||||
onSuccess: (_data: void, id: string) => {
|
||||
void queryClient.invalidateQueries({ queryKey: CONVERSATIONS_KEY });
|
||||
if (activeId === id) onSelect(null);
|
||||
},
|
||||
});
|
||||
|
||||
const conversations = data?.items ?? [];
|
||||
|
||||
return (
|
||||
<div className="app-chat-sidebar">
|
||||
<div className="app-chat-sidebar-header">
|
||||
<Button
|
||||
block
|
||||
icon={<PlusOutlined />}
|
||||
loading={createMutation.isPending}
|
||||
onClick={() => createMutation.mutate()}
|
||||
type="primary"
|
||||
>
|
||||
新会话
|
||||
</Button>
|
||||
</div>
|
||||
<div className="app-chat-sidebar-list">
|
||||
{isLoading ? (
|
||||
<div className="app-chat-sidebar-loading">
|
||||
<Spin />
|
||||
</div>
|
||||
) : (
|
||||
conversations.map((item: Conversation) => (
|
||||
<Flex
|
||||
align="center"
|
||||
className={`app-chat-sidebar-item ${activeId === item.id ? "app-chat-sidebar-item-active" : ""}`}
|
||||
gap="small"
|
||||
justify="space-between"
|
||||
key={item.id}
|
||||
onClick={() => onSelect(item.id)}
|
||||
>
|
||||
<Typography.Text className="app-chat-sidebar-item-title" ellipsis>
|
||||
{item.title}
|
||||
</Typography.Text>
|
||||
<Popconfirm onConfirm={() => deleteMutation.mutate(item.id)} title="确定删除此会话?">
|
||||
<Button
|
||||
className="app-chat-sidebar-item-action"
|
||||
danger
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
size="small"
|
||||
type="text"
|
||||
/>
|
||||
</Popconfirm>
|
||||
</Flex>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
interface MessageBubbleProps {
|
||||
content: string;
|
||||
}
|
||||
|
||||
export function MessageBubble({ content }: MessageBubbleProps) {
|
||||
return <div className="app-chat-message-bubble">{content}</div>;
|
||||
}
|
||||
22
src/web/consoles/workbench/pages/ChatPage.tsx
Normal file
22
src/web/consoles/workbench/pages/ChatPage.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Flex } from "antd";
|
||||
import { useState } from "react";
|
||||
|
||||
import { ChatPanel } from "../components/chat/ChatPanel";
|
||||
import { ChatSidebar } from "../components/chat/ChatSidebar";
|
||||
import { useCurrentProject } from "../useCurrentProject";
|
||||
|
||||
export function ChatPage() {
|
||||
const project = useCurrentProject();
|
||||
const [activeConversationId, setActiveConversationId] = useState<null | string>(null);
|
||||
|
||||
return (
|
||||
<Flex className="app-chat-page" gap={0} vertical={false}>
|
||||
<ChatSidebar activeId={activeConversationId} onSelect={setActiveConversationId} projectId={project.id} />
|
||||
<ChatPanel
|
||||
conversationId={activeConversationId}
|
||||
onConversationCreated={setActiveConversationId}
|
||||
projectId={project.id}
|
||||
/>
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { DashboardOutlined } from "@ant-design/icons";
|
||||
import { MessageOutlined } from "@ant-design/icons";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { MenuItemConfig } from "../../menu";
|
||||
|
||||
export const WORKBENCH_MENU_ITEMS: readonly MenuItemConfig[] = [
|
||||
{ icon: createElement(DashboardOutlined), label: "总览", path: "", value: "overview" },
|
||||
{ icon: createElement(MessageOutlined), label: "聊天室", path: "", value: "chat" },
|
||||
] as const;
|
||||
|
||||
export function buildWorkbenchPath(projectId: string, relativePath = ""): string {
|
||||
|
||||
45
src/web/hooks/use-conversations.ts
Normal file
45
src/web/hooks/use-conversations.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type {
|
||||
Conversation,
|
||||
ConversationListResponse,
|
||||
ConversationResponse,
|
||||
MessageListResponse,
|
||||
} from "../../shared/api";
|
||||
|
||||
import { handleResponse, handleVoidResponse } from "../utils/api";
|
||||
|
||||
export async function createConversation(projectId: string, modelId?: string): Promise<Conversation> {
|
||||
const response = await fetch(`/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({ modelId }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
return handleResponse(response, (data) => (data as ConversationResponse).conversation);
|
||||
}
|
||||
|
||||
export async function deleteConversation(projectId: string, conversationId: string): Promise<void> {
|
||||
const response = await fetch(`/api/projects/${projectId}/conversations/${conversationId}`, { method: "DELETE" });
|
||||
return handleVoidResponse(response);
|
||||
}
|
||||
|
||||
export async function fetchConversation(projectId: string, conversationId: string): Promise<Conversation> {
|
||||
const response = await fetch(`/api/projects/${projectId}/conversations/${conversationId}`);
|
||||
return handleResponse(response, (data) => (data as ConversationResponse).conversation);
|
||||
}
|
||||
|
||||
export async function fetchConversations(projectId: string): Promise<ConversationListResponse> {
|
||||
const response = await fetch(`/api/projects/${projectId}/conversations?pageSize=100`);
|
||||
if (!response.ok) {
|
||||
const body = (await response.json().catch(() => null)) as null | { error?: string };
|
||||
throw new Error(body?.error ?? `HTTP ${response.status}`);
|
||||
}
|
||||
return response.json() as Promise<ConversationListResponse>;
|
||||
}
|
||||
|
||||
export async function fetchMessages(projectId: string, conversationId: string): Promise<MessageListResponse> {
|
||||
const response = await fetch(`/api/projects/${projectId}/conversations/${conversationId}/messages?pageSize=200`);
|
||||
if (!response.ok) {
|
||||
const body = (await response.json().catch(() => null)) as null | { error?: string };
|
||||
throw new Error(body?.error ?? `HTTP ${response.status}`);
|
||||
}
|
||||
return response.json() as Promise<MessageListResponse>;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import { Card, Descriptions, Space, Typography } from "antd";
|
||||
|
||||
import { useCurrentProject } from "../../consoles/workbench/useCurrentProject";
|
||||
|
||||
export function WorkbenchOverviewPage() {
|
||||
const project = useCurrentProject();
|
||||
|
||||
const items = [
|
||||
{ children: project.name, key: "name", label: "项目名称" },
|
||||
{ children: project.description || "暂无描述", key: "description", label: "项目描述" },
|
||||
{ children: project.status === "active" ? "进行中" : "已归档", key: "status", label: "状态" },
|
||||
];
|
||||
|
||||
return (
|
||||
<Space size="large" vertical>
|
||||
<Typography.Title level={2}>总览</Typography.Title>
|
||||
<Card>
|
||||
<Descriptions column={1} items={items} title={project.name} />
|
||||
</Card>
|
||||
</Space>
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Route, Routes } from "react-router";
|
||||
|
||||
import { AdminConsoleLayout } from "./consoles/admin/AdminConsoleLayout";
|
||||
import { ChatPage } from "./consoles/workbench/pages/ChatPage";
|
||||
import { WorkbenchProjectGate } from "./consoles/workbench/WorkbenchProjectGate";
|
||||
import { NotFoundPage } from "./pages/404";
|
||||
import { DashboardPage } from "./pages/dashboard";
|
||||
import { ModelsPage } from "./pages/models";
|
||||
import { ProjectsPage } from "./pages/projects";
|
||||
import { WorkbenchOverviewPage } from "./pages/workbench";
|
||||
|
||||
export function AppRoutes() {
|
||||
return (
|
||||
@@ -17,7 +17,8 @@ export function AppRoutes() {
|
||||
<Route element={<ModelsPage />} path="/models" />
|
||||
</Route>
|
||||
<Route element={<WorkbenchProjectGate />} path="/workbench/:projectId">
|
||||
<Route element={<WorkbenchOverviewPage />} path="" />
|
||||
<Route element={<ChatPage />} path="" />
|
||||
<Route element={<ChatPage />} path="chat" />
|
||||
</Route>
|
||||
<Route element={<NotFoundPage />} path="*" />
|
||||
</Routes>
|
||||
|
||||
@@ -53,6 +53,10 @@ body {
|
||||
padding: var(--ant-padding-xl) var(--ant-padding-xl);
|
||||
}
|
||||
|
||||
.app-chat-page {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.app-console-title {
|
||||
color: var(--ant-color-text-secondary);
|
||||
font-size: var(--ant-font-size);
|
||||
@@ -72,3 +76,87 @@ body {
|
||||
justify-content: center;
|
||||
min-height: 60vh;
|
||||
}
|
||||
|
||||
.app-chat-sidebar {
|
||||
display: flex;
|
||||
width: 260px;
|
||||
flex-direction: column;
|
||||
border-right: 1px solid var(--ant-color-border-secondary);
|
||||
background: var(--ant-color-bg-container);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-header {
|
||||
padding: var(--ant-padding-sm);
|
||||
border-bottom: 1px solid var(--ant-color-border-secondary);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-list {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.app-chat-sidebar-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: var(--ant-padding-xl);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item {
|
||||
cursor: pointer;
|
||||
padding: var(--ant-padding-xs) var(--ant-padding-sm);
|
||||
border-bottom: 1px solid var(--ant-color-border-secondary);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item:hover {
|
||||
background: var(--ant-color-bg-text-hover);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item-active {
|
||||
background: var(--ant-color-bg-text-hover);
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item-title {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item-action {
|
||||
opacity: 0;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
.app-chat-sidebar-item:hover .app-chat-sidebar-item-action,
|
||||
.app-chat-sidebar-item-active .app-chat-sidebar-item-action {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.app-chat-panel {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.app-chat-panel-empty {
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.app-chat-panel-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.app-chat-panel-sender {
|
||||
padding: var(--ant-padding-sm) var(--ant-padding);
|
||||
border-top: 1px solid var(--ant-color-border-secondary);
|
||||
}
|
||||
|
||||
.app-chat-message-bubble {
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
490
tests/server/routes/chat.test.ts
Normal file
490
tests/server/routes/chat.test.ts
Normal file
@@ -0,0 +1,490 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
|
||||
import type { Conversation, Message, RuntimeMode } from "../../../src/shared/api";
|
||||
|
||||
import { createModel } from "../../../src/server/db/models";
|
||||
import { createProject } from "../../../src/server/db/projects";
|
||||
import { createProvider } from "../../../src/server/db/providers";
|
||||
import { createMigratedMemoryTestDatabase } from "../../helpers";
|
||||
|
||||
const MODE: RuntimeMode = "test";
|
||||
|
||||
void mock.module("ai", () => ({
|
||||
createProviderRegistry: () => ({
|
||||
languageModel: () => ({}),
|
||||
}),
|
||||
generateText: () => Promise.resolve({ text: "mock", usage: {} }),
|
||||
stepCountIs: () => () => true,
|
||||
streamText: () => ({
|
||||
text: Promise.resolve("test reply from AI"),
|
||||
toUIMessageStreamResponse: () =>
|
||||
new Response('data: {"type":"text","text":"test reply from AI"}\n\n', {
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
|
||||
async function createConversationViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleCreateConversation: h } = await import("../../../src/server/routes/chat/create");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function deleteConversationViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleDeleteConversation: h } = await import("../../../src/server/routes/chat/delete");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function getConversationViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleGetConversation: h } = await import("../../../src/server/routes/chat/get");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function listConversationsViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleListConversations: h } = await import("../../../src/server/routes/chat/list");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function listMessagesViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleListMessages: h } = await import("../../../src/server/routes/chat/messages");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
function seedModel(db: Database, providerId: string, modelName = "GPT-4o"): string {
|
||||
const result = createModel(db, {
|
||||
capabilities: ["text"],
|
||||
modelId: "gpt-4o",
|
||||
name: modelName,
|
||||
providerId,
|
||||
});
|
||||
if ("error" in result) throw new Error(result.error);
|
||||
return result.model.id;
|
||||
}
|
||||
|
||||
function seedProject(db: Database, name = "测试项目"): string {
|
||||
const result = createProject(db, { description: "测试", name });
|
||||
if ("error" in result) throw new Error(result.error);
|
||||
return result.project.id;
|
||||
}
|
||||
|
||||
function seedProvider(db: Database, name = "测试供应商"): string {
|
||||
const result = createProvider(db, {
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.test.com/v1",
|
||||
name,
|
||||
type: "openai",
|
||||
});
|
||||
if ("error" in result) throw new Error(result.error);
|
||||
return result.provider.id;
|
||||
}
|
||||
|
||||
async function sendChatViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleSendChat: h } = await import("../../../src/server/routes/chat/send");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
describe("聊天 API 路由", () => {
|
||||
describe("POST /api/projects/:id/conversations", () => {
|
||||
test("创建会话成功", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-create");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const req = new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await createConversationViaHandler(req, db);
|
||||
expect(res.status).toBe(201);
|
||||
const body = (await res.json()) as { conversation: Conversation };
|
||||
expect(body.conversation.title).toBe("新会话");
|
||||
expect(body.conversation.projectId).toBe(projectId);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("无可用模型时返回 400", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-create-no-model");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
|
||||
const req = new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await createConversationViaHandler(req, db);
|
||||
expect(res.status).toBe(400);
|
||||
const body = (await res.json()) as { error: string };
|
||||
expect(body.error).toContain("模型");
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/projects/:id/conversations", () => {
|
||||
test("列出项目会话", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-list");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const req1 = new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
await createConversationViaHandler(req1, db);
|
||||
|
||||
const req2 = new Request(`http://localhost/api/projects/${projectId}/conversations?page=1&pageSize=10`);
|
||||
const res = await listConversationsViaHandler(req2, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { items: Conversation[]; total: number };
|
||||
expect(body.total).toBe(1);
|
||||
expect(body.items[0]?.title).toBe("新会话");
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("不同项目会话隔离", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-isolation");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectA = seedProject(db, "项目A");
|
||||
const projectB = seedProject(db, "项目B");
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
|
||||
const res = await listConversationsViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectB}/conversations?page=1&pageSize=10`),
|
||||
db,
|
||||
);
|
||||
const body = (await res.json()) as { items: Conversation[]; total: number };
|
||||
expect(body.total).toBe(0);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/projects/:id/conversations/:cid", () => {
|
||||
test("获取会话详情成功", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-get");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await getConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations/${created.id}`),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { conversation: Conversation };
|
||||
expect(body.conversation.title).toBe("新会话");
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("不存在的会话返回 404", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-get-404");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
|
||||
const res = await getConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations/nonexistent`),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(404);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("跨项目获取会话返回 403", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-get-403");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectA = seedProject(db, "项目A");
|
||||
const projectB = seedProject(db, "项目B");
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await getConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectB}/conversations/${created.id}`),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
const body = (await res.json()) as { error: string };
|
||||
expect(body.error).toContain("不属于该项目");
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/projects/:id/conversations/:cid", () => {
|
||||
test("删除会话成功", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-delete");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await deleteConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations/${created.id}`, { method: "DELETE" }),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const getRes = await getConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations/${created.id}`),
|
||||
db,
|
||||
);
|
||||
expect(getRes.status).toBe(404);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("跨项目删除会话返回 403", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-delete-403");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectA = seedProject(db, "项目A");
|
||||
const projectB = seedProject(db, "项目B");
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await deleteConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectB}/conversations/${created.id}`, { method: "DELETE" }),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
|
||||
const getRes = await getConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations/${created.id}`),
|
||||
db,
|
||||
);
|
||||
expect(getRes.status).toBe(200);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/projects/:id/conversations/:cid/messages", () => {
|
||||
test("跨项目获取消息返回 403", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-msg-403");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectA = seedProject(db, "项目A");
|
||||
const projectB = seedProject(db, "项目B");
|
||||
const providerId = seedProvider(db);
|
||||
seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await listMessagesViaHandler(
|
||||
new Request(
|
||||
`http://localhost/api/projects/${projectB}/conversations/${created.id}/messages?page=1&pageSize=10`,
|
||||
),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/projects/:id/chat", () => {
|
||||
test("发送消息成功", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-send");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
const providerId = seedProvider(db);
|
||||
const modelId = seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await sendChatViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/chat`, {
|
||||
body: JSON.stringify({
|
||||
conversationId: created.id,
|
||||
messages: [{ content: "你好", role: "user" }],
|
||||
modelDbId: modelId,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const msgRes = await listMessagesViaHandler(
|
||||
new Request(
|
||||
`http://localhost/api/projects/${projectId}/conversations/${created.id}/messages?page=1&pageSize=10`,
|
||||
),
|
||||
db,
|
||||
);
|
||||
const msgBody = (await msgRes.json()) as { items: Message[] };
|
||||
expect(msgBody.items.length).toBeGreaterThanOrEqual(1);
|
||||
expect(msgBody.items.some((m) => m.role === "user")).toBe(true);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("缺少 conversationId 返回 400", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-send-400");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectId = seedProject(db);
|
||||
|
||||
const res = await sendChatViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectId}/chat`, {
|
||||
body: JSON.stringify({ messages: [{ content: "hi", role: "user" }] }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(400);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test("跨项目发送消息返回 403", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-send-403");
|
||||
try {
|
||||
const db = handle.db;
|
||||
const projectA = seedProject(db, "项目A");
|
||||
const projectB = seedProject(db, "项目B");
|
||||
const providerId = seedProvider(db);
|
||||
const modelId = seedModel(db, providerId);
|
||||
|
||||
const createRes = await createConversationViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectA}/conversations`, {
|
||||
body: JSON.stringify({}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
const created = ((await createRes.json()) as { conversation: Conversation }).conversation;
|
||||
|
||||
const res = await sendChatViaHandler(
|
||||
new Request(`http://localhost/api/projects/${projectB}/chat`, {
|
||||
body: JSON.stringify({
|
||||
conversationId: created.id,
|
||||
messages: [{ content: "探测", role: "user" }],
|
||||
modelDbId: modelId,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
}),
|
||||
db,
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,8 +3,6 @@ import { describe, expect, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import { App } from "../../../src/web/app";
|
||||
import { ProjectProvider } from "../../../src/web/consoles/workbench/ProjectContext";
|
||||
import { WorkbenchOverviewPage } from "../../../src/web/pages/workbench";
|
||||
import { renderWithProviders } from "../test-utils";
|
||||
|
||||
const MOCK_PROJECT = {
|
||||
@@ -33,6 +31,12 @@ function createMockHandler(overrides?: { archivedAt?: string; status?: "active"
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
if (url.includes("/api/projects/") && url.includes("/conversations")) {
|
||||
return new Response(JSON.stringify({ items: [], page: 1, pageSize: 100, total: 0 }), {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
return new Response(JSON.stringify({ error: "Not Found" }), { status: 404 });
|
||||
};
|
||||
const mocked = handler as unknown as typeof fetch;
|
||||
@@ -41,7 +45,7 @@ function createMockHandler(overrides?: { archivedAt?: string; status?: "active"
|
||||
}
|
||||
|
||||
describe("Workbench 路由", () => {
|
||||
test("active 项目可进入 Workbench 并展示总览", async () => {
|
||||
test("active 项目可进入 Workbench", async () => {
|
||||
createMockHandler();
|
||||
|
||||
renderWithProviders(createElement(App), {
|
||||
@@ -102,12 +106,18 @@ describe("Workbench 路由", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("Workbench 总览页标题显示'总览'", () => {
|
||||
renderWithProviders(
|
||||
createElement(ProjectProvider, { children: createElement(WorkbenchOverviewPage), project: MOCK_PROJECT }),
|
||||
);
|
||||
test("Workbench 显示聊天室菜单", async () => {
|
||||
createMockHandler();
|
||||
|
||||
expect(screen.getByText("总览")).not.toBeNull();
|
||||
expect(screen.getAllByText(MOCK_PROJECT.name).length).toBeGreaterThan(0);
|
||||
renderWithProviders(createElement(App), {
|
||||
initialRoute: `/workbench/${MOCK_PROJECT.id}`,
|
||||
});
|
||||
|
||||
await waitFor(
|
||||
() => {
|
||||
expect(screen.getByText("聊天室")).not.toBeNull();
|
||||
},
|
||||
{ timeout: 10000 },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user