@bismawy/pi-arnative 0.16.1 → 0.18.0
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 +25 -16
- package/extensions/index.ts +41 -0
- package/lib/active-clock.ts +91 -0
- package/lib/ansi.ts +40 -78
- package/lib/box.ts +49 -68
- package/lib/chatbox.ts +371 -0
- package/lib/color.ts +3 -31
- package/lib/compat.ts +0 -60
- package/lib/edit-diff.ts +199 -0
- package/{extensions → lib/features}/arnative.ts +96 -423
- package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
- package/{extensions → lib/features}/budget.ts +17 -242
- package/lib/features/footer.ts +2232 -0
- package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
- package/{extensions → lib/features}/model-heal.ts +5 -28
- package/lib/features/progress-group.ts +1557 -0
- package/{extensions → lib/features}/render-cache.ts +113 -161
- package/{extensions → lib/features}/timestamps.ts +18 -94
- package/{extensions → lib/features}/todos.ts +44 -158
- package/lib/features/tools.ts +1202 -0
- package/lib/features/ui-render-tweaks.ts +606 -0
- package/lib/features/usage.ts +786 -0
- package/lib/format.ts +22 -4
- package/lib/git-status.ts +164 -0
- package/lib/list-picker.ts +1 -127
- package/lib/model-heal.ts +70 -162
- package/lib/patch.ts +0 -63
- package/lib/preset-store.ts +17 -133
- package/lib/provider-store.ts +573 -0
- package/lib/provider.ts +52 -851
- package/lib/runtime.ts +209 -0
- package/lib/shortcuts.ts +8 -58
- package/lib/spinner.ts +88 -162
- package/lib/status-widget.ts +4 -30
- package/lib/todos.ts +20 -143
- package/lib/usage-pricing.ts +143 -0
- package/lib/usage-store.ts +44 -378
- package/package.json +5 -6
- package/themes/arnative-blossom.json +0 -0
- package/themes/arnative-crimson.json +0 -0
- package/themes/arnative-cyberpunk.json +0 -0
- package/themes/arnative-dracula.json +0 -0
- package/themes/arnative-earthy.json +0 -0
- package/themes/arnative-garden.json +0 -0
- package/themes/arnative-gruvbox.json +0 -0
- package/themes/arnative-lemonade.json +0 -0
- package/themes/arnative-magical.json +0 -0
- package/themes/arnative-matrix.json +0 -0
- package/themes/arnative-neutral.json +0 -0
- package/themes/arnative-nord.json +0 -0
- package/themes/arnative-zinc.json +0 -0
- package/themes/arnative.json +0 -0
- package/CHANGELOG.md +0 -683
- package/extensions/animations.ts +0 -242
- package/extensions/footer.ts +0 -2617
- package/extensions/tools.ts +0 -2231
- package/extensions/ui-render-tweaks.ts +0 -1005
- package/extensions/usage-export.ts +0 -283
- package/extensions/usage.ts +0 -1406
- package/lib/check.ts +0 -21
- package/themes/gen-themes.mjs +0 -715
|
@@ -0,0 +1,1557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ADE-style chronological phase progress box for tool-heavy turns.
|
|
3
|
+
*
|
|
4
|
+
* Projects contiguous tool-execution turns (intermediate assistant messages with tool calls +
|
|
5
|
+
* their ToolExecutionComponents) into a single virtual TurnProgressBoxComponent at render time
|
|
6
|
+
* without mutating `chatContainer.children`. The final conclusion AssistantMessageComponent
|
|
7
|
+
* always renders normally below the progress box.
|
|
8
|
+
*/
|
|
9
|
+
import { basename, dirname, relative } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
AssistantMessageComponent,
|
|
12
|
+
InteractiveMode,
|
|
13
|
+
ToolExecutionComponent,
|
|
14
|
+
} from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
16
|
+
import { getActiveTheme, softOf, stripAnsi } from "../ansi.ts";
|
|
17
|
+
import { boxRow, createHoldRepeat, inSpans, type BoxTheme, type HoldRepeat, type TextSpan } from "../box.ts";
|
|
18
|
+
import { chatboxPrimaryPaint } from "../chatbox.ts";
|
|
19
|
+
import { patchOnce } from "../patch.ts";
|
|
20
|
+
import { TOOL_LAYOUT_KEY, choiceSetting } from "../preset-store.ts";
|
|
21
|
+
import { expandedBoxBodyLimit, fmtMs, getToolTiming, paintScrollEdge, shortPath, spinIcon } from "./tools.ts";
|
|
22
|
+
|
|
23
|
+
export type ToolLayoutMode = "Grouped" | "Classic";
|
|
24
|
+
export const TOOL_LAYOUT_CHOICES: readonly ToolLayoutMode[] = ["Grouped", "Classic"];
|
|
25
|
+
|
|
26
|
+
const toolLayoutSetting = choiceSetting(TOOL_LAYOUT_KEY, TOOL_LAYOUT_CHOICES, "Grouped");
|
|
27
|
+
const layoutListeners = new Set<() => void>();
|
|
28
|
+
|
|
29
|
+
export const activeToolLayout = (): ToolLayoutMode => toolLayoutSetting.get();
|
|
30
|
+
|
|
31
|
+
export function onToolLayoutChange(fn: () => void): void {
|
|
32
|
+
layoutListeners.add(fn);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function setToolLayout(mode: ToolLayoutMode): void {
|
|
36
|
+
toolLayoutSetting.set(mode);
|
|
37
|
+
for (const fn of layoutListeners) fn();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type PhaseCategory = "explore" | "edit" | "run" | "other";
|
|
41
|
+
|
|
42
|
+
export interface ToolLike {
|
|
43
|
+
toolName?: string;
|
|
44
|
+
toolCallId?: string;
|
|
45
|
+
args?: Record<string, unknown>;
|
|
46
|
+
expanded?: boolean;
|
|
47
|
+
isPartial?: boolean;
|
|
48
|
+
hideComponent?: boolean;
|
|
49
|
+
cwd?: string;
|
|
50
|
+
result?: {
|
|
51
|
+
content?: Array<{ type?: string; text?: string }>;
|
|
52
|
+
details?: Record<string, unknown>;
|
|
53
|
+
isError?: boolean;
|
|
54
|
+
durationMs?: number;
|
|
55
|
+
};
|
|
56
|
+
ui?: { requestRender?: () => void };
|
|
57
|
+
setExpanded?: (expanded: boolean) => void;
|
|
58
|
+
render: (width: number) => readonly string[];
|
|
59
|
+
handleMouse?: (event: unknown) => unknown;
|
|
60
|
+
invalidate?: () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface AssistantLike {
|
|
64
|
+
hasToolCalls?: boolean;
|
|
65
|
+
isStreaming?: boolean;
|
|
66
|
+
lastMessage?: {
|
|
67
|
+
content?: Array<{ type?: string; text?: string; thinking?: string }>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ProgressPhase {
|
|
72
|
+
title: string;
|
|
73
|
+
/** Additional narration lines after `title`, shown when the phase is expanded. */
|
|
74
|
+
narrationLines?: string[];
|
|
75
|
+
explicitTitle: boolean;
|
|
76
|
+
category: PhaseCategory;
|
|
77
|
+
tools: ToolLike[];
|
|
78
|
+
running: boolean;
|
|
79
|
+
hasError: boolean;
|
|
80
|
+
errorCount: number;
|
|
81
|
+
durationMs: number;
|
|
82
|
+
added: number;
|
|
83
|
+
removed: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface TurnBoxState {
|
|
87
|
+
/** Explicit user overrides per phase index. */
|
|
88
|
+
phaseOverrides: Map<number, boolean>;
|
|
89
|
+
/** Explicit user overrides for nested tool detail previews. */
|
|
90
|
+
detailOpen: WeakMap<object, boolean>;
|
|
91
|
+
/** Explicit user override for whole-box open/collapsed state; undefined auto-opens while running and collapses when finished. */
|
|
92
|
+
boxExpanded?: boolean;
|
|
93
|
+
/** Header [expand all] / [collapse all] override. */
|
|
94
|
+
allOverride?: boolean;
|
|
95
|
+
/** Manual viewport scroll offset into logical body lines; null/undefined auto-anchors. */
|
|
96
|
+
scrollOffset?: number | null;
|
|
97
|
+
/** Monotonic version bumped on every local interaction for render-cache invalidation. */
|
|
98
|
+
interactionVersion: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface HitPhaseRow {
|
|
102
|
+
row: number;
|
|
103
|
+
phaseIndex: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface HitToolRow {
|
|
107
|
+
row: number;
|
|
108
|
+
tool: ToolLike;
|
|
109
|
+
hintSpans: TextSpan[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface HitDetailRegion {
|
|
113
|
+
startRow: number;
|
|
114
|
+
endRow: number;
|
|
115
|
+
rowOffset: number;
|
|
116
|
+
indentCols: number;
|
|
117
|
+
width: number;
|
|
118
|
+
tool: ToolLike;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface RenderedHitMap {
|
|
122
|
+
width: number;
|
|
123
|
+
maxBody: number;
|
|
124
|
+
boxExpanded: boolean;
|
|
125
|
+
summaryRow: number | null;
|
|
126
|
+
headerBoxToggleSpans: TextSpan[];
|
|
127
|
+
headerToggleSpans: TextSpan[];
|
|
128
|
+
scrollUpSpans: TextSpan[];
|
|
129
|
+
scrollDownSpans: TextSpan[];
|
|
130
|
+
scrollOffset: number;
|
|
131
|
+
maxOffset: number;
|
|
132
|
+
scrollStep: number;
|
|
133
|
+
allExpanded: boolean;
|
|
134
|
+
phases: HitPhaseRow[];
|
|
135
|
+
tools: HitToolRow[];
|
|
136
|
+
details: HitDetailRegion[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface ProgressMouseEvent {
|
|
140
|
+
type: string;
|
|
141
|
+
button?: string;
|
|
142
|
+
x: number;
|
|
143
|
+
y: number;
|
|
144
|
+
width: number;
|
|
145
|
+
height: number;
|
|
146
|
+
screenX: number;
|
|
147
|
+
screenY: number;
|
|
148
|
+
clickCount?: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function forwardMouseEvent(component: ToolLike, event: ProgressMouseEvent): unknown {
|
|
152
|
+
const result = component.handleMouse?.(event) as Record<string, unknown> | undefined;
|
|
153
|
+
if (!result) return undefined;
|
|
154
|
+
if ("target" in result) return result;
|
|
155
|
+
if (!result.handled && !result.capture && !result.focus) return undefined;
|
|
156
|
+
return {
|
|
157
|
+
...result,
|
|
158
|
+
handled: true,
|
|
159
|
+
...(result.focus ? { focusTarget: component } : {}),
|
|
160
|
+
target: {
|
|
161
|
+
component,
|
|
162
|
+
originX: event.screenX - event.x,
|
|
163
|
+
originY: event.screenY - event.y,
|
|
164
|
+
width: event.width,
|
|
165
|
+
height: event.height,
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const EXPAND_ALL_LABEL = "[expand all]";
|
|
171
|
+
const COLLAPSE_ALL_LABEL = "[collapse all]";
|
|
172
|
+
const SHOW_LABEL = "[show]";
|
|
173
|
+
const HIDE_LABEL = "[hide]";
|
|
174
|
+
|
|
175
|
+
const EXPLORE_TOOLS = new Set([
|
|
176
|
+
"read",
|
|
177
|
+
"grep",
|
|
178
|
+
"ffgrep",
|
|
179
|
+
"find",
|
|
180
|
+
"fffind",
|
|
181
|
+
"ls",
|
|
182
|
+
"lsp_diagnostics",
|
|
183
|
+
"web_search",
|
|
184
|
+
"fetch_content",
|
|
185
|
+
"get_search_content",
|
|
186
|
+
"source_check",
|
|
187
|
+
"memory_read",
|
|
188
|
+
"memory_search",
|
|
189
|
+
"memory_status",
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
const EDIT_TOOLS = new Set([
|
|
193
|
+
"edit",
|
|
194
|
+
"write",
|
|
195
|
+
"lsp_fix",
|
|
196
|
+
"memory_write",
|
|
197
|
+
"memory_forget",
|
|
198
|
+
"memory_restore",
|
|
199
|
+
"scratchpad",
|
|
200
|
+
"todo",
|
|
201
|
+
]);
|
|
202
|
+
|
|
203
|
+
const RUN_TOOLS = new Set(["bash", "powershell", "codemode", "mcpScript"]);
|
|
204
|
+
|
|
205
|
+
/** Classify a tool name into a chronological ADE phase category. */
|
|
206
|
+
export function classifyTool(toolName: string | undefined): PhaseCategory {
|
|
207
|
+
const name = (toolName ?? "").toLowerCase();
|
|
208
|
+
if (EXPLORE_TOOLS.has(name)) return "explore";
|
|
209
|
+
if (EDIT_TOOLS.has(name)) return "edit";
|
|
210
|
+
if (RUN_TOOLS.has(name)) return "run";
|
|
211
|
+
if (/(search|find|grep|read|list|get_|inspect|describe|status|query|fetch)/.test(name)) return "explore";
|
|
212
|
+
if (/(edit|write|create|update|delete|remove|patch|fix|save)/.test(name)) return "edit";
|
|
213
|
+
if (/(bash|shell|exec|run|powershell|terminal|eval|script)/.test(name)) return "run";
|
|
214
|
+
return "other";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function isToolComponent(child: unknown): child is ToolLike {
|
|
218
|
+
if (!child || typeof child !== "object") return false;
|
|
219
|
+
if (child instanceof ToolExecutionComponent) return true;
|
|
220
|
+
// Support lightweight test doubles with explicit marker.
|
|
221
|
+
return (child as Record<symbol, unknown>)[TEST_TOOL_MARKER] === true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function isIntermediateAssistant(child: unknown): child is AssistantLike {
|
|
225
|
+
if (!child || typeof child !== "object") return false;
|
|
226
|
+
if (child instanceof AssistantMessageComponent) {
|
|
227
|
+
return Boolean((child as unknown as AssistantLike).hasToolCalls);
|
|
228
|
+
}
|
|
229
|
+
return (
|
|
230
|
+
(child as Record<symbol, unknown>)[TEST_ASSISTANT_MARKER] === true &&
|
|
231
|
+
Boolean((child as AssistantLike).hasToolCalls)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function hasAssistantText(assistant: AssistantLike): boolean {
|
|
236
|
+
const blocks = assistant.lastMessage?.content;
|
|
237
|
+
if (!Array.isArray(blocks)) return false;
|
|
238
|
+
return blocks.some((b) => b?.type === "text" && typeof b.text === "string" && b.text.trim().length > 0);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** True for silent (tool-call / thinking only) intermediate assistant messages that belong inside a progress box. */
|
|
242
|
+
function isSilentIntermediateAssistant(child: unknown): child is AssistantLike {
|
|
243
|
+
return isIntermediateAssistant(child) && !hasAssistantText(child);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const TEST_TOOL_MARKER = Symbol.for("pi-arnative.testTool");
|
|
247
|
+
const TEST_ASSISTANT_MARKER = Symbol.for("pi-arnative.testAssistant");
|
|
248
|
+
const PROGRESS_BOX_MARKER = Symbol.for("pi-arnative.turnProgressBox");
|
|
249
|
+
const SCROLL_HOLD_KEY = Symbol.for("pi-arnative.toolBoxScrollHold");
|
|
250
|
+
const SCROLL_OFFSET_KEY = Symbol.for("pi-arnative.toolBoxScrollOffset");
|
|
251
|
+
|
|
252
|
+
export function isProgressBox(child: unknown): child is TurnProgressBoxComponent {
|
|
253
|
+
return Boolean(child && typeof child === "object" && (child as Record<symbol, unknown>)[PROGRESS_BOX_MARKER] === true);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function cleanNarrationLine(line: string): string {
|
|
257
|
+
return line
|
|
258
|
+
.replace(/^#{1,6}\s+/, "")
|
|
259
|
+
.replace(/^[-*•]\s+/, "")
|
|
260
|
+
.replace(/^\d+\.\s+/, "")
|
|
261
|
+
.replace(/\*\*(.*?)\*\*/g, "$1")
|
|
262
|
+
.replace(/`([^`]+)`/g, "$1")
|
|
263
|
+
.replace(/[::]\s*$/, "")
|
|
264
|
+
.trim();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Extract all non-empty cleaned narration lines from an intermediate assistant message. */
|
|
268
|
+
function extractNarrationLines(assistant: AssistantLike): string[] {
|
|
269
|
+
const blocks = assistant.lastMessage?.content;
|
|
270
|
+
if (!Array.isArray(blocks)) return [];
|
|
271
|
+
const out: string[] = [];
|
|
272
|
+
for (const block of blocks) {
|
|
273
|
+
if (block?.type !== "text" || typeof block.text !== "string") continue;
|
|
274
|
+
for (const raw of block.text.split(/\r?\n/)) {
|
|
275
|
+
const trimmed = raw.trim();
|
|
276
|
+
if (!trimmed) continue;
|
|
277
|
+
const cleaned = cleanNarrationLine(trimmed);
|
|
278
|
+
if (cleaned) out.push(cleaned);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Extract a concise single-line phase title from an intermediate assistant message's text content. */
|
|
285
|
+
export function extractNarration(assistant: AssistantLike): string {
|
|
286
|
+
return extractNarrationLines(assistant)[0] ?? "";
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const DIFF_STATS_CACHE = new WeakMap<object, { diff: string | undefined; added: number; removed: number }>();
|
|
290
|
+
|
|
291
|
+
/** Count added and removed lines in a unified diff string from `edit` tool details. */
|
|
292
|
+
export function countDiffStats(tool: ToolLike): { added: number; removed: number } {
|
|
293
|
+
const diff = (tool.result?.details as { diff?: unknown } | undefined)?.diff;
|
|
294
|
+
const diffStr = typeof diff === "string" && diff ? diff : undefined;
|
|
295
|
+
const cached = DIFF_STATS_CACHE.get(tool);
|
|
296
|
+
if (cached && cached.diff === diffStr) return { added: cached.added, removed: cached.removed };
|
|
297
|
+
if (!diffStr) {
|
|
298
|
+
DIFF_STATS_CACHE.set(tool, { diff: undefined, added: 0, removed: 0 });
|
|
299
|
+
return { added: 0, removed: 0 };
|
|
300
|
+
}
|
|
301
|
+
let added = 0;
|
|
302
|
+
let removed = 0;
|
|
303
|
+
for (const line of diffStr.split("\n")) {
|
|
304
|
+
if (line.startsWith("+") && !line.startsWith("+++")) added += 1;
|
|
305
|
+
else if (line.startsWith("-") && !line.startsWith("---")) removed += 1;
|
|
306
|
+
}
|
|
307
|
+
DIFF_STATS_CACHE.set(tool, { diff: diffStr, added, removed });
|
|
308
|
+
return { added, removed };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function durationMsOf(tool: ToolLike): number {
|
|
312
|
+
if (tool.isPartial) return 0;
|
|
313
|
+
const raw = tool.result?.durationMs ?? getToolTiming(tool.toolCallId);
|
|
314
|
+
return typeof raw === "number" && Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function rawPathOf(tool: ToolLike): string {
|
|
318
|
+
const a = tool.args;
|
|
319
|
+
if (!a) return "";
|
|
320
|
+
const p = a.path ?? a.file_path ?? a.filePath ?? a.file;
|
|
321
|
+
return typeof p === "string" ? p.trim() : "";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function displayPath(p: string, cwd?: string): string {
|
|
325
|
+
if (!p) return "";
|
|
326
|
+
const norm = p.replace(/\\/g, "/");
|
|
327
|
+
if (cwd) {
|
|
328
|
+
const rel = relative(cwd, p).replace(/\\/g, "/");
|
|
329
|
+
if (rel && !rel.startsWith("..") && !rel.startsWith("/")) return rel;
|
|
330
|
+
}
|
|
331
|
+
return shortPath(norm);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function sharedFolder(paths: readonly string[]): string {
|
|
335
|
+
const dirs = paths
|
|
336
|
+
.map((p) => {
|
|
337
|
+
const norm = p.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
338
|
+
const d = dirname(norm);
|
|
339
|
+
return d && d !== "." && d !== "/" ? basename(d) : "";
|
|
340
|
+
})
|
|
341
|
+
.filter(Boolean);
|
|
342
|
+
if (dirs.length < 2) return "";
|
|
343
|
+
const first = dirs[0]!;
|
|
344
|
+
return dirs.every((d) => d === first) ? first : "";
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function commandOf(tool: ToolLike): string {
|
|
348
|
+
const a = tool.args;
|
|
349
|
+
if (!a) return "";
|
|
350
|
+
if (typeof a.command === "string") return a.command.trim();
|
|
351
|
+
if (typeof a.code === "string") return a.code.trim();
|
|
352
|
+
return "";
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function summaryArgOf(tool: ToolLike): string {
|
|
356
|
+
const a = tool.args;
|
|
357
|
+
if (!a) return "";
|
|
358
|
+
return typeof a.toolSummary === "string" ? a.toolSummary.trim() : "";
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function friendlyToolName(name: string | undefined): string {
|
|
362
|
+
const n = name ?? "tool";
|
|
363
|
+
if (n === "powershell") return "PS>";
|
|
364
|
+
if (n === "web_search") return "search";
|
|
365
|
+
if (n === "fetch_content") return "fetch";
|
|
366
|
+
if (n === "get_search_content") return "get_content";
|
|
367
|
+
return n;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function normalizeDir(dir: string): string {
|
|
371
|
+
return dir.replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Collapse whitespace and strip a redundant leading `cd <cwd> && ` prefix. */
|
|
375
|
+
function cleanCommand(cmd: string, cwd?: string): string {
|
|
376
|
+
const flat = cmd.replace(/\s+/g, " ").trim();
|
|
377
|
+
if (!flat || !cwd) return flat;
|
|
378
|
+
const match = flat.match(/^cd\s+(?:"([^"]+)"|'([^']+)'|(\S+))\s*&&\s*(.+)$/);
|
|
379
|
+
if (!match) return flat;
|
|
380
|
+
const targetDir = match[1] ?? match[2] ?? match[3] ?? "";
|
|
381
|
+
if (normalizeDir(targetDir) === normalizeDir(cwd)) return match[4]!.trim();
|
|
382
|
+
return flat;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function targetLabelOf(tool: ToolLike): string {
|
|
386
|
+
const name = (tool.toolName ?? "").toLowerCase();
|
|
387
|
+
const a = tool.args ?? {};
|
|
388
|
+
const p = rawPathOf(tool);
|
|
389
|
+
if (name === "read" || name === "edit" || name === "write" || name === "ls") {
|
|
390
|
+
return displayPath(p, tool.cwd) || ".";
|
|
391
|
+
}
|
|
392
|
+
if (name === "grep" || name === "ffgrep") {
|
|
393
|
+
const pat = typeof a.pattern === "string" ? a.pattern.trim() : "";
|
|
394
|
+
const scope = p ? ` in ${displayPath(p, tool.cwd)}` : "";
|
|
395
|
+
return pat ? `"${pat}"${scope}` : displayPath(p, tool.cwd);
|
|
396
|
+
}
|
|
397
|
+
if (name === "find" || name === "fffind") {
|
|
398
|
+
const pat = typeof a.pattern === "string" ? a.pattern.trim() : "";
|
|
399
|
+
const scope = p ? displayPath(p, tool.cwd) : "";
|
|
400
|
+
return [pat, scope].filter(Boolean).join(" · ");
|
|
401
|
+
}
|
|
402
|
+
if (name === "bash" || name === "powershell") {
|
|
403
|
+
const cmd = cleanCommand(commandOf(tool), tool.cwd);
|
|
404
|
+
return cmd || summaryArgOf(tool);
|
|
405
|
+
}
|
|
406
|
+
if (name === "codemode" || name === "mcpscript") {
|
|
407
|
+
const sum = summaryArgOf(tool);
|
|
408
|
+
if (sum) return sum;
|
|
409
|
+
const first = commandOf(tool)
|
|
410
|
+
.split(/\r?\n/)
|
|
411
|
+
.map((l) => l.trim())
|
|
412
|
+
.find((l) => l && !l.startsWith("//"));
|
|
413
|
+
return first ? first.replace(/\s+/g, " ").trim() : "script";
|
|
414
|
+
}
|
|
415
|
+
if (name === "web_search") {
|
|
416
|
+
const q = typeof a.query === "string" ? a.query : Array.isArray(a.queries) ? String(a.queries[0] ?? "") : "";
|
|
417
|
+
return q.trim();
|
|
418
|
+
}
|
|
419
|
+
if (name === "fetch_content") {
|
|
420
|
+
const u = typeof a.url === "string" ? a.url : Array.isArray(a.urls) ? String(a.urls[0] ?? "") : "";
|
|
421
|
+
return u.trim();
|
|
422
|
+
}
|
|
423
|
+
for (const key of ["toolSummary", "tool", "path", "query", "pattern", "action", "subject", "url"]) {
|
|
424
|
+
const v = a[key];
|
|
425
|
+
if (typeof v === "string" && v.trim()) return v.trim();
|
|
426
|
+
}
|
|
427
|
+
return "";
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Generate a descriptive heuristic phase title when the agent did not narrate the step. */
|
|
431
|
+
function heuristicPhaseTitle(category: PhaseCategory, tools: readonly ToolLike[]): string {
|
|
432
|
+
const paths = tools.map(rawPathOf).filter(Boolean);
|
|
433
|
+
const uniqueFiles = [...new Set(paths.map((p) => basename(p.replace(/\\/g, "/"))))].filter(Boolean);
|
|
434
|
+
const names = tools.map((t) => (t.toolName ?? "").toLowerCase());
|
|
435
|
+
|
|
436
|
+
if (category === "explore") {
|
|
437
|
+
if (names.every((n) => n === "web_search" || n === "fetch_content" || n === "get_search_content" || n === "source_check")) {
|
|
438
|
+
const firstQ = targetLabelOf(tools[0]!).replace(/\s+/g, " ").trim();
|
|
439
|
+
return firstQ ? `Researched ${firstQ}` : "Researched web sources";
|
|
440
|
+
}
|
|
441
|
+
if (names.every((n) => n === "grep" || n === "ffgrep") && tools.length === 1) {
|
|
442
|
+
const pat = typeof tools[0]?.args?.pattern === "string" ? tools[0].args.pattern.replace(/\s+/g, " ").trim() : "";
|
|
443
|
+
return pat ? `Searched for "${pat}"` : "Searched codebase";
|
|
444
|
+
}
|
|
445
|
+
if (uniqueFiles.length === 1) {
|
|
446
|
+
const verb = names.every((n) => n === "read") ? "Read" : "Explored";
|
|
447
|
+
return `${verb} ${uniqueFiles[0]}`;
|
|
448
|
+
}
|
|
449
|
+
const folder = sharedFolder(paths);
|
|
450
|
+
if (folder) return `Explored ${folder} files`;
|
|
451
|
+
if (uniqueFiles.length > 1) return `Explored ${uniqueFiles[0]} +${uniqueFiles.length - 1}`;
|
|
452
|
+
return tools.length === 1 ? `Inspected ${targetLabelOf(tools[0]!) || "workspace"}` : "Explored workspace";
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (category === "edit") {
|
|
456
|
+
if (names.every((n) => n === "todo" || n === "scratchpad")) return "Updated task plan";
|
|
457
|
+
if (names.every((n) => n.startsWith("memory_"))) return "Updated memory";
|
|
458
|
+
const hasWrite = names.some((n) => n === "write");
|
|
459
|
+
const hasEdit = names.some((n) => n === "edit" || n === "lsp_fix");
|
|
460
|
+
const verb = hasWrite && hasEdit ? "Created & updated" : hasWrite ? "Created" : "Updated";
|
|
461
|
+
if (uniqueFiles.length === 1) return `${verb} ${uniqueFiles[0]}`;
|
|
462
|
+
const folder = sharedFolder(paths);
|
|
463
|
+
if (folder) return `${verb} ${folder} files`;
|
|
464
|
+
if (uniqueFiles.length > 1) return `${verb} ${uniqueFiles.length} files`;
|
|
465
|
+
return `${verb} ${tools.length} ${tools.length === 1 ? "item" : "items"}`;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (category === "run") {
|
|
469
|
+
if (tools.length === 1) {
|
|
470
|
+
const cmd = commandOf(tools[0]!);
|
|
471
|
+
const sum = summaryArgOf(tools[0]!);
|
|
472
|
+
if (/\b(go\s+test|npm\s+test|bun\s+test|pytest|vitest|jest|cargo\s+test|node\s+--test)\b/i.test(cmd)) {
|
|
473
|
+
return "Verified tests";
|
|
474
|
+
}
|
|
475
|
+
if (/\b(npm\s+run\s+build|tsc\b|cargo\s+build|go\s+build|vite\s+build)\b/i.test(cmd)) {
|
|
476
|
+
return "Built project";
|
|
477
|
+
}
|
|
478
|
+
const gitMatch = cmd.match(/\bgit\s+(status|diff|log|branch|switch|checkout|commit|push|pull)\b/i);
|
|
479
|
+
if (gitMatch) return `Ran git ${gitMatch[1]!.toLowerCase()}`;
|
|
480
|
+
if (sum) return sum;
|
|
481
|
+
const clean = cleanCommand(cmd, tools[0]!.cwd);
|
|
482
|
+
if (clean) return `Ran ${clean}`;
|
|
483
|
+
}
|
|
484
|
+
const allTests = tools.every((t) =>
|
|
485
|
+
/\b(go\s+test|npm\s+test|bun\s+test|pytest|vitest|jest|cargo\s+test|node\s+--test)\b/i.test(commandOf(t)),
|
|
486
|
+
);
|
|
487
|
+
if (allTests) return "Verified tests";
|
|
488
|
+
return `Ran ${tools.length} commands`;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (tools.length === 1) {
|
|
492
|
+
const sum = summaryArgOf(tools[0]!);
|
|
493
|
+
return sum || `Used ${friendlyToolName(tools[0]!.toolName)}`;
|
|
494
|
+
}
|
|
495
|
+
return `Completed ${tools.length} tool actions`;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Group a turn's intermediate assistant messages and tool calls into chronological phases. */
|
|
499
|
+
export function buildPhases(items: readonly (AssistantLike | ToolLike)[]): ProgressPhase[] {
|
|
500
|
+
interface DraftPhase {
|
|
501
|
+
title: string;
|
|
502
|
+
narrationLines?: string[];
|
|
503
|
+
explicitTitle: boolean;
|
|
504
|
+
category: PhaseCategory;
|
|
505
|
+
tools: ToolLike[];
|
|
506
|
+
batchOwner?: AssistantLike;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const drafts: DraftPhase[] = [];
|
|
510
|
+
let lastAssistant: AssistantLike | undefined;
|
|
511
|
+
let pendingNarration: string[] | undefined;
|
|
512
|
+
|
|
513
|
+
for (const item of items) {
|
|
514
|
+
if (isIntermediateAssistant(item)) {
|
|
515
|
+
lastAssistant = item;
|
|
516
|
+
const lines = extractNarrationLines(item);
|
|
517
|
+
if (lines.length > 0) pendingNarration = lines;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if (!isToolComponent(item) || item.hideComponent) continue;
|
|
521
|
+
|
|
522
|
+
const cat = classifyTool(item.toolName);
|
|
523
|
+
const prev = drafts[drafts.length - 1];
|
|
524
|
+
if (pendingNarration) {
|
|
525
|
+
drafts.push({
|
|
526
|
+
title: pendingNarration[0]!,
|
|
527
|
+
narrationLines: pendingNarration.length > 1 ? pendingNarration.slice(1) : undefined,
|
|
528
|
+
explicitTitle: true,
|
|
529
|
+
category: cat,
|
|
530
|
+
tools: [item],
|
|
531
|
+
batchOwner: lastAssistant,
|
|
532
|
+
});
|
|
533
|
+
pendingNarration = undefined;
|
|
534
|
+
} else if (
|
|
535
|
+
prev &&
|
|
536
|
+
((prev.explicitTitle && prev.batchOwner !== undefined && prev.batchOwner === lastAssistant) ||
|
|
537
|
+
(!prev.explicitTitle && prev.category === cat))
|
|
538
|
+
) {
|
|
539
|
+
prev.tools.push(item);
|
|
540
|
+
} else {
|
|
541
|
+
drafts.push({
|
|
542
|
+
title: "",
|
|
543
|
+
explicitTitle: false,
|
|
544
|
+
category: cat,
|
|
545
|
+
tools: [item],
|
|
546
|
+
batchOwner: lastAssistant,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return drafts.map((d) => {
|
|
552
|
+
let running = false;
|
|
553
|
+
let errorCount = 0;
|
|
554
|
+
let durationMs = 0;
|
|
555
|
+
let added = 0;
|
|
556
|
+
let removed = 0;
|
|
557
|
+
for (const tool of d.tools) {
|
|
558
|
+
if (tool.isPartial || !tool.result) running = true;
|
|
559
|
+
if (tool.result?.isError) errorCount += 1;
|
|
560
|
+
durationMs += durationMsOf(tool);
|
|
561
|
+
const stat = countDiffStats(tool);
|
|
562
|
+
added += stat.added;
|
|
563
|
+
removed += stat.removed;
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
title: d.explicitTitle ? d.title : heuristicPhaseTitle(d.category, d.tools),
|
|
567
|
+
...(d.narrationLines ? { narrationLines: d.narrationLines } : {}),
|
|
568
|
+
explicitTitle: d.explicitTitle,
|
|
569
|
+
category: d.category,
|
|
570
|
+
tools: d.tools,
|
|
571
|
+
running,
|
|
572
|
+
hasError: errorCount > 0,
|
|
573
|
+
errorCount,
|
|
574
|
+
durationMs,
|
|
575
|
+
added,
|
|
576
|
+
removed,
|
|
577
|
+
};
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function formatPhaseBadge(phase: ProgressPhase, th: BoxTheme | null): string {
|
|
582
|
+
const fg = (color: string, s: string) => (th ? th.fg(color, s) : s);
|
|
583
|
+
const parts: string[] = [];
|
|
584
|
+
|
|
585
|
+
if (phase.category === "explore") {
|
|
586
|
+
let searches = 0;
|
|
587
|
+
let reads = 0;
|
|
588
|
+
let other = 0;
|
|
589
|
+
for (const t of phase.tools) {
|
|
590
|
+
const n = (t.toolName ?? "").toLowerCase();
|
|
591
|
+
if (n === "read") reads += 1;
|
|
592
|
+
else if (n.includes("grep") || n.includes("find") || n.includes("search") || n === "ls") searches += 1;
|
|
593
|
+
else other += 1;
|
|
594
|
+
}
|
|
595
|
+
if (searches) parts.push(`${searches} ${searches === 1 ? "search" : "searches"}`);
|
|
596
|
+
if (reads) parts.push(`${reads} ${reads === 1 ? "read" : "reads"}`);
|
|
597
|
+
if (other) parts.push(`${other} ${other === 1 ? "step" : "steps"}`);
|
|
598
|
+
} else if (phase.category === "edit") {
|
|
599
|
+
const files = new Set(phase.tools.map(rawPathOf).filter(Boolean));
|
|
600
|
+
const count = files.size || phase.tools.length;
|
|
601
|
+
const unit = files.size ? (count === 1 ? "file" : "files") : count === 1 ? "change" : "changes";
|
|
602
|
+
parts.push(`${count} ${unit}`);
|
|
603
|
+
if (phase.added > 0 || phase.removed > 0) {
|
|
604
|
+
parts.push(`${fg("toolDiffAdded", `+${phase.added}`)}${fg("dim", "/")}${fg("toolDiffRemoved", `-${phase.removed}`)}`);
|
|
605
|
+
}
|
|
606
|
+
} else if (phase.category === "run") {
|
|
607
|
+
const n = phase.tools.length;
|
|
608
|
+
parts.push(`${n} ${n === 1 ? "command" : "commands"}`);
|
|
609
|
+
if (phase.running) parts.push(fg("warning", "running"));
|
|
610
|
+
else if (phase.hasError) parts.push(fg("error", `${phase.errorCount} failed`));
|
|
611
|
+
} else {
|
|
612
|
+
const n = phase.tools.length;
|
|
613
|
+
parts.push(`${n} ${n === 1 ? "step" : "steps"}`);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (phase.category !== "run" && phase.hasError) {
|
|
617
|
+
parts.push(fg("error", `${phase.errorCount} failed`));
|
|
618
|
+
}
|
|
619
|
+
if (parts.length === 0) return "";
|
|
620
|
+
const sep = phase.category === "explore" ? ", " : " · ";
|
|
621
|
+
return `${fg("dim", "(")}${parts.map((p) => (p.includes("\x1b[") ? p : fg("dim", p))).join(fg("dim", sep))}${fg("dim", ")")}`;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** One-line category recap shown inside a collapsed completed Progress box. */
|
|
625
|
+
export function formatCollapsedSummary(phases: readonly ProgressPhase[], th: BoxTheme | null): string {
|
|
626
|
+
const fg = (color: string, s: string) => (th ? th.fg(color, s) : s);
|
|
627
|
+
const dim = (s: string) => fg("dim", s);
|
|
628
|
+
const soft = softOf(th);
|
|
629
|
+
|
|
630
|
+
let reads = 0;
|
|
631
|
+
let searches = 0;
|
|
632
|
+
const editedFiles = new Set<string>();
|
|
633
|
+
let editToolCount = 0;
|
|
634
|
+
let added = 0;
|
|
635
|
+
let removed = 0;
|
|
636
|
+
let commands = 0;
|
|
637
|
+
let other = 0;
|
|
638
|
+
|
|
639
|
+
for (const phase of phases) {
|
|
640
|
+
if (phase.category === "explore") {
|
|
641
|
+
for (const t of phase.tools) {
|
|
642
|
+
const n = (t.toolName ?? "").toLowerCase();
|
|
643
|
+
if (n === "read") reads += 1;
|
|
644
|
+
else searches += 1;
|
|
645
|
+
}
|
|
646
|
+
} else if (phase.category === "edit") {
|
|
647
|
+
for (const t of phase.tools) {
|
|
648
|
+
const p = rawPathOf(t);
|
|
649
|
+
if (p) editedFiles.add(p);
|
|
650
|
+
editToolCount += 1;
|
|
651
|
+
}
|
|
652
|
+
added += phase.added;
|
|
653
|
+
removed += phase.removed;
|
|
654
|
+
} else if (phase.category === "run") {
|
|
655
|
+
commands += phase.tools.length;
|
|
656
|
+
} else {
|
|
657
|
+
other += phase.tools.length;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const parts: string[] = [];
|
|
662
|
+
if (reads > 0) parts.push(soft(`${reads} read`));
|
|
663
|
+
if (searches > 0) parts.push(soft(`${searches} ${searches === 1 ? "search" : "searches"}`));
|
|
664
|
+
const editedCount = editedFiles.size || editToolCount;
|
|
665
|
+
if (editedCount > 0) {
|
|
666
|
+
const diff =
|
|
667
|
+
added > 0 || removed > 0
|
|
668
|
+
? ` ${dim("(")}${fg("toolDiffAdded", `+${added}`)}${dim("/")}${fg("toolDiffRemoved", `-${removed}`)}${dim(")")}`
|
|
669
|
+
: "";
|
|
670
|
+
parts.push(`${soft(`${editedCount} edited`)}${diff}`);
|
|
671
|
+
}
|
|
672
|
+
if (commands > 0) parts.push(soft(`${commands} ${commands === 1 ? "command" : "commands"}`));
|
|
673
|
+
if (other > 0) parts.push(soft(`${other} ${other === 1 ? "tool" : "tools"}`));
|
|
674
|
+
|
|
675
|
+
return parts.join(dim(" · "));
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function renderTopBorder(
|
|
679
|
+
width: number,
|
|
680
|
+
leftStyled: string,
|
|
681
|
+
centerStyled: string,
|
|
682
|
+
rightStyled: string,
|
|
683
|
+
dim: (s: string) => string,
|
|
684
|
+
): string {
|
|
685
|
+
if (width < 6) return dim("╭" + "─".repeat(Math.max(0, width - 2)) + "╮");
|
|
686
|
+
const rightPart = rightStyled ? ` ${rightStyled} ` : "";
|
|
687
|
+
const rightW = visibleWidth(rightPart);
|
|
688
|
+
const centerPart = centerStyled ? ` ${centerStyled} ` : "";
|
|
689
|
+
const centerW = visibleWidth(centerPart);
|
|
690
|
+
|
|
691
|
+
// Place `↑ N more` centered in the top border (matching chatbox) when room allows.
|
|
692
|
+
if (centerW > 0 && width >= 8 + rightW + centerW + 4) {
|
|
693
|
+
const maxLeft = Math.max(4, width - 8 - rightW - centerW);
|
|
694
|
+
const fittedLeft = truncateToWidth(leftStyled, maxLeft, "…");
|
|
695
|
+
const leftW = visibleWidth(fittedLeft);
|
|
696
|
+
const minStart = 4 + leftW + 1;
|
|
697
|
+
const maxStart = width - 2 - rightW - 1 - centerW;
|
|
698
|
+
const centerStart = Math.max(minStart, Math.min(maxStart, Math.floor((width - centerW) / 2)));
|
|
699
|
+
const fill1 = Math.max(1, centerStart - (4 + leftW));
|
|
700
|
+
const fill2 = Math.max(1, width - 2 - rightW - (centerStart + centerW));
|
|
701
|
+
const raw = `${dim("╭─")} ${fittedLeft} ${dim("─".repeat(fill1))}${centerPart}${dim("─".repeat(fill2))}${rightPart}${dim("─╮")}`;
|
|
702
|
+
return truncateToWidth(raw, width, "");
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// Fixed columns: `╭─ ` (3) + ` ` (1) + at least 1 fill `─` (1) + rightPart + `─╮` (2) = 7 + rightW.
|
|
706
|
+
const maxLeft = Math.max(4, width - 7 - rightW);
|
|
707
|
+
const fittedLeft = truncateToWidth(leftStyled, maxLeft, "…");
|
|
708
|
+
const leftW = visibleWidth(fittedLeft);
|
|
709
|
+
const fillCount = Math.max(1, width - 6 - leftW - rightW);
|
|
710
|
+
const raw = `${dim("╭─")} ${fittedLeft} ${dim("─".repeat(fillCount))}${rightPart}${dim("─╮")}`;
|
|
711
|
+
return truncateToWidth(raw, width, "");
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function renderBottomBorder(width: number, dim: (s: string) => string): string {
|
|
715
|
+
return dim(`╰${"─".repeat(Math.max(0, width - 2))}╯`);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function frameRowExact(content: string, dim: (s: string) => string): string {
|
|
719
|
+
return `${dim("│")} ${content} ${dim("│")}`;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function padBetween(left: string, right: string, inner: number): string {
|
|
723
|
+
const rightW = visibleWidth(right);
|
|
724
|
+
if (rightW === 0) {
|
|
725
|
+
const fitted = truncateToWidth(left, inner, "…");
|
|
726
|
+
const pad = Math.max(0, inner - visibleWidth(fitted));
|
|
727
|
+
return pad > 0 ? `${fitted}${" ".repeat(pad)}` : fitted;
|
|
728
|
+
}
|
|
729
|
+
const minGap = inner - rightW >= 10 ? 2 : 1;
|
|
730
|
+
const avail = Math.max(4, inner - rightW - minGap);
|
|
731
|
+
const fittedLeft = truncateToWidth(left, avail, "…");
|
|
732
|
+
const gap = Math.max(minGap, inner - visibleWidth(fittedLeft) - rightW);
|
|
733
|
+
return `${fittedLeft}${" ".repeat(gap)}${right}`;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** Right-most clickable span for a button label (`[show]`, `[hide]`, `[expand all]`, `[collapse all]`),
|
|
737
|
+
* padded by 1 cell on each side so near-edge clicks never miss and tool args containing the
|
|
738
|
+
* same text never steal the hitbox. */
|
|
739
|
+
function buttonSpan(line: string, row: number, label: string, maxCol: number): TextSpan[] {
|
|
740
|
+
if (!label) return [];
|
|
741
|
+
const plain = stripAnsi(line);
|
|
742
|
+
const at = plain.lastIndexOf(label);
|
|
743
|
+
if (at < 0) return [];
|
|
744
|
+
const start = visibleWidth(plain.slice(0, at));
|
|
745
|
+
const end = start + visibleWidth(label) - 1;
|
|
746
|
+
return [{ row, start: Math.max(0, start - 1), end: Math.min(maxCol, end + 1) }];
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
export class TurnProgressBoxComponent {
|
|
750
|
+
readonly [PROGRESS_BOX_MARKER] = true;
|
|
751
|
+
private items: (AssistantLike | ToolLike)[] = [];
|
|
752
|
+
private readonly state: TurnBoxState;
|
|
753
|
+
private lastHitMap: RenderedHitMap | null = null;
|
|
754
|
+
/** True between a press-handled toggle and its matching release. */
|
|
755
|
+
private pressConsumed = false;
|
|
756
|
+
/** True right after a press-handled release so a synchronous trailing click is ignored. */
|
|
757
|
+
private suppressNextClick = false;
|
|
758
|
+
private scrollHold: HoldRepeat | null = null;
|
|
759
|
+
private scrollHoldActive = false;
|
|
760
|
+
private requestRender?: () => void;
|
|
761
|
+
private detailRenderCache = new WeakMap<
|
|
762
|
+
object,
|
|
763
|
+
{ width: number; result: unknown; expanded: boolean; scrollOffset: number; lines: string[] }
|
|
764
|
+
>();
|
|
765
|
+
private frameCache: {
|
|
766
|
+
width: number;
|
|
767
|
+
maxBody: number;
|
|
768
|
+
theme: unknown;
|
|
769
|
+
spin: string;
|
|
770
|
+
interactionVersion: number;
|
|
771
|
+
scrollOffset: number | null | undefined;
|
|
772
|
+
itemsSig: number;
|
|
773
|
+
lines: string[];
|
|
774
|
+
} | null = null;
|
|
775
|
+
|
|
776
|
+
constructor(state: TurnBoxState) {
|
|
777
|
+
this.state = state;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
private stopScrollHold(): void {
|
|
781
|
+
this.scrollHold?.stop();
|
|
782
|
+
this.scrollHold = null;
|
|
783
|
+
this.scrollHoldActive = false;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
private startScrollHold(dir: -1 | 1): void {
|
|
787
|
+
this.stopScrollHold();
|
|
788
|
+
const hold = createHoldRepeat();
|
|
789
|
+
this.scrollHold = hold;
|
|
790
|
+
this.scrollHoldActive = true;
|
|
791
|
+
hold.start(() => {
|
|
792
|
+
const hit = this.lastHitMap;
|
|
793
|
+
if (!hit || hit.maxOffset <= 0) {
|
|
794
|
+
this.scrollHoldActive = false;
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
const cur = this.state.scrollOffset ?? hit.scrollOffset;
|
|
798
|
+
const next = Math.max(0, Math.min(hit.maxOffset, cur + dir * Math.max(1, hit.scrollStep)));
|
|
799
|
+
if (next === cur) {
|
|
800
|
+
this.scrollHoldActive = false;
|
|
801
|
+
return false;
|
|
802
|
+
}
|
|
803
|
+
this.state.scrollOffset = next;
|
|
804
|
+
this.bumpAndRequestRender();
|
|
805
|
+
return true;
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
update(items: (AssistantLike | ToolLike)[], requestRender?: () => void): void {
|
|
810
|
+
this.items = items;
|
|
811
|
+
if (requestRender) this.requestRender = requestRender;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
getItems(): readonly (AssistantLike | ToolLike)[] {
|
|
815
|
+
return this.items;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
getInteractionVersion(): number {
|
|
819
|
+
return this.state.interactionVersion;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
isDynamic(): boolean {
|
|
823
|
+
if (this.scrollHoldActive) return true;
|
|
824
|
+
for (const item of this.items) {
|
|
825
|
+
if (isToolComponent(item)) {
|
|
826
|
+
if (item.isPartial || !item.result) return true;
|
|
827
|
+
if ((item as unknown as Record<symbol, unknown>)[SCROLL_HOLD_KEY] !== undefined) return true;
|
|
828
|
+
}
|
|
829
|
+
if (isIntermediateAssistant(item) && item.isStreaming) return true;
|
|
830
|
+
}
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/** 32-bit FNV-1a scroll offset hash across the box viewport and contained tools so render-cache detects scroll ticks at any turn length. */
|
|
835
|
+
getScrollSignature(): number {
|
|
836
|
+
let sig = 0x811c9dc5 | 0;
|
|
837
|
+
const ownOffset = this.state.scrollOffset == null ? -1 : this.state.scrollOffset | 0;
|
|
838
|
+
sig = Math.imul(sig ^ ownOffset, 0x01000193) | 0;
|
|
839
|
+
for (const item of this.items) {
|
|
840
|
+
if (isToolComponent(item)) {
|
|
841
|
+
const offset = Number((item as unknown as Record<symbol, unknown>)[SCROLL_OFFSET_KEY] ?? 0) | 0;
|
|
842
|
+
sig = Math.imul(sig ^ offset, 0x01000193) | 0;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return sig;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
invalidate(): void {
|
|
849
|
+
this.frameCache = null;
|
|
850
|
+
// Keep lastHitMap intact until the next render replaces it; mouse events can arrive
|
|
851
|
+
// between invalidate/requestRender and the throttled frame paint.
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
private computeItemsSig(): number {
|
|
855
|
+
let sig = 0x811c9dc5 | 0;
|
|
856
|
+
sig = Math.imul(sig ^ this.items.length, 0x01000193) | 0;
|
|
857
|
+
for (let i = 0; i < this.items.length; i++) {
|
|
858
|
+
const item = this.items[i]!;
|
|
859
|
+
if (isToolComponent(item)) {
|
|
860
|
+
const flags =
|
|
861
|
+
(item.isPartial ? 1 : 0) |
|
|
862
|
+
(item.result ? 2 : 0) |
|
|
863
|
+
(item.result?.isError ? 4 : 0) |
|
|
864
|
+
(this.isDetailOpen(item) ? 8 : 0) |
|
|
865
|
+
(item.hideComponent ? 16 : 0);
|
|
866
|
+
const dur = Math.trunc(durationMsOf(item));
|
|
867
|
+
const off = Number((item as unknown as Record<symbol, unknown>)[SCROLL_OFFSET_KEY] ?? 0) | 0;
|
|
868
|
+
const contentLen = Array.isArray(item.result?.content) ? item.result.content.length : 0;
|
|
869
|
+
sig = Math.imul(sig ^ flags, 0x01000193) | 0;
|
|
870
|
+
sig = Math.imul(sig ^ dur, 0x01000193) | 0;
|
|
871
|
+
sig = Math.imul(sig ^ off, 0x01000193) | 0;
|
|
872
|
+
sig = Math.imul(sig ^ contentLen, 0x01000193) | 0;
|
|
873
|
+
} else if (isIntermediateAssistant(item)) {
|
|
874
|
+
sig = Math.imul(sig ^ (item.isStreaming ? 3 : 1), 0x01000193) | 0;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return sig;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
private isPhaseExpanded(phase: ProgressPhase, index: number): boolean {
|
|
881
|
+
if (this.state.phaseOverrides.has(index)) return this.state.phaseOverrides.get(index)!;
|
|
882
|
+
if (this.state.allOverride !== undefined) return this.state.allOverride;
|
|
883
|
+
if (phase.running || phase.hasError) return true;
|
|
884
|
+
if (phase.tools.some((t) => this.isDetailOpen(t))) return true;
|
|
885
|
+
return false;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
private isDetailOpen(tool: ToolLike): boolean {
|
|
889
|
+
// Real ToolExecutionComponents own `expanded`, so reading it keeps parent [show]/[hide],
|
|
890
|
+
// nested tool [hide]/body clicks, and Ctrl+O in lockstep. Fallback map is only for mocks
|
|
891
|
+
// without `setExpanded`.
|
|
892
|
+
if (typeof tool.setExpanded === "function") return Boolean(tool.expanded);
|
|
893
|
+
if (this.state.detailOpen.has(tool)) return Boolean(this.state.detailOpen.get(tool));
|
|
894
|
+
return Boolean(tool.expanded);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
private setDetailOpen(tool: ToolLike, open: boolean): void {
|
|
898
|
+
this.state.detailOpen.set(tool, open);
|
|
899
|
+
tool.setExpanded?.(open);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
private bumpAndRequestRender(): void {
|
|
903
|
+
this.state.interactionVersion += 1;
|
|
904
|
+
this.requestRender?.();
|
|
905
|
+
for (const item of this.items) {
|
|
906
|
+
if (isToolComponent(item) && item.ui?.requestRender) {
|
|
907
|
+
item.ui.requestRender();
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
render(width: number): string[] {
|
|
914
|
+
if (width < 8) return [];
|
|
915
|
+
const th = getActiveTheme<BoxTheme>();
|
|
916
|
+
const maxBody = expandedBoxBodyLimit();
|
|
917
|
+
const spin = spinIcon();
|
|
918
|
+
const itemsSig = this.computeItemsSig();
|
|
919
|
+
const fc = this.frameCache;
|
|
920
|
+
if (
|
|
921
|
+
fc &&
|
|
922
|
+
fc.width === width &&
|
|
923
|
+
fc.maxBody === maxBody &&
|
|
924
|
+
fc.theme === th &&
|
|
925
|
+
fc.spin === spin &&
|
|
926
|
+
fc.interactionVersion === this.state.interactionVersion &&
|
|
927
|
+
fc.scrollOffset === this.state.scrollOffset &&
|
|
928
|
+
fc.itemsSig === itemsSig
|
|
929
|
+
) {
|
|
930
|
+
return fc.lines;
|
|
931
|
+
}
|
|
932
|
+
const fg = (color: string, s: string) => (th ? th.fg(color, s) : s);
|
|
933
|
+
const bold = th?.bold?.bind(th) ?? ((s: string) => s);
|
|
934
|
+
const dim = (s: string) => fg("dim", s);
|
|
935
|
+
const soft = softOf(th);
|
|
936
|
+
const inner = Math.max(8, width - 4);
|
|
937
|
+
|
|
938
|
+
const phases = buildPhases(this.items);
|
|
939
|
+
if (phases.length === 0) return [];
|
|
940
|
+
|
|
941
|
+
let totalTools = 0;
|
|
942
|
+
let totalMs = 0;
|
|
943
|
+
let totalErrors = 0;
|
|
944
|
+
let anyRunning = false;
|
|
945
|
+
for (const p of phases) {
|
|
946
|
+
totalTools += p.tools.length;
|
|
947
|
+
totalMs += p.durationMs;
|
|
948
|
+
totalErrors += p.errorCount;
|
|
949
|
+
if (p.running) anyRunning = true;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const expandedFlags = phases.map((p, i) => this.isPhaseExpanded(p, i));
|
|
953
|
+
const allExpanded = expandedFlags.every(Boolean);
|
|
954
|
+
const boxExpanded = this.state.boxExpanded ?? anyRunning;
|
|
955
|
+
|
|
956
|
+
const stepCount = `${totalTools} ${totalTools === 1 ? "step" : "steps"}`;
|
|
957
|
+
const headerMeta = [
|
|
958
|
+
fg("muted", stepCount),
|
|
959
|
+
totalMs > 0 ? dim(fmtMs(totalMs)) : "",
|
|
960
|
+
totalErrors > 0 ? fg("error", `${totalErrors} failed`) : "",
|
|
961
|
+
]
|
|
962
|
+
.filter(Boolean)
|
|
963
|
+
.join(dim(" · "));
|
|
964
|
+
const headerLeft = `${fg("accent", bold("Progress"))}${dim(" · ")}${headerMeta}`;
|
|
965
|
+
|
|
966
|
+
if (!boxExpanded) {
|
|
967
|
+
this.stopScrollHold();
|
|
968
|
+
const topLine = renderTopBorder(width, headerLeft, "", dim(SHOW_LABEL), dim);
|
|
969
|
+
const summaryLine = frameRowExact(padBetween(formatCollapsedSummary(phases, th), "", inner), dim);
|
|
970
|
+
const botLine = renderBottomBorder(width, dim);
|
|
971
|
+
const lines: string[] = ["", topLine, summaryLine, botLine];
|
|
972
|
+
this.lastHitMap = {
|
|
973
|
+
width,
|
|
974
|
+
maxBody,
|
|
975
|
+
boxExpanded: false,
|
|
976
|
+
summaryRow: 2,
|
|
977
|
+
headerBoxToggleSpans: buttonSpan(topLine, 1, SHOW_LABEL, width - 1),
|
|
978
|
+
headerToggleSpans: [],
|
|
979
|
+
scrollUpSpans: [],
|
|
980
|
+
scrollDownSpans: [],
|
|
981
|
+
scrollOffset: 0,
|
|
982
|
+
maxOffset: 0,
|
|
983
|
+
scrollStep: Math.max(1, Math.floor(maxBody / 2)),
|
|
984
|
+
allExpanded,
|
|
985
|
+
phases: [],
|
|
986
|
+
tools: [],
|
|
987
|
+
details: [],
|
|
988
|
+
};
|
|
989
|
+
this.frameCache = {
|
|
990
|
+
width,
|
|
991
|
+
maxBody,
|
|
992
|
+
theme: th,
|
|
993
|
+
spin,
|
|
994
|
+
interactionVersion: this.state.interactionVersion,
|
|
995
|
+
scrollOffset: this.state.scrollOffset,
|
|
996
|
+
itemsSig,
|
|
997
|
+
lines,
|
|
998
|
+
};
|
|
999
|
+
return lines;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
const toggleText = allExpanded ? COLLAPSE_ALL_LABEL : EXPAND_ALL_LABEL;
|
|
1003
|
+
const showAllToggle = width >= 56;
|
|
1004
|
+
const headerRight = showAllToggle ? `${dim(toggleText)}${dim(" · ")}${dim(HIDE_LABEL)}` : dim(HIDE_LABEL);
|
|
1005
|
+
|
|
1006
|
+
const bodyLines: string[] = [];
|
|
1007
|
+
const logicalPhases: Array<{ bodyRow: number; phaseIndex: number }> = [];
|
|
1008
|
+
const logicalTools: Array<{ bodyRow: number; tool: ToolLike; hintLabel: string }> = [];
|
|
1009
|
+
const logicalDetails: Array<{
|
|
1010
|
+
startBodyRow: number;
|
|
1011
|
+
endBodyRow: number;
|
|
1012
|
+
rowOffset: number;
|
|
1013
|
+
indentCols: number;
|
|
1014
|
+
width: number;
|
|
1015
|
+
tool: ToolLike;
|
|
1016
|
+
}> = [];
|
|
1017
|
+
|
|
1018
|
+
const maxDurW = phases.reduce((m, p) => (p.durationMs > 0 ? Math.max(m, fmtMs(p.durationMs).length) : m), 0);
|
|
1019
|
+
|
|
1020
|
+
for (let i = 0; i < phases.length; i++) {
|
|
1021
|
+
const phase = phases[i]!;
|
|
1022
|
+
const expanded = expandedFlags[i]!;
|
|
1023
|
+
const arrow = expanded ? fg("accent", "▾") : dim("▸");
|
|
1024
|
+
const statusIcon = phase.running
|
|
1025
|
+
? fg("warning", spinIcon())
|
|
1026
|
+
: phase.hasError
|
|
1027
|
+
? fg("error", "✗")
|
|
1028
|
+
: fg("success", "✓");
|
|
1029
|
+
const styleTitle = (s: string) => (phase.hasError ? fg("error", s) : fg("text", s));
|
|
1030
|
+
const badge = width >= 54 ? formatPhaseBadge(phase, th) : "";
|
|
1031
|
+
const durText = phase.durationMs > 0 ? dim(fmtMs(phase.durationMs).padStart(maxDurW)) : "";
|
|
1032
|
+
|
|
1033
|
+
const rightMain = [badge, durText].filter(Boolean).join(" ");
|
|
1034
|
+
if (!expanded) {
|
|
1035
|
+
const leftMain = `${arrow} ${statusIcon} ${styleTitle(phase.title)}`;
|
|
1036
|
+
const phaseBodyRow = bodyLines.length;
|
|
1037
|
+
bodyLines.push(frameRowExact(padBetween(leftMain, rightMain, inner), dim));
|
|
1038
|
+
logicalPhases.push({ bodyRow: phaseBodyRow, phaseIndex: i });
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
const rightW = visibleWidth(rightMain);
|
|
1043
|
+
const prefix = `${arrow} ${statusIcon} `;
|
|
1044
|
+
const prefixW = visibleWidth(prefix);
|
|
1045
|
+
const minGap = rightW > 0 && inner - rightW >= 10 ? 2 : rightW > 0 ? 1 : 0;
|
|
1046
|
+
const firstAvail = Math.max(8, inner - prefixW - rightW - minGap);
|
|
1047
|
+
const contIndent = " ".repeat(prefixW);
|
|
1048
|
+
const contAvail = Math.max(8, inner - prefixW);
|
|
1049
|
+
const wrappedTitle = wrapTextWithAnsi(styleTitle(phase.title), firstAvail);
|
|
1050
|
+
const firstLine = wrappedTitle[0] ?? "";
|
|
1051
|
+
const firstRow = bodyLines.length;
|
|
1052
|
+
bodyLines.push(frameRowExact(padBetween(`${prefix}${firstLine}`, rightMain, inner), dim));
|
|
1053
|
+
logicalPhases.push({ bodyRow: firstRow, phaseIndex: i });
|
|
1054
|
+
for (let w = 1; w < wrappedTitle.length; w++) {
|
|
1055
|
+
const contRow = bodyLines.length;
|
|
1056
|
+
bodyLines.push(boxRow(`${contIndent}${wrappedTitle[w]}`, width, dim));
|
|
1057
|
+
logicalPhases.push({ bodyRow: contRow, phaseIndex: i });
|
|
1058
|
+
}
|
|
1059
|
+
if (phase.narrationLines) {
|
|
1060
|
+
for (const extra of phase.narrationLines) {
|
|
1061
|
+
for (const wl of wrapTextWithAnsi(soft(extra), contAvail)) {
|
|
1062
|
+
const extraRow = bodyLines.length;
|
|
1063
|
+
bodyLines.push(boxRow(`${contIndent}${wl}`, width, dim));
|
|
1064
|
+
logicalPhases.push({ bodyRow: extraRow, phaseIndex: i });
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
for (let j = 0; j < phase.tools.length; j++) {
|
|
1070
|
+
const tool = phase.tools[j]!;
|
|
1071
|
+
const isLast = j === phase.tools.length - 1;
|
|
1072
|
+
const branch = dim(isLast ? " └─" : " ├─");
|
|
1073
|
+
const toolRunning = Boolean(tool.isPartial || !tool.result);
|
|
1074
|
+
const toolErr = Boolean(tool.result?.isError);
|
|
1075
|
+
const toolIcon = toolRunning
|
|
1076
|
+
? fg("warning", spinIcon())
|
|
1077
|
+
: toolErr
|
|
1078
|
+
? fg("error", "✗")
|
|
1079
|
+
: fg("success", "✓");
|
|
1080
|
+
const nameText = fg("accent", friendlyToolName(tool.toolName));
|
|
1081
|
+
const target = targetLabelOf(tool);
|
|
1082
|
+
const targetStyled = target ? ` ${soft(target)}` : "";
|
|
1083
|
+
const stat = countDiffStats(tool);
|
|
1084
|
+
const statText =
|
|
1085
|
+
stat.added > 0 || stat.removed > 0
|
|
1086
|
+
? ` ${fg("toolDiffAdded", `+${stat.added}`)}${dim("/")}${fg("toolDiffRemoved", `-${stat.removed}`)}`
|
|
1087
|
+
: "";
|
|
1088
|
+
const detailOpen = this.isDetailOpen(tool);
|
|
1089
|
+
const dur = fmtMs(durationMsOf(tool) || undefined);
|
|
1090
|
+
const hintLabel = detailOpen ? HIDE_LABEL : SHOW_LABEL;
|
|
1091
|
+
const rightParts = [dur, hintLabel].filter(Boolean).join(" · ");
|
|
1092
|
+
const toolRight = dim(rightParts);
|
|
1093
|
+
const detailIndent = isLast ? " " : " │ ";
|
|
1094
|
+
|
|
1095
|
+
if (!detailOpen) {
|
|
1096
|
+
const toolLeft = `${branch} ${toolIcon} ${nameText}${targetStyled}${statText}`;
|
|
1097
|
+
const toolBodyRow = bodyLines.length;
|
|
1098
|
+
bodyLines.push(frameRowExact(padBetween(toolLeft, toolRight, inner), dim));
|
|
1099
|
+
logicalTools.push({ bodyRow: toolBodyRow, tool, hintLabel });
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
const toolPrefix = `${branch} ${toolIcon} `;
|
|
1104
|
+
const toolPrefixW = visibleWidth(toolPrefix);
|
|
1105
|
+
const toolRightW = visibleWidth(toolRight);
|
|
1106
|
+
const toolMinGap = toolRightW > 0 && inner - toolRightW >= 10 ? 2 : toolRightW > 0 ? 1 : 0;
|
|
1107
|
+
const toolFirstAvail = Math.max(8, inner - toolPrefixW - toolRightW - toolMinGap);
|
|
1108
|
+
const wrappedTool = wrapTextWithAnsi(`${nameText}${targetStyled}${statText}`, toolFirstAvail);
|
|
1109
|
+
const toolFirstRow = bodyLines.length;
|
|
1110
|
+
bodyLines.push(frameRowExact(padBetween(`${toolPrefix}${wrappedTool[0] ?? ""}`, toolRight, inner), dim));
|
|
1111
|
+
logicalTools.push({ bodyRow: toolFirstRow, tool, hintLabel });
|
|
1112
|
+
for (let w = 1; w < wrappedTool.length; w++) {
|
|
1113
|
+
const contRow = bodyLines.length;
|
|
1114
|
+
bodyLines.push(boxRow(`${dim(detailIndent)}${wrappedTool[w]}`, width, dim));
|
|
1115
|
+
logicalTools.push({ bodyRow: contRow, tool, hintLabel: "" });
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
const indentCols = 2 + visibleWidth(detailIndent); // `│ ` + detailIndent
|
|
1119
|
+
const detailWidth = Math.max(16, width - indentCols - 2);
|
|
1120
|
+
const toolScroll = Number((tool as unknown as Record<symbol, unknown>)[SCROLL_OFFSET_KEY] ?? 0);
|
|
1121
|
+
const cachedDetail = !toolRunning ? this.detailRenderCache.get(tool) : undefined;
|
|
1122
|
+
let rawDetail: readonly string[];
|
|
1123
|
+
if (
|
|
1124
|
+
cachedDetail &&
|
|
1125
|
+
cachedDetail.width === detailWidth &&
|
|
1126
|
+
cachedDetail.result === tool.result &&
|
|
1127
|
+
cachedDetail.expanded === detailOpen &&
|
|
1128
|
+
cachedDetail.scrollOffset === toolScroll
|
|
1129
|
+
) {
|
|
1130
|
+
rawDetail = cachedDetail.lines;
|
|
1131
|
+
} else {
|
|
1132
|
+
rawDetail = tool.render(detailWidth);
|
|
1133
|
+
if (!toolRunning && tool.result) {
|
|
1134
|
+
this.detailRenderCache.set(tool, {
|
|
1135
|
+
width: detailWidth,
|
|
1136
|
+
result: tool.result,
|
|
1137
|
+
expanded: detailOpen,
|
|
1138
|
+
scrollOffset: toolScroll,
|
|
1139
|
+
lines: [...rawDetail],
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
// Drop leading empty spacer line from fallback tool renderers.
|
|
1144
|
+
let startIdx = 0;
|
|
1145
|
+
while (startIdx < rawDetail.length && stripAnsi(rawDetail[startIdx]!).trim() === "") startIdx += 1;
|
|
1146
|
+
const detailLines = rawDetail.slice(startIdx);
|
|
1147
|
+
if (detailLines.length > 0) {
|
|
1148
|
+
const startBodyRow = bodyLines.length;
|
|
1149
|
+
for (const dl of detailLines) {
|
|
1150
|
+
bodyLines.push(boxRow(`${dim(detailIndent)}${dl}`, width, dim));
|
|
1151
|
+
}
|
|
1152
|
+
logicalDetails.push({
|
|
1153
|
+
startBodyRow,
|
|
1154
|
+
endBodyRow: bodyLines.length - 1,
|
|
1155
|
+
rowOffset: startIdx,
|
|
1156
|
+
indentCols,
|
|
1157
|
+
width: detailWidth,
|
|
1158
|
+
tool,
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// Cap tall boxes with the same adaptive viewport limit and `↑/↓ N more` border
|
|
1165
|
+
// affordances used by chatbox and expanded tool boxes.
|
|
1166
|
+
const maxOffset = Math.max(0, bodyLines.length - maxBody);
|
|
1167
|
+
let offset = 0;
|
|
1168
|
+
if (maxOffset === 0) {
|
|
1169
|
+
this.stopScrollHold();
|
|
1170
|
+
if (this.state.scrollOffset != null) this.state.scrollOffset = null;
|
|
1171
|
+
} else if (this.state.scrollOffset != null) {
|
|
1172
|
+
offset = Math.max(0, Math.min(this.state.scrollOffset, maxOffset));
|
|
1173
|
+
this.state.scrollOffset = offset;
|
|
1174
|
+
} else {
|
|
1175
|
+
// Auto mode always tails the newest row. Anchoring to an early error would fight
|
|
1176
|
+
// the running-anchor: in every inter-tool gap nothing is running, so the viewport
|
|
1177
|
+
// bounced error → bottom → error for the rest of the turn.
|
|
1178
|
+
offset = maxOffset;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
const visibleBody = maxOffset > 0 ? bodyLines.slice(offset, offset + maxBody) : bodyLines;
|
|
1182
|
+
const above = offset;
|
|
1183
|
+
const below = bodyLines.length - (offset + visibleBody.length);
|
|
1184
|
+
const aboveLabel = above > 0 ? `\u2191 ${above} more` : "";
|
|
1185
|
+
const belowLabel = below > 0 ? `\u2193 ${below} more` : "";
|
|
1186
|
+
const scrollPaint = chatboxPrimaryPaint(th);
|
|
1187
|
+
|
|
1188
|
+
// Leading spacer separates the progress box cleanly from the preceding user bubble.
|
|
1189
|
+
const topLine = renderTopBorder(width, headerLeft, aboveLabel ? scrollPaint(aboveLabel) : "", headerRight, dim);
|
|
1190
|
+
const plainBot = renderBottomBorder(width, dim);
|
|
1191
|
+
const botLine = belowLabel ? paintScrollEdge(plainBot, "╰", "╯", belowLabel, width, dim, scrollPaint) : plainBot;
|
|
1192
|
+
const lines: string[] = ["", topLine, ...visibleBody, botLine];
|
|
1193
|
+
const botRow = lines.length - 1;
|
|
1194
|
+
|
|
1195
|
+
const headerBoxToggleSpans = buttonSpan(topLine, 1, HIDE_LABEL, width - 1);
|
|
1196
|
+
const headerToggleSpans = showAllToggle ? buttonSpan(topLine, 1, toggleText, width - 1) : [];
|
|
1197
|
+
const scrollUpSpans = aboveLabel ? buttonSpan(topLine, 1, aboveLabel, width - 1) : [];
|
|
1198
|
+
const scrollDownSpans = belowLabel ? buttonSpan(botLine, botRow, belowLabel, width - 1) : [];
|
|
1199
|
+
|
|
1200
|
+
const visEndExclusive = offset + visibleBody.length;
|
|
1201
|
+
const hitPhases: HitPhaseRow[] = [];
|
|
1202
|
+
for (const p of logicalPhases) {
|
|
1203
|
+
if (p.bodyRow >= offset && p.bodyRow < visEndExclusive) {
|
|
1204
|
+
hitPhases.push({ row: 2 + (p.bodyRow - offset), phaseIndex: p.phaseIndex });
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
const hitTools: HitToolRow[] = [];
|
|
1208
|
+
for (const t of logicalTools) {
|
|
1209
|
+
if (t.bodyRow >= offset && t.bodyRow < visEndExclusive) {
|
|
1210
|
+
const row = 2 + (t.bodyRow - offset);
|
|
1211
|
+
const renderedRow = visibleBody[t.bodyRow - offset]!;
|
|
1212
|
+
hitTools.push({ row, tool: t.tool, hintSpans: buttonSpan(renderedRow, row, t.hintLabel, width - 1) });
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
const hitDetails: HitDetailRegion[] = [];
|
|
1216
|
+
for (const d of logicalDetails) {
|
|
1217
|
+
const visStart = Math.max(d.startBodyRow, offset);
|
|
1218
|
+
const visEnd = Math.min(d.endBodyRow, visEndExclusive - 1);
|
|
1219
|
+
if (visStart <= visEnd) {
|
|
1220
|
+
hitDetails.push({
|
|
1221
|
+
startRow: 2 + (visStart - offset),
|
|
1222
|
+
endRow: 2 + (visEnd - offset),
|
|
1223
|
+
rowOffset: d.rowOffset + (visStart - d.startBodyRow),
|
|
1224
|
+
indentCols: d.indentCols,
|
|
1225
|
+
width: d.width,
|
|
1226
|
+
tool: d.tool,
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
this.lastHitMap = {
|
|
1232
|
+
width,
|
|
1233
|
+
maxBody,
|
|
1234
|
+
boxExpanded: true,
|
|
1235
|
+
summaryRow: null,
|
|
1236
|
+
headerBoxToggleSpans,
|
|
1237
|
+
headerToggleSpans,
|
|
1238
|
+
scrollUpSpans,
|
|
1239
|
+
scrollDownSpans,
|
|
1240
|
+
scrollOffset: offset,
|
|
1241
|
+
maxOffset,
|
|
1242
|
+
scrollStep: Math.max(1, Math.floor(maxBody / 2)),
|
|
1243
|
+
allExpanded,
|
|
1244
|
+
phases: hitPhases,
|
|
1245
|
+
tools: hitTools,
|
|
1246
|
+
details: hitDetails,
|
|
1247
|
+
};
|
|
1248
|
+
this.frameCache = {
|
|
1249
|
+
width,
|
|
1250
|
+
maxBody,
|
|
1251
|
+
theme: th,
|
|
1252
|
+
spin,
|
|
1253
|
+
interactionVersion: this.state.interactionVersion,
|
|
1254
|
+
scrollOffset: this.state.scrollOffset,
|
|
1255
|
+
itemsSig,
|
|
1256
|
+
lines,
|
|
1257
|
+
};
|
|
1258
|
+
return lines;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
private togglePhase(hit: RenderedHitMap, hitPhase: HitPhaseRow): boolean {
|
|
1262
|
+
const phase = buildPhases(this.items)[hitPhase.phaseIndex];
|
|
1263
|
+
if (!phase) return false;
|
|
1264
|
+
const nextExp = !this.isPhaseExpanded(phase, hitPhase.phaseIndex);
|
|
1265
|
+
const clickedBodyRow = hit.scrollOffset + (hitPhase.row - 2);
|
|
1266
|
+
if (nextExp && clickedBodyRow > hit.scrollOffset + Math.max(1, hit.maxBody - 3)) {
|
|
1267
|
+
this.state.scrollOffset = Math.max(hit.scrollOffset, clickedBodyRow - Math.max(1, hit.maxBody - 4));
|
|
1268
|
+
} else if (this.state.scrollOffset == null) {
|
|
1269
|
+
this.state.scrollOffset = hit.scrollOffset;
|
|
1270
|
+
}
|
|
1271
|
+
this.state.phaseOverrides.set(hitPhase.phaseIndex, nextExp);
|
|
1272
|
+
return true;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
private toggleTool(hit: RenderedHitMap, hitTool: HitToolRow): void {
|
|
1276
|
+
const nextOpen = !this.isDetailOpen(hitTool.tool);
|
|
1277
|
+
const clickedBodyRow = hit.scrollOffset + (hitTool.row - 2);
|
|
1278
|
+
if (nextOpen && clickedBodyRow > hit.scrollOffset + Math.max(1, hit.maxBody - 5)) {
|
|
1279
|
+
this.state.scrollOffset = Math.max(hit.scrollOffset, clickedBodyRow - 1);
|
|
1280
|
+
} else if (this.state.scrollOffset == null) {
|
|
1281
|
+
this.state.scrollOffset = hit.scrollOffset;
|
|
1282
|
+
}
|
|
1283
|
+
this.setDetailOpen(hitTool.tool, nextOpen);
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
handleMouse(event: ProgressMouseEvent): unknown {
|
|
1287
|
+
// Swallow the release/drag/click tail of an action already performed on press,
|
|
1288
|
+
// independent of whether a re-render has painted yet or the pointer drifted by 1 cell.
|
|
1289
|
+
if (event.type === "press") {
|
|
1290
|
+
this.stopScrollHold();
|
|
1291
|
+
this.pressConsumed = false;
|
|
1292
|
+
this.suppressNextClick = false;
|
|
1293
|
+
} else if (this.pressConsumed) {
|
|
1294
|
+
if (event.type === "release") {
|
|
1295
|
+
this.stopScrollHold();
|
|
1296
|
+
this.pressConsumed = false;
|
|
1297
|
+
this.suppressNextClick = true;
|
|
1298
|
+
return { handled: true, render: false };
|
|
1299
|
+
}
|
|
1300
|
+
if (event.type === "drag") {
|
|
1301
|
+
return { handled: true, render: false };
|
|
1302
|
+
}
|
|
1303
|
+
} else if (this.suppressNextClick && event.type === "click") {
|
|
1304
|
+
this.suppressNextClick = false;
|
|
1305
|
+
return { handled: true, render: false };
|
|
1306
|
+
} else if (event.type === "release") {
|
|
1307
|
+
this.stopScrollHold();
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
if (!this.lastHitMap || (event.width > 0 && this.lastHitMap.width !== event.width)) {
|
|
1311
|
+
this.render(event.width);
|
|
1312
|
+
}
|
|
1313
|
+
const hit = this.lastHitMap;
|
|
1314
|
+
if (!hit) return undefined;
|
|
1315
|
+
|
|
1316
|
+
// Forward any mouse event inside an open nested tool detail directly to the real ToolExecutionComponent.
|
|
1317
|
+
for (const region of hit.details) {
|
|
1318
|
+
if (
|
|
1319
|
+
event.y >= region.startRow &&
|
|
1320
|
+
event.y <= region.endRow &&
|
|
1321
|
+
event.x >= region.indentCols &&
|
|
1322
|
+
event.x < region.indentCols + region.width
|
|
1323
|
+
) {
|
|
1324
|
+
const wasOpen = this.isDetailOpen(region.tool);
|
|
1325
|
+
const localEvent = {
|
|
1326
|
+
...event,
|
|
1327
|
+
x: event.x - region.indentCols,
|
|
1328
|
+
y: event.y - region.startRow + region.rowOffset,
|
|
1329
|
+
width: region.width,
|
|
1330
|
+
height: region.endRow - region.startRow + 1 + region.rowOffset,
|
|
1331
|
+
};
|
|
1332
|
+
const res = forwardMouseEvent(region.tool, localEvent);
|
|
1333
|
+
// If clicking [hide] or the body inside the nested tool box collapsed it, keep box state in sync.
|
|
1334
|
+
if (wasOpen && !this.isDetailOpen(region.tool)) {
|
|
1335
|
+
this.state.detailOpen.set(region.tool, false);
|
|
1336
|
+
this.bumpAndRequestRender();
|
|
1337
|
+
} else if (res) {
|
|
1338
|
+
this.state.interactionVersion += 1;
|
|
1339
|
+
}
|
|
1340
|
+
return res;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
const onHeaderBoxToggle = inSpans(hit.headerBoxToggleSpans, event.y, event.x);
|
|
1345
|
+
const onHeaderToggle = inSpans(hit.headerToggleSpans, event.y, event.x);
|
|
1346
|
+
const onScrollUp = inSpans(hit.scrollUpSpans, event.y, event.x);
|
|
1347
|
+
const onScrollDown = inSpans(hit.scrollDownSpans, event.y, event.x);
|
|
1348
|
+
const hitPhase = hit.phases.find((p) => p.row === event.y);
|
|
1349
|
+
const onPhaseChevron = Boolean(hitPhase && event.x >= 1 && event.x <= 6);
|
|
1350
|
+
const hitTool = hit.tools.find((t) => t.row === event.y);
|
|
1351
|
+
const onToolHint = hitTool ? inSpans(hitTool.hintSpans, event.y, event.x) : false;
|
|
1352
|
+
|
|
1353
|
+
// Explicit controls ([show], [hide], [expand all], [collapse all], ↑/↓ N more, and
|
|
1354
|
+
// phase ▸/▾ chevron) act immediately on left press so 1-cell drift never drops the action.
|
|
1355
|
+
if ((onHeaderBoxToggle || onHeaderToggle || onScrollUp || onScrollDown || onToolHint || onPhaseChevron) && event.type === "press" && event.button === "left") {
|
|
1356
|
+
if (onScrollUp || onScrollDown) {
|
|
1357
|
+
const dir: -1 | 1 = onScrollUp ? -1 : 1;
|
|
1358
|
+
const next = Math.max(0, Math.min(hit.maxOffset, hit.scrollOffset + dir * Math.max(1, hit.scrollStep)));
|
|
1359
|
+
this.state.scrollOffset = next;
|
|
1360
|
+
this.pressConsumed = true;
|
|
1361
|
+
this.startScrollHold(dir);
|
|
1362
|
+
this.bumpAndRequestRender();
|
|
1363
|
+
return { handled: true, render: true };
|
|
1364
|
+
}
|
|
1365
|
+
if (onHeaderBoxToggle) {
|
|
1366
|
+
const nextBox = !hit.boxExpanded;
|
|
1367
|
+
this.state.boxExpanded = nextBox;
|
|
1368
|
+
if (!nextBox) {
|
|
1369
|
+
this.state.scrollOffset = null;
|
|
1370
|
+
for (const item of this.items) {
|
|
1371
|
+
if (isToolComponent(item)) this.setDetailOpen(item, false);
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
} else if (onHeaderToggle) {
|
|
1375
|
+
const nextAll = !hit.allExpanded;
|
|
1376
|
+
this.state.phaseOverrides.clear();
|
|
1377
|
+
this.state.allOverride = nextAll;
|
|
1378
|
+
this.state.scrollOffset = nextAll ? 0 : null;
|
|
1379
|
+
if (!nextAll) {
|
|
1380
|
+
for (const item of this.items) {
|
|
1381
|
+
if (isToolComponent(item)) this.setDetailOpen(item, false);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
} else if (onToolHint && hitTool) {
|
|
1385
|
+
this.toggleTool(hit, hitTool);
|
|
1386
|
+
} else if (onPhaseChevron && hitPhase) {
|
|
1387
|
+
this.togglePhase(hit, hitPhase);
|
|
1388
|
+
}
|
|
1389
|
+
this.pressConsumed = true;
|
|
1390
|
+
this.bumpAndRequestRender();
|
|
1391
|
+
return { handled: true, render: true };
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Row body clicks act on single left click so drag-to-select still works for copying text.
|
|
1395
|
+
if (event.type === "click" && event.button === "left" && (event.clickCount ?? 1) === 1) {
|
|
1396
|
+
if (!hit.boxExpanded && hit.summaryRow === event.y) {
|
|
1397
|
+
this.state.boxExpanded = true;
|
|
1398
|
+
this.bumpAndRequestRender();
|
|
1399
|
+
return { handled: true, render: true };
|
|
1400
|
+
}
|
|
1401
|
+
if (hitPhase && this.togglePhase(hit, hitPhase)) {
|
|
1402
|
+
this.bumpAndRequestRender();
|
|
1403
|
+
return { handled: true, render: true };
|
|
1404
|
+
}
|
|
1405
|
+
if (hitTool) {
|
|
1406
|
+
this.toggleTool(hit, hitTool);
|
|
1407
|
+
this.bumpAndRequestRender();
|
|
1408
|
+
return { handled: true, render: true };
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
return undefined;
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// Persistent plain state across /reload (WeakMap keyed by the turn's first tool);
|
|
1417
|
+
// component instances stay module-local so /reload always runs the freshly loaded class.
|
|
1418
|
+
const PROGRESS_STATES_KEY = Symbol.for("pi-arnative.progressGroupStates");
|
|
1419
|
+
const moduleBoxes = new WeakMap<object, TurnProgressBoxComponent>();
|
|
1420
|
+
|
|
1421
|
+
function getProgressStates(): WeakMap<object, TurnBoxState> {
|
|
1422
|
+
const g = globalThis as Record<symbol, WeakMap<object, TurnBoxState> | undefined>;
|
|
1423
|
+
let states = g[PROGRESS_STATES_KEY];
|
|
1424
|
+
if (!states) {
|
|
1425
|
+
states = new WeakMap();
|
|
1426
|
+
g[PROGRESS_STATES_KEY] = states;
|
|
1427
|
+
}
|
|
1428
|
+
return states;
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
function boxForAnchor(anchor: object): TurnProgressBoxComponent {
|
|
1432
|
+
let box = moduleBoxes.get(anchor);
|
|
1433
|
+
if (!box) {
|
|
1434
|
+
const states = getProgressStates();
|
|
1435
|
+
let state = states.get(anchor);
|
|
1436
|
+
if (!state) {
|
|
1437
|
+
state = {
|
|
1438
|
+
phaseOverrides: new Map(),
|
|
1439
|
+
detailOpen: new WeakMap(),
|
|
1440
|
+
interactionVersion: 0,
|
|
1441
|
+
};
|
|
1442
|
+
states.set(anchor, state);
|
|
1443
|
+
}
|
|
1444
|
+
box = new TurnProgressBoxComponent(state);
|
|
1445
|
+
moduleBoxes.set(anchor, box);
|
|
1446
|
+
}
|
|
1447
|
+
return box;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/** Synchronize all turn progress boxes with Pi's Ctrl+O (`setToolsExpanded`) shortcut. */
|
|
1451
|
+
export function syncProgressBoxesExpanded(children: readonly unknown[], expanded: boolean): void {
|
|
1452
|
+
const states = getProgressStates();
|
|
1453
|
+
let changed = false;
|
|
1454
|
+
for (const child of children) {
|
|
1455
|
+
if (!child || typeof child !== "object") continue;
|
|
1456
|
+
const state = states.get(child);
|
|
1457
|
+
if (!state) continue;
|
|
1458
|
+
state.phaseOverrides.clear();
|
|
1459
|
+
state.detailOpen = new WeakMap();
|
|
1460
|
+
state.boxExpanded = expanded;
|
|
1461
|
+
state.allOverride = expanded;
|
|
1462
|
+
state.scrollOffset = expanded ? 0 : null;
|
|
1463
|
+
state.interactionVersion += 1;
|
|
1464
|
+
changed = true;
|
|
1465
|
+
}
|
|
1466
|
+
if (changed) {
|
|
1467
|
+
for (const fn of layoutListeners) fn();
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* Project raw `chatContainer.children` into virtual render children:
|
|
1473
|
+
* contiguous runs of intermediate assistant messages + tool executions become one
|
|
1474
|
+
* `TurnProgressBoxComponent`, while user messages and final conclusion assistant messages
|
|
1475
|
+
* pass through unchanged.
|
|
1476
|
+
*/
|
|
1477
|
+
export function projectTranscriptChildren(
|
|
1478
|
+
children: readonly unknown[],
|
|
1479
|
+
requestRender?: () => void,
|
|
1480
|
+
): unknown[] {
|
|
1481
|
+
if (activeToolLayout() !== "Grouped") return [...children];
|
|
1482
|
+
|
|
1483
|
+
const out: unknown[] = [];
|
|
1484
|
+
let i = 0;
|
|
1485
|
+
|
|
1486
|
+
while (i < children.length) {
|
|
1487
|
+
const child = children[i];
|
|
1488
|
+
if (!isSilentIntermediateAssistant(child) && !isToolComponent(child)) {
|
|
1489
|
+
out.push(child);
|
|
1490
|
+
i += 1;
|
|
1491
|
+
continue;
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// Collect contiguous run of silent intermediate assistants and tool executions.
|
|
1495
|
+
// Assistant messages with visible text stay outside as normal chat messages.
|
|
1496
|
+
const run: (AssistantLike | ToolLike)[] = [];
|
|
1497
|
+
let firstVisibleTool: ToolLike | undefined;
|
|
1498
|
+
while (i < children.length) {
|
|
1499
|
+
const cur = children[i];
|
|
1500
|
+
if (isSilentIntermediateAssistant(cur)) {
|
|
1501
|
+
run.push(cur);
|
|
1502
|
+
i += 1;
|
|
1503
|
+
} else if (isToolComponent(cur)) {
|
|
1504
|
+
run.push(cur);
|
|
1505
|
+
if (!cur.hideComponent && !firstVisibleTool) firstVisibleTool = cur;
|
|
1506
|
+
i += 1;
|
|
1507
|
+
} else {
|
|
1508
|
+
break;
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
// A run with zero visible tools (e.g. tool call streaming hasn't created a component yet)
|
|
1513
|
+
// passes through untouched so nothing disappears.
|
|
1514
|
+
if (!firstVisibleTool) {
|
|
1515
|
+
for (const item of run) out.push(item);
|
|
1516
|
+
continue;
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
const box = boxForAnchor(firstVisibleTool);
|
|
1520
|
+
box.update(run, requestRender);
|
|
1521
|
+
out.push(box);
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
return out;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
// Keep Ctrl+O (`setToolsExpanded`) synchronized with progress box accordion state.
|
|
1528
|
+
if (InteractiveMode?.prototype) {
|
|
1529
|
+
const modeProto = InteractiveMode.prototype as unknown as Record<string, unknown>;
|
|
1530
|
+
if (typeof modeProto.setToolsExpanded === "function") {
|
|
1531
|
+
patchOnce(
|
|
1532
|
+
modeProto,
|
|
1533
|
+
"setToolsExpanded",
|
|
1534
|
+
(orig) =>
|
|
1535
|
+
function (this: { chatContainer?: { children?: unknown[] } }, expanded: boolean) {
|
|
1536
|
+
const res = orig.call(this, expanded);
|
|
1537
|
+
if (Array.isArray(this.chatContainer?.children)) {
|
|
1538
|
+
syncProgressBoxesExpanded(this.chatContainer.children, expanded);
|
|
1539
|
+
}
|
|
1540
|
+
return res;
|
|
1541
|
+
},
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
export const __test = {
|
|
1547
|
+
TEST_TOOL_MARKER,
|
|
1548
|
+
TEST_ASSISTANT_MARKER,
|
|
1549
|
+
PROGRESS_BOX_MARKER,
|
|
1550
|
+
EXPAND_ALL_LABEL,
|
|
1551
|
+
COLLAPSE_ALL_LABEL,
|
|
1552
|
+
SHOW_LABEL,
|
|
1553
|
+
HIDE_LABEL,
|
|
1554
|
+
extractNarration,
|
|
1555
|
+
countDiffStats,
|
|
1556
|
+
heuristicPhaseTitle,
|
|
1557
|
+
};
|