Files
syops/EXTERNAL-INTEGRATION-API.md
T
2026-04-27 17:03:56 +08:00

424 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 文档清洗系统对外接口文档
版本:`v1`
适用对象:需要根据 Dify 文档信息反查文档清洗系统中的本地原文件,并获取原文件下载地址的外部系统。
## 1. 接入目标
外部系统通常已经从 Dify 拿到了某条知识库文档的以下信息:
- `dataset_id`
- `remote_document_id`
其中:
- `dataset_id` = Dify 知识库 id
- `remote_document_id` = Dify 文档 id
- `remote_document_id` 应从 Dify 文档接口返回的 `id` 获取,不是 batch id
外部系统可使用本文档提供的接口,反查这条 Dify 文档在文档清洗系统中的本地映射关系,并获取原文件下载地址。
## 2. 接入原则
- 外部系统应由服务端调用查询接口,不要在前端浏览器中保存 `INTEGRATION_API_KEY`。
- 查询接口需要鉴权。
- 文件下载地址为短期签名链接,前端可直接打开,不需要再附带鉴权 key。
- 下载链接过期后,应重新调用查询接口获取新的下载链接。
## 3. 基础信息
- Base URL:`http://192.168.100.230:8891`
- 鉴权方式:HTTP Header
- Header 名称:`X-Integration-Key`
请求示例:
```http
X-Integration-Key: <平台分配的对接密钥>
```
说明:
- 该 key 由文档清洗系统平台方提供。
- 外部系统请将该 key 保存在自身服务端配置中,不要下发到前端页面。
## 4. 推荐对接流程
### 4.1 外部系统后端流程
1. 从 Dify 获取目标文档的:
- `dataset_id`
- `remote_document_id`
2. 调用文档清洗系统查询接口:
- `GET /api/integration/dify-file`
3. 解析返回结果,读取:
- 文件名
- 所属知识库
- 所属部门
- 原文件下载地址
4. 将需要展示的字段返回给自身前端页面。
5. 前端用户点击下载时,直接打开 `source_download_url`。
### 4.2 外部系统前端流程
1. 调用外部系统自己的后端接口。
2. 展示外部系统后端返回的文件信息。
3. 用户点击“下载原文件”时,直接访问 `source_download_url`。
4. 如果下载失败且提示链接失效,由外部系统后端重新查询一次并返回新链接。
## 5. 核心接口:按 Dify 文档反查本地文件
- 方法:`GET`
- 路径:`/api/integration/dify-file`
### 5.1 请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `dataset_id` | `string` | 是 | Dify 知识库 id |
| `remote_document_id` | `string` | 是 | Dify 文档 id,应取自 Dify 文档接口返回的 `id` |
### 5.2 请求头
```http
X-Integration-Key: <平台分配的对接密钥>
```
### 5.3 请求示例
```bash
curl -H "X-Integration-Key: YOUR_KEY" \
"http://192.168.100.230:8891/api/integration/dify-file?dataset_id=7a9f158c-90f5-478a-8956-2732c72a7546&remote_document_id=abcd1234"
```
### 5.4 成功响应示例
```json
{
"queried_at": "2026-04-01T12:00:00+08:00",
"document_id": 14,
"knowledge_base": {
"id": 3,
"name": "03 质量",
"department_id": 2,
"department_name": "质量部",
"dify_dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546"
},
"title": "假期控制程序1",
"source_filename": "假期控制程序1.doc",
"source_ext": ".doc",
"file_name": "假期控制程序1.doc",
"version_id": 22,
"version_no": 1,
"convert_status": "success",
"sync_status": "synced",
"uploaded_at": "2026-04-01T11:32:10+08:00",
"converted_at": "2026-04-01T11:33:05+08:00",
"download_url_expires_at": "2026-04-01T12:15:00+08:00",
"source_download_url": "http://192.168.100.230:8891/api/integration/download?version_id=22&kind=source&token=xxxxx",
"markdown_download_url": "http://192.168.100.230:8891/api/integration/download?version_id=22&kind=md&token=yyyyy",
"markdown_download_url_expires_at": "2026-04-01T12:15:00+08:00",
"markdown_public_url": "http://192.168.100.230:8891/storage/markdown/group_0003/document_000014/v001/document.md",
"dify": {
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "abcd1234",
"status": "synced",
"last_indexing_status": "completed",
"last_synced_at": "2026-04-01T11:34:20+08:00"
}
}
```
## 6. 核心接口:批量按 Dify 文档反查本地文件
- 方法:`POST`
- 路径:`/api/integration/dify-files/resolve`
### 6.1 请求头
```http
X-Integration-Key: <平台分配的对接密钥>
Content-Type: application/json
```
### 6.2 请求体
```json
{
"items": [
{
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "abcd1234"
},
{
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "efgh5678"
}
]
}
```
说明:
- `items` 为待查询列表。
- 建议每次批量查询控制在 `100` 条以内。
- 返回结果会按请求里的输入顺序返回。
- `remote_document_id` 应取自 Dify 文档接口返回的 `id`,不是 batch id。
### 6.3 请求示例
```bash
curl -X POST \
-H "X-Integration-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d "{\"items\":[{\"dataset_id\":\"7a9f158c-90f5-478a-8956-2732c72a7546\",\"remote_document_id\":\"abcd1234\"},{\"dataset_id\":\"7a9f158c-90f5-478a-8956-2732c72a7546\",\"remote_document_id\":\"efgh5678\"}]}" \
"http://192.168.100.230:8891/api/integration/dify-files/resolve"
```
### 6.4 成功响应示例
```json
{
"data": [
{
"request": {
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "abcd1234"
},
"found": true,
"document": {
"document_id": 14,
"source_filename": "假期控制程序1.doc",
"source_download_url": "http://192.168.100.230:8891/api/integration/download?version_id=22&kind=source&token=xxxxx",
"knowledge_base": {
"id": 3,
"name": "03 质量",
"department_id": 2,
"department_name": "质量部",
"dify_dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546"
},
"dify": {
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "abcd1234"
}
}
},
{
"request": {
"dataset_id": "7a9f158c-90f5-478a-8956-2732c72a7546",
"remote_document_id": "efgh5678"
},
"found": false,
"document": null
}
]
}
```
### 6.5 返回说明
- `request`
- 原样回显本次输入的查询条件
- `found`
- 是否命中文档清洗系统中的本地映射
- `document`
- 命中时返回完整文档信息
- 未命中时返回 `null`
## 6.6 版本映射说明
- 反查接口返回的是“命中的同步版本”,不是盲目返回当前最新版本。
- 如果同一文档本地后来又上传了新版本,但新版本尚未同步到 Dify,接口仍会返回旧的已同步版本。
- 如果某条文档从未同步到 Dify,则不会存在可用的 `remote_document_id` 映射。
## 7. 返回字段说明
### 7.1 顶层字段
| 字段名 | 类型 | 说明 |
|---|---|---|
| `queried_at` | `string` | 本次查询结果生成时间 |
| `document_id` | `integer` | 文档清洗系统内部文档 id |
| `title` | `string` | 文档清洗系统内部文档标题 |
| `source_filename` | `string` | 原始上传文件名 |
| `source_ext` | `string` | 原文件扩展名 |
| `file_name` | `string` | 与 `source_filename` 相同,便于统一读取 |
| `version_id` | `integer` | 当前版本 id |
| `version_no` | `integer` | 当前版本号 |
| `convert_status` | `string` | 转换状态,如 `queued` `running` `success` `failed` |
| `sync_status` | `string` | 同步状态,如 `unsynced` `syncing` `synced` `sync_failed` |
| `uploaded_at` | `string` | 原文件上传时间 |
| `converted_at` | `string` | Markdown 转换完成时间 |
| `download_url_expires_at` | `string` | 原文件下载链接过期时间 |
| `source_download_url` | `string` | 原文件签名下载链接 |
| `markdown_download_url` | `string` | Markdown 签名下载链接 |
| `markdown_download_url_expires_at` | `string` | Markdown 下载链接过期时间 |
| `markdown_public_url` | `string` | Markdown 在本系统内的访问地址 |
| `knowledge_base` | `object` | 所属知识库信息 |
| `dify` | `object` | Dify 映射信息 |
### 7.2 `knowledge_base` 字段说明
| 字段名 | 类型 | 说明 |
|---|---|---|
| `id` | `integer` | 文档清洗系统内部知识库 id |
| `name` | `string` | 知识库名称 |
| `department_id` | `integer` \| `null` | 所属部门 id |
| `department_name` | `string` \| `null` | 所属部门名称 |
| `dify_dataset_id` | `string` \| `null` | 该知识库在 Dify 对应的 dataset id |
### 7.3 `dify` 字段说明
| 字段名 | 类型 | 说明 |
|---|---|---|
| `dataset_id` | `string` | Dify 知识库 id |
| `remote_document_id` | `string` | Dify 文档 id |
| `status` | `string` | 本地记录的最近同步状态 |
| `last_indexing_status` | `string` | Dify 最近索引状态 |
| `last_synced_at` | `string` \| `null` | 最近同步完成时间 |
## 8. 下载说明
### 8.1 下载接口
外部系统通常不需要自己拼下载接口,直接使用查询结果中的:
- `source_download_url`
- `markdown_download_url`
即可。
### 8.2 下载接口格式
- 方法:`GET`
- 路径:`/api/integration/download`
请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `version_id` | `integer` | 是 | 文档清洗系统内部版本 id |
| `kind` | `string` | 是 | `source` 或 `md` |
| `token` | `string` | 是 | 签名令牌 |
示例:
```text
http://192.168.100.230:8891/api/integration/download?version_id=22&kind=source&token=xxxxx
```
### 8.3 下载接口说明
- 下载接口不需要再附带 `X-Integration-Key`。
- 这是一个短期签名链接,可直接在浏览器打开。
- 链接过期后,请重新调用查询接口获取新的下载链接。
## 9. 可选查询接口
如果外部系统不是从 Dify 文档开始反查,也可使用以下接口。
### 9.1 获取知识库列表
- 方法:`GET`
- 路径:`/api/integration/knowledge-bases`
查询参数:
| 参数名 | 必填 | 说明 |
|---|---|---|
| `department_id` | 否 | 按部门筛选 |
| `q` | 否 | 按知识库名称模糊搜索 |
请求示例:
```bash
curl -H "X-Integration-Key: YOUR_KEY" \
"http://192.168.100.230:8891/api/integration/knowledge-bases"
```
### 9.2 获取文件列表
- 方法:`GET`
- 路径:`/api/integration/files`
查询参数:
| 参数名 | 必填 | 说明 |
|---|---|---|
| `knowledge_base_id` | 否 | 按知识库筛选 |
| `department_id` | 否 | 按部门筛选 |
| `file_name` | 否 | 按文件名或标题模糊搜索 |
请求示例:
```bash
curl -H "X-Integration-Key: YOUR_KEY" \
"http://192.168.100.230:8891/api/integration/files?knowledge_base_id=3"
```
### 9.3 获取单个文件详情
- 方法:`GET`
- 路径:`/api/integration/files/{file_id}`
请求示例:
```bash
curl -H "X-Integration-Key: YOUR_KEY" \
"http://192.168.100.230:8891/api/integration/files/14"
```
## 10. 状态码说明
### 10.1 查询接口
| 状态码 | 说明 |
|---|---|
| `200` | 查询成功 |
| `401` | 鉴权失败,`X-Integration-Key` 无效 |
| `404` | 未找到对应的本地文件映射 |
| `503` | 接口未配置完成,通常为平台未配置 `INTEGRATION_API_KEY` |
### 10.2 下载接口
| 状态码 | 说明 |
|---|---|
| `200` | 下载成功 |
| `401` | 签名无效或已过期 |
| `404` | 文件不存在 |
| `409` | 请求 `md`,但 Markdown 尚未生成 |
## 11. 对接建议
- 外部系统前端不要直接调用查询接口。
- 外部系统后端负责:
- 保存 integration key
- 调用查询接口
- 将结果整理后返回给自身前端
- 外部系统前端负责:
- 展示文件信息
- 使用 `source_download_url` 直接下载原文件
## 12. 最小接入流程
1. 外部系统从 Dify 获取:
- `dataset_id`
- `remote_document_id`
2. 外部系统后端调用:
- `GET /api/integration/dify-file?dataset_id=...&remote_document_id=...`
3. 从返回结果中读取:
- `source_filename`
- `source_download_url`
- `knowledge_base.name`
- `knowledge_base.department_name`
4. 将上述字段展示给用户。
5. 用户点击下载时,直接打开 `source_download_url`。
## 13. 对接注意事项
- `source_download_url` 为短期签名链接,不是永久地址。
- `source_download_url` 可直接用于下载,无需再次附加鉴权 key。
- 若链接过期,外部系统应重新调用查询接口获取新链接。
- 若返回 `404`,表示该 Dify 文档在文档清洗系统中没有本地文件映射。