@causyne/agent 1.20.84 → 1.20.85
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 +1 -1
- package/src/tui/chatRenderer.js +24 -3
- package/src/tui/sessionSidebar.js +8 -1
- package/src/tui/sessionSidebar.ts +7 -1
- package/src/tui/stage.js +1 -1
- package/src/tui/stageSurface.js +21 -5
- package/src/tui/toolActivityPolicy.js +29 -0
- package/src/tui/turnRail.js +11 -3
- package/src/tui/turnRail.ts +10 -3
- package/src/tui/viewport.js +20 -4
- package/src/tui/viewport.ts +24 -2
- package/src/tui/viewportAnchor.d.ts +19 -0
- package/src/tui/viewportAnchor.js +77 -0
package/package.json
CHANGED
package/src/tui/chatRenderer.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// Pure JS over ./theme.js and ./text.js. No dependency, no shell.
|
|
10
10
|
|
|
11
11
|
import readline from 'readline';
|
|
12
|
+
import { compactSuccessfulToolResult } from './toolActivityPolicy.js';
|
|
12
13
|
import {
|
|
13
14
|
paint, bold, dim, badge, GLYPHS, terminalWidth, HAS_COLOR, fileLink,
|
|
14
15
|
safeLink, HAS_HYPERLINKS
|
|
@@ -1051,7 +1052,7 @@ export class ChatRenderer {
|
|
|
1051
1052
|
const detailText = detail ? paint('ink', truncate(detail, Math.max(10, width - used - 12), GLYPHS.ellipsis)) : '';
|
|
1052
1053
|
|
|
1053
1054
|
this.write(`${paint('muted', GLYPHS.arrow)} ${index} ${name} ${detailText}`);
|
|
1054
|
-
}, { step, tool: msg.tool });
|
|
1055
|
+
}, { step, tool: msg.tool, verb: verb || msg.tool, detail });
|
|
1055
1056
|
}
|
|
1056
1057
|
|
|
1057
1058
|
onStepComplete(msg) {
|
|
@@ -1065,6 +1066,23 @@ export class ChatRenderer {
|
|
|
1065
1066
|
// result the user finished with ten turns ago.
|
|
1066
1067
|
this.captureLastOutput(result);
|
|
1067
1068
|
|
|
1069
|
+
const tail = this.transcript?.frames?.at(-1);
|
|
1070
|
+
if (compactSuccessfulToolResult(this.stepCount, result, tail, msg?.tool)) {
|
|
1071
|
+
const { step, verb, detail, tool } = tail.meta;
|
|
1072
|
+
const completedElapsed = elapsed;
|
|
1073
|
+
this.updateTailFrame('step', () => {
|
|
1074
|
+
const width = this.width();
|
|
1075
|
+
const index = paint('muted', String(step).padStart(2, '0'));
|
|
1076
|
+
const name = paint('accent', padEnd(verb || tool, STATUS_GUTTER));
|
|
1077
|
+
const used = 2 + 2 + 1 + Math.max(visibleWidth(verb || tool), STATUS_GUTTER) + 1;
|
|
1078
|
+
const tailText = completedElapsed ? ` ${dim(completedElapsed)}` : '';
|
|
1079
|
+
const detailWidth = Math.max(5, width - used - visibleWidth(tailText) - 10);
|
|
1080
|
+
const detailText = detail ? paint('ink', truncate(detail, detailWidth, GLYPHS.ellipsis)) : '';
|
|
1081
|
+
this.write(`${paint('success', GLYPHS.check)} ${index} ${name} ${detailText}${tailText}`);
|
|
1082
|
+
}, { ...tail.meta, success: true, elapsed: completedElapsed });
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1068
1086
|
this.frame('result', () => {
|
|
1069
1087
|
if (result.success) {
|
|
1070
1088
|
const elapsedText = elapsed ? ` ${dim(elapsed)}` : '';
|
|
@@ -1154,10 +1172,13 @@ export class ChatRenderer {
|
|
|
1154
1172
|
if (wrapped.length > PREVIEW_LINES + 1) break;
|
|
1155
1173
|
}
|
|
1156
1174
|
|
|
1157
|
-
|
|
1175
|
+
// A long tool-heavy run needs a concise preview. Full output remains
|
|
1176
|
+
// available through the existing Ctrl+O expansion path and spill files.
|
|
1177
|
+
const previewLimit = this.stepCount >= 12 ? 5 : PREVIEW_LINES;
|
|
1178
|
+
for (const line of wrapped.slice(0, previewLimit)) {
|
|
1158
1179
|
this.write(` ${paint('rule', GLYPHS.vLine)} ${dim(line)}`);
|
|
1159
1180
|
}
|
|
1160
|
-
const clipped = wrapped.length >
|
|
1181
|
+
const clipped = wrapped.length > previewLimit || source.length < payload.length;
|
|
1161
1182
|
if (clipped) this.write(this.clipHint(result.spillPath));
|
|
1162
1183
|
}
|
|
1163
1184
|
|
|
@@ -78,10 +78,17 @@ export function sessionSidebarRows(facts, height) {
|
|
|
78
78
|
const completed = facts.plan.filter(item => item.status === 'completed').length;
|
|
79
79
|
line();
|
|
80
80
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
81
|
-
|
|
81
|
+
const planItems = facts.plan.length <= 6 ? facts.plan : [
|
|
82
|
+
...facts.plan.filter(item => item.status === 'in_progress').slice(0, 2),
|
|
83
|
+
...facts.plan.filter(item => item.status !== 'in_progress' && item.status !== 'completed').slice(0, 3),
|
|
84
|
+
...facts.plan.filter(item => item.status === 'completed').slice(-2)
|
|
85
|
+
].slice(0, 6);
|
|
86
|
+
for (const item of planItems) {
|
|
82
87
|
const mark = item.status === 'completed' ? '✓' : item.status === 'in_progress' ? '▸' : '·';
|
|
83
88
|
line(`${mark} ${item.text || item.step || item.content || ''}`);
|
|
84
89
|
}
|
|
90
|
+
if (facts.plan.length > planItems.length)
|
|
91
|
+
line(`${facts.plan.length - planItems.length} additional plan steps`);
|
|
85
92
|
}
|
|
86
93
|
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
87
94
|
const visible = rows.slice(0, count);
|
|
@@ -95,10 +95,16 @@ export function sessionSidebarRows(facts: SidebarFacts, height: number): string[
|
|
|
95
95
|
const completed = facts.plan.filter(item => item.status === 'completed').length;
|
|
96
96
|
line();
|
|
97
97
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
98
|
-
|
|
98
|
+
const planItems = facts.plan.length <= 6 ? facts.plan : [
|
|
99
|
+
...facts.plan.filter(item => item.status === 'in_progress').slice(0, 2),
|
|
100
|
+
...facts.plan.filter(item => item.status !== 'in_progress' && item.status !== 'completed').slice(0, 3),
|
|
101
|
+
...facts.plan.filter(item => item.status === 'completed').slice(-2)
|
|
102
|
+
].slice(0, 6);
|
|
103
|
+
for (const item of planItems) {
|
|
99
104
|
const mark = item.status === 'completed' ? '✓' : item.status === 'in_progress' ? '▸' : '·';
|
|
100
105
|
line(`${mark} ${item.text || item.step || item.content || ''}`);
|
|
101
106
|
}
|
|
107
|
+
if (facts.plan.length > planItems.length) line(`${facts.plan.length - planItems.length} additional plan steps`);
|
|
102
108
|
}
|
|
103
109
|
|
|
104
110
|
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
package/src/tui/stage.js
CHANGED
|
@@ -232,7 +232,7 @@ export class Stage {
|
|
|
232
232
|
|
|
233
233
|
const next = this.normalize(rows);
|
|
234
234
|
const target = caret && Number.isFinite(caret.row)
|
|
235
|
-
? { row: Math.max(0, Math.min(Math.floor(caret.row), this.height - 1)), col: Math.max(0, Math.floor(caret.col) || 0), hideCursor: Boolean(caret.hideCursor) }
|
|
235
|
+
? { row: Math.max(0, Math.min(Math.floor(caret.row), this.height - 1)), col: Math.max(0, Math.min(this.width() - 2, Math.floor(caret.col) || 0)), hideCursor: Boolean(caret.hideCursor) }
|
|
236
236
|
: { row: this.height - 1, col: 0, hideCursor: Boolean(caret?.hideCursor) };
|
|
237
237
|
|
|
238
238
|
const changed = [];
|
package/src/tui/stageSurface.js
CHANGED
|
@@ -57,6 +57,8 @@ const MIN_VIEWPORT_ROWS = 2;
|
|
|
57
57
|
const RAIL_MIN_WIDTH = 60;
|
|
58
58
|
const RAIL_WIDTH = 2;
|
|
59
59
|
const RAIL_BUDGET = RAIL_WIDTH + 1;
|
|
60
|
+
// The interactive turn rail takes three columns at the left of each row.
|
|
61
|
+
const TURN_RAIL_BUDGET = 3;
|
|
60
62
|
|
|
61
63
|
// Which marker wins when several frames fall in the same rail row. A proportional
|
|
62
64
|
// rail on a long session compresses dozens of frames per row, and what you want
|
|
@@ -373,7 +375,9 @@ export class StageSurface {
|
|
|
373
375
|
if (!this.transcript.isEmpty()) this.opened = true;
|
|
374
376
|
const before = this.lastHeight;
|
|
375
377
|
const mainWidth = sessionMainWidth(this.width);
|
|
376
|
-
const contentWidth = this.railEnabled(mainWidth)
|
|
378
|
+
const contentWidth = this.railEnabled(mainWidth)
|
|
379
|
+
? Math.max(20, mainWidth - RAIL_BUDGET - (hasSessionTurns(this.transcript) ? TURN_RAIL_BUDGET : 0))
|
|
380
|
+
: mainWidth;
|
|
377
381
|
const after = this.transcript.height(contentWidth);
|
|
378
382
|
this.lastHeight = after;
|
|
379
383
|
// Growth is what drives auto-scroll. Handing the delta over is also what
|
|
@@ -426,7 +430,9 @@ export class StageSurface {
|
|
|
426
430
|
const sidebar = sidebarVisible(width);
|
|
427
431
|
const mainWidth = sessionMainWidth(width);
|
|
428
432
|
const rail = this.railEnabled(mainWidth);
|
|
429
|
-
const contentWidth = rail
|
|
433
|
+
const contentWidth = rail
|
|
434
|
+
? Math.max(20, mainWidth - RAIL_BUDGET - (hasSessionTurns(this.transcript) ? TURN_RAIL_BUDGET : 0))
|
|
435
|
+
: mainWidth;
|
|
430
436
|
|
|
431
437
|
let block = promptRows;
|
|
432
438
|
let caretRow = caret && Number.isFinite(caret.row) ? caret.row : Math.max(0, block.length - 1);
|
|
@@ -536,7 +542,9 @@ export class StageSurface {
|
|
|
536
542
|
const painted = sidebar ? joinSessionSidebar(mainRows, panelRows, mainWidth) : mainRows;
|
|
537
543
|
return this.stage.paint(painted, {
|
|
538
544
|
row: rows.length + caretRow,
|
|
539
|
-
|
|
545
|
+
// The composer may never put its caret into the sidebar or the reserved
|
|
546
|
+
// right margin, even if layout measurements differ after a resize.
|
|
547
|
+
col: Math.max(0, Math.min(mainWidth - 2, Number.isFinite(caretCol) ? Math.floor(caretCol) : 0)),
|
|
540
548
|
hideCursor: caret?.hideCursor
|
|
541
549
|
});
|
|
542
550
|
}
|
|
@@ -624,7 +632,9 @@ export class StageSurface {
|
|
|
624
632
|
// Re-derive from the new geometry: growing the viewport lowers maxScrollTop,
|
|
625
633
|
// and leaving scrollTop above it would read as "the user scrolled away".
|
|
626
634
|
const mainWidth = sessionMainWidth(this.width);
|
|
627
|
-
const contentWidth = this.railEnabled(mainWidth)
|
|
635
|
+
const contentWidth = this.railEnabled(mainWidth)
|
|
636
|
+
? Math.max(20, mainWidth - RAIL_BUDGET - (hasSessionTurns(this.transcript) ? TURN_RAIL_BUDGET : 0))
|
|
637
|
+
: mainWidth;
|
|
628
638
|
this.viewport.resize({ height: Math.max(MIN_VIEWPORT_ROWS, this.stage.height - 2), width: contentWidth });
|
|
629
639
|
this.lastHeight = this.transcript.height(contentWidth);
|
|
630
640
|
return this.stage.reserved;
|
|
@@ -656,6 +666,9 @@ export class StageSurface {
|
|
|
656
666
|
|
|
657
667
|
const kinds = new Array(height).fill(null);
|
|
658
668
|
for (const marker of this.transcript.markers(contentWidth)) {
|
|
669
|
+
// Suppress repetitive step/result/reasoning noise on the overview rail.
|
|
670
|
+
// Checkpoints, user turns, approvals, and errors remain discoverable.
|
|
671
|
+
if ((RAIL_PRIORITY[marker.kind] || 0) < 50) continue;
|
|
659
672
|
const slot = slotOf(marker.row);
|
|
660
673
|
const held = kinds[slot];
|
|
661
674
|
if (!held || (RAIL_PRIORITY[marker.kind] || 0) > (RAIL_PRIORITY[held] || 0)) {
|
|
@@ -722,7 +735,10 @@ export class StageSurface {
|
|
|
722
735
|
row = ` ${leftCell} ${row}`;
|
|
723
736
|
}
|
|
724
737
|
}
|
|
725
|
-
|
|
738
|
+
// Turn markers consume their own three-column lane. Preserve ALL
|
|
739
|
+
// previously rendered command text instead of clipping its last cells.
|
|
740
|
+
const rowWidth = contentWidth + (hasTurns ? TURN_RAIL_BUDGET : 0);
|
|
741
|
+
rows[i] = `${padEnd(truncate(row, rowWidth, ''), rowWidth)} ${rightCell}`;
|
|
726
742
|
}
|
|
727
743
|
return rows;
|
|
728
744
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the full execution trail, but collapse the second, visually redundant
|
|
3
|
+
* success row once a long tool-heavy run is underway. A failure, a non-empty
|
|
4
|
+
* output, a diagnostic, or any missing result stays expanded.
|
|
5
|
+
*
|
|
6
|
+
* Only the presentation changes. Original tool results still pass through
|
|
7
|
+
* captureLastOutput() and the AgentLoop is not affected.
|
|
8
|
+
*/
|
|
9
|
+
export const TOOL_ACTIVITY_COMPACT_AFTER = 8;
|
|
10
|
+
|
|
11
|
+
export function compactSuccessfulToolResult(stepCount, result, tail, toolName) {
|
|
12
|
+
if (!Number.isFinite(stepCount) || stepCount < TOOL_ACTIVITY_COMPACT_AFTER) return false;
|
|
13
|
+
if (!result || result.success !== true) return false;
|
|
14
|
+
if (!tail || tail.kind !== 'step' || !tail.meta) return false;
|
|
15
|
+
if (tail.meta.step !== stepCount || String(tail.meta.tool ?? '') !== String(toolName ?? '')) return false;
|
|
16
|
+
// Only truly silent successes qualify. Unknown provider-specific result
|
|
17
|
+
// fields must remain visible, rather than being hidden by an allowlist of
|
|
18
|
+
// familiar output property names.
|
|
19
|
+
const metadataOnly = new Set([
|
|
20
|
+
'success', 'exitCode', 'exit_code', 'durationMs', 'elapsedMs', 'duration',
|
|
21
|
+
'statusCode', 'status_code'
|
|
22
|
+
]);
|
|
23
|
+
for (const [key, value] of Object.entries(result)) {
|
|
24
|
+
if (metadataOnly.has(key)) continue;
|
|
25
|
+
if (value == null || value === '' || (Array.isArray(value) && value.length === 0)) continue;
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
package/src/tui/turnRail.js
CHANGED
|
@@ -168,6 +168,12 @@ export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIn
|
|
|
168
168
|
return { cells, layout: { dotRows, rowToTurn } };
|
|
169
169
|
}
|
|
170
170
|
const count = turns.length;
|
|
171
|
+
// Dense imported histories may contain hundreds of turns. Show a measured
|
|
172
|
+
// set of quiet milestones, while always retaining active/hovered endpoints.
|
|
173
|
+
// All turn objects are preserved for keyboard navigation and preview.
|
|
174
|
+
const quietStep = count > Math.max(16, Math.floor(height / 2))
|
|
175
|
+
? Math.max(2, Math.ceil(count / Math.max(12, Math.floor(height / 3))))
|
|
176
|
+
: 1;
|
|
171
177
|
// If count fits comfortably, stack consecutive dots centered or upper-aligned
|
|
172
178
|
const startRow = count <= height
|
|
173
179
|
? Math.max(0, Math.floor((height - count) / 3))
|
|
@@ -186,9 +192,11 @@ export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIn
|
|
|
186
192
|
}
|
|
187
193
|
rowIdx = Math.max(0, Math.min(height - 1, rowIdx));
|
|
188
194
|
dotRows.set(turnNum, rowIdx);
|
|
189
|
-
rowToTurn.set(rowIdx, turnNum);
|
|
190
195
|
const isHovered = hoveredTurnIndex === turnNum;
|
|
191
196
|
const isActive = activeTurnIndex === turnNum;
|
|
197
|
+
const showQuiet = quietStep === 1 || i === 0 || i === count - 1 || i % quietStep === 0;
|
|
198
|
+
if (isHovered || isActive || showQuiet)
|
|
199
|
+
rowToTurn.set(rowIdx, turnNum);
|
|
192
200
|
if (isHovered) {
|
|
193
201
|
// Hovered dot: bright silver highlighted circle
|
|
194
202
|
cells[rowIdx] = '\x1b[1m\x1b[38;2;230;235;245m●\x1b[0m';
|
|
@@ -197,8 +205,8 @@ export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIn
|
|
|
197
205
|
// Active / current turn: bright white circle
|
|
198
206
|
cells[rowIdx] = '\x1b[1m\x1b[38;2;250;250;250m●\x1b[0m';
|
|
199
207
|
}
|
|
200
|
-
else if (!cells[rowIdx]?.includes('●')) {
|
|
201
|
-
// Muted inactive turns:
|
|
208
|
+
else if (showQuiet && !cells[rowIdx]?.includes('●')) {
|
|
209
|
+
// Muted inactive turns: sparse gray dot in long sessions
|
|
202
210
|
cells[rowIdx] = '\x1b[38;2;115;115;120m•\x1b[0m';
|
|
203
211
|
}
|
|
204
212
|
}
|
package/src/tui/turnRail.ts
CHANGED
|
@@ -214,6 +214,12 @@ export function buildTurnRailCells(
|
|
|
214
214
|
}
|
|
215
215
|
|
|
216
216
|
const count = turns.length;
|
|
217
|
+
// Dense imported histories may contain hundreds of turns. Show a measured
|
|
218
|
+
// set of quiet milestones, while always retaining active/hovered endpoints.
|
|
219
|
+
// All turn objects are preserved for keyboard navigation and preview.
|
|
220
|
+
const quietStep = count > Math.max(16, Math.floor(height / 2))
|
|
221
|
+
? Math.max(2, Math.ceil(count / Math.max(12, Math.floor(height / 3))))
|
|
222
|
+
: 1;
|
|
217
223
|
// If count fits comfortably, stack consecutive dots centered or upper-aligned
|
|
218
224
|
const startRow = count <= height
|
|
219
225
|
? Math.max(0, Math.floor((height - count) / 3))
|
|
@@ -233,10 +239,11 @@ export function buildTurnRailCells(
|
|
|
233
239
|
rowIdx = Math.max(0, Math.min(height - 1, rowIdx));
|
|
234
240
|
|
|
235
241
|
dotRows.set(turnNum, rowIdx);
|
|
236
|
-
rowToTurn.set(rowIdx, turnNum);
|
|
237
242
|
|
|
238
243
|
const isHovered = hoveredTurnIndex === turnNum;
|
|
239
244
|
const isActive = activeTurnIndex === turnNum;
|
|
245
|
+
const showQuiet = quietStep === 1 || i === 0 || i === count - 1 || i % quietStep === 0;
|
|
246
|
+
if (isHovered || isActive || showQuiet) rowToTurn.set(rowIdx, turnNum);
|
|
240
247
|
|
|
241
248
|
if (isHovered) {
|
|
242
249
|
// Hovered dot: bright silver highlighted circle
|
|
@@ -244,8 +251,8 @@ export function buildTurnRailCells(
|
|
|
244
251
|
} else if (isActive && !cells[rowIdx]?.includes('230;235;245m')) {
|
|
245
252
|
// Active / current turn: bright white circle
|
|
246
253
|
cells[rowIdx] = '\x1b[1m\x1b[38;2;250;250;250m●\x1b[0m';
|
|
247
|
-
} else if (!cells[rowIdx]?.includes('●')) {
|
|
248
|
-
// Muted inactive turns:
|
|
254
|
+
} else if (showQuiet && !cells[rowIdx]?.includes('●')) {
|
|
255
|
+
// Muted inactive turns: sparse gray dot in long sessions
|
|
249
256
|
cells[rowIdx] = '\x1b[38;2;115;115;120m•\x1b[0m';
|
|
250
257
|
}
|
|
251
258
|
}
|
package/src/tui/viewport.js
CHANGED
|
@@ -82,6 +82,7 @@ export class ScrollAcceleration {
|
|
|
82
82
|
// How close to the bottom counts as "back at the bottom" for the purposes of
|
|
83
83
|
// resuming follow.
|
|
84
84
|
const REENGAGE_TOLERANCE = 1;
|
|
85
|
+
import { captureReadingAnchor, restoreReadingAnchor } from './viewportAnchor.js';
|
|
85
86
|
export class Viewport {
|
|
86
87
|
transcript;
|
|
87
88
|
viewHeight;
|
|
@@ -115,10 +116,25 @@ export class Viewport {
|
|
|
115
116
|
* Geometry changed — a resize, or the input box growing under the viewport.
|
|
116
117
|
*/
|
|
117
118
|
resize({ height, width } = {}) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
const nextHeight = height !== undefined && Number.isFinite(height) && height > 0
|
|
120
|
+
? Math.floor(height) : this.viewHeight;
|
|
121
|
+
const nextWidth = width !== undefined && Number.isFinite(width) && width > 0
|
|
122
|
+
? Math.floor(width) : this.viewWidth;
|
|
123
|
+
const reflow = nextWidth !== this.viewWidth;
|
|
124
|
+
const anchor = reflow && this.manual && this.transcript?.markers
|
|
125
|
+
? captureReadingAnchor(this.transcript.markers(this.viewWidth), this.scrollTop, this.transcript.height(this.viewWidth)) : null;
|
|
126
|
+
this.viewHeight = nextHeight;
|
|
127
|
+
this.viewWidth = nextWidth;
|
|
128
|
+
if (anchor && this.transcript?.markers) {
|
|
129
|
+
this.scrollTop = restoreReadingAnchor(anchor, this.transcript.markers(nextWidth), this.transcript.height(nextWidth), nextHeight);
|
|
130
|
+
const max = this.maxScrollTop();
|
|
131
|
+
// A deliberate scroll must not suddenly become follow-mode just because
|
|
132
|
+
// a paragraph gained or lost wrapped rows during resize.
|
|
133
|
+
this.manual = max > 0 && this.scrollTop < max;
|
|
134
|
+
if (!this.manual)
|
|
135
|
+
this.unseen = 0;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
122
138
|
this.refresh();
|
|
123
139
|
}
|
|
124
140
|
contentHeight() {
|
package/src/tui/viewport.ts
CHANGED
|
@@ -96,7 +96,10 @@ export class ScrollAcceleration {
|
|
|
96
96
|
// resuming follow.
|
|
97
97
|
const REENGAGE_TOLERANCE = 1;
|
|
98
98
|
|
|
99
|
+
import { captureReadingAnchor, restoreReadingAnchor } from './viewportAnchor.js';
|
|
100
|
+
|
|
99
101
|
export interface TranscriptMarker {
|
|
102
|
+
id?: number;
|
|
100
103
|
row: number;
|
|
101
104
|
kind: string;
|
|
102
105
|
}
|
|
@@ -172,8 +175,27 @@ export class Viewport {
|
|
|
172
175
|
* Geometry changed — a resize, or the input box growing under the viewport.
|
|
173
176
|
*/
|
|
174
177
|
resize({ height, width }: { height?: number; width?: number } = {}): void {
|
|
175
|
-
|
|
176
|
-
|
|
178
|
+
const nextHeight = height !== undefined && Number.isFinite(height) && height > 0
|
|
179
|
+
? Math.floor(height) : this.viewHeight;
|
|
180
|
+
const nextWidth = width !== undefined && Number.isFinite(width) && width > 0
|
|
181
|
+
? Math.floor(width) : this.viewWidth;
|
|
182
|
+
const reflow = nextWidth !== this.viewWidth;
|
|
183
|
+
const anchor = reflow && this.manual && this.transcript?.markers
|
|
184
|
+
? captureReadingAnchor(this.transcript.markers(this.viewWidth), this.scrollTop,
|
|
185
|
+
this.transcript.height(this.viewWidth)) : null;
|
|
186
|
+
|
|
187
|
+
this.viewHeight = nextHeight;
|
|
188
|
+
this.viewWidth = nextWidth;
|
|
189
|
+
if (anchor && this.transcript?.markers) {
|
|
190
|
+
this.scrollTop = restoreReadingAnchor(anchor, this.transcript.markers(nextWidth),
|
|
191
|
+
this.transcript.height(nextWidth), nextHeight);
|
|
192
|
+
const max = this.maxScrollTop();
|
|
193
|
+
// A deliberate scroll must not suddenly become follow-mode just because
|
|
194
|
+
// a paragraph gained or lost wrapped rows during resize.
|
|
195
|
+
this.manual = max > 0 && this.scrollTop < max;
|
|
196
|
+
if (!this.manual) this.unseen = 0;
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
177
199
|
this.refresh();
|
|
178
200
|
}
|
|
179
201
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ReadingMarker { id?: number; row: number; kind?: string }
|
|
2
|
+
export interface ReadingAnchor {
|
|
3
|
+
readonly id: number | null;
|
|
4
|
+
readonly index: number;
|
|
5
|
+
readonly offset: number;
|
|
6
|
+
readonly fraction: number;
|
|
7
|
+
readonly originalTop: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function captureReadingAnchor(
|
|
10
|
+
markers: ReadingMarker[] | null | undefined,
|
|
11
|
+
top: number,
|
|
12
|
+
totalHeight: number
|
|
13
|
+
): ReadingAnchor | null;
|
|
14
|
+
export declare function restoreReadingAnchor(
|
|
15
|
+
anchor: ReadingAnchor | null,
|
|
16
|
+
markers: ReadingMarker[] | null | undefined,
|
|
17
|
+
totalHeight: number,
|
|
18
|
+
viewportHeight: number
|
|
19
|
+
): number;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preserve the reader's location while terminal geometry changes.
|
|
3
|
+
*
|
|
4
|
+
* Row indices are screen-layout positions, not document positions. Re-wrapping a
|
|
5
|
+
* transcript changes frame heights, so keeping the same numeric scrollTop can
|
|
6
|
+
* jump to unrelated content. Frame identity + relative offset is stable across
|
|
7
|
+
* width changes, without storing or duplicating any message content.
|
|
8
|
+
*
|
|
9
|
+
* This module is deliberately independent of Stage, Transcript, and Node I/O.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
function finiteRow(value) {
|
|
13
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function safeMarkers(markers) {
|
|
17
|
+
if (!Array.isArray(markers)) return [];
|
|
18
|
+
return markers
|
|
19
|
+
.filter(marker => marker && Number.isFinite(marker.row) && marker.row >= 0)
|
|
20
|
+
.map((marker, index) => ({
|
|
21
|
+
id: marker.id,
|
|
22
|
+
row: finiteRow(marker.row),
|
|
23
|
+
originalIndex: index
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Capture an anchor at the first visible row, measured within its frame. */
|
|
28
|
+
export function captureReadingAnchor(markers, top, totalHeight) {
|
|
29
|
+
const entries = safeMarkers(markers);
|
|
30
|
+
if (!entries.length) return null;
|
|
31
|
+
const row = finiteRow(top);
|
|
32
|
+
let selected = 0;
|
|
33
|
+
for (let i = 0; i < entries.length; i++) {
|
|
34
|
+
if (entries[i].row > row) break;
|
|
35
|
+
selected = i;
|
|
36
|
+
}
|
|
37
|
+
const start = entries[selected].row;
|
|
38
|
+
const next = entries[selected + 1]?.row ?? finiteRow(totalHeight);
|
|
39
|
+
const frameLength = Math.max(1, next - start);
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
id: entries[selected].id ?? null,
|
|
42
|
+
index: entries[selected].originalIndex,
|
|
43
|
+
offset: Math.max(0, row - start),
|
|
44
|
+
fraction: Math.max(0, Math.min(1, (row - start) / frameLength)),
|
|
45
|
+
originalTop: row
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Restore to the corresponding frame and approximately the same content line. */
|
|
50
|
+
export function restoreReadingAnchor(anchor, markers, totalHeight, viewportHeight) {
|
|
51
|
+
const total = finiteRow(totalHeight);
|
|
52
|
+
const visible = Math.max(1, finiteRow(viewportHeight));
|
|
53
|
+
const max = Math.max(0, total - visible);
|
|
54
|
+
if (!anchor || !max) return 0;
|
|
55
|
+
const entries = safeMarkers(markers);
|
|
56
|
+
if (!entries.length) return Math.min(max, finiteRow(anchor.originalTop));
|
|
57
|
+
|
|
58
|
+
let selected = anchor.id == null
|
|
59
|
+
? -1
|
|
60
|
+
: entries.findIndex(marker => marker.id === anchor.id);
|
|
61
|
+
if (selected < 0 && anchor.id == null && entries[anchor.index]?.originalIndex === anchor.index) {
|
|
62
|
+
selected = anchor.index;
|
|
63
|
+
}
|
|
64
|
+
if (selected < 0) {
|
|
65
|
+
// Frames may have been evicted while the user was reading. Use the first
|
|
66
|
+
// surviving frame rather than silently jumping to an unrelated old row.
|
|
67
|
+
selected = 0;
|
|
68
|
+
}
|
|
69
|
+
const start = entries[selected].row;
|
|
70
|
+
const end = entries[selected + 1]?.row ?? total;
|
|
71
|
+
const length = Math.max(1, end - start);
|
|
72
|
+
const fraction = Number.isFinite(anchor.fraction) ? Math.max(0, Math.min(1, anchor.fraction)) : 0;
|
|
73
|
+
// Relative position is more useful than raw offset after a long paragraph
|
|
74
|
+
// wraps from 15 lines to 30, while staying inside the same frame.
|
|
75
|
+
const offset = Math.min(length - 1, Math.round(fraction * length));
|
|
76
|
+
return Math.max(0, Math.min(max, start + offset));
|
|
77
|
+
}
|