chatccc 0.2.251 → 0.2.253
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/cccagent.mjs +12 -3
- package/bin/chatccc.mjs +15 -6
- package/deepccc-agent/package.json +1 -1
- package/dist/deepccc-agent/src/cli.js +634 -0
- package/dist/deepccc-agent/src/config.js +76 -0
- package/dist/deepccc-agent/src/context.js +348 -0
- package/dist/deepccc-agent/src/file-log.js +34 -0
- package/dist/deepccc-agent/src/file-tools.js +1208 -0
- package/dist/deepccc-agent/src/index.js +571 -0
- package/dist/deepccc-agent/src/permissions.js +170 -0
- package/dist/deepccc-agent/src/privacy.js +124 -0
- package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
- package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
- package/dist/deepccc-agent/src/progress/reducer.js +102 -0
- package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
- package/dist/deepccc-agent/src/progress/view.js +30 -0
- package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
- package/dist/deepccc-agent/src/session-search.js +276 -0
- package/dist/deepccc-agent/src/session-select.js +23 -0
- package/dist/deepccc-agent/src/sigint.js +26 -0
- package/dist/deepccc-agent/src/skills.js +178 -0
- package/dist/deepccc-agent/src/web-tools.js +246 -0
- package/dist/src/adapters/adapter-interface.js +19 -0
- package/dist/src/adapters/ccc-adapter.js +112 -0
- package/dist/src/adapters/claude-adapter.js +497 -0
- package/dist/src/adapters/claude-session-meta-store.js +92 -0
- package/dist/src/adapters/codex-adapter.js +279 -0
- package/dist/src/adapters/codex-session-meta-store.js +94 -0
- package/dist/src/adapters/cursor-adapter.js +491 -0
- package/dist/src/adapters/cursor-session-meta-store.js +116 -0
- package/dist/src/adapters/jsonl-stream.js +104 -0
- package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
- package/dist/src/adapters/raw-stream-log.js +106 -0
- package/dist/src/adapters/resource-monitor.js +113 -0
- package/dist/src/agent-activity.js +133 -0
- package/dist/src/agent-delegate-task-rpc.js +129 -0
- package/dist/src/agent-delegate-task.js +48 -0
- package/dist/src/agent-file-rpc.js +152 -0
- package/dist/src/agent-image-rpc.js +148 -0
- package/dist/src/agent-platform-routing.js +13 -0
- package/dist/src/agent-reload-config-rpc.js +23 -0
- package/dist/src/agent-rpc-body.js +87 -0
- package/dist/src/agent-stop-stuck.js +110 -0
- package/dist/src/card-action-routing.js +7 -0
- package/dist/src/card-plain-text.js +101 -0
- package/dist/src/cardkit.js +158 -0
- package/dist/src/cards.js +573 -0
- package/dist/src/chatgpt-subscription-rpc.js +18 -0
- package/dist/src/chatgpt-subscription.js +199 -0
- package/dist/src/chrome-devtools-guard.js +238 -0
- package/dist/src/claude-sdk-installer.js +249 -0
- package/dist/src/codex-reset-actions.js +143 -0
- package/dist/src/config-utils.js +149 -0
- package/dist/src/config.js +804 -0
- package/dist/src/cursor-usage.js +77 -0
- package/dist/src/exit-banner.js +28 -0
- package/dist/src/feishu-api.js +1404 -0
- package/dist/src/feishu-message-ingress.js +137 -0
- package/dist/src/feishu-platform.js +97 -0
- package/dist/src/format-message.js +252 -0
- package/dist/src/git-command.js +155 -0
- package/dist/src/im-skills.js +121 -0
- package/dist/src/index.js +833 -0
- package/dist/src/litellm-proxy.js +300 -0
- package/dist/src/orchestrator.js +2078 -0
- package/dist/src/package-root.js +26 -0
- package/dist/src/platform-adapter.js +7 -0
- package/dist/src/platform-startup.js +6 -0
- package/dist/src/privacy.js +100 -0
- package/dist/src/progress/reducer.js +102 -0
- package/dist/src/progress/terminal-renderer.js +264 -0
- package/dist/src/progress/view.js +30 -0
- package/dist/src/response-stall.js +14 -0
- package/dist/src/runtime-entry.js +13 -0
- package/dist/src/runtime-reload.js +19 -0
- package/dist/src/session-chat-binding.js +183 -0
- package/dist/src/session-name.js +7 -0
- package/dist/src/session.js +2144 -0
- package/dist/src/shared-prefix.js +16 -0
- package/dist/src/shared.js +493 -0
- package/dist/src/sim-agent.js +105 -0
- package/dist/src/sim-platform.js +142 -0
- package/dist/src/sim-store.js +231 -0
- package/dist/src/simplify.js +99 -0
- package/dist/src/startup-lifecycle.js +209 -0
- package/dist/src/stream-state.js +141 -0
- package/dist/src/terminal-error.js +100 -0
- package/dist/src/trace.js +50 -0
- package/dist/src/turn-cards.js +92 -0
- package/dist/src/update-command-guard.js +114 -0
- package/{src/web-ui.ts → dist/src/web-ui.js} +749 -823
- package/dist/src/wechat-platform.js +545 -0
- package/package.json +7 -5
- package/deepccc-agent/LICENSE +0 -201
- package/deepccc-agent/bin/deepccc.mjs +0 -26
- package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
- package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
- package/deepccc-agent/package-lock.json +0 -2027
- package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -852
- package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
- package/deepccc-agent/src/__tests__/config.test.ts +0 -34
- package/deepccc-agent/src/__tests__/context.test.ts +0 -341
- package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
- package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
- package/deepccc-agent/src/cli.ts +0 -682
- package/deepccc-agent/src/config.ts +0 -101
- package/deepccc-agent/src/context.ts +0 -465
- package/deepccc-agent/src/file-log.ts +0 -38
- package/deepccc-agent/src/file-tools.ts +0 -1493
- package/deepccc-agent/src/index.ts +0 -676
- package/deepccc-agent/src/permissions.ts +0 -226
- package/deepccc-agent/src/privacy.ts +0 -141
- package/deepccc-agent/src/proc-tree-kill.ts +0 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
- package/deepccc-agent/src/progress/reducer.ts +0 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
- package/deepccc-agent/src/progress/view.ts +0 -77
- package/deepccc-agent/src/raw-stream-log.ts +0 -124
- package/deepccc-agent/src/session-search.ts +0 -370
- package/deepccc-agent/src/session-select.ts +0 -48
- package/deepccc-agent/src/sigint.ts +0 -50
- package/deepccc-agent/src/skills.ts +0 -205
- package/deepccc-agent/src/web-tools.ts +0 -313
- package/deepccc-agent/tsconfig.build.json +0 -13
- package/deepccc-agent/tsconfig.json +0 -13
- package/deepccc-agent/vitest.config.ts +0 -7
- package/src/__tests__/adapter-interface.test.ts +0 -152
- package/src/__tests__/agent-activity.test.ts +0 -86
- package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
- package/src/__tests__/agent-image-rpc.test.ts +0 -34
- package/src/__tests__/agent-platform-routing.test.ts +0 -26
- package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
- package/src/__tests__/agent-rpc-body.test.ts +0 -42
- package/src/__tests__/builtin-chat-session.test.ts +0 -532
- package/src/__tests__/builtin-cli-json.test.ts +0 -39
- package/src/__tests__/builtin-config.test.ts +0 -26
- package/src/__tests__/builtin-context.test.ts +0 -319
- package/src/__tests__/builtin-file-tools.test.ts +0 -240
- package/src/__tests__/builtin-permissions.test.ts +0 -219
- package/src/__tests__/builtin-session-search.test.ts +0 -262
- package/src/__tests__/builtin-session-select.test.ts +0 -116
- package/src/__tests__/builtin-sigint.test.ts +0 -56
- package/src/__tests__/builtin-skills.test.ts +0 -284
- package/src/__tests__/builtin-web-tools.test.ts +0 -220
- package/src/__tests__/card-action-routing.test.ts +0 -18
- package/src/__tests__/card-plain-text.test.ts +0 -45
- package/src/__tests__/cardkit.test.ts +0 -60
- package/src/__tests__/cards.test.ts +0 -607
- package/src/__tests__/ccc-adapter.test.ts +0 -194
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
- package/src/__tests__/chatgpt-subscription.test.ts +0 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
- package/src/__tests__/claude-adapter.test.ts +0 -614
- package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
- package/src/__tests__/claude-sdk-installer.test.ts +0 -285
- package/src/__tests__/codex-adapter.test.ts +0 -331
- package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
- package/src/__tests__/codex-reset-actions.test.ts +0 -146
- package/src/__tests__/config-reload.test.ts +0 -284
- package/src/__tests__/config-sample.test.ts +0 -97
- package/src/__tests__/config-utils.test.ts +0 -40
- package/src/__tests__/config.test.ts +0 -395
- package/src/__tests__/crash-logging.test.ts +0 -360
- package/src/__tests__/cursor-adapter.test.ts +0 -890
- package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
- package/src/__tests__/feishu-api.test.ts +0 -60
- package/src/__tests__/feishu-avatar.test.ts +0 -504
- package/src/__tests__/feishu-message-ingress.test.ts +0 -138
- package/src/__tests__/feishu-platform.test.ts +0 -75
- package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
- package/src/__tests__/format-message.test.ts +0 -316
- package/src/__tests__/git-command.test.ts +0 -288
- package/src/__tests__/im-skills.test.ts +0 -125
- package/src/__tests__/jsonl-stream.test.ts +0 -79
- package/src/__tests__/orchestrator.test.ts +0 -1268
- package/src/__tests__/package-files.test.ts +0 -24
- package/src/__tests__/platform-startup.test.ts +0 -19
- package/src/__tests__/privacy.test.ts +0 -198
- package/src/__tests__/proc-tree-kill.test.ts +0 -108
- package/src/__tests__/progress-reducer.test.ts +0 -121
- package/src/__tests__/raw-stream-log.test.ts +0 -106
- package/src/__tests__/response-stall.test.ts +0 -49
- package/src/__tests__/restart.test.ts +0 -232
- package/src/__tests__/session-ccc-config.test.ts +0 -66
- package/src/__tests__/session.test.ts +0 -3004
- package/src/__tests__/shared-prefix.test.ts +0 -36
- package/src/__tests__/sim-agent.test.ts +0 -174
- package/src/__tests__/sim-platform.test.ts +0 -93
- package/src/__tests__/sim-store.test.ts +0 -214
- package/src/__tests__/simplify.test.ts +0 -283
- package/src/__tests__/startup-lifecycle.test.ts +0 -231
- package/src/__tests__/stop-session.test.ts +0 -162
- package/src/__tests__/stream-state.test.ts +0 -164
- package/src/__tests__/terminal-error.test.ts +0 -54
- package/src/__tests__/terminal-renderer.test.ts +0 -247
- package/src/__tests__/update-command-guard.test.ts +0 -144
- package/src/__tests__/web-ui.test.ts +0 -438
- package/src/__tests__/wechat-platform.test.ts +0 -111
- package/src/adapters/adapter-interface.ts +0 -217
- package/src/adapters/ccc-adapter.ts +0 -150
- package/src/adapters/claude-adapter.ts +0 -673
- package/src/adapters/claude-session-meta-store.ts +0 -120
- package/src/adapters/codex-adapter.ts +0 -426
- package/src/adapters/codex-session-meta-store.ts +0 -131
- package/src/adapters/cursor-adapter.ts +0 -681
- package/src/adapters/cursor-session-meta-store.ts +0 -154
- package/src/adapters/jsonl-stream.ts +0 -157
- package/src/adapters/raw-stream-log.ts +0 -124
- package/src/adapters/resource-monitor.ts +0 -141
- package/src/agent-activity.ts +0 -175
- package/src/agent-delegate-task-rpc.ts +0 -153
- package/src/agent-delegate-task.ts +0 -91
- package/src/agent-file-rpc.ts +0 -172
- package/src/agent-image-rpc.ts +0 -168
- package/src/agent-platform-routing.ts +0 -28
- package/src/agent-reload-config-rpc.ts +0 -34
- package/src/agent-rpc-body.ts +0 -92
- package/src/agent-stop-stuck.ts +0 -129
- package/src/card-action-routing.ts +0 -14
- package/src/card-plain-text.ts +0 -108
- package/src/cardkit.ts +0 -179
- package/src/cards.ts +0 -684
- package/src/chatgpt-subscription-rpc.ts +0 -27
- package/src/chatgpt-subscription.ts +0 -299
- package/src/chrome-devtools-guard.ts +0 -318
- package/src/claude-sdk-installer.ts +0 -324
- package/src/codex-reset-actions.ts +0 -184
- package/src/config-utils.ts +0 -211
- package/src/config.ts +0 -1063
- package/src/cursor-usage.ts +0 -128
- package/src/exit-banner.ts +0 -33
- package/src/feishu-api.ts +0 -1616
- package/src/feishu-message-ingress.ts +0 -195
- package/src/feishu-platform.ts +0 -159
- package/src/format-message.ts +0 -293
- package/src/git-command.ts +0 -202
- package/src/im-skills.ts +0 -149
- package/src/index.ts +0 -1089
- package/src/litellm-proxy.ts +0 -374
- package/src/orchestrator.ts +0 -2543
- package/src/platform-adapter.ts +0 -70
- package/src/platform-startup.ts +0 -16
- package/src/privacy.ts +0 -118
- package/src/progress/reducer.ts +0 -113
- package/src/progress/terminal-renderer.ts +0 -294
- package/src/progress/view.ts +0 -77
- package/src/response-stall.ts +0 -28
- package/src/runtime-reload.ts +0 -34
- package/src/session-chat-binding.ts +0 -292
- package/src/session-name.ts +0 -8
- package/src/session.ts +0 -2659
- package/src/shared-prefix.ts +0 -29
- package/src/shared.ts +0 -552
- package/src/sim-agent.ts +0 -167
- package/src/sim-platform.ts +0 -177
- package/src/sim-store.ts +0 -317
- package/src/simplify.ts +0 -120
- package/src/startup-lifecycle.ts +0 -250
- package/src/stream-state.ts +0 -177
- package/src/terminal-error.ts +0 -129
- package/src/trace.ts +0 -51
- package/src/turn-cards.ts +0 -118
- package/src/update-command-guard.ts +0 -165
- package/src/wechat-platform.ts +0 -680
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* progress/terminal-renderer.ts — 终端"过程区块"渲染器
|
|
3
|
+
*
|
|
4
|
+
* 把 ProgressView 渲染为终端里的一块固定区域(对应飞书过程卡片):
|
|
5
|
+
* - 状态行:生成中 / 完成 / 已停止 / 异常结束
|
|
6
|
+
* - 工具行:每个工具调用一行(emoji + 名称 + ✓/✗ + 摘要),不再滚屏刷 JSON
|
|
7
|
+
* - 正文行:模型流式输出,原地更新不滚动
|
|
8
|
+
*
|
|
9
|
+
* 实现要点:
|
|
10
|
+
* - 隐藏光标(\x1b[?25l)→ 每帧整块重绘(\x1b[{n}A 上移 + \x1b[2K 清行 + \x1b[J 清下方)
|
|
11
|
+
* - 帧节流合并重绘,避免高频文本增量导致闪烁
|
|
12
|
+
* - 每行按显示宽度截断(CJK/emoji 算 2 列),保证永不折行:一旦折行,
|
|
13
|
+
* 实际占用的屏幕行数会超过 blockLines 计数,下次重绘上移行数不足,
|
|
14
|
+
* 会清掉区块上方的历史内容(用户输入的问题被"吃掉")。
|
|
15
|
+
* - end() 时把终态区块定型留在屏幕上(与飞书完成卡片留在消息流语义一致),恢复光标
|
|
16
|
+
*/
|
|
17
|
+
import { getToolEmoji, truncateContent } from "./cards-helpers.js";
|
|
18
|
+
import { progressView } from "./view.js";
|
|
19
|
+
const RESET = "\x1b[0m";
|
|
20
|
+
const DIM = "\x1b[2m";
|
|
21
|
+
const BOLD = "\x1b[1m";
|
|
22
|
+
const GREEN = "\x1b[32m";
|
|
23
|
+
const YELLOW = "\x1b[33m";
|
|
24
|
+
const RED = "\x1b[31m";
|
|
25
|
+
const HIDE_CURSOR = "\x1b[?25l";
|
|
26
|
+
const SHOW_CURSOR = "\x1b[?25h";
|
|
27
|
+
/** 生成中标题后的心跳点号动画帧(1 → 2 → 3 → 2 循环,避免跳变感) */
|
|
28
|
+
const ANIM_FRAMES = ["·", "··", "···", "··"];
|
|
29
|
+
const ANSI_TOKEN = /\x1b\[[0-9;]*m/;
|
|
30
|
+
/** 单个字符在终端里的显示列宽:CJK/emoji 占 2 列,控制符/ZWJ/变体选择符占 0 列 */
|
|
31
|
+
export function charWidth(ch) {
|
|
32
|
+
const code = ch.codePointAt(0);
|
|
33
|
+
if (code < 32 || (code >= 0x7f && code <= 0xa0))
|
|
34
|
+
return 0; // 控制字符
|
|
35
|
+
if (code === 0x200d || code === 0xfe0e || code === 0xfe0f)
|
|
36
|
+
return 0; // ZWJ / VS15 / VS16
|
|
37
|
+
if ((code >= 0x1100 && code <= 0x115f) || // 谚文 Jamo
|
|
38
|
+
(code >= 0x2e80 && code <= 0x303f) || // CJK 部首 / 标点
|
|
39
|
+
(code >= 0x3040 && code <= 0xa4cf) || // 假名 / 汉字 / 谚文 / 彝文
|
|
40
|
+
(code >= 0xac00 && code <= 0xd7a3) || // 谚文音节
|
|
41
|
+
(code >= 0xf900 && code <= 0xfaff) || // CJK 兼容汉字
|
|
42
|
+
(code >= 0xfe30 && code <= 0xfe6f) || // CJK 兼容形式
|
|
43
|
+
(code >= 0xff00 && code <= 0xff60) || // 全角形式
|
|
44
|
+
(code >= 0xffe0 && code <= 0xffe6) || // 全角符号
|
|
45
|
+
(code >= 0x2600 && code <= 0x27bf) || // 杂项符号 + Dingbats(emoji 呈现 2 列)
|
|
46
|
+
(code >= 0x1f000 && code <= 0x1faff) // emoji 补充区
|
|
47
|
+
) {
|
|
48
|
+
return 2;
|
|
49
|
+
}
|
|
50
|
+
return 1;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 按终端显示宽度截断一行(CJK/emoji 计 2 列),ANSI 序列零宽且永不截断。
|
|
54
|
+
* 截断位置若处于未闭合的颜色状态,自动补 \x1b[0m,避免颜色泄漏到后续输出。
|
|
55
|
+
*/
|
|
56
|
+
export function clipToWidth(s, maxCols) {
|
|
57
|
+
if (maxCols <= 0)
|
|
58
|
+
return "";
|
|
59
|
+
let out = "";
|
|
60
|
+
let visible = 0;
|
|
61
|
+
let colorOpen = false;
|
|
62
|
+
let i = 0;
|
|
63
|
+
while (i < s.length) {
|
|
64
|
+
if (s.charCodeAt(i) === 0x1b) {
|
|
65
|
+
const m = ANSI_TOKEN.exec(s.slice(i));
|
|
66
|
+
if (m && m.index === 0) {
|
|
67
|
+
out += m[0];
|
|
68
|
+
colorOpen = m[0] !== "\x1b[0m";
|
|
69
|
+
i += m[0].length;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const cp = s.codePointAt(i);
|
|
74
|
+
const ch = String.fromCodePoint(cp);
|
|
75
|
+
const w = charWidth(ch);
|
|
76
|
+
if (visible + w > maxCols)
|
|
77
|
+
break;
|
|
78
|
+
out += ch;
|
|
79
|
+
visible += w;
|
|
80
|
+
i += ch.length;
|
|
81
|
+
}
|
|
82
|
+
if (colorOpen)
|
|
83
|
+
out += "\x1b[0m";
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
function buildStatusLine(view) {
|
|
87
|
+
switch (view.status) {
|
|
88
|
+
case "done":
|
|
89
|
+
return `${BOLD}${GREEN}✅ 完成${RESET}`;
|
|
90
|
+
case "stopped":
|
|
91
|
+
return `${YELLOW}⏹ 已停止${RESET}`;
|
|
92
|
+
case "error":
|
|
93
|
+
return `${RED}❌ 异常结束${RESET}`;
|
|
94
|
+
case "generating": {
|
|
95
|
+
const hint = view.showStop ? `${DIM} · Ctrl+C 停止${RESET}` : "";
|
|
96
|
+
return `⏳ ${view.headerTitle}${hint}`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function buildToolLine(tool) {
|
|
101
|
+
const emoji = getToolEmoji(tool.name);
|
|
102
|
+
const mark = tool.status === "running"
|
|
103
|
+
? "…"
|
|
104
|
+
: tool.status === "ok"
|
|
105
|
+
? `${GREEN}✓${RESET}`
|
|
106
|
+
: `${RED}✗${RESET}`;
|
|
107
|
+
const info = tool.status === "running" ? (tool.detail ?? "") : (tool.summary ?? "");
|
|
108
|
+
// 摘要/详情用正常色:最终区块里不出现浅色文字(可读性优先)
|
|
109
|
+
return ` ${emoji} ${tool.name} ${mark} ${info}`;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 把 ProgressView 展开为区块的完整行列表(不含 ANSI 定位序列,只含内容与颜色)。
|
|
113
|
+
* 单独导出便于单元测试;每行按终端宽度截断,避免长行折行导致行数计数失准。
|
|
114
|
+
*/
|
|
115
|
+
export function buildBlockLines(view, width, maxBodyLines = 30, maxBodyChars = 12000) {
|
|
116
|
+
const clip = (s) => clipToWidth(s, width);
|
|
117
|
+
const lines = [];
|
|
118
|
+
lines.push(clip(buildStatusLine(view)));
|
|
119
|
+
for (const tool of view.tools) {
|
|
120
|
+
lines.push(clip(buildToolLine(tool)));
|
|
121
|
+
}
|
|
122
|
+
const body = truncateContent(view.text, maxBodyLines, maxBodyChars);
|
|
123
|
+
if (body.trim()) {
|
|
124
|
+
for (const line of body.split("\n")) {
|
|
125
|
+
lines.push(clip(line));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
lines.push(`${DIM}等待 Agent 输出...${RESET}`);
|
|
130
|
+
}
|
|
131
|
+
return lines;
|
|
132
|
+
}
|
|
133
|
+
export class TerminalProgressRenderer {
|
|
134
|
+
out;
|
|
135
|
+
frameMs;
|
|
136
|
+
animMs;
|
|
137
|
+
maxBodyLines;
|
|
138
|
+
maxBodyChars;
|
|
139
|
+
view;
|
|
140
|
+
blockLines = 0;
|
|
141
|
+
dirty = false;
|
|
142
|
+
timer = null;
|
|
143
|
+
ended = false;
|
|
144
|
+
animFrame = 0;
|
|
145
|
+
animTimer = null;
|
|
146
|
+
constructor(opts = {}) {
|
|
147
|
+
this.out = opts.out ?? process.stdout;
|
|
148
|
+
this.frameMs = opts.frameMs ?? 66;
|
|
149
|
+
this.animMs = opts.animMs ?? 300;
|
|
150
|
+
this.maxBodyLines = opts.maxBodyLines ?? 30;
|
|
151
|
+
this.maxBodyChars = opts.maxBodyChars ?? 12000;
|
|
152
|
+
this.view = progressView();
|
|
153
|
+
}
|
|
154
|
+
/** 开始一轮区块:隐藏光标并渲染首帧,随后启动生成中心跳动画 */
|
|
155
|
+
begin(view) {
|
|
156
|
+
this.view = view;
|
|
157
|
+
this.out.write(HIDE_CURSOR);
|
|
158
|
+
this.renderNow(view);
|
|
159
|
+
this.startAnimation();
|
|
160
|
+
}
|
|
161
|
+
/** 标记视图已更新,按帧节流合并重绘(高频增量不闪烁) */
|
|
162
|
+
render(view) {
|
|
163
|
+
this.view = view;
|
|
164
|
+
if (this.ended)
|
|
165
|
+
return;
|
|
166
|
+
this.dirty = true;
|
|
167
|
+
if (this.timer)
|
|
168
|
+
return;
|
|
169
|
+
this.timer = setTimeout(() => {
|
|
170
|
+
this.timer = null;
|
|
171
|
+
if (this.dirty && !this.ended) {
|
|
172
|
+
this.dirty = false;
|
|
173
|
+
this.renderNow(this.view);
|
|
174
|
+
}
|
|
175
|
+
}, this.frameMs);
|
|
176
|
+
}
|
|
177
|
+
/** 强制立即重绘(工具结果、终态等低频率但需及时反馈的事件) */
|
|
178
|
+
flush() {
|
|
179
|
+
if (this.timer) {
|
|
180
|
+
clearTimeout(this.timer);
|
|
181
|
+
this.timer = null;
|
|
182
|
+
}
|
|
183
|
+
if (this.ended)
|
|
184
|
+
return;
|
|
185
|
+
this.dirty = false;
|
|
186
|
+
this.renderNow(this.view);
|
|
187
|
+
}
|
|
188
|
+
/** 结束一轮:定型终态区块(留在屏幕上),恢复光标,停止动画 */
|
|
189
|
+
end(view) {
|
|
190
|
+
if (this.timer) {
|
|
191
|
+
clearTimeout(this.timer);
|
|
192
|
+
this.timer = null;
|
|
193
|
+
}
|
|
194
|
+
this.stopAnimation();
|
|
195
|
+
this.ended = true;
|
|
196
|
+
this.renderNow(view);
|
|
197
|
+
this.out.write(SHOW_CURSOR);
|
|
198
|
+
}
|
|
199
|
+
/** 兜底清理:清除未决定时器并恢复光标 */
|
|
200
|
+
dispose() {
|
|
201
|
+
if (this.timer) {
|
|
202
|
+
clearTimeout(this.timer);
|
|
203
|
+
this.timer = null;
|
|
204
|
+
}
|
|
205
|
+
this.stopAnimation();
|
|
206
|
+
if (!this.ended) {
|
|
207
|
+
this.out.write(SHOW_CURSOR);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
startAnimation() {
|
|
211
|
+
if (this.animMs <= 0 || this.animTimer)
|
|
212
|
+
return;
|
|
213
|
+
this.animTimer = setInterval(() => {
|
|
214
|
+
this.animFrame++;
|
|
215
|
+
// 终态(done/stopped/error)由 end() 停止;此处兜底跳过,避免结束后还在重绘
|
|
216
|
+
if (this.ended || this.view.status !== "generating")
|
|
217
|
+
return;
|
|
218
|
+
// 无新事件也持续重绘,点号动画让静止期(思考/工具运行中)也有动态感
|
|
219
|
+
this.renderNow(this.view);
|
|
220
|
+
}, this.animMs);
|
|
221
|
+
this.animTimer.unref?.();
|
|
222
|
+
}
|
|
223
|
+
stopAnimation() {
|
|
224
|
+
if (this.animTimer) {
|
|
225
|
+
clearInterval(this.animTimer);
|
|
226
|
+
this.animTimer = null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
renderNow(view) {
|
|
230
|
+
const width = this.out.columns && this.out.columns > 0 ? this.out.columns : 80;
|
|
231
|
+
const lines = buildBlockLines(this.applyAnimation(view), width, this.maxBodyLines, this.maxBodyChars);
|
|
232
|
+
const newLines = lines.length;
|
|
233
|
+
if (this.blockLines > 0) {
|
|
234
|
+
// 关键:光标此刻停在区块【最后一行】行尾,回到区块第一行只需上移
|
|
235
|
+
// blockLines - 1 行。上移 blockLines 行会每帧多上移 1 行,把区块上方
|
|
236
|
+
// 的历史内容逐行"吃掉"(用户看到的"一行一行往上吃")。
|
|
237
|
+
this.out.write(`\x1b[${this.blockLines - 1}A`);
|
|
238
|
+
this.out.write("\r");
|
|
239
|
+
}
|
|
240
|
+
for (let i = 0; i < lines.length; i++) {
|
|
241
|
+
this.out.write(`\r\x1b[2K${lines[i]}`);
|
|
242
|
+
if (i < lines.length - 1) {
|
|
243
|
+
this.out.write("\n");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (newLines < this.blockLines) {
|
|
247
|
+
// 新区块比旧区块短:清掉下方多出的行
|
|
248
|
+
this.out.write("\n");
|
|
249
|
+
for (let i = 0; i < this.blockLines - newLines; i++) {
|
|
250
|
+
this.out.write("\x1b[2K\n");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
this.out.write("\x1b[J");
|
|
254
|
+
this.blockLines = newLines;
|
|
255
|
+
}
|
|
256
|
+
/** 生成中阶段在标题后叠加心跳点号动画;终态原样返回(buildBlockLines 保持纯函数可测) */
|
|
257
|
+
applyAnimation(view) {
|
|
258
|
+
if (view.status !== "generating")
|
|
259
|
+
return view;
|
|
260
|
+
const base = view.headerTitle.replace(/\.\.\.\s*$/, "").trim();
|
|
261
|
+
const dots = ANIM_FRAMES[this.animFrame % ANIM_FRAMES.length];
|
|
262
|
+
return { ...view, headerTitle: `${base} ${dots}` };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* progress/view.ts — 平台无关的"过程进度"视图模型
|
|
3
|
+
*
|
|
4
|
+
* 这是飞书过程卡片与终端过程区块共享的唯一数据模型:
|
|
5
|
+
* - 飞书:buildProgressCard(view) 把 ProgressView 渲染为卡片 JSON
|
|
6
|
+
* - 终端:TerminalProgressRenderer 把 ProgressView 渲染为 ANSI 区块
|
|
7
|
+
* - 事件流:reduceProgress(prev, event) 把 ChatEvent 增量合并进 ProgressView
|
|
8
|
+
*
|
|
9
|
+
* 语义与飞书卡片对齐:
|
|
10
|
+
* - headerTitle 是"生成中"阶段展示的标题(如"正在启动 Agent · 0秒")
|
|
11
|
+
* - status 决定终态外观(完成 / 已停止 / 异常结束)
|
|
12
|
+
* - text 是正文累积(对应卡片 main_content / 终端区块正文)
|
|
13
|
+
* - tools 是本轮工具调用列表(飞书卡片暂不展示,终端折叠为单行)
|
|
14
|
+
*/
|
|
15
|
+
/** 创建 ProgressView,未提供的字段使用与飞书 buildProgressCard 一致的默认值 */
|
|
16
|
+
export function progressView(init = {}) {
|
|
17
|
+
return {
|
|
18
|
+
status: init.status ?? "generating",
|
|
19
|
+
headerTitle: init.headerTitle ?? "生成中...",
|
|
20
|
+
headerTemplate: init.headerTemplate ?? "blue",
|
|
21
|
+
text: init.text ?? "",
|
|
22
|
+
tools: init.tools ?? [],
|
|
23
|
+
showStop: init.showStop ?? true,
|
|
24
|
+
updatedAt: Date.now(),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** 只读浅拷贝并覆盖部分字段,返回新视图(reducer 的不可变更新用) */
|
|
28
|
+
export function withProgressView(prev, patch) {
|
|
29
|
+
return { ...prev, ...patch, updatedAt: Date.now() };
|
|
30
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { createWriteStream } from "node:fs";
|
|
2
|
+
import { mkdir, readdir, rm, stat, unlink } from "node:fs/promises";
|
|
3
|
+
import { once } from "node:events";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createGzip } from "node:zlib";
|
|
6
|
+
export function sanitizeLogPathSegment(value) {
|
|
7
|
+
const safe = value.replace(/[^a-zA-Z0-9._-]+/g, "_").replace(/^[._]+|_+$/g, "");
|
|
8
|
+
return safe || "unknown";
|
|
9
|
+
}
|
|
10
|
+
async function cleanupOldRawStreamLogs(rootDir, retentionDays) {
|
|
11
|
+
if (!Number.isFinite(retentionDays) || retentionDays <= 0)
|
|
12
|
+
return;
|
|
13
|
+
const cutoff = Date.now() - retentionDays * 24 * 60 * 60 * 1000;
|
|
14
|
+
const visit = async (dir) => {
|
|
15
|
+
let entries;
|
|
16
|
+
try {
|
|
17
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await Promise.all(entries.map(async (entry) => {
|
|
23
|
+
const path = join(dir, entry.name);
|
|
24
|
+
if (entry.isDirectory()) {
|
|
25
|
+
await visit(path);
|
|
26
|
+
try {
|
|
27
|
+
await rm(path, { recursive: false });
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Directory is not empty or already gone.
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (!entry.isFile())
|
|
35
|
+
return;
|
|
36
|
+
try {
|
|
37
|
+
const info = await stat(path);
|
|
38
|
+
if (info.mtimeMs < cutoff)
|
|
39
|
+
await rm(path, { force: true });
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Best-effort cleanup only.
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
await visit(rootDir);
|
|
47
|
+
}
|
|
48
|
+
export async function createRawStreamLog(options) {
|
|
49
|
+
if (!options.enabled)
|
|
50
|
+
return null;
|
|
51
|
+
const maxBytes = Math.max(0, Math.floor(options.maxBytesPerTurn));
|
|
52
|
+
const tool = sanitizeLogPathSegment(options.tool);
|
|
53
|
+
const session = sanitizeLogPathSegment(options.sessionId);
|
|
54
|
+
const label = sanitizeLogPathSegment(options.label);
|
|
55
|
+
const dir = join(options.rootDir, tool, session);
|
|
56
|
+
await mkdir(dir, { recursive: true });
|
|
57
|
+
void cleanupOldRawStreamLogs(join(options.rootDir, tool), options.retentionDays);
|
|
58
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
59
|
+
const filePath = join(dir, `${timestamp}-${label}.jsonl.gz`);
|
|
60
|
+
const output = createWriteStream(filePath);
|
|
61
|
+
const gzip = createGzip();
|
|
62
|
+
gzip.pipe(output);
|
|
63
|
+
let bytes = 0;
|
|
64
|
+
let truncated = false;
|
|
65
|
+
let ended = false;
|
|
66
|
+
const writeLine = (line) => {
|
|
67
|
+
if (ended || truncated)
|
|
68
|
+
return;
|
|
69
|
+
const payload = `${line}\n`;
|
|
70
|
+
const payloadBytes = Buffer.byteLength(payload, "utf-8");
|
|
71
|
+
if (maxBytes > 0 && bytes + payloadBytes > maxBytes) {
|
|
72
|
+
const marker = JSON.stringify({
|
|
73
|
+
type: "deepccc_raw_stream_log_truncated",
|
|
74
|
+
reason: "max_bytes_per_turn_exceeded",
|
|
75
|
+
maxBytesPerTurn: maxBytes,
|
|
76
|
+
writtenBytes: bytes,
|
|
77
|
+
});
|
|
78
|
+
gzip.write(`${marker}\n`);
|
|
79
|
+
truncated = true;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
bytes += payloadBytes;
|
|
83
|
+
gzip.write(payload);
|
|
84
|
+
};
|
|
85
|
+
const close = async ({ keep }) => {
|
|
86
|
+
if (ended)
|
|
87
|
+
return;
|
|
88
|
+
ended = true;
|
|
89
|
+
gzip.end();
|
|
90
|
+
try {
|
|
91
|
+
await once(output, "finish");
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// Ignore close errors; caller cannot recover from debug log failures.
|
|
95
|
+
}
|
|
96
|
+
if (!keep) {
|
|
97
|
+
try {
|
|
98
|
+
await unlink(filePath);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Best-effort cleanup only.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
return { filePath, writeLine, close };
|
|
106
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-search.ts — DeepCCC 历史会话关键词检索
|
|
3
|
+
*
|
|
4
|
+
* 供 agent 通过 session_search 工具按需查找很久以前的原始消息/工具调用:
|
|
5
|
+
* - 主数据源:~/.deepccc/sessions/<sessionId>/context.json(明文 JSON,含 summary、
|
|
6
|
+
* messages.content 与结构化 toolCalls)
|
|
7
|
+
* - 可选数据源:~/.deepccc/raw-stream-logs/deepccc/<sessionId>/*.jsonl.gz(gzip 原始流,
|
|
8
|
+
* 逐行解压检索,默认关闭)
|
|
9
|
+
*
|
|
10
|
+
* 纯关键词匹配(多词 AND、大小写不敏感),不依赖向量索引。
|
|
11
|
+
*/
|
|
12
|
+
import { createReadStream, existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { createInterface } from "node:readline";
|
|
15
|
+
import { createGunzip } from "node:zlib";
|
|
16
|
+
import { RAW_STREAM_LOGS_DIR } from "./config.js";
|
|
17
|
+
import { DEFAULT_BUILTIN_CONTEXT_DIR } from "./context.js";
|
|
18
|
+
const DEFAULT_MAX_RESULTS = 20;
|
|
19
|
+
const MAX_RESULTS_CAP = 50;
|
|
20
|
+
const DEFAULT_MAX_SNIPPET_CHARS = 400;
|
|
21
|
+
const MAX_SNIPPET_CHARS_CAP = 2_000;
|
|
22
|
+
const DEFAULT_MAX_RAW_LOG_BYTES_PER_FILE = 4 * 1024 * 1024;
|
|
23
|
+
function clamp(value, min, max) {
|
|
24
|
+
return Math.min(max, Math.max(min, value));
|
|
25
|
+
}
|
|
26
|
+
function tokenize(query) {
|
|
27
|
+
return query
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.split(/\s+/)
|
|
30
|
+
.map((term) => term.trim())
|
|
31
|
+
.filter((term) => term.length > 0);
|
|
32
|
+
}
|
|
33
|
+
/** 多词 AND、大小写不敏感:候选文本必须包含全部关键词 */
|
|
34
|
+
function matchesTerms(text, terms) {
|
|
35
|
+
const lower = text.toLowerCase();
|
|
36
|
+
return terms.every((term) => lower.includes(term));
|
|
37
|
+
}
|
|
38
|
+
function makeSnippet(text, terms, maxChars) {
|
|
39
|
+
if (text.length <= maxChars)
|
|
40
|
+
return text;
|
|
41
|
+
const lower = text.toLowerCase();
|
|
42
|
+
let hitIndex = -1;
|
|
43
|
+
for (const term of terms) {
|
|
44
|
+
const index = lower.indexOf(term);
|
|
45
|
+
if (index !== -1) {
|
|
46
|
+
hitIndex = index;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const half = Math.floor(maxChars / 2);
|
|
51
|
+
if (hitIndex === -1) {
|
|
52
|
+
return `${text.slice(0, half)}…${text.slice(-half)}`;
|
|
53
|
+
}
|
|
54
|
+
const start = Math.max(0, hitIndex - half);
|
|
55
|
+
const end = Math.min(text.length, hitIndex + half);
|
|
56
|
+
const prefix = start > 0 ? "…" : "";
|
|
57
|
+
const suffix = end < text.length ? "…" : "";
|
|
58
|
+
return `${prefix}${text.slice(start, end)}${suffix}`;
|
|
59
|
+
}
|
|
60
|
+
function searchContextDir(query, options, maxResults, maxSnippetChars) {
|
|
61
|
+
const dir = options.contextDir ?? DEFAULT_BUILTIN_CONTEXT_DIR;
|
|
62
|
+
const terms = tokenize(query);
|
|
63
|
+
const matches = [];
|
|
64
|
+
if (terms.length === 0 || !existsSync(dir)) {
|
|
65
|
+
return { matches, scannedSessions: 0, totalHits: 0 };
|
|
66
|
+
}
|
|
67
|
+
const restrictTo = options.sessionId?.trim();
|
|
68
|
+
let scannedSessions = 0;
|
|
69
|
+
let totalHits = 0;
|
|
70
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
71
|
+
if (!entry.isDirectory())
|
|
72
|
+
continue;
|
|
73
|
+
if (restrictTo && entry.name !== restrictTo)
|
|
74
|
+
continue;
|
|
75
|
+
const filePath = join(dir, entry.name, "context.json");
|
|
76
|
+
if (!existsSync(filePath))
|
|
77
|
+
continue;
|
|
78
|
+
let state;
|
|
79
|
+
try {
|
|
80
|
+
state = JSON.parse(readFileSync(filePath, "utf8"));
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
continue; // 损坏或非 JSON 的会话文件跳过
|
|
84
|
+
}
|
|
85
|
+
if (!state || typeof state !== "object")
|
|
86
|
+
continue;
|
|
87
|
+
if (restrictTo && state.sessionId !== restrictTo && entry.name !== restrictTo)
|
|
88
|
+
continue;
|
|
89
|
+
scannedSessions += 1;
|
|
90
|
+
const sessionId = typeof state.sessionId === "string" ? state.sessionId : entry.name;
|
|
91
|
+
if (typeof state.summary === "string" && matchesTerms(state.summary, terms)) {
|
|
92
|
+
totalHits += 1;
|
|
93
|
+
matches.push({
|
|
94
|
+
sessionId,
|
|
95
|
+
source: "summary",
|
|
96
|
+
snippet: makeSnippet(state.summary, terms, maxSnippetChars),
|
|
97
|
+
filePath,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (Array.isArray(state.messages)) {
|
|
101
|
+
state.messages.forEach((message, messageIndex) => {
|
|
102
|
+
if (!message || typeof message !== "object")
|
|
103
|
+
return;
|
|
104
|
+
const raw = message;
|
|
105
|
+
const role = raw.role === "user" || raw.role === "assistant" ? raw.role : undefined;
|
|
106
|
+
if (typeof raw.content === "string" && matchesTerms(raw.content, terms)) {
|
|
107
|
+
totalHits += 1;
|
|
108
|
+
matches.push({
|
|
109
|
+
sessionId,
|
|
110
|
+
source: "context",
|
|
111
|
+
role,
|
|
112
|
+
messageIndex,
|
|
113
|
+
snippet: makeSnippet(raw.content, terms, maxSnippetChars),
|
|
114
|
+
filePath,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (Array.isArray(raw.toolCalls)) {
|
|
118
|
+
raw.toolCalls.forEach((call, toolCallIndex) => {
|
|
119
|
+
if (!call || typeof call !== "object")
|
|
120
|
+
return;
|
|
121
|
+
const callRaw = call;
|
|
122
|
+
const name = typeof callRaw.name === "string" ? callRaw.name : "";
|
|
123
|
+
const input = typeof callRaw.input === "string" ? callRaw.input : "";
|
|
124
|
+
const output = typeof callRaw.output === "string" ? callRaw.output : "";
|
|
125
|
+
if (name.length === 0 && input.length === 0 && output.length === 0)
|
|
126
|
+
return;
|
|
127
|
+
const haystack = [name, input, output].join("\n");
|
|
128
|
+
if (!matchesTerms(haystack, terms))
|
|
129
|
+
return;
|
|
130
|
+
totalHits += 1;
|
|
131
|
+
const source = output || input || name;
|
|
132
|
+
matches.push({
|
|
133
|
+
sessionId,
|
|
134
|
+
source: "context",
|
|
135
|
+
role,
|
|
136
|
+
messageIndex,
|
|
137
|
+
toolCallIndex,
|
|
138
|
+
toolCallName: name,
|
|
139
|
+
snippet: makeSnippet(source, terms, maxSnippetChars),
|
|
140
|
+
filePath,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return { matches: matches.slice(0, maxResults), scannedSessions, totalHits };
|
|
148
|
+
}
|
|
149
|
+
async function searchRawLogs(query, options, maxResults, maxSnippetChars) {
|
|
150
|
+
const terms = tokenize(query);
|
|
151
|
+
const rootDir = options.rawLogsDir ?? RAW_STREAM_LOGS_DIR;
|
|
152
|
+
const matches = [];
|
|
153
|
+
if (terms.length === 0 || !existsSync(rootDir)) {
|
|
154
|
+
return { matches, scannedFiles: 0, truncated: false };
|
|
155
|
+
}
|
|
156
|
+
const toolRoot = join(rootDir, "deepccc");
|
|
157
|
+
if (!existsSync(toolRoot))
|
|
158
|
+
return { matches, scannedFiles: 0, truncated: false };
|
|
159
|
+
const restrictTo = options.sessionId?.trim();
|
|
160
|
+
const maxBytesPerFile = Math.max(0, options.maxRawLogBytesPerFile ?? DEFAULT_MAX_RAW_LOG_BYTES_PER_FILE);
|
|
161
|
+
// 收集所有 .jsonl.gz 文件,按 mtime 新→旧排序(最新轮次优先)
|
|
162
|
+
const files = [];
|
|
163
|
+
for (const sessionEntry of readdirSync(toolRoot, { withFileTypes: true })) {
|
|
164
|
+
if (!sessionEntry.isDirectory())
|
|
165
|
+
continue;
|
|
166
|
+
if (restrictTo && sessionEntry.name !== restrictTo)
|
|
167
|
+
continue;
|
|
168
|
+
const sessionDir = join(toolRoot, sessionEntry.name);
|
|
169
|
+
let entries;
|
|
170
|
+
try {
|
|
171
|
+
entries = readdirSync(sessionDir, { withFileTypes: true });
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
for (const fileEntry of entries) {
|
|
177
|
+
if (!fileEntry.isFile() || !fileEntry.name.endsWith(".jsonl.gz"))
|
|
178
|
+
continue;
|
|
179
|
+
const filePath = join(sessionDir, fileEntry.name);
|
|
180
|
+
try {
|
|
181
|
+
const info = statSync(filePath);
|
|
182
|
+
files.push({ path: filePath, sessionId: sessionEntry.name, mtimeMs: info.mtimeMs });
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
// 无法 stat 的文件跳过
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
files.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
190
|
+
let scannedFiles = 0;
|
|
191
|
+
let truncated = false;
|
|
192
|
+
for (const file of files) {
|
|
193
|
+
if (matches.length >= maxResults) {
|
|
194
|
+
truncated = true;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
scannedFiles += 1;
|
|
198
|
+
const snippets = await searchGzipFileLines(file.path, terms, maxBytesPerFile, maxSnippetChars);
|
|
199
|
+
for (const snippet of snippets) {
|
|
200
|
+
matches.push({
|
|
201
|
+
sessionId: file.sessionId,
|
|
202
|
+
source: "raw-log",
|
|
203
|
+
snippet,
|
|
204
|
+
filePath: file.path,
|
|
205
|
+
});
|
|
206
|
+
if (matches.length >= maxResults)
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return { matches: matches.slice(0, maxResults), scannedFiles, truncated };
|
|
211
|
+
}
|
|
212
|
+
async function searchGzipFileLines(filePath, terms, maxBytes, maxSnippetChars) {
|
|
213
|
+
const hits = [];
|
|
214
|
+
let bytes = 0;
|
|
215
|
+
try {
|
|
216
|
+
const source = createReadStream(filePath);
|
|
217
|
+
const gunzip = createGunzip();
|
|
218
|
+
// 防崩溃(线上事故):截断/损坏的 gzip 会在 zlib 层 emit 'error'(如
|
|
219
|
+
// "unexpected end of file")。若无人监听,错误事件会升级为 uncaughtException
|
|
220
|
+
// 直接杀死整个服务(崩溃黑匣子 exit(1))。显式挂监听把错误降级为
|
|
221
|
+
// “跳过该文件”:destroy 流让 for await 正常结束,不再冒泡。
|
|
222
|
+
source.on("error", () => gunzip.destroy());
|
|
223
|
+
gunzip.on("error", () => gunzip.destroy());
|
|
224
|
+
const reader = createInterface({ input: source.pipe(gunzip), crlfDelay: Infinity });
|
|
225
|
+
// readline 会把 input 流的 error 转发到自己身上,同样需要监听避免冒泡
|
|
226
|
+
reader.on("error", () => { });
|
|
227
|
+
for await (const line of reader) {
|
|
228
|
+
bytes += Buffer.byteLength(line, "utf-8");
|
|
229
|
+
if (bytes > maxBytes)
|
|
230
|
+
break;
|
|
231
|
+
if (!matchesTerms(line, terms))
|
|
232
|
+
continue;
|
|
233
|
+
hits.push(makeSnippet(line, terms, maxSnippetChars));
|
|
234
|
+
if (hits.length >= 50)
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// 损坏的 gzip 文件按 best-effort 跳过
|
|
240
|
+
}
|
|
241
|
+
return hits;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 关键词检索历史会话存档。context.json 为同步扫描;raw-stream-logs 为
|
|
245
|
+
* gzip 逐行解压(异步,仅 options.includeRawLogs 时启用)。
|
|
246
|
+
*/
|
|
247
|
+
export async function searchBuiltinSessions(query, options = {}) {
|
|
248
|
+
const terms = tokenize(query);
|
|
249
|
+
const maxResults = clamp(options.maxResults ?? DEFAULT_MAX_RESULTS, 1, MAX_RESULTS_CAP);
|
|
250
|
+
const maxSnippetChars = clamp(options.maxSnippetChars ?? DEFAULT_MAX_SNIPPET_CHARS, 80, MAX_SNIPPET_CHARS_CAP);
|
|
251
|
+
if (terms.length === 0) {
|
|
252
|
+
return {
|
|
253
|
+
query,
|
|
254
|
+
terms,
|
|
255
|
+
matches: [],
|
|
256
|
+
truncated: false,
|
|
257
|
+
scannedSessions: 0,
|
|
258
|
+
scannedRawLogFiles: 0,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const contextResult = searchContextDir(query, options, maxResults, maxSnippetChars);
|
|
262
|
+
let rawResult = { matches: [], scannedFiles: 0, truncated: false };
|
|
263
|
+
if (options.includeRawLogs) {
|
|
264
|
+
rawResult = await searchRawLogs(query, options, maxResults, maxSnippetChars);
|
|
265
|
+
}
|
|
266
|
+
const matches = [...contextResult.matches, ...rawResult.matches].slice(0, maxResults);
|
|
267
|
+
const truncated = contextResult.totalHits > maxResults || rawResult.truncated || rawResult.matches.length > maxResults;
|
|
268
|
+
return {
|
|
269
|
+
query,
|
|
270
|
+
terms,
|
|
271
|
+
matches,
|
|
272
|
+
truncated,
|
|
273
|
+
scannedSessions: contextResult.scannedSessions,
|
|
274
|
+
scannedRawLogFiles: rawResult.scannedFiles,
|
|
275
|
+
};
|
|
276
|
+
}
|