@vincemakes/kiso-code 0.1.30 → 0.1.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vincemakes/kiso-code",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "description": "kiso CLI — the coding-agent reference product: kiso chat / kiso resume / kiso sessions.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,13 +18,13 @@
18
18
  "test": "vitest run"
19
19
  },
20
20
  "dependencies": {
21
- "@vincemakes/kiso-core": "0.1.29",
22
- "@vincemakes/kiso-evals": "0.1.29",
23
- "@vincemakes/kiso-provider-anthropic": "0.1.29",
24
- "@vincemakes/kiso-provider-openai": "0.1.29",
25
- "@vincemakes/kiso-runtime": "0.1.29",
26
- "@vincemakes/kiso-tools-node": "0.1.29",
27
- "@vincemakes/kiso-tui": "0.1.29"
21
+ "@vincemakes/kiso-core": "0.1.30",
22
+ "@vincemakes/kiso-evals": "0.1.31",
23
+ "@vincemakes/kiso-provider-anthropic": "0.1.31",
24
+ "@vincemakes/kiso-provider-openai": "0.1.31",
25
+ "@vincemakes/kiso-runtime": "0.1.31",
26
+ "@vincemakes/kiso-tools-node": "0.1.31",
27
+ "@vincemakes/kiso-tui": "0.1.32"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^26.1.2",
package/dist/body.d.ts DELETED
@@ -1,121 +0,0 @@
1
- /**
2
- * v2d — the body renderer: ONE writer for the stdout scroll region.
3
- *
4
- * The v2b/v2c body writes streamed directly (each event wrote its bytes),
5
- * so the tool lines, thinking folds, and text deltas could interleave in
6
- * the same frame — and a tool's life was scattered across several writes
7
- * (the "leak"). v2d: every event handler ONLY mutates cell state; the
8
- * Body's render loop is the only thing that writes the region.
9
- *
10
- * Frozen semantics: a completed cell prints its final form into the
11
- * scroll region ONCE and is never touched again. The ACTIVE TAIL — the
12
- * unfinished cells — renders at the region's bottom (between the frozen
13
- * area and the dock) and redraws in place, CSI 2026 wrapped. State
14
- * changes coalesce to ≥16ms frames; a 200ms heartbeat drives the running
15
- * spinners and elapsed timers. An over-height tail (rare) overflows to
16
- * freeze by completion order.
17
- *
18
- * Pipes / NO_COLOR: the Body runs in PASSTHROUGH — every mutation writes
19
- * the v2b/v2c line-mode bytes immediately, byte-for-byte (the existing
20
- * e2e guards it). The cell renderer never activates.
21
- *
22
- * The cell model is ours (UserCell / ThinkingCell / ToolCell / TextCell /
23
- * NoticeCell / raw block) — deliberately NOT pi's Component interface
24
- * shape (ADR-0040).
25
- */
26
- /** One completed-or-active body line. The renderer's only state. */
27
- export type BodyCell = {
28
- kind: "user";
29
- text: string;
30
- done: true;
31
- } | {
32
- kind: "thinking";
33
- text: string;
34
- done: boolean;
35
- } | {
36
- kind: "tool";
37
- name: string;
38
- input: string;
39
- state: "pending" | "approval" | "running" | "done";
40
- isError: boolean;
41
- resultText: string;
42
- diff: import("./diff.js").DiffLine[] | null;
43
- added: number;
44
- removed: number;
45
- startedAt: number | null;
46
- doneAt: number | null;
47
- done: boolean;
48
- } | {
49
- kind: "text";
50
- text: string;
51
- done: boolean;
52
- } | {
53
- kind: "notice";
54
- text: string;
55
- done: true;
56
- } | {
57
- kind: "raw";
58
- lines: string[];
59
- done: true;
60
- } | {
61
- kind: "terminal";
62
- label: string;
63
- line: string;
64
- done: true;
65
- };
66
- export interface BodyOptions {
67
- /** Is the cell renderer live? A color TTY with a real size — checked
68
- * per mutation (the TIOCSWINSZ can land after main constructs us). */
69
- active: () => boolean;
70
- /** The terminal height (rows) — live, for the region geometry. */
71
- height: () => number;
72
- /** The terminal width (cols) — live, for wrap estimates. */
73
- width: () => number;
74
- /** The input line's edit column — the render's cursor home. */
75
- editCol: () => number;
76
- /** The dock's redraw (the bottom three rows) — the body never writes
77
- * below the region, but the frame may call it to re-pin the chrome. */
78
- onDock?: () => void;
79
- /** The stdout writer — injectable for unit tests (default: stdout). */
80
- write?: (s: string) => void;
81
- }
82
- export declare class Body {
83
- #private;
84
- constructor(opts: BodyOptions);
85
- /** Teardown — flush a pending frame, stop the timers. */
86
- close(): void;
87
- /** The last COMPLETE thinking block, for /think. */
88
- lastThinking(): string | null;
89
- /** The last completed tool call, for /last. */
90
- lastTool(): {
91
- name: string;
92
- input: Record<string, unknown>;
93
- result: {
94
- content: string;
95
- isError: boolean;
96
- };
97
- } | null;
98
- userLine(text: string): void;
99
- thinkingAppend(text: string): void;
100
- thinkingEnd(): void;
101
- toolStart(name: string, callId: string, input: Record<string, unknown>): void;
102
- toolApproval(callId: string, diff: import("./diff.js").DiffResult | null): void;
103
- toolRunning(callId: string): void;
104
- toolSucceeded(callId: string): void;
105
- toolFailed(callId: string, error: string): void;
106
- toolResult(callId: string, result: {
107
- content: string;
108
- isError: boolean;
109
- }): void;
110
- textAppend(text: string): void;
111
- textEnd(): void;
112
- /** The terminal's status line + the rhythm gap (one blank). */
113
- terminal(label: string, statusLine: string): void;
114
- notice(text: string): void;
115
- /** A pre-rendered block (the banner, the session line, slash-command
116
- * outputs) — frozen immediately. */
117
- raw(lines: string[]): void;
118
- /** The one writer. Frozen cells print once; the tail redraws in place,
119
- * CSI 2026 wrapped; the cursor lands at the input edit column. */
120
- render(): void;
121
- }
package/dist/body.js DELETED
@@ -1,491 +0,0 @@
1
- /**
2
- * v2d — the body renderer: ONE writer for the stdout scroll region.
3
- *
4
- * The v2b/v2c body writes streamed directly (each event wrote its bytes),
5
- * so the tool lines, thinking folds, and text deltas could interleave in
6
- * the same frame — and a tool's life was scattered across several writes
7
- * (the "leak"). v2d: every event handler ONLY mutates cell state; the
8
- * Body's render loop is the only thing that writes the region.
9
- *
10
- * Frozen semantics: a completed cell prints its final form into the
11
- * scroll region ONCE and is never touched again. The ACTIVE TAIL — the
12
- * unfinished cells — renders at the region's bottom (between the frozen
13
- * area and the dock) and redraws in place, CSI 2026 wrapped. State
14
- * changes coalesce to ≥16ms frames; a 200ms heartbeat drives the running
15
- * spinners and elapsed timers. An over-height tail (rare) overflows to
16
- * freeze by completion order.
17
- *
18
- * Pipes / NO_COLOR: the Body runs in PASSTHROUGH — every mutation writes
19
- * the v2b/v2c line-mode bytes immediately, byte-for-byte (the existing
20
- * e2e guards it). The cell renderer never activates.
21
- *
22
- * The cell model is ours (UserCell / ThinkingCell / ToolCell / TextCell /
23
- * NoticeCell / raw block) — deliberately NOT pi's Component interface
24
- * shape (ADR-0040).
25
- */
26
- import { truncateDiff } from "./diff.js";
27
- import { displayWidth } from "./editor.js";
28
- import { escapeTerminal, foldResult, foldThinking, palette, renderTerminalGap, renderToolSummary, } from "./render.js";
29
- /** The spinner glyphs, cycled by the heartbeat (v3 §05 — the working family). */
30
- const SPINNER = ["▖", "▘", "▝", "▗"];
31
- const TOOL_SUMMARY_MAX = 60; // the tool line's parameter summary, chars
32
- const FRAME_MS = 16; // state changes coalesce to ≥16ms frames
33
- const HEARTBEAT_MS = 200; // spinner / elapsed cadence
34
- export class Body {
35
- #active;
36
- #opts;
37
- #cells = [];
38
- #nextFrozen = 0; // index of the first not-yet-printed cell
39
- #frozenRows = 0; // the frozen area's rows filled WITHOUT scrolling (then the real LFs take over)
40
- #oldTailTop = 0; // the tail's previous first row — for the clear pass
41
- #frameTimer = null;
42
- #heartbeat = null;
43
- #dirty = false;
44
- #spinnerI = 0;
45
- #lastThinking = null;
46
- #lastTool = null;
47
- #pendingCalls = new Map();
48
- #pipeBuf = ""; // the passthrough's thinking buffer — the cell model needs no buffer
49
- #toolCells = new Map(); // callId → cell index (parallel tools)
50
- #write;
51
- constructor(opts) {
52
- this.#opts = opts;
53
- this.#write = opts.write ?? ((s) => process.stdout.write(s));
54
- this.#active = opts.active();
55
- if (this.#isActive()) {
56
- this.#heartbeat = setInterval(() => {
57
- // #14/#15: the idle heartbeat PAINTS NOTHING unless an
58
- // ANIMATION advances — only a RUNNING tool's glyph/elapsed
59
- // changes between beats. The #14 fix skipped an all-frozen
60
- // body; #15 widened the skip to ANY no-change body: a cell
61
- // that stays unfinished without animating (an unclosed text
62
- // or thinking block) would otherwise re-paint the tail AND
63
- // the dock every 200ms with zero change — the short-session
64
- // leak (measured: 51KB / 46 beats after the recap, LF=0).
65
- if (!this.#cells.some((c) => c.kind === "tool" && c.state === "running"))
66
- return;
67
- this.#spinnerI = (this.#spinnerI + 1) % SPINNER.length;
68
- this.#dirty = true; // the running cells' glyph/elapsed advance
69
- this.#scheduleFrame();
70
- }, HEARTBEAT_MS);
71
- this.#heartbeat.unref();
72
- }
73
- }
74
- /** Live re-check — a TTY whose size lands after construction flips in. */
75
- #isActive() {
76
- this.#active = this.#opts.active();
77
- return this.#active;
78
- }
79
- /** Teardown — flush a pending frame, stop the timers. */
80
- close() {
81
- if (this.#frameTimer !== null) {
82
- clearTimeout(this.#frameTimer);
83
- this.#frameTimer = null;
84
- }
85
- if (this.#heartbeat !== null) {
86
- clearInterval(this.#heartbeat);
87
- this.#heartbeat = null;
88
- }
89
- if (this.#dirty)
90
- this.render();
91
- }
92
- /** The last COMPLETE thinking block, for /think. */
93
- lastThinking() {
94
- return this.#lastThinking;
95
- }
96
- /** The last completed tool call, for /last. */
97
- lastTool() {
98
- return this.#lastTool;
99
- }
100
- // ---- mutations (the ONLY way the CLI touches the body) ----
101
- userLine(text) {
102
- if (!this.#isActive()) {
103
- this.#closeOpenThinking();
104
- this.#closeOpenText();
105
- const p = palette();
106
- this.#write(`${p.blue}you> ${escapeTerminal(text)}${p.reset}\n`);
107
- return;
108
- }
109
- this.#closeOpenThinking();
110
- this.#closeOpenText();
111
- this.#cells.push({ kind: "user", text, done: true });
112
- this.#mark();
113
- }
114
- thinkingAppend(text) {
115
- if (!this.#isActive()) {
116
- this.#pipeBuf += text; // buffered; the fold prints at the block's end
117
- return;
118
- }
119
- const last = this.#cells[this.#cells.length - 1];
120
- if (last !== undefined && last.kind === "thinking" && !last.done) {
121
- last.text += text;
122
- }
123
- else {
124
- this.#cells.push({ kind: "thinking", text, done: false });
125
- }
126
- this.#mark();
127
- }
128
- thinkingEnd() {
129
- const last = this.#cells[this.#cells.length - 1];
130
- if (last !== undefined && last.kind === "thinking" && !last.done) {
131
- last.done = true;
132
- this.#lastThinking = last.text;
133
- if (!this.#isActive())
134
- process.stdout.write(foldThinking(last.text));
135
- this.#mark();
136
- }
137
- }
138
- toolStart(name, callId, input) {
139
- const summary = JSON.stringify(input).slice(0, TOOL_SUMMARY_MAX);
140
- // Registered BEFORE the passthrough branch — /last and the pipe
141
- // summary need the call on BOTH paths.
142
- this.#pendingCalls.set(callId, { name, input, result: { content: "", isError: false } });
143
- if (!this.#isActive()) {
144
- this.#closeOpenThinking();
145
- this.#closeOpenText();
146
- process.stdout.write(`→ ${escapeTerminal(name)}(${escapeTerminal(JSON.stringify(input).slice(0, 200))})\n`);
147
- return;
148
- }
149
- this.#toolCells.set(callId, this.#cells.length);
150
- this.#cells.push({ kind: "tool", name, input: summary, state: "pending", isError: false, resultText: "", diff: null, added: 0, removed: 0, startedAt: null, doneAt: null, done: false });
151
- this.#mark();
152
- }
153
- toolApproval(callId, diff) {
154
- if (!this.#isActive())
155
- return;
156
- const cell = this.#toolCell(callId);
157
- if (cell !== null && cell.kind === "tool" && !cell.done) {
158
- cell.state = "approval";
159
- // v2e: the mini-diff renders BELOW the tool line at the approval
160
- // moment — the human sees the change before deciding. Auto-allowed
161
- // tools pass null (nobody is looking — no diff, no cost).
162
- cell.diff = diff === null ? null : truncateDiff(diff.lines);
163
- cell.added = diff?.added ?? 0;
164
- cell.removed = diff?.removed ?? 0;
165
- }
166
- this.#mark();
167
- }
168
- toolRunning(callId) {
169
- if (!this.#isActive()) {
170
- const p = palette();
171
- process.stdout.write(`${p.dim} running…${p.reset}\n`);
172
- return;
173
- }
174
- const cell = this.#toolCell(callId);
175
- if (cell !== null && cell.kind === "tool" && !cell.done) {
176
- cell.state = "running";
177
- cell.startedAt = Date.now();
178
- }
179
- this.#mark();
180
- }
181
- toolSucceeded(callId) {
182
- if (!this.#isActive()) {
183
- process.stdout.write(` ok\n`);
184
- }
185
- }
186
- toolFailed(callId, error) {
187
- if (!this.#isActive()) {
188
- const p = palette();
189
- process.stdout.write(`${p.red} failed: ${escapeTerminal(error.slice(0, 160))}${p.reset}\n`);
190
- }
191
- }
192
- toolResult(callId, result) {
193
- const call = this.#pendingCalls.get(callId);
194
- if (call !== undefined) {
195
- call.result = result;
196
- this.#lastTool = { name: call.name, input: call.input, result };
197
- this.#pendingCalls.delete(callId);
198
- }
199
- if (!this.#isActive()) {
200
- // The v2b/v2c pipe bytes: the summary line + the [result] line.
201
- const p = palette();
202
- process.stdout.write(`${renderToolSummary(call?.name ?? "?", call?.input ?? {}, result)}\n` +
203
- `${p.dim}${result.isError ? p.red : p.dim} [result${result.isError ? " ✗" : ""}] ${foldResult(result.content)}${p.reset}\n`);
204
- return;
205
- }
206
- const cell = this.#toolCell(callId);
207
- this.#toolCells.delete(callId);
208
- if (cell !== null && cell.kind === "tool" && !cell.done) {
209
- cell.state = "done";
210
- cell.isError = result.isError;
211
- cell.resultText = result.content;
212
- cell.doneAt = Date.now();
213
- cell.done = true;
214
- }
215
- this.#mark();
216
- }
217
- textAppend(text) {
218
- if (!this.#isActive()) {
219
- this.#closeOpenThinking();
220
- this.#closeOpenText();
221
- process.stdout.write(escapeTerminal(text));
222
- return;
223
- }
224
- const last = this.#cells[this.#cells.length - 1];
225
- if (last !== undefined && last.kind === "text" && !last.done) {
226
- last.text += text;
227
- }
228
- else {
229
- this.#closeOpenThinking();
230
- this.#closeOpenText();
231
- this.#cells.push({ kind: "text", text, done: false });
232
- }
233
- this.#mark();
234
- }
235
- textEnd() {
236
- if (!this.#isActive()) {
237
- process.stdout.write("\n");
238
- return;
239
- }
240
- const last = this.#cells[this.#cells.length - 1];
241
- if (last !== undefined && last.kind === "text" && !last.done)
242
- last.done = true;
243
- this.#mark();
244
- }
245
- /** The terminal's status line + the rhythm gap (one blank). */
246
- terminal(label, statusLine) {
247
- if (!this.#isActive()) {
248
- this.#closeOpenThinking();
249
- this.#closeOpenText();
250
- // the v2c bytes: the terminal label (\ndone\n) + the status gap.
251
- process.stdout.write(label + renderTerminalGap(statusLine));
252
- return;
253
- }
254
- this.#closeOpenThinking();
255
- this.#closeOpenText();
256
- this.#cells.push({ kind: "terminal", label: label.trim(), line: statusLine, done: true });
257
- this.#mark();
258
- }
259
- notice(text) {
260
- if (!this.#isActive()) {
261
- this.#closeOpenThinking();
262
- this.#closeOpenText();
263
- process.stdout.write(`${text}\n`);
264
- return;
265
- }
266
- this.#closeOpenThinking();
267
- this.#closeOpenText();
268
- this.#cells.push({ kind: "notice", text, done: true });
269
- this.#mark();
270
- }
271
- /** A pre-rendered block (the banner, the session line, slash-command
272
- * outputs) — frozen immediately. */
273
- raw(lines) {
274
- if (!this.#isActive()) {
275
- this.#closeOpenThinking();
276
- this.#closeOpenText();
277
- for (const line of lines)
278
- process.stdout.write(`${line}\n`);
279
- return;
280
- }
281
- this.#closeOpenThinking();
282
- this.#closeOpenText();
283
- this.#cells.push({ kind: "raw", lines, done: true });
284
- this.#mark();
285
- }
286
- // ---- rendering ----
287
- #mark() {
288
- if (!this.#isActive())
289
- return;
290
- this.#dirty = true;
291
- this.#scheduleFrame();
292
- }
293
- #scheduleFrame() {
294
- if (this.#frameTimer !== null)
295
- return;
296
- this.#frameTimer = setTimeout(() => {
297
- this.#frameTimer = null;
298
- if (this.#dirty) {
299
- this.#dirty = false;
300
- this.render();
301
- }
302
- }, FRAME_MS);
303
- this.#frameTimer.unref();
304
- }
305
- /** The one writer. Frozen cells print once; the tail redraws in place,
306
- * CSI 2026 wrapped; the cursor lands at the input edit column. */
307
- render() {
308
- if (!this.#isActive())
309
- return;
310
- const H = this.#opts.height();
311
- const W = this.#opts.width();
312
- if (H < 4)
313
- return;
314
- const out = [];
315
- // #13 (P1), v2d-B: NO DECSTBM — overflow scrolls with a REAL LF at
316
- // the screen's last row, so the frozen lines enter the terminal's
317
- // NATIVE scrollback deterministically (region-scrolled lines are
318
- // terminal-dependent; some terminals drop them — the measured v2d-A
319
- // defect). The body fills from the top without scrolling; once full,
320
- // every new frozen line scrolls the whole screen (\x1b[H;1H\n — the
321
- // top line leaves into the scrollback) and lands at the body's
322
- // bottom row, just above the active tail. The dock is redrawn after.
323
- // The tail (the remaining ACTIVE cells) and its geometry — computed
324
- // FIRST from the final nextFrozen, so the frozen cells are NOT in it
325
- // (a stale tail would re-draw them — the double-render).
326
- let nextFrozen = this.#nextFrozen;
327
- while (nextFrozen < this.#cells.length && this.#cells[nextFrozen].done)
328
- nextFrozen += 1;
329
- const tail = this.#cells.slice(nextFrozen);
330
- const tailHeight = tail.reduce((n, c) => n + this.#cellHeight(c, W), 0);
331
- const tailTop = Math.max(1, H - 3 - tailHeight); // v3 §03: 4 dock rows below
332
- const writeRow = Math.max(1, tailTop - 1); // the frozen area's bottom row
333
- let scrolled = 0;
334
- for (let i = this.#nextFrozen; i < nextFrozen; i += 1) {
335
- for (const line of this.#cellLines(this.#cells[i], W)) {
336
- if (this.#frozenRows < writeRow) {
337
- this.#frozenRows += 1;
338
- out.push(`\x1b[${this.#frozenRows};1H\x1b[0K${line}`);
339
- }
340
- else {
341
- out.push(`\x1b[${H};1H\n`); // the REAL LF — the whole screen scrolls
342
- out.push(`\x1b[${writeRow};1H\x1b[0K${line}`);
343
- scrolled += 1;
344
- }
345
- }
346
- this.#nextFrozen += 1;
347
- }
348
- // 2. the active tail — clear its old area (shifted up by the freeze
349
- // scrolls) and the current area, draw the cells at the body's bottom.
350
- out.push("\x1b[?2026h");
351
- const clearFrom = Math.min(this.#oldTailTop === 0 ? tailTop : this.#oldTailTop - scrolled, tailTop);
352
- for (let row = clearFrom; row <= H - 4; row += 1) {
353
- out.push(`\x1b[${row};1H\x1b[0K`);
354
- }
355
- let row = tailTop;
356
- for (const cell of tail) {
357
- for (const line of this.#cellLines(cell, W)) {
358
- out.push(`\x1b[${row};1H${line}`);
359
- row += 1;
360
- }
361
- }
362
- // 3. the cursor home — the input line's edit column.
363
- out.push(`\x1b[${H};${this.#opts.editCol()}H`);
364
- out.push("\x1b[?2026l");
365
- this.#write(out.join(""));
366
- // 4. the dock rows — the freeze scrolls shifted them; redraw (the
367
- // dock's own redraw re-pins the cursor at the edit position).
368
- this.#opts.onDock?.();
369
- }
370
- // ---- cell → lines ----
371
- #cellLines(cell, W) {
372
- const p = palette();
373
- switch (cell.kind) {
374
- case "user":
375
- // v3 §02: the user message is a SGR BACKGROUND block, no
376
- // prefix — every line carries the block's background
377
- // (multi-line whole; resize-safe). Pipes stay plain.
378
- return cell.text.split("\n").map((l) => `${p.bg}${escapeTerminal(l)}${p.reset}`);
379
- case "thinking": {
380
- const block = cell.text;
381
- const trimmed = escapeTerminal(block.trim());
382
- if (trimmed.length <= 100)
383
- return [`${p.dim}…${trimmed}${p.reset}`];
384
- return [`${p.dim}…${trimmed.slice(0, 100)} (${block.length} chars · /think)${p.reset}`];
385
- }
386
- case "tool": {
387
- const name = escapeTerminal(cell.name);
388
- const summary = escapeTerminal(cell.input);
389
- if (cell.state === "done") {
390
- const elapsed = cell.startedAt !== null && cell.doneAt !== null ? ((cell.doneAt - cell.startedAt) / 1000).toFixed(1) : "?";
391
- if (cell.isError) {
392
- const err = escapeTerminal(cell.resultText.split("\n")[0].slice(0, 60));
393
- return [`${p.red}✗ ${name} (${err}, ${elapsed}s)${p.reset}`];
394
- }
395
- const delta = cell.added + cell.removed > 0 ? `, +${cell.added} -${cell.removed}` : "";
396
- return [`${p.blue}✓ ${name}${p.reset} (${summary}${delta}, ${elapsed}s)`];
397
- }
398
- if (cell.state === "approval") {
399
- const lines = [`→ ${name} ${summary} ${p.blue}⏸${p.reset}`];
400
- // v2e: the mini-diff — ▎ blue edge (the brick motif), - red /
401
- // + green / context dim; NO_COLOR keeps the ± prefixes plain.
402
- if (cell.diff !== null) {
403
- for (const d of cell.diff) {
404
- const body = d.kind === "-"
405
- ? `${p.red}- ${escapeTerminal(d.text)}${p.reset}`
406
- : d.kind === "+"
407
- ? `${p.green}+ ${escapeTerminal(d.text)}${p.reset}`
408
- : `${p.dim} ${escapeTerminal(d.text)}${p.reset}`;
409
- lines.push(`${p.blue}▎${p.reset}${body}`);
410
- }
411
- }
412
- return lines;
413
- }
414
- if (cell.state === "running") {
415
- const elapsed = cell.startedAt !== null ? Math.max(1, Math.round((Date.now() - cell.startedAt) / 1000)) : 1;
416
- return [`→ ${name} ${summary} ${p.blue}${SPINNER[this.#spinnerI % SPINNER.length]}${p.reset} ${elapsed}s`];
417
- }
418
- return [`→ ${name} ${summary}`];
419
- }
420
- case "text": {
421
- const text = escapeTerminal(cell.text);
422
- const wrapped = this.#wrap(text, W);
423
- return wrapped.length > 0 ? wrapped : [""];
424
- }
425
- case "notice":
426
- return [escapeTerminal(cell.text)];
427
- case "raw":
428
- return cell.lines.map((l) => escapeTerminal(l));
429
- case "terminal":
430
- // the honest label (done / aborted / error) + the status + the
431
- // rhythm gap blank
432
- return [cell.label, cell.line, ""];
433
- }
434
- }
435
- #cellHeight(cell, W) {
436
- const lines = this.#cellLines(cell, W);
437
- return Math.max(1, lines.length);
438
- }
439
- /** Wrap a body text by display width (the terminal's own wrapping,
440
- * approximated — documented; the region clamp keeps the dock safe). */
441
- #wrap(text, W) {
442
- if (W < 4)
443
- return [text];
444
- const out = [];
445
- let current = "";
446
- let width = 0;
447
- for (const ch of text) {
448
- const cw = displayWidth(ch);
449
- if (ch === "\n" || width + cw > W) {
450
- out.push(current);
451
- current = "";
452
- width = 0;
453
- if (ch === "\n")
454
- continue;
455
- }
456
- current += ch;
457
- width += cw;
458
- }
459
- out.push(current);
460
- return out;
461
- }
462
- #toolCell(callId) {
463
- const i = this.#toolCells.get(callId);
464
- return i === undefined ? null : (this.#cells[i] ?? null);
465
- }
466
- /** Close an open TEXT cell when a new cell starts — the runtime emits
467
- * no text_end (it is an adapter-level event), so the stream's next
468
- * cell is the close signal; without it the freeze blocks behind the
469
- * open text and everything after it re-renders in the tail forever
470
- * (the #13 flood reproduced the overwrite). */
471
- #closeOpenText() {
472
- const last = this.#cells[this.#cells.length - 1];
473
- if (last !== undefined && last.kind === "text" && !last.done)
474
- last.done = true;
475
- }
476
- /** Close an open thinking cell when a new cell starts (the block's
477
- * fold freezes at the transition). */
478
- #closeOpenThinking() {
479
- if (!this.#isActive() && this.#pipeBuf !== "") {
480
- this.#lastThinking = this.#pipeBuf;
481
- process.stdout.write(foldThinking(this.#pipeBuf));
482
- this.#pipeBuf = "";
483
- return;
484
- }
485
- const last = this.#cells[this.#cells.length - 1];
486
- if (last !== undefined && last.kind === "thinking" && !last.done) {
487
- last.done = true;
488
- this.#lastThinking = last.text;
489
- }
490
- }
491
- }