feat: 本地 API 支持按需转写语音消息

读取消息时返回本地已有的语音转写及其状态
新增 query/voice-transcribe 接口按 messageRef 转写指定语音
This commit is contained in:
qinsehm1128 committed 2026-10-12 15:16:50 +08:00
1 parent e5f0aa59f4
commit 4d9af4a193
11 files changed
+843 -14

No files matched your search

+47
View File
@@ -337,3 +337,50 @@ curl -X POST -H "$AUTH" -H 'Content-Type: application/json' "$BASE/query/convers
调用方**必须**把 `coverage` 当真:`coverage.state` 不是 `complete` 且 `evidence` 为空时,只能说明"这段范围暂时无法确认",**不能**下"没有找到"的结论。索引落后时服务端会自动请求一次追赶同步,但不会让请求无限等待;`freshness.catchUp` 为 `pending` 表示追赶仍在后台进行,稍后重试即可拿到更新的覆盖。
`query/messages` 与 `query/conversation-overview` 直读聊天数据库,不受索引新鲜度影响。
### 语音转文字(voice-transcribe)
语音消息的正文固定是 `[语音消息]`。本机已转写过的语音,读取时会直接带上转写结果(只读缓存,不触发识别):
- `query/messages`、`query/message-context`:`voiceTranscriptState` 与 `voiceTranscript`,`derivedSource` 为 `voice_transcript`;
- `/chatlog`:语音消息额外带 `messageRef`、`voiceTranscriptState` 与 `voiceTranscript`。
`voiceTranscriptState` 为 `pending` 表示**还没转写**,不能当成"语音里没说话"。需要内容时,把这些语音的 `messageRef` 交给 `POST /api/v1/query/voice-transcribe`:
```bash
curl -X POST -H "$AUTH" -H 'Content-Type: application/json' "$BASE/query/voice-transcribe" \
-d '{"messageRefs":["<语音消息的 messageRef>"],"waitMs":15000}'
```
| 字段 | 含义 |
| ------------- | --------------------------------------------------------------------------------------- |
| `messageRefs` | 必填,1~20 条;重复的引用会合并 |
| `waitMs` | 可选,最多同步等待的毫秒数,默认 15000,上限 60000;`0` 表示只排队、不等待 |
| `force` | 可选,`true` 时忽略已有转写和失败记录、重新识别;只在用户明确要求重试时使用 |
识别在本机离线进行(需要先在 TraceMemo 设置里下载语音识别模型),和界面共用同一份缓存与同一条串行队列:界面上点开语音会优先处理,API 的任务随后继续。等待超时的条目返回 `pending`,识别会在后台继续;稍后用同样的 `messageRefs` 再调用即可取回结果,不会重复排队。
```json
{
"status": "completed",
"items": [
{ "messageRef": "…", "state": "transcribed", "transcript": "…", "cached": false },
{ "messageRef": "…", "state": "pending", "code": "still_processing", "hint": "…" }
],
"summary": { "transcribed": 1, "pending": 1, "failed": 0 }
}
```
每个未成功的条目都带 `code` 与 `hint`(下一步该怎么做):
| `code` | 含义 |
| ----------------------- | ------------------------------------------ |
| `still_processing` | 仍在后台转写,稍后原样重试 |
| `previously_failed` | 之前识别失败,默认不自动重跑 |
| `recognition_failed` | 本次识别失败(音频损坏、过短或没有人声) |
| `recognition_timeout` | 识别超时 |
| `not_voice_message` | 不是语音消息 |
| `message_not_found` | 找不到消息(可能已切换账号) |
| `invalid_message_ref` | `messageRef` 无法解析 |
整次请求的状态:`200 completed`;`400 invalid_tool_arguments`(带 `field`、`constraint`、`hint`);`503 voice_model_not_ready`(模型未下载)或 `knowledge_unavailable`(数据库未连接);`429 busy`(队列已满,带 `retryAfterSeconds` 与 `Retry-After` 头)。
+14
View File
@@ -42,6 +42,7 @@ description: 通过 TraceMemo 本地 HTTP API 按需读取用户有权访问的
| POST | `/query/search` | 受限语义关键词检索(依赖本地索引,见 freshness) |
| POST | `/query/message-context` | 用 `messageRef` 取某条消息的前后文 |
| POST | `/query/conversation-overview` | 按会话与时间范围提取可总结的证据 |
| POST | `/query/voice-transcribe` | 把指定语音消息转成文字(本机离线识别) |
| GET | `/query/capabilities` | Query 端点能力目录 |
| GET | `/wechat-personal/send-capability` | 个人微信发送能力状态(文字 / 图片 / 语音) |
| GET | `/scheduled-reports` | 查询全部定时日报任务 |
@@ -118,6 +119,7 @@ description: 通过 TraceMemo 本地 HTTP API 按需读取用户有权访问的
- `query/search`:受限语义关键词检索,依赖本地索引;
- `query/message-context`:用返回的 `messageRef` 取前后文;
- `query/conversation-overview`:按会话与时间范围提取可总结的证据。
- `query/voice-transcribe`:把指定语音消息的 `messageRef` 转成文字。
两个要点:
@@ -147,6 +149,18 @@ description: 通过 TraceMemo 本地 HTTP API 按需读取用户有权访问的
不要只根据 `[图片]` 猜测内容,不要把一次 OCR 当作完整图片理解,也不要直接读取任意本地图片路径。
### 语音消息
语音消息的正文固定是 `[语音消息]`,转写结果在 `voiceTranscript`,状态在 `voiceTranscriptState`(`/chatlog` 与 `query/*` 都会返回)。
1. `transcribed`:直接使用 `voiceTranscript`。
2. `pending`:还没转写,**不能**说成“语音里没内容”。回答需要它时,把这些语音的 `messageRef` 批量(每次最多 20 条)交给 `POST /query/voice-transcribe`。
3. 返回 `pending`(`still_processing`)时,稍后用同样的 `messageRefs` 再调用一次;不要换参数反复重试。
4. 返回 `failed` 时按 `hint` 处理;`previously_failed` 只有用户明确要求才传 `force: true` 重试。
5. `503 voice_model_not_ready`:告诉用户先在 TraceMemo 设置中下载语音识别模型,在此之前不要重试。
6. `429 busy`:等待 `retryAfterSeconds` 后再调用。
7. 只转写回答问题所需的语音,不要一次把整个会话的语音都提交。
## 隐私和安全
只读取用户请求所需的会话和时间范围。不要把完整聊天数据库、密钥或 Token 暴露给用户。Reader API 本身不自动把聊天转发到外部服务器,但当前 Agent 可能会把工具结果交给其配置的模型;如有疑问,提醒用户检查 Agent 的数据策略。