tacticremote 1.7.0 → 1.8.1
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/LICENSE +28 -0
- package/README.md +33 -41
- package/bin/tacticremote.js +41 -101
- package/package.json +30 -42
- package/bin/tactic-remote-server.js +0 -636
- package/lib/authChallengeManager.js +0 -1
- package/lib/bonjourPublisher.js +0 -68
- package/lib/claudeSessionManager.js +0 -1
- package/lib/claudeTransport.js +0 -1
- package/lib/errorCodes.js +0 -1
- package/lib/execSafe.js +0 -1
- package/lib/fileService.js +0 -1
- package/lib/hookInstaller.js +0 -253
- package/lib/outputParser.js +0 -1
- package/lib/pushClient.js +0 -1
- package/lib/rateLimiter.js +0 -1
- package/lib/serverIdentity.js +0 -1
- package/lib/streamJsonParser.js +0 -1
- package/lib/tmuxManager.js +0 -1
- package/lib/uploadService.js +0 -1
- package/lib/validators.js +0 -1
- package/middleware/authMiddleware.js +0 -152
- package/routes/httpHandler.js +0 -111
- package/routes/wsHandler.js +0 -1094
- package/server.js +0 -236
- package/shared/server-core/authChallengeManager.js +0 -108
- package/shared/server-core/claudeSessionManager.js +0 -222
- package/shared/server-core/claudeTransport.js +0 -176
- package/shared/server-core/errorCodes.js +0 -91
- package/shared/server-core/execSafe.js +0 -185
- package/shared/server-core/fileService.js +0 -155
- package/shared/server-core/outputParser.js +0 -174
- package/shared/server-core/protocol-manifest.json +0 -59
- package/shared/server-core/protocol-manifest.schema.json +0 -90
- package/shared/server-core/protocolManifest.js +0 -42
- package/shared/server-core/pushClient.js +0 -198
- package/shared/server-core/rateLimiter.js +0 -98
- package/shared/server-core/serverConnectionLifecycle.js +0 -118
- package/shared/server-core/serverHandshake.js +0 -72
- package/shared/server-core/serverIdentity.js +0 -102
- package/shared/server-core/streamJsonParser.js +0 -321
- package/shared/server-core/tmuxManager.js +0 -596
- package/shared/server-core/unixProcessLifecycle.js +0 -96
- package/shared/server-core/unixServerHooks.js +0 -148
- package/shared/server-core/unixWsRuntime.js +0 -412
- package/shared/server-core/uploadService.js +0 -321
- package/shared/server-core/validators.js +0 -274
- package/shared/server-core/windowsServerHooks.js +0 -174
- package/shared/server-core/windowsWsRuntime.js +0 -413
package/routes/wsHandler.js
DELETED
|
@@ -1,1094 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebSocket message handler for MacServer
|
|
3
|
-
* Supports both legacy protocol and new req/res/event frame protocol
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const tmux = require('../lib/tmuxManager');
|
|
7
|
-
const fileService = require('../lib/fileService');
|
|
8
|
-
const uploadService = require('../lib/uploadService');
|
|
9
|
-
const { ErrorCodes, createError } = require('../lib/errorCodes');
|
|
10
|
-
const { parseClaudeOutput } = require('../lib/outputParser');
|
|
11
|
-
const { tmuxCapture, tmuxGetPanePath, execSafe } = require('../lib/execSafe');
|
|
12
|
-
const { validatePath, validateSessionName } = require('../lib/validators');
|
|
13
|
-
const authChallengeManager = require('../lib/authChallengeManager');
|
|
14
|
-
const { ClaudeSessionManager } = require('../lib/claudeSessionManager');
|
|
15
|
-
const pushClient = require('../lib/pushClient');
|
|
16
|
-
const { createUnixWsRuntime } = require('../shared/server-core/unixWsRuntime');
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Create WebSocket message handler
|
|
20
|
-
* @param {Object} server - Server instance
|
|
21
|
-
* @returns {Object} Handler methods
|
|
22
|
-
*/
|
|
23
|
-
function createWsHandler(server) {
|
|
24
|
-
let claudeSessionManager;
|
|
25
|
-
|
|
26
|
-
const runtime = createUnixWsRuntime({
|
|
27
|
-
server,
|
|
28
|
-
tmux,
|
|
29
|
-
fileService,
|
|
30
|
-
authChallengeManager,
|
|
31
|
-
parseClaudeOutput,
|
|
32
|
-
tmuxCapture,
|
|
33
|
-
execSafe,
|
|
34
|
-
validatePath,
|
|
35
|
-
getStructuredSessionManager: () => claudeSessionManager
|
|
36
|
-
});
|
|
37
|
-
const {
|
|
38
|
-
outputMonitors,
|
|
39
|
-
clientMonitors,
|
|
40
|
-
getClientPath,
|
|
41
|
-
updateClientState,
|
|
42
|
-
setClientPath,
|
|
43
|
-
normalizeClientSession,
|
|
44
|
-
sendToClient,
|
|
45
|
-
sendResponse,
|
|
46
|
-
sendErrorResponse,
|
|
47
|
-
sendEvent,
|
|
48
|
-
broadcastEvent,
|
|
49
|
-
broadcast,
|
|
50
|
-
withSensitivePathAuth,
|
|
51
|
-
collectGitInfo,
|
|
52
|
-
collectWorktrees,
|
|
53
|
-
startOutputMonitor,
|
|
54
|
-
stopMonitor,
|
|
55
|
-
removeClientFromMonitor,
|
|
56
|
-
cleanupClient,
|
|
57
|
-
captureAndBroadcast
|
|
58
|
-
} = runtime;
|
|
59
|
-
|
|
60
|
-
claudeSessionManager = new ClaudeSessionManager({
|
|
61
|
-
broadcastToSubscribers: (subscribers, eventName, payload) => {
|
|
62
|
-
for (const ws of subscribers) {
|
|
63
|
-
if (ws.readyState === 1) {
|
|
64
|
-
sendEvent(ws, eventName, payload);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
allowedBasePath: server.allowedBasePath
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Handle incoming WebSocket message
|
|
74
|
-
* Supports both legacy protocol (type as method name) and new protocol (req/res/event)
|
|
75
|
-
*/
|
|
76
|
-
async function handleMessage(ws, message) {
|
|
77
|
-
// New protocol: handle 'req' type messages
|
|
78
|
-
if (message.type === 'req' && message.id && message.method) {
|
|
79
|
-
console.log(`📨 Request [${message.id}]: ${message.method}`);
|
|
80
|
-
await handleRequest(ws, message);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Legacy protocol: type is the method name
|
|
85
|
-
console.log(`📨 Received: ${message.type}`);
|
|
86
|
-
await handleLegacyMessage(ws, message);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Handle new protocol request messages
|
|
91
|
-
*/
|
|
92
|
-
async function handleRequest(ws, message) {
|
|
93
|
-
const { id, method, params = {} } = message;
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
switch (method) {
|
|
97
|
-
case 'list_sessions': {
|
|
98
|
-
const result = await tmux.listSessions();
|
|
99
|
-
const sessions = result.sessions.map(s => ({
|
|
100
|
-
...s,
|
|
101
|
-
managed: server.managedSessions.has(s.name)
|
|
102
|
-
}));
|
|
103
|
-
sendResponse(ws, id, { sessions });
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
case 'send_to_claude': {
|
|
108
|
-
// Safety guard: reject terminal steer when a structured session has a pending approval
|
|
109
|
-
if (claudeSessionManager.hasAnyPendingApproval()) {
|
|
110
|
-
console.warn('[send_to_claude] Blocked: a structured session has a pending tool approval — refusing terminal send to prevent accidental y/n');
|
|
111
|
-
sendErrorResponse(ws, id, {
|
|
112
|
-
type: 'error',
|
|
113
|
-
code: 'STEER_BLOCKED_BY_APPROVAL',
|
|
114
|
-
error: 'Cannot send to terminal while a tool approval is pending in a structured session',
|
|
115
|
-
retryable: false
|
|
116
|
-
});
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
const result = await tmux.sendToClaude(params.content, params.session, params.path || getClientPath(ws), params.cols, params.rows, params.command);
|
|
120
|
-
if (result.success) {
|
|
121
|
-
sendResponse(ws, id, { status: 'sent' });
|
|
122
|
-
startOutputMonitor(ws, params.session || tmux.DEFAULT_SESSION);
|
|
123
|
-
} else {
|
|
124
|
-
sendErrorResponse(ws, id, result.error);
|
|
125
|
-
}
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
case 'change_path': {
|
|
130
|
-
const result = await withSensitivePathAuth(ws, params.path, 'change_path',
|
|
131
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
132
|
-
async () => fileService.changePath(params.path, server.allowedBasePath, getClientPath(ws))
|
|
133
|
-
);
|
|
134
|
-
if (result.success) {
|
|
135
|
-
setClientPath(ws, result.path);
|
|
136
|
-
sendResponse(ws, id, { path: result.path });
|
|
137
|
-
} else {
|
|
138
|
-
sendErrorResponse(ws, id, result.error);
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
case 'start_claude': {
|
|
144
|
-
console.log(`[DEBUG start_claude] params.session=${params.session}, params.command=${params.command}, params.path=${params.path}, clientPath=${getClientPath(ws)}`);
|
|
145
|
-
const result = await tmux.startClaude(params.session, params.command, params.path || getClientPath(ws), params.cols, params.rows);
|
|
146
|
-
console.log(`[DEBUG start_claude] result: success=${result.success}, isRunning=${result.isRunning}, alreadyRunning=${result.alreadyRunning}, error=${result.error}`);
|
|
147
|
-
if (result.success) {
|
|
148
|
-
sendResponse(ws, id, {
|
|
149
|
-
status: 'running',
|
|
150
|
-
isRunning: result.isRunning === true,
|
|
151
|
-
alreadyRunning: result.alreadyRunning === true
|
|
152
|
-
});
|
|
153
|
-
startOutputMonitor(ws, params.session || tmux.DEFAULT_SESSION);
|
|
154
|
-
} else {
|
|
155
|
-
sendErrorResponse(ws, id, result.error);
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
case 'stop_claude': {
|
|
161
|
-
const result = await tmux.stopClaude(params.session);
|
|
162
|
-
stopMonitor(params.session || tmux.DEFAULT_SESSION);
|
|
163
|
-
if (result.success) {
|
|
164
|
-
sendResponse(ws, id, { status: 'stopped' });
|
|
165
|
-
} else {
|
|
166
|
-
sendErrorResponse(ws, id, result.error);
|
|
167
|
-
}
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
case 'attach_session': {
|
|
172
|
-
// Remove this client from monitors for other sessions
|
|
173
|
-
const clientSessions = clientMonitors.get(ws);
|
|
174
|
-
if (clientSessions) {
|
|
175
|
-
for (const name of [...clientSessions]) {
|
|
176
|
-
if (name !== params.session) {
|
|
177
|
-
removeClientFromMonitor(ws, name);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const result = await tmux.attachSession(params.session);
|
|
183
|
-
if (result.success) {
|
|
184
|
-
updateClientState(ws, { currentSession: normalizeClientSession(params.session) });
|
|
185
|
-
if (result.path) setClientPath(ws, result.path);
|
|
186
|
-
sendResponse(ws, id, {
|
|
187
|
-
path: getClientPath(ws),
|
|
188
|
-
isRunning: result.isRunning,
|
|
189
|
-
content: result.output?.content
|
|
190
|
-
});
|
|
191
|
-
if (result.isRunning) {
|
|
192
|
-
startOutputMonitor(ws, params.session);
|
|
193
|
-
}
|
|
194
|
-
} else {
|
|
195
|
-
sendErrorResponse(ws, id, result.error);
|
|
196
|
-
}
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
case 'client_state': {
|
|
201
|
-
updateClientState(ws, {
|
|
202
|
-
foreground: params.foreground === true,
|
|
203
|
-
currentSession: normalizeClientSession(params.currentSession),
|
|
204
|
-
supportsClientState: true
|
|
205
|
-
});
|
|
206
|
-
sendResponse(ws, id, { status: 'ok' });
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
case 'create_session': {
|
|
211
|
-
console.log(`[DEBUG create_session] params.name=${params.name}, params.path=${params.path}, clientPath=${getClientPath(ws)}, allowedBasePath=${server.allowedBasePath}`);
|
|
212
|
-
const result = await withSensitivePathAuth(ws, params.path || getClientPath(ws), 'create_session',
|
|
213
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
214
|
-
async () => tmux.createSession(params.name, params.path, server.allowedBasePath, getClientPath(ws))
|
|
215
|
-
);
|
|
216
|
-
console.log(`[DEBUG create_session] result: success=${result.success}, path=${result.path}, error=${JSON.stringify(result.error)}`);
|
|
217
|
-
if (result.success) {
|
|
218
|
-
server.managedSessions.add(params.name);
|
|
219
|
-
setClientPath(ws, result.path);
|
|
220
|
-
const listResult = await tmux.listSessions();
|
|
221
|
-
sendResponse(ws, id, { path: result.path, sessions: listResult.sessions });
|
|
222
|
-
} else {
|
|
223
|
-
sendErrorResponse(ws, id, result.error);
|
|
224
|
-
}
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
case 'get_output': {
|
|
229
|
-
const result = await tmux.getSessionOutput(params.session);
|
|
230
|
-
if (result.success) {
|
|
231
|
-
sendResponse(ws, id, {
|
|
232
|
-
content: result.content,
|
|
233
|
-
isRunning: result.isRunning,
|
|
234
|
-
userQuestion: result.userQuestion,
|
|
235
|
-
claudeResponse: result.claudeResponse
|
|
236
|
-
});
|
|
237
|
-
} else {
|
|
238
|
-
sendErrorResponse(ws, id, result.error);
|
|
239
|
-
}
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
case 'send_key': {
|
|
244
|
-
const result = await tmux.sendKeyToSession(params.key, params.session);
|
|
245
|
-
if (result.success) {
|
|
246
|
-
sendResponse(ws, id, { status: 'key_sent' });
|
|
247
|
-
setTimeout(() => {
|
|
248
|
-
captureAndBroadcast(params.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
249
|
-
}, 300);
|
|
250
|
-
} else {
|
|
251
|
-
sendErrorResponse(ws, id, result.error);
|
|
252
|
-
}
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
case 'send_sequence': {
|
|
257
|
-
if (!Array.isArray(params.keys)) {
|
|
258
|
-
sendErrorResponse(ws, id, { type: 'error', code: 'INVALID_PARAMS', error: 'keys must be an array', retryable: false });
|
|
259
|
-
break;
|
|
260
|
-
}
|
|
261
|
-
const result = await tmux.sendKeySequence(params.session, params.keys);
|
|
262
|
-
if (result.success) {
|
|
263
|
-
sendResponse(ws, id, { status: 'sequence_sent' });
|
|
264
|
-
setTimeout(() => {
|
|
265
|
-
captureAndBroadcast(params.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
266
|
-
}, 300);
|
|
267
|
-
} else {
|
|
268
|
-
sendErrorResponse(ws, id, result.error);
|
|
269
|
-
}
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
case 'delete_session': {
|
|
274
|
-
const result = await tmux.deleteSession(params.session);
|
|
275
|
-
stopMonitor(params.session);
|
|
276
|
-
if (result.success) {
|
|
277
|
-
server.managedSessions.delete(params.session);
|
|
278
|
-
const listResult = await tmux.listSessions();
|
|
279
|
-
sendResponse(ws, id, { session: params.session, sessions: listResult.sessions });
|
|
280
|
-
} else {
|
|
281
|
-
sendErrorResponse(ws, id, result.error);
|
|
282
|
-
}
|
|
283
|
-
break;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
case 'kill_session': {
|
|
287
|
-
const result = await tmux.killSession(params.session);
|
|
288
|
-
stopMonitor(params.session);
|
|
289
|
-
if (result.success) {
|
|
290
|
-
server.managedSessions.delete(params.session);
|
|
291
|
-
const listResult = await tmux.listSessions();
|
|
292
|
-
sendResponse(ws, id, { session: params.session, sessions: listResult.sessions });
|
|
293
|
-
} else {
|
|
294
|
-
sendErrorResponse(ws, id, result.error);
|
|
295
|
-
}
|
|
296
|
-
break;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
case 'get_full_output': {
|
|
300
|
-
const result = await tmux.getFullSessionOutput(params.session, params.lines);
|
|
301
|
-
if (result.success) {
|
|
302
|
-
sendResponse(ws, id, { content: result.content, lines: params.lines });
|
|
303
|
-
} else {
|
|
304
|
-
sendErrorResponse(ws, id, result.error);
|
|
305
|
-
}
|
|
306
|
-
break;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
case 'create_folder': {
|
|
310
|
-
const result = await withSensitivePathAuth(ws, params.path, 'create_folder',
|
|
311
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
312
|
-
async () => fileService.createFolder(params.path, server.allowedBasePath, getClientPath(ws))
|
|
313
|
-
);
|
|
314
|
-
if (result.success) {
|
|
315
|
-
sendResponse(ws, id, { path: result.path });
|
|
316
|
-
} else {
|
|
317
|
-
sendErrorResponse(ws, id, result.error);
|
|
318
|
-
}
|
|
319
|
-
break;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
case 'list_directory': {
|
|
323
|
-
const result = await withSensitivePathAuth(ws, params.path, 'list_directory',
|
|
324
|
-
{ allowBrowse: true, allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
325
|
-
async () => fileService.listDirectory(params.path, server.allowedBasePath, getClientPath(ws))
|
|
326
|
-
);
|
|
327
|
-
if (result.success) {
|
|
328
|
-
sendResponse(ws, id, { path: result.path, items: result.items });
|
|
329
|
-
} else {
|
|
330
|
-
sendErrorResponse(ws, id, result.error);
|
|
331
|
-
}
|
|
332
|
-
break;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
case 'upload_start': {
|
|
336
|
-
const result = uploadService.startUpload(
|
|
337
|
-
params.filename,
|
|
338
|
-
Number(params.size),
|
|
339
|
-
params.mimeType,
|
|
340
|
-
Number(params.totalChunks),
|
|
341
|
-
params.destPath,
|
|
342
|
-
server.allowedBasePath,
|
|
343
|
-
getClientPath(ws)
|
|
344
|
-
);
|
|
345
|
-
if (result.success) {
|
|
346
|
-
console.log(`📤 Upload started: ${params.filename} (${(Number(params.size) / 1024).toFixed(0)}KB, ${params.totalChunks} chunks) → uploadId: ${result.uploadId}`);
|
|
347
|
-
sendResponse(ws, id, { uploadId: result.uploadId, ready: true });
|
|
348
|
-
} else {
|
|
349
|
-
sendErrorResponse(ws, id, result.error);
|
|
350
|
-
}
|
|
351
|
-
break;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
case 'upload_chunk': {
|
|
355
|
-
const result = uploadService.receiveChunk(
|
|
356
|
-
params.uploadId,
|
|
357
|
-
Number(params.chunkIndex),
|
|
358
|
-
params.data
|
|
359
|
-
);
|
|
360
|
-
if (result.success) {
|
|
361
|
-
if (result.complete) {
|
|
362
|
-
console.log(`✅ Upload complete: ${result.filePath} (${result.totalBytes} bytes)`);
|
|
363
|
-
sendResponse(ws, id, {
|
|
364
|
-
complete: true,
|
|
365
|
-
filePath: result.filePath,
|
|
366
|
-
totalBytes: result.totalBytes,
|
|
367
|
-
mimeType: result.mimeType
|
|
368
|
-
});
|
|
369
|
-
} else {
|
|
370
|
-
sendResponse(ws, id, {
|
|
371
|
-
chunkIndex: result.chunkIndex,
|
|
372
|
-
receivedBytes: result.receivedBytes
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
} else {
|
|
376
|
-
sendErrorResponse(ws, id, result.error);
|
|
377
|
-
}
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
case 'upload_cancel': {
|
|
382
|
-
uploadService.cancelUpload(params.uploadId);
|
|
383
|
-
console.log(`🚫 Upload cancelled: ${params.uploadId}`);
|
|
384
|
-
sendResponse(ws, id, { cancelled: true });
|
|
385
|
-
break;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
case 'resize_session': {
|
|
389
|
-
const result = await tmux.resizeSession(params.session, params.cols, params.rows);
|
|
390
|
-
if (result.success) {
|
|
391
|
-
sendResponse(ws, id, { cols: result.cols, rows: result.rows });
|
|
392
|
-
} else {
|
|
393
|
-
sendErrorResponse(ws, id, result.error);
|
|
394
|
-
}
|
|
395
|
-
break;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
case 'auth_response': {
|
|
399
|
-
const resolved = authChallengeManager.resolveChallenge(
|
|
400
|
-
params.challengeId,
|
|
401
|
-
params.granted === true || params.granted === 'true'
|
|
402
|
-
);
|
|
403
|
-
if (resolved) {
|
|
404
|
-
sendResponse(ws, id, { status: 'received' });
|
|
405
|
-
} else {
|
|
406
|
-
sendErrorResponse(ws, id, {
|
|
407
|
-
type: 'error',
|
|
408
|
-
code: 'INVALID_CHALLENGE',
|
|
409
|
-
error: 'Challenge not found or already resolved',
|
|
410
|
-
retryable: false
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
case 'git_info': {
|
|
417
|
-
const sessionCheck = validateSessionName(params.session);
|
|
418
|
-
if (!sessionCheck.valid) {
|
|
419
|
-
sendErrorResponse(ws, id, { type: 'error', code: 'INVALID_PARAMS', error: sessionCheck.error, retryable: false });
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
let cwd;
|
|
423
|
-
try { cwd = (await tmuxGetPanePath(params.session)).trim(); } catch { cwd = getClientPath(ws); }
|
|
424
|
-
if (!cwd) {
|
|
425
|
-
cwd = getClientPath(ws);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const pathOptions = {
|
|
429
|
-
allowedBasePath: server.allowedBasePath,
|
|
430
|
-
currentPath: getClientPath(ws)
|
|
431
|
-
};
|
|
432
|
-
const result = await withSensitivePathAuth(ws, cwd, 'git_info', pathOptions, async () => {
|
|
433
|
-
const validation = validatePath(cwd, pathOptions);
|
|
434
|
-
if (!validation.valid) {
|
|
435
|
-
return {
|
|
436
|
-
success: false,
|
|
437
|
-
error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error)
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
return {
|
|
441
|
-
success: true,
|
|
442
|
-
payload: await collectGitInfo(validation.path)
|
|
443
|
-
};
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
if (result.success) {
|
|
447
|
-
sendResponse(ws, id, result.payload);
|
|
448
|
-
} else {
|
|
449
|
-
sendErrorResponse(ws, id, result.error);
|
|
450
|
-
}
|
|
451
|
-
break;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
case 'list_worktrees': {
|
|
455
|
-
// Discover git worktrees rooted at the given path (or the
|
|
456
|
-
// client's current path). Returns [] if not a git repo.
|
|
457
|
-
let cwd = (params && typeof params.path === 'string' && params.path) || getClientPath(ws);
|
|
458
|
-
const pathOptions = {
|
|
459
|
-
allowedBasePath: server.allowedBasePath,
|
|
460
|
-
currentPath: getClientPath(ws)
|
|
461
|
-
};
|
|
462
|
-
const result = await withSensitivePathAuth(ws, cwd, 'list_worktrees', pathOptions, async () => {
|
|
463
|
-
const validation = validatePath(cwd, pathOptions);
|
|
464
|
-
if (!validation.valid) {
|
|
465
|
-
return {
|
|
466
|
-
success: false,
|
|
467
|
-
error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error)
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
const worktrees = await collectWorktrees(validation.path);
|
|
471
|
-
return { success: true, payload: { worktrees } };
|
|
472
|
-
});
|
|
473
|
-
if (result.success) {
|
|
474
|
-
sendResponse(ws, id, result.payload);
|
|
475
|
-
} else {
|
|
476
|
-
sendErrorResponse(ws, id, result.error);
|
|
477
|
-
}
|
|
478
|
-
break;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// ── Structured JSON mode methods ──
|
|
482
|
-
|
|
483
|
-
case 'start_claude_structured': {
|
|
484
|
-
const cwd = params.path || getClientPath(ws);
|
|
485
|
-
const result = await withSensitivePathAuth(ws, cwd, 'start_claude_structured',
|
|
486
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
487
|
-
async () => {
|
|
488
|
-
const validation = validatePath(cwd, {
|
|
489
|
-
allowedBasePath: server.allowedBasePath,
|
|
490
|
-
currentPath: getClientPath(ws)
|
|
491
|
-
});
|
|
492
|
-
if (!validation.valid) {
|
|
493
|
-
return { success: false, error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error) };
|
|
494
|
-
}
|
|
495
|
-
return { success: true, path: validation.path };
|
|
496
|
-
}
|
|
497
|
-
);
|
|
498
|
-
if (!result.success) {
|
|
499
|
-
sendErrorResponse(ws, id, result.error);
|
|
500
|
-
break;
|
|
501
|
-
}
|
|
502
|
-
const resolvedPath = result.path;
|
|
503
|
-
const additionalArgs = [];
|
|
504
|
-
if (params.plan_mode === true || params.plan_mode === 'true') {
|
|
505
|
-
additionalArgs.push('--allowedTools', 'Read,Glob,Grep,LS');
|
|
506
|
-
}
|
|
507
|
-
try {
|
|
508
|
-
const result = claudeSessionManager.createSession(resolvedPath, ws, additionalArgs);
|
|
509
|
-
ws._structuredSessionId = result.sessionId;
|
|
510
|
-
setClientPath(ws, resolvedPath);
|
|
511
|
-
sendResponse(ws, id, {
|
|
512
|
-
sessionId: result.sessionId,
|
|
513
|
-
cwd: result.cwd,
|
|
514
|
-
mode: 'structured'
|
|
515
|
-
});
|
|
516
|
-
console.log(`🔧 Structured session started: ${result.sessionId} at ${resolvedPath}`);
|
|
517
|
-
} catch (err) {
|
|
518
|
-
sendErrorResponse(ws, id, {
|
|
519
|
-
type: 'error',
|
|
520
|
-
code: 'INTERNAL_ERROR',
|
|
521
|
-
error: `Failed to start structured session: ${err.message}`,
|
|
522
|
-
retryable: true
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
case 'send_structured': {
|
|
529
|
-
const sessionId = params.session_id;
|
|
530
|
-
if (!sessionId) {
|
|
531
|
-
sendErrorResponse(ws, id, {
|
|
532
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
533
|
-
error: 'session_id is required', retryable: false
|
|
534
|
-
});
|
|
535
|
-
break;
|
|
536
|
-
}
|
|
537
|
-
// Refuse to start a new user turn only when THIS structured
|
|
538
|
-
// session is waiting for approval. Other sessions must remain
|
|
539
|
-
// usable; otherwise one pending approval blocks the whole app.
|
|
540
|
-
if (claudeSessionManager.hasPendingApproval(sessionId)) {
|
|
541
|
-
console.warn('[send_structured] Blocked: a structured session has a pending tool approval');
|
|
542
|
-
sendErrorResponse(ws, id, {
|
|
543
|
-
type: 'error',
|
|
544
|
-
code: 'SEND_BLOCKED_BY_APPROVAL',
|
|
545
|
-
error: 'Cannot send a new message while a tool approval is pending',
|
|
546
|
-
retryable: true
|
|
547
|
-
});
|
|
548
|
-
break;
|
|
549
|
-
}
|
|
550
|
-
const sent = claudeSessionManager.sendMessage(sessionId, params.content);
|
|
551
|
-
if (sent) {
|
|
552
|
-
sendResponse(ws, id, { status: 'sent' });
|
|
553
|
-
} else {
|
|
554
|
-
sendErrorResponse(ws, id, {
|
|
555
|
-
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
556
|
-
error: `Structured session ${sessionId} not found or process dead`,
|
|
557
|
-
retryable: false
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
case 'stop_structured': {
|
|
564
|
-
const sessionId = params.session_id;
|
|
565
|
-
if (!sessionId) {
|
|
566
|
-
sendErrorResponse(ws, id, {
|
|
567
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
568
|
-
error: 'session_id is required', retryable: false
|
|
569
|
-
});
|
|
570
|
-
break;
|
|
571
|
-
}
|
|
572
|
-
const sessionExists = claudeSessionManager.getSession(sessionId);
|
|
573
|
-
if (!sessionExists) {
|
|
574
|
-
sendErrorResponse(ws, id, {
|
|
575
|
-
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
576
|
-
error: `Structured session ${sessionId} not found`,
|
|
577
|
-
retryable: false
|
|
578
|
-
});
|
|
579
|
-
break;
|
|
580
|
-
}
|
|
581
|
-
claudeSessionManager.stopSession(sessionId);
|
|
582
|
-
sendResponse(ws, id, { status: 'stopped' });
|
|
583
|
-
console.log(`🔧 Structured session stopped: ${sessionId}`);
|
|
584
|
-
break;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
case 'list_structured_sessions': {
|
|
588
|
-
const sessions = claudeSessionManager.listSessions();
|
|
589
|
-
sendResponse(ws, id, { sessions });
|
|
590
|
-
break;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
case 'attach_structured_session': {
|
|
594
|
-
const sessionId = params.session_id;
|
|
595
|
-
if (!sessionId) {
|
|
596
|
-
sendErrorResponse(ws, id, {
|
|
597
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
598
|
-
error: 'session_id is required', retryable: false
|
|
599
|
-
});
|
|
600
|
-
break;
|
|
601
|
-
}
|
|
602
|
-
const session = claudeSessionManager.getSession(sessionId);
|
|
603
|
-
if (!session) {
|
|
604
|
-
sendErrorResponse(ws, id, {
|
|
605
|
-
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
606
|
-
error: `Structured session ${sessionId} not found`,
|
|
607
|
-
retryable: false
|
|
608
|
-
});
|
|
609
|
-
break;
|
|
610
|
-
}
|
|
611
|
-
claudeSessionManager.addSubscriber(sessionId, ws);
|
|
612
|
-
ws._structuredSessionId = sessionId;
|
|
613
|
-
sendResponse(ws, id, {
|
|
614
|
-
sessionId: session.sessionId,
|
|
615
|
-
cwd: session.cwd,
|
|
616
|
-
isRunning: session.isRunning,
|
|
617
|
-
mode: 'structured'
|
|
618
|
-
});
|
|
619
|
-
break;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
case 'respond_approval': {
|
|
623
|
-
const { session_id, request_id, behavior, updated_input, message } = params;
|
|
624
|
-
if (!session_id || !request_id || !behavior) {
|
|
625
|
-
sendErrorResponse(ws, id, {
|
|
626
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
627
|
-
error: 'session_id, request_id, and behavior are required', retryable: false
|
|
628
|
-
});
|
|
629
|
-
break;
|
|
630
|
-
}
|
|
631
|
-
if (behavior !== 'allow' && behavior !== 'deny') {
|
|
632
|
-
sendErrorResponse(ws, id, {
|
|
633
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
634
|
-
error: 'behavior must be "allow" or "deny"', retryable: false
|
|
635
|
-
});
|
|
636
|
-
break;
|
|
637
|
-
}
|
|
638
|
-
const sent = claudeSessionManager.respondApproval(
|
|
639
|
-
session_id, request_id, behavior, updated_input, message
|
|
640
|
-
);
|
|
641
|
-
if (sent) {
|
|
642
|
-
sendResponse(ws, id, { status: 'sent' });
|
|
643
|
-
} else {
|
|
644
|
-
sendErrorResponse(ws, id, {
|
|
645
|
-
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
646
|
-
error: `Structured session ${session_id} not found or process dead`,
|
|
647
|
-
retryable: false
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
break;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
case 'steer_structured': {
|
|
654
|
-
const { session_id, content } = params;
|
|
655
|
-
if (!session_id || !content || typeof content !== 'string' || content.trim() === '') {
|
|
656
|
-
sendErrorResponse(ws, id, {
|
|
657
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
658
|
-
error: 'session_id and content (non-empty string) are required',
|
|
659
|
-
retryable: false
|
|
660
|
-
});
|
|
661
|
-
break;
|
|
662
|
-
}
|
|
663
|
-
const steerSession = claudeSessionManager.getSession(session_id);
|
|
664
|
-
if (!steerSession) {
|
|
665
|
-
sendErrorResponse(ws, id, {
|
|
666
|
-
type: 'error', code: 'SESSION_NOT_FOUND',
|
|
667
|
-
error: `Structured session ${session_id} not found`,
|
|
668
|
-
retryable: false
|
|
669
|
-
});
|
|
670
|
-
break;
|
|
671
|
-
}
|
|
672
|
-
if (claudeSessionManager.hasPendingApproval(session_id)) {
|
|
673
|
-
sendErrorResponse(ws, id, {
|
|
674
|
-
type: 'error', code: 'STEER_BLOCKED_BY_APPROVAL',
|
|
675
|
-
error: 'Cannot steer while a tool approval is pending',
|
|
676
|
-
retryable: false
|
|
677
|
-
});
|
|
678
|
-
break;
|
|
679
|
-
}
|
|
680
|
-
const steered = claudeSessionManager.steerMessage(session_id, content);
|
|
681
|
-
if (steered) {
|
|
682
|
-
sendResponse(ws, id, { status: 'steered' });
|
|
683
|
-
} else {
|
|
684
|
-
sendErrorResponse(ws, id, {
|
|
685
|
-
type: 'error', code: 'TRANSPORT_UNAVAILABLE',
|
|
686
|
-
error: 'Transport stdin is closed or unavailable',
|
|
687
|
-
retryable: true
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
break;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
// ── Push notification methods ──
|
|
694
|
-
|
|
695
|
-
case 'register_push': {
|
|
696
|
-
const { deviceToken, alertsEnabled, apnsEnvironment } = params;
|
|
697
|
-
const normalizedAlertsEnabled = !(alertsEnabled === false || alertsEnabled === 'false');
|
|
698
|
-
const normalizedApnsEnvironment = typeof apnsEnvironment === 'string' && apnsEnvironment.trim() !== ''
|
|
699
|
-
? apnsEnvironment.trim()
|
|
700
|
-
: 'production';
|
|
701
|
-
if (!deviceToken || typeof deviceToken !== 'string' || deviceToken.trim() === '') {
|
|
702
|
-
sendErrorResponse(ws, id, {
|
|
703
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
704
|
-
error: 'deviceToken is required',
|
|
705
|
-
retryable: false
|
|
706
|
-
});
|
|
707
|
-
break;
|
|
708
|
-
}
|
|
709
|
-
if (!pushClient.isConfigured()) {
|
|
710
|
-
sendErrorResponse(ws, id, {
|
|
711
|
-
type: 'error', code: 'PUSH_NOT_CONFIGURED',
|
|
712
|
-
error: 'Push notifications are not configured on this server',
|
|
713
|
-
retryable: false
|
|
714
|
-
});
|
|
715
|
-
break;
|
|
716
|
-
}
|
|
717
|
-
const identity = server.serverIdentity;
|
|
718
|
-
if (!identity || !identity.serverId || !identity.serverSecret) {
|
|
719
|
-
sendErrorResponse(ws, id, {
|
|
720
|
-
type: 'error', code: 'INTERNAL_ERROR',
|
|
721
|
-
error: 'Server identity not available',
|
|
722
|
-
retryable: true
|
|
723
|
-
});
|
|
724
|
-
break;
|
|
725
|
-
}
|
|
726
|
-
try {
|
|
727
|
-
const result = await pushClient.register(
|
|
728
|
-
identity.serverId,
|
|
729
|
-
identity.serverSecret,
|
|
730
|
-
deviceToken.trim(),
|
|
731
|
-
normalizedAlertsEnabled,
|
|
732
|
-
normalizedApnsEnvironment
|
|
733
|
-
);
|
|
734
|
-
if (result.ok) {
|
|
735
|
-
sendResponse(ws, id, { status: 'registered', serverId: identity.serverId });
|
|
736
|
-
} else {
|
|
737
|
-
sendErrorResponse(ws, id, {
|
|
738
|
-
type: 'error', code: 'PUSH_REGISTER_FAILED',
|
|
739
|
-
error: result.error || 'Failed to register with push service',
|
|
740
|
-
retryable: true
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
} catch (err) {
|
|
744
|
-
console.error(`[push] register_push error: ${err.message}`);
|
|
745
|
-
sendErrorResponse(ws, id, {
|
|
746
|
-
type: 'error', code: 'INTERNAL_ERROR',
|
|
747
|
-
error: 'Failed to register with push service',
|
|
748
|
-
retryable: true
|
|
749
|
-
});
|
|
750
|
-
}
|
|
751
|
-
break;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
case 'unregister_push': {
|
|
755
|
-
const { deviceToken: unregToken } = params;
|
|
756
|
-
if (!unregToken || typeof unregToken !== 'string' || unregToken.trim() === '') {
|
|
757
|
-
sendErrorResponse(ws, id, {
|
|
758
|
-
type: 'error', code: 'INVALID_PARAMS',
|
|
759
|
-
error: 'deviceToken is required',
|
|
760
|
-
retryable: false
|
|
761
|
-
});
|
|
762
|
-
break;
|
|
763
|
-
}
|
|
764
|
-
if (!pushClient.isConfigured()) {
|
|
765
|
-
sendResponse(ws, id, { status: 'ok' });
|
|
766
|
-
break;
|
|
767
|
-
}
|
|
768
|
-
const unregIdentity = server.serverIdentity;
|
|
769
|
-
if (unregIdentity && unregIdentity.serverId && unregIdentity.serverSecret) {
|
|
770
|
-
pushClient.unregister(
|
|
771
|
-
unregIdentity.serverId,
|
|
772
|
-
unregIdentity.serverSecret,
|
|
773
|
-
unregToken.trim()
|
|
774
|
-
).catch((err) => {
|
|
775
|
-
console.error(`[push] unregister error: ${err.message}`);
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
sendResponse(ws, id, { status: 'ok' });
|
|
779
|
-
break;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
default:
|
|
783
|
-
sendErrorResponse(ws, id, {
|
|
784
|
-
type: 'error',
|
|
785
|
-
code: 'INVALID_REQUEST',
|
|
786
|
-
error: `Unknown method: ${method}`,
|
|
787
|
-
retryable: false
|
|
788
|
-
});
|
|
789
|
-
}
|
|
790
|
-
} catch (err) {
|
|
791
|
-
console.error(`Request error [${id}]:`, err);
|
|
792
|
-
sendErrorResponse(ws, id, {
|
|
793
|
-
type: 'error',
|
|
794
|
-
code: 'INTERNAL_ERROR',
|
|
795
|
-
error: err.message,
|
|
796
|
-
retryable: true
|
|
797
|
-
});
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
/**
|
|
802
|
-
* Handle legacy protocol messages (backward compatibility)
|
|
803
|
-
*/
|
|
804
|
-
async function handleLegacyMessage(ws, message) {
|
|
805
|
-
try {
|
|
806
|
-
switch (message.type) {
|
|
807
|
-
case 'send_to_claude': {
|
|
808
|
-
// Safety guard: reject terminal steer when a structured session has a pending approval
|
|
809
|
-
if (claudeSessionManager.hasAnyPendingApproval()) {
|
|
810
|
-
console.warn('[send_to_claude] Blocked: a structured session has a pending tool approval — refusing terminal send to prevent accidental y/n');
|
|
811
|
-
sendToClient(ws, { type: 'error', error: 'Cannot send to terminal while a tool approval is pending in a structured session' });
|
|
812
|
-
break;
|
|
813
|
-
}
|
|
814
|
-
const result = await tmux.sendToClaude(message.content, message.session, getClientPath(ws), message.cols, message.rows, message.command);
|
|
815
|
-
if (result.success) {
|
|
816
|
-
sendToClient(ws, { type: 'status', status: 'sent' });
|
|
817
|
-
startOutputMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
818
|
-
} else {
|
|
819
|
-
sendToClient(ws, result.error);
|
|
820
|
-
}
|
|
821
|
-
break;
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
case 'change_path': {
|
|
825
|
-
const result = await withSensitivePathAuth(ws, message.path, 'change_path',
|
|
826
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
827
|
-
async () => fileService.changePath(message.path, server.allowedBasePath, getClientPath(ws))
|
|
828
|
-
);
|
|
829
|
-
if (result.success) {
|
|
830
|
-
setClientPath(ws, result.path);
|
|
831
|
-
sendToClient(ws, { type: 'path_changed', path: result.path });
|
|
832
|
-
console.log(`📂 Changed path to: ${result.path} (client: ${ws._clientId})`);
|
|
833
|
-
} else {
|
|
834
|
-
sendToClient(ws, result.error);
|
|
835
|
-
}
|
|
836
|
-
break;
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
case 'start_claude': {
|
|
840
|
-
const result = await tmux.startClaude(message.session, message.command, getClientPath(ws), message.cols, message.rows);
|
|
841
|
-
if (result.success) {
|
|
842
|
-
sendToClient(ws, { type: 'status', status: 'running' });
|
|
843
|
-
startOutputMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
844
|
-
} else {
|
|
845
|
-
sendToClient(ws, result.error);
|
|
846
|
-
}
|
|
847
|
-
break;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
case 'stop_claude': {
|
|
851
|
-
const result = await tmux.stopClaude(message.session);
|
|
852
|
-
stopMonitor(message.session || tmux.DEFAULT_SESSION);
|
|
853
|
-
if (result.success) {
|
|
854
|
-
sendToClient(ws, { type: 'status', status: 'stopped' });
|
|
855
|
-
} else {
|
|
856
|
-
sendToClient(ws, result.error);
|
|
857
|
-
}
|
|
858
|
-
break;
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
case 'list_sessions': {
|
|
862
|
-
const result = await tmux.listSessions();
|
|
863
|
-
const sessions = result.sessions.map(s => ({
|
|
864
|
-
...s,
|
|
865
|
-
managed: server.managedSessions.has(s.name)
|
|
866
|
-
}));
|
|
867
|
-
sendToClient(ws, { type: 'sessions', sessions });
|
|
868
|
-
break;
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
case 'attach_session': {
|
|
872
|
-
// Remove this client from monitors for other sessions
|
|
873
|
-
const legacyClientSessions = clientMonitors.get(ws);
|
|
874
|
-
if (legacyClientSessions) {
|
|
875
|
-
for (const name of [...legacyClientSessions]) {
|
|
876
|
-
if (name !== message.session) {
|
|
877
|
-
removeClientFromMonitor(ws, name);
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
const result = await tmux.attachSession(message.session);
|
|
883
|
-
if (result.success) {
|
|
884
|
-
updateClientState(ws, { currentSession: normalizeClientSession(message.session) });
|
|
885
|
-
if (result.path) setClientPath(ws, result.path);
|
|
886
|
-
sendToClient(ws, { type: 'path_changed', path: getClientPath(ws) });
|
|
887
|
-
console.log(`📎 Attached to session: ${message.session} at ${getClientPath(ws)}`);
|
|
888
|
-
|
|
889
|
-
if (result.output && result.output.success) {
|
|
890
|
-
sendToClient(ws, {
|
|
891
|
-
type: 'claude_output',
|
|
892
|
-
content: result.output.content,
|
|
893
|
-
session: message.session
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
sendToClient(ws, {
|
|
898
|
-
type: 'status',
|
|
899
|
-
status: result.isRunning ? 'running' : 'stopped'
|
|
900
|
-
});
|
|
901
|
-
|
|
902
|
-
if (result.isRunning) {
|
|
903
|
-
startOutputMonitor(ws, message.session);
|
|
904
|
-
}
|
|
905
|
-
} else {
|
|
906
|
-
sendToClient(ws, result.error);
|
|
907
|
-
}
|
|
908
|
-
break;
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
case 'client_state': {
|
|
912
|
-
updateClientState(ws, {
|
|
913
|
-
foreground: message.foreground === true,
|
|
914
|
-
currentSession: normalizeClientSession(message.currentSession),
|
|
915
|
-
supportsClientState: true
|
|
916
|
-
});
|
|
917
|
-
sendToClient(ws, { type: 'status', status: 'ok' });
|
|
918
|
-
break;
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
case 'create_session': {
|
|
922
|
-
const result = await withSensitivePathAuth(ws, message.path || getClientPath(ws), 'create_session',
|
|
923
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
924
|
-
async () => tmux.createSession(message.content, message.path, server.allowedBasePath, getClientPath(ws))
|
|
925
|
-
);
|
|
926
|
-
if (result.success) {
|
|
927
|
-
server.managedSessions.add(message.content);
|
|
928
|
-
const expandedPath = result.path;
|
|
929
|
-
setClientPath(ws, expandedPath);
|
|
930
|
-
sendToClient(ws, { type: 'path_changed', path: expandedPath });
|
|
931
|
-
const listResult = await tmux.listSessions();
|
|
932
|
-
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
933
|
-
} else {
|
|
934
|
-
sendToClient(ws, result.error);
|
|
935
|
-
}
|
|
936
|
-
break;
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
case 'get_output': {
|
|
940
|
-
const result = await tmux.getSessionOutput(message.session);
|
|
941
|
-
if (result.success) {
|
|
942
|
-
sendToClient(ws, {
|
|
943
|
-
type: 'claude_output',
|
|
944
|
-
content: result.content,
|
|
945
|
-
userQuestion: result.userQuestion,
|
|
946
|
-
claudeResponse: result.claudeResponse,
|
|
947
|
-
session: message.session
|
|
948
|
-
});
|
|
949
|
-
} else {
|
|
950
|
-
sendToClient(ws, result.error);
|
|
951
|
-
}
|
|
952
|
-
break;
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
case 'subscribe_output': {
|
|
956
|
-
const session = message.session || tmux.DEFAULT_SESSION;
|
|
957
|
-
const result = await tmux.getSessionOutput(session);
|
|
958
|
-
if (result.success) {
|
|
959
|
-
sendToClient(ws, {
|
|
960
|
-
type: 'claude_output',
|
|
961
|
-
content: result.content,
|
|
962
|
-
session: session
|
|
963
|
-
});
|
|
964
|
-
}
|
|
965
|
-
startOutputMonitor(ws, session);
|
|
966
|
-
break;
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
case 'unsubscribe_output': {
|
|
970
|
-
removeClientFromMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
971
|
-
break;
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
case 'send_key': {
|
|
975
|
-
const result = await tmux.sendKeyToSession(message.content, message.session);
|
|
976
|
-
if (result.success) {
|
|
977
|
-
sendToClient(ws, { type: 'status', status: 'key_sent' });
|
|
978
|
-
setTimeout(() => {
|
|
979
|
-
captureAndBroadcast(message.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
980
|
-
}, 300);
|
|
981
|
-
} else {
|
|
982
|
-
sendToClient(ws, result.error);
|
|
983
|
-
}
|
|
984
|
-
break;
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
case 'delete_session': {
|
|
988
|
-
const result = await tmux.deleteSession(message.session);
|
|
989
|
-
stopMonitor(message.session);
|
|
990
|
-
if (result.success) {
|
|
991
|
-
server.managedSessions.delete(message.session);
|
|
992
|
-
sendToClient(ws, { type: 'session_deleted', session: message.session });
|
|
993
|
-
const listResult = await tmux.listSessions();
|
|
994
|
-
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
995
|
-
} else {
|
|
996
|
-
sendToClient(ws, result.error);
|
|
997
|
-
}
|
|
998
|
-
break;
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
case 'kill_session': {
|
|
1002
|
-
const result = await tmux.killSession(message.session);
|
|
1003
|
-
stopMonitor(message.session);
|
|
1004
|
-
if (result.success) {
|
|
1005
|
-
server.managedSessions.delete(message.session);
|
|
1006
|
-
sendToClient(ws, { type: 'session_killed', session: message.session });
|
|
1007
|
-
const listResult = await tmux.listSessions();
|
|
1008
|
-
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
1009
|
-
} else {
|
|
1010
|
-
sendToClient(ws, result.error);
|
|
1011
|
-
}
|
|
1012
|
-
break;
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
case 'get_full_output': {
|
|
1016
|
-
const result = await tmux.getFullSessionOutput(message.session, message.lines);
|
|
1017
|
-
if (result.success) {
|
|
1018
|
-
sendToClient(ws, {
|
|
1019
|
-
type: 'full_output',
|
|
1020
|
-
content: result.content,
|
|
1021
|
-
session: message.session,
|
|
1022
|
-
lines: message.lines
|
|
1023
|
-
});
|
|
1024
|
-
} else {
|
|
1025
|
-
sendToClient(ws, result.error);
|
|
1026
|
-
}
|
|
1027
|
-
break;
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
case 'create_folder': {
|
|
1031
|
-
const result = await withSensitivePathAuth(ws, message.path, 'create_folder',
|
|
1032
|
-
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
1033
|
-
async () => fileService.createFolder(message.path, server.allowedBasePath, getClientPath(ws))
|
|
1034
|
-
);
|
|
1035
|
-
if (result.success) {
|
|
1036
|
-
sendToClient(ws, { type: 'folder_created', path: result.path });
|
|
1037
|
-
console.log(`📁 Created folder: ${result.path}`);
|
|
1038
|
-
} else {
|
|
1039
|
-
sendToClient(ws, result.error);
|
|
1040
|
-
}
|
|
1041
|
-
break;
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1044
|
-
case 'list_directory': {
|
|
1045
|
-
const result = await withSensitivePathAuth(ws, message.path, 'list_directory',
|
|
1046
|
-
{ allowBrowse: true, allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
1047
|
-
async () => fileService.listDirectory(message.path, server.allowedBasePath, getClientPath(ws))
|
|
1048
|
-
);
|
|
1049
|
-
if (result.success) {
|
|
1050
|
-
sendToClient(ws, {
|
|
1051
|
-
type: 'directory_list',
|
|
1052
|
-
path: result.path,
|
|
1053
|
-
items: result.items
|
|
1054
|
-
});
|
|
1055
|
-
console.log(`📂 Listed directory: ${result.path} (${result.items.length} items)`);
|
|
1056
|
-
} else {
|
|
1057
|
-
sendToClient(ws, result.error);
|
|
1058
|
-
}
|
|
1059
|
-
break;
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
default:
|
|
1063
|
-
console.log(`Unknown message type: ${message.type}`);
|
|
1064
|
-
}
|
|
1065
|
-
} catch (err) {
|
|
1066
|
-
console.error(`Legacy message error [${message.type}]:`, err);
|
|
1067
|
-
sendToClient(ws, {
|
|
1068
|
-
type: 'error',
|
|
1069
|
-
error: err.message || 'Internal server error'
|
|
1070
|
-
});
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
return {
|
|
1075
|
-
sendToClient,
|
|
1076
|
-
broadcast,
|
|
1077
|
-
broadcastEvent,
|
|
1078
|
-
sendEvent,
|
|
1079
|
-
sendResponse,
|
|
1080
|
-
sendErrorResponse,
|
|
1081
|
-
handleMessage,
|
|
1082
|
-
startOutputMonitor,
|
|
1083
|
-
stopMonitor,
|
|
1084
|
-
removeClientFromMonitor,
|
|
1085
|
-
cleanupClient,
|
|
1086
|
-
captureAndBroadcast,
|
|
1087
|
-
getClientPath,
|
|
1088
|
-
setClientPath,
|
|
1089
|
-
outputMonitors,
|
|
1090
|
-
claudeSessionManager
|
|
1091
|
-
};
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
module.exports = { createWsHandler };
|