chatccc 0.2.221 → 0.2.223
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/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +40 -0
- package/src/__tests__/builtin-skills.test.ts +141 -0
- package/src/__tests__/card-plain-text.test.ts +7 -6
- package/src/__tests__/cards.test.ts +179 -178
- package/src/__tests__/progress-reducer.test.ts +110 -0
- package/src/__tests__/terminal-renderer.test.ts +143 -0
- package/src/builtin/cli.ts +38 -1
- package/src/builtin/index.ts +12 -0
- package/src/builtin/skills.ts +108 -0
- package/src/cards.ts +280 -275
- 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 +935 -937
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* progress/reducer.ts — ChatEvent 事件流 → ProgressView 的增量合并
|
|
3
|
+
*
|
|
4
|
+
* 这是飞书与终端共享的唯一一份"事件 → 过程视图"公共逻辑:
|
|
5
|
+
* 终端 REPL 直接消费 ChatSession.chat() 的事件流,逐条 reduce;
|
|
6
|
+
* 飞书侧 display loop 若未来接入事件流,同样使用本 reducer,保证两端
|
|
7
|
+
* 看到的过程状态(正文累积、工具调用、终态判定)完全一致。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ChatEvent } from "../builtin/index.ts";
|
|
11
|
+
import {
|
|
12
|
+
withProgressView,
|
|
13
|
+
type ProgressToolCall,
|
|
14
|
+
type ProgressView,
|
|
15
|
+
} from "./view.ts";
|
|
16
|
+
|
|
17
|
+
/** 工具调用 input 单行摘要(终端折叠行展示用) */
|
|
18
|
+
export function summarizeToolInput(input: unknown, maxChars = 120): string {
|
|
19
|
+
let raw: string;
|
|
20
|
+
if (typeof input === "string") {
|
|
21
|
+
raw = input;
|
|
22
|
+
} else {
|
|
23
|
+
try {
|
|
24
|
+
raw = JSON.stringify(input);
|
|
25
|
+
} catch {
|
|
26
|
+
raw = String(input);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const oneLine = raw.replace(/\s+/g, " ").trim();
|
|
30
|
+
return oneLine.length > maxChars ? oneLine.slice(0, maxChars) + "…" : oneLine;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 工具结果单行摘要(成功/失败各取首行) */
|
|
34
|
+
export function summarizeToolResult(content: unknown, maxChars = 120): string {
|
|
35
|
+
let raw: string;
|
|
36
|
+
if (typeof content === "string") {
|
|
37
|
+
raw = content;
|
|
38
|
+
} else if (content instanceof Error) {
|
|
39
|
+
raw = content.message;
|
|
40
|
+
} else {
|
|
41
|
+
try {
|
|
42
|
+
raw = JSON.stringify(content);
|
|
43
|
+
} catch {
|
|
44
|
+
raw = String(content);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const firstLine = raw.split("\n")[0] ?? "";
|
|
48
|
+
return firstLine.length > maxChars ? firstLine.slice(0, maxChars) + "…" : firstLine;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 把一条 ChatEvent 合并进 ProgressView,返回新视图(不可变更新)。
|
|
53
|
+
* 未识别的/不影响展示的事件(如 compact)返回原视图。
|
|
54
|
+
*/
|
|
55
|
+
export function reduceProgress(prev: ProgressView, event: ChatEvent): ProgressView {
|
|
56
|
+
switch (event.type) {
|
|
57
|
+
case "text":
|
|
58
|
+
// accumulated 是全文累积,直接全量替换,天然幂等
|
|
59
|
+
return withProgressView(prev, { text: event.accumulated });
|
|
60
|
+
|
|
61
|
+
case "tool_use": {
|
|
62
|
+
const tool: ProgressToolCall = {
|
|
63
|
+
id: event.id ?? `tool-${prev.tools.length + 1}`,
|
|
64
|
+
name: event.name,
|
|
65
|
+
status: "running",
|
|
66
|
+
detail: summarizeToolInput(event.input),
|
|
67
|
+
};
|
|
68
|
+
return withProgressView(prev, { tools: [...prev.tools, tool] });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
case "tool_result": {
|
|
72
|
+
const nextStatus = event.is_error ? ("error" as const) : ("ok" as const);
|
|
73
|
+
const summary = summarizeToolResult(event.content);
|
|
74
|
+
let matched = false;
|
|
75
|
+
const tools = prev.tools.map((t) => {
|
|
76
|
+
if (matched || t.id !== event.tool_use_id) return t;
|
|
77
|
+
matched = true;
|
|
78
|
+
return { ...t, status: nextStatus, summary };
|
|
79
|
+
});
|
|
80
|
+
if (!matched) {
|
|
81
|
+
// id 缺失或失配:兜底更新最后一个 running 工具,避免状态悬挂
|
|
82
|
+
const lastRunning = [...prev.tools].reverse().findIndex((t) => t.status === "running");
|
|
83
|
+
if (lastRunning >= 0) {
|
|
84
|
+
const idx = prev.tools.length - 1 - lastRunning;
|
|
85
|
+
const updated = prev.tools.map((t, i) =>
|
|
86
|
+
i === idx ? { ...t, status: nextStatus, summary } : t,
|
|
87
|
+
);
|
|
88
|
+
return withProgressView(prev, { tools: updated });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return withProgressView(prev, { tools });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
case "done":
|
|
95
|
+
return withProgressView(prev, {
|
|
96
|
+
status: "done",
|
|
97
|
+
showStop: false,
|
|
98
|
+
text: event.text,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
case "error":
|
|
102
|
+
return withProgressView(prev, { status: "error", showStop: false });
|
|
103
|
+
|
|
104
|
+
case "compact":
|
|
105
|
+
// 旧上下文压缩不影响当前过程展示
|
|
106
|
+
return prev;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -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
|
+
}
|