@causyne/agent 1.20.127 → 1.20.128
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/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +3 -1
- package/src/agentLoop.js +59 -26
- package/src/agentLoop.ts +59 -24
- package/src/browser/causyneBrowser.js +31 -0
- package/src/browser/causyneBrowser.ts +32 -0
- package/src/commands.js +177 -13
- package/src/commands.ts +194 -13
- package/src/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -0
- package/src/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +2 -0
- package/src/providers/antigravity.ts +2 -0
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/ptyTerminal.js +17 -5
- package/src/tools/ptyTerminal.ts +19 -5
- package/src/web/client/browserPanel.js +234 -31
- package/src/web/client/browserPanel.ts +240 -31
- package/src/web/client/chatStream.js +19 -7
- package/src/web/client/chatStream.ts +17 -9
- package/src/web/client/cliPanel.js +3 -3
- package/src/web/client/cliPanel.ts +3 -3
- package/src/web/client/dom.js +53 -2
- package/src/web/client/dom.ts +54 -3
- package/src/web/client/filesAndCanvas.js +3 -1
- package/src/web/client/filesAndCanvas.ts +3 -1
- package/src/web/client/main.js +28 -13
- package/src/web/client/main.ts +35 -12
- package/src/web/client/settingsModal.js +641 -8
- package/src/web/client/settingsModal.ts +791 -8
- package/src/web/client/state.js +31 -1
- package/src/web/client/state.ts +27 -1
- package/src/web/client/studioLayout.js +39 -4
- package/src/web/client/studioLayout.ts +47 -4
- package/src/web/client/studioShell.js +370 -37
- package/src/web/client/studioShell.ts +368 -36
- package/src/web/client/terminalPanel.js +378 -129
- package/src/web/client/terminalPanel.ts +394 -107
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/types.ts +3 -0
- package/src/web/client/validators.js +51 -10
- package/src/web/client/validators.ts +54 -10
- package/src/web/server/routesBrowser.js +64 -10
- package/src/web/server/routesBrowser.ts +62 -13
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/staticAssets.js +24 -0
- package/src/web/server/staticAssets.ts +25 -0
- package/src/web/skybridgeServer.js +3 -0
- package/src/web/skybridgeServer.ts +2 -0
- package/src/web/skybridgeUi.js +5 -1
- package/src/web/skybridgeUi.ts +8 -1
- package/src/web/styles/overlays.css +499 -0
- package/src/web/styles/studio.css +274 -46
- package/src/web/ui/modalsTemplate.js +320 -71
- package/src/web/ui/modalsTemplate.ts +320 -71
- package/src/web/ui/studioTemplate.js +19 -21
- package/src/web/ui/studioTemplate.ts +19 -21
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import { announceAlert, dom } from './dom.js';
|
|
2
2
|
import { apiUrl, authHeaders, state } from './state.js';
|
|
3
3
|
import { hydrateStartShellOptions } from './studioStart.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import { setStudioTabDynamicTitle } from './studioShell.js';
|
|
5
|
+
import { TerminalTheme, observeTerminalCursor } from './terminalTheme.js';
|
|
6
|
+
const TYPED_INPUT_WINDOW_MS = 250;
|
|
7
|
+
const INPUT_GESTURES = ['keydown', 'beforeinput', 'paste', 'compositionend', 'drop', 'mousedown', 'touchstart'];
|
|
8
|
+
const FOCUS_REPORTS = new Set(['\x1b[I', '\x1b[O']);
|
|
9
|
+
const TERMINAL_FONT_STACK = 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace';
|
|
10
|
+
const mountedTerminals = new Map();
|
|
11
|
+
let viewportResizeObserver = null;
|
|
12
|
+
let themeObserver = null;
|
|
7
13
|
let streamAbort = null;
|
|
8
14
|
let streamedSessionId = null;
|
|
15
|
+
function resolveXtermConstructors() {
|
|
16
|
+
const g = window;
|
|
17
|
+
const TerminalCtor = g.Terminal;
|
|
18
|
+
const FitCtor = typeof g.FitAddon === 'function' ? g.FitAddon : g.FitAddon?.FitAddon;
|
|
19
|
+
if (typeof TerminalCtor !== 'function' || typeof FitCtor !== 'function')
|
|
20
|
+
return null;
|
|
21
|
+
return { TerminalCtor, FitCtor };
|
|
22
|
+
}
|
|
9
23
|
function stopTerminalStream() {
|
|
10
24
|
if (streamAbort) {
|
|
11
25
|
try {
|
|
@@ -21,13 +35,51 @@ function activeSession() {
|
|
|
21
35
|
return null;
|
|
22
36
|
return state.terminalSessions.find((s) => s.id === state.activeTerminalId) ?? null;
|
|
23
37
|
}
|
|
38
|
+
function readViewportThemeColors() {
|
|
39
|
+
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
|
|
40
|
+
const fallbackBg = isLight ? '#f6f8f6' : '#0d100d';
|
|
41
|
+
const fallbackFg = isLight ? '#182018' : '#e6ece4';
|
|
42
|
+
if (!dom.termViewport)
|
|
43
|
+
return { background: fallbackBg, foreground: fallbackFg, isLight };
|
|
44
|
+
const style = window.getComputedStyle(dom.termViewport);
|
|
45
|
+
return {
|
|
46
|
+
background: style.backgroundColor || fallbackBg,
|
|
47
|
+
foreground: style.color || fallbackFg,
|
|
48
|
+
isLight
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function applyThemeToAllMounted() {
|
|
52
|
+
const { background, foreground, isLight } = readViewportThemeColors();
|
|
53
|
+
for (const inst of mountedTerminals.values()) {
|
|
54
|
+
inst.palette.update(background, foreground, isLight);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function estimateInitialDimensions() {
|
|
58
|
+
const curMounted = state.activeTerminalId ? mountedTerminals.get(state.activeTerminalId) : undefined;
|
|
59
|
+
if (curMounted) {
|
|
60
|
+
const proposed = curMounted.fitAddon.proposeDimensions();
|
|
61
|
+
if (proposed && Number.isFinite(proposed.cols) && Number.isFinite(proposed.rows)) {
|
|
62
|
+
return {
|
|
63
|
+
cols: Math.max(20, Math.min(400, proposed.cols)),
|
|
64
|
+
rows: Math.max(6, Math.min(160, proposed.rows))
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const rect = dom.termViewport?.getBoundingClientRect();
|
|
69
|
+
if (rect && rect.width > 120 && rect.height > 80) {
|
|
70
|
+
const cols = Math.max(20, Math.min(400, Math.floor((rect.width - 28) / 7.8)));
|
|
71
|
+
const rows = Math.max(6, Math.min(160, Math.floor((rect.height - 16) / 18)));
|
|
72
|
+
return { cols, rows };
|
|
73
|
+
}
|
|
74
|
+
return { cols: 88, rows: 26 };
|
|
75
|
+
}
|
|
24
76
|
function syncTerminalChrome() {
|
|
25
77
|
const cur = activeSession();
|
|
26
78
|
const hasSession = Boolean(cur);
|
|
27
79
|
if (dom.termEmpty)
|
|
28
80
|
dom.termEmpty.style.display = hasSession ? 'none' : 'flex';
|
|
29
81
|
if (dom.termViewport)
|
|
30
|
-
dom.termViewport.style.display = hasSession ? '
|
|
82
|
+
dom.termViewport.style.display = hasSession ? 'flex' : 'none';
|
|
31
83
|
if (dom.termControlBanner) {
|
|
32
84
|
const showAgentBanner = Boolean(cur && cur.status === 'running' && cur.control === 'agent');
|
|
33
85
|
dom.termControlBanner.classList.toggle('visible', showAgentBanner);
|
|
@@ -49,6 +101,212 @@ function syncTerminalChrome() {
|
|
|
49
101
|
if (dom.termMeta) {
|
|
50
102
|
dom.termMeta.textContent = cur ? `${cur.shellLabel} · ${cur.cols}×${cur.rows}` : '';
|
|
51
103
|
}
|
|
104
|
+
setStudioTabDynamicTitle('terminal', cur ? cur.name : null);
|
|
105
|
+
}
|
|
106
|
+
function syncVisibleTerminalInstance() {
|
|
107
|
+
for (const [id, inst] of mountedTerminals) {
|
|
108
|
+
const isActive = id === state.activeTerminalId;
|
|
109
|
+
inst.hostEl.classList.toggle('active', isActive);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function scheduleRemoteResize(inst, cols, rows) {
|
|
113
|
+
if (inst.lastSyncedCols === cols && inst.lastSyncedRows === rows)
|
|
114
|
+
return;
|
|
115
|
+
const sess = state.terminalSessions.find((s) => s.id === inst.sessionId);
|
|
116
|
+
if (!sess || sess.status !== 'running')
|
|
117
|
+
return;
|
|
118
|
+
if (inst.resizeTimer)
|
|
119
|
+
clearTimeout(inst.resizeTimer);
|
|
120
|
+
inst.resizeTimer = setTimeout(() => {
|
|
121
|
+
inst.resizeTimer = null;
|
|
122
|
+
if (inst.lastSyncedCols === cols && inst.lastSyncedRows === rows)
|
|
123
|
+
return;
|
|
124
|
+
inst.lastSyncedCols = cols;
|
|
125
|
+
inst.lastSyncedRows = rows;
|
|
126
|
+
sess.cols = cols;
|
|
127
|
+
sess.rows = rows;
|
|
128
|
+
if (state.activeTerminalId === inst.sessionId && dom.termMeta) {
|
|
129
|
+
dom.termMeta.textContent = `${sess.shellLabel} · ${cols}×${rows}`;
|
|
130
|
+
}
|
|
131
|
+
void fetch(apiUrl('/api/terminal/input'), {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
134
|
+
body: JSON.stringify({ id: inst.sessionId, cols, rows })
|
|
135
|
+
}).catch(() => null);
|
|
136
|
+
}, 60);
|
|
137
|
+
}
|
|
138
|
+
function fitTerminalInstance(inst) {
|
|
139
|
+
if (!dom.termViewport || !inst.hostEl.classList.contains('active'))
|
|
140
|
+
return;
|
|
141
|
+
if (inst.hostEl.clientWidth < 40 || inst.hostEl.clientHeight < 30)
|
|
142
|
+
return;
|
|
143
|
+
const dims = inst.fitAddon.proposeDimensions();
|
|
144
|
+
if (!dims || !Number.isFinite(dims.cols) || !Number.isFinite(dims.rows))
|
|
145
|
+
return;
|
|
146
|
+
const cols = Math.max(20, Math.min(400, dims.cols));
|
|
147
|
+
const rows = Math.max(6, Math.min(160, dims.rows));
|
|
148
|
+
if (inst.xterm.cols !== cols || inst.xterm.rows !== rows) {
|
|
149
|
+
inst.xterm.resize(cols, rows);
|
|
150
|
+
}
|
|
151
|
+
scheduleRemoteResize(inst, cols, rows);
|
|
152
|
+
}
|
|
153
|
+
export function fitActiveTerminal() {
|
|
154
|
+
if (!state.activeTerminalId)
|
|
155
|
+
return;
|
|
156
|
+
const inst = mountedTerminals.get(state.activeTerminalId);
|
|
157
|
+
if (!inst)
|
|
158
|
+
return;
|
|
159
|
+
requestAnimationFrame(() => {
|
|
160
|
+
fitTerminalInstance(inst);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
export function focusActiveTerminal() {
|
|
164
|
+
if (!state.activeTerminalId)
|
|
165
|
+
return;
|
|
166
|
+
const inst = mountedTerminals.get(state.activeTerminalId);
|
|
167
|
+
if (inst)
|
|
168
|
+
inst.xterm.focus();
|
|
169
|
+
}
|
|
170
|
+
function flushInputQueue(inst) {
|
|
171
|
+
if (inst.flushingInput || inst.pendingInput.length === 0)
|
|
172
|
+
return;
|
|
173
|
+
inst.flushingInput = true;
|
|
174
|
+
void (async () => {
|
|
175
|
+
try {
|
|
176
|
+
while (inst.pendingInput.length > 0) {
|
|
177
|
+
const first = inst.pendingInput.shift();
|
|
178
|
+
let combined = first.data;
|
|
179
|
+
let typed = first.typed;
|
|
180
|
+
while (inst.pendingInput.length > 0 && inst.pendingInput[0].typed === typed) {
|
|
181
|
+
combined += inst.pendingInput.shift().data;
|
|
182
|
+
}
|
|
183
|
+
const res = await fetch(apiUrl('/api/terminal/input'), {
|
|
184
|
+
method: 'POST',
|
|
185
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
186
|
+
body: JSON.stringify({ id: inst.sessionId, data: combined, typed })
|
|
187
|
+
}).catch(() => null);
|
|
188
|
+
if (res && res.ok) {
|
|
189
|
+
const body = await res.json().catch(() => ({}));
|
|
190
|
+
const target = state.terminalSessions.find((s) => s.id === inst.sessionId);
|
|
191
|
+
if (target && body.control && target.control !== body.control) {
|
|
192
|
+
target.control = body.control;
|
|
193
|
+
syncTerminalChrome();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
inst.flushingInput = false;
|
|
200
|
+
}
|
|
201
|
+
})();
|
|
202
|
+
}
|
|
203
|
+
function enqueueTerminalInput(sessionId, data, typed) {
|
|
204
|
+
const inst = mountedTerminals.get(sessionId);
|
|
205
|
+
if (!inst)
|
|
206
|
+
return;
|
|
207
|
+
inst.pendingInput.push({ data, typed });
|
|
208
|
+
flushInputQueue(inst);
|
|
209
|
+
}
|
|
210
|
+
function disposeMountedTerminal(sessionId) {
|
|
211
|
+
const inst = mountedTerminals.get(sessionId);
|
|
212
|
+
if (!inst)
|
|
213
|
+
return;
|
|
214
|
+
if (inst.resizeTimer)
|
|
215
|
+
clearTimeout(inst.resizeTimer);
|
|
216
|
+
inst.cleanupGestures();
|
|
217
|
+
inst.dataSub.dispose();
|
|
218
|
+
inst.cursorSub.dispose();
|
|
219
|
+
inst.palette.dispose();
|
|
220
|
+
inst.xterm.dispose();
|
|
221
|
+
inst.hostEl.remove();
|
|
222
|
+
mountedTerminals.delete(sessionId);
|
|
223
|
+
}
|
|
224
|
+
function ensureMountedTerminal(session) {
|
|
225
|
+
const existing = mountedTerminals.get(session.id);
|
|
226
|
+
if (existing)
|
|
227
|
+
return existing;
|
|
228
|
+
if (!dom.termViewport)
|
|
229
|
+
return null;
|
|
230
|
+
const ctors = resolveXtermConstructors();
|
|
231
|
+
if (!ctors) {
|
|
232
|
+
announceAlert('Terminal emulator assets failed to load.');
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const hostEl = document.createElement('div');
|
|
236
|
+
hostEl.className = 'term-instance';
|
|
237
|
+
hostEl.dataset.sessionId = session.id;
|
|
238
|
+
if (session.id === state.activeTerminalId)
|
|
239
|
+
hostEl.classList.add('active');
|
|
240
|
+
dom.termViewport.appendChild(hostEl);
|
|
241
|
+
const xterm = new ctors.TerminalCtor({
|
|
242
|
+
cols: Math.max(20, Math.min(400, session.cols || 88)),
|
|
243
|
+
rows: Math.max(6, Math.min(160, session.rows || 26)),
|
|
244
|
+
cursorBlink: true,
|
|
245
|
+
cursorStyle: 'block',
|
|
246
|
+
fontFamily: TERMINAL_FONT_STACK,
|
|
247
|
+
fontSize: 13,
|
|
248
|
+
lineHeight: 1.25,
|
|
249
|
+
minimumContrastRatio: 4.5,
|
|
250
|
+
scrollback: 5000,
|
|
251
|
+
convertEol: session.transport !== 'pty',
|
|
252
|
+
allowProposedApi: false
|
|
253
|
+
});
|
|
254
|
+
const fitAddon = new ctors.FitCtor();
|
|
255
|
+
xterm.loadAddon(fitAddon);
|
|
256
|
+
xterm.open(hostEl);
|
|
257
|
+
xterm.textarea?.setAttribute('aria-label', `${session.name} interactive terminal`);
|
|
258
|
+
const palette = new TerminalTheme(xterm);
|
|
259
|
+
const { background, foreground, isLight } = readViewportThemeColors();
|
|
260
|
+
palette.update(background, foreground, isLight);
|
|
261
|
+
const cursorSub = observeTerminalCursor(xterm, hostEl, () => palette.cursor);
|
|
262
|
+
// Copy on Ctrl+C / Cmd+C when text is selected instead of sending SIGINT; allow paste naturally
|
|
263
|
+
xterm.attachCustomKeyEventHandler((ev) => {
|
|
264
|
+
const key = ev.key.toLowerCase();
|
|
265
|
+
if ((ev.metaKey || ev.ctrlKey) && key === 'c' && xterm.hasSelection()) {
|
|
266
|
+
if (ev.type === 'keydown' && navigator.clipboard?.writeText) {
|
|
267
|
+
void navigator.clipboard.writeText(xterm.getSelection()).catch(() => { });
|
|
268
|
+
}
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
if ((ev.metaKey || (ev.ctrlKey && ev.shiftKey)) && key === 'v') {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
if (ev.ctrlKey && !ev.metaKey && !ev.altKey && ev.key === '`') {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
});
|
|
279
|
+
let gestureAt = Number.NEGATIVE_INFINITY;
|
|
280
|
+
const markGesture = () => { gestureAt = performance.now(); };
|
|
281
|
+
for (const type of INPUT_GESTURES) {
|
|
282
|
+
hostEl.addEventListener(type, markGesture, { capture: true, passive: true });
|
|
283
|
+
}
|
|
284
|
+
const cleanupGestures = () => {
|
|
285
|
+
for (const type of INPUT_GESTURES) {
|
|
286
|
+
hostEl.removeEventListener(type, markGesture, { capture: true });
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const dataSub = xterm.onData((data) => {
|
|
290
|
+
const isTyped = (performance.now() - gestureAt < TYPED_INPUT_WINDOW_MS) && !FOCUS_REPORTS.has(data);
|
|
291
|
+
enqueueTerminalInput(session.id, data, isTyped);
|
|
292
|
+
});
|
|
293
|
+
const inst = {
|
|
294
|
+
sessionId: session.id,
|
|
295
|
+
hostEl,
|
|
296
|
+
xterm,
|
|
297
|
+
fitAddon,
|
|
298
|
+
palette,
|
|
299
|
+
cursorSub,
|
|
300
|
+
dataSub,
|
|
301
|
+
cleanupGestures,
|
|
302
|
+
lastSyncedCols: session.cols,
|
|
303
|
+
lastSyncedRows: session.rows,
|
|
304
|
+
resizeTimer: null,
|
|
305
|
+
pendingInput: [],
|
|
306
|
+
flushingInput: false
|
|
307
|
+
};
|
|
308
|
+
mountedTerminals.set(session.id, inst);
|
|
309
|
+
return inst;
|
|
52
310
|
}
|
|
53
311
|
function renderTerminalTabs() {
|
|
54
312
|
if (!dom.termTabs || !dom.termNewBtn)
|
|
@@ -57,6 +315,12 @@ function renderTerminalTabs() {
|
|
|
57
315
|
for (const sess of state.terminalSessions) {
|
|
58
316
|
const pill = document.createElement('div');
|
|
59
317
|
pill.className = `btab${sess.id === state.activeTerminalId ? ' active' : ''}`;
|
|
318
|
+
pill.addEventListener('auxclick', (e) => {
|
|
319
|
+
if (e.button === 1) {
|
|
320
|
+
e.preventDefault();
|
|
321
|
+
void closeTerminalSession(sess.id);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
60
324
|
const lbl = document.createElement('button');
|
|
61
325
|
lbl.type = 'button';
|
|
62
326
|
lbl.className = 'btab-label';
|
|
@@ -67,6 +331,7 @@ function renderTerminalTabs() {
|
|
|
67
331
|
cls.className = 'btab-close';
|
|
68
332
|
cls.textContent = '×';
|
|
69
333
|
cls.title = 'Close terminal';
|
|
334
|
+
cls.setAttribute('aria-label', `Close ${sess.name}`);
|
|
70
335
|
cls.addEventListener('click', () => { void closeTerminalSession(sess.id); });
|
|
71
336
|
pill.appendChild(lbl);
|
|
72
337
|
pill.appendChild(cls);
|
|
@@ -74,20 +339,20 @@ function renderTerminalTabs() {
|
|
|
74
339
|
}
|
|
75
340
|
dom.termTabs.appendChild(dom.termNewBtn);
|
|
76
341
|
syncTerminalChrome();
|
|
77
|
-
|
|
78
|
-
function appendViewportText(chunk, replace = false) {
|
|
79
|
-
if (!dom.termViewport)
|
|
80
|
-
return;
|
|
81
|
-
const next = replace ? chunk : ((dom.termViewport.textContent || '') + chunk);
|
|
82
|
-
dom.termViewport.textContent = next.length > MAX_CLIENT_VIEWPORT_CHARS
|
|
83
|
-
? next.slice(next.length - MAX_CLIENT_VIEWPORT_CHARS)
|
|
84
|
-
: next;
|
|
85
|
-
dom.termViewport.scrollTop = dom.termViewport.scrollHeight;
|
|
342
|
+
syncVisibleTerminalInstance();
|
|
86
343
|
}
|
|
87
344
|
function startTerminalStream(sessionId) {
|
|
88
345
|
if (streamedSessionId === sessionId && streamAbort)
|
|
89
346
|
return;
|
|
90
347
|
stopTerminalStream();
|
|
348
|
+
const sess = state.terminalSessions.find((s) => s.id === sessionId);
|
|
349
|
+
if (!sess)
|
|
350
|
+
return;
|
|
351
|
+
const inst = ensureMountedTerminal(sess);
|
|
352
|
+
syncVisibleTerminalInstance();
|
|
353
|
+
if (inst) {
|
|
354
|
+
requestAnimationFrame(() => fitTerminalInstance(inst));
|
|
355
|
+
}
|
|
91
356
|
const ctrl = new AbortController();
|
|
92
357
|
streamAbort = ctrl;
|
|
93
358
|
streamedSessionId = sessionId;
|
|
@@ -112,10 +377,8 @@ function startTerminalStream(sessionId) {
|
|
|
112
377
|
try {
|
|
113
378
|
const ev = JSON.parse(line);
|
|
114
379
|
const target = state.terminalSessions.find((s) => s.id === sessionId);
|
|
380
|
+
const mounted = mountedTerminals.get(sessionId);
|
|
115
381
|
if (ev.type === 'snapshot') {
|
|
116
|
-
if (sessionId === state.activeTerminalId) {
|
|
117
|
-
appendViewportText(ev.data || '', true);
|
|
118
|
-
}
|
|
119
382
|
if (target) {
|
|
120
383
|
if (ev.control)
|
|
121
384
|
target.control = ev.control;
|
|
@@ -123,14 +386,39 @@ function startTerminalStream(sessionId) {
|
|
|
123
386
|
target.status = ev.status;
|
|
124
387
|
if (ev.exitCode !== undefined)
|
|
125
388
|
target.exitCode = ev.exitCode;
|
|
389
|
+
if (typeof ev.cols === 'number')
|
|
390
|
+
target.cols = ev.cols;
|
|
391
|
+
if (typeof ev.rows === 'number')
|
|
392
|
+
target.rows = ev.rows;
|
|
393
|
+
}
|
|
394
|
+
if (mounted) {
|
|
395
|
+
mounted.xterm.reset();
|
|
396
|
+
const payload = typeof ev.rawData === 'string' ? ev.rawData : (ev.data || '');
|
|
397
|
+
if (payload)
|
|
398
|
+
mounted.xterm.write(payload);
|
|
399
|
+
requestAnimationFrame(() => fitTerminalInstance(mounted));
|
|
126
400
|
}
|
|
127
401
|
syncTerminalChrome();
|
|
128
402
|
}
|
|
129
|
-
else if (ev.type === 'output'
|
|
130
|
-
|
|
131
|
-
|
|
403
|
+
else if (ev.type === 'output') {
|
|
404
|
+
const payload = typeof ev.rawData === 'string' ? ev.rawData : (ev.data || '');
|
|
405
|
+
if (mounted && payload) {
|
|
406
|
+
mounted.xterm.write(payload);
|
|
132
407
|
}
|
|
133
408
|
}
|
|
409
|
+
else if (ev.type === 'meta') {
|
|
410
|
+
if (target) {
|
|
411
|
+
if (typeof ev.cols === 'number')
|
|
412
|
+
target.cols = ev.cols;
|
|
413
|
+
if (typeof ev.rows === 'number')
|
|
414
|
+
target.rows = ev.rows;
|
|
415
|
+
if (ev.control)
|
|
416
|
+
target.control = ev.control;
|
|
417
|
+
if (ev.status)
|
|
418
|
+
target.status = ev.status;
|
|
419
|
+
}
|
|
420
|
+
syncTerminalChrome();
|
|
421
|
+
}
|
|
134
422
|
else if (ev.type === 'control' && ev.control) {
|
|
135
423
|
if (target)
|
|
136
424
|
target.control = ev.control;
|
|
@@ -141,14 +429,14 @@ function startTerminalStream(sessionId) {
|
|
|
141
429
|
target.status = 'exited';
|
|
142
430
|
target.exitCode = ev.exitCode ?? 0;
|
|
143
431
|
}
|
|
144
|
-
if (
|
|
145
|
-
|
|
432
|
+
if (mounted) {
|
|
433
|
+
mounted.xterm.write(`\r\n\x1b[2m[process exited with code ${ev.exitCode ?? 0}]\x1b[0m\r\n`);
|
|
146
434
|
}
|
|
147
435
|
renderTerminalTabs();
|
|
148
436
|
}
|
|
149
437
|
}
|
|
150
438
|
catch {
|
|
151
|
-
// Ignore malformed line
|
|
439
|
+
// Ignore malformed stream line
|
|
152
440
|
}
|
|
153
441
|
}
|
|
154
442
|
nl = buf.indexOf('\n');
|
|
@@ -168,8 +456,13 @@ function startTerminalStream(sessionId) {
|
|
|
168
456
|
}
|
|
169
457
|
export function selectTerminalSession(sessionId) {
|
|
170
458
|
state.activeTerminalId = sessionId;
|
|
459
|
+
const sess = activeSession();
|
|
460
|
+
if (sess)
|
|
461
|
+
ensureMountedTerminal(sess);
|
|
171
462
|
renderTerminalTabs();
|
|
172
463
|
startTerminalStream(sessionId);
|
|
464
|
+
fitActiveTerminal();
|
|
465
|
+
focusActiveTerminal();
|
|
173
466
|
}
|
|
174
467
|
export async function loadTerminalSessions() {
|
|
175
468
|
const res = await fetch(apiUrl('/api/terminal/sessions'), { headers: authHeaders() }).catch(() => null);
|
|
@@ -180,25 +473,36 @@ export async function loadTerminalSessions() {
|
|
|
180
473
|
state.availableShells = data.shells;
|
|
181
474
|
hydrateStartShellOptions(data.shells);
|
|
182
475
|
}
|
|
183
|
-
|
|
184
|
-
|
|
476
|
+
const incoming = Array.isArray(data.sessions) ? data.sessions : [];
|
|
477
|
+
const validIds = new Set(incoming.map((s) => s.id));
|
|
478
|
+
for (const id of [...mountedTerminals.keys()]) {
|
|
479
|
+
if (!validIds.has(id))
|
|
480
|
+
disposeMountedTerminal(id);
|
|
481
|
+
}
|
|
482
|
+
state.terminalSessions = incoming;
|
|
483
|
+
if (state.activeTerminalId && !validIds.has(state.activeTerminalId)) {
|
|
185
484
|
state.activeTerminalId = null;
|
|
186
485
|
stopTerminalStream();
|
|
187
486
|
}
|
|
188
487
|
if (!state.activeTerminalId && state.terminalSessions.length > 0) {
|
|
189
488
|
state.activeTerminalId = state.terminalSessions[0].id;
|
|
190
489
|
}
|
|
490
|
+
const cur = activeSession();
|
|
491
|
+
if (cur)
|
|
492
|
+
ensureMountedTerminal(cur);
|
|
191
493
|
renderTerminalTabs();
|
|
192
494
|
if (state.activeTerminalId) {
|
|
193
495
|
startTerminalStream(state.activeTerminalId);
|
|
496
|
+
fitActiveTerminal();
|
|
194
497
|
}
|
|
195
498
|
}
|
|
196
499
|
export async function openNewTerminalSession(shell) {
|
|
197
500
|
const chosenShell = shell || dom.termShellSelect?.value || dom.startShellSelect?.value || undefined;
|
|
501
|
+
const { cols, rows } = estimateInitialDimensions();
|
|
198
502
|
const res = await fetch(apiUrl('/api/terminal/sessions'), {
|
|
199
503
|
method: 'POST',
|
|
200
504
|
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
201
|
-
body: JSON.stringify({ shell: chosenShell, cols
|
|
505
|
+
body: JSON.stringify({ shell: chosenShell, cols, rows })
|
|
202
506
|
}).catch(() => null);
|
|
203
507
|
if (!res) {
|
|
204
508
|
announceAlert('Failed to open workspace terminal.');
|
|
@@ -211,44 +515,37 @@ export async function openNewTerminalSession(shell) {
|
|
|
211
515
|
}
|
|
212
516
|
state.terminalSessions.push(data.session);
|
|
213
517
|
selectTerminalSession(data.session.id);
|
|
214
|
-
dom.termInput?.focus();
|
|
215
518
|
}
|
|
216
519
|
export async function closeTerminalSession(sessionId) {
|
|
217
520
|
await fetch(apiUrl(`/api/terminal/sessions/${encodeURIComponent(sessionId)}`), {
|
|
218
521
|
method: 'DELETE',
|
|
219
522
|
headers: authHeaders()
|
|
220
523
|
}).catch(() => null);
|
|
524
|
+
disposeMountedTerminal(sessionId);
|
|
221
525
|
state.terminalSessions = state.terminalSessions.filter((s) => s.id !== sessionId);
|
|
222
526
|
if (state.activeTerminalId === sessionId) {
|
|
223
527
|
stopTerminalStream();
|
|
224
528
|
state.activeTerminalId = state.terminalSessions[0]?.id ?? null;
|
|
225
|
-
if (dom.termViewport && !state.activeTerminalId)
|
|
226
|
-
dom.termViewport.textContent = '';
|
|
227
529
|
}
|
|
228
530
|
renderTerminalTabs();
|
|
229
|
-
if (state.activeTerminalId)
|
|
531
|
+
if (state.activeTerminalId) {
|
|
230
532
|
startTerminalStream(state.activeTerminalId);
|
|
231
|
-
|
|
232
|
-
async function sendTerminalData(data, typed = true) {
|
|
233
|
-
if (!state.activeTerminalId) {
|
|
234
|
-
await openNewTerminalSession();
|
|
533
|
+
fitActiveTerminal();
|
|
235
534
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const cur = activeSession();
|
|
247
|
-
if (cur && body.control) {
|
|
248
|
-
cur.control = body.control;
|
|
249
|
-
syncTerminalChrome();
|
|
250
|
-
}
|
|
535
|
+
}
|
|
536
|
+
export async function closeAllTerminalSessions() {
|
|
537
|
+
const ids = state.terminalSessions.map((s) => s.id);
|
|
538
|
+
stopTerminalStream();
|
|
539
|
+
for (const id of ids) {
|
|
540
|
+
disposeMountedTerminal(id);
|
|
541
|
+
await fetch(apiUrl(`/api/terminal/sessions/${encodeURIComponent(id)}`), {
|
|
542
|
+
method: 'DELETE',
|
|
543
|
+
headers: authHeaders()
|
|
544
|
+
}).catch(() => null);
|
|
251
545
|
}
|
|
546
|
+
state.terminalSessions = [];
|
|
547
|
+
state.activeTerminalId = null;
|
|
548
|
+
renderTerminalTabs();
|
|
252
549
|
}
|
|
253
550
|
async function sendTerminalSignal(signal) {
|
|
254
551
|
const id = state.activeTerminalId;
|
|
@@ -266,92 +563,44 @@ export function deactivateTerminalPanel() {
|
|
|
266
563
|
export function bindTerminalPanelEvents() {
|
|
267
564
|
dom.termNewBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
268
565
|
dom.termEmptyStartBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
269
|
-
dom.termSigintBtn?.addEventListener('click', () => {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
dom.termViewport.textContent = '';
|
|
273
|
-
});
|
|
274
|
-
dom.termTakeControlBtn?.addEventListener('click', () => {
|
|
275
|
-
void sendTerminalData('', true);
|
|
276
|
-
dom.termInput?.focus();
|
|
566
|
+
dom.termSigintBtn?.addEventListener('click', () => {
|
|
567
|
+
void sendTerminalSignal('SIGINT');
|
|
568
|
+
focusActiveTerminal();
|
|
277
569
|
});
|
|
278
|
-
|
|
279
|
-
if (!
|
|
570
|
+
dom.termClearBtn?.addEventListener('click', () => {
|
|
571
|
+
if (!state.activeTerminalId)
|
|
280
572
|
return;
|
|
281
|
-
const
|
|
282
|
-
if (
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
cmdHistory.shift();
|
|
286
|
-
historyIdx = cmdHistory.length;
|
|
287
|
-
}
|
|
288
|
-
dom.termInput.value = '';
|
|
289
|
-
void sendTerminalData(`${line}\n`, true);
|
|
290
|
-
};
|
|
291
|
-
dom.termSendBtn?.addEventListener('click', submitCommandLine);
|
|
292
|
-
dom.termInput?.addEventListener('keydown', (e) => {
|
|
293
|
-
if (e.key === 'Enter') {
|
|
294
|
-
e.preventDefault();
|
|
295
|
-
submitCommandLine();
|
|
296
|
-
}
|
|
297
|
-
else if (e.ctrlKey && e.key.toLowerCase() === 'c') {
|
|
298
|
-
e.preventDefault();
|
|
299
|
-
void sendTerminalSignal('SIGINT');
|
|
300
|
-
}
|
|
301
|
-
else if (e.key === 'ArrowUp' && cmdHistory.length > 0) {
|
|
302
|
-
e.preventDefault();
|
|
303
|
-
historyIdx = Math.max(0, (historyIdx < 0 ? cmdHistory.length : historyIdx) - 1);
|
|
304
|
-
if (dom.termInput)
|
|
305
|
-
dom.termInput.value = cmdHistory[historyIdx] ?? '';
|
|
306
|
-
}
|
|
307
|
-
else if (e.key === 'ArrowDown' && cmdHistory.length > 0) {
|
|
308
|
-
e.preventDefault();
|
|
309
|
-
historyIdx = Math.min(cmdHistory.length, historyIdx + 1);
|
|
310
|
-
if (dom.termInput)
|
|
311
|
-
dom.termInput.value = cmdHistory[historyIdx] ?? '';
|
|
573
|
+
const inst = mountedTerminals.get(state.activeTerminalId);
|
|
574
|
+
if (inst) {
|
|
575
|
+
inst.xterm.clear();
|
|
576
|
+
inst.xterm.focus();
|
|
312
577
|
}
|
|
313
578
|
});
|
|
314
|
-
|
|
315
|
-
dom.termViewport?.addEventListener('keydown', (e) => {
|
|
579
|
+
dom.termTakeControlBtn?.addEventListener('click', () => {
|
|
316
580
|
if (!state.activeTerminalId)
|
|
317
581
|
return;
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
if (e.key === 'Backspace') {
|
|
342
|
-
e.preventDefault();
|
|
343
|
-
void sendTerminalData('\x7f', true);
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
if (e.key === 'Tab') {
|
|
347
|
-
e.preventDefault();
|
|
348
|
-
void sendTerminalData('\t', true);
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
if (e.key.length === 1 && !e.ctrlKey && !e.altKey) {
|
|
352
|
-
e.preventDefault();
|
|
353
|
-
void sendTerminalData(e.key, true);
|
|
354
|
-
}
|
|
582
|
+
enqueueTerminalInput(state.activeTerminalId, '', true);
|
|
583
|
+
focusActiveTerminal();
|
|
584
|
+
});
|
|
585
|
+
if (dom.termViewport && typeof ResizeObserver !== 'undefined') {
|
|
586
|
+
viewportResizeObserver = new ResizeObserver(() => {
|
|
587
|
+
if (!state.activeTerminalId)
|
|
588
|
+
return;
|
|
589
|
+
const inst = mountedTerminals.get(state.activeTerminalId);
|
|
590
|
+
if (inst)
|
|
591
|
+
fitTerminalInstance(inst);
|
|
592
|
+
});
|
|
593
|
+
viewportResizeObserver.observe(dom.termViewport);
|
|
594
|
+
}
|
|
595
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
596
|
+
themeObserver = new MutationObserver(() => {
|
|
597
|
+
applyThemeToAllMounted();
|
|
598
|
+
});
|
|
599
|
+
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
600
|
+
}
|
|
601
|
+
window.addEventListener('beforeunload', () => {
|
|
602
|
+
stopTerminalStream();
|
|
603
|
+
viewportResizeObserver?.disconnect();
|
|
604
|
+
themeObserver?.disconnect();
|
|
355
605
|
});
|
|
356
|
-
window.addEventListener('beforeunload', stopTerminalStream);
|
|
357
606
|
}
|