@causyne/agent 1.20.124 → 1.20.127
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/package.json +4 -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 +83 -27
- package/src/agentLoop.ts +92 -26
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +30 -3
- package/src/browser/causyneBrowser.ts +33 -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 +188 -32
- package/src/commands.ts +209 -32
- 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/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/providers/antigravity.js +62 -9
- package/src/providers/antigravity.ts +73 -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/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/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 +1096 -0
- package/src/tools/ptyTerminal.ts +1231 -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 +702 -0
- package/src/web/client/browserPanel.ts +663 -0
- package/src/web/client/chatStream.js +309 -0
- package/src/web/client/chatStream.ts +289 -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 +220 -0
- package/src/web/client/dom.ts +221 -0
- package/src/web/client/filesAndCanvas.js +231 -0
- package/src/web/client/filesAndCanvas.ts +238 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +301 -0
- package/src/web/client/main.ts +368 -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 +191 -0
- package/src/web/client/settingsModal.ts +176 -0
- package/src/web/client/state.js +250 -0
- package/src/web/client/state.ts +233 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +54 -0
- package/src/web/client/studioLayout.ts +60 -0
- package/src/web/client/studioShell.js +327 -0
- package/src/web/client/studioShell.ts +340 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +357 -0
- package/src/web/client/terminalPanel.ts +345 -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 +319 -0
- package/src/web/client/validators.js +106 -0
- package/src/web/client/validators.ts +96 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +355 -0
- package/src/web/server/routesBrowser.ts +386 -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/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 +80 -0
- package/src/web/server/staticAssets.ts +91 -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 +76 -2449
- package/src/web/skybridgeServer.ts +118 -2600
- package/src/web/skybridgeUi.js +16 -450
- package/src/web/skybridgeUi.ts +16 -454
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +436 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +723 -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 +139 -0
- package/src/web/ui/modalsTemplate.ts +139 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +258 -0
- package/src/web/ui/studioTemplate.ts +258 -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,357 @@
|
|
|
1
|
+
import { announceAlert, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state } from './state.js';
|
|
3
|
+
import { hydrateStartShellOptions } from './studioStart.js';
|
|
4
|
+
const MAX_CLIENT_VIEWPORT_CHARS = 180_000;
|
|
5
|
+
const cmdHistory = [];
|
|
6
|
+
let historyIdx = -1;
|
|
7
|
+
let streamAbort = null;
|
|
8
|
+
let streamedSessionId = null;
|
|
9
|
+
function stopTerminalStream() {
|
|
10
|
+
if (streamAbort) {
|
|
11
|
+
try {
|
|
12
|
+
streamAbort.abort();
|
|
13
|
+
}
|
|
14
|
+
catch { /* ignore */ }
|
|
15
|
+
streamAbort = null;
|
|
16
|
+
}
|
|
17
|
+
streamedSessionId = null;
|
|
18
|
+
}
|
|
19
|
+
function activeSession() {
|
|
20
|
+
if (!state.activeTerminalId)
|
|
21
|
+
return null;
|
|
22
|
+
return state.terminalSessions.find((s) => s.id === state.activeTerminalId) ?? null;
|
|
23
|
+
}
|
|
24
|
+
function syncTerminalChrome() {
|
|
25
|
+
const cur = activeSession();
|
|
26
|
+
const hasSession = Boolean(cur);
|
|
27
|
+
if (dom.termEmpty)
|
|
28
|
+
dom.termEmpty.style.display = hasSession ? 'none' : 'flex';
|
|
29
|
+
if (dom.termViewport)
|
|
30
|
+
dom.termViewport.style.display = hasSession ? 'block' : 'none';
|
|
31
|
+
if (dom.termControlBanner) {
|
|
32
|
+
const showAgentBanner = Boolean(cur && cur.status === 'running' && cur.control === 'agent');
|
|
33
|
+
dom.termControlBanner.classList.toggle('visible', showAgentBanner);
|
|
34
|
+
if (dom.termControlText && showAgentBanner) {
|
|
35
|
+
dom.termControlText.textContent = `Causyne is driving ${cur?.name || 'terminal'}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (dom.termStatus) {
|
|
39
|
+
if (!cur) {
|
|
40
|
+
dom.termStatus.textContent = 'No active terminal';
|
|
41
|
+
}
|
|
42
|
+
else if (cur.status === 'exited') {
|
|
43
|
+
dom.termStatus.textContent = `${cur.name} · exited (${cur.exitCode ?? 0})`;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
dom.termStatus.textContent = `${cur.name} · ${cur.transport.toUpperCase()} · ${cur.control === 'agent' ? 'agent control' : 'interactive'}`;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (dom.termMeta) {
|
|
50
|
+
dom.termMeta.textContent = cur ? `${cur.shellLabel} · ${cur.cols}×${cur.rows}` : '';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function renderTerminalTabs() {
|
|
54
|
+
if (!dom.termTabs || !dom.termNewBtn)
|
|
55
|
+
return;
|
|
56
|
+
dom.termTabs.textContent = '';
|
|
57
|
+
for (const sess of state.terminalSessions) {
|
|
58
|
+
const pill = document.createElement('div');
|
|
59
|
+
pill.className = `btab${sess.id === state.activeTerminalId ? ' active' : ''}`;
|
|
60
|
+
const lbl = document.createElement('button');
|
|
61
|
+
lbl.type = 'button';
|
|
62
|
+
lbl.className = 'btab-label';
|
|
63
|
+
lbl.textContent = sess.status === 'exited' ? `${sess.name} (exited)` : sess.name;
|
|
64
|
+
lbl.addEventListener('click', () => { selectTerminalSession(sess.id); });
|
|
65
|
+
const cls = document.createElement('button');
|
|
66
|
+
cls.type = 'button';
|
|
67
|
+
cls.className = 'btab-close';
|
|
68
|
+
cls.textContent = '×';
|
|
69
|
+
cls.title = 'Close terminal';
|
|
70
|
+
cls.addEventListener('click', () => { void closeTerminalSession(sess.id); });
|
|
71
|
+
pill.appendChild(lbl);
|
|
72
|
+
pill.appendChild(cls);
|
|
73
|
+
dom.termTabs.appendChild(pill);
|
|
74
|
+
}
|
|
75
|
+
dom.termTabs.appendChild(dom.termNewBtn);
|
|
76
|
+
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;
|
|
86
|
+
}
|
|
87
|
+
function startTerminalStream(sessionId) {
|
|
88
|
+
if (streamedSessionId === sessionId && streamAbort)
|
|
89
|
+
return;
|
|
90
|
+
stopTerminalStream();
|
|
91
|
+
const ctrl = new AbortController();
|
|
92
|
+
streamAbort = ctrl;
|
|
93
|
+
streamedSessionId = sessionId;
|
|
94
|
+
void (async () => {
|
|
95
|
+
try {
|
|
96
|
+
const res = await fetch(apiUrl(`/api/terminal/stream?id=${encodeURIComponent(sessionId)}`), { headers: authHeaders(), signal: ctrl.signal });
|
|
97
|
+
if (!res.ok || !res.body)
|
|
98
|
+
return;
|
|
99
|
+
const reader = res.body.getReader();
|
|
100
|
+
const dec = new TextDecoder();
|
|
101
|
+
let buf = '';
|
|
102
|
+
while (true) {
|
|
103
|
+
const step = await reader.read();
|
|
104
|
+
if (step.done)
|
|
105
|
+
break;
|
|
106
|
+
buf += dec.decode(step.value, { stream: true });
|
|
107
|
+
let nl = buf.indexOf('\n');
|
|
108
|
+
while (nl !== -1) {
|
|
109
|
+
const line = buf.slice(0, nl).trim();
|
|
110
|
+
buf = buf.slice(nl + 1);
|
|
111
|
+
if (line) {
|
|
112
|
+
try {
|
|
113
|
+
const ev = JSON.parse(line);
|
|
114
|
+
const target = state.terminalSessions.find((s) => s.id === sessionId);
|
|
115
|
+
if (ev.type === 'snapshot') {
|
|
116
|
+
if (sessionId === state.activeTerminalId) {
|
|
117
|
+
appendViewportText(ev.data || '', true);
|
|
118
|
+
}
|
|
119
|
+
if (target) {
|
|
120
|
+
if (ev.control)
|
|
121
|
+
target.control = ev.control;
|
|
122
|
+
if (ev.status)
|
|
123
|
+
target.status = ev.status;
|
|
124
|
+
if (ev.exitCode !== undefined)
|
|
125
|
+
target.exitCode = ev.exitCode;
|
|
126
|
+
}
|
|
127
|
+
syncTerminalChrome();
|
|
128
|
+
}
|
|
129
|
+
else if (ev.type === 'output' && typeof ev.data === 'string') {
|
|
130
|
+
if (sessionId === state.activeTerminalId) {
|
|
131
|
+
appendViewportText(ev.data, false);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (ev.type === 'control' && ev.control) {
|
|
135
|
+
if (target)
|
|
136
|
+
target.control = ev.control;
|
|
137
|
+
syncTerminalChrome();
|
|
138
|
+
}
|
|
139
|
+
else if (ev.type === 'exit') {
|
|
140
|
+
if (target) {
|
|
141
|
+
target.status = 'exited';
|
|
142
|
+
target.exitCode = ev.exitCode ?? 0;
|
|
143
|
+
}
|
|
144
|
+
if (sessionId === state.activeTerminalId) {
|
|
145
|
+
appendViewportText(`\n[process exited with code ${ev.exitCode ?? 0}]\n`, false);
|
|
146
|
+
}
|
|
147
|
+
renderTerminalTabs();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// Ignore malformed line
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
nl = buf.indexOf('\n');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
// Aborted cleanly
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
if (streamAbort === ctrl) {
|
|
163
|
+
streamAbort = null;
|
|
164
|
+
streamedSessionId = null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
}
|
|
169
|
+
export function selectTerminalSession(sessionId) {
|
|
170
|
+
state.activeTerminalId = sessionId;
|
|
171
|
+
renderTerminalTabs();
|
|
172
|
+
startTerminalStream(sessionId);
|
|
173
|
+
}
|
|
174
|
+
export async function loadTerminalSessions() {
|
|
175
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), { headers: authHeaders() }).catch(() => null);
|
|
176
|
+
if (!res || !res.ok)
|
|
177
|
+
return;
|
|
178
|
+
const data = await res.json();
|
|
179
|
+
if (Array.isArray(data.shells)) {
|
|
180
|
+
state.availableShells = data.shells;
|
|
181
|
+
hydrateStartShellOptions(data.shells);
|
|
182
|
+
}
|
|
183
|
+
state.terminalSessions = Array.isArray(data.sessions) ? data.sessions : [];
|
|
184
|
+
if (state.activeTerminalId && !state.terminalSessions.some((s) => s.id === state.activeTerminalId)) {
|
|
185
|
+
state.activeTerminalId = null;
|
|
186
|
+
stopTerminalStream();
|
|
187
|
+
}
|
|
188
|
+
if (!state.activeTerminalId && state.terminalSessions.length > 0) {
|
|
189
|
+
state.activeTerminalId = state.terminalSessions[0].id;
|
|
190
|
+
}
|
|
191
|
+
renderTerminalTabs();
|
|
192
|
+
if (state.activeTerminalId) {
|
|
193
|
+
startTerminalStream(state.activeTerminalId);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export async function openNewTerminalSession(shell) {
|
|
197
|
+
const chosenShell = shell || dom.termShellSelect?.value || dom.startShellSelect?.value || undefined;
|
|
198
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), {
|
|
199
|
+
method: 'POST',
|
|
200
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
201
|
+
body: JSON.stringify({ shell: chosenShell, cols: 100, rows: 28 })
|
|
202
|
+
}).catch(() => null);
|
|
203
|
+
if (!res) {
|
|
204
|
+
announceAlert('Failed to open workspace terminal.');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const data = await res.json().catch(() => ({ error: `HTTP ${res.status}` }));
|
|
208
|
+
if (!res.ok || !data.session) {
|
|
209
|
+
announceAlert(data.error || 'Failed to open workspace terminal.');
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
state.terminalSessions.push(data.session);
|
|
213
|
+
selectTerminalSession(data.session.id);
|
|
214
|
+
dom.termInput?.focus();
|
|
215
|
+
}
|
|
216
|
+
export async function closeTerminalSession(sessionId) {
|
|
217
|
+
await fetch(apiUrl(`/api/terminal/sessions/${encodeURIComponent(sessionId)}`), {
|
|
218
|
+
method: 'DELETE',
|
|
219
|
+
headers: authHeaders()
|
|
220
|
+
}).catch(() => null);
|
|
221
|
+
state.terminalSessions = state.terminalSessions.filter((s) => s.id !== sessionId);
|
|
222
|
+
if (state.activeTerminalId === sessionId) {
|
|
223
|
+
stopTerminalStream();
|
|
224
|
+
state.activeTerminalId = state.terminalSessions[0]?.id ?? null;
|
|
225
|
+
if (dom.termViewport && !state.activeTerminalId)
|
|
226
|
+
dom.termViewport.textContent = '';
|
|
227
|
+
}
|
|
228
|
+
renderTerminalTabs();
|
|
229
|
+
if (state.activeTerminalId)
|
|
230
|
+
startTerminalStream(state.activeTerminalId);
|
|
231
|
+
}
|
|
232
|
+
async function sendTerminalData(data, typed = true) {
|
|
233
|
+
if (!state.activeTerminalId) {
|
|
234
|
+
await openNewTerminalSession();
|
|
235
|
+
}
|
|
236
|
+
const id = state.activeTerminalId;
|
|
237
|
+
if (!id)
|
|
238
|
+
return;
|
|
239
|
+
const res = await fetch(apiUrl('/api/terminal/input'), {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
242
|
+
body: JSON.stringify({ id, data, typed })
|
|
243
|
+
}).catch(() => null);
|
|
244
|
+
if (res && res.ok) {
|
|
245
|
+
const body = await res.json().catch(() => ({}));
|
|
246
|
+
const cur = activeSession();
|
|
247
|
+
if (cur && body.control) {
|
|
248
|
+
cur.control = body.control;
|
|
249
|
+
syncTerminalChrome();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async function sendTerminalSignal(signal) {
|
|
254
|
+
const id = state.activeTerminalId;
|
|
255
|
+
if (!id)
|
|
256
|
+
return;
|
|
257
|
+
await fetch(apiUrl('/api/terminal/signal'), {
|
|
258
|
+
method: 'POST',
|
|
259
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
260
|
+
body: JSON.stringify({ id, signal })
|
|
261
|
+
}).catch(() => null);
|
|
262
|
+
}
|
|
263
|
+
export function deactivateTerminalPanel() {
|
|
264
|
+
stopTerminalStream();
|
|
265
|
+
}
|
|
266
|
+
export function bindTerminalPanelEvents() {
|
|
267
|
+
dom.termNewBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
268
|
+
dom.termEmptyStartBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
269
|
+
dom.termSigintBtn?.addEventListener('click', () => { void sendTerminalSignal('SIGINT'); });
|
|
270
|
+
dom.termClearBtn?.addEventListener('click', () => {
|
|
271
|
+
if (dom.termViewport)
|
|
272
|
+
dom.termViewport.textContent = '';
|
|
273
|
+
});
|
|
274
|
+
dom.termTakeControlBtn?.addEventListener('click', () => {
|
|
275
|
+
void sendTerminalData('', true);
|
|
276
|
+
dom.termInput?.focus();
|
|
277
|
+
});
|
|
278
|
+
const submitCommandLine = () => {
|
|
279
|
+
if (!dom.termInput)
|
|
280
|
+
return;
|
|
281
|
+
const line = dom.termInput.value;
|
|
282
|
+
if (line.trim()) {
|
|
283
|
+
cmdHistory.push(line);
|
|
284
|
+
if (cmdHistory.length > 100)
|
|
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] ?? '';
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
// Direct interactive keyboard forwarding when viewport itself is focused
|
|
315
|
+
dom.termViewport?.addEventListener('keydown', (e) => {
|
|
316
|
+
if (!state.activeTerminalId)
|
|
317
|
+
return;
|
|
318
|
+
if (e.metaKey)
|
|
319
|
+
return;
|
|
320
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'c') {
|
|
321
|
+
e.preventDefault();
|
|
322
|
+
void sendTerminalSignal('SIGINT');
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'd') {
|
|
326
|
+
e.preventDefault();
|
|
327
|
+
void sendTerminalData('\x04', true);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'l') {
|
|
331
|
+
e.preventDefault();
|
|
332
|
+
if (dom.termViewport)
|
|
333
|
+
dom.termViewport.textContent = '';
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (e.key === 'Enter') {
|
|
337
|
+
e.preventDefault();
|
|
338
|
+
void sendTerminalData('\n', true);
|
|
339
|
+
return;
|
|
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
|
+
}
|
|
355
|
+
});
|
|
356
|
+
window.addEventListener('beforeunload', stopTerminalStream);
|
|
357
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { announceAlert, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state } from './state.js';
|
|
3
|
+
import { hydrateStartShellOptions } from './studioStart.js';
|
|
4
|
+
import type { AvailableShellPayload, TerminalSessionPayload } from './types.js';
|
|
5
|
+
|
|
6
|
+
interface TerminalStreamChunk {
|
|
7
|
+
type?: string;
|
|
8
|
+
sessionId?: string;
|
|
9
|
+
data?: string;
|
|
10
|
+
control?: 'user' | 'agent';
|
|
11
|
+
status?: 'running' | 'exited';
|
|
12
|
+
exitCode?: number | null;
|
|
13
|
+
cols?: number;
|
|
14
|
+
rows?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const MAX_CLIENT_VIEWPORT_CHARS = 180_000;
|
|
18
|
+
const cmdHistory: string[] = [];
|
|
19
|
+
let historyIdx = -1;
|
|
20
|
+
let streamAbort: AbortController | null = null;
|
|
21
|
+
let streamedSessionId: string | null = null;
|
|
22
|
+
|
|
23
|
+
function stopTerminalStream(): void {
|
|
24
|
+
if (streamAbort) {
|
|
25
|
+
try { streamAbort.abort(); } catch { /* ignore */ }
|
|
26
|
+
streamAbort = null;
|
|
27
|
+
}
|
|
28
|
+
streamedSessionId = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function activeSession(): TerminalSessionPayload | null {
|
|
32
|
+
if (!state.activeTerminalId) return null;
|
|
33
|
+
return state.terminalSessions.find((s) => s.id === state.activeTerminalId) ?? null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function syncTerminalChrome(): void {
|
|
37
|
+
const cur = activeSession();
|
|
38
|
+
const hasSession = Boolean(cur);
|
|
39
|
+
if (dom.termEmpty) dom.termEmpty.style.display = hasSession ? 'none' : 'flex';
|
|
40
|
+
if (dom.termViewport) dom.termViewport.style.display = hasSession ? 'block' : 'none';
|
|
41
|
+
if (dom.termControlBanner) {
|
|
42
|
+
const showAgentBanner = Boolean(cur && cur.status === 'running' && cur.control === 'agent');
|
|
43
|
+
dom.termControlBanner.classList.toggle('visible', showAgentBanner);
|
|
44
|
+
if (dom.termControlText && showAgentBanner) {
|
|
45
|
+
dom.termControlText.textContent = `Causyne is driving ${cur?.name || 'terminal'}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (dom.termStatus) {
|
|
49
|
+
if (!cur) {
|
|
50
|
+
dom.termStatus.textContent = 'No active terminal';
|
|
51
|
+
} else if (cur.status === 'exited') {
|
|
52
|
+
dom.termStatus.textContent = `${cur.name} · exited (${cur.exitCode ?? 0})`;
|
|
53
|
+
} else {
|
|
54
|
+
dom.termStatus.textContent = `${cur.name} · ${cur.transport.toUpperCase()} · ${cur.control === 'agent' ? 'agent control' : 'interactive'}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (dom.termMeta) {
|
|
58
|
+
dom.termMeta.textContent = cur ? `${cur.shellLabel} · ${cur.cols}×${cur.rows}` : '';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function renderTerminalTabs(): void {
|
|
63
|
+
if (!dom.termTabs || !dom.termNewBtn) return;
|
|
64
|
+
dom.termTabs.textContent = '';
|
|
65
|
+
for (const sess of state.terminalSessions) {
|
|
66
|
+
const pill = document.createElement('div');
|
|
67
|
+
pill.className = `btab${sess.id === state.activeTerminalId ? ' active' : ''}`;
|
|
68
|
+
const lbl = document.createElement('button');
|
|
69
|
+
lbl.type = 'button';
|
|
70
|
+
lbl.className = 'btab-label';
|
|
71
|
+
lbl.textContent = sess.status === 'exited' ? `${sess.name} (exited)` : sess.name;
|
|
72
|
+
lbl.addEventListener('click', () => { selectTerminalSession(sess.id); });
|
|
73
|
+
const cls = document.createElement('button');
|
|
74
|
+
cls.type = 'button';
|
|
75
|
+
cls.className = 'btab-close';
|
|
76
|
+
cls.textContent = '×';
|
|
77
|
+
cls.title = 'Close terminal';
|
|
78
|
+
cls.addEventListener('click', () => { void closeTerminalSession(sess.id); });
|
|
79
|
+
pill.appendChild(lbl);
|
|
80
|
+
pill.appendChild(cls);
|
|
81
|
+
dom.termTabs.appendChild(pill);
|
|
82
|
+
}
|
|
83
|
+
dom.termTabs.appendChild(dom.termNewBtn);
|
|
84
|
+
syncTerminalChrome();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function appendViewportText(chunk: string, replace = false): void {
|
|
88
|
+
if (!dom.termViewport) return;
|
|
89
|
+
const next = replace ? chunk : ((dom.termViewport.textContent || '') + chunk);
|
|
90
|
+
dom.termViewport.textContent = next.length > MAX_CLIENT_VIEWPORT_CHARS
|
|
91
|
+
? next.slice(next.length - MAX_CLIENT_VIEWPORT_CHARS)
|
|
92
|
+
: next;
|
|
93
|
+
dom.termViewport.scrollTop = dom.termViewport.scrollHeight;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function startTerminalStream(sessionId: string): void {
|
|
97
|
+
if (streamedSessionId === sessionId && streamAbort) return;
|
|
98
|
+
stopTerminalStream();
|
|
99
|
+
const ctrl = new AbortController();
|
|
100
|
+
streamAbort = ctrl;
|
|
101
|
+
streamedSessionId = sessionId;
|
|
102
|
+
|
|
103
|
+
void (async () => {
|
|
104
|
+
try {
|
|
105
|
+
const res = await fetch(
|
|
106
|
+
apiUrl(`/api/terminal/stream?id=${encodeURIComponent(sessionId)}`),
|
|
107
|
+
{ headers: authHeaders(), signal: ctrl.signal }
|
|
108
|
+
);
|
|
109
|
+
if (!res.ok || !res.body) return;
|
|
110
|
+
const reader = res.body.getReader();
|
|
111
|
+
const dec = new TextDecoder();
|
|
112
|
+
let buf = '';
|
|
113
|
+
while (true) {
|
|
114
|
+
const step = await reader.read();
|
|
115
|
+
if (step.done) break;
|
|
116
|
+
buf += dec.decode(step.value, { stream: true });
|
|
117
|
+
let nl = buf.indexOf('\n');
|
|
118
|
+
while (nl !== -1) {
|
|
119
|
+
const line = buf.slice(0, nl).trim();
|
|
120
|
+
buf = buf.slice(nl + 1);
|
|
121
|
+
if (line) {
|
|
122
|
+
try {
|
|
123
|
+
const ev = JSON.parse(line) as TerminalStreamChunk;
|
|
124
|
+
const target = state.terminalSessions.find((s) => s.id === sessionId);
|
|
125
|
+
if (ev.type === 'snapshot') {
|
|
126
|
+
if (sessionId === state.activeTerminalId) {
|
|
127
|
+
appendViewportText(ev.data || '', true);
|
|
128
|
+
}
|
|
129
|
+
if (target) {
|
|
130
|
+
if (ev.control) target.control = ev.control;
|
|
131
|
+
if (ev.status) target.status = ev.status;
|
|
132
|
+
if (ev.exitCode !== undefined) target.exitCode = ev.exitCode;
|
|
133
|
+
}
|
|
134
|
+
syncTerminalChrome();
|
|
135
|
+
} else if (ev.type === 'output' && typeof ev.data === 'string') {
|
|
136
|
+
if (sessionId === state.activeTerminalId) {
|
|
137
|
+
appendViewportText(ev.data, false);
|
|
138
|
+
}
|
|
139
|
+
} else if (ev.type === 'control' && ev.control) {
|
|
140
|
+
if (target) target.control = ev.control;
|
|
141
|
+
syncTerminalChrome();
|
|
142
|
+
} else if (ev.type === 'exit') {
|
|
143
|
+
if (target) {
|
|
144
|
+
target.status = 'exited';
|
|
145
|
+
target.exitCode = ev.exitCode ?? 0;
|
|
146
|
+
}
|
|
147
|
+
if (sessionId === state.activeTerminalId) {
|
|
148
|
+
appendViewportText(`\n[process exited with code ${ev.exitCode ?? 0}]\n`, false);
|
|
149
|
+
}
|
|
150
|
+
renderTerminalTabs();
|
|
151
|
+
}
|
|
152
|
+
} catch {
|
|
153
|
+
// Ignore malformed line
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
nl = buf.indexOf('\n');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch {
|
|
160
|
+
// Aborted cleanly
|
|
161
|
+
} finally {
|
|
162
|
+
if (streamAbort === ctrl) {
|
|
163
|
+
streamAbort = null;
|
|
164
|
+
streamedSessionId = null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function selectTerminalSession(sessionId: string): void {
|
|
171
|
+
state.activeTerminalId = sessionId;
|
|
172
|
+
renderTerminalTabs();
|
|
173
|
+
startTerminalStream(sessionId);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function loadTerminalSessions(): Promise<void> {
|
|
177
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), { headers: authHeaders() }).catch(() => null);
|
|
178
|
+
if (!res || !res.ok) return;
|
|
179
|
+
const data = await res.json() as {
|
|
180
|
+
sessions?: TerminalSessionPayload[];
|
|
181
|
+
shells?: AvailableShellPayload[];
|
|
182
|
+
};
|
|
183
|
+
if (Array.isArray(data.shells)) {
|
|
184
|
+
state.availableShells = data.shells;
|
|
185
|
+
hydrateStartShellOptions(data.shells);
|
|
186
|
+
}
|
|
187
|
+
state.terminalSessions = Array.isArray(data.sessions) ? data.sessions : [];
|
|
188
|
+
if (state.activeTerminalId && !state.terminalSessions.some((s) => s.id === state.activeTerminalId)) {
|
|
189
|
+
state.activeTerminalId = null;
|
|
190
|
+
stopTerminalStream();
|
|
191
|
+
}
|
|
192
|
+
if (!state.activeTerminalId && state.terminalSessions.length > 0) {
|
|
193
|
+
state.activeTerminalId = state.terminalSessions[0]!.id;
|
|
194
|
+
}
|
|
195
|
+
renderTerminalTabs();
|
|
196
|
+
if (state.activeTerminalId) {
|
|
197
|
+
startTerminalStream(state.activeTerminalId);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export async function openNewTerminalSession(shell?: string): Promise<void> {
|
|
202
|
+
const chosenShell = shell || dom.termShellSelect?.value || dom.startShellSelect?.value || undefined;
|
|
203
|
+
const res = await fetch(apiUrl('/api/terminal/sessions'), {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
206
|
+
body: JSON.stringify({ shell: chosenShell, cols: 100, rows: 28 })
|
|
207
|
+
}).catch(() => null);
|
|
208
|
+
if (!res) { announceAlert('Failed to open workspace terminal.'); return; }
|
|
209
|
+
const data = await res.json().catch(() => ({ error: `HTTP ${res.status}` })) as {
|
|
210
|
+
ok?: boolean;
|
|
211
|
+
session?: TerminalSessionPayload;
|
|
212
|
+
error?: string;
|
|
213
|
+
};
|
|
214
|
+
if (!res.ok || !data.session) {
|
|
215
|
+
announceAlert(data.error || 'Failed to open workspace terminal.');
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
state.terminalSessions.push(data.session);
|
|
219
|
+
selectTerminalSession(data.session.id);
|
|
220
|
+
dom.termInput?.focus();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export async function closeTerminalSession(sessionId: string): Promise<void> {
|
|
224
|
+
await fetch(apiUrl(`/api/terminal/sessions/${encodeURIComponent(sessionId)}`), {
|
|
225
|
+
method: 'DELETE',
|
|
226
|
+
headers: authHeaders()
|
|
227
|
+
}).catch(() => null);
|
|
228
|
+
state.terminalSessions = state.terminalSessions.filter((s) => s.id !== sessionId);
|
|
229
|
+
if (state.activeTerminalId === sessionId) {
|
|
230
|
+
stopTerminalStream();
|
|
231
|
+
state.activeTerminalId = state.terminalSessions[0]?.id ?? null;
|
|
232
|
+
if (dom.termViewport && !state.activeTerminalId) dom.termViewport.textContent = '';
|
|
233
|
+
}
|
|
234
|
+
renderTerminalTabs();
|
|
235
|
+
if (state.activeTerminalId) startTerminalStream(state.activeTerminalId);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function sendTerminalData(data: string, typed = true): Promise<void> {
|
|
239
|
+
if (!state.activeTerminalId) {
|
|
240
|
+
await openNewTerminalSession();
|
|
241
|
+
}
|
|
242
|
+
const id = state.activeTerminalId;
|
|
243
|
+
if (!id) return;
|
|
244
|
+
const res = await fetch(apiUrl('/api/terminal/input'), {
|
|
245
|
+
method: 'POST',
|
|
246
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
247
|
+
body: JSON.stringify({ id, data, typed })
|
|
248
|
+
}).catch(() => null);
|
|
249
|
+
if (res && res.ok) {
|
|
250
|
+
const body = await res.json().catch(() => ({})) as { control?: 'user' | 'agent' };
|
|
251
|
+
const cur = activeSession();
|
|
252
|
+
if (cur && body.control) {
|
|
253
|
+
cur.control = body.control;
|
|
254
|
+
syncTerminalChrome();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function sendTerminalSignal(signal: 'SIGINT' | 'SIGTERM' | 'SIGTSTP'): Promise<void> {
|
|
260
|
+
const id = state.activeTerminalId;
|
|
261
|
+
if (!id) return;
|
|
262
|
+
await fetch(apiUrl('/api/terminal/signal'), {
|
|
263
|
+
method: 'POST',
|
|
264
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
265
|
+
body: JSON.stringify({ id, signal })
|
|
266
|
+
}).catch(() => null);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function deactivateTerminalPanel(): void {
|
|
270
|
+
stopTerminalStream();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function bindTerminalPanelEvents(): void {
|
|
274
|
+
dom.termNewBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
275
|
+
dom.termEmptyStartBtn?.addEventListener('click', () => { void openNewTerminalSession(); });
|
|
276
|
+
dom.termSigintBtn?.addEventListener('click', () => { void sendTerminalSignal('SIGINT'); });
|
|
277
|
+
dom.termClearBtn?.addEventListener('click', () => {
|
|
278
|
+
if (dom.termViewport) dom.termViewport.textContent = '';
|
|
279
|
+
});
|
|
280
|
+
dom.termTakeControlBtn?.addEventListener('click', () => {
|
|
281
|
+
void sendTerminalData('', true);
|
|
282
|
+
dom.termInput?.focus();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const submitCommandLine = (): void => {
|
|
286
|
+
if (!dom.termInput) return;
|
|
287
|
+
const line = dom.termInput.value;
|
|
288
|
+
if (line.trim()) {
|
|
289
|
+
cmdHistory.push(line);
|
|
290
|
+
if (cmdHistory.length > 100) cmdHistory.shift();
|
|
291
|
+
historyIdx = cmdHistory.length;
|
|
292
|
+
}
|
|
293
|
+
dom.termInput.value = '';
|
|
294
|
+
void sendTerminalData(`${line}\n`, true);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
dom.termSendBtn?.addEventListener('click', submitCommandLine);
|
|
298
|
+
dom.termInput?.addEventListener('keydown', (e) => {
|
|
299
|
+
if (e.key === 'Enter') {
|
|
300
|
+
e.preventDefault();
|
|
301
|
+
submitCommandLine();
|
|
302
|
+
} else if (e.ctrlKey && e.key.toLowerCase() === 'c') {
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
void sendTerminalSignal('SIGINT');
|
|
305
|
+
} else if (e.key === 'ArrowUp' && cmdHistory.length > 0) {
|
|
306
|
+
e.preventDefault();
|
|
307
|
+
historyIdx = Math.max(0, (historyIdx < 0 ? cmdHistory.length : historyIdx) - 1);
|
|
308
|
+
if (dom.termInput) dom.termInput.value = cmdHistory[historyIdx] ?? '';
|
|
309
|
+
} else if (e.key === 'ArrowDown' && cmdHistory.length > 0) {
|
|
310
|
+
e.preventDefault();
|
|
311
|
+
historyIdx = Math.min(cmdHistory.length, historyIdx + 1);
|
|
312
|
+
if (dom.termInput) dom.termInput.value = cmdHistory[historyIdx] ?? '';
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// Direct interactive keyboard forwarding when viewport itself is focused
|
|
317
|
+
dom.termViewport?.addEventListener('keydown', (e) => {
|
|
318
|
+
if (!state.activeTerminalId) return;
|
|
319
|
+
if (e.metaKey) return;
|
|
320
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'c') {
|
|
321
|
+
e.preventDefault();
|
|
322
|
+
void sendTerminalSignal('SIGINT');
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'd') {
|
|
326
|
+
e.preventDefault();
|
|
327
|
+
void sendTerminalData('\x04', true);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (e.ctrlKey && e.key.toLowerCase() === 'l') {
|
|
331
|
+
e.preventDefault();
|
|
332
|
+
if (dom.termViewport) dom.termViewport.textContent = '';
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (e.key === 'Enter') { e.preventDefault(); void sendTerminalData('\n', true); return; }
|
|
336
|
+
if (e.key === 'Backspace') { e.preventDefault(); void sendTerminalData('\x7f', true); return; }
|
|
337
|
+
if (e.key === 'Tab') { e.preventDefault(); void sendTerminalData('\t', true); return; }
|
|
338
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.altKey) {
|
|
339
|
+
e.preventDefault();
|
|
340
|
+
void sendTerminalData(e.key, true);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
window.addEventListener('beforeunload', stopTerminalStream);
|
|
345
|
+
}
|