@songtonyli/dsh-cli 0.1.24 → 0.1.26

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.
@@ -45,6 +45,130 @@ var AuthorizationDeclinedError = class extends AuthorizationError {
45
45
  }
46
46
  };
47
47
  //#endregion
48
+ //#region lib/types/alt-screen.js
49
+ /**
50
+ * The terminal's alternate screen, which a surface that owns the whole
51
+ * terminal draws on.
52
+ *
53
+ * A terminal keeps two screens. The main one is the conversation's own
54
+ * scrollback, which every line the application has ever printed belongs to;
55
+ * the alternate one is a scratch screen exactly `terminal.rows` tall with no
56
+ * scrollback at all, the screen a pager or an editor runs on. Switching to it
57
+ * saves the main screen, and switching back restores it unchanged: the
58
+ * surface drawn here cannot rewrite a line of the conversation, cannot scroll
59
+ * one out of the viewport, and leaves nothing of itself behind when it goes -
60
+ * none of which an overlay drawn over the conversation can promise, because
61
+ * its rows are the main screen's rows and become its scrollback.
62
+ *
63
+ * The screen is addressed by absolute row: every line is drawn where it
64
+ * belongs rather than by printing downward, so nothing here ever scrolls the
65
+ * terminal. Each paint writes only the rows whose text changed since the last
66
+ * one, wrapped in one synchronized-output pair so the terminal shows whole
67
+ * frames.
68
+ * @module @deepseek-ai/dsh-tui-app/alt-screen
69
+ */
70
+ /** Switch to the alternate screen, saving the main one. */
71
+ const ENTER = "\x1B[?1049h";
72
+ /** Switch back to the main screen, restoring it as it was. */
73
+ const LEAVE = "\x1B[?1049l";
74
+ /** Clear the whole screen. */
75
+ const CLEAR = "\x1B[2J";
76
+ /** Clear the row the cursor is on. */
77
+ const CLEAR_ROW = "\x1B[2K";
78
+ /** Stop drawing the terminal's own caret. */
79
+ const HIDE_CARET = "\x1B[?25l";
80
+ /** Draw the terminal's own caret again. */
81
+ const SHOW_CARET = "\x1B[?25h";
82
+ /**
83
+ * Stop wrapping at the right margin, so a line that fills the last row cannot
84
+ * scroll the screen.
85
+ */
86
+ const NO_AUTOWRAP = "\x1B[?7l";
87
+ /** Wrap at the right margin again, which is what the conversation needs. */
88
+ const AUTOWRAP = "\x1B[?7h";
89
+ /** Hold the frame back until every row of it has been written. */
90
+ const BEGIN_FRAME = "\x1B[?2026h";
91
+ /** Show the frame. */
92
+ const END_FRAME = "\x1B[?2026l";
93
+ /**
94
+ * The alternate screen as one surface: enter it, paint frames on it, leave it.
95
+ *
96
+ * Entering and leaving are balanced and idempotent, so a caller that takes the
97
+ * terminal twice, or gives it back twice, still leaves the terminal on the
98
+ * screen it started on.
99
+ */
100
+ var AlternateScreen = class {
101
+ terminal;
102
+ /** The rows the last paint left on the screen, which the next one is drawn against. */
103
+ drawn = [];
104
+ /** The terminal width the drawn rows were written at, so a resize repaints them all. */
105
+ drawnWidth = 0;
106
+ entered = false;
107
+ /**
108
+ * @param terminal - the terminal whose two screens these are.
109
+ */
110
+ constructor(terminal) {
111
+ this.terminal = terminal;
112
+ }
113
+ /** Whether the alternate screen is the one the terminal is showing. */
114
+ get active() {
115
+ return this.entered;
116
+ }
117
+ /**
118
+ * Take the terminal, saving the main screen and clearing this one.
119
+ *
120
+ * The caret is the surface's to draw, and none of the surfaces here draw
121
+ * one, so it is hidden for as long as this screen is up.
122
+ */
123
+ enter() {
124
+ if (this.entered) return;
125
+ this.entered = true;
126
+ this.drawn = [];
127
+ this.drawnWidth = this.terminal.columns;
128
+ this.terminal.write(`${ENTER}${NO_AUTOWRAP}${HIDE_CARET}${CLEAR}`);
129
+ }
130
+ /**
131
+ * Draw one frame.
132
+ * @param lines - the frame, one entry per screen row from the top; rows the
133
+ * frame no longer has are cleared. Nothing is written while the main screen
134
+ * is the one the terminal shows.
135
+ */
136
+ paint(lines) {
137
+ if (!this.entered) return;
138
+ if (this.terminal.columns !== this.drawnWidth) {
139
+ this.drawn = [];
140
+ this.drawnWidth = this.terminal.columns;
141
+ }
142
+ let frame = BEGIN_FRAME;
143
+ for (const [row, line] of lines.entries()) {
144
+ if (this.drawn[row] === line) continue;
145
+ frame += `${cursorTo(row)}${CLEAR_ROW}${line}`;
146
+ }
147
+ for (let row = lines.length; row < this.drawn.length; row += 1) frame += `${cursorTo(row)}${CLEAR_ROW}`;
148
+ this.drawn = [...lines];
149
+ frame += END_FRAME;
150
+ this.terminal.write(frame);
151
+ }
152
+ /**
153
+ * Give the terminal back, which restores the main screen exactly as it was
154
+ * when {@link AlternateScreen.enter} saved it.
155
+ */
156
+ leave() {
157
+ if (!this.entered) return;
158
+ this.entered = false;
159
+ this.drawn = [];
160
+ this.terminal.write(`${BEGIN_FRAME}${LEAVE}${AUTOWRAP}${SHOW_CARET}${END_FRAME}`);
161
+ }
162
+ };
163
+ /**
164
+ * Move the cursor to the first column of one screen row.
165
+ * @param row - the row, counted from 0 at the top of the screen.
166
+ * @returns the escape sequence that puts the cursor there.
167
+ */
168
+ function cursorTo(row) {
169
+ return `\u001b[${String(row + 1)};1H`;
170
+ }
171
+ //#endregion
48
172
  //#region lib/types/attach.js
49
173
  /**
50
174
  * `/attach`: read a local file and admit it into the attachment store as the
@@ -2021,8 +2145,7 @@ function paintLine(text, runs, restore) {
2021
2145
  //#region lib/types/motion.js
2022
2146
  /**
2023
2147
  * Chrome motion: the brief lift a docked surface draws when the keyboard
2024
- * lands on it or the mark it holds moves, and the reveal the full-screen
2025
- * reader grows and shrinks through.
2148
+ * lands on it or the mark it holds moves.
2026
2149
  *
2027
2150
  * Pure. A motion is a clock plus a three-level intensity ramp, so nothing
2028
2151
  * here queries a terminal, builds a color, or arms a timer: the application
@@ -2240,6 +2363,18 @@ function editorTheme(palette) {
2240
2363
  * content wraps at and is prepended after any fade recoloring, so the fade
2241
2364
  * keeps matching the block's own text and the gutter's styling never enters
2242
2365
  * that match.
2366
+ *
2367
+ * pi-tui asks every component for its lines on every frame, and the
2368
+ * transcript is never pruned, so each block keeps the lines it last drew
2369
+ * ({@link LastDrawn}) and hands them back until its width, fold, focus, or
2370
+ * content changes - a settled block costs a frame one key comparison. What
2371
+ * changes per frame - a fade's level, the focus mark's lift - is drawn over
2372
+ * those kept lines. Fenced code is the expensive part of a reply, so a reply
2373
+ * also keeps the coloured fences of its last Markdown parse
2374
+ * ({@link FenceMemo}): a stream delta re-parses the whole message but colours
2375
+ * only the fence that changed. `invalidate()` drops all of it, which the
2376
+ * application calls on every block when a grammar lands; a new terminal width
2377
+ * needs no call, because it misses every key on its own.
2243
2378
  * @module @deepseek-ai/dsh-tui-app/blocks
2244
2379
  */
2245
2380
  /** Columns the focus gutter takes from the width a block's content wraps at. */
@@ -2301,6 +2436,85 @@ function withGutter(palette, lines, focused, level) {
2301
2436
  function markedRange(section, marker) {
2302
2437
  return section.to > section.from ? section : marker;
2303
2438
  }
2439
+ /**
2440
+ * The last drawing of one block and the key it was built for. One entry, so a
2441
+ * block holds exactly one drawing however many frames ask for it, and a key
2442
+ * that names every input the drawing depends on is what keeps it current.
2443
+ */
2444
+ var LastDrawn = class {
2445
+ entry;
2446
+ /**
2447
+ * The drawing for `key`, built once per key change.
2448
+ * @param key - every input the drawing depends on, joined.
2449
+ * @param build - builds the drawing when the key differs from the last one.
2450
+ * @returns the same value for the same key until {@link LastDrawn.clear}.
2451
+ */
2452
+ get(key, build) {
2453
+ if (this.entry?.key !== key) this.entry = {
2454
+ key,
2455
+ value: build()
2456
+ };
2457
+ return this.entry.value;
2458
+ }
2459
+ /** Forget the drawing, so the next frame builds it again. */
2460
+ clear() {
2461
+ this.entry = void 0;
2462
+ }
2463
+ };
2464
+ /**
2465
+ * The coloured fences of one reply, kept from one Markdown parse to the next.
2466
+ *
2467
+ * pi-tui's `Markdown` re-parses the whole message whenever its text changes
2468
+ * and asks the theme to colour every fence it finds, so a streaming reply
2469
+ * that already carries three fences would colour all three on every delta.
2470
+ * This keeps what the last parse asked for: a fence that is asked for again
2471
+ * is answered from memory, and a fence the last parse did not ask for is
2472
+ * dropped at the next {@link FenceMemo.settle}, so the memo never holds more
2473
+ * than the reply shows. A fence the highlighter left plain is not kept, so
2474
+ * the grammar that lands is asked for it.
2475
+ */
2476
+ var FenceMemo = class {
2477
+ inner;
2478
+ /** What the previous parse asked for. */
2479
+ previous = /* @__PURE__ */ new Map();
2480
+ /** What the parse in progress has asked for so far. */
2481
+ current = /* @__PURE__ */ new Map();
2482
+ /** Whether a parse asked for anything since the last settle. */
2483
+ asked = false;
2484
+ /** @param inner - the highlighter that colours a fence this memo does not hold. */
2485
+ constructor(inner) {
2486
+ this.inner = inner;
2487
+ }
2488
+ lines(code, lang) {
2489
+ this.asked = true;
2490
+ const key = `${String(lang)} ${code}`;
2491
+ const known = this.current.get(key) ?? this.previous.get(key);
2492
+ if (known !== void 0) {
2493
+ this.current.set(key, known);
2494
+ return known;
2495
+ }
2496
+ const coloured = this.inner.lines(code, lang);
2497
+ if (coloured !== void 0) this.current.set(key, coloured);
2498
+ return coloured;
2499
+ }
2500
+ /**
2501
+ * Close the parse that just ran: what it asked for is kept for the next
2502
+ * one, and what only the parse before it asked for goes. A render that
2503
+ * parsed nothing - `Markdown` served its own cache - changes nothing.
2504
+ */
2505
+ settle() {
2506
+ if (!this.asked) return;
2507
+ this.asked = false;
2508
+ this.previous = this.current;
2509
+ this.current = /* @__PURE__ */ new Map();
2510
+ }
2511
+ /** Forget every fence, so the next parse colours each one afresh. */
2512
+ clear() {
2513
+ this.previous = /* @__PURE__ */ new Map();
2514
+ this.current = /* @__PURE__ */ new Map();
2515
+ this.asked = false;
2516
+ }
2517
+ };
2304
2518
  /** A prompt the user submitted, drawn with a leading `›`. */
2305
2519
  var UserBlock = class {
2306
2520
  theme;
@@ -2312,6 +2526,8 @@ var UserBlock = class {
2312
2526
  highlight;
2313
2527
  /** How far above its settled accent the focus mark is drawn right now. */
2314
2528
  highlightLevel = 0;
2529
+ /** The wrapped prompt, by width and whether the gutter narrowed it. */
2530
+ drawn = new LastDrawn();
2315
2531
  constructor(theme, text, turn) {
2316
2532
  this.theme = theme;
2317
2533
  this.text = text;
@@ -2335,11 +2551,15 @@ var UserBlock = class {
2335
2551
  this.highlight = part;
2336
2552
  this.highlightLevel = level;
2337
2553
  }
2338
- invalidate() {}
2554
+ invalidate() {
2555
+ this.drawn.clear();
2556
+ }
2339
2557
  render(width) {
2340
2558
  const palette = this.theme.palette;
2341
2559
  const { marked, content: inner } = focusFrame(width, this.highlight);
2342
- const lines = ["", ...wrapTextWithAnsi(this.text, Math.max(1, inner - 2)).map((line, index) => `${palette.accent(index === 0 ? "›" : " ")} ${palette.bold(line)}`)];
2560
+ const lines = this.drawn.get(`${String(width)}:${String(marked)}`, () => {
2561
+ return ["", ...wrapTextWithAnsi(this.text, Math.max(1, inner - 2)).map((line, index) => `${palette.accent(index === 0 ? "›" : " ")} ${palette.bold(line)}`)];
2562
+ });
2343
2563
  return marked ? withGutter(palette, lines, () => true, this.highlightLevel) : lines;
2344
2564
  }
2345
2565
  };
@@ -2383,6 +2603,8 @@ var ContextBlock = class {
2383
2603
  /** How far above its settled accent the focus mark is drawn right now. */
2384
2604
  highlightLevel = 0;
2385
2605
  expanded = false;
2606
+ /** The wrapped block, by width, fold, and whether the gutter narrowed it. */
2607
+ drawn = new LastDrawn();
2386
2608
  /**
2387
2609
  * @param theme - the palette the glyph and title are styled with.
2388
2610
  * @param title - form and producer, a notice summary, or `system prompt`.
@@ -2424,10 +2646,34 @@ var ContextBlock = class {
2424
2646
  setExpanded(expanded) {
2425
2647
  this.expanded = expanded;
2426
2648
  }
2427
- invalidate() {}
2649
+ invalidate() {
2650
+ this.drawn.clear();
2651
+ }
2428
2652
  render(width) {
2429
2653
  const palette = this.theme.palette;
2430
2654
  const { marked, content: inner } = focusFrame(width, this.highlight);
2655
+ const { lines, head, ranges, cut, kept, body } = this.drawn.get(`${String(width)}:${String(marked)}:${String(this.expanded)}`, () => this.layout(inner, marked));
2656
+ if (this.highlight === void 0) return lines;
2657
+ const section = ranges[this.highlight];
2658
+ if (section === void 0) return withGutter(palette, lines, () => true, this.highlightLevel);
2659
+ const drawn = Math.min(section.to, cut ? kept : body);
2660
+ const held = markedRange({
2661
+ from: head + section.from,
2662
+ to: head + drawn
2663
+ }, {
2664
+ from: head + (cut ? kept : body),
2665
+ to: lines.length
2666
+ });
2667
+ return withGutter(palette, lines, (line) => line >= held.from && line < held.to, this.highlightLevel);
2668
+ }
2669
+ /**
2670
+ * Wrap the title and every body row, then fold the body.
2671
+ * @param inner - the columns the block's own rows lay out in.
2672
+ * @param marked - whether the block draws the focus gutter, which names the fold key.
2673
+ * @returns the lines and where the head, each part, and the fold fall in them.
2674
+ */
2675
+ layout(inner, marked) {
2676
+ const palette = this.theme.palette;
2431
2677
  const textWidth = Math.max(1, inner - 2);
2432
2678
  const indent = (line) => ` ${line}`;
2433
2679
  const dimRows = (text) => wrapTextWithAnsi(text, textWidth).map((line) => indent(palette.dim(line)));
@@ -2446,19 +2692,14 @@ var ContextBlock = class {
2446
2692
  const kept = this.theme.contextPreviewLines;
2447
2693
  const cut = !this.expanded && body.length > kept;
2448
2694
  const shown = cut ? [...body.slice(0, kept), ...dimRows(foldMarker(body.length - kept, marked ? "marked" : "transcript"))] : body;
2449
- const lines = [...head, ...shown];
2450
- if (this.highlight === void 0) return lines;
2451
- const section = ranges[this.highlight];
2452
- if (section === void 0) return withGutter(palette, lines, () => true, this.highlightLevel);
2453
- const drawn = Math.min(section.to, cut ? kept : body.length);
2454
- const held = markedRange({
2455
- from: head.length + section.from,
2456
- to: head.length + drawn
2457
- }, {
2458
- from: head.length + (cut ? kept : body.length),
2459
- to: lines.length
2460
- });
2461
- return withGutter(palette, lines, (line) => line >= held.from && line < held.to, this.highlightLevel);
2695
+ return {
2696
+ lines: [...head, ...shown],
2697
+ head: head.length,
2698
+ ranges,
2699
+ cut,
2700
+ kept,
2701
+ body: body.length
2702
+ };
2462
2703
  }
2463
2704
  };
2464
2705
  /**
@@ -2494,11 +2735,18 @@ var AssistantBlock = class {
2494
2735
  highlight;
2495
2736
  /** How far above its settled accent the focus mark is drawn right now. */
2496
2737
  highlightLevel = 0;
2738
+ /** The coloured fences of the last parse; absent when the theme colours none. */
2739
+ fences;
2740
+ /** The settled message, by width, focus, and content; a streaming message is drawn afresh each frame. */
2741
+ drawn = new LastDrawn();
2742
+ /** Counts every change to the text, the reasoning, or the committed state. */
2743
+ revision = 0;
2497
2744
  constructor(theme, turn) {
2498
2745
  this.theme = theme;
2499
2746
  this.turn = turn;
2500
2747
  const palette = theme.palette;
2501
- this.markdown = new Markdown("", 0, 0, markdownTheme(palette, theme.codeHighlight));
2748
+ this.fences = theme.codeHighlight === void 0 ? void 0 : new FenceMemo(theme.codeHighlight);
2749
+ this.markdown = new Markdown("", 0, 0, markdownTheme(palette, this.fences));
2502
2750
  this.reasoningText = new Text("", 0, 0);
2503
2751
  }
2504
2752
  /**
@@ -2562,6 +2810,7 @@ var AssistantBlock = class {
2562
2810
  */
2563
2811
  appendText(delta) {
2564
2812
  this.text += delta;
2813
+ this.revision += 1;
2565
2814
  this.markdown.setText(this.text);
2566
2815
  }
2567
2816
  /**
@@ -2570,6 +2819,7 @@ var AssistantBlock = class {
2570
2819
  */
2571
2820
  appendReasoning(delta) {
2572
2821
  this.reasoning += delta;
2822
+ this.revision += 1;
2573
2823
  this.reasoningText.setText(this.theme.palette.dim(this.theme.palette.italic(this.reasoning.trimEnd())));
2574
2824
  }
2575
2825
  /**
@@ -2582,6 +2832,7 @@ var AssistantBlock = class {
2582
2832
  this.text = text;
2583
2833
  this.reasoning = reasoning;
2584
2834
  this.interrupted = interrupted;
2835
+ this.revision += 1;
2585
2836
  this.fade = void 0;
2586
2837
  this.reasoningFade = void 0;
2587
2838
  this.markdown.setText(text);
@@ -2590,9 +2841,22 @@ var AssistantBlock = class {
2590
2841
  invalidate() {
2591
2842
  this.markdown.invalidate();
2592
2843
  this.reasoningText.invalidate();
2844
+ this.fences?.clear();
2845
+ this.drawn.clear();
2593
2846
  }
2594
2847
  render(width) {
2595
- const { content: inner } = focusFrame(width, this.highlight);
2848
+ const { marked, content: inner } = focusFrame(width, this.highlight);
2849
+ const { lines, reasoning, reply } = this.fade !== void 0 || this.reasoningFade !== void 0 ? this.compose(inner) : this.drawn.get(`${String(width)}:${String(marked)}:${String(this.revision)}`, () => this.compose(inner));
2850
+ if (this.highlight === void 0) return lines;
2851
+ const section = this.parts()[this.highlight]?.kind === "reasoning" ? reasoning : reply;
2852
+ return withGutter(this.theme.palette, lines, (index) => index >= section.from && index < section.to, this.highlightLevel);
2853
+ }
2854
+ /**
2855
+ * Stack the reasoning over the reply, each through its own tail.
2856
+ * @param inner - the columns the message lays out in.
2857
+ * @returns the lines, gutter not yet applied, and where each section falls.
2858
+ */
2859
+ compose(inner) {
2596
2860
  const lines = [""];
2597
2861
  const reasoning = {
2598
2862
  from: 0,
@@ -2614,9 +2878,11 @@ var AssistantBlock = class {
2614
2878
  reply.to = lines.length;
2615
2879
  }
2616
2880
  if (this.interrupted) lines.push(this.theme.palette.dim("[interrupted]"));
2617
- if (this.highlight === void 0) return lines;
2618
- const section = this.parts()[this.highlight]?.kind === "reasoning" ? reasoning : reply;
2619
- return withGutter(this.theme.palette, lines, (index) => index >= section.from && index < section.to, this.highlightLevel);
2881
+ return {
2882
+ lines,
2883
+ reasoning,
2884
+ reply
2885
+ };
2620
2886
  }
2621
2887
  /**
2622
2888
  * The reasoning lines, with the streaming tail floated out toward dim italic.
@@ -2650,6 +2916,7 @@ var AssistantBlock = class {
2650
2916
  renderText(width, at) {
2651
2917
  const fade = this.fade;
2652
2918
  const lines = this.markdown.render(width);
2919
+ this.fences?.settle();
2653
2920
  if (fade === void 0) return lines;
2654
2921
  if (this.fadeWidth !== void 0 && this.fadeWidth !== width) fade.flush();
2655
2922
  this.fadeWidth = width;
@@ -2684,6 +2951,10 @@ var ToolBlock = class {
2684
2951
  highlight;
2685
2952
  /** How far above its settled accent the focus mark is drawn right now. */
2686
2953
  highlightLevel = 0;
2954
+ /** The coloured, folded, wrapped body, by width, fold, focus, status, and result. */
2955
+ drawn = new LastDrawn();
2956
+ /** Counts every result the card was given. */
2957
+ revision = 0;
2687
2958
  constructor(theme, name, call, turn) {
2688
2959
  this.theme = theme;
2689
2960
  this.name = name;
@@ -2756,6 +3027,7 @@ var ToolBlock = class {
2756
3027
  this.resultLines = lines;
2757
3028
  this.resultCode = code;
2758
3029
  this.status = isError ? "error" : "done";
3030
+ this.revision += 1;
2759
3031
  }
2760
3032
  /**
2761
3033
  * Whether the whole body is drawn right now.
@@ -2771,36 +3043,28 @@ var ToolBlock = class {
2771
3043
  setExpanded(expanded) {
2772
3044
  this.expanded = expanded;
2773
3045
  }
2774
- invalidate() {}
3046
+ invalidate() {
3047
+ this.drawn.clear();
3048
+ }
2775
3049
  render(width) {
2776
3050
  const palette = this.theme.palette;
2777
3051
  const { marked, content: outer } = focusFrame(width, this.highlight);
2778
- const header = `${this.status === "running" ? palette.warning("●") : this.status === "done" ? palette.success("●") : palette.error("●")} ${palette.bold(this.name)}${this.call.title === "" ? "" : ` ${palette.dim(this.call.title)}`}`;
2779
- const body = [...this.call.lines, ...this.resultLines];
2780
- const code = this.call.code === void 0 && this.resultCode === void 0 ? void 0 : [...this.call.code ?? new Array(this.call.lines.length).fill(void 0), ...this.resultCode ?? new Array(this.resultLines.length).fill(void 0)];
2781
- const kept = this.expanded ? body : foldRows(body, this.theme.toolPreviewLines, (hidden) => foldMarker(hidden, marked ? "marked" : "transcript"));
2782
- const shown = paintDiffRows(paintCodeRows(kept, code, this.theme.codeHighlight), kept, palette);
2783
- const callCount = Math.min(this.call.lines.length, shown.length);
2784
- const inner = Math.max(1, outer - 4);
2785
- const rows = (lines) => lines.flatMap((line) => wrapTextWithAnsi(line, inner).map((part) => ` ${palette.dim("│")} ${part}`));
2786
- const callLines = [...wrapTextWithAnsi(header, outer), ...rows(shown.slice(0, callCount))];
2787
- const resultLines = rows(shown.slice(callCount));
2788
- const call = faded(callLines, this.fade, this.repaintFloor - 1);
2789
- const result = faded(resultLines, this.resultFade, this.repaintFloor - 1 - callLines.length);
2790
- const lines = [
3052
+ const layout = this.drawn.get(`${String(width)}:${String(marked)}:${String(this.expanded)}:${this.status}:${String(this.revision)}`, () => this.layout(outer, marked));
3053
+ const call = faded(layout.call, this.fade, this.repaintFloor - 1);
3054
+ const result = faded(layout.result, this.resultFade, this.repaintFloor - 1 - layout.call.length);
3055
+ const lines = call === layout.call && result === layout.result ? layout.lines : [
2791
3056
  "",
2792
3057
  ...call,
2793
3058
  ...result
2794
3059
  ];
2795
3060
  if (this.highlight === void 0) return lines;
2796
- const cut = !this.expanded && body.length > this.theme.toolPreviewLines;
2797
- const sections = lines.length - (cut ? rows(shown.slice(-1)).length : 0);
3061
+ const sections = lines.length - layout.marker;
2798
3062
  const section = this.parts()[this.highlight]?.kind === "result" ? {
2799
- from: 1 + callLines.length,
3063
+ from: 1 + layout.call.length,
2800
3064
  to: lines.length
2801
3065
  } : {
2802
3066
  from: 1,
2803
- to: 1 + callLines.length
3067
+ to: 1 + layout.call.length
2804
3068
  };
2805
3069
  const held = markedRange({
2806
3070
  from: section.from,
@@ -2811,6 +3075,37 @@ var ToolBlock = class {
2811
3075
  });
2812
3076
  return withGutter(palette, lines, (index) => index >= held.from && index < held.to, this.highlightLevel);
2813
3077
  }
3078
+ /**
3079
+ * Colour, fold, and wrap the card body.
3080
+ * @param outer - the columns the card lays out in.
3081
+ * @param marked - whether the card draws the focus gutter, which names the fold key.
3082
+ * @returns the header with the call rows, the result rows, both stacked
3083
+ * under the blank line, and how many lines the fold marker took.
3084
+ */
3085
+ layout(outer, marked) {
3086
+ const palette = this.theme.palette;
3087
+ const header = `${this.status === "running" ? palette.warning("●") : this.status === "done" ? palette.success("●") : palette.error("●")} ${palette.bold(this.name)}${this.call.title === "" ? "" : ` ${palette.dim(this.call.title)}`}`;
3088
+ const body = [...this.call.lines, ...this.resultLines];
3089
+ const code = this.call.code === void 0 && this.resultCode === void 0 ? void 0 : [...this.call.code ?? new Array(this.call.lines.length).fill(void 0), ...this.resultCode ?? new Array(this.resultLines.length).fill(void 0)];
3090
+ const kept = this.expanded ? body : foldRows(body, this.theme.toolPreviewLines, (hidden) => foldMarker(hidden, marked ? "marked" : "transcript"));
3091
+ const shown = paintDiffRows(paintCodeRows(kept, code, this.theme.codeHighlight), kept, palette);
3092
+ const callCount = Math.min(this.call.lines.length, shown.length);
3093
+ const inner = Math.max(1, outer - 4);
3094
+ const rows = (lines) => lines.flatMap((line) => wrapTextWithAnsi(line, inner).map((part) => ` ${palette.dim("│")} ${part}`));
3095
+ const call = [...wrapTextWithAnsi(header, outer), ...rows(shown.slice(0, callCount))];
3096
+ const result = rows(shown.slice(callCount));
3097
+ const cut = !this.expanded && body.length > this.theme.toolPreviewLines;
3098
+ return {
3099
+ call,
3100
+ result,
3101
+ lines: [
3102
+ "",
3103
+ ...call,
3104
+ ...result
3105
+ ],
3106
+ marker: cut ? rows(shown.slice(-1)).length : 0
3107
+ };
3108
+ }
2814
3109
  };
2815
3110
  /**
2816
3111
  * Draw one group of a card's rows at the mix its fade reports.
@@ -6631,29 +6926,24 @@ function readerRows(state, groups, render) {
6631
6926
  ];
6632
6927
  }
6633
6928
  //#endregion
6634
- //#region lib/types/reader-overlay.js
6929
+ //#region lib/types/reader-screen.js
6635
6930
  /**
6636
- * The reader as a mounted overlay: the component pi-tui composites over the
6637
- * viewport, and the key map that drives it.
6931
+ * The reader as the surface that owns the terminal: the pane drawn on the
6932
+ * alternate screen, and the key map that drives it.
6638
6933
  *
6639
- * It costs the frame no rows. pi-tui composites an overlay into the last
6640
- * terminal-height lines of the frame after the tree rendered, so the
6641
- * conversation underneath keeps every line it had and the renderer's repaint
6642
- * boundary is never raised. The application places it against the bottom of
6643
- * the viewport and tells it how many rows it may draw, which is one fewer for
6644
- * every viewport line the renderer can no longer repaint; the pane fills
6645
- * exactly that many.
6646
- *
6647
- * Opening and closing are the two moments it moves: the application hands the
6648
- * pane a reveal it reads per render, and the pane draws that share of its
6649
- * frame from the bottom rule up, so the reader rises into place and sinks
6650
- * away without the frame underneath it gaining or losing a line.
6934
+ * It costs the conversation nothing at all. The application switches the
6935
+ * terminal to its alternate screen (`./alt-screen.ts`) and holds the
6936
+ * main screen off it for as long as the reader is up, so the conversation
6937
+ * keeps every line it had, the renderer's repaint boundary never moves, and
6938
+ * no row the reader draws can reach the terminal's scrollback. The pane fills
6939
+ * the screen: it is told how many rows the terminal has and returns exactly
6940
+ * that many.
6651
6941
  *
6652
6942
  * The pane holds no copy of the transcript: it re-reads the blocks and their
6653
6943
  * turns on every render, so a reply that is still streaming grows inside it,
6654
6944
  * a tool result that lands appears, and a new turn joins the list, all with
6655
6945
  * no push and no second clock.
6656
- * @module @deepseek-ai/dsh-tui-app/reader-overlay
6946
+ * @module @deepseek-ai/dsh-tui-app/reader-screen
6657
6947
  */
6658
6948
  /** The key that opens the query line. */
6659
6949
  const FILTER_KEY = "/";
@@ -6663,9 +6953,6 @@ const FILTER_KEY = "/";
6663
6953
  */
6664
6954
  var ReaderPane = class {
6665
6955
  options;
6666
- /** Where the reader left the keyboard and the reading. */
6667
- settled;
6668
- resolve;
6669
6956
  /** Whether the reader is still open; a second close is ignored. */
6670
6957
  open = true;
6671
6958
  state;
@@ -6678,9 +6965,6 @@ var ReaderPane = class {
6678
6965
  */
6679
6966
  constructor(options) {
6680
6967
  this.options = options;
6681
- this.settled = new Promise((resolve) => {
6682
- this.resolve = resolve;
6683
- });
6684
6968
  this.state = {
6685
6969
  cursor: options.cursor,
6686
6970
  column: "pane",
@@ -6690,11 +6974,9 @@ var ReaderPane = class {
6690
6974
  invalidate() {}
6691
6975
  /**
6692
6976
  * Draw the reader at `width`.
6693
- * @param width - the columns the overlay was laid out at.
6694
- * @returns as many lines as `rows()` reports, so the overlay covers the
6695
- * viewport it was placed in - fewer while a reveal is still growing or
6696
- * shrinking it - and blank lines once the transcript it was reading is
6697
- * gone.
6977
+ * @param width - the columns the screen has.
6978
+ * @returns as many lines as `rows()` reports, so the reader covers the
6979
+ * screen, and blank lines once the transcript it was reading is gone.
6698
6980
  */
6699
6981
  render(width) {
6700
6982
  this.width = width;
@@ -6724,23 +7006,7 @@ var ReaderPane = class {
6724
7006
  rows,
6725
7007
  minColumns: this.options.minColumns,
6726
7008
  totalTurns: groups.length
6727
- }).slice(rows - this.drawnRows(rows));
6728
- }
6729
- /**
6730
- * How many of the frame's lines the reveal draws right now.
6731
- *
6732
- * The overlay is anchored at the bottom of the viewport, so a pane that
6733
- * draws fewer lines keeps every line it does draw on the terminal row it
6734
- * will settle on: the reader rises into place rather than sliding, and the
6735
- * conversation above it keeps its own lines throughout.
6736
- * @param rows - the height the frame was laid out at.
6737
- * @returns the lines to keep, counted from the bottom rule up; at least one
6738
- * and never more than the frame has.
6739
- */
6740
- drawnRows(rows) {
6741
- const reveal = this.options.reveal?.() ?? 1;
6742
- if (reveal >= 1) return rows;
6743
- return Math.max(1, Math.min(rows, Math.ceil(reveal * rows)));
7009
+ });
6744
7010
  }
6745
7011
  /**
6746
7012
  * Answer one key press.
@@ -6774,7 +7040,7 @@ var ReaderPane = class {
6774
7040
  * The geometry of the frame as it stands.
6775
7041
  * @param visible - the turns the rail lists.
6776
7042
  * @param blocks - the navigable blocks.
6777
- * @param rows - the rows the overlay draws.
7043
+ * @param rows - the rows the screen has.
6778
7044
  * @returns the geometry, with the walking pane measured.
6779
7045
  */
6780
7046
  geometryFor(visible, blocks, rows) {
@@ -6787,7 +7053,7 @@ var ReaderPane = class {
6787
7053
  close(target) {
6788
7054
  if (!this.open) return;
6789
7055
  this.open = false;
6790
- this.resolve({
7056
+ this.options.onExit({
6791
7057
  cursor: this.state.cursor,
6792
7058
  target
6793
7059
  });
@@ -7260,6 +7526,12 @@ var SyntaxHighlighter = class {
7260
7526
  * far into a block the renderer can still follow it, and
7261
7527
  * {@link GuardedMainScreen} gives the application the one moment where that
7262
7528
  * question can be answered: after a frame was built and before it is written.
7529
+ *
7530
+ * {@link GuardedMainScreen.suspend} is the other half of that rule: a
7531
+ * full-screen surface that takes the terminal holds the main screen off it
7532
+ * entirely rather than drawing over the conversation, so no line of the
7533
+ * conversation is rewritten while the surface is up and none of the surface's
7534
+ * own rows can reach the terminal's scrollback.
7263
7535
  * @module @deepseek-ai/dsh-tui-app/screen
7264
7536
  */
7265
7537
  /**
@@ -7281,40 +7553,6 @@ function repaintFloor(start, viewportTop) {
7281
7553
  return Math.max(0, viewportTop - start);
7282
7554
  }
7283
7555
  /**
7284
- * Blank rows at the foot of the frame, which a surface mounts while it needs
7285
- * the whole viewport.
7286
- *
7287
- * The renderer repaints a line only at or after {@link repaintFloor}'s own
7288
- * `viewportTop`, a high-water mark a taller frame raises for good. A frame
7289
- * that has since shrunk therefore leaves the top of the viewport out of
7290
- * reach, and an overlay that may not draw there stops short of the screen.
7291
- * These rows push the frame back up to that mark: the terminal scrolls by
7292
- * exactly the shortfall, every line of the viewport becomes repaintable
7293
- * again, and an overlay composited into the frame's last `terminal.rows`
7294
- * lines covers all of them. They are never seen - the overlay is drawn over
7295
- * them, and they are gone before it is.
7296
- *
7297
- * pi-tui's `Text` drops blank lines, so the rows are a component of their own.
7298
- */
7299
- var ViewportPad = class {
7300
- rows;
7301
- /**
7302
- * @param rows - how many blank lines to draw, read on every render.
7303
- */
7304
- constructor(rows) {
7305
- this.rows = rows;
7306
- }
7307
- invalidate() {}
7308
- /**
7309
- * Draw the pad.
7310
- * @returns that many empty lines, and none at all where the frame already
7311
- * reaches the mark.
7312
- */
7313
- render() {
7314
- return Array.from({ length: Math.max(0, this.rows()) }, () => "");
7315
- }
7316
- };
7317
- /**
7318
7556
  * The main screen that settles each frame between building it and writing it.
7319
7557
  *
7320
7558
  * The guard is handed the frame's first repaintable line: the higher of the
@@ -7325,9 +7563,17 @@ var ViewportPad = class {
7325
7563
  * tomorrow. Settling before the write also means a fade reaches the terminal
7326
7564
  * already settled instead of changing in the next frame, by which time its
7327
7565
  * lines may sit above the window.
7566
+ *
7567
+ * It can also be suspended, which stops every write to the terminal while
7568
+ * leaving the renderer's record of the main screen exactly as it was. The
7569
+ * terminal restores that same screen when the surface that took it gives it
7570
+ * back, so the first frame after {@link GuardedMainScreen.resume} writes only
7571
+ * what changed meanwhile.
7328
7572
  */
7329
7573
  var GuardedMainScreen = class extends TuiMainScreen {
7330
7574
  guard;
7575
+ /** What draws instead while another surface holds the terminal; absent while this screen does. */
7576
+ onSuspendedRender;
7331
7577
  /**
7332
7578
  * @param terminal - the terminal the tree renders into.
7333
7579
  * @param showHardwareCursor - whether the terminal's own cursor is the caret.
@@ -7343,6 +7589,57 @@ var GuardedMainScreen = class extends TuiMainScreen {
7343
7589
  this.guard = guard;
7344
7590
  }
7345
7591
  /**
7592
+ * Stop writing to the terminal, and answer every render request with
7593
+ * `onRender` instead.
7594
+ *
7595
+ * Nothing about the renderer's record of the main screen changes, so the
7596
+ * frame it will judge its next write against stays the frame the terminal
7597
+ * still holds behind the surface that took over.
7598
+ * @param onRender - called wherever a frame would have been written, so the
7599
+ * surface that owns the terminal draws on the application's existing render
7600
+ * requests rather than on a clock of its own.
7601
+ */
7602
+ suspend(onRender) {
7603
+ this.onSuspendedRender = onRender;
7604
+ }
7605
+ /** Write to the terminal again, and draw whatever changed while suspended. */
7606
+ resume() {
7607
+ if (this.onSuspendedRender === void 0) return;
7608
+ this.onSuspendedRender = void 0;
7609
+ this.requestRender();
7610
+ }
7611
+ /**
7612
+ * Request a frame, or draw the surface that suspended this screen.
7613
+ * @param force - redraw from scratch; ignored while suspended, where
7614
+ * discarding the record would lose the screen the terminal is holding.
7615
+ */
7616
+ requestRender(force = false) {
7617
+ const suspended = this.onSuspendedRender;
7618
+ if (suspended === void 0) {
7619
+ super.requestRender(force);
7620
+ return;
7621
+ }
7622
+ suspended();
7623
+ }
7624
+ /**
7625
+ * Write a frame now, or draw the suspending surface now.
7626
+ * @param force - redraw from scratch; ignored while suspended, as in
7627
+ * {@link GuardedMainScreen.requestRender}.
7628
+ */
7629
+ renderNow(force = false) {
7630
+ const suspended = this.onSuspendedRender;
7631
+ if (suspended === void 0) {
7632
+ super.renderNow(force);
7633
+ return;
7634
+ }
7635
+ suspended();
7636
+ }
7637
+ /** Write the frame, unless another surface holds the terminal. */
7638
+ doRender() {
7639
+ if (this.onSuspendedRender !== void 0) return;
7640
+ super.doRender();
7641
+ }
7642
+ /**
7346
7643
  * Build the frame, let the guard settle what its geometry decides, and build
7347
7644
  * it again for every pass that changed a line.
7348
7645
  * @param width - the total width the tree lays out in.
@@ -7524,8 +7821,6 @@ function renderSubagentPanel(view, render) {
7524
7821
  const QUIT_TOAST = "press Ctrl+C again to quit";
7525
7822
  /** What the line says when the transcript holds nothing the reader could open. */
7526
7823
  const NOTHING_TO_READ_TOAST = "nothing in the transcript to read yet";
7527
- /** What the line says when a fold key named a block the renderer can no longer rewrite. */
7528
- const ABOVE_WINDOW_TOAST = "above the repaint window · opened in the reader";
7529
7824
  /**
7530
7825
  * What the line says while the stop is armed. It names the turn it would
7531
7826
  * stop, so a press after a turn boundary is read for what it is.
@@ -7735,21 +8030,8 @@ function splitModelValue(value) {
7735
8030
  }
7736
8031
  /** What the transcript is told when a session switch took the conversation the reader was showing. */
7737
8032
  const READER_GONE = "the transcript changed · reader closed";
7738
- /**
7739
- * How the reader is mounted: the whole viewport, anchored at its bottom edge.
7740
- *
7741
- * pi-tui composites an overlay into the last `terminal.rows` lines of the
7742
- * frame, so anchoring at the bottom puts the pane's first line exactly on the
7743
- * frame's last repaintable line whatever the frame's own length is - and the
7744
- * pane draws one line fewer for every viewport line the renderer can no
7745
- * longer repaint ({@link TuiApp.viewportFloor}), so opening the reader never
7746
- * rewrites a line above the window.
7747
- */
7748
- const READER_OVERLAY = {
7749
- width: "100%",
7750
- maxHeight: "100%",
7751
- anchor: "bottom-left"
7752
- };
8033
+ /** What the transcript is told when a fold key named a block the renderer can no longer rewrite. */
8034
+ const ABOVE_WINDOW_NOTICE = "above the repaint window · opened in the reader";
7753
8035
  /**
7754
8036
  * Row layout of the todo picker: the label column grows with the widest todo
7755
8037
  * line instead of stopping at the list's 32-column default, which would cut a
@@ -7990,8 +8272,8 @@ var TuiApp = class {
7990
8272
  transferringQueue = false;
7991
8273
  /** Holds {@link panel} exactly while the bound session has subagent rows. */
7992
8274
  panelSlot = new Container();
7993
- /** Blank rows that give the mounted reader the whole viewport; see {@link ViewportPad}. */
7994
- pad = new ViewportPad(() => this.readerPad);
8275
+ /** The terminal's alternate screen, which the reader draws on; see {@link AlternateScreen}. */
8276
+ readerScreen;
7995
8277
  /** Colours fenced code in replies; see {@link SyntaxHighlighter}. */
7996
8278
  codeHighlight;
7997
8279
  /** The terminal's background once it answered; undefined until then, and on a terminal that answers none. */
@@ -8063,14 +8345,10 @@ var TuiApp = class {
8063
8345
  chrome;
8064
8346
  /** The lift the subagent panel's text was last built with, which only a repaint changes. */
8065
8347
  panelLift = 0;
8066
- /** The reader on screen right now, while one is open. */
8348
+ /** The reader on the alternate screen right now, while one is open. */
8067
8349
  reader;
8068
- /** Blank rows {@link ViewportPad} draws so the mounted reader covers the whole viewport. */
8069
- readerPad = 0;
8070
- /** The motion growing the open reader into place; settled or absent draws it whole. */
8071
- readerOpening;
8072
- /** The reader shrinking away, still mounted until its motion settles. */
8073
- readerClosing;
8350
+ /** Set while a paint of the open reader is already queued for this turn of the loop. */
8351
+ readerPaintQueued = false;
8074
8352
  /** Prompts shown or waiting on the modal queue, which the reader steps aside for. */
8075
8353
  queuedModals = 0;
8076
8354
  /** Until when an `Escape` that reaches the editor does nothing at all. */
@@ -8079,8 +8357,9 @@ var TuiApp = class {
8079
8357
  stopArmedUntil = 0;
8080
8358
  /**
8081
8359
  * How many of the viewport's own first lines the renderer can no longer
8082
- * repaint, from the last frame the guard settled. An overlay composites
8083
- * into those lines, so this is what decides where one can be drawn.
8360
+ * repaint, from the last frame the guard settled. The transient line
8361
+ * composites into those lines, so this is what decides whether it can float
8362
+ * at all.
8084
8363
  */
8085
8364
  viewportFloor = 0;
8086
8365
  /** Set while {@link TuiApp.bind} replays a session's history, which draws its cards settled. */
@@ -8128,6 +8407,7 @@ var TuiApp = class {
8128
8407
  this.tui = new GuardedMainScreen(deps.terminal, true, (viewportTop, width, frameLines) => {
8129
8408
  return this.settleFrame(viewportTop, width, frameLines);
8130
8409
  });
8410
+ this.readerScreen = new AlternateScreen(deps.terminal);
8131
8411
  this.header = new Text("", 0, 0);
8132
8412
  this.inspector = new InspectorPane(() => this.inspectorView(), {
8133
8413
  palette,
@@ -8169,8 +8449,7 @@ var TuiApp = class {
8169
8449
  this.queueSlot,
8170
8450
  this.editor,
8171
8451
  this.panelSlot,
8172
- this.footer,
8173
- this.pad
8452
+ this.footer
8174
8453
  ];
8175
8454
  for (const child of tree) this.tui.addChild(child);
8176
8455
  }
@@ -8682,7 +8961,6 @@ var TuiApp = class {
8682
8961
  */
8683
8962
  showModal(prompt, signal) {
8684
8963
  this.queuedModals += 1;
8685
- this.finishReaderClose();
8686
8964
  this.hideReader(true);
8687
8965
  if (this.reader === void 0) this.focusEditor();
8688
8966
  return this.modals.run(prompt, signal).finally(() => {
@@ -8692,12 +8970,15 @@ var TuiApp = class {
8692
8970
  });
8693
8971
  }
8694
8972
  /**
8695
- * Take the reader off the screen for as long as something else needs it, or
8696
- * put it back.
8973
+ * Take the reader off the terminal for as long as something else needs it,
8974
+ * or put it back. The reader itself stays open throughout, on the section
8975
+ * it was reading.
8697
8976
  * @param hidden - whether the reader steps aside.
8698
8977
  */
8699
8978
  hideReader(hidden) {
8700
- this.reader?.handle.setHidden(hidden);
8979
+ if (this.reader === void 0) return;
8980
+ if (hidden) this.hideReaderScreen();
8981
+ else this.showReader();
8701
8982
  }
8702
8983
  /**
8703
8984
  * Walk a list one entry at a time: the picker opens on `rows`, `Enter`
@@ -8911,25 +9192,25 @@ var TuiApp = class {
8911
9192
  * their global meaning wherever the keyboard is; everything else is what
8912
9193
  * {@link resolveKey} makes of the key in the region that holds it.
8913
9194
  *
8914
- * A reader that is still shrinking away goes first, before the key is read:
8915
- * pi-tui hands the key stream back to an overlay it still holds a focus
8916
- * restore for, so a key this listener leaves to the editor would reach the
8917
- * pane instead of the caret.
9195
+ * A reader holding the terminal takes every key here, so nothing reaches
9196
+ * the editor behind it and no frame of the conversation is asked for while
9197
+ * the terminal is showing another screen.
8918
9198
  * @param data - the raw key bytes.
8919
9199
  * @returns the consume marker, or undefined for the keys pi-tui's own
8920
9200
  * editor answers.
8921
9201
  */
8922
9202
  onKey(data) {
8923
9203
  if (isKeyRelease(data)) return { consume: true };
8924
- this.finishReaderClose();
8925
9204
  if (this.modals.isActive()) {
8926
9205
  if (!matchesKey(data, "ctrl+c")) return void 0;
8927
9206
  this.modals.withdrawActive();
8928
9207
  return { consume: true };
8929
9208
  }
8930
- if (this.reader !== void 0) {
8931
- if (!matchesKey(data, "ctrl+c")) return void 0;
8932
- this.reader.pane.withdraw();
9209
+ const reading = this.reader;
9210
+ if (reading !== void 0 && this.readerScreen.active) {
9211
+ if (matchesKey(data, "ctrl+c")) reading.pane.withdraw();
9212
+ else reading.pane.handleInput(data);
9213
+ this.queueReaderPaint();
8933
9214
  return { consume: true };
8934
9215
  }
8935
9216
  if (this.focus === "editor" && !matchesKey(data, "escape")) this.disarmStop();
@@ -9114,10 +9395,12 @@ var TuiApp = class {
9114
9395
  /**
9115
9396
  * Read the transcript full screen.
9116
9397
  *
9117
- * The reader is an overlay, so it costs the conversation no line and the
9118
- * renderer's repaint boundary stays where it was. The frame is settled
9119
- * first, so the pane knows how much of the viewport the renderer can still
9120
- * repaint before it decides how tall to draw.
9398
+ * The reader takes the terminal rather than drawing over the conversation:
9399
+ * the frame on screen is written one last time, the main screen is then
9400
+ * held off the terminal, and the alternate screen carries the reader. The
9401
+ * conversation keeps every line it had, the renderer's repaint boundary
9402
+ * stays where it was, and nothing the reader draws can reach the terminal's
9403
+ * scrollback.
9121
9404
  * @param at - the section to open on; the newest one when omitted.
9122
9405
  */
9123
9406
  openReader(at) {
@@ -9127,46 +9410,79 @@ var TuiApp = class {
9127
9410
  this.showToast(NOTHING_TO_READ_TOAST);
9128
9411
  return;
9129
9412
  }
9130
- this.finishReaderClose();
9131
- this.tui.renderNow();
9132
- this.readerOpening = this.startMotion(6);
9133
9413
  const pane = new ReaderPane({
9134
9414
  palette: this.deps.palette,
9135
9415
  blocks: () => navigableBlocks(this.chat.children),
9136
- rows: () => Math.max(1, this.deps.terminal.rows - this.viewportFloor),
9416
+ rows: () => Math.max(1, this.deps.terminal.rows),
9137
9417
  minColumns: this.deps.readerMinColumns,
9138
9418
  cursor,
9139
- reveal: () => this.readerReveal()
9419
+ onExit: (exit) => {
9420
+ this.closeReader(exit);
9421
+ }
9140
9422
  });
9141
- this.reader = {
9142
- handle: this.tui.showOverlay(pane, READER_OVERLAY),
9143
- pane
9144
- };
9145
- this.tui.requestRender();
9146
- pane.settled.then((exit) => {
9147
- this.closeReader(exit);
9423
+ this.reader = { pane };
9424
+ this.showReader();
9425
+ }
9426
+ /**
9427
+ * Put the open reader on the terminal: settle the conversation, hold the
9428
+ * main screen off the terminal, and draw the reader on the alternate one.
9429
+ *
9430
+ * Called with a reader open and the conversation on the terminal.
9431
+ *
9432
+ * Every render the application already asks for draws the reader while it is
9433
+ * up, so a landing tool result, a streamed word, and a resize all reach it
9434
+ * without a clock of its own.
9435
+ */
9436
+ showReader() {
9437
+ this.tui.renderNow();
9438
+ this.tui.suspend(() => {
9439
+ this.queueReaderPaint();
9148
9440
  });
9441
+ this.readerScreen.enter();
9442
+ this.paintReader();
9443
+ }
9444
+ /**
9445
+ * Take the reader off the terminal and give the conversation its screen
9446
+ * back, leaving the reader itself open. Both steps answer for a terminal
9447
+ * the reader never took, so a caller needs to know only that it is done.
9448
+ */
9449
+ hideReaderScreen() {
9450
+ this.readerScreen.leave();
9451
+ this.tui.resume();
9452
+ }
9453
+ /**
9454
+ * Draw the open reader, at most once per turn of the loop.
9455
+ *
9456
+ * The pane re-reads the conversation on every paint, so a burst of session
9457
+ * events costs one drawing rather than one per event.
9458
+ */
9459
+ queueReaderPaint() {
9460
+ if (this.readerPaintQueued) return;
9461
+ this.readerPaintQueued = true;
9462
+ queueMicrotask(() => {
9463
+ this.readerPaintQueued = false;
9464
+ this.paintReader();
9465
+ });
9466
+ }
9467
+ /** Draw the open reader now, if one holds the terminal. */
9468
+ paintReader() {
9469
+ const open = this.reader;
9470
+ if (open === void 0 || !this.readerScreen.active) return;
9471
+ this.readerScreen.paint(open.pane.render(Math.max(1, this.deps.terminal.columns)));
9149
9472
  }
9150
9473
  /**
9151
9474
  * Take the reader down and put the keyboard where it left it.
9152
9475
  * @param exit - the section last read and the region that takes over.
9153
9476
  */
9154
9477
  closeReader(exit) {
9155
- const open = this.reader;
9156
- if (open === void 0) return;
9478
+ if (this.reader === void 0) return;
9157
9479
  this.reader = void 0;
9158
- this.readerOpening = void 0;
9159
9480
  this.cursor = exit.cursor;
9481
+ this.hideReaderScreen();
9160
9482
  this.setFocus(exit.target);
9161
9483
  this.refreshQueue();
9162
9484
  this.refreshSubagentPanel();
9163
9485
  this.armEscapeHandoff();
9164
- const motion = this.startMotion(4);
9165
- if (motion === void 0) open.handle.hide();
9166
- else this.readerClosing = {
9167
- handle: open.handle,
9168
- motion
9169
- };
9170
9486
  this.tui.requestRender();
9171
9487
  }
9172
9488
  /**
@@ -9177,11 +9493,10 @@ var TuiApp = class {
9177
9493
  * @returns whether a reader was open, so the caller can report that it came down.
9178
9494
  */
9179
9495
  dropReader() {
9180
- this.finishReaderClose();
9181
9496
  const open = this.reader;
9182
9497
  if (open === void 0) return false;
9183
9498
  this.reader = void 0;
9184
- open.handle.hide();
9499
+ this.hideReaderScreen();
9185
9500
  open.pane.withdraw();
9186
9501
  return true;
9187
9502
  }
@@ -9542,7 +9857,6 @@ var TuiApp = class {
9542
9857
  * @param region - the region that takes the keyboard.
9543
9858
  */
9544
9859
  setFocus(region) {
9545
- this.finishReaderClose();
9546
9860
  if (region === "editor") this.chrome = void 0;
9547
9861
  else this.startChrome("landing", 6);
9548
9862
  this.focus = region;
@@ -9578,8 +9892,8 @@ var TuiApp = class {
9578
9892
  if (section === void 0 || block === void 0 || !isFoldable(block)) return;
9579
9893
  this.tui.renderNow();
9580
9894
  if (this.highlighted?.block !== block) {
9895
+ this.notice(ABOVE_WINDOW_NOTICE);
9581
9896
  this.openReader(section.cursor);
9582
- this.showToast(ABOVE_WINDOW_TOAST);
9583
9897
  return;
9584
9898
  }
9585
9899
  block.setExpanded(!block.isExpanded());
@@ -10452,28 +10766,6 @@ var TuiApp = class {
10452
10766
  return this.focus === "panel" ? this.markLift() : 0;
10453
10767
  }
10454
10768
  /**
10455
- * How much of its height the reader draws right now.
10456
- * @returns the share of the pane's rows: growing while it opens, shrinking
10457
- * while it comes down, and 1 for a reader that is simply up.
10458
- */
10459
- readerReveal() {
10460
- const closing = this.readerClosing;
10461
- if (closing !== void 0) return 1 - closing.motion.progress();
10462
- return this.readerOpening?.progress() ?? 1;
10463
- }
10464
- /**
10465
- * Take a shrinking reader off the screen now, for anything that needs the
10466
- * viewport back before the motion settles: another overlay, a prompt, a
10467
- * session change, or the next key press - pi-tui would hand the key stream
10468
- * back to an overlay it still holds a focus restore for.
10469
- */
10470
- finishReaderClose() {
10471
- const closing = this.readerClosing;
10472
- if (closing === void 0) return;
10473
- this.readerClosing = void 0;
10474
- closing.handle.hide();
10475
- }
10476
- /**
10477
10769
  * Arm the fade tick while streamed text or a card still draws below the last
10478
10770
  * brightness level and disarm it otherwise, so a session that is not
10479
10771
  * streaming runs no fade timer. Exactly one runs at a time, and a stopped
@@ -10512,19 +10804,14 @@ var TuiApp = class {
10512
10804
  * @param viewportTop - the frame's first repaintable line.
10513
10805
  * @param width - the width it was built at.
10514
10806
  * @param frameLines - how many lines the frame has, which is what places
10515
- * an overlay: pi-tui composites one into the frame's last `rows` lines.
10807
+ * the transient line: pi-tui composites an overlay into the frame's last
10808
+ * `rows` lines.
10516
10809
  * @returns whether anything changed a line, so the frame is built again
10517
10810
  * before it is written.
10518
10811
  */
10519
10812
  settleFrame(viewportTop, width, frameLines) {
10520
10813
  const rows = this.deps.terminal.rows;
10521
- const floor = repaintFloor(Math.max(frameLines - this.readerPad, rows) - rows, viewportTop);
10522
- const pad = this.reader === void 0 && this.readerClosing === void 0 ? 0 : floor;
10523
- this.viewportFloor = floor - pad;
10524
- if (pad !== this.readerPad) {
10525
- this.readerPad = pad;
10526
- return true;
10527
- }
10814
+ this.viewportFloor = repaintFloor(Math.max(frameLines, rows) - rows, viewportTop);
10528
10815
  const section = this.focusedSection();
10529
10816
  const wanted = section !== void 0 && this.focus === "transcript" ? {
10530
10817
  block: section.block,
@@ -10562,7 +10849,6 @@ var TuiApp = class {
10562
10849
  this.reasoningTail?.tick();
10563
10850
  this.blockFades.tick();
10564
10851
  if (this.toast?.clock.expired() === true) this.hideToast();
10565
- if (this.readerClosing?.motion.needsRepaint() === false) this.finishReaderClose();
10566
10852
  if (this.panelLift !== this.panelLiftNow()) this.refreshSubagentPanel();
10567
10853
  this.motions.tick();
10568
10854
  this.tui.requestRender();