pi-wechat-session-hub 0.1.2 → 0.1.4
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 +57 -1
- package/README.md +6 -2
- package/docs/implementation-spec.md +3 -1
- package/package.json +72 -72
- package/scripts/release-preflight.mjs +79 -27
- package/src/gateway/command-router.ts +87 -45
- package/src/gateway/turn-health.ts +213 -6
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,60 @@
|
|
|
6
6
|
|
|
7
7
|
暂无.
|
|
8
8
|
|
|
9
|
+
## [0.1.4] - 2026-11-12
|
|
10
|
+
|
|
11
|
+
本版重做 `/watch` 会话健康总览: 默认视图改为「当前打开着的会话」, 并新增「最后提问」列.
|
|
12
|
+
|
|
13
|
+
### 改进
|
|
14
|
+
|
|
15
|
+
- **`/watch` 默认视图改为按「是否还开在后台」筛选**
|
|
16
|
+
- 原规则按会话状态筛选(非空闲), 结果是 256 个会话里有 250 个长期空闲的历史会话被藏起来, 而真正开在后台但刚跑完的会话(worker 状态 idle)反而看不见
|
|
17
|
+
- 改为按 registry 中是否还有 worker 实例判断: 有 worker = 当前可操作, 直接展示, 不论 running 还是 idle; 异常与断连会话始终展示
|
|
18
|
+
- 汇总行改为「打开 N 个(共 M)」, 不再用总数误导; 页尾补充「共 M 个会话, 发 `/watch --all` 查看全部」
|
|
19
|
+
- 实测本机: 默认视图从 2 行变为 7 行, 正好对应当前开着的 7 个会话
|
|
20
|
+
- **新增「最后提问」列, 取代会话标题**
|
|
21
|
+
- 取 transcript 中最后一条 user 消息, 即你自己最后一句话, 比会话标题更能帮助回忆起该会话在干什么
|
|
22
|
+
- 超长内容按 Unicode code point 截断到 22 字并加省略号, 避免手机端横向滚动
|
|
23
|
+
- 仅对会被展示的行读取, 不为几百个历史会话做无意义 IO
|
|
24
|
+
- 读取采用与 `readLatestFailure` 相同的动态扩窗策略(64KB → 2MB): 长会话中最后一条用户消息会被大量 assistant 输出挤出初始窗口, 固定窗口会误报「暂无提问」
|
|
25
|
+
- **活动时间改为相对描述**: `2026-11-12 14:54` 改为 `14m` / `2h` / `1d`, 手机上无需心算
|
|
26
|
+
- 移除表格中的 PID 列(手机端无用且撑宽), 需要诊断时用 `/wx status`
|
|
27
|
+
|
|
28
|
+
### 测试
|
|
29
|
+
|
|
30
|
+
- 更新 `/watch` 用例: 断言默认视图按 worker 实例筛选、展示最后提问而非会话标题、`--all` 不再输出分页提示
|
|
31
|
+
- 全量测试 134 条, 131 通过 / 0 失败 / 3 跳过
|
|
32
|
+
|
|
33
|
+
### 升级提示
|
|
34
|
+
|
|
35
|
+
- 升级后需重启 Pi TUI 并执行 `/wx restart`, 否则 daemon 仍跑旧代码
|
|
36
|
+
|
|
37
|
+
## [0.1.3] - 2026-11-11
|
|
38
|
+
|
|
39
|
+
本版修复 `/watch` 会话健康总览的误报: 用户主动中断与历史遗留失败被长期当作「需要处理」的异常.
|
|
40
|
+
|
|
41
|
+
### 修复
|
|
42
|
+
|
|
43
|
+
- **`/watch` 把用户主动中断当成错误**
|
|
44
|
+
- transcript 中 `stopReason: aborted` / `errorMessage: Operation aborted` 表示用户自己按了中断, 属于正常操作痕迹, 但原逻辑只看到 `errorMessage` 非空就判为失败, 结果在总览里永久显示错误并进入「需要处理」列表
|
|
45
|
+
- 新增 `interrupted` 状态: 主动中断显示为「已中断」, 默认与空闲会话一起收起, `--all` 仍可查看
|
|
46
|
+
- 实测样本: 本机 248 个会话中有 3 个属于此类误报
|
|
47
|
+
- **历史遗留失败被当作当前故障**
|
|
48
|
+
- 健康状态由 transcript 最后一条 assistant 记录推导, 而 transcript 不会再变化, 因此一个多月前的一次失败会每天出现在「需要处理」里
|
|
49
|
+
- 失败信息现在带上 transcript 条目的 `timestamp`(`detectedAt`), 超过 24 小时的失败视为历史记录, 不再报异常; 「需要处理」列表同时显示失败发生时间
|
|
50
|
+
- 缺少可信时间戳时按未过期处理, 避免因解析不到时间而漏报真实故障
|
|
51
|
+
- **上游合规拦截被归为「未知错误」**
|
|
52
|
+
- 新增 `policy_blocked` 分类, 识别 `Request blocked` / `are not allowed` / `violates` / `account suspension` 等文本, 显示为「请求被上游合规策略拦截」, 便于判断应该换模型或换账号, 而不是当成偶发故障重试
|
|
53
|
+
|
|
54
|
+
### 测试
|
|
55
|
+
|
|
56
|
+
- 新增主动中断不算故障、时间窗口生效与边界、缺少时间戳不漏报、合规拦截分类、`readLatestFailure` 带时间戳等用例
|
|
57
|
+
- 全量测试 134 条, 131 通过 / 0 失败 / 3 跳过
|
|
58
|
+
|
|
59
|
+
### 升级提示
|
|
60
|
+
|
|
61
|
+
- 升级后需重启 Pi TUI, 让新的健康判定生效
|
|
62
|
+
|
|
9
63
|
## [0.1.2] - 2026-11-11
|
|
10
64
|
|
|
11
65
|
本版核心是**修复 npm 安装场景下后台服务无法启动**, 并新增 Windows 登录后自动启动、会话健康总览, 以及一批微信端展示修复.
|
|
@@ -89,7 +143,9 @@
|
|
|
89
143
|
- 授权根目录白名单与 fail-closed 命令处理
|
|
90
144
|
- AGPL-3.0 许可证与安装文档
|
|
91
145
|
|
|
92
|
-
[未发布]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.
|
|
146
|
+
[未发布]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.4...HEAD
|
|
147
|
+
[0.1.4]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.3...v0.1.4
|
|
148
|
+
[0.1.3]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.2...v0.1.3
|
|
93
149
|
[0.1.2]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.1...v0.1.2
|
|
94
150
|
[0.1.1]: https://github.com/yangfeng20/pi-wechat-session-hub/compare/v0.1.0...v0.1.1
|
|
95
151
|
[0.1.0]: https://github.com/yangfeng20/pi-wechat-session-hub/releases/tag/v0.1.0
|
package/README.md
CHANGED
|
@@ -204,14 +204,18 @@ Windows 自动启动默认关闭; 开启后在当前用户登录 Windows 时使
|
|
|
204
204
|
| `/last` | **否** | 只读回顾目标会话最近一轮对话 |
|
|
205
205
|
| `/stop` | 是 | 停止目标会话的 Pi worker |
|
|
206
206
|
| `/retry` | 是 | 重新执行指定消息, 可能产生副作用 |
|
|
207
|
-
| `/watch` | 否 |
|
|
207
|
+
| `/watch` | 否 | 默认总览「打开着」的会话; `--all` 查看全部历史会话 |
|
|
208
208
|
| `/status` | 否 | 展示 Hub、当前会话与 UI 模式可用性 |
|
|
209
209
|
| `/projects` | 否 | 列出已登记项目 |
|
|
210
210
|
| `/help` | 否 | 查看命令帮助 |
|
|
211
211
|
|
|
212
212
|
`/new` 默认在后台创建会话, 也可显式使用 `/new --mode ui` 在新 Windows Terminal tab 中创建可见 UI 会话. `/sessions` 默认列出 Hub 管理的全部活跃前台和后台会话, 一次回复展示全部匹配会话, 以 **markdown 表格**展示索引、标题、模式与状态, 当前微信路由在标题后标记为 `(当前)`, 表格下方汇总涉及的项目. 标题优先显示 session name; 未命名时使用 Pi `/resume` 同样采用的首条用户文本, 最长 32 个 Unicode 字符, 超出以 `...` 省略. `/resume 3` 把微信 route 切换到索引为 `3` 的会话. 如果切换目标的 agent 已处于 idle, 回复会附带最近一次回复预览; 超过 3800 个 Unicode 字符时只显示结尾并提示完整内容仍在会话中. 如果 agent 正在运行, 只确认切换, 不发送旧回复. 索引在会话排序变化后保持不变, 不因列表重排而复用.
|
|
213
213
|
|
|
214
|
-
`/watch`
|
|
214
|
+
`/watch` 默认只展示**打开着**的会话(registry 中仍有 worker 实例)以及异常或断连会话; `/watch --all` 展开全部历史会话. 表格列包含索引、**最后提问**、项目、状态与相对活动时间, 「最后提问」直接取 transcript 中最后一条 user 消息, 比会话标题更能帮助回忆起该会话在做什么.
|
|
215
|
+
|
|
216
|
+
之所以按「是否还有 worker 实例」而非「会话状态」筛选, 是因为大量历史会话长期处于空闲状态, 若按状态展示会让总览被上百个早已关闭的会话淹没. 失败类型直接从 Pi transcript 的 `stopReason` 与 `errorMessage` 读取, 可识别周/5 小时/月额度、TPM 限流、网络错误、鉴权错误与上游合规拦截; 如果上游错误提供重置时间, 会原样显示.
|
|
217
|
+
|
|
218
|
+
只有真正需要用户处理的失败才算异常: 用户主动中断(`aborted`)归为「已中断」, 默认与空闲会话一起收起; 超过 24 小时的失败视为历史记录, 不再占据异常列表. 这样早已结束的会话不会永久显示为错误. 当前 worker 的 `running` 状态可直接读取, 但 Hub 没有独立的 turn 定时心跳, 因此不臆测“卡住”状态. `/status` 额外输出 `可见 UI 模式`; 平台不支持或缺少 `wt.exe` 时显示 `不可用` 及具体原因.
|
|
215
219
|
|
|
216
220
|
## 在微信端找回上下文
|
|
217
221
|
|
|
@@ -95,7 +95,9 @@ control endpoint 为 Windows named pipe 或 POSIX Unix socket. 客户端使用 `
|
|
|
95
95
|
|
|
96
96
|
`/sessions` 默认状态为 `active`, active 集合由当前 UI 和 background owner 共同构成. 需要查看已保存/待启动 session 时可使用 `--state all` 或相应过滤项. 一次回复列出全部匹配会话, 每个会话展示稳定索引、标题、项目、模式和状态; 当前微信 route 在会话项和列表摘要中标记. 标题优先使用 session name, 未命名时读取 Pi `/resume` 同样采用的首条 user 文本, 并在 32 个 Unicode 字符后以 `...` 省略. 微信命令帮助按会话、项目和 Hub 状态分组; `/status` 使用中文状态和会话摘要, 不显示 PID. 空闲会话的 `/resume` 回复附带最近 assistant 文本预览, 最多 800 个 Unicode 字符, 超长时明确提示剩余内容仍保留在 transcript.
|
|
97
97
|
|
|
98
|
-
`/watch`
|
|
98
|
+
`/watch` 默认只显示「打开着」的会话(registry 中仍有 worker 实例)以及错误、断连的会话; `--all` 展开全部历史会话. 表格列包含索引、最后提问、项目、状态与相对活动时间, 「最后提问」取 transcript 中最后一条 user 消息. 健康状态读取 worker registry, 错误分类读取每个 Pi transcript 最后一条 assistant message 的 `stopReason` 和 `errorMessage`, 不调用模型也不更改 session. 当前 worker 心跳不是定时刷新, 因此不据此推断 turn 卡住.
|
|
99
|
+
|
|
100
|
+
失败只在“确实需要用户处理”时算异常: 用户主动中断(`aborted`)归为「已中断」并从默认视图收起; 失败时间取自 transcript 条目的 `timestamp`, 超过 24 小时视为历史记录, 不再报异常. 缺少可信时间戳时按未过期处理, 避免漏报真实故障. `/last` 遇到最近一轮 assistant 错误时显示错误摘要 (例如额度耗尽及上游提供的重置时间), 不再显示“尚未产生回复”。
|
|
99
101
|
|
|
100
102
|
`SessionIndexRecord.publicIndex` 在首次写入时分配, 旧 registry 首次读取时按最近活动时间排序迁移. 索引持久保存, 不随列表排序变化; app 不主动删除旧 session 索引, 新会话递增分配. `session-index.json` 的 UUID 仍仅供内部关联使用.
|
|
101
103
|
|
package/package.json
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "pi-wechat-session-hub",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "通过微信管理本机多个 Pi 会话的 Pi 扩展。",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"pi-package",
|
|
7
|
-
"pi",
|
|
8
|
-
"wechat",
|
|
9
|
-
"weixin",
|
|
10
|
-
"ilink",
|
|
11
|
-
"session-manager",
|
|
12
|
-
"remote-control"
|
|
13
|
-
],
|
|
14
|
-
"license": "AGPL-3.0-or-later",
|
|
15
|
-
"author": "yangfeng20",
|
|
16
|
-
"repository": {
|
|
17
|
-
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/yangfeng20/pi-wechat-session-hub.git"
|
|
19
|
-
},
|
|
20
|
-
"bugs": {
|
|
21
|
-
"url": "https://github.com/yangfeng20/pi-wechat-session-hub/issues"
|
|
22
|
-
},
|
|
23
|
-
"homepage": "https://github.com/yangfeng20/pi-wechat-session-hub#readme",
|
|
24
|
-
"type": "module",
|
|
25
|
-
"engines": {
|
|
26
|
-
"node": ">=22.19.0"
|
|
27
|
-
},
|
|
28
|
-
"files": [
|
|
29
|
-
"extensions/",
|
|
30
|
-
"src/",
|
|
31
|
-
"scripts/",
|
|
32
|
-
"docs/",
|
|
33
|
-
"README.md",
|
|
34
|
-
"CHANGELOG.md",
|
|
35
|
-
"LICENSE",
|
|
36
|
-
"tsconfig.json"
|
|
37
|
-
],
|
|
38
|
-
"scripts": {
|
|
39
|
-
"typecheck": "tsc --noEmit",
|
|
40
|
-
"test": "node --experimental-transform-types --test --test-timeout=30000 --test-force-exit tests/config-store.test.ts tests/project-service.test.ts tests/project-command-parser.test.ts tests/registry.test.ts tests/pi-rpc-client.test.ts tests/background-worker.test.ts tests/background-worker-resume.test.ts tests/pi-cli.test.ts tests/durable-message-queue.test.ts tests/gateway-command-parser.test.ts tests/gateway-command-router.test.ts tests/durable-message-queue-context.test.ts tests/turn-health.test.ts tests/wechat-ilink.test.ts tests/credential-store.test.ts tests/ilink-qr-login.test.ts tests/gateway-runtime.test.ts tests/wechat-manager-command-parser.test.ts tests/ui-worker-manager.test.ts tests/windows-terminal-launcher.test.ts tests/gateway-service-controller.test.ts tests/windows-logon-autostart-manager.test.ts tests/ui-session-owner-handoff.test.ts tests/ui-resume-owner-liveness.test.ts tests/wechat-session-hub-command.test.ts",
|
|
41
|
-
"test:pi-runtime": "node scripts/pi-rpc-smoke.mjs",
|
|
42
|
-
"test:wt-capability": "node --experimental-transform-types scripts/probe-wt-capability.mjs",
|
|
43
|
-
"release:check": "node scripts/release-preflight.mjs",
|
|
44
|
-
"release:verify": "node scripts/release-preflight.mjs --verify"
|
|
45
|
-
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"@earendil-works/pi-coding-agent": "*"
|
|
48
|
-
},
|
|
49
|
-
"devDependencies": {
|
|
50
|
-
"@earendil-works/pi-coding-agent": "0.85.1",
|
|
51
|
-
"@types/node": "^22.0.0",
|
|
52
|
-
"@types/qrcode-terminal": "0.12.2",
|
|
53
|
-
"typescript": "^5.9.3"
|
|
54
|
-
},
|
|
55
|
-
"pi": {
|
|
56
|
-
"extensions": [
|
|
57
|
-
"./extensions/*.ts"
|
|
58
|
-
]
|
|
59
|
-
},
|
|
60
|
-
"piCompatibility": {
|
|
61
|
-
"testedVersions": [
|
|
62
|
-
"0.85.1",
|
|
63
|
-
"1.0.0",
|
|
64
|
-
"1.3.0"
|
|
65
|
-
],
|
|
66
|
-
"supportedRange": ">=0.85.1 <2.0.0"
|
|
67
|
-
},
|
|
68
|
-
"dependencies": {
|
|
69
|
-
"jiti": "^2.7.0",
|
|
70
|
-
"qrcode-terminal": "0.12.0"
|
|
71
|
-
}
|
|
72
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-wechat-session-hub",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "通过微信管理本机多个 Pi 会话的 Pi 扩展。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi",
|
|
8
|
+
"wechat",
|
|
9
|
+
"weixin",
|
|
10
|
+
"ilink",
|
|
11
|
+
"session-manager",
|
|
12
|
+
"remote-control"
|
|
13
|
+
],
|
|
14
|
+
"license": "AGPL-3.0-or-later",
|
|
15
|
+
"author": "yangfeng20",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/yangfeng20/pi-wechat-session-hub.git"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/yangfeng20/pi-wechat-session-hub/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/yangfeng20/pi-wechat-session-hub#readme",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22.19.0"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"extensions/",
|
|
30
|
+
"src/",
|
|
31
|
+
"scripts/",
|
|
32
|
+
"docs/",
|
|
33
|
+
"README.md",
|
|
34
|
+
"CHANGELOG.md",
|
|
35
|
+
"LICENSE",
|
|
36
|
+
"tsconfig.json"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "node --experimental-transform-types --test --test-timeout=30000 --test-force-exit tests/config-store.test.ts tests/project-service.test.ts tests/project-command-parser.test.ts tests/registry.test.ts tests/pi-rpc-client.test.ts tests/background-worker.test.ts tests/background-worker-resume.test.ts tests/pi-cli.test.ts tests/durable-message-queue.test.ts tests/gateway-command-parser.test.ts tests/gateway-command-router.test.ts tests/durable-message-queue-context.test.ts tests/turn-health.test.ts tests/wechat-ilink.test.ts tests/credential-store.test.ts tests/ilink-qr-login.test.ts tests/gateway-runtime.test.ts tests/wechat-manager-command-parser.test.ts tests/ui-worker-manager.test.ts tests/windows-terminal-launcher.test.ts tests/gateway-service-controller.test.ts tests/windows-logon-autostart-manager.test.ts tests/ui-session-owner-handoff.test.ts tests/ui-resume-owner-liveness.test.ts tests/wechat-session-hub-command.test.ts",
|
|
41
|
+
"test:pi-runtime": "node scripts/pi-rpc-smoke.mjs",
|
|
42
|
+
"test:wt-capability": "node --experimental-transform-types scripts/probe-wt-capability.mjs",
|
|
43
|
+
"release:check": "node scripts/release-preflight.mjs",
|
|
44
|
+
"release:verify": "node scripts/release-preflight.mjs --verify"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": "0.85.1",
|
|
51
|
+
"@types/node": "^22.0.0",
|
|
52
|
+
"@types/qrcode-terminal": "0.12.2",
|
|
53
|
+
"typescript": "^5.9.3"
|
|
54
|
+
},
|
|
55
|
+
"pi": {
|
|
56
|
+
"extensions": [
|
|
57
|
+
"./extensions/*.ts"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
"piCompatibility": {
|
|
61
|
+
"testedVersions": [
|
|
62
|
+
"0.85.1",
|
|
63
|
+
"1.0.0",
|
|
64
|
+
"1.3.0"
|
|
65
|
+
],
|
|
66
|
+
"supportedRange": ">=0.85.1 <2.0.0"
|
|
67
|
+
},
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"jiti": "^2.7.0",
|
|
70
|
+
"qrcode-terminal": "0.12.0"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -150,17 +150,25 @@ async function runVerify() {
|
|
|
150
150
|
|
|
151
151
|
const published = packument.versions?.[version];
|
|
152
152
|
if (!published) {
|
|
153
|
-
|
|
153
|
+
const rawVersions = Object.keys(packument.versions ?? {});
|
|
154
|
+
if (rawVersions.includes("0.0.0-stage") || packument["dist-tags"]?.latest !== version) {
|
|
155
|
+
fail(`registry 上暂时看不到 ${version}: npm 可能正在做发布后安全扫描(网页显示 Validating), 稍等几分钟后重试`);
|
|
156
|
+
} else {
|
|
157
|
+
fail(`registry 上找不到 ${version}, 发布可能尚未生效`);
|
|
158
|
+
}
|
|
154
159
|
report();
|
|
155
160
|
return;
|
|
156
161
|
}
|
|
157
162
|
|
|
163
|
+
// 版本级 readme 在正常发布的包里也常为空, 真正影响搜索索引的是顶层 readme,
|
|
164
|
+
// 因此只在顶层也为空时判失败, 避免误报。
|
|
158
165
|
const packumentReadme = typeof packument.readme === "string" ? packument.readme : "";
|
|
159
166
|
const versionReadme = typeof published.readme === "string" ? published.readme : "";
|
|
160
|
-
if (packumentReadme.trim().length === 0
|
|
161
|
-
fail("
|
|
167
|
+
if (packumentReadme.trim().length === 0) {
|
|
168
|
+
fail("顶层 readme 为空, 该包可能无法进入 npm search 索引(0.1.0 的同款问题)");
|
|
162
169
|
} else {
|
|
163
|
-
|
|
170
|
+
const versionNote = versionReadme.trim().length === 0 ? "(版本级为空属正常)" : "";
|
|
171
|
+
note(`顶层 readme ${packumentReadme.length} 字符, 版本级 readme ${versionReadme.length} 字符${versionNote}`);
|
|
164
172
|
}
|
|
165
173
|
|
|
166
174
|
const latest = packument["dist-tags"]?.latest;
|
|
@@ -170,37 +178,81 @@ async function runVerify() {
|
|
|
170
178
|
note(`dist-tags.latest = ${version}`);
|
|
171
179
|
}
|
|
172
180
|
|
|
173
|
-
|
|
174
|
-
note(
|
|
181
|
+
const searchHit = await checkNpmSearch(name).catch((error) => {
|
|
182
|
+
note(`npm search 检查跳过: ${error instanceof Error ? error.message : String(error)}`);
|
|
183
|
+
return undefined;
|
|
184
|
+
});
|
|
185
|
+
if (searchHit === true) {
|
|
186
|
+
note("npm search 已能检索到该包");
|
|
187
|
+
} else if (searchHit === false) {
|
|
188
|
+
note("npm search 暂未收录, 索引有延迟, 通常几分钟到数小时");
|
|
175
189
|
}
|
|
176
190
|
|
|
177
|
-
const
|
|
191
|
+
const galleryHit = await checkGallery(name).catch((error) => {
|
|
192
|
+
note(`pi.dev/packages 检查跳过: ${error instanceof Error ? error.message : String(error)}`);
|
|
193
|
+
return undefined;
|
|
194
|
+
});
|
|
195
|
+
if (galleryHit === true) {
|
|
196
|
+
note("pi.dev/packages 已能搜到该包");
|
|
197
|
+
} else if (galleryHit === false) {
|
|
198
|
+
note("pi.dev/packages 暂未收录, 站点有自己的索引缓存, 需要等待刷新");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
report();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 查询 npm search 是否已收录该包。
|
|
206
|
+
*
|
|
207
|
+
* @param {string} name 包名。
|
|
208
|
+
* @returns {Promise<boolean>} 已收录返回 true, 未收录返回 false。
|
|
209
|
+
*/
|
|
210
|
+
async function checkNpmSearch(name) {
|
|
211
|
+
const response = await fetchWithTimeout(
|
|
178
212
|
`${REGISTRY}/-/v1/search?text=${encodeURIComponent(`${name} keywords:pi-package`)}&size=20`,
|
|
179
|
-
{ headers: { "user-agent": "pi-wechat-session-hub-release-check" } },
|
|
180
213
|
);
|
|
181
|
-
if (
|
|
182
|
-
|
|
183
|
-
const hit = result.objects?.some((item) => item.package?.name === name);
|
|
184
|
-
if (hit) {
|
|
185
|
-
note("npm search 已能检索到该包");
|
|
186
|
-
} else {
|
|
187
|
-
note("npm search 暂未收录, 索引有延迟, 通常几分钟到数小时");
|
|
188
|
-
}
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
throw new Error(`search 接口返回 ${response.status}`);
|
|
189
216
|
}
|
|
217
|
+
const result = await response.json();
|
|
218
|
+
return Boolean(result.objects?.some((item) => item.package?.name === name));
|
|
219
|
+
}
|
|
190
220
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
221
|
+
/**
|
|
222
|
+
* 查询 pi.dev 包画廊是否已收录该包。
|
|
223
|
+
*
|
|
224
|
+
* @param {string} name 包名。
|
|
225
|
+
* @returns {Promise<boolean>} 已收录返回 true, 未收录返回 false。
|
|
226
|
+
*/
|
|
227
|
+
async function checkGallery(name) {
|
|
228
|
+
const response = await fetchWithTimeout(`https://pi.dev/packages?name=${encodeURIComponent(name)}`);
|
|
229
|
+
if (!response.ok) {
|
|
230
|
+
throw new Error(`pi.dev 返回 ${response.status}`);
|
|
201
231
|
}
|
|
232
|
+
const html = await response.text();
|
|
233
|
+
return html.includes(name);
|
|
234
|
+
}
|
|
202
235
|
|
|
203
|
-
|
|
236
|
+
/**
|
|
237
|
+
* 带超时的 fetch。
|
|
238
|
+
*
|
|
239
|
+
* 外部站点(尤其是经 Cloudflare 的 pi.dev)在网络受限时可能长时间不响应,
|
|
240
|
+
* 必须显式限时, 否则会拖住整个校验流程。
|
|
241
|
+
*
|
|
242
|
+
* @param {string} url 请求地址。
|
|
243
|
+
* @returns {Promise<Response>} 响应对象。
|
|
244
|
+
*/
|
|
245
|
+
async function fetchWithTimeout(url) {
|
|
246
|
+
const controller = new AbortController();
|
|
247
|
+
const timer = setTimeout(() => controller.abort(), 15000);
|
|
248
|
+
try {
|
|
249
|
+
return await fetch(url, {
|
|
250
|
+
headers: { "user-agent": "pi-wechat-session-hub-release-check" },
|
|
251
|
+
signal: controller.signal,
|
|
252
|
+
});
|
|
253
|
+
} finally {
|
|
254
|
+
clearTimeout(timer);
|
|
255
|
+
}
|
|
204
256
|
}
|
|
205
257
|
|
|
206
258
|
/**
|
|
@@ -23,6 +23,8 @@ import { UiWorkerManagerError } from "../runtime/ui-worker-manager.ts";
|
|
|
23
23
|
import {
|
|
24
24
|
evaluateHealth,
|
|
25
25
|
formatHealthState,
|
|
26
|
+
formatRelativeAge,
|
|
27
|
+
readLastUserQuestion,
|
|
26
28
|
readLatestFailure,
|
|
27
29
|
classifyFailure,
|
|
28
30
|
type SessionHealth,
|
|
@@ -641,10 +643,12 @@ export class GatewayCommandRouter {
|
|
|
641
643
|
/**
|
|
642
644
|
* 展示所有受管会话的运行与错误总览。
|
|
643
645
|
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
+
* 默认只显示「打开着」的会话(有 worker 实例)与异常会话; 纯历史会话需要
|
|
647
|
+
* `--all` 才展开。这样总览才对应「当前能操作的会话」, 而不会被上百个
|
|
648
|
+
* 早已关闭的历史会话淹没。
|
|
649
|
+
* 失败原因与最后提问都直接从 transcript 读取, 不触发模型请求。
|
|
646
650
|
*
|
|
647
|
-
* @param all
|
|
651
|
+
* @param all 是否包含全部历史会话。
|
|
648
652
|
* @param config 当前配置快照。
|
|
649
653
|
* @returns markdown 总览表格。
|
|
650
654
|
*/
|
|
@@ -664,68 +668,81 @@ export class GatewayCommandRouter {
|
|
|
664
668
|
return "尚无已登记会话。";
|
|
665
669
|
}
|
|
666
670
|
|
|
667
|
-
const
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
state
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
671
|
+
const nowMs = Date.now();
|
|
672
|
+
const healthRows: Array<SessionHealth & { title: string; projectName: string; question?: string }> =
|
|
673
|
+
await Promise.all(
|
|
674
|
+
sessions.map(async (session) => {
|
|
675
|
+
const worker = workersBySession.get(session.sessionId);
|
|
676
|
+
const failure = await readLatestFailure(session.sessionFile);
|
|
677
|
+
const { state } = evaluateHealth(session, worker, failure, nowMs);
|
|
678
|
+
const title = await resolveSessionTitle(session);
|
|
679
|
+
const projectName = Object.hasOwn(config.projects, session.projectAlias)
|
|
680
|
+
? config.projects[session.projectAlias].displayName
|
|
681
|
+
: session.projectAlias;
|
|
682
|
+
// 只有会被展示的行才读最后提问, 避免为几百个历史会话做无意义 IO。
|
|
683
|
+
const shown = all || worker !== undefined || state === "error" ||
|
|
684
|
+
state === "disconnected" || state === "starting" || state === "stopping";
|
|
685
|
+
const question = shown ? await readLastUserQuestion(session.sessionFile) : undefined;
|
|
686
|
+
return {
|
|
687
|
+
session,
|
|
688
|
+
...(worker ? { worker } : {}),
|
|
689
|
+
state,
|
|
690
|
+
...(failure ? { failure } : {}),
|
|
691
|
+
title,
|
|
692
|
+
projectName,
|
|
693
|
+
...(question ? { question } : {}),
|
|
694
|
+
};
|
|
695
|
+
}),
|
|
696
|
+
);
|
|
686
697
|
|
|
687
|
-
|
|
688
|
-
const
|
|
689
|
-
const
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
const
|
|
698
|
+
// 打开着 = registry 中还有 worker 实例; 这是区分「当前可操作会话」与历史记录的唯一可靠依据。
|
|
699
|
+
const isOpen = (row: SessionHealth): boolean => row.worker !== undefined;
|
|
700
|
+
const isAbnormal = (row: SessionHealth): boolean =>
|
|
701
|
+
row.state === "error" || row.state === "disconnected" || row.state === "stopping";
|
|
702
|
+
|
|
703
|
+
const visibleRows = all ? healthRows : healthRows.filter((row) => isOpen(row) || isAbnormal(row));
|
|
704
|
+
const openRows = visibleRows.filter(isOpen);
|
|
705
|
+
const runningCount = openRows.filter((row) => row.state === "running").length;
|
|
706
|
+
const startingCount = openRows.filter((row) => row.state === "starting").length;
|
|
707
|
+
const stoppingCount = openRows.filter((row) => row.state === "stopping").length;
|
|
708
|
+
const idleCount = openRows.filter((row) => row.state === "idle") .length;
|
|
709
|
+
const errorCount = visibleRows.filter((row) => row.state === "error" || row.state === "disconnected").length;
|
|
710
|
+
|
|
711
|
+
const scope = all
|
|
712
|
+
? `${healthRows.length} 个会话`
|
|
713
|
+
: `打开 ${openRows.length} 个(共 ${healthRows.length})`;
|
|
693
714
|
const summary = [
|
|
694
|
-
`**总览** · ${
|
|
695
|
-
`${runningCount}
|
|
715
|
+
`**总览** · ${scope}`,
|
|
716
|
+
...(runningCount ? [`${runningCount} 运行中`] : []),
|
|
696
717
|
...(startingCount ? [`${startingCount} 启动中`] : []),
|
|
697
|
-
`${
|
|
698
|
-
`${idleCount}
|
|
718
|
+
...(stoppingCount ? [`${stoppingCount} 停止中`] : []),
|
|
719
|
+
...(idleCount ? [`${idleCount} 空闲`] : []),
|
|
720
|
+
...(errorCount ? [`🔴 ${errorCount} 异常`] : []),
|
|
699
721
|
].join(" · ");
|
|
700
722
|
|
|
701
|
-
const visibleRows = all
|
|
702
|
-
? healthRows
|
|
703
|
-
: healthRows.filter((row) => row.state !== "idle");
|
|
704
723
|
if (visibleRows.length === 0) {
|
|
705
724
|
return [
|
|
706
725
|
summary,
|
|
707
|
-
"
|
|
726
|
+
"当前没有打开着或异常的会话。",
|
|
708
727
|
"发送 `/watch --all` 查看全部会话。",
|
|
709
728
|
].join("\n\n");
|
|
710
729
|
}
|
|
711
730
|
|
|
712
731
|
const tableRows = visibleRows.map((row) => {
|
|
713
732
|
const index = String(row.session.publicIndex ?? "?");
|
|
714
|
-
const
|
|
733
|
+
const question = escapeMarkdownTableCell(truncateMiddle(row.question ?? "(暂无提问)", 22));
|
|
715
734
|
const project = escapeMarkdownTableCell(row.projectName);
|
|
716
735
|
const state = formatHealthState(row.state);
|
|
717
|
-
const
|
|
718
|
-
return `| ${index} | ${
|
|
736
|
+
const activity = formatRelativeAge(row.session.lastActivityAt, nowMs);
|
|
737
|
+
return `| ${index} | ${question} | ${project} | ${state} | ${activity} |`;
|
|
719
738
|
});
|
|
720
739
|
const table = [
|
|
721
|
-
"| 索引 |
|
|
740
|
+
"| 索引 | 最后提问 | 项目 | 状态 | 活动 |",
|
|
722
741
|
"| --- | --- | --- | --- | --- |",
|
|
723
742
|
...tableRows,
|
|
724
743
|
].join("\n");
|
|
725
744
|
|
|
726
|
-
const attentionRows = visibleRows.filter(
|
|
727
|
-
row.state === "error" || row.state === "disconnected" || row.state === "stopping",
|
|
728
|
-
);
|
|
745
|
+
const attentionRows = visibleRows.filter(isAbnormal);
|
|
729
746
|
const attention = attentionRows.length === 0
|
|
730
747
|
? []
|
|
731
748
|
: [
|
|
@@ -737,7 +754,10 @@ export class GatewayCommandRouter {
|
|
|
737
754
|
const recovery = !row.worker || row.worker.state === "stale"
|
|
738
755
|
? `; 可发送 \`/resume ${index}\` 重连`
|
|
739
756
|
: "";
|
|
740
|
-
|
|
757
|
+
const when = row.failure.detectedAt
|
|
758
|
+
? `(${formatLocalMinute(row.failure.detectedAt)})`
|
|
759
|
+
: "";
|
|
760
|
+
return `- **${index} ${title}**:${formatFailureSummary(row.failure)}${when}${recovery}`;
|
|
741
761
|
}
|
|
742
762
|
if (row.state === "disconnected") {
|
|
743
763
|
const failureNote = row.failure ? `; ${formatFailureSummary(row.failure)}` : "";
|
|
@@ -747,7 +767,11 @@ export class GatewayCommandRouter {
|
|
|
747
767
|
}),
|
|
748
768
|
];
|
|
749
769
|
|
|
750
|
-
|
|
770
|
+
const footnotes = all
|
|
771
|
+
? []
|
|
772
|
+
: [`共 ${healthRows.length} 个会话,发 \`/watch --all\` 查看全部。`];
|
|
773
|
+
|
|
774
|
+
return [summary, table, ...attention, ...footnotes].join("\n\n");
|
|
751
775
|
}
|
|
752
776
|
|
|
753
777
|
/**
|
|
@@ -1417,6 +1441,24 @@ function truncateTail(text: string, limit: number): { text: string; truncated: b
|
|
|
1417
1441
|
};
|
|
1418
1442
|
}
|
|
1419
1443
|
|
|
1444
|
+
/**
|
|
1445
|
+
* 按 Unicode code point 截断文本并在末尾加省略号。
|
|
1446
|
+
*
|
|
1447
|
+
* 用于总览表格中的「最后提问」列: 手机端表格宽度有限, 过长内容会把表格
|
|
1448
|
+
* 撑到需要横向滚动。用 code point 切割避免拆坏 emoji。
|
|
1449
|
+
*
|
|
1450
|
+
* @param text 原始文本。
|
|
1451
|
+
* @param limit 最大 code point 数, 超出时末尾追加省略号。
|
|
1452
|
+
* @returns 可安全放进表格单元格的短文本。
|
|
1453
|
+
*/
|
|
1454
|
+
function truncateMiddle(text: string, limit: number): string {
|
|
1455
|
+
const characters = Array.from(text);
|
|
1456
|
+
if (characters.length <= limit) {
|
|
1457
|
+
return text;
|
|
1458
|
+
}
|
|
1459
|
+
return `${characters.slice(0, limit).join("")}…`;
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1420
1462
|
/**
|
|
1421
1463
|
* 从 Pi transcript 尾部按需扩展窗口, 读取最近一轮 user 与 assistant 文本。
|
|
1422
1464
|
*
|
|
@@ -4,7 +4,14 @@ import type { WorkerInstanceRecord, SessionIndexRecord } from "../registry/types
|
|
|
4
4
|
/**
|
|
5
5
|
* 会话健康状态, 用于 /watch 总览。
|
|
6
6
|
*/
|
|
7
|
-
export type HealthState =
|
|
7
|
+
export type HealthState =
|
|
8
|
+
| "starting"
|
|
9
|
+
| "running"
|
|
10
|
+
| "stopping"
|
|
11
|
+
| "error"
|
|
12
|
+
| "disconnected"
|
|
13
|
+
| "interrupted"
|
|
14
|
+
| "idle";
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
17
|
* 上游失败的归一化分类。
|
|
@@ -16,6 +23,7 @@ export type FailureKind =
|
|
|
16
23
|
| "rate_limit"
|
|
17
24
|
| "usage_limit"
|
|
18
25
|
| "auth"
|
|
26
|
+
| "policy_blocked"
|
|
19
27
|
| "service_unavailable"
|
|
20
28
|
| "network"
|
|
21
29
|
| "aborted"
|
|
@@ -32,6 +40,13 @@ export interface TurnFailure {
|
|
|
32
40
|
resetAt?: string;
|
|
33
41
|
/** 归一化前的原始错误前缀, 用于排查。 */
|
|
34
42
|
raw: string;
|
|
43
|
+
/**
|
|
44
|
+
* 该失败发生的时刻, 取自 transcript 记录的 timestamp。
|
|
45
|
+
*
|
|
46
|
+
* 用于区分“当前故障”与“历史记录”: transcript 不会再变化, 若不看时间,
|
|
47
|
+
* 十几天前的一次失败会永久显示为异常。
|
|
48
|
+
*/
|
|
49
|
+
detectedAt?: string;
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
/**
|
|
@@ -53,21 +68,61 @@ const TAIL_READ_MAX_BYTES = 2 * 1024 * 1024;
|
|
|
53
68
|
/** 提取到的原始错误在快照中保留的最大字符数。 */
|
|
54
69
|
const MAX_RAW_ERROR_CHARS = 200;
|
|
55
70
|
|
|
71
|
+
/**
|
|
72
|
+
* 失败被视为“当前故障”的最大年龄, 超过后只当作历史记录。
|
|
73
|
+
*
|
|
74
|
+
* 取 24 小时: 足以覆盖一次夜间失败到次日晚间的排查窗口, 又不会让
|
|
75
|
+
* 早已结束的会话永久占据 /watch 的异常列表。
|
|
76
|
+
*/
|
|
77
|
+
export const FAILURE_ACTIVE_WINDOW_MS = 24 * 60 * 60 * 1000;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 判断一条失败记录是否已经超出“当前故障”的时间窗口。
|
|
81
|
+
*
|
|
82
|
+
* 缺少可信时间戳时一律视为未过期, 避免因为解析不到 timestamp 而漏报真实故障。
|
|
83
|
+
*
|
|
84
|
+
* @param failure 失败信息。
|
|
85
|
+
* @param nowMs 当前时间毫秒数。
|
|
86
|
+
* @param windowMs 判定窗口, 默认 {@link FAILURE_ACTIVE_WINDOW_MS}。
|
|
87
|
+
* @returns 已超出窗口返回 true。
|
|
88
|
+
*/
|
|
89
|
+
export function isFailureExpired(
|
|
90
|
+
failure: TurnFailure,
|
|
91
|
+
nowMs: number,
|
|
92
|
+
windowMs: number = FAILURE_ACTIVE_WINDOW_MS,
|
|
93
|
+
): boolean {
|
|
94
|
+
if (!failure.detectedAt) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const detectedMs = Date.parse(failure.detectedAt);
|
|
98
|
+
if (!Number.isFinite(detectedMs)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return nowMs - detectedMs > windowMs;
|
|
102
|
+
}
|
|
103
|
+
|
|
56
104
|
/**
|
|
57
105
|
* 计算会话健康状态。
|
|
58
106
|
*
|
|
59
107
|
* 只使用 registry 中明确记录的 worker state, 不从静态 heartbeatAt 推断“卡住”:
|
|
60
108
|
* heartbeatAt 只在状态变化时更新, 并非定时心跳, 用它判卡住会误报长时间运行的 turn。
|
|
61
109
|
*
|
|
110
|
+
* 失败信息只在“确实需要用户处理”时才算异常:
|
|
111
|
+
*
|
|
112
|
+
* - `aborted` 表示用户主动中断, 不是故障, 归为已中断
|
|
113
|
+
* - 超出 {@link FAILURE_ACTIVE_WINDOW_MS} 的失败属于历史记录, 不再报异常
|
|
114
|
+
*
|
|
62
115
|
* @param session 会话索引记录。
|
|
63
116
|
* @param worker 该会话当前的 worker 实例, 可为空。
|
|
64
117
|
* @param failure 最近一次 turn 的失败信息, 可为空。
|
|
118
|
+
* @param nowMs 当前时间毫秒数, 用于判定失败是否过期。
|
|
65
119
|
* @returns 当前健康状态。
|
|
66
120
|
*/
|
|
67
121
|
export function evaluateHealth(
|
|
68
122
|
session: SessionIndexRecord,
|
|
69
123
|
worker: WorkerInstanceRecord | undefined,
|
|
70
124
|
failure: TurnFailure | undefined,
|
|
125
|
+
nowMs: number = Date.now(),
|
|
71
126
|
): { state: HealthState } {
|
|
72
127
|
if (worker?.state === "stale") {
|
|
73
128
|
return { state: "disconnected" };
|
|
@@ -81,7 +136,10 @@ export function evaluateHealth(
|
|
|
81
136
|
if (worker?.state === "running") {
|
|
82
137
|
return { state: "running" };
|
|
83
138
|
}
|
|
84
|
-
if (failure) {
|
|
139
|
+
if (failure?.kind === "aborted") {
|
|
140
|
+
return { state: "interrupted" };
|
|
141
|
+
}
|
|
142
|
+
if (failure && !isFailureExpired(failure, nowMs)) {
|
|
85
143
|
return { state: "error" };
|
|
86
144
|
}
|
|
87
145
|
if (!worker && session.state === "active") {
|
|
@@ -129,6 +187,16 @@ export function classifyFailure(errorMessage: string): TurnFailure {
|
|
|
129
187
|
if (lowered.includes("not supported for this image model") || lowered.includes("complexity")) {
|
|
130
188
|
return { kind: "unknown", summary: "模型不支持该请求参数", raw };
|
|
131
189
|
}
|
|
190
|
+
// 上游把第三方客户端或账号合规拦截归在这里, 与网络/额度无关, 换模型或换账号才能解决。
|
|
191
|
+
if (
|
|
192
|
+
lowered.includes("request blocked") ||
|
|
193
|
+
lowered.includes("not allowed") ||
|
|
194
|
+
lowered.includes("violates") ||
|
|
195
|
+
lowered.includes("unsupported_country") ||
|
|
196
|
+
lowered.includes("account suspension")
|
|
197
|
+
) {
|
|
198
|
+
return { kind: "policy_blocked", summary: "请求被上游合规策略拦截", raw };
|
|
199
|
+
}
|
|
132
200
|
if (lowered.includes("auth_unavailable") || lowered.includes("unauthorized") || lowered.includes("401")) {
|
|
133
201
|
return { kind: "auth", summary: "上游鉴权不可用", raw };
|
|
134
202
|
}
|
|
@@ -175,8 +243,7 @@ export function extractResetTime(errorMessage: string): string | undefined {
|
|
|
175
243
|
*
|
|
176
244
|
* @param sessionFile Pi session 文件路径。
|
|
177
245
|
* @returns 失败信息; 最后一条 assistant 正常结束或无法读取时返回 undefined。
|
|
178
|
-
*/
|
|
179
|
-
export async function readLatestFailure(sessionFile: string): Promise<TurnFailure | undefined> {
|
|
246
|
+
*/export async function readLatestFailure(sessionFile: string): Promise<TurnFailure | undefined> {
|
|
180
247
|
const handle = await open(sessionFile, "r").catch(() => undefined);
|
|
181
248
|
if (!handle) {
|
|
182
249
|
return undefined;
|
|
@@ -235,17 +302,155 @@ function scanFailureLines(buffer: string): TurnFailure | undefined {
|
|
|
235
302
|
}
|
|
236
303
|
const stopReason = typeof message.stopReason === "string" ? message.stopReason : undefined;
|
|
237
304
|
const errorMessage = typeof message.errorMessage === "string" ? message.errorMessage.trim() : "";
|
|
305
|
+
const timestamp = typeof entry.timestamp === "string" ? entry.timestamp : undefined;
|
|
238
306
|
if (errorMessage) {
|
|
239
|
-
return classifyFailure(errorMessage);
|
|
307
|
+
return withTimestamp(classifyFailure(errorMessage), timestamp);
|
|
240
308
|
}
|
|
241
309
|
if (stopReason === "error") {
|
|
242
|
-
return
|
|
310
|
+
return withTimestamp(
|
|
311
|
+
{ kind: "unknown", summary: "上游返回错误但未提供详情", raw: "stopReason=error" },
|
|
312
|
+
timestamp,
|
|
313
|
+
);
|
|
243
314
|
}
|
|
244
315
|
return undefined;
|
|
245
316
|
}
|
|
246
317
|
return undefined;
|
|
247
318
|
}
|
|
248
319
|
|
|
320
|
+
/**
|
|
321
|
+
* 把 transcript 记录的 timestamp 附到失败信息上。
|
|
322
|
+
*
|
|
323
|
+
* @param failure 已分类的失败信息。
|
|
324
|
+
* @param timestamp transcript 条目上的时间戳。
|
|
325
|
+
* @returns 带 detectedAt 的新对象; 无可用时间戳时原样返回。
|
|
326
|
+
*/
|
|
327
|
+
function withTimestamp(failure: TurnFailure, timestamp: string | undefined): TurnFailure {
|
|
328
|
+
return timestamp ? { ...failure, detectedAt: timestamp } : failure;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 从 session transcript 尾部读取最后一条 user 消息的文本。
|
|
333
|
+
*
|
|
334
|
+
* 用于 `/watch` 总览的「最后提问」列: 相比会话标题, 用户自己最后一句话
|
|
335
|
+
* 更能帮助回忆起这个会话在干什么。只读文件尾部有限窗口, 不扫全文。
|
|
336
|
+
*
|
|
337
|
+
* @param sessionFile Pi session 文件路径。
|
|
338
|
+
* @returns 清理过空白的提问文本; 无提问或无法读取时返回 undefined。
|
|
339
|
+
*/
|
|
340
|
+
export async function readLastUserQuestion(sessionFile: string): Promise<string | undefined> {
|
|
341
|
+
let handle: Awaited<ReturnType<typeof open>> | undefined;
|
|
342
|
+
try {
|
|
343
|
+
handle = await open(sessionFile, "r");
|
|
344
|
+
const { size } = await handle.stat();
|
|
345
|
+
if (size === 0) {
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
// 一条 turn 可能包含大量 assistant 工具输出, 把最后一条 user 消息挤到
|
|
349
|
+
// 初始窗口之外, 因此和 readLatestFailure 一样逐次扩大窗口直到找到为止。
|
|
350
|
+
let windowBytes = Math.min(size, TAIL_READ_BYTES);
|
|
351
|
+
for (;;) {
|
|
352
|
+
const start = size - windowBytes;
|
|
353
|
+
const chunk = Buffer.alloc(windowBytes);
|
|
354
|
+
await handle.read(chunk, 0, windowBytes, start);
|
|
355
|
+
const text = chunk.toString("utf8");
|
|
356
|
+
const buffer = start > 0 ? text.slice(text.indexOf("\n") + 1) : text;
|
|
357
|
+
const question = scanLastUserQuestion(buffer);
|
|
358
|
+
if (question !== undefined) {
|
|
359
|
+
return question;
|
|
360
|
+
}
|
|
361
|
+
if (start === 0 || windowBytes >= TAIL_READ_MAX_BYTES) {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
windowBytes = Math.min(size, windowBytes * 2);
|
|
365
|
+
}
|
|
366
|
+
} catch {
|
|
367
|
+
return undefined;
|
|
368
|
+
} finally {
|
|
369
|
+
await handle?.close();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 在一段 transcript 文本中从后向前定位最后一条 user 消息的文本。
|
|
375
|
+
*
|
|
376
|
+
* @param buffer 已剔除尾部半行的 transcript 文本片段。
|
|
377
|
+
* @returns 提问文本; 未找到时返回 undefined。
|
|
378
|
+
*/
|
|
379
|
+
function scanLastUserQuestion(buffer: string): string | undefined {
|
|
380
|
+
const lines = buffer.split("\n");
|
|
381
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
382
|
+
const line = lines[index];
|
|
383
|
+
if (!line.trim()) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
let entry: unknown;
|
|
387
|
+
try {
|
|
388
|
+
entry = JSON.parse(line) as unknown;
|
|
389
|
+
} catch {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (!isRecord(entry) || entry.type !== "message" || !isRecord(entry.message)) {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (entry.message.role !== "user") {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const question = extractMessageText(entry.message.content);
|
|
399
|
+
if (question) {
|
|
400
|
+
return question;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 从 message.content 中提取纯文本。
|
|
408
|
+
*
|
|
409
|
+
* content 两种形态都要兼容: 纯字符串, 或带 type 的内容块数组。
|
|
410
|
+
*
|
|
411
|
+
* @param content transcript 中记录的 message.content。
|
|
412
|
+
* @returns 合并空白后的文本; 无文本时返回 undefined。
|
|
413
|
+
*/
|
|
414
|
+
function extractMessageText(content: unknown): string | undefined {
|
|
415
|
+
let text = "";
|
|
416
|
+
if (typeof content === "string") {
|
|
417
|
+
text = content;
|
|
418
|
+
} else if (Array.isArray(content)) {
|
|
419
|
+
text = content
|
|
420
|
+
.filter((block): block is Record<string, unknown> => isRecord(block) && block.type === "text")
|
|
421
|
+
.map((block) => (typeof block.text === "string" ? block.text : ""))
|
|
422
|
+
.join(" ");
|
|
423
|
+
}
|
|
424
|
+
const normalized = text.replace(/\s+/g, " ").trim();
|
|
425
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 将时间格式化为相对描述, 用于总览表格。
|
|
430
|
+
*
|
|
431
|
+
* @param isoTimestamp ISO 时间戳。
|
|
432
|
+
* @param nowMs 当前毫秒数。
|
|
433
|
+
* @returns 形如 `刚刚` / `5m` / `3h` / `2d` 的短文本。
|
|
434
|
+
*/
|
|
435
|
+
export function formatRelativeAge(isoTimestamp: string, nowMs: number = Date.now()): string {
|
|
436
|
+
const parsed = Date.parse(isoTimestamp);
|
|
437
|
+
if (!Number.isFinite(parsed)) {
|
|
438
|
+
return isoTimestamp;
|
|
439
|
+
}
|
|
440
|
+
const minutes = Math.floor((nowMs - parsed) / 60000);
|
|
441
|
+
if (minutes < 1) {
|
|
442
|
+
return "刚刚";
|
|
443
|
+
}
|
|
444
|
+
if (minutes < 60) {
|
|
445
|
+
return `${minutes}m`;
|
|
446
|
+
}
|
|
447
|
+
const hours = Math.floor(minutes / 60);
|
|
448
|
+
if (hours < 24) {
|
|
449
|
+
return `${hours}h`;
|
|
450
|
+
}
|
|
451
|
+
return `${Math.floor(hours / 24)}d`;
|
|
452
|
+
}
|
|
453
|
+
|
|
249
454
|
/**
|
|
250
455
|
* 将心跳年龄格式化为简短的中文描述。
|
|
251
456
|
*
|
|
@@ -289,6 +494,8 @@ export function formatHealthState(state: HealthState): string {
|
|
|
289
494
|
return "🔴 错误";
|
|
290
495
|
case "disconnected":
|
|
291
496
|
return "⚫ 断连";
|
|
497
|
+
case "interrupted":
|
|
498
|
+
return "⚪ 已中断";
|
|
292
499
|
case "idle":
|
|
293
500
|
return "⚪ 空闲";
|
|
294
501
|
}
|