proper-base 0.4.0 → 0.5.1
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 +31 -5
- package/index.ts +92 -10
- package/package.json +4 -3
- package/src/autocomplete-details.ts +154 -21
- package/src/editor-mouse.ts +69 -0
- package/src/editor-navigation.ts +52 -1
- package/src/image-preview.ts +21 -4
- package/src/prompt-jump.ts +308 -54
- package/src/settings.ts +239 -0
- package/src/startup-defaults.ts +143 -0
- package/src/transcript-cleanup.ts +109 -9
package/src/settings.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { Component, TUI } from "@earendil-works/pi-tui";
|
|
5
|
+
|
|
6
|
+
// Keyed by its original name: a /reload of a build that still runs the
|
|
7
|
+
// rail-only module must find and hand over the wrapper it installed.
|
|
8
|
+
const INSTALLED = Symbol.for("pi-proper-base.rail-setting");
|
|
9
|
+
|
|
10
|
+
/** Each toggle proper-base splices into Pi's `/settings` menu. */
|
|
11
|
+
const TOGGLES = [
|
|
12
|
+
{
|
|
13
|
+
id: "proper-base-session-rail",
|
|
14
|
+
key: "sessionRail",
|
|
15
|
+
label: "Session action rail",
|
|
16
|
+
description:
|
|
17
|
+
"proper-base: colored per-action jump symbols along the transcript's right edge",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "proper-base-editor-mouse",
|
|
21
|
+
key: "editorMouse",
|
|
22
|
+
label: "Prompt mouse clicks",
|
|
23
|
+
description:
|
|
24
|
+
"proper-base: clicking the prompt moves the cursor; off leaves the cursor where typing put it",
|
|
25
|
+
},
|
|
26
|
+
] as const;
|
|
27
|
+
type ToggleKey = (typeof TOGGLES)[number]["key"];
|
|
28
|
+
|
|
29
|
+
type SettingItem = {
|
|
30
|
+
id: string;
|
|
31
|
+
label: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
currentValue: string;
|
|
34
|
+
values?: string[];
|
|
35
|
+
};
|
|
36
|
+
/** Pi's settings selector, by shape: a component holding a SettingsList. */
|
|
37
|
+
type SettingsHost = Component & {
|
|
38
|
+
settingsList?: {
|
|
39
|
+
items?: SettingItem[];
|
|
40
|
+
onChange?: (id: string, value: string) => void;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
type ComponentContainer = Component & {
|
|
44
|
+
children: Component[];
|
|
45
|
+
addChild(component: Component): void;
|
|
46
|
+
};
|
|
47
|
+
type InstalledContainer = ComponentContainer & { [INSTALLED]?: () => void };
|
|
48
|
+
|
|
49
|
+
export type SettingsController = {
|
|
50
|
+
/** Whether the session action rail is enabled. Cached per session. */
|
|
51
|
+
enabled(): boolean;
|
|
52
|
+
/** Whether prompt clicks may move the editor cursor. Cached per session. */
|
|
53
|
+
editorMouse(): boolean;
|
|
54
|
+
dispose(): void;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function configPath(agentDir: string): string {
|
|
58
|
+
return join(agentDir, "proper-base.json");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readToggle(agentDir: string, key: ToggleKey): boolean {
|
|
62
|
+
try {
|
|
63
|
+
const parsed = JSON.parse(
|
|
64
|
+
readFileSync(configPath(agentDir), "utf8"),
|
|
65
|
+
) as unknown;
|
|
66
|
+
return (parsed as Record<string, unknown> | null)?.[key] !== false;
|
|
67
|
+
} catch {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function readRailEnabled(agentDir: string): boolean {
|
|
73
|
+
return readToggle(agentDir, "sessionRail");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function readEditorMouseEnabled(agentDir: string): boolean {
|
|
77
|
+
return readToggle(agentDir, "editorMouse");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Fail-open like the history store: a read-only agent dir only costs
|
|
81
|
+
* persistence, never the session. Other keys in the file are preserved. */
|
|
82
|
+
function writeToggle(agentDir: string, key: ToggleKey, enabled: boolean): void {
|
|
83
|
+
try {
|
|
84
|
+
let parsed: Record<string, unknown> = {};
|
|
85
|
+
try {
|
|
86
|
+
const existing = JSON.parse(
|
|
87
|
+
readFileSync(configPath(agentDir), "utf8"),
|
|
88
|
+
) as unknown;
|
|
89
|
+
if (existing && typeof existing === "object") {
|
|
90
|
+
parsed = existing as Record<string, unknown>;
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
// Damaged or absent config starts fresh.
|
|
94
|
+
}
|
|
95
|
+
parsed[key] = enabled;
|
|
96
|
+
writeFileSync(
|
|
97
|
+
configPath(agentDir),
|
|
98
|
+
`${JSON.stringify(parsed, null, "\t")}\n`,
|
|
99
|
+
);
|
|
100
|
+
} catch {
|
|
101
|
+
// Persistence is best-effort.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function componentName(component: Component): string | undefined {
|
|
106
|
+
return (component as { constructor?: { name?: string } }).constructor?.name;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function childrenOf(component: Component): Component[] {
|
|
110
|
+
return (component as Partial<ComponentContainer>).children ?? [];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function findEditorContainer(
|
|
114
|
+
tui: TUI,
|
|
115
|
+
editor: Component,
|
|
116
|
+
): ComponentContainer | undefined {
|
|
117
|
+
const roots = (tui as TUI & { layoutRoot?: Component }).layoutRoot
|
|
118
|
+
? [(tui as TUI & { layoutRoot?: Component }).layoutRoot as Component]
|
|
119
|
+
: tui.children;
|
|
120
|
+
const visit = (component: Component): ComponentContainer | undefined => {
|
|
121
|
+
const children = childrenOf(component);
|
|
122
|
+
if (
|
|
123
|
+
children.includes(editor) &&
|
|
124
|
+
typeof (component as Partial<ComponentContainer>).addChild === "function"
|
|
125
|
+
) {
|
|
126
|
+
return component as ComponentContainer;
|
|
127
|
+
}
|
|
128
|
+
for (const child of children) {
|
|
129
|
+
const found = visit(child);
|
|
130
|
+
if (found) return found;
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
};
|
|
134
|
+
for (const root of roots) {
|
|
135
|
+
const found = visit(root);
|
|
136
|
+
if (found) return found;
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Add proper-base's toggles to Pi's native `/settings` menu: the session
|
|
143
|
+
* action rail and prompt mouse clicks.
|
|
144
|
+
*
|
|
145
|
+
* Pi has no extension hook into that menu, so this is a guarded
|
|
146
|
+
* compatibility layer in the house style: the container Pi mounts selectors
|
|
147
|
+
* into is the editor's own parent, its `addChild` is wrapped once, and every
|
|
148
|
+
* settings selector that mounts receives the extra items spliced into its
|
|
149
|
+
* `SettingsList`, whose instance-held `onChange` is wrapped to keep the
|
|
150
|
+
* toggles out of Pi's own switch. A renamed component or list shape installs
|
|
151
|
+
* or injects nothing and both features simply stay enabled.
|
|
152
|
+
*
|
|
153
|
+
* Choices persist as `sessionRail` and `editorMouse` in the agent
|
|
154
|
+
* directory's `proper-base.json`; other sessions pick a change up at their
|
|
155
|
+
* next start.
|
|
156
|
+
*
|
|
157
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Session action rail]]
|
|
158
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt mouse clicks]]
|
|
159
|
+
*/
|
|
160
|
+
export function installSettings(
|
|
161
|
+
tui: TUI,
|
|
162
|
+
editor: Component,
|
|
163
|
+
agentDir: string,
|
|
164
|
+
): SettingsController {
|
|
165
|
+
const state: Record<ToggleKey, boolean> = {
|
|
166
|
+
sessionRail: readToggle(agentDir, "sessionRail"),
|
|
167
|
+
editorMouse: readToggle(agentDir, "editorMouse"),
|
|
168
|
+
};
|
|
169
|
+
let disposed = false;
|
|
170
|
+
let uninstall: (() => void) | undefined;
|
|
171
|
+
|
|
172
|
+
const inject = (selector: SettingsHost): void => {
|
|
173
|
+
const list = selector.settingsList;
|
|
174
|
+
const items = list?.items;
|
|
175
|
+
if (!list || !Array.isArray(items) || typeof list.onChange !== "function")
|
|
176
|
+
return;
|
|
177
|
+
if (items.some((item) => item?.id === TOGGLES[0].id)) return;
|
|
178
|
+
for (const toggle of TOGGLES) {
|
|
179
|
+
items.push({
|
|
180
|
+
id: toggle.id,
|
|
181
|
+
label: toggle.label,
|
|
182
|
+
description: toggle.description,
|
|
183
|
+
currentValue: state[toggle.key] ? "true" : "false",
|
|
184
|
+
values: ["true", "false"],
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const native = list.onChange;
|
|
188
|
+
list.onChange = (id, value) => {
|
|
189
|
+
const toggle = TOGGLES.find((entry) => entry.id === id);
|
|
190
|
+
if (toggle) {
|
|
191
|
+
state[toggle.key] = value === "true";
|
|
192
|
+
writeToggle(agentDir, toggle.key, state[toggle.key]);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
native(id, value);
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const wrap = () => {
|
|
200
|
+
if (disposed) return;
|
|
201
|
+
const container = findEditorContainer(tui, editor) as
|
|
202
|
+
| InstalledContainer
|
|
203
|
+
| undefined;
|
|
204
|
+
if (!container) return;
|
|
205
|
+
// A reload runs the new factory before the outgoing instance shuts
|
|
206
|
+
// down; take over any wrapper it left.
|
|
207
|
+
container[INSTALLED]?.();
|
|
208
|
+
const native = container.addChild.bind(container);
|
|
209
|
+
const wrapped = (component: Component) => {
|
|
210
|
+
native(component);
|
|
211
|
+
if (componentName(component) === "SettingsSelectorComponent") {
|
|
212
|
+
inject(component as SettingsHost);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
container.addChild = wrapped;
|
|
216
|
+
const restore = () => {
|
|
217
|
+
if (container.addChild === wrapped) {
|
|
218
|
+
container.addChild = native;
|
|
219
|
+
}
|
|
220
|
+
delete container[INSTALLED];
|
|
221
|
+
};
|
|
222
|
+
container[INSTALLED] = restore;
|
|
223
|
+
uninstall = restore;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
// The editor mounts into its container right after the factory returns,
|
|
227
|
+
// so the parent walk has to wait a tick.
|
|
228
|
+
queueMicrotask(wrap);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
enabled: () => state.sessionRail,
|
|
232
|
+
editorMouse: () => state.editorMouse,
|
|
233
|
+
dispose() {
|
|
234
|
+
disposed = true;
|
|
235
|
+
uninstall?.();
|
|
236
|
+
uninstall = undefined;
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
const SETTINGS_FILE_NAME = "settings.json";
|
|
5
|
+
const CONFIG_FILE_NAME = "proper-base.json";
|
|
6
|
+
/**
|
|
7
|
+
* proper-llm-router's routing placeholder. Its provider is registered by an
|
|
8
|
+
* extension and carries no credentials, so Pi's startup resolver skips it and
|
|
9
|
+
* silently falls back; the router re-selects it at every session start anyway.
|
|
10
|
+
*/
|
|
11
|
+
const PLACEHOLDER_PROVIDER = "llm-router";
|
|
12
|
+
|
|
13
|
+
/** The model identity Pi's startup defaults are keyed by. */
|
|
14
|
+
export type DefaultModel = { provider: string; id: string };
|
|
15
|
+
|
|
16
|
+
type JsonObject = Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
function readJsonObject(path: string): JsonObject | undefined {
|
|
19
|
+
try {
|
|
20
|
+
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
21
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
22
|
+
? (parsed as JsonObject)
|
|
23
|
+
: undefined;
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Whether the session's model and effort follow into Pi's startup defaults. */
|
|
30
|
+
export function stickyDefaultsEnabled(agentDir: string): boolean {
|
|
31
|
+
return (
|
|
32
|
+
readJsonObject(join(agentDir, CONFIG_FILE_NAME))?.stickyDefaults !== false
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function settingsPath(agentDir: string): string {
|
|
37
|
+
return join(agentDir, SETTINGS_FILE_NAME);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Pi's settings, or nothing when persistence is off or the file is absent or
|
|
42
|
+
* damaged. A damaged file is never replaced with a defaults-only one.
|
|
43
|
+
*/
|
|
44
|
+
function openSettings(agentDir: string): JsonObject | undefined {
|
|
45
|
+
if (!stickyDefaultsEnabled(agentDir)) return undefined;
|
|
46
|
+
return readJsonObject(settingsPath(agentDir));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Replace Pi's settings file, preserving every other key and its two-space
|
|
51
|
+
* format. Writing from outside is safe against Pi's own saves: its settings
|
|
52
|
+
* writer re-reads the file under a lock and merges back only the fields it
|
|
53
|
+
* marked modified, so keys written here survive.
|
|
54
|
+
*
|
|
55
|
+
* ponytail: read-modify-write without Pi's lock. A Pi settings write landing
|
|
56
|
+
* inside one of these read/write pairs would be overwritten; take the lock if
|
|
57
|
+
* extensions ever get access to it.
|
|
58
|
+
*/
|
|
59
|
+
function writeSettings(agentDir: string, settings: JsonObject): boolean {
|
|
60
|
+
try {
|
|
61
|
+
writeFileSync(
|
|
62
|
+
settingsPath(agentDir),
|
|
63
|
+
`${JSON.stringify(settings, null, 2)}\n`,
|
|
64
|
+
"utf8",
|
|
65
|
+
);
|
|
66
|
+
return true;
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function perModelLevel(
|
|
73
|
+
settings: JsonObject,
|
|
74
|
+
model: DefaultModel,
|
|
75
|
+
): unknown | undefined {
|
|
76
|
+
const levels = settings.modelThinkingLevels;
|
|
77
|
+
if (!levels || typeof levels !== "object") return undefined;
|
|
78
|
+
return (levels as JsonObject)[`${model.provider}/${model.id}`];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Record the active model as Pi's startup default.
|
|
83
|
+
*
|
|
84
|
+
* Pi writes `defaultProvider`/`defaultModel` only when Ctrl+S is pressed in the
|
|
85
|
+
* `/model` picker; a plain selection lives and dies with the session, so a new
|
|
86
|
+
* session silently reopens on whichever model was saved last. Extensions get no
|
|
87
|
+
* access to Pi's settings manager, so the two keys are rewritten directly.
|
|
88
|
+
*
|
|
89
|
+
* An unchanged selection writes nothing, so restores and repeated cycling
|
|
90
|
+
* through the same model cause no churn. The routing placeholder is never
|
|
91
|
+
* stored, because Pi cannot start on it.
|
|
92
|
+
*/
|
|
93
|
+
export function persistDefaultModel(
|
|
94
|
+
agentDir: string,
|
|
95
|
+
model: DefaultModel,
|
|
96
|
+
): boolean {
|
|
97
|
+
if (model.provider === PLACEHOLDER_PROVIDER) return false;
|
|
98
|
+
const settings = openSettings(agentDir);
|
|
99
|
+
if (!settings) return false;
|
|
100
|
+
if (
|
|
101
|
+
settings.defaultProvider === model.provider &&
|
|
102
|
+
settings.defaultModel === model.id
|
|
103
|
+
) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return writeSettings(agentDir, {
|
|
107
|
+
...settings,
|
|
108
|
+
defaultProvider: model.provider,
|
|
109
|
+
defaultModel: model.id,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Record the active thinking level as Pi's startup default.
|
|
115
|
+
*
|
|
116
|
+
* Persisting the level is what makes it hold, not merely what makes it
|
|
117
|
+
* reappear. Pi recomputes the level on every model switch from
|
|
118
|
+
* `modelThinkingLevels`, then `defaultThinkingLevel`, and only then the level
|
|
119
|
+
* in effect, so a session-only choice does not survive the next `/model`,
|
|
120
|
+
* `Ctrl+P`, `/clear`, or routed prompt: the saved default overwrites it mid
|
|
121
|
+
* session. Writing the same key the resolver reads keeps the current choice
|
|
122
|
+
* both durable and stable across switches.
|
|
123
|
+
*
|
|
124
|
+
* The event carries the level after Pi clamps it to the model, so moving to a
|
|
125
|
+
* model that cannot reach the current level records the level actually in
|
|
126
|
+
* effect. A level matching a `modelThinkingLevels` entry for the current model
|
|
127
|
+
* is not stored: that rule is per model, and promoting it to the global default
|
|
128
|
+
* would hand it to every model without one.
|
|
129
|
+
*/
|
|
130
|
+
export function persistDefaultThinkingLevel(
|
|
131
|
+
agentDir: string,
|
|
132
|
+
level: string,
|
|
133
|
+
model: DefaultModel | undefined,
|
|
134
|
+
): boolean {
|
|
135
|
+
const settings = openSettings(agentDir);
|
|
136
|
+
if (!settings) return false;
|
|
137
|
+
if (settings.defaultThinkingLevel === level) return false;
|
|
138
|
+
if (model && perModelLevel(settings, model) === level) return false;
|
|
139
|
+
return writeSettings(agentDir, {
|
|
140
|
+
...settings,
|
|
141
|
+
defaultThinkingLevel: level,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
@@ -41,6 +41,7 @@ type State = {
|
|
|
41
41
|
expanded: Map<object, boolean>;
|
|
42
42
|
hits: Map<number, DetailHit>;
|
|
43
43
|
pendingHits: DetailHit[];
|
|
44
|
+
outline: OutlineEntry[];
|
|
44
45
|
globalExpanded: boolean;
|
|
45
46
|
assistantCache: WeakMap<AssistantComponent, AssistantRenderCache>;
|
|
46
47
|
};
|
|
@@ -50,6 +51,17 @@ type AssistantRenderCache = {
|
|
|
50
51
|
lines: Map<string, string[]>;
|
|
51
52
|
};
|
|
52
53
|
type DetailKind = "tool" | "error";
|
|
54
|
+
/**
|
|
55
|
+
* One transcript action, in scroll-content coordinates.
|
|
56
|
+
*
|
|
57
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Session action rail]]
|
|
58
|
+
*/
|
|
59
|
+
export type OutlineEntry = {
|
|
60
|
+
row: number;
|
|
61
|
+
kind: "user" | "assistant" | "tool" | "error";
|
|
62
|
+
/** Short action-type name: `prompt`, `reply`, or the tool's name. */
|
|
63
|
+
label: string;
|
|
64
|
+
};
|
|
53
65
|
type Detail = {
|
|
54
66
|
owner: object;
|
|
55
67
|
kind: DetailKind;
|
|
@@ -84,6 +96,7 @@ export type TranscriptCleanupController = {
|
|
|
84
96
|
completeAssistant(message: object): void;
|
|
85
97
|
completeTool(toolCallId: string): void;
|
|
86
98
|
settle(): void;
|
|
99
|
+
outline(): OutlineEntry[];
|
|
87
100
|
update(ctx: ExtensionContext, tui: TUI): void;
|
|
88
101
|
uninstall(): void;
|
|
89
102
|
};
|
|
@@ -109,6 +122,7 @@ export function installTranscriptCleanup(
|
|
|
109
122
|
expanded: new Map(),
|
|
110
123
|
hits: new Map(),
|
|
111
124
|
pendingHits: [],
|
|
125
|
+
outline: [],
|
|
112
126
|
globalExpanded: ctx.ui.getToolsExpanded(),
|
|
113
127
|
assistantCache: new WeakMap(),
|
|
114
128
|
};
|
|
@@ -170,6 +184,9 @@ export function installTranscriptCleanup(
|
|
|
170
184
|
};
|
|
171
185
|
state.tui.requestRender();
|
|
172
186
|
},
|
|
187
|
+
outline() {
|
|
188
|
+
return state.outline;
|
|
189
|
+
},
|
|
173
190
|
update(nextCtx, nextTui) {
|
|
174
191
|
state = {
|
|
175
192
|
...state,
|
|
@@ -197,17 +214,34 @@ export function installTranscriptCleanup(
|
|
|
197
214
|
state = { ...state, globalExpanded, expanded: new Map() };
|
|
198
215
|
}
|
|
199
216
|
state.pendingHits.length = 0;
|
|
217
|
+
const sink: OutlineEntry[] = [];
|
|
200
218
|
let lines: string[];
|
|
201
219
|
if (state.activeStart === undefined && state.ctx.isIdle()) {
|
|
202
|
-
lines = renderCollapsed(chat.children, width, state);
|
|
220
|
+
lines = renderCollapsed(chat.children, width, state, sink);
|
|
203
221
|
} else {
|
|
204
222
|
const start = state.activeStart ?? chat.children.length;
|
|
223
|
+
const collapsed = renderCollapsed(
|
|
224
|
+
chat.children.slice(0, start),
|
|
225
|
+
width,
|
|
226
|
+
state,
|
|
227
|
+
sink,
|
|
228
|
+
);
|
|
205
229
|
lines = [
|
|
206
|
-
...
|
|
207
|
-
...renderActive(
|
|
230
|
+
...collapsed,
|
|
231
|
+
...renderActive(
|
|
232
|
+
chat.children.slice(start),
|
|
233
|
+
width,
|
|
234
|
+
state,
|
|
235
|
+
sink,
|
|
236
|
+
collapsed.length,
|
|
237
|
+
),
|
|
208
238
|
];
|
|
209
239
|
}
|
|
210
|
-
|
|
240
|
+
const offset = documentRowOffset(document, chat, width);
|
|
241
|
+
state.outline = sink
|
|
242
|
+
.map((entry) => ({ ...entry, row: offset + entry.row }))
|
|
243
|
+
.sort((a, b) => a.row - b.row);
|
|
244
|
+
indexDetailHits(lines, offset, state);
|
|
211
245
|
return lines;
|
|
212
246
|
};
|
|
213
247
|
chat[INSTALLED] = controller;
|
|
@@ -218,6 +252,8 @@ function renderActive(
|
|
|
218
252
|
children: Component[],
|
|
219
253
|
width: number,
|
|
220
254
|
state: State,
|
|
255
|
+
sink: OutlineEntry[],
|
|
256
|
+
base: number,
|
|
221
257
|
): string[] {
|
|
222
258
|
const output: string[][] = Array.from({ length: children.length });
|
|
223
259
|
let hasTextBelow = false;
|
|
@@ -227,23 +263,54 @@ function renderActive(
|
|
|
227
263
|
const native = child.render(width);
|
|
228
264
|
output[index] =
|
|
229
265
|
state.completed.has(child) && hasTextBelow
|
|
230
|
-
? renderGroup([child], width, state)
|
|
266
|
+
? renderGroup([child], width, state, [], 0)
|
|
231
267
|
: native;
|
|
232
268
|
if (hasSectionText(native)) hasTextBelow = true;
|
|
233
269
|
}
|
|
270
|
+
// Outline rows are recorded on a forward pass because the render loop above
|
|
271
|
+
// walks backwards; the discarded renderGroup sink avoids double records.
|
|
272
|
+
let row = base;
|
|
273
|
+
for (let index = 0; index < children.length; index++) {
|
|
274
|
+
const rendered = output[index];
|
|
275
|
+
const child = children[index];
|
|
276
|
+
if (child && rendered?.length) {
|
|
277
|
+
const entry = actionEntry(child);
|
|
278
|
+
if (entry) sink.push({ ...entry, row });
|
|
279
|
+
}
|
|
280
|
+
row += rendered?.length ?? 0;
|
|
281
|
+
}
|
|
234
282
|
return output.flat();
|
|
235
283
|
}
|
|
236
284
|
|
|
285
|
+
function actionEntry(child: Component): Omit<OutlineEntry, "row"> | undefined {
|
|
286
|
+
if (isUserBoundary(child)) {
|
|
287
|
+
return { kind: "user", label: "prompt" };
|
|
288
|
+
}
|
|
289
|
+
const name = componentName(child);
|
|
290
|
+
if (name === "AssistantMessageComponent") {
|
|
291
|
+
return { kind: "assistant", label: "reply" };
|
|
292
|
+
}
|
|
293
|
+
if (name === "ToolExecutionComponent") {
|
|
294
|
+
const tool = child as ToolComponent;
|
|
295
|
+
return {
|
|
296
|
+
kind: tool.result?.isError === true ? "error" : "tool",
|
|
297
|
+
label: tool.toolName ?? "tool",
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
237
303
|
function renderCollapsed(
|
|
238
304
|
children: Component[],
|
|
239
305
|
width: number,
|
|
240
306
|
state: State,
|
|
307
|
+
sink: OutlineEntry[],
|
|
241
308
|
): string[] {
|
|
242
309
|
const lines: string[] = [];
|
|
243
310
|
let group: Component[] = [];
|
|
244
311
|
const flush = () => {
|
|
245
312
|
if (!group.length) return;
|
|
246
|
-
lines.push(...renderGroup(group, width, state));
|
|
313
|
+
lines.push(...renderGroup(group, width, state, sink, lines.length));
|
|
247
314
|
group = [];
|
|
248
315
|
};
|
|
249
316
|
|
|
@@ -278,12 +345,19 @@ function renderGroup(
|
|
|
278
345
|
group: Component[],
|
|
279
346
|
width: number,
|
|
280
347
|
state: State,
|
|
348
|
+
sink: OutlineEntry[],
|
|
349
|
+
base: number,
|
|
281
350
|
): string[] {
|
|
282
351
|
const lines: string[] = [];
|
|
283
352
|
|
|
284
353
|
for (const child of group) {
|
|
285
354
|
const name = componentName(child);
|
|
286
355
|
if (isUserBoundary(child)) {
|
|
356
|
+
sink.push({
|
|
357
|
+
kind: "user",
|
|
358
|
+
row: base + lines.length,
|
|
359
|
+
label: "prompt",
|
|
360
|
+
});
|
|
287
361
|
lines.push(...child.render(width));
|
|
288
362
|
continue;
|
|
289
363
|
}
|
|
@@ -292,10 +366,18 @@ function renderGroup(
|
|
|
292
366
|
continue;
|
|
293
367
|
}
|
|
294
368
|
if (name === "AssistantMessageComponent") {
|
|
369
|
+
const startRow = lines.length;
|
|
295
370
|
const assistant = child as AssistantComponent;
|
|
296
371
|
const message = assistant.lastMessage;
|
|
297
372
|
if (!message || !assistant.updateContent) {
|
|
298
373
|
lines.push(...child.render(width));
|
|
374
|
+
if (lines.length > startRow) {
|
|
375
|
+
sink.push({
|
|
376
|
+
kind: "assistant",
|
|
377
|
+
row: base + startRow,
|
|
378
|
+
label: "reply",
|
|
379
|
+
});
|
|
380
|
+
}
|
|
299
381
|
continue;
|
|
300
382
|
}
|
|
301
383
|
const hasToolCall = message.content.some(
|
|
@@ -344,11 +426,25 @@ function renderGroup(
|
|
|
344
426
|
);
|
|
345
427
|
}
|
|
346
428
|
}
|
|
429
|
+
// A turn whose reply was only tool calls emits nothing of its own; its
|
|
430
|
+
// visible actions are the tool rows, so no assistant entry is recorded.
|
|
431
|
+
if (lines.length > startRow) {
|
|
432
|
+
sink.push({
|
|
433
|
+
kind: "assistant",
|
|
434
|
+
row: base + startRow,
|
|
435
|
+
label: "reply",
|
|
436
|
+
});
|
|
437
|
+
}
|
|
347
438
|
continue;
|
|
348
439
|
}
|
|
349
440
|
if (name === "ToolExecutionComponent") {
|
|
350
441
|
const tool = child as ToolComponent;
|
|
351
442
|
const error = tool.result?.isError === true;
|
|
443
|
+
sink.push({
|
|
444
|
+
kind: error ? "error" : "tool",
|
|
445
|
+
row: base + lines.length,
|
|
446
|
+
label: tool.toolName ?? "tool",
|
|
447
|
+
});
|
|
352
448
|
lines.push(
|
|
353
449
|
...renderDetail(
|
|
354
450
|
{
|
|
@@ -437,7 +533,8 @@ function hasSectionText(lines: string[]): boolean {
|
|
|
437
533
|
const text = stripTerminalSequences(line).trim();
|
|
438
534
|
return (
|
|
439
535
|
text.length > 0 &&
|
|
440
|
-
|
|
536
|
+
// Pi 0.85.0 dropped the ellipsis from its default working label.
|
|
537
|
+
!/(?:^|\s)Working(?:\.\.\.|…)?(?:\s+\([^)]*\))?$/.test(text)
|
|
441
538
|
);
|
|
442
539
|
});
|
|
443
540
|
}
|
|
@@ -488,6 +585,10 @@ function detailColor(
|
|
|
488
585
|
}
|
|
489
586
|
|
|
490
587
|
function describeTool(tool: ToolComponent, error: boolean): string {
|
|
588
|
+
return `${error ? "error" : "tool"} · ${toolLabel(tool)}`;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function toolLabel(tool: ToolComponent): string {
|
|
491
592
|
const name = tool.toolName ?? "tool";
|
|
492
593
|
const args = asRecord(tool.args);
|
|
493
594
|
const value = [
|
|
@@ -502,8 +603,7 @@ function describeTool(tool: ToolComponent, error: boolean): string {
|
|
|
502
603
|
]
|
|
503
604
|
.map((key) => args?.[key])
|
|
504
605
|
.find((candidate): candidate is string => typeof candidate === "string");
|
|
505
|
-
|
|
506
|
-
return `${error ? "error" : "tool"} · ${name}${target}`;
|
|
606
|
+
return value ? `${name} · ${oneLine(value)}` : name;
|
|
507
607
|
}
|
|
508
608
|
|
|
509
609
|
function oneLine(text: string): string {
|