@causyne/agent 1.19.0 → 1.20.5
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/README.md +0 -18
- package/package.json +1 -1
- package/src/acp/client.js +2 -1
- package/src/acp/client.ts +2 -1
- package/src/acp/server.js +5 -6
- package/src/acp/server.ts +4 -6
- package/src/agentLoop.d.ts +1 -0
- package/src/agentLoop.js +505 -241
- package/src/agentLoop.ts +527 -258
- package/src/agentPolicy.js +9 -0
- package/src/agentPolicy.ts +9 -0
- package/src/approvalPolicy.js +55 -0
- package/src/approvalPolicy.ts +66 -0
- package/src/browser/browserRuntime.js +217 -14
- package/src/browser/browserRuntime.ts +255 -14
- package/src/browser/causyneBrowser.js +1215 -68
- package/src/browser/causyneBrowser.ts +1340 -72
- package/src/cliMode.js +66 -37
- package/src/commands.js +343 -72
- package/src/config.js +22 -12
- package/src/continuum/ingest.js +1 -1
- package/src/continuum/ingest.ts +1 -1
- package/src/continuum/ui/continuumPicker.js +18 -1
- package/src/continuum/ui/continuumPicker.ts +19 -1
- package/src/dynamicCatalogue.js +87 -20
- package/src/dynamicCatalogue.ts +85 -15
- package/src/launchIntent.js +40 -0
- package/src/launchIntent.ts +49 -0
- package/src/memoryManager.js +36 -8
- package/src/modelCatalogue.js +2 -0
- package/src/planCache.js +320 -11
- package/src/planCache.ts +402 -13
- package/src/providers/acp.js +67 -1
- package/src/providers/acp.ts +65 -2
- package/src/providers/antigravity.d.ts +5 -1
- package/src/providers/antigravity.js +643 -32
- package/src/providers/antigravity.ts +671 -33
- package/src/providers/base.d.ts +9 -0
- package/src/providers/factory.js +2 -0
- package/src/providers/pi.js +157 -0
- package/src/providers/pi.ts +144 -0
- package/src/provisioning/capabilityProvisioner.js +17 -0
- package/src/provisioning/capabilityTools.js +1 -1
- package/src/sandbox.js +30 -39
- package/src/sessionJournal.js +66 -10
- package/src/sessionJournal.ts +84 -10
- package/src/taskShape.js +9 -9
- package/src/taskShape.ts +9 -9
- package/src/timeMachine.js +124 -0
- package/src/timeMachine.ts +141 -0
- package/src/toolPacks.js +4 -10
- package/src/tools/archive.js +196 -59
- package/src/tools/archive.ts +220 -0
- package/src/tools/browserAutomator.js +78 -9
- package/src/tools/browserAutomator.ts +90 -9
- package/src/tools/desktopOpen.js +114 -31
- package/src/tools/desktopOpen.ts +144 -0
- package/src/tools/gitExec.js +5 -3
- package/src/tools/nativeEngine.js +4 -3
- package/src/tools/nativeEngine.ts +5 -3
- package/src/tools/openSourceToolProvisioner.js +5 -4
- package/src/tools/openSourceToolProvisioner.ts +5 -4
- package/src/tools/registry.js +3 -0
- package/src/tools/screenshot.js +55 -71
- package/src/tools/screenshot.ts +70 -0
- package/src/tools/sysRun.js +284 -117
- package/src/tools/sysRun.ts +347 -0
- package/src/tools/toolSelfRepair.js +109 -2
- package/src/tools/toolSelfRepair.ts +100 -2
- package/src/tools/updatePlan.js +49 -1
- package/src/tools/updatePlan.ts +43 -1
- package/src/tools/viewImage.js +93 -78
- package/src/tools/viewImage.ts +116 -0
- package/src/tools/webFetch.js +20 -6
- package/src/tools/webFetch.ts +26 -6
- package/src/tui/block.d.ts +1 -0
- package/src/tui/chatRenderer.d.ts +21 -0
- package/src/tui/chatRenderer.js +118 -45
- package/src/tui/clipboardSync.js +28 -1
- package/src/tui/clipboardSync.ts +26 -1
- package/src/tui/codeProgress.js +299 -0
- package/src/tui/codeProgress.ts +304 -0
- package/src/tui/codeStreamRenderer.js +116 -0
- package/src/tui/codeStreamRenderer.ts +124 -0
- package/src/tui/commandPresentation.js +11 -0
- package/src/tui/commandPresentation.ts +18 -0
- package/src/tui/completion.js +45 -13
- package/src/tui/composerControls.js +66 -0
- package/src/tui/composerControls.ts +78 -0
- package/src/tui/diff.d.ts +2 -0
- package/src/tui/home.js +4 -2
- package/src/tui/homeTransition.js +21 -0
- package/src/tui/homeTransition.ts +27 -0
- package/src/tui/homeView.js +62 -0
- package/src/tui/homeView.ts +91 -0
- package/src/tui/layout.js +46 -0
- package/src/tui/layout.ts +52 -0
- package/src/tui/messageActionPlacement.js +16 -0
- package/src/tui/messageActionPlacement.ts +25 -0
- package/src/tui/messageInteraction.js +221 -0
- package/src/tui/messageInteraction.ts +275 -0
- package/src/tui/modelPicker.js +13 -0
- package/src/tui/modelPicker.ts +12 -0
- package/src/tui/mouseHover.js +6 -0
- package/src/tui/mouseHover.ts +5 -0
- package/src/tui/prompt.js +199 -52
- package/src/tui/resizeRecovery.js +15 -0
- package/src/tui/resizeRecovery.ts +27 -0
- package/src/tui/scrollInteraction.js +97 -0
- package/src/tui/scrollInteraction.ts +189 -0
- package/src/tui/sessionLayout.js +12 -0
- package/src/tui/sessionLayout.ts +12 -0
- package/src/tui/sessionPicker.js +280 -0
- package/src/tui/sessionPicker.ts +364 -0
- package/src/tui/sessionSidebar.js +78 -0
- package/src/tui/sessionSidebar.ts +95 -0
- package/src/tui/setup.js +1 -1
- package/src/tui/setup.ts +1 -1
- package/src/tui/stageSurface.js +128 -57
- package/src/tui/statusLine.js +5 -5
- package/src/tui/text.d.ts +5 -0
- package/src/tui/textLayout.js +23 -0
- package/src/tui/textLayout.ts +28 -0
- package/src/tui/viewport.js +204 -279
- package/src/tui/viewport.ts +326 -0
- package/src/types/contracts.ts +54 -0
- package/src/utils/paths.js +5 -4
- package/src/utils/paths.ts +5 -5
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Sovereign TUI Scroll & Mouse Isolation Controller
|
|
2
|
+
//
|
|
3
|
+
// Implements OpenCode-compliant viewport containment:
|
|
4
|
+
// 1. Mouse wheel events are strictly confined to the chat box viewport.
|
|
5
|
+
// 2. Overscrolling past the top (0) or bottom (maxScrollTop) saturates cleanly.
|
|
6
|
+
// 3. Mouse reporting is NEVER released to the terminal during an active session,
|
|
7
|
+
// preventing terminal scrollback desync and UI freezes.
|
|
8
|
+
// 4. Scrollbar (rail) mouse interactions clamp cleanly even if clicked above or
|
|
9
|
+
// below the chat box bounds.
|
|
10
|
+
|
|
11
|
+
export interface MouseCoordinate {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
button?: number;
|
|
15
|
+
pressed?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface WheelCoordinate {
|
|
19
|
+
direction: 'up' | 'down' | 'left' | 'right';
|
|
20
|
+
x?: number;
|
|
21
|
+
y?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface KeyEvent {
|
|
25
|
+
name: string;
|
|
26
|
+
wheel?: WheelCoordinate;
|
|
27
|
+
mouse?: MouseCoordinate;
|
|
28
|
+
shift?: boolean;
|
|
29
|
+
alt?: boolean;
|
|
30
|
+
ctrl?: boolean;
|
|
31
|
+
text?: string;
|
|
32
|
+
sequence?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ViewportContract {
|
|
36
|
+
scrollTop: number;
|
|
37
|
+
maxScrollTop(): number;
|
|
38
|
+
wheel(direction: 'up' | 'down', notches?: number, now?: number): number;
|
|
39
|
+
scrollTo(position: number, options?: { snap?: boolean }): number;
|
|
40
|
+
toBottom(): number;
|
|
41
|
+
toTop(): number;
|
|
42
|
+
pageUp(): number;
|
|
43
|
+
pageDown(): number;
|
|
44
|
+
lineUp(n?: number): number;
|
|
45
|
+
lineDown(n?: number): number;
|
|
46
|
+
halfPageUp(): number;
|
|
47
|
+
halfPageDown(): number;
|
|
48
|
+
following: boolean;
|
|
49
|
+
height: number;
|
|
50
|
+
width: number;
|
|
51
|
+
lastWheelMoved?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SurfaceContract {
|
|
55
|
+
stage?: {
|
|
56
|
+
height: number;
|
|
57
|
+
width(): number;
|
|
58
|
+
reserved?: boolean;
|
|
59
|
+
};
|
|
60
|
+
viewport?: ViewportContract;
|
|
61
|
+
width?: number;
|
|
62
|
+
mouseCaptured?: boolean;
|
|
63
|
+
detached?: boolean;
|
|
64
|
+
releaseMouse?(): boolean;
|
|
65
|
+
captureMouse?(): boolean;
|
|
66
|
+
railEnabled?(width: number): boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface PromptScrollHost {
|
|
70
|
+
surface: SurfaceContract | null;
|
|
71
|
+
out: { rows?: number; columns?: number; write?(text: string): boolean } | null;
|
|
72
|
+
notice: string;
|
|
73
|
+
draw(): void;
|
|
74
|
+
scheduleDraw?(): void;
|
|
75
|
+
pickerLayout?: {
|
|
76
|
+
pickerStart: number;
|
|
77
|
+
pickerCount: number;
|
|
78
|
+
totalPromptRows: number;
|
|
79
|
+
} | null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Handle mouse wheel scrolling with full viewport containment.
|
|
84
|
+
* Replaces the buggy overscroll release with clean boundary saturation.
|
|
85
|
+
*/
|
|
86
|
+
export function handleViewportWheel(
|
|
87
|
+
key: KeyEvent,
|
|
88
|
+
viewport: ViewportContract | null | undefined,
|
|
89
|
+
surface: SurfaceContract | null | undefined,
|
|
90
|
+
host: PromptScrollHost
|
|
91
|
+
): boolean {
|
|
92
|
+
if (!viewport || !key.wheel) return true;
|
|
93
|
+
|
|
94
|
+
// Auto-heal: If the surface was ever detached, immediately re-capture mouse
|
|
95
|
+
// so the session stays responsive and never freezes.
|
|
96
|
+
if (surface && typeof surface.captureMouse === 'function' && surface.detached) {
|
|
97
|
+
surface.captureMouse();
|
|
98
|
+
if (host.notice.startsWith('top of this view')) {
|
|
99
|
+
host.notice = '';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Pure isolated scrolling: clamp strictly inside [0, maxScrollTop]
|
|
104
|
+
viewport.wheel(key.wheel.direction === 'down' ? 'down' : 'up');
|
|
105
|
+
|
|
106
|
+
// Immediately draw updated viewport
|
|
107
|
+
host.draw();
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Handle mouse clicks on the scrollbar rail.
|
|
113
|
+
* Safely clamps coordinates even when clicking above or below the chat box.
|
|
114
|
+
*/
|
|
115
|
+
export function handleScrollbarMouse(
|
|
116
|
+
key: KeyEvent,
|
|
117
|
+
viewport: ViewportContract | null | undefined,
|
|
118
|
+
surface: SurfaceContract | null | undefined,
|
|
119
|
+
host: PromptScrollHost
|
|
120
|
+
): boolean {
|
|
121
|
+
if (!viewport || !surface || !key.mouse || !key.mouse.pressed) return false;
|
|
122
|
+
if (key.mouse.button !== 0) return false; // Left click only
|
|
123
|
+
|
|
124
|
+
// If completion picker is active, check if click was on the picker
|
|
125
|
+
if (host.pickerLayout && Number.isFinite(key.mouse.y)) {
|
|
126
|
+
const termRows = host.out?.rows || 24;
|
|
127
|
+
const stageH = surface.stage?.height || termRows;
|
|
128
|
+
const promptTop = Math.max(1, stageH - host.pickerLayout.totalPromptRows + 1);
|
|
129
|
+
const pickerTop = promptTop + host.pickerLayout.pickerStart - 1;
|
|
130
|
+
const pickerBottom = pickerTop + host.pickerLayout.pickerCount;
|
|
131
|
+
if (key.mouse.y >= pickerTop && key.mouse.y <= pickerBottom) {
|
|
132
|
+
return false; // Leave to completion handler
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const termRows = host.out?.rows || 24;
|
|
137
|
+
const stageH = surface.stage?.height || termRows;
|
|
138
|
+
const stageTop = Math.max(0, termRows - stageH);
|
|
139
|
+
const viewportH = viewport.height;
|
|
140
|
+
const stageW = typeof surface.stage?.width === 'function' ? surface.stage.width() : (surface.width || 80);
|
|
141
|
+
const hasRail = typeof surface.railEnabled === 'function' ? surface.railEnabled(stageW) : true;
|
|
142
|
+
const contentW = hasRail ? Math.max(20, stageW - 3) : stageW;
|
|
143
|
+
|
|
144
|
+
const mouseX = key.mouse.x;
|
|
145
|
+
const mouseY = key.mouse.y;
|
|
146
|
+
|
|
147
|
+
// Rail occupies column contentW - 1 through stageW
|
|
148
|
+
const isRailColumn = mouseX >= contentW - 1 && mouseX <= stageW;
|
|
149
|
+
|
|
150
|
+
if (isRailColumn) {
|
|
151
|
+
// Auto-heal if detached
|
|
152
|
+
if (typeof surface.captureMouse === 'function' && surface.detached) {
|
|
153
|
+
surface.captureMouse();
|
|
154
|
+
if (host.notice.startsWith('top of this view')) host.notice = '';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Boundary clamping:
|
|
158
|
+
// Clicks preceding/above chat box clamp to top (0)
|
|
159
|
+
if (mouseY < stageTop) {
|
|
160
|
+
viewport.scrollTo(0);
|
|
161
|
+
host.draw();
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Clicks below viewport clamp to bottom (maxScrollTop) and resume following
|
|
166
|
+
if (mouseY >= stageTop + viewportH) {
|
|
167
|
+
viewport.toBottom();
|
|
168
|
+
host.draw();
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Clicks on rail inside viewport: compute proportional scroll
|
|
173
|
+
const relY = mouseY - stageTop;
|
|
174
|
+
const fraction = viewportH > 1 ? relY / (viewportH - 1) : 0;
|
|
175
|
+
const clampedFraction = Math.max(0, Math.min(1, fraction));
|
|
176
|
+
const target = Math.round(clampedFraction * viewport.maxScrollTop());
|
|
177
|
+
|
|
178
|
+
if (target >= viewport.maxScrollTop()) {
|
|
179
|
+
viewport.toBottom();
|
|
180
|
+
} else {
|
|
181
|
+
viewport.scrollTo(target);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
host.draw();
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Keep an opened conversation anchored to the terminal bottom at every width. */
|
|
2
|
+
export function sessionStageHeight(maxHeight, promptRows, contentRows, fullSession) {
|
|
3
|
+
const limit = Math.max(1, Math.floor(maxHeight));
|
|
4
|
+
return fullSession ? limit : Math.min(limit, Math.max(1, promptRows + contentRows));
|
|
5
|
+
}
|
|
6
|
+
/** Place a short transcript beside the input; spare space belongs above it. */
|
|
7
|
+
export function alignTranscriptRows(rows, contentRows) {
|
|
8
|
+
const visible = Math.max(0, Math.min(rows.length, Math.floor(contentRows)));
|
|
9
|
+
if (visible === rows.length)
|
|
10
|
+
return rows;
|
|
11
|
+
return Array(rows.length - visible).fill('').concat(rows.slice(0, visible));
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Keep an opened conversation anchored to the terminal bottom at every width. */
|
|
2
|
+
export function sessionStageHeight(maxHeight: number, promptRows: number, contentRows: number, fullSession: boolean): number {
|
|
3
|
+
const limit = Math.max(1, Math.floor(maxHeight));
|
|
4
|
+
return fullSession ? limit : Math.min(limit, Math.max(1, promptRows + contentRows));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** Place a short transcript beside the input; spare space belongs above it. */
|
|
8
|
+
export function alignTranscriptRows(rows: string[], contentRows: number): string[] {
|
|
9
|
+
const visible = Math.max(0, Math.min(rows.length, Math.floor(contentRows)));
|
|
10
|
+
if (visible === rows.length) return rows;
|
|
11
|
+
return Array<string>(rows.length - visible).fill('').concat(rows.slice(0, visible));
|
|
12
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// Sovereign Interactive Session Picker — modal dialog for listing, switching, and creating sessions.
|
|
2
|
+
// Authored in Strict TypeScript for Causyne Agent.
|
|
3
|
+
// Source-available under BUSL-1.1.
|
|
4
|
+
// @ts-expect-error untyped local TUI dialogs
|
|
5
|
+
import { SelectLayer } from './dialogs.js';
|
|
6
|
+
// @ts-expect-error untyped local TUI theme
|
|
7
|
+
import { GLYPHS } from './theme.js';
|
|
8
|
+
import { listSessions, getActiveSessionId, switchSession, createSession, forkSession } from '../sessionJournal.js';
|
|
9
|
+
import { revokeAllAlways } from '../approvalGrants.js';
|
|
10
|
+
/** The divider counts conversation turns that the history renderer can show. */
|
|
11
|
+
function visibleHistoryCount(messages) {
|
|
12
|
+
return messages.filter((message) => {
|
|
13
|
+
if (message.role !== 'user' && message.role !== 'assistant')
|
|
14
|
+
return false;
|
|
15
|
+
if (typeof message.content === 'string')
|
|
16
|
+
return message.content.trim().length > 0;
|
|
17
|
+
if (Array.isArray(message.content)) {
|
|
18
|
+
return message.content.some((part) => typeof part === 'string' && part.trim().length > 0
|
|
19
|
+
|| !!part && typeof part === 'object' && 'text' in part
|
|
20
|
+
&& typeof part.text === 'string' && part.text.trim().length > 0);
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}).length;
|
|
24
|
+
}
|
|
25
|
+
function formatTimeAgo(timestamp) {
|
|
26
|
+
const diffMs = Math.max(0, Date.now() - timestamp);
|
|
27
|
+
const minutes = Math.floor(diffMs / (60 * 1000));
|
|
28
|
+
if (minutes < 1)
|
|
29
|
+
return 'just now';
|
|
30
|
+
if (minutes < 60)
|
|
31
|
+
return `${minutes}m ago`;
|
|
32
|
+
const hours = Math.floor(minutes / 60);
|
|
33
|
+
if (hours < 24)
|
|
34
|
+
return `${hours}h ago`;
|
|
35
|
+
const days = Math.floor(hours / 24);
|
|
36
|
+
return `${days}d ago`;
|
|
37
|
+
}
|
|
38
|
+
function dateGroup(timestamp) {
|
|
39
|
+
const date = new Date(timestamp);
|
|
40
|
+
if (!Number.isFinite(date.getTime()))
|
|
41
|
+
return 'Unknown date';
|
|
42
|
+
const today = new Date();
|
|
43
|
+
if (date.toDateString() === today.toDateString())
|
|
44
|
+
return 'Today';
|
|
45
|
+
const yesterday = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
|
|
46
|
+
return date.toDateString() === yesterday.toDateString() ? 'Yesterday' : date.toDateString();
|
|
47
|
+
}
|
|
48
|
+
function shortenPath(p) {
|
|
49
|
+
if (!p)
|
|
50
|
+
return '(default)';
|
|
51
|
+
const home = process.env.HOME || '';
|
|
52
|
+
if (home && p.startsWith(home)) {
|
|
53
|
+
return '~' + p.slice(home.length);
|
|
54
|
+
}
|
|
55
|
+
return p.split('/').pop() || p;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Build item list for SelectLayer modal.
|
|
59
|
+
* The top item is always "+ New Session" to guarantee clean separation and quick reset.
|
|
60
|
+
*/
|
|
61
|
+
export function buildSessionPickerItems(sessions, activeId, currentWorkspace) {
|
|
62
|
+
const items = [];
|
|
63
|
+
// Pinned item 0: Create new clean conversation
|
|
64
|
+
items.push({
|
|
65
|
+
id: '__NEW_SESSION__',
|
|
66
|
+
label: '+ New Session (Start fresh conversation)',
|
|
67
|
+
category: 'Actions',
|
|
68
|
+
description: 'Begin a clean session in this workspace without previous context',
|
|
69
|
+
searchText: 'new session fresh clear reset start',
|
|
70
|
+
value: {
|
|
71
|
+
type: 'new'
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
// Pinned item 1: Fork active conversation if available
|
|
75
|
+
if (activeId) {
|
|
76
|
+
const active = sessions.find(s => s.id === activeId);
|
|
77
|
+
if (active) {
|
|
78
|
+
items.push({
|
|
79
|
+
id: '__FORK_SESSION__',
|
|
80
|
+
label: `Branch / Fork Current Session ("${active.title}")`,
|
|
81
|
+
category: 'Actions',
|
|
82
|
+
description: 'Copy conversation history into a new branch without copying permissions',
|
|
83
|
+
searchText: 'fork branch clone copy session fresh permissions',
|
|
84
|
+
value: {
|
|
85
|
+
type: 'fork',
|
|
86
|
+
id: activeId,
|
|
87
|
+
title: active.title
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const s of [...sessions].sort((a, b) => b.updatedAt - a.updatedAt)) {
|
|
93
|
+
// A session without any turns has no conversation to resume. Keep the
|
|
94
|
+
// active one available, but do not fill the picker with abandoned starts.
|
|
95
|
+
if (s.messageCount === 0 && s.id !== activeId)
|
|
96
|
+
continue;
|
|
97
|
+
const isCurrent = s.id === activeId;
|
|
98
|
+
const timeAgo = formatTimeAgo(s.updatedAt);
|
|
99
|
+
const wsLabel = shortenPath(s.workspace);
|
|
100
|
+
const msgCount = `${s.messageCount} msg${s.messageCount === 1 ? '' : 's'}`;
|
|
101
|
+
const category = dateGroup(s.updatedAt);
|
|
102
|
+
const mark = isCurrent ? `● ` : ` `;
|
|
103
|
+
const tag = isCurrent ? ` [active]` : '';
|
|
104
|
+
items.push({
|
|
105
|
+
id: s.id,
|
|
106
|
+
label: `${mark}${s.title}${tag}`,
|
|
107
|
+
category,
|
|
108
|
+
description: `${msgCount} ${GLYPHS.dot} ${timeAgo} ${GLYPHS.dot} ${wsLabel}${s.workspace === currentWorkspace ? ' (current)' : ''}`,
|
|
109
|
+
searchText: `${s.title} ${s.id} ${s.workspace || ''} ${category}`,
|
|
110
|
+
value: {
|
|
111
|
+
type: 'switch',
|
|
112
|
+
id: s.id,
|
|
113
|
+
title: s.title,
|
|
114
|
+
workspace: s.workspace,
|
|
115
|
+
messageCount: s.messageCount,
|
|
116
|
+
isCurrent
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return items;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Launch the interactive Session Picker modal.
|
|
124
|
+
* Single Enter switches session or creates a new one immediately.
|
|
125
|
+
*/
|
|
126
|
+
export async function runInteractiveSessionPicker(prompt, options = {}) {
|
|
127
|
+
const workspace = options.workspace || process.cwd();
|
|
128
|
+
const sessions = listSessions();
|
|
129
|
+
const activeId = getActiveSessionId();
|
|
130
|
+
const items = buildSessionPickerItems(sessions, activeId, workspace);
|
|
131
|
+
const conversationCount = items.filter(item => item.value.type === 'switch').length;
|
|
132
|
+
const cleanPrompt = () => {
|
|
133
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
134
|
+
prompt.editor.clear();
|
|
135
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
136
|
+
prompt.scheduleDraw();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const picked = await new Promise((resolve) => {
|
|
140
|
+
const layer = new SelectLayer({
|
|
141
|
+
title: `Sessions (${conversationCount})`,
|
|
142
|
+
placeholder: 'search sessions by title, id, or workspace...',
|
|
143
|
+
emptyText: 'no matching sessions found',
|
|
144
|
+
width: 78,
|
|
145
|
+
height: 20,
|
|
146
|
+
footer: `enter select session ${GLYPHS.dot} esc cancel`,
|
|
147
|
+
items,
|
|
148
|
+
onPick: (item) => {
|
|
149
|
+
resolve(item || null);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
// Opening the picker should highlight a conversation, not an action that
|
|
153
|
+
// creates a new one. Actions remain available above the dated list.
|
|
154
|
+
const selected = items.findIndex(item => item.id === activeId);
|
|
155
|
+
const firstConversation = items.findIndex(item => item.value.type === 'switch');
|
|
156
|
+
layer.list.moveTo(selected >= 0 ? selected : Math.max(0, firstConversation), 15);
|
|
157
|
+
prompt.openLayer(layer);
|
|
158
|
+
});
|
|
159
|
+
cleanPrompt();
|
|
160
|
+
if (!picked || !picked.value) {
|
|
161
|
+
return 'Cancelled. Session was not changed.';
|
|
162
|
+
}
|
|
163
|
+
// Case 1: Start fresh new session
|
|
164
|
+
if (picked.value.type === 'new') {
|
|
165
|
+
let newSession;
|
|
166
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.startNewSession === 'function') {
|
|
167
|
+
newSession = options.loop.memoryManager.startNewSession('Untitled');
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
newSession = createSession(workspace, 'Untitled', true);
|
|
171
|
+
}
|
|
172
|
+
if (options.loop) {
|
|
173
|
+
options.loop.messages = [];
|
|
174
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
175
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
176
|
+
options.loop.tokenTracker.reset();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const renderer = prompt.renderer || options.loop?.connection?.renderer;
|
|
180
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
181
|
+
renderer.renderSessionHistory([], {
|
|
182
|
+
id: newSession.id,
|
|
183
|
+
title: newSession.title,
|
|
184
|
+
messagesCount: 0
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (prompt.surface?.viewport && typeof prompt.surface.viewport.toBottom === 'function') {
|
|
188
|
+
prompt.surface.viewport.toBottom();
|
|
189
|
+
}
|
|
190
|
+
cleanPrompt();
|
|
191
|
+
return `Started new session: "${newSession.title}" (${newSession.id}) in ${shortenPath(workspace)}.`;
|
|
192
|
+
}
|
|
193
|
+
// Case 2: Fork session into a new branch without copying permissions
|
|
194
|
+
if (picked.value.type === 'fork') {
|
|
195
|
+
const sourceId = picked.value.id || activeId;
|
|
196
|
+
if (!sourceId) {
|
|
197
|
+
return 'Cancelled. No session to fork.';
|
|
198
|
+
}
|
|
199
|
+
let forked = null;
|
|
200
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.forkSession === 'function') {
|
|
201
|
+
forked = options.loop.memoryManager.forkSession();
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
forked = forkSession(sourceId, { makeActive: true });
|
|
205
|
+
}
|
|
206
|
+
if (!forked) {
|
|
207
|
+
cleanPrompt();
|
|
208
|
+
return `Could not fork session "${sourceId}".`;
|
|
209
|
+
}
|
|
210
|
+
// CRITICAL SECURITY INVARIANT (Claude Code Architecture, p. 24):
|
|
211
|
+
// Permissions and allowlists MUST NOT be copied across the fork.
|
|
212
|
+
revokeAllAlways();
|
|
213
|
+
if (options.loop && typeof options.loop.resetApprovalScope === 'function') {
|
|
214
|
+
options.loop.resetApprovalScope();
|
|
215
|
+
}
|
|
216
|
+
const historyMessages = options.loop?.memoryManager && typeof options.loop.memoryManager.loadHistory === 'function'
|
|
217
|
+
? options.loop.memoryManager.loadHistory()
|
|
218
|
+
: forked.messages.filter(m => m.role !== 'system');
|
|
219
|
+
if (options.loop) {
|
|
220
|
+
options.loop.messages = historyMessages;
|
|
221
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
222
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
223
|
+
options.loop.tokenTracker.reset();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const renderer = prompt.renderer || options.loop?.connection?.renderer;
|
|
227
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
228
|
+
renderer.renderSessionHistory(historyMessages, {
|
|
229
|
+
id: forked.id,
|
|
230
|
+
title: forked.title,
|
|
231
|
+
messagesCount: visibleHistoryCount(historyMessages)
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (prompt.surface?.viewport && typeof prompt.surface.viewport.toBottom === 'function') {
|
|
235
|
+
prompt.surface.viewport.toBottom();
|
|
236
|
+
}
|
|
237
|
+
cleanPrompt();
|
|
238
|
+
return `Forked into "${forked.title}" (${forked.messages.length} messages, ${forked.id}). Permissions and standing approvals were reset; any gated actions will request authorization anew.`;
|
|
239
|
+
}
|
|
240
|
+
// Case 3: Switch to selected session
|
|
241
|
+
const targetId = picked.value.id;
|
|
242
|
+
if (!targetId) {
|
|
243
|
+
return 'Cancelled. No session selected.';
|
|
244
|
+
}
|
|
245
|
+
let switched = null;
|
|
246
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.useSession === 'function') {
|
|
247
|
+
switched = options.loop.memoryManager.useSession(targetId);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
switched = switchSession(targetId);
|
|
251
|
+
}
|
|
252
|
+
if (!switched) {
|
|
253
|
+
cleanPrompt();
|
|
254
|
+
return `⚠️ Could not switch to session "${targetId}". File may have been removed.`;
|
|
255
|
+
}
|
|
256
|
+
const historyMessages = options.loop?.memoryManager && typeof options.loop.memoryManager.loadHistory === 'function'
|
|
257
|
+
? options.loop.memoryManager.loadHistory()
|
|
258
|
+
: switched.messages.filter(m => m.role !== 'system');
|
|
259
|
+
if (options.loop) {
|
|
260
|
+
options.loop.messages = historyMessages;
|
|
261
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
262
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
263
|
+
options.loop.tokenTracker.reset();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// Render the conversation history to the transcript & viewport
|
|
267
|
+
const renderer = prompt.renderer || options.loop?.connection?.renderer;
|
|
268
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
269
|
+
renderer.renderSessionHistory(historyMessages, {
|
|
270
|
+
id: switched.id,
|
|
271
|
+
title: switched.title,
|
|
272
|
+
messagesCount: visibleHistoryCount(historyMessages)
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
if (prompt.surface?.viewport && typeof prompt.surface.viewport.toBottom === 'function') {
|
|
276
|
+
prompt.surface.viewport.toBottom();
|
|
277
|
+
}
|
|
278
|
+
cleanPrompt();
|
|
279
|
+
return `Switched to "${switched.title}" (${switched.messages.length} messages, ${switched.id}).`;
|
|
280
|
+
}
|