@turri-dev/cli 0.4.3-next.8482fed → 0.4.3-next.a60ada5

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.
@@ -0,0 +1,21 @@
1
+ import { join, relative } from "node:path";
2
+ import { DefaultResourceLoader, getAgentDir } from "@earendil-works/pi-coding-agent";
3
+ import { PACKAGE_ROOT, themeFiles } from "./environment.js";
4
+ /** pi already picks the project's theme over the user's, then turri's bundled fallback. In the chat these copies
5
+ * of a brand theme are expected; a broken file or a clash with a theme from elsewhere is still worth showing. */
6
+ export function quietBrandThemeOverrides(cwd = process.cwd(), agentDir = getAgentDir()) {
7
+ const directories = [join(cwd, ".pi", "themes"), join(agentDir, "themes"), join(PACKAGE_ROOT, ".pi", "themes")];
8
+ const files = new Set(themeFiles());
9
+ const expected = ({ type, collision }) => {
10
+ if (type !== "collision" || collision?.resourceType !== "theme")
11
+ return false;
12
+ const file = `${collision.name}.json`;
13
+ const isCopy = (path) => directories.some((dir) => relative(dir, path) === file);
14
+ return files.has(file) && isCopy(collision.winnerPath) && isCopy(collision.loserPath);
15
+ };
16
+ const getThemes = DefaultResourceLoader.prototype.getThemes;
17
+ DefaultResourceLoader.prototype.getThemes = function () {
18
+ const { themes, diagnostics } = getThemes.call(this);
19
+ return { themes, diagnostics: diagnostics.filter((item) => !expected(item)) };
20
+ };
21
+ }
@@ -1,5 +1,5 @@
1
1
  import type { Theme } from "@earendil-works/pi-coding-agent";
2
- export type ButtonRole = "tab" | "new" | "close" | "panel";
2
+ export type ButtonRole = "tab" | "new" | "close" | "panel" | "regenerating";
3
3
  export type ButtonPaint = (text: string, active: boolean, hovered: boolean, role: ButtonRole) => string;
4
4
  /** The layout owns the padding, so the entire coloured surface is also the click target. */
5
5
  export declare function buttonLabel(theme: Theme, text: string, active: boolean, hovered: boolean, role: ButtonRole): string;
@@ -1,5 +1,7 @@
1
1
  import { colorToRgb, parseColor } from "@earendil-works/pi-tui";
2
+ import { BRAND_PINK } from "./regenerate/plain-palette.js";
2
3
  const abyss = parseColor("#0a0f1a");
4
+ const pink = parseColor(BRAND_PINK.dark);
3
5
  const white = parseColor("#f4f7f9");
4
6
  const linear = (byte) => byte <= 10 ? byte / 3294.6 : ((byte / 255 + 0.055) / 1.055) ** 2.4;
5
7
  function luminance(color) {
@@ -11,6 +13,8 @@ function luminance(color) {
11
13
  const inkSwitch = Math.sqrt((luminance(abyss) + 0.05) * (luminance(white) + 0.05)) - 0.05;
12
14
  /** The layout owns the padding, so the entire coloured surface is also the click target. */
13
15
  export function buttonLabel(theme, text, active, hovered, role) {
16
+ if (role === "regenerating")
17
+ return theme.style(text, { bg: pink, fg: abyss, bold: true, underline: hovered });
14
18
  if (role === "new" || active || hovered) {
15
19
  const fill = theme.colors[role === "new" ? "success" : role === "close" && hovered ? "error" : "accent"];
16
20
  return theme.style(text, { bg: fill, fg: luminance(fill) > inkSwitch ? abyss : white, bold: true, underline: hovered });
@@ -18,6 +18,8 @@ export interface QuestionDialogOptions {
18
18
  question: string;
19
19
  /** The choices. Without them, a line to type the answer on. */
20
20
  options?: string[];
21
+ /** Clickable choices when this dialog is embedded in the side panel. */
22
+ clickable?: boolean;
21
23
  /** The choice the cursor starts on. */
22
24
  selected?: number;
23
25
  /** The terminal's height, for the window the brief scrolls in. */
@@ -40,6 +42,9 @@ export declare class QuestionDialog implements Component, Focusable {
40
42
  private maxScroll;
41
43
  private window;
42
44
  private answered;
45
+ private choiceLines;
46
+ private choiceTop;
47
+ private pressed;
43
48
  private focusedNow;
44
49
  constructor(options: QuestionDialogOptions);
45
50
  get focused(): boolean;
@@ -15,6 +15,9 @@ export class QuestionDialog {
15
15
  maxScroll = 0;
16
16
  window = 0;
17
17
  answered = false;
18
+ choiceLines = [];
19
+ choiceTop = 0;
20
+ pressed;
18
21
  focusedNow = false;
19
22
  constructor(options) {
20
23
  this.options = options;
@@ -61,10 +64,13 @@ export class QuestionDialog {
61
64
  const brief = context === "" ? [] : context.split("\n").flatMap((line) => (line.trim() === "" ? [""] : wrapTextWithAnsi(styled(line, style), inner)));
62
65
  const question = wrapTextWithAnsi(style.bold(style.accent(this.options.question)), inner);
63
66
  const choices = this.options.options ?? [];
67
+ this.choiceLines = [];
64
68
  const body = this.input !== undefined
65
69
  ? this.input.render(inner)
66
70
  : // A long option wraps, its lines under its first, rather than cut off where the person cannot read it.
67
71
  choices.flatMap((choice, index) => wrapTextWithAnsi(choice, Math.max(1, inner - 2)).map((line, at) => {
72
+ if (this.options.clickable)
73
+ this.choiceLines.push(index);
68
74
  const shown = `${at === 0 && index === this.selected ? "›" : " "} ${line}`;
69
75
  return index === this.selected ? style.accent(shown) : shown;
70
76
  }));
@@ -89,7 +95,9 @@ export class QuestionDialog {
89
95
  }
90
96
  lines.push("");
91
97
  }
92
- lines.push(...question.map(pad), ...body.map(pad), "", pad(style.muted(hint)));
98
+ lines.push(...question.map(pad));
99
+ this.choiceTop = lines.length;
100
+ lines.push(...body.map(pad), "", pad(style.muted(hint)));
93
101
  return lines;
94
102
  }
95
103
  handleInput(data) {
@@ -131,6 +139,17 @@ export class QuestionDialog {
131
139
  }
132
140
  }
133
141
  handleMouse(event) {
142
+ if (this.options.clickable && event.button === "left" && (event.type === "press" || event.type === "click")) {
143
+ const index = this.choiceLines[event.y - this.choiceTop];
144
+ const down = this.pressed;
145
+ this.pressed = event.type === "press" ? index : undefined;
146
+ if (index === undefined)
147
+ return;
148
+ this.selected = index;
149
+ if (event.type === "click" && down === index)
150
+ this.finish(this.options.options?.[index]);
151
+ return { handled: true };
152
+ }
134
153
  if (event.type !== "wheel" || event.wheelDelta === undefined || this.maxScroll === 0)
135
154
  return undefined;
136
155
  this.scrollBy(event.wheelDelta);
@@ -1,122 +1,20 @@
1
- import { type Color } from "@earendil-works/pi-tui";
2
- import { type Job } from "./jobs.ts";
3
- /**
4
- * The live panel above the editor while background work runs.
5
- *
6
- * One block per running job: what it is and how long it has been going, the stage it is
7
- * at, what is happening this second, and the last things its agents did. What waits is not
8
- * in it - the side panel lists the queue. It disappears when nothing is running - the finished
9
- * job announces itself in the transcript instead.
10
- *
11
- * It is drawn as a component rather than handed over as lines, because pi cuts a list of
12
- * lines off at ten. Instead it sizes itself: compact by default, and the agents' trace in full
13
- * while tool output is expanded - pi's own ctrl+o, which the person already uses to see more of
14
- * what the agent did. What finished stays counted either way: the side panel lists it.
15
- */
16
- export declare const JOB_PANEL_KEY = "turri-jobs";
17
- /**
18
- * The key that opens the side panel on its jobs, and goes back to it. Not ctrl+j: a terminal without the Kitty keyboard
19
- * protocol sends ctrl+j as a line feed - what it sends for shift+enter too, and what a dialog reads as enter - so it
20
- * would answer a question, and take a new line from the editor. ctrl+h is the byte old terminals sent for backspace;
21
- * every terminal now sends backspace as DEL, which pi never takes for ctrl+h, so it is free - in pi, and in a dialog.
22
- */
1
+ import type { Job } from "./jobs.ts";
2
+ /** Opens the side panel; ctrl+j would conflict with the editor's newline. */
23
3
  export declare const PANEL_KEY = "ctrl+h";
24
- /**
25
- * Each key in the line under the panel - ctrl+o, ctrl+h - breathes until it is first pressed: fully there, a little
26
- * faint, and back, so it reads as something to press, not as text. Once every BREATH_MS, redrawn every BREATH_TICK_MS
27
- * while the panel shows and a key still breathes, at most BREATH_FADE of the way from the text's colour to the dim one.
28
- * Pressed once, it stays still for the rest of the process; the other breathes on.
29
- */
30
- export declare const BREATH_MS = 2400;
31
- /** The keys of the line, by what they do: pressed once, each stops breathing. */
32
- export type PanelKey = "trace" | "peek";
33
- /** A key of the line was pressed - ctrl+o, or the side panel opened: it stops breathing. */
34
- export declare function keyPressed(key: PanelKey): void;
35
- export declare function breathes(key: PanelKey): boolean;
36
- /** For tests: every key breathing again. */
37
- export declare function resetKeysForTests(): void;
38
- /** How faint a breathing key is at `now`: 0, fully there, at the start of each breath, BREATH_FADE halfway through. */
39
- export declare function breath(now: number): number;
40
4
  export declare function spinner(now: number): string;
41
- /**
42
- * What finished, a line a module - the module that finished last on top, its name, then its pulses newest first -
43
- * fitted to `width`: the newest that fit, the rest counted as earlier. The side panel lists every one.
44
- */
45
- export declare function finishedLines(finished: Job[], width: number | undefined, marks?: MarkPaint): string[];
46
- /** A pulse that ran to its end and whose build did not pass - stopped at a gate - is not a success. */
5
+ /** A pulse that ran to its end and whose build did not pass is not a success. */
47
6
  export declare function didNotPass(job: Job): boolean;
48
- /** How a finished pulse's mark is coloured: green it passed, red it failed or did not pass, amber it was stopped. */
49
- export interface MarkPaint {
7
+ interface MarkPaint {
50
8
  success(text: string): string;
51
9
  error(text: string): string;
52
10
  warning(text: string): string;
53
11
  }
54
- /** A finished pulse's mark, in the colour of how it ended. */
55
12
  export declare function finishedMark(job: Job, paint?: MarkPaint): string;
56
- /** What a pulse cost, as its line says it: " · $0.12", or nothing when the price is not known. */
57
13
  export declare function pulseCost(job: Job): string;
58
- /**
59
- * One line saying what a spec change does: the files, the lines added and removed, and the
60
- * first line it adds - usually the concept or the functional spec it is about.
61
- */
62
- /** How a diff is coloured: added lines green, removed lines red. Plain when there is no theme. */
63
14
  export interface DiffStyle {
64
15
  added(text: string): string;
65
16
  removed(text: string): string;
66
17
  }
18
+ /** One line saying what a spec change does: the files, the lines added and removed, and the first line it adds. */
67
19
  export declare function summarizeDiff(diff: string, style?: DiffStyle): string;
68
- /** The lines of a diff worth reading in a panel: the hunks, not the headers. */
69
- /**
70
- * A line of the panel wrapped to its width, its continuations under its text: indented as far as it is,
71
- * and past the rule of a diff box when it is in one.
72
- */
73
- export declare function wrapKeepingIndent(line: string, width: number): string[];
74
- /** A diff's colours for the +/- alone, when there is nothing to highlight with. */
75
- export interface PanelOptions {
76
- /** Everything, rather than the compact view. */
77
- expanded?: boolean;
78
- now?: number;
79
- width?: number;
80
- /** The lines of help about ctrl+o and the side panel - left out where they do not apply, as in the side panel itself. */
81
- hints?: boolean;
82
- /** How the finished pulses' marks are coloured. */
83
- marks?: MarkPaint;
84
- /** How each key of the line is painted - breathing until it is pressed, in the TUI. */
85
- keys?: (key: PanelKey, text: string) => string;
86
- }
87
- export declare function jobPanelLines(jobs: Job[], options?: PanelOptions): string[];
88
- interface Redrawable {
89
- requestRender(): void;
90
- }
91
- /** What of pi's theme the panel paints with: the diff's colours and bands, and ***plain's colours in it. */
92
- interface PanelTheme {
93
- fg(color: "toolDiffAdded" | "toolDiffRemoved" | "success" | "error" | "warning", text: string): string;
94
- /** Whether it is made for a dark or a light surface, and how many colours the terminal has: the diff's palette. */
95
- appearance?: "dark" | "light";
96
- getColorMode?(): string;
97
- /** Each token's concrete colour - what the line of keys breathes between. */
98
- colors?: Readonly<Record<string, Color>>;
99
- }
100
- /**
101
- * The keys of the line, painted at `now`'s breath: one not pressed yet in the text's colour mixed toward the dim one, one
102
- * pressed as it is. Plain where the theme has no colours to mix.
103
- */
104
- export declare function breathing(theme: PanelTheme, now: number): (key: PanelKey, text: string) => string;
105
- /** The parts of pi's TUI the panel needs: enough to draw, redraw, and ask about ctrl+o. */
106
- export interface PanelUI {
107
- setWidget(key: string, content: ((tui: Redrawable, theme: PanelTheme) => {
108
- render(width: number): string[];
109
- invalidate(): void;
110
- }) | undefined, options?: {
111
- placement?: "aboveEditor" | "belowEditor";
112
- }): void;
113
- getToolsExpanded(): boolean;
114
- }
115
- export interface JobPanel {
116
- /** Where to draw. Until this is set there is no UI, and the panel does nothing. */
117
- attach(ui: PanelUI): void;
118
- /** Redraw soon, e.g. because a job changed. */
119
- refresh(): void;
120
- }
121
- export declare function createJobPanel(list: () => Job[]): JobPanel;
122
20
  export {};
@@ -1,98 +1,14 @@
1
- import { foregroundAnsi, mixColors, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
2
- import { formatDuration, formatElapsed, pulseName } from "./jobs.js";
3
- /**
4
- * The live panel above the editor while background work runs.
5
- *
6
- * One block per running job: what it is and how long it has been going, the stage it is
7
- * at, what is happening this second, and the last things its agents did. What waits is not
8
- * in it - the side panel lists the queue. It disappears when nothing is running - the finished
9
- * job announces itself in the transcript instead.
10
- *
11
- * It is drawn as a component rather than handed over as lines, because pi cuts a list of
12
- * lines off at ten. Instead it sizes itself: compact by default, and the agents' trace in full
13
- * while tool output is expanded - pi's own ctrl+o, which the person already uses to see more of
14
- * what the agent did. What finished stays counted either way: the side panel lists it.
15
- */
16
- export const JOB_PANEL_KEY = "turri-jobs";
17
- /**
18
- * The key that opens the side panel on its jobs, and goes back to it. Not ctrl+j: a terminal without the Kitty keyboard
19
- * protocol sends ctrl+j as a line feed - what it sends for shift+enter too, and what a dialog reads as enter - so it
20
- * would answer a question, and take a new line from the editor. ctrl+h is the byte old terminals sent for backspace;
21
- * every terminal now sends backspace as DEL, which pi never takes for ctrl+h, so it is free - in pi, and in a dialog.
22
- */
1
+ /** Opens the side panel; ctrl+j would conflict with the editor's newline. */
23
2
  export const PANEL_KEY = "ctrl+h";
24
- const TICK_MS = 1000;
25
- /**
26
- * Each key in the line under the panel - ctrl+o, ctrl+h - breathes until it is first pressed: fully there, a little
27
- * faint, and back, so it reads as something to press, not as text. Once every BREATH_MS, redrawn every BREATH_TICK_MS
28
- * while the panel shows and a key still breathes, at most BREATH_FADE of the way from the text's colour to the dim one.
29
- * Pressed once, it stays still for the rest of the process; the other breathes on.
30
- */
31
- export const BREATH_MS = 2400;
32
- const BREATH_TICK_MS = 100;
33
- const BREATH_FADE = 0.6;
34
- const pressed = new Set();
35
- /** A key of the line was pressed - ctrl+o, or the side panel opened: it stops breathing. */
36
- export function keyPressed(key) {
37
- pressed.add(key);
38
- }
39
- export function breathes(key) {
40
- return !pressed.has(key);
41
- }
42
- /** For tests: every key breathing again. */
43
- export function resetKeysForTests() {
44
- pressed.clear();
45
- }
46
- /** How faint a breathing key is at `now`: 0, fully there, at the start of each breath, BREATH_FADE halfway through. */
47
- export function breath(now) {
48
- const phase = (now % BREATH_MS) / BREATH_MS;
49
- return BREATH_FADE * (0.5 - 0.5 * Math.cos(2 * Math.PI * phase));
50
- }
51
- /** Tool-call progress arrives many times a second; the screen does not need to. */
52
- const REDRAW_MS = 100;
53
- /** Activity lines per running job while compact. */
54
- const COMPACT_ACTIVITY = 2;
55
- /**
56
- * A spinner rather than a disclosure arrow: a `▸` promises something opens when you pick
57
- * it. The frame follows the clock, so it moves on every redraw, including the tick.
58
- */
59
3
  const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
60
4
  export function spinner(now) {
61
5
  return SPINNER[Math.floor(now / 1000) % SPINNER.length];
62
6
  }
63
- /** A finished pulse in its module's line: its mark, number and kind - `✓ 3 regenerate`. What it regenerated is in the side panel. */
64
- function finishedItem(job, marks) {
65
- if (job.module === undefined || job.pulse === undefined)
66
- return `${finishedMark(job, marks)} ${pulseName(job)}`;
67
- return `${finishedMark(job, marks)} ${job.pulse} ${job.kind}`;
68
- }
69
- /**
70
- * What finished, a line a module - the module that finished last on top, its name, then its pulses newest first -
71
- * fitted to `width`: the newest that fit, the rest counted as earlier. The side panel lists every one.
72
- */
73
- export function finishedLines(finished, width, marks = PLAIN_MARKS) {
74
- const byModule = new Map();
75
- for (const job of [...finished].sort((a, b) => (b.finishedAt ?? 0) - (a.finishedAt ?? 0) || b.sequence - a.sequence)) {
76
- const key = job.module ?? job.kind;
77
- byModule.set(key, [...(byModule.get(key) ?? []), job]);
78
- }
79
- const pad = Math.max(...[...byModule.keys()].map((name) => visibleWidth(name)));
80
- return [...byModule].map(([name, jobs]) => {
81
- const head = ` ${name}${" ".repeat(pad - visibleWidth(name))} `;
82
- const items = jobs.map((job) => finishedItem(job, marks));
83
- const lineOf = (shown) => `${head}${items.slice(0, shown).join(" · ")}${shown < items.length ? ` · … ${items.length - shown} earlier` : ""}`;
84
- let shown = items.length;
85
- while (width !== undefined && shown > 1 && visibleWidth(lineOf(shown)) > width)
86
- shown -= 1;
87
- return lineOf(shown);
88
- });
89
- }
90
- /** A pulse that ran to its end and whose build did not pass - stopped at a gate - is not a success. */
7
+ /** A pulse that ran to its end and whose build did not pass is not a success. */
91
8
  export function didNotPass(job) {
92
9
  return job.status === "succeeded" && job.details?.passed === false;
93
10
  }
94
11
  const PLAIN_MARKS = { success: (text) => text, error: (text) => text, warning: (text) => text };
95
- /** A finished pulse's mark, in the colour of how it ended. */
96
12
  export function finishedMark(job, paint = PLAIN_MARKS) {
97
13
  if (didNotPass(job) || job.status === "failed")
98
14
  return paint.error("✗");
@@ -100,18 +16,11 @@ export function finishedMark(job, paint = PLAIN_MARKS) {
100
16
  return paint.warning("■");
101
17
  return paint.success("✓");
102
18
  }
103
- /** What a pulse cost, as its line says it: " · $0.12", or nothing when the price is not known. */
104
19
  export function pulseCost(job) {
105
20
  return job.usage.cost > 0 ? ` · $${job.usage.cost.toFixed(2)}` : "";
106
21
  }
107
- /**
108
- * Jobs whose tool draws its own progress widget, which already shows the functional spec being
109
- * built: the panel leaves it out, so it is not written twice.
110
- */
111
- const SPEC_SHOWN_BY_WIDGET = new Set(["regenerate"]);
112
- /** Spec lines shown per running job while expanded. */
113
- const EXPANDED_SPEC_LINES = 30;
114
22
  const PLAIN_DIFF = { added: (text) => text, removed: (text) => text };
23
+ /** One line saying what a spec change does: the files, the lines added and removed, and the first line it adds. */
115
24
  export function summarizeDiff(diff, style = PLAIN_DIFF) {
116
25
  const files = new Set();
117
26
  let added = 0;
@@ -136,204 +45,10 @@ export function summarizeDiff(diff, style = PLAIN_DIFF) {
136
45
  else if (line.startsWith("-")) {
137
46
  removed += 1;
138
47
  const text = line.slice(1).replace(/^- /, "").trim();
139
- if (firstRemoved === undefined && text !== "" && !/^\*\*\*.*\*\*\*$/.test(text))
48
+ if (firstRemoved === undefined && text !== "")
140
49
  firstRemoved = text;
141
50
  }
142
51
  }
143
- // What it adds, when it adds anything; what it takes out, when that is all it does.
144
52
  const what = first !== undefined ? ` · ${first}` : firstRemoved !== undefined ? ` · removes: ${firstRemoved}` : "";
145
53
  return `${[...files].join(", ")} ${style.added(`+${added}`)} ${style.removed(`−${removed}`)}${what}`;
146
54
  }
147
- /** The lines of a diff worth reading in a panel: the hunks, not the headers. */
148
- /**
149
- * A line of the panel wrapped to its width, its continuations under its text: indented as far as it is,
150
- * and past the rule of a diff box when it is in one.
151
- */
152
- export function wrapKeepingIndent(line, width) {
153
- if (visibleWidth(line) <= width)
154
- return [line];
155
- const plain = line.replace(/\x1b\[[0-9;]*m/g, "");
156
- const indent = /^(\s*(?:│ )?)/.exec(plain)?.[1] ?? "";
157
- // Inside a diff box its rule goes on down the continuations; elsewhere, blanks.
158
- const lead = indent;
159
- const inner = Math.max(10, width - indent.length);
160
- const [first = "", ...rest] = wrapTextWithAnsi(line, width);
161
- return [first, ...rest.flatMap((part) => wrapTextWithAnsi(part, inner)).map((part) => `${lead}${part}`)];
162
- }
163
- export function jobPanelLines(jobs, options = {}) {
164
- const now = options.now ?? Date.now();
165
- const expanded = options.expanded === true;
166
- // Newest first, as every list of pulses is.
167
- const inOrder = [...jobs].sort((a, b) => b.sequence - a.sequence);
168
- const running = inOrder.filter((job) => job.status === "running").sort((a, b) => b.startedAt - a.startedAt || b.sequence - a.sequence);
169
- // A module whose work is being planned: the planner is an agent, and takes a while - shown at once, as work going on.
170
- const planning = inOrder.filter((job) => job.status === "queued" && job.planning !== undefined);
171
- // Nothing running or being planned: no panel - what waits and what finished are in the side panel.
172
- if (running.length === 0 && planning.length === 0)
173
- return [];
174
- const finished = inOrder.filter((job) => (job.status === "succeeded" || job.status === "failed" || job.status === "stopped") && job.replacedBy === undefined);
175
- const lines = [];
176
- // What finished, on top - a line a module, newest first - and a rule under it: what runs now is below, by the input.
177
- if (finished.length > 0) {
178
- lines.push(...finishedLines(finished, options.width, options.marks ?? PLAIN_MARKS));
179
- lines.push(` ${"─".repeat(Math.max(10, (options.width ?? 80) - 4))}`);
180
- }
181
- for (const [index, job] of running.entries()) {
182
- if (index > 0)
183
- lines.push("");
184
- // Its module, how long and what it has cost: what it is doing follows.
185
- lines.push(`${spinner(now)} ${pulseName(job)} · ${formatDuration(job)}${pulseCost(job)}`);
186
- // What it is on now, and for how long - beside the job's own time, which is the whole of it.
187
- const onIt = job.building?.since !== undefined ? ` · ${formatElapsed(now - job.building.since)}` : "";
188
- if (job.building !== undefined && SPEC_SHOWN_BY_WIDGET.has(job.kind)) {
189
- // The module widget shows its text; the panel, how long it has taken so far.
190
- lines.push(` ${job.building.label}${onIt}`);
191
- }
192
- else if (job.building !== undefined) {
193
- const text = job.building.text.split("\n").filter((line) => line.trim() !== "");
194
- if (!expanded) {
195
- const more = text.length > 1 ? ` (+${text.length - 1} line${text.length === 2 ? "" : "s"} · ctrl+o)` : "";
196
- lines.push(` ${job.building.label}${onIt}: ${(text[0] ?? "").trim()}${more}`);
197
- }
198
- else {
199
- lines.push(` ${job.building.label}${onIt}:`);
200
- for (const line of text.slice(0, EXPANDED_SPEC_LINES))
201
- lines.push(` │ ${line}`);
202
- if (text.length > EXPANDED_SPEC_LINES)
203
- lines.push(` │ … ${text.length - EXPANDED_SPEC_LINES} more lines - ${PANEL_KEY} shows it all`);
204
- }
205
- }
206
- const activity = expanded ? job.activity : job.activity.slice(-COMPACT_ACTIVITY);
207
- for (const line of activity)
208
- lines.push(` ${line}`);
209
- if (job.now !== undefined) {
210
- const seconds = Math.max(0, Math.round((now - job.now.since) / 1000));
211
- lines.push(` … ${job.now.label} · ${seconds}s`);
212
- }
213
- }
214
- for (const job of planning) {
215
- if (lines.length > 0 && running.length > 0)
216
- lines.push("");
217
- lines.push(`${spinner(now)} ${job.module ?? job.summary} · planning the pulses · ${formatElapsed(now - job.planning.since)}`);
218
- lines.push(" working out what to regenerate and cutting it into pulses - the first starts once it is done");
219
- }
220
- // Always a line of help: how to see more here, and how to inspect the jobs in the side panel.
221
- if (options.hints === false) {
222
- // The side panel shows it all already, and says its own keys.
223
- }
224
- else {
225
- // Set apart from the trace above it.
226
- lines.push("");
227
- const key = options.keys ?? ((_key, text) => text);
228
- lines.push(` ${key("trace", `ctrl+o to ${expanded ? "collapse" : "expand"} the trace`)} · ${key("peek", `${PANEL_KEY} to peek at regen`)}`);
229
- }
230
- // A line is one row of the screen: a newline in one - an agent's words, a command - would draw past what the renderer
231
- // tracks and stay on the screen, so each part is a row of its own.
232
- return lines.flatMap((line) => line.split(/\r?\n/));
233
- }
234
- /**
235
- * The keys of the line, painted at `now`'s breath: one not pressed yet in the text's colour mixed toward the dim one, one
236
- * pressed as it is. Plain where the theme has no colours to mix.
237
- */
238
- export function breathing(theme, now) {
239
- const text = theme.colors?.text;
240
- const dim = theme.colors?.dim ?? theme.colors?.muted;
241
- const mode = theme.getColorMode?.();
242
- if (text === undefined || dim === undefined || (mode !== "truecolor" && mode !== "256color"))
243
- return (_key, line) => line;
244
- const ansi = foregroundAnsi(mixColors(text, dim, breath(now)), mode);
245
- return (key, line) => (breathes(key) ? `${ansi}${line}\x1b[39m` : line);
246
- }
247
- export function createJobPanel(list) {
248
- let ui;
249
- let tui;
250
- let mounted = false;
251
- let timer;
252
- /** The keys breathing: a redraw a tick of it, while the panel shows and a key still breathes. */
253
- let breathTimer;
254
- /** Whether the trace was open when the panel was first drawn. */
255
- let firstExpanded;
256
- let pending;
257
- const active = () => list().some((job) => job.status === "running" || job.status === "queued");
258
- const draw = () => {
259
- if (ui === undefined)
260
- return;
261
- const target = ui;
262
- try {
263
- if (active() && !mounted) {
264
- mounted = true;
265
- target.setWidget(JOB_PANEL_KEY, (next, theme) => {
266
- tui = next;
267
- return {
268
- // Laid out on every render, so ctrl+o takes effect on the very next frame.
269
- render: (width) => {
270
- const expanded = target.getToolsExpanded();
271
- // ctrl+o turns the trace open or closed: once it is not as it first was, ctrl+o was pressed.
272
- if (firstExpanded === undefined)
273
- firstExpanded = expanded;
274
- else if (expanded !== firstExpanded)
275
- keyPressed("trace");
276
- const marks = { success: (text) => theme.fg("success", text), error: (text) => theme.fg("error", text), warning: (text) => theme.fg("warning", text) };
277
- const lines = jobPanelLines(list(), { expanded, width, marks, keys: breathing(theme, Date.now()) });
278
- // Opened, nothing is cut off: every line wraps. Closed, a line each - the panel stays short.
279
- return expanded ? lines.flatMap((line) => wrapKeepingIndent(line, width)) : lines.map((line) => truncateToWidth(line, width));
280
- },
281
- invalidate() { },
282
- };
283
- }, { placement: "aboveEditor" });
284
- }
285
- else if (!active() && mounted) {
286
- mounted = false;
287
- tui = undefined;
288
- target.setWidget(JOB_PANEL_KEY, undefined);
289
- }
290
- else {
291
- tui?.requestRender();
292
- }
293
- }
294
- catch {
295
- // The panel is a convenience; losing it must not fail the work it shows.
296
- }
297
- // The elapsed time moves even when the agents are quiet - a long model call is exactly
298
- // when someone looks to see whether anything is happening.
299
- if (mounted && timer === undefined) {
300
- timer = setInterval(draw, TICK_MS);
301
- timer.unref?.();
302
- breathTimer = setInterval(() => {
303
- // Both pressed: nothing breathes, and the panel is drawn as often as before.
304
- if (!breathes("trace") && !breathes("peek")) {
305
- clearInterval(breathTimer);
306
- breathTimer = undefined;
307
- return;
308
- }
309
- tui?.requestRender();
310
- }, BREATH_TICK_MS);
311
- breathTimer.unref?.();
312
- }
313
- else if (!mounted && timer !== undefined) {
314
- clearInterval(timer);
315
- timer = undefined;
316
- if (breathTimer !== undefined)
317
- clearInterval(breathTimer);
318
- breathTimer = undefined;
319
- }
320
- };
321
- return {
322
- attach(next) {
323
- // A new session starts with pi's widgets cleared, so whatever was mounted is gone.
324
- ui = next;
325
- mounted = false;
326
- tui = undefined;
327
- draw();
328
- },
329
- refresh() {
330
- if (pending !== undefined)
331
- return;
332
- pending = setTimeout(() => {
333
- pending = undefined;
334
- draw();
335
- }, REDRAW_MS);
336
- pending.unref?.();
337
- },
338
- };
339
- }
@@ -5,6 +5,7 @@ import type { MarkedPage } from "./spec-map.ts";
5
5
  import { type SpecMapState } from "./spec-map-view.ts";
6
6
  import type { TranscriptStyle } from "./transcript-view.ts";
7
7
  import type { ButtonPaint, PointerHover } from "../pointer-hover.ts";
8
+ import type { notificationView } from "../terminal-notifications.ts";
8
9
  /**
9
10
  * The side panel: the spec and how far the code has been regenerated, beside the conversation rather than instead of
10
11
  * it.
@@ -22,6 +23,7 @@ export interface SidePanelOptions {
22
23
  earlier?: () => Job[];
23
24
  /** The regeneration's pages. Not given: the pulse list alone. */
24
25
  map?: () => MarkedPage[];
26
+ notifications?: ReturnType<typeof notificationView>;
25
27
  pointer?: PointerHover;
26
28
  tabLabel?: ButtonPaint;
27
29
  /** Rows the panel may use. */