-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtool-execution.ts
More file actions
315 lines (285 loc) · 9.4 KB
/
Copy pathtool-execution.ts
File metadata and controls
315 lines (285 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { Container, Spacer, type TUI } from "@earendil-works/pi-tui";
import type { ToolDef } from "../../../core/tools/index.ts";
import { GrokToolRow } from "../grok/tool-row.ts";
import { readToolProgress } from "../tool-progress.ts";
import { createBoundedRenderSignature } from "./render-signature.ts";
import { hasCompletedTodoTasks, TODO_STRIKE_FRAME_INTERVAL_MS, TODO_STRIKE_TOTAL_FRAMES } from "./todo-strike.ts";
import { ToolExecutionImages } from "./tool-execution-images.ts";
import { ToolExecutionRenderer } from "./tool-execution-renderer.ts";
import type { ToolExecutionIdentity, ToolExecutionRenderState, ToolExecutionResult } from "./tool-execution-types.ts";
export interface ToolExecutionOptions {
showImages?: boolean;
imageWidthCells?: number;
}
/** Visual shell chosen by interactive chrome; classic remains the default. */
export type ToolExecutionPresentation = "classic" | "grok";
const PENDING_RENDER_FRAME_INTERVAL_MS = 80;
export class ToolExecutionComponent extends Container {
private readonly identity: ToolExecutionIdentity;
private readonly ui: TUI;
private readonly renderer: ToolExecutionRenderer | undefined;
private readonly images: ToolExecutionImages | undefined;
private readonly grokRow: GrokToolRow | undefined;
private readonly presentation: ToolExecutionPresentation;
private args: unknown;
private expanded = false;
private showImages: boolean;
private imageWidthCells: number;
private isPartial = true;
private executionStarted = false;
private argsComplete = false;
private spinnerFrame?: number;
private spinnerInterval?: NodeJS.Timeout;
private todoStrikeInterval?: NodeJS.Timeout;
private result?: ToolExecutionResult;
private cachedLines?: string[];
private cachedRevision?: number;
private cachedSignature?: string;
private cachedWidth?: number;
private lastDisplaySignature?: string;
constructor(
toolName: string,
toolCallId: string,
args: unknown,
options: ToolExecutionOptions = {},
toolDefinition: ToolDef | undefined,
ui: TUI,
cwd: string,
presentation: ToolExecutionPresentation = "classic",
) {
super();
this.identity = { toolName, toolCallId, cwd, toolDefinition };
this.args = args;
this.showImages = options.showImages ?? true;
this.imageWidthCells = options.imageWidthCells ?? 60;
this.ui = ui;
this.presentation = presentation;
const initialState = this.createRenderState();
if (this.presentation === "grok") {
this.grokRow = new GrokToolRow({
toolName: this.identity.toolName,
isPartial: initialState.isPartial,
result: initialState.result,
});
this.addChild(new Spacer(1));
this.addChild(this.grokRow);
} else {
this.renderer = new ToolExecutionRenderer(this.identity, initialState, () => {
this.invalidate();
this.ui.requestRender();
});
this.images = new ToolExecutionImages(() => {
this.invalidateRenderCache();
this.ui.requestRender();
});
this.addChild(new Spacer(1));
this.addChild(this.renderer);
this.addChild(this.images);
}
this.updateSpinnerAnimation();
this.updateDisplay();
}
updateArgs(args: unknown): void {
this.args = args;
this.lastDisplaySignature = undefined;
this.updateSpinnerAnimation();
this.updateDisplay();
}
markExecutionStarted(): void {
this.executionStarted = true;
this.updateSpinnerAnimation();
this.updateDisplay();
this.ui.requestRender();
}
setArgsComplete(): void {
this.argsComplete = true;
this.updateSpinnerAnimation();
this.updateDisplay();
this.ui.requestRender();
}
updateResult(result: ToolExecutionResult, isPartial = false): void {
this.result = result;
this.isPartial = isPartial;
if (!isPartial) this.argsComplete = true;
this.lastDisplaySignature = undefined;
this.updateSpinnerAnimation();
this.updateTodoStrikeAnimation();
this.updateDisplay();
this.images?.updateResult(result);
this.invalidateRenderCache();
}
stopAnimation(): void {
this.stopSpinnerAnimation();
this.stopTodoStrikeAnimation();
}
override dispose(): void {
this.stopAnimation();
super.dispose();
}
setExpanded(expanded: boolean): void {
this.expanded = expanded;
this.updateDisplay();
}
setShowImages(show: boolean): void {
this.showImages = show;
this.updateDisplay();
}
setImageWidthCells(width: number): void {
this.imageWidthCells = Math.max(1, Math.floor(width));
this.updateDisplay();
}
override invalidate(): void {
this.invalidateRenderCache(false);
super.invalidate();
this.lastDisplaySignature = undefined;
this.updateDisplay();
}
override render(width: number): string[] {
if (this.presentation === "grok") return super.render(width);
const signature = this.createRenderSignature();
const revision = this.getRenderRevision();
if (
this.cachedLines &&
this.cachedWidth === width &&
this.cachedSignature === signature &&
this.cachedRevision === revision
) {
return this.cachedLines;
}
let lines: string[];
const renderer = this.renderer!;
const images = this.images!;
if (renderer.hasRendererDefinition && renderer.renderShell === "self") {
const contentLines = renderer.render(width);
const imageLines = images.render(width);
if (contentLines.length === 0 && imageLines.length === 0) return [];
lines = contentLines.length > 0 ? ["", ...contentLines, ...imageLines] : imageLines;
} else {
lines = super.render(width);
}
this.cachedWidth = width;
this.cachedSignature = signature;
this.cachedRevision = this.getRenderRevision();
this.cachedLines = lines;
return lines;
}
private updateDisplay(): void {
const displaySignature = this.createRenderSignature();
if (this.lastDisplaySignature === displaySignature) return;
this.lastDisplaySignature = displaySignature;
this.invalidateRenderCache();
const state = this.createRenderState();
if (this.grokRow) {
this.grokRow.update({
toolName: this.identity.toolName,
isPartial: state.isPartial,
result: state.result,
});
return;
}
const renderer = this.renderer!;
this.renderer!.update(state);
this.images!.updateOptions({
showImages: state.showImages,
maxWidthCells: this.imageWidthCells,
showRendererFallback: renderer.hasResultRenderer,
});
}
private createRenderState(): ToolExecutionRenderState {
return {
args: this.args,
executionStarted: this.executionStarted,
argsComplete: this.argsComplete,
isPartial: this.isPartial,
expanded: this.expanded,
showImages: this.showImages,
spinnerFrame: this.spinnerFrame,
result: this.result,
};
}
private createRenderSignature(): string {
return createBoundedRenderSignature({
...this.createRenderState(),
imageWidthCells: this.imageWidthCells,
toolCallId: this.identity.toolCallId,
toolName: this.identity.toolName,
});
}
private updateSpinnerAnimation(): void {
const isStreamingArgs = !this.argsComplete && ["edit", "write", "apply_patch"].includes(this.identity.toolName);
const isPartialTask = this.isPartial && this.identity.toolName === "task" && this.result !== undefined;
const isPartialProgress =
this.isPartial && this.result !== undefined && readToolProgress(this.result.details) !== undefined;
if (isStreamingArgs || isPartialTask || isPartialProgress) this.startSpinnerAnimation();
else this.stopSpinnerAnimation();
}
private updateTodoStrikeAnimation(): void {
const shouldAnimate =
this.identity.toolName === "todo" &&
this.executionStarted &&
!this.isPartial &&
this.result !== undefined &&
!this.result.isError &&
hasCompletedTodoTasks(this.result.details);
if (!shouldAnimate) {
this.stopTodoStrikeAnimation();
return;
}
if (this.todoStrikeInterval) return;
this.spinnerFrame = 0;
this.todoStrikeInterval = setInterval(() => {
const next = (this.spinnerFrame ?? 0) + 1;
if (next > TODO_STRIKE_TOTAL_FRAMES) {
this.stopTodoStrikeAnimation();
return;
}
this.spinnerFrame = next;
this.invalidateRenderCache();
this.updateDisplay();
this.ui.requestRender();
}, TODO_STRIKE_FRAME_INTERVAL_MS);
this.todoStrikeInterval.unref?.();
}
private stopTodoStrikeAnimation(): void {
if (this.todoStrikeInterval) {
clearInterval(this.todoStrikeInterval);
this.todoStrikeInterval = undefined;
}
if (!this.spinnerInterval && this.spinnerFrame !== undefined) {
this.spinnerFrame = undefined;
this.invalidateRenderCache();
this.updateDisplay();
this.ui.requestRender();
}
}
private startSpinnerAnimation(): void {
if (this.spinnerInterval) return;
this.spinnerInterval = setInterval(() => {
this.spinnerFrame = ((this.spinnerFrame ?? -1) + 1) % 10;
this.invalidateRenderCache();
this.updateDisplay();
this.ui.requestRender();
}, PENDING_RENDER_FRAME_INTERVAL_MS);
this.spinnerInterval.unref?.();
}
private stopSpinnerAnimation(): void {
if (!this.spinnerInterval) return;
clearInterval(this.spinnerInterval);
this.spinnerInterval = undefined;
if (!this.todoStrikeInterval) this.spinnerFrame = undefined;
this.invalidateRenderCache();
}
override isRenderCacheTrackable(): boolean {
// Renderer components receive state changes only through this shell. Async
// renderers use the supplied invalidate callback, so the shell is a safe
// revision boundary even when an extension component is not trackable.
return true;
}
private invalidateRenderCache(notifyParent = true): void {
this.cachedLines = undefined;
this.cachedRevision = undefined;
this.cachedSignature = undefined;
this.cachedWidth = undefined;
if (notifyParent) this.markRenderInvalidated();
}
}