@causyne/agent 1.19.0 → 1.20.0
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/cliMode.js +20 -14
- package/src/commands.js +219 -72
- package/src/continuum/ingest.js +1 -1
- package/src/continuum/ingest.ts +1 -1
- package/src/memoryManager.js +10 -6
- package/src/sessionJournal.js +14 -1
- package/src/sessionJournal.ts +21 -1
- package/src/tui/chatRenderer.js +86 -0
- package/src/tui/modelPicker.js +33 -0
- package/src/tui/modelPicker.ts +28 -0
- package/src/tui/prompt.js +73 -2
- package/src/tui/sessionPicker.js +185 -0
- package/src/tui/sessionPicker.ts +259 -0
- package/src/tui/stageSurface.js +9 -0
- package/src/tui/statusLine.js +5 -5
package/src/tui/modelPicker.js
CHANGED
|
@@ -131,13 +131,38 @@ export async function runInteractiveModelPicker(prompt, store = { loadConfig, sa
|
|
|
131
131
|
});
|
|
132
132
|
prompt.openLayer(layer);
|
|
133
133
|
});
|
|
134
|
+
const notifyPromptAndClean = (providerId, modelId) => {
|
|
135
|
+
if (typeof prompt.updateModel === 'function') {
|
|
136
|
+
prompt.updateModel(providerId, modelId);
|
|
137
|
+
}
|
|
138
|
+
else if (prompt.renderer?.status) {
|
|
139
|
+
prompt.renderer.status.setSession({ provider: providerId, model: modelId });
|
|
140
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
141
|
+
prompt.scheduleDraw();
|
|
142
|
+
}
|
|
143
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
144
|
+
prompt.editor.clear();
|
|
145
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
146
|
+
prompt.scheduleDraw();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
134
149
|
if (!picked || !picked.value) {
|
|
150
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
151
|
+
prompt.editor.clear();
|
|
152
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
153
|
+
prompt.scheduleDraw();
|
|
154
|
+
}
|
|
135
155
|
return 'Cancelled. Model was not changed.';
|
|
136
156
|
}
|
|
137
157
|
const target = picked.value;
|
|
138
158
|
// Case 1: ACP Peer Agent or Antigravity
|
|
139
159
|
if (target.type === 'acp') {
|
|
140
160
|
if (!target.installed) {
|
|
161
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
162
|
+
prompt.editor.clear();
|
|
163
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
164
|
+
prompt.scheduleDraw();
|
|
165
|
+
}
|
|
141
166
|
return (`⚠️ Notice: "${target.providerLabel}" is not installed on this machine.\n` +
|
|
142
167
|
`Setup: ${target.setupInstruction || 'Install the CLI binary.'}\n\n` +
|
|
143
168
|
`Model was not changed.`);
|
|
@@ -153,6 +178,7 @@ export async function runInteractiveModelPicker(prompt, store = { loadConfig, sa
|
|
|
153
178
|
options.loop.updateTierLimits();
|
|
154
179
|
}
|
|
155
180
|
}
|
|
181
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
156
182
|
return `Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
157
183
|
}
|
|
158
184
|
// Case 2: BYOK Provider (Cloud or Local API)
|
|
@@ -183,6 +209,7 @@ export async function runInteractiveModelPicker(prompt, store = { loadConfig, sa
|
|
|
183
209
|
options.loop.updateTierLimits();
|
|
184
210
|
}
|
|
185
211
|
}
|
|
212
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
186
213
|
return `Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
187
214
|
}
|
|
188
215
|
// Key is required: open modal input dialog (InputLayer)
|
|
@@ -201,6 +228,11 @@ export async function runInteractiveModelPicker(prompt, store = { loadConfig, sa
|
|
|
201
228
|
prompt.openLayer(layer);
|
|
202
229
|
});
|
|
203
230
|
if (!enteredKey || !enteredKey.trim()) {
|
|
231
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
232
|
+
prompt.editor.clear();
|
|
233
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
234
|
+
prompt.scheduleDraw();
|
|
235
|
+
}
|
|
204
236
|
return `Cancelled. An API key is required to use ${target.providerLabel} / ${target.modelId}. Model was not changed.`;
|
|
205
237
|
}
|
|
206
238
|
const cleanKey = enteredKey.trim();
|
|
@@ -217,5 +249,6 @@ export async function runInteractiveModelPicker(prompt, store = { loadConfig, sa
|
|
|
217
249
|
options.loop.updateTierLimits();
|
|
218
250
|
}
|
|
219
251
|
}
|
|
252
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
220
253
|
return `Saved API key. Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
221
254
|
}
|
package/src/tui/modelPicker.ts
CHANGED
|
@@ -187,7 +187,24 @@ export async function runInteractiveModelPicker(
|
|
|
187
187
|
prompt.openLayer(layer);
|
|
188
188
|
});
|
|
189
189
|
|
|
190
|
+
const notifyPromptAndClean = (providerId: string, modelId: string) => {
|
|
191
|
+
if (typeof (prompt as any).updateModel === 'function') {
|
|
192
|
+
(prompt as any).updateModel(providerId, modelId);
|
|
193
|
+
} else if ((prompt as any).renderer?.status) {
|
|
194
|
+
(prompt as any).renderer.status.setSession({ provider: providerId, model: modelId });
|
|
195
|
+
if (typeof (prompt as any).scheduleDraw === 'function') (prompt as any).scheduleDraw();
|
|
196
|
+
}
|
|
197
|
+
if ((prompt as any).editor && typeof (prompt as any).editor.clear === 'function') {
|
|
198
|
+
(prompt as any).editor.clear();
|
|
199
|
+
if (typeof (prompt as any).scheduleDraw === 'function') (prompt as any).scheduleDraw();
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
190
203
|
if (!picked || !picked.value) {
|
|
204
|
+
if ((prompt as any).editor && typeof (prompt as any).editor.clear === 'function') {
|
|
205
|
+
(prompt as any).editor.clear();
|
|
206
|
+
if (typeof (prompt as any).scheduleDraw === 'function') (prompt as any).scheduleDraw();
|
|
207
|
+
}
|
|
191
208
|
return 'Cancelled. Model was not changed.';
|
|
192
209
|
}
|
|
193
210
|
|
|
@@ -196,6 +213,10 @@ export async function runInteractiveModelPicker(
|
|
|
196
213
|
// Case 1: ACP Peer Agent or Antigravity
|
|
197
214
|
if (target.type === 'acp') {
|
|
198
215
|
if (!target.installed) {
|
|
216
|
+
if ((prompt as any).editor && typeof (prompt as any).editor.clear === 'function') {
|
|
217
|
+
(prompt as any).editor.clear();
|
|
218
|
+
if (typeof (prompt as any).scheduleDraw === 'function') (prompt as any).scheduleDraw();
|
|
219
|
+
}
|
|
199
220
|
return (
|
|
200
221
|
`⚠️ Notice: "${target.providerLabel}" is not installed on this machine.\n` +
|
|
201
222
|
`Setup: ${target.setupInstruction || 'Install the CLI binary.'}\n\n` +
|
|
@@ -215,6 +236,7 @@ export async function runInteractiveModelPicker(
|
|
|
215
236
|
options.loop.updateTierLimits();
|
|
216
237
|
}
|
|
217
238
|
}
|
|
239
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
218
240
|
return `Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
219
241
|
}
|
|
220
242
|
|
|
@@ -246,6 +268,7 @@ export async function runInteractiveModelPicker(
|
|
|
246
268
|
options.loop.updateTierLimits();
|
|
247
269
|
}
|
|
248
270
|
}
|
|
271
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
249
272
|
return `Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
250
273
|
}
|
|
251
274
|
|
|
@@ -266,6 +289,10 @@ export async function runInteractiveModelPicker(
|
|
|
266
289
|
});
|
|
267
290
|
|
|
268
291
|
if (!enteredKey || !enteredKey.trim()) {
|
|
292
|
+
if ((prompt as any).editor && typeof (prompt as any).editor.clear === 'function') {
|
|
293
|
+
(prompt as any).editor.clear();
|
|
294
|
+
if (typeof (prompt as any).scheduleDraw === 'function') (prompt as any).scheduleDraw();
|
|
295
|
+
}
|
|
269
296
|
return `Cancelled. An API key is required to use ${target.providerLabel} / ${target.modelId}. Model was not changed.`;
|
|
270
297
|
}
|
|
271
298
|
|
|
@@ -286,5 +313,6 @@ export async function runInteractiveModelPicker(
|
|
|
286
313
|
}
|
|
287
314
|
}
|
|
288
315
|
|
|
316
|
+
notifyPromptAndClean(target.providerId, target.modelId);
|
|
289
317
|
return `Saved API key. Now using ${target.providerLabel} / ${target.modelId}.`;
|
|
290
318
|
}
|
package/src/tui/prompt.js
CHANGED
|
@@ -422,6 +422,17 @@ export class ChatPrompt {
|
|
|
422
422
|
this.scheduleDraw();
|
|
423
423
|
}
|
|
424
424
|
|
|
425
|
+
/** Update active model and provider displayed in agentRow and status footer. */
|
|
426
|
+
updateModel(provider, model) {
|
|
427
|
+
if (this.renderer && this.renderer.status) {
|
|
428
|
+
this.renderer.status.setSession({
|
|
429
|
+
provider: provider ? String(provider) : '',
|
|
430
|
+
model: model ? String(model) : ''
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
this.scheduleDraw();
|
|
434
|
+
}
|
|
435
|
+
|
|
425
436
|
/**
|
|
426
437
|
* Browse the compiled skill library.
|
|
427
438
|
*
|
|
@@ -585,6 +596,23 @@ export class ChatPrompt {
|
|
|
585
596
|
this.editor.clear();
|
|
586
597
|
this.completion = null;
|
|
587
598
|
|
|
599
|
+
// Control commands take priority over busy turns so the user is never locked out.
|
|
600
|
+
// Commands like /stop, /new, /model, /session, /sessions, /continuum, /clear can interrupt
|
|
601
|
+
// a stalled/long-running turn and execute immediately.
|
|
602
|
+
const isControlCommand = this.isCommand(text) && (
|
|
603
|
+
text === '/stop' || text === '/interrupt' ||
|
|
604
|
+
text.startsWith('/new') ||
|
|
605
|
+
text.startsWith('/model') ||
|
|
606
|
+
text.startsWith('/session') ||
|
|
607
|
+
text.startsWith('/sessions') ||
|
|
608
|
+
text.startsWith('/continuum') ||
|
|
609
|
+
text === '/clear'
|
|
610
|
+
);
|
|
611
|
+
|
|
612
|
+
if (isControlCommand && this.busy) {
|
|
613
|
+
this.interrupt();
|
|
614
|
+
}
|
|
615
|
+
|
|
588
616
|
if (this.busy) {
|
|
589
617
|
// The turn is still running: hold the line rather than dropping it.
|
|
590
618
|
this.queue.push(text);
|
|
@@ -880,7 +908,14 @@ export class ChatPrompt {
|
|
|
880
908
|
}
|
|
881
909
|
|
|
882
910
|
handleEscape() {
|
|
883
|
-
if (this.completion) {
|
|
911
|
+
if (this.completion) {
|
|
912
|
+
this.completion = null;
|
|
913
|
+
if (this.editor.getText().startsWith('/')) {
|
|
914
|
+
this.editor.clear();
|
|
915
|
+
}
|
|
916
|
+
this.scheduleDraw();
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
884
919
|
if (this.busy) return this.interrupt();
|
|
885
920
|
this.editor.clear();
|
|
886
921
|
}
|
|
@@ -1005,6 +1040,35 @@ export class ChatPrompt {
|
|
|
1005
1040
|
if (key.name === 'up') { this.moveSelection(-1); return true; }
|
|
1006
1041
|
if (key.name === 'down') { this.moveSelection(1); return true; }
|
|
1007
1042
|
if (key.name === 'enter' && !key.alt) {
|
|
1043
|
+
const item = this.completion.items && this.completion.items[this.selected];
|
|
1044
|
+
if (this.completion.kind === 'command' && item) {
|
|
1045
|
+
if (item.value === '/model' || item.value === '/sessions' || item.value === '/session' || item.value === '/new' || item.value === '/continuum') {
|
|
1046
|
+
this.editor.clear();
|
|
1047
|
+
this.completion = null;
|
|
1048
|
+
this.deliver(item.value);
|
|
1049
|
+
return true;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (this.completion.kind === 'command-argument' && item) {
|
|
1053
|
+
if (this.completion.command === 'model') {
|
|
1054
|
+
this.editor.clear();
|
|
1055
|
+
this.completion = null;
|
|
1056
|
+
this.deliver(`/model ${item.value}`);
|
|
1057
|
+
return true;
|
|
1058
|
+
}
|
|
1059
|
+
if (this.completion.command === 'sessions' || this.completion.command === 'session') {
|
|
1060
|
+
this.editor.clear();
|
|
1061
|
+
this.completion = null;
|
|
1062
|
+
this.deliver(`/sessions ${item.value}`);
|
|
1063
|
+
return true;
|
|
1064
|
+
}
|
|
1065
|
+
if (this.completion.command === 'continuum') {
|
|
1066
|
+
this.editor.clear();
|
|
1067
|
+
this.completion = null;
|
|
1068
|
+
this.deliver(`/continuum ${item.value}`);
|
|
1069
|
+
return true;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1008
1072
|
if (this.completionIsExact()) {
|
|
1009
1073
|
// Nothing left to complete. Close the picker and let the key through to
|
|
1010
1074
|
// submit, rather than re-offering what is already on the line.
|
|
@@ -1015,7 +1079,14 @@ export class ChatPrompt {
|
|
|
1015
1079
|
return true;
|
|
1016
1080
|
}
|
|
1017
1081
|
if (key.name === 'tab') { this.acceptCompletion(); return true; }
|
|
1018
|
-
if (key.name === 'escape') {
|
|
1082
|
+
if (key.name === 'escape') {
|
|
1083
|
+
this.completion = null;
|
|
1084
|
+
if (this.editor.getText().startsWith('/')) {
|
|
1085
|
+
this.editor.clear();
|
|
1086
|
+
}
|
|
1087
|
+
this.scheduleDraw();
|
|
1088
|
+
return true;
|
|
1089
|
+
}
|
|
1019
1090
|
return false;
|
|
1020
1091
|
}
|
|
1021
1092
|
|
|
@@ -0,0 +1,185 @@
|
|
|
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 } from '../sessionJournal.js';
|
|
9
|
+
function formatTimeAgo(timestamp) {
|
|
10
|
+
const diffMs = Date.now() - timestamp;
|
|
11
|
+
const minutes = Math.floor(diffMs / (60 * 1000));
|
|
12
|
+
if (minutes < 1)
|
|
13
|
+
return 'just now';
|
|
14
|
+
if (minutes < 60)
|
|
15
|
+
return `${minutes}m ago`;
|
|
16
|
+
const hours = Math.floor(minutes / (60 * 60 * 1000));
|
|
17
|
+
if (hours < 24)
|
|
18
|
+
return `${hours}h ago`;
|
|
19
|
+
const days = Math.floor(hours / (24 * 60 * 60 * 1000));
|
|
20
|
+
return `${days}d ago`;
|
|
21
|
+
}
|
|
22
|
+
function shortenPath(p) {
|
|
23
|
+
if (!p)
|
|
24
|
+
return '(default)';
|
|
25
|
+
const home = process.env.HOME || '';
|
|
26
|
+
if (home && p.startsWith(home)) {
|
|
27
|
+
return '~' + p.slice(home.length);
|
|
28
|
+
}
|
|
29
|
+
return p.split('/').pop() || p;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build item list for SelectLayer modal.
|
|
33
|
+
* The top item is always "+ New Session" to guarantee clean separation and quick reset.
|
|
34
|
+
*/
|
|
35
|
+
export function buildSessionPickerItems(sessions, activeId, currentWorkspace) {
|
|
36
|
+
const items = [];
|
|
37
|
+
// Pinned item 0: Create new clean conversation
|
|
38
|
+
items.push({
|
|
39
|
+
id: '__NEW_SESSION__',
|
|
40
|
+
label: '+ New Session (Start fresh conversation)',
|
|
41
|
+
category: 'Actions',
|
|
42
|
+
description: 'Begin a clean session in this workspace without previous context',
|
|
43
|
+
searchText: 'new session fresh clear reset start',
|
|
44
|
+
value: {
|
|
45
|
+
type: 'new'
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
for (const s of sessions) {
|
|
49
|
+
const isCurrent = s.id === activeId;
|
|
50
|
+
const timeAgo = formatTimeAgo(s.updatedAt);
|
|
51
|
+
const wsLabel = shortenPath(s.workspace);
|
|
52
|
+
const msgCount = `${s.messageCount} msg${s.messageCount === 1 ? '' : 's'}`;
|
|
53
|
+
let category = 'Other Workspaces';
|
|
54
|
+
if (s.workspace === currentWorkspace || !s.workspace) {
|
|
55
|
+
category = 'Current Workspace';
|
|
56
|
+
}
|
|
57
|
+
if (s.title.startsWith('[Adopted]')) {
|
|
58
|
+
category = 'Continuum (Peer Agent)';
|
|
59
|
+
}
|
|
60
|
+
const mark = isCurrent ? `● ` : ` `;
|
|
61
|
+
const tag = isCurrent ? ` [active]` : '';
|
|
62
|
+
items.push({
|
|
63
|
+
id: s.id,
|
|
64
|
+
label: `${mark}${s.title}${tag}`,
|
|
65
|
+
category,
|
|
66
|
+
description: `${msgCount} ${GLYPHS.dot} ${timeAgo} ${GLYPHS.dot} ${wsLabel} ${GLYPHS.dot} ${s.id}`,
|
|
67
|
+
searchText: `${s.title} ${s.id} ${s.workspace || ''} ${category}`,
|
|
68
|
+
value: {
|
|
69
|
+
type: 'switch',
|
|
70
|
+
id: s.id,
|
|
71
|
+
title: s.title,
|
|
72
|
+
workspace: s.workspace,
|
|
73
|
+
messageCount: s.messageCount,
|
|
74
|
+
isCurrent
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return items;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Launch the interactive Session Picker modal.
|
|
82
|
+
* Single Enter switches session or creates a new one immediately.
|
|
83
|
+
*/
|
|
84
|
+
export async function runInteractiveSessionPicker(prompt, options = {}) {
|
|
85
|
+
const workspace = options.workspace || process.cwd();
|
|
86
|
+
const sessions = listSessions();
|
|
87
|
+
const activeId = getActiveSessionId();
|
|
88
|
+
const items = buildSessionPickerItems(sessions, activeId, workspace);
|
|
89
|
+
const cleanPrompt = () => {
|
|
90
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
91
|
+
prompt.editor.clear();
|
|
92
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
93
|
+
prompt.scheduleDraw();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const picked = await new Promise((resolve) => {
|
|
97
|
+
const layer = new SelectLayer({
|
|
98
|
+
title: `Sessions · Manage & Switch Conversations (${sessions.length})`,
|
|
99
|
+
placeholder: 'search sessions by title, id, or workspace...',
|
|
100
|
+
emptyText: 'no matching sessions found',
|
|
101
|
+
width: 78,
|
|
102
|
+
height: 20,
|
|
103
|
+
footer: `enter select session ${GLYPHS.dot} esc cancel`,
|
|
104
|
+
items,
|
|
105
|
+
onPick: (item) => {
|
|
106
|
+
resolve(item || null);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
prompt.openLayer(layer);
|
|
110
|
+
});
|
|
111
|
+
cleanPrompt();
|
|
112
|
+
if (!picked || !picked.value) {
|
|
113
|
+
return 'Cancelled. Session was not changed.';
|
|
114
|
+
}
|
|
115
|
+
// Case 1: Start fresh new session
|
|
116
|
+
if (picked.value.type === 'new') {
|
|
117
|
+
let newSession;
|
|
118
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.startNewSession === 'function') {
|
|
119
|
+
newSession = options.loop.memoryManager.startNewSession('Untitled');
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
newSession = createSession(workspace, 'Untitled', true);
|
|
123
|
+
}
|
|
124
|
+
if (options.loop) {
|
|
125
|
+
options.loop.messages = [];
|
|
126
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
127
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
128
|
+
options.loop.tokenTracker.reset();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const renderer = prompt.renderer || options.loop?.connection?.renderer;
|
|
132
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
133
|
+
renderer.renderSessionHistory([], {
|
|
134
|
+
id: newSession.id,
|
|
135
|
+
title: newSession.title,
|
|
136
|
+
messagesCount: 0
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (prompt.surface?.viewport && typeof prompt.surface.viewport.toBottom === 'function') {
|
|
140
|
+
prompt.surface.viewport.toBottom();
|
|
141
|
+
}
|
|
142
|
+
cleanPrompt();
|
|
143
|
+
return `Started new session: "${newSession.title}" (${newSession.id}) in ${shortenPath(workspace)}.`;
|
|
144
|
+
}
|
|
145
|
+
// Case 2: Switch to selected session
|
|
146
|
+
const targetId = picked.value.id;
|
|
147
|
+
if (!targetId) {
|
|
148
|
+
return 'Cancelled. No session selected.';
|
|
149
|
+
}
|
|
150
|
+
let switched = null;
|
|
151
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.useSession === 'function') {
|
|
152
|
+
switched = options.loop.memoryManager.useSession(targetId);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
switched = switchSession(targetId);
|
|
156
|
+
}
|
|
157
|
+
if (!switched) {
|
|
158
|
+
cleanPrompt();
|
|
159
|
+
return `⚠️ Could not switch to session "${targetId}". File may have been removed.`;
|
|
160
|
+
}
|
|
161
|
+
const historyMessages = options.loop?.memoryManager && typeof options.loop.memoryManager.loadHistory === 'function'
|
|
162
|
+
? options.loop.memoryManager.loadHistory()
|
|
163
|
+
: switched.messages.filter(m => m.role !== 'system');
|
|
164
|
+
if (options.loop) {
|
|
165
|
+
options.loop.messages = historyMessages;
|
|
166
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
167
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
168
|
+
options.loop.tokenTracker.reset();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Render the conversation history to the transcript & viewport
|
|
172
|
+
const renderer = prompt.renderer || options.loop?.connection?.renderer;
|
|
173
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
174
|
+
renderer.renderSessionHistory(historyMessages, {
|
|
175
|
+
id: switched.id,
|
|
176
|
+
title: switched.title,
|
|
177
|
+
messagesCount: switched.messages.length
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (prompt.surface?.viewport && typeof prompt.surface.viewport.toBottom === 'function') {
|
|
181
|
+
prompt.surface.viewport.toBottom();
|
|
182
|
+
}
|
|
183
|
+
cleanPrompt();
|
|
184
|
+
return `Switched to "${switched.title}" (${switched.messages.length} messages, ${switched.id}).`;
|
|
185
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
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
|
+
|
|
5
|
+
// @ts-expect-error untyped local TUI dialogs
|
|
6
|
+
import { SelectLayer } from './dialogs.js';
|
|
7
|
+
// @ts-expect-error untyped local TUI theme
|
|
8
|
+
import { GLYPHS } from './theme.js';
|
|
9
|
+
import {
|
|
10
|
+
listSessions,
|
|
11
|
+
getActiveSessionId,
|
|
12
|
+
switchSession,
|
|
13
|
+
createSession,
|
|
14
|
+
type SessionRecord
|
|
15
|
+
} from '../sessionJournal.js';
|
|
16
|
+
|
|
17
|
+
export interface SessionPickerItemValue {
|
|
18
|
+
type: 'new' | 'switch';
|
|
19
|
+
id?: string | undefined;
|
|
20
|
+
title?: string | undefined;
|
|
21
|
+
workspace?: string | undefined;
|
|
22
|
+
messageCount?: number | undefined;
|
|
23
|
+
isCurrent?: boolean | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SessionPickerItem {
|
|
27
|
+
id: string;
|
|
28
|
+
label: string;
|
|
29
|
+
category: string;
|
|
30
|
+
description: string;
|
|
31
|
+
searchText?: string;
|
|
32
|
+
value: SessionPickerItemValue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface InteractiveSessionPickerOptions {
|
|
36
|
+
loop?: {
|
|
37
|
+
taskId?: string;
|
|
38
|
+
messages?: Array<{ role: string; content?: unknown }>;
|
|
39
|
+
tokenTracker?: { reset?: () => void; sessionTokens?: number };
|
|
40
|
+
memoryManager?: {
|
|
41
|
+
startNewSession?: (title?: string) => any;
|
|
42
|
+
useSession?: (id: string) => any;
|
|
43
|
+
loadHistory?: () => Array<{ role: string; content?: unknown }>;
|
|
44
|
+
sessionId?: string | null;
|
|
45
|
+
session?: any;
|
|
46
|
+
};
|
|
47
|
+
} | undefined;
|
|
48
|
+
workspace?: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function formatTimeAgo(timestamp: number): string {
|
|
52
|
+
const diffMs = Date.now() - timestamp;
|
|
53
|
+
const minutes = Math.floor(diffMs / (60 * 1000));
|
|
54
|
+
if (minutes < 1) return 'just now';
|
|
55
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
56
|
+
const hours = Math.floor(minutes / (60 * 60 * 1000));
|
|
57
|
+
if (hours < 24) return `${hours}h ago`;
|
|
58
|
+
const days = Math.floor(hours / (24 * 60 * 60 * 1000));
|
|
59
|
+
return `${days}d ago`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function shortenPath(p: string | undefined): string {
|
|
63
|
+
if (!p) return '(default)';
|
|
64
|
+
const home = process.env.HOME || '';
|
|
65
|
+
if (home && p.startsWith(home)) {
|
|
66
|
+
return '~' + p.slice(home.length);
|
|
67
|
+
}
|
|
68
|
+
return p.split('/').pop() || p;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Build item list for SelectLayer modal.
|
|
73
|
+
* The top item is always "+ New Session" to guarantee clean separation and quick reset.
|
|
74
|
+
*/
|
|
75
|
+
export function buildSessionPickerItems(
|
|
76
|
+
sessions: Array<Omit<SessionRecord, 'messages'> & { messageCount: number }>,
|
|
77
|
+
activeId: string | null,
|
|
78
|
+
currentWorkspace: string
|
|
79
|
+
): SessionPickerItem[] {
|
|
80
|
+
const items: SessionPickerItem[] = [];
|
|
81
|
+
|
|
82
|
+
// Pinned item 0: Create new clean conversation
|
|
83
|
+
items.push({
|
|
84
|
+
id: '__NEW_SESSION__',
|
|
85
|
+
label: '+ New Session (Start fresh conversation)',
|
|
86
|
+
category: 'Actions',
|
|
87
|
+
description: 'Begin a clean session in this workspace without previous context',
|
|
88
|
+
searchText: 'new session fresh clear reset start',
|
|
89
|
+
value: {
|
|
90
|
+
type: 'new'
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
for (const s of sessions) {
|
|
95
|
+
const isCurrent = s.id === activeId;
|
|
96
|
+
const timeAgo = formatTimeAgo(s.updatedAt);
|
|
97
|
+
const wsLabel = shortenPath(s.workspace);
|
|
98
|
+
const msgCount = `${s.messageCount} msg${s.messageCount === 1 ? '' : 's'}`;
|
|
99
|
+
|
|
100
|
+
let category = 'Other Workspaces';
|
|
101
|
+
if (s.workspace === currentWorkspace || !s.workspace) {
|
|
102
|
+
category = 'Current Workspace';
|
|
103
|
+
}
|
|
104
|
+
if (s.title.startsWith('[Adopted]')) {
|
|
105
|
+
category = 'Continuum (Peer Agent)';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const mark = isCurrent ? `● ` : ` `;
|
|
109
|
+
const tag = isCurrent ? ` [active]` : '';
|
|
110
|
+
|
|
111
|
+
items.push({
|
|
112
|
+
id: s.id,
|
|
113
|
+
label: `${mark}${s.title}${tag}`,
|
|
114
|
+
category,
|
|
115
|
+
description: `${msgCount} ${GLYPHS.dot} ${timeAgo} ${GLYPHS.dot} ${wsLabel} ${GLYPHS.dot} ${s.id}`,
|
|
116
|
+
searchText: `${s.title} ${s.id} ${s.workspace || ''} ${category}`,
|
|
117
|
+
value: {
|
|
118
|
+
type: 'switch',
|
|
119
|
+
id: s.id,
|
|
120
|
+
title: s.title,
|
|
121
|
+
workspace: s.workspace,
|
|
122
|
+
messageCount: s.messageCount,
|
|
123
|
+
isCurrent
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return items;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Launch the interactive Session Picker modal.
|
|
133
|
+
* Single Enter switches session or creates a new one immediately.
|
|
134
|
+
*/
|
|
135
|
+
export async function runInteractiveSessionPicker(
|
|
136
|
+
prompt: {
|
|
137
|
+
openLayer: (layer: unknown) => void;
|
|
138
|
+
editor?: { clear: () => void };
|
|
139
|
+
scheduleDraw?: () => void;
|
|
140
|
+
updateModel?: (provider?: string, model?: string) => void;
|
|
141
|
+
renderer?: { status?: { setSession: (info: any) => void } };
|
|
142
|
+
},
|
|
143
|
+
options: InteractiveSessionPickerOptions = {}
|
|
144
|
+
): Promise<string> {
|
|
145
|
+
const workspace = options.workspace || process.cwd();
|
|
146
|
+
const sessions = listSessions();
|
|
147
|
+
const activeId = getActiveSessionId();
|
|
148
|
+
|
|
149
|
+
const items = buildSessionPickerItems(sessions, activeId, workspace);
|
|
150
|
+
|
|
151
|
+
const cleanPrompt = () => {
|
|
152
|
+
if (prompt.editor && typeof prompt.editor.clear === 'function') {
|
|
153
|
+
prompt.editor.clear();
|
|
154
|
+
if (typeof prompt.scheduleDraw === 'function') prompt.scheduleDraw();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const picked = await new Promise<SessionPickerItem | null>((resolve) => {
|
|
159
|
+
const layer = new SelectLayer({
|
|
160
|
+
title: `Sessions · Manage & Switch Conversations (${sessions.length})`,
|
|
161
|
+
placeholder: 'search sessions by title, id, or workspace...',
|
|
162
|
+
emptyText: 'no matching sessions found',
|
|
163
|
+
width: 78,
|
|
164
|
+
height: 20,
|
|
165
|
+
footer: `enter select session ${GLYPHS.dot} esc cancel`,
|
|
166
|
+
items,
|
|
167
|
+
onPick: (item: SessionPickerItem | null) => {
|
|
168
|
+
resolve(item || null);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
prompt.openLayer(layer);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
cleanPrompt();
|
|
175
|
+
|
|
176
|
+
if (!picked || !picked.value) {
|
|
177
|
+
return 'Cancelled. Session was not changed.';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Case 1: Start fresh new session
|
|
181
|
+
if (picked.value.type === 'new') {
|
|
182
|
+
let newSession: SessionRecord;
|
|
183
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.startNewSession === 'function') {
|
|
184
|
+
newSession = options.loop.memoryManager.startNewSession('Untitled');
|
|
185
|
+
} else {
|
|
186
|
+
newSession = createSession(workspace, 'Untitled', true);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (options.loop) {
|
|
190
|
+
options.loop.messages = [];
|
|
191
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
192
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
193
|
+
options.loop.tokenTracker.reset();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const renderer = (prompt as any).renderer || (options.loop as any)?.connection?.renderer;
|
|
198
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
199
|
+
renderer.renderSessionHistory([], {
|
|
200
|
+
id: newSession.id,
|
|
201
|
+
title: newSession.title,
|
|
202
|
+
messagesCount: 0
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if ((prompt as any).surface?.viewport && typeof (prompt as any).surface.viewport.toBottom === 'function') {
|
|
206
|
+
(prompt as any).surface.viewport.toBottom();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
cleanPrompt();
|
|
210
|
+
return `Started new session: "${newSession.title}" (${newSession.id}) in ${shortenPath(workspace)}.`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Case 2: Switch to selected session
|
|
214
|
+
const targetId = picked.value.id;
|
|
215
|
+
if (!targetId) {
|
|
216
|
+
return 'Cancelled. No session selected.';
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let switched: SessionRecord | null = null;
|
|
220
|
+
if (options.loop?.memoryManager && typeof options.loop.memoryManager.useSession === 'function') {
|
|
221
|
+
switched = options.loop.memoryManager.useSession(targetId);
|
|
222
|
+
} else {
|
|
223
|
+
switched = switchSession(targetId);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!switched) {
|
|
227
|
+
cleanPrompt();
|
|
228
|
+
return `⚠️ Could not switch to session "${targetId}". File may have been removed.`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const historyMessages = options.loop?.memoryManager && typeof options.loop.memoryManager.loadHistory === 'function'
|
|
232
|
+
? options.loop.memoryManager.loadHistory()
|
|
233
|
+
: switched.messages.filter(m => m.role !== 'system');
|
|
234
|
+
|
|
235
|
+
if (options.loop) {
|
|
236
|
+
options.loop.messages = historyMessages;
|
|
237
|
+
options.loop.taskId = `cli_chat_${Date.now()}`;
|
|
238
|
+
if (options.loop.tokenTracker && typeof options.loop.tokenTracker.reset === 'function') {
|
|
239
|
+
options.loop.tokenTracker.reset();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Render the conversation history to the transcript & viewport
|
|
244
|
+
const renderer = (prompt as any).renderer || (options.loop as any)?.connection?.renderer;
|
|
245
|
+
if (renderer && typeof renderer.renderSessionHistory === 'function') {
|
|
246
|
+
renderer.renderSessionHistory(historyMessages, {
|
|
247
|
+
id: switched.id,
|
|
248
|
+
title: switched.title,
|
|
249
|
+
messagesCount: switched.messages.length
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if ((prompt as any).surface?.viewport && typeof (prompt as any).surface.viewport.toBottom === 'function') {
|
|
254
|
+
(prompt as any).surface.viewport.toBottom();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
cleanPrompt();
|
|
258
|
+
return `Switched to "${switched.title}" (${switched.messages.length} messages, ${switched.id}).`;
|
|
259
|
+
}
|