@chen_hu_hu/pi-qq 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # pi-qq
2
2
 
3
- Connect [pi](https://github.com/earendil-works/pi) to a QQ private chat. Send text, images, or files from mobile QQ; pi works on your machine and sends the result back to QQ.
3
+ Connect [pi](https://github.com/earendil-works/pi) to a QQ private chat. Send text, voice, images, or files from mobile QQ; pi works on your machine and sends the result back to QQ.
4
4
 
5
5
  ## Install
6
6
 
@@ -27,42 +27,57 @@ Credentials are kept in process memory only. Restarting pi requires scanning aga
27
27
  Send a QQ private message to the bot to start a task.
28
28
 
29
29
  - **Text**: normal chat message.
30
+ - **Voice**: QQ built-in ASR text is sent to pi directly; the audio is not downloaded or saved. If ASR returns no text, the bot asks you to resend or type it.
30
31
  - **Images**: sent to pi as vision input.
31
- - **Files**: saved under the current workspace at `.pi/pi-qq/YYYY-MM-DD/<msgId>/`; the saved path, size, and original filename are added to the prompt.
32
+ - **Files**: saved under the current workspace at `.pi/pi-qq/<sessionId>/uploads/`. A file-only message does not start pi; send the instruction in the next text or voice message, and the saved path(s), size, and original filename are added to that prompt.
33
+
34
+ When you upload multiple files before giving an instruction, they are combined into the next prompt. `/new` clears files waiting for an instruction.
35
+
36
+ Uploads are scoped to the current pi session. If you upload the same filename with identical content, pi reuses the existing file; the same filename with different content is saved as `name-1.ext`; different filenames with identical content are kept as separate files.
32
37
 
33
38
  Example file prompt seen by pi:
34
39
 
35
40
  ```text
36
41
  User sent the following file via QQ (saved locally; use read/bash or other tools to access it):
37
- - /home/chen/project/myproj/.pi/pi-qq/2026-10-29/xxx/report.pdf (1.4MB, original name: report.pdf)
42
+ - /home/chen/project/myproj/.pi/pi-qq/<sessionId>/uploads/report.pdf (1.4MB, original name: report.pdf)
38
43
 
39
44
  User message:
40
45
  Summarize this file.
41
46
  ```
42
47
 
48
+ ### Voice messages
49
+
50
+ Voice messages use QQ's built-in ASR (`asr_refer_text`). The transcript is sent to pi as normal user text; pi never receives the audio file.
51
+
52
+ - The voice attachment is not downloaded or saved as `.amr` / `.silk`.
53
+ - No local or external speech-to-text service is used.
54
+ - If QQ ASR returns no text, the bot replies `Could not transcribe this voice message; please resend or type it.` and does not start a pi run.
55
+ - Voice and text are separate QQ messages; send them one after another when needed.
56
+ - To inspect the raw ASR result, set `logLevel` to `debug`; the log includes `asr_refer_text` and attachment metadata.
57
+
43
58
  ## Commands
44
59
 
45
60
  Run these in the pi terminal:
46
61
 
47
- | Command | Description |
48
- | --- | --- |
49
- | `/qq-login` | Log in with a QR code and bind the scanning QQ user |
50
- | `/qq-status` | Show login, connection, model, context, and bound user |
51
- | `/qq-logout` | Log out and clear in-memory credentials |
52
- | `/qq-test-budget` | Show passive-reply budget and stream-bubble diagnostics |
53
- | `/qq-test-md [text]` | Send a Markdown test message |
54
- | `/qq-test-stream` | Run a streamed timeline diagnostic |
55
- | `/qq-new` | Reset the current session (used by QQ `/new`) |
62
+ | Command | Description |
63
+ | -------------------- | ------------------------------------------------------- |
64
+ | `/qq-login` | Log in with a QR code and bind the scanning QQ user |
65
+ | `/qq-status` | Show login, connection, model, context, and bound user |
66
+ | `/qq-logout` | Log out and clear in-memory credentials |
67
+ | `/qq-test-budget` | Show passive-reply budget and stream-bubble diagnostics |
68
+ | `/qq-test-md [text]` | Send a Markdown test message |
69
+ | `/qq-test-stream` | Run a streamed timeline diagnostic |
70
+ | `/qq-new` | Reset the current session (used by QQ `/new`) |
56
71
 
57
72
  QQ-side commands:
58
73
 
59
- | Command | Description |
60
- | --- | --- |
61
- | `/new` | Start a new pi session |
62
- | `/stop` | Abort the current run |
63
- | `/compact` | Compact the current context |
64
- | `/status` | Show session and context status |
65
- | `/help` | Show command help |
74
+ | Command | Description |
75
+ | ---------- | ------------------------------- |
76
+ | `/new` | Start a new pi session |
77
+ | `/stop` | Abort the current run |
78
+ | `/compact` | Compact the current context |
79
+ | `/status` | Show session and context status |
80
+ | `/help` | Show command help |
66
81
 
67
82
  ## Settings
68
83
 
@@ -72,22 +87,24 @@ Create `~/.pi/agent/pi-qq.json` to override defaults. This file contains setting
72
87
  {
73
88
  "markdownSupport": false,
74
89
  "streamFlushMs": 10000,
75
- "debug": false
90
+ "logLevel": "silent"
76
91
  }
77
92
  ```
78
93
 
79
- | Key | Default | Description |
80
- | --- | --- | --- |
81
- | `streamAssistant` | `true` | Stream assistant replies |
82
- | `markdownSupport` | `false` | Bot has QQ Markdown permission; pass Markdown through |
83
- | `deliverAs` | `"steer"` | Queue new messages while busy: `steer` or `followUp` |
84
- | `imageMaxSize` | `10485760` | QQ image download limit in bytes (10MB) |
85
- | `fileMaxSize` | `52428800` | QQ file download limit in bytes (50MB) |
86
- | `heartbeatMinutes` | `10` | Long-task heartbeat interval; `0` disables |
87
- | `heartbeatMaxMessages` | `3` | Per-run message heartbeat limit |
88
- | `streamRelay` | `true` | Use append-mode stream bubbles |
89
- | `streamFlushMs` | `10000` | Stream summary frame interval in ms (minimum 300) |
90
- | `debug` | `false` | SDK and stream retry/timeout diagnostics |
94
+ | Key | Default | Description |
95
+ | ---------------------- | ---------- | ------------------------------------------------------------ |
96
+ | `streamAssistant` | `true` | Stream assistant replies |
97
+ | `markdownSupport` | `false` | Bot has QQ Markdown permission; pass Markdown through |
98
+ | `deliverAs` | `"steer"` | Queue new messages while busy: `steer` or `followUp` |
99
+ | `imageMaxSize` | `10485760` | QQ image download limit in bytes (10MB) |
100
+ | `fileMaxSize` | `52428800` | QQ file download limit in bytes (50MB) |
101
+ | `heartbeatMinutes` | `10` | Long-task heartbeat interval; `0` disables |
102
+ | `heartbeatMaxMessages` | `3` | Per-run message heartbeat limit |
103
+ | `streamRelay` | `true` | Use append-mode stream bubbles |
104
+ | `streamFlushMs` | `10000` | Stream summary frame interval in ms (minimum 300) |
105
+ | `logLevel` | `"silent"` | Log threshold: `silent`, `error`, `warn`, `info`, or `debug` |
106
+
107
+ When enabled, logs are written to `<cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log`. `PI_QQ_LOG_DIR` overrides the directory. `info` records metadata only; `debug` records full events, attachments, prompts, and tool payloads. Legacy `debug: true` maps to `logLevel: "debug"`.
91
108
 
92
109
  ## Notes
93
110
 
@@ -95,7 +112,3 @@ Create `~/.pi/agent/pi-qq.json` to override defaults. This file contains setting
95
112
  - Incoming images are passed to the model as vision input; the current model must support image input.
96
113
  - Saved files are workspace-local under `.pi/`; add `.pi/` to your own project `.gitignore` if needed.
97
114
  - QQ limits passive replies per inbound message. Use `/qq-test-budget` to inspect the local budget.
98
-
99
- ## License
100
-
101
- MIT
@@ -5,11 +5,11 @@
5
5
 
6
6
  技术栈:
7
7
 
8
- | 依赖 | 用途 |
9
- | --- | --- |
10
- | `@tencent-connect/qqbot-nodejs` | QQ 开放平台协议层:WebSocket Gateway、C2C 消息收发、媒体上传、流式消息 |
11
- | `@tencent-connect/qqbot-connector` | 扫码登录:展示二维码,扫码后换取 `appId / appSecret / userOpenid`(仅内存) |
12
- | `@earendil-works/pi-coding-agent`(ExtensionAPI) | pi 扩展宿主:事件订阅、命令注册、消息注入、上下文用量 |
8
+ | 依赖 | 用途 |
9
+ | ------------------------------------------------- | --------------------------------------------------------------------------- |
10
+ | `@tencent-connect/qqbot-nodejs` | QQ 开放平台协议层:WebSocket Gateway、C2C 消息收发、媒体上传、流式消息 |
11
+ | `@tencent-connect/qqbot-connector` | 扫码登录:展示二维码,扫码后换取 `appId / appSecret / userOpenid`(仅内存) |
12
+ | `@earendil-works/pi-coding-agent`(ExtensionAPI) | pi 扩展宿主:事件订阅、命令注册、消息注入、上下文用量 |
13
13
 
14
14
  ---
15
15
 
@@ -157,7 +157,7 @@ pi-qq 扩展 ── pi.sendUserMessage(text+images) ─┴─ pi 事件:
157
157
  - **额度提示**:每条文本消息、每段活动各占一条 msg_seq 消息;长回合任务的气泡数多于单气泡模式,需关注被动额度;若平台拒绝(帧失败),该回合自动降级消息路径,文本仍完整送达。
158
158
  - **降级**:任何一帧失败即标 dead,已追加部分作废,未发出的结果文本自动转消息路径补发;`streamRelay: false` 时整条时间线关闭,assistant 消息走普通消息路径(M2 行为)。
159
159
  - **typing 保鲜**:run 开始立即发送"正在输入",之后每 50s 续期(0 额度),`agent_end` 清理。
160
- - **空窗口活性线**:窗口无增量且静默 ≥60s 时补一条 `⏱ 已运行 X 分钟…`——长思考/单工具长执行期间每分钟一行,不再无推送(debug 模式带时间戳)。
160
+ - **空窗口活性线**:窗口无增量且静默 ≥60s 时补一条 `⏱ Running for X minutes…`——长思考/单工具长执行期间每分钟一行,不再无推送(debug 模式带时间戳)。
161
161
  - **心跳兜底**:流不可用时降级为普通消息心跳(每 run ≤3 条保险丝;debug 模式带时间戳)。
162
162
  - 不发送"已收到"开始确认(节省被动额度):QQ 触发的 run 在首个汇总帧前保持安静。
163
163
  - 非 QQ 触发的 run(本地终端)不发心跳/时间线,但 assistant 消息仍照常回传(现状)。
@@ -166,13 +166,13 @@ pi-qq 扩展 ── pi.sendUserMessage(text+images) ─┴─ pi 事件:
166
166
 
167
167
  QQ 开放平台的发送限制直接决定回传策略,必须统一管理:
168
168
 
169
- | 限制 | 数值 | 应对 |
170
- | --- | --- | --- |
171
- | 被动回复额度 | 每条入站消息 5 分钟内有效,约 4 条/小时 | 扩展内维护回复预算(复用 SDK `ReplyLimiter` 思路);assistant 时间线按文本/活动分泡 |
172
- | 主动消息 | 无 `msgId` 发送受平台额度限制 | 被动额度耗尽时才降级为主动发送,且给出明确提示 |
173
- | 流式消息 | 仅 C2C;节流 ≥300ms;基于 5 分钟内 `msgId` | 流式仅用于 assistant 文本;无关联 `msgId` 时自动降级 `sendText` |
174
- | 文本长度 | 单条 ~5000 字符 | SDK 分块;超出部分按序发送 |
175
- | markdown | 默认无权限(`markdownSupport: false`) | Markdown → 纯文本降级渲染 |
169
+ | 限制 | 数值 | 应对 |
170
+ | ------------ | ------------------------------------------ | ----------------------------------------------------------------------------------- |
171
+ | 被动回复额度 | 每条入站消息 5 分钟内有效,约 4 条/小时 | 扩展内维护回复预算(复用 SDK `ReplyLimiter` 思路);assistant 时间线按文本/活动分泡 |
172
+ | 主动消息 | 无 `msgId` 发送受平台额度限制 | 被动额度耗尽时才降级为主动发送,且给出明确提示 |
173
+ | 流式消息 | 仅 C2C;节流 ≥300ms;基于 5 分钟内 `msgId` | 流式仅用于 assistant 文本;无关联 `msgId` 时自动降级 `sendText` |
174
+ | 文本长度 | 单条 ~5000 字符 | SDK 分块;超出部分按序发送 |
175
+ | markdown | 默认无权限(`markdownSupport: false`) | Markdown → 纯文本降级渲染 |
176
176
 
177
177
  **合并规则**(默认):assistant 文本消息与活动时间线严格分泡(每条文本消息 1 条纯文本气泡,工具间隔另起活动气泡);不再发送用量摘要,上下文占比通过 `/status` 查询。气泡数随回合内文本消息数增长,需关注 `msgId` 的被动额度;若平台拒绝(帧失败),自动降级消息路径。工具调用过程本身不逐条回传。
178
178
 
@@ -180,31 +180,31 @@ QQ 开放平台的发送限制直接决定回传策略,必须统一管理:
180
180
 
181
181
  `~/.pi/agent/pi-qq.json`(只保存设置;不含凭据、不写入登录信息):
182
182
 
183
- | 字段 | 默认 | 说明 |
184
- | --- | --- | --- |
185
- | `streamAssistant` | `true` | assistant 流式回传开关 |
186
- | `markdownSupport` | `false` | 机器人是否有 markdown 权限;`true` 时回传透传 Markdown |
187
- | `deliverAs` | `"steer"` | 忙碌时新消息的处理方式 |
188
- | `imageMaxSize` | `10MB` | QQ 图片下载上限 |
189
- | `fileMaxSize` | `50MB` | QQ 文件下载上限 |
190
- | `heartbeatMinutes` | `10`(0 关闭) | F7 节流心跳间隔 |
191
- | `heartbeatMaxMessages` | `3` | F7 每 run 消息心跳上限(流式心跳不受限) |
192
- | `streamRelay` | `true` | F7 流式载体开关(append 追加 + 节流合帧 + 限流退避,实测可用;失败自动降级消息路径) |
193
- | `streamFlushMs` | `10000`(最小 300) | F7 流式汇总帧间隔(毫秒,默认每 10s 汇总工具调用) |
194
- | `debug` | `false` | 诊断模式:SDK 日志转终端 + 流式重试/超时日志 + 行组/文本帧时间戳 |
183
+ | 字段 | 默认 | 说明 |
184
+ | ---------------------- | ------------------- | ------------------------------------------------------------------------------------ |
185
+ | `streamAssistant` | `true` | assistant 流式回传开关 |
186
+ | `markdownSupport` | `false` | 机器人是否有 markdown 权限;`true` 时回传透传 Markdown |
187
+ | `deliverAs` | `"steer"` | 忙碌时新消息的处理方式 |
188
+ | `imageMaxSize` | `10MB` | QQ 图片下载上限 |
189
+ | `fileMaxSize` | `50MB` | QQ 文件下载上限 |
190
+ | `heartbeatMinutes` | `10`(0 关闭) | F7 节流心跳间隔 |
191
+ | `heartbeatMaxMessages` | `3` | F7 每 run 消息心跳上限(流式心跳不受限) |
192
+ | `streamRelay` | `true` | F7 流式载体开关(append 追加 + 节流合帧 + 限流退避,实测可用;失败自动降级消息路径) |
193
+ | `streamFlushMs` | `10000`(最小 300) | F7 流式汇总帧间隔(毫秒,默认每 10s 汇总工具调用) |
194
+ | `debug` | `false` | 诊断模式:SDK 日志转终端 + 流式重试/超时日志 + 行组/文本帧时间戳 |
195
195
 
196
196
  ## 5. 命令总表
197
197
 
198
- | 位置 | 命令 | 行为 |
199
- | --- | --- | --- |
200
- | pi 侧 | `/qq-login` | 扫码登录并绑定(QR only) |
201
- | pi 侧 | `/qq-status` | 登录态、连接状态、绑定用户 |
202
- | pi 侧 | `/qq-test-budget` | 诊断:被动额度、流式气泡数、最近 msgId 新鲜度 |
203
- | pi 侧 | `/qq-logout` | 清除内存凭据并断开 |
204
- | pi 侧 | `/qq-new` | 重置当前会话(QQ `/new` 调用) |
205
- | pi 侧 | `/qq-test-md [text]` | 诊断:发送 Markdown 消息,验证 C2C markdown 渲染能力 |
206
- | pi 侧 | `/qq-test-stream` | 诊断:开流→三帧→complete,验证流式帧是否渲染 |
207
- | QQ 侧 | `/new` `/stop` `/compact` `/status` `/help` | 会话控制(见 F2) |
198
+ | 位置 | 命令 | 行为 |
199
+ | ----- | ------------------------------------------- | ---------------------------------------------------- |
200
+ | pi 侧 | `/qq-login` | 扫码登录并绑定(QR only) |
201
+ | pi 侧 | `/qq-status` | 登录态、连接状态、绑定用户 |
202
+ | pi 侧 | `/qq-test-budget` | 诊断:被动额度、流式气泡数、最近 msgId 新鲜度 |
203
+ | pi 侧 | `/qq-logout` | 清除内存凭据并断开 |
204
+ | pi 侧 | `/qq-new` | 重置当前会话(QQ `/new` 调用) |
205
+ | pi 侧 | `/qq-test-md [text]` | 诊断:发送 Markdown 消息,验证 C2C markdown 渲染能力 |
206
+ | pi 侧 | `/qq-test-stream` | 诊断:开流→三帧→complete,验证流式帧是否渲染 |
207
+ | QQ 侧 | `/new` `/stop` `/compact` `/status` `/help` | 会话控制(见 F2) |
208
208
 
209
209
  ## 6. 可靠性与安全
210
210
 
@@ -216,24 +216,24 @@ QQ 开放平台的发送限制直接决定回传策略,必须统一管理:
216
216
 
217
217
  ## 7. 决策记录
218
218
 
219
- | # | 问题 | 决策 |
220
- | --- | --- | --- |
221
- | 1 | TUI 二维码渲染 | 登录协议用 connector;展示用 `qrcode-terminal`(零依赖)在 `ctx.ui` 弹窗画码 |
222
- | 2 | thinking 内容 | 不回传具体内容(始终剔除,无配置开关);F7 时间线中仅以 `[💭] Thinking...` 标记体现 |
223
- | 3 | tool 消息 | **完全不回传**;只回传 assistant 消息,上下文占比通过 `/status` 查询 |
224
- | 4 | 图片方向 | F3 为 QQ → pi(用户发图给 pi);pi 不向 QQ 发图片(LLM 仅输出纯文本) |
225
- | 5 | 成本/用量摘要 | 已移除每轮用量摘要;不展示 token、缓存命中率、成本 |
226
- | 6 | 回复格式 | 回复通常为 Markdown 格式的总结/提问文本(不在回复中写代码);默认轻量转纯文本,有 markdown 权限可透传 |
227
- | 7 | 登录方式 | 仅 `/qq-login` 扫码;凭据只存进程内存、不落盘;pi 进程退出后必须重新扫码 |
228
- | 8 | 长任务反馈 | typing 保鲜 + 每 10s 汇总工具调用 + 节流心跳(默认 10 分钟/条,流式载体优先,每 run ≤3 条消息心跳保险丝)+ 机会性流式(平台拒绝即降级);不发送开始确认 |
229
- | 9 | 流式可用性(实测三轮) | ① C2C markdown(msg_type=2,`sendMarkdown`)渲染正常;② SDK 内置流式 `input_mode="replace"` 被平台拒绝("已经提交的消息内容不可修改"),首帧可渲染、后续帧报错;③ 自实现 `AppendStream`(input_mode="append",增量追加 + DONE)实测通过;补齐节流合帧(500ms)与限流退避重试后**默认开启**,失败自动降级消息路径 |
219
+ | # | 问题 | 决策 |
220
+ | --- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
221
+ | 1 | TUI 二维码渲染 | 登录协议用 connector;展示用 `qrcode-terminal`(零依赖)在 `ctx.ui` 弹窗画码 |
222
+ | 2 | thinking 内容 | 不回传具体内容(始终剔除,无配置开关);F7 时间线中仅以 `[💭] Thinking...` 标记体现 |
223
+ | 3 | tool 消息 | **完全不回传**;只回传 assistant 消息,上下文占比通过 `/status` 查询 |
224
+ | 4 | 图片方向 | F3 为 QQ → pi(用户发图给 pi);pi 不向 QQ 发图片(LLM 仅输出纯文本) |
225
+ | 5 | 成本/用量摘要 | 已移除每轮用量摘要;不展示 token、缓存命中率、成本 |
226
+ | 6 | 回复格式 | 回复通常为 Markdown 格式的总结/提问文本(不在回复中写代码);默认轻量转纯文本,有 markdown 权限可透传 |
227
+ | 7 | 登录方式 | 仅 `/qq-login` 扫码;凭据只存进程内存、不落盘;pi 进程退出后必须重新扫码 |
228
+ | 8 | 长任务反馈 | typing 保鲜 + 每 10s 汇总工具调用 + 节流心跳(默认 10 分钟/条,流式载体优先,每 run ≤3 条消息心跳保险丝)+ 机会性流式(平台拒绝即降级);不发送开始确认 |
229
+ | 9 | 流式可用性(实测三轮) | ① C2C markdown(msg_type=2,`sendMarkdown`)渲染正常;② SDK 内置流式 `input_mode="replace"` 被平台拒绝("已经提交的消息内容不可修改"),首帧可渲染、后续帧报错;③ 自实现 `AppendStream`(input_mode="append",增量追加 + DONE)实测通过;补齐节流合帧(500ms)与限流退避重试后**默认开启**,失败自动降级消息路径 |
230
230
 
231
231
  ## 8. 里程碑
232
232
 
233
- | 阶段 | 内容 | 验收 |
234
- | --- | --- | --- |
235
- | M1 | F1 扫码登录 + 内存凭据 + 连接启动 | 扫码后 `/qq-status` 在线;重启 pi 需重新扫码 |
236
- | M2 | F2 单人聊天 + F4 assistant 回传(先非流式) | QQ 发消息得到 pi 回复 |
237
- | M4 | F3 QQ 图片/文件消息 + markdownSupport + 打磨 | 图片可进入 pi 会话;文件保存并关联到提示词 |
233
+ | 阶段 | 内容 | 验收 |
234
+ | ---- | -------------------------------------------- | -------------------------------------------- |
235
+ | M1 | F1 扫码登录 + 内存凭据 + 连接启动 | 扫码后 `/qq-status` 在线;重启 pi 需重新扫码 |
236
+ | M2 | F2 单人聊天 + F4 assistant 回传(先非流式) | QQ 发消息得到 pi 回复 |
237
+ | M4 | F3 QQ 图片/文件消息 + markdownSupport + 打磨 | 图片可进入 pi 会话;文件保存并关联到提示词 |
238
238
 
239
239
  > 原 M3 用量摘要已取消(2026-10-29)。
@@ -0,0 +1,209 @@
1
+ # pi-qq — features-v2.md
2
+
3
+ > V2 增量:F8 语音自动转写、F9 轻量日志基础设施、F10 文件消息等待指令、F11 文件存储与去重。
4
+ > 基线见 [features-v1.md](./features-v1.md);本文只描述新增/变更内容。
5
+
6
+ ## 1. 背景与结论
7
+
8
+ - 当前语音附件被当作普通文件保存,pi 只能本地解码 SILK 并转写;慢且可能误识别。
9
+ - 实测确认 C2C 语音消息的 `attachments[]` 包含 `asr_refer_text`(示例:`"测试。"`),`content` 为空;SDK 1.0.5 已透传该字段。
10
+ - 结论:直接使用 QQ 内置 ASR;不下载语音附件,不接本地或外部 STT。
11
+
12
+ ## 2. F8 语音消息自动转写
13
+
14
+ ### 触发与识别
15
+
16
+ - 仅 C2C、白名单用户。
17
+ - `content_type === "voice"` 视为语音消息。
18
+ - 读取并 trim `asr_refer_text`。
19
+
20
+ ### 行为
21
+
22
+ | 情况 | 处理 |
23
+ | ----------- | ------------------------------------------------------------------------------------- |
24
+ | 有 ASR 文本 | `markQQTrigger()` → `pi.sendUserMessage(text, { deliverAs })` |
25
+ | ASR 为空 | 回复 `Could not transcribe this voice message; please resend or type it.`,不触发 run |
26
+ | 语音消息 | 不下载 `url` / `voice_wav_url`,不保存文件,不走图片/文件分支 |
27
+
28
+ - 语音和文字是两条独立消息,不做同条组合。
29
+ - 转写文本不参与 QQ 命令解析,作为普通用户消息交给 pi。
30
+ - 正常一条语音消息只有一个语音附件;多值时按顺序换行拼接,仅作防御。
31
+
32
+ ### 不实现
33
+
34
+ - 本地 Whisper 或外部 STT 回退
35
+ - 保存原始语音
36
+
37
+ ### 代码边界
38
+
39
+ - 新增 `src/voice.ts`:`isVoiceAttachment()`、`resolveVoiceTranscript()`。
40
+ - `src/inbound.ts`:在图片/文件分流前增加语音分支,处理完直接返回。
41
+
42
+ ### 验收
43
+
44
+ - 发语音后 pi 直接收到文本,不出现 `.amr` / `.silk` 路径。
45
+ - 无 `ffmpeg`、`pilk`、`faster-whisper` 等调用,工作区不新增语音文件。
46
+ - 空 ASR 只回提示,不启动 agent。
47
+ - 文字、图片、文件行为不变。
48
+
49
+ ## 3. F9 日志基础设施
50
+
51
+ ### 实现
52
+
53
+ - 自研轻量 logger,不引入第三方框架。
54
+ - 单文件按天:`<cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log`;可用 `PI_QQ_LOG_DIR` 覆盖。
55
+ - 每条一行 JSON:`time`、`level`、`scope`、`msg` + 结构化字段。
56
+ - 日志写入失败不得影响 QQ 消息处理。
57
+ - SDK 日志接入同一 logger。
58
+
59
+ ### 配置
60
+
61
+ ```json
62
+ {
63
+ "logLevel": "silent"
64
+ }
65
+ ```
66
+
67
+ | 级别 | 输出 |
68
+ | -------- | ---------------------------------------------------- |
69
+ | `silent` | 不写日志(默认) |
70
+ | `error` | error |
71
+ | `warn` | warn + error |
72
+ | `info` | info + warn + error |
73
+ | `debug` | 全部,含 raw 事件、附件、prompt、工具参数/结果、正文 |
74
+
75
+ - 配置键使用 `logLevel`,不使用 `log_level`。
76
+ - 移除 `debug` 配置;旧 `debug: true` 迁移为 `logLevel: "debug"`,其他情况默认 `silent`。
77
+ - 移除 `debug` 控制的 QQ 气泡时间戳;日志级别不影响聊天展示。
78
+ - `info` 只记元数据(如 `hasAsr`、`asrLength`);`debug` 才记录 `asr_refer_text` 全文和完整附件。
79
+
80
+ ### 验收
81
+
82
+ - 各级别过滤正确;默认 `silent` 不产生日志。
83
+ - `logLevel: "debug"` 能看到语音原始附件和 ASR 文本;`info` 看不到正文。
84
+ - 旧 `debug` 配置可迁移。
85
+ - 日志路径、日期滚动、写入失败降级正常。
86
+
87
+ ## 4. F10 文件消息等待指令
88
+
89
+ ### 目标
90
+
91
+ QQ 不能同时发送文字和文件。非图片文件上传后先保存,等待用户下一条文字或语音指令,避免立即触发 pi。
92
+
93
+ ### 行为
94
+
95
+ | 情况 | 处理 |
96
+ | ------------ | ------------------------------------------------------------------------------------------------------------- |
97
+ | 只发文件 | 保存到 F11 定义的 session uploads 目录;加入 pending 队列;不回复;不调用 `sendUserMessage` / `markQQTrigger` |
98
+ | 连续上传文件 | 追加到同一 pending 队列;不回复 |
99
+ | 下一条文字 | 合并 pending 文件与当前文件,`formatFilePrompt(files, text)` 后 `sendUserMessage`;清空 pending;触发 run |
100
+ | 下一条语音 | 用 ASR 文本作为指令,合并 pending 文件;清空 pending |
101
+ | `/new` | 清空 pending |
102
+ | 其他 QQ 命令 | 不消费 pending,命令行为不变 |
103
+ | 图片 | 保持 v1 行为;图片可以随文字发送,不进入 pending |
104
+
105
+ - 部分文件保存失败:只把成功文件加入 pending,并回复失败清单。
106
+ - 文件消息不回复确认,不触发 agent run。
107
+ - pending 状态只保存在当前会话内存;pi 重启后丢失,已落盘文件仍保留。
108
+ - pending 文件按保存顺序合并;消费后清空,不重复注入。
109
+
110
+ ### 代码边界
111
+
112
+ - `src/inbound.ts`:维护 pending 队列;文件消息先入队,下一条文字/语音指令出队并合并。
113
+ - `/new` 命令清空 pending。
114
+ - 图片路径不变。
115
+
116
+ ### 验收
117
+
118
+ - 只发文件:文件已保存,不回复,`sendUserMessage` / `markQQTrigger` 均未调用。
119
+ - 文件后发文字:只触发一次 run,提示词包含文件路径和文字指令。
120
+ - 连续两个文件后发文字:两个文件路径都在提示词中。
121
+ - 文件后发语音:ASR 文本与文件一起注入。
122
+ - `/new`:pending 被清空。
123
+ - 文件保存失败:不进入 pending,仍回复失败信息。
124
+ - 图片、普通文字、既有文件+文字行为不变。
125
+
126
+ ## 5. F11 文件存储与去重
127
+
128
+ ### 路径
129
+
130
+ - 非图片文件保存到:
131
+ ```text
132
+ <cwd>/.pi/pi-qq/<sessionId>/uploads/
133
+ ```
134
+ - `sessionId` 来自 `ctx.sessionManager.getSessionId()`。
135
+ - 不再使用日期目录或 `msgId` 子目录。
136
+ - 文件名使用清理后的 basename;同名文件通过后缀区分。
137
+
138
+ ### 索引
139
+
140
+ 索引文件:
141
+
142
+ ```text
143
+ <cwd>/.pi/pi-qq/<sessionId>/uploads/.index.json
144
+ ```
145
+
146
+ 格式只记录文件名和内容摘要,不记录完整路径:
147
+
148
+ ```json
149
+ {
150
+ "a.txt": "<sha256>",
151
+ "a-1.txt": "<sha256>"
152
+ }
153
+ ```
154
+
155
+ ### 保存规则
156
+
157
+ | 情况 | 处理 |
158
+ | -------------------- | ------------------------------------------------------------ |
159
+ | 文件名不重复 | 保存文件;计算 SHA-256;写入索引;引用文件 |
160
+ | 文件名重复,内容不同 | 保存为下一个可用文件名(如 `a-1.txt`);写入索引;引用新文件 |
161
+ | 文件名重复,内容相同 | 不保存新文件;复用已有文件路径 |
162
+
163
+ - 只有文件名重复时才比较内容摘要;文件名不重复时即使内容已存在于其他文件名下,也按规则保存新文件。
164
+ - 文件名重复且内容不同时,从 `name-1.ext` 开始寻找可用文件名;如果该后缀文件已存在且内容相同,复用该后缀文件。
165
+ - 文件名重复且内容相同,复用已有同名文件,不生成副本。
166
+ - 图片、语音不进入该文件索引。
167
+
168
+ ### 索引维护
169
+
170
+ - QQ 登录 / 会话启动时加载索引:
171
+ - 对每个 `fileName` 检查 `uploads/fileName` 是否存在。
172
+ - 文件不存在则删除对应索引项。
173
+ - 只做存在性检查,不读取文件内容,不重新计算 hash。
174
+ - 索引有变化才原子写回。
175
+ - 保存或复用文件前再次检查目标文件是否存在;不存在则删除索引项并按新文件处理。
176
+ - 索引缺失或损坏时从空索引开始;遇到同名文件时再计算已有文件 hash 并补写索引。
177
+ - 文件保存与索引更新按用户消息串行执行;当前消息处理完成前不处理下一条,避免并发覆盖索引。
178
+ - 索引写入失败只记 warn,不影响文件保存和消息处理。
179
+ - 不新增配置参数。
180
+
181
+ ### 与 pending 的关系
182
+
183
+ - 同名同内容重复上传:只返回一个 `SavedFile`,pending 不重复加入。
184
+ - 不同文件名但内容相同:按规则保存两份,pending 保留两个路径。
185
+
186
+ ### 验收
187
+
188
+ - 文件保存到 `.pi/pi-qq/<sessionId>/uploads/`,不再出现日期 / `msgId` 目录。
189
+ - 索引文件只包含文件名到 SHA-256 的映射。
190
+ - 文件名不重复:保存并引用。
191
+ - 同名不同内容:保存为 `name-1.ext` 并引用新文件。
192
+ - 同名同内容:不新增文件,复用已有路径。
193
+ - 登录 / 会话启动:删除指向不存在文件的索引项,且不重新计算所有文件 hash。
194
+ - 索引缺失或损坏:同名文件仍能通过现有文件 hash 正确判断。
195
+ - 索引写入失败不影响文件保存和消息处理。
196
+
197
+ ## 6. 配置变更
198
+
199
+ | 配置 | 变更 |
200
+ | ---------- | -------------------- |
201
+ | `logLevel` | 新增;默认 `silent` |
202
+ | `debug` | 移除;仅保留迁移读取 |
203
+
204
+ ## 7. 限制
205
+
206
+ - `asr_refer_text` 可能为空或识别不准确;V2 只提示用户重发或改用文字,不做回退。
207
+ - `debug` 日志可能包含消息正文和 URL token,仅在排查时本地开启。
208
+ - 文件 pending 不跨 pi 重启;未消费的文件仍保留在 `.pi/pi-qq/<sessionId>/uploads/` 下。
209
+ - 索引信任写入时的 hash;如果用户手动修改同名文件内容,登录时的存在性检查不会发现。
package/doc/plan-v1.md CHANGED
@@ -12,11 +12,11 @@ npm init -y
12
12
  npm i @tencent-connect/qqbot-nodejs @tencent-connect/qqbot-connector qrcode-terminal
13
13
  ```
14
14
 
15
- | 包 | 版本锚点 | 用途 |
16
- | --- | --- | --- |
17
- | `@tencent-connect/qqbot-nodejs` | ^1.0.5 | QQ 协议层(本地另有源码 `/home/chen/project/qqbot-nodejs` 可查阅) |
18
- | `@tencent-connect/qqbot-connector` | ^1.2.0 | 扫码登录(唯一登录方式) |
19
- | `qrcode-terminal` | ^0.12 | 二维码 ASCII 渲染(零依赖) |
15
+ | 包 | 版本锚点 | 用途 |
16
+ | ---------------------------------- | -------- | ------------------------------------------------------------------ |
17
+ | `@tencent-connect/qqbot-nodejs` | ^1.0.5 | QQ 协议层(本地另有源码 `/home/chen/project/qqbot-nodejs` 可查阅) |
18
+ | `@tencent-connect/qqbot-connector` | ^1.2.0 | 扫码登录(唯一登录方式) |
19
+ | `qrcode-terminal` | ^0.12 | 二维码 ASCII 渲染(零依赖) |
20
20
 
21
21
  开发加载方式:`pi --extension /home/chen/project/pi-qq`(jiti 直接跑 TS,无需构建)。
22
22
 
@@ -45,13 +45,13 @@ pi-qq/
45
45
 
46
46
  ## 2. pi 事件与 SDK 接线总表
47
47
 
48
- | 方向 | 信号 | 处理 |
49
- | --- | --- | --- |
50
- | QQ → pi | `bot.on("message")`(kind=c2c) | 白名单 → QQ 命令(`/new` `/stop` `/compact` `/status` `/help`)→ 文本+图片 → `pi.sendUserMessage()` |
51
- | pi → QQ | `message_update`(assistant) | 流式:`openStream().update(转换后全文)`,节流 ≥300ms |
52
- | pi → QQ | `message_end`(assistant) | `stream.complete()`;无流上下文时 `sendText`;`stopReason==="error"` 回错误摘要 |
53
- | 状态 | `session_start` / `session_shutdown` | 有内存登录态则启动 bot / 幂等停止 |
54
- | 收尾 | `agent_end` | 封口最后一个流式气泡(DONE);不再发送用量摘要 |
48
+ | 方向 | 信号 | 处理 |
49
+ | ------- | ------------------------------------ | --------------------------------------------------------------------------------------------------- |
50
+ | QQ → pi | `bot.on("message")`(kind=c2c) | 白名单 → QQ 命令(`/new` `/stop` `/compact` `/status` `/help`)→ 文本+图片 → `pi.sendUserMessage()` |
51
+ | pi → QQ | `message_update`(assistant) | 流式:`openStream().update(转换后全文)`,节流 ≥300ms |
52
+ | pi → QQ | `message_end`(assistant) | `stream.complete()`;无流上下文时 `sendText`;`stopReason==="error"` 回错误摘要 |
53
+ | 状态 | `session_start` / `session_shutdown` | 有内存登录态则启动 bot / 幂等停止 |
54
+ | 收尾 | `agent_end` | 封口最后一个流式气泡(DONE);不再发送用量摘要 |
55
55
 
56
56
  ## 3. 里程碑
57
57
 
@@ -111,13 +111,13 @@ pi-qq/
111
111
 
112
112
  ## 5. 风险与预案
113
113
 
114
- | 风险 | 影响 | 预案 |
115
- | --- | --- | --- |
116
- | 流式消息对非 C2C/过期 `msgId` 抛错 | 流式路径崩溃 | `openStream` 前置校验,失败即降级 `sendText`(M4 已含) |
117
- | 被动额度耗尽(长 run 多回复) | 消息发不出去 | reply-budget 统一发牌,超限降级主动+提示;摘要固定合并 |
118
- | markdown 转换漏判 | QQ 端残留记号 | 转换函数单测覆盖常见结构;允许透传开关兜底 |
119
- | connector 混淆代码异常 / 扫码接口变更 | 无法登录 | 重试扫码;升级 connector;保留日志排查 |
120
- | pi 事件在非 TUI 模式无 UI | 扩展报错 | 所有 `ctx.ui` 调用前判 `ctx.hasUI`/`ctx.mode` |
114
+ | 风险 | 影响 | 预案 |
115
+ | ------------------------------------- | ------------- | ------------------------------------------------------- |
116
+ | 流式消息对非 C2C/过期 `msgId` 抛错 | 流式路径崩溃 | `openStream` 前置校验,失败即降级 `sendText`(M4 已含) |
117
+ | 被动额度耗尽(长 run 多回复) | 消息发不出去 | reply-budget 统一发牌,超限降级主动+提示;摘要固定合并 |
118
+ | markdown 转换漏判 | QQ 端残留记号 | 转换函数单测覆盖常见结构;允许透传开关兜底 |
119
+ | connector 混淆代码异常 / 扫码接口变更 | 无法登录 | 重试扫码;升级 connector;保留日志排查 |
120
+ | pi 事件在非 TUI 模式无 UI | 扩展报错 | 所有 `ctx.ui` 调用前判 `ctx.hasUI`/`ctx.mode` |
121
121
 
122
122
  ## 6. Todo-List(随进度勾选)
123
123
 
@@ -186,5 +186,5 @@ pi-qq/
186
186
  - [x] `debug` 抓超时日志:AppendStream 限流重试/最终失败 + run start/end 日志
187
187
  - [x] 验收:QQ 发图后 pi 能描述图片内容;全程额度不超限(2026-10-29 实测通过)
188
188
  - [x] 文件传输实机验收(2026-10-29 用户实测通过:QQ 发 zip/pdf 等文件,保存到工作空间 `.pi/pi-qq/` 并在提示词中关联路径)
189
- - 2026-10-29:图片+文字、纯图片(均被模型正确识别)、非图片附件拒绝(zip 提示“附件处理失败”)、多气泡+摘要额度诊断已实测通过。
189
+ - 2026-10-29:图片+文字、纯图片(均被模型正确识别)、非图片附件拒绝(zip 提示“Attachment failed”)、多气泡+摘要额度诊断已实测通过。
190
190
  - 无视觉模型场景按用户决定不验证(代码仍保留 `ctx.model.input` 检查)。