@myrialabs/clopen 0.4.6 → 0.4.8
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/backend/auth/session-invalidation.test.ts +17 -21
- package/backend/auth/session-invalidation.ts +10 -11
- package/backend/db-client/drivers/mysql.ts +21 -0
- package/backend/db-client/drivers/postgres.ts +20 -0
- package/backend/db-client/drivers/sqlite.ts +21 -0
- package/backend/db-client/drivers/types.ts +19 -0
- package/backend/db-client/query-executor.ts +89 -1
- package/backend/engine/adapters/claude/models.ts +46 -3
- package/backend/files/file-archive.test.ts +86 -26
- package/backend/files/file-archive.ts +214 -134
- package/backend/terminal/index.ts +2 -5
- package/backend/terminal/ptykit.ts +64 -0
- package/backend/tunnel/tunnel-config.ts +1 -1
- package/backend/utils/ws.ts +14 -0
- package/backend/ws/db-client/query-history-retention.test.ts +217 -0
- package/backend/ws/db-client/query.ts +77 -2
- package/backend/ws/files/write.ts +23 -7
- package/backend/ws/git/ignored.ts +65 -0
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/projects/crud.ts +4 -4
- package/backend/ws/system/operations.ts +23 -0
- package/backend/ws/terminal/index.ts +6 -33
- package/backend/ws/terminal/tunnel.ts +76 -0
- package/dist/assets/__vite-browser-external-BIHI7g3E.js +1 -0
- package/dist/assets/index-B_F_OgoC.js +1 -0
- package/dist/assets/index-jGo0X72U.js +1 -0
- package/dist/assets/{main-TYNK6Orl.js → main-BkCc3WEB.js} +321 -260
- package/dist/assets/main-CWDwET4x.css +1 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm +0 -0
- package/dist/index.html +2 -2
- package/package.json +7 -11
- package/shared/constants/engines.ts +4 -4
- package/shared/types/db-client/query.ts +33 -0
- package/shared/utils/db-client/split-sql.test.ts +88 -0
- package/shared/utils/db-client/split-sql.ts +132 -0
- package/backend/terminal/helpers.ts +0 -14
- package/backend/terminal/pty-manager.ts +0 -4
- package/backend/terminal/pty-session-manager.ts +0 -349
- package/backend/terminal/stream-manager.ts +0 -300
- package/backend/ws/terminal/access.ts +0 -56
- package/backend/ws/terminal/persistence.ts +0 -145
- package/backend/ws/terminal/session.ts +0 -470
- package/backend/ws/terminal/stream.ts +0 -82
- package/dist/assets/main-BkMrek4_.css +0 -1
|
@@ -1,470 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Terminal Session Management
|
|
3
|
-
*
|
|
4
|
-
* HTTP endpoints for terminal session lifecycle:
|
|
5
|
-
* - Create session
|
|
6
|
-
* - Resize viewport
|
|
7
|
-
* - Cancel (Ctrl+C)
|
|
8
|
-
* - Kill session
|
|
9
|
-
* - Check shell availability
|
|
10
|
-
* - Get PTY status
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { t } from 'elysia';
|
|
14
|
-
import { createRouter } from '$shared/utils/ws-server';
|
|
15
|
-
import { ptySessionManager } from '../../terminal/pty-session-manager';
|
|
16
|
-
import { terminalStreamManager } from '../../terminal/stream-manager';
|
|
17
|
-
import { debug } from '$shared/utils/logger';
|
|
18
|
-
import { resolve } from 'path';
|
|
19
|
-
import { isWindows } from '../../terminal/shell-utils';
|
|
20
|
-
import { existsSync } from '../../terminal/helpers';
|
|
21
|
-
import { ws } from '$backend/utils/ws';
|
|
22
|
-
import { activePtyProcesses } from '../../terminal/pty-manager';
|
|
23
|
-
import { requireProjectAccess, requireCurrentProjectAccess } from '../access';
|
|
24
|
-
import { requirePtySessionAccess } from './access';
|
|
25
|
-
|
|
26
|
-
export const sessionHandler = createRouter()
|
|
27
|
-
// Create new terminal session
|
|
28
|
-
.http('terminal:create-session', {
|
|
29
|
-
data: t.Object({
|
|
30
|
-
sessionId: t.String(),
|
|
31
|
-
streamId: t.Optional(t.String()),
|
|
32
|
-
workingDirectory: t.Optional(t.String()),
|
|
33
|
-
projectPath: t.Optional(t.String()),
|
|
34
|
-
cols: t.Optional(t.Number()),
|
|
35
|
-
rows: t.Optional(t.Number())
|
|
36
|
-
}),
|
|
37
|
-
response: t.Object({
|
|
38
|
-
sessionId: t.String(),
|
|
39
|
-
streamId: t.String(),
|
|
40
|
-
pid: t.Number(),
|
|
41
|
-
currentDirectory: t.String(),
|
|
42
|
-
cols: t.Number(),
|
|
43
|
-
rows: t.Number()
|
|
44
|
-
})
|
|
45
|
-
}, async ({ data, conn }) => {
|
|
46
|
-
const {
|
|
47
|
-
sessionId,
|
|
48
|
-
streamId,
|
|
49
|
-
workingDirectory,
|
|
50
|
-
projectPath,
|
|
51
|
-
cols = 80,
|
|
52
|
-
rows = 24
|
|
53
|
-
} = data;
|
|
54
|
-
|
|
55
|
-
const { projectId } = requireCurrentProjectAccess(conn);
|
|
56
|
-
|
|
57
|
-
// If a PTY session already exists for this id, it must belong to the
|
|
58
|
-
// caller's current project. Prevents hijacking another project's PTY id.
|
|
59
|
-
const existingForOwnership = ptySessionManager.getSession(sessionId);
|
|
60
|
-
if (existingForOwnership && existingForOwnership.projectId && existingForOwnership.projectId !== projectId) {
|
|
61
|
-
throw new Error('Access denied');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
debug.log('terminal', `🌐 WebSocket: Creating PTY session: ${sessionId}`);
|
|
65
|
-
|
|
66
|
-
// CRITICAL: Clear ALL existing listeners FIRST before any other operation
|
|
67
|
-
// This prevents duplicate output when reconnecting to a running PTY session
|
|
68
|
-
// (e.g., when switching between projects and coming back)
|
|
69
|
-
const existingPtySession = ptySessionManager.getSession(sessionId);
|
|
70
|
-
if (existingPtySession) {
|
|
71
|
-
const dataListenerCount = existingPtySession.dataListeners.size;
|
|
72
|
-
const exitListenerCount = existingPtySession.exitListeners.size;
|
|
73
|
-
|
|
74
|
-
if (dataListenerCount > 0 || exitListenerCount > 0) {
|
|
75
|
-
debug.log('terminal', `🧹 EARLY CLEANUP: Clearing ${dataListenerCount} data listeners and ${exitListenerCount} exit listeners from PTY session: ${sessionId}`);
|
|
76
|
-
existingPtySession.dataListeners.clear();
|
|
77
|
-
existingPtySession.exitListeners.clear();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Validate and set working directory
|
|
82
|
-
let cwd = process.cwd();
|
|
83
|
-
let currentDirectory = workingDirectory || '';
|
|
84
|
-
|
|
85
|
-
// Priority: workingDirectory > projectPath > home
|
|
86
|
-
if (workingDirectory && workingDirectory !== '~') {
|
|
87
|
-
let resolvedPath: string;
|
|
88
|
-
if (workingDirectory.startsWith('~')) {
|
|
89
|
-
const homeDir = isWindows ? process.env.USERPROFILE : process.env.HOME;
|
|
90
|
-
resolvedPath = workingDirectory.replace('~', homeDir || process.cwd());
|
|
91
|
-
} else {
|
|
92
|
-
resolvedPath = resolve(workingDirectory);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (await existsSync(resolvedPath)) {
|
|
96
|
-
cwd = resolvedPath;
|
|
97
|
-
currentDirectory = resolvedPath;
|
|
98
|
-
}
|
|
99
|
-
} else if (projectPath && await existsSync(projectPath)) {
|
|
100
|
-
cwd = projectPath;
|
|
101
|
-
currentDirectory = projectPath;
|
|
102
|
-
} else {
|
|
103
|
-
// Use home directory as fallback
|
|
104
|
-
const homeDir = process.platform === 'win32'
|
|
105
|
-
? process.env.USERPROFILE
|
|
106
|
-
: process.env.HOME;
|
|
107
|
-
cwd = homeDir || process.cwd();
|
|
108
|
-
currentDirectory = cwd;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Create or get existing persistent PTY session
|
|
112
|
-
const ptySession = await ptySessionManager.createSession(
|
|
113
|
-
sessionId,
|
|
114
|
-
cwd,
|
|
115
|
-
ws.getUserId(conn),
|
|
116
|
-
projectId || '',
|
|
117
|
-
{ cols, rows }
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
debug.log('terminal', `✅ PTY session ready with PID: ${ptySession.pty.pid}`);
|
|
121
|
-
|
|
122
|
-
// Register with terminal stream manager for persistence
|
|
123
|
-
const registeredStreamId = terminalStreamManager.createStream(
|
|
124
|
-
sessionId,
|
|
125
|
-
'interactive-shell', // No specific command - it's an interactive shell
|
|
126
|
-
ptySession.pty,
|
|
127
|
-
currentDirectory,
|
|
128
|
-
projectPath || '',
|
|
129
|
-
projectId || '',
|
|
130
|
-
streamId,
|
|
131
|
-
{ cols, rows }
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
// Broadcast initial ready event (frontend filters by sessionId)
|
|
135
|
-
ws.emit.project(projectId, 'terminal:ready', {
|
|
136
|
-
sessionId,
|
|
137
|
-
streamId: registeredStreamId,
|
|
138
|
-
pid: ptySession.pty.pid,
|
|
139
|
-
cols,
|
|
140
|
-
rows
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// Broadcast initial directory info (frontend filters by sessionId)
|
|
144
|
-
ws.emit.project(projectId, 'terminal:directory', {
|
|
145
|
-
sessionId,
|
|
146
|
-
newDirectory: currentDirectory
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// Setup data listener for PTY output - broadcasts to project room
|
|
150
|
-
// Connection-independent: works regardless of which connection set it up
|
|
151
|
-
// All users in the project see terminal output (collaborative)
|
|
152
|
-
// Frontend filters by sessionId to display in the correct terminal tab
|
|
153
|
-
const dataListener = (output: string) => {
|
|
154
|
-
const currentSeq = ptySessionManager.getSession(sessionId)?.outputSeq || 0;
|
|
155
|
-
ws.emit.project(projectId, 'terminal:output', {
|
|
156
|
-
sessionId,
|
|
157
|
-
content: output,
|
|
158
|
-
seq: currentSeq,
|
|
159
|
-
projectId,
|
|
160
|
-
timestamp: new Date().toISOString()
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
// Setup exit listener - broadcasts to project room
|
|
165
|
-
const exitListener = (event: { exitCode: number; signal?: number | string }) => {
|
|
166
|
-
debug.log('terminal', `🏁 PTY session ${sessionId} exited with code: ${event.exitCode}`);
|
|
167
|
-
|
|
168
|
-
// Update stream status
|
|
169
|
-
const currentStreamId = terminalStreamManager.getStreamBySession(sessionId)?.streamId;
|
|
170
|
-
if (currentStreamId) {
|
|
171
|
-
terminalStreamManager.updateStatus(
|
|
172
|
-
currentStreamId,
|
|
173
|
-
event.exitCode === 0 ? 'completed' : 'error'
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Broadcast exit event to project room
|
|
178
|
-
ws.emit.project(projectId, 'terminal:exit', {
|
|
179
|
-
sessionId,
|
|
180
|
-
exitCode: event.exitCode
|
|
181
|
-
});
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
// Add NEW listeners to PTY session (now guaranteed to be the only listeners)
|
|
185
|
-
ptySessionManager.addDataListener(sessionId, dataListener);
|
|
186
|
-
ptySessionManager.addExitListener(sessionId, exitListener);
|
|
187
|
-
|
|
188
|
-
debug.log('terminal', `✅ Added fresh listeners to PTY session ${sessionId}`);
|
|
189
|
-
|
|
190
|
-
// Replay serialized terminal state for reconnection (e.g., after browser refresh)
|
|
191
|
-
// The headless xterm preserves full terminal state including clear/scrollback
|
|
192
|
-
const serializedOutput = terminalStreamManager.getSerializedOutput(registeredStreamId);
|
|
193
|
-
if (serializedOutput) {
|
|
194
|
-
debug.log('terminal', `📜 Replaying serialized terminal state for session ${sessionId}`);
|
|
195
|
-
ws.emit.project(projectId, 'terminal:output', {
|
|
196
|
-
sessionId,
|
|
197
|
-
content: serializedOutput,
|
|
198
|
-
projectId,
|
|
199
|
-
timestamp: new Date().toISOString()
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Broadcast terminal tab created to all project users
|
|
204
|
-
ws.emit.project(projectId, 'terminal:tab-created', {
|
|
205
|
-
sessionId,
|
|
206
|
-
streamId: registeredStreamId,
|
|
207
|
-
pid: ptySession.pty.pid,
|
|
208
|
-
currentDirectory,
|
|
209
|
-
cols,
|
|
210
|
-
rows
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
// Return session info
|
|
214
|
-
return {
|
|
215
|
-
sessionId,
|
|
216
|
-
streamId: registeredStreamId,
|
|
217
|
-
pid: ptySession.pty.pid,
|
|
218
|
-
currentDirectory,
|
|
219
|
-
cols,
|
|
220
|
-
rows
|
|
221
|
-
};
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
// Clear headless terminal buffer (sync with frontend clear)
|
|
225
|
-
.http('terminal:clear', {
|
|
226
|
-
data: t.Object({
|
|
227
|
-
sessionId: t.String()
|
|
228
|
-
}),
|
|
229
|
-
response: t.Object({
|
|
230
|
-
sessionId: t.String()
|
|
231
|
-
})
|
|
232
|
-
}, async ({ data, conn }) => {
|
|
233
|
-
const { sessionId } = data;
|
|
234
|
-
requirePtySessionAccess(conn, sessionId);
|
|
235
|
-
terminalStreamManager.clearHeadlessTerminal(sessionId);
|
|
236
|
-
return { sessionId };
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
// Resize terminal viewport
|
|
240
|
-
.http('terminal:resize', {
|
|
241
|
-
data: t.Object({
|
|
242
|
-
sessionId: t.String(),
|
|
243
|
-
cols: t.Number(),
|
|
244
|
-
rows: t.Number()
|
|
245
|
-
}),
|
|
246
|
-
response: t.Object({
|
|
247
|
-
sessionId: t.String(),
|
|
248
|
-
cols: t.Number(),
|
|
249
|
-
rows: t.Number()
|
|
250
|
-
})
|
|
251
|
-
}, async ({ data, conn }) => {
|
|
252
|
-
const { sessionId, cols, rows } = data;
|
|
253
|
-
|
|
254
|
-
requirePtySessionAccess(conn, sessionId);
|
|
255
|
-
|
|
256
|
-
debug.log('terminal', `🔧 Resizing PTY session ${sessionId} to ${cols}x${rows}`);
|
|
257
|
-
|
|
258
|
-
const success = ptySessionManager.resize(sessionId, cols, rows);
|
|
259
|
-
|
|
260
|
-
if (!success) {
|
|
261
|
-
throw new Error('No active PTY session found');
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Keep headless terminal in sync with PTY dimensions
|
|
265
|
-
terminalStreamManager.resizeHeadlessTerminal(sessionId, cols, rows);
|
|
266
|
-
|
|
267
|
-
return { sessionId, cols, rows };
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
// Send Ctrl+C interrupt signal
|
|
271
|
-
.http('terminal:cancel', {
|
|
272
|
-
data: t.Object({
|
|
273
|
-
sessionId: t.String()
|
|
274
|
-
}),
|
|
275
|
-
response: t.Object({
|
|
276
|
-
sessionId: t.String(),
|
|
277
|
-
pid: t.Number()
|
|
278
|
-
})
|
|
279
|
-
}, async ({ data, conn }) => {
|
|
280
|
-
const { sessionId } = data;
|
|
281
|
-
|
|
282
|
-
const session = requirePtySessionAccess(conn, sessionId);
|
|
283
|
-
|
|
284
|
-
debug.log('terminal', `🛑 Sending Ctrl+C signal to PTY session: ${sessionId}`);
|
|
285
|
-
|
|
286
|
-
const pid = session.pty.pid;
|
|
287
|
-
|
|
288
|
-
// Write Ctrl+C character (\x03) to PTY
|
|
289
|
-
session.pty.write('\x03');
|
|
290
|
-
|
|
291
|
-
debug.log('terminal', `✅ Sent Ctrl+C signal to PTY session ${sessionId} (PID: ${pid})`);
|
|
292
|
-
|
|
293
|
-
return {
|
|
294
|
-
sessionId,
|
|
295
|
-
pid
|
|
296
|
-
};
|
|
297
|
-
})
|
|
298
|
-
|
|
299
|
-
// Kill terminal session
|
|
300
|
-
.http('terminal:kill-session', {
|
|
301
|
-
data: t.Object({
|
|
302
|
-
sessionId: t.String()
|
|
303
|
-
}),
|
|
304
|
-
response: t.Object({
|
|
305
|
-
sessionId: t.String(),
|
|
306
|
-
pid: t.Optional(t.Number())
|
|
307
|
-
})
|
|
308
|
-
}, async ({ data, conn }) => {
|
|
309
|
-
const { sessionId } = data;
|
|
310
|
-
|
|
311
|
-
const session = ptySessionManager.getSession(sessionId);
|
|
312
|
-
|
|
313
|
-
if (!session) {
|
|
314
|
-
debug.log('terminal', `💀 [kill-session] No active PTY session found for: ${sessionId}`);
|
|
315
|
-
return { sessionId };
|
|
316
|
-
}
|
|
317
|
-
if (!session.projectId) throw new Error('Access denied');
|
|
318
|
-
requireProjectAccess(conn, session.projectId);
|
|
319
|
-
|
|
320
|
-
debug.log('terminal', `💀 [kill-session] Killing PTY session: ${sessionId}`);
|
|
321
|
-
|
|
322
|
-
const pid = session.pty?.pid;
|
|
323
|
-
debug.log('terminal', `💀 [kill-session] Found PTY session with PID: ${pid}`);
|
|
324
|
-
|
|
325
|
-
// Kill the PTY session (handles listener cleanup internally)
|
|
326
|
-
const killed = ptySessionManager.killSession(sessionId);
|
|
327
|
-
|
|
328
|
-
if (!killed) {
|
|
329
|
-
debug.error('terminal', `💀 [kill-session] Failed to kill PTY session: ${sessionId}`);
|
|
330
|
-
throw new Error('Failed to kill PTY session');
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
debug.log('terminal', `💀 [kill-session] Successfully killed PTY session: ${sessionId} (PID: ${pid})`);
|
|
334
|
-
|
|
335
|
-
// Clean up stream and headless terminal
|
|
336
|
-
const stream = terminalStreamManager.getStreamBySession(sessionId);
|
|
337
|
-
if (stream) {
|
|
338
|
-
terminalStreamManager.removeStream(stream.streamId);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// Broadcast terminal tab closed to all project users
|
|
342
|
-
const projectId = ws.getProjectId(conn);
|
|
343
|
-
ws.emit.project(projectId, 'terminal:tab-closed', {
|
|
344
|
-
sessionId
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
return {
|
|
348
|
-
sessionId,
|
|
349
|
-
pid
|
|
350
|
-
};
|
|
351
|
-
})
|
|
352
|
-
|
|
353
|
-
// Check shell availability
|
|
354
|
-
.http('terminal:check-shell', {
|
|
355
|
-
data: t.Object({}),
|
|
356
|
-
response: t.Object({
|
|
357
|
-
available: t.Boolean(),
|
|
358
|
-
path: t.Union([t.String(), t.Null()]),
|
|
359
|
-
platform: t.String(),
|
|
360
|
-
isWindows: t.Boolean(),
|
|
361
|
-
shellType: t.String()
|
|
362
|
-
})
|
|
363
|
-
}, async () => {
|
|
364
|
-
const platformIsWindows = process.platform === 'win32';
|
|
365
|
-
|
|
366
|
-
try {
|
|
367
|
-
let shellType = 'Shell';
|
|
368
|
-
let shellPath = null;
|
|
369
|
-
|
|
370
|
-
if (platformIsWindows) {
|
|
371
|
-
shellType = 'PowerShell';
|
|
372
|
-
shellPath = 'powershell.exe';
|
|
373
|
-
} else {
|
|
374
|
-
shellType = 'Bash';
|
|
375
|
-
shellPath = '/bin/bash';
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
return {
|
|
379
|
-
available: true,
|
|
380
|
-
path: shellPath,
|
|
381
|
-
platform: process.platform,
|
|
382
|
-
isWindows: platformIsWindows,
|
|
383
|
-
shellType
|
|
384
|
-
};
|
|
385
|
-
} catch (error) {
|
|
386
|
-
return {
|
|
387
|
-
available: true,
|
|
388
|
-
path: platformIsWindows ? 'cmd.exe' : '/bin/sh',
|
|
389
|
-
platform: process.platform,
|
|
390
|
-
isWindows: platformIsWindows,
|
|
391
|
-
shellType: platformIsWindows ? 'CMD' : 'Shell'
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
})
|
|
395
|
-
|
|
396
|
-
// Get PTY status
|
|
397
|
-
.http('terminal:pty-status', {
|
|
398
|
-
data: t.Object({
|
|
399
|
-
sessionId: t.String()
|
|
400
|
-
}),
|
|
401
|
-
response: t.Object({
|
|
402
|
-
isActive: t.Boolean(),
|
|
403
|
-
sessionId: t.String(),
|
|
404
|
-
pid: t.Optional(t.Number()),
|
|
405
|
-
message: t.Optional(t.String())
|
|
406
|
-
})
|
|
407
|
-
}, async ({ data, conn }) => {
|
|
408
|
-
const { sessionId } = data;
|
|
409
|
-
|
|
410
|
-
const ptySession = ptySessionManager.getSession(sessionId);
|
|
411
|
-
if (!ptySession || !ptySession.projectId) {
|
|
412
|
-
return {
|
|
413
|
-
isActive: false,
|
|
414
|
-
sessionId,
|
|
415
|
-
message: 'PTY not found'
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
requireProjectAccess(conn, ptySession.projectId);
|
|
419
|
-
|
|
420
|
-
const pty = activePtyProcesses.get(sessionId);
|
|
421
|
-
|
|
422
|
-
if (pty) {
|
|
423
|
-
const isActive = pty.pid > 0;
|
|
424
|
-
|
|
425
|
-
return {
|
|
426
|
-
isActive,
|
|
427
|
-
sessionId,
|
|
428
|
-
pid: pty.pid
|
|
429
|
-
};
|
|
430
|
-
} else {
|
|
431
|
-
return {
|
|
432
|
-
isActive: false,
|
|
433
|
-
sessionId,
|
|
434
|
-
message: 'PTY not found'
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
})
|
|
438
|
-
|
|
439
|
-
// List active PTY sessions for a project
|
|
440
|
-
// Used after browser refresh to discover existing sessions
|
|
441
|
-
.http('terminal:list-sessions', {
|
|
442
|
-
data: t.Object({
|
|
443
|
-
projectId: t.String()
|
|
444
|
-
}),
|
|
445
|
-
response: t.Object({
|
|
446
|
-
sessions: t.Array(t.Object({
|
|
447
|
-
sessionId: t.String(),
|
|
448
|
-
pid: t.Number(),
|
|
449
|
-
cwd: t.String(),
|
|
450
|
-
createdAt: t.String(),
|
|
451
|
-
lastActivityAt: t.String()
|
|
452
|
-
}))
|
|
453
|
-
})
|
|
454
|
-
}, async ({ data, conn }) => {
|
|
455
|
-
const { projectId } = data;
|
|
456
|
-
requireProjectAccess(conn, projectId);
|
|
457
|
-
|
|
458
|
-
const allSessions = ptySessionManager.getAllSessions();
|
|
459
|
-
const projectSessions = allSessions
|
|
460
|
-
.filter(session => session.projectId === projectId)
|
|
461
|
-
.map(session => ({
|
|
462
|
-
sessionId: session.sessionId,
|
|
463
|
-
pid: session.pty.pid,
|
|
464
|
-
cwd: session.cwd,
|
|
465
|
-
createdAt: session.createdAt.toISOString(),
|
|
466
|
-
lastActivityAt: session.lastActivityAt.toISOString()
|
|
467
|
-
}));
|
|
468
|
-
|
|
469
|
-
return { sessions: projectSessions };
|
|
470
|
-
});
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Terminal Stream Events
|
|
3
|
-
*
|
|
4
|
-
* Handles real-time terminal I/O events:
|
|
5
|
-
* - Input (keyboard events)
|
|
6
|
-
* - Output (PTY stdout)
|
|
7
|
-
* - Exit events
|
|
8
|
-
* - Directory changes
|
|
9
|
-
* - Ready notifications
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { t } from 'elysia';
|
|
13
|
-
import { createRouter } from '$shared/utils/ws-server';
|
|
14
|
-
import { ptySessionManager } from '../../terminal/pty-session-manager';
|
|
15
|
-
import { debug } from '$shared/utils/logger';
|
|
16
|
-
import { ws } from '$backend/utils/ws';
|
|
17
|
-
import { requirePtySessionAccess } from './access';
|
|
18
|
-
|
|
19
|
-
export const streamHandler = createRouter()
|
|
20
|
-
// Send keyboard input to terminal
|
|
21
|
-
.on('terminal:input', {
|
|
22
|
-
data: t.Object({
|
|
23
|
-
sessionId: t.String(),
|
|
24
|
-
data: t.Any()
|
|
25
|
-
})
|
|
26
|
-
}, async ({ data, conn }) => {
|
|
27
|
-
const { sessionId, data: input } = data;
|
|
28
|
-
|
|
29
|
-
const ptySession = requirePtySessionAccess(conn, sessionId);
|
|
30
|
-
const projectId = ptySession.projectId!;
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
// Write to PTY stdin
|
|
34
|
-
const success = ptySessionManager.write(sessionId, input);
|
|
35
|
-
|
|
36
|
-
if (!success) {
|
|
37
|
-
// Broadcast error (frontend filters by sessionId)
|
|
38
|
-
ws.emit.project(projectId, 'terminal:error', {
|
|
39
|
-
sessionId,
|
|
40
|
-
error: 'Session not found or PTY not available'
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
} catch (error) {
|
|
44
|
-
debug.error('terminal', 'Error sending input:', error);
|
|
45
|
-
ws.emit.project(projectId, 'terminal:error', {
|
|
46
|
-
sessionId,
|
|
47
|
-
error: error instanceof Error ? error.message : 'Unknown error'
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
// Event declarations - Server → Client
|
|
53
|
-
.emit('terminal:ready', t.Object({
|
|
54
|
-
sessionId: t.String(),
|
|
55
|
-
streamId: t.String(),
|
|
56
|
-
pid: t.Number(),
|
|
57
|
-
cols: t.Number(),
|
|
58
|
-
rows: t.Number()
|
|
59
|
-
}))
|
|
60
|
-
|
|
61
|
-
.emit('terminal:output', t.Object({
|
|
62
|
-
sessionId: t.String(),
|
|
63
|
-
content: t.String(),
|
|
64
|
-
seq: t.Optional(t.Number()),
|
|
65
|
-
projectId: t.Optional(t.String()),
|
|
66
|
-
timestamp: t.String()
|
|
67
|
-
}))
|
|
68
|
-
|
|
69
|
-
.emit('terminal:directory', t.Object({
|
|
70
|
-
sessionId: t.String(),
|
|
71
|
-
newDirectory: t.String()
|
|
72
|
-
}))
|
|
73
|
-
|
|
74
|
-
.emit('terminal:exit', t.Object({
|
|
75
|
-
sessionId: t.String(),
|
|
76
|
-
exitCode: t.Number()
|
|
77
|
-
}))
|
|
78
|
-
|
|
79
|
-
.emit('terminal:error', t.Object({
|
|
80
|
-
sessionId: t.String(),
|
|
81
|
-
error: t.String()
|
|
82
|
-
}));
|