chatccc 0.2.243 → 0.2.244

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.
Files changed (59) hide show
  1. package/README.md +5 -5
  2. package/bin/cccagent.mjs +17 -17
  3. package/deepccc-agent/bin/deepccc.mjs +26 -26
  4. package/deepccc-agent/os-prompts/darwin.md +8 -0
  5. package/deepccc-agent/os-prompts/linux.md +8 -0
  6. package/deepccc-agent/os-prompts/win32.md +9 -0
  7. package/deepccc-agent/package.json +2 -1
  8. package/deepccc-agent/src/__tests__/chat-session.test.ts +39 -0
  9. package/deepccc-agent/src/__tests__/cli-json.test.ts +49 -49
  10. package/deepccc-agent/src/__tests__/config.test.ts +26 -26
  11. package/deepccc-agent/src/__tests__/context.test.ts +319 -319
  12. package/deepccc-agent/src/__tests__/file-tools.test.ts +240 -240
  13. package/deepccc-agent/src/__tests__/permissions.test.ts +195 -195
  14. package/deepccc-agent/src/__tests__/progress-reducer.test.ts +121 -121
  15. package/deepccc-agent/src/__tests__/session-search.test.ts +262 -262
  16. package/deepccc-agent/src/__tests__/session-select.test.ts +116 -116
  17. package/deepccc-agent/src/__tests__/sigint.test.ts +56 -56
  18. package/deepccc-agent/src/__tests__/skills.test.ts +284 -284
  19. package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +247 -247
  20. package/deepccc-agent/src/__tests__/web-tools.test.ts +220 -220
  21. package/deepccc-agent/src/config.ts +84 -84
  22. package/deepccc-agent/src/context.ts +465 -465
  23. package/deepccc-agent/src/file-log.ts +38 -38
  24. package/deepccc-agent/src/index.ts +46 -16
  25. package/deepccc-agent/src/proc-tree-kill.ts +61 -61
  26. package/deepccc-agent/src/progress/cards-helpers.ts +76 -76
  27. package/deepccc-agent/src/progress/reducer.ts +113 -113
  28. package/deepccc-agent/src/progress/terminal-renderer.ts +294 -294
  29. package/deepccc-agent/src/progress/view.ts +77 -77
  30. package/deepccc-agent/src/raw-stream-log.ts +124 -124
  31. package/deepccc-agent/src/session-search.ts +370 -370
  32. package/deepccc-agent/src/session-select.ts +48 -48
  33. package/deepccc-agent/src/sigint.ts +50 -50
  34. package/deepccc-agent/src/skills.ts +205 -205
  35. package/deepccc-agent/src/web-tools.ts +313 -313
  36. package/deepccc-agent/tsconfig.build.json +13 -13
  37. package/deepccc-agent/tsconfig.json +13 -13
  38. package/deepccc-agent/vitest.config.ts +7 -7
  39. package/package.json +73 -73
  40. package/src/__tests__/builtin-chat-session.test.ts +522 -522
  41. package/src/__tests__/builtin-config.test.ts +26 -26
  42. package/src/__tests__/builtin-context.test.ts +319 -319
  43. package/src/__tests__/builtin-file-tools.test.ts +240 -240
  44. package/src/__tests__/builtin-permissions.test.ts +211 -211
  45. package/src/__tests__/builtin-session-search.test.ts +262 -262
  46. package/src/__tests__/builtin-session-select.test.ts +116 -116
  47. package/src/__tests__/builtin-sigint.test.ts +56 -56
  48. package/src/__tests__/builtin-skills.test.ts +284 -284
  49. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  50. package/src/__tests__/config.test.ts +17 -17
  51. package/src/__tests__/progress-reducer.test.ts +121 -121
  52. package/src/__tests__/session-ccc-config.test.ts +45 -45
  53. package/src/__tests__/session.test.ts +298 -298
  54. package/src/adapters/ccc-adapter.ts +145 -145
  55. package/src/config-utils.ts +13 -13
  56. package/src/config.ts +13 -13
  57. package/src/progress/reducer.ts +113 -113
  58. package/src/session-chat-binding.ts +83 -83
  59. package/src/session.ts +311 -311
@@ -1,294 +1,294 @@
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
-
18
- import { getToolEmoji, truncateContent } from "./cards-helpers.js";
19
- import { progressView, type ProgressToolCall, type ProgressView } from "./view.js";
20
-
21
- const RESET = "\x1b[0m";
22
- const DIM = "\x1b[2m";
23
- const BOLD = "\x1b[1m";
24
- const GREEN = "\x1b[32m";
25
- const YELLOW = "\x1b[33m";
26
- const RED = "\x1b[31m";
27
-
28
- const HIDE_CURSOR = "\x1b[?25l";
29
- const SHOW_CURSOR = "\x1b[?25h";
30
-
31
- /** 生成中标题后的心跳点号动画帧(1 → 2 → 3 → 2 循环,避免跳变感) */
32
- const ANIM_FRAMES = ["·", "··", "···", "··"];
33
-
34
- const ANSI_TOKEN = /\x1b\[[0-9;]*m/;
35
-
36
- /** 单个字符在终端里的显示列宽:CJK/emoji 占 2 列,控制符/ZWJ/变体选择符占 0 列 */
37
- export function charWidth(ch: string): number {
38
- const code = ch.codePointAt(0)!;
39
- if (code < 32 || (code >= 0x7f && code <= 0xa0)) return 0; // 控制字符
40
- if (code === 0x200d || code === 0xfe0e || code === 0xfe0f) return 0; // ZWJ / VS15 / VS16
41
- if (
42
- (code >= 0x1100 && code <= 0x115f) || // 谚文 Jamo
43
- (code >= 0x2e80 && code <= 0x303f) || // CJK 部首 / 标点
44
- (code >= 0x3040 && code <= 0xa4cf) || // 假名 / 汉字 / 谚文 / 彝文
45
- (code >= 0xac00 && code <= 0xd7a3) || // 谚文音节
46
- (code >= 0xf900 && code <= 0xfaff) || // CJK 兼容汉字
47
- (code >= 0xfe30 && code <= 0xfe6f) || // CJK 兼容形式
48
- (code >= 0xff00 && code <= 0xff60) || // 全角形式
49
- (code >= 0xffe0 && code <= 0xffe6) || // 全角符号
50
- (code >= 0x2600 && code <= 0x27bf) || // 杂项符号 + Dingbats(emoji 呈现 2 列)
51
- (code >= 0x1f000 && code <= 0x1faff) // emoji 补充区
52
- ) {
53
- return 2;
54
- }
55
- return 1;
56
- }
57
-
58
- /**
59
- * 按终端显示宽度截断一行(CJK/emoji 计 2 列),ANSI 序列零宽且永不截断。
60
- * 截断位置若处于未闭合的颜色状态,自动补 \x1b[0m,避免颜色泄漏到后续输出。
61
- */
62
- export function clipToWidth(s: string, maxCols: number): string {
63
- if (maxCols <= 0) return "";
64
- let out = "";
65
- let visible = 0;
66
- let colorOpen = false;
67
- let i = 0;
68
- while (i < s.length) {
69
- if (s.charCodeAt(i) === 0x1b) {
70
- const m = ANSI_TOKEN.exec(s.slice(i));
71
- if (m && m.index === 0) {
72
- out += m[0];
73
- colorOpen = m[0] !== "\x1b[0m";
74
- i += m[0].length;
75
- continue;
76
- }
77
- }
78
- const cp = s.codePointAt(i)!;
79
- const ch = String.fromCodePoint(cp);
80
- const w = charWidth(ch);
81
- if (visible + w > maxCols) break;
82
- out += ch;
83
- visible += w;
84
- i += ch.length;
85
- }
86
- if (colorOpen) out += "\x1b[0m";
87
- return out;
88
- }
89
-
90
- export interface TerminalRendererOptions {
91
- /** 输出流,默认 process.stdout */
92
- out?: NodeJS.WritableStream & { columns?: number };
93
- /** 帧节流毫秒数,默认 66(约 15fps) */
94
- frameMs?: number;
95
- /** 生成中心跳动画毫秒数,默认 300;<=0 禁用动画 */
96
- animMs?: number;
97
- /** 正文最大行数(超出截断,保留首行+末段) */
98
- maxBodyLines?: number;
99
- /** 正文最大字符数 */
100
- maxBodyChars?: number;
101
- }
102
-
103
- function buildStatusLine(view: ProgressView): string {
104
- switch (view.status) {
105
- case "done":
106
- return `${BOLD}${GREEN}✅ 完成${RESET}`;
107
- case "stopped":
108
- return `${YELLOW}⏹ 已停止${RESET}`;
109
- case "error":
110
- return `${RED}❌ 异常结束${RESET}`;
111
- case "generating": {
112
- const hint = view.showStop ? `${DIM} · Ctrl+C 停止${RESET}` : "";
113
- return `⏳ ${view.headerTitle}${hint}`;
114
- }
115
- }
116
- }
117
-
118
- function buildToolLine(tool: ProgressToolCall): string {
119
- const emoji = getToolEmoji(tool.name);
120
- const mark =
121
- tool.status === "running"
122
- ? "…"
123
- : tool.status === "ok"
124
- ? `${GREEN}✓${RESET}`
125
- : `${RED}✗${RESET}`;
126
- const info = tool.status === "running" ? (tool.detail ?? "") : (tool.summary ?? "");
127
- // 摘要/详情用正常色:最终区块里不出现浅色文字(可读性优先)
128
- return ` ${emoji} ${tool.name} ${mark} ${info}`;
129
- }
130
-
131
- /**
132
- * 把 ProgressView 展开为区块的完整行列表(不含 ANSI 定位序列,只含内容与颜色)。
133
- * 单独导出便于单元测试;每行按终端宽度截断,避免长行折行导致行数计数失准。
134
- */
135
- export function buildBlockLines(
136
- view: ProgressView,
137
- width: number,
138
- maxBodyLines = 30,
139
- maxBodyChars = 12000,
140
- ): string[] {
141
- const clip = (s: string) => clipToWidth(s, width);
142
- const lines: string[] = [];
143
- lines.push(clip(buildStatusLine(view)));
144
- for (const tool of view.tools) {
145
- lines.push(clip(buildToolLine(tool)));
146
- }
147
- const body = truncateContent(view.text, maxBodyLines, maxBodyChars);
148
- if (body.trim()) {
149
- for (const line of body.split("\n")) {
150
- lines.push(clip(line));
151
- }
152
- } else {
153
- lines.push(`${DIM}等待 Agent 输出...${RESET}`);
154
- }
155
- return lines;
156
- }
157
-
158
- export class TerminalProgressRenderer {
159
- private readonly out: NodeJS.WritableStream & { columns?: number };
160
- private readonly frameMs: number;
161
- private readonly animMs: number;
162
- private readonly maxBodyLines: number;
163
- private readonly maxBodyChars: number;
164
- private view: ProgressView;
165
- private blockLines = 0;
166
- private dirty = false;
167
- private timer: NodeJS.Timeout | null = null;
168
- private ended = false;
169
- private animFrame = 0;
170
- private animTimer: NodeJS.Timeout | null = null;
171
-
172
- constructor(opts: TerminalRendererOptions = {}) {
173
- this.out = opts.out ?? process.stdout;
174
- this.frameMs = opts.frameMs ?? 66;
175
- this.animMs = opts.animMs ?? 300;
176
- this.maxBodyLines = opts.maxBodyLines ?? 30;
177
- this.maxBodyChars = opts.maxBodyChars ?? 12000;
178
- this.view = progressView();
179
- }
180
-
181
- /** 开始一轮区块:隐藏光标并渲染首帧,随后启动生成中心跳动画 */
182
- begin(view: ProgressView): void {
183
- this.view = view;
184
- this.out.write(HIDE_CURSOR);
185
- this.renderNow(view);
186
- this.startAnimation();
187
- }
188
-
189
- /** 标记视图已更新,按帧节流合并重绘(高频增量不闪烁) */
190
- render(view: ProgressView): void {
191
- this.view = view;
192
- if (this.ended) return;
193
- this.dirty = true;
194
- if (this.timer) return;
195
- this.timer = setTimeout(() => {
196
- this.timer = null;
197
- if (this.dirty && !this.ended) {
198
- this.dirty = false;
199
- this.renderNow(this.view);
200
- }
201
- }, this.frameMs);
202
- }
203
-
204
- /** 强制立即重绘(工具结果、终态等低频率但需及时反馈的事件) */
205
- flush(): void {
206
- if (this.timer) {
207
- clearTimeout(this.timer);
208
- this.timer = null;
209
- }
210
- if (this.ended) return;
211
- this.dirty = false;
212
- this.renderNow(this.view);
213
- }
214
-
215
- /** 结束一轮:定型终态区块(留在屏幕上),恢复光标,停止动画 */
216
- end(view: ProgressView): void {
217
- if (this.timer) {
218
- clearTimeout(this.timer);
219
- this.timer = null;
220
- }
221
- this.stopAnimation();
222
- this.ended = true;
223
- this.renderNow(view);
224
- this.out.write(SHOW_CURSOR);
225
- }
226
-
227
- /** 兜底清理:清除未决定时器并恢复光标 */
228
- dispose(): void {
229
- if (this.timer) {
230
- clearTimeout(this.timer);
231
- this.timer = null;
232
- }
233
- this.stopAnimation();
234
- if (!this.ended) {
235
- this.out.write(SHOW_CURSOR);
236
- }
237
- }
238
-
239
- private startAnimation(): void {
240
- if (this.animMs <= 0 || this.animTimer) return;
241
- this.animTimer = setInterval(() => {
242
- this.animFrame++;
243
- // 终态(done/stopped/error)由 end() 停止;此处兜底跳过,避免结束后还在重绘
244
- if (this.ended || this.view.status !== "generating") return;
245
- // 无新事件也持续重绘,点号动画让静止期(思考/工具运行中)也有动态感
246
- this.renderNow(this.view);
247
- }, this.animMs);
248
- this.animTimer.unref?.();
249
- }
250
-
251
- private stopAnimation(): void {
252
- if (this.animTimer) {
253
- clearInterval(this.animTimer);
254
- this.animTimer = null;
255
- }
256
- }
257
-
258
- private renderNow(view: ProgressView): void {
259
- const width = this.out.columns && this.out.columns > 0 ? this.out.columns : 80;
260
- const lines = buildBlockLines(this.applyAnimation(view), width, this.maxBodyLines, this.maxBodyChars);
261
- const newLines = lines.length;
262
-
263
- if (this.blockLines > 0) {
264
- // 关键:光标此刻停在区块【最后一行】行尾,回到区块第一行只需上移
265
- // blockLines - 1 行。上移 blockLines 行会每帧多上移 1 行,把区块上方
266
- // 的历史内容逐行"吃掉"(用户看到的"一行一行往上吃")。
267
- this.out.write(`\x1b[${this.blockLines - 1}A`);
268
- this.out.write("\r");
269
- }
270
- for (let i = 0; i < lines.length; i++) {
271
- this.out.write(`\r\x1b[2K${lines[i]}`);
272
- if (i < lines.length - 1) {
273
- this.out.write("\n");
274
- }
275
- }
276
- if (newLines < this.blockLines) {
277
- // 新区块比旧区块短:清掉下方多出的行
278
- this.out.write("\n");
279
- for (let i = 0; i < this.blockLines - newLines; i++) {
280
- this.out.write("\x1b[2K\n");
281
- }
282
- }
283
- this.out.write("\x1b[J");
284
- this.blockLines = newLines;
285
- }
286
-
287
- /** 生成中阶段在标题后叠加心跳点号动画;终态原样返回(buildBlockLines 保持纯函数可测) */
288
- private applyAnimation(view: ProgressView): ProgressView {
289
- if (view.status !== "generating") return view;
290
- const base = view.headerTitle.replace(/\.\.\.\s*$/, "").trim();
291
- const dots = ANIM_FRAMES[this.animFrame % ANIM_FRAMES.length];
292
- return { ...view, headerTitle: `${base} ${dots}` };
293
- }
294
- }
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
+
18
+ import { getToolEmoji, truncateContent } from "./cards-helpers.js";
19
+ import { progressView, type ProgressToolCall, type ProgressView } from "./view.js";
20
+
21
+ const RESET = "\x1b[0m";
22
+ const DIM = "\x1b[2m";
23
+ const BOLD = "\x1b[1m";
24
+ const GREEN = "\x1b[32m";
25
+ const YELLOW = "\x1b[33m";
26
+ const RED = "\x1b[31m";
27
+
28
+ const HIDE_CURSOR = "\x1b[?25l";
29
+ const SHOW_CURSOR = "\x1b[?25h";
30
+
31
+ /** 生成中标题后的心跳点号动画帧(1 → 2 → 3 → 2 循环,避免跳变感) */
32
+ const ANIM_FRAMES = ["·", "··", "···", "··"];
33
+
34
+ const ANSI_TOKEN = /\x1b\[[0-9;]*m/;
35
+
36
+ /** 单个字符在终端里的显示列宽:CJK/emoji 占 2 列,控制符/ZWJ/变体选择符占 0 列 */
37
+ export function charWidth(ch: string): number {
38
+ const code = ch.codePointAt(0)!;
39
+ if (code < 32 || (code >= 0x7f && code <= 0xa0)) return 0; // 控制字符
40
+ if (code === 0x200d || code === 0xfe0e || code === 0xfe0f) return 0; // ZWJ / VS15 / VS16
41
+ if (
42
+ (code >= 0x1100 && code <= 0x115f) || // 谚文 Jamo
43
+ (code >= 0x2e80 && code <= 0x303f) || // CJK 部首 / 标点
44
+ (code >= 0x3040 && code <= 0xa4cf) || // 假名 / 汉字 / 谚文 / 彝文
45
+ (code >= 0xac00 && code <= 0xd7a3) || // 谚文音节
46
+ (code >= 0xf900 && code <= 0xfaff) || // CJK 兼容汉字
47
+ (code >= 0xfe30 && code <= 0xfe6f) || // CJK 兼容形式
48
+ (code >= 0xff00 && code <= 0xff60) || // 全角形式
49
+ (code >= 0xffe0 && code <= 0xffe6) || // 全角符号
50
+ (code >= 0x2600 && code <= 0x27bf) || // 杂项符号 + Dingbats(emoji 呈现 2 列)
51
+ (code >= 0x1f000 && code <= 0x1faff) // emoji 补充区
52
+ ) {
53
+ return 2;
54
+ }
55
+ return 1;
56
+ }
57
+
58
+ /**
59
+ * 按终端显示宽度截断一行(CJK/emoji 计 2 列),ANSI 序列零宽且永不截断。
60
+ * 截断位置若处于未闭合的颜色状态,自动补 \x1b[0m,避免颜色泄漏到后续输出。
61
+ */
62
+ export function clipToWidth(s: string, maxCols: number): string {
63
+ if (maxCols <= 0) return "";
64
+ let out = "";
65
+ let visible = 0;
66
+ let colorOpen = false;
67
+ let i = 0;
68
+ while (i < s.length) {
69
+ if (s.charCodeAt(i) === 0x1b) {
70
+ const m = ANSI_TOKEN.exec(s.slice(i));
71
+ if (m && m.index === 0) {
72
+ out += m[0];
73
+ colorOpen = m[0] !== "\x1b[0m";
74
+ i += m[0].length;
75
+ continue;
76
+ }
77
+ }
78
+ const cp = s.codePointAt(i)!;
79
+ const ch = String.fromCodePoint(cp);
80
+ const w = charWidth(ch);
81
+ if (visible + w > maxCols) break;
82
+ out += ch;
83
+ visible += w;
84
+ i += ch.length;
85
+ }
86
+ if (colorOpen) out += "\x1b[0m";
87
+ return out;
88
+ }
89
+
90
+ export interface TerminalRendererOptions {
91
+ /** 输出流,默认 process.stdout */
92
+ out?: NodeJS.WritableStream & { columns?: number };
93
+ /** 帧节流毫秒数,默认 66(约 15fps) */
94
+ frameMs?: number;
95
+ /** 生成中心跳动画毫秒数,默认 300;<=0 禁用动画 */
96
+ animMs?: number;
97
+ /** 正文最大行数(超出截断,保留首行+末段) */
98
+ maxBodyLines?: number;
99
+ /** 正文最大字符数 */
100
+ maxBodyChars?: number;
101
+ }
102
+
103
+ function buildStatusLine(view: ProgressView): string {
104
+ switch (view.status) {
105
+ case "done":
106
+ return `${BOLD}${GREEN}✅ 完成${RESET}`;
107
+ case "stopped":
108
+ return `${YELLOW}⏹ 已停止${RESET}`;
109
+ case "error":
110
+ return `${RED}❌ 异常结束${RESET}`;
111
+ case "generating": {
112
+ const hint = view.showStop ? `${DIM} · Ctrl+C 停止${RESET}` : "";
113
+ return `⏳ ${view.headerTitle}${hint}`;
114
+ }
115
+ }
116
+ }
117
+
118
+ function buildToolLine(tool: ProgressToolCall): string {
119
+ const emoji = getToolEmoji(tool.name);
120
+ const mark =
121
+ tool.status === "running"
122
+ ? "…"
123
+ : tool.status === "ok"
124
+ ? `${GREEN}✓${RESET}`
125
+ : `${RED}✗${RESET}`;
126
+ const info = tool.status === "running" ? (tool.detail ?? "") : (tool.summary ?? "");
127
+ // 摘要/详情用正常色:最终区块里不出现浅色文字(可读性优先)
128
+ return ` ${emoji} ${tool.name} ${mark} ${info}`;
129
+ }
130
+
131
+ /**
132
+ * 把 ProgressView 展开为区块的完整行列表(不含 ANSI 定位序列,只含内容与颜色)。
133
+ * 单独导出便于单元测试;每行按终端宽度截断,避免长行折行导致行数计数失准。
134
+ */
135
+ export function buildBlockLines(
136
+ view: ProgressView,
137
+ width: number,
138
+ maxBodyLines = 30,
139
+ maxBodyChars = 12000,
140
+ ): string[] {
141
+ const clip = (s: string) => clipToWidth(s, width);
142
+ const lines: string[] = [];
143
+ lines.push(clip(buildStatusLine(view)));
144
+ for (const tool of view.tools) {
145
+ lines.push(clip(buildToolLine(tool)));
146
+ }
147
+ const body = truncateContent(view.text, maxBodyLines, maxBodyChars);
148
+ if (body.trim()) {
149
+ for (const line of body.split("\n")) {
150
+ lines.push(clip(line));
151
+ }
152
+ } else {
153
+ lines.push(`${DIM}等待 Agent 输出...${RESET}`);
154
+ }
155
+ return lines;
156
+ }
157
+
158
+ export class TerminalProgressRenderer {
159
+ private readonly out: NodeJS.WritableStream & { columns?: number };
160
+ private readonly frameMs: number;
161
+ private readonly animMs: number;
162
+ private readonly maxBodyLines: number;
163
+ private readonly maxBodyChars: number;
164
+ private view: ProgressView;
165
+ private blockLines = 0;
166
+ private dirty = false;
167
+ private timer: NodeJS.Timeout | null = null;
168
+ private ended = false;
169
+ private animFrame = 0;
170
+ private animTimer: NodeJS.Timeout | null = null;
171
+
172
+ constructor(opts: TerminalRendererOptions = {}) {
173
+ this.out = opts.out ?? process.stdout;
174
+ this.frameMs = opts.frameMs ?? 66;
175
+ this.animMs = opts.animMs ?? 300;
176
+ this.maxBodyLines = opts.maxBodyLines ?? 30;
177
+ this.maxBodyChars = opts.maxBodyChars ?? 12000;
178
+ this.view = progressView();
179
+ }
180
+
181
+ /** 开始一轮区块:隐藏光标并渲染首帧,随后启动生成中心跳动画 */
182
+ begin(view: ProgressView): void {
183
+ this.view = view;
184
+ this.out.write(HIDE_CURSOR);
185
+ this.renderNow(view);
186
+ this.startAnimation();
187
+ }
188
+
189
+ /** 标记视图已更新,按帧节流合并重绘(高频增量不闪烁) */
190
+ render(view: ProgressView): void {
191
+ this.view = view;
192
+ if (this.ended) return;
193
+ this.dirty = true;
194
+ if (this.timer) return;
195
+ this.timer = setTimeout(() => {
196
+ this.timer = null;
197
+ if (this.dirty && !this.ended) {
198
+ this.dirty = false;
199
+ this.renderNow(this.view);
200
+ }
201
+ }, this.frameMs);
202
+ }
203
+
204
+ /** 强制立即重绘(工具结果、终态等低频率但需及时反馈的事件) */
205
+ flush(): void {
206
+ if (this.timer) {
207
+ clearTimeout(this.timer);
208
+ this.timer = null;
209
+ }
210
+ if (this.ended) return;
211
+ this.dirty = false;
212
+ this.renderNow(this.view);
213
+ }
214
+
215
+ /** 结束一轮:定型终态区块(留在屏幕上),恢复光标,停止动画 */
216
+ end(view: ProgressView): void {
217
+ if (this.timer) {
218
+ clearTimeout(this.timer);
219
+ this.timer = null;
220
+ }
221
+ this.stopAnimation();
222
+ this.ended = true;
223
+ this.renderNow(view);
224
+ this.out.write(SHOW_CURSOR);
225
+ }
226
+
227
+ /** 兜底清理:清除未决定时器并恢复光标 */
228
+ dispose(): void {
229
+ if (this.timer) {
230
+ clearTimeout(this.timer);
231
+ this.timer = null;
232
+ }
233
+ this.stopAnimation();
234
+ if (!this.ended) {
235
+ this.out.write(SHOW_CURSOR);
236
+ }
237
+ }
238
+
239
+ private startAnimation(): void {
240
+ if (this.animMs <= 0 || this.animTimer) return;
241
+ this.animTimer = setInterval(() => {
242
+ this.animFrame++;
243
+ // 终态(done/stopped/error)由 end() 停止;此处兜底跳过,避免结束后还在重绘
244
+ if (this.ended || this.view.status !== "generating") return;
245
+ // 无新事件也持续重绘,点号动画让静止期(思考/工具运行中)也有动态感
246
+ this.renderNow(this.view);
247
+ }, this.animMs);
248
+ this.animTimer.unref?.();
249
+ }
250
+
251
+ private stopAnimation(): void {
252
+ if (this.animTimer) {
253
+ clearInterval(this.animTimer);
254
+ this.animTimer = null;
255
+ }
256
+ }
257
+
258
+ private renderNow(view: ProgressView): void {
259
+ const width = this.out.columns && this.out.columns > 0 ? this.out.columns : 80;
260
+ const lines = buildBlockLines(this.applyAnimation(view), width, this.maxBodyLines, this.maxBodyChars);
261
+ const newLines = lines.length;
262
+
263
+ if (this.blockLines > 0) {
264
+ // 关键:光标此刻停在区块【最后一行】行尾,回到区块第一行只需上移
265
+ // blockLines - 1 行。上移 blockLines 行会每帧多上移 1 行,把区块上方
266
+ // 的历史内容逐行"吃掉"(用户看到的"一行一行往上吃")。
267
+ this.out.write(`\x1b[${this.blockLines - 1}A`);
268
+ this.out.write("\r");
269
+ }
270
+ for (let i = 0; i < lines.length; i++) {
271
+ this.out.write(`\r\x1b[2K${lines[i]}`);
272
+ if (i < lines.length - 1) {
273
+ this.out.write("\n");
274
+ }
275
+ }
276
+ if (newLines < this.blockLines) {
277
+ // 新区块比旧区块短:清掉下方多出的行
278
+ this.out.write("\n");
279
+ for (let i = 0; i < this.blockLines - newLines; i++) {
280
+ this.out.write("\x1b[2K\n");
281
+ }
282
+ }
283
+ this.out.write("\x1b[J");
284
+ this.blockLines = newLines;
285
+ }
286
+
287
+ /** 生成中阶段在标题后叠加心跳点号动画;终态原样返回(buildBlockLines 保持纯函数可测) */
288
+ private applyAnimation(view: ProgressView): ProgressView {
289
+ if (view.status !== "generating") return view;
290
+ const base = view.headerTitle.replace(/\.\.\.\s*$/, "").trim();
291
+ const dots = ANIM_FRAMES[this.animFrame % ANIM_FRAMES.length];
292
+ return { ...view, headerTitle: `${base} ${dots}` };
293
+ }
294
+ }