@abelxiaoxing/cadence 2.0.35 → 2.0.37

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.
Files changed (40) hide show
  1. package/README.de.md +4 -4
  2. package/README.es.md +4 -4
  3. package/README.fr.md +4 -4
  4. package/README.ja.md +3 -4
  5. package/README.ko.md +4 -4
  6. package/README.md +4 -4
  7. package/README.pt-BR.md +4 -4
  8. package/README.ru.md +5 -4
  9. package/README.zh-CN.md +4 -4
  10. package/THIRD_PARTY_NOTICES.md +9 -1
  11. package/docs/conversation-mode.md +21 -2
  12. package/docs/reuse-and-distribution.md +1 -1
  13. package/docs/service-management.md +35 -9
  14. package/package.json +18 -18
  15. package/src/abort-scope.ts +47 -0
  16. package/src/agent-browser/labeled-rule.ts +74 -0
  17. package/src/agent-browser/session-navigator.ts +187 -0
  18. package/src/agent-browser/source.ts +170 -0
  19. package/src/agent-browser/transcript-content.ts +442 -0
  20. package/src/agent-browser/transcript-pane.ts +324 -0
  21. package/src/agent-panel-model.ts +4 -248
  22. package/src/agent-panel.ts +138 -432
  23. package/src/coordinator-tools.mjs +42 -14
  24. package/src/image-content.mjs +7 -28
  25. package/src/native-build.ts +1 -1
  26. package/src/research-scan.mjs +393 -0
  27. package/src/research-tools.mjs +406 -342
  28. package/src/sdk-worker.mjs +40 -55
  29. package/src/service/contracts/core.ts +0 -1
  30. package/src/service/contracts/workbench.ts +10 -16
  31. package/src/service-cli.mjs +328 -106
  32. package/src/service-host.mjs +545 -404
  33. package/src/service-http-client.ts +55 -50
  34. package/src/worker-public.mjs +1 -2
  35. package/src/worker-toolkit.mjs +110 -0
  36. package/src/worker-workspace.d.mts +13 -0
  37. package/src/worker-workspace.mjs +48 -0
  38. package/web/dist/assets/cadence.css +1 -1
  39. package/web/dist/assets/cadence.js +17 -17
  40. package/web/dist/manifest.json +7 -7
@@ -0,0 +1,324 @@
1
+ /** Adapted from gotgenes/pi-packages @ 52854c12d590142e61a4f6a6947be9084035300d.
2
+ * MIT; see licenses/pi-subagents-MIT.txt and provenance/agent-browser-reuse.json. */
3
+ import type {
4
+ KeybindingsManager,
5
+ Theme,
6
+ } from "@earendil-works/pi-coding-agent";
7
+ import {
8
+ type Component,
9
+ type Keybinding,
10
+ type KeyId,
11
+ type MarkdownTheme,
12
+ matchesKey,
13
+ type TUI,
14
+ type TuiMouseEvent,
15
+ type TuiMouseEventResult,
16
+ truncateToWidth,
17
+ } from "@earendil-works/pi-tui";
18
+ import { labeledRule } from "./labeled-rule.ts";
19
+ import type {
20
+ EntryHeading,
21
+ ModelIdentity,
22
+ TranscriptSource,
23
+ } from "./source.ts";
24
+ import { TranscriptContent } from "./transcript-content.ts";
25
+
26
+ const CHROME_LINES = 2;
27
+ export const VIEWPORT_HEIGHT_PCT = 70;
28
+ interface ScrollOptions {
29
+ follow?: boolean;
30
+ }
31
+ export interface ReadingPosition {
32
+ offset: number;
33
+ follow: boolean;
34
+ }
35
+ export interface TranscriptPaneOptions {
36
+ tui: TUI;
37
+ theme: Theme;
38
+ keys: Pick<KeybindingsManager, "matches" | "getKeys">;
39
+ source: TranscriptSource;
40
+ heading: EntryHeading;
41
+ done: (result: undefined) => void;
42
+ cwd: string;
43
+ markdownTheme: MarkdownTheme;
44
+ position: ReadingPosition;
45
+ }
46
+ const formatModel = (model: ModelIdentity) => `${model.provider}/${model.id}`;
47
+ export class TranscriptPane implements Component {
48
+ readonly capturesViewportInput = true;
49
+ private readonly position: ReadingPosition;
50
+ private get scrollOffset() {
51
+ return this.position.offset;
52
+ }
53
+ private set scrollOffset(value: number) {
54
+ this.position.offset = value;
55
+ }
56
+ private get autoScroll() {
57
+ return this.position.follow;
58
+ }
59
+ private set autoScroll(value: boolean) {
60
+ this.position.follow = value;
61
+ }
62
+ private unsubscribe: (() => void) | undefined;
63
+ private closed = false;
64
+
65
+ private readonly tui: TUI;
66
+ private readonly theme: Theme;
67
+ private readonly keys: Pick<KeybindingsManager, "matches" | "getKeys">;
68
+ private readonly source: TranscriptSource;
69
+ private readonly heading: EntryHeading;
70
+ private readonly done: (result: undefined) => void;
71
+ private readonly content: TranscriptContent;
72
+ /** Width the host last rendered at; input must use the same layout. */
73
+ private renderedWidth: number | undefined;
74
+
75
+ constructor({
76
+ tui,
77
+ theme,
78
+ keys,
79
+ source,
80
+ heading,
81
+ done,
82
+ cwd,
83
+ markdownTheme,
84
+ position,
85
+ }: TranscriptPaneOptions) {
86
+ this.position = position;
87
+ this.tui = tui;
88
+ this.theme = theme;
89
+ this.keys = keys;
90
+ this.source = source;
91
+ this.heading = heading;
92
+ this.done = done;
93
+ this.content = new TranscriptContent({ tui, cwd, markdownTheme, source });
94
+ this.unsubscribe = source.subscribe(() => {
95
+ if (this.closed) return;
96
+ this.content.apply();
97
+ this.tui.requestRender();
98
+ });
99
+ }
100
+
101
+ handleInput(data: string): void {
102
+ if (
103
+ matchesKey(data, "escape") ||
104
+ matchesKey(data, "q") ||
105
+ matchesKey(data, "ctrl+c")
106
+ ) {
107
+ this.closed = true;
108
+ this.done(undefined);
109
+ return;
110
+ }
111
+
112
+ const { viewportHeight } = this.scrollBounds(this.inputWidth());
113
+
114
+ if (matchesKey(data, "up") || matchesKey(data, "k")) {
115
+ this.scrollBy(-1);
116
+ } else if (matchesKey(data, "down") || matchesKey(data, "j")) {
117
+ this.scrollBy(1);
118
+ } else if (
119
+ this.keys.matches(data, "tui.altScreen.pageUp") ||
120
+ matchesKey(data, "shift+up")
121
+ ) {
122
+ this.scrollBy(-viewportHeight, { follow: false });
123
+ } else if (
124
+ this.keys.matches(data, "tui.altScreen.pageDown") ||
125
+ matchesKey(data, "shift+down")
126
+ ) {
127
+ this.scrollBy(viewportHeight);
128
+ } else if (
129
+ this.keys.matches(data, "tui.altScreen.top") ||
130
+ matchesKey(data, "home")
131
+ ) {
132
+ this.scrollTo(0, { follow: false });
133
+ } else if (
134
+ this.keys.matches(data, "tui.altScreen.bottom") ||
135
+ matchesKey(data, "end")
136
+ ) {
137
+ this.scrollTo(Number.POSITIVE_INFINITY);
138
+ } else if (this.keys.matches(data, "app.tools.expand")) {
139
+ this.content.toggleExpanded();
140
+ }
141
+ this.tui.requestRender();
142
+ }
143
+
144
+ /** The wheel scrolls the transcript; every other mouse event is left to the host. */
145
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
146
+ if (event.type !== "wheel") return undefined;
147
+ this.scrollBy(event.wheelDelta ?? 0);
148
+ this.tui.requestRender();
149
+ return { handled: true };
150
+ }
151
+
152
+ render(width: number): string[] {
153
+ if (width < 6) return [];
154
+ const th = this.theme;
155
+ this.renderedWidth = width;
156
+ const lines: string[] = [];
157
+
158
+ // Rows span at most the width the host supplied: pi-tui rejects a wider one.
159
+ const fitLine = (content: string): string =>
160
+ truncateToWidth(content, width);
161
+ const { model, thinkingLevel } = this.source.sessionModel();
162
+ const paint = th.getThinkingBorderColor(thinkingLevel ?? "off");
163
+
164
+ lines.push(
165
+ labeledRule(width, paint, this.headerLabels(model, thinkingLevel)),
166
+ );
167
+
168
+ const { totalLines, viewportHeight, maxScroll } = this.scrollBounds(width);
169
+ if (this.autoScroll) this.scrollOffset = maxScroll;
170
+ const visibleStart = Math.min(this.scrollOffset, maxScroll);
171
+ this.scrollOffset = visibleStart;
172
+ const visible = this.content.slice(width, visibleStart, viewportHeight);
173
+ for (let i = 0; i < viewportHeight; i++)
174
+ lines.push(fitLine(visible[i] ?? ""));
175
+
176
+ const scrollPct =
177
+ totalLines <= viewportHeight
178
+ ? "100%"
179
+ : `${Math.round(((visibleStart + viewportHeight) / totalLines) * 100)}%`;
180
+ const position = th.fg(
181
+ "dim",
182
+ `${totalLines} lines · ${scrollPct} · ${this.autoScroll ? "following" : "paused"}`,
183
+ );
184
+ const hints = th.fg("dim", this.footerHint());
185
+ lines.push(labeledRule(width, paint, [position], [hints]));
186
+
187
+ return lines;
188
+ }
189
+
190
+ // fallow-ignore-next-line unused-class-member
191
+ invalidate(): void {
192
+ this.content.invalidate();
193
+ }
194
+
195
+ dispose(): void {
196
+ this.closed = true;
197
+ if (this.unsubscribe) {
198
+ this.unsubscribe();
199
+ this.unsubscribe = undefined;
200
+ }
201
+ }
202
+
203
+ // ---- Private ----
204
+
205
+ /**
206
+ * The key hints the footer rule carries on its right, naming the keys the
207
+ * operator actually bound: the first key of each binding, and no segment
208
+ * for a pair left wholly unbound.
209
+ */
210
+ private footerHint(): string {
211
+ const page = this.keyPair("tui.altScreen.pageUp", "tui.altScreen.pageDown");
212
+ const ends = this.keyPair("tui.altScreen.top", "tui.altScreen.bottom");
213
+ return ["↑↓ scroll", page, ends, "Esc list"]
214
+ .filter((segment) => segment !== "")
215
+ .join(" · ");
216
+ }
217
+
218
+ /** `PageUp/PageDown` for a pair of bindings; the bound side alone when the other is unbound. */
219
+ private keyPair(first: Keybinding, second: Keybinding): string {
220
+ return [first, second]
221
+ .flatMap((keybinding) => this.keys.getKeys(keybinding).slice(0, 1))
222
+ .map(displayKey)
223
+ .join("/");
224
+ }
225
+
226
+ /**
227
+ * Header labels, most to least informative: the rule drops the task first,
228
+ * then the model and thinking level, before it truncates the agent's name.
229
+ */
230
+ private headerLabels(
231
+ model: ModelIdentity | undefined,
232
+ thinkingLevel: string | undefined,
233
+ ): string[] {
234
+ const th = this.theme;
235
+ const { name, modeLabel, description } = this.heading;
236
+ const identity =
237
+ th.bold(name) + (modeLabel ? ` ${th.fg("muted", `(${modeLabel})`)}` : "");
238
+ const runtime = describeRuntime(model, thinkingLevel);
239
+ const runtimeTag = runtime ? th.fg("muted", ` · ${runtime}`) : "";
240
+ return [
241
+ `${identity} ${th.fg("muted", description)}${runtimeTag}`,
242
+ identity + runtimeTag,
243
+ identity,
244
+ ];
245
+ }
246
+
247
+ private scrollBy(delta: number, options?: ScrollOptions): void {
248
+ this.scrollTo(this.scrollOffset + delta, options);
249
+ }
250
+
251
+ /**
252
+ * Move to `offset`, clamped to the transcript. The pane follows new output
253
+ * when it lands on the bottom, unless the move says otherwise: paging up or
254
+ * jumping to the top stops following even on a transcript that fits.
255
+ */
256
+ private scrollTo(offset: number, { follow }: ScrollOptions = {}): void {
257
+ const { maxScroll } = this.scrollBounds(this.inputWidth());
258
+ this.scrollOffset = Math.min(maxScroll, Math.max(0, offset));
259
+ this.autoScroll = follow ?? this.scrollOffset >= maxScroll;
260
+ }
261
+
262
+ /**
263
+ * Scroll geometry at a given layout width.
264
+ *
265
+ * The single place a width becomes a viewport height, so `render` and
266
+ * `handleInput` cannot disagree about how far the transcript scrolls.
267
+ */
268
+ private scrollBounds(width: number): {
269
+ totalLines: number;
270
+ viewportHeight: number;
271
+ maxScroll: number;
272
+ } {
273
+ const totalLines = this.content.lineCount(width);
274
+ const viewportHeight = this.viewportHeight(totalLines);
275
+ return {
276
+ totalLines,
277
+ viewportHeight,
278
+ maxScroll: Math.max(0, totalLines - viewportHeight),
279
+ };
280
+ }
281
+
282
+ /**
283
+ * The width `handleInput` must lay out at: the one the host actually supplied,
284
+ * so scroll bounds match the layout on screen. Before the first paint there is
285
+ * none, so fall back to the full terminal width.
286
+ */
287
+ private inputWidth(): number {
288
+ return this.renderedWidth ?? this.tui.terminal.columns;
289
+ }
290
+
291
+ /**
292
+ * Rows the transcript gets: what it needs, capped at the pane's share of the
293
+ * terminal so a long or live transcript cannot crowd out the conversation, and
294
+ * floored so a short one still reads as a pane.
295
+ */
296
+ private viewportHeight(totalLines: number): number {
297
+ const cap =
298
+ Math.floor((this.tui.terminal.rows * VIEWPORT_HEIGHT_PCT) / 100) -
299
+ CHROME_LINES;
300
+ return Math.max(0, Math.min(Math.max(1, totalLines), cap));
301
+ }
302
+ }
303
+
304
+ /** A key id in Pi's display style: `ctrl+b` → `Ctrl+B`, `pageUp` → `PageUp`. */
305
+ function displayKey(key: KeyId): string {
306
+ return key
307
+ .split("+")
308
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
309
+ .join("+");
310
+ }
311
+
312
+ /**
313
+ * `anthropic/claude-sonnet-5 • high`, in Pi's footer wording: `thinking off` rather
314
+ * than a bare `off`, and `thinking <level>` when no model is named to attach it to.
315
+ */
316
+ function describeRuntime(
317
+ model: ModelIdentity | undefined,
318
+ thinkingLevel: string | undefined,
319
+ ): string {
320
+ if (!model) return thinkingLevel ? `thinking ${thinkingLevel}` : "";
321
+ if (!thinkingLevel) return formatModel(model);
322
+ const level = thinkingLevel === "off" ? "thinking off" : thinkingLevel;
323
+ return `${formatModel(model)} • ${level}`;
324
+ }
@@ -301,28 +301,6 @@ export function described(
301
301
  : `${agent.label} · ${text}`;
302
302
  }
303
303
 
304
- /** The `max` most recently active agents plus any pinned ones, shown in
305
- * stable start order, and how many others are hidden. */
306
- export function visibleAgents(
307
- agents: SubagentState[],
308
- max: number,
309
- pinned: (string | undefined)[] = [],
310
- ): { rows: SubagentState[]; hidden: number } {
311
- if (agents.length <= max) return { rows: agents, hidden: 0 };
312
- const recent = [...agents].sort((a, b) => b.activeAt - a.activeAt);
313
- const keep = new Set(recent.slice(0, max).map((a) => a.id));
314
- for (const id of pinned) {
315
- if (!id || keep.has(id) || !agents.some((a) => a.id === id)) continue;
316
- const drop = [...keep]
317
- .reverse()
318
- .find((k) => !pinned.includes(k) && k !== id);
319
- if (drop) keep.delete(drop);
320
- keep.add(id);
321
- }
322
- const rows = agents.filter((a) => keep.has(a.id));
323
- return { rows, hidden: agents.length - rows.length };
324
- }
325
-
326
304
  const s = (n: number) => (n === 1 ? "" : "s");
327
305
  const cut = (text: string, width: number) =>
328
306
  truncateToWidth(text, Math.max(0, width), "…");
@@ -368,16 +346,6 @@ function rowLine(
368
346
  width,
369
347
  );
370
348
  }
371
- const liveRow = (a: SubagentState, now: number, frame: number): Row => {
372
- const used = usage(a);
373
- return {
374
- symbol: statusSymbol(a, frame),
375
- kind: a.kind,
376
- text: described(a),
377
- failed: a.status === "error",
378
- facts: `${elapsedText(a, now).padStart(8)}${used ? ` · ${used}` : ""}`,
379
- };
380
- };
381
349
  const kindColumn = (kinds: string[]) =>
382
350
  Math.min(12, Math.max(7, ...kinds.map((k) => visibleWidth(k))));
383
351
  /** The kind every agent shares, if any; it then names the batch instead of
@@ -519,20 +487,16 @@ export interface LiveFrame {
519
487
  agents: SubagentState[];
520
488
  now: number;
521
489
  frame: number;
522
- maxRows: number;
523
490
  /** Show the `↓ to manage` hint: the input box has focus. */
524
491
  hint: boolean;
525
492
  }
526
- /** Above the input while a batch is open. Running: a spinner header with the
527
- * batch's outcome counts, then its running and failed/cancelled members; a
528
- * member that finished cleanly is only counted. Settled: the card it is about
529
- * to become, so the collapse into the transcript does not change the text. */
493
+ /** One summary line; the adapted session browser owns all agent rows. */
530
494
  export function liveLines(f: LiveFrame, width: number, paint: Paint): string[] {
531
495
  const { agents } = f;
532
496
  if (agents.length === 0) return [];
533
497
  const running = agents.filter((a) => a.status === "running");
534
498
  if (running.length === 0)
535
- return cardLines(cardOf(agents), false, width, paint, false);
499
+ return cardLines(cardOf(agents), false, width, paint, false).slice(0, 1);
536
500
  const kind = sharedKind(agents);
537
501
  const tally = (status: SubagentStatus) =>
538
502
  agents.filter((a) => a.status === status).length;
@@ -545,217 +509,9 @@ export function liveLines(f: LiveFrame, width: number, paint: Paint): string[] {
545
509
  const [spin, role] = statusSymbol(running[0], f.frame);
546
510
  const header = hinted(
547
511
  `${paint(role, spin)} ${running.length} ${noun(kind, running.length)} running${counts}`,
548
- f.hint ? " (↓ to manage)" : "",
512
+ f.hint ? " (↓ to view)" : "",
549
513
  width,
550
514
  paint,
551
515
  );
552
- const listed = [
553
- ...running,
554
- ...agents.filter((a) => a.status === "error" || a.status === "cancelled"),
555
- ];
556
- const rows = listed.slice(0, f.maxRows);
557
- const more = listed.length - rows.length;
558
- const kindWidth = kind ? 0 : kindColumn(rows.map((a) => a.kind));
559
- const leads = tree(rows.length, more);
560
- const lines = [
561
- header,
562
- ...rows.map((a, i) =>
563
- rowLine(liveRow(a, f.now, f.frame), leads[i], kindWidth, width, paint),
564
- ),
565
- ];
566
- if (more) lines.push(cut(`${paint("dim", " └ ")}… +${more} more`, width));
567
- return lines;
568
- }
569
-
570
- export interface PanelFrame {
571
- agents: SubagentState[];
572
- focus: "input" | "panel";
573
- /** Selected row: an agent id, or undefined while the input has focus. */
574
- selected: string | undefined;
575
- /** Agent shown in place of the main view, if any. */
576
- view: string | undefined;
577
- now: number;
578
- frame: number;
579
- maxRows: number;
580
- }
581
- /** Below the input only while it is being used (focus moved down to it, or
582
- * an agent view is open): this Pi session's agents, at most `maxRows` of the
583
- * most recently active plus the selection, `+K more`, and a key hint. */
584
- export function panelLines(
585
- f: PanelFrame,
586
- width: number,
587
- paint: Paint,
588
- ): string[] {
589
- if (f.agents.length === 0 || (f.focus !== "panel" && f.view === undefined))
590
- return [];
591
- const { rows, hidden } = visibleAgents(f.agents, f.maxRows, [
592
- f.selected,
593
- f.view,
594
- ]);
595
- const lines = hidden ? [cut(paint("dim", ` +${hidden} more`), width)] : [];
596
- const kindWidth = kindColumn(rows.map((a) => a.kind));
597
- for (const a of rows) {
598
- const selected = f.focus === "panel" && f.selected === a.id;
599
- lines.push(
600
- rowLine(
601
- liveRow(a, f.now, f.frame),
602
- selected ? "▶" : " ",
603
- kindWidth,
604
- width,
605
- paint,
606
- selected,
607
- ),
608
- );
609
- }
610
- if (f.view === undefined)
611
- lines.push(
612
- cut(paint("dim", " ↑↓ select · Enter open · Esc back to input"), width),
613
- );
614
- return lines;
615
- }
616
-
617
- export type PanelKey =
618
- | "down"
619
- | "up"
620
- | "next"
621
- | "previous"
622
- | "enter"
623
- | "escape"
624
- | "interrupt"
625
- | "pageUp"
626
- | "pageDown"
627
- | "halfUp"
628
- | "halfDown"
629
- | "home"
630
- | "end"
631
- | "other";
632
- /** `pass` leaves the key to Pi; `open`/`close` show or leave an agent view. */
633
- export type PanelAction = "pass" | "consume" | "open" | "close";
634
- export interface Scroll {
635
- top: number;
636
- /** Stick to the newest line as the transcript grows. */
637
- follow: boolean;
638
- }
639
-
640
- /** Focus model. F_input (default) passes every key except ↓ on an idle, empty
641
- * editor, which moves focus down to the agent list; F_panel moves its
642
- * selection, and ↑ past the first row or Esc returns to the input. An agent
643
- * view keeps F_panel and replaces the main view until Esc or Ctrl+C. */
644
- export class PanelFocus {
645
- focus: "input" | "panel" = "input";
646
- selected: string | undefined;
647
- view: string | undefined;
648
- /** Rows of the last rendered agent view, for paging. */
649
- page = 10;
650
- private readonly scrolls = new Map<string, Scroll>();
651
- constructor(
652
- private readonly store: SubagentStore,
653
- private readonly maxRows = 6,
654
- ) {}
655
-
656
- key(key: PanelKey, editorIdle: boolean): PanelAction {
657
- if (this.focus === "input" && this.view === undefined) {
658
- if (key !== "down" || !editorIdle || this.store.size === 0) return "pass";
659
- this.focus = "panel";
660
- this.selected = visibleAgents(
661
- this.store.ordered(),
662
- this.maxRows,
663
- ).rows[0]?.id;
664
- return "consume";
665
- }
666
- const rows = this.store.ordered().map((a) => a.id);
667
- // A selection the store dropped falls back to the first row.
668
- const at = Math.max(0, rows.indexOf(this.selected ?? ""));
669
- switch (key) {
670
- case "up":
671
- case "previous":
672
- // ↑ past the first row returns to the input, the inverse of ↓.
673
- if (at === 0 && this.view === undefined) this.toInput();
674
- else this.selected = rows[Math.max(0, at - 1)];
675
- return "consume";
676
- case "down":
677
- case "next":
678
- this.selected = rows[Math.min(rows.length - 1, at + 1)];
679
- return "consume";
680
- case "enter":
681
- this.selected = rows[at];
682
- if (this.selected === undefined) {
683
- this.toInput();
684
- return "consume";
685
- }
686
- if (this.view === undefined) {
687
- this.view = this.selected;
688
- return "open";
689
- }
690
- this.view = this.selected;
691
- return "consume";
692
- case "escape":
693
- case "interrupt":
694
- if (this.view !== undefined) return this.leave();
695
- this.toInput();
696
- // Ctrl+C keeps Pi's own clear/interrupt/exit meaning.
697
- return key === "escape" ? "consume" : "pass";
698
- case "pageUp":
699
- case "pageDown":
700
- case "halfUp":
701
- case "halfDown":
702
- case "home":
703
- case "end":
704
- if (this.view !== undefined) {
705
- this.scroll(this.view, key);
706
- return "consume";
707
- }
708
- // The main transcript scrolls itself; Ctrl+U/D must not edit or exit.
709
- return key === "halfUp" || key === "halfDown" ? "consume" : "pass";
710
- default:
711
- if (this.view !== undefined) return "consume";
712
- this.toInput();
713
- return "pass";
714
- }
715
- }
716
- /** Pi replaced the agent view (another dialog took the editor slot). */
717
- abandon(): void {
718
- this.view = undefined;
719
- this.toInput();
720
- }
721
- /** Top line for this view, remembered per agent across switches. */
722
- settle(id: string, total: number, height: number): number {
723
- const s = this.scrolls.get(id) ?? { top: 0, follow: true };
724
- const max = Math.max(0, total - height);
725
- const top = s.follow ? max : Math.min(Math.max(0, s.top), max);
726
- this.scrolls.set(id, { top, follow: top >= max });
727
- this.page = Math.max(1, height);
728
- return top;
729
- }
730
- scrollOf(id: string): Scroll | undefined {
731
- return this.scrolls.get(id);
732
- }
733
- /** The store dropped these agents: forget their scroll positions. */
734
- forget(ids: string[]): void {
735
- for (const id of ids) this.scrolls.delete(id);
736
- }
737
- private toInput(): void {
738
- this.focus = "input";
739
- this.selected = undefined;
740
- }
741
- private leave(): PanelAction {
742
- this.view = undefined;
743
- return "close";
744
- }
745
- private scroll(id: string, key: PanelKey): void {
746
- const s = this.scrolls.get(id) ?? { top: 0, follow: true };
747
- const step =
748
- key === "pageUp" || key === "pageDown"
749
- ? Math.max(1, this.page - 1)
750
- : Math.max(1, Math.floor(this.page / 2));
751
- const sign = key === "pageUp" || key === "halfUp" ? -1 : 1;
752
- this.scrolls.set(
753
- id,
754
- key === "home"
755
- ? { top: 0, follow: false }
756
- : key === "end"
757
- ? { top: s.top, follow: true }
758
- : { top: s.top + sign * step, follow: false },
759
- );
760
- }
516
+ return [header];
761
517
  }