@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.
- package/CHANGELOG.md +20 -1
- package/README-zh.md +1 -1
- package/README.md +1 -1
- package/doc/archive/README.md +14 -0
- package/doc/archive/delivery-v4-zh.md +327 -0
- package/doc/archive/delivery-v4.md +327 -0
- package/doc/todo.md +184 -0
- package/index.ts +97 -0
- package/package.json +1 -1
- package/src/infra/i18n.ts +6 -0
- package/src/qq/inbound-command.ts +7 -0
- package/src/qq/qq-diagnostics.ts +2 -2
- package/src/qq/reply-sender.ts +5 -0
- package/src/relay/bubble-writer.ts +141 -12
- package/src/relay/run-stream-relay.ts +166 -12
- package/src/relay/stream-block-format.ts +139 -22
- package/src/relay/stream-session.ts +38 -5
- package/doc/bugfix-v4.md +0 -698
- package/doc/enhance-status.md +0 -427
- package/doc/send-skill.md +0 -257
|
@@ -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
|
}
|