feat: Initial commit with full deployment architecture and scripts

This commit is contained in:
super committed 2026-02-24 16:45:11 +08:00
commit abb3efe114
96 files changed
+16789

No files matched your search

File diff suppressed because it is too large. Load diff
+412
View File
@@ -0,0 +1,412 @@
# OpenClaw Gateway 启动问题排查记录
## 任务背景
用户遇到电脑启动问题,具体是在尝试启动 OpenClaw Gateway 服务时遇到了错误。本文档详细记录了排查过程中的每一步操作及其背后的用途与语法。
## 排查过程
### 步骤 1:检查项目结构并创建文档目录
**执行命令**:
```bash
mkdir -p docs
```
**命令用途**:
创建项目的文档目录,用于存放本次排查的知识记录文件。
**命令详细解释**:
- `mkdir`:创建目录的命令
- `-p`:递归创建目录,如果父目录不存在也会创建
- `docs`:要创建的目录名称
**执行输出**:
```
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $ trae-sandbox 'mkdir -p docs'
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $
```
### 步骤 2:检查 OpenClaw 版本
**执行命令**:
```bash
openclaw --version
```
**命令用途**:
确认 OpenClaw 工具是否正确安装,并查看其版本信息。
**命令详细解释**:
- `openclaw`:OpenClaw 命令行工具
- `--version`:显示版本信息的参数
**执行输出**:
```
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $ trae-sandbox 'openclaw --version'
2026.2.15
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $
```
### 步骤 3:尝试安装 Gateway 服务
**执行命令**:
```bash
openclaw gateway install
```
**命令用途**:
根据错误提示,尝试安装 Gateway 服务。
**命令详细解释**:
- `openclaw`:OpenClaw 命令行工具
- `gateway`:Gateway 服务子命令
- `install`:安装 Gateway 服务的参数
**执行输出**:
```
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $ trae-sandbox 'openclaw gateway install'
🦞 OpenClaw 2026.2.15 (3fe22ea)
Automation with claws: minimal fuss, maximal pinch.
Gateway install failed: Error: EPERM: operation not permitted, open '/Users/teemo/Library/LaunchAgents/ai.openclaw.gateway.plist'
(TraeAI-1) ~/Documents/dev/AI/hxops [1] $
```
### 步骤 4:使用 sudo 权限安装 Gateway 服务
**执行命令**:
```bash
sudo openclaw gateway install
```
**命令用途**:
使用管理员权限尝试安装 Gateway 服务,解决权限不足的问题。
**命令详细解释**:
- `sudo`:以管理员权限执行命令
- `openclaw gateway install`:安装 Gateway 服务的命令
**执行输出**:
```
(TraeAI-1) ~/Documents/dev/AI/hxops [1] $ sudo openclaw gateway install
Password:
🦞 OpenClaw 2026.2.15 (3fe22ea)
Type the command with confidence—nature will provide the stack trace if needed.
Gateway install failed: Error: launchctl bootstrap failed: Bootstrap failed: 125: Domain does not support specified action
(TraeAI-1) ~/Documents/dev/AI/hxops [1] $
```
### 步骤 5:尝试使用 launchctl 手动安装
**执行命令**:
```bash
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/ai.openclaw.gateway.plist
```
**命令用途**:
根据错误提示中的建议,尝试使用 launchctl 命令手动引导启动 Gateway 服务。
**命令详细解释**:
- `launchctl`:macOS 系统的服务管理工具
- `bootstrap`:引导加载服务的命令
- `gui/$UID`:指定用户会话域,$UID 是当前用户的 ID
- `~/Library/LaunchAgents/ai.openclaw.gateway.plist`:服务配置文件的路径
**执行输出**:
```
trae-sandbox 'launchctl bootstrap gui/$UID ~/Library/LaunchAgents/ai.openclaw.gateway.plist'
Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
(TraeAI-1) ~/Documents/dev/AI/hxops [5] $
```
### 步骤 6:检查 LaunchAgents 目录中的 plist 文件
**执行命令**:
```bash
ls -la ~/Library/LaunchAgents/ | grep openclaw
```
**命令用途**:
检查 LaunchAgents 目录中是否存在 OpenClaw 的配置文件。
**命令详细解释**:
- `ls -la`:列出目录中的所有文件,包括隐藏文件和详细信息
- `~/Library/LaunchAgents/`:用户的 LaunchAgents 目录路径
- `|`:管道符号,将前一个命令的输出传递给后一个命令
- `grep openclaw`:筛选包含 "openclaw" 的行
**执行输出**:
```
ls -la ~/Library/LaunchAgents/ | grep openclaw
-rw-r--r--@ 1 teemo staff 1613 Feb 19 23:00 ai.openclaw.gateway.plist
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $
```
### 步骤 7:查看 plist 文件内容
**执行命令**:
```bash
cat ~/Library/LaunchAgents/ai.openclaw.gateway.plist
```
**命令用途**:
查看 Gateway 服务的配置文件内容,了解服务的具体配置。
**命令详细解释**:
- `cat`:查看文件内容的命令
- `~/Library/LaunchAgents/ai.openclaw.gateway.plist`:配置文件的路径
**执行输出**:
```
cat ~/Library/LaunchAgents/ai.openclaw.gateway.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ai.openclaw.gateway</string>
<key>Comment</key>
<string>OpenClaw Gateway (v2026.2.15)</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/bin/node</string>
<string>/opt/homebrew/lib/node_modules/openclaw/dist/index.js</string>
<string>gateway</string>
<string>--port</string>
<string>18789</string>
</array>
<key>StandardOutPath</key>
<string>/Users/teemo/.openclaw/logs/gateway.log</string>
<key>StandardErrorPath</key>
<string>/Users/teemo/.openclaw/logs/gateway.err.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>HOME</key>
<string>/Users/teemo</string>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin</string>
<key>OPENCLAW_GATEWAY_PORT</key>
<string>18789</string>
<key>OPENCLAW_GATEWAY_TOKEN</key>
<string>a42c3bd4bf35ad4b10fed405e79a5b23c54d9b361933f9fd</string>
<key>OPENCLAW_LAUNCHD_LABEL</key>
<string>ai.openclaw.gateway</string>
<key>OPENCLAW_SYSTEMD_UNIT</key>
<string>openclaw-gateway.service</string>
<key>OPENCLAW_SERVICE_MARKER</key>
<string>openclaw</string>
<key>OPENCLAW_SERVICE_KIND</key>
<string>gateway</string>
<key>OPENCLAW_SERVICE_VERSION</key>
<string>2026.2.15</string>
</dict>
</dict>
</plist>
(TraeAI-1) ~/Documents/dev/AI/hxops [0] $
```
## 问题分析
### 问题现象
用户在尝试启动 OpenClaw Gateway 服务时遇到了以下错误:
```
Gateway service not loaded.
Start with: openclaw gateway install
Start with: openclaw gateway
Start with: launchctl bootstrap gui/$UID ~/Library/LaunchAgents/ai.openclaw.gateway.plist
```
### 排查结果
1. OpenClaw 工具已正确安装,版本为 2026.2.15
2. 尝试安装 Gateway 服务时遇到权限问题
3. 使用 sudo 权限安装时遇到 Bootstrap 失败错误
4. 尝试手动使用 launchctl 引导时遇到 I/O 错误
5. LaunchAgents 目录中已存在 ai.openclaw.gateway.plist 文件
6. plist 文件配置看起来正常,包含了正确的服务配置
### 可能的问题原因
1. 权限问题:虽然使用了 sudo,但可能仍存在权限不足的问题
2. 服务冲突:可能有其他服务占用了相同的端口或资源
3. 配置文件问题:plist 文件可能存在隐藏的格式问题
4. 系统兼容性问题:当前系统版本可能与 OpenClaw 版本不兼容
## 解决方案
### 日志分析
通过检查日志文件 `~/.openclaw/logs/gateway.err.log`,发现了具体的错误原因:
```
2026-02-17T18:36:59.818+08:00 Gateway start blocked: set gateway.mode=local (current: unset) or pass --allow-unconfigured.
2026-02-17T18:36:59.821+08:00 Config write audit: /Users/teemo/.openclaw/logs/config-audit.jsonl
```
**错误原因**:Gateway 服务启动被阻止,因为缺少 `gateway.mode` 配置,当前状态为 "unset"。
### 建议尝试的步骤
1. **设置 gateway.mode 配置**:
```bash
openclaw config set gateway.mode local
```
2. **或者使用 --allow-unconfigured 参数启动**:
```bash
openclaw gateway start --allow-unconfigured
```
3. **检查端口占用**:
```bash
lsof -i :18789
```
4. **检查日志文件**:
```bash
ls -la ~/.openclaw/logs/
cat ~/.openclaw/logs/gateway.err.log
```
5. **尝试重新创建 plist 文件**:
```bash
rm ~/Library/LaunchAgents/ai.openclaw.gateway.plist
openclaw gateway install
```
6. **检查 Node.js 安装**:
```bash
node --version
which node
```
7. **尝试直接启动 Gateway**:
```bash
openclaw gateway
```
## 总结
本次排查过程中,我们:
1. 创建了文档目录用于存放知识记录
2. 确认了 OpenClaw 工具的版本
3. 尝试了多种安装 Gateway 服务的方法
4. 检查了服务配置文件的状态和内容
5. 分析了可能的问题原因
6. 检查了日志文件,发现了具体的错误原因
7. 提供了详细的解决方案
通过检查日志文件,我们发现了 Gateway 服务启动失败的具体原因:缺少 `gateway.mode` 配置。这是一个关键的配置项,需要设置为 "local" 或者在启动时使用 `--allow-unconfigured` 参数。
### 关键知识点
1. **macOS 服务管理**:
- `launchctl` 是 macOS 系统的服务管理工具
- 服务配置文件通常存放在 `~/Library/LaunchAgents/` 目录
2. **OpenClaw 服务管理**:
- `openclaw gateway install` 用于安装 Gateway 服务
- `openclaw gateway start` 用于启动 Gateway 服务
- `openclaw config set` 用于设置配置项
3. **OpenClaw 配置管理**:
- `gateway.mode` 是 Gateway 服务的必要配置项
- 可以设置为 "local" 模式
- 或者使用 `--allow-unconfigured` 参数跳过配置检查
4. **权限管理**:
- `sudo` 命令可以临时获取管理员权限
- 某些系统操作需要管理员权限才能执行
5. **排错技巧**:
- 查看错误信息中的建议
- 检查配置文件的内容和权限
- 查看日志文件获取详细错误信息
- 检查端口占用情况
### 实际操作与解决方案
### 执行的操作步骤
1. **设置 gateway.mode 配置**(需要 sudo 权限):
```bash
sudo openclaw config set gateway.mode local
```
**执行输出**:
```
Config overwrite: /Users/teemo/.openclaw/openclaw.json (sha256 7f0b8d21cb7a93d63d1c88ac5283755466a910ca4e19d1437daf8db3059b1a84 -> b04724b773c7fa9308dd31b5e4fcb87de2f5dcffc4ed3dcb7eeb3b2e8ff4ce2f, backup=/Users/teemo/.openclaw/openclaw.json.bak)
Updated gateway.mode. Restart the gateway to apply.
```
2. **修复配置文件权限**:
```bash
sudo chown teemo:staff /Users/teemo/.openclaw/openclaw.json
```
3. **启动 Gateway 服务**:
```bash
openclaw gateway
```
**执行输出**:
```
15:07:33 [canvas] host mounted at http://127.0.0.1:18789/__openclaw__/canvas/ (root /Users/teemo/.openclaw/canvas)
15:07:34 [heartbeat] started
15:07:34 [gateway] agent model: vllm//data/ai-platform/models/Qwen2.5-72B-Instruct-GPTQ-Int4
15:07:34 [gateway] listening on ws://127.0.0.1:18789 (PID 95771)
15:07:34 [gateway] listening on ws://[::1]:18789
15:07:34 [gateway] log file: /tmp/openclaw/openclaw-2026-02-19.log
15:07:34 [browser/service] Browser control service ready (profiles=2)
```
### 最终解决方案
根据实际操作,解决 OpenClaw Gateway 服务启动问题的完整步骤是:
1. **设置 gateway.mode 配置**(需要 sudo 权限):
```bash
sudo openclaw config set gateway.mode local
```
2. **修复配置文件权限**:
```bash
sudo chown teemo:staff /Users/teemo/.openclaw/openclaw.json
```
3. **启动 Gateway 服务**:
```bash
openclaw gateway
```
**替代方案**:如果不想修改配置,也可以使用 `--allow-unconfigured` 参数启动:
```bash
openclaw gateway start --allow-unconfigured
```
### 服务状态
Gateway 服务已经成功启动,正在监听以下地址:
- ws://127.0.0.1:18789
- ws://[::1]:18789
服务运行正常,包含以下组件:
- Canvas 服务
- 心跳服务
- 浏览器控制服务
**注意**:虽然有一个关于 cron 服务的权限错误和版本更新提示,但这些都不影响 Gateway 服务的核心功能。
+124
View File
@@ -0,0 +1,124 @@
# Enterprise AI Gateway Project Context
## 1. 🚀 项目一句话概述
这是一个企业级 AI 聚合网关平台,旨在统一管理和分发各类 AI Agent(如 Dify 应用),为企业内部提供统一的 AI 对话入口、权限控制(RBAC)、审计日志及个性化工作台功能。
## 2. 🛠 核心技术栈 (Tech Stack)
### 前端 (Frontend)
- **框架**: Vue 3 (Composition API) + TypeScript
- **构建工具**: Vite
- **UI 框架**: TailwindCSS + Lucide Icons
- **路由**: Vue Router 4
- **国际化**: Vue I18n
- **网络请求**: 原生 Fetch API (用于 SSE 流式传输) + Axios (常规请求)
### 后端 (Backend)
- **框架**: Flask (Python 3.12)
- **ORM**: SQLAlchemy (Flask-SQLAlchemy)
- **数据库迁移**: Alembic (Flask-Migrate)
- **服务器**: Gunicorn (生产) / Werkzeug (开发)
- **依赖库**: `requests` (HTTP调用), `pydantic` (数据验证)
### 基础设施 (Infrastructure)
- **数据库**: PostgreSQL
- **缓存**: Redis (用于 Session 或 任务队列,可选)
- **容器化**: Docker + Docker Compose
## 3. 📂 核心目录结构 (Directory Structure)
```text
hxops/
├── backend/ # Flask 后端根目录
│ ├── app/
│ │ ├── api/ # API 路由蓝图 (Blueprints)
│ │ │ ├── admin.py # 管理员接口 (Agent管理, 部门管理)
│ │ │ ├── chat.py # 对话接口 (SSE流式代理)
│ │ │ ├── user.py # 用户端接口 (首页, 收藏, 常用问题)
│ │ │ └── ...
│ │ ├── services/ # 业务逻辑层
│ │ │ └── dify.py # Dify API 集成服务 (核心)
│ │ ├── models.py # 数据库模型定义
│ │ ├── decorators.py # 权限控制装饰器
│ │ └── __init__.py # App 工厂函数
│ ├── migrations/ # 数据库迁移脚本
│ ├── run.py # 启动入口 (端口 5002)
│ └── requirements.txt # Python 依赖
├── frontend/ # Vue 前端根目录
│ ├── src/
│ │ ├── composables/ # 组合式函数
│ │ │ └── useChat.ts # 聊天逻辑封装 (SSE 处理)
│ │ ├── views/ # 页面组件
│ │ │ ├── AdminView.vue # 管理后台
│ │ │ ├── ChatView.vue # 对话界面
│ │ │ └── WorkspaceView.vue # 用户工作台
│ │ ├── router/ # 路由配置
│ │ └── main.ts # 入口文件
│ └── vite.config.ts # Vite 配置 (含代理设置)
├── docs/ # 项目文档
│ └── k_Enterprise_AI_Gateway_Architecture_*.md # 架构文档
└── scripts/ # 运维与测试脚本
├── mock_dify_server.py # 模拟 Dify 服务
└── test_chat_flow.py # 对话流程测试脚本
```
## 4. 🔄 核心业务流与架构设计
### 4.1 前后端交互规范
- **API 前缀**: `/api` (如 `/api/chat/completions`, `/api/admin/agents`)
- **跨域处理**: 开发环境通过 Vite Proxy (`frontend/vite.config.ts`) 转发 `/api` 请求到后端端口 (5002)。
- **响应格式**: 标准 RESTful JSON,流式接口使用 Server-Sent Events (SSE)。
### 4.2 鉴权流程 (Simplified RBAC)
- **当前机制**: 基于 Header 的简易认证 (开发/网关模式)。
- **前端**: `useChat.ts` 或请求拦截器中自动注入 `X-User-Id`, `X-Username`, `X-Role` 头。
- **后端**: `app/decorators.py` 中的 `_get_current_user` 解析 Header,自动查找或创建(Lazy Create)数据库用户对象,并注入到 Flask全局变量 `g` 中。
### 4.3 核心链路:AI 对话流
1. **用户提问**: 前端 `ChatView` 调用 `useChat.sendMessage`,发起 `POST /api/chat/completions`。
2. **权限校验**: 后端 `chat.py` 使用 `@require_agent_permission` 校验用户是否有权访问该 Agent。
3. **代理转发**: 调用 `DifyService.stream_chat`,使用 Agent 配置的 API Key 向 Dify 服务发起请求。
4. **流式响应**: Dify 返回 SSE 数据流,后端通过 Python生成器 (`yield`) 逐块透传给前端。
5. **审计记录**: 对话结束 (`message_end` 事件) 时,后端自动将完整问答、Token 消耗、耗时写入 `chat_logs` 表。
## 5. 🗄️ 核心数据模型 (Database Models)
- **User**: 用户表 (id, username, role, department)。
- **Agent**: AI 应用配置表 (id, name, dify_app_id, dify_api_key, api_base_url)。
- *关键字段*: `type` (api/iframe), `is_active`。
- **user_agent_permissions**: 多对多关联表,控制 User 对 Agent 的访问权限。
- **ChatLog**: 审计日志表 (user_id, agent_id, query, answer, tokens_used, conversation_id)。
- **UserHomepageConfig**: 用户个性化配置 (收藏的 Agent, 首页布局)。
## 6. 📝 待办事项与技术债 (TODOs)
- [ ] **鉴权升级**: 目前使用 Header 透传用户信息,生产环境需对接 SSO (OAuth2/OIDC) 并发放真实 JWT。
- [ ] **网络连通性**: Dify 真实服务 (`192.168.100.230`) 目前不可达,暂时使用了本地 Mock Server (`127.0.0.1:5001`)。需解决网络/防火墙问题。
- [ ] **端口规范**: 后端已从 5000 迁至 5002 以避开 macOS 端口冲突,需确保部署脚本同步更新。
- [ ] **前端重构**: `fetch` 与 `axios` 混用,建议统一封装请求库。
## 7. 🤖 【重要】AI 接手专属 Context 提示词
```text
你好,我需要你帮我开发/调试一个“企业级 AI 聚合网关”项目。
以下是项目的基础上下文,请阅读并记住,后续你的所有代码生成都必须遵循以下架构和规范:
1. **技术栈**:
- 前端: Vue 3 + TypeScript + TailwindCSS + Vite。
- 后端: Flask + SQLAlchemy + PostgreSQL (端口 5002)。
2. **核心业务**:
- 这是一个 BFF (Backend for Frontend) 系统,代理用户请求转发给底层 LLM 服务 (如 Dify)。
- 核心功能是 SSE 流式对话、Agent 权限管理(RBAC)、审计日志记录。
3. **关键代码逻辑**:
- 对话逻辑: 前端 `useChat.ts` -> 后端 `api/chat.py` -> `services/dify.py`。
- 鉴权逻辑: 依赖 `X-User-Id` 请求头,通过 `decorators.py` 实现用户自动注入。
4. **数据模型**:
- `Agent` 表存储 Dify API Key 和 Base URL。
- `ChatLog` 表记录所有对话详情。
5. **注意事项**:
- 所有的 API 请求都必须通过 `/api` 前缀。
- 开发环境后端运行在 5002 端口 (5000 被 macOS 占用)。
- 当前连接的是本地 Mock Dify Server (5001),因远程服务网络不通。
- 修改数据库模型后必须生成并应用 Alembic 迁移。
```
+34
View File
@@ -0,0 +1,34 @@
# 企业 AI 网关功能测试矩阵 (Enterprise AI Gateway Test Matrix)
本文档旨在对系统的所有功能进行提炼,并规范化测试代码的撰写要求。
## 1. 测试代码撰写通用要求
所有测试代码必须遵循以下三层注释标准:
1. **文件层注释**:注明文件用途、引用的模块、逻辑关系。
2. **函数层注释**:在函数名上方说明其用途、核心逻辑与实现方式,对传参与输出进行说明。
3. **代码层注释**:在函数内部,对关键代码行进行注释,解释逻辑意图。
---
## 2. 功能测试矩阵
| 功能分类 | 功能子项 | 功能描述 | 测试代码撰写要求 | 测试输入数据 (示例) | 预期测试输出 (示例) |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **用户认证** | **用户注册** | 新用户通过填写表单提交注册申请,状态默认为 `pending`。需校验手机号、工号唯一性。 | 模拟 POST 请求 `/api/user/register`,验证必填字段缺失、字段重复等异常场景。 | `{ "username": "test_user", "password": "pwd", "email": "test@e.com", "real_name": "张三", "employee_id": "EMP001", "phone_number": "13800000000" }` | HTTP 201 Created; 返回 `{"id": 1}`;数据库中状态为 `pending`。 |
| **用户认证** | **用户登录** | 用户通过用户名/密码登录,系统校验状态是否为 `active`。 | 模拟 POST `/api/user/login`。测试:1. 正常登录; 2. 密码错误; 3. 未审批用户登录(403)。 | `{ "username": "test_user", "password": "pwd" }` | HTTP 200 OK; 返回 `{"id": 1, "username": "test_user", "role": "user"}`。 |
| **用户管理** | **审批用户** | 管理员将注册申请状态的用户置为 `active`。 | 模拟 Admin 身份 POST `/api/admin/users/{id}/approve`。 | Header: `X-Role: admin`, URL Path: `/api/admin/users/1/approve` | HTTP 200 OK; 数据库中该用户 `status` 变更为 `active`。 |
| **用户管理** | **拒绝用户** | 管理员拒绝注册申请,将用户状态置为 `rejected`。 | 模拟 Admin 身份 POST `/api/admin/users/{id}/reject`。验证非 Admin 调用应返回 403。 | Header: `X-Role: admin`, URL Path: `/api/admin/users/1/reject` | HTTP 200 OK; 数据库中该用户 `status` 变更为 `rejected`。 |
| **用户管理** | **用户列表** | 管理员查看所有用户,支持过滤状态。 | 模拟 Admin GET `/api/admin/users`。 | Header: `X-Role: admin` | HTTP 200 OK; 返回用户列表 JSON 数组。 |
| **Agent 管理** | **创建 Agent** | 管理员接入新的 Dify 应用 (API 模式或 Iframe 模式)。 | 模拟 Admin POST `/api/admin/agents`。测试必填项校验。 | `{ "name": "HR Assistant", "type": "api", "dify_app_id": "uuid", "dify_api_key": "sk-..." }` | HTTP 201 Created; 返回 `{"id": 1}`。 |
| **Agent 管理** | **更新 Agent** | 管理员修改 Agent 配置 (如名称、描述、API Key)。 | 模拟 Admin PUT `/api/admin/agents/{id}`。 | `{ "description": "Updated Desc" }` | HTTP 200 OK; 数据库中字段更新成功。 |
| **Agent 管理** | **删除 Agent** | 管理员删除不再使用的 Agent。 | 模拟 Admin DELETE `/api/admin/agents/{id}`。测试删除不存在的 ID (404)。 | URL Path: `/api/admin/agents/1` | HTTP 200 OK; 再次查询该 ID 返回 404 或空。 |
| **权限管理** | **分配权限** | 管理员为用户分配可访问的 Agent (多对多关系)。 | 模拟 Admin PUT `/api/admin/users/{id}` (带 `agent_ids`)。 | `{ "agent_ids": [1, 2] }` | HTTP 200 OK; 数据库 `user_agent_permissions` 表新增关联记录。 |
| **权限管理** | **权限验证** | 用户访问 Agent 列表或对话接口时,系统校验其是否有权访问该 Agent。 | 1. 模拟 User 登录; 2. 访问未授权 Agent 的对话接口。 | Header: `X-Username: user1`, URL: `/api/chat/messages` (agent_id=99) | HTTP 403 Forbidden; 返回错误提示 "Access denied"。 |
| **权限管理** | **我的应用** | 用户登录后,只展示其拥有权限的 Agent 列表。 | 模拟 User GET `/api/user/home`。 | Header: `X-Username: user1` | HTTP 200 OK; 返回 JSON 中 `agents` 列表仅包含已授权项。 |
| **对话系统** | **发送消息** | 用户向 Agent 发送对话,系统转发至 Dify 并流式返回。 | 模拟 User POST `/api/chat/messages`。需 Mock Dify API 的响应。 | `{ "query": "Hello", "agent_id": 1, "inputs": {}, "response_mode": "streaming" }` | HTTP 200 OK (Stream); 返回流式 SSE 数据或 JSON。 |
| **对话系统** | **对话历史** | 获取用户与 Agent 的历史对话记录。 | 模拟 User GET `/api/chat/history?agent_id=1`。 | Query Param: `agent_id=1` | HTTP 200 OK; 返回 `ChatLog` 列表。 |
| **对话系统** | **反馈评价** | 用户对某次对话结果进行评分 (1-5分) 和评论。 | 模拟 User POST `/api/chat/feedbacks`。 | `{ "message_id": "msg_123", "rating": 5, "content": "Good job" }` | HTTP 200 OK; 数据库 `feedbacks` 表新增记录。 |
| **个性化** | **常用问题** | 用户添加/删除自定义的常用问题 (Quick Questions)。 | 模拟 User POST/DELETE `/api/user/quick-questions`。 | `{ "title": "日报", "question": "写一份日报", "agent_id": 1 }` | HTTP 201 Created; 再次查询列表能看到该问题。 |
| **报表监控** | **仪表盘** | 管理员查看系统概览 (Agent 数、用户数、对话总数)。 | 模拟 Admin GET `/api/admin/dashboard` (或各统计接口)。 | Header: `X-Role: admin` | HTTP 200 OK; 返回 `{ "total_agents": 10, "total_users": 50 ... }`。 |
| **报表监控** | **数据导出** | 管理员导出用户、日志等数据为 Excel/CSV。 | 模拟 Admin GET `/api/admin/database/{table_name}`。 | URL Path: `/api/admin/database/chat_logs` | HTTP 200 OK; 返回完整数据列表。 |