@aoshizuku/pi-bettertui 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.
- package/README.md +327 -327
- package/package.json +1 -1
- package/src/format.ts +165 -165
- package/src/gutter.ts +236 -236
- package/src/index.ts +660 -660
- package/src/segment.ts +311 -311
- package/src/shell-description.ts +75 -75
- package/src/thinking.ts +524 -524
- package/src/timeline.ts +881 -881
- package/src/tool-lines.ts +173 -173
- package/src/tool-row.ts +387 -387
- package/src/tool-summary.ts +237 -237
- package/src/types.ts +13 -13
package/src/tool-row.ts
CHANGED
|
@@ -1,387 +1,387 @@
|
|
|
1
|
-
import type { Theme, ToolRenderers } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { type Component, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
|
-
|
|
4
|
-
import type { BetterTuiConfig } from "./config.js";
|
|
5
|
-
import { argsOf, formatElapsed, sanitizeText } from "./format.js";
|
|
6
|
-
import { clickSegment, paintSegment } from "./fold-view.js";
|
|
7
|
-
import { ActivityTimeline, groupAction, groupActive, groupCounts, groupDuration, groupFailed, groupInterrupted, groupSummary, type Segment, type ToolGroup, type ToolRow } from "./timeline.js";
|
|
8
|
-
import { childLine, commandLines, memberDetailLines, runningBlock, runningStatus, showsRunningBlock } from "./tool-lines.js";
|
|
9
|
-
import {
|
|
10
|
-
TOOL_LABELS,
|
|
11
|
-
callDetail,
|
|
12
|
-
isFailed,
|
|
13
|
-
isInterrupted,
|
|
14
|
-
isShellTool,
|
|
15
|
-
resultLines,
|
|
16
|
-
skillReadName,
|
|
17
|
-
summarizeResult,
|
|
18
|
-
} from "./tool-summary.js";
|
|
19
|
-
import { skillHeader } from "./skill.js";
|
|
20
|
-
|
|
21
|
-
/** Text before a row's marker: none, the marker is the gutter's column 0. */
|
|
22
|
-
const MARKER_PAD = "";
|
|
23
|
-
/** Detail column of a standalone running command (fold-more.md §5.8). */
|
|
24
|
-
const SINGLE_DETAIL_RAIL = " ";
|
|
25
|
-
|
|
26
|
-
type RenderCall = NonNullable<ToolRenderers["renderCall"]>;
|
|
27
|
-
type RenderResult = NonNullable<ToolRenderers["renderResult"]>;
|
|
28
|
-
type RenderContext = Parameters<RenderCall>[2];
|
|
29
|
-
type ToolResult = Parameters<RenderResult>[0];
|
|
30
|
-
|
|
31
|
-
const EMPTY_COMPONENT: Component = {
|
|
32
|
-
render: () => [],
|
|
33
|
-
invalidate: () => {},
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export interface ToolRenderDeps {
|
|
37
|
-
timeline: ActivityTimeline;
|
|
38
|
-
getConfig: () => BetterTuiConfig;
|
|
39
|
-
getTheme: () => Theme;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface RowViewOptions {
|
|
43
|
-
timeline: ActivityTimeline;
|
|
44
|
-
toolCallId: string;
|
|
45
|
-
row: ToolRow;
|
|
46
|
-
getConfig: () => BetterTuiConfig;
|
|
47
|
-
getTheme: () => Theme;
|
|
48
|
-
cwd: string;
|
|
49
|
-
contextExpanded: boolean;
|
|
50
|
-
invalidate: () => void;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export class ToolRowView implements Component {
|
|
54
|
-
private timeline: ActivityTimeline;
|
|
55
|
-
private toolCallId: string;
|
|
56
|
-
private row: ToolRow;
|
|
57
|
-
private getConfig: () => BetterTuiConfig;
|
|
58
|
-
private getTheme: () => Theme;
|
|
59
|
-
private cwd: string;
|
|
60
|
-
private contextExpanded: boolean;
|
|
61
|
-
private invalidateSelf: () => void;
|
|
62
|
-
/** Line → item of the last fold host render, for clicks. */
|
|
63
|
-
private foldTargets: string[] = [];
|
|
64
|
-
|
|
65
|
-
constructor(options: RowViewOptions) {
|
|
66
|
-
this.timeline = options.timeline;
|
|
67
|
-
this.toolCallId = options.toolCallId;
|
|
68
|
-
this.row = options.row;
|
|
69
|
-
this.getConfig = options.getConfig;
|
|
70
|
-
this.getTheme = options.getTheme;
|
|
71
|
-
this.cwd = options.cwd;
|
|
72
|
-
this.contextExpanded = options.contextExpanded;
|
|
73
|
-
this.invalidateSelf = options.invalidate;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
update(options: Omit<RowViewOptions, "timeline" | "toolCallId" | "row">): void {
|
|
77
|
-
this.getConfig = options.getConfig;
|
|
78
|
-
this.getTheme = options.getTheme;
|
|
79
|
-
this.cwd = options.cwd;
|
|
80
|
-
this.contextExpanded = options.contextExpanded;
|
|
81
|
-
this.invalidateSelf = options.invalidate;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** A session seed may replace the row object after the component was built. */
|
|
85
|
-
private currentRow(): ToolRow {
|
|
86
|
-
return this.timeline.row(this.toolCallId) ?? this.row;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
render(width: number): string[] {
|
|
90
|
-
if (width <= 0) return [];
|
|
91
|
-
const theme = this.getTheme();
|
|
92
|
-
const config = this.getConfig();
|
|
93
|
-
|
|
94
|
-
// Fold (fold-more.md §5.6): decided on every render, so a segment crossing the threshold
|
|
95
|
-
// or a late seed changes what this row draws without pi calling renderCall again.
|
|
96
|
-
const role = this.timeline.foldRole(this.toolCallId);
|
|
97
|
-
if (role.kind === "hidden") return [];
|
|
98
|
-
if (role.kind === "host") return this.renderHost(role.segment, width, theme, config);
|
|
99
|
-
|
|
100
|
-
const row = this.currentRow();
|
|
101
|
-
const group = row.group;
|
|
102
|
-
// Only the first member draws a group; the others stay empty even if pi kept their view.
|
|
103
|
-
if (group && group.members[0] !== row) return [];
|
|
104
|
-
const expanded = group ? group.expanded || this.contextExpanded : row.expanded || this.contextExpanded;
|
|
105
|
-
// Markers sit in column 0 of the shared gutter (DESIGN.md §2); pi's outputPad is not used here.
|
|
106
|
-
const pad = MARKER_PAD;
|
|
107
|
-
|
|
108
|
-
if (group && group.members.length > 1) {
|
|
109
|
-
return this.renderGroup(group, width, pad, theme, expanded);
|
|
110
|
-
}
|
|
111
|
-
return this.renderSingle(row, width, pad, theme, config, expanded);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
handleMouse(event: { type?: string; button?: string; y?: number }): { handled: boolean } | undefined {
|
|
115
|
-
if (event.type !== "click" || event.button !== "left" || typeof event.y !== "number") return undefined;
|
|
116
|
-
const role = this.timeline.foldRole(this.toolCallId);
|
|
117
|
-
if (role.kind === "hidden") return undefined;
|
|
118
|
-
if (role.kind === "host") {
|
|
119
|
-
if (!clickSegment(this.timeline, role.segment, this.foldTargets, event.y)) return undefined;
|
|
120
|
-
this.invalidateSelf();
|
|
121
|
-
return { handled: true };
|
|
122
|
-
}
|
|
123
|
-
if (event.y !== 0) return undefined;
|
|
124
|
-
const row = this.currentRow();
|
|
125
|
-
const group = row.group;
|
|
126
|
-
if (group && group.members.length > 1) this.timeline.toggleGroup(group);
|
|
127
|
-
else this.timeline.toggleRow(row);
|
|
128
|
-
this.invalidateSelf();
|
|
129
|
-
return { handled: true };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
invalidate(): void {
|
|
133
|
-
// Rendering is derived from the live timeline; nothing is cached here.
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private renderHost(segment: Segment, width: number, theme: Theme, config: BetterTuiConfig): string[] {
|
|
137
|
-
const paint = paintSegment(segment, width, {
|
|
138
|
-
timeline: this.timeline,
|
|
139
|
-
config,
|
|
140
|
-
theme,
|
|
141
|
-
cwd: this.cwd,
|
|
142
|
-
expanded: this.contextExpanded,
|
|
143
|
-
});
|
|
144
|
-
this.foldTargets = paint.targets;
|
|
145
|
-
return paint.lines;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private renderGroup(
|
|
149
|
-
group: ToolGroup,
|
|
150
|
-
width: number,
|
|
151
|
-
pad: string,
|
|
152
|
-
theme: Theme,
|
|
153
|
-
expanded: boolean,
|
|
154
|
-
): string[] {
|
|
155
|
-
const counts = groupCounts(group);
|
|
156
|
-
const failed = groupFailed(group);
|
|
157
|
-
const active = groupActive(group);
|
|
158
|
-
// A process line, not an event (DESIGN.md §2.1): column 0 stays blank and the line is dim;
|
|
159
|
-
// running and done differ by tense (Exploring / Explored). Only a failure gets a marker.
|
|
160
|
-
const marker = failed > 0
|
|
161
|
-
? theme.fg(failed === counts.calls ? "error" : "warning", failed === counts.calls ? "×" : "!")
|
|
162
|
-
: " ";
|
|
163
|
-
const action = groupAction(group);
|
|
164
|
-
const summary = groupSummary(group) || `${counts.calls} tools`;
|
|
165
|
-
const duration = groupDuration(group);
|
|
166
|
-
const failure = failed > 0 ? theme.fg("error", ` · ${failed} failed`) : "";
|
|
167
|
-
// An interrupt is a count on the process line, never a marker (DESIGN.md §9).
|
|
168
|
-
const stopped = groupInterrupted(group);
|
|
169
|
-
const interrupted = stopped > 0 ? theme.fg("warning", ` · ${stopped} interrupted`) : "";
|
|
170
|
-
const time = duration >= 500 ? theme.fg("dim", ` · ${formatElapsed(duration)}`) : "";
|
|
171
|
-
const disclosure = theme.fg("dim", expanded || active || failed > 0 ? " ▾" : " ▸");
|
|
172
|
-
const header = `${pad}${marker} ${theme.fg("dim", `${action} ${summary}`)}${failure}${interrupted}${time}${disclosure}`;
|
|
173
|
-
|
|
174
|
-
const lines = [header];
|
|
175
|
-
const selected = expanded
|
|
176
|
-
? group.members
|
|
177
|
-
: failed > 0
|
|
178
|
-
? group.members.filter((member) => isFailed(member.result)).slice(-2)
|
|
179
|
-
: active
|
|
180
|
-
? group.members.slice(-2)
|
|
181
|
-
: [];
|
|
182
|
-
|
|
183
|
-
for (let index = 0; index < selected.length; index++) {
|
|
184
|
-
const member = selected[index]!;
|
|
185
|
-
const isLast = index === selected.length - 1;
|
|
186
|
-
lines.push(`${pad}${childLine(member, isLast ? "└" : "├", width, theme, this.cwd)}`);
|
|
187
|
-
if (expanded) {
|
|
188
|
-
lines.push(...this.renderMemberDetail(member, width, pad, isLast, theme, 8));
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return lines.map((line) => truncateToWidth(line, width, "…"));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private renderSingle(
|
|
196
|
-
row: ToolRow,
|
|
197
|
-
width: number,
|
|
198
|
-
pad: string,
|
|
199
|
-
theme: Theme,
|
|
200
|
-
config: BetterTuiConfig,
|
|
201
|
-
expanded: boolean,
|
|
202
|
-
): string[] {
|
|
203
|
-
const running = row.endedAt === undefined;
|
|
204
|
-
// With fold on, a running command is the same fixed-height block a segment host draws
|
|
205
|
-
// (fold-more.md §5.8); with fold off the preview stays as it was.
|
|
206
|
-
const block = this.timeline.foldEnabled && isShellTool(row.name) && running && !expanded;
|
|
207
|
-
const lines = [this.renderSingleHeader(row, width, pad, theme, expanded, block)];
|
|
208
|
-
if (expanded) {
|
|
209
|
-
if (isShellTool(row.name)) lines.push(...commandLines(row, width, `${pad} `, theme));
|
|
210
|
-
lines.push(...this.renderMemberDetail(row, width, pad, true, theme, 14, true));
|
|
211
|
-
} else if (isFailed(row.result)) {
|
|
212
|
-
// An interrupted call shows no detail: its last line only repeats `interrupted`.
|
|
213
|
-
lines.push(...this.renderMemberDetail(row, width, pad, true, theme, 2, true));
|
|
214
|
-
} else if (block) {
|
|
215
|
-
// Under one second the command stays its header line (fold-more.md §5.8).
|
|
216
|
-
if (showsRunningBlock(row)) {
|
|
217
|
-
lines.push(...runningBlock(row, width, theme, `${pad}${SINGLE_DETAIL_RAIL}`, config.tools.shellPreviewLines));
|
|
218
|
-
}
|
|
219
|
-
} else if (isShellTool(row.name) && running && config.tools.shellPreviewLines > 0) {
|
|
220
|
-
const preview = resultLines({ content: [{ type: "text", text: row.partialText }], isError: false }, config.tools.shellPreviewLines, "tail");
|
|
221
|
-
for (let index = 0; index < preview.length; index++) {
|
|
222
|
-
const rail = index === 0 ? " └ " : " ";
|
|
223
|
-
lines.push(`${pad}${theme.fg("dim", rail)}${theme.fg("toolOutput", preview[index]!)}`);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return lines.map((line) => truncateToWidth(line, width, "…"));
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
private renderSingleHeader(row: ToolRow, width: number, pad: string, theme: Theme, expanded: boolean, block = false): string {
|
|
230
|
-
const skill = skillReadName(row.name, row.args);
|
|
231
|
-
if (skill !== undefined) return this.renderSkillHeader(row, skill, pad, theme, expanded);
|
|
232
|
-
const label = TOOL_LABELS[row.name] ?? row.name;
|
|
233
|
-
const detail = sanitizeText(callDetail(row.name, row.args, this.cwd));
|
|
234
|
-
const labelText = theme.fg("toolTitle", theme.bold(label));
|
|
235
|
-
const detailText = detail ? ` ${theme.fg("muted", `(${detail})`)}` : "";
|
|
236
|
-
if (isInterrupted(row.result)) {
|
|
237
|
-
// Same `●` as every single row; warning says it did not finish, without calling it a
|
|
238
|
-
// failure, in the color of `✻ Interrupted` below (DESIGN.md §9).
|
|
239
|
-
const time = isShellTool(row.name) || this.rowDuration(row) >= 1_000
|
|
240
|
-
? theme.fg("dim", ` · ${formatElapsed(this.rowDuration(row))}`)
|
|
241
|
-
: "";
|
|
242
|
-
return `${pad}${theme.fg("warning", "●")} ${labelText}${detailText}${theme.fg("warning", " · interrupted")}${time}`;
|
|
243
|
-
}
|
|
244
|
-
const marker = row.result?.isError
|
|
245
|
-
? theme.fg("error", "×")
|
|
246
|
-
: row.endedAt === undefined
|
|
247
|
-
? theme.fg("accent", "●")
|
|
248
|
-
: theme.fg("success", "●");
|
|
249
|
-
const summary = block ? runningStatus(row) : this.singleSummary(row);
|
|
250
|
-
const summaryText = summary
|
|
251
|
-
? theme.fg(row.result?.isError ? "error" : "dim", ` · ${summary}`)
|
|
252
|
-
: "";
|
|
253
|
-
return `${pad}${marker} ${labelText}${detailText}${summaryText}`;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/** A read of `…/<name>/SKILL.md`, drawn like a `/skill:name` invocation. */
|
|
257
|
-
private renderSkillHeader(row: ToolRow, name: string, pad: string, theme: Theme, expanded: boolean): string {
|
|
258
|
-
if (row.result?.isError) {
|
|
259
|
-
const label = theme.fg("customMessageLabel", theme.bold("[Skill]"));
|
|
260
|
-
// The error text itself follows on the detail lines below the header.
|
|
261
|
-
const summary = theme.fg("error", " · Failed");
|
|
262
|
-
return `${pad}${theme.fg("error", "×")} ${label} ${theme.fg("muted", `(${name})`)}${summary}`;
|
|
263
|
-
}
|
|
264
|
-
return `${pad}${skillHeader(theme, name, expanded)}`;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private singleSummary(row: ToolRow): string {
|
|
268
|
-
if (row.result) {
|
|
269
|
-
const base = summarizeResult(row.name, row.result, row.args);
|
|
270
|
-
const duration = this.rowDuration(row);
|
|
271
|
-
const time = duration >= 1_000 || isShellTool(row.name) ? ` · ${formatElapsed(duration)}` : "";
|
|
272
|
-
return base + time;
|
|
273
|
-
}
|
|
274
|
-
if (row.endedAt !== undefined) return "Done";
|
|
275
|
-
return isShellTool(row.name) ? `Running… · ${formatElapsed(this.rowDuration(row))}` : "Running…";
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
private rowDuration(row: ToolRow): number {
|
|
279
|
-
return Math.max(0, (row.endedAt ?? Date.now()) - row.startedAt);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
private renderMemberDetail(
|
|
283
|
-
row: ToolRow,
|
|
284
|
-
width: number,
|
|
285
|
-
pad: string,
|
|
286
|
-
isLast: boolean,
|
|
287
|
-
_theme: Theme,
|
|
288
|
-
maxLines: number,
|
|
289
|
-
standalone = false,
|
|
290
|
-
): string[] {
|
|
291
|
-
const rail = standalone ? " " : isLast ? " " : " │ ";
|
|
292
|
-
return memberDetailLines(row, width, _theme, `${pad}${rail}`, maxLines);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function ensureRow(
|
|
297
|
-
deps: ToolRenderDeps,
|
|
298
|
-
toolName: string,
|
|
299
|
-
args: unknown,
|
|
300
|
-
context: RenderContext,
|
|
301
|
-
): ToolRow {
|
|
302
|
-
const existing = deps.timeline.row(context.toolCallId);
|
|
303
|
-
if (existing) {
|
|
304
|
-
existing.args = argsOf(args);
|
|
305
|
-
existing.invalidate = context.invalidate;
|
|
306
|
-
deps.timeline.detachSkillRead(context.toolCallId);
|
|
307
|
-
return existing;
|
|
308
|
-
}
|
|
309
|
-
const row = deps.timeline.begin(
|
|
310
|
-
context.toolCallId,
|
|
311
|
-
toolName,
|
|
312
|
-
args,
|
|
313
|
-
Date.now(),
|
|
314
|
-
deps.getConfig().tools.groupReadOnly,
|
|
315
|
-
);
|
|
316
|
-
row.args = argsOf(args);
|
|
317
|
-
row.invalidate = context.invalidate;
|
|
318
|
-
return row;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
export function createToolRenderers(
|
|
322
|
-
toolName: string,
|
|
323
|
-
_native: ToolRenderers | undefined,
|
|
324
|
-
deps: ToolRenderDeps,
|
|
325
|
-
): ToolRenderers {
|
|
326
|
-
return {
|
|
327
|
-
renderShell: "self",
|
|
328
|
-
renderCall(args, _theme, context) {
|
|
329
|
-
const row = ensureRow(deps, toolName, args, context);
|
|
330
|
-
if (context.executionStarted && row.endedAt === undefined) {
|
|
331
|
-
row.startedAt ||= Date.now();
|
|
332
|
-
// Normally set by tool_execution_start already; a row rebuilt after it still needs it.
|
|
333
|
-
row.execStartedAt ??= Date.now();
|
|
334
|
-
}
|
|
335
|
-
// Ctrl+O reaches tool rows only; thinking hosts read it from here (fold-more.md §5.11).
|
|
336
|
-
deps.timeline.globalExpanded = context.expanded;
|
|
337
|
-
if (row.group && row.group.members[0] !== row) return EMPTY_COMPONENT;
|
|
338
|
-
|
|
339
|
-
const previous = context.lastComponent;
|
|
340
|
-
const view = previous instanceof ToolRowView
|
|
341
|
-
? previous
|
|
342
|
-
: new ToolRowView({
|
|
343
|
-
timeline: deps.timeline,
|
|
344
|
-
toolCallId: context.toolCallId,
|
|
345
|
-
row,
|
|
346
|
-
getConfig: deps.getConfig,
|
|
347
|
-
getTheme: deps.getTheme,
|
|
348
|
-
cwd: context.cwd,
|
|
349
|
-
contextExpanded: context.expanded,
|
|
350
|
-
invalidate: context.invalidate,
|
|
351
|
-
});
|
|
352
|
-
view.update({
|
|
353
|
-
getConfig: deps.getConfig,
|
|
354
|
-
getTheme: deps.getTheme,
|
|
355
|
-
cwd: context.cwd,
|
|
356
|
-
contextExpanded: context.expanded,
|
|
357
|
-
invalidate: context.invalidate,
|
|
358
|
-
});
|
|
359
|
-
return view;
|
|
360
|
-
},
|
|
361
|
-
renderResult(result, options, _theme, context) {
|
|
362
|
-
const row = ensureRow(deps, toolName, context.args, context);
|
|
363
|
-
row.invalidate = context.invalidate;
|
|
364
|
-
// pi also hands running output to renderResult; it is a preview, not the end of the call.
|
|
365
|
-
if (options.isPartial && row.endedAt === undefined) {
|
|
366
|
-
deps.timeline.update(context.toolCallId, result);
|
|
367
|
-
deps.timeline.refreshHost(row.id);
|
|
368
|
-
return EMPTY_COMPONENT;
|
|
369
|
-
}
|
|
370
|
-
// A redraw (for example Ctrl+O) must not turn a restored result into a
|
|
371
|
-
// new one: keep the persisted end timestamp when the row already has it.
|
|
372
|
-
const endedAt = row.endedAt ?? Date.now();
|
|
373
|
-
deps.timeline.setResult(context.toolCallId, {
|
|
374
|
-
content: result.content,
|
|
375
|
-
details: result.details,
|
|
376
|
-
// pi keeps the error flag on the context, not on the result object.
|
|
377
|
-
isError: context.isError === true || (result as { isError?: unknown }).isError === true,
|
|
378
|
-
durationMs: context.durationMs,
|
|
379
|
-
}, endedAt);
|
|
380
|
-
row.invalidate = context.invalidate;
|
|
381
|
-
if (row.group && row.group.members[0] !== row) deps.timeline.refresh(row.id);
|
|
382
|
-
// The host of a fold segment draws this result; it never invalidates this row back.
|
|
383
|
-
deps.timeline.refreshHost(row.id);
|
|
384
|
-
return EMPTY_COMPONENT;
|
|
385
|
-
},
|
|
386
|
-
};
|
|
387
|
-
}
|
|
1
|
+
import type { Theme, ToolRenderers } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type Component, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
import type { BetterTuiConfig } from "./config.js";
|
|
5
|
+
import { argsOf, formatElapsed, sanitizeText } from "./format.js";
|
|
6
|
+
import { clickSegment, paintSegment } from "./fold-view.js";
|
|
7
|
+
import { ActivityTimeline, groupAction, groupActive, groupCounts, groupDuration, groupFailed, groupInterrupted, groupSummary, type Segment, type ToolGroup, type ToolRow } from "./timeline.js";
|
|
8
|
+
import { childLine, commandLines, memberDetailLines, runningBlock, runningStatus, showsRunningBlock } from "./tool-lines.js";
|
|
9
|
+
import {
|
|
10
|
+
TOOL_LABELS,
|
|
11
|
+
callDetail,
|
|
12
|
+
isFailed,
|
|
13
|
+
isInterrupted,
|
|
14
|
+
isShellTool,
|
|
15
|
+
resultLines,
|
|
16
|
+
skillReadName,
|
|
17
|
+
summarizeResult,
|
|
18
|
+
} from "./tool-summary.js";
|
|
19
|
+
import { skillHeader } from "./skill.js";
|
|
20
|
+
|
|
21
|
+
/** Text before a row's marker: none, the marker is the gutter's column 0. */
|
|
22
|
+
const MARKER_PAD = "";
|
|
23
|
+
/** Detail column of a standalone running command (fold-more.md §5.8). */
|
|
24
|
+
const SINGLE_DETAIL_RAIL = " ";
|
|
25
|
+
|
|
26
|
+
type RenderCall = NonNullable<ToolRenderers["renderCall"]>;
|
|
27
|
+
type RenderResult = NonNullable<ToolRenderers["renderResult"]>;
|
|
28
|
+
type RenderContext = Parameters<RenderCall>[2];
|
|
29
|
+
type ToolResult = Parameters<RenderResult>[0];
|
|
30
|
+
|
|
31
|
+
const EMPTY_COMPONENT: Component = {
|
|
32
|
+
render: () => [],
|
|
33
|
+
invalidate: () => {},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export interface ToolRenderDeps {
|
|
37
|
+
timeline: ActivityTimeline;
|
|
38
|
+
getConfig: () => BetterTuiConfig;
|
|
39
|
+
getTheme: () => Theme;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface RowViewOptions {
|
|
43
|
+
timeline: ActivityTimeline;
|
|
44
|
+
toolCallId: string;
|
|
45
|
+
row: ToolRow;
|
|
46
|
+
getConfig: () => BetterTuiConfig;
|
|
47
|
+
getTheme: () => Theme;
|
|
48
|
+
cwd: string;
|
|
49
|
+
contextExpanded: boolean;
|
|
50
|
+
invalidate: () => void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class ToolRowView implements Component {
|
|
54
|
+
private timeline: ActivityTimeline;
|
|
55
|
+
private toolCallId: string;
|
|
56
|
+
private row: ToolRow;
|
|
57
|
+
private getConfig: () => BetterTuiConfig;
|
|
58
|
+
private getTheme: () => Theme;
|
|
59
|
+
private cwd: string;
|
|
60
|
+
private contextExpanded: boolean;
|
|
61
|
+
private invalidateSelf: () => void;
|
|
62
|
+
/** Line → item of the last fold host render, for clicks. */
|
|
63
|
+
private foldTargets: string[] = [];
|
|
64
|
+
|
|
65
|
+
constructor(options: RowViewOptions) {
|
|
66
|
+
this.timeline = options.timeline;
|
|
67
|
+
this.toolCallId = options.toolCallId;
|
|
68
|
+
this.row = options.row;
|
|
69
|
+
this.getConfig = options.getConfig;
|
|
70
|
+
this.getTheme = options.getTheme;
|
|
71
|
+
this.cwd = options.cwd;
|
|
72
|
+
this.contextExpanded = options.contextExpanded;
|
|
73
|
+
this.invalidateSelf = options.invalidate;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
update(options: Omit<RowViewOptions, "timeline" | "toolCallId" | "row">): void {
|
|
77
|
+
this.getConfig = options.getConfig;
|
|
78
|
+
this.getTheme = options.getTheme;
|
|
79
|
+
this.cwd = options.cwd;
|
|
80
|
+
this.contextExpanded = options.contextExpanded;
|
|
81
|
+
this.invalidateSelf = options.invalidate;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A session seed may replace the row object after the component was built. */
|
|
85
|
+
private currentRow(): ToolRow {
|
|
86
|
+
return this.timeline.row(this.toolCallId) ?? this.row;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
render(width: number): string[] {
|
|
90
|
+
if (width <= 0) return [];
|
|
91
|
+
const theme = this.getTheme();
|
|
92
|
+
const config = this.getConfig();
|
|
93
|
+
|
|
94
|
+
// Fold (fold-more.md §5.6): decided on every render, so a segment crossing the threshold
|
|
95
|
+
// or a late seed changes what this row draws without pi calling renderCall again.
|
|
96
|
+
const role = this.timeline.foldRole(this.toolCallId);
|
|
97
|
+
if (role.kind === "hidden") return [];
|
|
98
|
+
if (role.kind === "host") return this.renderHost(role.segment, width, theme, config);
|
|
99
|
+
|
|
100
|
+
const row = this.currentRow();
|
|
101
|
+
const group = row.group;
|
|
102
|
+
// Only the first member draws a group; the others stay empty even if pi kept their view.
|
|
103
|
+
if (group && group.members[0] !== row) return [];
|
|
104
|
+
const expanded = group ? group.expanded || this.contextExpanded : row.expanded || this.contextExpanded;
|
|
105
|
+
// Markers sit in column 0 of the shared gutter (DESIGN.md §2); pi's outputPad is not used here.
|
|
106
|
+
const pad = MARKER_PAD;
|
|
107
|
+
|
|
108
|
+
if (group && group.members.length > 1) {
|
|
109
|
+
return this.renderGroup(group, width, pad, theme, expanded);
|
|
110
|
+
}
|
|
111
|
+
return this.renderSingle(row, width, pad, theme, config, expanded);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
handleMouse(event: { type?: string; button?: string; y?: number }): { handled: boolean } | undefined {
|
|
115
|
+
if (event.type !== "click" || event.button !== "left" || typeof event.y !== "number") return undefined;
|
|
116
|
+
const role = this.timeline.foldRole(this.toolCallId);
|
|
117
|
+
if (role.kind === "hidden") return undefined;
|
|
118
|
+
if (role.kind === "host") {
|
|
119
|
+
if (!clickSegment(this.timeline, role.segment, this.foldTargets, event.y)) return undefined;
|
|
120
|
+
this.invalidateSelf();
|
|
121
|
+
return { handled: true };
|
|
122
|
+
}
|
|
123
|
+
if (event.y !== 0) return undefined;
|
|
124
|
+
const row = this.currentRow();
|
|
125
|
+
const group = row.group;
|
|
126
|
+
if (group && group.members.length > 1) this.timeline.toggleGroup(group);
|
|
127
|
+
else this.timeline.toggleRow(row);
|
|
128
|
+
this.invalidateSelf();
|
|
129
|
+
return { handled: true };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
invalidate(): void {
|
|
133
|
+
// Rendering is derived from the live timeline; nothing is cached here.
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private renderHost(segment: Segment, width: number, theme: Theme, config: BetterTuiConfig): string[] {
|
|
137
|
+
const paint = paintSegment(segment, width, {
|
|
138
|
+
timeline: this.timeline,
|
|
139
|
+
config,
|
|
140
|
+
theme,
|
|
141
|
+
cwd: this.cwd,
|
|
142
|
+
expanded: this.contextExpanded,
|
|
143
|
+
});
|
|
144
|
+
this.foldTargets = paint.targets;
|
|
145
|
+
return paint.lines;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private renderGroup(
|
|
149
|
+
group: ToolGroup,
|
|
150
|
+
width: number,
|
|
151
|
+
pad: string,
|
|
152
|
+
theme: Theme,
|
|
153
|
+
expanded: boolean,
|
|
154
|
+
): string[] {
|
|
155
|
+
const counts = groupCounts(group);
|
|
156
|
+
const failed = groupFailed(group);
|
|
157
|
+
const active = groupActive(group);
|
|
158
|
+
// A process line, not an event (DESIGN.md §2.1): column 0 stays blank and the line is dim;
|
|
159
|
+
// running and done differ by tense (Exploring / Explored). Only a failure gets a marker.
|
|
160
|
+
const marker = failed > 0
|
|
161
|
+
? theme.fg(failed === counts.calls ? "error" : "warning", failed === counts.calls ? "×" : "!")
|
|
162
|
+
: " ";
|
|
163
|
+
const action = groupAction(group);
|
|
164
|
+
const summary = groupSummary(group) || `${counts.calls} tools`;
|
|
165
|
+
const duration = groupDuration(group);
|
|
166
|
+
const failure = failed > 0 ? theme.fg("error", ` · ${failed} failed`) : "";
|
|
167
|
+
// An interrupt is a count on the process line, never a marker (DESIGN.md §9).
|
|
168
|
+
const stopped = groupInterrupted(group);
|
|
169
|
+
const interrupted = stopped > 0 ? theme.fg("warning", ` · ${stopped} interrupted`) : "";
|
|
170
|
+
const time = duration >= 500 ? theme.fg("dim", ` · ${formatElapsed(duration)}`) : "";
|
|
171
|
+
const disclosure = theme.fg("dim", expanded || active || failed > 0 ? " ▾" : " ▸");
|
|
172
|
+
const header = `${pad}${marker} ${theme.fg("dim", `${action} ${summary}`)}${failure}${interrupted}${time}${disclosure}`;
|
|
173
|
+
|
|
174
|
+
const lines = [header];
|
|
175
|
+
const selected = expanded
|
|
176
|
+
? group.members
|
|
177
|
+
: failed > 0
|
|
178
|
+
? group.members.filter((member) => isFailed(member.result)).slice(-2)
|
|
179
|
+
: active
|
|
180
|
+
? group.members.slice(-2)
|
|
181
|
+
: [];
|
|
182
|
+
|
|
183
|
+
for (let index = 0; index < selected.length; index++) {
|
|
184
|
+
const member = selected[index]!;
|
|
185
|
+
const isLast = index === selected.length - 1;
|
|
186
|
+
lines.push(`${pad}${childLine(member, isLast ? "└" : "├", width, theme, this.cwd)}`);
|
|
187
|
+
if (expanded) {
|
|
188
|
+
lines.push(...this.renderMemberDetail(member, width, pad, isLast, theme, 8));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return lines.map((line) => truncateToWidth(line, width, "…"));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private renderSingle(
|
|
196
|
+
row: ToolRow,
|
|
197
|
+
width: number,
|
|
198
|
+
pad: string,
|
|
199
|
+
theme: Theme,
|
|
200
|
+
config: BetterTuiConfig,
|
|
201
|
+
expanded: boolean,
|
|
202
|
+
): string[] {
|
|
203
|
+
const running = row.endedAt === undefined;
|
|
204
|
+
// With fold on, a running command is the same fixed-height block a segment host draws
|
|
205
|
+
// (fold-more.md §5.8); with fold off the preview stays as it was.
|
|
206
|
+
const block = this.timeline.foldEnabled && isShellTool(row.name) && running && !expanded;
|
|
207
|
+
const lines = [this.renderSingleHeader(row, width, pad, theme, expanded, block)];
|
|
208
|
+
if (expanded) {
|
|
209
|
+
if (isShellTool(row.name)) lines.push(...commandLines(row, width, `${pad} `, theme));
|
|
210
|
+
lines.push(...this.renderMemberDetail(row, width, pad, true, theme, 14, true));
|
|
211
|
+
} else if (isFailed(row.result)) {
|
|
212
|
+
// An interrupted call shows no detail: its last line only repeats `interrupted`.
|
|
213
|
+
lines.push(...this.renderMemberDetail(row, width, pad, true, theme, 2, true));
|
|
214
|
+
} else if (block) {
|
|
215
|
+
// Under one second the command stays its header line (fold-more.md §5.8).
|
|
216
|
+
if (showsRunningBlock(row)) {
|
|
217
|
+
lines.push(...runningBlock(row, width, theme, `${pad}${SINGLE_DETAIL_RAIL}`, config.tools.shellPreviewLines));
|
|
218
|
+
}
|
|
219
|
+
} else if (isShellTool(row.name) && running && config.tools.shellPreviewLines > 0) {
|
|
220
|
+
const preview = resultLines({ content: [{ type: "text", text: row.partialText }], isError: false }, config.tools.shellPreviewLines, "tail");
|
|
221
|
+
for (let index = 0; index < preview.length; index++) {
|
|
222
|
+
const rail = index === 0 ? " └ " : " ";
|
|
223
|
+
lines.push(`${pad}${theme.fg("dim", rail)}${theme.fg("toolOutput", preview[index]!)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return lines.map((line) => truncateToWidth(line, width, "…"));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private renderSingleHeader(row: ToolRow, width: number, pad: string, theme: Theme, expanded: boolean, block = false): string {
|
|
230
|
+
const skill = skillReadName(row.name, row.args);
|
|
231
|
+
if (skill !== undefined) return this.renderSkillHeader(row, skill, pad, theme, expanded);
|
|
232
|
+
const label = TOOL_LABELS[row.name] ?? row.name;
|
|
233
|
+
const detail = sanitizeText(callDetail(row.name, row.args, this.cwd));
|
|
234
|
+
const labelText = theme.fg("toolTitle", theme.bold(label));
|
|
235
|
+
const detailText = detail ? ` ${theme.fg("muted", `(${detail})`)}` : "";
|
|
236
|
+
if (isInterrupted(row.result)) {
|
|
237
|
+
// Same `●` as every single row; warning says it did not finish, without calling it a
|
|
238
|
+
// failure, in the color of `✻ Interrupted` below (DESIGN.md §9).
|
|
239
|
+
const time = isShellTool(row.name) || this.rowDuration(row) >= 1_000
|
|
240
|
+
? theme.fg("dim", ` · ${formatElapsed(this.rowDuration(row))}`)
|
|
241
|
+
: "";
|
|
242
|
+
return `${pad}${theme.fg("warning", "●")} ${labelText}${detailText}${theme.fg("warning", " · interrupted")}${time}`;
|
|
243
|
+
}
|
|
244
|
+
const marker = row.result?.isError
|
|
245
|
+
? theme.fg("error", "×")
|
|
246
|
+
: row.endedAt === undefined
|
|
247
|
+
? theme.fg("accent", "●")
|
|
248
|
+
: theme.fg("success", "●");
|
|
249
|
+
const summary = block ? runningStatus(row) : this.singleSummary(row);
|
|
250
|
+
const summaryText = summary
|
|
251
|
+
? theme.fg(row.result?.isError ? "error" : "dim", ` · ${summary}`)
|
|
252
|
+
: "";
|
|
253
|
+
return `${pad}${marker} ${labelText}${detailText}${summaryText}`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A read of `…/<name>/SKILL.md`, drawn like a `/skill:name` invocation. */
|
|
257
|
+
private renderSkillHeader(row: ToolRow, name: string, pad: string, theme: Theme, expanded: boolean): string {
|
|
258
|
+
if (row.result?.isError) {
|
|
259
|
+
const label = theme.fg("customMessageLabel", theme.bold("[Skill]"));
|
|
260
|
+
// The error text itself follows on the detail lines below the header.
|
|
261
|
+
const summary = theme.fg("error", " · Failed");
|
|
262
|
+
return `${pad}${theme.fg("error", "×")} ${label} ${theme.fg("muted", `(${name})`)}${summary}`;
|
|
263
|
+
}
|
|
264
|
+
return `${pad}${skillHeader(theme, name, expanded)}`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private singleSummary(row: ToolRow): string {
|
|
268
|
+
if (row.result) {
|
|
269
|
+
const base = summarizeResult(row.name, row.result, row.args);
|
|
270
|
+
const duration = this.rowDuration(row);
|
|
271
|
+
const time = duration >= 1_000 || isShellTool(row.name) ? ` · ${formatElapsed(duration)}` : "";
|
|
272
|
+
return base + time;
|
|
273
|
+
}
|
|
274
|
+
if (row.endedAt !== undefined) return "Done";
|
|
275
|
+
return isShellTool(row.name) ? `Running… · ${formatElapsed(this.rowDuration(row))}` : "Running…";
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private rowDuration(row: ToolRow): number {
|
|
279
|
+
return Math.max(0, (row.endedAt ?? Date.now()) - row.startedAt);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private renderMemberDetail(
|
|
283
|
+
row: ToolRow,
|
|
284
|
+
width: number,
|
|
285
|
+
pad: string,
|
|
286
|
+
isLast: boolean,
|
|
287
|
+
_theme: Theme,
|
|
288
|
+
maxLines: number,
|
|
289
|
+
standalone = false,
|
|
290
|
+
): string[] {
|
|
291
|
+
const rail = standalone ? " " : isLast ? " " : " │ ";
|
|
292
|
+
return memberDetailLines(row, width, _theme, `${pad}${rail}`, maxLines);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function ensureRow(
|
|
297
|
+
deps: ToolRenderDeps,
|
|
298
|
+
toolName: string,
|
|
299
|
+
args: unknown,
|
|
300
|
+
context: RenderContext,
|
|
301
|
+
): ToolRow {
|
|
302
|
+
const existing = deps.timeline.row(context.toolCallId);
|
|
303
|
+
if (existing) {
|
|
304
|
+
existing.args = argsOf(args);
|
|
305
|
+
existing.invalidate = context.invalidate;
|
|
306
|
+
deps.timeline.detachSkillRead(context.toolCallId);
|
|
307
|
+
return existing;
|
|
308
|
+
}
|
|
309
|
+
const row = deps.timeline.begin(
|
|
310
|
+
context.toolCallId,
|
|
311
|
+
toolName,
|
|
312
|
+
args,
|
|
313
|
+
Date.now(),
|
|
314
|
+
deps.getConfig().tools.groupReadOnly,
|
|
315
|
+
);
|
|
316
|
+
row.args = argsOf(args);
|
|
317
|
+
row.invalidate = context.invalidate;
|
|
318
|
+
return row;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function createToolRenderers(
|
|
322
|
+
toolName: string,
|
|
323
|
+
_native: ToolRenderers | undefined,
|
|
324
|
+
deps: ToolRenderDeps,
|
|
325
|
+
): ToolRenderers {
|
|
326
|
+
return {
|
|
327
|
+
renderShell: "self",
|
|
328
|
+
renderCall(args, _theme, context) {
|
|
329
|
+
const row = ensureRow(deps, toolName, args, context);
|
|
330
|
+
if (context.executionStarted && row.endedAt === undefined) {
|
|
331
|
+
row.startedAt ||= Date.now();
|
|
332
|
+
// Normally set by tool_execution_start already; a row rebuilt after it still needs it.
|
|
333
|
+
row.execStartedAt ??= Date.now();
|
|
334
|
+
}
|
|
335
|
+
// Ctrl+O reaches tool rows only; thinking hosts read it from here (fold-more.md §5.11).
|
|
336
|
+
deps.timeline.globalExpanded = context.expanded;
|
|
337
|
+
if (row.group && row.group.members[0] !== row) return EMPTY_COMPONENT;
|
|
338
|
+
|
|
339
|
+
const previous = context.lastComponent;
|
|
340
|
+
const view = previous instanceof ToolRowView
|
|
341
|
+
? previous
|
|
342
|
+
: new ToolRowView({
|
|
343
|
+
timeline: deps.timeline,
|
|
344
|
+
toolCallId: context.toolCallId,
|
|
345
|
+
row,
|
|
346
|
+
getConfig: deps.getConfig,
|
|
347
|
+
getTheme: deps.getTheme,
|
|
348
|
+
cwd: context.cwd,
|
|
349
|
+
contextExpanded: context.expanded,
|
|
350
|
+
invalidate: context.invalidate,
|
|
351
|
+
});
|
|
352
|
+
view.update({
|
|
353
|
+
getConfig: deps.getConfig,
|
|
354
|
+
getTheme: deps.getTheme,
|
|
355
|
+
cwd: context.cwd,
|
|
356
|
+
contextExpanded: context.expanded,
|
|
357
|
+
invalidate: context.invalidate,
|
|
358
|
+
});
|
|
359
|
+
return view;
|
|
360
|
+
},
|
|
361
|
+
renderResult(result, options, _theme, context) {
|
|
362
|
+
const row = ensureRow(deps, toolName, context.args, context);
|
|
363
|
+
row.invalidate = context.invalidate;
|
|
364
|
+
// pi also hands running output to renderResult; it is a preview, not the end of the call.
|
|
365
|
+
if (options.isPartial && row.endedAt === undefined) {
|
|
366
|
+
deps.timeline.update(context.toolCallId, result);
|
|
367
|
+
deps.timeline.refreshHost(row.id);
|
|
368
|
+
return EMPTY_COMPONENT;
|
|
369
|
+
}
|
|
370
|
+
// A redraw (for example Ctrl+O) must not turn a restored result into a
|
|
371
|
+
// new one: keep the persisted end timestamp when the row already has it.
|
|
372
|
+
const endedAt = row.endedAt ?? Date.now();
|
|
373
|
+
deps.timeline.setResult(context.toolCallId, {
|
|
374
|
+
content: result.content,
|
|
375
|
+
details: result.details,
|
|
376
|
+
// pi keeps the error flag on the context, not on the result object.
|
|
377
|
+
isError: context.isError === true || (result as { isError?: unknown }).isError === true,
|
|
378
|
+
durationMs: context.durationMs,
|
|
379
|
+
}, endedAt);
|
|
380
|
+
row.invalidate = context.invalidate;
|
|
381
|
+
if (row.group && row.group.members[0] !== row) deps.timeline.refresh(row.id);
|
|
382
|
+
// The host of a fold segment draws this result; it never invalidates this row back.
|
|
383
|
+
deps.timeline.refreshHost(row.id);
|
|
384
|
+
return EMPTY_COMPONENT;
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
}
|