@chen_hu_hu/pi-qq 1.3.5 โ†’ 1.4.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.
@@ -0,0 +1,327 @@
1
+ # pi-qq delivery-v4
2
+
3
+ > **Archived record.** Content reflects the v4 delivery snapshot and may reference behavior that has since changed. See [README](../../README.md) for current behavior.
4
+ > Status: v4 delivered. Tool block types were verified on real QQ; long-running tool handling and offline delivery remain open items.
5
+ > Version: 1.4.0
6
+ > Date: 2026-11-04
7
+ > Previous: [delivery-v1.md](./delivery-v1.md), [delivery-v2.md](./delivery-v2.md), [delivery-v3.md](./delivery-v3.md).
8
+ > Open work: [doc/todo.md](../todo.md).
9
+
10
+ ## 1. Scope
11
+
12
+ v4 is an output-quality and observability release. This record consolidates six former v4 working documents (names are historical):
13
+
14
+ - tool start visibility (`tool-return`);
15
+ - skill display (`send-skill`);
16
+ - `/status`, language, and battery (`enhance-status`);
17
+ - QQ background delivery (`bugfix-v4`);
18
+ - long-running tools (`long-tool`);
19
+ - tool result code-block types (`block-type`).
20
+
21
+ The main changes are:
22
+
23
+ 1. a new tool output format with typed code blocks;
24
+ 2. a dedicated Skill block for `SKILL.md` reads;
25
+ 3. `/status` and `/qq-status` with agent state, language, and battery;
26
+ 4. QQ-side `/last` as a manual recovery path;
27
+ 5. debug diagnostics for long tools and stream failures.
28
+
29
+ The release does not change the transport: pi-qq still uses QQ C2C private messages, passive replies, and `stream_messages` for long output.
30
+
31
+ ## 2. Tool output
32
+
33
+ ### 2.1 Layout
34
+
35
+ Each content block gets its own bubble. Thinking stays plain text:
36
+
37
+ ```text
38
+ **๐Ÿ’ญ ๆ€่€ƒ**
39
+
40
+ reasoning...
41
+ ```
42
+
43
+ Tool calls use a second-level heading:
44
+
45
+ ````markdown
46
+ ## ๐Ÿ’ป bash
47
+
48
+ ```bash
49
+ npm test
50
+ ```
51
+
52
+ ```text
53
+ 12 passed
54
+ ```
55
+ ````
56
+
57
+ Rules:
58
+
59
+ - the tool header is `## <icon> <name>`;
60
+ - `bash` commands use a `bash` code block and preserve newlines;
61
+ - commands are displayed in full, without the old 200-character argument limit;
62
+ - `read` / `write` / `edit` keep their path as inline code;
63
+ - other tools keep their primary argument as inline code;
64
+ - tool call and result stay in the same bubble;
65
+ - a tool bubble is never split between the call and its result.
66
+
67
+ Result code-block language is chosen by tool and, for `read`, by file extension:
68
+
69
+ | Source | Result language |
70
+ | --------------------- | ----------------------------------------------------- |
71
+ | `bash` | `text` |
72
+ | `write`, `edit` | `text` |
73
+ | `read` | language from extension; unknown falls back to `text` |
74
+ | error or empty result | `text` |
75
+ | other tools | `text` |
76
+
77
+ Common mappings include `md` โ†’ `markdown`, `json` โ†’ `json`, `yaml` โ†’ `yaml`, `ts` โ†’ `typescript`, `tsx` โ†’ `tsx`, `js` โ†’ `javascript`, `py` โ†’ `python`, `sh`/`bash`/`zsh` โ†’ `bash`, `html` โ†’ `html`, `css` โ†’ `css`, `sql` โ†’ `sql`, `rs` โ†’ `rust`, `go` โ†’ `go`, and `diff`/`patch` โ†’ `diff`.
78
+
79
+ Special file names:
80
+
81
+ - `Dockerfile` โ†’ `dockerfile`;
82
+ - `Makefile` โ†’ `makefile`;
83
+ - `CMakeLists.txt` โ†’ `cmake`;
84
+ - `README` โ†’ `markdown`.
85
+
86
+ Empty and failed results always use `text`. Result text is still capped at 600 characters / 8 lines; the bash command itself is not truncated.
87
+
88
+ Fixed tool icons:
89
+
90
+ | Tool | Icon | Tool | Icon |
91
+ | -------- | ---- | -------- | ---- |
92
+ | `bash` | ๐Ÿ’ป | `read` | ๐Ÿ“– |
93
+ | `write` | ๐Ÿ“ | `edit` | โœ๏ธ |
94
+ | `grep` | ๐Ÿ”Ž | `search` | ๐Ÿ” |
95
+ | `find` | ๐Ÿ—‚๏ธ | `ls` | ๐Ÿ“ |
96
+ | `list` | ๐Ÿ“‹ | `web` | ๐ŸŒ |
97
+ | `fetch` | ๐Ÿ“ก | `delete` | ๐Ÿ—‘๏ธ |
98
+ | `remove` | โŒ | `todo` | โœ… |
99
+ | `test` | ๐Ÿงช | other | ๐Ÿ”ง |
100
+
101
+ ### 2.2 Skill block
102
+
103
+ A `read` of a `SKILL.md` file is rendered as:
104
+
105
+ ```markdown
106
+ ## ๐Ÿงฉ Skill
107
+
108
+ `playwright-cli`
109
+ ```
110
+
111
+ - skill name is the parent directory;
112
+ - `path` and `file_path` are both supported;
113
+ - Unix and Windows separators are supported;
114
+ - the `SKILL.md` content is never forwarded;
115
+ - the generic `๐Ÿ“– read` line and its result are suppressed;
116
+ - each read produces one skill bubble; no de-duplication;
117
+ - QQ does not support `/skill:`; only model-initiated reads are detected.
118
+
119
+ ### 2.3 Bubble boundaries
120
+
121
+ - Thinking, tools, and text are separate blocks.
122
+ - A run may produce many bubbles, but a tool call and result are always together.
123
+ - `MAX_BUBBLE_CHARS = 12_000` triggers a safe split at a block boundary or line start.
124
+ - Do not split inside an open code fence or while a resume checkpoint/tool is active.
125
+ - Parallel tools are shown one at a time in start order; results are correlated by `toolCallId`.
126
+ - Text and thinking that arrive while tools are pending are deferred so they cannot appear between a tool call and its result.
127
+
128
+ ## 3. Status, language, and battery
129
+
130
+ QQ `/status` and terminal `/qq-status` share one formatter.
131
+
132
+ Current fields:
133
+
134
+ | Field | Content |
135
+ | ---------- | ------------------------------------------------------------- |
136
+ | title | `๐Ÿ–ฅ๏ธ pi-qq status` / `pi-qq ็Šถๆ€` |
137
+ | login | QR login and masked AppID, or `not logged in` |
138
+ | connection | `connected`, `reconnecting`, `stopped`, `starting`, `blocked` |
139
+ | agent | `running (QQ)`, `running (local)`, or `completed` |
140
+ | model | current model name |
141
+ | context | tokens / window / percent |
142
+ | QQ user | masked bound openid |
143
+ | battery | percent and charging/discharging/AC; omitted when no battery |
144
+ | settings | `~/.pi/agent/pi-qq.json` |
145
+
146
+ Agent state uses `ctx.isIdle()` as the global signal and `runFeedback.isQQRunActive()` to distinguish a QQ-triggered run from a local run.
147
+
148
+ Battery support:
149
+
150
+ - Linux: `/sys/class/power_supply/BAT*`;
151
+ - macOS: `pmset -g batt`;
152
+ - Windows: PowerShell `Get-CimInstance Win32_Battery`;
153
+ - 30-second cache;
154
+ - failures are silent and hide the battery line;
155
+ - no new npm dependency.
156
+
157
+ Language:
158
+
159
+ - new `language` setting: `"zh-cn"` or `"en-us"`;
160
+ - default is `"zh-cn"`;
161
+ - user-facing command replies, status text, errors, stream placeholders, and pi-qq log messages are localized;
162
+ - logs keep stable fields and add `msgKey` for language-independent filtering;
163
+ - SDK-originated errors and third-party log text remain unmodified;
164
+ - invalid values fall back to the default and produce a warning.
165
+
166
+ `QQ user` replaces the old `Bound user` label and masks the openid.
167
+
168
+ ## 4. Tool start visibility
169
+
170
+ The investigation found that tool start information is delayed by the stream batching layer, not by the tool itself:
171
+
172
+ - `AppendStream` uses a 500ms throttle with a 300ms floor;
173
+ - a short tool call and its result can land in the same buffer and appear together;
174
+ - a long tool normally sends its start frame after about 500ms;
175
+ - parallel tools are serialized by `RunFeedback.activeTool`, so a later tool may wait for the current tool to finish.
176
+
177
+ The proposed `flushAfter` behavior was not implemented in v4. The block-type change still improves visibility because bash parameters are no longer a single inline-code line that QQ may collapse.
178
+
179
+ Version 1.3.6 added debug timing logs:
180
+
181
+ - tool enqueue / append start / append done;
182
+ - stream buffer, flush timer, and flush result;
183
+ - event-loop `meanMs`, `maxMs`, `p99Ms`, and later `driftMs`.
184
+
185
+ ## 5. Long-running tools
186
+
187
+ ### 5.1 Observed failure chain
188
+
189
+ A long tool can leave QQ silent because:
190
+
191
+ 1. the run has no new assistant event while the tool runs;
192
+ 2. the only tool frame is the start bubble;
193
+ 3. a long gap makes the next stream frame likely to hit `msg_id` expiry or passive-reply limits;
194
+ 4. `BubbleWriter` resumes with the same expired `msg_id` / `stream_msg_id`;
195
+ 5. after resume attempts are exhausted, `killStream()` makes later tool output disappear;
196
+ 6. `onToolEnd()` early-returns when the writer is dead, leaving `activeTool` and deferred text stuck;
197
+ 7. only the final assistant text may fall back to a normal message, and that fallback becomes proactive after 5 minutes.
198
+
199
+ Additional contributing factors:
200
+
201
+ - each thinking/tool bubble is a new stream reply, but `ReplyBudget` does not count stream bubbles against the 4-reply limit;
202
+ - `/stop` waits for `waitForEnd()` before replying, which can hold the SDK concurrency guard;
203
+ - no `tool_execution_update` progress is forwarded;
204
+ - new QQ messages are queued as `steer` until the tool batch finishes.
205
+
206
+ ### 5.2 What shipped
207
+
208
+ - debug diagnostics only; no behavior fix;
209
+ - QQ `/last` as the manual recovery path:
210
+ - while running: `RunFeedback.repushCurrent()` rebinds to the new `/last` message and pushes current text;
211
+ - when idle: `sendStreamReply()` pushes the latest completed assistant text.
212
+ - diagnostics added in 1.3.6 and the follow-up debug pass:
213
+ - tool `durationMs`, `updateCount`, `updateSpanMs`, first partial output;
214
+ - run target selection, tool queued/started/finished/ignored, assistant final decision, run cleanup;
215
+ - writer `dead`, `hasPendingResume`, `resumeAttempts`, `lastFrameAgeMs`;
216
+ - stream frame `httpStatus`, `bizCode`, `path`, `msgSeq`, `streamMsgId`, `bufferLength`;
217
+ - `/stop` wait duration;
218
+ - event-loop drift.
219
+
220
+ ### 5.3 Known protocol risks
221
+
222
+ Not fixed in v4:
223
+
224
+ - stream bubble quota is not accounted;
225
+ - `DONE` frames currently have empty `content_raw`;
226
+ - stream requests send both `event_id` and `msg_id`, although the protocol says one of them;
227
+ - stream resume reuses expired identifiers;
228
+ - no pending-output queue for expired passive windows;
229
+ - no final ordinary-text fallback after a successful stream;
230
+ - SDK heartbeat ACK timeout is not implemented;
231
+ - session persistence is not enabled.
232
+
233
+ ## 6. QQ background and offline delivery
234
+
235
+ The reported case was: iPhone QQ stays in the background for a long time, pi keeps running, and after reopening QQ no bot output from the offline period is visible. New inbound messages still reach pi, and the bot can reply to them.
236
+
237
+ The likely explanation is client-side/offline delivery: QQ does not replay the streamed bot content that was produced while the phone was offline. This was not fully confirmed because no incident log was available.
238
+
239
+ What v4 shipped:
240
+
241
+ - `/last` as a manual recovery path;
242
+ - status/diagnostic visibility for login, connection, agent, model, context, and budget.
243
+
244
+ What v4 did not ship:
245
+
246
+ - 60-minute passive-window correction (the code still uses 5 minutes);
247
+ - persistent final text after streaming;
248
+ - offline summary on the next inbound message;
249
+ - SDK heartbeat ACK timeout;
250
+ - session persistence;
251
+ - automatic rebind of an active run to a new inbound message.
252
+
253
+ ## 7. Diagnostics
254
+
255
+ Terminal commands:
256
+
257
+ - `/qq-test-budget` โ€” passive budget and stream bubble counters;
258
+ - `/qq-test-md [text]` โ€” Markdown rendering test;
259
+ - `/qq-test-stream` โ€” stream timeline and resume test.
260
+
261
+ Debug logging:
262
+
263
+ - enable `logLevel: "debug"` in `~/.pi/agent/pi-qq.json`;
264
+ - logs are written to `<cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log`;
265
+ - `PI_QQ_LOG_DIR` can override the directory;
266
+ - `info` records metadata; `debug` records full events, attachments, prompts, and tool arguments/results.
267
+
268
+ ## 8. Modules and boundaries
269
+
270
+ <!-- prettier-ignore -->
271
+ | Module | Responsibility |
272
+ | ------ | -------------- |
273
+ | `index.ts` | Extension wiring, pi events, login/session commands, status collection |
274
+ | `src/qq/` | QQ lifecycle, QR login, inbound routing, replies, diagnostics, status formatter |
275
+ | `src/relay/` | Thinking/tool/text relay, bubble layout, stream sessions, tool result formatting |
276
+ | `src/quota/` | Passive reply budget and diagnostics |
277
+ | `src/attachments/` | File download, storage, pending files, voice transcript |
278
+ | `src/infra/` | Settings, i18n, logging, power status, display formatting |
279
+
280
+ Boundary rules:
281
+
282
+ - formatting is pure and lives in `stream-block-format.ts`;
283
+ - relay orchestration lives in `run-stream-relay.ts` / `bubble-writer.ts`;
284
+ - stream frames and retries live in `stream-session.ts`;
285
+ - inbound QQ parsing and command routing live under `src/qq/`;
286
+ - all configuration goes through `settings.ts`;
287
+ - modules receive dependencies from the entrypoint and remain independently testable.
288
+
289
+ Images use the same save/pending path as files; the earlier image-specific pipeline and `imageMaxSize` were removed before v4.
290
+
291
+ ## 9. Verification
292
+
293
+ - Unit tests: **172 passed** across 18 test files.
294
+ - TypeScript, ESLint, and Prettier checks pass.
295
+ - Real QQ block-type verification on 2026-11-04:
296
+ - test directory: `/home/chen/project/pi-qq-test`;
297
+ - files: `hello.py`, `Hello.java`, `hello.rs`, `hello.ts`;
298
+ - log: `.pi/pi-qq/logs/pi-qq-2026-11-04.log`;
299
+ - observed `## ๐Ÿ’ป bash` with a bash block, `## ๐Ÿ“– read` with rust/typescript result blocks, `## ๐Ÿ“ write` with an inline path;
300
+ - event-loop `driftMs` was 0โ€“1ms and `maxMs` about 101ms.
301
+ - `/qq-test-stream` was not separately executed during this acceptance.
302
+ - A full manual pass of `/status` and long-running/offline scenarios is still recommended.
303
+
304
+ ## 10. Risks and limitations
305
+
306
+ - Long tasks can exhaust the 4 passive replies because stream bubbles are not counted.
307
+ - A 5-minute-old message falls back to proactive delivery; users may disable active messages.
308
+ - Stream resume does not recover an expired `msg_id` / `stream_msg_id`.
309
+ - Long tools have no progress heartbeat; only `typing` is sent.
310
+ - Tool start may be delayed by the 500ms throttle.
311
+ - QQ Markdown support for language-specific highlighting varies by client.
312
+ - A 12k split is stateful; extreme single-line or unclosed-fence content may still hit platform behavior.
313
+ - C2C private chat only; credentials stay in process memory.
314
+ - Debug logs may contain prompts, tool arguments, and tool results.
315
+
316
+ ## 11. Open items
317
+
318
+ - [doc/todo.md](../todo.md) item 1: long-running bash argument/tool-start visibility.
319
+ - [doc/todo.md](../todo.md) item 2: long-running tool leading to missing QQ output.
320
+ - Fix stream quota accounting before adding more per-block bubbles.
321
+ - Decide how to handle expired passive windows and pending output.
322
+ - Validate `DONE` content and the `event_id` / `msg_id` usage against the QQ API.
323
+ - Add a progress heartbeat or forward `tool_execution_update`.
324
+ - Add SDK heartbeat ACK timeout and consider session persistence.
325
+ - Decide whether `/status` and `/last` need further real-client acceptance.
326
+
327
+ Until the live logs confirm the failure mode, the investigation and diagnostics remain the source of truth; no speculative relay rewrite is included in v4.
package/doc/todo.md ADDED
@@ -0,0 +1,184 @@
1
+ # TODO
2
+
3
+ ## 1. ้•ฟ่ฟ่กŒ bash ๅทฅๅ…ท็š„ๅ‚ๆ•ฐๅปถ่ฟŸๆ˜พ็คบ
4
+
5
+ > ็Šถๆ€๏ผšTODO๏ผŒ็ญ‰ๅพ…้•ฟๆœŸไฝฟ็”จๆ—ฅๅฟ—
6
+ > ่ฎฐๅฝ•ๆ—ฅๆœŸ๏ผš2026-11-04
7
+ > ็›ธๅ…ณ็‰ˆๆœฌ๏ผšpi-qq 1.3.6๏ผˆไป…ๅขžๅŠ  debug ๆ—ฅๅฟ—๏ผ‰
8
+
9
+ ### ็Žฐ่ฑก
10
+
11
+ ๆญฃๅธธไฝฟ็”จไธญ๏ผŒbash ๆ‰ง่กŒ e2e ๆต‹่ฏ•้€šๅธธ้œ€่ฆๆ•ฐๅˆ†้’Ÿใ€‚ๆญคๆ—ถ QQ ไธŠๅช่ƒฝ็œ‹ๅˆฐ `bash` ๅ’Œไธ€ไธช็‚น๏ผŒ็œ‹ไธๅˆฐๅ‘ฝไปคๅ‚ๆ•ฐ๏ผ›็†่ฎบไธŠๅทฅๅ…ทๅ็งฐๅ’Œๅ‚ๆ•ฐๅบ”่ฏฅๅœจ `tool_execution_start` ๅŽ็ซ‹ๅณๅ‡บ็Žฐ๏ผŒๅชๆœ‰็ป“ๆžœ้œ€่ฆ็ญ‰ๅพ…ๅทฅๅ…ท็ป“ๆŸใ€‚
12
+
13
+ ### ๅทฒ็Ÿฅไบ‹ๅฎž
14
+
15
+ - `AppendStream` ้ป˜่ฎคๆœ‰ 500ms ่Š‚ๆต๏ผ›่ฟ™ๆ˜ฏ pi-qq ่‡ชๅทฑ่ฎพ็ฝฎ็š„ๅ€ผ๏ผŒไธๆ˜ฏ QQ ้™ๅˆถใ€‚
16
+ - 500ms ๅฎšๆ—ถๅ™จไธไพ่ต–ๅทฅๅ…ท Promise๏ผ›้•ฟๅทฅๅ…ท็š„ๅฏๅŠจๅธงๆ›พๅœจๅทฅๅ…ท็ป“ๆŸๅ‰้€š่ฟ‡ `stream_messages` ๅ‘ๅ‡บใ€‚
17
+ - pi ๅ†…็ฝฎ bash ไฝฟ็”จๅผ‚ๆญฅ `spawn`๏ผŒๆญฃๅธธๆƒ…ๅ†ตไธ‹ไธไผš้˜ปๅกž Node ไบ‹ไปถๅพช็Žฏใ€‚
18
+ - ๆœฌๅœฐๅผ‚ๆญฅๅญ่ฟ›็จ‹ๅฏๆŒ็ปญ่งฆๅ‘ 500ms timer๏ผŒ่ฏดๆ˜Žโ€œbash ๅญ่ฟ›็จ‹ๆœฌ่บซโ€ไธๆ˜ฏๅฟ…็„ถ็š„ไบ‹ไปถๅพช็Žฏ้˜ปๅกžๆบใ€‚
19
+ - ๅฝ“ๅ‰่ฟ˜ๆฒกๆœ‰่ฟ™ๆฌก e2e ็š„้•ฟๆœŸไฝฟ็”จๆ—ฅๅฟ—๏ผŒๆ— ๆณ•ๅˆคๆ–ญ๏ผš
20
+ - ๅฏๅŠจๅธงๆ˜ฏๅฆ็œŸ็š„ๅ‘ๅ‡บ๏ผ›
21
+ - `content_raw` ๆ˜ฏๅฆๅŒ…ๅซๅ‘ฝไปค๏ผ›
22
+ - timer/้˜Ÿๅˆ—/ไบ‹ไปถๅพช็Žฏๆ˜ฏๅฆ่ขซๅปถ่ฟŸ๏ผ›
23
+ - QQ ๆ˜ฏๅฆๆŠŠ้•ฟ่กŒๅ†…ไปฃ็ ๆŠ˜ๅ ๆˆไบ†ไธ€ไธช็‚นใ€‚
24
+
25
+ ### ๅทฒ็ปๆทปๅŠ ็š„่ฏŠๆ–ญๆ—ฅๅฟ—
26
+
27
+ ็‰ˆๆœฌ 1.3.6 ๅชๅขžๅŠ ไบ† debug ๆ—ฅๅฟ—๏ผŒๆฒกๆœ‰ๆ”นๅ˜่กŒไธบ๏ผš
28
+
29
+ - `scope: "stream"`๏ผŒ`msgKey: "log.streamTiming"`๏ผš
30
+ - `phase: "tool queued"`๏ผšๅทฅๅ…ท่กŒ่ฟ›ๅ…ฅ้˜Ÿๅˆ—๏ผ›
31
+ - `phase: "append start"`๏ผš้˜Ÿๅˆ—ๅผ€ๅง‹ๅค„็†๏ผŒๅŒ…ๅซ `waitedMs`๏ผ›
32
+ - `phase: "append done"`๏ผš้˜Ÿๅˆ—ๅค„็†ๅฎŒๆˆ๏ผŒๅŒ…ๅซ `totalMs`ใ€‚
33
+ - `AppendStream.onDebug`๏ผˆๅŒ scope `"stream"`๏ผ‰๏ผš
34
+ - `append: buffered/immediate/while flushing`๏ผ›
35
+ - `timer: fired after ...`๏ผ›
36
+ - `flush: start/done`ใ€‚
37
+ - `scope: "event-loop"`๏ผŒ`msgKey: "log.eventLoopDelay"`๏ผš
38
+ - ๆฏ 5 ็ง’่ฎฐๅฝ•ไธ€ๆฌก `meanMs`ใ€`maxMs`ใ€`p99Ms`ใ€‚
39
+
40
+ ๆ‰€ๆœ‰ๆ—ฅๅฟ—ๅชๅœจ `logLevel: "debug"` ๆ—ถๅ†™ๅ…ฅ๏ผš
41
+
42
+ ```json
43
+ {
44
+ "logLevel": "debug"
45
+ }
46
+ ```
47
+
48
+ ๆ—ฅๅฟ—่ทฏๅพ„๏ผš
49
+
50
+ ```text
51
+ <cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log
52
+ ```
53
+
54
+ ### ไธ‹ๆฌกๆ—ฅๅฟ—ๅˆ†ๆžๆญฅ้ชค
55
+
56
+ 1. ๆ‰พๅˆฐ e2e ็š„ `toolStart`๏ผš
57
+ - `scope: "tool"`
58
+ - `toolName: "bash"`
59
+ - `args` ๆ˜ฏๅฆๅŒ…ๅซๅฎŒๆ•ด `command`ใ€‚
60
+ 2. ๆ‰พๅˆฐ้šๅŽ `scope: "stream"` ็š„ๆ—ถๅบๆ—ฅๅฟ—๏ผš
61
+ - `tool queued` ๆ—ถ้—ด๏ผ›
62
+ - `append start` ็š„ `waitedMs`๏ผ›
63
+ - `append done` ็š„ `totalMs`๏ผ›
64
+ - `append: buffered` / `timer: fired` / `flush: start` ๆ—ถ้—ดใ€‚
65
+ 3. ๆ‰พๅˆฐๅฏนๅบ”็š„ `POST .../stream_messages`๏ผš
66
+ - ่ฏทๆฑ‚ๆ—ถ้—ด๏ผ›
67
+ - `content_raw` ๆ˜ฏๅฆๅŒ…ๅซๅ‘ฝไปคๅ‚ๆ•ฐใ€‚
68
+ 4. ๅฏน็…ง `scope: "event-loop"` ็š„ `maxMs` / `p99Ms`๏ผš
69
+ - ๅฆ‚ๆžœไบ‹ไปถๅพช็Žฏๅปถ่ฟŸ่พพๅˆฐๆ•ฐ็ง’/ๆ•ฐๅˆ†้’Ÿ๏ผŒ่ฏดๆ˜Ž Node ไธป็บฟ็จ‹่ขซ้ฅฟๆญป๏ผ›
70
+ - ๅฆ‚ๆžœ event-loop ๆญฃๅธธ๏ผŒไฝ† `append start` ็š„ `waitedMs` ๅพˆๅคง๏ผŒ่ฏดๆ˜Žๅ‰้ข็š„ๆต/้˜Ÿๅˆ—้˜ปๅกž๏ผ›
71
+ - ๅฆ‚ๆžœ POST ๅพˆๅฟซไธ” `content_raw` ๆœ‰ๅ‘ฝไปค๏ผŒไฝ† QQ ๅชๆ˜พ็คบไธ€ไธช็‚น๏ผŒ่ฏดๆ˜Žๆ˜ฏ QQ ๆธฒๆŸ“/ๆˆชๆ–ญใ€‚
72
+
73
+ ### ไธ‹ไธ€ๆญฅ
74
+
75
+ - ๆ”ถ้›†้•ฟๆœŸไฝฟ็”จๆ—ฅๅฟ—ๅŽ๏ผŒ็กฎๅฎšๆ˜ฏไบ‹ไปถๅพช็Žฏใ€้˜Ÿๅˆ—ใ€็ฝ‘็ปœ่ฟ˜ๆ˜ฏ QQ ๆธฒๆŸ“้—ฎ้ข˜ใ€‚
76
+ - ๅฆ‚ๆžœๆ˜ฏ้˜Ÿๅˆ—/็ผ“ๅ†ฒ้—ฎ้ข˜๏ผŒ่€ƒ่™‘็ป™ๅทฅๅ…ทๅฏๅŠจ่กŒๅขžๅŠ  `flushAfter`ใ€‚
77
+ - ้•ฟๅ‘ฝไปคๅ‚ๆ•ฐๅทฒๆ”นไธบ bash fenced code block๏ผ›ๅฆ‚ๆžœไปๆ˜พ็คบๅผ‚ๅธธ๏ผŒๆฃ€ๆŸฅ QQ ๅฏนไปฃ็ ๅ—็š„ๆธฒๆŸ“ๅนถ่€ƒ่™‘็ผฉ็Ÿญๅ‘ฝไปคๆ‘˜่ฆใ€‚
78
+ - ๅฆ‚ๆžœๆ˜ฏ e2e CPU ๅฏผ่‡ด็š„ไบ‹ไปถๅพช็Žฏ้ฅฅ้ฅฟ๏ผŒ้œ€่ฆ่ฏ„ไผฐ TUI/tool output ็š„ๅŒๆญฅๅผ€้”€๏ผŒๆˆ–ๆŠŠ relay ็งปๅ‡บไธป่ฟ›็จ‹ใ€‚
79
+ - ไฟฎๅคๅŽ่กฅๆต‹่ฏ•๏ผŒๆ›ดๆ–ฐ `doc/todo.md` / `CHANGELOG.md`๏ผŒๅ†ๅ‘ๅธƒ็‰ˆๆœฌใ€‚
80
+
81
+ ## 2. ้•ฟ่€—ๆ—ถๅ‘ฝไปคๅฏผ่‡ด QQ ไพงๆ— ่พ“ๅ‡บ/ๅคฑๅŽปๅ“ๅบ”
82
+
83
+ > ็Šถๆ€๏ผšTODO๏ผŒ็ญ‰ๅพ…้•ฟๆœŸไฝฟ็”จๆ—ฅๅฟ—
84
+ > ่ฎฐๅฝ•ๆ—ฅๆœŸ๏ผš2026-11-04
85
+ > ็›ธๅ…ณ็‰ˆๆœฌ๏ผšpi-qq 1.4.0
86
+ > ่ฏฆ็ป†่ฐƒ็ ”๏ผš`doc/archive/delivery-v4-zh.md`
87
+
88
+ ### ็Žฐ่ฑก
89
+
90
+ QQ ไพงๆ‰ง่กŒไธ€ไธช่ฟ่กŒๆ•ฐๅˆ†้’Ÿๅˆฐๆ•ฐๅๅˆ†้’Ÿ็š„ `bash` ๅ‘ฝไปคๅŽ๏ผŒๅฏ่ƒฝๅชๆœ‰ๆœ€ๅˆ็š„ๅทฅๅ…ทๆฐ”ๆณก๏ผŒไน‹ๅŽ๏ผš
91
+
92
+ - ๅ‘ฝไปค็ป“ๆŸๆฒกๆœ‰ๅทฅๅ…ท็ป“ๆžœ๏ผ›
93
+ - ๆฒกๆœ‰ๆœ€็ปˆๅ›ž็ญ”๏ผ›
94
+ - ้•ฟๅ‘ฝไปคๆœŸ้—ดๅ‘้€ๆ™ฎ้€šๆถˆๆฏ๏ผŒ็›ดๅˆฐๅ‘ฝไปค็ป“ๆŸๆ‰ๆœ‰ๅๅบ”๏ผ›
95
+ - ็œ‹่ตทๆฅๅƒๆœบๅ™จไบบโ€œๅคฑๅŽปๅ“ๅบ”โ€๏ผŒไฝ†็”ต่„‘ไธŠ็š„ pi TUI ไปๅœจ่ฟ่กŒใ€‚
96
+
97
+ ### ๅทฒ็Ÿฅไบ‹ๅฎž
98
+
99
+ - pi ็š„ bash ๅทฅๅ…ทไฝฟ็”จๅผ‚ๆญฅ `spawn`๏ผŒ้•ฟๆ—ถ้—ด่ฟ่กŒ็š„ๆ™ฎ้€šๅ‘ฝไปคๆœฌ่บซไธไผš้˜ปๅกž Node ไบ‹ไปถๅพช็Žฏใ€‚
100
+ - ้•ฟๅทฅๅ…ทๆœŸ้—ด pi-qq ไธ่ฎข้˜… `tool_execution_update`๏ผŒๆ‰€ไปฅๆฒกๆœ‰่ฟ›ๅบฆๅธง๏ผ›ๅชๆœ‰ 50 ็ง’ไธ€ๆฌก็š„ `typing`ใ€‚
101
+ - ๆฏไธชๆ€่€ƒ/ๅทฅๅ…ทๆฐ”ๆณก้ƒฝไผšๆ–ฐๅปบไธ€ๆกๆตๅผๅ›žๅค๏ผŒไฝ† `ReplyBudget` ๅช็ปŸ่ฎกๆ™ฎ้€šๆ–‡ๆœฌ๏ผŒๆฒกๆœ‰ๆŠŠๆตๅผๆฐ”ๆณก่ฎกๅ…ฅ 4 ๆฌก่ขซๅŠจๅ›žๅคไธŠ้™ใ€‚
102
+ - ้•ฟ้™้ป˜ๅŽ็š„็ป“ๆžœๅธงๅฆ‚ๆžœๅ›  `msg_id` ่ฟ‡ๆœŸๆˆ–่ขซๅŠจๆฌกๆ•ฐ่ถ…้™ๅคฑ่ดฅ๏ผŒ`BubbleWriter` ไผšๆฒฟ็”จๅŒไธ€ไธช่ฟ‡ๆœŸ `msg_id` / `stream_msg_id` resume๏ผŒๆœ€็ปˆ `killStream()`๏ผ›ไน‹ๅŽ `onToolStart()` / `onToolEnd()` ็›ดๆŽฅๆ—ฉ้€€๏ผŒๅŽ็ปญๅทฅๅ…ท่พ“ๅ‡บไธขๅคฑใ€‚
103
+ - ่ฟ่กŒไธญๆ”ถๅˆฐ็š„ๆ™ฎ้€šๆถˆๆฏไปฅ `steer` ่ฟ›ๅ…ฅๅฝ“ๅ‰ run๏ผŒ่ฆ็ญ‰ๅฝ“ๅ‰ๅทฅๅ…ทๆ‰นๆฌก็ป“ๆŸๆ‰ๅค„็†ใ€‚
104
+ - `/stop` ็›ฎๅ‰่ฆ็ญ‰ `waitForEnd()` ๆ‰ๅ›žๅค๏ผ›ๅฆ‚ๆžœๅทฅๅ…ทไธๅ“ๅบ” abort ๆˆ–ๆตๅผ่ฏทๆฑ‚ๅœจ้‡่ฏ•๏ผŒไผšๅ ไฝ SDK ็š„ๅนถๅ‘้”ใ€‚
105
+
106
+ ### ไธดๆ—ถๆขๅคๆ‰‹ๆฎต
107
+
108
+ QQ ไพง็š„ `/last` ๅทฒๅฏ็”จไบŽๆ‰‹ๅŠจๆขๅค๏ผš
109
+
110
+ - **run ่ฟ่กŒไธญ**๏ผš`/last` ่ฐƒ็”จ `RunFeedback.repushCurrent()`๏ผŒๆŠŠๅฝ“ๅ‰่พ“ๅ‡บ้‡ๆŽจๅˆฐๆ–ฐๆถˆๆฏ็š„ `msg_id` ไธŠ๏ผŒๅนถ่ฎฉๅŽ็ปญ delta ็ปง็ปญๅ†™ๅ…ฅๆ–ฐๆฐ”ๆณก๏ผ›
111
+ - **run ๅทฒ็ป“ๆŸ**๏ผš`/last` ็”จๆตๅผ้‡ๆ–ฐๆŽจ้€ๆœ€่ฟ‘ไธ€ๆฌกๅฎŒๆˆ็š„ assistant ๆ–‡ๆœฌใ€‚
112
+
113
+ ๅ› ๆญคๆœฌๆฌกไธๅฎž็Žฐ่กŒไธบไฟฎๅค๏ผ›`/last` ไฝœไธบ็”จๆˆทๅฏ็”จ็š„ๆ‰‹ๅŠจๆขๅค่ทฏๅพ„ใ€‚
114
+
115
+ ### ๆœฌๆฌกๅ†ณ็ญ–
116
+
117
+ - ๆš‚ไธๅฎžๆ–ฝ v4 ๅฝ’ๆกฃไธญ็š„้”™่ฏฏๅˆ†็ฑปใ€้ข„็ฎ—ใ€ๅฟƒ่ทณใ€่‡ชๅŠจ้‡็ป‘็ญ‰่กŒไธบไฟฎๆ”นใ€‚
118
+ - ๅช่ฟฝๅŠ ๅฏ้ชŒ่ฏ็š„ debug ๆ—ฅๅฟ—๏ผŒ็ญ‰ๅพ…้•ฟๆœŸไฝฟ็”จๆ—ฅๅฟ—ๅŽๅ†ๅ†ณๅฎšไฟฎๅค่Œƒๅ›ดใ€‚
119
+ - ๆ–ฐๅขž่ฏŠๆ–ญๆ—ฅๅฟ—ๅœจ `logLevel: "debug"` ๆ—ถๅ†™ๅ…ฅ๏ผ›`stream error` / `stream unavailable` ๅŽŸๆœฌๅฐฑๆ˜ฏ `warn`๏ผŒไฟ็•™ไธบ้”™่ฏฏ่ฎฐๅฝ•๏ผŒไธๆ”นๅ˜่กŒไธบใ€‚
120
+
121
+ ### ๅทฒ่ฟฝๅŠ ็š„่ฏŠๆ–ญๆ—ฅๅฟ—
122
+
123
+ - `scope: "tool"`๏ผš
124
+ - `start` / `end` ๅขžๅŠ  `parentToolCallId`ใ€`durationMs`๏ผ›
125
+ - `end` ๅขžๅŠ  `updateCount`ใ€`updateSpanMs`ใ€`firstPartial`๏ผ›
126
+ - `update` ่ฎฐๅฝ•้ฆ–ๆฌก้ƒจๅˆ†่พ“ๅ‡บ๏ผŒไน‹ๅŽๆฏ 50 ๆฌก่ฎฐๅฝ•ไธ€ๆฌก่ฟ›ๅบฆใ€‚
127
+ - `scope: "run"`๏ผš
128
+ - `run started`๏ผš`phase: "no qq target"`ใ€`source: pending/recovery`ใ€`targetAgeMs`๏ผ›
129
+ - `tool start` / `tool end`๏ผš`phase: queued / output started / result recorded / output finished / ignored: stream dead / ignored: no active run`๏ผ›
130
+ - `stream takeover decision`๏ผš`textLength`ใ€`deltaLength`ใ€`hadPendingTools`ใ€`flushOk`ใ€`hasPushed`ใ€`streamTookOver`ใ€`deferredTextLen`๏ผ›
131
+ - `run ended`๏ผš`hasPendingTools`ใ€`hasPushed`ใ€`lastCompletedTextLength`๏ผ›
132
+ - `stream timing`๏ผš`phase: last repush / last repush ignored`๏ผ›
133
+ - ๅทฅๅ…ทใ€ๆœ€็ปˆๆถˆๆฏๅ’Œ cleanup ็›ธๅ…ณๆ—ฅๅฟ—้ƒฝๅธฆ `diagnostics`๏ผš`activeTool`ใ€`tools`ใ€`deferredTextLen`ใ€`writer stats`ใ€`budget`ใ€‚
134
+ - `scope: "stream"`๏ผš
135
+ - `stream timing` ็š„ `stats` ๅŒ…ๅซ `dead`ใ€`hasStream`ใ€`hasPendingResume`ใ€`resumeAttempts`ใ€`bubbleChars`ใ€`hasPushed`ใ€`lastKind`ใ€`toolOpen`ใ€`lastFrameAgeMs`๏ผ›
136
+ - `stream open` ่ฎฐๅฝ•ๆ˜ฏๅฆ resumeใ€checkpoint ็š„ `index` / `hasStreamMsgId` / `bufferLength`ใ€`budget`๏ผ›
137
+ - `stream error` ๅขžๅŠ  `phase: "frame failed"`๏ผŒ่ฎฐๅฝ• `httpStatus`ใ€`bizCode`ใ€`path`ใ€`msgSeq`ใ€`streamMsgId`ใ€`bufferLength`๏ผ›
138
+ - `stream unavailable` ่ฎฐๅฝ• writer `stats` ๅ’Œ `budget`๏ผ›
139
+ - `sendStreamReply()`๏ผˆidle `/last`๏ผ‰่ฎฐๅฝ• `phase: "last reply stream"`ใ€`msgId`ใ€`textLength`ใ€‚
140
+ - `AppendStream` ็š„ `flush start/done` ๅขžๅŠ  `msgSeq` ๅ’Œ `streamMsgId`๏ผˆๅญ˜ๅœจๆ—ถ๏ผ‰ใ€‚
141
+ - `scope: "inbound"`๏ผš`/stop` ่ฎฐๅฝ• `wait start` / `wait done` ๅŠ `waitedMs`ใ€‚
142
+ - `scope: "event-loop"`๏ผšๅขžๅŠ  `driftMs`๏ผŒ็”จไบŽๅ‘็Žฐ timer ่ขซไบ‹ไปถๅพช็Žฏๅปถ่ฟŸใ€‚
143
+
144
+ ### ไธ‹ๆฌกๆ—ฅๅฟ—ๅˆ†ๆžๆญฅ้ชค
145
+
146
+ 1. ๆ‰พๅˆฐ้•ฟๅ‘ฝไปค็š„ `scope: "tool"` `start` / `end`๏ผš
147
+ - `durationMs` ๆ˜ฏๅฆไธŽ็”จๆˆทๆ„Ÿ็Ÿฅไธ€่‡ด๏ผ›
148
+ - `updateCount` ๆ˜ฏๅฆไธบ 0๏ผŒๅˆคๆ–ญ้•ฟๅ‘ฝไปคๆœŸ้—ดๆ˜ฏๅฆๅฎŒๅ…จๆฒกๆœ‰้ƒจๅˆ†่พ“ๅ‡บใ€‚
149
+ 2. ็œ‹ `scope: "stream"` ็š„ `stream timing`๏ผš
150
+ - `tool queued` ๆ—ถ้—ด๏ผ›
151
+ - `append start` ็š„ `waitedMs` / `lastFrameAgeMs`๏ผ›
152
+ - `append done` ็š„ `totalMs`๏ผ›
153
+ - ๅทฅๅ…ท็ป“ๆŸๅˆฐ็ป“ๆžœๅธงไน‹้—ด้š”ไบ†ๅคšไน…ใ€‚
154
+ 3. ็œ‹ `stream error` ็š„ `phase: "frame failed"`๏ผš
155
+ - `httpStatus` / `bizCode` ๆ˜ฏๅฆไธบ `304103`ใ€`40034005`ใ€`40034128`ใ€`40034024`ใ€`429`ใ€`50002`๏ผ›
156
+ - `streamMsgId` ๆ˜ฏๅฆๅญ˜ๅœจ๏ผŒ`bufferLength` ๆ˜ฏๅฆๆŒ็ปญๅขž้•ฟใ€‚
157
+ 4. ็œ‹ `stream unavailable` ไน‹ๅŽ `scope: "run"` ็š„๏ผš
158
+ - `tool start/end` ๆ˜ฏๅฆๅ‡บ็Žฐ `ignored: stream dead`๏ผ›
159
+ - `stream takeover decision` ็š„ `flushOk` / `hasPushed` / `hadPendingTools`๏ผ›
160
+ - `run ended` ็š„ `hasPendingTools`ใ€‚
161
+ 5. ๅฆ‚ๆžœ็”จๆˆทไฝฟ็”จไบ† `/last`๏ผŒ็œ‹ `phase: "last repush"` ๆˆ– `"last repush ignored"`๏ผŒ็กฎ่ฎคๆ˜ฏๅฆๆขๅคใ€ๆขๅคๅŽๆ˜ฏๅฆไปๆœ‰่พ“ๅ‡บใ€‚
162
+ 6. ็œ‹ `scope: "event-loop"` ็š„ `maxMs` / `p99Ms` / `driftMs`๏ผš
163
+ - ๅฆ‚ๆžœ่พพๅˆฐๆ•ฐ็ง’็”š่‡ณๆ›ด้ซ˜๏ผŒไผ˜ๅ…ˆๆŽ’ๆŸฅ CPU ้ฅฅ้ฅฟ๏ผ›
164
+ - ๅฆ‚ๆžœๆญฃๅธธ๏ผŒๅˆ™ๆ›ดๅฏ่ƒฝๆ˜ฏ QQ ่ขซๅŠจ็ช—ๅฃ/้…้ขๆˆ– relay ็Šถๆ€้—ฎ้ข˜ใ€‚
165
+ 7. ๅฏน็…ง `/qq-test-budget` ็š„ `remaining`ใ€`streamBubbles`ใ€`streamBubblesTotal`๏ผŒ็กฎ่ฎคๆ˜ฏๅฆๆ—ฉๅทฒ่ถ…่ฟ‡ 4 ๆฌกใ€‚
166
+
167
+ ### ้œ€่ฆๆไพ›็š„ไฟกๆฏ
168
+
169
+ ๅค็ŽฐๅŽ่ฏทๆไพ›๏ผš
170
+
171
+ - `<cwd>/.pi/pi-qq/logs/pi-qq-YYYY-MM-DD.log`๏ผ›
172
+ - ้•ฟๅ‘ฝไปคๅ†…ๅฎนใ€ๅผ€ๅง‹ๆ—ถ้—ดๅ’Œ็ป“ๆŸๆ—ถ้—ด๏ผ›
173
+ - QQ ไธŠๆœ€ๅŽไธ€ๆกๅฏ่งๆถˆๆฏๅŠๆ—ถ้—ด๏ผ›
174
+ - ๆœŸ้—ดๆ˜ฏๅฆๅ‘้€่ฟ‡ `/status`ใ€`/stop`ใ€ๆ™ฎ้€šๆ–‡ๆœฌใ€`/last`๏ผ›
175
+ - `/qq-test-budget` ๅ’Œ `/qq-status` ่พ“ๅ‡บ๏ผ›
176
+ - ๆ‰‹ๆœบ QQ ๆ˜ฏๅฆๅœจๅ‰ๅฐ๏ผŒๆ˜ฏๅฆๅ…่ฎธไธปๅŠจๆถˆๆฏใ€‚
177
+
178
+ ### ไธ‹ไธ€ๆญฅ
179
+
180
+ - ๅฆ‚ๆžœๆ—ฅๅฟ—็กฎ่ฎค `msg_id` ่ฟ‡ๆœŸ/่ขซๅŠจๆฌกๆ•ฐ่ถ…้™๏ผšๆŒ‰ `doc/archive/delivery-v4-zh.md` ็š„โ€œ้•ฟ่€—ๆ—ถๅทฅๅ…ทโ€ๅปบ่ฎฎๅฎž็Žฐ้”™่ฏฏๅˆ†็ฑปๅ’Œ้ข„็ฎ—ไฟฎๅคใ€‚
181
+ - ๅฆ‚ๆžœๆ—ฅๅฟ—ๆ˜พ็คบๆตไธ€็›ดๅญ˜ๆดปไฝ†ๆฒกๆœ‰ๆ–ฐๅธง๏ผšไผ˜ๅ…ˆๅฎž็Žฐ้•ฟๅทฅๅ…ทๅฟƒ่ทณๅ’Œ `tool_execution_update` ่ฝฌๅ‘ใ€‚
182
+ - ๅฆ‚ๆžœไบ‹ไปถๅพช็Žฏ `driftMs` / `p99Ms` ๅพˆ้ซ˜๏ผšๆŽ’ๆŸฅ e2e CPUใ€TUI ๆธฒๆŸ“ๅ’ŒๅŒๆญฅๅทฅๅ…ทไปฃ็ ใ€‚
183
+ - ๅฆ‚ๆžœ `/stop` ็š„ `wait done` ็ผบๅคฑๆˆ– `waitedMs` ๅพˆๅคง๏ผšๅฎž็Žฐ้ž้˜ปๅกž `/stop`ใ€‚
184
+ - ๅฆ‚ๆžœ `/last` ๅทฒ่ƒฝ็จณๅฎšๆขๅค๏ผŒไฟๆŒไธบๆ‰‹ๅŠจๅ…œๅบ•๏ผŒไธๆ€ฅไบŽ่‡ชๅŠจ้‡็ป‘ใ€‚
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
  import { join } from "node:path";
3
+ import { monitorEventLoopDelay } from "node:perf_hooks";
3
4
  import { getSettings, readConfig, type Credentials, type ResolvedSettings } from "./src/infra/settings";
4
5
  import { startBot, type BotState, type RunningBot } from "./src/qq/qq-bot";
5
6
  import { runQrLogin, type QrLoginResult } from "./src/qq/qr-login";
@@ -45,6 +46,54 @@ const runFeedback = new RunFeedback({
45
46
  onWarn: (msg) => sessionCtx?.ui.notify(`pi-qq ${msg}`, "warning"),
46
47
  });
47
48
 
49
+ const eventLoopDelay = monitorEventLoopDelay({ resolution: 100 });
50
+ let eventLoopTimer: ReturnType<typeof setInterval> | undefined;
51
+
52
+ interface ToolUpdateStats {
53
+ count: number;
54
+ firstAt: number;
55
+ lastAt: number;
56
+ firstPartial: string;
57
+ }
58
+
59
+ const toolUpdateStats = new Map<string, ToolUpdateStats>();
60
+ const TOOL_UPDATE_LOG_EVERY = 50;
61
+
62
+ function clearToolUpdateStats(): void {
63
+ toolUpdateStats.clear();
64
+ }
65
+
66
+ function roundMs(value: number): number {
67
+ return Math.round((value / 1e6) * 10) / 10;
68
+ }
69
+
70
+ function startEventLoopMonitor(): void {
71
+ if (!isLogEnabled("debug") || eventLoopTimer) return;
72
+ eventLoopDelay.disable();
73
+ eventLoopDelay.reset();
74
+ eventLoopDelay.enable();
75
+ let lastTickAt = Date.now();
76
+ eventLoopTimer = setInterval(() => {
77
+ const now = Date.now();
78
+ const driftMs = now - lastTickAt - 5_000;
79
+ lastTickAt = now;
80
+ log.debug("event-loop", "event loop delay", {
81
+ meanMs: roundMs(eventLoopDelay.mean),
82
+ maxMs: roundMs(eventLoopDelay.max),
83
+ p99Ms: roundMs(eventLoopDelay.percentile(99)),
84
+ driftMs,
85
+ });
86
+ eventLoopDelay.reset();
87
+ }, 5_000);
88
+ eventLoopTimer.unref?.();
89
+ }
90
+
91
+ function stopEventLoopMonitor(): void {
92
+ if (eventLoopTimer) clearInterval(eventLoopTimer);
93
+ eventLoopTimer = undefined;
94
+ eventLoopDelay.disable();
95
+ }
96
+
48
97
  export default function (pi: ExtensionAPI) {
49
98
  activeSettings = getSettings(readConfig().config);
50
99
  configureI18n(activeSettings.language);
@@ -91,12 +140,15 @@ export default function (pi: ExtensionAPI) {
91
140
  stateDetail = undefined;
92
141
  sessionCtx = undefined;
93
142
  runFeedback.dispose();
143
+ clearToolUpdateStats();
144
+ stopEventLoopMonitor();
94
145
  };
95
146
 
96
147
  const startWithCredentials = async (ctx: ExtensionContext, creds: Credentials, allowedOpenid: string) => {
97
148
  const settings = activeSettings;
98
149
  configureLogger(settings.logLevel, join(ctx.cwd, ".pi", "pi-qq", "logs"));
99
150
  stopIfRunning();
151
+ startEventLoopMonitor();
100
152
  stateDetail = undefined;
101
153
  sessionCtx = ctx;
102
154
  const uploadDir = sessionUploadDir(ctx.cwd, ctx.sessionManager.getSessionId());
@@ -141,6 +193,7 @@ export default function (pi: ExtensionAPI) {
141
193
  pi.on("session_start", async (_event, ctx) => {
142
194
  const { settings, error } = applyConfig(ctx);
143
195
  configureLogger(settings.logLevel, join(ctx.cwd, ".pi", "pi-qq", "logs"));
196
+ startEventLoopMonitor();
144
197
 
145
198
  const login = getLoginState();
146
199
  log.info("session", "started", {
@@ -175,22 +228,66 @@ export default function (pi: ExtensionAPI) {
175
228
  });
176
229
 
177
230
  pi.on("tool_execution_start", (event) => {
231
+ toolUpdateStats.delete(event.toolCallId);
178
232
  if (isLogEnabled("debug")) {
179
233
  log.debug("tool", "start", {
180
234
  toolName: event.toolName,
181
235
  toolCallId: event.toolCallId,
236
+ parentToolCallId: event.parentToolCallId ?? null,
237
+ at: Date.now(),
182
238
  args: previewForLog(event.args),
183
239
  });
184
240
  }
185
241
  void runFeedback.onToolStart(event.toolName, event.args, event.toolCallId);
186
242
  });
187
243
 
244
+ pi.on("tool_execution_update", (event) => {
245
+ if (!isLogEnabled("debug")) return;
246
+ const now = Date.now();
247
+ const existing = toolUpdateStats.get(event.toolCallId);
248
+ if (!existing) {
249
+ const first: ToolUpdateStats = {
250
+ count: 1,
251
+ firstAt: now,
252
+ lastAt: now,
253
+ firstPartial: previewForLog(event.partialResult, 300),
254
+ };
255
+ toolUpdateStats.set(event.toolCallId, first);
256
+ log.debug("tool", "update", {
257
+ phase: "first",
258
+ toolName: event.toolName,
259
+ toolCallId: event.toolCallId,
260
+ count: 1,
261
+ partial: first.firstPartial,
262
+ });
263
+ return;
264
+ }
265
+ existing.count += 1;
266
+ existing.lastAt = now;
267
+ if (existing.count % TOOL_UPDATE_LOG_EVERY === 0) {
268
+ log.debug("tool", "update", {
269
+ phase: "progress",
270
+ toolName: event.toolName,
271
+ toolCallId: event.toolCallId,
272
+ count: existing.count,
273
+ spanMs: existing.lastAt - existing.firstAt,
274
+ });
275
+ }
276
+ });
277
+
188
278
  pi.on("tool_execution_end", (event) => {
279
+ const updates = toolUpdateStats.get(event.toolCallId);
280
+ toolUpdateStats.delete(event.toolCallId);
189
281
  if (isLogEnabled("debug")) {
190
282
  log.debug("tool", "end", {
191
283
  toolName: event.toolName,
192
284
  toolCallId: event.toolCallId,
285
+ parentToolCallId: event.parentToolCallId ?? null,
193
286
  isError: event.isError,
287
+ durationMs: event.durationMs ?? null,
288
+ updateCount: updates?.count ?? 0,
289
+ updateSpanMs: updates ? updates.lastAt - updates.firstAt : null,
290
+ firstPartial: updates?.firstPartial ?? null,
194
291
  result: previewForLog(event.result),
195
292
  });
196
293
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chen_hu_hu/pi-qq",
3
- "version": "1.3.5",
3
+ "version": "1.4.0",
4
4
  "description": "Connect pi to QQ private chats.",
5
5
  "license": "MIT",
6
6
  "author": "CQ-Developer",