pi-stereo-view 0.1.10 → 0.1.12
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/CHANGELOG.md +17 -0
- package/README.md +3 -1
- package/package.json +1 -1
- package/src/compact-tools.ts +3 -2
- package/src/content.ts +71 -14
- package/src/index.ts +22 -6
- package/src/layout.ts +67 -6
- package/src/native.ts +67 -39
- package/src/nested-calls.ts +61 -0
- package/src/nested-tree.ts +161 -0
- package/src/tool-indent.ts +21 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.12 - 2026-11-04
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Keep extension widgets at the bottom of Activity while its history scrolls independently. Widgets return to Pi's normal placement when they cannot fit safely.
|
|
8
|
+
- Restore the nested Codemode call tree with independent controls and original native result panels, including shared results and saved sessions.
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Keep committed editor batch results visible under their originating nested calls.
|
|
13
|
+
|
|
14
|
+
## 0.1.11 - 2026-10-29
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- Show nested tool calls in Activity with their arguments, status and native result views. Recorded calls remain visible after reopening a session.
|
|
19
|
+
|
|
3
20
|
## 0.1.10 - 2026-10-26
|
|
4
21
|
|
|
5
22
|
### Changed
|
package/README.md
CHANGED
|
@@ -27,7 +27,9 @@ Unrecognized content stays in Conversation unless its metadata links it to a kno
|
|
|
27
27
|
|
|
28
28
|
Late output linked to a tool call appears directly below that call in Activity, even if other work has happened since. Combined history keeps its original order.
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Expand a parent tool such as Codemode to browse its nested call tree. Adjacent calls of the same tool form a group with individual call headers. Each independent native result opens under its call. A combined result covering several calls stays whole inside their group. Panels without child execution identities remain available inside the parent's output disclosure. Session restoration uses Pi's saved nested-call metadata and the original saved panels.
|
|
31
|
+
|
|
32
|
+
Messages and tools keep their familiar appearance. Extension widgets appear at the bottom of Activity when they fit, below its independently scrolling history, while the input stays full width below both panels. If the widgets cannot fit without being clipped, the view temporarily returns to combined history and Pi's normal widget placement. Combined history and zoomed Conversation also keep normal widget placement.
|
|
31
33
|
|
|
32
34
|
## Features
|
|
33
35
|
|
package/package.json
CHANGED
package/src/compact-tools.ts
CHANGED
|
@@ -23,6 +23,7 @@ export function toolDescription(args: unknown): string {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
interface CompactToolOptions {
|
|
26
|
+
alignControl?: boolean;
|
|
26
27
|
clock?(): string;
|
|
27
28
|
gutter?(): number;
|
|
28
29
|
count?(): number;
|
|
@@ -39,10 +40,10 @@ export class CompactTool implements Component {
|
|
|
39
40
|
width = Math.max(0, width - (this.options.gutter?.() ?? 0));
|
|
40
41
|
const { name, args, status } = this.summary();
|
|
41
42
|
const theme = this.theme();
|
|
42
|
-
const dot = theme.fg(status === "running" ? "warning" : status, "●");
|
|
43
|
+
const dot = theme.fg(status === "running" ? "warning" : status === "unknown" ? "dim" : status, "●");
|
|
43
44
|
const count = this.options.count?.();
|
|
44
45
|
const description = count ? "" : toolDescription(args);
|
|
45
|
-
const control = this.options.toggle ? theme.fg("muted", this.options.expanded?.() ? "▾" : "▸") + " " : "";
|
|
46
|
+
const control = this.options.toggle ? theme.fg("muted", this.options.expanded?.() ? "▾" : "▸") + " " : this.options.alignControl ? " " : "";
|
|
46
47
|
const text = `${dot} ${control}${singleLine(name)}${count ? ` ×${count}` : ""}${description ? " " + theme.fg("muted", description) : ""}`;
|
|
47
48
|
const label = this.options.clock?.() ?? "";
|
|
48
49
|
const clock = label && width >= visibleWidth(label) + 8 ? label : "";
|
package/src/content.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Container, Markdown, Text, stripTerminalSequences, type Component } from "@earendil-works/pi-tui";
|
|
2
2
|
import { searchText, type SearchEntry } from "./search.js";
|
|
3
|
+
import { NestedCalls } from "./nested-calls.js";
|
|
3
4
|
|
|
4
5
|
export type Pane = "activity" | "chat";
|
|
5
6
|
export type ContentKind = "dialogue" | "system" | "thinking" | "tool" | "shell";
|
|
@@ -34,7 +35,7 @@ interface NativeAssistant extends Container {
|
|
|
34
35
|
export interface ToolSummary {
|
|
35
36
|
name: string;
|
|
36
37
|
args: unknown;
|
|
37
|
-
status: "running" | "success" | "error";
|
|
38
|
+
status: "running" | "success" | "error" | "unknown";
|
|
38
39
|
}
|
|
39
40
|
/** One native owner and its pane projection; spacing and search retain that same owner. */
|
|
40
41
|
export interface ContentEntry {
|
|
@@ -87,13 +88,49 @@ const nativeKinds: Record<string, { kind: ContentKind; accepts(content: NativeCo
|
|
|
87
88
|
},
|
|
88
89
|
};
|
|
89
90
|
|
|
91
|
+
/** Traverse the same component links as the native render cache, including wrappers. */
|
|
92
|
+
export function componentTree(root: Component | undefined): NativeContent[] {
|
|
93
|
+
const seen = new Set<Component>();
|
|
94
|
+
const nodes: NativeContent[] = [];
|
|
95
|
+
const visit = (component: Component | undefined): void => {
|
|
96
|
+
if (!component || seen.has(component) || typeof component.render !== "function") return;
|
|
97
|
+
seen.add(component);
|
|
98
|
+
const node = component as NativeContent & { children?: Component[]; child?: Component; component?: Component };
|
|
99
|
+
nodes.push(node);
|
|
100
|
+
if (Array.isArray(node.children)) for (const child of node.children) visit(child);
|
|
101
|
+
visit(node.child); visit(node.component); visit(node.customComponent);
|
|
102
|
+
};
|
|
103
|
+
visit(root);
|
|
104
|
+
return nodes;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Preserve searchable tool-result text even when a custom renderer abbreviates it. */
|
|
108
|
+
function nestedResultText(component: Component | undefined): string[] {
|
|
109
|
+
return componentTree(component).flatMap(node => nameOf(node) === "ToolExecutionComponent" ? [contentText(node.result?.content)] : []).filter(Boolean);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Unwrap layout-only containers while retaining each original renderer and shared note. */
|
|
113
|
+
function nativePanelParts(source: NativeContent): Component[] | undefined {
|
|
114
|
+
if (!source.customComponent) return undefined;
|
|
115
|
+
const parts: Component[] = [];
|
|
116
|
+
const seen = new Set<Component>();
|
|
117
|
+
const visit = (component: Component): void => {
|
|
118
|
+
if (seen.has(component)) return;
|
|
119
|
+
seen.add(component);
|
|
120
|
+
if (component.constructor === Container) for (const child of (component as Container).children) visit(child);
|
|
121
|
+
else if (nameOf(component) !== "Spacer") parts.push(component);
|
|
122
|
+
};
|
|
123
|
+
visit(source.customComponent);
|
|
124
|
+
return parts.filter(part => nameOf(part) === "ToolExecutionComponent").length > 1 ? parts : undefined;
|
|
125
|
+
}
|
|
90
126
|
/** Shared semantic index for painting and search, independent of wording, severity and lifecycle. */
|
|
91
127
|
export class NativeContentIndex {
|
|
92
128
|
private signature: unknown[] = [];
|
|
93
129
|
private content: ContentEntry[] = [];
|
|
130
|
+
private readonly expandedPanels = new WeakMap<Component, Component>();
|
|
94
131
|
private readonly views = new WeakMap<NativeAssistant, Partial<Record<Pane, Container>>>();
|
|
95
132
|
|
|
96
|
-
constructor(private readonly source: Container) {}
|
|
133
|
+
constructor(private readonly source: Container, readonly nested = new NestedCalls()) {}
|
|
97
134
|
|
|
98
135
|
/** Return the same classification until native ownership or assistant content changes. */
|
|
99
136
|
entries(pane?: Pane): ContentEntry[] {
|
|
@@ -121,13 +158,19 @@ export class NativeContentIndex {
|
|
|
121
158
|
private refresh(): void {
|
|
122
159
|
const history = this.source.children[2] as Container;
|
|
123
160
|
if (!Array.isArray(history?.children)) throw new Error("Pi's history document is not supported");
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
161
|
+
const makeSignature = (): unknown[] => {
|
|
162
|
+
const values: unknown[] = [this.nested.revision, ...this.source.children, ...history.children];
|
|
163
|
+
for (const component of history.children) {
|
|
164
|
+
const metadata = component as NativeContent;
|
|
165
|
+
values.push(metadata.toolCallId, metadata.entry?.data?.parentToolCallId, metadata.message?.details?.parentToolCallId, metadata.customComponent);
|
|
166
|
+
if (metadata.entry?.data?.parentToolCallId || metadata.message?.details?.parentToolCallId)
|
|
167
|
+
values.push(...(nativePanelParts(metadata) ?? []));
|
|
168
|
+
const native = assistant(component);
|
|
169
|
+
if (native) values.push(native.lastMessage, ...native.contentContainer.children);
|
|
170
|
+
}
|
|
171
|
+
return values;
|
|
172
|
+
};
|
|
173
|
+
const signature = makeSignature();
|
|
131
174
|
if (signature.length === this.signature.length && signature.every((value, index) => value === this.signature[index])) return;
|
|
132
175
|
const owners = new Map<string, NativeContent>();
|
|
133
176
|
for (const component of history.children) {
|
|
@@ -152,7 +195,16 @@ export class NativeContentIndex {
|
|
|
152
195
|
const kind = adapter?.accepts(native) ? adapter.kind : "system";
|
|
153
196
|
const parentId = native.entry?.data?.parentToolCallId ?? native.message?.details?.parentToolCallId;
|
|
154
197
|
const owner = kind === "system" && typeof parentId === "string" ? owners.get(parentId) : undefined;
|
|
155
|
-
|
|
198
|
+
if (owner && this.expandedPanels.get(component) !== owner) {
|
|
199
|
+
native.setExpanded?.(true);
|
|
200
|
+
this.expandedPanels.set(component, owner);
|
|
201
|
+
} else if (!owner) this.expandedPanels.delete(component);
|
|
202
|
+
const parts = owner && this.nested.entries(owner.toolCallId!).length ? nativePanelParts(native) : undefined;
|
|
203
|
+
if (parts) for (const part of parts) {
|
|
204
|
+
const adapter = nativeKinds[nameOf(part)];
|
|
205
|
+
const partKind = adapter?.accepts(part as NativeContent) ? adapter.kind : "system";
|
|
206
|
+
entries.push(this.entry(part, partKind, [], true, owner));
|
|
207
|
+
} else entries.push(this.entry(component, kind, spacing, true, owner));
|
|
156
208
|
}
|
|
157
209
|
spacing = [];
|
|
158
210
|
}
|
|
@@ -166,7 +218,7 @@ export class NativeContentIndex {
|
|
|
166
218
|
}
|
|
167
219
|
this.content = entries.flatMap(entry => entry.owner === entry.source
|
|
168
220
|
? [entry, ...(linked.get(entry.source) ?? [])] : []);
|
|
169
|
-
this.signature =
|
|
221
|
+
this.signature = makeSignature();
|
|
170
222
|
}
|
|
171
223
|
|
|
172
224
|
private entry(source: Component, kind: ContentKind, spacing: Component[], compact: boolean, owner?: NativeContent): ContentEntry {
|
|
@@ -183,7 +235,7 @@ export class NativeContentIndex {
|
|
|
183
235
|
if (typeof native.toolName !== "string") throw new Error("Pi's tool metadata is not supported");
|
|
184
236
|
return {
|
|
185
237
|
label: native.toolName,
|
|
186
|
-
text: [native.toolName, JSON.stringify(native.args, null, 2), contentText(native.result?.content)].filter(Boolean).join("\n"),
|
|
238
|
+
text: [native.toolName, JSON.stringify(native.args, null, 2), contentText(native.result?.content), this.nested.search(native.toolCallId ?? "")].filter(Boolean).join("\n"),
|
|
187
239
|
reveal: () => {
|
|
188
240
|
if (typeof native.setExpanded !== "function") throw new Error("Pi's tool reveal is not supported");
|
|
189
241
|
native.setExpanded(true);
|
|
@@ -198,10 +250,15 @@ export class NativeContentIndex {
|
|
|
198
250
|
if (typeof native.text !== "string") throw new Error("Pi's user text metadata is not supported");
|
|
199
251
|
return { label: "User", text: native.text };
|
|
200
252
|
}
|
|
253
|
+
const saved = contentText(native.message?.content) || native.message?.summary || "";
|
|
254
|
+
const rendered = componentText(native.customComponent ?? source) ||
|
|
255
|
+
searchText(source.render(width).join("\n")).split("\n").map(line => line.trimEnd()).join("\n").replace(/^\n+|\n+$/g, "");
|
|
256
|
+
const text = [saved || rendered];
|
|
257
|
+
if (native.customComponent && rendered && !text.some(part => part.includes(rendered))) text.push(rendered);
|
|
258
|
+
for (const result of nestedResultText(native.customComponent)) if (!text.some(part => part.includes(result))) text.push(result);
|
|
201
259
|
return {
|
|
202
260
|
label: owner?.toolName ?? (native.message?.summary !== undefined ? "Summary" : "System"),
|
|
203
|
-
text:
|
|
204
|
-
searchText(source.render(width).join("\n")).split("\n").map(line => line.trimEnd()).join("\n").replace(/^\n+|\n+$/g, ""),
|
|
261
|
+
text: text.filter(Boolean).join("\n"),
|
|
205
262
|
reveal: typeof native.setExpanded === "function" ? () => native.setExpanded!(true) : undefined,
|
|
206
263
|
};
|
|
207
264
|
} };
|
package/src/index.ts
CHANGED
|
@@ -5,12 +5,14 @@ import { TranscriptLayout } from "./layout.js";
|
|
|
5
5
|
import { TOOL_START_ENTRY, TranscriptTimestamps } from "./timestamps.js";
|
|
6
6
|
import { defaults, loadPreferences, savePreferences, type Preferences } from "./preferences.js";
|
|
7
7
|
import { beforeFirstFrame } from "./startup.js";
|
|
8
|
+
import { NestedCalls } from "./nested-calls.js";
|
|
8
9
|
|
|
9
10
|
/** Split native fullscreen history without changing message content or the input dock. */
|
|
10
11
|
export default async function transcript(pi: ExtensionAPI): Promise<void> {
|
|
11
12
|
let layout: TranscriptLayout | undefined;
|
|
12
13
|
let host: TUI | undefined;
|
|
13
14
|
let timestamps: TranscriptTimestamps | undefined;
|
|
15
|
+
const nested = new NestedCalls();
|
|
14
16
|
let unsubscribeInput: (() => void) | undefined;
|
|
15
17
|
let writes = Promise.resolve();
|
|
16
18
|
let preferences: Preferences = { ...defaults };
|
|
@@ -94,6 +96,7 @@ export default async function transcript(pi: ExtensionAPI): Promise<void> {
|
|
|
94
96
|
const createLayout = (tui: TUI, ui: ExtensionContext["ui"], session: ExtensionContext["sessionManager"]): void => {
|
|
95
97
|
host = tui;
|
|
96
98
|
timestamps = new TranscriptTimestamps(session);
|
|
99
|
+
nested.restore(session.getBranch());
|
|
97
100
|
layout = new TranscriptLayout(preferences, () => ui.theme,
|
|
98
101
|
message => ui.notify(message, "warning"), () => persist(ui), timestamps,
|
|
99
102
|
() => { void openSettings(ui).catch(error => ui.notify(String(error), "error")); }, text => {
|
|
@@ -107,7 +110,7 @@ export default async function transcript(pi: ExtensionAPI): Promise<void> {
|
|
|
107
110
|
}
|
|
108
111
|
void copyToClipboard(text).then(() => ui.notify("Block sent to clipboard.", "info"),
|
|
109
112
|
error => ui.notify("Could not copy block: " + String(error), "error"));
|
|
110
|
-
}, () => pi.getAllTools());
|
|
113
|
+
}, () => pi.getAllTools(), nested);
|
|
111
114
|
try { ensureLayout(); }
|
|
112
115
|
catch (error) {
|
|
113
116
|
layout.dispose();
|
|
@@ -171,18 +174,31 @@ export default async function transcript(pi: ExtensionAPI): Promise<void> {
|
|
|
171
174
|
ensureLayout();
|
|
172
175
|
});
|
|
173
176
|
pi.on("message_end", event => {
|
|
177
|
+
nested.absorb(event.message);
|
|
174
178
|
timestamps?.observe(event.message, true);
|
|
175
179
|
layout?.refreshSearch();
|
|
176
180
|
});
|
|
177
|
-
pi.on("tool_execution_update", () =>
|
|
178
|
-
|
|
181
|
+
pi.on("tool_execution_update", () => {
|
|
182
|
+
layout?.refreshSearch();
|
|
183
|
+
});
|
|
184
|
+
pi.on("tool_execution_end", event => {
|
|
185
|
+
nested.end(event);
|
|
186
|
+
layout?.refreshSearch();
|
|
187
|
+
host?.requestRender();
|
|
188
|
+
});
|
|
179
189
|
pi.on("tool_execution_start", event => {
|
|
180
|
-
if (
|
|
190
|
+
if (event.parentToolCallId) {
|
|
191
|
+
nested.start(event);
|
|
192
|
+
layout?.refreshSearch();
|
|
193
|
+
host?.requestRender();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (!timestamps) return;
|
|
181
197
|
pi.appendEntry(TOOL_START_ENTRY, { toolCallId: event.toolCallId, time: Date.now() });
|
|
182
198
|
host?.requestRender();
|
|
183
199
|
});
|
|
184
|
-
pi.on("session_tree", () => { closeSearch(); timestamps?.reset(); });
|
|
185
|
-
pi.on("session_compact", () => { closeSearch(); timestamps?.reset(); });
|
|
200
|
+
pi.on("session_tree", (_event, ctx) => { closeSearch(); nested.restore(ctx.sessionManager.getBranch()); timestamps?.reset(); host?.requestRender(); });
|
|
201
|
+
pi.on("session_compact", (_event, ctx) => { closeSearch(); nested.restore(ctx.sessionManager.getBranch()); timestamps?.reset(); host?.requestRender(); });
|
|
186
202
|
pi.on("session_shutdown", async () => {
|
|
187
203
|
releaseStartup();
|
|
188
204
|
closeSearch();
|
package/src/layout.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from "@earendil-works/pi-tui";
|
|
7
7
|
import { NativeDocument, type Pane } from "./native.js";
|
|
8
8
|
import { NativeContentIndex } from "./content.js";
|
|
9
|
+
import { NestedCalls } from "./nested-calls.js";
|
|
9
10
|
import type { Preferences } from "./preferences.js";
|
|
10
11
|
import type { TranscriptTimestamps } from "./timestamps.js";
|
|
11
12
|
import { PANEL_SEARCH_ROWS, PanelSearch, PanelSearchShortcut, type SearchHit } from "./search.js";
|
|
@@ -34,6 +35,15 @@ class PanelScrollView extends ScrollView {
|
|
|
34
35
|
return undefined;
|
|
35
36
|
}
|
|
36
37
|
}
|
|
38
|
+
class WidgetScrollView extends ScrollView {
|
|
39
|
+
override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
|
|
40
|
+
if (event.type !== "wheel" || !event.wheelDelta) return undefined;
|
|
41
|
+
this.scrollBy(event.wheelDelta);
|
|
42
|
+
return { handled: true, target: { component: this, originX: event.screenX - event.x,
|
|
43
|
+
originY: event.screenY - event.y, width: event.width, height: event.height } };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
class TranscriptColumns extends HStack {
|
|
38
48
|
/** Reserve a fixed width for Conversation, independent of its content. */
|
|
39
49
|
sizeConversation(component: Component, width: number): void {
|
|
@@ -74,10 +84,13 @@ class TranscriptRoot extends VStack {
|
|
|
74
84
|
}
|
|
75
85
|
|
|
76
86
|
class TranscriptPane extends VStack {
|
|
77
|
-
constructor(private readonly header: Component, private readonly view: ScrollView, private readonly height: () => number
|
|
87
|
+
constructor(private readonly header: Component, private readonly view: ScrollView, private readonly height: () => number,
|
|
88
|
+
private readonly widgets?: ScrollView, private readonly widgetsVisible: () => boolean = () => false) {
|
|
78
89
|
super([
|
|
79
90
|
{ component: header, basis: "auto", shrink: 0 },
|
|
80
91
|
{ component: view, basis: 0, grow: 1, minSize: 1 },
|
|
92
|
+
...(widgets ? [{ component: widgets, basis: "auto" as const, minSize: 0,
|
|
93
|
+
visible: () => widgetsVisible() }] : []),
|
|
81
94
|
]);
|
|
82
95
|
}
|
|
83
96
|
|
|
@@ -97,8 +110,9 @@ class TranscriptPane extends VStack {
|
|
|
97
110
|
// Empty preview rows avoid composing colored history text in ancestor measurements.
|
|
98
111
|
const headerRows = this.header.render(width).length;
|
|
99
112
|
const contentRows = this.view.children[0].render(this.view.getContentWidth(width)).length;
|
|
113
|
+
const widgetRows = this.widgets && this.widgetsVisible() ? this.widgets.render(width).length : 0;
|
|
100
114
|
const available = Math.max(1, this.height() - headerRows);
|
|
101
|
-
return Array<string>(headerRows + Math.min(contentRows, available)).fill("");
|
|
115
|
+
return Array<string>(headerRows + Math.min((widgetRows ? Math.max(1, contentRows) : contentRows) + widgetRows, available)).fill("");
|
|
102
116
|
}
|
|
103
117
|
}
|
|
104
118
|
|
|
@@ -136,6 +150,7 @@ export class TranscriptLayout {
|
|
|
136
150
|
private readonly openSettings: () => void,
|
|
137
151
|
private readonly copy?: (text: string | undefined) => void,
|
|
138
152
|
private readonly getTools: () => readonly ToolInfo[] = () => [],
|
|
153
|
+
private readonly nested = new NestedCalls(),
|
|
139
154
|
) {}
|
|
140
155
|
|
|
141
156
|
private button(icon: () => string, action: () => void, enabled = (): boolean => true, background?: () => Color): Component {
|
|
@@ -195,6 +210,22 @@ export class TranscriptLayout {
|
|
|
195
210
|
this.warn("pi-stereo-view split is unavailable: Pi's history document is not supported.");
|
|
196
211
|
return;
|
|
197
212
|
}
|
|
213
|
+
// Route only the known native dock shape. Keep the original untouched for fallback.
|
|
214
|
+
const nativeDock = dock as unknown as { entries: Array<Exclude<StackChild, Component>>; children: Component[] };
|
|
215
|
+
const entries = Array.isArray(nativeDock.entries) ? nativeDock.entries.map(entry => ({ ...entry })) : [];
|
|
216
|
+
const supportedWidgets = entries.length === 6 && entries.every((entry, i) => entry.component === nativeDock.children[i]) &&
|
|
217
|
+
[2, 4].every(i => entries[i].component.constructor.name === "Container") &&
|
|
218
|
+
entries[3].minSize === 3;
|
|
219
|
+
const above = supportedWidgets ? entries[2].component as Container : undefined;
|
|
220
|
+
const below = supportedWidgets ? entries[4].component as Container : undefined;
|
|
221
|
+
const projectedDock = supportedWidgets ? new VStack(entries.filter((_, i) => i !== 2 && i !== 4)) : undefined;
|
|
222
|
+
const widgetTree = above && below ? new VStack([above, below]) : undefined;
|
|
223
|
+
const widgetView = widgetTree ? new WidgetScrollView(widgetTree, { overscroll: "contain" }) : undefined;
|
|
224
|
+
const widgetsPresent = (): boolean => !!above && !!below && (above.children.length > 1 || below.children.length > 0);
|
|
225
|
+
const dockUnchanged = (): boolean => supportedWidgets && nativeDock.entries.length === 6 &&
|
|
226
|
+
nativeDock.entries.every((entry, i) => entry.component === nativeDock.children[i] &&
|
|
227
|
+
Object.keys({ ...entry, ...entries[i] }).every(key =>
|
|
228
|
+
entry[key as keyof typeof entry] === entries[i][key as keyof typeof entry]));
|
|
198
229
|
this.host = host;
|
|
199
230
|
this.original = root;
|
|
200
231
|
this.failed = false;
|
|
@@ -202,7 +233,7 @@ export class TranscriptLayout {
|
|
|
202
233
|
const timestampGutter = (): number => scroll.scrollbar === "auto" ? 1 : 0;
|
|
203
234
|
const showTimestamps = (): boolean => this.preferences.showTimestamps;
|
|
204
235
|
const showCopyButtons = (): boolean => this.preferences.showCopyButtons;
|
|
205
|
-
const index = new NativeContentIndex(document);
|
|
236
|
+
const index = new NativeContentIndex(document, this.nested);
|
|
206
237
|
const activityDocument = new NativeDocument(document, "activity", this.timestamps, this.theme, timestampGutter, showTimestamps,
|
|
207
238
|
() => this.activate("activity"), false, index, this.copy, showCopyButtons, () => this.preferences.compactTools, this.getTools,
|
|
208
239
|
() => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); });
|
|
@@ -225,9 +256,12 @@ export class TranscriptLayout {
|
|
|
225
256
|
() => this.searchPane() === "activity");
|
|
226
257
|
this.chat = new PanelScrollView(chatDocument, scrollOptions, () => this.activate("chat"), () => this.searchPane() === "chat");
|
|
227
258
|
let historyHeight = 0;
|
|
259
|
+
let projectWidgets = false;
|
|
228
260
|
const shown = ({ width, height }: { width: number; height: number }): boolean => {
|
|
229
261
|
const previousHeight = historyHeight;
|
|
230
|
-
|
|
262
|
+
const candidate = !!projectedDock && widgetsPresent() && dockUnchanged() && !this.failed &&
|
|
263
|
+
this.preferences.mode === "split" && this.preferences.zoom !== "chat";
|
|
264
|
+
historyHeight = Math.max(0, height - (candidate ? projectedDock : dock).render(width).length - 1);
|
|
231
265
|
const search = this.search;
|
|
232
266
|
if (search && previousHeight !== historyHeight) queueMicrotask(() => {
|
|
233
267
|
if (this.search === search) search.bar.reposition();
|
|
@@ -238,6 +272,7 @@ export class TranscriptLayout {
|
|
|
238
272
|
this.preferences.layout === "columns" ? width >= MIN_CHAT + MIN_ACTIVITY + 1 && historyHeight >= MIN_ROWS + searchRows :
|
|
239
273
|
width >= MIN_CHAT && historyHeight >= MIN_ROWS * 2 + 1 + searchRows;
|
|
240
274
|
this.fallback = this.preferences.mode === "split" && !fits;
|
|
275
|
+
projectWidgets = candidate && fits;
|
|
241
276
|
if (this.search && (this.fallback || this.failed || this.preferences.mode !== "split" ||
|
|
242
277
|
(this.preferences.zoom && this.preferences.zoom !== this.search.pane))) this.closeSearch(false);
|
|
243
278
|
this.searchShortcut.sync(!this.failed && this.preferences.mode === "split" && fits);
|
|
@@ -246,6 +281,28 @@ export class TranscriptLayout {
|
|
|
246
281
|
if (this.preferences.mode !== "split" || !fits) return false;
|
|
247
282
|
// Both panels project the same native history and supported content kinds.
|
|
248
283
|
activityDocument.check();
|
|
284
|
+
if (projectWidgets && widgetTree && widgetsPresent()) {
|
|
285
|
+
const chatWidth = Math.min(width - MIN_ACTIVITY - 1,
|
|
286
|
+
Math.max(MIN_CHAT, this.preferences.chatWidth ?? this.defaultWidth(width)));
|
|
287
|
+
const widgetWidth = this.preferences.zoom === "activity" || this.preferences.layout === "rows"
|
|
288
|
+
? width : width - chatWidth - 1;
|
|
289
|
+
const chatSearch = this.search?.pane === "chat" ? PANEL_SEARCH_ROWS : 0;
|
|
290
|
+
const activitySearch = this.search?.pane === "activity" ? PANEL_SEARCH_ROWS : 0;
|
|
291
|
+
const chatMin = MIN_ROWS + chatSearch;
|
|
292
|
+
const activityMin = MIN_ROWS + activitySearch;
|
|
293
|
+
const chatLimit = Math.max(chatMin, Math.min((this.preferences.chatHeight ?? this.automaticHeight) + chatSearch,
|
|
294
|
+
historyHeight - activityMin - 1));
|
|
295
|
+
const activityHeight = this.preferences.zoom || this.preferences.layout === "columns" ? historyHeight :
|
|
296
|
+
historyHeight - 1 - Math.min(chatLimit, Math.max(chatMin, chatPane.render(width).length));
|
|
297
|
+
// Do not clip opaque interactive widgets: their own wheel handlers may consume scrolling.
|
|
298
|
+
// If the entire surface cannot fit, leave Pi's original dock available instead.
|
|
299
|
+
const budget = Math.max(0, activityHeight - 3 - activitySearch);
|
|
300
|
+
if (widgetTree.render(widgetWidth).length > budget) {
|
|
301
|
+
projectWidgets = false;
|
|
302
|
+
this.fallback = true;
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
249
306
|
this.activity?.setScrollbar(scroll.scrollbar);
|
|
250
307
|
this.chat?.setScrollbar(scroll.scrollbar);
|
|
251
308
|
columns.sizeConversation(chatPane, this.preferences.chatWidth ?? this.defaultWidth(width));
|
|
@@ -258,6 +315,7 @@ export class TranscriptLayout {
|
|
|
258
315
|
historyHeight - activityMinimum - 1)), chatMinimum, activityMinimum);
|
|
259
316
|
return true;
|
|
260
317
|
} catch (error) {
|
|
318
|
+
projectWidgets = false;
|
|
261
319
|
this.failed = true;
|
|
262
320
|
this.searchShortcut.dispose();
|
|
263
321
|
this.disposeDocuments?.();
|
|
@@ -332,7 +390,8 @@ export class TranscriptLayout {
|
|
|
332
390
|
{ component: this.searchRows[target], basis: "auto", shrink: 0,
|
|
333
391
|
visible: () => this.search?.pane === target },
|
|
334
392
|
]);
|
|
335
|
-
return new TranscriptPane(header, view, () => historyHeight
|
|
393
|
+
return new TranscriptPane(header, view, () => historyHeight,
|
|
394
|
+
target === "activity" ? widgetView : undefined, () => projectWidgets && widgetsPresent());
|
|
336
395
|
};
|
|
337
396
|
const activityPane = pane("Activity", "activity", this.activity);
|
|
338
397
|
const chatPane = pane("Conversation", "chat", this.chat);
|
|
@@ -408,7 +467,9 @@ export class TranscriptLayout {
|
|
|
408
467
|
{ component: chatPane, basis: "auto", minSize: MIN_ROWS,
|
|
409
468
|
visible: viewport => shown(viewport) && this.preferences.zoom === "chat" },
|
|
410
469
|
{ component: transcript, basis: 0, grow: 1, minSize: 1, visible: viewport => !shown(viewport) },
|
|
411
|
-
{ component:
|
|
470
|
+
...(projectedDock ? [{ component: projectedDock, basis: "auto" as const, shrink: 0, minSize: 1,
|
|
471
|
+
visible: () => projectWidgets }] : []),
|
|
472
|
+
{ component: dock, basis: "auto", shrink: 0, minSize: 1, visible: () => !projectWidgets },
|
|
412
473
|
]);
|
|
413
474
|
this.mounted = mounted;
|
|
414
475
|
host.setLayoutRoot(mounted);
|
package/src/native.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Theme, ToolInfo } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { Container, Spacer, Text,
|
|
2
|
+
import { Container, Spacer, Text, type Component, type TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
3
3
|
import { searchText, type SearchEntry, type SearchHit } from "./search.js";
|
|
4
4
|
import { highlightSearchRows, searchHitRow, type SearchClock } from "./search-highlight.js";
|
|
5
5
|
import { CompactMessage, type TranscriptTimestamps } from "./timestamps.js";
|
|
@@ -7,6 +7,8 @@ import { NativeContentIndex, type ContentEntry, type Pane } from "./content.js";
|
|
|
7
7
|
import { CompactTool } from "./compact-tools.js";
|
|
8
8
|
import { toolPresentations, type ToolPresentation } from "./tool-presentation.js";
|
|
9
9
|
import { toolGroups, groupSummary, type ToolGroup } from "./tool-groups.js";
|
|
10
|
+
import { ToolIndent } from "./tool-indent.js";
|
|
11
|
+
import { NestedTree, type NativeTreeBody } from "./nested-tree.js";
|
|
10
12
|
export type { Pane } from "./content.js";
|
|
11
13
|
|
|
12
14
|
/** Decorate one selected block without changing native text or mouse coordinates. */
|
|
@@ -41,28 +43,6 @@ class SearchHighlight implements Component {
|
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
/** Keep hierarchy guides outside native rendering, search and pointer coordinates. */
|
|
45
|
-
class ToolIndent extends Container {
|
|
46
|
-
private readonly prefix: string;
|
|
47
|
-
|
|
48
|
-
constructor(component: Component, depth: number, body: boolean, private readonly theme: () => Theme) {
|
|
49
|
-
super();
|
|
50
|
-
this.addChild(component);
|
|
51
|
-
this.prefix = " │".repeat(depth) + (body ? " " : "");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
override render(width: number): string[] {
|
|
55
|
-
const inset = Math.min(width, this.prefix.length);
|
|
56
|
-
const prefix = this.theme().fg("dim", this.prefix.slice(0, inset));
|
|
57
|
-
return super.render(Math.max(0, width - inset)).map(line => truncateToWidth(prefix + line, width, ""));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
|
|
61
|
-
const inset = Math.min(event.width, this.prefix.length);
|
|
62
|
-
if (event.x < inset) return undefined;
|
|
63
|
-
return super.handleMouse({ ...event, x: event.x - inset, width: Math.max(0, event.width - inset) });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
46
|
/**
|
|
67
47
|
* Project the live native component tree, not messages or renderer strings.
|
|
68
48
|
* Assistant shells keep the native render method and original content components.
|
|
@@ -72,6 +52,7 @@ class ToolIndent extends Container {
|
|
|
72
52
|
export class NativeDocument extends Container {
|
|
73
53
|
private readonly stamped = new WeakMap<Component, CompactMessage>();
|
|
74
54
|
private readonly toolRows = new WeakMap<Component, CompactTool>();
|
|
55
|
+
private readonly nestedTrees = new WeakMap<Component, NestedTree>();
|
|
75
56
|
private readonly groupRows = new WeakMap<Component, CompactTool>();
|
|
76
57
|
private readonly indented = new WeakMap<Component, ToolIndent[]>();
|
|
77
58
|
private readonly openGroups = new WeakSet<Component>();
|
|
@@ -118,7 +99,7 @@ export class NativeDocument extends Container {
|
|
|
118
99
|
this.frameActive = true;
|
|
119
100
|
this.frameWidth = undefined;
|
|
120
101
|
if (this.compactBlocks.size) {
|
|
121
|
-
const present = new Set((this.
|
|
102
|
+
const present = new Set(this.index.entries(this.pane).map(entry => entry.source));
|
|
122
103
|
for (const [component, block] of this.compactBlocks) if (!present.has(component)) {
|
|
123
104
|
block.dispose();
|
|
124
105
|
this.compactBlocks.delete(component);
|
|
@@ -283,6 +264,29 @@ export class NativeDocument extends Container {
|
|
|
283
264
|
}
|
|
284
265
|
return row;
|
|
285
266
|
}
|
|
267
|
+
private nativeBlock(entry: ContentEntry): CompactMessage {
|
|
268
|
+
const component = entry.source;
|
|
269
|
+
const routed = entry.component;
|
|
270
|
+
this.clockOwners.set(component, entry.owner);
|
|
271
|
+
let stamped = this.stamped.get(routed);
|
|
272
|
+
if (!stamped) {
|
|
273
|
+
stamped = new CompactMessage(routed, () => this.timestamps.label(this.clockOwners.get(component) ?? component),
|
|
274
|
+
this.theme, this.timestampGutter, () => this.showTimestamps() && !(this.pane === "activity" && this.compactTools() && entry.kind === "tool"),
|
|
275
|
+
this.copy ? () => this.copyBlock(component) : undefined, this.showCopy);
|
|
276
|
+
this.stamped.set(routed, stamped);
|
|
277
|
+
}
|
|
278
|
+
this.compactBlocks.set(component, stamped);
|
|
279
|
+
return stamped;
|
|
280
|
+
}
|
|
281
|
+
private searchExcerpt(): Component {
|
|
282
|
+
return this.highlighted(new Text(this.theme().fg("accent", "Search match: ") + searchText(this.excerpt!.text), 1, 0), true);
|
|
283
|
+
}
|
|
284
|
+
private treeBody(entry: ContentEntry): NativeTreeBody {
|
|
285
|
+
const block = this.nativeBlock(entry);
|
|
286
|
+
return { entry, component: this.searchAnchor?.entry.source === entry.source && !this.excerpt ? this.highlighted(block) : block,
|
|
287
|
+
clock: () => block.searchClock(),
|
|
288
|
+
excerpt: this.excerpt?.entry.source === entry.source && entry.owner !== entry.source ? this.searchExcerpt() : undefined };
|
|
289
|
+
}
|
|
286
290
|
override render(width: number): string[] {
|
|
287
291
|
if (this.frameActive && this.frameWidth === width && this.frameLines) return this.frameLines;
|
|
288
292
|
const chat = this.source.children[2] as Container;
|
|
@@ -316,8 +320,28 @@ export class NativeDocument extends Container {
|
|
|
316
320
|
}
|
|
317
321
|
for (const source of opening) this.openGroups.add(source);
|
|
318
322
|
this.groups = groups;
|
|
319
|
-
|
|
320
|
-
|
|
323
|
+
const searched = this.searchAnchor ? this.index.entries(this.pane).find(entry => entry.source === this.searchAnchor!.entry.source) : undefined;
|
|
324
|
+
this.searchTool = searched?.owner;
|
|
325
|
+
const entries = this.index.entries(this.pane);
|
|
326
|
+
const trees = new Map<Component, NestedTree>();
|
|
327
|
+
if (this.pane === "activity") for (const entry of entries) {
|
|
328
|
+
if (!entry.tool) continue;
|
|
329
|
+
const id = Reflect.get(entry.source, "toolCallId") as string | undefined;
|
|
330
|
+
if (!id || (!this.index.nested.entries(id).length && !this.index.nested.isIncomplete(id))) continue;
|
|
331
|
+
let tree = this.nestedTrees.get(entry.source);
|
|
332
|
+
if (!tree) {
|
|
333
|
+
tree = new NestedTree(id, this.index.nested, this.theme, name => this.present(name), this.compactTools, () => {
|
|
334
|
+
this.beforeExpand(); if (this.searchAnchor) this.clearSearch(); this.frameWidth = undefined;
|
|
335
|
+
}, () => {
|
|
336
|
+
const clock = this.showTimestamps() && this.compactTools() ? this.timestamps.label(entry.source) : "";
|
|
337
|
+
return clock ? clock.length + 1 : 0;
|
|
338
|
+
});
|
|
339
|
+
this.nestedTrees.set(entry.source, tree);
|
|
340
|
+
}
|
|
341
|
+
trees.set(entry.source, tree);
|
|
342
|
+
}
|
|
343
|
+
for (const entry of entries) {
|
|
344
|
+
if (entry.owner !== entry.source && trees.has(entry.owner)) continue;
|
|
321
345
|
const component = entry.source;
|
|
322
346
|
const routed = entry.component;
|
|
323
347
|
this.clockOwners.set(component, entry.owner);
|
|
@@ -341,26 +365,25 @@ export class NativeDocument extends Container {
|
|
|
341
365
|
}
|
|
342
366
|
nativeBody = members && this.toolOpen(entry.owner);
|
|
343
367
|
}
|
|
368
|
+
const tree = trees.get(component);
|
|
344
369
|
const contentStart = result.length;
|
|
345
370
|
const bodyWidth = Math.max(0, width - (bodyDepth ? bodyDepth * 2 + 1 : 0));
|
|
346
|
-
if (nativeBody &&
|
|
371
|
+
if (nativeBody && tree) {
|
|
372
|
+
const panels = entries.filter(panel => panel.owner === component && panel.source !== component);
|
|
373
|
+
tree.setBodies(this.treeBody(entry), panels.map(panel => this.treeBody(panel)), this.searchAnchor?.entry.source);
|
|
374
|
+
result.push(tree);
|
|
375
|
+
visibleBlock = true;
|
|
376
|
+
} else if (nativeBody && !entry.compact) {
|
|
347
377
|
toolRun = false;
|
|
348
378
|
result.push(...entry.spacing, routed);
|
|
349
379
|
} else if (nativeBody) {
|
|
350
380
|
if (!compactTool) toolRun = false;
|
|
351
|
-
|
|
352
|
-
if (!stamped) {
|
|
353
|
-
stamped = new CompactMessage(routed, () => this.timestamps.label(this.clockOwners.get(component) ?? component),
|
|
354
|
-
this.theme, this.timestampGutter, () => this.showTimestamps() && !(this.pane === "activity" && this.compactTools() && entry.kind === "tool"),
|
|
355
|
-
this.copy ? () => this.copyBlock(component) : undefined, this.showCopy);
|
|
356
|
-
this.stamped.set(routed, stamped);
|
|
357
|
-
}
|
|
358
|
-
this.compactBlocks.set(component, stamped);
|
|
381
|
+
const stamped = this.nativeBlock(entry);
|
|
359
382
|
visibleBlock = stamped.prepare(bodyWidth, compactTool ? false : visibleBlock) || visibleBlock;
|
|
360
383
|
result.push(stamped);
|
|
361
384
|
clock = stamped.searchClock();
|
|
362
385
|
}
|
|
363
|
-
if (this.searchAnchor?.entry.source === component && !this.excerpt && result.length > start) {
|
|
386
|
+
if (!tree && this.searchAnchor?.entry.source === component && !this.excerpt && result.length > start) {
|
|
364
387
|
const last = result.length - 1;
|
|
365
388
|
result[last] = this.highlighted(result[last]);
|
|
366
389
|
}
|
|
@@ -369,20 +392,25 @@ export class NativeDocument extends Container {
|
|
|
369
392
|
const lines = [...prefix, ...result.slice(contentStart).flatMap(child => child.render(bodyWidth))];
|
|
370
393
|
let excerptRow: number | undefined;
|
|
371
394
|
if (this.excerpt?.entry.source === component) {
|
|
372
|
-
const fragment = new Text(this.theme().fg("accent", "Search match: ") + searchText(this.excerpt.text), 1, 0);
|
|
373
395
|
if (!lines.length && wasVisible) {
|
|
374
396
|
result.push(new Spacer(1));
|
|
375
397
|
lines.push("");
|
|
376
398
|
}
|
|
377
399
|
excerptRow = row + lines.length;
|
|
378
|
-
const decorated = this.
|
|
400
|
+
const decorated = this.searchExcerpt();
|
|
379
401
|
result.push(decorated);
|
|
380
402
|
lines.push(...decorated.render(bodyWidth));
|
|
381
403
|
visibleBlock = true;
|
|
382
404
|
}
|
|
383
405
|
// Compact controls/clocks are not saved text. Search the original body only.
|
|
384
406
|
const prefixRows = prefix.length;
|
|
385
|
-
|
|
407
|
+
if (tree && nativeBody) {
|
|
408
|
+
for (const [source, position] of tree.positions) this.positions.set(source, {
|
|
409
|
+
...position, row: row + prefixRows + position.row,
|
|
410
|
+
excerptRow: position.excerptRow === undefined ? undefined : row + prefixRows + position.excerptRow,
|
|
411
|
+
});
|
|
412
|
+
if (excerptRow !== undefined) this.positions.set(component, { row, lines: [], excerptRow });
|
|
413
|
+
} else this.positions.set(component, { row: row + prefixRows, lines: lines.slice(prefixRows), clock, excerptRow });
|
|
386
414
|
row += lines.length;
|
|
387
415
|
}
|
|
388
416
|
for (let child = contentStart; child < result.length; child++) result[child] = this.indent(result[child], bodyDepth, true);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ToolSummary } from "./content.js";
|
|
2
|
+
|
|
3
|
+
type Start = { parentToolCallId?: string; toolCallId: string; toolName: string; args: unknown };
|
|
4
|
+
type End = { toolCallId: string; isError: boolean };
|
|
5
|
+
type SavedCall = { id: string; name: string; status: string; arguments?: unknown; error?: string };
|
|
6
|
+
type SavedMessage = { role: string; toolCallId?: string; nestedCalls?: { calls: SavedCall[]; complete: boolean } };
|
|
7
|
+
|
|
8
|
+
export interface NestedCall extends ToolSummary { id: string; parent: string; root: string; error?: string }
|
|
9
|
+
|
|
10
|
+
/** Execution identities only. Native tools and extensions retain ownership of all result UI. */
|
|
11
|
+
export class NestedCalls {
|
|
12
|
+
private readonly calls = new Map<string, NestedCall>();
|
|
13
|
+
private readonly roots = new Map<string, NestedCall[]>();
|
|
14
|
+
private readonly incomplete = new Set<string>();
|
|
15
|
+
revision = 0;
|
|
16
|
+
|
|
17
|
+
entries(root: string): readonly NestedCall[] { return this.roots.get(root) ?? []; }
|
|
18
|
+
private put(call: NestedCall): void {
|
|
19
|
+
const existing = this.calls.get(call.id);
|
|
20
|
+
if (existing) { Object.assign(existing, call); return; }
|
|
21
|
+
this.calls.set(call.id, call);
|
|
22
|
+
const list = this.roots.get(call.root) ?? [];
|
|
23
|
+
list.push(call); this.roots.set(call.root, list);
|
|
24
|
+
}
|
|
25
|
+
isIncomplete(root: string): boolean { return this.incomplete.has(root); }
|
|
26
|
+
search(root: string): string {
|
|
27
|
+
return this.entries(root).map(call => [call.name, JSON.stringify(call.args, null, 2), call.error].filter(Boolean).join("\n")).join("\n");
|
|
28
|
+
}
|
|
29
|
+
start(event: Start): void {
|
|
30
|
+
if (!event.parentToolCallId) return;
|
|
31
|
+
const parent = this.calls.get(event.parentToolCallId);
|
|
32
|
+
this.put({ id: event.toolCallId, parent: event.parentToolCallId, root: parent?.root ?? event.parentToolCallId,
|
|
33
|
+
name: event.toolName, args: event.args, status: "running" });
|
|
34
|
+
this.revision++;
|
|
35
|
+
}
|
|
36
|
+
end(event: End): void {
|
|
37
|
+
const call = this.calls.get(event.toolCallId);
|
|
38
|
+
if (!call) return;
|
|
39
|
+
call.status = event.isError ? "error" : "success";
|
|
40
|
+
this.revision++;
|
|
41
|
+
}
|
|
42
|
+
absorb(message: SavedMessage): void {
|
|
43
|
+
if (message.role !== "toolResult" || !message.toolCallId || !Array.isArray(message.nestedCalls?.calls)) return;
|
|
44
|
+
const root = message.toolCallId;
|
|
45
|
+
const ids = new Set(message.nestedCalls.calls.map(call => call.id));
|
|
46
|
+
for (const record of message.nestedCalls.calls) {
|
|
47
|
+
const existing = this.calls.get(record.id);
|
|
48
|
+
// Pi records nested identities as <caller-id>/<ordinal>, including deeper calls.
|
|
49
|
+
const parent = record.id.slice(0, record.id.lastIndexOf("/"));
|
|
50
|
+
this.put({ id: record.id, root, parent: existing?.parent ?? (ids.has(parent) ? parent : root),
|
|
51
|
+
name: record.name, args: record.arguments, error: record.error,
|
|
52
|
+
status: record.status === "ok" ? "success" : record.status === "error" ? "error" : "unknown" });
|
|
53
|
+
}
|
|
54
|
+
if (message.nestedCalls.complete) this.incomplete.delete(root); else this.incomplete.add(root);
|
|
55
|
+
this.revision++;
|
|
56
|
+
}
|
|
57
|
+
restore(branch: readonly { type: string; message?: unknown }[]): void {
|
|
58
|
+
this.calls.clear(); this.roots.clear(); this.incomplete.clear(); this.revision++;
|
|
59
|
+
for (const entry of branch) if (entry.type === "message" && entry.message && typeof entry.message === "object") this.absorb(entry.message as SavedMessage);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Container, Text, type Component } from "@earendil-works/pi-tui";
|
|
3
|
+
import { componentTree, type ContentEntry, type ToolSummary } from "./content.js";
|
|
4
|
+
import { CompactTool } from "./compact-tools.js";
|
|
5
|
+
import type { SearchClock } from "./search-highlight.js";
|
|
6
|
+
import type { NestedCall, NestedCalls } from "./nested-calls.js";
|
|
7
|
+
import type { ToolPresentation } from "./tool-presentation.js";
|
|
8
|
+
import { ToolIndent } from "./tool-indent.js";
|
|
9
|
+
|
|
10
|
+
export interface NativeTreeBody { entry: ContentEntry; component: Component; excerpt?: Component; clock?(): SearchClock | undefined }
|
|
11
|
+
interface Node { key: string; summary: ToolSummary; children: Node[]; count?: number }
|
|
12
|
+
|
|
13
|
+
/** Structural disclosure only: every result body is the original native component. */
|
|
14
|
+
export class NestedTree extends Container {
|
|
15
|
+
readonly positions = new Map<Component, { row: number; lines: string[]; clock?: SearchClock; excerptRow?: number }>();
|
|
16
|
+
private parent?: NativeTreeBody;
|
|
17
|
+
private panels: NativeTreeBody[] = [];
|
|
18
|
+
private reveal?: Component;
|
|
19
|
+
private readonly open = new Map<string, boolean>();
|
|
20
|
+
private readonly revealed = new Set<string>();
|
|
21
|
+
private readonly rows = new Map<string, CompactTool>();
|
|
22
|
+
private readonly indents = new WeakMap<Component, Map<string, ToolIndent>>();
|
|
23
|
+
private buckets = new Map<string, string>();
|
|
24
|
+
private nodes = new Map<string, Node>();
|
|
25
|
+
private mode?: boolean;
|
|
26
|
+
|
|
27
|
+
constructor(readonly rootId: string, private readonly calls: NestedCalls, private readonly theme: () => Theme,
|
|
28
|
+
private readonly present: (name: string) => ToolPresentation, private readonly compact: () => boolean, private readonly changed: () => void, private readonly gutter: () => number = () => 0) { super(); }
|
|
29
|
+
|
|
30
|
+
setBodies(parent: NativeTreeBody, panels: NativeTreeBody[], reveal?: Component): void {
|
|
31
|
+
this.parent = parent; this.panels = panels; this.reveal = reveal;
|
|
32
|
+
}
|
|
33
|
+
private isOpen(key: string): boolean { return this.revealed.has(key) || (this.open.get(key) ?? !this.compact()); }
|
|
34
|
+
private indent(component: Component, depth: number, body = false): Component {
|
|
35
|
+
const gutter = this.gutter();
|
|
36
|
+
if (!depth && !gutter) return component;
|
|
37
|
+
let levels = this.indents.get(component);
|
|
38
|
+
if (!levels) { levels = new Map(); this.indents.set(component, levels); }
|
|
39
|
+
const key = `${gutter}:${depth}:${body}`;
|
|
40
|
+
let view = levels.get(key);
|
|
41
|
+
if (!view) { view = new ToolIndent(component, depth, body, this.theme, gutter); levels.set(key, view); }
|
|
42
|
+
return view;
|
|
43
|
+
}
|
|
44
|
+
private header(node: Node, expandable: boolean): CompactTool {
|
|
45
|
+
// Include expandability in the key: late native output can turn a leaf into a disclosure.
|
|
46
|
+
const key = node.key + (expandable ? ":open" : ":leaf");
|
|
47
|
+
let row = this.rows.get(key);
|
|
48
|
+
if (!row) {
|
|
49
|
+
row = new CompactTool(() => this.nodes.get(node.key)!.summary, this.theme, {
|
|
50
|
+
alignControl: true,
|
|
51
|
+
count: node.count ? () => this.nodes.get(node.key)!.count! : undefined,
|
|
52
|
+
expanded: () => this.isOpen(node.key),
|
|
53
|
+
toggle: expandable ? () => { this.open.set(node.key, !this.isOpen(node.key)); this.changed(); } : undefined,
|
|
54
|
+
});
|
|
55
|
+
this.rows.set(key, row);
|
|
56
|
+
}
|
|
57
|
+
return row;
|
|
58
|
+
}
|
|
59
|
+
override render(width: number): string[] {
|
|
60
|
+
if (!this.parent) return [];
|
|
61
|
+
if (this.mode !== this.compact()) { this.open.clear(); this.mode = this.compact(); }
|
|
62
|
+
this.revealed.clear();
|
|
63
|
+
const records = this.calls.entries(this.rootId);
|
|
64
|
+
const siblings = new Map<string, NestedCall[]>();
|
|
65
|
+
for (const call of records) {
|
|
66
|
+
const list = siblings.get(call.parent) ?? []; list.push(call); siblings.set(call.parent, list);
|
|
67
|
+
}
|
|
68
|
+
this.nodes = new Map();
|
|
69
|
+
const buckets = new Map<string, string>();
|
|
70
|
+
const paths = new Map<string, string[]>();
|
|
71
|
+
const make = (parent: string, ancestors: string[], visiting = new Set<string>()): Node[] => {
|
|
72
|
+
if (visiting.has(parent)) return [];
|
|
73
|
+
const seen = new Set(visiting); seen.add(parent);
|
|
74
|
+
const runs: NestedCall[][] = [];
|
|
75
|
+
for (const call of siblings.get(parent) ?? []) {
|
|
76
|
+
const last = runs.at(-1);
|
|
77
|
+
if (last && this.present(last[0].name).key === this.present(call.name).key) last.push(call); else runs.push([call]);
|
|
78
|
+
}
|
|
79
|
+
return runs.map(run => {
|
|
80
|
+
const grouped = run.length > 1;
|
|
81
|
+
const key = grouped ? "group:" + run[0].id : run[0].id;
|
|
82
|
+
const path = [...ancestors, key]; paths.set(key, path);
|
|
83
|
+
const children = run.map(call => {
|
|
84
|
+
buckets.set(call.id, key);
|
|
85
|
+
const callPath = grouped ? [...path, call.id] : path; paths.set(call.id, callPath);
|
|
86
|
+
const node: Node = { key: call.id, summary: { ...call, name: this.present(call.name).name }, children: make(call.id, callPath, seen) };
|
|
87
|
+
this.nodes.set(node.key, node);
|
|
88
|
+
return node;
|
|
89
|
+
});
|
|
90
|
+
if (!grouped) return children[0];
|
|
91
|
+
const status = run.some(call => call.status === "error") ? "error" : run.some(call => call.status === "running") ? "running" :
|
|
92
|
+
run.some(call => call.status === "unknown") ? "unknown" : "success";
|
|
93
|
+
const group: Node = { key, count: run.length, summary: { name: this.present(run[0].name).name, args: undefined, status }, children };
|
|
94
|
+
this.nodes.set(key, group);
|
|
95
|
+
return group;
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const roots = make(this.rootId, []);
|
|
99
|
+
// A late sibling can turn an open singleton into a group. Carry only the
|
|
100
|
+
// immediately visible state; an explicitly closed group must remain closed.
|
|
101
|
+
for (const [id, key] of buckets) {
|
|
102
|
+
const previous = this.buckets.get(id);
|
|
103
|
+
if (previous && previous !== key && !this.open.has(key) && this.isOpen(previous)) this.open.set(key, true);
|
|
104
|
+
}
|
|
105
|
+
this.buckets = buckets;
|
|
106
|
+
const attached = new Map<string, NativeTreeBody[]>();
|
|
107
|
+
const identified = this.panels.map(panel => ({ panel, ids: [...new Set(componentTree(panel.entry.source)
|
|
108
|
+
.filter(node => node.constructor.name === "ToolExecutionComponent" && typeof node.toolCallId === "string").map(node => node.toolCallId!))] }));
|
|
109
|
+
const independent = new Map<string, Set<string>>();
|
|
110
|
+
for (const { ids } of identified) {
|
|
111
|
+
const bucket = ids.length === 1 ? buckets.get(ids[0]) : undefined;
|
|
112
|
+
if (!bucket) continue;
|
|
113
|
+
const group = independent.get(bucket) ?? new Set<string>();
|
|
114
|
+
group.add(ids[0]); independent.set(bucket, group);
|
|
115
|
+
}
|
|
116
|
+
for (const { panel, ids } of identified) {
|
|
117
|
+
const locations = ids.map(id => buckets.get(id));
|
|
118
|
+
// Independent native results belong to their calls. One shared result for
|
|
119
|
+
// several siblings stays whole at the group; opaque bodies stay at the caller.
|
|
120
|
+
const bucket = locations.length && locations[0] && locations.every(location => location === locations[0]) ? locations[0] : "";
|
|
121
|
+
const key = ids.length === 1 && (independent.get(bucket)?.size ?? 0) > 1 ? ids[0] : bucket;
|
|
122
|
+
const list = attached.get(key) ?? []; list.push(panel); attached.set(key, list);
|
|
123
|
+
if (this.reveal === panel.entry.source) for (const ancestor of paths.get(key) ?? ["output"]) this.revealed.add(ancestor);
|
|
124
|
+
}
|
|
125
|
+
const output: Node = { key: "output", summary: { ...this.parent.entry.tool!(), name: this.present(this.parent.entry.tool!().name).name + " output", args: undefined }, children: [] };
|
|
126
|
+
this.nodes.set(output.key, output);
|
|
127
|
+
if (this.reveal === this.parent.entry.source) this.revealed.add(output.key);
|
|
128
|
+
const result: Component[] = [];
|
|
129
|
+
this.positions.clear();
|
|
130
|
+
let row = 0;
|
|
131
|
+
const append = (component: Component, depth: number, body = false): string[] => {
|
|
132
|
+
const view = this.indent(component, depth, body);
|
|
133
|
+
result.push(view);
|
|
134
|
+
const lines = view.render(width); row += lines.length; return lines;
|
|
135
|
+
};
|
|
136
|
+
const appendBody = (body: NativeTreeBody, depth: number): void => {
|
|
137
|
+
const start = row;
|
|
138
|
+
const lines = append(body.component, depth, depth > 0);
|
|
139
|
+
const excerptRow = body.excerpt ? row : undefined;
|
|
140
|
+
if (body.excerpt) append(body.excerpt, depth, depth > 0);
|
|
141
|
+
this.positions.set(body.entry.source, { row: start, lines, clock: body.clock?.(), excerptRow });
|
|
142
|
+
};
|
|
143
|
+
const paint = (node: Node, depth: number): void => {
|
|
144
|
+
const panels = attached.get(node.key) ?? [];
|
|
145
|
+
const expandable = node.children.length > 0 || panels.length > 0;
|
|
146
|
+
append(this.header(node, expandable), depth);
|
|
147
|
+
if (!expandable || !this.isOpen(node.key)) return;
|
|
148
|
+
for (const child of node.children) paint(child, depth + 1);
|
|
149
|
+
for (const panel of panels) appendBody(panel, depth + 1);
|
|
150
|
+
};
|
|
151
|
+
for (const root of roots) paint(root, 0);
|
|
152
|
+
if (this.calls.isIncomplete(this.rootId)) append(new Text(this.theme().fg("dim", "Nested call record incomplete"), 0, 0), 0);
|
|
153
|
+
append(this.header(output, true), 0);
|
|
154
|
+
if (this.isOpen(output.key)) {
|
|
155
|
+
for (const panel of attached.get("") ?? []) appendBody(panel, 1);
|
|
156
|
+
appendBody(this.parent, 1);
|
|
157
|
+
}
|
|
158
|
+
this.children = result;
|
|
159
|
+
return super.render(width);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Container, truncateToWidth, type Component, type TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/** Keep hierarchy guides outside native rendering and pointer coordinates. */
|
|
5
|
+
export class ToolIndent extends Container {
|
|
6
|
+
private readonly prefix: string;
|
|
7
|
+
constructor(component: Component, depth: number, body: boolean, private readonly theme: () => Theme, gutter = 0) {
|
|
8
|
+
super(); this.addChild(component);
|
|
9
|
+
this.prefix = " ".repeat(gutter) + " │".repeat(depth) + (body ? " " : "");
|
|
10
|
+
}
|
|
11
|
+
override render(width: number): string[] {
|
|
12
|
+
const inset = Math.min(width, this.prefix.length);
|
|
13
|
+
const prefix = this.theme().fg("dim", this.prefix.slice(0, inset));
|
|
14
|
+
return super.render(Math.max(0, width - inset)).map(line => truncateToWidth(prefix + line, width, ""));
|
|
15
|
+
}
|
|
16
|
+
override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
|
|
17
|
+
const inset = Math.min(event.width, this.prefix.length);
|
|
18
|
+
if (event.x < inset) return undefined;
|
|
19
|
+
return super.handleMouse({ ...event, x: event.x - inset, width: Math.max(0, event.width - inset) });
|
|
20
|
+
}
|
|
21
|
+
}
|