chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,368 @@
|
||||
# API 总览
|
||||
|
||||
Academy HTTP API 使用 `/api/v1` 前缀,支持 JWT、API Key 和 Embed token 认证;内置 MCP Server 端点另用端点令牌认证。调用各资源接口前,需按客户端类型选择凭证,并遵循统一的响应、错误处理、分页和流式事件约定。
|
||||
|
||||
## Base URL 与版本前缀
|
||||
|
||||
- 所有业务 API 挂载在 `/api/v1` 前缀下(`router.go` 中 `r.Group("/api/v1")`)。
|
||||
- 健康检查:`GET /health`(无需认证),返回 `{"status":"ok"}`。
|
||||
- Swagger UI:后端的 `/swagger/index.html`,仅在非 `release` 模式(`GIN_MODE != release`)下注册。Docker Compose 默认使用 `release`;启用步骤、后端端口和空白页排查见[开发指南](../06-development/01-dev-guide.md#_6-2-gin-mode-与-swagger)。
|
||||
- 认证之外的特殊路径:`GET|HEAD /r/:token`(短时效资源授权 URL)、`GET /files`(认证后文件代理)、`GET|HEAD /api/v1/files/presigned`(HMAC 签名 URL,无需认证)、`GET /api/v1/files/presigned-preview`(Admin 诊断)。
|
||||
|
||||
```
|
||||
BASE=http://localhost:8080
|
||||
```
|
||||
|
||||
## 认证方式
|
||||
|
||||
认证由 `internal/middleware/auth.go` 的 `Auth` 中间件统一处理,按以下顺序尝试:
|
||||
|
||||
### JWT Bearer(Web 用户) {#_1-jwt-bearer-web-用户}
|
||||
|
||||
```
|
||||
Authorization: Bearer <access_token>
|
||||
```
|
||||
|
||||
- 通过 `POST /api/v1/auth/login`(或 register / OIDC;原生桌面应用使用 auto-setup)获得 `token` 与 `refresh_token`;`POST /api/v1/auth/refresh` 换发新 token。
|
||||
- 可选请求头 `X-Tenant-ID: <tenant_id>`:在 JWT 指向的空间之外切换目标空间(须为该空间活跃成员,或具备 `CanAccessAllTenants` 跨空间超管属性)。畸形或 `0` 值直接返回 400。
|
||||
- 若 JWT 未解析出任何空间且接口非“无空间可用”白名单(如 `/auth/me`、`/me/invitations` 等),返回 409 `{"code":"TENANT_REQUIRED"}`。
|
||||
|
||||
### API Key(机器主体) {#_2-api-key-机器主体}
|
||||
|
||||
```
|
||||
X-API-Key: <api_key>
|
||||
```
|
||||
|
||||
- 空间级(workspace)key:在 `POST /api/v1/tenants/:id/api-keys` 创建,绑定到单一空间;携带 `X-Tenant-ID` 指向其它空间会得到 403。
|
||||
- 平台级(platform)key:在 `POST /api/v1/system/admin/api-keys` 创建,必须携带 `X-Tenant-ID` 选择目标空间(`/system/admin/*`、`/tenants/all|search`、`POST /tenants` 除外),否则返回 409 `TENANT_REQUIRED`。
|
||||
- 授权模型(`internal/middleware/api_key_gate.go`,默认拒绝):每个 `/api/v1` 路由必须显式声明 API key 策略,未声明的路由对任何 key 一律 403。
|
||||
- `full_access` key:空间内全权(等效 Owner 的机器形态)。
|
||||
- 受限(scoped)key:按 capability 放行,并受 `knowledge_base_ids` 白名单约束。Capability 常量见 `internal/types/tenant_api_key.go`:`retrieve`、`ingest`、`chat`、`read_agents`、`manage_kbs`、`manage_agents`、`message_history`、`manage_models`、`manage_mcp_services`、`manage_datasources`、`manage_channels`、`manage_vector_stores`、`manage_storage_backends`、`manage_web_search`、`run_evaluations`、`manage_members`、`manage_spaces`、`manage_tenant_settings`;平台能力:`system_tenants_read/manage`、`system_settings_read/manage`、`system_runtime_read/manage`、`system_audit_read`。
|
||||
- 外部用户主体(可选,按空间 `api-principal-config` 配置):
|
||||
- `direct` 模式:`X-External-User-ID: <外部用户ID>`(≤128 字符)。
|
||||
- `signed_token` 模式:`X-External-User-Token: <HS256 JWT>`,要求 `aud=academy`、`exp`(生存期 ≤24h)、`tenant_id` claim 与目标空间一致、`sub` 为外部用户 ID。
|
||||
|
||||
### Embed publish token(匿名嵌入端) {#_3-embed-publish-token-匿名嵌入端}
|
||||
|
||||
`/api/v1/embed/:channel_id/*` 公开路由使用独立的 `EmbedAuth` 中间件(`internal/middleware/embed_auth.go`):
|
||||
|
||||
```
|
||||
Authorization: Embed <publish_token 或 session_token>
|
||||
```
|
||||
|
||||
- `POST /embed/:channel_id/exchange` 用 publish token 换取短时效 session token;会话级操作还需 `X-Embed-Session: <sig>`(创建会话时返回的签名句柄)。
|
||||
- IM 回调路由(`/api/v1/im/callback/:channel_id`)注册在全局认证中间件之前,使用各 IM 平台自身的签名验证。
|
||||
|
||||
### MCP 端点令牌(内置 MCP Server)
|
||||
|
||||
`/mcp/:endpoint_id`(不带 `/api/v1` 前缀)是空间对外暴露的 MCP Streamable HTTP 端点,由 `internal/middleware/mcp_endpoint_auth.go` 校验:
|
||||
|
||||
```
|
||||
Authorization: Bearer <endpoint_token>
|
||||
```
|
||||
|
||||
令牌在「设置 → 发布集成 → MCP Server」创建端点时一次性展示,可轮换。请求以端点所属空间的机器主体执行,权限限定在端点勾选的工具和知识库范围内。端点管理接口为 `/api/v1/mcp-endpoints`(Viewer+ 读取、Admin+ 变更,API Key 需 `manage_channels`),用法见 [MCP 集成](../03-features/08-mcp.md)。
|
||||
|
||||
### 认证流程图
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A["客户端请求"] --> B{"路径在免认证白名单?<br/>(login/register/oidc/presigned...)"}
|
||||
B -- "是" --> H["直接进入 Handler"]
|
||||
B -- "否" --> C{"Authorization: Bearer <JWT>?"}
|
||||
C -- "有效" --> D{"X-Tenant-ID 请求头?"}
|
||||
D -- "无" --> E["使用 JWT 内 tenant_id"]
|
||||
D -- "有" --> F{"IsTenantAccessible?<br/>(成员/跨空间超管)"}
|
||||
F -- "否" --> G["403 Forbidden"]
|
||||
F -- "是" --> E
|
||||
E --> R{"resolveTenantRole<br/>(成员表 → 超管 → 孤儿空间自愈 → EnableRBAC 兜底)"}
|
||||
R -- "无角色且 RBAC 强制" --> G
|
||||
R -- "得到角色" --> P["注入 tenant/user/role 上下文"]
|
||||
C -- "无/无效" --> K{"X-API-Key?"}
|
||||
K -- "无" --> U["401 Unauthorized"]
|
||||
K -- "有" --> L{"key 类型"}
|
||||
L -- "platform key" --> M{"X-Tenant-ID?"}
|
||||
M -- "缺失且非平台白名单路由" --> V["409 TENANT_REQUIRED"]
|
||||
M -- "有" --> P2["注入平台机器主体 + 目标空间"]
|
||||
L -- "workspace key" --> N{"X-Tenant-ID 与 key 空间一致?"}
|
||||
N -- "不一致" --> G
|
||||
N -- "一致/未携带" --> P3["注入空间机器主体<br/>(可选外部用户主体 Header)"]
|
||||
P --> Q["RBAC 角色守卫 (rbac.go)"]
|
||||
P2 --> S["APIKeyGate: 路由策略<br/>(full_access / capability / KB 白名单, 默认拒绝)"]
|
||||
P3 --> S
|
||||
Q --> H
|
||||
S --> H
|
||||
```
|
||||
|
||||
## 角色与权限模型(RBAC)
|
||||
|
||||
`internal/middleware/rbac.go` + `internal/middleware/access.go`:
|
||||
|
||||
| 角色 | 说明 |
|
||||
| --- | --- |
|
||||
| `owner` | 空间所有者:空间生命周期、API key、成员管理 |
|
||||
| `admin` | 空间管理员:模型/基础设施/渠道等空间级配置 |
|
||||
| `contributor` | 贡献者:可创建 KB/Agent,可修改**自己创建**的资源 |
|
||||
| `viewer` | 只读成员:读取与会话使用 |
|
||||
| SystemAdmin | 平台级管理员(`User.IsSystemAdmin`),独立于空间角色,守卫 `/system/admin/*`,始终强制 |
|
||||
|
||||
- 文档中“Viewer+ / Contributor+ / Admin+ / Owner”表示最低角色要求;“创建者 OR Admin+”对应 `RequireOwnershipOrRole`(Contributor 只能改自己创建的 KB/Agent/内容)。
|
||||
- `cfg.Tenant.EnableRBAC=false` 时角色守卫只记录日志不拦截(rollout fail-open);SystemAdmin 守卫不受此开关影响。
|
||||
- KB 级访问守卫 `KBAccessRead/Write`(`internal/middleware/kb_access.go`):解析“自有 / 组织共享 / 经共享 Agent 可见”三类访问,并把请求上下文的 tenant 重写为 KB 属主空间。
|
||||
- API key 主体会短路 JWT 角色守卫,其真实权限完全由 APIKeyGate(capability + KB 白名单)决定。
|
||||
- 被拒绝的请求会写入审计日志(`middleware.AuditServiceProvider`,1 分钟滑动窗口去重)。
|
||||
|
||||
## 通用响应格式与错误码
|
||||
|
||||
多数 handler 返回:
|
||||
|
||||
```json
|
||||
{ "success": true, "data": { ... } }
|
||||
```
|
||||
|
||||
列表类接口常见附加字段:`total`、`page`、`page_size`。少数例外:`/system/admin/*` 的部分读取接口直接返回原始行/数组(不含包装),`/system/info` 等使用 `{"code":0,"msg":"success","data":...}`。
|
||||
|
||||
错误统一由 `internal/middleware/error_handler.go` 输出(`internal/errors/errors.go` 的 `AppError`):
|
||||
|
||||
```json
|
||||
{ "success": false, "error": { "code": 1003, "message": "...", "details": null } }
|
||||
```
|
||||
|
||||
中间件层(认证/RBAC)直接返回 `{"error": "..."}`(部分带 `"code"` 字符串,如 `TENANT_REQUIRED`)。
|
||||
|
||||
| 错误码 | 含义 | HTTP |
|
||||
| --- | --- | --- |
|
||||
| 1000 | ErrBadRequest 请求错误 | 400 |
|
||||
| 1001 | ErrUnauthorized 未认证 | 401 |
|
||||
| 1002 | ErrForbidden 无权限 | 403 |
|
||||
| 1003 | ErrNotFound 资源不存在 | 404 |
|
||||
| 1004 | ErrMethodNotAllowed | 405 |
|
||||
| 1005 | ErrConflict 冲突 | 409 |
|
||||
| 1006 | ErrTooManyRequests 限流/配额 | 429 |
|
||||
| 1007 | ErrInternalServer 内部错误 | 500 |
|
||||
| 1008 | ErrServiceUnavailable 暂不可用 | 503 |
|
||||
| 1009 | ErrTimeout 超时 | — |
|
||||
| 1010 | ErrValidation 参数校验失败 | 400 |
|
||||
| 2000-2005 | 空间类:不存在/已存在/停用/名称必填/状态非法/自助创建被禁用 | 404/409/403/… |
|
||||
| 2100-2103 | Agent 类:缺思考模型/缺允许工具/迭代次数非法(1-20)/温度非法(0-2) | 400 |
|
||||
| 2200-2201 | VectorStore 绑定非法 / 当前不可用 | 400 |
|
||||
|
||||
另有非编码错误:`types.StorageQuotaExceededError`(存储配额超限)、`types.DuplicateKnowledgeError`(重复文件/URL,上传接口返回 409 且 `data` 携带已存在的 Knowledge)。
|
||||
|
||||
## 分页规范
|
||||
|
||||
`internal/handler/list_pagination.go`:
|
||||
|
||||
| 参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page` | int | 否 | 页码,默认 1,必须 ≥1 |
|
||||
| `page_size` | int | 否 | 每页条数,默认 20,范围 1-100 |
|
||||
|
||||
超范围或非法值返回校验错误(code 1010)。列表响应携带 `total/page/page_size`。部分接口使用游标分页:审计日志(`after_id`+`limit`,响应带 `next_cursor`)、系统运行时任务(`cursor`+`page_size`,响应带 `next_cursor/has_more`)、Wiki index/log(`cursor`+`limit`)。
|
||||
|
||||
## 流式接口协议(SSE)
|
||||
|
||||
聊天类接口(`POST /api/v1/knowledge-chat/:session_id`、`POST /api/v1/agent-chat/:session_id`、`GET /api/v1/sessions/continue-stream/:session_id`,以及 embed 端对应路由)返回 Server-Sent Events:
|
||||
|
||||
```
|
||||
Content-Type: text/event-stream
|
||||
Cache-Control: no-cache
|
||||
Connection: keep-alive
|
||||
X-Accel-Buffering: no
|
||||
```
|
||||
|
||||
每个事件为 `event: message`,`data:` 为 `types.StreamResponse` JSON:
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `id` | string | 请求 ID |
|
||||
| `response_type` | string | `answer` / `references` / `thinking` / `tool_call` / `tool_result` / `command_output` / `reflection` / `session_title` / `agent_query` / `artifacts_pending` / `memory_recalled` / `user_message_injected` / `context_compacted` / `tool_approval_required` / `tool_approval_resolved` / `mcp_oauth_required` / `mcp_oauth_resolved` / `error` / `complete`;技能安装记录流另有 `install_prompt` / `install_output` |
|
||||
| `content` | string | 增量文本 |
|
||||
| `done` | bool | 该类型事件是否结束 |
|
||||
| `knowledge_references` | []SearchResult | `references` 事件携带的引用 |
|
||||
| `tool_calls` | []LLMToolCall | 工具调用事件 |
|
||||
| `data` | object | 事件附加元数据(如工具结果的 `success`、回答的 `truncated`) |
|
||||
| `session_id` / `assistant_message_id` | string | `agent_query` 事件携带 |
|
||||
| `usage` | TokenUsage | `prompt_tokens/completion_tokens/total_tokens/cache_*` |
|
||||
| `finish_reason` | string | 结束原因 |
|
||||
|
||||
`SearchResult`(`knowledge_references`、检索接口的结果)中的 `source_locators` 是该分块在原始文件中的位置,可据此在原文里定位引用(该功能上线前入库的文档为空):
|
||||
|
||||
| 字段 | 适用 `type` | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `type` | — | `pdf` / `docx` / `slide` / `sheet` / `text` / `time` / `section` |
|
||||
| `page`、`bbox` | `pdf` | 页码(从 1 起);`bbox` 为 `[x0,y0,x1,y1]`,相对页面宽高的比例,原点在页面左上角,可缺省 |
|
||||
| `block` | `docx` | 正文中第几个段落或表格(从 1 起) |
|
||||
| `slide` | `slide` | 第几张幻灯片(从 1 起) |
|
||||
| `sheet`、`row_start`、`row_end` | `sheet` | 工作表名(CSV 为空)与行号(从 1 起,同 Excel 行号) |
|
||||
| `start`、`end` | `text` | 原文件文本的字符区间(Unicode 码点) |
|
||||
| `start_ms`、`end_ms` | `time` | 音频时间区间(毫秒) |
|
||||
| `section`、`title` | `section` | EPUB 书脊中的第几项(从 1 起)及章节标题 |
|
||||
| `quote` | 全部 | 被引用的文字,最多 300 字 |
|
||||
|
||||
取值为 0 的数值字段会省略。合并后的检索结果携带所合并分块位置的并集。
|
||||
|
||||
流以 `response_type:"complete"`(`done:true`)终止;出错时以 `response_type:"error"`(`done:true`)终止。工具执行失败以 `tool_result`(`data.success=false`)返回,`error` 只表示整轮失败;回答因输出上限被截断时,`answer` 事件带 `data.truncated=true`。`continue-stream` 采用重放 + 100ms 轮询追增量的续传语义(`?message_id=` 必填)。
|
||||
|
||||
## 文件引用形式(resource_urls)
|
||||
|
||||
回答与检索结果里引用到的图片/附件,默认以内部句柄 `resource://<handle>` 返回,客户端要再调一次带鉴权的 `/files` 代理才能拿到内容。第三方 App 想拿到「拿来即可渲染」的链接时,可以切换成直链模式:
|
||||
|
||||
| 作用范围 | 用法 |
|
||||
| --- | --- |
|
||||
| 单次请求 | 在 URL 上加 `?resource_urls=public` |
|
||||
| 整个部署 | 环境变量 `RESOURCE_URL_MODE=public` |
|
||||
|
||||
取值只有 `handle`(默认)与 `public`,传其它值返回 400。单次请求参数优先于环境变量,所以把部署默认设成 `public` 之后,仍可以用 `?resource_urls=handle` 单独退回。
|
||||
|
||||
支持该参数的接口:`POST /knowledge-chat/{session_id}`、`POST /agent-chat/{session_id}`、`GET /sessions/continue-stream/{session_id}`、`GET /messages/{session_id}/load`、`POST /knowledge-search`、`POST /knowledge-bases/{id}/hybrid-search`(兼容 GET)。改写覆盖答案正文、检索结果 `content` / `image_info`、`knowledge_references`、Agent 执行步骤与工具结果,以及消息上的图片附件;流式回答里跨 chunk 截断的引用会先缓冲再改写,客户端拿到的始终是完整链接。
|
||||
|
||||
使用前需要知道的几件事:
|
||||
|
||||
- **需要具备外链能力**:直链来自存储后端预签名,或 `APP_EXTERNAL_URL` + `/r/<token>`。两者都没有时(如 local 存储且未设 `APP_EXTERNAL_URL`),该引用保持 `resource://` 原样,客户端仍可回退到 `/files`;
|
||||
- **直链是限时匿名可读的**(Academy 签发的 grant 2 小时,MinIO 预签名 24 小时),任何拿到链接的人在过期前都能读取,不要写进日志或转发给不该看的人;
|
||||
- **嵌入渠道不支持**:`/api/v1/embed/...` 下的接口强制 `handle`,访客图片继续走渠道维度的鉴权代理;
|
||||
- **限定知识库的 API Key 用 `public` 会返回 403**:这类 Key 本身就被禁止访问 `/files` 代理,能拿到匿名直链等于绕过同一道限制;
|
||||
- **同一文件的直链在有效期内复用**,重复请求不会反复签发凭证,客户端与 CDN 缓存因此能命中。
|
||||
|
||||
各渠道(Web / IM / 嵌入挂件 / API)分别拿到哪种形式、以及图片加载不出来时怎么排查,见[图片与文件的对外访问](../03-features/21-file-access.md)。
|
||||
|
||||
## 检索接口怎么选 {#retrieval-api}
|
||||
|
||||
对外有两个检索接口,都要求 API Key 有 `retrieve`(或 full)权限,都返回 `SearchResult` 列表。
|
||||
|
||||
**默认用 `POST /knowledge-search`**。它和产品内的问答走同一条检索流程(召回 → rerank → 合并 → 截断),返回的就是页面上问答会用到的那些片段。`POST /knowledge-bases/{id}/hybrid-search` 是更底层的召回接口:默认不做 rerank,分数就是召回分,适合需要看到或控制召回原始结果的场景。
|
||||
|
||||
### 按场景选
|
||||
|
||||
| 我想…… | 用哪个 | 请求体要点 |
|
||||
| --- | --- | --- |
|
||||
| 给自己的 RAG / 智能体拿检索结果,排序和页面问答一致 | `knowledge-search` | `query` + `knowledge_base_ids`,其余不填 |
|
||||
| 同时搜多个知识库,且它们用的 embedding 模型不同 | `knowledge-search` | `knowledge_base_ids` |
|
||||
| 只在某几个文档或标签里搜 | `knowledge-search` | `knowledge_ids` / `tag_ids`;只搜指定文档时不要再传它们所在的 `knowledge_base_ids`,两者取并集 |
|
||||
| 调整返回条数或召回阈值,但仍然要 rerank | `knowledge-search` | `match_count`、`vector_threshold`、`keyword_threshold` |
|
||||
| 换一个 rerank 模型,或改 rerank 阈值 | `knowledge-search` | `rerank.model_id`、`rerank.threshold` |
|
||||
| 不要 rerank,直接拿召回结果 | `knowledge-search` 或 `hybrid-search` | 前者传 `"rerank":{"enabled":false}`;后者不传 `rerank` |
|
||||
| 结果为空,想知道原因 | `knowledge-search` | 看响应里的 `meta.rerank.outcome` |
|
||||
| 已经自己算好了查询向量 | `hybrid-search` | `query_embedding` + `disable_keywords_match: true` |
|
||||
| 评测召回质量:固定一个库、固定参数,看原始召回分 | `hybrid-search` | 不传 `rerank` |
|
||||
| 在上面的评测基础上,再对比加 rerank 后的效果 | `hybrid-search` | 同一请求加上 `rerank` |
|
||||
| 父块、相邻块要作为单独的结果行返回,而不是拼进正文 | `hybrid-search` | 默认如此,`skip_context_enrichment: true` 可关 |
|
||||
|
||||
### 两者的差别
|
||||
|
||||
| | `knowledge-search` | `hybrid-search` |
|
||||
| --- | --- | --- |
|
||||
| rerank | 默认开(用空间配置),`rerank` 对象可以覆盖或关闭 | 默认关,传 `rerank` 对象才开 |
|
||||
| 多知识库 | 可以,embedding 模型可以不同 | `knowledge_base_ids` 可以,但 embedding 模型必须相同,路径上的 `{id}` 也要在其中 |
|
||||
| 预计算向量 | 不支持 | `query_embedding` |
|
||||
| 上下文块 | 合并进结果的 `content` | 作为额外的结果行返回 |
|
||||
| `match_count` 省略时 | 空间配置的 `rerank_top_k`(默认 10) | 50 |
|
||||
| `meta.rerank` | 每次都返回 | 带了 `rerank` 才返回 |
|
||||
|
||||
两个接口的召回参数(`vector_threshold`、`keyword_threshold`、`match_count`、`disable_keywords_match`、`disable_vector_match`)和 `rerank` 对象含义相同;`knowledge-search` 省略的参数沿用空间的检索配置(`GET /tenants/kv/retrieval-config`)。
|
||||
|
||||
### rerank 对象
|
||||
|
||||
两个接口的 `rerank` 字段结构相同:
|
||||
|
||||
| 字段 | 类型 | 默认值 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `enabled` | bool | `true` | 设为 `false` 关闭 rerank,结果保持召回顺序 |
|
||||
| `model_id` | string | 见下文 | rerank 模型 ID(`GET /models` 里 `type` 为 `Rerank` 的模型)。ID 不存在、未激活或不是 rerank 模型时返回 400,不会悄悄换成别的模型 |
|
||||
| `top_k` | int | 接口的返回条数 | rerank 后最多保留几条;负数或超过 200 返回 400 |
|
||||
| `threshold` | float | 空间检索配置里的 `rerank_threshold`(未配置时 0.2) | 模型分数下限;`0` 和负数都是合法值 |
|
||||
|
||||
不传 `model_id` 时依次使用:空间检索配置里的 `rerank_model_id` → 空间里第一个 rerank 模型。都没有时不做 rerank,按召回顺序返回,`meta.rerank.outcome` 为 `no_model`。
|
||||
|
||||
rerank 的过程和问答链路、智能推理的 `search_knowledge` 工具共用同一套实现(`internal/reranking`):
|
||||
|
||||
1. 文本候选送给模型打分的文本 = 文档标题 + 去掉 Markdown 标记的分块正文 + 图片描述与 OCR 文本 + 生成的问题。FAQ 条目不加标题。模型配置了单篇或单次请求长度上限(`max_document_chars` / `max_request_chars`)时,超长的文本从尾部截到上限再送出,先截掉的是附加的图片文本和生成问题;不会因为一条超长文本让整批打分失败。
|
||||
2. 保留分数不低于 `threshold` 的结果。如果一条都没有、而 `threshold` 高于 0.3,就把阈值降到 `max(threshold×0.7, 0.3)` 再筛一次。还是没有的话,最高分不低于 0.15 时只保留这一条,否则返回空列表。
|
||||
3. 排序分 = `0.6×模型分 + 0.3×召回分 + 0.1×来源权重`;结果的 `metadata` 里带 `model_score` 和 `base_score`。
|
||||
4. 用 MMR(λ=0.7)从中挑出 `top_k` 条,降低内容重复。
|
||||
|
||||
图片向量命中还可按原图重排:需要重排模型声明并实现图片输入,普通文本重排仍使用文本信息;视觉对话模型可以接收保留下来的命中图片。图片候选的保留与上下文规则见[检索问答流程](../02-architecture/04-rag-pipeline.md)。
|
||||
|
||||
`hybrid-search` 开启 rerank 时,召回深度至少与 `top_k` 相同,候选池是融合后排名前 `max(top_k, 50)` 的分块,所以 `match_count` 很小也有足够的候选给模型挑。
|
||||
|
||||
rerank 模型加载失败或调用出错时,请求不会失败,而是按召回顺序返回,并在 `meta.rerank` 里写明原因。
|
||||
|
||||
### meta.rerank 诊断
|
||||
|
||||
`knowledge-search` 的响应总是带 `meta.rerank`;`hybrid-search` 只在请求里带了 `rerank` 对象时才带。
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"data": [],
|
||||
"meta": {
|
||||
"rerank": {
|
||||
"applied": true,
|
||||
"outcome": "all_below_threshold",
|
||||
"model_id": "rr-1",
|
||||
"model_source": "tenant",
|
||||
"threshold": 0.3,
|
||||
"effective_threshold": 0.3,
|
||||
"top_score": 0.08,
|
||||
"candidate_count": 24,
|
||||
"result_count": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `applied` | rerank 分数是否决定了返回顺序 |
|
||||
| `outcome` | 见下表 |
|
||||
| `model_id` / `model_source` | 使用的模型,以及它从哪来:`request`(请求指定)、`tenant`(空间配置)、`auto`(自动选择) |
|
||||
| `threshold` / `effective_threshold` | 请求的阈值,以及降级后实际使用的阈值 |
|
||||
| `top_score` | 候选中的最高模型分 |
|
||||
| `candidate_count` / `result_count` | 送去 rerank 的候选数 / 返回条数 |
|
||||
| `error` | `model_error`、`model_unavailable` 时的错误信息 |
|
||||
|
||||
| `outcome` | 含义 |
|
||||
| --- | --- |
|
||||
| `ok` | 有结果达到阈值 |
|
||||
| `threshold_degraded` | 原阈值下没有结果,降低阈值后才有 |
|
||||
| `fallback_top1` | 所有阈值都没过,只保留了最高分的一条 |
|
||||
| `all_below_threshold` | 模型认为没有候选相关,结果为空。可以换个说法重新提问,或者调低 `rerank.threshold` |
|
||||
| `model_error` | 模型调用失败,按召回顺序返回 |
|
||||
| `model_unavailable` | 模型加载失败(凭证、地址等配置问题),按召回顺序返回 |
|
||||
| `no_model` | 空间里没有 rerank 模型,按召回顺序返回 |
|
||||
| `disabled` | 请求里 `rerank.enabled` 为 `false` |
|
||||
| `no_candidates` | 召回阶段没有结果 |
|
||||
|
||||
## 限流说明
|
||||
|
||||
| 面 | 限制 | 来源 |
|
||||
| --- | --- | --- |
|
||||
| 公开分享链接接口(`/auth/invitations/lookup`、`/auth/register-by-invite`) | 每 IP 30 次/分钟(两个端点共享额度),超限 429(code 1006) | `internal/middleware/auth_public_ratelimit.go` |
|
||||
| Embed 公开路由 | 每 (channel, IP) `rate_limit_per_minute`(默认 30)/分钟;channel 级 `rate_limit_per_minute*20`(下限 120)/分钟;channel 级 `rate_limit_per_day`(默认 10000)/天;超限 429 | `internal/middleware/embed_auth.go` |
|
||||
| 反代信任 | 仅信任 `ACADEMY_TRUSTED_PROXIES`(默认回环+内网段)的 `X-Forwarded-For`,防止伪造 IP 绕过限流 | `router.go` `trustedProxies()` |
|
||||
|
||||
其余业务接口无全局限流;自助创建空间等配额类拒绝同样使用 429(code 1006)。
|
||||
|
||||
## API 分组导航
|
||||
|
||||
| 分组 | 文档 | 主要前缀 |
|
||||
| --- | --- | --- |
|
||||
| 认证与用户 | [02-api-auth.md](./02-api-auth.md) | `/auth`、`/me/invitations` |
|
||||
| 租户(空间)与成员 | [02-api-tenant.md](./02-api-tenant.md) | `/tenants` |
|
||||
| 组织与共享 | [02-api-org.md](./02-api-org.md) | `/organizations`、`/shared-*`、`/knowledge-bases/:id/shares`、`/agents/:id/shares` |
|
||||
| 知识库与知识 | [02-api-knowledge.md](./02-api-knowledge.md) | `/knowledge-bases`、`/knowledge`、知识库文件夹、画廊、`/image-attrs/schema` |
|
||||
| 分块与标签 | [02-api-chunks.md](./02-api-chunks.md) | `/chunks`、`/knowledge-bases/:id/tags`、`/chunker/preview` |
|
||||
| FAQ 与 Wiki | [02-api-faq-wiki.md](./02-api-faq-wiki.md) | `/knowledge-bases/:id/faq`、`/faq`、`/knowledgebase/:kb_id/wiki`、`/wiki-search` |
|
||||
| 会话、消息与聊天 | [02-api-chat.md](./02-api-chat.md) | `/sessions`、`/messages`、`/knowledge-chat`、`/agent-chat`、`/knowledge-search` |
|
||||
| 模型与初始化 | [02-api-model-system.md](./02-api-model-system.md) | `/models`、`/initialization`、`/evaluation`、`/academycloud` |
|
||||
| 系统与平台管理 | [02-api-system.md](./02-api-system.md) | `/system`、`/system/admin` |
|
||||
| 基础设施与数据源 | [02-api-infra.md](./02-api-infra.md) | `/vector-stores`、`/storage-backends`、`/web-search-providers`、`/datasource` |
|
||||
| Agent 与 MCP | [02-api-agent-mcp.md](./02-api-agent-mcp.md) | `/agents`、`/mcp-services`、`/agent`、`/user/favorites` |
|
||||
| 内置 MCP Server | [MCP 集成](../03-features/08-mcp.md) | `/mcp-endpoints`、`/mcp/:endpoint_id` |
|
||||
| 本机浏览器 | [本机浏览器](../05-clients/09-local-browser.md) | `/me/browser`、`/local-browser` |
|
||||
| 沙箱、技能与个人变量 | [02-api-sandbox-skills.md](./02-api-sandbox-skills.md) | `/sandbox-configs`、`/skills`、`/me/env-vars` |
|
||||
| 长期记忆 | [02-api-memory.md](./02-api-memory.md) | `/memory`、`/tenants/kv/memory-config` |
|
||||
| IM、Embed 与文件服务 | [02-api-channels.md](./02-api-channels.md) | `/im`、`/im-channels`、`/wechat`、`/embed-channels`、`/embed`、`/files`、`/r/:token` |
|
||||
|
||||
新增配置与个人接口分别见[沙箱、技能与个人变量](02-api-sandbox-skills.md)、[长期记忆](02-api-memory.md);生成文件列表与下载见[会话与聊天](02-api-chat.md)。
|
||||
@@ -0,0 +1,509 @@
|
||||
# API 参考:Agent、MCP 与技能
|
||||
|
||||
管理智能体、MCP 服务及其凭据、技能和资源收藏。智能体的工具范围与调用审批配置通过本组接口维护。
|
||||
|
||||
## Agent(/api/v1/agents)
|
||||
|
||||
读:Viewer+(API key `read_agents`/`manage_agents`/`chat`/full);写:创建者 OR Admin+(API key `manage_agents`/full);内置 Agent(`is_builtin=true`)始终 Admin+。
|
||||
|
||||
### GET /api/v1/agents/placeholders
|
||||
|
||||
用途:提示词占位符定义(须先于 `/:id` 注册)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"all":{...},"system_prompt":{...},"agent_system_prompt":{...},"context_template":{...},"rewrite_system_prompt":{...},"rewrite_prompt":{...},"fallback_prompt":{...}}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/placeholders -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/type-presets
|
||||
|
||||
用途:智能推理 Agent 类型预设(rag-qa / wiki-qa / hybrid / custom 等)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[{type,system_prompt,allowed_tools,kb_compatibility}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/type-presets -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/tools
|
||||
|
||||
用途:获取智能体可选的内置工具目录,权限 Viewer+。响应 `{"success":true,"data":{"tools":[...],"default_tools":[...],"switch_managed_tools":[...]}}`;每项工具带 `requirement.any_of` / `all_of` 知识库能力要求,编辑器据此判断是否可选。
|
||||
|
||||
创建或更新时新增未知工具名返回 400;已存储的未知项保留,响应顶层 `allowed_tools_warnings.kept_unknown` 列出这些名称。由独立开关管理的工具及无后继的旧技能工具从列表移除,`allowed_tools_warnings.dropped` 返回移除项。旧检索工具名会映射为后继工具,重复项去重。
|
||||
|
||||
### POST /api/v1/agents
|
||||
|
||||
用途:创建自定义 Agent。权限:Contributor+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 名称 |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `avatar` | string | 否 | 头像/emoji |
|
||||
| `config` | object | 否 | Agent 配置(`types.CustomAgentConfig`,见下) |
|
||||
|
||||
`config` 主要字段:`agent_mode`(`quick-answer`/`smart-reasoning`,省略时默认为 `smart-reasoning`)、`agent_type`(`rag-qa/wiki-qa/hybrid-rag-wiki/data-analysis/custom`)、`system_prompt`、`model_id`、`temperature`(0-2,非法返回 code 2103)、`max_iterations`(1-20,非法返回 code 2102)、`allowed_tools`(未设置/null 使用默认工具,显式 `[]` 不选内置工具;联网、记忆、MCP 与沙箱按独立配置注册)、`mcp_selection_mode`/`mcp_services`、`skills_selection_mode`、`kb_selection_mode`/`knowledge_bases`、`web_search_enabled`、`question_suggestions` 等(完整定义见 `internal/types/custom_agent.go`)。
|
||||
|
||||
响应:201 `{"success":true,"data":{id,name,description,avatar,is_builtin,created_by,config,creator_name,...}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agents -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"name":"售后助手","config":{"agent_mode":"quick-answer","kb_selection_mode":"selected","knowledge_bases":["kb-1"]}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/agents
|
||||
|
||||
用途:Agent 列表(含内置)。权限:Viewer+。查询参数:`creator`(`mine`/`others`,可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":[Agent],"disabled_own_agent_ids":[...]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/:id
|
||||
|
||||
用途:Agent 详情。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{Agent}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/agent-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/agents/:id
|
||||
|
||||
用途:更新 Agent。权限:创建者 OR Admin+。请求体:`name/description/avatar/config`。`name` 必填;省略 `avatar` 保留原头像;`config` 整体替换,省略的配置项不会保留原值,只改个别设置请用 `PATCH`。Agent 已分享到组织时,新加入知识库范围的知识库须是调用者可分享的(知识库创建者或 Admin+,改为 `all` 需要 Admin+),否则 403。
|
||||
|
||||
响应:200 `{"success":true,"data":{Agent}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/agents/agent-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"客服助手","description":"更新描述","config":{...}}'
|
||||
```
|
||||
|
||||
### PATCH /api/v1/agents/:id
|
||||
|
||||
用途:部分更新 Agent,只修改请求中出现的字段,其余配置(包括头像)保持不变。权限与 `PUT` 相同:创建者 OR Admin+;API key `manage_agents`/full。复用 `PUT` 校验(包括上面的组织分享知识库范围规则),并额外校验技能名称。
|
||||
|
||||
请求体字段(均可选):
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| `name` | string | 名称,不能为空白 |
|
||||
| `description` | string | 描述 |
|
||||
| `agent_mode` | string | `quick-answer` / `smart-reasoning`。MCP 与技能只在 `smart-reasoning` 下运行;切到该模式会打开多轮对话 |
|
||||
| `system_prompt` | string | 完整的系统提示词(整段替换) |
|
||||
| `model_id` | string | 对话模型 ID |
|
||||
| `kb_selection_mode` | string | `all` / `selected` / `none` |
|
||||
| `knowledge_bases` | string[] | 选中的知识库 ID,整体替换;未同时传 `kb_selection_mode` 时模式切换为 `selected` |
|
||||
| `mcp_selection_mode` | string | `all` / `selected` / `none` |
|
||||
| `mcp_services` | string[] | 选中的 MCP 服务 ID,整体替换;未同时传 `mcp_selection_mode` 时模式切换为 `selected` |
|
||||
| `skills_selection_mode` | string | `all` / `selected` / `none`;`all` 和 `none` 清空选中的技能名称 |
|
||||
| `selected_skills` | string[] | 最多 50 个技能名称(不是 ID),整体替换;未同时传 `skills_selection_mode` 时模式切换为 `selected` |
|
||||
| `web_search_enabled` | bool | 是否启用联网搜索 |
|
||||
| `multi_turn_enabled` | bool | 是否启用多轮对话 |
|
||||
|
||||
技能名称来自 `GET /api/v1/skills?sandbox_config_id=<agent.config.sandbox_config_id>`,只在 `smart-reasoning` 模式生效。PATCH 后模式为 `selected` 时,所有名称必须是该工作空间、该沙箱当前可用的技能;不可用或未知名称返回 400 并列出名称。未绑定沙箱时,通过 PATCH 将技能设为 `all` 或 `selected`(包括只传 `selected_skills`)返回 400,须先在智能体编辑器选择沙箱。
|
||||
|
||||
响应:200 `{"success":true,"data":{Agent}}`;`agent_mode` 或 selection mode 取值非法、名称为空、技能列表超过 50 项或上述技能校验失败时 400,Agent 不存在时 404。
|
||||
|
||||
```bash
|
||||
curl -X PATCH $BASE/api/v1/agents/agent-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"model_id":"model-2","knowledge_bases":["kb-1","kb-2"]}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/agents/:id
|
||||
|
||||
用途:删除 Agent。权限:创建者 OR Admin+。
|
||||
|
||||
响应:200 `{"success":true,"message":"Agent deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/agents/agent-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/agents/:id/copy
|
||||
|
||||
用途:复制 Agent(副本归调用者)。权限:Contributor+。无请求体。
|
||||
|
||||
响应:201 `{"success":true,"data":{新 Agent}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agents/agent-1/copy -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/:id/suggested-questions
|
||||
|
||||
用途:Agent 起始建议问题(注册在组外以避免与 `/agents/:id/shares` 冲突)。权限:Viewer+;API key `read_agents`/`manage_agents`/`chat`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `knowledge_base_ids` | string | 否 | 逗号分隔 KB |
|
||||
| `knowledge_ids` | string | 否 | 逗号分隔知识 ID |
|
||||
| `tag_scopes` | string | 否 | JSON 数组的标签范围 |
|
||||
| `limit` | int | 否 | 上限 30 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"questions":[{question,source,knowledge_base_id}]}}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/agents/agent-1/suggested-questions?limit=6" -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
## MCP 服务(/api/v1/mcp-services)
|
||||
|
||||
空间级外部工具服务集成。读:Viewer+;写/测试/审批策略:Admin+。API key:`manage_mcp_services`/full。Handler: `internal/handler/mcp_service.go`
|
||||
|
||||
### POST /api/v1/mcp-services
|
||||
|
||||
用途:创建 MCP 服务。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是 | 名称 |
|
||||
| `description` | string | 否 | 旧版描述,兼容保留;管理界面统一编辑 `usage_instructions` |
|
||||
| `usage_instructions` | string | 否 | 服务用途、适用场景和关键约束,模型据此判断何时使用该服务;管理界面在第二步要求填写 |
|
||||
| `enabled` | bool | 否 | 启用 |
|
||||
| `transport_type` | string | 是 | `sse` / `http-streamable`;`stdio` 出于安全原因被拒绝 |
|
||||
| `url` | *string | 否 | 服务 URL(SSE/HTTP) |
|
||||
| `headers` | map[string]string | 否 | HTTP 头 |
|
||||
| `auth_config` | object | 否 | `auth_type`(`api_key/bearer/oauth`)、`api_key_header`、`custom_headers`、`scopes`、`auth_server_metadata_url`、`client_id`(OAuth 预注册客户端,留空则动态客户端注册;空字符串清除)(密钥与 `client_secret` 走 credentials 子资源,创建时可随请求体提交) |
|
||||
| `advanced_config` | object | 否 | `{timeout,retry_count,retry_delay}`,默认 30 秒 / 3 次 / 1 秒;`timeout` 大于 60 秒时也会延长 Agent 单次调用该服务工具的等待窗口 |
|
||||
| `stdio_config` / `env_vars` | object | 否 | 仅为兼容旧数据保留;stdio 已禁用,不生效 |
|
||||
|
||||
响应:200 `{"success":true,"data":{MCPServiceResponse}}`(含 `credentials:{api_key:{configured},token:{configured},client_secret:{configured}}`;已同步工具目录的服务还带 `catalog:{tool_count,stale,synced_at}`)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-services -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"github","transport_type":"sse","url":"https://mcp.example.com/sse"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services
|
||||
|
||||
用途:MCP 服务列表。权限:Viewer+。响应:200 `{"success":true,"data":[MCPServiceResponse]}`
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `agent_id` | string | 否 | 与 `agent_source_tenant_id` 同时传入时,列出该共享智能体可 @ 的 MCP 服务 |
|
||||
| `agent_source_tenant_id` | int | 否 | 共享智能体的来源空间 ID,与对话请求的同名参数一致 |
|
||||
|
||||
两个参数都传时走共享智能体路径:只返回该智能体在来源空间以 `selected` 模式指定且已启用的服务(`all`/`none` 模式返回空列表),且每项只含 ID、名称、说明、使用说明、传输类型、启用状态和工具目录摘要,不含 URL、请求头、认证配置等连接细节;调用者无权使用该智能体时返回 403。只传其一或都不传时,列出调用者自己空间的服务。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id
|
||||
|
||||
用途:详情。权限:Viewer+。响应:200 `{"success":true,"data":{MCPServiceResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/mcp-services/:id
|
||||
|
||||
用途:部分更新(map 语义;`auth_config` 中不可携带 api_key/token)。权限:Admin+。字段同创建(均可选)。
|
||||
|
||||
提交 `usage_instructions` 时必须为去除首尾空白后非空的字符串,最长 16000 字符。仅修改连接或启用状态时可省略该字段,原值保持不变。
|
||||
|
||||
响应:200 `{"success":true,"data":{MCPServiceResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/mcp-services/mcp-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"enabled":false}'
|
||||
```
|
||||
|
||||
### POST /api/v1/mcp-services/:id/usage-instructions/generate
|
||||
|
||||
用途:根据已同步、未过期的 MCP 工具目录生成精简使用说明。权限:Admin+;API key 需要 `manage_mcp_services` 或 full。
|
||||
|
||||
请求:`{"language":"zh-CN"}`。支持 `zh-CN`、`en-US`、`ja-JP`、`ko-KR`、`ru-RU`,默认中文。
|
||||
|
||||
优先使用空间默认的可用对话模型,否则使用首个可用对话模型。输入包括服务名称、服务端说明和已启用工具的名称、描述;OAuth 目录沿用当前用户的授权范围。不会连接 MCP、调用工具或自动保存生成结果。
|
||||
|
||||
响应:200 `{"success":true,"data":{"usage_instructions":"按模块和时间范围查询远程日志;已有查询 ID 时读取对应日志。"}}`。生成目标为 2–3 句简短说明,最多 500 字符;用户可编辑后通过 PUT 保存。目录未同步、过期、无启用工具或无可用对话模型时返回 400。
|
||||
|
||||
### DELETE /api/v1/mcp-services/:id
|
||||
|
||||
用途:删除。权限:Admin+。响应:200 `{"success":true,"message":"MCP service deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/mcp-services/mcp-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/mcp-services/:id/test
|
||||
|
||||
用途:连接测试(探测外部服务)。权限:Admin+。响应:200 `{"success":true,"data":{"success","message","oauth_required","tools":[...],"resources":[...]}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-services/mcp-1/test -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id/metadata
|
||||
|
||||
用途:读取持久工具目录,不连接上游。权限:Viewer+;OAuth 目录按当前有效授权主体隔离。
|
||||
|
||||
响应:200 `{"success":true,"data":null}` 表示未同步;已同步时 `data` 为目录快照,包含服务端信息、instructions、tools 和同步时间。连接配置变更后的快照标记 `stale:true`,不能用于加载运行时工具。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1/metadata -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/mcp-services/:id/metadata/refresh
|
||||
|
||||
用途:显式连接上游、完整拉取并原子更新工具目录。静态认证目录需 Admin+;OAuth 用户可同步自己的目录(Viewer+)。API Key 需要 MCP 管理能力。
|
||||
|
||||
响应为更新后的目录快照。失败保留原快照;连接在刷新期间变化返回 409,目录无效/过大或上游同步失败返回 400,元数据存储不可用返回 503。不会覆盖人工使用说明和单工具启用/审批策略。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-services/mcp-1/metadata/refresh -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id/tools
|
||||
|
||||
用途:工具列表。权限:Viewer+。响应:200 `{"success":true,"data":[{name,description,inputSchema,require_approval}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1/tools -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id/resources
|
||||
|
||||
用途:资源列表。权限:Viewer+。响应:200 `{"success":true,"data":[{uri,name,description,mimeType}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1/resources -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/mcp-services/:id/credentials
|
||||
|
||||
用途:设置密钥(`api_key`/`token`/`client_secret`,指针字段,省略保留)。权限:Admin+。Handler: `internal/handler/mcp_credentials.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"fields":{"api_key":{"configured"},"token":{"configured"},"client_secret":{"configured"}}}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/mcp-services/mcp-1/credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"token":"ghp_..."}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/mcp-services/:id/credentials/:field
|
||||
|
||||
用途:删除凭证字段(`api_key`、`token` 或 `client_secret`)。权限:Admin+。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/mcp-services/mcp-1/credentials/token -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id/tool-approvals
|
||||
|
||||
用途:工具启停与人工审批策略列表。权限:Viewer+。响应:200 `{"success":true,"data":[{service_id,tool_name,require_approval,enabled,...}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1/tool-approvals -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/mcp-services/:id/tool-approvals/:tool_name
|
||||
|
||||
用途:更新某工具的 enabled(启停)和 require_approval(人工审批)。权限:Admin+。两者至少提供一个,省略的字段保持原值;无记录默认启用且不要求审批。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/mcp-services/mcp-1/tool-approvals/create_issue \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d '{"require_approval":true}'
|
||||
```
|
||||
|
||||
## MCP OAuth
|
||||
|
||||
Handler: `internal/handler/mcp_oauth.go`
|
||||
|
||||
### GET /api/v1/mcp-oauth/callback
|
||||
|
||||
用途:第三方 OAuth 授权回调(免认证,靠单次 `state` 参数认证;注册在 `/mcp-services` 组之外)。查询参数:`code`、`state`、`error`。
|
||||
|
||||
响应:302 重定向到前端(成功 `#mcp_oauth_result=success`,失败 `#mcp_oauth_error=<code>`)。
|
||||
|
||||
```bash
|
||||
curl -i "$BASE/api/v1/mcp-oauth/callback?code=xxx&state=yyy"
|
||||
```
|
||||
|
||||
### POST /api/v1/mcp-services/:id/oauth/authorize-url
|
||||
|
||||
用途:生成用户级授权 URL。权限:Viewer+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `redirect_uri` | string | 是 | 后端回调 URL(绝对地址) |
|
||||
| `frontend_redirect` | string | 否 | 回调后前端跳转(默认 `/`) |
|
||||
|
||||
响应:200 `{"success":true,"data":{"authorization_url","authorization_attempt"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-services/mcp-1/oauth/authorize-url -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"redirect_uri":"'$BASE'/api/v1/mcp-oauth/callback"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/mcp-services/:id/oauth/assertion
|
||||
|
||||
用途:以 JWT Bearer 授权(RFC 7523)为本人获取 OAuth token,无需浏览器跳转。适用于 Academy 部署在其他应用之后、由该应用负责登录并代用户调用 Academy 的场景:该应用签发代表当前用户的断言(如 MCP Enterprise-Managed Authorization 使用的 ID-JAG),Academy 原样提交给授权服务器,由授权服务器校验。使用该服务已注册的 OAuth 客户端(不会进行动态客户端注册;尚无客户端时返回 400)。获得的 token 与授权回调保存的 token 相同,后续刷新、状态查询与吊销不变。权限:Viewer+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `assertion` | string | 是 | 提交给授权服务器的 JWT 断言(请求体上限 16 KiB) |
|
||||
|
||||
响应:200,`data` 与 `oauth/status` 相同。授权服务器拒绝断言(4xx,或 2xx 响应体含 OAuth `error`)时返回 400 并附 OAuth 错误码;授权服务器 5xx 或网络错误返回 500。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-services/mcp-1/oauth/assertion -H "X-API-Key: $API_KEY" \
|
||||
-H "X-External-User-ID: user-1" -H 'Content-Type: application/json' -d '{"assertion":"'$ASSERTION'"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/mcp-services/:id/oauth/status
|
||||
|
||||
用途:查询本人授权状态。权限:Viewer+。查询参数:`authorization_attempt`(可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"authorized","state":"authorized|pending","refresh_available","expires_at"}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/mcp-services/mcp-1/oauth/status -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/mcp-services/:id/oauth/token
|
||||
|
||||
用途:吊销本人 OAuth token。权限:Viewer+。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/mcp-services/mcp-1/oauth/token -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## MCP Server 端点(/api/v1/mcp-endpoints) {#mcp-server-端点}
|
||||
|
||||
管理当前空间对外发布的 MCP 端点,外部 MCP 客户端连接 `/mcp/:endpoint_id`。读:Viewer+;写:Admin+。API key:`manage_channels`/full。Handler: `internal/handler/mcp_endpoint.go`。用途与工具说明见[MCP 集成](../03-features/08-mcp.md#供外部客户端调用)。
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/mcp-endpoints` | 端点列表(不含令牌) |
|
||||
| GET | `/mcp-endpoints/tools` | 工具目录:`{groups,tools:[{name,group,destructive}],default_tools}` |
|
||||
| POST | `/mcp-endpoints` | 创建;201,响应含一次性 `token` |
|
||||
| GET | `/mcp-endpoints/:endpoint_id` | 详情 |
|
||||
| PUT | `/mcp-endpoints/:endpoint_id` | 部分更新,省略的字段保持原值 |
|
||||
| DELETE | `/mcp-endpoints/:endpoint_id` | 删除,使用该端点的客户端立即失效 |
|
||||
| POST | `/mcp-endpoints/:endpoint_id/rotate-token` | 轮换令牌,响应含新 `token`,旧令牌立即失效 |
|
||||
|
||||
请求字段(创建和更新相同,均可选):
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `name` | string | 名称,创建时必填 |
|
||||
| `description` | string | 说明 |
|
||||
| `enabled` | bool | 默认 true;停用后连接返回 403 |
|
||||
| `knowledge_base_ids` | string[] | 可访问的知识库,空数组表示空间内全部 |
|
||||
| `tools` | string[] | 暴露的工具,至少一个;创建时省略则使用 `default_tools`(全部只读工具) |
|
||||
| `default_agent_id` | string | `ask` 使用的 Agent,空为内置快速问答;内部内置 Agent 不可选 |
|
||||
| `rate_limit_per_minute` | int | 每分钟工具调用上限,0 或省略为 60,最大 6000 |
|
||||
|
||||
响应 `data` 为 `{id,tenant_id,name,description,enabled,token_hint,knowledge_base_ids,tools,default_agent_id,rate_limit_per_minute,path,last_used_at,created_at,updated_at}`,创建和轮换时额外带 `token`。使用受限 API Key 调用时,端点的知识库和工具所需能力不能超出该 Key 的范围,否则 403。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/mcp-endpoints -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"name":"产品文档助手","knowledge_base_ids":["kb-1"],"tools":["search_knowledge","read_document","ask"]}'
|
||||
```
|
||||
|
||||
## Agent 运行时交互(/api/v1/agent)
|
||||
|
||||
对话中的人工审批与 OAuth 恢复;权限均 Viewer+(发起会话的人才有上下文),API key 默认拒绝。
|
||||
|
||||
### POST /api/v1/agent/tool-approvals/:pending_id
|
||||
|
||||
用途:裁决待审批的工具调用。Handler: `internal/handler/mcp_service.go` 的 `ResolveToolApproval`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `decision` | string | 是(`binding:"required"`) | `approve` / `reject` |
|
||||
| `modified_args` | JSON | 否 | 修改后的工具参数 |
|
||||
| `reason` | string | 否 | 理由 |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agent/tool-approvals/p-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"decision":"approve"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/agent/mcp-oauth-resolutions/:pending_id
|
||||
|
||||
用途:恢复因 MCP OAuth 暂停的 Agent 运行。Handler: `internal/handler/mcp_oauth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `service_id` | string | 是(`binding:"required"`) | MCP 服务 ID |
|
||||
| `decision` | string | 否 | `authorize`(默认)/ `cancel` |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agent/mcp-oauth-resolutions/p-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"service_id":"mcp-1"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/agent/mcp-oauth-resolutions/:pending_id/cancel
|
||||
|
||||
用途:取消暂停中的 OAuth 流程。无请求体。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agent/mcp-oauth-resolutions/p-1/cancel -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 技能、沙箱与个人变量
|
||||
|
||||
`GET /api/v1/skills?sandbox_config_id=...` 返回指定配置下可用技能的名称/说明及 skills_available;传 `agent_id` + `agent_source_tenant_id` 时按共享智能体的来源空间和其沙箱配置返回,详见[沙箱与技能 API](02-api-sandbox-skills.md#沙箱内技能)。目录收录、安装、模板、进度、文件与个人变量的完整接口见[沙箱与技能 API](02-api-sandbox-skills.md)。
|
||||
|
||||
智能体 config 增加 `sandbox_config_id`;与 skills_selection_mode、selected_skills 一起决定可用技能。shell/文件工具按后端能力注册,旧 read_skill / execute_skill_script 不再注册。
|
||||
|
||||
## 长期记忆
|
||||
|
||||
智能体 config 的 `memory_enabled` 为 nil 时继承空间,false 禁用本智能体的记忆读写。个人管理、主题/文档偏好、导出与立即整理见[长期记忆 API](02-api-memory.md),使用步骤见[跨会话长期记忆](../03-features/23-memory.md)。
|
||||
|
||||
## 用户收藏(/api/v1/user/favorites)
|
||||
|
||||
按用户维度存储(非资源创建者维度);权限均 Viewer+,仅 JWT(API key 默认拒绝)。Handler: `internal/handler/user_resource_favorite.go`
|
||||
|
||||
### GET /api/v1/user/favorites
|
||||
|
||||
用途:收藏列表。查询参数:`type`(必填,`kb` 或 `agent`)。
|
||||
|
||||
响应:200 `{"success":true,"data":[{type,id,created_at}]}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/user/favorites?type=kb" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/user/favorites
|
||||
|
||||
用途:添加收藏。请求体:`{"type":"kb|agent","id":"<资源ID>"}`(均必填)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/user/favorites -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"type":"kb","id":"kb-1"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/user/favorites/:type/:id
|
||||
|
||||
用途:取消收藏。路径参数:`type`、`id`。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/user/favorites/kb/kb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:由 `internal/router/router.go` 调用,`RegisterCustomAgentRoutes`、`RegisterSkillRoutes`、`RegisterUserFavoriteRoutes` 定义在 `routes_agent.go`,`RegisterMCPServiceRoutes`(含 MCP OAuth 与 `/agent` 运行时交互)在 `routes_infra.go`,`RegisterMCPEndpointRoutes` 与公开的 `/mcp/:endpoint_id` 在 `routes_mcp_endpoint.go`。Handler:`internal/handler/custom_agent.go`、`internal/handler/mcp_service.go`、`internal/handler/mcp_credentials.go`、`internal/handler/mcp_oauth.go`、`internal/handler/mcp_endpoint.go`、`internal/handler/skill_handler.go`、`internal/handler/user_resource_favorite.go`。
|
||||
@@ -0,0 +1,294 @@
|
||||
# API 参考:认证与用户
|
||||
|
||||
提供注册、登录、令牌刷新、个人资料和邀请处理接口。认证要求随接口而异,公开接口在各条目中标注。
|
||||
|
||||
除特别标注外,本组接口在认证中间件之后仅要求“已登录”(无角色下限)。免认证接口见各条目。
|
||||
|
||||
## 认证(/api/v1/auth)
|
||||
|
||||
### POST /api/v1/auth/register
|
||||
|
||||
用途:注册新用户(自助注册模式)。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `username` | string | 是 | 用户名,2–50 字符 |
|
||||
| `email` | string | 是 | 邮箱 |
|
||||
| `password` | string | 是 | 密码(8–32 位,字母+数字;复杂模式另需大小写和特殊字符) |
|
||||
|
||||
是否自动创建个人空间由服务端的 `auth.default_tenant_mode` 决定,请求体不能指定。`invite_register`(仅限邀请注册)和 `invite_only`(禁止注册)模式下返回 403。
|
||||
|
||||
响应:201 `{"success":true,"message":"...","user":{User}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/register -H 'Content-Type: application/json' \
|
||||
-d '{"username":"alice","email":"a@ex.com","password":"secret123"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/register-by-invite
|
||||
|
||||
仅 `self_serve` 和 `invite_register` 模式允许凭有效邀请链接注册;`invite_only` 模式返回 403。
|
||||
|
||||
用途:通过邀请/分享链接 token 注册并加入空间。免认证,IP 限流 30 次/分钟。Handler: `internal/handler/auth_register_by_invite.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `token` | string | 是(`binding:"required"`) | 邀请 token |
|
||||
| `email` | string | 是(`binding:"required,email"`) | 邮箱 |
|
||||
| `username` | string | 是(`binding:"required"`) | 用户名 |
|
||||
| `password` | string | 是(`binding:"required,min=6"`) | 密码(8–32 位,字母+数字;复杂模式另需大小写和特殊字符) |
|
||||
|
||||
响应:201,同 Login(`user/active_tenant/memberships/token/refresh_token`)。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/register-by-invite -H 'Content-Type: application/json' \
|
||||
-d '{"token":"<invite_token>","email":"a@ex.com","username":"alice","password":"secret123"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/invitations/lookup
|
||||
|
||||
用途:匿名查询邀请 token 对应的空间信息(注册前预览)。免认证,IP 限流。Handler: `internal/handler/auth_register_by_invite.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `token` | string | 是(`binding:"required"`) | 邀请 token |
|
||||
|
||||
响应:200 `{"success":true,"data":{"tenant_id","tenant_name","role","expires_at"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/invitations/lookup -H 'Content-Type: application/json' -d '{"token":"<invite_token>"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/login
|
||||
|
||||
用途:邮箱密码登录。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `email` | string | 是(`binding:"required"`) | 邮箱 |
|
||||
| `password` | string | 是(`binding:"required"`) | 密码 |
|
||||
|
||||
响应:200 `{"success":true,"user":{...},"active_tenant":{...},"memberships":[...],"token":"...","refresh_token":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/login -H 'Content-Type: application/json' -d '{"email":"a@ex.com","password":"secret123"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/auto-setup
|
||||
|
||||
用途:原生桌面 Lite 自动建号、建空间及登录。必须携带桌面原生桥接提供的每进程随机 `X-Academy-Desktop-Token`,不接受匿名 HTTP 请求。普通浏览器使用注册/登录接口。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200,同 Login;缺少或错误的桌面凭据返回 401。
|
||||
|
||||
### GET /api/v1/auth/config
|
||||
|
||||
用途:查询注册模式等认证配置。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200 `{"success":true,"registration_mode":"self_serve|invite_register|invite_only","complex_password_enabled":false}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/auth/config
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/switch-tenant
|
||||
|
||||
用途:切换当前活跃空间并换发 token。需登录(无空间也可调用)。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `tenant_id` | uint64 | 是(`binding:"required"`) | 目标空间 ID |
|
||||
| `refresh_token` | string | 否 | 用于换发新 token |
|
||||
|
||||
响应:200,同 Login。
|
||||
|
||||
换签成功会把目标空间写入账号级「最近活跃租户」偏好,下次登录(密码/OIDC/换设备)与 refresh 都回到该空间。refresh JWT 不含 `tenant_id`,因此偏好写入失败则整次换签失败、不会发出新 token。API 客户端无需再补发 `PUT /auth/me/preferences`。Web UI 切空间不走本接口。一次换签会改变该用户所有设备的下次落点。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/switch-tenant -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"tenant_id":2}'
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/oidc/config
|
||||
|
||||
用途:查询 OIDC 是否启用及显示名。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200 `{"success":true,"enabled":bool,"provider_display_name":"..."}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/auth/oidc/config
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/oidc/start
|
||||
|
||||
免登录,直接返回 302 和指向 IdP 的 Location,可供企业门户链接使用。无需先请求 JSON 授权地址;回调由请求 origin 构造为 `/api/v1/auth/oidc/callback`。回调后的登录结果与原 OIDC 链路一致。
|
||||
|
||||
```bash
|
||||
curl -i "$BASE/api/v1/auth/oidc/start"
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/oidc/url
|
||||
|
||||
用途:获取 OIDC 授权跳转 URL。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `redirect_uri` | string | 是 | 回调地址 |
|
||||
|
||||
响应:200 `{"success":true,"authorization_url":"...","nonce":"..."}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/auth/oidc/url?redirect_uri=https://app.example.com/callback"
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/oidc/callback
|
||||
|
||||
用途:OIDC 授权回调(浏览器重定向进入)。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
查询参数:`code`、`state`、`error`、`error_description`(均由 OIDC 提供方带回)。
|
||||
|
||||
响应:302 重定向到前端,成功携带 `#oidc_result=<base64url>`,失败携带 `#oidc_error=...`。
|
||||
|
||||
```bash
|
||||
curl -i "$BASE/api/v1/auth/oidc/callback?code=xxx&state=yyy"
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/refresh
|
||||
|
||||
用途:用 refresh token 换发新 token。免认证。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `refreshToken` | string | 是(`binding:"required"`) | refresh token |
|
||||
|
||||
响应:200 `{"success":true,"access_token":"...","refresh_token":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/refresh -H 'Content-Type: application/json' -d '{"refreshToken":"<rt>"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/validate
|
||||
|
||||
用途:校验当前 token 是否有效。需登录(无空间可调用)。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200 `{"success":true,"message":"Token is valid","user":{UserInfo}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/auth/validate -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/auth/logout
|
||||
|
||||
用途:登出(失效当前 token)。需登录。无请求体。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200 `{"success":true,"message":"Logout successful"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/logout -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/auth/me
|
||||
|
||||
用途:查询当前调用者身份(用户/空间/成员关系/能力)。需登录;API key 亦可(策略 `apiKeyAny()`,任何有效 key)。Handler: `internal/handler/auth.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"user":{UserInfo},"tenant":{TenantResponse},"memberships":[...],"tenant_required":bool,"capabilities":{"can_create_tenant":bool,"auto_accept_invitation":bool}}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/auth/me -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### PUT /api/v1/auth/me/preferences
|
||||
|
||||
用途:更新个人偏好(最近活跃空间)。需登录。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `last_active_tenant_id` | *uint64 | 否 | 正整数设置/替换;`0` 清除(下次登录回 home);省略则不改。`POST /auth/switch-tenant` 成功时服务端会写同一字段。 |
|
||||
|
||||
响应:200 `{"success":true,"data":{UserPreferences}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/auth/me/preferences -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"last_active_tenant_id":2}'
|
||||
```
|
||||
|
||||
密码策略由 `GET /auth/config` 提供。复杂模式允许的特殊字符集为 `!@#$%^&*()_+-=[]{}|;:,.<>?`;不能只根据结构体 binding 的长度标签推导完整校验规则。
|
||||
|
||||
### POST /api/v1/auth/change-password
|
||||
|
||||
用途:修改密码。需登录。Handler: `internal/handler/auth.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `old_password` | string | 是(`binding:"required"`) | 旧密码 |
|
||||
| `new_password` | string | 是(`binding:"required"`) | 新密码(8–32 位,字母+数字;复杂模式另需大小写和特殊字符) |
|
||||
|
||||
响应:200 `{"success":true,"message":"Password changed successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/auth/change-password -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"old_password":"old","new_password":"NewPass123!"}'
|
||||
```
|
||||
|
||||
修改密码必须提供正确的旧密码,新密码不能与旧密码相同。成功后撤销全部会话,需要重新登录。默认策略 8–32 位、字母和数字;复杂模式要求大小写字母、数字和特殊字符。错误详情可为 `invalid_old_password`、`password_policy`、`same_password`,均返回 400。
|
||||
|
||||
### POST /api/v1/me/invitations/accept-by-token
|
||||
|
||||
需登录,仅操作当前用户,无空间的新用户也可调用。请求 `{"token":"<invite-token>"}`;有效共享邀请使当前用户加入对应空间。无效、过期、撤销的链接返回 410;空 token 返回 400。成功响应为 `{success:true,data:{membership:{tenant_id,role,status,joined_at},tenant_name}}`,前端据此切换空间。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/me/invitations/accept-by-token" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"token":"<invite-token>"}'
|
||||
```
|
||||
|
||||
## 我的邀请(/api/v1/me/invitations)
|
||||
|
||||
服务层保证“仅被邀请人可接受/拒绝”;无角色下限(无空间的新用户也可用)。Handler: `internal/handler/tenant_invitation.go`
|
||||
|
||||
### GET /api/v1/me/invitations
|
||||
|
||||
用途:列出发给我的邀请。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `include_terminal` | bool | 否 | `true` 时包含已完结的邀请 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"invitations":[TenantInvitationResponse],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/me/invitations -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/me/invitations/pending-count
|
||||
|
||||
用途:待处理邀请计数(轻量轮询)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"pending_count":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/me/invitations/pending-count -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/me/invitations/:inv_id/accept
|
||||
|
||||
用途:接受邀请,写入成员关系。路径参数:`inv_id` 邀请 ID。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{"membership":{"tenant_id","role","status","joined_at"}}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/me/invitations/12/accept -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/me/invitations/:inv_id/decline
|
||||
|
||||
用途:拒绝邀请。路径参数:`inv_id`。无请求体。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/me/invitations/12/decline -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_auth_tenant.go` 的 `RegisterAuthRoutes` 与 `RegisterMyInvitationRoutes`。Handler:`internal/handler/auth.go`、`internal/handler/auth_register_by_invite.go`、`internal/handler/tenant_invitation.go`。
|
||||
@@ -0,0 +1,469 @@
|
||||
# API 参考:IM、Embed 与文件服务
|
||||
|
||||
管理 IM 和网页嵌入渠道,并提供渠道回调、访客会话与文件访问接口。管理端、IM 平台回调和 Embed 访客使用各自的认证方式。
|
||||
|
||||
## IM 回调(免全局认证)
|
||||
|
||||
### GET|POST /api/v1/im/callback/:channel_id
|
||||
|
||||
用途:IM 平台(WeChat/Feishu/Slack/Telegram/DingTalk/QQBot/云之家等)事件回调与 URL 验证。注册在认证中间件之前,使用各平台自身的签名验证;验签失败 403,渠道不存在 404。收到消息立即 ACK,异步处理。Handler: `internal/handler/im.go`
|
||||
|
||||
响应:200 `{"success":true}` 或平台要求的 ACK 格式。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/im/callback/ch-1 -H 'Content-Type: application/json' -d '{"event":"..."}'
|
||||
```
|
||||
|
||||
## IM 渠道管理(需认证)
|
||||
|
||||
API key:`manage_channels`/full。IM 渠道携带外部 bot 凭证:列表 Viewer+,变更/开关/扫码登录 Admin+。
|
||||
|
||||
飞书/Lark credentials.api_base_url 同时影响 HTTP API 和 WebSocket bootstrap;云之家支持 session_mode=thread。配置示例及网络要求见[IM 集成](../03-features/12-im-integration.md)。IM/Embed 的记忆偏好来自绑定 Agent 的 config.memory_enabled,当前渠道接口没有单独的 memory_enabled 参数。
|
||||
|
||||
### POST /api/v1/agents/:id/im-channels
|
||||
|
||||
用途:为 Agent 创建 IM 渠道。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `platform` | string | 是 | `wecom/feishu/lark/slack/telegram/dingtalk/mattermost/wechat/qqbot/yunzhijia` |
|
||||
| `name` | string | 否 | 显示名 |
|
||||
| `mode` | string | 否 | `websocket`(默认;mattermost/yunzhijia 默认 `webhook`)/`webhook`/`longpoll`(wechat 强制 longpoll) |
|
||||
| `output_mode` | string | 否 | `stream`(默认)/`full`(wechat 强制 full) |
|
||||
| `locale` | string | 否 | 回复语言:`zh-CN`/`en-US`/`ja-JP`/`ko-KR`/`ru-RU`;空(默认)使用部署默认语言(`ACADEMY_LANGUAGE`,未设置为 `zh-CN`);其他值 400 |
|
||||
| `session_mode` | string | 否 | `user`(默认)/`thread` |
|
||||
| `knowledge_base_id` | string | 否 | 附件额外入库的 KB,须属于本空间,否则 400 |
|
||||
| `credentials` | object | 否 | 平台凭证 |
|
||||
| `enabled` | bool | 否 | 默认 true |
|
||||
|
||||
响应:200 `{"data":{IMChannel}}`;同渠道 bot 已存在返回 409。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agents/agent-1/im-channels -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"platform":"feishu","name":"飞书客服","locale":"en-US"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/:id/im-channels
|
||||
|
||||
用途:某 Agent 的 IM 渠道列表(摘要)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"data":[IMChannel]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/agent-1/im-channels -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/im-channels
|
||||
|
||||
用途:全空间 IM 渠道总览(不含凭证)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"data":[IMChannel]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/im-channels -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/im-channels/:id
|
||||
|
||||
用途:更新渠道(局部更新:`name/mode/output_mode/locale/session_mode/knowledge_base_id/credentials/enabled/agent_id` 均可选;`knowledge_base_id` 传空字符串解除关联,`locale` 传空字符串恢复默认语言)。权限:Admin+。
|
||||
|
||||
响应:200 `{"data":{IMChannel}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/im-channels/ch-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"enabled":false}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/im-channels/:id
|
||||
|
||||
用途:删除渠道。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/im-channels/ch-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/im-channels/:id/toggle
|
||||
|
||||
用途:启停切换。权限:Admin+。无请求体。
|
||||
|
||||
响应:200 `{"data":{IMChannel}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/im-channels/ch-1/toggle -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/wechat/qrcode
|
||||
|
||||
用途:生成 WeChat 登录二维码(绑定个人微信到空间)。权限:Admin+。无请求体。Handler: `internal/handler/wechat_qrcode.go`
|
||||
|
||||
响应:200 `{"data":{"qrcode_url","qrcode"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/wechat/qrcode -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/wechat/qrcode/status
|
||||
|
||||
用途:轮询扫码状态;确认后返回凭证。权限:Admin+。请求体:`{"qrcode":"<标识>"}`(必填)。
|
||||
|
||||
响应:200 `{"data":{"status":"pending|scanned|confirmed|expired","credentials":{bot_token,ilink_bot_id,ilink_user_id,baseurl}}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/wechat/qrcode/status -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"qrcode":"qr-1"}'
|
||||
```
|
||||
|
||||
## Embed 渠道管理(需认证)
|
||||
|
||||
API key:`manage_channels`/full。Handler: `internal/handler/embed_channel.go`
|
||||
|
||||
### POST /api/v1/agents/:id/embed-channels
|
||||
|
||||
用途:为 Agent 创建 Web 嵌入渠道。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 否 | 名称 |
|
||||
| `enabled` | bool | 否 | 默认 true |
|
||||
| `allowed_origins` | []string | 是 | 至少一个来源(精确 URL / `*.domain`;生产禁止 `*`) |
|
||||
| `welcome_message` | string | 否 | 欢迎语 |
|
||||
| `rate_limit_per_minute` | int | 否 | 每 IP/分钟,默认 30 |
|
||||
| `rate_limit_per_day` | int | 否 | 渠道/天,默认 10000 |
|
||||
| `primary_color` / `page_title` / `widget_position` | string | 否 | 外观(position: `bottom-right` 默认等四角) |
|
||||
| `header_title_mode` | string | 否 | `channel`(默认)/`session` |
|
||||
| `show_suggested_questions` | bool | 否 | 默认 true |
|
||||
| `allow_web_search` / `allow_file_upload` | bool | 否 | 默认 false |
|
||||
| `default_locale` | string | 否 | `zh-CN/en-US/ko-KR/ja-JP/ru-RU`/空(跟随浏览器) |
|
||||
| `webhook_url` / `webhook_secret` | string | 否 | 访客事件 webhook |
|
||||
| `agent_id` | string | 否 | 绑定 Agent |
|
||||
|
||||
响应:201 `{"success":true,"data":{embedChannelResponse 含 publish_token}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agents/agent-1/embed-channels -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"官网客服","allowed_origins":["https://example.com"]}'
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/:id/embed-channels
|
||||
|
||||
用途:某 Agent 的嵌入渠道列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[embedChannelResponse]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/agent-1/embed-channels -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed-channels
|
||||
|
||||
用途:全空间嵌入渠道列表(不含 publish token)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[embedChannelResponse]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed-channels -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed-channels/:channel_id
|
||||
|
||||
用途:渠道详情(含 publish token,用于复制部署代码)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{embedChannelResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed-channels/ec-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/embed-channels/:channel_id
|
||||
|
||||
用途:更新渠道(字段同创建,均可选)。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true,"data":{embedChannelResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/embed-channels/ec-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"enabled":false}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/embed-channels/:channel_id
|
||||
|
||||
用途:删除渠道。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/embed-channels/ec-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed-channels/:channel_id/rotate-token
|
||||
|
||||
用途:轮换 publish token(旧 token 失效)。权限:Admin+。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{embedChannelResponse 含新 publish_token}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed-channels/ec-1/rotate-token -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed-channels/:channel_id/preview-session
|
||||
|
||||
用途:签发管理端预览用短时效 session token(无需 publish token)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"session_token","expires_in"}}`;渠道禁用 403。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed-channels/ec-1/preview-session -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed-channels/:channel_id/stats
|
||||
|
||||
用途:渠道用量统计。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"session_count":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed-channels/ec-1/stats -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## Embed 嵌入页框架策略
|
||||
|
||||
### GET /api/v1/embed-frame-policy
|
||||
|
||||
用途:供 Nginx `auth_request` 子请求在返回 `/embed/:channel_id` 页面前获取该渠道的 `Content-Security-Policy: frame-ancestors`。免 token,只返回策略头,不返回渠道配置;响应 `Cache-Control: no-store`。
|
||||
|
||||
| 请求头 | 必填 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `X-Embed-Page-URI` | 是 | 嵌入页的相对 URI,如 `/embed/<channel_id>` |
|
||||
|
||||
响应:204 并带 CSP 头;渠道不存在、已停用、路径非法或白名单为空时 403。
|
||||
|
||||
```bash
|
||||
curl -i $BASE/api/v1/embed-frame-policy -H "X-Embed-Page-URI: /embed/ec-1"
|
||||
```
|
||||
|
||||
## Embed 公开路由(/api/v1/embed/:channel_id,EmbedAuth)
|
||||
|
||||
认证:`Authorization: Embed <publish_token|session_token>`;会话级操作附加 `X-Embed-Session: <sig>`。限流与 Origin 校验见总览。Handler: `internal/handler/embed_channel.go`;中间件:`internal/middleware/embed_auth.go`
|
||||
|
||||
以下 `$ET` 表示 Embed token 头:`-H "Authorization: Embed $EMBED_TOKEN"`。
|
||||
|
||||
### POST /api/v1/embed/:channel_id/exchange
|
||||
|
||||
用途:用 publish token 换取短时效 session token(session token 不可再次 exchange)。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{"session_token","expires_in"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/exchange -H "Authorization: Embed $PUBLISH_TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed/:channel_id/config
|
||||
|
||||
用途:渠道公开配置(无密钥)。
|
||||
|
||||
响应:200 `{"success":true,"data":{channel_id,name,display_title,knowledge_base_ids,agent_id,agent_name,welcome_message,primary_color,widget_position,allow_web_search,allow_file_upload,default_locale,...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed/ec-1/config -H "Authorization: Embed $EMBED_TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed/:channel_id/suggested-questions
|
||||
|
||||
用途:起始建议问题。查询参数:`limit`(≤12)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"questions":[...]}}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/embed/ec-1/suggested-questions?limit=6" -H "Authorization: Embed $EMBED_TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/embed/:channel_id/chunks/:chunk_id
|
||||
|
||||
用途:查看引用分块(内容脱敏;越权 403)。
|
||||
|
||||
响应:200 `{"success":true,"data":{chunk}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed/ec-1/chunks/c-1 -H "Authorization: Embed $EMBED_TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/sessions
|
||||
|
||||
用途:创建访客会话,返回会话 ID 与签名句柄。无请求体。
|
||||
|
||||
响应:201 `{"success":true,"data":{"id":"<session_id>","sig":"<签名>"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/sessions -H "Authorization: Embed $EMBED_TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/knowledge-chat/:session_id
|
||||
|
||||
用途:访客知识问答(SSE;payload 会被渠道约束改写后委托给 KnowledgeQA)。需 `X-Embed-Session`。请求体同 `/knowledge-chat`(`query` 必填)。
|
||||
|
||||
```bash
|
||||
curl -N -X POST $BASE/api/v1/embed/ec-1/knowledge-chat/s-1 \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"营业时间?"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/agent-chat/:session_id
|
||||
|
||||
用途:访客 Agent 问答(SSE)。需 `X-Embed-Session`。请求体同上。
|
||||
|
||||
```bash
|
||||
curl -N -X POST $BASE/api/v1/embed/ec-1/agent-chat/s-1 \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"帮我下单"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/embed/:channel_id/messages/:session_id/load
|
||||
|
||||
用途:加载访客会话消息(委托 `LoadMessages`,查询参数 `limit/before_time`)。需 `X-Embed-Session`。
|
||||
|
||||
响应:200 `{"success":true,"data":[Message]}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/embed/ec-1/messages/s-1/load?limit=20" \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/sessions/:session_id/stop
|
||||
|
||||
用途:停止生成(委托 StopSession;请求体 `{"message_id":"..."}`)。需 `X-Embed-Session`。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/sessions/s-1/stop \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"message_id":"m-1"}'
|
||||
```
|
||||
|
||||
### GET|POST /api/v1/embed/:channel_id/sessions/:session_id/messages/:message_id/suggestions
|
||||
|
||||
用途:读取 / 触发生成消息建议(渠道关闭建议时返回 `suppressed`)。需 `X-Embed-Session`。
|
||||
|
||||
响应:200 `{"success":true,"data":{"status","questions":[...]}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/embed/ec-1/sessions/s-1/messages/m-1/suggestions \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG"
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/sessions/:session_id/suggestion-events
|
||||
|
||||
用途:上报建议交互事件(委托 RecordEvent,字段同认证版)。需 `X-Embed-Session`。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/sessions/s-1/suggestion-events \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"suggestion_set_id":"ss-1","event_type":"impression"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/embed/:channel_id/sessions/:session_id/events
|
||||
|
||||
用途:转发访客事件到渠道 webhook。需 `X-Embed-Session`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `type` | string | 是 | `message_sent` / `message_received` |
|
||||
| `query` / `content` | string | 否 | 用户问题 / 机器人回复 |
|
||||
|
||||
响应:200 `{"success":true}`;不支持的类型 400。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/sessions/s-1/events \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"type":"message_sent","query":"你好"}'
|
||||
```
|
||||
|
||||
### MCP OAuth 与工具审批(访客侧)
|
||||
|
||||
以下路由均需 `X-Embed-Session`,委托到对应认证版 handler(`internal/handler/mcp_oauth.go`、`internal/handler/mcp_service.go`):
|
||||
|
||||
| 方法+路径 | 用途 |
|
||||
| --- | --- |
|
||||
| `POST /api/v1/embed/:channel_id/sessions/:session_id/mcp-oauth-resolutions/:pending_id` | 恢复 OAuth 暂停的运行(体:`service_id` 必填,`decision` 可选) |
|
||||
| `POST /api/v1/embed/:channel_id/sessions/:session_id/mcp-oauth-resolutions/:pending_id/cancel` | 取消 OAuth 流程 |
|
||||
| `POST /api/v1/embed/:channel_id/sessions/:session_id/mcp-services/:id/oauth/authorize-url` | 生成授权 URL(体:`redirect_uri` 必填) |
|
||||
| `GET /api/v1/embed/:channel_id/sessions/:session_id/mcp-services/:id/oauth/status` | 查询授权状态 |
|
||||
| `POST /api/v1/embed/:channel_id/sessions/:session_id/tool-approvals/:pending_id` | 工具审批(体:`decision` 必填) |
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/embed/ec-1/sessions/s-1/tool-approvals/p-1 \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -H "X-Embed-Session: $SIG" \
|
||||
-H 'Content-Type: application/json' -d '{"decision":"approve"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/embed/:channel_id/files
|
||||
|
||||
用途:访客侧图片代理(机器人回复内嵌图片;EmbedAuth 注入渠道空间,handler 强制同空间路径)。查询参数:`file_path`(必填)。
|
||||
|
||||
响应:200 文件流。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/embed/ec-1/files?file_path=local://1/exports/chart.png" \
|
||||
-H "Authorization: Embed $EMBED_TOKEN" -o chart.png
|
||||
```
|
||||
|
||||
## 文件服务
|
||||
|
||||
实现于 `internal/router/files.go`(非 handler 包)。
|
||||
|
||||
### GET /files
|
||||
|
||||
用途:认证后的统一文件代理(本地/MinIO/COS/TOS 等)。权限:任意已认证空间成员;API key 需非 KB 受限(full-access 或全空间 retrieve,`middleware.AllowFileServeAPIKey()`);路径强制同空间(`ValidateStoragePathTenant`)。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `file_path` | string | 是 | `provider://...` 路径(禁止 `..`;跨空间 403) |
|
||||
|
||||
响应:200 文件流(`X-Content-Type-Options: nosniff`;非白名单类型强制 `Content-Disposition: attachment`)。
|
||||
|
||||
```bash
|
||||
curl "$BASE/files?file_path=local://1/docs/a.png" -H "Authorization: Bearer $TOKEN" -o a.png
|
||||
```
|
||||
|
||||
### GET|HEAD /api/v1/files/presigned
|
||||
|
||||
用途:HMAC 签名 URL 文件访问(IM 平台内嵌图片;免认证,验签+过期校验,`SYSTEM_AES_KEY` 参与签名)。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `file_path` | string | 是 | 存储路径 |
|
||||
| `tenant_id` | uint64 | 是 | 空间 ID |
|
||||
| `expires` | string | 是 | Unix 过期时间 |
|
||||
| `sig` | string | 是 | HMAC 签名 |
|
||||
|
||||
响应:200 文件流(HEAD 仅返回头);签名无效/过期 403。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/files/presigned?file_path=local://1/x.png&tenant_id=1&expires=1790000000&sig=abc" -o x.png
|
||||
```
|
||||
|
||||
### GET /api/v1/files/presigned-preview
|
||||
|
||||
用途:诊断端点:返回给定路径将生成的预签名 HTTP URL。权限:Admin+,显式拒绝 API key(`DenyAPIKeyPrincipal`)。查询参数:`file_path`(必填)。
|
||||
|
||||
`file_path` 必须属于当前空间,与 `/files` 相同:`resource://` 句柄要求资源归属本空间,存储路径要求租户段为本空间 ID;否则 403,不会签发任何 URL。
|
||||
|
||||
响应:200 `{"file_path","provider","url","rewritten":bool,"hint"}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/files/presigned-preview?file_path=local://1/x.png" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET|HEAD /r/:token
|
||||
|
||||
用途:短时效资源授权 URL(IM 等无法携带认证头的客户端)。免认证,token 即能力凭证;无效/过期 404。
|
||||
|
||||
响应:200 文件流(`Cache-Control: private, max-age=300`)。
|
||||
|
||||
```bash
|
||||
curl $BASE/r/abc123 -o file.png
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_agent.go` 的 `RegisterIMRoutes`、`RegisterIMChannelRoutes`、`RegisterEmbedChannelRoutes`、`RegisterEmbedPublicRoutes`;`internal/router/files.go` 的 `serveFilesWithResources`、`servePresignedFiles`、`servePresignedPreview`、`serveResourceGrants`。Handler:`internal/handler/im.go`、`internal/handler/wechat_qrcode.go`、`internal/handler/embed_channel.go`。
|
||||
@@ -0,0 +1,591 @@
|
||||
# API 参考:会话、消息与聊天
|
||||
|
||||
创建和管理会话,读取消息与临时附件,并通过 SSE 获取知识问答或智能体回答。
|
||||
|
||||
会话为“用户私有”资源,handler 内部强制归属校验;路由层为 Viewer+。API key:会话/聊天需 `chat` capability(或 full-access);消息搜索需 `message_history`;知识检索需 `retrieve`。
|
||||
|
||||
## 会话(/api/v1/sessions)
|
||||
|
||||
### POST /api/v1/sessions
|
||||
|
||||
用途:创建会话。Handler: `internal/handler/session/handler.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `title` | string | 否 | 标题 |
|
||||
| `kind` | string | 否 | 空值(默认)为普通会话;`assistant` 为 Academy 助手会话;其他值返回 400 |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `project_dir` | string | 否 | 仅桌面版:把会话绑定到用户已批准的本机项目目录(绝对路径,须与批准列表完全一致),否则返回 400 |
|
||||
|
||||
响应:201 `{"success":true,"data":{Session}}`(`id,kind,title,description,tenant_id,user_id,is_pinned,last_request_state,created_at,...`)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"title":"新对话"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/sessions
|
||||
|
||||
用途:会话列表。Handler: `internal/handler/session/handler.go`
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page` / `page_size` | int | 否 | 分页 |
|
||||
| `keyword` | string | 否 | 标题模糊搜索 |
|
||||
| `kind` | string | 否 | 缺省或空值仅返回普通会话,适用于所有来源;`assistant` 仅返回调用者本人的助手会话(管理员也不能跨用户),按 `updated_at DESC` 排序;其他值返回 400 |
|
||||
| `source` | string | 否 | 来源过滤(web/embed/api/feishu/wechat/slack/...) |
|
||||
| `agent_id` | string | 否 | 按 Agent 过滤(IM 会话) |
|
||||
|
||||
响应:200 `{"success":true,"data":[SessionListItem],"total","page","page_size"}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/sessions?page=1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/sessions/:id
|
||||
|
||||
用途:会话详情。
|
||||
|
||||
响应:200 `{"success":true,"data":{Session}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/sessions/s-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/sessions/:id
|
||||
|
||||
用途:更新会话(标题/描述/置顶)。请求体:`title`、`description`、`is_pinned`(均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{Session}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/sessions/s-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"title":"重命名"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/sessions/:id
|
||||
|
||||
用途:删除会话。
|
||||
|
||||
响应:200 `{"success":true,"message":"Session deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/sessions/s-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/sessions/batch
|
||||
|
||||
用途:批量删除会话。`delete_all:true` 保留助手会话;指定 ID 删除仍按原有归属规则处理。请求体:`{"ids":["s-1"],"delete_all":false}`(二选一:`ids` 或 `delete_all:true`)。
|
||||
|
||||
响应:200 `{"success":true,"message":"Sessions deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/sessions/batch -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"ids":["s-1","s-2"]}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/sessions/:id/messages
|
||||
|
||||
用途:清空会话消息。
|
||||
|
||||
响应:200 `{"success":true,"message":"Session messages cleared successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/sessions/s-1/messages -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/generate_title
|
||||
|
||||
用途:根据上下文消息生成会话标题。Handler: `internal/handler/session/title.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `messages` | []Message | 是(`binding:"required"`) | 用作上下文的消息 |
|
||||
|
||||
响应:200 `{"success":true,"data":"生成的标题"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/generate_title -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"介绍下产品"}]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/stop
|
||||
|
||||
用途:停止正在生成的回答。Handler: `internal/handler/session/stream.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `message_id` | string | 是(`binding:"required"`) | 助手消息 ID |
|
||||
|
||||
响应:200 `{"success":true,"message":"Generation stopped"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/stop -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"message_id":"m-1"}'
|
||||
```
|
||||
|
||||
停止时,尚未送达智能体的追加消息(见下文[向运行中的回答追加消息](#steer))会一并丢弃,不会自动开始新一轮。
|
||||
|
||||
### POST /api/v1/sessions/:session_id/fork
|
||||
|
||||
用途:从某条历史消息分叉出新会话,源会话保持不变。只有会话归属人可以分叉。Handler: `internal/handler/session/fork.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `message_id` | string | 是 | 分叉点。用户消息:复制它之前的历史,客户端通常把该问题预填到输入框;助手消息:复制到这条回答为止的历史 |
|
||||
| `title` | string | 否 | 新会话标题,默认“源标题(分支)” |
|
||||
|
||||
复制的消息保留原时间线和生成文件;新会话记录 `parent_session_id` 与 `forked_from_message_id`。源会话绑定沙箱时,系统会给沙箱做快照,新会话首次使用沙箱时从分叉点对应轮次的工作区状态启动。无法携带工作区时分叉仍然成功,但返回 `degraded: true` 和原因:
|
||||
|
||||
| `reason` | 含义 |
|
||||
| --- | --- |
|
||||
| `NO_CHECKPOINT` | 分叉点之前的轮次没有工作区检查点 |
|
||||
| `SANDBOX_REPLACED` | 检查点属于会话已更换的旧沙箱 |
|
||||
| `SANDBOX_GONE` | 源会话当前没有可快照的沙箱 |
|
||||
| `SNAPSHOT_UNSUPPORTED` | 沙箱后端不支持快照或快照失败 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"session_id":"新会话 ID","degraded":false}}`。源会话正在生成、或分叉点是未完成的回答时返回 409(`code: FORK_SOURCE_BUSY`);会话或消息不存在返回 404;分叉点不是用户或助手消息返回 400。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/fork -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"message_id":"m-3"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/rewind
|
||||
|
||||
用途:把当前会话原地回退到某条消息。只有会话归属人可以回退。Handler: `internal/handler/session/rewind.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `message_id` | string | 是 | 回退点。用户消息:删除它本身及之后的消息;助手消息:保留这条回答,删除之后的消息 |
|
||||
|
||||
被删除消息的生成文件、追问建议和聊天历史索引会一并清理。会话绑定沙箱时,系统同时把 `/workspace` 重置到保留历史中最后一轮的检查点;工作区重置失败时整个操作失败,消息不会被删除。
|
||||
|
||||
响应:200 `{"success":true,"data":{"deleted_messages":N,"workspace_reset":true,"reason":""}}`。`workspace_reset` 为 false 时,`reason` 说明只回退了对话的原因:`NO_SANDBOX`(会话未绑定沙箱)或 `NO_CHECKPOINT`(保留的历史没有检查点)。
|
||||
|
||||
| 状态码 | `code` | 含义 |
|
||||
| --- | --- | --- |
|
||||
| 409 | `REWIND_SOURCE_BUSY` | 会话正在生成,或回退点是未完成的回答 |
|
||||
| 409 | `REWIND_NO_CHECKPOINT` | 沙箱仍在,但保留的历史找不到可用检查点;为避免文件领先于对话而拒绝 |
|
||||
| 409 | `REWIND_SANDBOX_REPLACED` | 检查点属于已更换的旧沙箱 |
|
||||
| 404 | — | 会话或消息不存在 |
|
||||
| 500 | — | 工作区重置失败,可重试 |
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/rewind -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"message_id":"m-3"}'
|
||||
```
|
||||
|
||||
### 向运行中的回答追加消息(steer) {#steer}
|
||||
|
||||
智能推理模式的回答生成期间,同一会话再次调用 `agent-chat` 会返回 409。此时可用以下接口把新消息排入当前轮。只有会话归属人可调用;快速问答没有可注入的执行循环,不支持追加。Handler: `internal/handler/session/steer.go`
|
||||
|
||||
| 方法 | 路径 | 用途 |
|
||||
| --- | --- | --- |
|
||||
| POST | `/api/v1/sessions/:session_id/steer` | 追加一条消息 |
|
||||
| GET | `/api/v1/sessions/:id/steer` | 列出当前轮尚未送达的排队消息,用于刷新页面后恢复队列;返回 `assistant_message_id` 与 `items[]`(`steer_id`、`content`、`delivery`、`mentioned_items`),无运行中的轮次时 `items` 为空 |
|
||||
| DELETE | `/api/v1/sessions/:id/steer/:steer_id` | 撤回一条排队消息 |
|
||||
| POST | `/api/v1/sessions/:session_id/steer/:steer_id/inject` | 把一条 `after` 消息改为 `inject` |
|
||||
|
||||
POST 请求体:
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query` | string | 是 | 消息内容,最多 10000 字符 |
|
||||
| `delivery` | string | 否 | `after`(默认):当前轮结束后作为下一轮问题发出;`inject`:在当前轮下一次迭代边界(包括即将给出最终回答前)送达智能体,智能体据此调整后继续 |
|
||||
| `mentioned_items` | []object | 否 | @提及项,格式同聊天请求 |
|
||||
| `steer_id` | string | 否 | 客户端生成的 UUID,用于重试去重;同一 ID 对应不同内容返回 409 |
|
||||
| `expected_assistant_message_id` | string | 否 | 客户端看到的运行中助手消息 ID;运行已切换时返回 409,客户端应重试 |
|
||||
| `channel` | string | 否 | 来源渠道 |
|
||||
|
||||
响应中的 `status`:
|
||||
|
||||
| `status` | 含义 |
|
||||
| --- | --- |
|
||||
| `queued` | 已排队,返回 `steer_id`、`delivery` 与 `assistant_message_id` |
|
||||
| `new_run` | 当前没有运行中的轮次,客户端应改为正常调用 `agent-chat` 发送 |
|
||||
| `already_injected` | 该消息已经送达智能体(重试、撤回或改为注入时可能出现),无法撤回 |
|
||||
| `deleted` / `gone` | 仅 DELETE:已撤回(`removed` 表示是否确实删除了一条)/ 当前没有运行中的轮次 |
|
||||
|
||||
每轮最多同时排队 10 条未送达消息,超出返回 400。当前轮正常结束后,第一条未送达的消息由服务端直接作为下一轮的问题启动,其余消息按原投递方式转入该轮;这一轮没有对应的 `agent-chat` 连接,可用 `GET /steer` 返回的 `assistant_message_id` 调用 continue-stream 接收。用户停止生成时,未送达的消息全部丢弃。已送达的消息写入会话历史,并通过 `user_message_injected` 事件通知客户端。查询运行状态失败时返回 503,可重试。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/steer -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"只看 2024 年的数据","delivery":"inject"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/pin 与 DELETE /api/v1/sessions/:id/pin
|
||||
|
||||
用途:置顶 / 取消置顶会话。无请求体。Handler: `internal/handler/session/handler.go`
|
||||
|
||||
响应:200 `{"success":true,"is_pinned":true|false}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/pin -H "Authorization: Bearer $TOKEN"
|
||||
curl -X DELETE $BASE/api/v1/sessions/s-1/pin -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/sessions/continue-stream/:session_id
|
||||
|
||||
用途:断线续传活跃流(重放历史事件 + 100ms 轮询新增量)。Handler: `internal/handler/session/stream.go`
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `message_id` | string | 是 | 要续传的助手消息 ID |
|
||||
|
||||
响应:200 SSE(`text/event-stream`,事件格式见总览“流式接口协议”)。重放已存储的事件时,同一事件 ID 下连续的未完成 `answer`/`thinking`/`reflection` 增量会合并为一帧,按事件 ID 累积内容的客户端得到的文本不变;实时推送部分不合并。
|
||||
|
||||
```bash
|
||||
curl -N "$BASE/api/v1/sessions/continue-stream/s-1?message_id=m-1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 沙箱图形桌面 {#sandbox-desktop}
|
||||
|
||||
这些路由由 `internal/router/routes_chat.go` 注册,尚未进入 Swagger。仅 Cube/E2B 桌面模板支持;部署和代理要求见[沙箱部署](../06-development/04-sandbox-deployment.md)。
|
||||
|
||||
### POST /api/v1/sessions/:session_id/sandbox/desktop-ticket
|
||||
|
||||
签发两分钟有效的一次性 WebSocket 票据。需要会话属主的有效登录 Bearer access token,不能只用 API Key。JWT 只放在本次 POST 的认证头中。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/sessions/$SESSION_ID/sandbox/desktop-ticket" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
响应:200 `{"success":true,"data":{"ticket":"<opaque-ticket>","expires_in":120}}`。
|
||||
|
||||
### GET /api/v1/sessions/:id/sandbox/desktop
|
||||
|
||||
WebSocket 握手使用 `?ticket=<opaque-ticket>`,不走普通 JWT 中间件。票据绑定用户、空间、会话与原 access token,使用一次即失效;用过、过期、未知票据统一拒绝。代理日志不能记录 ticket query。
|
||||
|
||||
每会话同时仅允许一条中继。沙箱未绑定、暂停、不支持桌面或启动失败等状态可能先完成 WebSocket upgrade,再以 `SANDBOX_NOT_BOUND`、`SANDBOX_PAUSED`、`DESKTOP_UNSUPPORTED`、`DESKTOP_START_FAILED` 等 close reason 断开,客户端应读取关闭原因。
|
||||
|
||||
### POST /api/v1/sessions/:session_id/sandbox/desktop/activity
|
||||
|
||||
会话属主上报键鼠活动,响应 200 `{"success":true}`。仅当服务端 RFB parser 降级时用于续期;parser 正常时忽略,不应通过轮询延长沙箱寿命。
|
||||
|
||||
## 会话附件(临时文档)
|
||||
|
||||
Handler: `internal/handler/session/temporary_document.go`
|
||||
|
||||
### POST /api/v1/sessions/:session_id/attachments
|
||||
|
||||
用途:上传并保存会话附件。保存后立即排入后台解析队列,上传完成不代表已解析。快速问答在回答流建立后等待未完成的解析;智能推理通过 `read_file` 按需读取正文,读取时才等待未就绪的文档。单独上传的图片在启用图片上传时会先等待就绪。multipart 字段:`file`(必填)、`agent_id`(可选,决定解析引擎/ASR 模型)、`parser_engine`(可选;使用共享智能体时忽略,由智能体的解析规则决定)。
|
||||
|
||||
响应:202 `{"success":true,"data":{TemporaryDocument}}`(`id,session_id,file_name,file_type,file_size,status(uploaded/processing/ready/failed),resource_ref,...`)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/attachments -H "Authorization: Bearer $TOKEN" -F 'file=@notes.pdf'
|
||||
```
|
||||
|
||||
`uploaded` 是可发送的已存储状态,客户端不应强制等到 `ready` 才允许提问。附件被有效会话消息(含分叉)引用期间不受默认 24 小时 TTL 清理;未引用的过期文件才由后台清理。
|
||||
|
||||
### GET /api/v1/sessions/:id/attachments
|
||||
|
||||
用途:附件列表。
|
||||
|
||||
响应:200 `{"success":true,"data":[TemporaryDocument]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/sessions/s-1/attachments -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/sessions/:id/attachments/:attachment_id
|
||||
|
||||
用途:附件详情(含解析状态)。
|
||||
|
||||
响应:200 `{"success":true,"data":{TemporaryDocument}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/sessions/s-1/attachments/a-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/sessions/:id/attachments/:attachment_id/preview
|
||||
|
||||
用途:附件原文件预览。
|
||||
|
||||
响应:200 文件流(`Content-Disposition: inline|attachment`,`Cache-Control: private`)。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/sessions/s-1/attachments/a-1/preview -H "Authorization: Bearer $TOKEN" -o preview.pdf
|
||||
```
|
||||
|
||||
### DELETE /api/v1/sessions/:id/attachments/:attachment_id
|
||||
|
||||
用途:删除附件。
|
||||
|
||||
响应:204 No Content
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/sessions/s-1/attachments/a-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 回答建议(Suggestions)
|
||||
|
||||
Handler: `internal/handler/message_suggestion.go`
|
||||
|
||||
### GET /api/v1/sessions/:id/messages/:message_id/suggestions
|
||||
|
||||
用途:读取某助手消息的追问建议。
|
||||
|
||||
响应:200 `{"success":true,"data":{MessageSuggestionSet}}`(`status(generating/ready/suppressed/failed),questions:[{id,text,category,source,knowledge_base_ids}],allow_regenerate,...`)
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/sessions/s-1/messages/m-1/suggestions -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/messages/:message_id/suggestions
|
||||
|
||||
用途:确保生成建议(幂等触发)。请求体:`{"regenerate":true}`(可选,强制重新生成)。
|
||||
|
||||
响应:200(就绪)或 202(生成中)`{"success":true,"data":{MessageSuggestionSet|null}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/messages/m-1/suggestions -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{}'
|
||||
```
|
||||
|
||||
### POST /api/v1/sessions/:session_id/suggestion-events
|
||||
|
||||
用途:上报建议交互事件(埋点)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `suggestion_set_id` | string | 是(`binding:"required"`) | 建议集 ID |
|
||||
| `question_id` | string | 否 | click/regenerate 时必填 |
|
||||
| `event_type` | string | 是(`binding:"required"`) | `impression/click/dismiss/regenerate` |
|
||||
|
||||
响应:204 No Content
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/sessions/s-1/suggestion-events -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"suggestion_set_id":"ss-1","event_type":"impression"}'
|
||||
```
|
||||
|
||||
## 聊天与检索
|
||||
|
||||
Handler: `internal/handler/session/qa.go`。API key:聊天需 `chat`/full;`knowledge-search` 需 `retrieve`/full。
|
||||
|
||||
### POST /api/v1/knowledge-chat/:session_id
|
||||
|
||||
用途:知识库问答(SSE 流式)。
|
||||
|
||||
请求体(KnowledgeQA/AgentQA 共用):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query` | string | 通常必填 | 用户问题;带非空内联 `images[].data` 或 `attachment_uploads[].data` 时可不输入文字,系统生成默认问题。仅传 `attachment_ids` 或图片 URL 不能省略文字;非法字符、无内容请求返回 400 |
|
||||
| `knowledge_base_ids` | []string | 否 | 检索的 KB |
|
||||
| `knowledge_ids` | []string | 否 | 限定知识文件 |
|
||||
| `agent_enabled` | bool | 否 | 是否启用 Agent 模式 |
|
||||
| `agent_id` | string | 否 | 自定义 Agent ID |
|
||||
| `agent_source_tenant_id` | uint64 | 否 | 共享智能体的来源空间,同名共享智能体来自多个空间时用于区分 |
|
||||
| `reasoning_effort` | string | 否 | 本轮思考强度:`off`、`auto`、`minimal`、`low`、`medium`、`high`、`xhigh`、`max`(`none`/`false` 视为 `off`,`true`/`on` 视为 `auto`);省略时沿用智能体配置。只作用于本轮,不修改智能体;模型不支持所选档位时自动调整到相近档位。非法值返回 400 |
|
||||
| `web_search_enabled` | bool | 否 | 联网搜索;只在智能体本身开启联网搜索时生效 |
|
||||
| `local_browser_enabled` | bool | 否 | 本轮允许使用已连接的本机浏览器;只能用于智能推理智能体的 `agent-chat`,否则返回 400。见[本机浏览器](../05-clients/09-local-browser.md) |
|
||||
| `summary_model_id` | string | 否 | 总结模型;使用共享智能体时忽略,始终使用智能体配置的模型 |
|
||||
| `mcp_service_ids` | []string | 否 | @提及的 MCP 服务 |
|
||||
| `skill_names` | []string | 否 | @提及的技能 |
|
||||
| `tag_ids` | []string | 否 | 标签过滤 |
|
||||
| `mentioned_items` | []object | 否 | @提及项(type/kb_id/kb_name/service_id/skill_name) |
|
||||
| `disable_title` | bool | 否 | 禁用自动标题 |
|
||||
| `images` | []object | 否 | 图片(`data` base64 / `url` / `caption`) |
|
||||
| `attachment_uploads` | []object | 否 | 内联附件(`data` base64、`file_name`、`file_size`) |
|
||||
| `attachment_ids` | []string | 否 | 已上传的会话附件 ID |
|
||||
| `channel` | string | 否 | 来源渠道 |
|
||||
| `suggestion_attribution` | object | 否 | 点击建议的归因信息 |
|
||||
| `question_origin` | object | 否 | 用户点选的建议问题来自哪里:`knowledge_base_id`、可选 `knowledge_id`。仅智能推理模式使用,智能体会先检索该来源;来源不在本轮检索范围内时忽略,不会扩大范围 |
|
||||
| `page_context` | object | 否 | 当前页面:`knowledge_base_id`、可选 `knowledge_id`、`wiki_slug`、`wiki_issue_ids`(最多保留 20 项),或 `agent_id`。知识库必须属于本轮检索范围;智能体独立校验,必须在当前租户可见(支持内置智能体)。无效部分丢弃,不扩大检索权限 |
|
||||
| `assistant_access` | string | 否 | Academy 助手权限等级:`read_only`、`confirm`、`auto_write`,默认 `confirm`,受空间上限约束 |
|
||||
|
||||
所选 `reasoning_effort` 会写入会话的 `last_request_state.reasoning_effort`,重新打开会话时前端据此恢复。
|
||||
|
||||
响应:200 SSE 流,`event: message` + `data: StreamResponse`(见总览),以 `complete` 事件结束。回答因模型单次输出上限被截断时,`answer` 事件带 `data.truncated: true`,截断前已生成的内容照常保留。
|
||||
|
||||
```bash
|
||||
curl -N -X POST $BASE/api/v1/knowledge-chat/s-1 -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"query":"退款政策是什么?","knowledge_base_ids":["kb-1"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/agent-chat/:session_id
|
||||
|
||||
用途:Agent 问答(SSE 流式,含 `thinking/tool_call/tool_result/tool_approval_required/mcp_oauth_required` 等事件)。请求体同上。
|
||||
|
||||
- 工具执行失败以 `tool_result` 事件返回(`data.success: false`,`data.error` 为原因),智能体会继续处理;`error` 事件只表示整轮执行失败。
|
||||
- 智能体中途接收追加消息时发出 `user_message_injected`;命令执行过程中以 `command_output` 更新工具卡片输出。
|
||||
- 同一会话已有智能推理回答在生成时返回 409,此时应改用 [steer 接口](#steer)追加消息。
|
||||
|
||||
```bash
|
||||
curl -N -X POST $BASE/api/v1/agent-chat/s-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"分析上季度数据","agent_id":"agent-1"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-search
|
||||
|
||||
用途:无会话知识检索(非流式),外部系统取检索结果的首选接口。和产品内问答走同一条检索流程(召回 → rerank → 合并 → 截断),排序与页面问答一致。和 `hybrid-search` 怎么选见[检索接口怎么选](./01-api-overview.md#retrieval-api)。Handler: `internal/handler/session/qa.go` 的 `SearchKnowledge`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query` | string | 是(`binding:"required"`) | 查询 |
|
||||
| `knowledge_base_id` | string | 否 | 单 KB(兼容旧版) |
|
||||
| `knowledge_base_ids` | []string | 否 | 多 KB |
|
||||
| `knowledge_ids` | []string | 否 | 指定文件,与 `knowledge_base_id(s)` 取并集(见下方说明) |
|
||||
| `tag_ids` | []string | 否 | 标签过滤 |
|
||||
| `mentioned_items` | []object | 否 | 带 KB 范围的标签提及 |
|
||||
| `vector_threshold` / `keyword_threshold` | float | 否 | 召回阈值;省略时用空间检索配置(默认 0.15 / 0.3) |
|
||||
| `match_count` | int | 否 | 返回条数,上限 200;省略时用空间配置的 `rerank_top_k`(默认 10)。召回深度会自动加大到不小于它 |
|
||||
| `disable_keywords_match` / `disable_vector_match` | bool | 否 | 关闭某一路召回;两个都为 `true` 返回 400 |
|
||||
| `rerank` | object | 否 | 覆盖 rerank 设置;`{"enabled":false}` 关闭 rerank。字段见 [rerank 对象](./01-api-overview.md#retrieval-api)。`rerank.top_k` 同时给出时优先于 `match_count` |
|
||||
|
||||
省略的字段都沿用空间的检索配置(`GET /tenants/kv/retrieval-config`),不传任何新字段时行为和以前一样。
|
||||
|
||||
检索范围是 `knowledge_base_id(s)` 与 `knowledge_ids` 的并集,与产品内 @ 提及的语义一致:每个知识库整库检索,`knowledge_ids` 中属于这些知识库的文件已被覆盖,不再单独限定;属于其他知识库的文件会额外加入检索。**只想在指定文件里搜时,只传 `knowledge_ids`,不要同时传它们所在的知识库**,否则会检索整个知识库。
|
||||
|
||||
响应:200 `{"success":true,"data":[SearchResult],"meta":{"rerank":{...}}}`。`SearchResult` 含 `id,content,knowledge_id,knowledge_title,score,chunk_type,knowledge_base_id,...`;经过 rerank 的结果 `metadata` 带 `model_score` 和 `base_score`。`data` 为空时看 `meta.rerank.outcome` 判断原因,见 [meta.rerank 诊断](./01-api-overview.md#retrieval-api)。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"部署要求","knowledge_base_ids":["kb-1"]}'
|
||||
|
||||
# 只用向量召回、取 5 条,并关闭 rerank
|
||||
curl -X POST $BASE/api/v1/knowledge-search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"query":"部署要求","knowledge_base_ids":["kb-1"],"disable_keywords_match":true,"match_count":5,"rerank":{"enabled":false}}'
|
||||
|
||||
# 只在指定文件里搜(不传 knowledge_base_ids)
|
||||
curl -X POST $BASE/api/v1/knowledge-search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"部署要求","knowledge_ids":["k-1","k-2"]}'
|
||||
```
|
||||
|
||||
## 消息(/api/v1/messages)
|
||||
|
||||
Handler: `internal/handler/message.go`
|
||||
|
||||
### POST /api/v1/messages/search
|
||||
|
||||
用途:聊天历史搜索。权限:Viewer+;API key `message_history`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query` | string | 是(`binding:"required"`) | 查询 |
|
||||
| `mode` | string | 否 | `keyword/vector/hybrid`(默认 hybrid) |
|
||||
| `limit` | int | 否 | 默认 20 |
|
||||
| `session_ids` | []string | 否 | 限定会话 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"total":N,"results":[{session_id,message_id,role,content,created_at,score}]}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/messages/search -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"query":"报价"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/messages/chat-history-stats
|
||||
|
||||
用途:聊天历史索引统计。权限:Viewer+;API key `message_history`/full。
|
||||
|
||||
响应:200 `{"success":true,"data":{indexed_message_count,knowledge_base_size,last_indexed_at,...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/messages/chat-history-stats -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/messages/:session_id/load
|
||||
|
||||
用途:加载会话消息(时间游标向前翻页)。权限:Viewer+;API key `chat`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `limit` | int | 否 | 默认 20 |
|
||||
| `before_time` | string | 否 | RFC3339/RFC3339Nano 时间戳 |
|
||||
|
||||
响应:200 `{"success":true,"data":[Message]}`(`id,session_id,role,content,is_completed,images,attachments,agent_steps,...`)
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/messages/s-1/load?limit=20" -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/messages/:session_id/:id
|
||||
|
||||
用途:删除单条消息。权限:Viewer+(handler 校验会话归属);API key `chat`/full。
|
||||
|
||||
响应:200 `{"success":true,"message":"Message deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/messages/s-1/m-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 会话生成文件
|
||||
|
||||
以下接口要求 Viewer+,API Key 需 chat 或 full-access,并按会话归属校验。不存在或不可访问的会话返回 404。
|
||||
|
||||
| 方法 | 路径 | 响应 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/api/v1/sessions/:id/artifacts` | 200 `{success:true,data:[Artifact]}`,汇总会话文件 |
|
||||
| GET | `/api/v1/sessions/:id/messages/:message_id/artifacts` | 同上,仅本条消息文件 |
|
||||
| GET | `/api/v1/sessions/:id/messages/:message_id/artifacts/:index/download` | 200 文件流,Content-Disposition: attachment |
|
||||
| DELETE | `/api/v1/sessions/:id/messages/:message_id/artifacts/:index` | 200 `{success:true,data:{file_name,deleted}}`,删除该文件 |
|
||||
|
||||
Artifact 字段:index、handle(可选 resource:// 引用)、file_name、file_type、file_size、source_path、mod_time、created_at。响应不返回底层对象存储 URL。下载 index 从 0 开始,必须使用对应消息列表的索引,不能拿会话汇总索引直接拼消息下载地址。非法索引返回 400,越界或文件不存在返回 404。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/sessions/session-1/messages/message-1/artifacts" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
curl "$BASE/api/v1/sessions/session-1/messages/message-1/artifacts/0/download" \
|
||||
-H "Authorization: Bearer $TOKEN" -o result.pdf
|
||||
```
|
||||
|
||||
### 删除生成的文件
|
||||
|
||||
删除会回收对象存储中的字节,**不可恢复**。与下载不同,删除只对会话归属人开放:通过共享智能体获得的只读访问可以下载文件,但不能删除 —— 不属于自己的会话一律按 404 处理(不区分「不存在」和「无权限」,与其余会话接口一致)。已删除的文件返回 404,重复删除同样返回 404。
|
||||
|
||||
字节只有在没有任何其他持有者时才会真正回收:同一份文件被存入知识库、被后续回答重新引用,或者所在会话被分叉出副本,都会让它保留下来。回收失败不影响删除结果(接口仍返回 200),文件在各处列表中都已消失。
|
||||
|
||||
删除后该文件在列表接口和产物库中都不再出现,但它在消息中的**位置会被保留**:`index` 就是下载地址,如果后面的文件依次前移,已有的下载链接就会指向错的文件。同一原因,沙箱里的同名文件不会在下一轮采集时被重新收录 —— 它的 mtime 并没有因为用户删除而改变。
|
||||
|
||||
| 参数 | 说明 |
|
||||
| --- | --- |
|
||||
| `all_versions` | 连同本会话中同一 `source_path` 的所有历史版本一并删除。布尔值,默认 `false` |
|
||||
|
||||
```bash
|
||||
curl -X DELETE "$BASE/api/v1/sessions/session-1/messages/message-1/artifacts/0" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### 跨会话产物列表
|
||||
|
||||
`GET /api/v1/artifacts` 列出当前用户网页对话中的所有生成文件,供首页侧栏「产物」页使用。范围与会话列表的 `source=web` 一致:本人会话及历史上无归属的租户级网页会话;IM 渠道、网页挂件(embed)和 API Key 会话一律不含,即使 IM 会话在库中没有归属人。同一会话中 `source_path` 相同的文件视为同一文件的多个版本,只返回最新一版,`version_count` 给出版本数。已删除的会话或消息中的文件不返回。权限要求与上表相同。
|
||||
|
||||
| 参数 | 说明 |
|
||||
| --- | --- |
|
||||
| `keyword` | 按文件名过滤,不区分大小写 |
|
||||
| `file_types` | 逗号分隔的扩展名,如 `.pdf,.pptx`(可省略点号) |
|
||||
| `page` / `page_size` | 分页,`page_size` 最大 100,默认 20 |
|
||||
|
||||
响应 `{success, data:[LibraryArtifact], total, page, page_size}`,按生成时间倒序。LibraryArtifact 字段:session_id、session_title、message_id、index、handle(可选)、file_name、file_type、file_size、source_path、created_at、version_count。下载时用其中的 session_id、message_id、index 调用上表的下载接口。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/artifacts?file_types=.pptx,.pdf&keyword=报告&page=1&page_size=30" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
`DELETE /api/v1/artifacts` 删除产物库中的一个文件,用 query 参数 `session_id`、`message_id`、`index` 定位,语义与上面的会话内删除一致。区别只在于 `all_versions` 缺省时视为 `true`(显式传值时两个接口的解析规则相同):产物库一行代表一个文件(`version_count` 给出版本数)而不是某一次生成,只删最新一版会让这一行继续留在列表里、显示上一版。传 `all_versions=false` 可只删当前这一版。
|
||||
|
||||
```bash
|
||||
curl -X DELETE "$BASE/api/v1/artifacts?session_id=session-1&message_id=message-1&index=0" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### 回答中的图片和文件引用
|
||||
|
||||
`GET /api/v1/sessions/:id/messages/:message_id/files?file_path=...` 是消息级鉴权代理。file_path 传该消息引用的资源句柄或受支持的存储引用,客户端应 URL 编码。后端校验消息访问权、资源与消息的绑定及知识库/共享 Agent 的当前访问权;任意文件路径不能凭会话 ID 访问。授权撤销后旧消息引用也会被拒绝。适用于共享 Agent、组织共享库回答图片与消息产物,详见[文件访问](../03-features/21-file-access.md)。
|
||||
|
||||
### 每轮用量
|
||||
|
||||
消息返回持久化的 usage,Agent 完成事件携带 turn_usage;包含本轮各用途模型调用聚合结果。工具调用自身并不都产生 Token,用量以提供商返回或后端已采集的记录为准。字段见[可观测性](../03-features/16-observability.md)。
|
||||
|
||||
智能体模式的完成事件在 `data.model_id` 中携带本轮实际运行的对话模型 ID(产生本轮用量的模型),可据此通过模型接口取得模型信息、按单价换算用量。消息记录中的 `model_id` 是请求时绑定的模型,请求指定的模型无效而回退到智能体模型时两者不同。快速问答的完成事件暂不携带该字段。
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_chat.go` 的 `RegisterSessionRoutes`、`RegisterChatRoutes`、`RegisterMessageRoutes`(由 `internal/router/router.go` 调用)。Handler:`internal/handler/session/`(handler.go、qa.go、stream.go、title.go、temporary_document.go、fork.go、rewind.go、steer.go、artifact_*.go)、`internal/handler/message.go`、`internal/handler/message_suggestion.go`。
|
||||
@@ -0,0 +1,224 @@
|
||||
# API 参考:分块与标签
|
||||
|
||||
分块(chunk)是检索的最小单元,标签用于给文档分类。两组接口均属于知识库资源,与[知识库与知识](./02-api-knowledge.md)共用同一套权限规则:读为 Viewer+ 且对父 KB 有 read 权限(API key `retrieve`),写为「KB 创建者 OR Admin+」且有 write 权限(API key `ingest`),均受 API key 的 KB 白名单约束。
|
||||
|
||||
通用约定(Base URL、认证、错误码、分页)见 [API 总览](./01-api-overview.md)。
|
||||
|
||||
## 分块(/api/v1/chunks)
|
||||
|
||||
Handler: `internal/handler/chunk.go`。读:Viewer+ 且父 KB read(API key `retrieve`/full);写:KB 创建者 OR Admin+ 且父 KB write(API key `ingest`/full)。
|
||||
|
||||
### GET /api/v1/chunks/:knowledge_id
|
||||
|
||||
用途:知识的分块列表。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page` | int | 否 | 默认 1 |
|
||||
| `page_size` | int | 否 | 默认 10,上限 100 |
|
||||
| `chunk_type` | string | 否 | 可重复,按分块类型过滤 |
|
||||
|
||||
响应:200 `{"success":true,"data":[Chunk],"total","page","page_size"}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/chunks/k-1?page=1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/chunks/by-id/:id
|
||||
|
||||
用途:按 chunk ID 获取单个分块(无需 knowledge_id)。
|
||||
|
||||
响应:200 `{"success":true,"data":{Chunk}}`。`Chunk.source_locators` 是分块在原始文件中的位置,结构见 [API 概览](01-api-overview.md)中的 `source_locators` 说明。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/chunks/by-id/c-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/chunks/:knowledge_id/:id
|
||||
|
||||
用途:编辑分块内容或启停状态(migration `000078` 起为带版本的乐观更新)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `content` | string | 否 | 新内容,去除首尾空白后不得为空,长度上限 200000 字节 |
|
||||
| `is_enabled` | bool | 否 | 启用/停用该分块 |
|
||||
| `expected_revision` | int | 否 | 期望的 `content_revision`,用于乐观并发控制 |
|
||||
|
||||
约束与副作用:
|
||||
|
||||
- 仅 `text` 类型分块可编辑,其它类型返回 400;
|
||||
- `expected_revision` 与当前 `content_revision` 不一致时返回 **409**(`Chunk was modified by another user; refresh and retry`);
|
||||
- 不允许在编辑中引入源内容里没有的图片 URL;删除 Markdown 图片会同步停用对应的 OCR/caption 子分块;
|
||||
- 编辑成功后 `content_revision` +1,旧版本写入 `chunk_revisions` 表,`index_status` 依次经历 `processing` → `ready`;重建检索索引失败时行仍保存但 `index_status = failed`,可再次提交同样内容触发重试;
|
||||
- 子分块编辑会按偏移量回写父分块内容(父分块的 `source_content` 保持不可变);
|
||||
- 内容或启停状态变化会入队一次文档摘要刷新。
|
||||
|
||||
响应:200 `{"success":true,"data":{Chunk},"summary_status":"pending","description":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/chunks/k-1/c-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"content":"修正后的内容","expected_revision":2}'
|
||||
```
|
||||
|
||||
### GET /api/v1/chunks/:knowledge_id/:id/revisions
|
||||
|
||||
用途:分块的历史版本列表(`chunk_revisions` 表,按 revision 倒序)。
|
||||
|
||||
响应:200 `{"success":true,"data":[{ChunkRevision}]}`,单条包含 `revision`、`content`、`is_enabled`、`editor_id`、`edit_source`、`edited_at`。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/chunks/k-1/c-1/revisions -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/chunks/:knowledge_id/:id/revert
|
||||
|
||||
用途:回滚到某个历史版本。回滚本身也是一次新编辑:`content_revision` 继续递增,当前内容会被存为新的历史版本。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `revision` | int | 是 | 目标历史版本号(非负) |
|
||||
| `expected_revision` | int | 否 | 乐观锁,语义同上,冲突返回 409 |
|
||||
|
||||
响应:200 `{"success":true,"data":{Chunk},"summary_status":"...","description":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/chunks/k-1/c-1/revert -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"revision":1}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/chunks/:knowledge_id/:id
|
||||
|
||||
用途:删除单个分块。
|
||||
|
||||
响应:200 `{"success":true,"message":"Chunk deleted"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/chunks/k-1/c-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/chunks/:knowledge_id
|
||||
|
||||
用途:删除知识下全部分块。
|
||||
|
||||
响应:200 `{"success":true,"message":"All chunks under knowledge deleted"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/chunks/k-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/chunks/by-id/:id/questions
|
||||
|
||||
用途:删除该分块下某条生成的问题。请求体:`{"question_id":"..."}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true,"message":"Generated question deleted"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/chunks/by-id/c-1/questions -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"question_id":"q-1"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/chunks/by-id/:id/questions
|
||||
|
||||
用途:新增或修改该分块的一条生成问题。请求体:`{"question":"...","question_id":"..."}`,`question` 必填;`question_id` 留空表示新增。
|
||||
|
||||
响应:200 `{"success":true,"data":{GeneratedQuestion}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/chunks/by-id/c-1/questions -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"question_id":"q-1","question":"Academy 如何配置向量库?"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/chunks/by-id/:id/questions/regenerate
|
||||
|
||||
用途:基于分块当前内容重新生成检索问题。内容编辑后原有问题不会被删除,而是标记为「过期」(revision 与当前正文不匹配),可用本接口刷新。
|
||||
|
||||
响应:200 `{"success":true,"data":[{GeneratedQuestion}]}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/chunks/by-id/c-1/questions/regenerate -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 标签(/api/v1/knowledge-bases/:id/tags)
|
||||
|
||||
Handler: `internal/handler/tag.go`。读:Viewer+ + KB read(API key `retrieve`/full);写:KB 创建者 OR Admin+ + KB write(API key `ingest`/full)。
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/tags
|
||||
|
||||
用途:标签列表。查询参数:`page`、`page_size`、`keyword`(均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":[KnowledgeTag]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/tags -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/tags
|
||||
|
||||
用途:创建标签。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 标签名 |
|
||||
| `color` | string | 否 | 颜色 |
|
||||
| `sort_order` | int | 否 | 排序 |
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeTag}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/tags -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"售后"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/tags/:tag_id
|
||||
|
||||
用途:更新标签(`tag_id` 支持 UUID 或整数 seq_id)。请求体:`name`/`color`/`sort_order`(指针字段,均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeTag}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/tags/t-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"售后支持"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge-bases/:id/tags/:tag_id
|
||||
|
||||
用途:删除标签。查询参数:`force`(bool,连同仍引用该标签的文档或 FAQ 条目一起删除)、`content_only`(bool,仅删除标签下的内容、保留标签)。请求体(可选):`{"exclude_ids":[int64]}`,列出删除时保留的 FAQ 条目 seq_id。不传 `force` 时,仍有知识或 FAQ 引用该标签会返回 400,文档不会被删除。
|
||||
|
||||
请求体格式不合法或 ID 非正整数返回 400;ID 不存在返回 404;ID 不属于当前知识库的 FAQ 条目返回 403。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge-bases/kb-1/tags/t-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 分块调试
|
||||
|
||||
### POST /api/v1/chunker/preview
|
||||
|
||||
用途:无状态分块预览(KB 编辑器调试面板)。权限:Viewer+;API key `retrieve`/`ingest`/full。Handler: `internal/handler/chunker_debug.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `text` | string | 是(代码校验非空,≤64k 字符) | 样例文本 |
|
||||
| `chunking_config.chunk_size` | int | 否 | 分块字符数 |
|
||||
| `chunking_config.chunk_overlap` | int | 否 | 重叠 |
|
||||
| `chunking_config.separators` | []string | 否 | 分隔符 |
|
||||
| `chunking_config.strategy` | string | 否 | `auto/heading/heuristic/recursive/legacy` |
|
||||
| `chunking_config.token_limit` | int | 否 | token 上限 |
|
||||
| `chunking_config.languages` | []string | 否 | 语言提示 |
|
||||
| `chunking_config.enable_parent_child` | bool | 否 | 按父子分块试切,返回的是子块(与检索粒度一致) |
|
||||
| `chunking_config.parent_chunk_size` / `child_chunk_size` | int | 否 | 父/子块大小,缺省 4096 / 384 |
|
||||
|
||||
样例文本中的内联 HTML `<table>` 会先转换为 Markdown 表格,与入库时的处理一致。
|
||||
|
||||
响应:200 `{"success":true,"data":{"selected_tier","tier_chain","rejected","profile","chunks":[...],"stats":{count,avg_chars,min_chars,max_chars,stddev_chars,truncated_to}}}`;文本超长 413;分块超时(5s)504。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/chunker/preview -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"text":"# 标题\n正文...","chunking_config":{"chunk_size":512}}'
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_knowledge.go` 的 `RegisterChunkRoutes`、`RegisterKnowledgeTagRoutes`、`RegisterChunkerDebugRoutes`。
|
||||
@@ -0,0 +1,501 @@
|
||||
# API 参考:FAQ 与 Wiki
|
||||
|
||||
管理知识库中的 FAQ 条目与 Wiki 页面,支持导入、检索、编辑和版本恢复。
|
||||
|
||||
两组均为 KB 内容子资源:读为 Viewer+ 且 KB read(API key `retrieve`/full);写为“KB 创建者 OR Admin+”且 KB write(API key `ingest`/full),并受 KB 白名单约束。跨库 Wiki 搜索 `POST /wiki-search` 无路径 KB,Viewer+ 且 API key `retrieve`/full,handler 内校验每个目标库的 allow-list 与共享库 Viewer 权限。
|
||||
|
||||
## FAQ(/api/v1/knowledge-bases/:id/faq)
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/faq/entries
|
||||
|
||||
用途:FAQ 条目列表。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page` / `page_size` | int | 否 | 分页 |
|
||||
| `tag_id` | int | 否 | 旧版单标签 seq_id |
|
||||
| `tag_ids` | string | 否 | 逗号分隔标签 UUID |
|
||||
| `keyword` | string | 否 | 关键字 |
|
||||
| `search_field` | string | 否 | `standard_question`/`similar_questions`/`answers`(默认全字段) |
|
||||
| `sort_order` | string | 否 | `asc`(默认按更新时间倒序) |
|
||||
| `is_enabled` | bool | 否 | 按启用状态筛选:`true` 仅启用、`false` 仅停用,不传返回全部;其他取值返回 400 |
|
||||
|
||||
响应:200 `{"success":true,"data":{分页 FAQEntry 列表}}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledge-bases/kb-1/faq/entries?page=1&is_enabled=false" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/faq/entries/export
|
||||
|
||||
用途:导出 FAQ。查询参数:`format`(`csv` 默认 / `json`)。
|
||||
|
||||
响应:200 文件下载(`text/csv` 或 `application/json`)。
|
||||
|
||||
```bash
|
||||
curl -OJ "$BASE/api/v1/knowledge-bases/kb-1/faq/entries/export?format=csv" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/faq/entries/:entry_id
|
||||
|
||||
用途:FAQ 条目详情(`entry_id` 为整数 seq_id)。
|
||||
|
||||
响应:200 `{"success":true,"data":{FAQEntry}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/faq/entries/12 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/faq/entries
|
||||
|
||||
用途:批量 upsert / 导入(异步任务)。Handler 方法 `UpsertEntries`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `entries` | []FAQEntryPayload | 是(`binding:"required"`) | 批量条目 |
|
||||
| `mode` | string | 是(`binding:"oneof=append replace"`) | 追加或替换 |
|
||||
| `knowledge_id` | string | 否 | FAQ 知识实体 ID |
|
||||
| `task_id` | string | 否 | 自定义任务 ID,仅允许字母、数字、`_`、`-`,最长 128 字符,否则返回 400 |
|
||||
| `dry_run` | bool | 否 | 仅校验不落库 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"task_id"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/faq/entries -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"mode":"append","entries":[{"standard_question":"如何退款?","answers":["联系客服"]}]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/faq/entry
|
||||
|
||||
用途:创建单条 FAQ。请求体(`types.FAQEntryPayload`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `standard_question` | string | 是(`binding:"required"`) | 标准问 |
|
||||
| `similar_questions` | []string | 否 | 相似问 |
|
||||
| `negative_questions` | []string | 否 | 负样例问 |
|
||||
| `answers` | []string | 否 | 答案列表 |
|
||||
| `answer_strategy` | string | 否 | `all` / `random` |
|
||||
| `tag_id` | int64 | 否 | 标签 seq_id |
|
||||
| `tag_name` | string | 否 | 标签名 |
|
||||
| `is_enabled` / `is_recommended` | *bool | 否 | 启用/推荐 |
|
||||
|
||||
响应:200 `{"success":true,"data":{FAQEntry}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/faq/entry -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"standard_question":"如何退款?","answers":["7 天内可退"]}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/faq/entries/:entry_id
|
||||
|
||||
用途:更新单条 FAQ(请求体同创建)。
|
||||
|
||||
响应:200 `{"success":true,"data":{FAQEntry}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/faq/entries/12 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"standard_question":"如何退款?","answers":["30 天内可退"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/faq/entries/:entry_id/similar-questions
|
||||
|
||||
用途:追加相似问。请求体:`{"similar_questions":["..."]}`(`binding:"required,min=1"`)。
|
||||
|
||||
响应:200 `{"success":true,"data":{FAQEntry}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/faq/entries/12/similar-questions \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"similar_questions":["退款怎么操作"]}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/faq/entries/fields
|
||||
|
||||
用途:批量更新条目字段(`is_enabled`/`is_recommended`/`tag_id`)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `by_id` | map[int64]object | 否 | 按条目 seq_id 更新 |
|
||||
| `by_tag` | map[int64]object | 否 | 按标签批量更新 |
|
||||
| `exclude_ids` | []int64 | 否 | `by_tag` 时排除的条目 |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/faq/entries/fields -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"by_id":{"12":{"is_enabled":false}}}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/faq/entries/tags
|
||||
|
||||
用途:批量改条目标签。请求体:`{"updates":{"<entry_id>":<tag_id|null>}}`(`binding:"required,min=1"`;null 移除标签)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/faq/entries/tags -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"updates":{"12":3}}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge-bases/:id/faq/entries
|
||||
|
||||
用途:批量删除条目。请求体:`{"ids":[int64]}`(`binding:"required,min=1"`)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge-bases/kb-1/faq/entries -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"ids":[12,13]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/faq/search
|
||||
|
||||
用途:FAQ 检索(只读语义,scoped key 用 `retrieve` 亦可调用)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query_text` | string | 是(`binding:"required"`) | 查询 |
|
||||
| `vector_threshold` | float64 | 否 | 向量阈值,默认 0.7 |
|
||||
| `match_count` | int | 否 | 默认 10,上限 50 |
|
||||
| `first_priority_tag_ids` / `second_priority_tag_ids` | []int64 | 否 | 标签优先级过滤 |
|
||||
| `only_recommended` | bool | 否 | 仅推荐条目 |
|
||||
|
||||
响应:200 `{"success":true,"data":[FAQEntry(含 match_type/score)]}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/faq/search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"query_text":"退款"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/faq/import/last-result/display
|
||||
|
||||
用途:设置最近一次导入结果面板的显示状态。请求体:`{"display_status":"open|close"}`(`binding:"required,oneof=open close"`)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/faq/import/last-result/display \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d '{"display_status":"close"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/faq/import/progress/:task_id
|
||||
|
||||
用途:查询 FAQ 导入/dry-run 进度(任务按空间隔离)。权限:Viewer+;API key `retrieve`/`ingest`/full。
|
||||
|
||||
响应:200 `{"success":true,"data":{status,progress,failed_entries,...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/faq/import/progress/task-1 -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
## Wiki(/api/v1/knowledgebase/:kb_id/wiki)
|
||||
|
||||
注意此组前缀为 `/knowledgebase/:kb_id/wiki`(单数,无连字符)。Handler: `internal/handler/wiki_page.go`。本组响应多为**原始对象**(不带 `success` 包装)。
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/pages
|
||||
|
||||
用途:Wiki 页面列表。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page_type` | string | 否 | 逗号分隔类型 |
|
||||
| `status` | string | 否 | 页面状态 |
|
||||
| `query` | string | 否 | 全文搜索 |
|
||||
| `category_path` | string | 否 | `/` 分隔路径过滤 |
|
||||
| `folder_id` | string | 否 | 精确目录过滤(空串=根) |
|
||||
| `category_depth` | int | 否 | 目录深度 |
|
||||
| `page` / `page_size` | int | 否 | 分页(默认 1/20) |
|
||||
| `sort_by` / `sort_order` | string | 否 | 排序字段:`title`、`created_at`、`updated_at`、`page_type`、`wiki_path`、`sort_order`、`depth`,其他值按 `updated_at`;默认 `updated_at` desc |
|
||||
|
||||
响应:200 `WikiPageListResponse`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledgebase/kb-1/wiki/pages?page=1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledgebase/:kb_id/wiki/pages
|
||||
|
||||
用途:创建页面。请求体(`types.WikiPage`):`slug`、`title`、`content`、`folder_id`、`page_type` 等(均可选,slug 缺省自动生成)。
|
||||
|
||||
响应:201 `WikiPage`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledgebase/kb-1/wiki/pages -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"title":"架构概览","content":"# 概览"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledgebase/:kb_id/wiki/move-page
|
||||
|
||||
用途:移动页面到目录。请求体:`{"slug":"<页面slug>","folder_id":"<目录ID|空=根>"}`(slug 必填)。
|
||||
|
||||
响应:200 `WikiPage`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledgebase/kb-1/wiki/move-page -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"slug":"overview","folder_id":"f-1"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/pages/*slug
|
||||
|
||||
用途:获取页面(`*slug` 为通配路径)。
|
||||
|
||||
响应:200 `WikiPageDetail`。保留 `WikiPage` 的所有字段,并增加 `in_link_titles`,将 `in_links` 中存在的页面 slug 映射为展示标题,例如 `{"concept/example":"示例标题"}`。没有可解析的反向链接时返回空对象;不存在的目标不出现在映射中。标题查询失败时返回 500,避免返回不完整的成功响应。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/pages/overview -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledgebase/:kb_id/wiki/pages/*slug
|
||||
|
||||
用途:更新页面(请求体同创建)。旧版本会先整份快照进 `wiki_page_revisions`,`version` 递增,`last_edit_source` 记为 `user`(Agent 工具写入时为 `agent`)。
|
||||
|
||||
响应:200 `WikiPage`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledgebase/kb-1/wiki/pages/overview -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"content":"# 更新后的概览"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/revisions/*slug
|
||||
|
||||
用途:页面版本历史(migration `000075`)。权限:Viewer+ + KBAccessRead。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `version` | int | 否 | 传入时返回**该版本全文**(用于 diff),无效或 < 1 返回 400,找不到返回 404(升级前写入的版本或已被保留策略清理的快照都没有全文,属正常情况) |
|
||||
| `limit` | int | 否 | 默认 50,上限 200;仅列表模式生效 |
|
||||
| `offset` | int | 否 | 分页偏移 |
|
||||
|
||||
不带 `version` 时返回历史列表(版本号倒序、**不含正文**)加上页面当前版本号;每条含 `edit_source`(`pipeline` / `agent` / `user` / `revert`)、`editor_id`、`edited_at`。
|
||||
|
||||
历史保留是两级上限:软上限 50 版只裁剪 `pipeline` 与空来源的快照,硬上限 200 版对所有来源生效,因此人工编辑不会被管道刷掉。
|
||||
|
||||
```bash
|
||||
# 历史列表
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/revisions/entity/acme-corp -H "Authorization: Bearer $TOKEN"
|
||||
# 取第 3 版全文
|
||||
curl "$BASE/api/v1/knowledgebase/kb-1/wiki/revisions/entity/acme-corp?version=3" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledgebase/:kb_id/wiki/revert
|
||||
|
||||
用途:把页面回滚到某个历史版本。权限:KB owner 或 Admin+ + KBAccessWrite。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `slug` | string | 是 | 目标页面 |
|
||||
| `version` | int | 是 | 目标版本号(≥ 1) |
|
||||
|
||||
回滚**不会把版本号退回去**:目标版本的内容会作为一个新版本写入,`last_edit_source` 记为 `revert`,所以回滚也能被回滚。回滚到当前版本返回 400(一般是前端历史列表过期)。
|
||||
|
||||
响应:200 `WikiPage`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledgebase/kb-1/wiki/revert -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"slug":"entity/acme-corp","version":3}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledgebase/:kb_id/wiki/pages/*slug
|
||||
|
||||
用途:删除页面。
|
||||
|
||||
响应:204 No Content
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledgebase/kb-1/wiki/pages/overview -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/folders
|
||||
|
||||
用途:目录列表。查询参数:`parent_id`(空=根)、`page_types`(逗号分隔)。
|
||||
|
||||
响应:200 `WikiFolderListResponse`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/folders -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledgebase/:kb_id/wiki/folders
|
||||
|
||||
用途:创建目录。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是 | 目录名 |
|
||||
| `parent_id` | string | 否 | 父目录 |
|
||||
|
||||
响应:201 `WikiFolder`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledgebase/kb-1/wiki/folders -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"设计文档"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledgebase/:kb_id/wiki/folders/:folder_id
|
||||
|
||||
用途:重命名/移动目录。请求体:`name`、`parent_id`、`move_parent`(bool),均可选。
|
||||
|
||||
响应:200 `WikiFolder`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledgebase/kb-1/wiki/folders/f-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"架构设计"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledgebase/:kb_id/wiki/folders/:folder_id
|
||||
|
||||
用途:删除目录。
|
||||
|
||||
响应:204 No Content
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledgebase/kb-1/wiki/folders/f-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/index
|
||||
|
||||
用途:Wiki 索引页(按类型分组窗口)。查询参数:`types`(逗号分隔)、`limit`(1-200,默认 50)、`cursor`(游标)。
|
||||
|
||||
响应:200 `WikiIndexResponse`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/index -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
::: warning 已移除
|
||||
`GET /api/v1/knowledgebase/:kb_id/wiki/log`(Wiki 变更日志)已随 migration `000077_remove_wiki_log` 一并下线,`wiki_log_entries` 表被删除。Wiki 变更现在统一投影到知识库活动流,改用 `GET /api/v1/knowledge-bases/:id/activity`。
|
||||
:::
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/graph
|
||||
|
||||
用途:页面关系图。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `mode` | string | 否 | `overview`(默认)/ `ego` |
|
||||
| `center` | string | 否 | ego 模式中心 slug(ego 时必填) |
|
||||
| `depth` | int | 否 | 1-3,默认 1 |
|
||||
| `types` | string | 否 | page_type 过滤 |
|
||||
| `limit` | int | 否 | 默认 500,上限 2000 |
|
||||
|
||||
响应:200 `WikiGraphData`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledgebase/kb-1/wiki/graph?mode=overview" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/stats
|
||||
|
||||
用途:Wiki 统计。
|
||||
|
||||
响应:200 `WikiStats`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/stats -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/search
|
||||
|
||||
用途:页面搜索。查询参数:`q`(必填)、`limit`(默认 10)。
|
||||
|
||||
匹配语义随存储方言变化:
|
||||
|
||||
- Postgres:`q` 按 POSIX 正则(`~*`,大小写不敏感)匹配。
|
||||
- SQLite / Lite:按 `LIKE` 字面子串匹配;`%` / `_` 会转义,不当通配符。
|
||||
|
||||
响应:200 `{"pages":[WikiPage]}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledgebase/kb-1/wiki/search?q=部署" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/wiki-search
|
||||
|
||||
用途:跨知识库 Wiki 页面搜索(无会话、非混合检索)。Handler: `internal/handler/wiki_page.go` 的 `SearchPagesAcross`。算法与单库 `GET .../wiki/search` 相同(标题 > slug > summary > 正文),对多个库做一次查询后按 `match_rank` 全局截断。API key:`retrieve`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query` | string | 是(`binding:"required"`) | 与单库 `q` 相同:Postgres 为 POSIX 正则(大小写不敏感);SQLite / Lite 为字面 `LIKE` 子串 |
|
||||
| `knowledge_base_ids` | []string | 条件必填 | 多库;与 `knowledge_base_id` 至少提供一个,最多 32 个 |
|
||||
| `knowledge_base_id` | string | 否 | 单库兼容字段,合并进 ids |
|
||||
| `limit` | int | 否 | 全局 top-N,默认 10,最大 50 |
|
||||
|
||||
任一目标库未开启 wiki → 400。库不存在或无读权限 → 404。API Key 白名单越权 → 403。非法正则(Postgres)→ 400。
|
||||
|
||||
响应:200 `{"success":true,"data":[WikiSearchHit]}`。命中只含导航字段 + `match_snippet`,不含正文;全文用 `GET /knowledgebase/:kb_id/wiki/pages/{slug}`。
|
||||
|
||||
| 响应字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `id` | string | 页面 ID |
|
||||
| `knowledge_base_id` | string | 来源库,跨库时用来拼读页 URL |
|
||||
| `slug` | string | 页面 slug |
|
||||
| `title` | string | 标题 |
|
||||
| `page_type` | string | 页面类型 |
|
||||
| `aliases` | []string | 别名 |
|
||||
| `summary` | string | 摘要 |
|
||||
| `match_snippet` | string | 正文中首次命中附近约 60+命中+60 字;无正文命中时省略。snippet 仍用 Go 正则编译 `query`;SQLite 下 `query` 含正则元字符时库内可命中、snippet 可能为空 |
|
||||
|
||||
`content`、树/链接/元数据、时间戳不在搜索结果里。单库 `GET .../wiki/search` 仍返回完整 `WikiPage`。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/wiki-search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"query":"部署|发布","knowledge_base_ids":["kb-1","kb-2"],"limit":10}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledgebase/:kb_id/wiki/rebuild-links
|
||||
|
||||
用途:重建页面互链。写权限。无请求体。
|
||||
|
||||
响应:200 `{"message":"Links rebuilt successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledgebase/kb-1/wiki/rebuild-links -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/lint
|
||||
|
||||
用途:Wiki 一致性检查报告。
|
||||
|
||||
响应:200 `WikiLintReport`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/lint -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledgebase/:kb_id/wiki/auto-fix
|
||||
|
||||
用途:自动修复 lint 问题。写权限。无请求体。
|
||||
|
||||
响应:200 `{"fixed":N,"message":"Auto-fixed N issues"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledgebase/kb-1/wiki/auto-fix -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledgebase/:kb_id/wiki/issues
|
||||
|
||||
用途:问题列表。查询参数:`slug`(按页面过滤)、`status`(`pending/ignored/resolved`)。
|
||||
|
||||
响应:200 `[WikiPageIssue]`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledgebase/kb-1/wiki/issues -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledgebase/:kb_id/wiki/issues/:issue_id/status
|
||||
|
||||
用途:更新问题状态。写权限。请求体:`{"status":"pending|ignored|resolved"}`(`binding:"required"`)。`issue_id` 必须属于路径中的知识库,否则 404。
|
||||
|
||||
响应:200 `{"message":"Issue status updated successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledgebase/kb-1/wiki/issues/i-1/status -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"status":"resolved"}'
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_knowledge.go` 的 `RegisterFAQRoutes` 与 `RegisterWikiPageRoutes`。Handler:`internal/handler/faq.go`、`internal/handler/wiki_page.go`。
|
||||
@@ -0,0 +1,486 @@
|
||||
# API 参考:基础设施与数据源
|
||||
|
||||
注册和管理向量存储、文件存储、网络搜索服务及数据源,提供连接测试与同步操作。
|
||||
|
||||
统一约定:读 Viewer+,写/连接测试 Admin+(凭证探测外部系统)。API key capability:向量库 `manage_vector_stores`、存储后端 `manage_storage_backends`、Web 搜索 `manage_web_search`、数据源 `manage_datasources`(均可 full-access)。
|
||||
|
||||
## 向量存储(/api/v1/vector-stores)
|
||||
|
||||
### GET /api/v1/vector-stores/types
|
||||
|
||||
用途:可用引擎类型与配置 schema。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[类型定义]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/vector-stores/types -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/vector-stores/test
|
||||
|
||||
用途:用原始配置测试连接(不落库)。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `engine_type` | string | 是(`binding:"required"`) | 引擎类型 |
|
||||
| `connection_config` | object | 是(`binding:"required"`) | 连接配置 |
|
||||
|
||||
响应:200 `{"success":true|false,"version":"...","error":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/vector-stores/test -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"engine_type":"qdrant","connection_config":{"addr":"qdrant:6334"}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/vector-stores
|
||||
|
||||
用途:创建向量库配置。权限:Admin+。字段:`name`(必填)、`engine_type`(必填)、`connection_config`(必填)、`index_config`(可选)。
|
||||
|
||||
响应:201 `{"success":true,"data":{VectorStoreResponse}}`(`id,tenant_id,name,engine_type,connection_config,index_config,...`)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/vector-stores -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"qdrant-main","engine_type":"qdrant","connection_config":{"addr":"qdrant:6334"}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/vector-stores
|
||||
|
||||
用途:向量库列表(环境变量注入的 `__env_*` store 在前)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[VectorStoreResponse]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/vector-stores -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/vector-stores/:id
|
||||
|
||||
用途:向量库详情(支持 `__env_*` ID)。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{VectorStoreResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/vector-stores/vs-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/vector-stores/:id
|
||||
|
||||
用途:更新(仅重命名;env store 不可改)。权限:Admin+。请求体:`{"name":"..."}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true,"data":{VectorStoreResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/vector-stores/vs-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"qdrant-prod"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/vector-stores/:id
|
||||
|
||||
用途:删除(env store 不可删)。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/vector-stores/vs-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/vector-stores/:id/test
|
||||
|
||||
用途:测试已保存/env 向量库。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true|false,"version","error"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/vector-stores/vs-1/test -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 存储后端(/api/v1/storage-backends)
|
||||
|
||||
请求体(Create/Update/TestRaw 共用 `storageBackendRequest`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 名称 |
|
||||
| `provider` | string | 是(`binding:"required"`) | 提供方:`local`/`minio`/`cos`/`tos`/`s3`/`oss`/`ks3`/`obs`,受 `STORAGE_ALLOW_LIST` 限制 |
|
||||
| `config` | object | 否 | 提供方配置,字段见[存储后端](../03-features/19-storage-backends.md#连接参数)(响应中凭证掩码) |
|
||||
| `status` | string | 否 | `active`(默认)/`disabled` |
|
||||
|
||||
### GET /api/v1/storage-backends/types
|
||||
|
||||
用途:`STORAGE_ALLOW_LIST` 允许的 provider 名称列表(未设置时返回全部)。权限:Viewer+。响应:200 `{"success":true,"data":["local","minio",...]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/storage-backends/types -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/storage-backends/test
|
||||
|
||||
用途:原始配置连接测试。权限:Admin+。响应:200 `{"success":bool,"error"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/storage-backends/test -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"t","provider":"minio","config":{"endpoint":"minio:9000"}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/storage-backends
|
||||
|
||||
用途:创建存储后端。权限:Admin+。响应:201 `{"success":true,"data":{StorageBackend}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/storage-backends -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"minio-main","provider":"minio","config":{"endpoint":"minio:9000"}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/storage-backends
|
||||
|
||||
用途:列表(含 `default_storage_backend_id`)。权限:Viewer+。响应:200 `{"success":true,"data":[...],"default_storage_backend_id":"..."}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/storage-backends -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/storage-backends/:id
|
||||
|
||||
用途:详情(凭证掩码)。权限:Viewer+。响应:200 `{"success":true,"data":{StorageBackend}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/storage-backends/sb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/storage-backends/:id
|
||||
|
||||
用途:更新。权限:Admin+。响应:200 `{"success":true,"data":{StorageBackend}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/storage-backends/sb-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"minio-prod","provider":"minio"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/storage-backends/:id
|
||||
|
||||
用途:删除。权限:Admin+。响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/storage-backends/sb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/storage-backends/:id/test
|
||||
|
||||
用途:测试已保存后端。权限:Admin+。响应:200 `{"success":bool,"error"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/storage-backends/sb-1/test -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/storage-backends/:id/default
|
||||
|
||||
用途:设为默认后端。权限:Admin+。响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/storage-backends/sb-1/default -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## Web 搜索(/api/v1/web-search 与 /api/v1/web-search-providers)
|
||||
|
||||
当前注册 14 个搜索提供商,包括 Metaso、Exa、Bocha、Brave、Serply。各自的 api_key 与 extra_config 参数见[联网搜索](../03-features/11-web-search.md)。
|
||||
|
||||
### GET /api/v1/web-search/providers
|
||||
|
||||
用途:内置搜索提供方目录(只读)。权限:Viewer+,仅 JWT(未声明 API key 策略)。Handler: `internal/handler/web_search.go`
|
||||
|
||||
响应:200 `{"success":true,"data":[...]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/web-search/providers -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/web-search-providers/types
|
||||
|
||||
用途:提供方类型与参数 schema。权限:Viewer+。Handler: `internal/handler/web_search_provider.go`
|
||||
|
||||
响应:200 `{"success":true,"data":[...]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/web-search-providers/types -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/web-search-providers/test
|
||||
|
||||
用途:原始凭证测试(不落库)。权限:Admin+。请求体:`provider`(`binding:"required"`)、`parameters`(可选)。
|
||||
|
||||
响应:200 `{"success":bool,"error"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/web-search-providers/test -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"provider":"tavily","parameters":{"api_key":"tvly-..."}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/web-search-providers
|
||||
|
||||
用途:创建提供方配置。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 名称 |
|
||||
| `provider` | string | 是(`binding:"required"`) | 类型(bing/tavily/google…) |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `parameters` | object | 否 | 参数(api_key 建议走 credentials 子资源) |
|
||||
| `is_default` | bool | 否 | 默认提供方 |
|
||||
|
||||
响应:201 `{"success":true,"data":{WebSearchProviderResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/web-search-providers -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"tavily-main","provider":"tavily"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/web-search-providers
|
||||
|
||||
用途:提供方列表。权限:Viewer+。响应:200 `{"success":true,"data":[...]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/web-search-providers -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/web-search-providers/:id
|
||||
|
||||
用途:详情。权限:Viewer+。响应:200 `{"success":true,"data":{...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/web-search-providers/wsp-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/web-search-providers/:id
|
||||
|
||||
用途:更新(空字段保留原值;APIKey 保留)。权限:Admin+。请求体:`name/description/parameters/is_default`(均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{...}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/web-search-providers/wsp-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"is_default":true}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/web-search-providers/:id
|
||||
|
||||
用途:删除。权限:Admin+。响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/web-search-providers/wsp-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/web-search-providers/:id/credentials
|
||||
|
||||
用途:设置 API key(`{"api_key":"..."}`,省略时返回状态)。权限:Admin+。Handler: `internal/handler/web_search_provider_credentials.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"fields":{"api_key":{"configured":bool}}}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/web-search-providers/wsp-1/credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"api_key":"tvly-..."}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/web-search-providers/:id/credentials/:field
|
||||
|
||||
用途:删除凭证字段(`field` 仅 `api_key`)。权限:Admin+。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/web-search-providers/wsp-1/credentials/api_key -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/web-search-providers/:id/test
|
||||
|
||||
用途:测试已保存提供方。权限:Admin+。响应:200 `{"success":bool,"error"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/web-search-providers/wsp-1/test -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 数据源(/api/v1/datasource)
|
||||
|
||||
外部内容连接器(Feishu/Notion/语雀等),同步任务会写入 KB。Handler: `internal/handler/datasource.go`。本组多数响应为原始对象/数组(无 `success` 包装)。
|
||||
|
||||
当前已注册类型为 feishu、lark、feishu_drive、lark_drive、notion、confluence、yuque、dingtalk、ima、rss、gitlab。各连接器的 credentials、资源选择与同步限制见[数据源导入](../03-features/10-datasource.md)。sync_deletions 开启后会真实删除该数据源归属下的已删除知识;source_created_at/source_updated_at 保存在知识 metadata 中。
|
||||
|
||||
### GET /api/v1/datasource/types
|
||||
|
||||
用途:可用连接器目录。权限:Viewer+。
|
||||
|
||||
响应:200 `[{type,name,description,icon,priority,auth_type,capabilities}]`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/datasource/types -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource/validate-credentials
|
||||
|
||||
用途:校验原始凭证(“测试连接”按钮,不落库)。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `type` | string | 是(`binding:"required"`) | 连接器类型 |
|
||||
| `credentials` | map | 是(`binding:"required"`) | 凭证 |
|
||||
|
||||
响应:200 `{"status":"connected"}`;失败 400 `{"error":"..."}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource/validate-credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"type":"notion","credentials":{"api_key":"ntn_xxx"}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource
|
||||
|
||||
用途:创建数据源。权限:Admin+。请求体(`types.DataSource`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `knowledge_base_id` | string | 是 | 目标 KB(须归属本空间) |
|
||||
| `name` | string | 是 | 名称 |
|
||||
| `type` | string | 是 | 连接器类型 |
|
||||
| `config` | object | 是 | 凭证(加密存储)+资源选择+设置 |
|
||||
| `sync_schedule` | string | 否 | cron 表达式 |
|
||||
| `sync_mode` | string | 否 | `incremental`(默认)/`full` |
|
||||
| `conflict_strategy` | string | 否 | `overwrite`(默认)/`skip` |
|
||||
| `sync_deletions` | bool | 否 | 默认 true |
|
||||
| `sync_log_retention_days` | int | 否 | 本数据源同步日志保留天数,默认 30;非正值按 30 天处理 |
|
||||
|
||||
响应:201 `DataSourceResponse`(凭证剥离,见 `internal/handler/dto/datasource.go`)。
|
||||
|
||||
后台每日凌晨(服务所在时区)清理过期的 `success`、`partial`、`canceled` 同步日志,包括暂停和仅手动同步的数据源。保留期从完成时间计算,旧日志缺少完成时间时使用开始时间。运行中或等待执行的日志不清理;现有数据源的 `failed` 日志也会保留,因为该状态可能仍在等待任务重试。数据源软删除后继续沿用原保留期,并可清理 `failed` 日志;无来源日志按默认 30 天保留。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"knowledge_base_id":"kb-1","name":"notion 同步","type":"notion","config":{}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/datasource
|
||||
|
||||
用途:数据源列表。权限:Viewer+。查询参数:`kb_id`(必填)。
|
||||
|
||||
响应:200 `[DataSourceResponse]`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/datasource?kb_id=kb-1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/datasource/:id
|
||||
|
||||
用途:详情。权限:Viewer+。响应:200 `DataSourceResponse`;404 `{"error":"data source not found"}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/datasource/ds-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/datasource/:id
|
||||
|
||||
用途:更新(`id/tenant_id/knowledge_base_id` 锁定为原值)。权限:Admin+。请求体同创建。
|
||||
|
||||
PUT 整体替换数据源配置,不支持部分更新:请先 GET 详情,修改后回传完整对象(`name`、`type`、`config`、`sync_schedule`、`sync_mode`、`conflict_strategy`、`sync_deletions`、`sync_log_retention_days`、`status` 等)。`sync_schedule` 和 `sync_deletions` 总是按请求体写入:省略 `sync_schedule` 会清空为空字符串(仅手动同步,已有定时任务随之移除),省略 `sync_deletions` 即为 false;其他字段省略时是否保留原值不作保证。凭证不经本接口修改,请求体中的 `config.credentials` 会被忽略,保留已存凭证。
|
||||
|
||||
响应:200 `DataSourceResponse`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/datasource/ds-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"name":"notion 同步 v2","type":"notion","knowledge_base_id":"kb-1",
|
||||
"config":{"type":"notion","resource_ids":["page-1"]},
|
||||
"sync_schedule":"0 0 */6 * * *","sync_mode":"incremental","conflict_strategy":"overwrite",
|
||||
"sync_deletions":true,"sync_log_retention_days":30,"status":"active"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/datasource/:id
|
||||
|
||||
用途:删除。权限:Admin+。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/datasource/ds-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/datasource/:id/credentials
|
||||
|
||||
用途:整体替换凭证(数据源凭证为“单一逻辑字段 `credentials`”的原子 map)。权限:Admin+。请求体:`{"credentials":{...}}`(非空 map 必填)。Handler: `internal/handler/datasource_credentials.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"fields":{"credentials":{"configured":bool}}}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/datasource/ds-1/credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"credentials":{"api_key":"ntn_xxx"}}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/datasource/:id/credentials/:field
|
||||
|
||||
用途:清空凭证(`field` 必须为 `credentials`)。权限:Admin+。响应:204。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/datasource/ds-1/credentials/credentials -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource/:id/validate
|
||||
|
||||
用途:校验已保存数据源连接。权限:Admin+。响应:200 `{"status":"connected"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource/ds-1/validate -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/datasource/:id/resources
|
||||
|
||||
用途:浏览外部资源树(懒加载)。权限:Admin+。查询参数:`parent_id`(可选,空=顶层)。
|
||||
|
||||
响应:200 `[{external_id,name,type,description,url,modified_at,parent_id,has_children,metadata}]`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/datasource/ds-1/resources?parent_id=" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource/:id/resource-ancestors
|
||||
|
||||
用途:解析资源祖先链(选择器展开)。权限:Admin+。请求体:`{"resource_ids":["..."]}`(必填)。
|
||||
|
||||
响应:200 `{"ancestors":[...]}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource/ds-1/resource-ancestors -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"resource_ids":["page-1"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource/:id/sync
|
||||
|
||||
用途:手动触发同步。权限:Admin+。响应:200 `SyncLog`(`id,status,started_at,items_total,items_created,items_updated,items_deleted,items_failed,...`)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource/ds-1/sync -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/datasource/:id/pause 与 POST /api/v1/datasource/:id/resume
|
||||
|
||||
用途:暂停 / 恢复定时同步。权限:Admin+。
|
||||
|
||||
响应:200 `{"status":"paused"}` / `{"status":"active"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/datasource/ds-1/pause -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/datasource/:id/logs
|
||||
|
||||
用途:同步日志列表。权限:Viewer+。查询参数:`limit`(默认 10,上限 100)、`offset`(默认 0)。
|
||||
|
||||
响应:200 `[SyncLog]`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/datasource/ds-1/logs?limit=10" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/datasource/logs/:log_id
|
||||
|
||||
用途:单条同步日志。权限:Viewer+。响应:200 `SyncLog`;404 `{"error":"sync log not found"}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/datasource/logs/log-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_infra.go` 的 `RegisterVectorStoreRoutes`、`RegisterStorageBackendRoutes`、`RegisterWebSearchRoutes`、`RegisterWebSearchProviderRoutes`、`RegisterDataSourceRoutes`。Handler:`internal/handler/vectorstore.go`、`internal/handler/storagebackend.go`、`internal/handler/web_search.go`、`internal/handler/web_search_provider.go`、`internal/handler/web_search_provider_credentials.go`、`internal/handler/datasource.go`、`internal/handler/datasource_credentials.go`。
|
||||
@@ -0,0 +1,807 @@
|
||||
# API 参考:知识库与知识
|
||||
|
||||
创建知识库,导入与管理文档,并查询处理进度、复制或移动内容。
|
||||
|
||||
权限速记:读路由为 Viewer+ 且需对 KB 有 read 权限(自有/组织共享/共享 Agent 可见);写路由为“KB 创建者 OR Admin+”且需 write 权限。API key:读需 `retrieve`,内容写需 `ingest`,KB 生命周期需 `manage_kbs`(均可被 full-access 覆盖),并受 KB 白名单约束。
|
||||
|
||||
分块、标签与分块预览接口(`/chunks`、`/knowledge-bases/:id/tags`、`/chunker/preview`)在[分块与标签](./02-api-chunks.md)。
|
||||
|
||||
## 知识库(/api/v1/knowledge-bases)
|
||||
|
||||
### POST /api/v1/knowledge-bases
|
||||
|
||||
用途:创建知识库。权限:Contributor+;API key `manage_kbs`/full。Handler: `internal/handler/knowledgebase.go`
|
||||
|
||||
请求体(`types.KnowledgeBase`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是 | 名称 |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `type` | string | 否 | `document`(默认)/`faq`/`wiki` |
|
||||
| `embedding_model_id` | string | 否 | Embedding 模型 ID |
|
||||
| `chunking_config` | object | 否 | 分块配置(chunk_size/overlap/separators/strategy…) |
|
||||
| `image_processing_config` | object | 否 | 图片处理配置:`model_id` / `image_attrs_enabled` / `image_actions`(`{ ocr: { on: [...], on_unobserved: bool } }`)/ `image_vector_enabled`(图片向量索引与召回,默认 false,见[知识库功能](../03-features/02-knowledge-base.md)) |
|
||||
| `storage_provider_config` | object | 否 | 存储配置 |
|
||||
| `vector_store_id` | string | 否 | 向量库绑定(非法返回 code 2200/2201) |
|
||||
| `faq_config` / `wiki_config` / `extract_config` / `indexing_strategy` | object | 否 | 类型相关配置 |
|
||||
| `summary_model_id` | string | 否 | 摘要模型,也是自动标签和 AI 描述的默认模型 |
|
||||
| `auto_tag_config` / `profile_config` | object | 否 | 自动标签、AI 知识库描述(仅 document 类型,见下文) |
|
||||
|
||||
响应:201 `{"success":true,"data":{KnowledgeBase}}`
|
||||
|
||||
`indexing_strategy` 里没写到的开关保持默认:向量和关键词开启,Wiki 和图谱关闭。显式传 `false` 才会关掉。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"产品文档","type":"document"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/graph-search {#graph-search}
|
||||
|
||||
独立的只读 JSON 图谱查询。按 `query` 对实体名称做**区分大小写的子串匹配**,返回匹配实体、其一跳邻居、定向关系和来源分块;也返回没有关系的匹配实体。无需把 Agent 对话输出重新解析成结构化数据。
|
||||
|
||||
权限:Viewer+ 且对该 KB 有 read 权限(自有、组织共享或共享 Agent)。API key 需 `retrieve`/full,并受 KB 白名单约束。共享 Agent 可带 `agent_id` / `agent_source_tenant_id`,授权规则与 KB 详情一致。
|
||||
|
||||
本接口不调用 Chat、Embedding 或 Rerank 模型,不修改数据,也不改变 `knowledge-search` / `hybrid-search` 的默认行为。自然语言实体抽取、跨库搜索、多跳遍历不在此接口范围内。
|
||||
|
||||
| JSON 字段 | 默认值 | 上限 / 含义 |
|
||||
| --- | --- | --- |
|
||||
| `query` | 必填 | 去除首尾空白后 1–512 字符;不是 Cypher |
|
||||
| `max_nodes` | 50 | 100,包含匹配实体和邻居 |
|
||||
| `max_relations` | 100 | 200 |
|
||||
| `max_source_chunks` | 20 | 50,来源按 ID 去重 |
|
||||
|
||||
上限字段 `0` 使用默认值;负数或超过上限返回 400。请求体最多 8 KiB,仅接收一个 JSON 对象,未知字段会被拒绝。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/knowledge-bases/$KB_ID/graph-search" \
|
||||
-H "X-API-Key: $API_KEY" -H 'Content-Type: application/json' \
|
||||
-d '{"query":"Acme","max_nodes":50,"max_relations":100,"max_source_chunks":20}'
|
||||
```
|
||||
|
||||
响应:200 `{"success":true,"data":{...}}`。`data` 字段:
|
||||
|
||||
- `knowledge_base_id`、`query`:本次作用域和规范化查询。
|
||||
- `entities`:`id`、`name`、`knowledge_id`、`attributes`、`source_chunk_ids`。ID 是不透明的文档实体身份,不暴露 Neo4j element ID;同名但不同文档的实体不会合并。名称/所属文档不变时,重建图存储不会改变公共 ID。
|
||||
- `relationships`:`id`、`source_id`、`target_id`、`type`、`source_chunk_ids`。方向为存储方向,不随查询命中哪一端而反转;两端必须实际出现在 `entities`。来源列表是两端实体来源的并集,不表示每个分块都直接陈述该关系。
|
||||
- `source_chunks`:`id`、`knowledge_id`、`title`、`content`、`chunk_index`、`start_at`、`end_at`、`content_truncated`、`title_truncated`。正文最多 2000 字符,标题最多 512 字符;截断时保留原始位置字段,不伪装成完整正文。无匹配结果时三个列表均为 `[]`。
|
||||
- `meta`:`truncated`、`truncation_reasons` 和本次生效的三个上限。它不是全图总量统计,也不提供分页游标;需要更完整的结果时应缩小实体名范围。
|
||||
|
||||
为限制模糊查询成本,内部最多检查 100 个名称匹配种子、512 条一跳关系和 512 个来源分块;查询执行最多 10 秒。候选种子按名称完全匹配优先、名称长度、名称、文档身份排序。属性最多 20 项,每项最多 1000 字符。
|
||||
|
||||
`truncation_reasons` 的值包括 `candidate_seeds`、`candidate_relations`、`candidate_source_chunks`、`max_nodes`、`max_relations`、`max_source_chunks`、`entity_fields`、`relation_fields`、`source_fields`。只有实际触达的上限才标注,不推测被省略结果的精确数量。
|
||||
|
||||
来源校验采用保守规则:实体引用的**所有**分块都必须属于当前授权 KB 的该文档,且文档/分块未删除、未禁用,文档不在删除中、分块当前索引就绪;任一来源失效即省略该实体及相关关系。关系两端还必须属于同一文档。无法校验或因预算未检查的图谱事实不会作为已验证结果返回。这可能降低部分旧图谱的召回率;查询不会自动重建图谱,也不能保证模型抽取的事实本身正确。
|
||||
|
||||
状态:400 参数错误;401 未认证;403 无读取权限/超出 key 范围;404 KB 不存在;409 KB 未开启图谱;500 来源数据库校验失败;503 图谱未配置、连接失败或查询超时。错误不附带部分图谱结果。部署配置见[知识图谱](../03-features/09-knowledge-graph.md)。
|
||||
|
||||
### 自动标签与 AI 描述配置
|
||||
|
||||
创建知识库时 `auto_tag_config`、`profile_config` 位于顶层;更新时放在 `config.auto_tag_config`、`config.profile_config`。两者仅 document 知识库支持,默认 enabled=false。
|
||||
|
||||
`auto_tag_config`(自动标签):
|
||||
|
||||
| 字段 | 类型 | 默认值 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `enabled` | bool | false | 解析后异步从已有标签中选择 |
|
||||
| `model_id` | string | 空 | 为空时使用知识库 `summary_model_id` |
|
||||
| `max_tags` | int | 3 | 每篇最多关联数量,上限 10 |
|
||||
| `skip_if_tagged` | bool | true | 已有标签则跳过;false 允许补充标签 |
|
||||
|
||||
开启后对新解析/重新解析的文档生效,不自动扫描全部旧文档。无候选标签或无可用模型时不阻断入库。
|
||||
|
||||
`profile_config`(AI 知识库描述):
|
||||
|
||||
| 字段 | 类型 | 默认值 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `enabled` | bool | false | 开启后,文档新增、删除、移动或摘要更新会自动刷新 `generated_profile` |
|
||||
| `model_id` | string | 空 | 为空时使用知识库 `summary_model_id` |
|
||||
| `custom_instructions` | string | 空 | 追加到生成提示词的补充要求 |
|
||||
|
||||
`generated_profile` 为只读字段,由系统写入,不覆盖手写 `description`;也可通过下文的 `profile/generate` 立即生成。更新示例:
|
||||
|
||||
```bash
|
||||
curl -X PUT "$BASE/api/v1/knowledge-bases/kb-1" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"name":"产品文档","config":{"auto_tag_config":{"enabled":true,"max_tags":3,"skip_if_tagged":true}}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases
|
||||
|
||||
用途:知识库列表。权限:Viewer+;API key `retrieve`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `agent_id` | string | 否 | 过滤某共享 Agent 可见的 KB |
|
||||
| `agent_source_tenant_id` | uint64 | 否 | 同名 Agent 被多个空间共享时,指定来源空间;取值会与共享关系校验,非法值直接 400 |
|
||||
| `creator` | string | 否 | `mine` / `others` |
|
||||
|
||||
响应:200 `{"success":true,"data":[KnowledgeBase],"total","page","page_size"}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id
|
||||
|
||||
用途:知识库详情(共享 KB 携带 `my_permission`)。权限:Viewer+,KB read。查询参数:`agent_id`(可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeBase}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id
|
||||
|
||||
用途:更新知识库。权限:创建者 OR Admin+,KB write;API key `manage_kbs`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 名称 |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `config` | object | 否 | 局部配置更新:`chunking_config`、`image_processing_config`、`faq_config`、`wiki_config`、`auto_tag_config`、`profile_config`、`indexing_strategy`。`image_processing_config` 不传则保持不变,传了则整体替换(未带的 `image_vector_enabled` 等字段回到 false) |
|
||||
| `vlm_config` | object | 否 | 多模态配置,整体替换:只取 `enabled`、`model_id`(须为 VLLM 模型)、`description_language`、`custom_instructions`(≤4000 字),未传的字段按空值处理;不想改就不要传。共享库需所有者空间或共享 admin 权限(editor 返回 403)。旧版内联字段 `model_name`/`base_url`/`api_key`/`interface_type` 不会被写入:启用托管 `model_id` 或关闭时会清空已存的旧版配置,但纯旧版库(未存 `model_id`,只存了 `model_name`/`base_url`)原样回传 GET 结果(`enabled=false` 且 `model_name`/`base_url` 与存储一致)视为不改;其余库 `enabled=true` 却缺 `model_id` 返回 400 |
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeBase}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"产品文档 v2"}'
|
||||
```
|
||||
|
||||
### PATCH /api/v1/knowledge-bases/:id
|
||||
|
||||
用途:只修改请求里出现的设置,其余保持不变。这包括名称、描述、模型、存储、分块、索引开关、FAQ、Wiki、自动标签、AI 描述、图片、语音和图谱提取里的字段:嵌套对象中没写到的字段也保持原值。权限与 PUT 相同;修改嵌入模型、摘要模型、多模态或语音时,共享知识库还需要管理员共享权限。存储实例只能由知识库所属空间修改,且知识库中已有文件时必须先迁移存储;共享管理员可以原样回传当前存储 ID,但不能更换。`embedding_model_id` 在知识库已有文档时不能改。`vector_store_id` 只能在创建时指定。`name` 传了就不能为空;`description` 传空字符串会清空描述。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 否 | 新名称 |
|
||||
| `description` | string | 否 | 新描述;空字符串清空 |
|
||||
| `embedding_model_id` / `summary_model_id` / `storage_backend_id` | string | 否 | 模型或存储实例 |
|
||||
| `chunking_config` | object | 否 | 只覆盖写到的分块字段 |
|
||||
| `indexing_strategy` | object | 否 | 只覆盖写到的开关;至少保留一种索引 |
|
||||
| `vlm_config` / `asr_config` / `image_processing_config` | object | 否 | 多模态、语音、图片 |
|
||||
| `faq_config` / `wiki_config` / `auto_tag_config` / `profile_config` | object | 否 | 类型相关设置 |
|
||||
| `extract_config` / `question_generation_config` | object | 否 | 图谱提取、分块问题生成 |
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeBase}}`
|
||||
|
||||
```bash
|
||||
curl -X PATCH $BASE/api/v1/knowledge-bases/kb-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"产品文档 v2"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge-bases/:id
|
||||
|
||||
用途:删除知识库(锁定为属主空间 + Admin;共享 editor 不可删)。权限:创建者 OR Admin+,KB write;API key `manage_kbs`/full。
|
||||
|
||||
响应:200 `{"success":true,"message":"Knowledge base deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge-bases/kb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/pin
|
||||
|
||||
用途:置顶/取消置顶(按用户维度存储)。权限:Viewer+,KB read。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{KnowledgeBase(is_pinned 已切换)}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/pin -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/hybrid-search(兼容 GET)
|
||||
|
||||
用途:KB 内的底层召回(向量+关键词),默认不做 rerank,返回召回分;可选开启 rerank。适合评测召回、传预计算向量等需要控制原始召回的场景,一般的检索请用 [`knowledge-search`](./02-api-chat.md),选择方法见[检索接口怎么选](./01-api-overview.md#retrieval-api)。权限:Viewer+,KB read;API key `retrieve`/full。GET 携带 JSON body 仅为向后兼容(#1727),推荐 POST。
|
||||
|
||||
查询参数:`resource_urls=handle|public`(`public` 把结果 `content` / `image_info` 里的 `resource://` 换成可加载直链,详见 [API 总览](./01-api-overview.md))。
|
||||
|
||||
请求体(`types.SearchParams`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `query_text` | string | 条件必填 | 查询文本(除非提供 `query_embedding`;开启 rerank 时必填) |
|
||||
| `query_embedding` | []float32 | 否 | 预计算向量 |
|
||||
| `vector_threshold` / `keyword_threshold` | float64 | 否 | 匹配阈值 |
|
||||
| `match_count` | int | 否 | 返回条数上限(默认 50) |
|
||||
| `disable_keywords_match` / `disable_vector_match` | bool | 否 | 关闭某一路召回 |
|
||||
| `knowledge_base_ids` | []string | 否 | 一次检索多个知识库,路径上的 `:id` 必须在其中;这些知识库的 embedding 模型必须相同,否则返回 400 |
|
||||
| `knowledge_ids` | []string | 否 | 限定知识条目 |
|
||||
| `tag_ids` | []string | 否 | 标签过滤(OR) |
|
||||
| `only_recommended` | bool | 否 | FAQ 仅推荐条目 |
|
||||
| `skip_context_enrichment` | bool | 否 | 跳过父块/上下文补齐 |
|
||||
| `rerank` | object | 否 | 传入即开启 rerank(`{}` 使用空间配置的模型),字段见 [rerank 对象](./01-api-overview.md#retrieval-api) |
|
||||
|
||||
响应:200 `{"success":true,"data":[SearchResult]}`;带 `rerank` 时多一个 `meta.rerank`(见 [meta.rerank 诊断](./01-api-overview.md#retrieval-api))。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/knowledge-bases/kb-1/hybrid-search?resource_urls=public" -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"query_text":"退款流程","match_count":5}'
|
||||
|
||||
# 固定召回参数,再用指定模型 rerank
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/hybrid-search -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"query_text":"退款流程","vector_threshold":0.3,"match_count":5,"rerank":{"model_id":"rr-1","threshold":0.2}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/copy
|
||||
|
||||
用途:跨 KB 拷贝内容(异步任务)。权限:Contributor+;API key `manage_kbs`/full(源/目标 KB 白名单在 handler 校验)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `source_id` | string | 是(`binding:"required"`) | 源 KB |
|
||||
| `target_id` | string | 否 | 目标 KB(为空则自动创建) |
|
||||
| `task_id` | string | 否 | 自定义任务 ID |
|
||||
|
||||
响应:200 `{"success":true,"data":{"task_id","source_id","target_id","message"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/copy -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"source_id":"kb-1"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/duplicate
|
||||
|
||||
用途:创建 KB 副本(仅复制设置,不复制内容/索引/分享)。权限:Contributor+,源 KB read;API key `manage_kbs`/full。无请求体。
|
||||
|
||||
响应:201 `{"success":true,"data":{"source_id","target_id","message","knowledge_base":{...}}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/duplicate -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/profile/generate
|
||||
|
||||
用途:立即重新生成知识库的 AI 描述(`generated_profile`),同步执行一次文档画像聚合和一次小模型调用,不修改手写 `description`。权限:与更新知识库相同(创建者/Admin 且 KB write);API key `manage_kbs`/full。无请求体。仅 document 类型;未配置模型返回 400。
|
||||
|
||||
响应:200 `{"success":true,"data":{"gist","topics":[...],"typical_questions":[...],"stats":{"document_count",...},"status":"ready","model_id","generated_at"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/profile/generate -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/copy/progress/:task_id
|
||||
|
||||
用途:查询拷贝进度(任务按空间隔离)。权限:Viewer+;API key `retrieve`/`manage_kbs`/full。
|
||||
|
||||
响应:200 `{"success":true,"data":{status,progress,message,...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/copy/progress/task-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/move-targets
|
||||
|
||||
用途:列出可作为移动目标的 KB(同类型/同 embedding)。权限:Viewer+,KB read。
|
||||
|
||||
响应:200 `{"success":true,"data":[KnowledgeBase]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/move-targets -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/files
|
||||
|
||||
用途:KB 范围文件代理(渲染共享 KB 内容中的图片;上下文 tenant 已被重写为 KB 属主)。权限:Viewer+,KB read;KB 受限 key 拒绝,全空间 `retrieve`/full key 放行。注册于 `serveKBScopedFiles`(`internal/router/files.go`)。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `file_path` | string | 是 | `provider://...` 存储路径(禁止 `..`) |
|
||||
|
||||
响应:200 文件流(`Content-Type` 按扩展名推断;`Cache-Control: private`)。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledge-bases/kb-1/files?file_path=local://1/exports/chart.png" \
|
||||
-H "Authorization: Bearer $TOKEN" -o chart.png
|
||||
```
|
||||
|
||||
## 知识(KB 内容,/api/v1/knowledge-bases/:id/knowledge 与 /api/v1/knowledge)
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/knowledge/file
|
||||
|
||||
用途:上传文件创建知识。权限:KB 创建者 OR Admin+,KB write;API key `ingest`/full。Handler: `internal/handler/knowledge.go`
|
||||
|
||||
multipart/form-data 字段:
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `file` | file | 是 | 上传文件 |
|
||||
| `fileName` | string | 否 | 自定义文件名或包含目录的相对路径,如 `docs/spec/design.md`;目录部分用于归类,最后一段作为文件名 |
|
||||
| `metadata` | JSON 字符串 | 否 | 自定义元数据 |
|
||||
| `enable_multimodel` | bool | 否 | 多模态处理开关 |
|
||||
| `tag_ids` | string | 否 | 逗号分隔标签 ID |
|
||||
| `channel` | string | 否 | 摄取渠道 |
|
||||
| `process_config` | JSON 字符串 | 否 | 解析配置覆盖(KnowledgeProcessOverrides),见下表 |
|
||||
|
||||
`process_config` 常用字段(均可选,省略时沿用知识库配置):
|
||||
|
||||
| 字段 | 类型 | 默认值 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `summary_enabled` | bool | true | 是否为本次导入的文档生成摘要;关闭后解析、索引及其他处理照常执行 |
|
||||
| `parser_engine_rules` | []object | 知识库配置 | 按文件类型指定解析引擎 |
|
||||
| `parser_engine_overrides` | map[string]string | 空 | 引擎参数,如 `pdf_force_scanned` |
|
||||
| `chunking_config` | object | 知识库配置 | 分块参数 |
|
||||
| `enable_multimodel` / `vlm_config` / `asr_config` | - | 知识库配置 | 多模态与语音识别 |
|
||||
| `question_generation_config` | object | 知识库配置 | 问题生成 |
|
||||
| `graph_enabled` / `extract_config` | - | 知识库配置 | 图谱抽取 |
|
||||
|
||||
响应:200 `{"success":true,"data":{Knowledge}}`;重复文件返回 409 且 `data` 为已存在的 Knowledge。正在删除或解析失败的同名文件不计为重复。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/knowledge/file \
|
||||
-H "X-API-Key: $API_KEY" -F 'file=@./manual.pdf' -F 'enable_multimodel=true'
|
||||
```
|
||||
|
||||
**上传到指定文件夹:** 在 `fileName` 中传入相对于知识库根目录的路径,使用 `/` 分隔目录,并保留文件名及扩展名。上传接口不单独接收 `folder_path` 字段。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/knowledge-bases/kb-1/knowledge/file" \
|
||||
-H "X-API-Key: $API_KEY" \
|
||||
-F 'file=@./design.md' \
|
||||
-F 'fileName=docs/spec/design.md'
|
||||
```
|
||||
|
||||
上述请求将文档归类到 `docs/spec`,返回的 Knowledge 中 `folder_path` 为 `docs/spec`、`file_name` 为 `design.md`。`file=@./design.md` 指定本地文件,`fileName` 指定知识库中的归类路径。省略 `fileName` 或仅传文件名时,文档位于知识库根目录。
|
||||
|
||||
文档知识库的目录由文档的 `folder_path` 推导,无需预先创建:上传到新路径后,目录及其中间层级会出现在目录树中。当前不支持独立创建、保留没有任何文档或子目录文档的空文件夹。Wiki 的创建目录接口管理的是 Wiki 目录,不用于文档知识库。
|
||||
|
||||
上传后可用本页的 `GET /api/v1/knowledge-bases/:id/knowledge/folders` 查看目录树。已有文档可通过 `POST /api/v1/knowledge/folder` 移到新路径,无需重新上传;该移动接口才使用 `folder_path` 请求字段。
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/knowledge/url
|
||||
|
||||
用途:从 URL 抓取创建知识。权限/API key 同上。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `url` | string | 是(`binding:"required"`) | 抓取地址 |
|
||||
| `file_name` / `file_type` / `title` | string | 否 | 覆盖信息 |
|
||||
| `enable_multimodel` | *bool | 否 | 多模态开关 |
|
||||
| `tag_ids` | []string | 否 | 标签 |
|
||||
| `channel` | string | 否 | 渠道 |
|
||||
| `process_config` | object | 否 | 解析覆盖 |
|
||||
|
||||
响应:201 `{"success":true,"data":{Knowledge}}`;重复 URL 返回 409。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/knowledge/url -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"url":"https://example.com/doc"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/knowledge/manual
|
||||
|
||||
用途:创建手工(Markdown)知识。权限/API key 同上。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `title` | string | 否 | 标题 |
|
||||
| `content` | string | 否 | Markdown 内容 |
|
||||
| `status` | string | 否 | `draft` / `publish` |
|
||||
| `tag_ids` | []string | 否 | 标签 |
|
||||
| `channel` | string | 否 | 渠道 |
|
||||
| `process_config` | object | 否 | 解析覆盖 |
|
||||
|
||||
响应:200 `{"success":true,"data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/knowledge/manual -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"title":"FAQ 汇总","content":"# 内容","status":"publish"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/knowledge
|
||||
|
||||
用途:KB 下知识列表。权限:Viewer+,KB read;API key `retrieve`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `page` / `page_size` | int | 否 | 分页(默认 1/20) |
|
||||
| `tag_ids` | string | 否 | 逗号分隔标签(OR) |
|
||||
| `keyword` | string | 否 | 关键字 |
|
||||
| `file_type` | string | 否 | 文件类型过滤 |
|
||||
| `parse_status` | string | 否 | `pending/processing/completed/failed` |
|
||||
| `source` | string | 否 | 渠道或 `manual`/`url` |
|
||||
| `start_time` / `end_time` | string | 否 | RFC3339,按 `updated_at` 过滤 |
|
||||
| `folder_path` | string | 否 | 按文件夹筛选;空字符串表示知识库根目录,不传则不按文件夹过滤 |
|
||||
| `folder_recursive` | bool | 否 | 与 `folder_path` 配合,为 `true` 时包含子文件夹中的文档 |
|
||||
| `sort_by` | string | 否 | 排序字段:`updated_at`、`created_at` 或 `file_name`;默认 `created_at` |
|
||||
| `sort_order` | string | 否 | 排序方向:`asc` 或 `desc`;默认 `desc` |
|
||||
|
||||
未传排序参数时按 `created_at desc` 排序,取值不在上述范围内返回 400。使用 `updated_at` 时,重新解析、编辑或状态变化会影响顺序;使用 `file_name` 时按展示文件名忽略大小写排序,文件名为空会依次回退到标题和来源。相同排序值按知识 ID 排序,保证翻页结果稳定。
|
||||
|
||||
响应:200 `{"success":true,"data":[Knowledge],"total","page","page_size"}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledge-bases/kb-1/knowledge?page=1&parse_status=completed" -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/knowledge/batch-download
|
||||
|
||||
用途:把同一知识库中的多个文档原始文件打包为 ZIP 下载。权限与单文件下载相同:Contributor+ 且 KB write(组织共享 Viewer 不可下载);API key `retrieve`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `ids` | []string | 是 | 知识 ID 列表,1~200 个 |
|
||||
|
||||
行为:
|
||||
|
||||
- 原始文件合计不超过 512 MiB,超出返回 400;
|
||||
- 没有原始文件的条目(如网页导入)会被跳过;所选条目都没有原始文件时返回 400;
|
||||
- ZIP 内保留知识库文件夹结构,重名文件自动加序号;
|
||||
- 任一 ID 不存在或不属于该知识库返回 404,读取失败返回 500,不会生成缺文件的压缩包;
|
||||
- 同一实例同时最多处理 4 个批量下载,超出返回 429。
|
||||
|
||||
响应:200 `application/zip` 文件流,文件名形如 `knowledge-files-20260923-150405.zip`。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/knowledge/batch-download \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"ids":["k-1","k-2"]}' -o knowledge-files.zip
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/knowledge/folders
|
||||
|
||||
用途:获取知识库的文件夹目录树。整目录上传时目录结构会被保留(migration `000079` 起存在 `knowledges.folder_path` 列,历史 `file_name` 中的路径已回填到该字段)。权限:Viewer+ + KBAccessRead。
|
||||
|
||||
目录树由知识条目的 `folder_path` 汇总生成;没有直接文档但包含子目录文档的中间目录也会返回。目录下最后一份文档及所有子目录文档移走后,该目录不再出现在树中。
|
||||
|
||||
响应:200。示例(仅有上述上传的 `docs/spec/design.md`):
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
"root_document_count": 0,
|
||||
"total_document_count": 1,
|
||||
"folders": [{
|
||||
"path": "docs",
|
||||
"name": "docs",
|
||||
"document_count": 0,
|
||||
"total_count": 1,
|
||||
"children": [{
|
||||
"path": "docs/spec",
|
||||
"name": "spec",
|
||||
"document_count": 1,
|
||||
"total_count": 1
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`root_document_count` 为知识库根目录中的文档数,`total_document_count` 为知识库总文档数;目录节点的 `document_count` 仅统计直接文档,`total_count` 包含所有子目录文档。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/knowledge/folders -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/knowledge/folders
|
||||
|
||||
用途:重命名或移动文件夹,连同其所有子目录一起改路径。目标路径已存在时两个文件夹合并;不允许移动到自己的子目录下。权限:KB owner 或 Admin+ + KBAccessWrite。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `from` | string | 是 | 原路径 |
|
||||
| `to` | string | 是 | 新路径 |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/knowledge/folders -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"from":"设计文档/旧版","to":"归档/设计文档"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge-bases/:id/knowledge
|
||||
|
||||
用途:清空 KB 全部内容(破坏性)。权限:Admin+,KB write;API key 仅 full-access。
|
||||
|
||||
响应:200 `{"success":true,"message":"Knowledge base contents clear task submitted","data":{"deleted_count":N}}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge-bases/kb-1/knowledge -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/batch
|
||||
|
||||
用途:按 ID 批量获取知识(跨 KB,handler 自行校验访问)。权限:Viewer+;API key `retrieve`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `ids` | []string | 是 | 知识 ID(可重复传参或逗号分隔) |
|
||||
| `kb_id` | string | 否 | 限定 KB |
|
||||
| `agent_id` | string | 否 | 共享 Agent 范围 |
|
||||
| `agent_source_tenant_id` | uint64 | 否 | 共享 Agent 的来源空间选择器,与共享关系校验 |
|
||||
|
||||
响应:200 `{"success":true,"data":[Knowledge]}`。处于 `pending`/`processing`/`finalizing` 的知识额外带 `last_activity_at`(RFC3339),取行的 `updated_at` 与该知识所有 span 最近一次写入中较晚的一个。超过 20 分钟无进展的知识再带 `stall_state`:`queued` 表示仍有任务在 asynq 队列或 Wiki 持久队列中等待(积压),`stalled` 表示已无任务可推进它(疑似卡住)。判定与 housekeeping 的积压判定相同;队列侧是一次全队列扫描,所有请求共享、缓存 60 秒。探测失败时不返回 `stall_state`,前端按普通解析中显示。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledge/batch?ids=k-1&ids=k-2" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/:id
|
||||
|
||||
用途:知识详情。权限:Viewer+,父 KB read。
|
||||
|
||||
响应:200 `{"success":true,"data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge/k-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/:id/stages 与 GET /api/v1/knowledge/:id/spans
|
||||
|
||||
用途:解析阶段/trace(两条路径同一 handler `GetKnowledgeSpans`)。权限:Viewer+,父 KB read。查询参数:`attempt`(int,0=最新一次)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"knowledge_id","attempt","latest_attempt","parse_status","current_stage","last_activity_at","stall_state","trace":{...},"last_error":{...}}}`
|
||||
|
||||
`last_activity_at` 只在解析进行中返回,取行的 `updated_at` 与本次 attempt 各 span 最近一次写入中较晚的一个;`stall_state` 含义同上。`current_stage` 是仍在运行的阶段;没有运行中的阶段时(如 `finalizing`,后处理阶段已关闭、摘要等子任务仍在跑),取仍在运行的子 span 所属的阶段。被 housekeeping 判定卡死的知识,其卡住位置的 span 会以 `TASK_STALLED` 标为失败,`last_error` 优先指向它。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge/k-1/spans -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge/:id
|
||||
|
||||
用途:删除知识(异步)。权限:KB 创建者 OR Admin+,KB write;API key `ingest`/full。
|
||||
|
||||
响应:200 `{"success":true,"message":"Delete task submitted","data":{"task_id"}}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge/k-1 -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge/:id
|
||||
|
||||
用途:更新知识元信息。权限同上。请求体(`types.Knowledge` 子集):`title`、`description`、`tags`、`custom_metadata`(均可选)。description 省略保持原摘要,显式空字符串清空摘要,非空值保存手工摘要;界面可在文档内容页编辑。
|
||||
|
||||
`custom_metadata` 是用户自填的描述性元数据(与系统内部使用的 `metadata` 分开存放,migration `000078`),校验规则见 `internal/application/service/knowledge.go`:
|
||||
|
||||
| 约束 | 值 |
|
||||
| --- | --- |
|
||||
| 字段数 | ≤ 20 |
|
||||
| 键长度 | 1-64 字符,不能为空白 |
|
||||
| 值类型 | string / number / boolean / null |
|
||||
| 值长度 | ≤ 1000 字符 |
|
||||
|
||||
整体覆盖式更新(传入的对象替换原有对象)。元数据发生变化且该文档已有摘要时,会自动入队一次摘要刷新(`summary_status` 转为 `pending`)。元数据文本会参与摘要生成与文档级模型上下文(`Knowledge.CustomMetadataText()`)。
|
||||
|
||||
响应:200 `{"success":true,"message":"Knowledge updated successfully","data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge/k-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"title":"新标题","custom_metadata":{"部门":"研发中心","密级":"内部","版本":3}}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/:id/regenerate-summary
|
||||
|
||||
用途:在分块内容或自定义元数据被编辑后,重新生成该文档的摘要。权限:KB owner 或 Admin+,且对父 KB 有 write 权限。
|
||||
|
||||
行为分两种:文档此前没有摘要(`summary_status` 为空或 `none`)时同步触发一次生成;已有摘要时改为入队刷新任务,`summary_status` 转为 `pending`,由 `knowledge_summary_refresh.go` 异步执行。
|
||||
|
||||
响应:200 `{"success":true,"data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/k-1/regenerate-summary -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge/manual/:id
|
||||
|
||||
用途:整篇替换手工录入的 Markdown。只适用于手工知识;上传文件不能用这个接口改正文。`content` 是完整正文,空内容会被拒绝。`status` 为 `draft`(不进入检索)或 `publish`(建立索引);不传 `status` 时按草稿保存。权限同上。
|
||||
|
||||
响应:200 `{"success":true,"data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge/manual/k-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"content":"# 更新内容","status":"publish"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/:id/reparse
|
||||
|
||||
用途:重新解析知识。权限同上。请求体(可选):`{"process_config":{...}}`。
|
||||
|
||||
响应:200 `{"success":true,"message":"Reparse task submitted","data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/k-1/reparse -H "X-API-Key: $API_KEY"
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/:id/cancel-parse
|
||||
|
||||
用途:取消解析。权限同上。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"message":"Knowledge parse cancelled","data":{Knowledge}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/k-1/cancel-parse -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/:id/download
|
||||
|
||||
用途:下载原始源文件(比预览更严格:Contributor+ 且 KB write;组织共享 Viewer 不可下载源文件)。API key `retrieve`/full。
|
||||
|
||||
响应:200 二进制流(`application/octet-stream`)。
|
||||
|
||||
```bash
|
||||
curl -OJ $BASE/api/v1/knowledge/k-1/download -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/:id/preview
|
||||
|
||||
用途:预览解析后的文件内容。权限:Viewer+,KB read。
|
||||
|
||||
响应:200 预览流(文本/HTML)。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge/k-1/preview -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge/image/:id/:chunk_id
|
||||
|
||||
用途:更新某分块的图片信息(caption/OCR 等)。权限:KB 创建者 OR Admin+,KB write。路径参数:`id` 知识 ID、`chunk_id` 分块 ID。请求体为图片信息 JSON。
|
||||
|
||||
响应:200 `{"success":true,...}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge/image/k-1/c-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"caption":"架构图"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/search
|
||||
|
||||
用途:跨 KB 文件搜索(会话 @文件 选择器)。权限:Viewer+;API key `retrieve`/full。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `keyword` / `query` | string | 条件必填 | 关键字(两者等价);为空时必须传 `recent=true`,否则返回 400 |
|
||||
| `file_types` | string | 否 | 逗号分隔的扩展名过滤,如 `csv,xlsx` |
|
||||
| `offset` / `limit` | int | 否 | 分页;`limit` 默认 20,范围 1~100 |
|
||||
| `recent` | bool | 否 | 关键字为空时返回最近文件 |
|
||||
| `agent_id` | string | 否 | 共享 Agent 范围 |
|
||||
| `agent_source_tenant_id` | uint64 | 否 | 共享 Agent 的来源空间选择器,与共享关系校验 |
|
||||
|
||||
响应:200 `{"success":true,"data":[Knowledge],"has_more":bool,"total":N}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/knowledge/search?keyword=报告&limit=20" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge/move/progress/:task_id
|
||||
|
||||
用途:查询移动任务进度。权限:Viewer+;API key `retrieve`/full。
|
||||
|
||||
响应:200 `{"success":true,"data":{MoveProgress}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge/move/progress/task-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge/tags
|
||||
|
||||
用途:批量更新知识标签。权限:Contributor+;API key `ingest`/full(KB 白名单在 handler 校验)。两种写法互斥:`updates`,或 `knowledge_ids` 加 `tag_ids`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `updates` | map[string][]string | 与 `knowledge_ids` 二选一 | knowledge_id → tag_ids,替换该文档现有标签 |
|
||||
| `kb_id` | string | 否 | 限定知识库;不传时从第一条知识推断 |
|
||||
| `knowledge_ids` | []string | 与 `updates` 二选一 | 最多 20 篇,同一组标签 |
|
||||
| `tag_ids` | []string | 使用 `knowledge_ids` 时必填 | 最多 50 个;空数组清空标签 |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge/tags -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"updates":{"k-1":["t-1"]},"kb_id":"kb-1"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/batch-reparse
|
||||
|
||||
用途:批量重解析。权限:Contributor+;API key `ingest`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `kb_id` | string | 是(`binding:"required"`) | KB ID |
|
||||
| `ids` | []string | 是(`binding:"required"`) | 知识 ID 列表 |
|
||||
| `process_config` | object | 否 | 解析覆盖 |
|
||||
|
||||
响应:200 `{"success":true,"message":"Batch reparse task submitted","data":{"task_id"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/batch-reparse -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' -d '{"kb_id":"kb-1","ids":["k-1","k-2"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/batch-delete
|
||||
|
||||
用途:批量删除(≤200 条)。权限:Contributor+;API key `ingest`/full。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `kb_id` | string | 是(`binding:"required"`) | KB ID |
|
||||
| `ids` | []string | 是(`binding:"required"`) | 知识 ID 列表(≤200) |
|
||||
|
||||
响应:200 `{"success":true,"message":"Batch delete task submitted","data":{"task_id","deleted_count"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/batch-delete -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"kb_id":"kb-1","ids":["k-1"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/folder
|
||||
|
||||
用途:把若干文档归类到指定文件夹(只改归类,不动知识库归属,也不重新解析)。权限:Contributor+ / API key `ingest`。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `kb_id` | string | 是 | 知识库 ID |
|
||||
| `knowledge_ids` | []string | 是 | 待移动的文档 |
|
||||
| `folder_path` | string | 否 | 相对于知识库根目录的目标文件夹路径,如 `docs/spec`;新路径无需预先创建,空字符串表示移回知识库根目录 |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/folder -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"kb_id":"kb-1","knowledge_ids":["k-1","k-2"],"folder_path":"设计文档"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/knowledge/move
|
||||
|
||||
用途:跨 KB 移动知识(异步)。权限:Contributor+;API key `ingest`/full(源+目标 KB 均需在白名单)。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `knowledge_ids` | []string | 是(`binding:"required,min=1"`) | 待移动知识 |
|
||||
| `source_kb_id` | string | 是(`binding:"required"`) | 源 KB |
|
||||
| `target_kb_id` | string | 是(`binding:"required"`) | 目标 KB |
|
||||
| `mode` | string | 是(`binding:"required,oneof=reuse_vectors reparse"`) | 复用向量或重解析 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"task_id","source_kb_id","target_kb_id","knowledge_count","message"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge/move -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"knowledge_ids":["k-1"],"source_kb_id":"kb-1","target_kb_id":"kb-2","mode":"reuse_vectors"}'
|
||||
```
|
||||
|
||||
## 画廊与图片属性 {#image-gallery-api}
|
||||
|
||||
画廊读取要求 Viewer+、目标知识库可读;API Key 需 `retrieve` 或 full,并遵守知识库范围。画廊与图片向量开关独立,列表返回已提取图片的描述、OCR 和来源。
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/gallery-config
|
||||
|
||||
返回 `{"success":true,"data":{"attribute_sources":[...],"attributes":[...],"mode":"...","status":{...}}}`。客户端据返回的属性定义和使用策略构造搜索、筛选与排序;不要硬编码可用属性。属性 ID 带来源前缀,例如 `builtin:caption`、`system:contain.text`。
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/images
|
||||
|
||||
| 查询参数 | 说明 |
|
||||
| --- | --- |
|
||||
| `page` / `page_size` | 默认 1 / 20,page_size 最大 1000 |
|
||||
| `keyword` / `search_in` | 忽略大小写的子串搜索;search_in 为逗号分隔的可搜索属性 ID,默认描述与 OCR |
|
||||
| `sort_by` / `sort_order` | 可排序属性 ID,默认 created_at / desc;兼容 created_at、updated_at、caption 旧值 |
|
||||
| `knowledge_ids` | 逗号分隔的来源文档 ID;GET 只取前 50 个,更多文档请使用 POST |
|
||||
| `is_enabled` | true / false,按所属分块启用状态筛选 |
|
||||
| `attr_filters` | JSON 对象,属性 ID → 允许值数组,同属性 OR、不同属性 AND |
|
||||
| `attr_rules` | JSON 对象,属性 ID → 值 → on/off;on 强制显示优先于 off,未命中规则的图片保留 |
|
||||
|
||||
只接受 gallery-config 声明适用的搜索、筛选、排序字段,不适用字段会被剔除。响应 `{"success":true,"data":[ImageAsset],"total":N,"page":1,"page_size":20}`。每项包含 `id`、`chunk_id`、`knowledge_id`、`source_name`、`url`、`original_url`、`caption`、`ocr_text`、`attrs`、`is_enabled`、`status` 与时间戳。
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/images
|
||||
|
||||
与 GET 相同的只读查询,参数改放 JSON body;`knowledge_ids`、`search_in` 用字符串数组,`attr_filters` / `attr_rules` 直接传对象。来源文档最多 1000 个,超出返回 400,适合来源筛选全选,避免 URL 过长。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/knowledge-bases/kb-1/images" -H "X-API-Key: $API_KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"page":1,"page_size":20,"knowledge_ids":["k-1","k-2"],"keyword":"架构"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/image-attrs/schema
|
||||
|
||||
全局只读的图片观察属性注册表,需 Viewer+,API Key 需 retrieve/full,无路径知识库。知识库编辑器用它配置图片观察与 OCR 条件;画廊的实际搜索/筛选用法仍以每个知识库的 gallery-config 为准。
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_knowledge.go` 的 `RegisterKnowledgeBaseRoutes`、`RegisterKnowledgeRoutes`。Handler:`internal/handler/knowledgebase.go`、`internal/handler/knowledge.go`。
|
||||
@@ -0,0 +1,98 @@
|
||||
# API 参考:长期记忆
|
||||
|
||||
管理当前调用者的长期记忆、主题和文档偏好,以及空间级记忆配置。路径使用 `/api/v1` 前缀。
|
||||
|
||||
个人接口均要求 Viewer+,API Key 必须 full-access。作用域从凭证中确定,不接受任意 `subject_id`。示例中的 `$BASE` 为服务地址,`$TOKEN` 为当前用户的 Bearer token。
|
||||
|
||||
## 空间配置与请求开关
|
||||
|
||||
空间配置使用 `GET/PUT /tenants/kv/memory-config`,不使用租户名称/描述的更新接口。读取需 Viewer+,写入需 Admin+,API Key 需 manage_tenant_settings 或 full-access。响应为 `{success,data:MemoryConfig}`,PUT 直接传配置对象;个人 `PUT /memory/settings` 不能替代空间开关。
|
||||
|
||||
```bash
|
||||
curl -X PUT "$BASE/api/v1/tenants/kv/memory-config" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"enabled":true,"write_mode":"explicit_only","max_items":200}'
|
||||
```
|
||||
|
||||
`memory_config` 字段:`enabled`、`write_mode`(explicit_only/auto)、`extract_model_id`、`max_items`、`extract_delay_seconds`、`extract_min_interval_seconds`、`extract_instructions`、`interest_threshold`、`embedding_model_id`、`vector_recall`、`retrieval_conditioning`。语义见[长期记忆](../03-features/23-memory.md)。更新时提交需要保留的完整配置对象。
|
||||
|
||||
`CustomAgentConfig.memory_enabled` 省略继承空间,false 禁止本智能体使用记忆;IM/Embed 使用绑定智能体的配置,当前渠道结构没有独立的 memory_enabled 字段。
|
||||
|
||||
## 个人设置
|
||||
|
||||
| 方法 | 路径 | 请求 / 响应 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/memory/settings` | `{success,data:{workspace_enabled,user_enabled,effective,write_mode,item_count,max_items}}` |
|
||||
| PUT | `/memory/settings` | `{"enabled":true}`,enabled 必填;返回更新后的 settings |
|
||||
|
||||
`effective` 表示空间与个人开关的合并结果;一次聊天还受智能体开关约束。
|
||||
|
||||
```bash
|
||||
curl -X PUT "$BASE/api/v1/memory/settings" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"enabled":true}'
|
||||
```
|
||||
|
||||
## 条目
|
||||
|
||||
| 方法 | 路径 | 请求 / 响应 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/memory/items` | 可选 status、limit、offset;`{success,data:[MemoryItem],total}` |
|
||||
| POST | `/memory/items` | `{kind,content,importance}`;200 `{success,data:MemoryItem}` |
|
||||
| PUT | `/memory/items/:id` | `{content,importance}`;200 `{success,data:MemoryItem}` |
|
||||
| DELETE | `/memory/items/:id` | 200 `{"success":true}` |
|
||||
| POST | `/memory/items/:id/confirm` | 200 `{success,data:MemoryItem}`;推断已失效或其依据已被修改时返回 409 |
|
||||
| POST | `/memory/items/:id/reject` | 200 `{"success":true}` |
|
||||
| DELETE | `/memory/items` | 清空当前身份;200 `{success,removed}` |
|
||||
|
||||
`status` 可为 active、pending、superseded、archived,省略不过滤。`limit` 默认 50,合法范围 1–200,越界回落 50;`offset` 默认 0,负值归零。kind 为 profile/preference/fact/task/interest;内容为简短记忆,最长 300 个字符,importance 用于重要度排序。
|
||||
|
||||
MemoryItem 包括 `id`、`kind`、`content`、`topic`、`importance`、`origin`、`status`、`source_session_id`、`source_message_id`、`expires_at`、`superseded_by` 和创建/修改时间。pending 不参与提示词;编辑后按手工维护处理。
|
||||
|
||||
用于修改已有记忆的 pending 推断,确认前旧条目继续生效;确认会在同一事务中激活推断并替换旧条目。已失效、已到期或所依据内容已被修改/删除的推断不能确认,返回 409,客户端应刷新列表。内容几乎全是凭据等敏感信息时,新增或编辑返回 400。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/memory/items" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"kind":"preference","content":"回答先给结论,再解释依据","importance":3}'
|
||||
|
||||
curl "$BASE/api/v1/memory/items?status=pending&limit=50&offset=0" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
|
||||
curl -X POST "$BASE/api/v1/memory/items/item-1/confirm" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 主题和文档偏好
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/memory/topics` | 正在跟踪、尚未提升的主题;limit/offset 同条目,返回 data 与 total |
|
||||
| POST | `/memory/topics/:id/promote` | 手动提升为长期关注;返回 `{success,data:MemoryItem}` |
|
||||
| DELETE | `/memory/topics/:id` | 停止跟踪该主题;返回 success |
|
||||
| GET | `/memory/documents` | 文档偏好;limit/offset 同条目,返回 data 与 total |
|
||||
| DELETE | `/memory/documents/:id` | 删除该偏好记录;返回 success,不删除知识库文档 |
|
||||
|
||||
Topic 字段包括 id/topic/aliases/hits/threshold/last_seen_at;Document 字段包括 id/knowledge_id/knowledge_base_id/title/hits/last_used_at。删除使用的是偏好记录 id。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/memory/topics" -H "Authorization: Bearer $TOKEN"
|
||||
curl -X POST "$BASE/api/v1/memory/topics/topic-1/promote" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
curl -X DELETE "$BASE/api/v1/memory/documents/affinity-1" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 导出与立即整理
|
||||
|
||||
`GET /memory/export` 返回 `{success,total,truncated,data}`,带下载文件名 `academy-memories.json`。最多导出 20,000 条,触及上限时检查 truncated。
|
||||
|
||||
`POST /memory/consolidate` 返回 `{success,data:{merged,demoted,expired,reviewed,candidates,skipped?}}`,立即合并近义条目、归档到期事项。没有变化时 skipped 说明原因。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/memory/export" -H "Authorization: Bearer $TOKEN" \
|
||||
-o academy-memories.json
|
||||
curl -X POST "$BASE/api/v1/memory/consolidate" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
参数无效、内容为敏感信息或记忆未开启时返回 400;找不到当前身份的条目返回 404;确认冲突返回 409;认证/权限不满足返回 401/403。接口没有管理员读取他人记忆的 subject 参数。实现:`internal/handler/memory.go`、`internal/router/routes_memory.go`。
|
||||
@@ -0,0 +1,440 @@
|
||||
# API 参考:模型与初始化
|
||||
|
||||
管理模型、测试连接、初始化知识库,并发起评估任务。AcademyCloud 接口用于相关云服务接入。
|
||||
|
||||
系统信息与系统管理(`/system`、`/system/admin`)接口见[系统与平台管理](./02-api-system.md)。
|
||||
|
||||
## 模型(/api/v1/models)
|
||||
|
||||
API key:`manage_models` 或 full-access。
|
||||
|
||||
### GET /api/v1/models/providers
|
||||
|
||||
用途:厂商目录(前端据此动态渲染厂商下拉、图标、额外字段与模型选择)。权限:Viewer+。查询参数:`model_type`(可选:`chat/embedding/rerank/vllm/asr`,也接受 `KnowledgeQA` 等后端取值;未知取值返回 400),指定后只返回支持该类型的厂商及该类型的模型。Handler: `internal/handler/model_catalog.go`
|
||||
|
||||
响应:200 `{"success":true,"data":[ModelProviderDTO]}`,每项:
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `value` / `label` / `labels` / `description` / `descriptions` / `website` | 厂商 id、品牌名、按语言的名称与描述 |
|
||||
| `icon` | `data:image/svg+xml;base64,...`,可直接用于 `<img src>` |
|
||||
| `api` / `auth` / `requiresAuth` | 默认协议(`openai-completions` 等)、鉴权方式、是否需要密钥 |
|
||||
| `defaultUrls` / `modelTypes` | 按模型类型的默认地址与支持的类型。`defaultUrls` 仅对 Admin+(或 full-access / `manage_tenant_settings` API key)返回,其他调用方为空 |
|
||||
| `extraFields` | 厂商额外配置字段定义(`key,label,labels,type,required,default,placeholder,options,model_types,secret`),值存入 `parameters.extra_config` |
|
||||
| `credentialLabels` | 部分模型类型下凭证输入框的名称与提示(如火山引擎、LKEAP 重排的 API Key 一栏实为 Access Key ID / SecretId) |
|
||||
| `models` | 内置模型目录(`id,name,type,api,reasoning,input,context_window,max_output_tokens,dimension,thinking_levels,cost,source`),`source` 为该模型参数所依据的厂商文档链接 |
|
||||
| `thinking` | 厂商级思考编码摘要(`format`、`levels`) |
|
||||
| `order` | 列表排序值 |
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/models/providers?model_type=chat" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET|POST /api/v1/models/catalog/resolve
|
||||
|
||||
用途:按厂商、模型名、`base_url` 与 `extra_config` 解析有效接入配置(协议、思考等级、上下文),供模型编辑器实时展示。权限:Viewer+。
|
||||
|
||||
参数(GET 用查询参数,POST 用 JSON 请求体,字段相同):`provider`(厂商 ID)、`model`、`base_url`、`model_type`(默认 `chat`)、`api`、`thinking_control`、`remote_model_name`,以及该厂商声明的非密钥额外字段(如 Azure 的 `api_version`)。POST 请求体还可带 `spec` 对象(与模型 `parameters.spec` 相同),用于预览单行目录覆盖。密钥类字段一律不接受。
|
||||
|
||||
响应:200 `{"success":true,"data":{provider,api,remote_model,cataloged,model,capabilities,base_url,url}}`,其中 `capabilities` 为 `{provider,api,cataloged,reasoning,thinking_levels,thinking_format,input,context_window,max_output_tokens,max_tokens_field}`。`base_url` 与 `url`(实际请求地址,仅自行计算地址的厂商返回,如 Azure)只对 Admin+(或 full-access / `manage_tenant_settings` API key)返回。无法解析时返回 400。同一 `capabilities` 结构也随远程对话/视觉模型的 `ModelResponse.capabilities` 返回。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/models/catalog/resolve?provider=deepseek&model=deepseek-v4-pro" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/models
|
||||
|
||||
用途:创建模型。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required"`) | 模型名 |
|
||||
| `display_name` | string | 否 | 显示名 |
|
||||
| `type` | string | 是(`binding:"required"`) | 模型类型:`KnowledgeQA` / `Embedding` / `Rerank` / `VLLM` / `ASR`(按原样保存,不接受 `chat` 等前端写法) |
|
||||
| `source` | string | 是(`binding:"required"`) | 来源(`local` / `remote`) |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `parameters` | object | 是(`binding:"required"`) | 连接参数(`base_url`、`provider`、`extra_config`、`spec`、`context_window` 等,字段见[模型管理](../03-features/06-models.md#模型配置字段))。创建时可直接带 `api_key` / `app_secret`,之后经 credentials 子资源修改 |
|
||||
|
||||
`parameters` 会按模型目录校验(未知协议、错误的 compat 键、非法思考等级返回 400),`base_url` 经过 SSRF 校验。
|
||||
|
||||
自部署的 `generic` / `Qwen3-VL-Embedding` 默认通过二维 `messages` 批量提交独立输入(已验证 vLLM 0.29.0);单条输入保留一维会话格式。文本沿用 `BATCH_EMBED_SIZE` 分批,图片批量调用的默认上限为 5 张,这是客户端的保守分批设置,可通过 `parameters.spec.compat.max_image_batch_size` 调整。上传解析仍按单张图片调度任务,不会跨任务合并图片。旧版服务若不支持二维 `messages`,可设置 `parameters.spec.compat.batch_messages=false` 恢复逐条请求;WeMM-Embedding 预设也默认启用会话批处理;其他没有该预设的模型需显式启用。
|
||||
|
||||
批处理保持各输入的会话内容和向量索引映射,但不保证服务端跨批大小计算逐位相同。要求严格可复现时,应验证部署端的数值行为;vLLM 的 [Batch Invariance](https://docs.vllm.ai/en/v0.29.0/features/batch_invariance/) 模式需要在服务端启用,并针对实际模型验证。可设置 `ACADEMY_EMBEDDING_TEST_URL` 和 `ACADEMY_EMBEDDING_TEST_FIXTURE` 后运行 `go test ./internal/models/embedding -run '^TestLiveQwenMessagesBatchParity$' -v -count=1`:fixture 为 `{"texts":[...],"image_paths":[...]}`,文本和图片均至少两个;私有/IP 端点需配置 `SSRF_WHITELIST`,鉴权可使用 `ACADEMY_EMBEDDING_TEST_API_KEY`。该测试比较逐条、分批、倒序和重复逐条请求,记录最大绝对误差与最低余弦相似度,超过严格阈值会失败,不写入知识库。
|
||||
|
||||
响应:201 `{"success":true,"data":{ModelResponse}}`(`id,name,type,source,parameters,is_default,is_builtin,status,credentials,capabilities,...`;响应不含密钥)
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/models -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"name":"gpt-5.5","type":"KnowledgeQA","source":"remote","parameters":{"provider":"openai","base_url":"https://api.openai.com/v1","api_key":"sk-..."}}'
|
||||
```
|
||||
|
||||
### GET /api/v1/models
|
||||
|
||||
用途:模型列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[ModelResponse]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/models -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/models/:id
|
||||
|
||||
用途:模型详情。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{ModelResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/models/m-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/models/:id/copy
|
||||
|
||||
用途:复制当前空间中的非内置模型配置。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `display_name` | string | 是(`binding:"required"`) | 副本显示名;去除首尾空白后不能为空,最长 255 个字符 |
|
||||
|
||||
服务端按路径中的源模型 ID 读取完整配置:副本的 `name` 保持与源模型一致,`type`、`source`、`description`、`parameters` 及已存凭证一并复制,不接受客户端重传。副本不会继承内置或默认状态;已激活的本地模型不会重复下载,其他未激活的本地模型会重新进入下载流程。
|
||||
|
||||
响应:201 `{"success":true,"data":{ModelResponse}}`(响应不含密钥)
|
||||
|
||||
源模型不存在时返回 404;源模型为内置模型、ID 或 `display_name` 无效时返回 400。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/models/m-1/copy -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"display_name":"GPT-4o 副本"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/models/:id/debug
|
||||
|
||||
用途:调试已保存模型(发起真实上游调用,产生费用)。权限:Admin+。form-data 字段:`input`(≤64KB)、`options`(JSON 编码调试选项:`system_prompt`、`temperature`(0~2)、`top_p`、`max_tokens`(1~8192)、`thinking`、`reasoning_effort`(`off/auto/minimal/low/medium/high/xhigh/max`,设置后覆盖 `thinking`))、`documents`(JSON 数组,≤100 条)、`file`(可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"ok",elapsed_ms,request,raw_response,observations,error}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/models/m-1/debug -H "Authorization: Bearer $TOKEN" -F 'input=你好'
|
||||
```
|
||||
|
||||
### PUT /api/v1/models/:id
|
||||
|
||||
用途:更新模型(内置模型由服务层限定 SystemAdmin)。权限:Admin+ 或 SystemAdmin(`AdminOrSystemAdmin`)。请求体:`name`、`display_name`(指针)、`description`、`parameters`(保留已存密钥)、`source`、`type`(均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{ModelResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/models/m-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"display_name":"GPT-4o mini"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/models/:id
|
||||
|
||||
用途:删除模型。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true,"message":"Model deleted"}`
|
||||
|
||||
仍被当前空间的知识库、智能体或长期记忆引用时,响应为 HTTP 400;兼容 message 保留,同时 `error.code=2300`,`error.details` 给出具体对象和引用位置:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"error": {
|
||||
"code": 2300,
|
||||
"message": "model is used by 2 knowledge base(s); reconfigure or remove those references before deleting",
|
||||
"details": {
|
||||
"knowledge_bases": [
|
||||
{"id": "kb-1", "name": "Product docs", "bindings": ["vlm_model"]},
|
||||
{"id": "kb-2", "name": "Engineering", "bindings": ["vlm_model"]}
|
||||
],
|
||||
"agents": [],
|
||||
"long_term_memory": {"bindings": []},
|
||||
"knowledge_base_total": 2,
|
||||
"agent_total": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
知识库绑定值:`embedding_model`、`summary_model`、`image_processing_model`、`vlm_model`、`asr_model`、`wiki_synthesis_model`、`auto_tag_model`;智能体绑定值:`chat_model`、`rerank_model`、`vlm_model`、`asr_model`、`query_understand_model`、`follow_up_model`;长期记忆绑定值:`embedding_model`、`extract_model`。详情包含对象 `id`、`name`、合并后的 `bindings`,以及 `knowledge_base_total` / `agent_total`。列表最多各 50 条,删除守卫以总数为准。
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/models/m-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/models/:id/credentials
|
||||
|
||||
用途:设置模型密钥(密钥不经主 PUT 传输)。权限:Admin+ 或 SystemAdmin。Handler: `internal/handler/model_credentials.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `api_key` | *string | 否 | 新 API Key |
|
||||
| `app_secret` | *string | 否 | 新 App Secret(两者均省略时仅返回状态) |
|
||||
|
||||
响应:200 `{"success":true,"data":{"fields":{"api_key":{"configured":bool},"app_secret":{"configured":bool}}}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/models/m-1/credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"api_key":"sk-..."}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/models/:id/credentials/:field
|
||||
|
||||
用途:删除某个密钥字段(`api_key` 或 `app_secret`)。权限:Admin+ 或 SystemAdmin。
|
||||
|
||||
响应:204 No Content
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/models/m-1/credentials/api_key -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## AcademyCloud
|
||||
|
||||
Handler: `internal/handler/academycloud.go`。API key:`manage_models`/full。
|
||||
|
||||
### POST /api/v1/academycloud/credentials
|
||||
|
||||
用途:保存 AcademyCloud SaaS 凭证。权限:Admin+。请求体:`{"app_id":"...","app_secret":"..."}`(均 `binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true,"message":"凭证保存成功"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/academycloud/credentials -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"app_id":"app","app_secret":"secret"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/models/academycloud/status
|
||||
|
||||
用途:AcademyCloud 就绪状态探测。权限:Viewer+。
|
||||
|
||||
响应:200 服务状态对象。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/models/academycloud/status -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 初始化(/api/v1/initialization)
|
||||
|
||||
Handler: `internal/handler/initialization.go`。KB 配置类:API key `manage_kbs`(写)/`retrieve`(读);模型检测类:`manage_models`(均可 full-access)。
|
||||
|
||||
### GET /api/v1/initialization/config/:kbId
|
||||
|
||||
用途:读取 KB 当前模型/解析配置。权限:Viewer+,KB read。
|
||||
|
||||
模型 `baseUrl` 仅对 KB 所属空间的 Admin+(或 full-access / `manage_tenant_settings` API key)返回。通过组织分享访问的空间只能看到凭证是否已配置(`credentials.*`),看不到来源空间的模型地址和存储桶信息。
|
||||
|
||||
响应:200 `{"success":true,"data":{"hasFiles",llm,embedding,rerank,multimodal,documentSplitting,nodeExtract,questionGeneration}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/initialization/config/kb-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/initialize/:kbId
|
||||
|
||||
用途:初始化 KB 的模型与解析配置(首次配置向导)。权限:KB 创建者 OR Admin+,KB write。
|
||||
|
||||
只有 KB 所属空间可以调用;通过组织分享获得编辑权限的空间会被拒绝(403)。KB 已绑定模型时,该接口会原地更新这些模型的配置,这一步需要与 `PUT /models/:id` 相同的权限(Admin+,或拥有 `manage_models` 能力的 API key),否则 403。
|
||||
|
||||
主要字段(`InitializationRequest`):
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `llm.source` / `llm.modelName` | string | 是 | LLM 来源与模型名 |
|
||||
| `llm.baseUrl` / `llm.apiKey` | string | 否 | 连接参数 |
|
||||
| `embedding.source` / `embedding.modelName` | string | 是 | Embedding 模型 |
|
||||
| `embedding.baseUrl` / `embedding.apiKey` / `embedding.dimension` | — | 否 | 连接与维度 |
|
||||
| `rerank.enabled` + `rerank.modelName/baseUrl/apiKey` | — | 否 | Rerank 配置 |
|
||||
| `multimodal.enabled` + `multimodal.vlm.*` + `multimodal.storageType` + `multimodal.cos.*|minio.*` | — | 否 | 多模态与图床 |
|
||||
| `documentSplitting.chunkSize` / `separators` | int / []string | 是 | 分块配置 |
|
||||
| `documentSplitting.chunkOverlap` | int | 否 | 重叠 |
|
||||
| `nodeExtract.*` | — | 否 | 图谱抽取(enabled/text/tags/nodes/relations) |
|
||||
| `questionGeneration.*` | — | 否 | 问题生成(enabled/questionCount) |
|
||||
|
||||
响应:200 `{"success":true,"message":"知识库配置更新成功","data":{"models":[Model],"knowledge_base":{KnowledgeBase}}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/initialize/kb-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"llm":{"source":"remote","modelName":"gpt-4o-mini"},"embedding":{"source":"remote","modelName":"text-embedding-3-small"},"documentSplitting":{"chunkSize":512,"separators":["\n\n"]}}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/initialization/config/:kbId
|
||||
|
||||
用途:更新 KB 模型/分块配置(`KBModelConfigRequest`:`llmModelId` 必填,`embeddingModelId`、`vlm_config`、`asr_config`、`documentSplitting.*`、`multimodal.enabled`、`storageProvider`、`storageBackendId`、`nodeExtract.*`、`questionGeneration.*` 可选)。权限:KB 创建者 OR Admin+,KB write。
|
||||
|
||||
通过组织分享访问时,需要有效分享权限为 admin;editor 只能编辑内容,不能改设置(403)。存储绑定(`storageBackendId` / `storageProvider`)只有 KB 所属空间可以修改,其他空间提交与当前不同的值会返回 403。
|
||||
|
||||
响应:200 `{"success":true,"message":"配置更新成功"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/initialization/config/kb-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"llmModelId":"m-1","embeddingModelId":"m-2"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/initialization/ollama/status
|
||||
|
||||
用途:Ollama 可用性探测。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"available","version","baseUrl","error"}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/initialization/ollama/status -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/initialization/ollama/models
|
||||
|
||||
用途:列出本地 Ollama 模型。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"models":[...]}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/initialization/ollama/models -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/ollama/models/check
|
||||
|
||||
用途:批量检查模型是否已存在。权限:Admin+。请求体:`{"models":["llama3"]}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"models":{"llama3":true}}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/ollama/models/check -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"models":["llama3"]}'
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/ollama/models/download
|
||||
|
||||
用途:拉取 Ollama 模型(异步任务)。权限:Admin+。请求体:`{"modelName":"llama3"}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"taskId","modelName","status","progress"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/ollama/models/download -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"modelName":"llama3"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/initialization/ollama/download/progress/:taskId
|
||||
|
||||
用途:下载任务进度。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{id,modelName,status,progress,message,startTime,endTime}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/initialization/ollama/download/progress/task-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/initialization/ollama/download/tasks
|
||||
|
||||
用途:全部下载任务列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[DownloadTask]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/initialization/ollama/download/tasks -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### 模型连通性检测(均 POST,权限 Admin+)
|
||||
|
||||
请求体统一为 `ModelTestRequest`:
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `source` | string | 否 | 默认 `remote` |
|
||||
| `modelName` | string | 是 | 模型名 |
|
||||
| `baseUrl` / `apiKey` / `appSecret` | string | 否 | 连接参数 |
|
||||
| `provider` / `interfaceType` | string | 否 | 厂商/接口类型 |
|
||||
| `dimension` / `supportsDimensionOverride` | int / bool | 否 | embedding 维度;是否在请求中指定维度 |
|
||||
| `customHeaders` / `extraConfig` | map | 否 | 扩展 |
|
||||
| `spec` | object | 否 | 单行目录覆盖,与模型 `parameters.spec` 相同 |
|
||||
| `modelId` | string | 否 | 已存模型 ID:请求中缺失的密钥、`extraConfig` 与 `spec` 从该模型补齐 |
|
||||
|
||||
| 端点 | 用途 | 响应 data |
|
||||
| --- | --- | --- |
|
||||
| `POST /api/v1/initialization/remote/check` | LLM 远程连通性 | `{available,message}` |
|
||||
| `POST /api/v1/initialization/embedding/test` | Embedding 测试 | `{available,message,dimension}` |
|
||||
| `POST /api/v1/initialization/rerank/check` | Rerank 测试 | `{available,message}` |
|
||||
| `POST /api/v1/initialization/asr/check` | ASR 测试 | `{available,message}` |
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/remote/check -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"modelName":"gpt-4o-mini","baseUrl":"https://api.openai.com/v1","apiKey":"sk-..."}'
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/multimodal/test
|
||||
|
||||
用途:多模态(VLM+图床)端到端测试。权限:Admin+。multipart 字段:`image`(必填)、`vlm_model`、`vlm_base_url`(必填)、`vlm_api_key`、`vlm_interface_type`、`storage_type`(`cos|minio`,必填)及对应 `cos_*`/`minio_*` 字段、`chunk_size`、`chunk_overlap`、`separators`。
|
||||
|
||||
响应:200 `{"success":true,"data":{"success","caption","ocr","processing_time"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/multimodal/test -H "Authorization: Bearer $TOKEN" \
|
||||
-F 'image=@demo.png' -F 'vlm_model=qwen-vl' -F 'vlm_base_url=http://x' -F 'storage_type=minio'
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/extract/text-relation
|
||||
|
||||
用途:文本图谱抽取测试。权限:Admin+。请求体:`text`(必填,≤5000 字符)、`tags`(必填,至少一个)、`model_id`(必填)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"nodes":[GraphNode],"relations":[GraphRelation]}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/extract/text-relation -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"text":"小明在腾讯工作","tags":["人物","公司"],"model_id":"m-1"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/extract/fabri-tag
|
||||
|
||||
用途:生成示例标签。权限:Admin+。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{"tags":[...]}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/extract/fabri-tag -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/initialization/extract/fabri-text
|
||||
|
||||
用途:按标签生成示例文本。权限:Admin+。请求体:`{"tags":[...],"model_id":"m-1"}`(model_id 必填)。
|
||||
|
||||
响应:200 `{"success":true,"data":{"text":"..."}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/initialization/extract/fabri-text -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"model_id":"m-1","tags":["人物"]}'
|
||||
```
|
||||
|
||||
## 评估(/api/v1/evaluation)
|
||||
|
||||
Handler: `internal/handler/evaluation.go`。API key:`run_evaluations`/full。
|
||||
|
||||
### POST /api/v1/evaluation
|
||||
|
||||
用途:发起评估任务(驱动 LLM 调用,产生费用)。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `dataset_id` | string | 否 | 数据集 ID |
|
||||
| `knowledge_base_id` | string | 否 | 目标 KB |
|
||||
| `chat_id` | string | 否 | 对话模型 ID |
|
||||
| `rerank_id` | string | 否 | Rerank 模型 ID |
|
||||
|
||||
响应:200 `{"success":true,"data":{评估任务}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/evaluation -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"knowledge_base_id":"kb-1","chat_id":"m-1"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/evaluation
|
||||
|
||||
用途:查询评估结果。权限:Viewer+。查询参数:`task_id`(必填)。
|
||||
|
||||
响应:200 `{"success":true,"data":{评估结果}}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/evaluation?task_id=task-1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/router.go` 调用 `RegisterModelRoutes`、`RegisterInitializationRoutes`、`RegisterEvaluationRoutes`、`RegisterAcademyCloudRoutes`(定义在 `internal/router/routes_infra.go`)。Handler:`internal/handler/model.go`、`internal/handler/model_catalog.go`、`internal/handler/model_credentials.go`、`internal/handler/initialization.go`、`internal/handler/evaluation.go`、`internal/handler/academycloud.go`。
|
||||
@@ -0,0 +1,451 @@
|
||||
# API 参考:组织与共享
|
||||
|
||||
管理组织成员以及知识库和智能体的共享关系。组织以工作空间为成员单位。
|
||||
|
||||
组织(Organization)以“空间(tenant)”为成员单位。组织组路由的 API key 策略为 `manage_spaces` 或 full-access;两个只读的「共享知识库清单」端点额外允许 `retrieve`,方便只读 key 发现自己本就有权检索的共享库;KB/Agent 分享管理仅 full-access key 可用。
|
||||
|
||||
## 组织管理(/api/v1/organizations)
|
||||
|
||||
### POST /api/v1/organizations
|
||||
|
||||
用途:创建组织。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是 | 组织名称 |
|
||||
| `description` | string | 否 | 描述 |
|
||||
| `avatar` | string | 否 | 头像 URL |
|
||||
| `searchable` | bool | 否 | 是否可被搜索发现 |
|
||||
| `require_approval` | bool | 否 | 加入是否需审批 |
|
||||
| `member_limit` | int | 否 | 成员空间数上限 |
|
||||
| `invite_code_validity_days` | int | 否 | 邀请码有效期(天) |
|
||||
|
||||
响应:201 `{"success":true,"data":{OrganizationResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"研发组织"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations
|
||||
|
||||
用途:列出我所在的组织。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"organizations":[...],"total":N,"resource_counts":{"knowledge_bases":{"by_organization":{}},"agents":{"by_organization":{}}}}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/preview/:code
|
||||
|
||||
用途:按邀请码预览组织(不加入)。权限:Viewer+。路径参数:`code` 邀请码。
|
||||
|
||||
响应:200 `{"success":true,"data":{id,name,description,avatar,member_count,share_count,agent_share_count,is_already_member,require_approval,created_at}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/preview/ABC123 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/join
|
||||
|
||||
用途:凭邀请码加入组织。权限:Admin+。请求体:`{"invite_code":"..."}`(必填)。
|
||||
|
||||
响应:200 `{"success":true,"data":{OrganizationResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/join -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"invite_code":"ABC123"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/join-request
|
||||
|
||||
用途:提交加入申请(需审批的组织)。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `invite_code` | string | 是 | 邀请码 |
|
||||
| `message` | string | 否 | 申请附言 |
|
||||
| `role` | string | 否 | 期望角色 |
|
||||
|
||||
响应:200 `{"success":true,"data":{JoinRequest}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/join-request -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"invite_code":"ABC123","message":"申请加入"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/search
|
||||
|
||||
用途:搜索可发现(searchable)的组织。权限:Viewer+。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `q` | string | 否 | 关键字 |
|
||||
| `limit` | int | 否 | 默认 20,上限 100 |
|
||||
|
||||
响应:200 `{"success":true,"data":[SearchableOrganization],"total":N}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/organizations/search?q=研发" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/join-by-id
|
||||
|
||||
用途:按组织 ID 加入可发现组织(无需邀请码)。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `organization_id` | string | 是 | 目标组织 ID |
|
||||
| `message` | string | 否 | 附言 |
|
||||
| `role` | string | 否 | 期望角色 |
|
||||
|
||||
响应:200 `{"success":true,"data":{OrganizationResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/join-by-id -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"organization_id":"org-1"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id
|
||||
|
||||
用途:组织详情。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{OrganizationResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/organizations/:id
|
||||
|
||||
用途:更新组织(服务层校验调用者空间是组织 admin,不限于 owner)。权限:Admin+。请求体字段同创建(均可选)。
|
||||
|
||||
响应:200 `{"success":true,"data":{OrganizationResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/organizations/org-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"description":"更新描述"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/organizations/:id
|
||||
|
||||
用途:删除组织。权限:Admin+(服务层要求组织 owner)。
|
||||
|
||||
响应:200 `{"success":true,"message":"Organization deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/organizations/org-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/:id/leave
|
||||
|
||||
用途:本空间退出组织。权限:Admin+。无请求体。本空间分享进该组织的知识库和 Agent 会被一并撤销。
|
||||
|
||||
响应:200 `{"success":true,"message":"Left organization successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/org-1/leave -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/:id/request-upgrade
|
||||
|
||||
用途:申请提升本空间在组织内的角色。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `requested_role` | string | 是 | 期望的组织角色(`viewer/editor/admin`) |
|
||||
| `message` | string | 否 | 附言 |
|
||||
|
||||
响应:200 `{"success":true,"data":{JoinRequest}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/org-1/request-upgrade -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"requested_role":"editor"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/:id/invite-code
|
||||
|
||||
用途:生成组织邀请码。权限:Admin+(服务层要求组织 admin)。无请求体。
|
||||
|
||||
响应:200 `{"success":true,"data":{"invite_code":"..."}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/org-1/invite-code -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/search-tenants
|
||||
|
||||
用途:按空间 ID 解析可邀请的空间。权限:Admin+,且调用者空间须为组织 admin。自 v0.8.2 起只接受完整的空间 ID,不再按空间名称跨空间搜索,`limit` 参数已移除。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `q` | string | 是 | 完整的空间 ID |
|
||||
|
||||
响应:200 `{"success":true,"data":[{"tenant_id","tenant_name"}]}`。`q` 不是有效 ID、空间不存在或已是组织成员时返回空数组,否则返回唯一候选。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/organizations/org-1/search-tenants?q=10002" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/search-users
|
||||
|
||||
用途:已废弃别名,行为同 `search-tenants`。权限:Admin+。参数同上。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/organizations/org-1/search-users?q=10002" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/organizations/:id/invite
|
||||
|
||||
用途:直接邀请空间加入组织。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `tenant_id` | uint64 | 二选一 | 目标空间 ID(推荐) |
|
||||
| `user_id` | string | 二选一 | 兼容路径:用户 ID(解析为其空间) |
|
||||
| `representative_user_id` | string | 否 | 已忽略,仅为兼容保留:直接添加的空间不挂代表用户,避免邀请方指定对方空间里哪位用户的信息出现在成员列表中 |
|
||||
| `role` | string | 是 | 组织内角色 |
|
||||
|
||||
响应:200 `{"success":true,"message":"Member added successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/organizations/org-1/invite -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"tenant_id":2,"role":"viewer"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/members
|
||||
|
||||
用途:组织成员(空间)列表。权限:Viewer+。`email` 只对调用方自己空间那一行返回,其他空间只返回用户名和头像。
|
||||
|
||||
响应:200 `{"success":true,"data":{"members":[{id,user_id,representative_user_id,role,tenant_id,tenant_name,username,email,avatar,joined_at}],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/members -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/organizations/:id/members/:tenant_id
|
||||
|
||||
用途:修改成员空间的组织角色。权限:Admin+。路径参数 `tenant_id` 为成员空间 ID。请求体:`{"role":"editor"}`(必填,`viewer/editor/admin`)。
|
||||
|
||||
响应:200 `{"success":true,"message":"Member role updated successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/organizations/org-1/members/2 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"role":"editor"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/organizations/:id/members/:tenant_id
|
||||
|
||||
用途:移除成员空间(含自移除)。权限:Admin+。被移除空间分享进该组织的知识库和 Agent 会被一并撤销。
|
||||
|
||||
响应:200 `{"success":true,"message":"Member removed successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/organizations/org-1/members/2 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/join-requests
|
||||
|
||||
用途:加入申请队列。权限:Admin+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"requests":[{id,user_id,username,email,message,request_type,prev_role,requested_role,status,created_at,reviewed_at}],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/join-requests -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/organizations/:id/join-requests/:request_id/review
|
||||
|
||||
用途:审批加入/升级申请。权限:Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `approved` | bool | 是 | 通过/拒绝 |
|
||||
| `message` | string | 否 | 审批意见 |
|
||||
| `role` | string | 否 | 通过时授予的角色 |
|
||||
|
||||
响应:200 `{"success":true,"message":"Review completed"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/organizations/org-1/join-requests/req-1/review \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d '{"approved":true}'
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/shares
|
||||
|
||||
用途:查看共享到该组织的 KB 列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"shares":[KnowledgeBaseShareResponse],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/shares -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/agent-shares
|
||||
|
||||
用途:查看共享到该组织的 Agent 列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"shares":[AgentShareResponse],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/agent-shares -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/shared-knowledge-bases
|
||||
|
||||
用途:组织空间视图:组织内全部共享 KB(含我自己的)。权限:Viewer+;API key 需 `manage_spaces`、`retrieve` 或 full-access,且只返回该 key 白名单内的 KB。
|
||||
|
||||
响应:200 `{"success":true,"data":[...含 is_mine、source_from_agent 标记...],"total":N}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/shared-knowledge-bases -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/organizations/:id/shared-agents
|
||||
|
||||
用途:组织空间视图:组织内全部共享 Agent。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":[SharedAgentInfo],"total":N}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/organizations/org-1/shared-agents -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## KB 分享(/api/v1/knowledge-bases/:id/shares)
|
||||
|
||||
API key:仅 full-access。Handler: `internal/handler/organization.go`
|
||||
|
||||
### POST /api/v1/knowledge-bases/:id/shares
|
||||
|
||||
用途:把 KB 分享到组织。权限:KB 创建者 OR Admin+。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `organization_id` | string | 是 | 目标组织 |
|
||||
| `permission` | string | 是 | 共享权限(组织角色语义,如 `viewer/editor`) |
|
||||
|
||||
响应:201 `{"success":true,"data":{KBShare}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/knowledge-bases/kb-1/shares -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"organization_id":"org-1","permission":"viewer"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/shares
|
||||
|
||||
用途:查看该 KB 的分享列表。权限:Viewer+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"shares":[KnowledgeBaseShareResponse],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/shares -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/knowledge-bases/:id/shares/:share_id
|
||||
|
||||
用途:修改分享权限。权限:KB 创建者 OR Admin+。请求体:`{"permission":"editor"}`(必填)。
|
||||
|
||||
服务层规则(与取消分享共用):
|
||||
|
||||
- 原分享人需在 KB 所属空间内操作,且空间角色为 Contributor+;
|
||||
- KB 所属空间的 Admin+ 可以管理本空间的全部分享;
|
||||
- 目标组织中角色为 admin 的空间,其 Admin+ 用户只能**降低**权限或取消分享,不能把权限提升到超过当前值。
|
||||
|
||||
`share_id` 必须属于路径中的 KB,否则 404。
|
||||
|
||||
响应:200 `{"success":true,"message":"Share permission updated successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/knowledge-bases/kb-1/shares/s-1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"permission":"editor"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/knowledge-bases/:id/shares/:share_id
|
||||
|
||||
用途:取消分享。权限:KB 创建者 OR Admin+,服务层规则同上。`share_id` 必须属于路径中的 KB,否则 404。
|
||||
|
||||
响应:200 `{"success":true,"message":"Share removed successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/knowledge-bases/kb-1/shares/s-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## Agent 分享(/api/v1/agents/:id/shares)
|
||||
|
||||
API key:仅 full-access。Handler: `internal/handler/organization.go`
|
||||
|
||||
### POST /api/v1/agents/:id/shares
|
||||
|
||||
用途:把 Agent 分享到组织。权限:Agent 创建者 OR Admin+。请求体同 KB 分享(`organization_id` + `permission`,必填)。内置智能体不能分享(400):每个空间都有同 ID 的内置智能体,分享后接收方无法区分。Agent 的知识库范围会开放给组织成员,因此调用者必须有权直接分享其中每个知识库(知识库创建者或 Admin+),`kb_selection_mode: all` 只有 Admin+ 可以分享,否则 403。
|
||||
|
||||
响应:201 `{"success":true,"data":{AgentShare}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/agents/agent-1/shares -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"organization_id":"org-1","permission":"viewer"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/agents/:id/shares
|
||||
|
||||
用途:查看该 Agent 的分享列表。权限:Agent 创建者 OR Admin+。
|
||||
|
||||
响应:200 `{"success":true,"data":{"shares":[AgentShareResponse],"total":N}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/agents/agent-1/shares -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/agents/:id/shares/:share_id
|
||||
|
||||
用途:取消 Agent 分享。权限:Agent 创建者 OR Admin+,服务层规则同 KB 取消分享。`share_id` 必须属于路径中的 Agent,否则 404。
|
||||
|
||||
响应:200 `{"success":true,"message":"Share removed successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/agents/agent-1/shares/s-1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 共享资源聚合视图
|
||||
|
||||
### GET /api/v1/shared-knowledge-bases
|
||||
|
||||
用途:列出通过组织共享给我的 KB(去除属主侧向量库元数据)。权限:Viewer+;API key 需 `manage_spaces`、`retrieve` 或 full-access,且只返回该 key 白名单内的 KB。
|
||||
|
||||
响应:200 `{"success":true,"data":[...],"total":N}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/shared-knowledge-bases -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/shared-agents
|
||||
|
||||
用途:列出通过组织共享给我的 Agent。权限:Viewer+;API key 同上。
|
||||
|
||||
响应:200 `{"success":true,"data":[SharedAgentInfo],"total":N}`。`SharedAgentInfo` 含 `source_tenant_id`(来源空间)、`org_name`、`shared_by_username`、`permission`,以及 `web_search_ready`——只返回「来源空间的联网搜索是否可用」这一个布尔位,不下发来源空间的 provider 配置(会泄露配置),也不拿接收方空间的 provider ID 去比对(会误报不可用)。
|
||||
|
||||
使用共享 Agent 调用其它接口时,若同名 Agent 被多个空间共享,可带 `agent_source_tenant_id` 指明来源空间;该值会与共享关系逐一校验,非法或无权限时直接报错,不会静默回退到别的来源。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/shared-agents -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/shared-agents/disabled
|
||||
|
||||
用途:设置“本空间禁用某共享 Agent”(影响整个空间的会话下拉)。权限:Admin+;API key 同上。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `agent_id` | string | 是(`binding:"required"`) | 共享 Agent ID |
|
||||
| `disabled` | bool | 否 | 是否禁用(默认 false) |
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/shared-agents/disabled -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"agent_id":"agent-1","disabled":true}'
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_agent.go` 的 `RegisterOrganizationRoutes`。Handler:`internal/handler/organization.go`。
|
||||
@@ -0,0 +1,230 @@
|
||||
# API 参考:沙箱、技能与个人变量
|
||||
|
||||
管理沙箱配置、技能目录、安装任务、会话交互终端和个人环境变量。路径均以 `/api/v1` 为前缀;示例中的 `$BASE` 为服务地址,`$TOKEN` 为当前用户的 Bearer token。界面操作步骤见[技能目录与沙箱](../03-features/22-skills-sandbox.md)。
|
||||
|
||||
## 权限
|
||||
|
||||
| 资源 | 读 | 写 / 检测 |
|
||||
| --- | --- | --- |
|
||||
| 沙箱配置列表/详情 | Viewer+;API Key 必须 full-access | Admin+;API Key 必须 full-access |
|
||||
| 沙箱实例清单、已安装技能及文件 | Admin+ | Admin+;API Key 必须 full-access |
|
||||
| 技能目录列表 | Viewer+,JWT | 目录增删、安装、包文件浏览需 Admin+;写组的 API Key 必须 full-access |
|
||||
| `/skills` 可用技能 | Viewer+,JWT | 只读,查询参数 sandbox_config_id;共享智能体另传 agent_id、agent_source_tenant_id |
|
||||
| 会话交互终端 | 会话属主,仅登录 JWT | 取票据后建立 WebSocket |
|
||||
| `/me/env-vars*` | 已登录的当前调用者 | 仅修改自己,服务端推导身份;无额外管理员门槛,使用 Bearer JWT |
|
||||
|
||||
跨空间 ID 不授予访问权。个人变量接口不能替代空间技能管理接口。
|
||||
|
||||
## 沙箱配置
|
||||
|
||||
| 方法 | 路径 | 请求 / 响应 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/sandbox-configs` | 200 `{success,data:[ConfigResponse],workspace_scripts_disabled}` |
|
||||
| POST | `/sandbox-configs` | `{name,description?,config}`;201 `{success,data:ConfigResponse}` |
|
||||
| GET | `/sandbox-configs/:id` | 200 `{success,data:ConfigResponse}` |
|
||||
| PUT | `/sandbox-configs/:id` | `{name,description?,config}`,name 必填;200 同详情 |
|
||||
| DELETE | `/sandbox-configs/:id` | 可选 `force=true`;200 `{success:true}` |
|
||||
| GET | `/sandbox-configs/:id/sandboxes` | 200 `{success,data:SandboxInventory}`,包含占用和关联智能体 |
|
||||
| PUT | `/sandbox-configs/workspace-policy` | `{"scripts_disabled":true}`;返回 success/workspace_scripts_disabled |
|
||||
| POST | `/sandbox-configs/templates/query` | `{config,config_id?,ensure_standard?,replace_standard?,ensure_desktop?,replace_desktop?}`;查询远端模板,可创建/替换标准模板或桌面模板,replace 需要 config_id |
|
||||
|
||||
ConfigResponse 为 `{id,name,description,sandbox_type,config,created_at,updated_at}`,凭据脱敏。编辑时以保存的配置为基础更新;脱敏占位值保留旧凭据,`skill_image` 由安装服务维护,客户端不能替换。
|
||||
|
||||
### config 字段
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `sandbox_type` | string | docker/cube/e2b;local 已移除;Lite 桌面的 host 不能保存为配置 |
|
||||
| `default_timeout_sec` | int | 执行超时,0 使用内置默认 |
|
||||
| `terminal_idle_disconnect_sec` | int | 终端/桌面无操作多久断开,0 为默认 900 秒,生效范围 60 秒–24 小时 |
|
||||
| `desktop_enabled` | bool | 声明所选模板是 Cube/E2B 桌面模板;已安装技能后不能切换 |
|
||||
| `allow_private_endpoints` | bool | 允许私网集群地址,不放行 link-local/云元数据 |
|
||||
| `env_vars` | map[string]string | 该沙箱配置的环境,值加密保存 |
|
||||
| `skill_rollout` | string | next_turn(默认)/new_session |
|
||||
| `network` | object | Cube/E2B 网络策略,见下 |
|
||||
| `cube` / `e2b` / `docker` | object | 与 sandbox_type 对应的连接配置 |
|
||||
| `volume_mount` | object | 可选卷配置;后端能力决定是否生效,不能仅凭字段存在认定支持 |
|
||||
| `skill_image` | object | 安装服务维护的快照信息,只读 |
|
||||
|
||||
volume_mount 字段包括 enabled、mount_path、provider、volume_id、volume_name;volume_owner_fingerprint 由服务端管理。卷配置与后端支持能力共同决定是否挂载,编辑时保留服务端返回的归属信息。
|
||||
|
||||
后端配置:
|
||||
|
||||
| 后端 | 字段 |
|
||||
| --- | --- |
|
||||
| cube | api_url、proxy_url、sandbox_domain、template_id;api_key 按集群需要;http_timeout_sec、cube_sandbox_ttl_seconds、dns_servers |
|
||||
| e2b | api_key、template_id 必填;api_url、sandbox_domain、proxy_url 用于自托管;http_timeout_sec、e2b_sandbox_ttl_seconds |
|
||||
| docker | image 必填;host(空为本机 socket)、tls_cert_path(TCP 必需)、cpu_limit、memory_limit_mb、pids_limit、network_mode(bridge/none)、runtime、idle_ttl_seconds、http_timeout_sec |
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/sandbox-configs" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"name":"E2B 工作环境","config":{"sandbox_type":"e2b","e2b":{"api_key":"<e2b-key>","template_id":"<template-id>"}}}'
|
||||
|
||||
curl "$BASE/api/v1/sandbox-configs/cfg-1/sandboxes" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
修改后端身份和删除配置会检查远端实例。409 的 error.code 可为 `sandboxes_still_live`、`sandbox_inventory_unverifiable`、`skill_snapshot_release_failed` 等;423 表示配置正在被另一个请求修改。`force=true` 仅允许在库存无法核实时删除,不会跳过已确认的活跃/暂停实例。响应中的占用数据用于定位相关会话和智能体。
|
||||
|
||||
### 网络策略
|
||||
|
||||
`network`:
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `deny_egress_by_default` | false 默认允许出站;true 默认拒绝 |
|
||||
| `allow_out` | IPv4/CIDR/域名或单标签通配域名,域名用于默认拒绝模式 |
|
||||
| `deny_out` | IPv4/CIDR 拒绝清单 |
|
||||
| `cube_rules` | Cube 的 name/scheme/sni/host/methods/path/deny/audit/inject 规则,顺序有意义 |
|
||||
| `e2b_host_rules` | E2B 的 host/headers 规则;host 也须列入 allow_out |
|
||||
| `allow_public_inbound` | 兼容旧输入,保存时清除,入站始终要求凭据 |
|
||||
|
||||
`cube_rules[].inject` 为 `[{header,secret,format}]`,`e2b_host_rules[].headers` 为 header→secret,秘密字段加密保存并脱敏。Docker 用 `docker.network_mode`,不支持这些 L7 规则。
|
||||
|
||||
```json
|
||||
{
|
||||
"deny_egress_by_default": true,
|
||||
"allow_out": ["pypi.org", "files.pythonhosted.org"],
|
||||
"deny_out": []
|
||||
}
|
||||
```
|
||||
|
||||
### POST /system/sandbox-check
|
||||
|
||||
请求 `{config,config_id?,deep?}`;config_id 可用于恢复已保存的脱敏凭据。`deep=false` 做连接检查;true 还执行临时脚本,远端后端会实际创建并销毁沙箱,可能产生后端用量。
|
||||
|
||||
成功执行探测返回 `{success:true,data:{ok,provider,checks,capabilities}}`,探测失败也可为 HTTP 200,应检查 data.ok;参数错误返回 400 的 code/msg。每个 check 含 name/ok/message/reason/latency_ms,`ok=null` 表示跳过。例如默认拒绝出网时外网探针被策略跳过,不等于通过外网访问验证。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/system/sandbox-check" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"config_id":"cfg-1","deep":true}'
|
||||
```
|
||||
|
||||
## 技能目录
|
||||
|
||||
| 方法 | 路径 | 请求 / 响应 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/skills/catalog` | 200 `{success,data:[CatalogItem]}`;CatalogItem 含 `installations:[{sandbox_config_id,status,enabled,version,bundle_sha256,served?,...}]` |
|
||||
| POST | `/skills/catalog` | multipart `file` 或 JSON `{"source":"@owner/slug"}`;201 `{success,data:{id,name,version,description}}` |
|
||||
| POST | `/skills/catalog/:id/install` | `{"sandbox_config_ids":["cfg-1","cfg-2"]}`;202 `{success,data:{installs,errors?}}` |
|
||||
| GET | `/skills/catalog/:id/files` | 200 `{success,data:[FileEntry]}` |
|
||||
| GET | `/skills/catalog/:id/files/content?path=SKILL.md` | 200 `{success,data:FileContent}` |
|
||||
| DELETE | `/skills/catalog/:id` | 无安装引用时删除;200 `{success:true}` |
|
||||
|
||||
安装到多沙箱可能部分受理:检查 `data.installs` 和 `data.errors`,HTTP 202 不表示全部安装完成。目录删除不会隐式卸载各沙箱。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/skills/catalog" \
|
||||
-H "Authorization: Bearer $TOKEN" -F 'file=@skill.zip'
|
||||
curl -X POST "$BASE/api/v1/skills/catalog/catalog-1/install" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"sandbox_config_ids":["cfg-1"]}'
|
||||
```
|
||||
|
||||
来源写法、匿名下载要求和 ZIP 限制见[技能来源](../03-features/22-skills-sandbox.md#支持的来源)。
|
||||
|
||||
## 沙箱内技能
|
||||
|
||||
以下路径前缀是 `/sandbox-configs/:id/skills`,所有操作含读都需 Admin+。
|
||||
|
||||
| 方法 | 后缀 | 行为 |
|
||||
| --- | --- | --- |
|
||||
| GET | 空 | `{success,data:[SkillResponse]}` |
|
||||
| POST | 空 | ZIP file 或 source JSON 安装;202 `{success,data:{skill_id}}` |
|
||||
| GET | `/:skillId` | `{success,data:SkillResponse}` |
|
||||
| POST | `/:skillId/reinstall` | 复用包重装,可选 `{"instructions":"..."}`(≤10000 字符)作为安装说明;202 `{success,data:{skill_id}}` |
|
||||
| POST | `/:skillId/stop` | 停止安装;200 返回技能状态,已 failed 可重复调用,其他非 installing 状态拒绝 |
|
||||
| GET | `/:skillId/guidance` | 当前安装运行的说明:`{success,data:{accepting,messages:[{id,content,status}]}}` |
|
||||
| POST | `/:skillId/guidance` | 安装进行中追加说明 `{expected_message_id,steer_id,content}`:`expected_message_id` 取 SkillResponse 的 `install_message_id`,`steer_id` 为客户端生成的 UUID(重试不会重复追加),`content` 1–10000 字符;202 `{success:true}`,安装已换轮次返回 409 |
|
||||
| PATCH | `/:skillId` | `enabled?`、`envs?`;省略不修改 |
|
||||
| DELETE | `/:skillId` | 从此沙箱卸载,保留目录包 |
|
||||
| GET | `/:skillId/files` | 文件清单 |
|
||||
| GET | `/:skillId/files/content?path=SKILL.md` | 读取相对路径文件,拒绝穿越 |
|
||||
| GET | `/:skillId/install-events` | SSE 安装进度 |
|
||||
| GET | `/:skillId/transcript` | SSE 安装过程事件 |
|
||||
|
||||
SkillResponse 包括 id/name/version/description/enabled/status/error/bundle_sha256/installed_snapshot_id/install_session_id/install_message_id/created_at/updated_at,以及 `envs:[{name,description,required,is_set}]`。升级或重装进行中、或失败后,`served:{version}` 标明沙箱仍在提供的上一个就绪版本;智能体可用技能以该版本为准。文件响应可为 UTF-8 文本、小图片的 base64 或二进制元数据,不能假定所有文件都有文本内容。
|
||||
|
||||
PATCH 的 envs 只更新已声明变量;未声明名称忽略,空字符串清除值、保留声明。
|
||||
|
||||
```bash
|
||||
curl -X PATCH "$BASE/api/v1/sandbox-configs/cfg-1/skills/skill-1" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"enabled":true,"envs":{"API_TOKEN":"<workspace-token>"}}'
|
||||
|
||||
curl -N "$BASE/api/v1/sandbox-configs/cfg-1/skills/skill-1/install-events" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
进度载荷为 `{percent,stage,log?,status?,done}`。断开连接不取消安装;done 可能只是无法继续跟随进度,最终结果以技能 status 为准。无 Redis 时流提示改为轮询状态。transcript 的 204 表示安装刚开始、事件定位尚未准备;404 可表示无日志或日志已过期,不能据此判定安装失败。
|
||||
|
||||
`GET /skills?sandbox_config_id=cfg-1` 返回可用于智能体的技能元数据 `{success,data:[{name,description}],skills_available}`,与管理员看到的全部安装记录不同。
|
||||
|
||||
助手通过 `skill.list` 调用此接口,必须传入 `agent.get` 返回的 `sandbox_config_id`(可为空);仅投影 `name`、`description` 和 `skills_available`,不暴露共享智能体查询参数或技能管理接口。空配置 ID 返回空列表且 `skills_available:false`,应提示用户在智能体编辑器选择沙箱;配置技能时使用返回的 `name`,不是安装记录 ID。
|
||||
|
||||
同时传 `agent_id` 和 `agent_source_tenant_id`(共享智能体的来源空间 ID)时,列出该共享智能体可 @ 的技能:沙箱配置取自智能体本身,忽略查询中的 `sandbox_config_id`;智能体技能范围为 `selected` 时只返回指定技能,禁用时返回空列表且 `skills_available:false`。调用者无权使用该智能体时返回 403。
|
||||
|
||||
## 会话交互终端
|
||||
|
||||
对话侧栏的终端通过 WebSocket 连接本会话的远程沙箱,仅 Cube/E2B 支持。路由由 `internal/router/routes_chat.go` 注册,尚未进入 Swagger;桌面接口见[会话 API](02-api-chat.md#sandbox-desktop),代理要求见[沙箱部署](../06-development/04-sandbox-deployment.md#交互终端与图形桌面)。
|
||||
|
||||
### POST /api/v1/sessions/:session_id/sandbox/terminal-ticket
|
||||
|
||||
签发两分钟有效的握手票据。需要会话属主的登录 Bearer access token,API Key 不能调用。
|
||||
|
||||
```bash
|
||||
curl -X POST "$BASE/api/v1/sessions/$SESSION_ID/sandbox/terminal-ticket" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
响应:200 `{"success":true,"data":{"ticket":"<ticket>","expires_in":120}}`。
|
||||
|
||||
### GET /api/v1/sessions/:id/sandbox/terminal
|
||||
|
||||
WebSocket 握手,不走普通认证中间件:
|
||||
|
||||
| 查询参数 | 必填 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `ticket` | 是 | 上一步取得的票据,绑定用户、空间、会话和签发时的 access token |
|
||||
| `provision` | 否 | `1` 表示允许创建或唤醒沙箱(可能计费);省略时只连接正在运行的沙箱 |
|
||||
| `agent_id` / `agent_source_tenant_id` | 否 | 与 `provision=1` 一起使用,指定按哪个智能体(含共享智能体)的沙箱配置创建 |
|
||||
| `cols` / `rows` | 否 | 初始终端尺寸 |
|
||||
| `pty_id` | 否 | 重新接上之前 `ready` 帧返回的 Shell 进程 |
|
||||
|
||||
连接后,二进制帧双向传输终端输入输出;文本帧为 JSON 控制消息:服务端发送 `ready`(含 `pty_id`、`backend`、`reattachable`)、`exited`(含 `exit_code`)和 `error`,客户端可发送 `{"type":"resize","cols":..,"rows":..}`。`reattachable` 为 `false`(Docker)时没有 `pty_id`,断开即结束 Shell,客户端不应自动重连。错误码包括 `SANDBOX_NOT_BOUND`、`SANDBOX_PAUSED`、`SANDBOX_STOPPED`(不会暂停的 Docker 后端:容器已停止或已被空闲回收)、`TERMINAL_UNSUPPORTED`、`IDLE_DISCONNECTED`、`AUTH_REVOKED`、`INTERNAL`。每个会话最多同时 5 个终端,超出返回 429。
|
||||
|
||||
## Lite 本机项目目录
|
||||
|
||||
仅 Lite 桌面版提供,其他部署返回 404。它在运行 Lite 的电脑上弹出系统目录选择框,用户选定的目录会加入已批准列表;随后创建会话时把返回的 `dir` 作为 `project_dir` 传入,即可让该会话在[本机沙箱](../03-features/22-skills-sandbox.md#lite-host)中使用这个目录。`project_dir` 字段见[会话 API](02-api-chat.md)。
|
||||
|
||||
| 方法 | 路径 | 请求 / 响应 |
|
||||
| --- | --- | --- |
|
||||
| POST | `/system/host-project-dir` | 无请求体;200 `{"code":0,"msg":"success","data":{"dir":"/Users/me/project"}}`,取消选择时 `dir` 为空。仅接受登录 JWT,Viewer+,API Key 被拒绝 |
|
||||
|
||||
## 个人环境变量
|
||||
|
||||
调用者身份由当前认证上下文确定,不接收 user_id。个人接口返回变量名、来源及更新时间,不返回明文。
|
||||
|
||||
| 方法 | 路径 | 请求 |
|
||||
| --- | --- | --- |
|
||||
| GET | `/me/env-vars` | 返回按 sandbox_config_id 分组的配置变量与技能变量 |
|
||||
| PUT | `/me/env-vars/skill` | `{skill_id,name,value}`,名称须为技能已声明项 |
|
||||
| DELETE | `/me/env-vars/skill` | `{skill_id,name}` |
|
||||
| PUT | `/me/env-vars/sandbox` | `{sandbox_config_id,name,value}` |
|
||||
| DELETE | `/me/env-vars/sandbox` | `{sandbox_config_id,name}` |
|
||||
|
||||
读取为 `{success,data:[{sandbox_config_id,sandbox_config_name,description,vars,skills}]}`,source 标明 user/workspace/unset 等状态。设置/删除成功返回 success;删未设置项返回 404。个人沙箱变量拒绝 ACADEMY_ 前缀和 PATH 等保留名;技能声明变量可使用所需的 ACADEMY_* 凭据名。
|
||||
|
||||
```bash
|
||||
curl -X PUT "$BASE/api/v1/me/env-vars/skill" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"skill_id":"skill-1","name":"API_TOKEN","value":"<my-token>"}'
|
||||
|
||||
curl -X DELETE "$BASE/api/v1/me/env-vars/skill" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"skill_id":"skill-1","name":"API_TOKEN"}'
|
||||
```
|
||||
|
||||
覆盖顺序为个人技能值 > 个人沙箱值 > 空间技能值。写入个人值不会更改管理员配置。实现参考:`internal/router/routes_infra.go`、`routes_agent.go`、`routes_auth_tenant.go`,以及 `internal/handler/sandbox_config.go`、`sandbox_skill.go`、`skill_catalog.go`、`me_env_var.go`、`host_project_picker.go`。
|
||||
@@ -0,0 +1,322 @@
|
||||
# API 参考:系统与平台管理
|
||||
|
||||
提供部署级系统信息和平台管理接口,包括全局设置、任务队列、平台 API Key、跨空间审计和用户密码重置。功能说明见[平台管理与系统管理员](../03-features/20-platform-admin.md)。
|
||||
|
||||
`/system/admin/*` 全组挂 `SystemAdmin()` 守卫;平台 API Key 按能力细分(`system_settings_read/manage`、`system_runtime_read/manage`、`system_tenants_read/manage`、`system_audit_read`)。
|
||||
|
||||
## 系统信息(/api/v1/system)
|
||||
|
||||
Handler: `internal/handler/system.go`。API key:`manage_vector_stores`/full。本组响应使用 `{"code":0,"msg":"success","data":...}` 包装。
|
||||
|
||||
### GET /api/v1/system/capabilities
|
||||
|
||||
Viewer+;API Key 可读。返回 `{code:0,data:{edition,capabilities}}`,每个 capability 给出 supported/reason。前端据部署版本、实际注册路由和 Docker 开关控制菜单入口;隐藏菜单不代替后端权限校验。
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/system/capabilities" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
`capabilities` 中的 `settings.sandbox.host` 表示当前部署能否使用本机操作系统沙箱,目前仅 macOS 原生桌面应用可能为 supported。
|
||||
|
||||
### POST /api/v1/system/host-project-dir
|
||||
|
||||
用途:在运行 Academy 的本机弹出系统文件夹选择框,供新会话绑定本机项目目录(v0.8.2 起,仅原生桌面应用)。权限:Viewer+,仅 JWT,API Key 一律拒绝。无请求体。
|
||||
|
||||
响应:200 `{"code":0,"msg":"success","data":{"dir":"/Users/me/project"}}`,用户取消选择时 `dir` 为空字符串;非桌面部署返回 404。
|
||||
|
||||
### GET /api/v1/system/info
|
||||
|
||||
用途:系统版本与引擎信息。权限:Viewer+。
|
||||
|
||||
响应:200 `{"code":0,"msg":"success","data":{version,edition,commit_id,build_time,go_version,keyword_index_engine,vector_store_engine,graph_database_engine,minio_enabled,db_version,started_at,uptime_seconds}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/info -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/system/parser-engines
|
||||
|
||||
用途:解析引擎列表与 DocReader 连接状态。权限:Viewer+。
|
||||
|
||||
响应:200 `{"code":0,"msg":"success","data":[...],"docreader_addr","docreader_transport","connected"}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/parser-engines -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/parser-engines/check
|
||||
|
||||
用途:用给定配置探测解析引擎(`types.ParserEngineConfig` 请求体)。权限:Admin+。
|
||||
|
||||
响应:200,同上。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/parser-engines/check -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{}'
|
||||
```
|
||||
|
||||
### POST /api/v1/system/docreader/reconnect
|
||||
|
||||
用途:重连 DocReader。权限:Admin+。请求体:`{"addr":"host:port"}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"code":0,"msg":"连接成功",...,"connected":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/docreader/reconnect -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"addr":"docreader:50051"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/system/storage-engine-status
|
||||
|
||||
用途:对象存储引擎可用性。权限:Viewer+。
|
||||
|
||||
响应:200 `{"code":0,"msg":"success","data":{"engines":[{name,allowed,available,description}],"allowed_providers":[...],"minio_env_available":bool}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/storage-engine-status -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/storage-engine-check
|
||||
|
||||
用途:校验存储配置(SSRF 防护后探测)。权限:Admin+。请求体:`provider`(必填,`minio/cos/tos/s3/oss/ks3/obs`)+ 对应 `minio|cos|tos|s3|oss|ks3|obs` 配置对象。
|
||||
|
||||
响应:200 `{"code":0,"data":{"ok","message","bucket_created"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/storage-engine-check -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"provider":"minio","minio":{"endpoint":"minio:9000"}}'
|
||||
```
|
||||
|
||||
## 系统管理(/api/v1/system/admin,SystemAdmin 专属)
|
||||
|
||||
组级挂载 `SystemAdmin()` 守卫(始终强制,不受 EnableRBAC 影响);平台 API key 需对应 `system_*` capability。本组读取接口多返回原始行/数组(无包装)。Handler: `internal/handler/system.go`、`internal/handler/audit_log.go`。
|
||||
|
||||
### POST /api/v1/system/admin/promote
|
||||
|
||||
用途:授予 SystemAdmin。请求体:`user_id`(UUID,优先)或 `email`(二选一)。
|
||||
|
||||
响应:200 `UserInfo`(原始对象)。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/promote -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"email":"admin@ex.com"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/revoke
|
||||
|
||||
用途:撤销 SystemAdmin。请求体:`{"user_id":"..."}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `UserInfo`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/revoke -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"user_id":"u-1"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/list
|
||||
|
||||
用途:SystemAdmin 列表。查询参数:`offset`(默认 0)、`limit`(默认 50,上限 200)。
|
||||
|
||||
响应:200 `{"total":N,"admins":[UserInfo]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/admin/list -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/users/create
|
||||
|
||||
仅系统管理员;此接口不开放给 platform API Key。请求字段:username(2–50 字符)、email(合法邮箱)、password(可选或 null 自动生成)。显式空字符串仍要经过密码策略校验,不视为自动生成。
|
||||
|
||||
| HTTP 状态 | 响应与含义 |
|
||||
| --- | --- |
|
||||
| 201 | `{user:UserInfo,generated_password?}`,新建;仅自动生成时返回密码 |
|
||||
| 200 | `{user:UserInfo}`,已有身份,不修改账号或密码 |
|
||||
| 400 | 参数或密码策略不满足 |
|
||||
| 409 | 邮箱与用户名对应不同身份 |
|
||||
|
||||
这是原始响应对象,没有 success/data 包装,也没有 idempotent 字段。用 HTTP 状态区分新增与已有账号。空间分配遵循 auth.default_tenant_mode。
|
||||
|
||||
```bash
|
||||
curl -i -X POST "$BASE/api/v1/system/admin/users/create" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"username":"alice","email":"alice@example.com"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/users/reset-password
|
||||
|
||||
用途:重置用户密码。请求体:`email`(`binding:"required,email"`)、`new_password`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"message":"Password reset successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/users/reset-password -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"email":"a@ex.com","new_password":"newpass1"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/api-keys
|
||||
|
||||
用途:平台 API key 列表(掩码)。
|
||||
|
||||
响应:200 `{"success":true,"data":[{id,name,api_key,capabilities,expires_at_unix,...}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/admin/api-keys -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/api-keys
|
||||
|
||||
用途:创建平台 API key(明文仅返回一次)。请求体:`name`(非空)、`capabilities`(`system_*` 列表,必填)、`expires_at_unix`(可选,须为未来时间)。
|
||||
|
||||
响应:201 `{"success":true,"data":{...,"api_key":"<明文>","token":"<明文>"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/api-keys -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"ops","capabilities":["system_tenants_read"]}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/system/admin/api-keys/:key_id
|
||||
|
||||
用途:删除平台 API key。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/system/admin/api-keys/3 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/settings 与 GET /api/v1/system/admin/settings/:key
|
||||
|
||||
用途:平台运行时设置列表 / 单项(平台 key 需 `system_settings_read|manage`)。
|
||||
|
||||
响应:200 `[SystemSetting]` / `SystemSetting`(原始,无包装;字段:`key,value,value_type,description,last_modified_by,last_modified_at`)。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/admin/settings -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/system/admin/settings/:key
|
||||
|
||||
用途:更新设置(平台 key 需 `system_settings_manage`)。请求体:`{"value":<任意 JSON,按注册表类型校验>}`(必填)。
|
||||
|
||||
响应:200 `SystemSetting`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/system/admin/settings/default_storage_quota -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"value":10737418240}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/system/admin/settings/:key
|
||||
|
||||
用途:恢复设置默认值。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/system/admin/settings/default_storage_quota -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/runtime/queues
|
||||
|
||||
用途:asynq 队列深度与并发状态(Lite 模式返回 `available:false`;平台 key 需 `system_runtime_read|manage`)。
|
||||
|
||||
响应:200 `{"available",upstream_concurrency,parse_concurrency,wiki_concurrency,pools,queues,model_limiter_available,models,timestamp}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/admin/runtime/queues -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/runtime/queues/:queue/tasks
|
||||
|
||||
用途:队列任务列表。查询参数:`state`(`pending/active/scheduled/retry/archived/completed`)、`cursor`、`page_size`(默认 20,上限 100)。
|
||||
|
||||
响应:200 `{"available","tasks":[RuntimeTaskInfo],"page_size","has_more","next_cursor"}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/system/admin/runtime/queues/default/tasks?state=pending" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/runtime/queues/:queue/tasks/:task_id/actions/:action
|
||||
|
||||
用途:任务操作(`action` ∈ `cancel/run_now/delete`;平台 key 需 `system_runtime_manage`)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/runtime/queues/default/tasks/t-1/actions/cancel \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### DELETE /api/v1/system/admin/runtime/queues/:queue/archived
|
||||
|
||||
用途:清空归档任务。
|
||||
|
||||
响应:200 `{"success":true,"deleted":N}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/system/admin/runtime/queues/default/archived -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/system/admin/tenants/apply-default-storage-quota
|
||||
|
||||
用途:把当前默认存储配额批量写到全部空间(平台 key 需 `system_tenants_manage`)。无请求体。
|
||||
|
||||
响应:200 `{"affected":N,"quota_bytes":N,"quota_gb":N}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/system/admin/tenants/apply-default-storage-quota -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/system/admin/audit-log
|
||||
|
||||
用途:平台级审计日志(tenant_id=0 行;平台 key 需 `system_audit_read`)。查询参数同空间审计(`after_id/limit/action/outcome/actor`)。Handler: `internal/handler/audit_log.go`
|
||||
|
||||
响应:200 `{"success":true,"data":[AuditLog],"next_cursor":N}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/system/admin/audit-log -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_auth_tenant.go` 的 `RegisterSystemAdminRoutes` 与 `RegisterSystemRoutes`。Handler:`internal/handler/system.go`、`internal/handler/audit_log.go`。
|
||||
|
||||
|
||||
## 平台用户管理
|
||||
|
||||
以下接口仅系统管理员的用户会话可访问,API Key 不开放。
|
||||
|
||||
### GET /api/v1/system/admin/users
|
||||
|
||||
分页列出整个部署的用户。查询参数 `offset` 默认 0;`limit` 默认 50,最大 200。响应为未包装对象 `{"users":[UserInfo],"offset":0,"limit":50,"has_more":false}`,不返回密码或哈希。
|
||||
|
||||
### POST /api/v1/system/admin/users/status
|
||||
|
||||
请求体 `{"user_id":"目标用户 ID","is_active":false}`,两个字段都必填。停用会吊销目标用户的已有会话;不能停用自己或最后一位启用的系统管理员(400),用户不存在返回 404。成功返回 `{"message":"User status updated","user":UserInfo,"is_active":false}`。创建账号与重置密码仍使用上文的 `/users/create`、`/users/reset-password`。
|
||||
|
||||
## 模型目录(/api/v1/system/admin/model-catalog)
|
||||
|
||||
仅系统管理员用户会话可访问,API Key 不开放此组接口。
|
||||
|
||||
| 方法与路径 | 作用 |
|
||||
| --- | --- |
|
||||
| `GET /system/admin/model-catalog` | 返回当前 `version`、`baseline`、管理员 `overlay`、最近 20 个历史版本,以及 `builtin` / `deployment` / `effective` 目录 |
|
||||
| `POST /system/admin/model-catalog/preview` | 校验覆盖文档并返回候选目录(仅 `effective` 与规范化后的 `overlay`,`history` / `builtin` / `deployment` 为 `null`),不持久化、不发布 |
|
||||
| `PUT /system/admin/model-catalog` | 校验、保存新版本并发布;审计仅记录版本元信息 |
|
||||
|
||||
预览和发布使用相同请求体:
|
||||
|
||||
```json
|
||||
{
|
||||
"version": 0,
|
||||
"baseline": "GET 返回的部署基线标识",
|
||||
"overlay": {
|
||||
"providers": {
|
||||
"openai": {
|
||||
"models": [{"id": "gpt-5", "context_window": 128000}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
响应为未包装的目录状态对象。每个厂商条目额外带 `model_thinking_levels`(按对话模型 id 列出开启思考后可选的等级,已合并厂商映射与协议能力)和 `vendor_thinking_levels`(未单独配置等级的模型所用的厂商默认等级)。非法文档返回 400;版本过期或请求实例的部署基线不一致返回 409。发布先持久化再切换本实例,其他实例约 5 秒内同步。回滚使用历史 `overlay` 配合当前 `version` / `baseline` 再次发布。完整规则和限制见[模型管理](../03-features/06-models.md#系统管理员维护模型目录)。
|
||||
@@ -0,0 +1,387 @@
|
||||
# API 参考:租户(空间)与成员
|
||||
|
||||
管理工作空间、成员、邀请、API Key 和审计日志。操作作用于当前活跃空间,跨空间访问按接口权限校验。
|
||||
|
||||
所有 `/tenants/:id/*` 路由在组级挂载 `PathTenantMatch()`(`internal/middleware/access.go`):URL 中的 `:id` 必须等于当前活跃空间(跨空间超管例外),防止越权操作他人空间。
|
||||
|
||||
租户 `memory_config` 配置字段及个人记忆接口见[长期记忆 API](02-api-memory.md)。空间管理员更新配置时提交要保留的完整对象。
|
||||
|
||||
## 空间生命周期
|
||||
|
||||
### POST /api/v1/tenants
|
||||
|
||||
用途:创建空间(自助开新工作区;调用者自动成为 Owner)。权限:任何已登录用户(可无空间);API key 仅平台 key 且具 `system_tenants_manage`。Handler: `internal/handler/tenant.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是(`binding:"required,min=1,max=128"`) | 空间名称 |
|
||||
| `description` | string | 否(`binding:"max=512"`) | 描述 |
|
||||
|
||||
跨空间超管可提交完整 `types.Tenant`(含 `storage_quota`、`status` 等)。
|
||||
|
||||
响应:201 `{"success":true,"data":{Tenant}}`(配置允许时可能携带 `api_key`)。自助创建被禁用返回 403(code 2005),超配额返回 429。
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"我的空间"}'
|
||||
```
|
||||
|
||||
### GET /api/v1/tenants
|
||||
|
||||
用途:列出我可访问的空间。权限:已登录;API key 需 `manage_tenant_settings` 或 full-access。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"items":[TenantResponse]}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/tenants/all
|
||||
|
||||
用途:列出全部空间(跨空间超管)。权限:`CrossTenant()`(`CanAccessAllTenants` 且集群开启 `EnableCrossTenantAccess`);平台 key 需 `system_tenants_read|manage`。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"items":[TenantResponse]}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/all -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/tenants/search
|
||||
|
||||
用途:按关键字搜索空间(跨空间超管)。权限:同上。Handler: `internal/handler/tenant.go`
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `keyword` | string | 否 | 关键字 |
|
||||
| `tenant_id` | string | 否 | 精确空间 ID |
|
||||
| `page` / `page_size` | int | 否 | 分页(默认 1/20,上限 100) |
|
||||
|
||||
响应:200 `{"success":true,"data":{"items":[...],"total","page","page_size"}}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/tenants/search?keyword=demo&page=1" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/tenants/:id
|
||||
|
||||
用途:空间详情。权限:Viewer+;平台 key 需 `system_tenants_read|manage`。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{TenantResponse}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/tenants/:id
|
||||
|
||||
用途:更新空间配置。权限:Owner;平台 key 需 `system_tenants_manage`。Handler: `internal/handler/tenant.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | *string | 否(`binding:"omitempty,min=1,max=128"`) | 新名称 |
|
||||
| `description` | *string | 否(`binding:"omitempty,max=512"`) | 新描述 |
|
||||
|
||||
响应:200 `{"success":true,"data":{TenantResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/tenants/1 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"name":"新名字"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/tenants/:id
|
||||
|
||||
用途:删除空间。权限:Owner;平台 key 需 `system_tenants_manage`。空间记录与全部成员关系被软删除,成员随即失去访问;空间内的知识库、模型等数据不会被立即物理清除,已删除空间排队中的 Wiki 任务不再调用模型。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"message":"Workspace deleted successfully"}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/tenants/1 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 空间 KV 配置
|
||||
|
||||
`:key` 为配置键而非空间 ID(空间取自认证上下文),可选值:`web-search-config`、`prompt-templates`、`parser-engine-config`、`storage-engine-config`、`chat-history-config`、`retrieval-config`、`memory-config`、`assistant-config`。
|
||||
|
||||
`assistant-config` 是 Academy 助手的空间设置:`display_name`(界面上的助手名称,最多 24 字,空表示默认名称)和 `max_access_level`(成员可选的最高权限等级:`read_only` 只读、`confirm` 修改前确认、`auto_write` 可恢复的修改直接执行,删除类仍需确认;默认 `confirm`)。成员在对话请求里用 `assistant_access` 选择自己的等级,服务端按空间上限截断。
|
||||
|
||||
### GET /api/v1/tenants/kv/:key
|
||||
|
||||
用途:读取空间级 KV 配置。权限:Viewer+;API key 需 `manage_tenant_settings` 或 full-access。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{...对应配置对象...}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/kv/retrieval-config -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/tenants/kv/:key
|
||||
|
||||
用途:更新空间级 KV 配置。权限:Admin+;API key 需 `manage_tenant_settings` 或 full-access。请求体:与 `:key` 对应的配置 JSON 对象。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"message":"Configuration updated"}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/tenants/kv/web-search-config -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"enabled":true}'
|
||||
```
|
||||
|
||||
## API Key 与 API 主体
|
||||
|
||||
### GET /api/v1/tenants/:id/api-keys
|
||||
|
||||
用途:列出空间 API key(掩码显示)。权限:Owner,仅 JWT(API key 默认拒绝)。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":[{id,scope_type,name,api_key(掩码),full_access,knowledge_base_ids,capabilities,last_used_at,expires_at,created_at}]}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/1/api-keys -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/api-keys
|
||||
|
||||
用途:创建空间 API key(明文仅返回一次)。权限:Owner,仅 JWT。Handler: `internal/handler/tenant.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `name` | string | 是 | key 名称 |
|
||||
| `full_access` | bool | 否 | 空间全权 key(默认 false) |
|
||||
| `knowledge_base_ids` | []string | 否 | KB 白名单(scoped key) |
|
||||
| `capabilities` | []string | 否 | capability 列表(见总览) |
|
||||
| `expires_at_unix` | *int64 | 否 | 过期时间戳 |
|
||||
|
||||
响应:201 `{"success":true,"data":{...,"api_key":"<明文>","token":"<明文>"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/api-keys -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"name":"ingest-bot","capabilities":["ingest","retrieve"],"knowledge_base_ids":["kb-1"]}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/tenants/:id/api-keys/:key_id
|
||||
|
||||
Owner,仅 JWT。更新已有 Key 的 name、full_access、knowledge_base_ids、capabilities、expires_at_unix,授权字段按整份配置提交;不是只改一个字段的 PATCH。expires_at_unix 省略或 null 会清除已有到期时间。更改权限后使用同一 token,新授权在后续认证时生效,不重新返回明文。
|
||||
|
||||
返回 200 `{success,data:APIKeyResponse}`,Key 脱敏;非法能力/知识库范围返回 400,不存在返回 404。
|
||||
|
||||
```bash
|
||||
curl -X PUT "$BASE/api/v1/tenants/1/api-keys/5" \
|
||||
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"name":"search-bot","full_access":false,"knowledge_base_ids":["kb-1"],"capabilities":["retrieve"]}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/tenants/:id/api-keys/:key_id
|
||||
|
||||
用途:删除 API key。权限:Owner,仅 JWT。路径参数:`key_id`。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/tenants/1/api-keys/5 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/tenants/:id/api-principal-config
|
||||
|
||||
用途:读取 API 外部用户主体配置。权限:Owner,仅 JWT。Handler: `internal/handler/tenant.go`
|
||||
|
||||
响应:200 `{"success":true,"data":{"mode":"tenant|direct|signed_token","direct_header_name","signed_token_header_name","require_direct_header","has_hmac_secret"}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/1/api-principal-config -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### PUT /api/v1/tenants/:id/api-principal-config
|
||||
|
||||
用途:更新 API 外部用户主体配置。权限:Owner,仅 JWT。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `mode` | string | 是 | `tenant` / `direct` / `signed_token` |
|
||||
| `require_direct_header` | bool | 否 | direct 模式是否强制 Header |
|
||||
| `hmac_secret` | *string | 否 | signed_token 模式密钥(传 `***` 保留原值) |
|
||||
|
||||
响应:200,同 GET。
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/tenants/1/api-principal-config -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"mode":"signed_token","hmac_secret":"topsecret"}'
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/api-principal-test-token
|
||||
|
||||
用途:签发用于测试的外部用户 JWT。权限:Owner,仅 JWT。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `external_user_id` | string | 是 | 外部用户 ID(≤128 字符) |
|
||||
| `expires_in_seconds` | int | 否 | 1-3600,默认 900 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"token","header_name","expires_in_seconds","expires_at_unix","external_user_id"}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/api-principal-test-token -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"external_user_id":"u-123"}'
|
||||
```
|
||||
|
||||
## 成员管理(/tenants/:id/members)
|
||||
|
||||
Handler: `internal/handler/tenant_member.go`。API key 需 `manage_members` 或 full-access。
|
||||
|
||||
### GET /api/v1/tenants/:id/members
|
||||
|
||||
用途:成员列表。权限:Viewer+。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `q` | string | 否 | 邮箱/用户名过滤 |
|
||||
| `page` / `page_size` | int | 否 | 分页 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"members":[{user_id,email,username,avatar,role,status,invited_by,joined_at}],"total","page","page_size"}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/1/members -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/members
|
||||
|
||||
用途:直接添加成员。权限:Owner。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `email` | string | 是(`binding:"required,email"`) | 成员邮箱(须已注册) |
|
||||
| `role` | string | 是(`binding:"required"`) | `owner/admin/contributor/viewer` |
|
||||
|
||||
响应:201 `{"success":true,"data":{成员对象}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/members -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"email":"b@ex.com","role":"contributor"}'
|
||||
```
|
||||
|
||||
### PUT /api/v1/tenants/:id/members/:user_id
|
||||
|
||||
用途:修改成员角色。权限:Owner。请求体:`{"role":"admin"}`(`binding:"required"`)。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X PUT $BASE/api/v1/tenants/1/members/u-123 -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"role":"admin"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/tenants/:id/members/:user_id
|
||||
|
||||
用途:移除成员。权限:Owner。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/tenants/1/members/u-123 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/leave
|
||||
|
||||
用途:退出空间(任何成员可自行退出;服务层拒绝导致空间无 Owner 的退出)。权限:Viewer+,仅 JWT。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/leave -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 空间邀请(/tenants/:id/invitations 与 invite-links)
|
||||
|
||||
Handler: `internal/handler/tenant_invitation.go`、`internal/handler/tenant_invite_link.go`。API key 需 `manage_members` 或 full-access。
|
||||
|
||||
### GET /api/v1/tenants/:id/invitations
|
||||
|
||||
用途:空间邀请列表。权限:Viewer+。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `include_terminal` | bool | 否 | 包含已完结邀请 |
|
||||
| `page` / `page_size` | int | 否 | 分页 |
|
||||
|
||||
响应:200 `{"success":true,"data":{"invitations":[{id,tenant_id,invitee_email,inviter_email,role,status,message,expires_at,is_share_link,accepted_count,...}],"total","page","page_size"}}`
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/tenants/1/invitations -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/invitations
|
||||
|
||||
用途:邀请成员(被邀请人在 `/me/invitations` 确认后才入库)。权限:Owner。
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `email` | string | 是(`binding:"required,email"`) | 被邀请邮箱 |
|
||||
| `role` | string | 是(`binding:"required"`) | 授予角色 |
|
||||
| `message` | string | 否 | 附言 |
|
||||
|
||||
响应:201 `{"success":true,"data":{TenantInvitationResponse}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/invitations -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"email":"c@ex.com","role":"viewer"}'
|
||||
```
|
||||
|
||||
### DELETE /api/v1/tenants/:id/invitations/:inv_id
|
||||
|
||||
用途:撤销邀请。权限:Owner。
|
||||
|
||||
响应:200 `{"success":true}`
|
||||
|
||||
```bash
|
||||
curl -X DELETE $BASE/api/v1/tenants/1/invitations/12 -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### POST /api/v1/tenants/:id/invite-links
|
||||
|
||||
用途:创建分享链接(多次可用的注册邀请链接)。权限:Owner。Handler: `internal/handler/tenant_invite_link.go`
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `role` | string | 是(`binding:"required"`) | 链接授予的角色 |
|
||||
| `message` | string | 否 | 附言 |
|
||||
|
||||
响应:201 `{"success":true,"data":{id,token,invite_url,role,status,expires_at,is_share_link:true,accepted_count}}`
|
||||
|
||||
```bash
|
||||
curl -X POST $BASE/api/v1/tenants/1/invite-links -H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' -d '{"role":"viewer"}'
|
||||
```
|
||||
|
||||
## 审计日志
|
||||
|
||||
Handler: `internal/handler/audit_log.go`。游标分页。
|
||||
|
||||
### GET /api/v1/tenants/:id/audit-log
|
||||
|
||||
用途:空间审计日志(含被拒绝操作记录)。权限:Admin+,仅 JWT。
|
||||
|
||||
| 查询参数 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `after_id` | int | 否 | 游标(上次响应 `next_cursor`) |
|
||||
| `limit` | int | 否 | 1-100,默认 50 |
|
||||
| `action` | string | 否 | 按动作过滤(如 `rbac.member_added`) |
|
||||
| `outcome` | string | 否 | `success` / `denied` |
|
||||
| `actor` | string | 否 | 按操作者 user_id 过滤 |
|
||||
|
||||
响应:200 `{"success":true,"data":[AuditLog],"next_cursor":N}`
|
||||
|
||||
```bash
|
||||
curl "$BASE/api/v1/tenants/1/audit-log?limit=50" -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
### GET /api/v1/knowledge-bases/:id/activity
|
||||
|
||||
用途:单个 KB 的活动流(只读审计)。权限:KB 创建者 OR Admin+,且对 KB 有 read 权限;仅 JWT。查询参数同上(`after_id/limit/action/outcome/actor`)。注册于 `RegisterKnowledgeBaseActivityRoutes`。
|
||||
|
||||
响应:200 `{"success":true,"data":[AuditLog],"next_cursor":N}`。`details` 为动作负载;若该条由 API Key 触发,会包含 `api_key_id` 与 `api_key_name`(名称快照,不含明文 Key)。
|
||||
|
||||
```bash
|
||||
curl $BASE/api/v1/knowledge-bases/kb-1/activity -H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
## 实现参考
|
||||
|
||||
路由注册:`internal/router/routes_auth_tenant.go` 的 `RegisterTenantRoutes`。Handler:`internal/handler/tenant.go`、`internal/handler/tenant_member.go`、`internal/handler/tenant_invitation.go`、`internal/handler/tenant_invite_link.go`、`internal/handler/audit_log.go`。
|
||||
Reference in new issue
Block a user