@causyne/agent 1.20.124 → 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/bin/causyne.js +1 -1
- package/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +6 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +142 -53
- package/src/agentLoop.ts +151 -50
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +61 -3
- package/src/browser/causyneBrowser.ts +65 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +365 -45
- package/src/commands.ts +403 -45
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- 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/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -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 +64 -9
- package/src/providers/antigravity.ts +75 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1108 -0
- package/src/tools/ptyTerminal.ts +1245 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +905 -0
- package/src/web/client/browserPanel.ts +872 -0
- package/src/web/client/chatStream.js +321 -0
- package/src/web/client/chatStream.ts +297 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +271 -0
- package/src/web/client/dom.ts +272 -0
- package/src/web/client/filesAndCanvas.js +233 -0
- package/src/web/client/filesAndCanvas.ts +240 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +316 -0
- package/src/web/client/main.ts +391 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +824 -0
- package/src/web/client/settingsModal.ts +959 -0
- package/src/web/client/state.js +280 -0
- package/src/web/client/state.ts +259 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +89 -0
- package/src/web/client/studioLayout.ts +103 -0
- package/src/web/client/studioShell.js +660 -0
- package/src/web/client/studioShell.ts +672 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +606 -0
- package/src/web/client/terminalPanel.ts +632 -0
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +322 -0
- package/src/web/client/validators.js +147 -0
- package/src/web/client/validators.ts +140 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +409 -0
- package/src/web/server/routesBrowser.ts +435 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +104 -0
- package/src/web/server/staticAssets.ts +116 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +78 -2448
- package/src/web/skybridgeServer.ts +120 -2600
- package/src/web/skybridgeUi.js +20 -450
- package/src/web/skybridgeUi.ts +24 -455
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +935 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +951 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +388 -0
- package/src/web/ui/modalsTemplate.ts +388 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +256 -0
- package/src/web/ui/studioTemplate.ts +256 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- package/src/web/skybridgeStyles.ts +0 -828
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { announceAlert, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state } from './state.js';
|
|
3
|
+
import { hydrateStartShellOptions } from './studioStart.js';
|
|
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;
|
|
13
|
+
let streamAbort = null;
|
|
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
|
+
}
|
|
23
|
+
function stopTerminalStream() {
|
|
24
|
+
if (streamAbort) {
|
|
25
|
+
try {
|
|
26
|
+
streamAbort.abort();
|
|
27
|
+
}
|
|
28
|
+
catch { /* ignore */ }
|
|
29
|
+
streamAbort = null;
|
|
30
|
+
}
|
|
31
|
+
streamedSessionId = null;
|
|
32
|
+
}
|
|
33
|
+
function activeSession() {
|
|
34
|
+
if (!state.activeTerminalId)
|
|
35
|
+
return null;
|
|
36
|
+
return state.terminalSessions.find((s) => s.id === state.activeTerminalId) ?? null;
|
|
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
|
+
}
|
|
76
|
+
function syncTerminalChrome() {
|
|
77
|
+
const cur = activeSession();
|
|
78
|
+
const hasSession = Boolean(cur);
|
|
79
|
+
if (dom.termEmpty)
|
|
80
|
+
dom.termEmpty.style.display = hasSession ? 'none' : 'flex';
|
|
81
|
+
if (dom.termViewport)
|
|
82
|
+
dom.termViewport.style.display = hasSession ? 'flex' : 'none';
|
|
83
|
+
if (dom.termControlBanner) {
|
|
84
|
+
const showAgentBanner = Boolean(cur && cur.status === 'running' && cur.control === 'agent');
|
|
85
|
+
dom.termControlBanner.classList.toggle('visible', showAgentBanner);
|
|
86
|
+
if (dom.termControlText && showAgentBanner) {
|
|
87
|
+
dom.termControlText.textContent = `Causyne is driving ${cur?.name || 'terminal'}`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (dom.termStatus) {
|
|
91
|
+
if (!cur) {
|
|
92
|
+
dom.termStatus.textContent = 'No active terminal';
|
|
93
|
+
}
|
|
94
|
+
else if (cur.status === 'exited') {
|
|
95
|
+
dom.termStatus.textContent = `${cur.name} · exited (${cur.exitCode ?? 0})`;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
dom.termStatus.textContent = `${cur.name} · ${cur.transport.toUpperCase()} · ${cur.control === 'agent' ? 'agent control' : 'interactive'}`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (dom.termMeta) {
|
|
102
|
+
dom.termMeta.textContent = cur ? `${cur.shellLabel} · ${cur.cols}×${cur.rows}` : '';
|
|
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;
|
|
310
|
+
}
|
|
311
|
+
function renderTerminalTabs() {
|
|
312
|
+
if (!dom.termTabs || !dom.termNewBtn)
|
|
313
|
+
return;
|
|
314
|
+
dom.termTabs.textContent = '';
|
|
315
|
+
for (const sess of state.terminalSessions) {
|
|
316
|
+
const pill = document.createElement('div');
|
|
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
|
+
});
|
|
324
|
+
const lbl = document.createElement('button');
|
|
325
|
+
lbl.type = 'button';
|
|
326
|
+
lbl.className = 'btab-label';
|
|
327
|
+
lbl.textContent = sess.status === 'exited' ? `${sess.name} (exited)` : sess.name;
|
|
328
|
+
lbl.addEventListener('click', () => { selectTerminalSession(sess.id); });
|
|
329
|
+
const cls = document.createElement('button');
|
|
330
|
+
cls.type = 'button';
|
|
331
|
+
cls.className = 'btab-close';
|
|
332
|
+
cls.textContent = '×';
|
|
333
|
+
cls.title = 'Close terminal';
|
|
334
|
+
cls.setAttribute('aria-label', `Close ${sess.name}`);
|
|
335
|
+
cls.addEventListener('click', () => { void closeTerminalSession(sess.id); });
|
|
336
|
+
pill.appendChild(lbl);
|
|
337
|
+
pill.appendChild(cls);
|
|
338
|
+
dom.termTabs.appendChild(pill);
|
|
339
|
+
}
|
|
340
|
+
dom.termTabs.appendChild(dom.termNewBtn);
|
|
341
|
+
syncTerminalChrome();
|
|
342
|
+
syncVisibleTerminalInstance();
|
|
343
|
+
}
|
|
344
|
+
function startTerminalStream(sessionId) {
|
|
345
|
+
if (streamedSessionId === sessionId && streamAbort)
|
|
346
|
+
return;
|
|
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
|
+
}
|
|
356
|
+
const ctrl = new AbortController();
|
|
357
|
+
streamAbort = ctrl;
|
|
358
|
+
streamedSessionId = sessionId;
|
|
359
|
+
void (async () => {
|
|
360
|
+
try {
|
|
361
|
+
const res = await fetch(apiUrl(`/api/terminal/stream?id=${encodeURIComponent(sessionId)}`), { headers: authHeaders(), signal: ctrl.signal });
|
|
362
|
+
if (!res.ok || !res.body)
|
|
363
|
+
return;
|
|
364
|
+
const reader = res.body.getReader();
|
|
365
|
+
const dec = new TextDecoder();
|
|
366
|
+
let buf = '';
|
|
367
|
+
while (true) {
|
|
368
|
+
const step = await reader.read();
|
|
369
|
+
if (step.done)
|
|
370
|
+
break;
|
|
371
|
+
buf += dec.decode(step.value, { stream: true });
|
|
372
|
+
let nl = buf.indexOf('\n');
|
|
373
|
+
while (nl !== -1) {
|
|
374
|
+
const line = buf.slice(0, nl).trim();
|
|
375
|
+
buf = buf.slice(nl + 1);
|
|
376
|
+
if (line) {
|
|
377
|
+
try {
|
|
378
|
+
const ev = JSON.parse(line);
|
|
379
|
+
const target = state.terminalSessions.find((s) => s.id === sessionId);
|
|
380
|
+
const mounted = mountedTerminals.get(sessionId);
|
|
381
|
+
if (ev.type === 'snapshot') {
|
|
382
|
+
if (target) {
|
|
383
|
+
if (ev.control)
|
|
384
|
+
target.control = ev.control;
|
|
385
|
+
if (ev.status)
|
|
386
|
+
target.status = ev.status;
|
|
387
|
+
if (ev.exitCode !== undefined)
|
|
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));
|
|
400
|
+
}
|
|
401
|
+
syncTerminalChrome();
|
|
402
|
+
}
|
|
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);
|
|
407
|
+
}
|
|
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
|
+
}
|
|
422
|
+
else if (ev.type === 'control' && ev.control) {
|
|
423
|
+
if (target)
|
|
424
|
+
target.control = ev.control;
|
|
425
|
+
syncTerminalChrome();
|
|
426
|
+
}
|
|
427
|
+
else if (ev.type === 'exit') {
|
|
428
|
+
if (target) {
|
|
429
|
+
target.status = 'exited';
|
|
430
|
+
target.exitCode = ev.exitCode ?? 0;
|
|
431
|
+
}
|
|
432
|
+
if (mounted) {
|
|
433
|
+
mounted.xterm.write(`\r\n\x1b[2m[process exited with code ${ev.exitCode ?? 0}]\x1b[0m\r\n`);
|
|
434
|
+
}
|
|
435
|
+
renderTerminalTabs();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
// Ignore malformed stream line
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
nl = buf.indexOf('\n');
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
catch {
|
|
447
|
+
// Aborted cleanly
|
|
448
|
+
}
|
|
449
|
+
finally {
|
|
450
|
+
if (streamAbort === ctrl) {
|
|
451
|
+
streamAbort = null;
|
|
452
|
+
streamedSessionId = null;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
})();
|
|
456
|
+
}
|
|
457
|
+
export function selectTerminalSession(sessionId) {
|
|
458
|
+
state.activeTerminalId = sessionId;
|
|
459
|
+
const sess = activeSession();
|
|
460
|
+
if (sess)
|
|
461
|
+
ensureMountedTerminal(sess);
|
|
462
|
+
renderTerminalTabs();
|
|
463
|
+
startTerminalStream(sessionId);
|
|
464
|
+
fitActiveTerminal();
|
|
465
|
+
focusActiveTerminal();
|
|
466
|
+
}
|
|
467
|
+
export async function loadTerminalSessions() {
|
|
468
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), { headers: authHeaders() }).catch(() => null);
|
|
469
|
+
if (!res || !res.ok)
|
|
470
|
+
return;
|
|
471
|
+
const data = await res.json();
|
|
472
|
+
if (Array.isArray(data.shells)) {
|
|
473
|
+
state.availableShells = data.shells;
|
|
474
|
+
hydrateStartShellOptions(data.shells);
|
|
475
|
+
}
|
|
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)) {
|
|
484
|
+
state.activeTerminalId = null;
|
|
485
|
+
stopTerminalStream();
|
|
486
|
+
}
|
|
487
|
+
if (!state.activeTerminalId && state.terminalSessions.length > 0) {
|
|
488
|
+
state.activeTerminalId = state.terminalSessions[0].id;
|
|
489
|
+
}
|
|
490
|
+
const cur = activeSession();
|
|
491
|
+
if (cur)
|
|
492
|
+
ensureMountedTerminal(cur);
|
|
493
|
+
renderTerminalTabs();
|
|
494
|
+
if (state.activeTerminalId) {
|
|
495
|
+
startTerminalStream(state.activeTerminalId);
|
|
496
|
+
fitActiveTerminal();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
export async function openNewTerminalSession(shell) {
|
|
500
|
+
const chosenShell = shell || dom.termShellSelect?.value || dom.startShellSelect?.value || undefined;
|
|
501
|
+
const { cols, rows } = estimateInitialDimensions();
|
|
502
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), {
|
|
503
|
+
method: 'POST',
|
|
504
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
505
|
+
body: JSON.stringify({ shell: chosenShell, cols, rows })
|
|
506
|
+
}).catch(() => null);
|
|
507
|
+
if (!res) {
|
|
508
|
+
announceAlert('Failed to open workspace terminal.');
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const data = await res.json().catch(() => ({ error: `HTTP ${res.status}` }));
|
|
512
|
+
if (!res.ok || !data.session) {
|
|
513
|
+
announceAlert(data.error || 'Failed to open workspace terminal.');
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
state.terminalSessions.push(data.session);
|
|
517
|
+
selectTerminalSession(data.session.id);
|
|
518
|
+
}
|
|
519
|
+
export async function closeTerminalSession(sessionId) {
|
|
520
|
+
await fetch(apiUrl(`/api/terminal/sessions/${encodeURIComponent(sessionId)}`), {
|
|
521
|
+
method: 'DELETE',
|
|
522
|
+
headers: authHeaders()
|
|
523
|
+
}).catch(() => null);
|
|
524
|
+
disposeMountedTerminal(sessionId);
|
|
525
|
+
state.terminalSessions = state.terminalSessions.filter((s) => s.id !== sessionId);
|
|
526
|
+
if (state.activeTerminalId === sessionId) {
|
|
527
|
+
stopTerminalStream();
|
|
528
|
+
state.activeTerminalId = state.terminalSessions[0]?.id ?? null;
|
|
529
|
+
}
|
|
530
|
+
renderTerminalTabs();
|
|
531
|
+
if (state.activeTerminalId) {
|
|
532
|
+
startTerminalStream(state.activeTerminalId);
|
|
533
|
+
fitActiveTerminal();
|
|
534
|
+
}
|
|
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);
|
|
545
|
+
}
|
|
546
|
+
state.terminalSessions = [];
|
|
547
|
+
state.activeTerminalId = null;
|
|
548
|
+
renderTerminalTabs();
|
|
549
|
+
}
|
|
550
|
+
async function sendTerminalSignal(signal) {
|
|
551
|
+
const id = state.activeTerminalId;
|
|
552
|
+
if (!id)
|
|
553
|
+
return;
|
|
554
|
+
await fetch(apiUrl('/api/terminal/signal'), {
|
|
555
|
+
method: 'POST',
|
|
556
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
557
|
+
body: JSON.stringify({ id, signal })
|
|
558
|
+
}).catch(() => null);
|
|
559
|
+
}
|
|
560
|
+
export function deactivateTerminalPanel() {
|
|
561
|
+
stopTerminalStream();
|
|
562
|
+
}
|
|
563
|
+
export function bindTerminalPanelEvents() {
|
|
564
|
+
dom.termNewBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
565
|
+
dom.termEmptyStartBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
566
|
+
dom.termSigintBtn?.addEventListener('click', () => {
|
|
567
|
+
void sendTerminalSignal('SIGINT');
|
|
568
|
+
focusActiveTerminal();
|
|
569
|
+
});
|
|
570
|
+
dom.termClearBtn?.addEventListener('click', () => {
|
|
571
|
+
if (!state.activeTerminalId)
|
|
572
|
+
return;
|
|
573
|
+
const inst = mountedTerminals.get(state.activeTerminalId);
|
|
574
|
+
if (inst) {
|
|
575
|
+
inst.xterm.clear();
|
|
576
|
+
inst.xterm.focus();
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
dom.termTakeControlBtn?.addEventListener('click', () => {
|
|
580
|
+
if (!state.activeTerminalId)
|
|
581
|
+
return;
|
|
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();
|
|
605
|
+
});
|
|
606
|
+
}
|