20 KiB
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":[类型定义]}
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":"..."}
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,...)
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]}
curl $BASE/api/v1/vector-stores -H "Authorization: Bearer $TOKEN"
GET /api/v1/vector-stores/:id
用途:向量库详情(支持 __env_* ID)。权限:Viewer+。
响应:200 {"success":true,"data":{VectorStoreResponse}}
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}}
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}
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"}
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 | 否 | 提供方配置,字段见存储后端(响应中凭证掩码) |
status |
string | 否 | active(默认)/disabled |
GET /api/v1/storage-backends/types
用途:STORAGE_ALLOW_LIST 允许的 provider 名称列表(未设置时返回全部)。权限:Viewer+。响应:200 {"success":true,"data":["local","minio",...]}
curl $BASE/api/v1/storage-backends/types -H "Authorization: Bearer $TOKEN"
POST /api/v1/storage-backends/test
用途:原始配置连接测试。权限:Admin+。响应:200 {"success":bool,"error"}
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}}
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":"..."}
curl $BASE/api/v1/storage-backends -H "Authorization: Bearer $TOKEN"
GET /api/v1/storage-backends/:id
用途:详情(凭证掩码)。权限:Viewer+。响应:200 {"success":true,"data":{StorageBackend}}
curl $BASE/api/v1/storage-backends/sb-1 -H "Authorization: Bearer $TOKEN"
PUT /api/v1/storage-backends/:id
用途:更新。权限:Admin+。响应:200 {"success":true,"data":{StorageBackend}}
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}
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"}
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}
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 参数见联网搜索。
GET /api/v1/web-search/providers
用途:内置搜索提供方目录(只读)。权限:Viewer+,仅 JWT(未声明 API key 策略)。Handler: internal/handler/web_search.go
响应:200 {"success":true,"data":[...]}
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":[...]}
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"}
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}}
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":[...]}
curl $BASE/api/v1/web-search-providers -H "Authorization: Bearer $TOKEN"
GET /api/v1/web-search-providers/:id
用途:详情。权限:Viewer+。响应:200 {"success":true,"data":{...}}
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":{...}}
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}
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}}}}
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。
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"}
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、资源选择与同步限制见数据源导入。sync_deletions 开启后会真实删除该数据源归属下的已删除知识;source_created_at/source_updated_at 保存在知识 metadata 中。
GET /api/v1/datasource/types
用途:可创建的连接器目录,只含当前部署已注册的连接器。权限:Viewer+。
响应:200 [{type,name,description,icon,priority,auth_type,capabilities,doc_url,permission_doc_url,permission_page_url,required_permissions,config_schema}]
config_schema 用 JSON Schema 子集描述该连接器 config.credentials 的字段,x-secret: true 标记的字段为密钥;前端按它渲染凭证表单。doc_url、permission_*、required_permissions 是凭证表单旁的接入指引。
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":"..."}
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 |
响应:201 DataSourceResponse(凭证剥离,见 internal/handler/dto/datasource.go)。
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]
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"}
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
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。
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}}}}
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。
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"}
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}]
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":[...]}
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,...)
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"}
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]
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"}
curl $BASE/api/v1/datasource/logs/log-1 -H "Authorization: Bearer $TOKEN"
插件(/api/v1/plugins)
插件目录与空间级开关。内置能力(模型厂商、数据源、IM 渠道、联网搜索、Agent 工具、文档解析引擎)按厂商分组,以内置插件的形式列出,例如 weknora.feishu 同时提供飞书知识库、飞书云盘两个数据源和飞书 IM 渠道。Handler: internal/handler/plugin.go。
GET /api/v1/plugins
用途:列出全部插件及当前空间是否启用。权限:Viewer+。
响应:200 {"success":true,"data":[{"manifest":{...},"enabled":true,"updatedAt":"..."}]}。manifest.contributes 按扩展点列出贡献(modelVendors、connectors、imChannels、webSearch、tools、parsers),required: true 的插件不能停用。
curl $BASE/api/v1/plugins -H "Authorization: Bearer $TOKEN"
GET /api/v1/plugins/:id
用途:单个插件,结构同上。权限:Viewer+。
GET /api/v1/plugins/contributions
用途:按扩展点列出全部贡献,可用 ?point=connectors 只取一个扩展点。每项带 pluginId、qualifiedId(如 weknora.feishu/feishu)、aliases(存量数据使用的短名)和 enabled。权限:Viewer+。
PUT /api/v1/plugins/:id/enabled
用途:为当前空间启用或停用插件。停用后,该插件的贡献不再出现在 /datasource/types、/web-search-providers/types、/im-channels/platforms、/models/providers 中,新建对应实例返回 400;已有实例不受影响。必需插件不能停用(400)。权限:Admin+。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
enabled |
bool | 是 | 是否启用 |
响应:200 {"success":true,"data":{"manifest":{...},"enabled":false}}
curl -X PUT $BASE/api/v1/plugins/weknora.notion/enabled -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' -d '{"enabled":false}'
实现参考
路由注册: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。