docs: update 3 reference docs + add Android design, Feishu bot config, productization plan

- Rewrite api-reference.md: 245 endpoints across 38 modules, correct auth paths and response format
- Rewrite 内置工具列表.md: all 56 real tools in 11 categories
- Fix quickstart.md: local dev ports (3001/8038) vs Docker (8037/8038), --port 8038
- Add android-app-design.md: Kotlin/Compose/MVVM design with SSE, FCM, voice
- Add 飞书智能体配置手册.md: all 6 bots config, capabilities, memory architecture
- Add 产品化落地方案.md: PWA/voice/push/Flutter productization roadmap

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
renjianbo
2026-06-13 21:58:10 +08:00
parent ab1589921a
commit a7512a5423
6 changed files with 1814 additions and 604 deletions

View File

@@ -1,403 +1,391 @@
# 🔌 API 参考文档
# API 参考文档
> **API Reference**
天工智能体平台后端 RESTful API 接口规范。所有 API 前缀为 `/api/v1`
本文档描述了天工智能体平台后端 RESTful API 接口规范。所有 API 均通过 Nginx 反向代理暴露,前缀为 `/api/v1`
> 💡 **交互式文档**:启动后端服务后,可访问 `http://localhost:8037/docs` 查看 Swagger UI 文档。
启动后端后访问 Swagger UI 可查看完整接口列表并在线调试
---
## 一、通用规范
## 通用规范
### 基础 URL
```
生产环境https://your-domain.com/api/v1
开发环境:http://localhost:8037/api/v1
```
| 环境 | 地址 |
|------|------|
| 本地开发 | `http://localhost:8038/api/v1` |
| Docker 部署 | `http://localhost:8037/api/v1` |
| 生产环境 | `https://your-domain.com/api/v1` |
### 认证方式
所有受保护的接口需在请求头携带 JWT Token
受保护的接口需在请求头携带 JWT Token
```http
Authorization: Bearer <access_token>
```
Token 通过登录接口获取。
### 响应格式
```json
// 成功响应
{
"code": 200,
"message": "success",
"data": { ... }
}
成功响应直接返回 Pydantic 模型序列化 JSON无外层包装
// 错误响应
{
"code": 40001,
"message": "用户不存在",
"data": null
}
```json
{"access_token": "eyJhbGciOiJIUzI1NiIs...", "token_type": "bearer"}
```
### 通用错误码
错误响应:
| 状态码 | 错误码 | 说明 |
|:------|:-------|:------|
| 200 | 200 | 请求成功 |
| 400 | 40000 | 请求参数错误 |
| 401 | 40100 | 未认证Token 缺失或过期) |
| 403 | 40300 | 无权限访问 |
| 404 | 40400 | 资源不存在 |
| 500 | 50000 | 服务器内部错误 |
```json
{"error": "ERROR_CODE", "message": "human readable message"}
```
### ID 格式
所有主键Agent / Workflow / Execution / Goal / Task 等)均为 UUID 字符串36 字符),不是整数。
---
## 二、用户模块
## 认证模块 `/api/v1/auth`
### 2.1 用户注册
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/auth/register` | 用户注册email + password + nickname |
| POST | `/auth/login` | 登录,返回 access_token |
| GET | `/auth/me` | 获取当前用户信息(需 Bearer Token |
注册新用户账号。
### 登录
```http
POST /api/v1/users/register
POST /api/v1/auth/login
Content-Type: application/x-www-form-urlencoded
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `email` | string | | 邮箱地址 |
| `password` | string | | 密码(至少 8 位,含字母和数字) |
| `nickname` | string | ❌ | 用户昵称 |
|------|------|------|------|
| username | string | | 用户名 |
| password | string | | 密码 |
**请求示例:**
响应:
```json
{
"email": "user@example.com",
"password": "Abc12345",
"nickname": "张三"
}
```
**响应示例:**
```json
{
"code": 200,
"message": "success",
"data": {
"id": 1,
"email": "user@example.com",
"nickname": "张三",
"created_at": "2026-05-10T08:00:00Z"
}
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer"
}
```
---
### 2.2 用户登录
## 智能体模块 `/api/v1/agents`
获取 JWT 访问令牌。
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/agents` | 智能体列表(分页、搜索、状态筛选) |
| POST | `/agents` | 创建智能体(含 workflow_config |
| GET | `/agents/{agent_id}` | 智能体详情 |
| PUT | `/agents/{agent_id}` | 更新智能体(自动递增版本号) |
| DELETE | `/agents/{agent_id}` | 删除智能体(所有者或管理员) |
| POST | `/agents/{agent_id}/deploy` | 发布智能体 |
| POST | `/agents/{agent_id}/stop` | 停止智能体 |
| POST | `/agents/{agent_id}/duplicate` | 深拷贝智能体 |
| GET | `/agents/{agent_id}/export` | 导出为 JSON |
| POST | `/agents/import` | 从 JSON 导入 |
| POST | `/agents/{agent_id}/execute` | 执行智能体agent/sequential/pipeline/debate/graph |
| POST | `/agents/{agent_id}/create-main-agent` | 升级为主 Agent |
| GET | `/agents/{agent_id}/preview-chat-history` | 设计预览对话历史 |
```http
POST /api/v1/users/login
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `email` | string | ✅ | 邮箱地址 |
| `password` | string | ✅ | 密码 |
**响应示例:**
```json
{
"code": 200,
"message": "success",
"data": {
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"refresh_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer",
"expires_in": 1800
}
}
```
---
### 2.3 Token 刷新
使用 Refresh Token 获取新的 Access Token。
```http
POST /api/v1/users/refresh
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `refresh_token` | string | ✅ | 登录时获取的 Refresh Token |
**响应示例:**
```json
{
"code": 200,
"message": "success",
"data": {
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"refresh_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer",
"expires_in": 1800
}
}
```
---
### 2.4 获取当前用户信息
获取已登录用户的个人信息。
```http
GET /api/v1/users/me
```
**请求头:**
```http
Authorization: Bearer <access_token>
```
**响应示例:**
```json
{
"code": 200,
"message": "success",
"data": {
"id": 1,
"email": "user@example.com",
"nickname": "张三",
"avatar": "https://...",
"created_at": "2026-05-10T08:00:00Z"
}
}
```
---
## 三、智能体模块
### 3.1 创建智能体
创建一个新的 AI 智能体。
### 创建智能体
```http
POST /api/v1/agents
Authorization: Bearer <token>
Content-Type: application/json
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `name` | string | ✅ | 智能体名称 |
| `description` | string | ❌ | 智能体描述 |
| `prompt_template` | string | ✅ | 系统提示词模板 |
| `model_config` | object | ✅ | LLM 模型配置 |
**`model_config` 对象:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `provider` | string | ✅ | 模型提供商:`openai` / `azure` / `local` |
| `model_name` | string | ✅ | 模型名称:`gpt-4o` / `gpt-3.5-turbo` |
| `temperature` | number | ❌ | 生成温度(默认 0.7 |
| `max_tokens` | integer | ❌ | 最大 Token 数(默认 2048 |
**请求示例:**
```json
{
"name": "智能客服助手",
"description": "用于解答常见产品问题",
"prompt_template": "你是一个专业的客服助手...",
"name": "智能体名称",
"description": "描述",
"system_prompt": "系统提示词...",
"model_config": {
"provider": "openai",
"model_name": "gpt-4o",
"temperature": 0.5,
"max_tokens": 2048
"model_name": "deepseek-v4-pro",
"temperature": 0.8
},
"iteration_limit": 15,
"include_tools": [],
"workflow_config": {
"nodes": [
{"id": "start", "type": "start", "label": "开始"},
{"id": "agent", "type": "agent", "label": "智能体"},
{"id": "end", "type": "end", "label": "结束"}
],
"edges": [
{"source": "start", "target": "agent"},
{"source": "agent", "target": "end"}
]
}
}
```
---
### 3.2 获取智能体列表
## 智能体对话 `/api/v1/agent-chat`
获取当前用户的所有智能体。
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/agent-chat/{agent_id}` | 与指定智能体对话 |
| POST | `/agent-chat/{agent_id}/stream` | 流式对话SSE |
| POST | `/agent-chat/bare` | 默认对话(无需 Agent 配置) |
| POST | `/agent-chat/bare/stream` | 默认流式对话SSE |
| POST | `/agent-chat/orchestrate` | 多 Agent 编排route/sequential/debate |
| POST | `/agent-chat/orchestrate/graph` | 图模式多 Agent 编排 |
### 对话请求
```http
GET /api/v1/agents?page=1&page_size=20
POST /api/v1/agent-chat/{agent_id}
Authorization: Bearer <token>
Content-Type: application/json
```
**查询参数:**
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|:-----|:-----|:-----|:-------|:------|
| `page` | integer | ❌ | 1 | 页码 |
| `page_size` | integer | ❌ | 20 | 每页条数(最大 100 |
---
### 3.3 获取智能体详情
```http
GET /api/v1/agents/{agent_id}
```
**路径参数:**
| 参数 | 类型 | 说明 |
|:-----|:-----|:------|
| `agent_id` | integer | 智能体 ID |
---
### 3.4 更新智能体
```http
PUT /api/v1/agents/{agent_id}
```
**请求体:**(部分更新)
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `name` | string | | 智能体名称 |
| `description` | string | | 智能体描述 |
| `prompt_template` | string | | 系统提示词 |
| `model_config` | object | ❌ | 模型配置 |
|------|------|------|------|
| message | string | | 用户消息 |
| session_id | string | | 会话 ID用于恢复上下文 |
| streamlined | boolean | | 是否精简输出 |
### 流式对话 SSE 事件
| 事件 | 说明 |
|------|------|
| token | 输出 token 增量 |
| tool_call | 工具调用开始 |
| tool_result | 工具返回结果 |
| done | 对话完成(含 token_usage |
| error | 错误信息 |
### 对话分支
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/agent-chat/branches` | 创建分支 |
| GET | `/agent-chat/branches` | 列出分支 |
| GET | `/agent-chat/branches/{branch_id}` | 分支详情 |
| DELETE | `/agent-chat/branches/{branch_id}` | 删除分支 |
| POST | `/agent-chat/branches/{branch_id}/resume` | 从分支恢复 |
---
### 3.5 删除智能体
## 工作流模块 `/api/v1/workflows`
```http
DELETE /api/v1/agents/{agent_id}
```
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/workflows` | 工作流列表 |
| POST | `/workflows` | 创建工作流 |
| GET | `/workflows/{workflow_id}` | 工作流详情 |
| PUT | `/workflows/{workflow_id}` | 更新工作流 |
| DELETE | `/workflows/{workflow_id}` | 删除工作流 |
| POST | `/workflows/{workflow_id}/execute` | 执行工作流 |
| POST | `/workflows/validate` | 验证工作流配置 |
| GET | `/workflows/{workflow_id}/export` | 导出 JSON |
| POST | `/workflows/import` | 导入 JSON |
| GET | `/workflows/{workflow_id}/versions` | 版本历史 |
| GET | `/workflows/{workflow_id}/versions/{v}` | 特定版本详情 |
| POST | `/workflows/{workflow_id}/versions/{v}/rollback` | 回滚到指定版本 |
### 工作流模板
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/workflows/templates` | 模板列表 |
| GET | `/workflows/templates/{id}` | 模板详情 |
| POST | `/workflows/templates/{id}/create` | 从模板创建 |
---
## 四、对话模块
## 知识库模块 `/api/v1/knowledge-bases`
### 4.1 创建对话会话
```http
POST /api/v1/conversations
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `agent_id` | integer | ✅ | 关联的智能体 ID |
| `title` | string | ❌ | 会话标题(可选) |
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/knowledge-bases` | 知识库列表 |
| POST | `/knowledge-bases` | 创建知识库 |
| GET | `/knowledge-bases/{kb_id}` | 知识库详情 |
| DELETE | `/knowledge-bases/{kb_id}` | 删除知识库 |
| GET | `/knowledge-bases/{kb_id}/documents` | 文档列表 |
| POST | `/knowledge-bases/{kb_id}/documents` | 添加文档 |
| DELETE | `/knowledge-bases/{kb_id}/documents/{doc_id}` | 删除文档 |
| POST | `/knowledge-bases/{kb_id}/search` | 语义搜索 |
| POST | `/knowledge-bases/{kb_id}/rag` | RAG 检索增强生成 |
---
### 4.2 发送消息
## 工具模块 `/api/v1/tools`
向指定对话发送消息支持流式响应SSE
```http
POST /api/v1/conversations/{conversation_id}/messages
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `content` | string | ✅ | 用户消息内容 |
**流式响应SSE**
```
data: {"type": "token", "content": "你好"}
data: {"type": "token", "content": ""}
data: {"type": "token", "content": "请问有什么可以帮您?"}
data: {"type": "done"}
data: {"type": "error", "content": "..."}
```
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/tools` | 工具列表 |
| GET | `/tools/categories` | 工具分类 |
| GET | `/tools/builtin` | 内置工具列表56个 |
| GET | `/tools/{tool_id}` | 工具详情 |
| POST | `/tools` | 创建自定义工具 |
| PUT | `/tools/{tool_id}` | 更新自定义工具 |
| DELETE | `/tools/{tool_id}` | 删除自定义工具 |
| POST | `/tools/test/http` | 测试 HTTP 工具 |
| POST | `/tools/test/code` | 测试代码工具 |
| POST | `/tools/{tool_id}/use` | 调用工具 |
---
### 4.3 获取对话历史
## 目标与任务
```http
GET /api/v1/conversations/{conversation_id}/messages?page=1&page_size=50
```
### 目标 `/api/v1/goals`
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/goals` | 目标列表 |
| POST | `/goals` | 创建目标 |
| GET | `/goals/{goal_id}` | 目标详情 |
| PUT | `/goals/{goal_id}` | 更新目标 |
| DELETE | `/goals/{goal_id}` | 删除目标 |
| POST | `/goals/{goal_id}/start` | 启动 |
| POST | `/goals/{goal_id}/pause` | 暂停 |
| POST | `/goals/{goal_id}/resume` | 恢复 |
| POST | `/goals/{goal_id}/decompose` | 分解为任务 |
| POST | `/goals/{goal_id}/replan` | 重新规划 |
| POST | `/goals/{goal_id}/execute-async` | 异步执行 |
| POST | `/goals/{goal_id}/interact` | 对话交互 |
| GET | `/goals/{goal_id}/tasks` | 任务树 |
### 任务 `/api/v1/tasks`
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/tasks` | 任务列表 |
| POST | `/tasks` | 创建任务 |
| GET | `/tasks/{task_id}` | 任务详情 |
| PUT | `/tasks/{task_id}` | 更新任务 |
| DELETE | `/tasks/{task_id}` | 删除任务 |
| POST | `/tasks/{task_id}/execute` | 执行 |
| POST | `/tasks/{task_id}/retry` | 重试 |
| POST | `/tasks/{task_id}/claim` | 认领 |
| POST | `/tasks/{task_id}/release` | 释放 |
| POST | `/tasks/{task_id}/complete` | 完成 |
| POST | `/tasks/{task_id}/fail` | 标记失败 |
| POST | `/tasks/{task_id}/approve` | 审批通过 |
| POST | `/tasks/{task_id}/reject` | 审批拒绝 |
| GET | `/tasks/{task_id}/blockers` | 阻塞项 |
| GET | `/tasks/available/{goal_id}` | 可接任务 |
---
## 五、知识库模块
## 智能体蜂群 `/api/v1/swarm`
### 5.1 上传文档
```http
POST /api/v1/knowledge/documents/upload
```
**请求格式:** `multipart/form-data`
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `file` | file | ✅ | 支持 PDF / TXT / Markdown |
| `agent_id` | integer | ✅ | 关联的智能体 ID |
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/swarm/run` | 运行 Agent 蜂群 |
| POST | `/swarm/run/stream` | 流式运行SSE |
---
### 5.2 文档检索
## 通知模块 `/api/v1/notifications`
```http
POST /api/v1/knowledge/search
```
**请求体:**
| 参数 | 类型 | 必填 | 说明 |
|:-----|:-----|:-----|:------|
| `query` | string | ✅ | 检索关键词 |
| `agent_id` | integer | ✅ | 限定知识库范围 |
| `top_k` | integer | ❌ | 返回条数(默认 5 |
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/notifications` | 通知列表 |
| GET | `/notifications/unread-count` | 未读数量 |
| PUT | `/notifications/{id}/read` | 标记已读 |
| PUT | `/notifications/read-all` | 全部已读 |
| DELETE | `/notifications/{id}` | 删除通知 |
---
## 六、健康检查
## 飞书集成 `/api/v1/feishu`
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/feishu/event` | 飞书事件回调 |
| POST | `/feishu/bind` | 绑定飞书用户 |
| POST | `/feishu/unbind` | 解绑 |
| POST | `/feishu/lookup` | 按 open_id 查找 |
| GET | `/feishu/status` | 集成状态 |
| GET | `/feishu/pending` | 待审批绑定 |
| POST | `/feishu/bind-pending` | 审批绑定 |
| GET | `/feishu/default-agent` | 默认 Agent |
| POST | `/feishu/default-agent` | 设置默认 Agent |
---
## 监控模块
### 系统监控 `/api/v1/monitoring`
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/monitoring/overview` | 系统概览 |
| GET | `/monitoring/executions` | 执行统计 |
| GET | `/monitoring/node-types` | 节点类型用量 |
| GET | `/monitoring/activities` | 最近动态 |
### Agent 监控 `/api/v1/agent-monitoring`
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/agent-monitoring/overview` | Agent 监控概览 |
| GET | `/agent-monitoring/llm-calls` | LLM 调用统计 |
| GET | `/agent-monitoring/agents-stats` | Agent 统计 |
| GET | `/agent-monitoring/tool-usage` | 工具用量 |
| GET | `/agent-monitoring/daily-trend` | 每日趋势 |
---
## 其他模块速查
| 模块 | 前缀 | 端点数 | 说明 |
|------|------|--------|------|
| 执行管理 | `/executions` | 5 | 执行创建/状态/暂停恢复 |
| 执行日志 | `/execution-logs` | 8 | 日志查看/性能分析/调用链 |
| 数据源 | `/data-sources` | 7 | MySQL/PG/Mongo/Redis/CSV/JSON/API/S3 |
| 模型配置 | `/model-configs` | 6 | LLM 模型配置/测试 |
| 协作 | `/collaboration` | 2 | WebSocket 协作 + 用户列表 |
| 权限管理 | `/permissions` | 14 | 角色/权限/用户-资源分配 |
| 告警规则 | `/alert-rules` | 8 | 告警 CRUD/日志/确认 |
| Agent 市场 | `/agent-market` | 14 | 发布/安装/评分/收藏/升级 |
| 模板市场 | `/template-market` | 11 | 模板发布/安装/评分/收藏 |
| 插件 | `/plugins` | 10 | 插件 CRUD/市场/安装/开关 |
| 编排模板 | `/orchestration-templates` | 5 | 多 Agent 编排模板 CRUD |
| 平台模板 | `/platform` | 4 | 场景模板/快捷创建 |
| 节点模板 | `/node-templates` | 6 | 节点配置模板 CRUD |
| 节点测试 | `/nodes` | 1 | 单节点调试 |
| 审批 | `/approval` | 2 | 审批查询/决议 |
| 定时任务 | `/agent-schedules` | 5 | Cron 定时任务/手动触发 |
| Webhook | `/webhooks` | 2 | 按 ID/名称触发工作流 |
| 批量操作 | `/batch` | 3 | 批量执行/导出/删除 |
| 文件上传 | `/uploads` | 2 | 工作区文件上传/预览 |
| 系统日志 | `/system-logs` | 3 | 统一日志/统计/应用日志 |
| 审计日志 | `/audit-logs` | 2 | 操作审计日志/统计 |
| 反馈 | `/feedback` | 3 | 用户反馈分析/记录/负样本 |
| WebSocket | `/ws/executions/{id}` | 1 | 执行状态实时推送 |
---
## 健康检查
```http
GET /api/v1/health
GET /health
```
**响应:**
```json
{
"status": "ok",
"version": "1.0.0",
"timestamp": "2026-05-10T08:00:00Z"
"builtin_tools": {"count": 56, "ready": true}
}
```
---
> 📎 **相关文档**[快速开始指南](./quickstart.md) | [开发指南](./development-guide.md)
> 完整接口文档:启动后端后访问 Swagger UI本地 `http://localhost:8038/docs`Docker `http://localhost:8037/docs`),包含全部 245 个端点的详细参数和在线调试功能。
> 快速验证命令参考 [平台资料](../平台资料.md)