chatccc 0.2.222 → 0.2.224
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/config.sample.json +40 -39
- package/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +33 -0
- package/src/__tests__/builtin-config.test.ts +1 -0
- package/src/__tests__/card-plain-text.test.ts +7 -6
- package/src/__tests__/cards.test.ts +179 -178
- package/src/__tests__/ccc-adapter.test.ts +20 -0
- package/src/__tests__/config-reload.test.ts +52 -52
- package/src/__tests__/config-sample.test.ts +41 -40
- package/src/__tests__/orchestrator.test.ts +836 -803
- package/src/__tests__/progress-reducer.test.ts +110 -0
- package/src/__tests__/session.test.ts +1183 -1173
- package/src/__tests__/terminal-renderer.test.ts +143 -0
- package/src/adapters/ccc-adapter.ts +1 -0
- package/src/builtin/cli.ts +42 -1
- package/src/builtin/index.ts +10 -0
- package/src/cards.ts +280 -275
- package/src/config.ts +203 -192
- package/src/progress/reducer.ts +108 -0
- package/src/progress/terminal-renderer.ts +190 -0
- package/src/progress/view.ts +77 -0
- package/src/session.ts +938 -937
- package/src/web-ui.ts +610 -607
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
* - end() 时把终态区块定型留在屏幕上(与飞书完成卡片留在消息流语义一致),恢复光标
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { getToolEmoji, truncateContent } from "../cards.ts";
|
|
16
|
+
import { progressView, type ProgressToolCall, type ProgressView } from "./view.ts";
|
|
17
|
+
|
|
18
|
+
const RESET = "\x1b[0m";
|
|
19
|
+
const DIM = "\x1b[2m";
|
|
20
|
+
const GREEN = "\x1b[32m";
|
|
21
|
+
const YELLOW = "\x1b[33m";
|
|
22
|
+
const RED = "\x1b[31m";
|
|
23
|
+
|
|
24
|
+
const HIDE_CURSOR = "\x1b[?25l";
|
|
25
|
+
const SHOW_CURSOR = "\x1b[?25h";
|
|
26
|
+
|
|
27
|
+
export interface TerminalRendererOptions {
|
|
28
|
+
/** 输出流,默认 process.stdout */
|
|
29
|
+
out?: NodeJS.WritableStream & { columns?: number };
|
|
30
|
+
/** 帧节流毫秒数,默认 66(约 15fps) */
|
|
31
|
+
frameMs?: number;
|
|
32
|
+
/** 正文最大行数(超出截断,保留首行+末段) */
|
|
33
|
+
maxBodyLines?: number;
|
|
34
|
+
/** 正文最大字符数 */
|
|
35
|
+
maxBodyChars?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function buildStatusLine(view: ProgressView): string {
|
|
39
|
+
switch (view.status) {
|
|
40
|
+
case "done":
|
|
41
|
+
return `${GREEN}✅ 完成${RESET}`;
|
|
42
|
+
case "stopped":
|
|
43
|
+
return `${YELLOW}⏹ 已停止${RESET}`;
|
|
44
|
+
case "error":
|
|
45
|
+
return `${RED}❌ 异常结束${RESET}`;
|
|
46
|
+
case "generating": {
|
|
47
|
+
const hint = view.showStop ? `${DIM} · Ctrl+C 停止${RESET}` : "";
|
|
48
|
+
return `⏳ ${view.headerTitle}${hint}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function buildToolLine(tool: ProgressToolCall): string {
|
|
54
|
+
const emoji = getToolEmoji(tool.name);
|
|
55
|
+
const mark =
|
|
56
|
+
tool.status === "running"
|
|
57
|
+
? `${DIM}…${RESET}`
|
|
58
|
+
: tool.status === "ok"
|
|
59
|
+
? `${GREEN}✓${RESET}`
|
|
60
|
+
: `${RED}✗${RESET}`;
|
|
61
|
+
const info = tool.status === "running" ? (tool.detail ?? "") : (tool.summary ?? "");
|
|
62
|
+
return ` ${emoji} ${tool.name} ${mark} ${DIM}${info}${RESET}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 把 ProgressView 展开为区块的完整行列表(不含 ANSI 定位序列,只含内容与颜色)。
|
|
67
|
+
* 单独导出便于单元测试;每行按终端宽度截断,避免长行折行导致行数计数失准。
|
|
68
|
+
*/
|
|
69
|
+
export function buildBlockLines(
|
|
70
|
+
view: ProgressView,
|
|
71
|
+
width: number,
|
|
72
|
+
maxBodyLines = 30,
|
|
73
|
+
maxBodyChars = 12000,
|
|
74
|
+
): string[] {
|
|
75
|
+
const clip = (s: string) => (s.length > width ? s.slice(0, Math.max(0, width - 1)) : s);
|
|
76
|
+
const lines: string[] = [];
|
|
77
|
+
lines.push(clip(buildStatusLine(view)));
|
|
78
|
+
for (const tool of view.tools) {
|
|
79
|
+
lines.push(clip(buildToolLine(tool)));
|
|
80
|
+
}
|
|
81
|
+
const body = truncateContent(view.text, maxBodyLines, maxBodyChars);
|
|
82
|
+
if (body.trim()) {
|
|
83
|
+
for (const line of body.split("\n")) {
|
|
84
|
+
lines.push(clip(line));
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
lines.push(`${DIM}等待 Agent 输出...${RESET}`);
|
|
88
|
+
}
|
|
89
|
+
return lines;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class TerminalProgressRenderer {
|
|
93
|
+
private readonly out: NodeJS.WritableStream & { columns?: number };
|
|
94
|
+
private readonly frameMs: number;
|
|
95
|
+
private readonly maxBodyLines: number;
|
|
96
|
+
private readonly maxBodyChars: number;
|
|
97
|
+
private view: ProgressView;
|
|
98
|
+
private blockLines = 0;
|
|
99
|
+
private dirty = false;
|
|
100
|
+
private timer: NodeJS.Timeout | null = null;
|
|
101
|
+
private ended = false;
|
|
102
|
+
|
|
103
|
+
constructor(opts: TerminalRendererOptions = {}) {
|
|
104
|
+
this.out = opts.out ?? process.stdout;
|
|
105
|
+
this.frameMs = opts.frameMs ?? 66;
|
|
106
|
+
this.maxBodyLines = opts.maxBodyLines ?? 30;
|
|
107
|
+
this.maxBodyChars = opts.maxBodyChars ?? 12000;
|
|
108
|
+
this.view = progressView();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 开始一轮区块:隐藏光标并渲染首帧 */
|
|
112
|
+
begin(view: ProgressView): void {
|
|
113
|
+
this.view = view;
|
|
114
|
+
this.out.write(HIDE_CURSOR);
|
|
115
|
+
this.renderNow(view);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 标记视图已更新,按帧节流合并重绘(高频增量不闪烁) */
|
|
119
|
+
render(view: ProgressView): void {
|
|
120
|
+
this.view = view;
|
|
121
|
+
if (this.ended) return;
|
|
122
|
+
this.dirty = true;
|
|
123
|
+
if (this.timer) return;
|
|
124
|
+
this.timer = setTimeout(() => {
|
|
125
|
+
this.timer = null;
|
|
126
|
+
if (this.dirty && !this.ended) {
|
|
127
|
+
this.dirty = false;
|
|
128
|
+
this.renderNow(this.view);
|
|
129
|
+
}
|
|
130
|
+
}, this.frameMs);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 强制立即重绘(工具结果、终态等低频率但需及时反馈的事件) */
|
|
134
|
+
flush(): void {
|
|
135
|
+
if (this.timer) {
|
|
136
|
+
clearTimeout(this.timer);
|
|
137
|
+
this.timer = null;
|
|
138
|
+
}
|
|
139
|
+
if (this.ended) return;
|
|
140
|
+
this.dirty = false;
|
|
141
|
+
this.renderNow(this.view);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 结束一轮:定型终态区块(留在屏幕上),恢复光标 */
|
|
145
|
+
end(view: ProgressView): void {
|
|
146
|
+
if (this.timer) {
|
|
147
|
+
clearTimeout(this.timer);
|
|
148
|
+
this.timer = null;
|
|
149
|
+
}
|
|
150
|
+
this.ended = true;
|
|
151
|
+
this.renderNow(view);
|
|
152
|
+
this.out.write(SHOW_CURSOR);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 兜底清理:清除未决定时器并恢复光标 */
|
|
156
|
+
dispose(): void {
|
|
157
|
+
if (this.timer) {
|
|
158
|
+
clearTimeout(this.timer);
|
|
159
|
+
this.timer = null;
|
|
160
|
+
}
|
|
161
|
+
if (!this.ended) {
|
|
162
|
+
this.out.write(SHOW_CURSOR);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private renderNow(view: ProgressView): void {
|
|
167
|
+
const width = this.out.columns && this.out.columns > 0 ? this.out.columns : 80;
|
|
168
|
+
const lines = buildBlockLines(view, width, this.maxBodyLines, this.maxBodyChars);
|
|
169
|
+
const newLines = lines.length;
|
|
170
|
+
|
|
171
|
+
if (this.blockLines > 0) {
|
|
172
|
+
this.out.write(`\x1b[${this.blockLines}A`);
|
|
173
|
+
}
|
|
174
|
+
for (let i = 0; i < lines.length; i++) {
|
|
175
|
+
this.out.write(`\r\x1b[2K${lines[i]}`);
|
|
176
|
+
if (i < lines.length - 1) {
|
|
177
|
+
this.out.write("\n");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (newLines < this.blockLines) {
|
|
181
|
+
// 新区块比旧区块短:清掉下方多出的行
|
|
182
|
+
this.out.write("\n");
|
|
183
|
+
for (let i = 0; i < this.blockLines - newLines; i++) {
|
|
184
|
+
this.out.write("\x1b[2K\n");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
this.out.write("\x1b[J");
|
|
188
|
+
this.blockLines = newLines;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
|
|
16
|
+
export type ProgressStatus = "generating" | "done" | "stopped" | "error";
|
|
17
|
+
|
|
18
|
+
export type ProgressToolStatus = "running" | "ok" | "error";
|
|
19
|
+
|
|
20
|
+
export interface ProgressToolCall {
|
|
21
|
+
/** 工具调用 ID(ChatEvent.tool_use.id,可能缺失) */
|
|
22
|
+
id: string;
|
|
23
|
+
/** 工具名,如 edit_file */
|
|
24
|
+
name: string;
|
|
25
|
+
status: ProgressToolStatus;
|
|
26
|
+
/** 工具输入摘要(终端折叠行展示,截断保存) */
|
|
27
|
+
detail?: string;
|
|
28
|
+
/** 工具结果摘要(成功/失败,截断保存) */
|
|
29
|
+
summary?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ProgressView {
|
|
33
|
+
/** 当前阶段状态,决定终态外观 */
|
|
34
|
+
status: ProgressStatus;
|
|
35
|
+
/** 生成中阶段的头部标题(对应飞书卡片 header title) */
|
|
36
|
+
headerTitle: string;
|
|
37
|
+
/** 卡片头部颜色模板(飞书用),终端忽略 */
|
|
38
|
+
headerTemplate: string;
|
|
39
|
+
/** 正文累积内容 */
|
|
40
|
+
text: string;
|
|
41
|
+
/** 本轮工具调用列表 */
|
|
42
|
+
tools: ProgressToolCall[];
|
|
43
|
+
/** 是否展示停止按钮 / 停止提示 */
|
|
44
|
+
showStop: boolean;
|
|
45
|
+
/** 最近一次更新时间戳 */
|
|
46
|
+
updatedAt: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ProgressViewInit {
|
|
50
|
+
status?: ProgressStatus;
|
|
51
|
+
headerTitle?: string;
|
|
52
|
+
headerTemplate?: string;
|
|
53
|
+
text?: string;
|
|
54
|
+
tools?: ProgressToolCall[];
|
|
55
|
+
showStop?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 创建 ProgressView,未提供的字段使用与飞书 buildProgressCard 一致的默认值 */
|
|
59
|
+
export function progressView(init: ProgressViewInit = {}): ProgressView {
|
|
60
|
+
return {
|
|
61
|
+
status: init.status ?? "generating",
|
|
62
|
+
headerTitle: init.headerTitle ?? "生成中...",
|
|
63
|
+
headerTemplate: init.headerTemplate ?? "blue",
|
|
64
|
+
text: init.text ?? "",
|
|
65
|
+
tools: init.tools ?? [],
|
|
66
|
+
showStop: init.showStop ?? true,
|
|
67
|
+
updatedAt: Date.now(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 只读浅拷贝并覆盖部分字段,返回新视图(reducer 的不可变更新用) */
|
|
72
|
+
export function withProgressView(
|
|
73
|
+
prev: ProgressView,
|
|
74
|
+
patch: Partial<ProgressView>,
|
|
75
|
+
): ProgressView {
|
|
76
|
+
return { ...prev, ...patch, updatedAt: Date.now() };
|
|
77
|
+
}
|