@uncarved/jade 0.1.0 → 0.1.2
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 +198 -188
- package/dist/distro.d.ts +12 -1
- package/dist/distro.js +37 -2
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +159 -29
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugins/policy.d.ts +41 -9
- package/dist/plugins/policy.js +367 -58
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +10 -0
- package/dist/plugins/prompts.js +29 -2
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.js +42 -7
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +16 -0
- package/dist/plugins/review.js +92 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/provider.d.ts +62 -3
- package/dist/provider.js +150 -7
- package/dist/provider.js.map +1 -1
- package/dist/sessions.d.ts +9 -0
- package/dist/sessions.js +14 -1
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +5 -1
- package/dist/sys.js +3 -2
- package/dist/sys.js.map +1 -1
- package/dist/tools/screenshot.d.ts +15 -0
- package/dist/tools/screenshot.js +162 -0
- package/dist/tools/screenshot.js.map +1 -0
- package/dist/tools/shell.d.ts +6 -0
- package/dist/tools/shell.js +26 -7
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +4 -0
- package/dist/tools/task.js +4 -1
- package/dist/tools/task.js.map +1 -1
- package/dist/ui/configure.js +121 -28
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/markdown.d.ts +17 -0
- package/dist/ui/markdown.js +204 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +2 -0
- package/dist/ui/render.js +10 -0
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/screen.d.ts +8 -0
- package/dist/ui/screen.js +3 -0
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.js +85 -13
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +17 -0
- package/dist/ui/tui.js +367 -93
- package/dist/ui/tui.js.map +1 -1
- package/package.json +5 -5
- package/src/distro.ts +46 -4
- package/src/index.ts +2 -0
- package/src/main.ts +156 -31
- package/src/mcp.ts +1 -1
- package/src/plugins/policy.ts +366 -51
- package/src/plugins/prompts.ts +36 -2
- package/src/plugins/repeats.ts +42 -8
- package/src/plugins/review.ts +109 -0
- package/src/provider.ts +170 -11
- package/src/sessions.ts +14 -1
- package/src/sys.ts +5 -2
- package/src/tools/screenshot.ts +171 -0
- package/src/tools/shell.ts +25 -7
- package/src/tools/task.ts +5 -1
- package/src/ui/configure.ts +127 -29
- package/src/ui/markdown.ts +219 -0
- package/src/ui/render.ts +9 -0
- package/src/ui/screen.ts +5 -0
- package/src/ui/transcript.ts +84 -16
- package/src/ui/tui.tsx +480 -142
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// 终端里显示模型回复的 Markdown:标题、粗体、斜体、行内代码、链接、列表、引用、分隔线、代码块和表格。
|
|
2
|
+
// 回复是流式到达的,按完整的行逐行处理:代码块要记住是否在块里,表格要攒齐所有行才能算列宽,
|
|
3
|
+
// 所以做成有状态的 markdownStream。表格和分隔线跟终端宽度有关,交出去的是按宽度现排的函数。
|
|
4
|
+
|
|
5
|
+
import stringWidth from "string-width";
|
|
6
|
+
import wrapAnsi from "wrap-ansi";
|
|
7
|
+
import { bold, cyan, dim, italic, strike, underline } from "./screen.ts";
|
|
8
|
+
|
|
9
|
+
/** 排好版的一行,或者按显示宽度现排的若干行(表格、分隔线) */
|
|
10
|
+
export type MdLine = string | { render(width: number): string[] };
|
|
11
|
+
|
|
12
|
+
export interface MarkdownStream {
|
|
13
|
+
/** 送进一个完整的行,返回可以显示的行;表格的行先攒着,等表格结束 */
|
|
14
|
+
push(line: string): MdLine[];
|
|
15
|
+
/** 回复结束:交出攒着的内容,恢复初始状态 */
|
|
16
|
+
flush(): MdLine[];
|
|
17
|
+
/** 攒着还没交出的内容,流式输出时先显示在底部 */
|
|
18
|
+
pending(): MdLine[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Align = "left" | "center" | "right";
|
|
22
|
+
|
|
23
|
+
const FENCE = /^\s*(`{3,}|~{3,})\s*([^`\s]*)/;
|
|
24
|
+
const RULE = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
|
|
25
|
+
const DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
|
|
26
|
+
|
|
27
|
+
export function markdownStream(): MarkdownStream {
|
|
28
|
+
/** 所在代码块的开头标记(``` 或 ~~~),不在代码块里时为 undefined */
|
|
29
|
+
let fence: string | undefined;
|
|
30
|
+
/** 带 | 的一行:下一行是分隔行时它是表头,否则是普通的一行 */
|
|
31
|
+
let candidate: string | undefined;
|
|
32
|
+
let table: { align: Align[]; rows: string[][] } | undefined;
|
|
33
|
+
|
|
34
|
+
const endTable = (): MdLine[] => {
|
|
35
|
+
const out: MdLine[] = [];
|
|
36
|
+
if (table) out.push(tableLine(table.rows, table.align));
|
|
37
|
+
else if (candidate !== undefined) out.push(renderLine(candidate));
|
|
38
|
+
table = undefined;
|
|
39
|
+
candidate = undefined;
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function push(line: string): MdLine[] {
|
|
44
|
+
if (fence) {
|
|
45
|
+
const m = line.match(FENCE);
|
|
46
|
+
if (m && m[1]![0] === fence[0] && m[1]!.length >= fence.length && !m[2]) {
|
|
47
|
+
fence = undefined;
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
return [` ${line}`];
|
|
51
|
+
}
|
|
52
|
+
if (table) {
|
|
53
|
+
if (line.includes("|") && line.trim()) {
|
|
54
|
+
table.rows.push(cells(line));
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
return [...endTable(), ...push(line)];
|
|
58
|
+
}
|
|
59
|
+
if (candidate !== undefined) {
|
|
60
|
+
if (DELIMITER.test(line) && line.includes("-")) {
|
|
61
|
+
const header = cells(candidate);
|
|
62
|
+
const align = cells(line).map((c): Align => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : "left"));
|
|
63
|
+
while (align.length < header.length) align.push("left");
|
|
64
|
+
table = { align, rows: [header] };
|
|
65
|
+
candidate = undefined;
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
return [...endTable(), ...push(line)];
|
|
69
|
+
}
|
|
70
|
+
const m = line.match(FENCE);
|
|
71
|
+
if (m) {
|
|
72
|
+
fence = m[1]!;
|
|
73
|
+
return m[2] ? [dim(` ${m[2]}`)] : [];
|
|
74
|
+
}
|
|
75
|
+
if (splitCells(line).length > 1) {
|
|
76
|
+
candidate = line;
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return [renderLine(line)];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
push,
|
|
84
|
+
flush() {
|
|
85
|
+
const out = endTable();
|
|
86
|
+
fence = undefined;
|
|
87
|
+
return out;
|
|
88
|
+
},
|
|
89
|
+
pending: () => (table ? [tableLine(table.rows, table.align)] : candidate !== undefined ? [renderLine(candidate)] : []),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 整段 Markdown 排好版(测试和一次性显示用) */
|
|
94
|
+
export function renderMarkdown(text: string, width: number): string[] {
|
|
95
|
+
const md = markdownStream();
|
|
96
|
+
const out = [...text.split("\n").flatMap((l) => md.push(l)), ...md.flush()];
|
|
97
|
+
return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width)));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 代码块和表格以外的一行 */
|
|
101
|
+
function renderLine(line: string): MdLine {
|
|
102
|
+
if (RULE.test(line)) return { render: (width) => [dim("─".repeat(Math.max(1, Math.min(width, 80))))] };
|
|
103
|
+
const heading = line.match(/^\s{0,3}(#{1,6})\s+(.*?)(\s+#+)?\s*$/);
|
|
104
|
+
if (heading) {
|
|
105
|
+
const text = inline(heading[2]!);
|
|
106
|
+
return heading[1]!.length === 1 ? bold(underline(text)) : bold(text);
|
|
107
|
+
}
|
|
108
|
+
const quote = line.match(/^\s*>\s?(.*)$/);
|
|
109
|
+
if (quote) return dim("│ ") + inline(quote[1]!);
|
|
110
|
+
const item = line.match(/^(\s*)([-*+])\s+(\[[ xX]\]\s+)?(.*)$/);
|
|
111
|
+
if (item) {
|
|
112
|
+
const box = item[3] ? (item[3][1] === " " ? "☐ " : "☑ ") : "";
|
|
113
|
+
return `${item[1]}${box ? "" : cyan("• ")}${box}${inline(item[4]!)}`;
|
|
114
|
+
}
|
|
115
|
+
const ordered = line.match(/^(\s*)(\d+[.)])\s+(.*)$/);
|
|
116
|
+
if (ordered) return `${ordered[1]}${cyan(ordered[2]!)} ${inline(ordered[3]!)}`;
|
|
117
|
+
return inline(line);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 行内格式:`代码`、**粗体**、*斜体*、~~删除线~~、[链接](url)、反斜杠转义。代码里的内容原样显示 */
|
|
121
|
+
export function inline(text: string): string {
|
|
122
|
+
return text
|
|
123
|
+
.split(/(`+[^`]*?`+)/)
|
|
124
|
+
.map((part, i) => {
|
|
125
|
+
if (i % 2 === 1) return cyan(part.replace(/^`+ ?|`+$/g, "").replace(/ $/, ""));
|
|
126
|
+
return part
|
|
127
|
+
.replace(/\\([\\`*_{}[\]()#+\-.!|~<>])/g, (_, c: string) => `\0${c.charCodeAt(0)}\0`)
|
|
128
|
+
.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label: string, url: string) =>
|
|
129
|
+
label === url ? underline(url) : `${underline(label)}${dim(` (${url})`)}`,
|
|
130
|
+
)
|
|
131
|
+
.replace(/<(https?:\/\/[^>\s]+)>/g, (_, url: string) => underline(url))
|
|
132
|
+
.replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*|(?<![\w])__(?=\S)(.+?)(?<=\S)__(?![\w])/g, (_, a?: string, b?: string) => bold(a ?? b!))
|
|
133
|
+
.replace(/(?<![*\w])\*(?=[^\s*])(.+?)(?<=[^\s*])\*(?![*\w])|(?<![_\w])_(?=[^\s_])(.+?)(?<=[^\s_])_(?![_\w])/g, (_, a?: string, b?: string) => italic(a ?? b!))
|
|
134
|
+
.replace(/~~(?=\S)(.+?)(?<=\S)~~/g, (_, s: string) => strike(s))
|
|
135
|
+
.replace(/\0(\d+)\0/g, (_, c: string) => String.fromCharCode(Number(c)));
|
|
136
|
+
})
|
|
137
|
+
.join("");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 按没转义、不在行内代码里的 | 拆开一行 */
|
|
141
|
+
function splitCells(line: string): string[] {
|
|
142
|
+
const out: string[] = [];
|
|
143
|
+
let cell = "";
|
|
144
|
+
let code = false;
|
|
145
|
+
for (let i = 0; i < line.length; i++) {
|
|
146
|
+
const c = line[i]!;
|
|
147
|
+
if (c === "\\" && line[i + 1] === "|") {
|
|
148
|
+
cell += "|";
|
|
149
|
+
i++;
|
|
150
|
+
} else if (c === "`") {
|
|
151
|
+
code = !code;
|
|
152
|
+
cell += c;
|
|
153
|
+
} else if (c === "|" && !code) {
|
|
154
|
+
out.push(cell);
|
|
155
|
+
cell = "";
|
|
156
|
+
} else cell += c;
|
|
157
|
+
}
|
|
158
|
+
out.push(cell);
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** 表格一行的各个单元格,去掉两端的 | */
|
|
163
|
+
function cells(line: string): string[] {
|
|
164
|
+
const parts = splitCells(line.trim());
|
|
165
|
+
if (parts.length > 1 && parts[0]!.trim() === "") parts.shift();
|
|
166
|
+
if (parts.length > 1 && parts.at(-1)!.trim() === "") parts.pop();
|
|
167
|
+
return parts.map((c) => c.trim());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function tableLine(rows: string[][], align: Align[]): MdLine {
|
|
171
|
+
const shown = rows.map((r, i) => r.map((c) => (i === 0 ? bold(inline(c)) : inline(c))));
|
|
172
|
+
return { render: (width) => renderTable(shown, align, width) };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 画带边框的表格。放不下时按比例收窄较宽的列、单元格内折行;
|
|
177
|
+
* 窄到每列连 3 列都分不到时,改成每行一段「表头: 值」
|
|
178
|
+
*/
|
|
179
|
+
function renderTable(rows: string[][], align: Align[], width: number): string[] {
|
|
180
|
+
const n = Math.max(...rows.map((r) => r.length));
|
|
181
|
+
const grid = rows.map((r) => Array.from({ length: n }, (_, i) => r[i] ?? ""));
|
|
182
|
+
const natural = Array.from({ length: n }, (_, i) => Math.max(1, ...grid.map((r) => stringWidth(r[i]!))));
|
|
183
|
+
const room = width - (3 * n + 1);
|
|
184
|
+
if (room < 3 * n) {
|
|
185
|
+
const [head, ...body] = grid;
|
|
186
|
+
return body.flatMap((r, k) => [
|
|
187
|
+
...(k ? [""] : []),
|
|
188
|
+
...r.flatMap((c, i) => wrapAnsi(`${head![i]}${dim(":")} ${c}`, Math.max(1, width), { hard: true }).split("\n")),
|
|
189
|
+
]);
|
|
190
|
+
}
|
|
191
|
+
// 从最窄的列开始,能放下的列按原宽,放不下的平分剩下的宽度
|
|
192
|
+
const widths = [...natural];
|
|
193
|
+
if (natural.reduce((a, b) => a + b, 0) > room) {
|
|
194
|
+
let left = room;
|
|
195
|
+
let open = n;
|
|
196
|
+
for (const i of [...natural.keys()].sort((a, b) => natural[a]! - natural[b]!)) {
|
|
197
|
+
const share = Math.floor(left / open);
|
|
198
|
+
widths[i] = Math.min(natural[i]!, share);
|
|
199
|
+
left -= widths[i]!;
|
|
200
|
+
open--;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const pad = (s: string, w: number, a: Align) => {
|
|
204
|
+
const gap = w - stringWidth(s);
|
|
205
|
+
if (a === "right") return " ".repeat(gap) + s;
|
|
206
|
+
if (a === "center") return " ".repeat(Math.floor(gap / 2)) + s + " ".repeat(Math.ceil(gap / 2));
|
|
207
|
+
return s + " ".repeat(gap);
|
|
208
|
+
};
|
|
209
|
+
const border = (l: string, m: string, r: string) => dim(l + widths.map((w) => "─".repeat(w + 2)).join(m) + r);
|
|
210
|
+
const row = (r: string[]) => {
|
|
211
|
+
const parts = r.map((c, i) => wrapAnsi(c, widths[i]!, { hard: true, trim: true }).split("\n"));
|
|
212
|
+
const height = Math.max(...parts.map((p) => p.length));
|
|
213
|
+
return Array.from({ length: height }, (_, k) =>
|
|
214
|
+
dim("│ ") + parts.map((p, i) => pad(p[k] ?? "", widths[i]!, align[i] ?? "left")).join(dim(" │ ")) + dim(" │"),
|
|
215
|
+
);
|
|
216
|
+
};
|
|
217
|
+
const [head, ...body] = grid;
|
|
218
|
+
return [border("┌", "┬", "┐"), ...row(head!), border("├", "┼", "┤"), ...body.flatMap(row), border("└", "┴", "┘")];
|
|
219
|
+
}
|
package/src/ui/render.ts
CHANGED
|
@@ -33,6 +33,15 @@ export function summarize(cwd: string, name: string, input: unknown): string {
|
|
|
33
33
|
return where || clip(JSON.stringify(input), 120);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/** summarize 省略了内容时(目前只有多行或很长的 bash 命令),调用行展开后先显示完整的参数 */
|
|
37
|
+
export function callDetails(name: string, input: unknown): string | undefined {
|
|
38
|
+
if (name !== "bash") return undefined;
|
|
39
|
+
const command = String(((input ?? {}) as Record<string, unknown>).command ?? "");
|
|
40
|
+
const lines = command.split("\n");
|
|
41
|
+
if (lines.length === 1 && lines[0]!.length <= 120) return undefined;
|
|
42
|
+
return detailsOf(lines.map((l, i) => `${i === 0 ? "$" : " "} ${l}`), dim, 200);
|
|
43
|
+
}
|
|
44
|
+
|
|
36
45
|
/**
|
|
37
46
|
* 审批时显示的内容:修改显示成带行号和上下文的 diff(读出文件、按工具的规则算出修改后的内容),
|
|
38
47
|
* 命令显示命令本身。文件读不了、替换不成立时退回到只显示参数里的新旧文本
|
package/src/ui/screen.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface StatusInfo {
|
|
|
23
23
|
spent?: { input: number; output: number };
|
|
24
24
|
/** 上一次请求的输入中命中缓存的比例(0–1);服务不报告缓存用量时为 undefined */
|
|
25
25
|
cache?: number;
|
|
26
|
+
/** 输出速度(token/秒):live 为 true 时是正在输出的这次回复按字数估的,否则是上一次回复按服务端报的用量算的 */
|
|
27
|
+
speed?: { tps: number; live?: boolean };
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface CommandInfo {
|
|
@@ -79,6 +81,9 @@ export const green = paint("32");
|
|
|
79
81
|
export const yellow = paint("33");
|
|
80
82
|
export const cyan = paint("36");
|
|
81
83
|
export const bold = paint("1");
|
|
84
|
+
export const italic = paint("3");
|
|
85
|
+
export const underline = paint("4");
|
|
86
|
+
export const strike = paint("9");
|
|
82
87
|
|
|
83
88
|
// ---------- 纯文本实现 ----------
|
|
84
89
|
|
package/src/ui/transcript.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { readAll, type Block, type EventDataMap, type LogEvent, type LogStore, t
|
|
|
7
7
|
import type { ShellProgress } from "../tools/shell.ts";
|
|
8
8
|
import type { TaskProgress } from "../tools/task.ts";
|
|
9
9
|
import { renderTodos, type Todo } from "../tools/todo.ts";
|
|
10
|
-
import { clip, detailsOf, summarize } from "./render.ts";
|
|
10
|
+
import { callDetails, clip, detailsOf, summarize } from "./render.ts";
|
|
11
11
|
import { bold, cyan, dim, green, red, yellow, type Screen } from "./screen.ts";
|
|
12
12
|
|
|
13
13
|
export type Notice = Parameters<Parameters<Runtime["subscribe"]>[0]>[0];
|
|
@@ -57,10 +57,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
57
57
|
for (const k of Object.keys(usage) as (keyof Usage)[]) usage[k] += u?.[k] ?? 0;
|
|
58
58
|
if (u) screen.status({ spent: { input: usage.inputTokens, output: usage.outputTokens } });
|
|
59
59
|
};
|
|
60
|
-
/**
|
|
60
|
+
/** 一个事件带来的用量:模型回复、压缩时的总结调用、auto 模式的审查;子 Agent 的用量在它自己的日志里 */
|
|
61
61
|
async function countUsage(e: LogEvent) {
|
|
62
62
|
if (e.type === "model.responded") addUsage((e as LogEvent<"model.responded">).data.usage);
|
|
63
|
-
else if (e.type === "jade/compact/compacted") addUsage((e.data as { usage?: Usage }).usage);
|
|
63
|
+
else if (e.type === "jade/compact/compacted" || e.type === "jade/policy/reviewed") addUsage((e.data as { usage?: Usage }).usage);
|
|
64
64
|
else if (e.type === "tool.completed") {
|
|
65
65
|
const child = (e as LogEvent<"tool.completed">).data.child;
|
|
66
66
|
if (child) for (const c of await readAll(store, child.sessionId)) await countUsage(c);
|
|
@@ -79,6 +79,33 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* 输出速度:从第一个增量开始计时(不算等待首个 token 的时间),输出中按字数估 token 数,每半秒更新一次状态栏;
|
|
84
|
+
* 回复结束后用服务端报的输出 token 数算准确值。工具参数的增量也算输出,只调用工具的回复同样从第一个增量开始计时。
|
|
85
|
+
* 一个增量都没收到的回复(不支持流式的服务)不更新速度:从发出请求开始算会把等待首个 token 的时间也算进去,数字偏低
|
|
86
|
+
*/
|
|
87
|
+
const speed = { first: 0, estimate: 0, shown: 0 };
|
|
88
|
+
const startSpeed = () => {
|
|
89
|
+
speed.first = 0;
|
|
90
|
+
speed.estimate = 0;
|
|
91
|
+
};
|
|
92
|
+
const trackSpeed = (text: string) => {
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
if (!speed.first) speed.first = now;
|
|
95
|
+
const wide = text.match(/[^\x00-\x7f]/g)?.length ?? 0;
|
|
96
|
+
speed.estimate += (text.length - wide) / 4 + wide;
|
|
97
|
+
if (now - speed.shown < 500 || now - speed.first < 1000) return;
|
|
98
|
+
speed.shown = now;
|
|
99
|
+
screen.status({ speed: { tps: speed.estimate / ((now - speed.first) / 1000), live: true } });
|
|
100
|
+
};
|
|
101
|
+
const finishSpeed = (u: Usage | undefined) => {
|
|
102
|
+
const seconds = (Date.now() - speed.first) / 1000;
|
|
103
|
+
const tokens = u?.outputTokens ?? Math.round(speed.estimate);
|
|
104
|
+
if (speed.first && tokens > 0 && seconds >= 0.2) screen.status({ speed: { tps: tokens / seconds } });
|
|
105
|
+
speed.first = 0;
|
|
106
|
+
speed.estimate = 0;
|
|
107
|
+
};
|
|
108
|
+
|
|
82
109
|
const toolNames = new Map<string, string>();
|
|
83
110
|
/** 正在重新显示恢复的会话里已有的内容:只输出,不显示进行中的状态 */
|
|
84
111
|
let replaying = false;
|
|
@@ -92,6 +119,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
92
119
|
const running = new Set<string>();
|
|
93
120
|
/** 子 Agent 调用过的工具,结束时放进 task 结果的折叠内容里 */
|
|
94
121
|
const steps = new Map<string, string[]>();
|
|
122
|
+
/** 正在运行的子 Agent 的标签,显示在它的进度行开头 */
|
|
123
|
+
const tasks = new Map<string, string>();
|
|
124
|
+
/** 调用行省略了的参数(例如很长的命令),结果出来后放在折叠内容的最前面 */
|
|
125
|
+
const calls = new Map<string, string>();
|
|
95
126
|
const progress = (id: string, line: string, o?: { output?: boolean }) => {
|
|
96
127
|
running.add(id);
|
|
97
128
|
screen.progress(id, line, o);
|
|
@@ -116,6 +147,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
116
147
|
for (const id of running) screen.progress(id, undefined);
|
|
117
148
|
running.clear();
|
|
118
149
|
steps.clear();
|
|
150
|
+
tasks.clear();
|
|
151
|
+
calls.clear();
|
|
119
152
|
self.lastEnd = undefined;
|
|
120
153
|
},
|
|
121
154
|
};
|
|
@@ -130,6 +163,13 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
130
163
|
screen.log(yellow("[output above discarded; retrying]"));
|
|
131
164
|
}
|
|
132
165
|
streamed = false;
|
|
166
|
+
startSpeed();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
trackSpeed(n.delta.text);
|
|
170
|
+
// 工具参数只用来计时,不显示;也不算已经输出了内容(重试时不用标记作废,回复结束时照常打印没流式输出的正文)
|
|
171
|
+
if (n.delta.kind === "tool_input") {
|
|
172
|
+
screen.busy("Writing");
|
|
133
173
|
return;
|
|
134
174
|
}
|
|
135
175
|
streamed = true;
|
|
@@ -158,13 +198,22 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
158
198
|
|
|
159
199
|
function renderProgress(toolUseId: string, p: ShellProgress | TaskProgress) {
|
|
160
200
|
if (typeof p !== "object" || p === null) return;
|
|
161
|
-
|
|
201
|
+
// 每个子 Agent 只有一行进度:标签、调用过几个工具、正在做什么。并行的几个靠标签分清
|
|
202
|
+
const taskLine = (what: string) => {
|
|
203
|
+
const n = steps.get(toolUseId)?.length ?? 0;
|
|
204
|
+
progress(toolUseId, dim(` ↳ ${tasks.get(toolUseId) ?? "sub-agent"}${n ? ` (${n} tool call${n > 1 ? "s" : ""})` : ""} · ${what}`));
|
|
205
|
+
};
|
|
206
|
+
if (p.kind === "task-start") {
|
|
207
|
+
tasks.set(toolUseId, p.description);
|
|
208
|
+
return taskLine("starting");
|
|
209
|
+
}
|
|
210
|
+
if (p.kind === "task-model") return taskLine("thinking");
|
|
162
211
|
if (p.kind === "task-tool") {
|
|
163
212
|
// 要用户批准的调用在审批时显示
|
|
164
213
|
if (p.decision === "ask") return;
|
|
165
214
|
const step = `${p.decision === "deny" ? "✗" : "·"} ${p.name}(${summarize(cwd, p.name, p.input)})`;
|
|
166
215
|
steps.set(toolUseId, [...(steps.get(toolUseId) ?? []), step]);
|
|
167
|
-
return
|
|
216
|
+
return taskLine(step);
|
|
168
217
|
}
|
|
169
218
|
// 第三方工具的进度格式 jade 不认识,不显示
|
|
170
219
|
if (typeof (p as { text?: unknown }).text !== "string") return;
|
|
@@ -175,16 +224,18 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
175
224
|
switch (e.type) {
|
|
176
225
|
case "model.requested":
|
|
177
226
|
activity("Waiting for the model");
|
|
227
|
+
if (!replaying) startSpeed();
|
|
178
228
|
return;
|
|
179
229
|
case "model.responded": {
|
|
180
230
|
const d = e.data as EventDataMap["model.responded"];
|
|
181
231
|
void countUsage(e);
|
|
182
232
|
showContext(d.usage);
|
|
233
|
+
if (!replaying) finishSpeed(d.usage);
|
|
183
234
|
for (const b of d.content) if (b.kind === "tool_use") toolNames.set(b.id, b.name);
|
|
184
|
-
// 不支持流式的 provider
|
|
235
|
+
// 不支持流式的 provider、重新显示历史:一次送出最终文本,和流式输出一样排版
|
|
185
236
|
if (!streamed) {
|
|
186
237
|
const text = textOf(d.content).trim();
|
|
187
|
-
if (text) screen.
|
|
238
|
+
if (text) screen.stream("text", text);
|
|
188
239
|
}
|
|
189
240
|
streamed = false;
|
|
190
241
|
screen.endStream();
|
|
@@ -193,9 +244,11 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
193
244
|
case "tool.decided": {
|
|
194
245
|
const d = e.data as EventDataMap["tool.decided"];
|
|
195
246
|
const name = toolNames.get(d.toolUseId) ?? "?";
|
|
247
|
+
const full = callDetails(name, d.input);
|
|
248
|
+
if (full) calls.set(d.toolUseId, full);
|
|
196
249
|
if (d.decision === "ask") return;
|
|
197
250
|
const head = `${bold(name)}(${summarize(cwd, name, d.input)})`;
|
|
198
|
-
if (d.decision === "deny") screen.log(red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), { id: d.toolUseId });
|
|
251
|
+
if (d.decision === "deny") screen.log(red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), { id: d.toolUseId, details: full });
|
|
199
252
|
else {
|
|
200
253
|
activity(`Running ${name}`);
|
|
201
254
|
if (name === "todo_write") {
|
|
@@ -205,7 +258,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
205
258
|
screen.log(renderTodos((d.input as { todos?: Todo[] }).todos ?? []).replace(/^/gm, " "));
|
|
206
259
|
} else if (d.decidedBy !== "user" || replaying) {
|
|
207
260
|
// 用户批准的调用在审批时已经显示过;重新显示历史时没有审批这一步
|
|
208
|
-
|
|
261
|
+
// auto 模式下审查放行的带着理由
|
|
262
|
+
screen.log(cyan("● ") + head + (d.reason ? dim(` — ${clip(d.reason, 160)}`) : ""), { id: d.toolUseId, details: full });
|
|
209
263
|
}
|
|
210
264
|
}
|
|
211
265
|
return;
|
|
@@ -217,6 +271,9 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
217
271
|
doneProgress(d.toolUseId);
|
|
218
272
|
const sub = steps.get(d.toolUseId) ?? [];
|
|
219
273
|
steps.delete(d.toolUseId);
|
|
274
|
+
tasks.delete(d.toolUseId);
|
|
275
|
+
const call = calls.get(d.toolUseId);
|
|
276
|
+
calls.delete(d.toolUseId);
|
|
220
277
|
if (quiet.delete(d.toolUseId) && !d.isError) return;
|
|
221
278
|
const text = textOf(d.content).trim();
|
|
222
279
|
const lines = text.split("\n");
|
|
@@ -224,12 +281,14 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
224
281
|
const shown = d.isError ? 4 : 1;
|
|
225
282
|
const width = d.isError ? 200 : 160;
|
|
226
283
|
const hidden = sub.length > 0 || lines.length > shown || lines.some((l) => l.length > width);
|
|
227
|
-
const details =
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
284
|
+
const details =
|
|
285
|
+
hidden || call
|
|
286
|
+
? [
|
|
287
|
+
...(call ? [call] : []),
|
|
288
|
+
...(sub.length ? [dim(` sub-agent ran ${sub.length} tool call${sub.length > 1 ? "s" : ""}:`), ...sub.map((l) => dim(` ${l}`))] : []),
|
|
289
|
+
...(hidden ? [detailsOf(lines, d.isError ? red : (s) => s, MAX_DETAIL_LINES)] : []),
|
|
290
|
+
].join("\n")
|
|
291
|
+
: undefined;
|
|
233
292
|
if (d.isError) screen.amend(d.toolUseId, red(`⎿ ${lines.slice(0, shown).map((l) => clip(l, width)).join("\n ")}`), { details });
|
|
234
293
|
else screen.amend(d.toolUseId, dim(`⎿ ${clip(lines[0] ?? "", width)}${lines.length > 1 ? ` (+${lines.length - 1} lines)` : ""}`), { details });
|
|
235
294
|
return;
|
|
@@ -237,6 +296,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
237
296
|
case "tool.interrupted": {
|
|
238
297
|
const d = e.data as EventDataMap["tool.interrupted"];
|
|
239
298
|
doneProgress(d.toolUseId);
|
|
299
|
+
tasks.delete(d.toolUseId);
|
|
300
|
+
calls.delete(d.toolUseId);
|
|
240
301
|
screen.amend(d.toolUseId, yellow("⎿ interrupted before finishing; not retried"));
|
|
241
302
|
return;
|
|
242
303
|
}
|
|
@@ -279,6 +340,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
279
340
|
}
|
|
280
341
|
return;
|
|
281
342
|
}
|
|
343
|
+
case "jade/policy/reviewed":
|
|
344
|
+
// 审查的结果显示在工具调用那一行,这里只计用量
|
|
345
|
+
void countUsage(e);
|
|
346
|
+
return;
|
|
282
347
|
case "jade/compact/trimmed": {
|
|
283
348
|
const d = e.data as { saved: number };
|
|
284
349
|
screen.log(dim(`[cleared old tool output, ~${d.saved} tokens]`));
|
|
@@ -323,7 +388,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
323
388
|
if (d.kind === "approval") {
|
|
324
389
|
const name = toolNames.get(d.toolUseId) ?? "?";
|
|
325
390
|
const head = `${bold(name)}(${summarize(cwd, name, asked.get(d.toolUseId))})`;
|
|
326
|
-
screen.log(d.approved ? green(`✓ ${head}`) : red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), {
|
|
391
|
+
screen.log(d.approved ? green(`✓ ${head}`) : red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), {
|
|
392
|
+
id: d.toolUseId,
|
|
393
|
+
details: callDetails(name, asked.get(d.toolUseId)),
|
|
394
|
+
});
|
|
327
395
|
continue;
|
|
328
396
|
}
|
|
329
397
|
if (d.kind === "retry") {
|