@causyne/agent 1.20.12 → 1.20.14
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/LICENSE +1 -1
- package/package.json +1 -1
- package/src/SmartTools.js +33 -0
- package/src/SmartTools.ts +37 -0
- package/src/acp/client.js +157 -4
- package/src/acp/client.ts +152 -5
- package/src/agentLoop.js +857 -178
- package/src/agentLoop.ts +865 -183
- package/src/browser/browserRuntime.js +8 -1
- package/src/browser/browserRuntime.ts +8 -1
- package/src/browser/causyneBrowser.js +20 -4
- package/src/browser/causyneBrowser.ts +20 -4
- package/src/browser/urlPolicy.js +35 -0
- package/src/browser/urlPolicy.ts +34 -0
- package/src/cliMode.js +6 -2
- package/src/commands.js +14 -0
- package/src/contextManager.d.ts +1 -0
- package/src/core/contextCompactor.js +277 -0
- package/src/core/contextCompactor.ts +353 -0
- package/src/decideTools.js +157 -0
- package/src/decideTools.ts +195 -0
- package/src/decisionPolicy.js +5 -3
- package/src/decisionPolicy.ts +5 -3
- package/src/implementationPlan.js +284 -0
- package/src/implementationPlan.ts +275 -0
- package/src/observer/coldObserver.js +10 -4
- package/src/observer/coldObserver.ts +15 -7
- package/src/observer/evidence.js +11 -5
- package/src/observer/evidence.ts +13 -6
- package/src/observer.js +196 -0
- package/src/observer.ts +287 -0
- package/src/providers/acp.js +362 -109
- package/src/providers/acp.ts +359 -111
- package/src/providers/antigravity.js +133 -105
- package/src/providers/antigravity.ts +130 -109
- package/src/providers/base.d.ts +3 -0
- package/src/providers/budgetEnforcer.js +13 -0
- package/src/providers/budgetEnforcer.ts +13 -3
- package/src/provisioning/capabilityProvisioner.js +14 -10
- package/src/sandbox.js +4 -2
- package/src/saveTools.js +146 -0
- package/src/saveTools.ts +169 -0
- package/src/session/cachePressure.js +77 -0
- package/src/session/cachePressure.ts +122 -0
- package/src/smartToolsUse.js +69 -12
- package/src/smartToolsUse.ts +103 -12
- package/src/taskShape.js +7 -7
- package/src/taskShape.ts +7 -7
- package/src/timeMachine.js +55 -20
- package/src/timeMachine.ts +64 -20
- package/src/toolPacks.js +217 -310
- package/src/toolPacks.ts +505 -0
- package/src/tools/deferredMcpSearch.js +21 -13
- package/src/tools/deferredMcpSearch.ts +23 -15
- package/src/tools/updatePlan.js +2 -2
- package/src/tools/updatePlan.ts +2 -2
- package/src/toolsList.js +203 -0
- package/src/toolsList.ts +224 -0
- package/src/tui/chatRenderer.js +49 -14
- package/src/tui/codeStreamRenderer.js +16 -5
- package/src/tui/codeStreamRenderer.ts +14 -4
- package/src/tui/prompt.js +4 -2
- package/src/tui/sessionSidebar.js +10 -2
- package/src/tui/sessionSidebar.ts +10 -3
- package/src/tui/stageSurface.js +27 -1
- package/src/tui/text.d.ts +1 -0
- package/src/tui/turnRail.js +274 -0
- package/src/tui/turnRail.ts +334 -0
- package/src/types/contracts.ts +103 -0
- package/src/verificationPlan.js +141 -0
- package/src/verificationPlan.ts +166 -0
- package/src/toolPacks.d.ts +0 -10
|
@@ -76,15 +76,26 @@ export class CodeStreamRenderer extends ChatRenderer {
|
|
|
76
76
|
const note = content.trim().split('\n').at(-1) || '';
|
|
77
77
|
this.modelNote = note.slice(-140);
|
|
78
78
|
this.paintCodePanel(this.activeCode);
|
|
79
|
+
if (message.streaming === false)
|
|
80
|
+
this.lastReasoning = '';
|
|
79
81
|
return true;
|
|
80
82
|
}
|
|
83
|
+
const handled = super.handle(message);
|
|
84
|
+
if (message.streaming === false)
|
|
85
|
+
this.lastReasoning = '';
|
|
86
|
+
return handled;
|
|
81
87
|
}
|
|
82
|
-
if (['agent_message', 'step_start', 'task_complete', 'error'].includes(message?.type || '')) {
|
|
88
|
+
if (['agent_message', 'agent_activity', 'notice', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')) {
|
|
89
|
+
if (this._activeReasoning && this.lastReasoning) {
|
|
90
|
+
super.onAgentReasoning({ content: this.lastReasoning, streaming: false });
|
|
91
|
+
}
|
|
83
92
|
this._activeReasoning = false;
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.
|
|
93
|
+
this.lastReasoning = '';
|
|
94
|
+
if (['step_start', 'task_complete', 'error'].includes(message?.type || '')
|
|
95
|
+
|| (message?.type === 'agent_message' && message.streaming !== true)) {
|
|
96
|
+
this.activeCode = null;
|
|
97
|
+
this.modelNote = '';
|
|
98
|
+
}
|
|
88
99
|
}
|
|
89
100
|
return super.handle(message);
|
|
90
101
|
}
|
|
@@ -90,14 +90,24 @@ export class CodeStreamRenderer extends ChatRenderer {
|
|
|
90
90
|
const note = content.trim().split('\n').at(-1) || '';
|
|
91
91
|
this.modelNote = note.slice(-140);
|
|
92
92
|
this.paintCodePanel(this.activeCode);
|
|
93
|
+
if (message.streaming === false) this.lastReasoning = '';
|
|
93
94
|
return true;
|
|
94
95
|
}
|
|
96
|
+
const handled = super.handle(message);
|
|
97
|
+
if (message.streaming === false) this.lastReasoning = '';
|
|
98
|
+
return handled;
|
|
95
99
|
}
|
|
96
|
-
if (['agent_message', 'step_start', 'task_complete', 'error'].includes(message?.type || '')) {
|
|
100
|
+
if (['agent_message', 'agent_activity', 'notice', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')) {
|
|
101
|
+
if (this._activeReasoning && this.lastReasoning) {
|
|
102
|
+
super.onAgentReasoning({ content: this.lastReasoning, streaming: false });
|
|
103
|
+
}
|
|
97
104
|
this._activeReasoning = false;
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
|
|
105
|
+
this.lastReasoning = '';
|
|
106
|
+
if (['step_start', 'task_complete', 'error'].includes(message?.type || '')
|
|
107
|
+
|| (message?.type === 'agent_message' && message.streaming !== true)) {
|
|
108
|
+
this.activeCode = null;
|
|
109
|
+
this.modelNote = '';
|
|
110
|
+
}
|
|
101
111
|
}
|
|
102
112
|
return super.handle(message);
|
|
103
113
|
}
|
package/src/tui/prompt.js
CHANGED
|
@@ -35,6 +35,7 @@ import { settleStageResize } from './resizeRecovery.js';
|
|
|
35
35
|
import { sidebarVisible, sessionMainWidth } from './sessionSidebar.js';
|
|
36
36
|
import { isStartupDialog, startupDialogView } from './homeView.js';
|
|
37
37
|
import { handleMessageMouse, handleMessageActionMenuMouse, isMouseMotion } from './messageInteraction.js';
|
|
38
|
+
import { handleTurnRailMouse } from './turnRail.js';
|
|
38
39
|
import {
|
|
39
40
|
resetTurnInterruptState,
|
|
40
41
|
shouldAcceptStatusUpdate,
|
|
@@ -899,6 +900,7 @@ export class ChatPrompt {
|
|
|
899
900
|
// future case could act on it by accident — and, worse, a terminal that
|
|
900
901
|
// kept reporting after a failed teardown would type into the user's line.
|
|
901
902
|
if (key.name === 'mouse') {
|
|
903
|
+
if (handleTurnRailMouse(key, this.surface, this)) return true;
|
|
902
904
|
if (this.messageActionLoop && isMouseMotion(key.sequence)) {
|
|
903
905
|
return handleMessageMouse(this, this.messageActionLoop, key);
|
|
904
906
|
}
|
|
@@ -953,8 +955,8 @@ export class ChatPrompt {
|
|
|
953
955
|
// sit at almost the same seam — a footer is immediately followed by the next
|
|
954
956
|
// question — so accepting either made the jump land twice two rows apart
|
|
955
957
|
// instead of once per exchange.
|
|
956
|
-
if (key.alt && key.name === '<') { viewport.jump(-1, ['user']); return true; }
|
|
957
|
-
if (key.alt && key.name === '>') { viewport.jump(1, ['user']); return true; }
|
|
958
|
+
if (key.name === '[' || (key.alt && key.name === '<')) { viewport.jump(-1, ['user']); return true; }
|
|
959
|
+
if (key.name === ']' || (key.alt && key.name === '>')) { viewport.jump(1, ['user']); return true; }
|
|
958
960
|
|
|
959
961
|
if (key.alt && key.name === 'u') { this.offerUndo(); return true; }
|
|
960
962
|
|
|
@@ -6,6 +6,11 @@ export const SIDEBAR_WIDTH = 42;
|
|
|
6
6
|
export const SIDEBAR_THRESHOLD = 120;
|
|
7
7
|
/** A side panel is useful only when a readable conversation column remains. */
|
|
8
8
|
export function sidebarVisible(columns, home = false) {
|
|
9
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
10
|
+
const setting = String(process.env.CAUSYNE_SIDEBAR ?? '').trim().toLowerCase();
|
|
11
|
+
if (setting === 'off' || setting === '0' || setting === 'false')
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
9
14
|
return !home && Number.isFinite(columns) && columns > SIDEBAR_THRESHOLD;
|
|
10
15
|
}
|
|
11
16
|
/** Reserve the last terminal column so a wide row cannot wrap the stage. */
|
|
@@ -37,7 +42,10 @@ export function sessionSidebarRows(facts, height) {
|
|
|
37
42
|
const limit = Number.isFinite(facts.contextLimit) ? Number(facts.contextLimit) : 0;
|
|
38
43
|
const used = Number.isFinite(facts.contextChars) ? Number(facts.contextChars) : 0;
|
|
39
44
|
if (limit > 0) {
|
|
40
|
-
const
|
|
45
|
+
const rawPercent = (used / limit) * 100;
|
|
46
|
+
const percent = used > 0
|
|
47
|
+
? Math.max(1, Math.min(100, Math.round(rawPercent)))
|
|
48
|
+
: 0;
|
|
41
49
|
styled(dim(`${percent}% of available window`));
|
|
42
50
|
}
|
|
43
51
|
else {
|
|
@@ -55,7 +63,7 @@ export function sessionSidebarRows(facts, height) {
|
|
|
55
63
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
56
64
|
for (const item of facts.plan) {
|
|
57
65
|
const mark = item.status === 'completed' ? '✓' : item.status === 'in_progress' ? '▸' : '·';
|
|
58
|
-
line(`${mark} ${item.text || item.step || ''}`);
|
|
66
|
+
line(`${mark} ${item.text || item.step || item.content || ''}`);
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
@@ -14,11 +14,15 @@ export interface SidebarFacts {
|
|
|
14
14
|
model?: string;
|
|
15
15
|
contextChars?: number;
|
|
16
16
|
contextLimit?: number;
|
|
17
|
-
plan?: Array<{ text?: string; step?: string; status?: string }>;
|
|
17
|
+
plan?: Array<{ text?: string; step?: string; content?: string; status?: string }>;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/** A side panel is useful only when a readable conversation column remains. */
|
|
21
21
|
export function sidebarVisible(columns: number, home = false): boolean {
|
|
22
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
23
|
+
const setting = String(process.env.CAUSYNE_SIDEBAR ?? '').trim().toLowerCase();
|
|
24
|
+
if (setting === 'off' || setting === '0' || setting === 'false') return false;
|
|
25
|
+
}
|
|
22
26
|
return !home && Number.isFinite(columns) && columns > SIDEBAR_THRESHOLD;
|
|
23
27
|
}
|
|
24
28
|
|
|
@@ -53,7 +57,10 @@ export function sessionSidebarRows(facts: SidebarFacts, height: number): string[
|
|
|
53
57
|
const limit = Number.isFinite(facts.contextLimit) ? Number(facts.contextLimit) : 0;
|
|
54
58
|
const used = Number.isFinite(facts.contextChars) ? Number(facts.contextChars) : 0;
|
|
55
59
|
if (limit > 0) {
|
|
56
|
-
const
|
|
60
|
+
const rawPercent = (used / limit) * 100;
|
|
61
|
+
const percent = used > 0
|
|
62
|
+
? Math.max(1, Math.min(100, Math.round(rawPercent)))
|
|
63
|
+
: 0;
|
|
57
64
|
styled(dim(`${percent}% of available window`));
|
|
58
65
|
} else {
|
|
59
66
|
styled(dim('No context measurement yet'));
|
|
@@ -71,7 +78,7 @@ export function sessionSidebarRows(facts: SidebarFacts, height: number): string[
|
|
|
71
78
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
72
79
|
for (const item of facts.plan) {
|
|
73
80
|
const mark = item.status === 'completed' ? '✓' : item.status === 'in_progress' ? '▸' : '·';
|
|
74
|
-
line(`${mark} ${item.text || item.step || ''}`);
|
|
81
|
+
line(`${mark} ${item.text || item.step || item.content || ''}`);
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
|
package/src/tui/stageSurface.js
CHANGED
|
@@ -34,6 +34,7 @@ import { composerWidth } from './layout.js';
|
|
|
34
34
|
import { placeHome, placeDialog } from './homeView.js';
|
|
35
35
|
import { sidebarVisible, sessionMainWidth, sessionComposerWidth, sessionSidebarRows, joinSessionSidebar } from './sessionSidebar.js';
|
|
36
36
|
import { sessionStageHeight, alignTranscriptRows } from './sessionLayout.js';
|
|
37
|
+
import { collectSessionTurns, buildTurnRailCells, renderTurnPreviewCard, overlayTurnPreviewCard } from './turnRail.js';
|
|
37
38
|
|
|
38
39
|
// Rows of evicted transcript held for the exit log. Eviction only happens after
|
|
39
40
|
// MAX_FRAMES, so this is a bound on a pathological session, not a working limit.
|
|
@@ -433,6 +434,14 @@ export class StageSurface {
|
|
|
433
434
|
this.decorate(rows, contentWidth);
|
|
434
435
|
markHoveredMessage(rows, this, contentWidth);
|
|
435
436
|
if (rail) this.attachRail(rows, contentWidth);
|
|
437
|
+
if (this.hoveredTurnIndex && this.turnRailLayout?.turns?.length) {
|
|
438
|
+
const hoveredTurn = this.turnRailLayout.turns.find(t => t.turnIndex === this.hoveredTurnIndex);
|
|
439
|
+
if (hoveredTurn) {
|
|
440
|
+
const card = renderTurnPreviewCard(hoveredTurn, 52);
|
|
441
|
+
const anchorY = this.hoveredTurnAnchorY ?? Math.floor(rows.length / 2);
|
|
442
|
+
rows = overlayTurnPreviewCard(rows, card, anchorY, contentWidth + 1, mainWidth);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
436
445
|
if (this.messageActionOverlay) {
|
|
437
446
|
const overlay = this.messageActionOverlay;
|
|
438
447
|
const placed = placeMessageActionPanel(rows, overlay.panel, contentWidth,
|
|
@@ -625,9 +634,26 @@ export class StageSurface {
|
|
|
625
634
|
* keeps the rail in one column instead of ragged against the text.
|
|
626
635
|
*/
|
|
627
636
|
attachRail(rows, contentWidth) {
|
|
637
|
+
const turns = collectSessionTurns(this.transcript, contentWidth);
|
|
638
|
+
const railResult = turns.length > 0
|
|
639
|
+
? buildTurnRailCells(turns, rows.length, this.activeTurnIndex || turns.length, this.hoveredTurnIndex)
|
|
640
|
+
: null;
|
|
641
|
+
|
|
642
|
+
this.turnRailLayout = {
|
|
643
|
+
railX: contentWidth + 1,
|
|
644
|
+
stageTop: (this.out.rows || this.stage.height) - this.stage.height,
|
|
645
|
+
viewportHeight: rows.length,
|
|
646
|
+
dotRows: railResult?.layout?.dotRows || new Map(),
|
|
647
|
+
rowToTurn: railResult?.layout?.rowToTurn || new Map(),
|
|
648
|
+
turns
|
|
649
|
+
};
|
|
650
|
+
|
|
628
651
|
const rail = this.buildRail(rows.length, contentWidth);
|
|
629
652
|
for (let i = 0; i < rows.length; i++) {
|
|
630
|
-
|
|
653
|
+
const cell = railResult?.cells[i] && railResult.cells[i] !== ' '
|
|
654
|
+
? railResult.cells[i]
|
|
655
|
+
: rail[i];
|
|
656
|
+
rows[i] = `${padEnd(rows[i], contentWidth)} ${cell}`;
|
|
631
657
|
}
|
|
632
658
|
return rows;
|
|
633
659
|
}
|
package/src/tui/text.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export function safeDisplay(value: string): string;
|
|
2
|
+
export function stripAnsi(text: string): string;
|
|
2
3
|
export function truncate(value: string, width: number, ellipsis?: string): string;
|
|
3
4
|
export function padEnd(value: string, width: number): string;
|
|
4
5
|
export function visibleWidth(value: string): number;
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { truncate, visibleWidth, wrapBlock, stripAnsi, padEnd } from './text.js';
|
|
2
|
+
// @ts-expect-error untyped local TUI theme
|
|
3
|
+
import { paint, bold, dim, GLYPHS } from './theme.js';
|
|
4
|
+
export function formatTurnTimestamp(ts) {
|
|
5
|
+
if (!ts || !Number.isFinite(ts))
|
|
6
|
+
return '';
|
|
7
|
+
const date = new Date(ts);
|
|
8
|
+
const now = new Date();
|
|
9
|
+
const sameDay = date.getFullYear() === now.getFullYear() &&
|
|
10
|
+
date.getMonth() === now.getMonth() &&
|
|
11
|
+
date.getDate() === now.getDate();
|
|
12
|
+
const timeStr = date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true });
|
|
13
|
+
if (sameDay) {
|
|
14
|
+
return timeStr;
|
|
15
|
+
}
|
|
16
|
+
const dateStr = date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' });
|
|
17
|
+
return `${dateStr} at ${timeStr}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Collect all user turns from transcript frames.
|
|
21
|
+
*/
|
|
22
|
+
export function collectSessionTurns(transcript, contentWidth = 80) {
|
|
23
|
+
if (!transcript || !Array.isArray(transcript.frames) || transcript.frames.length === 0) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const flat = typeof transcript.flatten === 'function' ? transcript.flatten(contentWidth) : null;
|
|
27
|
+
const markers = flat?.markers || [];
|
|
28
|
+
const frames = transcript.frames;
|
|
29
|
+
const userFrames = [];
|
|
30
|
+
for (let i = 0; i < frames.length; i++) {
|
|
31
|
+
const f = frames[i];
|
|
32
|
+
if (f && f.kind === 'user') {
|
|
33
|
+
userFrames.push({ frame: f, index: i });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const totalTurns = userFrames.length;
|
|
37
|
+
if (totalTurns === 0)
|
|
38
|
+
return [];
|
|
39
|
+
const turns = [];
|
|
40
|
+
for (let k = 0; k < userFrames.length; k++) {
|
|
41
|
+
const item = userFrames[k];
|
|
42
|
+
if (!item)
|
|
43
|
+
continue;
|
|
44
|
+
const { frame, index } = item;
|
|
45
|
+
const turnIndex = k + 1;
|
|
46
|
+
const nextUserFrameIndex = k + 1 < userFrames.length ? userFrames[k + 1].index : frames.length;
|
|
47
|
+
let userPrompt = typeof frame.meta?.messageText === 'string' ? frame.meta.messageText : '';
|
|
48
|
+
if (!userPrompt && typeof frame.produce === 'function') {
|
|
49
|
+
const rows = frame.produce(contentWidth) || [];
|
|
50
|
+
userPrompt = stripAnsi(rows.join(' ')).trim();
|
|
51
|
+
}
|
|
52
|
+
let assistantSummary = '';
|
|
53
|
+
for (let j = index + 1; j < nextUserFrameIndex; j++) {
|
|
54
|
+
const cand = frames[j];
|
|
55
|
+
if (cand && (cand.kind === 'assistant' || typeof cand.meta?.messageText === 'string')) {
|
|
56
|
+
assistantSummary = typeof cand.meta?.messageText === 'string'
|
|
57
|
+
? cand.meta.messageText
|
|
58
|
+
: stripAnsi((cand.produce?.(contentWidth) || []).join(' ')).trim();
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const marker = markers.find((m) => m.id === frame.id);
|
|
63
|
+
const startRow = marker ? marker.row : 0;
|
|
64
|
+
turns.push({
|
|
65
|
+
turnIndex,
|
|
66
|
+
totalTurns,
|
|
67
|
+
userPrompt: userPrompt.replace(/\s+/g, ' ').trim(),
|
|
68
|
+
assistantSummary: assistantSummary.replace(/\s+/g, ' ').trim(),
|
|
69
|
+
timestamp: frame.ts || Date.now(),
|
|
70
|
+
formattedDate: formatTurnTimestamp(frame.ts || Date.now()),
|
|
71
|
+
frameId: frame.id,
|
|
72
|
+
startRow
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return turns;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Build the vertical column of turn rail cells.
|
|
79
|
+
*/
|
|
80
|
+
export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIndex) {
|
|
81
|
+
const cells = new Array(Math.max(0, height)).fill(' ');
|
|
82
|
+
const dotRows = new Map();
|
|
83
|
+
const rowToTurn = new Map();
|
|
84
|
+
if (turns.length === 0 || height <= 0) {
|
|
85
|
+
return { cells, layout: { dotRows, rowToTurn } };
|
|
86
|
+
}
|
|
87
|
+
const count = turns.length;
|
|
88
|
+
// If count fits comfortably, stack consecutive dots centered or top-aligned
|
|
89
|
+
const startRow = count <= height
|
|
90
|
+
? Math.max(0, Math.floor((height - count) / 3))
|
|
91
|
+
: 0;
|
|
92
|
+
for (let i = 0; i < count; i++) {
|
|
93
|
+
const turn = turns[i];
|
|
94
|
+
if (!turn)
|
|
95
|
+
continue;
|
|
96
|
+
const turnNum = turn.turnIndex;
|
|
97
|
+
let rowIdx;
|
|
98
|
+
if (count <= height) {
|
|
99
|
+
rowIdx = startRow + i;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
rowIdx = Math.round((i / (count - 1)) * (height - 1));
|
|
103
|
+
}
|
|
104
|
+
rowIdx = Math.max(0, Math.min(height - 1, rowIdx));
|
|
105
|
+
dotRows.set(turnNum, rowIdx);
|
|
106
|
+
rowToTurn.set(rowIdx, turnNum);
|
|
107
|
+
const isHovered = hoveredTurnIndex === turnNum;
|
|
108
|
+
const isActive = activeTurnIndex === turnNum || (turnNum === count && !hoveredTurnIndex);
|
|
109
|
+
if (isHovered) {
|
|
110
|
+
// Hovered dot: bright silver highlighted circle
|
|
111
|
+
cells[rowIdx] = '\x1b[1m\x1b[38;2;230;235;245m●\x1b[0m';
|
|
112
|
+
}
|
|
113
|
+
else if (isActive) {
|
|
114
|
+
// Active / current turn: bright white circle
|
|
115
|
+
cells[rowIdx] = '\x1b[1m\x1b[38;2;250;250;250m●\x1b[0m';
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
// Muted inactive turns: subtle gray dot
|
|
119
|
+
cells[rowIdx] = '\x1b[38;2;115;115;120m•\x1b[0m';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { cells, layout: { dotRows, rowToTurn } };
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Render the Floating Turn Preview Card popover.
|
|
126
|
+
* Exactly matches the UX/UI in Screen Recording 2026-10-30 at 02.42.17:
|
|
127
|
+
* - Rounded elevated box (╭─╮ / ╰─╯) with dark translucent background
|
|
128
|
+
* - Line 1-2: User prompt in bold primary white
|
|
129
|
+
* - Line 3-4: Assistant preview in muted secondary text
|
|
130
|
+
* - Footer: Left 'X of Y', Right timestamp
|
|
131
|
+
*/
|
|
132
|
+
export function renderTurnPreviewCard(turn, maxCardWidth = 50) {
|
|
133
|
+
const cardWidth = Math.max(38, Math.min(60, maxCardWidth));
|
|
134
|
+
const innerWidth = cardWidth - 4; // 2 border chars + 2 padding chars
|
|
135
|
+
const cardBg = '\x1b[48;2;28;28;32m';
|
|
136
|
+
const borderStyle = `${cardBg}\x1b[38;2;75;75;82m`;
|
|
137
|
+
const reset = '\x1b[0m';
|
|
138
|
+
// Format header: User Prompt (bold primary)
|
|
139
|
+
const rawPrompt = turn.userPrompt || 'User request';
|
|
140
|
+
const promptLines = wrapBlock(rawPrompt, innerWidth).slice(0, 2);
|
|
141
|
+
if (promptLines.length === 2 && promptLines[1]) {
|
|
142
|
+
promptLines[1] = truncate(promptLines[1], innerWidth - 3, '...');
|
|
143
|
+
}
|
|
144
|
+
// Format body: Assistant Summary (muted secondary)
|
|
145
|
+
let bodyLines = [];
|
|
146
|
+
if (turn.assistantSummary) {
|
|
147
|
+
bodyLines = wrapBlock(turn.assistantSummary, innerWidth).slice(0, 3);
|
|
148
|
+
if (bodyLines.length === 3 && bodyLines[2]) {
|
|
149
|
+
bodyLines[2] = truncate(bodyLines[2], innerWidth - 3, '...');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Format footer: 'X of Y' on left, timestamp on right
|
|
153
|
+
const counterText = `${turn.turnIndex} of ${turn.totalTurns}`;
|
|
154
|
+
const dateText = turn.formattedDate;
|
|
155
|
+
const footerSpace = Math.max(2, innerWidth - counterText.length - dateText.length);
|
|
156
|
+
const footerLine = `${counterText}${' '.repeat(footerSpace)}${dateText}`;
|
|
157
|
+
// Assemble rows
|
|
158
|
+
const rows = [];
|
|
159
|
+
const topBorder = `${borderStyle}╭${'─'.repeat(cardWidth - 2)}╮${reset}`;
|
|
160
|
+
const bottomBorder = `${borderStyle}╰${'─'.repeat(cardWidth - 2)}╯${reset}`;
|
|
161
|
+
rows.push(topBorder);
|
|
162
|
+
// User prompt rows
|
|
163
|
+
for (const pl of promptLines) {
|
|
164
|
+
const padded = padEnd(pl, innerWidth);
|
|
165
|
+
rows.push(`${borderStyle}│${cardBg} \x1b[1m\x1b[38;2;240;240;245m${padded}${borderStyle} │${reset}`);
|
|
166
|
+
}
|
|
167
|
+
// Body rows if present
|
|
168
|
+
if (bodyLines.length > 0) {
|
|
169
|
+
rows.push(`${borderStyle}│${cardBg}${' '.repeat(innerWidth + 2)}${borderStyle}│${reset}`);
|
|
170
|
+
for (const bl of bodyLines) {
|
|
171
|
+
const padded = padEnd(bl, innerWidth);
|
|
172
|
+
rows.push(`${borderStyle}│${cardBg} \x1b[38;2;160;160;168m${padded}${borderStyle} │${reset}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Empty spacing before footer
|
|
176
|
+
rows.push(`${borderStyle}│${cardBg}${' '.repeat(innerWidth + 2)}${borderStyle}│${reset}`);
|
|
177
|
+
// Footer row
|
|
178
|
+
const paddedFooter = padEnd(footerLine, innerWidth);
|
|
179
|
+
rows.push(`${borderStyle}│${cardBg} \x1b[38;2;125;125;132m${paddedFooter}${borderStyle} │${reset}`);
|
|
180
|
+
rows.push(bottomBorder);
|
|
181
|
+
return rows;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Overlay the floating preview card onto viewport rows at a specific anchorY.
|
|
185
|
+
*/
|
|
186
|
+
export function overlayTurnPreviewCard(viewportRows, cardRows, anchorY, railX, screenWidth) {
|
|
187
|
+
if (cardRows.length === 0 || viewportRows.length === 0)
|
|
188
|
+
return viewportRows;
|
|
189
|
+
const cardWidth = visibleWidth(cardRows[0] || '');
|
|
190
|
+
// Clamp card placement horizontally: preferably to the left of the rail, or to the right
|
|
191
|
+
let x = railX - cardWidth - 1;
|
|
192
|
+
if (x < 0 || (railX <= 4 && railX + 2 + cardWidth <= screenWidth)) {
|
|
193
|
+
x = Math.max(0, Math.min(screenWidth - cardWidth - 1, railX + 2));
|
|
194
|
+
}
|
|
195
|
+
// Center vertically around anchorY
|
|
196
|
+
const startY = Math.max(0, Math.min(viewportRows.length - cardRows.length, anchorY - Math.floor(cardRows.length / 2)));
|
|
197
|
+
const out = viewportRows.slice();
|
|
198
|
+
for (let i = 0; i < cardRows.length; i++) {
|
|
199
|
+
const targetY = startY + i;
|
|
200
|
+
if (targetY >= out.length)
|
|
201
|
+
break;
|
|
202
|
+
const baseRow = out[targetY] || '';
|
|
203
|
+
const cardRow = cardRows[i] || '';
|
|
204
|
+
// Composite cardRow onto baseRow at column x
|
|
205
|
+
const beforePart = padEnd(truncate(baseRow, x, ''), x);
|
|
206
|
+
const afterPart = baseRow.length > x + cardWidth ? baseRow.slice(x + cardWidth) : '';
|
|
207
|
+
out[targetY] = `${beforePart}${cardRow}${afterPart}`;
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
export function isMouseMotion(sequence) {
|
|
212
|
+
const code = Number(/^\x1b\[<(\d+);/.exec(sequence || '')?.[1]);
|
|
213
|
+
return Number.isFinite(code) && (code & 32) !== 0;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Handle mouse interaction (hover and click) for the Turn Rail.
|
|
217
|
+
*/
|
|
218
|
+
export function handleTurnRailMouse(event, surface, host) {
|
|
219
|
+
if (!surface?.turnRailLayout || !event.mouse)
|
|
220
|
+
return false;
|
|
221
|
+
const layout = surface.turnRailLayout;
|
|
222
|
+
const mouseX = event.mouse.x;
|
|
223
|
+
const mouseY = event.mouse.y;
|
|
224
|
+
const stageTop = layout.stageTop ?? 0;
|
|
225
|
+
const relY = mouseY - stageTop;
|
|
226
|
+
const isRail = mouseX >= layout.railX - 1 && mouseX <= layout.railX + 1;
|
|
227
|
+
if (!isRail || relY < 0 || relY >= layout.viewportHeight) {
|
|
228
|
+
if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
|
|
229
|
+
surface.hoveredTurnIndex = null;
|
|
230
|
+
surface.hoveredTurnAnchorY = null;
|
|
231
|
+
if (typeof host?.scheduleDraw === 'function')
|
|
232
|
+
host.scheduleDraw();
|
|
233
|
+
else if (typeof host?.draw === 'function')
|
|
234
|
+
host.draw();
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
const turnNum = layout.rowToTurn?.get(relY);
|
|
239
|
+
if (turnNum) {
|
|
240
|
+
if (isMouseMotion(event.sequence)) {
|
|
241
|
+
if (surface.hoveredTurnIndex !== turnNum) {
|
|
242
|
+
surface.hoveredTurnIndex = turnNum;
|
|
243
|
+
surface.hoveredTurnAnchorY = relY;
|
|
244
|
+
if (typeof host?.scheduleDraw === 'function')
|
|
245
|
+
host.scheduleDraw();
|
|
246
|
+
else if (typeof host?.draw === 'function')
|
|
247
|
+
host.draw();
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
if (event.mouse.button === 0 && event.mouse.pressed) {
|
|
252
|
+
const turn = layout.turns?.find((t) => t.turnIndex === turnNum);
|
|
253
|
+
if (turn && surface.viewport && typeof surface.viewport.scrollTo === 'function') {
|
|
254
|
+
surface.viewport.scrollTo(turn.startRow);
|
|
255
|
+
surface.hoveredTurnIndex = null;
|
|
256
|
+
surface.hoveredTurnAnchorY = null;
|
|
257
|
+
if (typeof host?.draw === 'function')
|
|
258
|
+
host.draw();
|
|
259
|
+
else if (typeof host?.scheduleDraw === 'function')
|
|
260
|
+
host.scheduleDraw();
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
|
|
266
|
+
surface.hoveredTurnIndex = null;
|
|
267
|
+
surface.hoveredTurnAnchorY = null;
|
|
268
|
+
if (typeof host?.scheduleDraw === 'function')
|
|
269
|
+
host.scheduleDraw();
|
|
270
|
+
else if (typeof host?.draw === 'function')
|
|
271
|
+
host.draw();
|
|
272
|
+
}
|
|
273
|
+
return false;
|
|
274
|
+
}
|