@abelxiaoxing/cadence 2.0.36 → 2.0.38
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.de.md +4 -4
- package/README.es.md +4 -4
- package/README.fr.md +4 -4
- package/README.ja.md +4 -4
- package/README.ko.md +4 -4
- package/README.md +11 -9
- package/README.pt-BR.md +4 -4
- package/README.ru.md +4 -4
- package/README.zh-CN.md +12 -10
- package/THIRD_PARTY_NOTICES.md +9 -1
- package/agents/design-explorer.md +4 -2
- package/agents/diagnosis-worker.md +5 -2
- package/agents/implementation-worker.md +19 -12
- package/docs/conversation-mode.md +46 -18
- package/docs/reuse-and-distribution.md +1 -1
- package/docs/service-management.md +35 -9
- package/package.json +16 -8
- package/src/agent-browser/labeled-rule.ts +74 -0
- package/src/agent-browser/session-navigator.ts +214 -0
- package/src/agent-browser/source.ts +174 -0
- package/src/agent-browser/transcript-content.ts +442 -0
- package/src/agent-browser/transcript-pane.ts +324 -0
- package/src/agent-panel-cadence.ts +147 -628
- package/src/agent-panel-model.ts +34 -267
- package/src/agent-panel.ts +139 -437
- package/src/cadence-instructions.ts +8 -7
- package/src/cadence-mode.ts +30 -9
- package/src/change-tools.mjs +268 -0
- package/src/coordinator-tools.mjs +44 -16
- package/src/git-environment.d.mts +2 -0
- package/src/git-environment.mjs +24 -0
- package/src/image-content.mjs +7 -28
- package/src/index.ts +1 -1
- package/src/native-build.ts +1 -1
- package/src/native-workflow-history.ts +20 -3
- package/src/operator-cli.mjs +18 -11
- package/src/research-scan.mjs +393 -0
- package/src/research-tools.mjs +406 -342
- package/src/sdk-worker.mjs +42 -55
- package/src/service/contracts/core.ts +0 -1
- package/src/service/contracts/workbench.ts +21 -19
- package/src/service/contracts/workflow.ts +283 -16
- package/src/service/coordination.ts +7 -0
- package/src/service-cli.mjs +652 -149
- package/src/service-host.mjs +2745 -1137
- package/src/worker-capture.mjs +24 -1
- package/src/worker-public.mjs +19 -2
- package/src/worker-redaction.mjs +11 -0
- package/src/worker-toolkit.mjs +157 -0
- package/src/worker-workspace.d.mts +13 -0
- package/src/worker-workspace.mjs +48 -0
- package/src/workspace.ts +4 -24
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +18 -17
- package/web/dist/manifest.json +7 -7
package/src/agent-panel.ts
CHANGED
|
@@ -1,93 +1,43 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* the
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* (a display-only session entry) and the live view goes away. The list below
|
|
8
|
-
* the input appears only while it is being used. */
|
|
9
|
-
import {
|
|
10
|
-
AssistantMessageComponent,
|
|
11
|
-
type ExtensionAPI,
|
|
12
|
-
type ExtensionContext,
|
|
13
|
-
getMarkdownTheme,
|
|
14
|
-
type Theme,
|
|
15
|
-
ToolExecutionComponent,
|
|
16
|
-
UserMessageComponent,
|
|
1
|
+
/** Readonly bus binding and compact batch summary. Agent browsing is delegated
|
|
2
|
+
* to the adapted upstream session navigator, not a second panel/focus model. */
|
|
3
|
+
import type {
|
|
4
|
+
ExtensionAPI,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
Theme,
|
|
17
7
|
} from "@earendil-works/pi-coding-agent";
|
|
18
8
|
import {
|
|
19
|
-
type Component,
|
|
20
|
-
Container,
|
|
21
9
|
Editor,
|
|
22
10
|
isKeyRelease,
|
|
23
|
-
Key,
|
|
24
|
-
type KeyId,
|
|
25
11
|
matchesKey,
|
|
26
|
-
Spacer,
|
|
27
|
-
Text,
|
|
28
12
|
type TUI,
|
|
29
|
-
|
|
13
|
+
type TuiMainScreen,
|
|
30
14
|
} from "@earendil-works/pi-tui";
|
|
15
|
+
import { SessionNavigator } from "./agent-browser/session-navigator.ts";
|
|
16
|
+
import { cadenceBrowserSource } from "./agent-browser/source.ts";
|
|
17
|
+
import type { ReadingPosition } from "./agent-browser/transcript-pane.ts";
|
|
31
18
|
import {
|
|
32
19
|
type AgentCard,
|
|
33
20
|
CARD_ENTRY,
|
|
34
21
|
cardLines,
|
|
35
22
|
cardOf,
|
|
36
23
|
decodeCard,
|
|
37
|
-
factsText,
|
|
38
24
|
liveLines,
|
|
39
|
-
MAX_ENDED,
|
|
40
25
|
type Paint,
|
|
41
|
-
type PanelAction,
|
|
42
|
-
PanelFocus,
|
|
43
|
-
type PanelKey,
|
|
44
|
-
panelLines,
|
|
45
26
|
SUBAGENT_EVENTS,
|
|
46
27
|
SUBAGENT_VIEW,
|
|
47
28
|
type SubagentState,
|
|
48
29
|
SubagentStore,
|
|
49
|
-
statusSymbol,
|
|
50
30
|
} from "./agent-panel-model.ts";
|
|
51
31
|
|
|
52
32
|
export interface AgentPanelOptions {
|
|
53
|
-
/**
|
|
54
|
-
maxRows?: number;
|
|
55
|
-
/** Keep a finished batch live instead of collapsing it into a card
|
|
56
|
-
* (CADENCE_AGENT_PANEL=always). */
|
|
33
|
+
/** Keep a settled summary instead of appending a session card. */
|
|
57
34
|
keep?: boolean;
|
|
58
35
|
now?: () => number;
|
|
59
36
|
}
|
|
60
|
-
const LIVE = "cadence-agents"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
/** A batch whose agents all ended stays live this long before it collapses
|
|
64
|
-
* into its card, so the outcome is seen and a start that follows at once
|
|
65
|
-
* (the next workflow step) joins the same batch. */
|
|
37
|
+
const LIVE = "cadence-agents";
|
|
38
|
+
const SPIN_MS = 100;
|
|
39
|
+
/** Adjacent workflow steps join the same batch during this settle window. */
|
|
66
40
|
export const SETTLE_MS = 1500;
|
|
67
|
-
|
|
68
|
-
/** Rows kept for Pi's footer/status lines while an agent view is shown. */
|
|
69
|
-
const RESERVED = 5;
|
|
70
|
-
const KEYS: [KeyId, PanelKey][] = [
|
|
71
|
-
[Key.down, "down"],
|
|
72
|
-
[Key.up, "up"],
|
|
73
|
-
[Key.ctrl("n"), "next"],
|
|
74
|
-
[Key.ctrl("p"), "previous"],
|
|
75
|
-
[Key.enter, "enter"],
|
|
76
|
-
[Key.escape, "escape"],
|
|
77
|
-
[Key.ctrl("c"), "interrupt"],
|
|
78
|
-
[Key.pageUp, "pageUp"],
|
|
79
|
-
[Key.pageDown, "pageDown"],
|
|
80
|
-
[Key.ctrl("u"), "halfUp"],
|
|
81
|
-
[Key.ctrl("d"), "halfDown"],
|
|
82
|
-
[Key.home, "home"],
|
|
83
|
-
[Key.end, "end"],
|
|
84
|
-
];
|
|
85
|
-
/** Key releases (Kitty protocol) stay Pi's. */
|
|
86
|
-
export function panelKey(data: string): PanelKey | undefined {
|
|
87
|
-
if (isKeyRelease(data)) return undefined;
|
|
88
|
-
return KEYS.find(([id]) => matchesKey(data, id))?.[1] ?? "other";
|
|
89
|
-
}
|
|
90
|
-
|
|
91
41
|
const themePaint =
|
|
92
42
|
(theme: Theme): Paint =>
|
|
93
43
|
(role, text) =>
|
|
@@ -97,28 +47,29 @@ export function registerAgentPanel(
|
|
|
97
47
|
pi: ExtensionAPI,
|
|
98
48
|
options: AgentPanelOptions = {},
|
|
99
49
|
): void {
|
|
100
|
-
|
|
50
|
+
let store: SubagentStore | undefined;
|
|
101
51
|
for (const channel of Object.values(SUBAGENT_EVENTS))
|
|
102
52
|
pi.events.on(channel, (data) => {
|
|
103
|
-
store
|
|
53
|
+
store?.apply(channel, data);
|
|
104
54
|
});
|
|
105
|
-
// Pi renders the card where it was appended and again on resume; Ctrl+O
|
|
106
|
-
// (Pi's tool-output expansion) lists every member.
|
|
107
55
|
pi.registerEntryRenderer(CARD_ENTRY, (entry, { expanded }, theme) => {
|
|
108
56
|
const card = decodeCard(entry.data);
|
|
109
57
|
if (!card) return undefined;
|
|
110
|
-
const paint = themePaint(theme);
|
|
111
58
|
return {
|
|
112
59
|
render: (width) =>
|
|
113
|
-
cardLines(
|
|
114
|
-
|
|
115
|
-
|
|
60
|
+
cardLines(
|
|
61
|
+
card,
|
|
62
|
+
expanded,
|
|
63
|
+
Math.max(1, width - 1),
|
|
64
|
+
themePaint(theme),
|
|
65
|
+
).map((line) => ` ${line}`),
|
|
116
66
|
invalidate() {},
|
|
117
67
|
};
|
|
118
68
|
});
|
|
119
69
|
let detach: (() => void) | undefined;
|
|
120
70
|
pi.on("session_start", (_event, ctx) => {
|
|
121
71
|
detach?.();
|
|
72
|
+
store = new SubagentStore(options.now);
|
|
122
73
|
detach =
|
|
123
74
|
ctx.mode === "tui" && ctx.hasUI
|
|
124
75
|
? attachPanel(
|
|
@@ -133,6 +84,7 @@ export function registerAgentPanel(
|
|
|
133
84
|
pi.on("session_shutdown", () => {
|
|
134
85
|
detach?.();
|
|
135
86
|
detach = undefined;
|
|
87
|
+
store = undefined;
|
|
136
88
|
});
|
|
137
89
|
}
|
|
138
90
|
|
|
@@ -144,111 +96,75 @@ function attachPanel(
|
|
|
144
96
|
commit: (card: AgentCard) => void,
|
|
145
97
|
): () => void {
|
|
146
98
|
const now = options.now ?? Date.now;
|
|
147
|
-
const maxRows = Math.max(1, options.maxRows ?? 6);
|
|
148
99
|
const keep = options.keep ?? process.env.CADENCE_AGENT_PANEL === "always";
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
let
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
// list's slot below it while any agent is kept for review.
|
|
162
|
-
live = false,
|
|
163
|
-
list = false,
|
|
164
|
-
// The last batch, kept live after it ended (CADENCE_AGENT_PANEL=always).
|
|
165
|
-
kept: SubagentState[] = [],
|
|
166
|
-
// The agent last announced on `subagent:view`, so producers load only it.
|
|
167
|
-
announced: string | undefined;
|
|
168
|
-
const announceView = () => {
|
|
169
|
-
if (focus.view === announced) return;
|
|
170
|
-
announced = focus.view;
|
|
171
|
-
announce(announced ?? null);
|
|
172
|
-
};
|
|
173
|
-
const transcripts = new Map<string, Transcript>();
|
|
100
|
+
let tui: TUI | undefined;
|
|
101
|
+
let frame = 0;
|
|
102
|
+
let spinner: ReturnType<typeof setInterval> | undefined;
|
|
103
|
+
let settling: ReturnType<typeof setTimeout> | undefined;
|
|
104
|
+
let mounted = false;
|
|
105
|
+
let detached = false;
|
|
106
|
+
let opening = false;
|
|
107
|
+
let kept: SubagentState[] = [];
|
|
108
|
+
let browser: SessionNavigator | undefined;
|
|
109
|
+
let closeBrowser: (() => void) | undefined;
|
|
110
|
+
const positions = new Map<string, ReadingPosition>();
|
|
111
|
+
const liveAgents = () => (store.batch().length ? store.batch() : kept);
|
|
174
112
|
const stopSpinner = () => {
|
|
175
113
|
clearInterval(spinner);
|
|
176
114
|
spinner = undefined;
|
|
177
115
|
};
|
|
178
|
-
const liveAgents = () => {
|
|
179
|
-
const open = store.batch();
|
|
180
|
-
return open.length ? open : kept;
|
|
181
|
-
};
|
|
182
|
-
const widget =
|
|
183
|
-
(lines: (width: number) => string[]) => (t: TUI, theme: Theme) => {
|
|
184
|
-
tui = t;
|
|
185
|
-
paint = themePaint(theme);
|
|
186
|
-
return { render: lines, invalidate() {} };
|
|
187
|
-
};
|
|
188
|
-
const liveWidget = widget((width) =>
|
|
189
|
-
// The agent view takes this place; a summary above it would be noise.
|
|
190
|
-
view
|
|
191
|
-
? []
|
|
192
|
-
: liveLines(
|
|
193
|
-
{
|
|
194
|
-
agents: liveAgents(),
|
|
195
|
-
now: now(),
|
|
196
|
-
frame,
|
|
197
|
-
maxRows,
|
|
198
|
-
hint: focus.focus === "input",
|
|
199
|
-
},
|
|
200
|
-
width,
|
|
201
|
-
paint,
|
|
202
|
-
),
|
|
203
|
-
);
|
|
204
|
-
const listWidget = widget((width) => panelLines(panelFrame(), width, paint));
|
|
205
116
|
const mount = () => {
|
|
206
|
-
|
|
207
|
-
//
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
117
|
+
// Keep a handle only for a live batch (or an explicitly kept summary),
|
|
118
|
+
// never just because old agents remain in memory.
|
|
119
|
+
const show = liveAgents().length > 0;
|
|
120
|
+
if (show === mounted) return;
|
|
121
|
+
mounted = show;
|
|
122
|
+
ctx.ui.setWidget(
|
|
123
|
+
LIVE,
|
|
124
|
+
show
|
|
125
|
+
? (t, theme) => {
|
|
126
|
+
tui = t;
|
|
127
|
+
return {
|
|
128
|
+
render: (width) =>
|
|
129
|
+
browser || opening
|
|
130
|
+
? []
|
|
131
|
+
: liveLines(
|
|
132
|
+
{ agents: liveAgents(), now: now(), frame, hint: true },
|
|
133
|
+
width,
|
|
134
|
+
themePaint(theme),
|
|
135
|
+
),
|
|
136
|
+
invalidate() {},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
: undefined,
|
|
140
|
+
{ placement: "aboveEditor" },
|
|
141
|
+
);
|
|
223
142
|
};
|
|
224
|
-
|
|
225
|
-
* goes away and older ended agents beyond the review bound are dropped. */
|
|
226
|
-
const close = () => {
|
|
143
|
+
const closeBatch = () => {
|
|
227
144
|
clearTimeout(settling);
|
|
228
145
|
settling = undefined;
|
|
229
146
|
if (!store.settled()) return;
|
|
230
147
|
const agents = store.closeBatch();
|
|
231
148
|
if (keep) kept = agents;
|
|
232
|
-
else
|
|
149
|
+
else {
|
|
233
150
|
try {
|
|
234
151
|
commit(cardOf(agents));
|
|
235
152
|
} catch {
|
|
236
|
-
|
|
153
|
+
/* A replaced runtime cannot append. */
|
|
237
154
|
}
|
|
238
|
-
|
|
239
|
-
for (const id of
|
|
240
|
-
focus.forget(dropped);
|
|
155
|
+
}
|
|
156
|
+
for (const id of store.prune()) positions.delete(id);
|
|
241
157
|
};
|
|
242
|
-
// One redraw per spinner frame while anything runs; Pi's differential
|
|
243
|
-
// renderer rewrites only the changed rows.
|
|
244
158
|
const changed = () => {
|
|
159
|
+
if (detached) return;
|
|
160
|
+
for (const id of store.prune()) positions.delete(id);
|
|
245
161
|
if (kept.length && store.batch().length) kept = [];
|
|
246
162
|
if (!store.settled()) {
|
|
247
163
|
clearTimeout(settling);
|
|
248
164
|
settling = undefined;
|
|
249
165
|
} else if (!settling) {
|
|
250
166
|
settling = setTimeout(() => {
|
|
251
|
-
|
|
167
|
+
closeBatch();
|
|
252
168
|
changed();
|
|
253
169
|
}, SETTLE_MS);
|
|
254
170
|
settling.unref?.();
|
|
@@ -257,7 +173,7 @@ function attachPanel(
|
|
|
257
173
|
else if (!spinner) {
|
|
258
174
|
spinner = setInterval(() => {
|
|
259
175
|
frame++;
|
|
260
|
-
|
|
176
|
+
tui?.requestRender();
|
|
261
177
|
}, SPIN_MS);
|
|
262
178
|
spinner.unref?.();
|
|
263
179
|
}
|
|
@@ -265,303 +181,89 @@ function attachPanel(
|
|
|
265
181
|
tui?.requestRender();
|
|
266
182
|
};
|
|
267
183
|
const unsubscribe = store.subscribe(changed);
|
|
268
|
-
const
|
|
269
|
-
(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (action === "open") openView();
|
|
276
|
-
else if (action === "close") view?.done();
|
|
277
|
-
announceView();
|
|
278
|
-
mount();
|
|
279
|
-
tui?.requestRender();
|
|
280
|
-
return action === "pass" ? undefined : { consume: true };
|
|
281
|
-
};
|
|
282
|
-
const unlisten = ctx.ui.onTerminalInput((data) => {
|
|
283
|
-
if (focus.view !== undefined) {
|
|
284
|
-
// The view handles its own keys. Pi may have put its editor back in the
|
|
285
|
-
// view's slot (another dialog came and went); then the view is gone.
|
|
286
|
-
if (!(focused() instanceof Editor)) return undefined;
|
|
287
|
-
focus.abandon();
|
|
288
|
-
view = undefined;
|
|
289
|
-
announceView();
|
|
290
|
-
mount();
|
|
291
|
-
}
|
|
292
|
-
if (focus.focus === "panel" && !editorFocused()) {
|
|
293
|
-
// A dialog took focus: its keys are not the panel's.
|
|
294
|
-
focus.abandon();
|
|
295
|
-
mount();
|
|
296
|
-
tui?.requestRender();
|
|
297
|
-
return undefined;
|
|
298
|
-
}
|
|
299
|
-
const key = panelKey(data);
|
|
300
|
-
if (!key) return undefined;
|
|
301
|
-
const idle = ctx.ui.getEditorText() === "" && editorFocused();
|
|
302
|
-
return act(focus.key(key, idle));
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
const openView = () => {
|
|
184
|
+
const open = () => {
|
|
185
|
+
if (opening || browser || detached || !tui) return;
|
|
186
|
+
opening = true;
|
|
187
|
+
// This is the current renderer's public mode, not a saved setting or a
|
|
188
|
+
// version fallback. Regular mode retains the upstream docked mount to
|
|
189
|
+
// avoid writing overlay pixels into the terminal's scrollback.
|
|
190
|
+
const fullscreen = tui.mode === "fullscreen";
|
|
306
191
|
void ctx.ui
|
|
307
|
-
.custom<void>(
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
192
|
+
.custom<void>(
|
|
193
|
+
(t, theme, keys, done) => {
|
|
194
|
+
closeBrowser = () => done();
|
|
195
|
+
browser = new SessionNavigator(
|
|
196
|
+
t,
|
|
197
|
+
theme,
|
|
198
|
+
keys,
|
|
199
|
+
// Transcript caches belong to this interaction, not future batches.
|
|
200
|
+
cadenceBrowserSource(store, announce, now),
|
|
201
|
+
ctx.cwd,
|
|
202
|
+
positions,
|
|
203
|
+
closeBrowser,
|
|
204
|
+
);
|
|
205
|
+
if (detached) done();
|
|
206
|
+
return browser;
|
|
207
|
+
},
|
|
208
|
+
fullscreen
|
|
209
|
+
? {
|
|
210
|
+
overlay: true,
|
|
211
|
+
overlayOptions: {
|
|
212
|
+
width: "100%",
|
|
213
|
+
maxHeight: "70%",
|
|
214
|
+
anchor: "center",
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
: { overlay: false },
|
|
218
|
+
)
|
|
219
|
+
.catch(() => {
|
|
220
|
+
if (!detached)
|
|
221
|
+
ctx.ui.notify("Could not open the agent browser", "error");
|
|
318
222
|
})
|
|
319
223
|
.finally(() => {
|
|
320
|
-
|
|
321
|
-
|
|
224
|
+
browser?.dispose();
|
|
225
|
+
browser = undefined;
|
|
226
|
+
closeBrowser = undefined;
|
|
227
|
+
opening = false;
|
|
228
|
+
if (!detached) changed();
|
|
322
229
|
});
|
|
323
230
|
};
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
231
|
+
const unlisten = ctx.ui.onTerminalInput((data) => {
|
|
232
|
+
if (
|
|
233
|
+
detached ||
|
|
234
|
+
opening ||
|
|
235
|
+
browser ||
|
|
236
|
+
isKeyRelease(data) ||
|
|
237
|
+
!matchesKey(data, "down")
|
|
238
|
+
)
|
|
239
|
+
return;
|
|
240
|
+
if (
|
|
241
|
+
store.running() === 0 ||
|
|
242
|
+
ctx.ui.getEditorText() !== "" ||
|
|
243
|
+
tui?.hasOverlay() ||
|
|
244
|
+
!(
|
|
245
|
+
(tui as TuiMainScreen | undefined)?.getFocusedComponent() instanceof
|
|
246
|
+
Editor
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
return;
|
|
250
|
+
open();
|
|
251
|
+
return { consume: true };
|
|
332
252
|
});
|
|
333
|
-
const viewLines = (width: number): string[] => {
|
|
334
|
-
const agent = focus.view === undefined ? undefined : store.get(focus.view);
|
|
335
|
-
if (!agent) return [];
|
|
336
|
-
const panelHeight = panelLines(panelFrame(), width, paint).length;
|
|
337
|
-
const height = Math.max(
|
|
338
|
-
3,
|
|
339
|
-
(tui?.terminal.rows ?? 24) - panelHeight - RESERVED,
|
|
340
|
-
);
|
|
341
|
-
let transcript = transcripts.get(agent.id);
|
|
342
|
-
if (!transcript && tui) {
|
|
343
|
-
transcript = new Transcript(tui, ctx.cwd, ctx.ui.getToolsExpanded());
|
|
344
|
-
transcripts.set(agent.id, transcript);
|
|
345
|
-
}
|
|
346
|
-
const body = transcript?.render(agent, width, paint) ?? [];
|
|
347
|
-
const top = focus.settle(agent.id, body.length, height - 2);
|
|
348
|
-
const rows = body.slice(top, top + height - 2);
|
|
349
|
-
while (rows.length < height - 2) rows.push("");
|
|
350
|
-
return [
|
|
351
|
-
truncateToWidth(banner(agent), width, "…"),
|
|
352
|
-
...rows,
|
|
353
|
-
truncateToWidth(
|
|
354
|
-
paint(
|
|
355
|
-
"dim",
|
|
356
|
-
"↑↓ select · Enter open · PgUp/PgDn scroll · Esc back to main",
|
|
357
|
-
),
|
|
358
|
-
width,
|
|
359
|
-
"…",
|
|
360
|
-
),
|
|
361
|
-
];
|
|
362
|
-
};
|
|
363
|
-
const banner = (agent: SubagentState): string => {
|
|
364
|
-
if (agent.status === "running")
|
|
365
|
-
return paint(
|
|
366
|
-
"dim",
|
|
367
|
-
`→ viewing: ${agent.label} (Esc to return to main)`,
|
|
368
|
-
);
|
|
369
|
-
const [symbol, role] = statusSymbol(agent, frame);
|
|
370
|
-
const word = { done: "finished", error: "failed", cancelled: "cancelled" }[
|
|
371
|
-
agent.status
|
|
372
|
-
];
|
|
373
|
-
const facts = factsText(agent, now());
|
|
374
|
-
return `${paint(role, `${symbol} ${word}`)}${paint(
|
|
375
|
-
"dim",
|
|
376
|
-
` (${facts}) · ${agent.label} (Esc to return to main)`,
|
|
377
|
-
)}`;
|
|
378
|
-
};
|
|
379
|
-
|
|
380
253
|
changed();
|
|
381
254
|
return () => {
|
|
255
|
+
detached = true;
|
|
382
256
|
unsubscribe();
|
|
383
257
|
unlisten();
|
|
384
258
|
stopSpinner();
|
|
385
|
-
|
|
386
|
-
// in; one still running carries over to the next session.
|
|
387
|
-
if (settling) close();
|
|
259
|
+
if (settling) closeBatch();
|
|
388
260
|
clearTimeout(settling);
|
|
389
|
-
|
|
261
|
+
browser?.dispose();
|
|
390
262
|
try {
|
|
391
|
-
|
|
392
|
-
if (
|
|
393
|
-
if (list) ctx.ui.setWidget(LIST, undefined);
|
|
263
|
+
closeBrowser?.();
|
|
264
|
+
if (mounted) ctx.ui.setWidget(LIVE, undefined);
|
|
394
265
|
} catch {
|
|
395
|
-
|
|
266
|
+
/* The old runtime already cleared its UI. */
|
|
396
267
|
}
|
|
397
268
|
};
|
|
398
269
|
}
|
|
399
|
-
|
|
400
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: match OSC 133 terminal marks
|
|
401
|
-
const OSC_133 = /\x1b\]133;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
402
|
-
type Message = Record<string, unknown> & { content?: unknown };
|
|
403
|
-
const textOf = (content: unknown): string =>
|
|
404
|
-
typeof content === "string"
|
|
405
|
-
? content
|
|
406
|
-
: Array.isArray(content)
|
|
407
|
-
? content
|
|
408
|
-
.map((block) =>
|
|
409
|
-
block?.type === "text"
|
|
410
|
-
? String(block.text ?? "")
|
|
411
|
-
: block?.type === "image"
|
|
412
|
-
? "[image]"
|
|
413
|
-
: "",
|
|
414
|
-
)
|
|
415
|
-
.filter(Boolean)
|
|
416
|
-
.join("\n")
|
|
417
|
-
: "";
|
|
418
|
-
|
|
419
|
-
/** One agent's messages as Pi's own transcript components, the way Pi renders
|
|
420
|
-
* a resumed session. Only the tail is rebuilt: new messages, a replaced
|
|
421
|
-
* streaming snapshot, or everything once older messages were dropped. */
|
|
422
|
-
class Transcript {
|
|
423
|
-
private readonly root = new Container();
|
|
424
|
-
private parts: Component[][] = [];
|
|
425
|
-
private tail: object | undefined;
|
|
426
|
-
private tailStreaming = false;
|
|
427
|
-
private omitted = 0;
|
|
428
|
-
private revision = -1;
|
|
429
|
-
private readonly tools = new Map<string, ToolExecutionComponent>();
|
|
430
|
-
constructor(
|
|
431
|
-
private readonly tui: TUI,
|
|
432
|
-
private readonly cwd: string,
|
|
433
|
-
private readonly expanded: boolean,
|
|
434
|
-
) {}
|
|
435
|
-
|
|
436
|
-
render(agent: SubagentState, width: number, paint: Paint): string[] {
|
|
437
|
-
this.sync(agent);
|
|
438
|
-
// Pi marks its own prompts for terminal shell integration; a sliced view
|
|
439
|
-
// of another agent's prompts must not open unmatched marks.
|
|
440
|
-
let lines: string[];
|
|
441
|
-
try {
|
|
442
|
-
lines = this.root.render(width).map((line) => line.replace(OSC_133, ""));
|
|
443
|
-
} catch {
|
|
444
|
-
// Producer data must never break Pi's own render loop.
|
|
445
|
-
lines = [paint("error", " (this transcript could not be rendered)")];
|
|
446
|
-
}
|
|
447
|
-
if (agent.omitted)
|
|
448
|
-
lines.unshift(
|
|
449
|
-
paint("dim", `… ${agent.omitted} earlier messages not retained`),
|
|
450
|
-
);
|
|
451
|
-
if (!agent.messages.length) lines.push(paint("dim", " (no messages yet)"));
|
|
452
|
-
return lines;
|
|
453
|
-
}
|
|
454
|
-
private sync(agent: SubagentState): void {
|
|
455
|
-
if (agent.revision === this.revision) return;
|
|
456
|
-
this.revision = agent.revision;
|
|
457
|
-
const { messages } = agent;
|
|
458
|
-
if (agent.omitted !== this.omitted) {
|
|
459
|
-
this.root.clear();
|
|
460
|
-
this.parts = [];
|
|
461
|
-
this.tools.clear();
|
|
462
|
-
this.omitted = agent.omitted;
|
|
463
|
-
}
|
|
464
|
-
let from = this.parts.length;
|
|
465
|
-
// A streaming snapshot may be replaced or mutated in place.
|
|
466
|
-
if (from > 0 && (this.tailStreaming || messages[from - 1] !== this.tail))
|
|
467
|
-
from--;
|
|
468
|
-
for (const part of this.parts.splice(from))
|
|
469
|
-
for (const component of part) this.root.removeChild(component);
|
|
470
|
-
for (let i = from; i < messages.length; i++) {
|
|
471
|
-
const last = i === messages.length - 1;
|
|
472
|
-
const part = this.build(messages[i] as Message, last && agent.streaming);
|
|
473
|
-
for (const component of part) this.root.addChild(component);
|
|
474
|
-
this.parts.push(part);
|
|
475
|
-
}
|
|
476
|
-
this.tail = messages.at(-1);
|
|
477
|
-
this.tailStreaming = agent.streaming;
|
|
478
|
-
}
|
|
479
|
-
private build(message: Message, streaming: boolean): Component[] {
|
|
480
|
-
switch (message.role) {
|
|
481
|
-
case "user": {
|
|
482
|
-
const text = textOf(message.content);
|
|
483
|
-
return text
|
|
484
|
-
? [new Spacer(1), new UserMessageComponent(text, getMarkdownTheme())]
|
|
485
|
-
: [];
|
|
486
|
-
}
|
|
487
|
-
case "assistant": {
|
|
488
|
-
// Pi's component assumes well-formed blocks; keep only those.
|
|
489
|
-
const content = (
|
|
490
|
-
Array.isArray(message.content) ? message.content : []
|
|
491
|
-
).filter(
|
|
492
|
-
(b) =>
|
|
493
|
-
(b?.type === "text" && typeof b.text === "string") ||
|
|
494
|
-
(b?.type === "thinking" && typeof b.thinking === "string") ||
|
|
495
|
-
b?.type === "toolCall",
|
|
496
|
-
);
|
|
497
|
-
const parts: Component[] = [
|
|
498
|
-
new AssistantMessageComponent(
|
|
499
|
-
{ ...message, content } as never,
|
|
500
|
-
false,
|
|
501
|
-
getMarkdownTheme(),
|
|
502
|
-
),
|
|
503
|
-
];
|
|
504
|
-
const failed =
|
|
505
|
-
message.stopReason === "aborted" || message.stopReason === "error";
|
|
506
|
-
for (const block of content) {
|
|
507
|
-
if (block.type !== "toolCall") continue;
|
|
508
|
-
const tool = this.tool(
|
|
509
|
-
String(block.name ?? "tool"),
|
|
510
|
-
String(block.id ?? ""),
|
|
511
|
-
block.arguments ?? {},
|
|
512
|
-
);
|
|
513
|
-
// Pi's edit renderer previews complete arguments against the file
|
|
514
|
-
// as it is now, where an applied edit no longer matches and would
|
|
515
|
-
// read as a failure; a transcript edit shows its call and result only.
|
|
516
|
-
if (!streaming && block.name !== "edit") tool.setArgsComplete();
|
|
517
|
-
if (failed)
|
|
518
|
-
tool.updateResult({
|
|
519
|
-
content: [
|
|
520
|
-
{
|
|
521
|
-
type: "text",
|
|
522
|
-
text: String(message.errorMessage ?? "Operation aborted"),
|
|
523
|
-
},
|
|
524
|
-
],
|
|
525
|
-
isError: true,
|
|
526
|
-
});
|
|
527
|
-
parts.push(tool);
|
|
528
|
-
}
|
|
529
|
-
return parts;
|
|
530
|
-
}
|
|
531
|
-
case "toolResult": {
|
|
532
|
-
const result = {
|
|
533
|
-
content: Array.isArray(message.content) ? message.content : [],
|
|
534
|
-
details: message.details,
|
|
535
|
-
isError: message.isError === true,
|
|
536
|
-
};
|
|
537
|
-
const id = String(message.toolCallId ?? "");
|
|
538
|
-
const pending = this.tools.get(id);
|
|
539
|
-
if (pending) {
|
|
540
|
-
pending.updateResult(result, streaming);
|
|
541
|
-
return [];
|
|
542
|
-
}
|
|
543
|
-
const orphan = this.tool(String(message.toolName ?? "tool"), id, {});
|
|
544
|
-
orphan.updateResult(result, streaming);
|
|
545
|
-
return [orphan];
|
|
546
|
-
}
|
|
547
|
-
default: {
|
|
548
|
-
const text = textOf(message.content);
|
|
549
|
-
return text ? [new Spacer(1), new Text(text, 1, 0)] : [];
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
private tool(name: string, id: string, args: unknown) {
|
|
554
|
-
const tool = new ToolExecutionComponent(
|
|
555
|
-
name,
|
|
556
|
-
id,
|
|
557
|
-
args,
|
|
558
|
-
{},
|
|
559
|
-
undefined,
|
|
560
|
-
this.tui,
|
|
561
|
-
this.cwd,
|
|
562
|
-
);
|
|
563
|
-
tool.setExpanded(this.expanded);
|
|
564
|
-
this.tools.set(id, tool);
|
|
565
|
-
return tool;
|
|
566
|
-
}
|
|
567
|
-
}
|