@causyne/agent 1.20.82 → 1.20.83
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
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Continuum TUI Picker: interactive modal dialog for browsing and adopting peer agent sessions.
|
|
2
2
|
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
3
|
// @ts-expect-error untyped local TUI dialogs
|
|
4
|
-
import {
|
|
4
|
+
import { TextLayer } from '../../tui/dialogs.js';
|
|
5
5
|
// @ts-expect-error untyped local TUI theme
|
|
6
6
|
import { GLYPHS } from '../../tui/theme.js';
|
|
7
|
+
import { ContinuumPickerLayer } from './continuumPickerView.js';
|
|
7
8
|
import { ContinuumScanner } from '../scanner.js';
|
|
8
9
|
import { adoptContinuumSession } from '../ingest.js';
|
|
9
10
|
function formatTimeAgo(timestamp) {
|
|
@@ -43,16 +44,26 @@ export async function runInteractiveContinuum(prompt, options = {}) {
|
|
|
43
44
|
}
|
|
44
45
|
const items = buildContinuumItems(sessions);
|
|
45
46
|
const pickedSession = await new Promise((resolve) => {
|
|
46
|
-
const layer = new
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
const layer = new ContinuumPickerLayer({
|
|
48
|
+
sessions: items,
|
|
49
|
+
onPick: (item) => resolve(item?.value || null),
|
|
50
|
+
onPreview: (session) => {
|
|
51
|
+
void openContinuumPreview(session.id, prompt, scanner)
|
|
52
|
+
.then((opened) => {
|
|
53
|
+
if (!opened)
|
|
54
|
+
prompt.openLayer(new TextLayer({
|
|
55
|
+
title: 'Continuum · Preview unavailable',
|
|
56
|
+
lines: ['This session could not be loaded.', 'It may have moved or been deleted.'],
|
|
57
|
+
width: 72,
|
|
58
|
+
height: 9
|
|
59
|
+
}));
|
|
60
|
+
})
|
|
61
|
+
.catch(() => prompt.openLayer(new TextLayer({
|
|
62
|
+
title: 'Continuum · Preview unavailable',
|
|
63
|
+
lines: ['The source agent session could not be read.'],
|
|
64
|
+
width: 72,
|
|
65
|
+
height: 9
|
|
66
|
+
})));
|
|
56
67
|
}
|
|
57
68
|
});
|
|
58
69
|
prompt.openLayer(layer);
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Authorship: Strict TypeScript (Causyne Agent).
|
|
3
3
|
|
|
4
4
|
// @ts-expect-error untyped local TUI dialogs
|
|
5
|
-
import {
|
|
5
|
+
import { TextLayer } from '../../tui/dialogs.js';
|
|
6
6
|
// @ts-expect-error untyped local TUI theme
|
|
7
7
|
import { GLYPHS } from '../../tui/theme.js';
|
|
8
|
+
import { ContinuumPickerLayer } from './continuumPickerView.js';
|
|
8
9
|
import type { ContinuumSessionSummary } from '../types.js';
|
|
9
10
|
import { ContinuumScanner } from '../scanner.js';
|
|
10
11
|
import { adoptContinuumSession } from '../ingest.js';
|
|
@@ -63,16 +64,25 @@ export async function runInteractiveContinuum(
|
|
|
63
64
|
const items = buildContinuumItems(sessions);
|
|
64
65
|
|
|
65
66
|
const pickedSession = await new Promise<ContinuumSessionSummary | null>((resolve) => {
|
|
66
|
-
const layer = new
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
const layer = new ContinuumPickerLayer({
|
|
68
|
+
sessions: items,
|
|
69
|
+
onPick: (item) => resolve(item?.value || null),
|
|
70
|
+
onPreview: (session) => {
|
|
71
|
+
void openContinuumPreview(session.id, prompt, scanner)
|
|
72
|
+
.then((opened) => {
|
|
73
|
+
if (!opened) prompt.openLayer(new TextLayer({
|
|
74
|
+
title: 'Continuum · Preview unavailable',
|
|
75
|
+
lines: ['This session could not be loaded.', 'It may have moved or been deleted.'],
|
|
76
|
+
width: 72,
|
|
77
|
+
height: 9
|
|
78
|
+
}));
|
|
79
|
+
})
|
|
80
|
+
.catch(() => prompt.openLayer(new TextLayer({
|
|
81
|
+
title: 'Continuum · Preview unavailable',
|
|
82
|
+
lines: ['The source agent session could not be read.'],
|
|
83
|
+
width: 72,
|
|
84
|
+
height: 9
|
|
85
|
+
})));
|
|
76
86
|
}
|
|
77
87
|
});
|
|
78
88
|
prompt.openLayer(layer);
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Continuum: provider-aware, keyboard-operated session browser.
|
|
3
|
+
* This is a presentation-only layer over the existing SelectLayer/SelectList.
|
|
4
|
+
* No adapter, journal, tool policy or adoption behavior is changed here.
|
|
5
|
+
*/
|
|
6
|
+
// @ts-expect-error The existing dialog component is implemented in JavaScript.
|
|
7
|
+
import { SelectLayer } from '../../tui/dialogs.js';
|
|
8
|
+
// @ts-expect-error The existing theme is implemented in JavaScript.
|
|
9
|
+
import { bold, dim, paint, GLYPHS } from '../../tui/theme.js';
|
|
10
|
+
import { safeDisplay, truncate, visibleWidth } from '../../tui/text.js';
|
|
11
|
+
const MAX_PANEL_HEIGHT = 34;
|
|
12
|
+
const MIN_DETAIL_WIDTH = 46;
|
|
13
|
+
const SOURCE_ORDER = [
|
|
14
|
+
'codex', 'antigravity', 'opencode', 'claude_code', 'causyne', 'cursor'
|
|
15
|
+
];
|
|
16
|
+
const PLATFORM_NAMES = {
|
|
17
|
+
codex: 'Codex',
|
|
18
|
+
antigravity: 'Antigravity',
|
|
19
|
+
opencode: 'OpenCode',
|
|
20
|
+
claude_code: 'Claude Code',
|
|
21
|
+
causyne: 'Causyne',
|
|
22
|
+
cursor: 'Cursor'
|
|
23
|
+
};
|
|
24
|
+
function clean(value) {
|
|
25
|
+
// safeDisplay strips control sequences that could corrupt a terminal frame.
|
|
26
|
+
return safeDisplay(String(value ?? '')).replace(/\s+/g, ' ').trim();
|
|
27
|
+
}
|
|
28
|
+
function shortWorkspace(value) {
|
|
29
|
+
const path = clean(value).replace(/\\/g, '/').replace(/\/+$/, '');
|
|
30
|
+
if (!path)
|
|
31
|
+
return 'Unknown workspace';
|
|
32
|
+
const parts = path.split('/').filter(Boolean);
|
|
33
|
+
return parts.length > 2 ? `…/${parts.slice(-2).join('/')}` : path;
|
|
34
|
+
}
|
|
35
|
+
function relativeTime(timestamp, now) {
|
|
36
|
+
if (!Number.isFinite(timestamp) || timestamp <= 0)
|
|
37
|
+
return 'unknown';
|
|
38
|
+
const seconds = Math.max(0, Math.floor((now - timestamp) / 1000));
|
|
39
|
+
if (seconds < 60)
|
|
40
|
+
return 'now';
|
|
41
|
+
if (seconds < 3600)
|
|
42
|
+
return `${Math.floor(seconds / 60)}m`;
|
|
43
|
+
if (seconds < 86400)
|
|
44
|
+
return `${Math.floor(seconds / 3600)}h`;
|
|
45
|
+
if (seconds < 7 * 86400)
|
|
46
|
+
return `${Math.floor(seconds / 86400)}d`;
|
|
47
|
+
return `${Math.floor(seconds / (7 * 86400))}w`;
|
|
48
|
+
}
|
|
49
|
+
function countLabel(count) {
|
|
50
|
+
if (!Number.isFinite(count) || count < 0)
|
|
51
|
+
return '0 msgs';
|
|
52
|
+
return `${Math.floor(count).toLocaleString('en-US')} ${count === 1 ? 'msg' : 'msgs'}`;
|
|
53
|
+
}
|
|
54
|
+
/** Fit two pieces of styled text in one terminal row, retaining the left title. */
|
|
55
|
+
function pairedRow(left, right, columns) {
|
|
56
|
+
const width = Math.max(1, Math.floor(columns));
|
|
57
|
+
const rightWidth = visibleWidth(right);
|
|
58
|
+
if (!right || width < 45 || rightWidth > width - 20)
|
|
59
|
+
return truncate(left, width, '…');
|
|
60
|
+
const title = truncate(left, Math.max(1, width - rightWidth - 2), '…');
|
|
61
|
+
return title + ' '.repeat(Math.max(1, width - visibleWidth(title) - rightWidth)) + right;
|
|
62
|
+
}
|
|
63
|
+
function sourceName(value) {
|
|
64
|
+
return clean(value.sourceLabel) || PLATFORM_NAMES[value.sourcePlatform] || 'Other';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Group by agent with recency inside each source. A stable tie-breaker means
|
|
68
|
+
* the selected session never jumps unpredictably between redraws.
|
|
69
|
+
*/
|
|
70
|
+
export function sortContinuumItems(items) {
|
|
71
|
+
const order = new Map(SOURCE_ORDER.map((platform, index) => [platform, index]));
|
|
72
|
+
return [...items].sort((a, b) => {
|
|
73
|
+
const groupA = order.get(a.value.sourcePlatform) ?? SOURCE_ORDER.length;
|
|
74
|
+
const groupB = order.get(b.value.sourcePlatform) ?? SOURCE_ORDER.length;
|
|
75
|
+
return groupA - groupB
|
|
76
|
+
|| (Number(b.value.updatedAt) || 0) - (Number(a.value.updatedAt) || 0)
|
|
77
|
+
|| a.value.id.localeCompare(b.value.id);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function renderedItem(item, selected, width, now) {
|
|
81
|
+
const title = clean(item.value.title) || 'Untitled session';
|
|
82
|
+
const prefix = selected ? ` ${paint('accent', '▸')} ` : ' ';
|
|
83
|
+
const stats = dim(`${countLabel(item.value.messageCount)} ${GLYPHS.dot} ${relativeTime(item.value.updatedAt, now)}`);
|
|
84
|
+
const headline = pairedRow(prefix + (selected ? bold(paint('ink', title)) : paint('ink', title)), stats, width);
|
|
85
|
+
const workspace = clean(item.value.workspacePath);
|
|
86
|
+
const sub = ` ${dim(shortWorkspace(workspace))}`;
|
|
87
|
+
return [truncate(headline, width, '…'), truncate(sub, width, '…')];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Render a viewport of category headings + TWO-row session entries.
|
|
91
|
+
* Headers never occupy the only remaining row; selected items stay visible.
|
|
92
|
+
*/
|
|
93
|
+
export function renderContinuumWindow(entries, start, selected, width, budget, now = Date.now()) {
|
|
94
|
+
const rows = [];
|
|
95
|
+
const safeWidth = Math.max(1, Math.floor(width));
|
|
96
|
+
const allowed = Math.max(0, Math.floor(budget));
|
|
97
|
+
let lastIndex = start - 1;
|
|
98
|
+
let itemCount = 0;
|
|
99
|
+
let previousGroup = null;
|
|
100
|
+
for (let index = start; index < entries.length; index++) {
|
|
101
|
+
const item = entries[index]?.item;
|
|
102
|
+
if (!item)
|
|
103
|
+
continue;
|
|
104
|
+
const group = sourceName(item.value);
|
|
105
|
+
const needHeader = group !== previousGroup;
|
|
106
|
+
const needed = 2 + (needHeader ? 1 : 0);
|
|
107
|
+
if (rows.length + needed > allowed) {
|
|
108
|
+
// With only two rows remaining, show the item without an orphaned header.
|
|
109
|
+
if (!(needHeader && rows.length + 2 <= allowed))
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
else if (needHeader) {
|
|
113
|
+
rows.push(dim(` ${truncate(group.toUpperCase(), Math.max(1, safeWidth - 5), '…')}`));
|
|
114
|
+
}
|
|
115
|
+
rows.push(...renderedItem(item, index === selected, safeWidth, now));
|
|
116
|
+
lastIndex = index;
|
|
117
|
+
itemCount++;
|
|
118
|
+
previousGroup = group;
|
|
119
|
+
}
|
|
120
|
+
return { rows, firstIndex: start, lastIndex, itemCount };
|
|
121
|
+
}
|
|
122
|
+
export class ContinuumPickerLayer extends SelectLayer {
|
|
123
|
+
items;
|
|
124
|
+
scopes;
|
|
125
|
+
onPreview;
|
|
126
|
+
scopeIndex = 0;
|
|
127
|
+
visibleItems = 5;
|
|
128
|
+
constructor(options) {
|
|
129
|
+
const items = sortContinuumItems(options.sessions);
|
|
130
|
+
super({
|
|
131
|
+
title: `Continuum · ${items.length} recent cross-agent sessions`,
|
|
132
|
+
width: 112,
|
|
133
|
+
height: MAX_PANEL_HEIGHT,
|
|
134
|
+
footer: `↑↓ move ${GLYPHS.dot} tab source ${GLYPHS.dot} F2 preview ${GLYPHS.dot} enter adopt ${GLYPHS.dot} esc close`,
|
|
135
|
+
placeholder: 'Search sessions, projects, or IDs',
|
|
136
|
+
emptyText: 'No matching sessions',
|
|
137
|
+
items,
|
|
138
|
+
onPick: options.onPick
|
|
139
|
+
});
|
|
140
|
+
this.items = items;
|
|
141
|
+
this.scopes = ['all', ...SOURCE_ORDER.filter(platform => items.some(item => item.value.sourcePlatform === platform))];
|
|
142
|
+
this.onPreview = options.onPreview;
|
|
143
|
+
}
|
|
144
|
+
get scope() { return this.scopes[this.scopeIndex] ?? 'all'; }
|
|
145
|
+
get availableScopes() { return this.scopes; }
|
|
146
|
+
preferredHeight() {
|
|
147
|
+
// No fixed 34-row empty frame when the machine has only a few sessions.
|
|
148
|
+
const items = this.list.filtered.length;
|
|
149
|
+
const categories = new Set(this.list.filtered.map(({ item }) => sourceName(item.value))).size;
|
|
150
|
+
return Math.min(MAX_PANEL_HEIGHT, Math.max(12, 3 + 4 + 2 + items * 2 + categories));
|
|
151
|
+
}
|
|
152
|
+
setScope(direction) {
|
|
153
|
+
const oldId = this.list.current?.value.id;
|
|
154
|
+
this.scopeIndex = (this.scopeIndex + direction + this.scopes.length) % this.scopes.length;
|
|
155
|
+
const scope = this.scope;
|
|
156
|
+
const chosen = scope === 'all' ? this.items :
|
|
157
|
+
this.items.filter(item => item.value.sourcePlatform === scope);
|
|
158
|
+
const query = this.list.query;
|
|
159
|
+
this.list.setItems(chosen);
|
|
160
|
+
this.list.query = query;
|
|
161
|
+
this.list.refilter();
|
|
162
|
+
const preserved = this.list.filtered.findIndex(({ item }) => item.value.id === oldId);
|
|
163
|
+
this.list.selected = preserved >= 0 ? preserved : 0;
|
|
164
|
+
this.list.offset = 0;
|
|
165
|
+
}
|
|
166
|
+
key(event) {
|
|
167
|
+
if (!event)
|
|
168
|
+
return null;
|
|
169
|
+
if (event.name === 'tab') {
|
|
170
|
+
this.setScope(event.shift ? -1 : 1);
|
|
171
|
+
return 'handled';
|
|
172
|
+
}
|
|
173
|
+
if (event.name === 'f2') {
|
|
174
|
+
const current = this.list.current;
|
|
175
|
+
if (current && this.onPreview)
|
|
176
|
+
this.onPreview(current.value);
|
|
177
|
+
return 'handled';
|
|
178
|
+
}
|
|
179
|
+
if (event.name === 'pageup' || event.name === 'pagedown') {
|
|
180
|
+
const direction = event.name === 'pageup' ? -1 : 1;
|
|
181
|
+
this.list.moveTo(this.list.selected + direction * Math.max(1, this.visibleItems - 1), Math.max(1, this.visibleItems));
|
|
182
|
+
return 'handled';
|
|
183
|
+
}
|
|
184
|
+
const query = this.list.query;
|
|
185
|
+
const result = super.key(event);
|
|
186
|
+
if (this.list.query !== query) {
|
|
187
|
+
this.list.selected = 0;
|
|
188
|
+
this.list.offset = 0;
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
body(width, height) {
|
|
193
|
+
const w = Math.max(8, Math.floor(width));
|
|
194
|
+
const h = Math.max(1, Math.floor(height));
|
|
195
|
+
const rows = [];
|
|
196
|
+
const scope = this.scope;
|
|
197
|
+
const label = scope === 'all' ? 'ALL SOURCES' : PLATFORM_NAMES[scope];
|
|
198
|
+
const filtered = this.list.filtered;
|
|
199
|
+
const count = filtered.length;
|
|
200
|
+
const total = this.items.length;
|
|
201
|
+
const narrow = w < MIN_DETAIL_WIDTH;
|
|
202
|
+
const sourceText = ` ${paint('accent', label.toUpperCase())}`;
|
|
203
|
+
rows.push(pairedRow(sourceText, w >= 49 ? dim(`${count} / ${total} sessions ${this.scopeIndex + 1}/${this.scopes.length} sources`) : '', w));
|
|
204
|
+
const lead = ` ${paint('accent', GLYPHS.arrow)} `;
|
|
205
|
+
const room = Math.max(1, w - visibleWidth(lead) - 1);
|
|
206
|
+
const query = clean(this.list.query);
|
|
207
|
+
let tail = query;
|
|
208
|
+
while (visibleWidth(tail) > room)
|
|
209
|
+
tail = [...tail].slice(1).join('');
|
|
210
|
+
const shown = tail || dim(narrow ? 'Search sessions...' : 'Search by title, project, provider, or ID...');
|
|
211
|
+
rows.push(lead + truncate(shown, room, '…'));
|
|
212
|
+
this.cursor = { x: Math.min(w - 1, visibleWidth(lead) + visibleWidth(tail)), y: 1 };
|
|
213
|
+
rows.push(paint('rule', '─'.repeat(w)));
|
|
214
|
+
// Small terminals favor visible list navigation over the detail inspector.
|
|
215
|
+
const details = h >= 18 ? 5 : h >= 12 ? 3 : h >= 9 ? 1 : 0;
|
|
216
|
+
const budget = Math.max(0, h - 3 - details);
|
|
217
|
+
if (budget > 0 && count === 0) {
|
|
218
|
+
rows.push('');
|
|
219
|
+
if (rows.length < h - details)
|
|
220
|
+
rows.push(dim(' No sessions match this filter.'));
|
|
221
|
+
if (rows.length < h - details)
|
|
222
|
+
rows.push(dim(' Tab changes source. Ctrl+U clears search.'));
|
|
223
|
+
}
|
|
224
|
+
else if (budget > 0) {
|
|
225
|
+
const selected = Math.min(Math.max(0, this.list.selected), count - 1);
|
|
226
|
+
let from = Math.min(Math.max(0, this.list.offset), selected);
|
|
227
|
+
let output = renderContinuumWindow(filtered, from, selected, w, budget);
|
|
228
|
+
while (output.lastIndex < selected && from < selected) {
|
|
229
|
+
from++;
|
|
230
|
+
output = renderContinuumWindow(filtered, from, selected, w, budget);
|
|
231
|
+
}
|
|
232
|
+
this.list.offset = from;
|
|
233
|
+
this.visibleItems = Math.max(1, output.itemCount);
|
|
234
|
+
this.list.lastItemRows = Math.max(1, this.visibleItems);
|
|
235
|
+
rows.push(...output.rows);
|
|
236
|
+
}
|
|
237
|
+
while (rows.length < h - details)
|
|
238
|
+
rows.push('');
|
|
239
|
+
const selected = this.list.current?.value;
|
|
240
|
+
if (details > 0) {
|
|
241
|
+
rows.push(paint('rule', '─'.repeat(w)));
|
|
242
|
+
}
|
|
243
|
+
if (details >= 3) {
|
|
244
|
+
const heading = selected ? ` ${sourceName(selected)}` : ' No session selected';
|
|
245
|
+
const countText = selected ? dim(countLabel(selected.messageCount)) : '';
|
|
246
|
+
rows.push(pairedRow(heading, countText, w));
|
|
247
|
+
rows.push(dim(' ' + truncate(selected ? shortWorkspace(selected.workspacePath) : 'Change the source or search query.', Math.max(1, w - 3), '…')));
|
|
248
|
+
}
|
|
249
|
+
if (details >= 5) {
|
|
250
|
+
const snippet = selected ? clean(selected.snippet) : '';
|
|
251
|
+
rows.push(dim(' ' + truncate(snippet || 'No excerpt available', Math.max(1, w - 3), '…')));
|
|
252
|
+
const id = selected ? clean(selected.id) : '';
|
|
253
|
+
rows.push(dim(' ' + truncate(id || 'No session ID', Math.max(1, w - 3), '…')));
|
|
254
|
+
}
|
|
255
|
+
while (rows.length < h)
|
|
256
|
+
rows.push('');
|
|
257
|
+
return rows.slice(0, h).map(row => truncate(row, w, '…'));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Continuum: provider-aware, keyboard-operated session browser.
|
|
3
|
+
* This is a presentation-only layer over the existing SelectLayer/SelectList.
|
|
4
|
+
* No adapter, journal, tool policy or adoption behavior is changed here.
|
|
5
|
+
*/
|
|
6
|
+
// @ts-expect-error The existing dialog component is implemented in JavaScript.
|
|
7
|
+
import { SelectLayer } from '../../tui/dialogs.js';
|
|
8
|
+
// @ts-expect-error The existing theme is implemented in JavaScript.
|
|
9
|
+
import { bold, dim, paint, GLYPHS } from '../../tui/theme.js';
|
|
10
|
+
import { safeDisplay, truncate, visibleWidth } from '../../tui/text.js';
|
|
11
|
+
import type { AgentPlatform, ContinuumSessionSummary } from '../types.js';
|
|
12
|
+
|
|
13
|
+
export interface ContinuumPickerItem {
|
|
14
|
+
label: string;
|
|
15
|
+
category: string;
|
|
16
|
+
description: string;
|
|
17
|
+
searchText: string;
|
|
18
|
+
hint?: string;
|
|
19
|
+
value: ContinuumSessionSummary;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ContinuumPickerViewOptions {
|
|
23
|
+
sessions: ContinuumPickerItem[];
|
|
24
|
+
onPick: (item: ContinuumPickerItem | null) => void;
|
|
25
|
+
onPreview?: (session: ContinuumSessionSummary) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface KeyEvent {
|
|
29
|
+
name?: string;
|
|
30
|
+
ctrl?: boolean;
|
|
31
|
+
shift?: boolean;
|
|
32
|
+
meta?: boolean;
|
|
33
|
+
sequence?: string;
|
|
34
|
+
text?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface FilteredEntry { item: ContinuumPickerItem; }
|
|
38
|
+
interface ListWindow {
|
|
39
|
+
rows: string[];
|
|
40
|
+
firstIndex: number;
|
|
41
|
+
lastIndex: number;
|
|
42
|
+
itemCount: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const MAX_PANEL_HEIGHT = 34;
|
|
46
|
+
const MIN_DETAIL_WIDTH = 46;
|
|
47
|
+
const SOURCE_ORDER: AgentPlatform[] = [
|
|
48
|
+
'codex', 'antigravity', 'opencode', 'claude_code', 'causyne', 'cursor'
|
|
49
|
+
];
|
|
50
|
+
const PLATFORM_NAMES: Record<AgentPlatform, string> = {
|
|
51
|
+
codex: 'Codex',
|
|
52
|
+
antigravity: 'Antigravity',
|
|
53
|
+
opencode: 'OpenCode',
|
|
54
|
+
claude_code: 'Claude Code',
|
|
55
|
+
causyne: 'Causyne',
|
|
56
|
+
cursor: 'Cursor'
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type Scope = AgentPlatform | 'all';
|
|
60
|
+
|
|
61
|
+
function clean(value: unknown): string {
|
|
62
|
+
// safeDisplay strips control sequences that could corrupt a terminal frame.
|
|
63
|
+
return safeDisplay(String(value ?? '')).replace(/\s+/g, ' ').trim();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function shortWorkspace(value: string): string {
|
|
67
|
+
const path = clean(value).replace(/\\/g, '/').replace(/\/+$/, '');
|
|
68
|
+
if (!path) return 'Unknown workspace';
|
|
69
|
+
const parts = path.split('/').filter(Boolean);
|
|
70
|
+
return parts.length > 2 ? `…/${parts.slice(-2).join('/')}` : path;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function relativeTime(timestamp: number, now: number): string {
|
|
74
|
+
if (!Number.isFinite(timestamp) || timestamp <= 0) return 'unknown';
|
|
75
|
+
const seconds = Math.max(0, Math.floor((now - timestamp) / 1000));
|
|
76
|
+
if (seconds < 60) return 'now';
|
|
77
|
+
if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
|
|
78
|
+
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h`;
|
|
79
|
+
if (seconds < 7 * 86400) return `${Math.floor(seconds / 86400)}d`;
|
|
80
|
+
return `${Math.floor(seconds / (7 * 86400))}w`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function countLabel(count: number): string {
|
|
84
|
+
if (!Number.isFinite(count) || count < 0) return '0 msgs';
|
|
85
|
+
return `${Math.floor(count).toLocaleString('en-US')} ${count === 1 ? 'msg' : 'msgs'}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Fit two pieces of styled text in one terminal row, retaining the left title. */
|
|
89
|
+
function pairedRow(left: string, right: string, columns: number): string {
|
|
90
|
+
const width = Math.max(1, Math.floor(columns));
|
|
91
|
+
const rightWidth = visibleWidth(right);
|
|
92
|
+
if (!right || width < 45 || rightWidth > width - 20) return truncate(left, width, '…');
|
|
93
|
+
const title = truncate(left, Math.max(1, width - rightWidth - 2), '…');
|
|
94
|
+
return title + ' '.repeat(Math.max(1, width - visibleWidth(title) - rightWidth)) + right;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function sourceName(value: ContinuumSessionSummary): string {
|
|
98
|
+
return clean(value.sourceLabel) || PLATFORM_NAMES[value.sourcePlatform] || 'Other';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Group by agent with recency inside each source. A stable tie-breaker means
|
|
103
|
+
* the selected session never jumps unpredictably between redraws.
|
|
104
|
+
*/
|
|
105
|
+
export function sortContinuumItems(items: ContinuumPickerItem[]): ContinuumPickerItem[] {
|
|
106
|
+
const order = new Map(SOURCE_ORDER.map((platform, index) => [platform, index]));
|
|
107
|
+
return [...items].sort((a, b) => {
|
|
108
|
+
const groupA = order.get(a.value.sourcePlatform) ?? SOURCE_ORDER.length;
|
|
109
|
+
const groupB = order.get(b.value.sourcePlatform) ?? SOURCE_ORDER.length;
|
|
110
|
+
return groupA - groupB
|
|
111
|
+
|| (Number(b.value.updatedAt) || 0) - (Number(a.value.updatedAt) || 0)
|
|
112
|
+
|| a.value.id.localeCompare(b.value.id);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function renderedItem(
|
|
117
|
+
item: ContinuumPickerItem, selected: boolean, width: number, now: number
|
|
118
|
+
): string[] {
|
|
119
|
+
const title = clean(item.value.title) || 'Untitled session';
|
|
120
|
+
const prefix = selected ? ` ${paint('accent', '▸')} ` : ' ';
|
|
121
|
+
const stats = dim(`${countLabel(item.value.messageCount)} ${GLYPHS.dot} ${relativeTime(item.value.updatedAt, now)}`);
|
|
122
|
+
const headline = pairedRow(
|
|
123
|
+
prefix + (selected ? bold(paint('ink', title)) : paint('ink', title)), stats, width
|
|
124
|
+
);
|
|
125
|
+
const workspace = clean(item.value.workspacePath);
|
|
126
|
+
const sub = ` ${dim(shortWorkspace(workspace))}`;
|
|
127
|
+
return [truncate(headline, width, '…'), truncate(sub, width, '…')];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Render a viewport of category headings + TWO-row session entries.
|
|
132
|
+
* Headers never occupy the only remaining row; selected items stay visible.
|
|
133
|
+
*/
|
|
134
|
+
export function renderContinuumWindow(
|
|
135
|
+
entries: FilteredEntry[], start: number, selected: number,
|
|
136
|
+
width: number, budget: number, now = Date.now()
|
|
137
|
+
): ListWindow {
|
|
138
|
+
const rows: string[] = [];
|
|
139
|
+
const safeWidth = Math.max(1, Math.floor(width));
|
|
140
|
+
const allowed = Math.max(0, Math.floor(budget));
|
|
141
|
+
let lastIndex = start - 1;
|
|
142
|
+
let itemCount = 0;
|
|
143
|
+
let previousGroup: string | null = null;
|
|
144
|
+
for (let index = start; index < entries.length; index++) {
|
|
145
|
+
const item = entries[index]?.item;
|
|
146
|
+
if (!item) continue;
|
|
147
|
+
const group = sourceName(item.value);
|
|
148
|
+
const needHeader = group !== previousGroup;
|
|
149
|
+
const needed = 2 + (needHeader ? 1 : 0);
|
|
150
|
+
if (rows.length + needed > allowed) {
|
|
151
|
+
// With only two rows remaining, show the item without an orphaned header.
|
|
152
|
+
if (!(needHeader && rows.length + 2 <= allowed)) break;
|
|
153
|
+
} else if (needHeader) {
|
|
154
|
+
rows.push(dim(` ${truncate(group.toUpperCase(), Math.max(1, safeWidth - 5), '…')}`));
|
|
155
|
+
}
|
|
156
|
+
rows.push(...renderedItem(item, index === selected, safeWidth, now));
|
|
157
|
+
lastIndex = index;
|
|
158
|
+
itemCount++;
|
|
159
|
+
previousGroup = group;
|
|
160
|
+
}
|
|
161
|
+
return { rows, firstIndex: start, lastIndex, itemCount };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export class ContinuumPickerLayer extends SelectLayer {
|
|
165
|
+
// Inherited instance fields from the existing JavaScript SelectLayer.
|
|
166
|
+
declare list: {
|
|
167
|
+
all: ContinuumPickerItem[];
|
|
168
|
+
filtered: FilteredEntry[];
|
|
169
|
+
selected: number;
|
|
170
|
+
offset: number;
|
|
171
|
+
query: string;
|
|
172
|
+
current: ContinuumPickerItem | null;
|
|
173
|
+
lastItemRows?: number;
|
|
174
|
+
setItems(items: ContinuumPickerItem[]): void;
|
|
175
|
+
moveTo(index: number, visibleRows: number): void;
|
|
176
|
+
refilter(): void;
|
|
177
|
+
};
|
|
178
|
+
declare cursor: { x: number; y: number } | null;
|
|
179
|
+
private readonly items: ContinuumPickerItem[];
|
|
180
|
+
private readonly scopes: Scope[];
|
|
181
|
+
private readonly onPreview: ((session: ContinuumSessionSummary) => void) | undefined;
|
|
182
|
+
private scopeIndex = 0;
|
|
183
|
+
private visibleItems = 5;
|
|
184
|
+
|
|
185
|
+
constructor(options: ContinuumPickerViewOptions) {
|
|
186
|
+
const items = sortContinuumItems(options.sessions);
|
|
187
|
+
super({
|
|
188
|
+
title: `Continuum · ${items.length} recent cross-agent sessions`,
|
|
189
|
+
width: 112,
|
|
190
|
+
height: MAX_PANEL_HEIGHT,
|
|
191
|
+
footer: `↑↓ move ${GLYPHS.dot} tab source ${GLYPHS.dot} F2 preview ${GLYPHS.dot} enter adopt ${GLYPHS.dot} esc close`,
|
|
192
|
+
placeholder: 'Search sessions, projects, or IDs',
|
|
193
|
+
emptyText: 'No matching sessions',
|
|
194
|
+
items,
|
|
195
|
+
onPick: options.onPick
|
|
196
|
+
});
|
|
197
|
+
this.items = items;
|
|
198
|
+
this.scopes = ['all', ...SOURCE_ORDER.filter(platform =>
|
|
199
|
+
items.some(item => item.value.sourcePlatform === platform))];
|
|
200
|
+
this.onPreview = options.onPreview;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
get scope(): Scope { return this.scopes[this.scopeIndex] ?? 'all'; }
|
|
204
|
+
get availableScopes(): readonly Scope[] { return this.scopes; }
|
|
205
|
+
|
|
206
|
+
preferredHeight(): number {
|
|
207
|
+
// No fixed 34-row empty frame when the machine has only a few sessions.
|
|
208
|
+
const items = this.list.filtered.length;
|
|
209
|
+
const categories = new Set(this.list.filtered.map(({ item }: FilteredEntry) => sourceName(item.value))).size;
|
|
210
|
+
return Math.min(MAX_PANEL_HEIGHT, Math.max(12, 3 + 4 + 2 + items * 2 + categories));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private setScope(direction: number): void {
|
|
214
|
+
const oldId = this.list.current?.value.id;
|
|
215
|
+
this.scopeIndex = (this.scopeIndex + direction + this.scopes.length) % this.scopes.length;
|
|
216
|
+
const scope = this.scope;
|
|
217
|
+
const chosen = scope === 'all' ? this.items :
|
|
218
|
+
this.items.filter(item => item.value.sourcePlatform === scope);
|
|
219
|
+
const query = this.list.query;
|
|
220
|
+
this.list.setItems(chosen);
|
|
221
|
+
this.list.query = query;
|
|
222
|
+
this.list.refilter();
|
|
223
|
+
const preserved = this.list.filtered.findIndex(({ item }: FilteredEntry) => item.value.id === oldId);
|
|
224
|
+
this.list.selected = preserved >= 0 ? preserved : 0;
|
|
225
|
+
this.list.offset = 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
key(event: KeyEvent | null): string | null {
|
|
229
|
+
if (!event) return null;
|
|
230
|
+
if (event.name === 'tab') {
|
|
231
|
+
this.setScope(event.shift ? -1 : 1);
|
|
232
|
+
return 'handled';
|
|
233
|
+
}
|
|
234
|
+
if (event.name === 'f2') {
|
|
235
|
+
const current = this.list.current;
|
|
236
|
+
if (current && this.onPreview) this.onPreview(current.value);
|
|
237
|
+
return 'handled';
|
|
238
|
+
}
|
|
239
|
+
if (event.name === 'pageup' || event.name === 'pagedown') {
|
|
240
|
+
const direction = event.name === 'pageup' ? -1 : 1;
|
|
241
|
+
this.list.moveTo(this.list.selected + direction * Math.max(1, this.visibleItems - 1),
|
|
242
|
+
Math.max(1, this.visibleItems));
|
|
243
|
+
return 'handled';
|
|
244
|
+
}
|
|
245
|
+
const query = this.list.query;
|
|
246
|
+
const result = super.key(event);
|
|
247
|
+
if (this.list.query !== query) {
|
|
248
|
+
this.list.selected = 0;
|
|
249
|
+
this.list.offset = 0;
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
body(width: number, height: number): string[] {
|
|
255
|
+
const w = Math.max(8, Math.floor(width));
|
|
256
|
+
const h = Math.max(1, Math.floor(height));
|
|
257
|
+
const rows: string[] = [];
|
|
258
|
+
const scope = this.scope;
|
|
259
|
+
const label = scope === 'all' ? 'ALL SOURCES' : PLATFORM_NAMES[scope];
|
|
260
|
+
const filtered = this.list.filtered;
|
|
261
|
+
const count = filtered.length;
|
|
262
|
+
const total = this.items.length;
|
|
263
|
+
const narrow = w < MIN_DETAIL_WIDTH;
|
|
264
|
+
|
|
265
|
+
const sourceText = ` ${paint('accent', label.toUpperCase())}`;
|
|
266
|
+
rows.push(pairedRow(sourceText,
|
|
267
|
+
w >= 49 ? dim(`${count} / ${total} sessions ${this.scopeIndex + 1}/${this.scopes.length} sources`) : '', w));
|
|
268
|
+
|
|
269
|
+
const lead = ` ${paint('accent', GLYPHS.arrow)} `;
|
|
270
|
+
const room = Math.max(1, w - visibleWidth(lead) - 1);
|
|
271
|
+
const query = clean(this.list.query);
|
|
272
|
+
let tail = query;
|
|
273
|
+
while (visibleWidth(tail) > room) tail = [...tail].slice(1).join('');
|
|
274
|
+
const shown = tail || dim(narrow ? 'Search sessions...' : 'Search by title, project, provider, or ID...');
|
|
275
|
+
rows.push(lead + truncate(shown, room, '…'));
|
|
276
|
+
this.cursor = { x: Math.min(w - 1, visibleWidth(lead) + visibleWidth(tail)), y: 1 };
|
|
277
|
+
rows.push(paint('rule', '─'.repeat(w)));
|
|
278
|
+
|
|
279
|
+
// Small terminals favor visible list navigation over the detail inspector.
|
|
280
|
+
const details = h >= 18 ? 5 : h >= 12 ? 3 : h >= 9 ? 1 : 0;
|
|
281
|
+
const budget = Math.max(0, h - 3 - details);
|
|
282
|
+
if (budget > 0 && count === 0) {
|
|
283
|
+
rows.push('');
|
|
284
|
+
if (rows.length < h - details) rows.push(dim(' No sessions match this filter.'));
|
|
285
|
+
if (rows.length < h - details) rows.push(dim(' Tab changes source. Ctrl+U clears search.'));
|
|
286
|
+
} else if (budget > 0) {
|
|
287
|
+
const selected = Math.min(Math.max(0, this.list.selected), count - 1);
|
|
288
|
+
let from = Math.min(Math.max(0, this.list.offset), selected);
|
|
289
|
+
let output = renderContinuumWindow(filtered, from, selected, w, budget);
|
|
290
|
+
while (output.lastIndex < selected && from < selected) {
|
|
291
|
+
from++;
|
|
292
|
+
output = renderContinuumWindow(filtered, from, selected, w, budget);
|
|
293
|
+
}
|
|
294
|
+
this.list.offset = from;
|
|
295
|
+
this.visibleItems = Math.max(1, output.itemCount);
|
|
296
|
+
this.list.lastItemRows = Math.max(1, this.visibleItems);
|
|
297
|
+
rows.push(...output.rows);
|
|
298
|
+
}
|
|
299
|
+
while (rows.length < h - details) rows.push('');
|
|
300
|
+
|
|
301
|
+
const selected = this.list.current?.value;
|
|
302
|
+
if (details > 0) {
|
|
303
|
+
rows.push(paint('rule', '─'.repeat(w)));
|
|
304
|
+
}
|
|
305
|
+
if (details >= 3) {
|
|
306
|
+
const heading = selected ? ` ${sourceName(selected)}` : ' No session selected';
|
|
307
|
+
const countText = selected ? dim(countLabel(selected.messageCount)) : '';
|
|
308
|
+
rows.push(pairedRow(heading, countText, w));
|
|
309
|
+
rows.push(dim(' ' + truncate(selected ? shortWorkspace(selected.workspacePath) : 'Change the source or search query.',
|
|
310
|
+
Math.max(1, w - 3), '…')));
|
|
311
|
+
}
|
|
312
|
+
if (details >= 5) {
|
|
313
|
+
const snippet = selected ? clean(selected.snippet) : '';
|
|
314
|
+
rows.push(dim(' ' + truncate(snippet || 'No excerpt available', Math.max(1, w - 3), '…')));
|
|
315
|
+
const id = selected ? clean(selected.id) : '';
|
|
316
|
+
rows.push(dim(' ' + truncate(id || 'No session ID', Math.max(1, w - 3), '…')));
|
|
317
|
+
}
|
|
318
|
+
while (rows.length < h) rows.push('');
|
|
319
|
+
return rows.slice(0, h).map(row => truncate(row, w, '…'));
|
|
320
|
+
}
|
|
321
|
+
}
|