@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
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { splitSqlStatements } from './split-sql';
|
|
3
|
+
|
|
4
|
+
describe('splitSqlStatements', () => {
|
|
5
|
+
test('returns a single statement unchanged (no trailing semicolon)', () => {
|
|
6
|
+
expect(splitSqlStatements('SELECT 1')).toEqual(['SELECT 1']);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('trims and drops empty statements', () => {
|
|
10
|
+
expect(splitSqlStatements(' SELECT 1 ; ; SELECT 2 ;')).toEqual(['SELECT 1', 'SELECT 2']);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('splits multiple statements', () => {
|
|
14
|
+
expect(splitSqlStatements('SELECT 1; DELETE FROM users')).toEqual([
|
|
15
|
+
'SELECT 1',
|
|
16
|
+
'DELETE FROM users'
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('ignores semicolons inside single-quoted strings', () => {
|
|
21
|
+
expect(splitSqlStatements("SELECT ';'; SELECT 'a;b'")).toEqual([
|
|
22
|
+
"SELECT ';'",
|
|
23
|
+
"SELECT 'a;b'"
|
|
24
|
+
]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('handles doubled single-quote escapes', () => {
|
|
28
|
+
expect(splitSqlStatements("SELECT 'O''Brien; Jr'; SELECT 2")).toEqual([
|
|
29
|
+
"SELECT 'O''Brien; Jr'",
|
|
30
|
+
'SELECT 2'
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('handles MySQL backslash escapes inside strings', () => {
|
|
35
|
+
expect(splitSqlStatements("SELECT 'a\\';b'; SELECT 2")).toEqual([
|
|
36
|
+
"SELECT 'a\\';b'",
|
|
37
|
+
'SELECT 2'
|
|
38
|
+
]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('ignores semicolons inside double-quoted identifiers', () => {
|
|
42
|
+
expect(splitSqlStatements('SELECT "a;b" FROM t; SELECT 2')).toEqual([
|
|
43
|
+
'SELECT "a;b" FROM t',
|
|
44
|
+
'SELECT 2'
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('ignores semicolons inside backtick identifiers', () => {
|
|
49
|
+
expect(splitSqlStatements('SELECT `a;b` FROM t; SELECT 2')).toEqual([
|
|
50
|
+
'SELECT `a;b` FROM t',
|
|
51
|
+
'SELECT 2'
|
|
52
|
+
]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('ignores semicolons inside line comments', () => {
|
|
56
|
+
expect(splitSqlStatements('SELECT 1 -- a; b\n; SELECT 2')).toEqual([
|
|
57
|
+
'SELECT 1 -- a; b',
|
|
58
|
+
'SELECT 2'
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('ignores semicolons inside block comments', () => {
|
|
63
|
+
expect(splitSqlStatements('SELECT 1 /* a; b */; SELECT 2')).toEqual([
|
|
64
|
+
'SELECT 1 /* a; b */',
|
|
65
|
+
'SELECT 2'
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('ignores semicolons inside Postgres dollar-quoted bodies', () => {
|
|
70
|
+
const sql = `CREATE FUNCTION f() RETURNS void AS $$ BEGIN PERFORM 1; PERFORM 2; END; $$ LANGUAGE plpgsql; SELECT 1`;
|
|
71
|
+
expect(splitSqlStatements(sql)).toEqual([
|
|
72
|
+
'CREATE FUNCTION f() RETURNS void AS $$ BEGIN PERFORM 1; PERFORM 2; END; $$ LANGUAGE plpgsql',
|
|
73
|
+
'SELECT 1'
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('handles tagged dollar quotes', () => {
|
|
78
|
+
const sql = `DO $body$ BEGIN RAISE NOTICE 'x;y'; END $body$; SELECT 1`;
|
|
79
|
+
expect(splitSqlStatements(sql)).toEqual([
|
|
80
|
+
"DO $body$ BEGIN RAISE NOTICE 'x;y'; END $body$",
|
|
81
|
+
'SELECT 1'
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('returns empty array for whitespace / comment only input', () => {
|
|
86
|
+
expect(splitSqlStatements(' ; ')).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* db-client — split a SQL string into individual statements on top-level `;`.
|
|
3
|
+
*
|
|
4
|
+
* The splitter is aware of the contexts where a `;` must NOT split a
|
|
5
|
+
* statement:
|
|
6
|
+
* - single/double-quoted strings, incl. doubled-quote (`''`, `""`) and
|
|
7
|
+
* backslash (`\'`, MySQL) escapes;
|
|
8
|
+
* - backtick-quoted identifiers (MySQL);
|
|
9
|
+
* - line (`-- …`) and block (`/* … *\/`) comments;
|
|
10
|
+
* - Postgres dollar-quoted bodies (`$$ … ; … $$`, `$tag$ … $tag$`), which
|
|
11
|
+
* routinely contain semicolons inside function / DO blocks.
|
|
12
|
+
*
|
|
13
|
+
* It is intentionally conservative: it never rewrites the SQL, only slices it
|
|
14
|
+
* on statement boundaries. Anything it can't confidently classify is kept in
|
|
15
|
+
* the current statement so the database — not this splitter — has the final say.
|
|
16
|
+
*/
|
|
17
|
+
export function splitSqlStatements(sql: string): string[] {
|
|
18
|
+
const statements: string[] = [];
|
|
19
|
+
let current = '';
|
|
20
|
+
let quote: "'" | '"' | '`' | null = null;
|
|
21
|
+
let inLineComment = false;
|
|
22
|
+
let inBlockComment = false;
|
|
23
|
+
let dollarTag: string | null = null;
|
|
24
|
+
|
|
25
|
+
let i = 0;
|
|
26
|
+
const n = sql.length;
|
|
27
|
+
while (i < n) {
|
|
28
|
+
const ch = sql[i];
|
|
29
|
+
const next = sql[i + 1];
|
|
30
|
+
|
|
31
|
+
if (inLineComment) {
|
|
32
|
+
current += ch;
|
|
33
|
+
if (ch === '\n') inLineComment = false;
|
|
34
|
+
i++;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (inBlockComment) {
|
|
39
|
+
if (ch === '*' && next === '/') {
|
|
40
|
+
current += '*/';
|
|
41
|
+
i += 2;
|
|
42
|
+
inBlockComment = false;
|
|
43
|
+
} else {
|
|
44
|
+
current += ch;
|
|
45
|
+
i++;
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (dollarTag !== null) {
|
|
51
|
+
if (ch === '$' && sql.startsWith(dollarTag, i)) {
|
|
52
|
+
current += dollarTag;
|
|
53
|
+
i += dollarTag.length;
|
|
54
|
+
dollarTag = null;
|
|
55
|
+
} else {
|
|
56
|
+
current += ch;
|
|
57
|
+
i++;
|
|
58
|
+
}
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (quote !== null) {
|
|
63
|
+
// MySQL backslash escapes apply inside string literals only.
|
|
64
|
+
if (ch === '\\' && quote === "'") {
|
|
65
|
+
current += ch;
|
|
66
|
+
if (next !== undefined) {
|
|
67
|
+
current += next;
|
|
68
|
+
i += 2;
|
|
69
|
+
} else {
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === quote) {
|
|
75
|
+
// Doubled quote is an escaped quote, not the end of the literal.
|
|
76
|
+
if (next === quote) {
|
|
77
|
+
current += ch + next;
|
|
78
|
+
i += 2;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
quote = null;
|
|
82
|
+
}
|
|
83
|
+
current += ch;
|
|
84
|
+
i++;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (ch === '-' && next === '-') {
|
|
89
|
+
inLineComment = true;
|
|
90
|
+
current += ch;
|
|
91
|
+
i++;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (ch === '/' && next === '*') {
|
|
96
|
+
inBlockComment = true;
|
|
97
|
+
current += '/*';
|
|
98
|
+
i += 2;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (ch === "'" || ch === '"' || ch === '`') {
|
|
103
|
+
quote = ch;
|
|
104
|
+
current += ch;
|
|
105
|
+
i++;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (ch === '$') {
|
|
110
|
+
const tagMatch = /^\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/.exec(sql.slice(i));
|
|
111
|
+
if (tagMatch) {
|
|
112
|
+
dollarTag = tagMatch[0];
|
|
113
|
+
current += dollarTag;
|
|
114
|
+
i += dollarTag.length;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (ch === ';') {
|
|
120
|
+
if (current.trim()) statements.push(current.trim());
|
|
121
|
+
current = '';
|
|
122
|
+
i++;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
current += ch;
|
|
127
|
+
i++;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (current.trim()) statements.push(current.trim());
|
|
131
|
+
return statements;
|
|
132
|
+
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Terminal domain helper functions
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Bun-compatible existsSync implementation
|
|
6
|
-
export async function existsSync(filePath: string): Promise<boolean> {
|
|
7
|
-
try {
|
|
8
|
-
const file = Bun.file(filePath);
|
|
9
|
-
await file.stat();
|
|
10
|
-
return true;
|
|
11
|
-
} catch {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Persistent PTY Session Manager
|
|
3
|
-
* Manages long-running interactive PTY sessions (one per terminal tab)
|
|
4
|
-
* Supports true TTY interaction with stdin/stdout streaming
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { IPty } from 'bun-pty';
|
|
8
|
-
import { spawn } from 'bun-pty';
|
|
9
|
-
import { debug } from '$shared/utils/logger';
|
|
10
|
-
import { createCleanPtyEnv } from './shell-utils';
|
|
11
|
-
|
|
12
|
-
export interface PtySession {
|
|
13
|
-
sessionId: string;
|
|
14
|
-
pty: IPty;
|
|
15
|
-
cwd: string;
|
|
16
|
-
userId: string;
|
|
17
|
-
projectId?: string;
|
|
18
|
-
createdAt: Date;
|
|
19
|
-
lastActivityAt: Date;
|
|
20
|
-
// Stream listeners
|
|
21
|
-
dataListeners: Set<(data: string) => void>;
|
|
22
|
-
exitListeners: Set<(event: { exitCode: number; signal?: number | string }) => void>;
|
|
23
|
-
// Output batching (performance optimization)
|
|
24
|
-
pendingOutput: string;
|
|
25
|
-
flushScheduled: boolean;
|
|
26
|
-
// Monotonically increasing sequence number for deduplication
|
|
27
|
-
outputSeq: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
class PtySessionManager {
|
|
31
|
-
private sessions = new Map<string, PtySession>();
|
|
32
|
-
|
|
33
|
-
constructor() {
|
|
34
|
-
// Sessions stay alive indefinitely until user closes the terminal tab
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Create or get existing PTY session
|
|
39
|
-
*/
|
|
40
|
-
async createSession(
|
|
41
|
-
sessionId: string,
|
|
42
|
-
cwd: string,
|
|
43
|
-
userId: string,
|
|
44
|
-
projectId?: string,
|
|
45
|
-
terminalSize?: { cols: number; rows: number }
|
|
46
|
-
): Promise<PtySession> {
|
|
47
|
-
// Return existing session if already created
|
|
48
|
-
const existing = this.sessions.get(sessionId);
|
|
49
|
-
if (existing && existing.pty) {
|
|
50
|
-
debug.log('terminal', `♻️ Reusing existing PTY session: ${sessionId}`);
|
|
51
|
-
existing.lastActivityAt = new Date();
|
|
52
|
-
return existing;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
debug.log('terminal', `🚀 Creating new interactive PTY session: ${sessionId}`);
|
|
56
|
-
|
|
57
|
-
// Determine shell based on platform
|
|
58
|
-
const isWindows = process.platform === 'win32';
|
|
59
|
-
let shell: string;
|
|
60
|
-
let shellArgs: string[] = [];
|
|
61
|
-
|
|
62
|
-
if (isWindows) {
|
|
63
|
-
// Windows: Use PowerShell in interactive mode
|
|
64
|
-
shell = 'powershell.exe';
|
|
65
|
-
shellArgs = ['-NoLogo']; // Interactive mode, no -Command
|
|
66
|
-
} else {
|
|
67
|
-
// Unix: Use user's default shell or bash
|
|
68
|
-
shell = process.env.SHELL || '/bin/bash';
|
|
69
|
-
shellArgs = []; // Interactive mode, no -c
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Use clean environment (no Bun/npm/Vite pollution)
|
|
73
|
-
const ptyEnv = createCleanPtyEnv(terminalSize);
|
|
74
|
-
|
|
75
|
-
// Terminal size
|
|
76
|
-
const cols = terminalSize?.cols || 80;
|
|
77
|
-
const rows = terminalSize?.rows || 24;
|
|
78
|
-
|
|
79
|
-
// Spawn interactive PTY
|
|
80
|
-
const pty = spawn(shell, shellArgs, {
|
|
81
|
-
name: 'xterm-256color',
|
|
82
|
-
cols,
|
|
83
|
-
rows,
|
|
84
|
-
cwd,
|
|
85
|
-
env: ptyEnv
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
debug.log('terminal', `✅ PTY spawned with PID: ${pty.pid}`);
|
|
89
|
-
let receivedInitialOutput = false;
|
|
90
|
-
|
|
91
|
-
// Create session
|
|
92
|
-
const session: PtySession = {
|
|
93
|
-
sessionId,
|
|
94
|
-
pty,
|
|
95
|
-
cwd,
|
|
96
|
-
userId,
|
|
97
|
-
projectId,
|
|
98
|
-
createdAt: new Date(),
|
|
99
|
-
lastActivityAt: new Date(),
|
|
100
|
-
dataListeners: new Set(),
|
|
101
|
-
exitListeners: new Set(),
|
|
102
|
-
// Initialize batching state
|
|
103
|
-
pendingOutput: '',
|
|
104
|
-
flushScheduled: false,
|
|
105
|
-
outputSeq: 0
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// Setup PTY event handlers with micro-task batching
|
|
109
|
-
pty.onData((data: string) => {
|
|
110
|
-
receivedInitialOutput = true;
|
|
111
|
-
session.lastActivityAt = new Date();
|
|
112
|
-
|
|
113
|
-
// IMPORTANT: Always persist output to stream manager FIRST
|
|
114
|
-
// This ensures output is saved even if there are no active listeners
|
|
115
|
-
try {
|
|
116
|
-
const { terminalStreamManager } = require('./stream-manager');
|
|
117
|
-
const stream = terminalStreamManager.getStreamBySession(sessionId);
|
|
118
|
-
if (stream) {
|
|
119
|
-
terminalStreamManager.addOutput(stream.streamId, data);
|
|
120
|
-
}
|
|
121
|
-
} catch (error) {
|
|
122
|
-
// Stream manager not available yet, skip buffering
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Batch output for high-frequency data (micro-task batching)
|
|
126
|
-
session.pendingOutput += data;
|
|
127
|
-
|
|
128
|
-
if (!session.flushScheduled) {
|
|
129
|
-
session.flushScheduled = true;
|
|
130
|
-
|
|
131
|
-
// Use queueMicrotask for minimal latency batching
|
|
132
|
-
queueMicrotask(() => {
|
|
133
|
-
const output = session.pendingOutput;
|
|
134
|
-
session.pendingOutput = '';
|
|
135
|
-
session.flushScheduled = false;
|
|
136
|
-
|
|
137
|
-
// Increment sequence number for deduplication
|
|
138
|
-
session.outputSeq++;
|
|
139
|
-
|
|
140
|
-
// Broadcast batched output to all listeners
|
|
141
|
-
session.dataListeners.forEach(listener => {
|
|
142
|
-
try {
|
|
143
|
-
listener(output);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
debug.error('terminal', 'Error in data listener:', error);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// Some shells do not paint the first prompt until they receive input.
|
|
153
|
-
// Only send a fallback newline if the PTY stayed completely silent after
|
|
154
|
-
// spawn; otherwise it duplicates the initial prompt on normal shells.
|
|
155
|
-
setTimeout(() => {
|
|
156
|
-
if (receivedInitialOutput) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
pty.write('\r');
|
|
162
|
-
debug.log('terminal', '📝 Sent fallback newline to trigger prompt');
|
|
163
|
-
} catch (error) {
|
|
164
|
-
debug.error('terminal', 'Failed to send fallback newline:', error);
|
|
165
|
-
}
|
|
166
|
-
}, 350);
|
|
167
|
-
|
|
168
|
-
pty.onExit((event: { exitCode: number; signal?: number | string }) => {
|
|
169
|
-
debug.log('terminal', `🏁 PTY session ${sessionId} exited with code: ${event.exitCode}`);
|
|
170
|
-
// Broadcast to all listeners
|
|
171
|
-
session.exitListeners.forEach(listener => {
|
|
172
|
-
try {
|
|
173
|
-
listener(event);
|
|
174
|
-
} catch (error) {
|
|
175
|
-
debug.error('terminal', 'Error in exit listener:', error);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
// Remove session
|
|
179
|
-
this.sessions.delete(sessionId);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Store session
|
|
183
|
-
this.sessions.set(sessionId, session);
|
|
184
|
-
|
|
185
|
-
return session;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Get existing session
|
|
190
|
-
*/
|
|
191
|
-
getSession(sessionId: string): PtySession | undefined {
|
|
192
|
-
return this.sessions.get(sessionId);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Write data to PTY (stdin)
|
|
197
|
-
*/
|
|
198
|
-
write(sessionId: string, data: string): boolean {
|
|
199
|
-
const session = this.sessions.get(sessionId);
|
|
200
|
-
if (!session || !session.pty) {
|
|
201
|
-
debug.error('terminal', `❌ Cannot write to session ${sessionId}: session not found`);
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
try {
|
|
206
|
-
session.pty.write(data);
|
|
207
|
-
session.lastActivityAt = new Date();
|
|
208
|
-
return true;
|
|
209
|
-
} catch (error) {
|
|
210
|
-
debug.error('terminal', `❌ Error writing to PTY ${sessionId}:`, error);
|
|
211
|
-
return false;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Resize PTY
|
|
217
|
-
*/
|
|
218
|
-
resize(sessionId: string, cols: number, rows: number): boolean {
|
|
219
|
-
const session = this.sessions.get(sessionId);
|
|
220
|
-
if (!session || !session.pty) {
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
try {
|
|
225
|
-
session.pty.resize(cols, rows);
|
|
226
|
-
debug.log('terminal', `🔧 PTY ${sessionId} resized to ${cols}x${rows}`);
|
|
227
|
-
return true;
|
|
228
|
-
} catch (error) {
|
|
229
|
-
debug.error('terminal', `❌ Error resizing PTY ${sessionId}:`, error);
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Kill PTY session
|
|
236
|
-
*/
|
|
237
|
-
killSession(sessionId: string, signal?: string): boolean {
|
|
238
|
-
const session = this.sessions.get(sessionId);
|
|
239
|
-
if (!session || !session.pty) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
try {
|
|
244
|
-
debug.log('terminal', `💀 Killing PTY session ${sessionId}`);
|
|
245
|
-
|
|
246
|
-
if (signal === 'SIGKILL' || signal === '9') {
|
|
247
|
-
session.pty.kill('SIGKILL');
|
|
248
|
-
} else if (signal === 'SIGTERM' || signal === '15') {
|
|
249
|
-
session.pty.kill('SIGTERM');
|
|
250
|
-
} else {
|
|
251
|
-
// Send Ctrl+C first for graceful termination
|
|
252
|
-
session.pty.write('\x03');
|
|
253
|
-
|
|
254
|
-
// Follow up with SIGKILL after delay
|
|
255
|
-
setTimeout(() => {
|
|
256
|
-
if (this.sessions.has(sessionId)) {
|
|
257
|
-
try {
|
|
258
|
-
session.pty.kill('SIGKILL');
|
|
259
|
-
} catch {
|
|
260
|
-
// Already dead, ignore
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}, 1000);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
this.sessions.delete(sessionId);
|
|
267
|
-
return true;
|
|
268
|
-
} catch (error) {
|
|
269
|
-
debug.error('terminal', `❌ Error killing PTY ${sessionId}:`, error);
|
|
270
|
-
this.sessions.delete(sessionId); // Remove anyway
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Add data listener
|
|
277
|
-
*/
|
|
278
|
-
addDataListener(sessionId: string, listener: (data: string) => void): boolean {
|
|
279
|
-
const session = this.sessions.get(sessionId);
|
|
280
|
-
if (!session) {
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
session.dataListeners.add(listener);
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Remove data listener
|
|
289
|
-
*/
|
|
290
|
-
removeDataListener(sessionId: string, listener: (data: string) => void): boolean {
|
|
291
|
-
const session = this.sessions.get(sessionId);
|
|
292
|
-
if (!session) {
|
|
293
|
-
return false;
|
|
294
|
-
}
|
|
295
|
-
return session.dataListeners.delete(listener);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Add exit listener
|
|
300
|
-
*/
|
|
301
|
-
addExitListener(
|
|
302
|
-
sessionId: string,
|
|
303
|
-
listener: (event: { exitCode: number; signal?: number | string }) => void
|
|
304
|
-
): boolean {
|
|
305
|
-
const session = this.sessions.get(sessionId);
|
|
306
|
-
if (!session) {
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
session.exitListeners.add(listener);
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Remove exit listener
|
|
315
|
-
*/
|
|
316
|
-
removeExitListener(
|
|
317
|
-
sessionId: string,
|
|
318
|
-
listener: (event: { exitCode: number; signal?: number | string }) => void
|
|
319
|
-
): boolean {
|
|
320
|
-
const session = this.sessions.get(sessionId);
|
|
321
|
-
if (!session) {
|
|
322
|
-
return false;
|
|
323
|
-
}
|
|
324
|
-
return session.exitListeners.delete(listener);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Get all active sessions
|
|
329
|
-
*/
|
|
330
|
-
getAllSessions(): PtySession[] {
|
|
331
|
-
return Array.from(this.sessions.values());
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Cleanup all sessions (on shutdown)
|
|
336
|
-
*/
|
|
337
|
-
dispose() {
|
|
338
|
-
debug.log('terminal', '🧹 Disposing all PTY sessions');
|
|
339
|
-
|
|
340
|
-
for (const sessionId of this.sessions.keys()) {
|
|
341
|
-
this.killSession(sessionId, 'SIGKILL');
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
this.sessions.clear();
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Export singleton instance
|
|
349
|
-
export const ptySessionManager = new PtySessionManager();
|