@uncarved/jade 0.1.1 → 0.1.3
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 +18 -6
- package/bin/jade.js +23 -0
- package/dist/commands.d.ts +20 -0
- package/dist/commands.js +20 -0
- package/dist/commands.js.map +1 -1
- package/dist/distro.d.ts +51 -7
- package/dist/distro.js +199 -48
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +479 -303
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugin-cli.d.ts +1 -1
- package/dist/plugin-cli.js +79 -36
- package/dist/plugin-cli.js.map +1 -1
- package/dist/plugins/budget.d.ts +2 -2
- package/dist/plugins/budget.js +2 -2
- package/dist/plugins/budget.js.map +1 -1
- package/dist/plugins/builtins.js +6 -3
- package/dist/plugins/builtins.js.map +1 -1
- package/dist/plugins/checks.d.ts +2 -1
- package/dist/plugins/checks.js +25 -5
- package/dist/plugins/checks.js.map +1 -1
- package/dist/plugins/compact.js +45 -1
- package/dist/plugins/compact.js.map +1 -1
- package/dist/plugins/core.d.ts +5 -0
- package/dist/plugins/core.js +39 -0
- package/dist/plugins/core.js.map +1 -1
- package/dist/plugins/host.d.ts +52 -0
- package/dist/plugins/host.js +115 -0
- package/dist/plugins/host.js.map +1 -0
- package/dist/plugins/install.d.ts +1 -1
- package/dist/plugins/loader.d.ts +238 -10
- package/dist/plugins/loader.js +175 -21
- package/dist/plugins/loader.js.map +1 -1
- package/dist/plugins/policy.d.ts +23 -5
- package/dist/plugins/policy.js +63 -10
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +14 -1
- package/dist/plugins/prompts.js +118 -16
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.d.ts +7 -0
- package/dist/plugins/repeats.js +74 -13
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +18 -0
- package/dist/plugins/review.js +130 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/plugins/sessions.d.ts +7 -0
- package/dist/plugins/sessions.js +110 -0
- package/dist/plugins/sessions.js.map +1 -0
- package/dist/provider.d.ts +57 -3
- package/dist/provider.js +80 -10
- package/dist/provider.js.map +1 -1
- package/dist/session-lifecycle.d.ts +61 -0
- package/dist/session-lifecycle.js +167 -0
- package/dist/session-lifecycle.js.map +1 -0
- package/dist/session-scope.d.ts +53 -0
- package/dist/session-scope.js +116 -0
- package/dist/session-scope.js.map +1 -0
- package/dist/sessions.d.ts +6 -0
- package/dist/sessions.js +20 -0
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +4 -1
- package/dist/sys.js +18 -1
- package/dist/sys.js.map +1 -1
- package/dist/tools/ask.d.ts +11 -0
- package/dist/tools/ask.js +62 -0
- package/dist/tools/ask.js.map +1 -0
- package/dist/tools/fs.d.ts +4 -0
- package/dist/tools/fs.js +53 -12
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/grep.js +3 -2
- package/dist/tools/grep.js.map +1 -1
- package/dist/tools/shell.js +5 -1
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +10 -2
- package/dist/tools/task.js +40 -9
- package/dist/tools/task.js.map +1 -1
- package/dist/tools/web.d.ts +33 -3
- package/dist/tools/web.js +351 -143
- package/dist/tools/web.js.map +1 -1
- package/dist/ui/configure.js +144 -66
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/diff.d.ts +1 -1
- package/dist/ui/diff.js +1 -2
- package/dist/ui/diff.js.map +1 -1
- package/dist/ui/editor.d.ts +8 -0
- package/dist/ui/editor.js +50 -1
- package/dist/ui/editor.js.map +1 -1
- package/dist/ui/markdown.d.ts +22 -0
- package/dist/ui/markdown.js +242 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +9 -8
- package/dist/ui/render.js +35 -62
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/renderers.d.ts +8 -0
- package/dist/ui/renderers.js +169 -0
- package/dist/ui/renderers.js.map +1 -0
- package/dist/ui/screen.d.ts +25 -2
- package/dist/ui/screen.js +104 -12
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.d.ts +13 -0
- package/dist/ui/transcript.js +157 -31
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +2 -0
- package/dist/ui/tui.js +383 -81
- package/dist/ui/tui.js.map +1 -1
- package/dist/update.d.ts +53 -0
- package/dist/update.js +255 -0
- package/dist/update.js.map +1 -0
- package/package.json +7 -6
- package/src/commands.ts +24 -0
- package/src/distro.ts +234 -52
- package/src/index.ts +3 -0
- package/src/main.ts +459 -300
- package/src/mcp.ts +1 -1
- package/src/plugin-cli.ts +73 -34
- package/src/plugins/budget.ts +4 -4
- package/src/plugins/builtins.ts +6 -3
- package/src/plugins/checks.ts +25 -6
- package/src/plugins/compact.ts +40 -1
- package/src/plugins/core.ts +40 -0
- package/src/plugins/host.ts +146 -0
- package/src/plugins/install.ts +1 -1
- package/src/plugins/loader.ts +379 -23
- package/src/plugins/policy.ts +70 -13
- package/src/plugins/prompts.ts +128 -16
- package/src/plugins/repeats.ts +75 -14
- package/src/plugins/review.ts +146 -0
- package/src/plugins/sessions.ts +109 -0
- package/src/provider.ts +117 -14
- package/src/session-lifecycle.ts +194 -0
- package/src/session-scope.ts +135 -0
- package/src/sessions.ts +17 -0
- package/src/sys.ts +18 -1
- package/src/tools/ask.ts +72 -0
- package/src/tools/fs.ts +53 -12
- package/src/tools/grep.ts +3 -2
- package/src/tools/shell.ts +5 -1
- package/src/tools/task.ts +56 -12
- package/src/tools/web.ts +370 -140
- package/src/ui/configure.ts +141 -67
- package/src/ui/diff.ts +1 -2
- package/src/ui/editor.ts +50 -1
- package/src/ui/markdown.ts +258 -0
- package/src/ui/render.ts +35 -48
- package/src/ui/renderers.ts +183 -0
- package/src/ui/screen.ts +113 -16
- package/src/ui/transcript.ts +164 -35
- package/src/ui/tui.tsx +424 -79
- package/src/update.ts +262 -0
package/src/ui/transcript.ts
CHANGED
|
@@ -7,7 +7,9 @@ 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 {
|
|
10
|
+
import type { ToolRenderer } from "../plugins/loader.ts";
|
|
11
|
+
import type { PendingCall } from "uncarved";
|
|
12
|
+
import { clip, clipStyled, detailsOf, preview, summarize } from "./render.ts";
|
|
11
13
|
import { bold, cyan, dim, green, red, yellow, type Screen } from "./screen.ts";
|
|
12
14
|
|
|
13
15
|
export type Notice = Parameters<Parameters<Runtime["subscribe"]>[0]>[0];
|
|
@@ -23,6 +25,8 @@ export interface TranscriptOptions {
|
|
|
23
25
|
compactAt?(): number;
|
|
24
26
|
/** 上一轮失败后能否用 /retry 重新请求 */
|
|
25
27
|
canRetry(): boolean;
|
|
28
|
+
/** 插件为自己的工具注册的显示方式(随会话的插件实例变) */
|
|
29
|
+
renderer?(tool: string): { plugin: string; renderer: ToolRenderer } | undefined;
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
export interface Transcript {
|
|
@@ -41,6 +45,12 @@ export interface Transcript {
|
|
|
41
45
|
turnFailed: boolean;
|
|
42
46
|
/** 开始新会话时清空 */
|
|
43
47
|
reset(): void;
|
|
48
|
+
/** 工具调用括号里的一行摘要:插件的 renderer 优先 */
|
|
49
|
+
summary(name: string, input: unknown): string;
|
|
50
|
+
/** 摘要省略了的参数,调用行展开后显示在最前面(已经缩进);没有时是 undefined */
|
|
51
|
+
details(name: string, input: unknown): string | undefined;
|
|
52
|
+
/** 审批时的预览:工具的 renderer 给的,没有时是参数的 JSON */
|
|
53
|
+
preview(c: PendingCall): Promise<string>;
|
|
44
54
|
}
|
|
45
55
|
|
|
46
56
|
/** 工具结果折叠起来的完整内容最多这么多行 */
|
|
@@ -57,38 +67,47 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
57
67
|
for (const k of Object.keys(usage) as (keyof Usage)[]) usage[k] += u?.[k] ?? 0;
|
|
58
68
|
if (u) screen.status({ spent: { input: usage.inputTokens, output: usage.outputTokens } });
|
|
59
69
|
};
|
|
60
|
-
/**
|
|
70
|
+
/** 一个事件带来的用量:模型回复、压缩时的总结调用、auto 模式的审查;子 Agent 的用量在它自己的日志里 */
|
|
61
71
|
async function countUsage(e: LogEvent) {
|
|
62
72
|
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);
|
|
73
|
+
else if (e.type === "jade/compact/compacted" || e.type === "jade/policy/reviewed") addUsage((e.data as { usage?: Usage }).usage);
|
|
64
74
|
else if (e.type === "tool.completed") {
|
|
65
75
|
const child = (e as LogEvent<"tool.completed">).data.child;
|
|
66
76
|
if (child) for (const c of await readAll(store, child.sessionId)) await countUsage(c);
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
79
|
|
|
70
|
-
/**
|
|
71
|
-
|
|
80
|
+
/**
|
|
81
|
+
* 报过缓存命中数的 provider/模型。有的服务只在命中时才带这个字段,报过一次之后没带就是没命中;
|
|
82
|
+
* 换了服务或模型要重新看,否则从报缓存的服务换到不报的,会一直显示 0%
|
|
83
|
+
*/
|
|
84
|
+
let reportsCache: string | undefined;
|
|
72
85
|
/** 上下文用量:最近一次回复的输入加输出,就是下一次请求的大致起点;以及这次输入的缓存命中率 */
|
|
73
|
-
function showContext(
|
|
86
|
+
function showContext(e: LogEvent<"model.responded">) {
|
|
87
|
+
const u = e.data.usage;
|
|
74
88
|
if (u?.inputTokens === undefined) return;
|
|
75
|
-
|
|
89
|
+
const by = e.origin.by === "model" ? `${e.origin.provider}/${e.origin.model}` : "";
|
|
90
|
+
if (u.cacheReadTokens !== undefined) reportsCache = by;
|
|
76
91
|
screen.status({
|
|
77
92
|
context: { used: u.inputTokens + (u.outputTokens ?? 0), limit: opts.contextWindow(), compactAt: opts.compactAt?.() },
|
|
78
|
-
cache: reportsCache && u.inputTokens > 0 ? (u.cacheReadTokens ?? 0) / u.inputTokens : undefined,
|
|
93
|
+
cache: reportsCache === by && u.inputTokens > 0 ? (u.cacheReadTokens ?? 0) / u.inputTokens : undefined,
|
|
79
94
|
});
|
|
80
95
|
}
|
|
81
96
|
|
|
82
97
|
/**
|
|
83
98
|
* 输出速度:从第一个增量开始计时(不算等待首个 token 的时间),输出中按字数估 token 数,每半秒更新一次状态栏;
|
|
84
|
-
* 回复结束后用服务端报的输出 token
|
|
99
|
+
* 回复结束后用服务端报的输出 token 数算准确值。工具参数的增量也算输出,只调用工具的回复同样从第一个增量开始计时。
|
|
100
|
+
* 一个增量都没收到的回复(不支持流式的服务)不更新速度:从发出请求开始算会把等待首个 token 的时间也算进去,数字偏低
|
|
85
101
|
*/
|
|
86
|
-
const speed = {
|
|
102
|
+
const speed = { first: 0, estimate: 0, shown: 0 };
|
|
87
103
|
const startSpeed = () => {
|
|
88
|
-
speed.requested = Date.now();
|
|
89
104
|
speed.first = 0;
|
|
90
105
|
speed.estimate = 0;
|
|
106
|
+
toolChars = 0;
|
|
91
107
|
};
|
|
108
|
+
/** 这次回复里工具参数已经生成的字数:写大文件时一写几分钟,显示出来才看得出不是卡住了 */
|
|
109
|
+
let toolChars = 0;
|
|
110
|
+
const toolLabel = () => (toolChars < 1000 ? "Writing" : `Writing (${Math.floor(toolChars / 1000)}k chars)`);
|
|
92
111
|
const trackSpeed = (text: string) => {
|
|
93
112
|
const now = Date.now();
|
|
94
113
|
if (!speed.first) speed.first = now;
|
|
@@ -99,10 +118,9 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
99
118
|
screen.status({ speed: { tps: speed.estimate / ((now - speed.first) / 1000), live: true } });
|
|
100
119
|
};
|
|
101
120
|
const finishSpeed = (u: Usage | undefined) => {
|
|
102
|
-
const
|
|
103
|
-
const seconds = (Date.now() - from) / 1000;
|
|
121
|
+
const seconds = (Date.now() - speed.first) / 1000;
|
|
104
122
|
const tokens = u?.outputTokens ?? Math.round(speed.estimate);
|
|
105
|
-
if (
|
|
123
|
+
if (speed.first && tokens > 0 && seconds >= 0.2) screen.status({ speed: { tps: tokens / seconds } });
|
|
106
124
|
speed.first = 0;
|
|
107
125
|
speed.estimate = 0;
|
|
108
126
|
};
|
|
@@ -120,13 +138,59 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
120
138
|
const running = new Set<string>();
|
|
121
139
|
/** 子 Agent 调用过的工具,结束时放进 task 结果的折叠内容里 */
|
|
122
140
|
const steps = new Map<string, string[]>();
|
|
123
|
-
|
|
141
|
+
/** 正在运行的子 Agent 的标签,显示在它的进度行开头 */
|
|
142
|
+
const tasks = new Map<string, string>();
|
|
143
|
+
/** 调用行省略了的参数(例如很长的命令),结果出来后放在折叠内容的最前面 */
|
|
144
|
+
const calls = new Map<string, string>();
|
|
145
|
+
/** renderer 的 before 在执行前记下的东西,结果出来时交给它的 result */
|
|
146
|
+
const befores = new Map<string, unknown>();
|
|
147
|
+
const progress =(id: string, line: string, o?: { output?: boolean }) => {
|
|
124
148
|
running.add(id);
|
|
125
149
|
screen.progress(id, line, o);
|
|
126
150
|
};
|
|
127
151
|
const doneProgress = (id: string) => {
|
|
128
152
|
if (running.delete(id)) screen.progress(id, undefined);
|
|
129
153
|
};
|
|
154
|
+
/** 有 result renderer 的调用的参数,结果出来时交给它 */
|
|
155
|
+
const inputs = new Map<string, unknown>();
|
|
156
|
+
|
|
157
|
+
/** 出过错的 renderer(插件 + 工具),只提示一次 */
|
|
158
|
+
const failed = new Set<string>();
|
|
159
|
+
/** 调用插件的 renderer;没有、出错时返回 undefined(用默认的显示) */
|
|
160
|
+
async function custom<T>(name: string, use: (r: ToolRenderer) => T | undefined | Promise<T | undefined>): Promise<T | undefined> {
|
|
161
|
+
const found = opts.renderer?.(name);
|
|
162
|
+
if (!found) return undefined;
|
|
163
|
+
try {
|
|
164
|
+
return await use(found.renderer);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
renderFailed(found.plugin, name, err);
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** 同步的版本:摘要和结果在事件通知里同步显示 */
|
|
171
|
+
function customSync<T>(name: string, use: (r: ToolRenderer) => T | undefined): T | undefined {
|
|
172
|
+
const found = opts.renderer?.(name);
|
|
173
|
+
if (!found) return undefined;
|
|
174
|
+
try {
|
|
175
|
+
return use(found.renderer);
|
|
176
|
+
} catch (err) {
|
|
177
|
+
renderFailed(found.plugin, name, err);
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function renderFailed(plugin: string, name: string, err: unknown) {
|
|
182
|
+
if (failed.has(`${plugin}:${name}`)) return;
|
|
183
|
+
failed.add(`${plugin}:${name}`);
|
|
184
|
+
screen.log(yellow(`[warning] plugin ${plugin}: showing ${name} failed, using the default display: ${errorText(err)}`));
|
|
185
|
+
}
|
|
186
|
+
const summary = (name: string, input: unknown): string => {
|
|
187
|
+
const s = customSync(name, (r) => r.summary?.(input));
|
|
188
|
+
return typeof s === "string" ? clip(s.split("\n")[0]!, 120) : summarize(cwd, input);
|
|
189
|
+
};
|
|
190
|
+
const details = (name: string, input: unknown): string | undefined => {
|
|
191
|
+
const d = customSync(name, (r) => r.details?.(input));
|
|
192
|
+
return typeof d === "string" && d ? d.replace(/^/gm, " ") : undefined;
|
|
193
|
+
};
|
|
130
194
|
|
|
131
195
|
const self: Transcript = {
|
|
132
196
|
usage,
|
|
@@ -135,15 +199,25 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
135
199
|
turnFailed: false,
|
|
136
200
|
onNotice,
|
|
137
201
|
replay,
|
|
202
|
+
summary,
|
|
203
|
+
details,
|
|
204
|
+
async preview(c) {
|
|
205
|
+
const p = await custom(c.name, (r) => r.preview?.(c.input));
|
|
206
|
+
return typeof p === "string" ? p : preview(c.input);
|
|
207
|
+
},
|
|
138
208
|
echo: (text) => screen.log(text.startsWith("/") ? cyan(`› ${text}`) : `\n${bold(`› ${text.replace(/\n/g, "\n ")}`)}`),
|
|
139
209
|
reset() {
|
|
140
210
|
for (const k of Object.keys(usage) as (keyof Usage)[]) usage[k] = 0;
|
|
141
|
-
reportsCache =
|
|
211
|
+
reportsCache = undefined;
|
|
142
212
|
toolNames.clear();
|
|
143
213
|
quiet.clear();
|
|
144
214
|
for (const id of running) screen.progress(id, undefined);
|
|
145
215
|
running.clear();
|
|
146
216
|
steps.clear();
|
|
217
|
+
tasks.clear();
|
|
218
|
+
calls.clear();
|
|
219
|
+
befores.clear();
|
|
220
|
+
inputs.clear();
|
|
147
221
|
self.lastEnd = undefined;
|
|
148
222
|
},
|
|
149
223
|
};
|
|
@@ -161,8 +235,17 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
161
235
|
startSpeed();
|
|
162
236
|
return;
|
|
163
237
|
}
|
|
164
|
-
streamed = true;
|
|
165
238
|
trackSpeed(n.delta.text);
|
|
239
|
+
// 工具参数只用来计时,不显示;也不算已经输出了内容(重试时不用标记作废,回复结束时照常打印没流式输出的正文)
|
|
240
|
+
if (n.delta.kind === "tool_input") {
|
|
241
|
+
// 只在千位变了时更新,不让每个增量都重画
|
|
242
|
+
const before = toolLabel();
|
|
243
|
+
toolChars += n.delta.text.length;
|
|
244
|
+
const label = toolLabel();
|
|
245
|
+
if (label !== before || toolChars === n.delta.text.length) screen.busy(label);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
streamed = true;
|
|
166
249
|
if (n.delta.kind === "thinking") {
|
|
167
250
|
screen.busy("Thinking");
|
|
168
251
|
screen.stream("thinking", n.delta.text);
|
|
@@ -188,13 +271,22 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
188
271
|
|
|
189
272
|
function renderProgress(toolUseId: string, p: ShellProgress | TaskProgress) {
|
|
190
273
|
if (typeof p !== "object" || p === null) return;
|
|
191
|
-
|
|
274
|
+
// 每个子 Agent 只有一行进度:标签、调用过几个工具、正在做什么。并行的几个靠标签分清
|
|
275
|
+
const taskLine = (what: string) => {
|
|
276
|
+
const n = steps.get(toolUseId)?.length ?? 0;
|
|
277
|
+
progress(toolUseId, dim(` ↳ ${tasks.get(toolUseId) ?? "sub-agent"}${n ? ` (${n} tool call${n > 1 ? "s" : ""})` : ""} · ${what}`));
|
|
278
|
+
};
|
|
279
|
+
if (p.kind === "task-start") {
|
|
280
|
+
tasks.set(toolUseId, p.description);
|
|
281
|
+
return taskLine("starting");
|
|
282
|
+
}
|
|
283
|
+
if (p.kind === "task-model") return taskLine("thinking");
|
|
192
284
|
if (p.kind === "task-tool") {
|
|
193
285
|
// 要用户批准的调用在审批时显示
|
|
194
286
|
if (p.decision === "ask") return;
|
|
195
|
-
const step = `${p.decision === "deny" ? "✗" : "·"} ${p.name}(${
|
|
287
|
+
const step = `${p.decision === "deny" ? "✗" : "·"} ${p.name}(${summary(p.name, p.input)})`;
|
|
196
288
|
steps.set(toolUseId, [...(steps.get(toolUseId) ?? []), step]);
|
|
197
|
-
return
|
|
289
|
+
return taskLine(step);
|
|
198
290
|
}
|
|
199
291
|
// 第三方工具的进度格式 jade 不认识,不显示
|
|
200
292
|
if (typeof (p as { text?: unknown }).text !== "string") return;
|
|
@@ -210,13 +302,13 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
210
302
|
case "model.responded": {
|
|
211
303
|
const d = e.data as EventDataMap["model.responded"];
|
|
212
304
|
void countUsage(e);
|
|
213
|
-
showContext(
|
|
305
|
+
showContext(e as LogEvent<"model.responded">);
|
|
214
306
|
if (!replaying) finishSpeed(d.usage);
|
|
215
307
|
for (const b of d.content) if (b.kind === "tool_use") toolNames.set(b.id, b.name);
|
|
216
|
-
// 不支持流式的 provider
|
|
308
|
+
// 不支持流式的 provider、重新显示历史:一次送出最终文本,和流式输出一样排版
|
|
217
309
|
if (!streamed) {
|
|
218
310
|
const text = textOf(d.content).trim();
|
|
219
|
-
if (text) screen.
|
|
311
|
+
if (text) screen.stream("text", text);
|
|
220
312
|
}
|
|
221
313
|
streamed = false;
|
|
222
314
|
screen.endStream();
|
|
@@ -225,9 +317,17 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
225
317
|
case "tool.decided": {
|
|
226
318
|
const d = e.data as EventDataMap["tool.decided"];
|
|
227
319
|
const name = toolNames.get(d.toolUseId) ?? "?";
|
|
320
|
+
const full = details(name, d.input);
|
|
321
|
+
if (full) calls.set(d.toolUseId, full);
|
|
322
|
+
if (opts.renderer?.(name)?.renderer.result) inputs.set(d.toolUseId, d.input);
|
|
228
323
|
if (d.decision === "ask") return;
|
|
229
|
-
|
|
230
|
-
if (d.decision === "
|
|
324
|
+
// 执行前交给 renderer 的 before(例如按文件当时的内容算 diff);重新显示历史时文件已经变了,不调用
|
|
325
|
+
if (d.decision === "allow" && !replaying) {
|
|
326
|
+
const before = customSync(name, (r) => r.before?.(d.input));
|
|
327
|
+
if (before !== undefined) befores.set(d.toolUseId, before);
|
|
328
|
+
}
|
|
329
|
+
const head = `${bold(name)}(${summary(name, d.input)})`;
|
|
330
|
+
if (d.decision === "deny") screen.log(red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), { id: d.toolUseId, details: full });
|
|
231
331
|
else {
|
|
232
332
|
activity(`Running ${name}`);
|
|
233
333
|
if (name === "todo_write") {
|
|
@@ -237,7 +337,8 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
237
337
|
screen.log(renderTodos((d.input as { todos?: Todo[] }).todos ?? []).replace(/^/gm, " "));
|
|
238
338
|
} else if (d.decidedBy !== "user" || replaying) {
|
|
239
339
|
// 用户批准的调用在审批时已经显示过;重新显示历史时没有审批这一步
|
|
240
|
-
|
|
340
|
+
// auto 模式下审查放行的带着理由
|
|
341
|
+
screen.log(cyan("● ") + head + (d.reason ? dim(` — ${clip(d.reason, 160)}`) : ""), { id: d.toolUseId, details: full });
|
|
241
342
|
}
|
|
242
343
|
}
|
|
243
344
|
return;
|
|
@@ -249,19 +350,36 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
249
350
|
doneProgress(d.toolUseId);
|
|
250
351
|
const sub = steps.get(d.toolUseId) ?? [];
|
|
251
352
|
steps.delete(d.toolUseId);
|
|
353
|
+
tasks.delete(d.toolUseId);
|
|
354
|
+
const call = calls.get(d.toolUseId);
|
|
355
|
+
calls.delete(d.toolUseId);
|
|
356
|
+
const before = befores.get(d.toolUseId);
|
|
357
|
+
befores.delete(d.toolUseId);
|
|
358
|
+
const input = inputs.get(d.toolUseId);
|
|
359
|
+
inputs.delete(d.toolUseId);
|
|
252
360
|
if (quiet.delete(d.toolUseId) && !d.isError) return;
|
|
253
361
|
const text = textOf(d.content).trim();
|
|
362
|
+
const name = toolNames.get(d.toolUseId) ?? "?";
|
|
363
|
+
const mine = customSync(name, (r) => r.result?.({ input, text, isError: d.isError === true, before }));
|
|
364
|
+
if (mine && typeof mine.line === "string") {
|
|
365
|
+
const own = typeof mine.details === "string" && mine.details ? [mine.details.replace(/^/gm, " ")] : [];
|
|
366
|
+
const folded = [...(call ? [call] : []), ...own].join("\n") || undefined;
|
|
367
|
+
screen.amend(d.toolUseId, (d.isError ? red : dim)("⎿ ") + clipStyled(mine.line.split("\n")[0]!, 200), { details: folded });
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
254
370
|
const lines = text.split("\n");
|
|
255
371
|
// 结果接在调用的那一行后面;摘要省略了内容时,把完整结果(和子 Agent 调用过的工具)折叠起来,点击展开
|
|
256
372
|
const shown = d.isError ? 4 : 1;
|
|
257
373
|
const width = d.isError ? 200 : 160;
|
|
258
374
|
const hidden = sub.length > 0 || lines.length > shown || lines.some((l) => l.length > width);
|
|
259
|
-
const details =
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
375
|
+
const details =
|
|
376
|
+
hidden || call
|
|
377
|
+
? [
|
|
378
|
+
...(call ? [call] : []),
|
|
379
|
+
...(sub.length ? [dim(` sub-agent ran ${sub.length} tool call${sub.length > 1 ? "s" : ""}:`), ...sub.map((l) => dim(` ${l}`))] : []),
|
|
380
|
+
...(hidden ? [detailsOf(lines, d.isError ? red : (s) => s, MAX_DETAIL_LINES)] : []),
|
|
381
|
+
].join("\n")
|
|
382
|
+
: undefined;
|
|
265
383
|
if (d.isError) screen.amend(d.toolUseId, red(`⎿ ${lines.slice(0, shown).map((l) => clip(l, width)).join("\n ")}`), { details });
|
|
266
384
|
else screen.amend(d.toolUseId, dim(`⎿ ${clip(lines[0] ?? "", width)}${lines.length > 1 ? ` (+${lines.length - 1} lines)` : ""}`), { details });
|
|
267
385
|
return;
|
|
@@ -269,6 +387,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
269
387
|
case "tool.interrupted": {
|
|
270
388
|
const d = e.data as EventDataMap["tool.interrupted"];
|
|
271
389
|
doneProgress(d.toolUseId);
|
|
390
|
+
tasks.delete(d.toolUseId);
|
|
391
|
+
calls.delete(d.toolUseId);
|
|
392
|
+
befores.delete(d.toolUseId);
|
|
393
|
+
inputs.delete(d.toolUseId);
|
|
272
394
|
screen.amend(d.toolUseId, yellow("⎿ interrupted before finishing; not retried"));
|
|
273
395
|
return;
|
|
274
396
|
}
|
|
@@ -311,6 +433,10 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
311
433
|
}
|
|
312
434
|
return;
|
|
313
435
|
}
|
|
436
|
+
case "jade/policy/reviewed":
|
|
437
|
+
// 审查的结果显示在工具调用那一行,这里只计用量
|
|
438
|
+
void countUsage(e);
|
|
439
|
+
return;
|
|
314
440
|
case "jade/compact/trimmed": {
|
|
315
441
|
const d = e.data as { saved: number };
|
|
316
442
|
screen.log(dim(`[cleared old tool output, ~${d.saved} tokens]`));
|
|
@@ -334,7 +460,7 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
334
460
|
if (!show) {
|
|
335
461
|
for (const e of events) {
|
|
336
462
|
await countUsage(e);
|
|
337
|
-
if (e.type === "model.responded") showContext(
|
|
463
|
+
if (e.type === "model.responded") showContext(e as LogEvent<"model.responded">);
|
|
338
464
|
}
|
|
339
465
|
return;
|
|
340
466
|
}
|
|
@@ -354,8 +480,11 @@ export function transcript(opts: TranscriptOptions): Transcript {
|
|
|
354
480
|
const d = e.data as EventDataMap["input.received"];
|
|
355
481
|
if (d.kind === "approval") {
|
|
356
482
|
const name = toolNames.get(d.toolUseId) ?? "?";
|
|
357
|
-
const head = `${bold(name)}(${
|
|
358
|
-
screen.log(d.approved ? green(`✓ ${head}`) : red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), {
|
|
483
|
+
const head = `${bold(name)}(${summary(name, asked.get(d.toolUseId))})`;
|
|
484
|
+
screen.log(d.approved ? green(`✓ ${head}`) : red(`✗ ${head}`) + dim(d.reason ? ` — ${clip(d.reason, 200)}` : ""), {
|
|
485
|
+
id: d.toolUseId,
|
|
486
|
+
details: details(name, asked.get(d.toolUseId)),
|
|
487
|
+
});
|
|
359
488
|
continue;
|
|
360
489
|
}
|
|
361
490
|
if (d.kind === "retry") {
|