@uncarved/jade 0.1.1 → 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.
Files changed (57) hide show
  1. package/README.md +198 -192
  2. package/dist/distro.d.ts +8 -1
  3. package/dist/distro.js +2 -1
  4. package/dist/distro.js.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/main.js +52 -12
  9. package/dist/main.js.map +1 -1
  10. package/dist/mcp.js +1 -1
  11. package/dist/mcp.js.map +1 -1
  12. package/dist/plugins/policy.d.ts +21 -5
  13. package/dist/plugins/policy.js +46 -9
  14. package/dist/plugins/policy.js.map +1 -1
  15. package/dist/plugins/prompts.d.ts +8 -0
  16. package/dist/plugins/prompts.js +16 -2
  17. package/dist/plugins/prompts.js.map +1 -1
  18. package/dist/plugins/review.d.ts +16 -0
  19. package/dist/plugins/review.js +92 -0
  20. package/dist/plugins/review.js.map +1 -0
  21. package/dist/provider.d.ts +1 -1
  22. package/dist/provider.js +7 -3
  23. package/dist/provider.js.map +1 -1
  24. package/dist/tools/task.d.ts +4 -0
  25. package/dist/tools/task.js +4 -1
  26. package/dist/tools/task.js.map +1 -1
  27. package/dist/ui/configure.js +55 -46
  28. package/dist/ui/configure.js.map +1 -1
  29. package/dist/ui/markdown.d.ts +17 -0
  30. package/dist/ui/markdown.js +204 -0
  31. package/dist/ui/markdown.js.map +1 -0
  32. package/dist/ui/render.d.ts +2 -0
  33. package/dist/ui/render.js +10 -0
  34. package/dist/ui/render.js.map +1 -1
  35. package/dist/ui/screen.d.ts +3 -0
  36. package/dist/ui/screen.js +3 -0
  37. package/dist/ui/screen.js.map +1 -1
  38. package/dist/ui/transcript.js +56 -20
  39. package/dist/ui/transcript.js.map +1 -1
  40. package/dist/ui/tui.js +76 -25
  41. package/dist/ui/tui.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/distro.ts +7 -2
  44. package/src/index.ts +1 -0
  45. package/src/main.ts +53 -15
  46. package/src/mcp.ts +1 -1
  47. package/src/plugins/policy.ts +53 -12
  48. package/src/plugins/prompts.ts +21 -2
  49. package/src/plugins/review.ts +109 -0
  50. package/src/provider.ts +8 -4
  51. package/src/tools/task.ts +5 -1
  52. package/src/ui/configure.ts +54 -43
  53. package/src/ui/markdown.ts +219 -0
  54. package/src/ui/render.ts +9 -0
  55. package/src/ui/screen.ts +3 -0
  56. package/src/ui/transcript.ts +59 -23
  57. package/src/ui/tui.tsx +73 -23
@@ -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
@@ -81,6 +81,9 @@ export const green = paint("32");
81
81
  export const yellow = paint("33");
82
82
  export const cyan = paint("36");
83
83
  export const bold = paint("1");
84
+ export const italic = paint("3");
85
+ export const underline = paint("4");
86
+ export const strike = paint("9");
84
87
 
85
88
  // ---------- 纯文本实现 ----------
86
89
 
@@ -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
- /** 一个事件带来的用量:模型回复、压缩时的总结调用;子 Agent 的用量在它自己的日志里 */
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);
@@ -81,11 +81,11 @@ export function transcript(opts: TranscriptOptions): Transcript {
81
81
 
82
82
  /**
83
83
  * 输出速度:从第一个增量开始计时(不算等待首个 token 的时间),输出中按字数估 token 数,每半秒更新一次状态栏;
84
- * 回复结束后用服务端报的输出 token 数算准确值。不支持流式的服务从发出请求开始算
84
+ * 回复结束后用服务端报的输出 token 数算准确值。工具参数的增量也算输出,只调用工具的回复同样从第一个增量开始计时。
85
+ * 一个增量都没收到的回复(不支持流式的服务)不更新速度:从发出请求开始算会把等待首个 token 的时间也算进去,数字偏低
85
86
  */
86
- const speed = { requested: 0, first: 0, estimate: 0, shown: 0 };
87
+ const speed = { first: 0, estimate: 0, shown: 0 };
87
88
  const startSpeed = () => {
88
- speed.requested = Date.now();
89
89
  speed.first = 0;
90
90
  speed.estimate = 0;
91
91
  };
@@ -99,10 +99,9 @@ export function transcript(opts: TranscriptOptions): Transcript {
99
99
  screen.status({ speed: { tps: speed.estimate / ((now - speed.first) / 1000), live: true } });
100
100
  };
101
101
  const finishSpeed = (u: Usage | undefined) => {
102
- const from = speed.first || speed.requested;
103
- const seconds = (Date.now() - from) / 1000;
102
+ const seconds = (Date.now() - speed.first) / 1000;
104
103
  const tokens = u?.outputTokens ?? Math.round(speed.estimate);
105
- if (from && tokens > 0 && seconds >= 0.2) screen.status({ speed: { tps: tokens / seconds } });
104
+ if (speed.first && tokens > 0 && seconds >= 0.2) screen.status({ speed: { tps: tokens / seconds } });
106
105
  speed.first = 0;
107
106
  speed.estimate = 0;
108
107
  };
@@ -120,6 +119,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
120
119
  const running = new Set<string>();
121
120
  /** 子 Agent 调用过的工具,结束时放进 task 结果的折叠内容里 */
122
121
  const steps = new Map<string, string[]>();
122
+ /** 正在运行的子 Agent 的标签,显示在它的进度行开头 */
123
+ const tasks = new Map<string, string>();
124
+ /** 调用行省略了的参数(例如很长的命令),结果出来后放在折叠内容的最前面 */
125
+ const calls = new Map<string, string>();
123
126
  const progress = (id: string, line: string, o?: { output?: boolean }) => {
124
127
  running.add(id);
125
128
  screen.progress(id, line, o);
@@ -144,6 +147,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
144
147
  for (const id of running) screen.progress(id, undefined);
145
148
  running.clear();
146
149
  steps.clear();
150
+ tasks.clear();
151
+ calls.clear();
147
152
  self.lastEnd = undefined;
148
153
  },
149
154
  };
@@ -161,8 +166,13 @@ export function transcript(opts: TranscriptOptions): Transcript {
161
166
  startSpeed();
162
167
  return;
163
168
  }
164
- streamed = true;
165
169
  trackSpeed(n.delta.text);
170
+ // 工具参数只用来计时,不显示;也不算已经输出了内容(重试时不用标记作废,回复结束时照常打印没流式输出的正文)
171
+ if (n.delta.kind === "tool_input") {
172
+ screen.busy("Writing");
173
+ return;
174
+ }
175
+ streamed = true;
166
176
  if (n.delta.kind === "thinking") {
167
177
  screen.busy("Thinking");
168
178
  screen.stream("thinking", n.delta.text);
@@ -188,13 +198,22 @@ export function transcript(opts: TranscriptOptions): Transcript {
188
198
 
189
199
  function renderProgress(toolUseId: string, p: ShellProgress | TaskProgress) {
190
200
  if (typeof p !== "object" || p === null) return;
191
- if (p.kind === "task-start") return progress(toolUseId, dim(` ↳ sub-agent: ${p.description}`));
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");
192
211
  if (p.kind === "task-tool") {
193
212
  // 要用户批准的调用在审批时显示
194
213
  if (p.decision === "ask") return;
195
214
  const step = `${p.decision === "deny" ? "✗" : "·"} ${p.name}(${summarize(cwd, p.name, p.input)})`;
196
215
  steps.set(toolUseId, [...(steps.get(toolUseId) ?? []), step]);
197
- return progress(toolUseId, dim(` ${step}`));
216
+ return taskLine(step);
198
217
  }
199
218
  // 第三方工具的进度格式 jade 不认识,不显示
200
219
  if (typeof (p as { text?: unknown }).text !== "string") return;
@@ -213,10 +232,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
213
232
  showContext(d.usage);
214
233
  if (!replaying) finishSpeed(d.usage);
215
234
  for (const b of d.content) if (b.kind === "tool_use") toolNames.set(b.id, b.name);
216
- // 不支持流式的 provider:直接打印最终文本
235
+ // 不支持流式的 provider、重新显示历史:一次送出最终文本,和流式输出一样排版
217
236
  if (!streamed) {
218
237
  const text = textOf(d.content).trim();
219
- if (text) screen.log(text);
238
+ if (text) screen.stream("text", text);
220
239
  }
221
240
  streamed = false;
222
241
  screen.endStream();
@@ -225,9 +244,11 @@ export function transcript(opts: TranscriptOptions): Transcript {
225
244
  case "tool.decided": {
226
245
  const d = e.data as EventDataMap["tool.decided"];
227
246
  const name = toolNames.get(d.toolUseId) ?? "?";
247
+ const full = callDetails(name, d.input);
248
+ if (full) calls.set(d.toolUseId, full);
228
249
  if (d.decision === "ask") return;
229
250
  const head = `${bold(name)}(${summarize(cwd, name, d.input)})`;
230
- 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 });
231
252
  else {
232
253
  activity(`Running ${name}`);
233
254
  if (name === "todo_write") {
@@ -237,7 +258,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
237
258
  screen.log(renderTodos((d.input as { todos?: Todo[] }).todos ?? []).replace(/^/gm, " "));
238
259
  } else if (d.decidedBy !== "user" || replaying) {
239
260
  // 用户批准的调用在审批时已经显示过;重新显示历史时没有审批这一步
240
- screen.log(cyan("● ") + head, { id: d.toolUseId });
261
+ // auto 模式下审查放行的带着理由
262
+ screen.log(cyan("● ") + head + (d.reason ? dim(` — ${clip(d.reason, 160)}`) : ""), { id: d.toolUseId, details: full });
241
263
  }
242
264
  }
243
265
  return;
@@ -249,6 +271,9 @@ export function transcript(opts: TranscriptOptions): Transcript {
249
271
  doneProgress(d.toolUseId);
250
272
  const sub = steps.get(d.toolUseId) ?? [];
251
273
  steps.delete(d.toolUseId);
274
+ tasks.delete(d.toolUseId);
275
+ const call = calls.get(d.toolUseId);
276
+ calls.delete(d.toolUseId);
252
277
  if (quiet.delete(d.toolUseId) && !d.isError) return;
253
278
  const text = textOf(d.content).trim();
254
279
  const lines = text.split("\n");
@@ -256,12 +281,14 @@ export function transcript(opts: TranscriptOptions): Transcript {
256
281
  const shown = d.isError ? 4 : 1;
257
282
  const width = d.isError ? 200 : 160;
258
283
  const hidden = sub.length > 0 || lines.length > shown || lines.some((l) => l.length > width);
259
- const details = hidden
260
- ? [
261
- ...(sub.length ? [dim(` sub-agent ran ${sub.length} tool call${sub.length > 1 ? "s" : ""}:`), ...sub.map((l) => dim(` ${l}`))] : []),
262
- detailsOf(lines, d.isError ? red : (s) => s, MAX_DETAIL_LINES),
263
- ].join("\n")
264
- : undefined;
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;
265
292
  if (d.isError) screen.amend(d.toolUseId, red(`⎿ ${lines.slice(0, shown).map((l) => clip(l, width)).join("\n ")}`), { details });
266
293
  else screen.amend(d.toolUseId, dim(`⎿ ${clip(lines[0] ?? "", width)}${lines.length > 1 ? ` (+${lines.length - 1} lines)` : ""}`), { details });
267
294
  return;
@@ -269,6 +296,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
269
296
  case "tool.interrupted": {
270
297
  const d = e.data as EventDataMap["tool.interrupted"];
271
298
  doneProgress(d.toolUseId);
299
+ tasks.delete(d.toolUseId);
300
+ calls.delete(d.toolUseId);
272
301
  screen.amend(d.toolUseId, yellow("⎿ interrupted before finishing; not retried"));
273
302
  return;
274
303
  }
@@ -311,6 +340,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
311
340
  }
312
341
  return;
313
342
  }
343
+ case "jade/policy/reviewed":
344
+ // 审查的结果显示在工具调用那一行,这里只计用量
345
+ void countUsage(e);
346
+ return;
314
347
  case "jade/compact/trimmed": {
315
348
  const d = e.data as { saved: number };
316
349
  screen.log(dim(`[cleared old tool output, ~${d.saved} tokens]`));
@@ -355,7 +388,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
355
388
  if (d.kind === "approval") {
356
389
  const name = toolNames.get(d.toolUseId) ?? "?";
357
390
  const head = `${bold(name)}(${summarize(cwd, name, asked.get(d.toolUseId))})`;
358
- screen.log(d.approved ? green(`✓ ${head}`) : red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), { id: d.toolUseId });
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
+ });
359
395
  continue;
360
396
  }
361
397
  if (d.kind === "retry") {