Repository navigation
Expand file tree
/
Copy pathtranscript.ts
More file actions
605 lines (569 loc) · 19.1 KB
/
Copy pathtranscript.ts
File metadata and controls
605 lines (569 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
* Transcript rows: append/replace/retext, windowed repaint, spacer, row renderable builders.
*/
import {
BoxRenderable,
MarkdownRenderable,
TextRenderable,
TextTableRenderable,
StyledText,
type BaseRenderable,
type CliRenderer,
} from "@opentui/core";
import { stringWidth } from "../view/height.js";
import { viewToTableContent, type McpStructuredView } from "../mcp-view.js";
import {
armLinkLine,
buildLinkLine,
findLinks,
paintLinkLine,
splitLinkSpans,
splitWrappedLinkSpans,
} from "../url-links.js";
import {
splitAtSettledHeading,
withholdIncompleteHeading,
} from "../markdown-parser.js";
import { diffLineChunks, retextStyledKindRow } from "./row-retext.js";
import {
blockLabel,
EXPAND_KEY,
expandedRowLines,
splitTrailingArrow,
isMarkdownRow,
isSentenceRow,
MAIN_AGENT,
paintStreamRow,
plainRowWrapWidth,
rowGroupGap,
streamRowGutter,
toolRowLines,
toolSentenceLines,
transcriptSyntaxStyle,
type PaintedStreamLine,
type RowLayout,
type StreamRow,
type StyledBodyLine,
} from "../stream.js";
import { type AppShell } from "./internals.js";
/**
* Surface every row is laid out against: the transcript's own column budget
* (rows right-align and wrap themselves) and whether writers need naming.
* The scroll bars are hidden, so the transcript owns the whole content zone.
*/
export function transcriptRowLayout(shell: AppShell): RowLayout {
return {
width: Math.max(1, shell.layout.contentWidth),
multiAgent: shell.agentVoices.size > 1,
};
}
/**
* Record a row's writer. Returns true when the transcript has just gained a
* second voice — every earlier row now needs the label it was painted without.
*/
export function noteAgentVoice(shell: AppShell, row: StreamRow): boolean {
if (row.role === "user") return false;
const before = shell.agentVoices.size;
shell.agentVoices.add(row.agent ?? MAIN_AGENT);
return before === 1 && shell.agentVoices.size === 2;
}
/** Row immediately before `index` in the log, or undefined at the start. */
function rowBefore(shell: AppShell, index: number): StreamRow | undefined {
return index > 0 ? shell.streamLog[index - 1] : undefined;
}
/** Blank rows the row at `index` claims above itself. */
export function gapBefore(shell: AppShell, index: number): number {
const row = shell.streamLog[index];
if (row === undefined) return 0;
return rowGroupGap(rowBefore(shell, index), row);
}
/**
* Writer label the row at `index` carries above it, or null mid-block.
* A block is exactly a gap-free run from one writer, so this tracks
* `gapBefore` rather than keeping its own notion of block boundaries.
*/
export function labelBefore(shell: AppShell, index: number): string | null {
const row = shell.streamLog[index];
if (row === undefined) return null;
return blockLabel(rowBefore(shell, index), row, transcriptRowLayout(shell));
}
/** Row count of the log `appendStreamRow` currently targets (parent or observe). */
export function streamRowCount(shell: AppShell): number {
return shell.observe !== null && shell.parentStreamLog !== null
? shell.parentStreamLog.length
: shell.streamLogBase + shell.streamLog.length;
}
/**
* Row at absolute `index` on the log `appendStreamRow` currently targets. A
* tool result rewrites the call row it answers rather than appending its
* own, and needs to read that row back to fold into it.
*
* `index` is absolute (see `streamLogBase`); a row already evicted by the
* retention cap reads back as undefined, same as one past the end.
*/
export function streamRowAt(
shell: AppShell,
index: number,
): StreamRow | undefined {
if (shell.observe !== null && shell.parentStreamLog !== null) {
const local = index - (shell.parentStreamLogBase ?? 0);
return local >= 0 && local < shell.parentStreamLog.length
? shell.parentStreamLog[local]
: undefined;
}
const local = index - shell.streamLogBase;
return local >= 0 && local < shell.streamLog.length
? shell.streamLog[local]
: undefined;
}
/**
* Identifies a transcript child as the eviction notice rather than a row.
* Identity, not position or state, is the source of truth: `streamLogBase`
* flips to nonzero the instant a trim happens, one step before the notice
* node itself exists in the paint tree, so deriving "is there a marker"
* from state would misalign row indices for exactly that transitional call.
*/
export const evictionMarkers = new WeakSet<BaseRenderable>();
/**
* Row-index code paths (below, and the two windowed-rebuild callers) treat
* `getChildren()` as a 1:1 array with `streamLog`. The leading bottom-anchor
* spacer (see `transcriptSpacers`) and, once retention has evicted anything,
* the eviction notice above the oldest retained row both break that — every
* consumer that needs the row-only view goes through here rather than the
* raw call.
*/
export function transcriptRowChildren(
shell: AppShell,
): readonly BaseRenderable[] {
const children = shell.transcript.getChildren().slice(1);
const first = children[0];
return first != null && evictionMarkers.has(first)
? children.slice(1)
: children;
}
/** The eviction-notice node, if the retention cap has dropped anything. */
export function transcriptMarker(shell: AppShell): BaseRenderable | undefined {
const children = shell.transcript.getChildren().slice(1);
const first = children[0];
return first != null && evictionMarkers.has(first) ? first : undefined;
}
/** Raw child-list offset before the first row: the spacer, plus the notice if present. */
export function transcriptRowOffset(shell: AppShell): number {
return transcriptMarker(shell) === undefined ? 1 : 2;
}
/**
* Rewrite a row's body on its existing paint node.
*
* Every row kind retextes in place — streaming markdown keeps the parser's
* block state, and the styled kinds (diff, tool sentence, expansion,
* structured) rewrite their line and table content. Returns false when the
* node shape does not match the row (a label or arrow appearing, a line-count
* change) and the caller must rebuild it.
*/
export function retextStreamRow(
shell: AppShell,
node: BaseRenderable,
row: StreamRow,
label: string | null,
): boolean {
const layout = transcriptRowLayout(shell);
if (label !== null) {
if (!(node instanceof BoxRenderable)) return false;
const [headerNode, innerNode] = node.getChildren();
if (!(headerNode instanceof TextRenderable) || innerNode === undefined)
return false;
if (!retextStreamRowBody(innerNode, row, layout)) return false;
headerNode.content = label;
return true;
}
return retextStreamRowBody(node, row, layout);
}
/** The shape-matching rewrite shared by labelled and unlabelled rows. */
function retextStreamRowBody(
node: BaseRenderable,
row: StreamRow,
layout: RowLayout,
): boolean {
if (retextStyledKindRow(node, row, layout)) return true;
if (
row.diff !== undefined ||
row.structured !== undefined ||
isSentenceRow(row)
)
return false;
if (node instanceof TextRenderable) {
if (isMarkdownRow(row)) return false;
paintPlainRowNode(
node,
row,
paintStreamRow(row, layout),
plainRowWrapWidth(row, layout),
);
return true;
}
if (!(node instanceof BoxRenderable) || !isMarkdownRow(row)) return false;
const [gutterNode, bodyNode] = node.getChildren();
if (!(gutterNode instanceof TextRenderable)) return false;
const gutter = streamRowGutter(row, layout);
gutterNode.content = gutter.content;
gutterNode.width = stringWidth(gutter.content);
const width = markdownBodyColumns(gutter, layout);
const content = markdownContent(row);
const split = splitAtSettledHeading(content);
// No settled heading behind the tail: a lone renderer, same as an unsplit
// body. A shape change (a heading just closed, or one just left the window
// a full rebuild trimmed) falls through to the caller's rebuild.
if (split === null) {
if (!(bodyNode instanceof MarkdownRenderable)) return false;
bodyNode.width = width;
bodyNode.content = content;
bodyNode.streaming = row.streaming === true;
return true;
}
if (!(bodyNode instanceof BoxRenderable)) return false;
const [frozenNode, liveNode] = bodyNode.getChildren();
if (
!(frozenNode instanceof MarkdownRenderable) ||
!(liveNode instanceof MarkdownRenderable)
) {
return false;
}
bodyNode.width = width;
frozenNode.width = width;
frozenNode.content = split.frozen;
liveNode.width = width;
liveNode.content = split.live;
liveNode.streaming = row.streaming === true;
liveNode.marginTop = split.gapRows;
return true;
}
/**
* Prefix column beside a body the renderer owns. Width is pinned to the painted
* columns so an empty gutter — a lone agent's own prose — costs none, and the
* answer starts on the transcript's first column.
*/
function gutterNode(
ctx: CliRenderer,
gutter: PaintedStreamLine,
): TextRenderable {
return new TextRenderable(ctx, {
content: gutter.content,
fg: gutter.fg,
flexShrink: 0,
width: stringWidth(gutter.content),
});
}
/**
* Columns a markdown body may paint into: the transcript budget less the
* row's own prefix. Pinned rather than left to `flexGrow`, which reports the
* body's intrinsic width to yoga and lets a wide table paint past the edge.
*/
function markdownBodyColumns(
gutter: PaintedStreamLine,
layout: RowLayout,
): number {
return Math.max(1, layout.width - stringWidth(gutter.content));
}
/**
* Markdown tables shrink to the row's column budget rather than overflowing:
* columns are fitted proportionally and cells wrap on word boundaries. A table
* still too wide for its narrowest fit is clipped by the body's pinned width,
* which keeps it inside the transcript instead of painting over the chrome.
*/
const TRANSCRIPT_TABLE_OPTIONS = {
wrapMode: "word",
columnFitter: "proportional",
} as const;
function markdownContent(row: StreamRow): string {
if (row.streaming !== true) return row.text;
return withholdIncompleteHeading(row.text);
}
/**
* Build the row-shaped paint node: a MarkdownRenderable body next to a plain
* gutter for markdown-bearing rows (assistant replies), a TextTableRenderable
* for structured rows (MCP results), a coloured diff body for edit-tool rows,
* and literal text for everything else.
*/
export function buildRowNode(
ctx: CliRenderer,
row: StreamRow,
layout: RowLayout,
onToggle?: () => void,
): TextRenderable | BoxRenderable {
if (isSentenceRow(row)) {
// The sentence is one line: it is cut to the columns beside the marker
// rather than wrapped, so a long URL or query cannot double the row.
const columns = Math.max(
1,
layout.width - stringWidth(streamRowGutter(row, layout).content),
);
if (row.structured !== undefined) {
// The table is what the sentence hides; collapsed, the sentence is the row.
return row.expanded === true
? createStructuredRowRenderable(
ctx,
row,
layout,
row.structured,
toolSentenceLines(row, columns),
onToggle,
)
: createStyledLinesRowRenderable(
ctx,
row,
layout,
toolSentenceLines(row, columns),
onToggle,
);
}
return createStyledLinesRowRenderable(
ctx,
row,
layout,
toolRowLines(row, columns),
onToggle,
);
}
if (row.diff !== undefined) {
return createStyledLinesRowRenderable(ctx, row, layout, row.diff.lines);
}
const expanded = expandedRowLines(row, layout);
if (expanded !== null) {
return createStyledLinesRowRenderable(ctx, row, layout, expanded);
}
if (row.structured !== undefined) {
return createStructuredRowRenderable(ctx, row, layout, row.structured);
}
if (!isMarkdownRow(row)) {
return buildPlainRowNode(
ctx,
row,
paintStreamRow(row, layout),
plainRowWrapWidth(row, layout),
);
}
const gutter = streamRowGutter(row, layout);
const wrapper = new BoxRenderable(ctx, {
flexDirection: "row",
width: "100%",
});
wrapper.add(gutterNode(ctx, gutter));
wrapper.add(createMarkdownBody(ctx, row, gutter, layout));
return wrapper;
}
/** Shared construction options for a transcript markdown body's renderer. */
function markdownBodyOptions(gutter: PaintedStreamLine, width: number) {
return {
syntaxStyle: transcriptSyntaxStyle(),
fg: gutter.fg,
width,
flexShrink: 0,
tableOptions: TRANSCRIPT_TABLE_OPTIONS,
} as const;
}
/**
* A literal-text row's paint node: always a single text node, as before. Rows
* holding URLs paint styled text (URL spans carry OSC-8 metadata) and arm as
* Ctrl+click targets; URL-free rows paint the plain string they always have.
*/
function buildPlainRowNode(
ctx: CliRenderer,
row: StreamRow,
painted: PaintedStreamLine,
wrapWidth: number,
): TextRenderable {
const node = new TextRenderable(ctx, {
content: painted.content,
fg: painted.fg,
});
paintPlainRowNode(node, row, painted, wrapWidth);
return node;
}
/**
* Links a plain row's pre-wrap text holds: wrapped fragments reassemble to
* one of these, which is what tells a real wrap across a short fragment line
* apart from a natural line break after the fact.
*/
function plainRowSourceUrls(row: StreamRow): string[] {
return findLinks(`${row.text}\n${row.summary ?? ""}`).map((hit) => hit.url);
}
/**
* Rewrite a plain row's text on its existing node. The node never changes
* shape, so a URL appearing or disappearing repaints in place instead of
* forcing a rebuild.
*/
function paintPlainRowNode(
node: TextRenderable,
row: StreamRow,
painted: PaintedStreamLine,
wrapWidth: number,
): void {
const lines = painted.content.split("\n");
if (!lines.some((line) => findLinks(line).length > 0)) {
node.content = painted.content;
node.fg = painted.fg;
// Route through the armer so a retext that drops the last URL disarms
// the handlers a previous arming installed (armLinkLine clears them).
armLinkLine(node, []);
return;
}
paintLinkLine(
node,
splitWrappedLinkSpans(
lines.map((text) => ({ text: text.trimEnd(), fg: painted.fg })),
wrapWidth,
plainRowSourceUrls(row),
),
);
}
/**
* A markdown row's body. Most rows have no settled heading yet (no heading at
* all, or the only one is still the open tail), and paint through a single
* renderer, same as before this fix existed. Once a heading closes, the body
* becomes a settled `frozen` renderer — everything through that heading,
* never streaming, never handed new content while the tail keeps growing, so
* it is never asked to re-highlight once written — stacked above the still
* `live` one, which carries the row's own streaming flag. Both halves use the
* library's default block mode, so paragraphs, lists and tables inside either
* one lay out exactly as a single unsplit body would.
*/
function createMarkdownBody(
ctx: CliRenderer,
row: StreamRow,
gutter: PaintedStreamLine,
layout: RowLayout,
): MarkdownRenderable | BoxRenderable {
const width = markdownBodyColumns(gutter, layout);
const content = markdownContent(row);
const split = splitAtSettledHeading(content);
if (split === null) {
// Native incremental block stability: only the trailing block is unstable.
return new MarkdownRenderable(ctx, {
...markdownBodyOptions(gutter, width),
content,
streaming: row.streaming === true,
});
}
const column = new BoxRenderable(ctx, { flexDirection: "column", width });
column.add(
new MarkdownRenderable(ctx, {
...markdownBodyOptions(gutter, width),
content: split.frozen,
streaming: false,
}),
);
column.add(
new MarkdownRenderable(ctx, {
...markdownBodyOptions(gutter, width),
content: split.live,
streaming: row.streaming === true,
marginTop: split.gapRows,
}),
);
return column;
}
/**
* Gutter + one text line per body row, for bodies that arrive already coloured
* and already laid out (a diff, an expanded tool call's structured arguments).
* Each line paints inside the body column, so a wrapped line lands under the
* body rather than in the shell's gutter.
*/
function createStyledLinesRowRenderable(
ctx: CliRenderer,
row: StreamRow,
layout: RowLayout,
lines: readonly StyledBodyLine[],
onToggle?: () => void,
): BoxRenderable {
const gutter = streamRowGutter(row, layout);
const wrapper = new BoxRenderable(ctx, {
flexDirection: "row",
width: "100%",
});
wrapper.add(gutterNode(ctx, gutter));
const body = new BoxRenderable(ctx, {
flexDirection: "column",
flexGrow: 1,
});
for (const line of lines) {
body.add(bodyLineNode(ctx, line, onToggle));
}
wrapper.add(body);
return wrapper;
}
/**
* One painted body line. A line ending in an expand arrow is split so the
* arrow is its own renderable and can answer a click; a line holding URLs
* paints styled text and arms as a Ctrl+click target (see url-links.ts);
* every other line is a single text node, as before.
*/
function bodyLineNode(
ctx: CliRenderer,
line: StyledBodyLine,
onToggle?: () => void,
): TextRenderable | BoxRenderable {
const split = onToggle === undefined ? null : splitTrailingArrow(line);
if (split === null || onToggle === undefined) {
return buildLinkLine(ctx, splitLinkSpans(line));
}
const wrapper = new BoxRenderable(ctx, { flexDirection: "row", flexGrow: 1 });
const body = buildLinkLine(ctx, splitLinkSpans(split.body));
body.flexShrink = 0;
wrapper.add(body);
wrapper.add(
new TextRenderable(ctx, {
content: new StyledText(diffLineChunks([split.arrow])),
flexShrink: 0,
width: stringWidth(split.arrow.text),
onMouseDown: (event) => {
// The transcript scroll box drags on the same press; a toggle is not a
// scroll gesture, so the arrow keeps the event.
event.stopPropagation();
onToggle();
},
}),
);
return wrapper;
}
/**
* Gutter + native table body for a structured (MCP result) row, under the head
* lines the row collapses to. The head and the table share one body column so
* the table stays inside the shell's gutter.
*/
function createStructuredRowRenderable(
ctx: CliRenderer,
row: StreamRow,
layout: RowLayout,
view: McpStructuredView,
head: readonly StyledBodyLine[] = [],
onToggle?: () => void,
): BoxRenderable {
const gutter = streamRowGutter(row, layout);
const wrapper = new BoxRenderable(ctx, {
flexDirection: "row",
width: "100%",
});
wrapper.add(gutterNode(ctx, gutter));
const body = new BoxRenderable(ctx, { flexDirection: "column", flexGrow: 1 });
for (const line of head) {
body.add(bodyLineNode(ctx, line, onToggle));
}
body.add(
new TextTableRenderable(ctx, {
content: viewToTableContent(view),
columnWidthMode: "content",
columnGap: 2,
showBorders: false,
wrapMode: "none",
flexGrow: 1,
}),
);
wrapper.add(body);
return wrapper;
}
/**
* Bare key the modal overlay claims for its expand/collapse hook. Deliberately
* not in SHELL_SHORTCUTS: it is live only while an overlay that supplied
* `onToggleExpand` is open, so it never shadows a prompt binding.
*/
export const OVERLAY_EXPAND_KEY = EXPAND_KEY;