@yu_robotics/remote-cli 1.0.0
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 +21 -0
- package/README.md +94 -0
- package/bin/remote-cli.js +2 -0
- package/dist/client/MessageHandler.d.ts +92 -0
- package/dist/client/MessageHandler.d.ts.map +1 -0
- package/dist/client/MessageHandler.js +496 -0
- package/dist/client/MessageHandler.js.map +1 -0
- package/dist/client/WebSocketClient.d.ts +109 -0
- package/dist/client/WebSocketClient.d.ts.map +1 -0
- package/dist/client/WebSocketClient.js +234 -0
- package/dist/client/WebSocketClient.js.map +1 -0
- package/dist/commands/config.d.ts +35 -0
- package/dist/commands/config.d.ts.map +1 -0
- package/dist/commands/config.js +195 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/init.d.ts +25 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +112 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/start.d.ts +20 -0
- package/dist/commands/start.d.ts.map +1 -0
- package/dist/commands/start.js +108 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/status.d.ts +37 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +71 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/stop.d.ts +23 -0
- package/dist/commands/stop.d.ts.map +1 -0
- package/dist/commands/stop.js +52 -0
- package/dist/commands/stop.js.map +1 -0
- package/dist/config/ConfigManager.d.ts +109 -0
- package/dist/config/ConfigManager.d.ts.map +1 -0
- package/dist/config/ConfigManager.js +262 -0
- package/dist/config/ConfigManager.js.map +1 -0
- package/dist/executor/ClaudeExecutor.d.ts +89 -0
- package/dist/executor/ClaudeExecutor.d.ts.map +1 -0
- package/dist/executor/ClaudeExecutor.js +365 -0
- package/dist/executor/ClaudeExecutor.js.map +1 -0
- package/dist/executor/ClaudePersistentExecutor.d.ts +175 -0
- package/dist/executor/ClaudePersistentExecutor.d.ts.map +1 -0
- package/dist/executor/ClaudePersistentExecutor.js +958 -0
- package/dist/executor/ClaudePersistentExecutor.js.map +1 -0
- package/dist/executor/index.d.ts +20 -0
- package/dist/executor/index.d.ts.map +1 -0
- package/dist/executor/index.js +48 -0
- package/dist/executor/index.js.map +1 -0
- package/dist/hooks/ClaudeCodeHooks.d.ts +281 -0
- package/dist/hooks/ClaudeCodeHooks.d.ts.map +1 -0
- package/dist/hooks/ClaudeCodeHooks.js +350 -0
- package/dist/hooks/ClaudeCodeHooks.js.map +1 -0
- package/dist/hooks/FeishuNotificationAdapter.d.ts +87 -0
- package/dist/hooks/FeishuNotificationAdapter.d.ts.map +1 -0
- package/dist/hooks/FeishuNotificationAdapter.js +280 -0
- package/dist/hooks/FeishuNotificationAdapter.js.map +1 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +10 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +333 -0
- package/dist/index.js.map +1 -0
- package/dist/security/DirectoryGuard.d.ts +54 -0
- package/dist/security/DirectoryGuard.d.ts.map +1 -0
- package/dist/security/DirectoryGuard.js +143 -0
- package/dist/security/DirectoryGuard.js.map +1 -0
- package/dist/types/config.d.ts +46 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +22 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/index.d.ts +110 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/FeishuMessageFormatter.d.ts +84 -0
- package/dist/utils/FeishuMessageFormatter.d.ts.map +1 -0
- package/dist/utils/FeishuMessageFormatter.js +395 -0
- package/dist/utils/FeishuMessageFormatter.js.map +1 -0
- package/dist/utils/stripAnsi.d.ts +21 -0
- package/dist/utils/stripAnsi.d.ts.map +1 -0
- package/dist/utils/stripAnsi.js +30 -0
- package/dist/utils/stripAnsi.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageHandler = void 0;
|
|
4
|
+
const hooks_1 = require("../hooks");
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
|
+
/**
|
|
7
|
+
* Message Handler
|
|
8
|
+
* Responsible for handling messages from WebSocket and invoking Claude executor
|
|
9
|
+
*/
|
|
10
|
+
class MessageHandler {
|
|
11
|
+
wsClient;
|
|
12
|
+
executor;
|
|
13
|
+
directoryGuard;
|
|
14
|
+
isDestroyed = false;
|
|
15
|
+
isExecuting = false;
|
|
16
|
+
currentOpenId;
|
|
17
|
+
notificationAdapter;
|
|
18
|
+
constructor(wsClient, executor, directoryGuard) {
|
|
19
|
+
this.wsClient = wsClient;
|
|
20
|
+
this.executor = executor;
|
|
21
|
+
this.directoryGuard = directoryGuard;
|
|
22
|
+
// Initialize Feishu notification adapter
|
|
23
|
+
this.notificationAdapter = new hooks_1.FeishuNotificationAdapter(wsClient);
|
|
24
|
+
this.notificationAdapter.register();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Handle message (supports new IncomingMessage format)
|
|
28
|
+
* @param message Message object
|
|
29
|
+
*/
|
|
30
|
+
async handleMessage(message) {
|
|
31
|
+
// Check if already destroyed
|
|
32
|
+
if (this.isDestroyed) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Validate message structure
|
|
36
|
+
if (!message || !this.isValidMessage(message)) {
|
|
37
|
+
this.sendResponse(message?.messageId || 'unknown', {
|
|
38
|
+
success: false,
|
|
39
|
+
error: 'Invalid message format',
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// Handle different types of messages
|
|
44
|
+
switch (message.type) {
|
|
45
|
+
case 'status':
|
|
46
|
+
await this.handleStatusQuery(message.messageId);
|
|
47
|
+
return;
|
|
48
|
+
case 'command':
|
|
49
|
+
await this.handleCommandMessage(message);
|
|
50
|
+
return;
|
|
51
|
+
case 'heartbeat':
|
|
52
|
+
// Silently ignore heartbeat responses from server
|
|
53
|
+
return;
|
|
54
|
+
case 'binding_confirm':
|
|
55
|
+
// Silently ignore binding confirmation from server
|
|
56
|
+
return;
|
|
57
|
+
default:
|
|
58
|
+
this.sendResponse(message.messageId, {
|
|
59
|
+
success: false,
|
|
60
|
+
error: `Unknown message type: ${message.type}`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Handle command message
|
|
66
|
+
*/
|
|
67
|
+
async handleCommandMessage(message) {
|
|
68
|
+
const { messageId, content, workingDirectory, openId, isSlashCommand } = message;
|
|
69
|
+
// Store openId for response routing and notifications
|
|
70
|
+
this.currentOpenId = openId;
|
|
71
|
+
this.notificationAdapter.setCurrentOpenId(openId);
|
|
72
|
+
// Handle /abort command first, even when busy
|
|
73
|
+
if (content?.trim() === '/abort') {
|
|
74
|
+
await this.handleAbortCommand(messageId);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// Check if executor is waiting for interactive input
|
|
78
|
+
if ('isWaitingInput' in this.executor && typeof this.executor.isWaitingInput === 'function') {
|
|
79
|
+
const executor = this.executor;
|
|
80
|
+
if (executor.isWaitingInput()) {
|
|
81
|
+
const input = content?.trim();
|
|
82
|
+
if (input) {
|
|
83
|
+
const sent = executor.sendInput(input);
|
|
84
|
+
if (sent) {
|
|
85
|
+
this.sendResponse(messageId, {
|
|
86
|
+
success: true,
|
|
87
|
+
output: `✅ Sent: "${input}"`,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
this.sendResponse(messageId, {
|
|
92
|
+
success: false,
|
|
93
|
+
error: '❌ Failed to send input - executor is no longer waiting',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.sendResponse(messageId, {
|
|
99
|
+
success: false,
|
|
100
|
+
error: '❌ Please provide a non-empty input',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Check if there is a task currently executing
|
|
107
|
+
if (this.isExecuting) {
|
|
108
|
+
this.sendResponse(messageId, {
|
|
109
|
+
success: false,
|
|
110
|
+
error: 'Executor is busy, please wait for current task to complete',
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
// If working directory is provided, validate and set it
|
|
115
|
+
if (workingDirectory) {
|
|
116
|
+
// Verify directory is in the whitelist
|
|
117
|
+
if (!this.directoryGuard.isSafePath(workingDirectory)) {
|
|
118
|
+
this.sendResponse(messageId, {
|
|
119
|
+
success: false,
|
|
120
|
+
error: `Directory not in whitelist: ${workingDirectory}\n\nAllowed directories:\n${this.directoryGuard
|
|
121
|
+
.getAllowedDirectories()
|
|
122
|
+
.map((d) => `• ${d}`)
|
|
123
|
+
.join('\n')}`,
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// Set working directory
|
|
128
|
+
this.executor.setWorkingDirectory(workingDirectory);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
this.isExecuting = true;
|
|
132
|
+
// Handle built-in commands (except /abort which was handled above)
|
|
133
|
+
const builtInResult = await this.handleBuiltInCommand(messageId, content);
|
|
134
|
+
if (builtInResult) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// Check if this is a passthrough slash command from server
|
|
138
|
+
if (isSlashCommand) {
|
|
139
|
+
console.log(`[MessageHandler] Executing passthrough slash command: ${content}`);
|
|
140
|
+
await this.executeSlashCommand(messageId, content);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Expand command shortcuts
|
|
144
|
+
const expandedContent = this.expandCommandShortcuts(content);
|
|
145
|
+
// Execute Claude command
|
|
146
|
+
await this.executeCommand(messageId, expandedContent);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
this.sendResponse(messageId, {
|
|
150
|
+
success: false,
|
|
151
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
this.isExecuting = false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Handle abort command
|
|
160
|
+
* Can be executed even when executor is busy
|
|
161
|
+
*/
|
|
162
|
+
async handleAbortCommand(messageId) {
|
|
163
|
+
const wasExecuting = this.isExecuting;
|
|
164
|
+
const aborted = await this.executor.abort();
|
|
165
|
+
if (aborted) {
|
|
166
|
+
this.isExecuting = false;
|
|
167
|
+
this.sendResponse(messageId, {
|
|
168
|
+
success: true,
|
|
169
|
+
output: wasExecuting
|
|
170
|
+
? '✅ Current command has been aborted'
|
|
171
|
+
: '⚠️ No command was executing, but executor has been reset',
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.sendResponse(messageId, {
|
|
176
|
+
success: true,
|
|
177
|
+
output: 'ℹ️ No command is currently executing',
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Handle status query
|
|
183
|
+
*/
|
|
184
|
+
async handleStatusQuery(messageId) {
|
|
185
|
+
this.wsClient.send({
|
|
186
|
+
type: 'status',
|
|
187
|
+
messageId,
|
|
188
|
+
status: {
|
|
189
|
+
connected: this.wsClient.isConnected(),
|
|
190
|
+
allowedDirectories: this.directoryGuard.getAllowedDirectories(),
|
|
191
|
+
currentWorkingDirectory: this.executor.getCurrentWorkingDirectory(),
|
|
192
|
+
},
|
|
193
|
+
timestamp: Date.now(),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Validate message structure
|
|
198
|
+
*/
|
|
199
|
+
isValidMessage(message) {
|
|
200
|
+
if (!message || typeof message !== 'object') {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (message.type !== 'command') {
|
|
204
|
+
return true; // Non-command messages don't need further validation
|
|
205
|
+
}
|
|
206
|
+
return Boolean(message.messageId && message.content);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Handle built-in commands
|
|
210
|
+
* @returns Returns true if built-in command was handled, otherwise false
|
|
211
|
+
*/
|
|
212
|
+
async handleBuiltInCommand(messageId, content) {
|
|
213
|
+
const trimmed = content.trim();
|
|
214
|
+
// /status command
|
|
215
|
+
if (trimmed === '/status') {
|
|
216
|
+
const cwd = this.executor.getCurrentWorkingDirectory();
|
|
217
|
+
const allowedDirs = this.directoryGuard.getAllowedDirectories();
|
|
218
|
+
this.sendResponse(messageId, {
|
|
219
|
+
success: true,
|
|
220
|
+
output: `📊 Status:
|
|
221
|
+
- Working Directory: ${cwd}
|
|
222
|
+
- Allowed Directories: ${allowedDirs.join(', ')}
|
|
223
|
+
- Connection: Active`,
|
|
224
|
+
});
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
// /help command
|
|
228
|
+
if (trimmed === '/help') {
|
|
229
|
+
this.sendResponse(messageId, {
|
|
230
|
+
success: true,
|
|
231
|
+
output: `📖 Available commands:
|
|
232
|
+
- /help - Show this help message
|
|
233
|
+
- /status - Show current status
|
|
234
|
+
- /abort - Abort the currently executing command
|
|
235
|
+
- /clear - Clear conversation context
|
|
236
|
+
- /cd <directory> - Change working directory
|
|
237
|
+
- /r or /resume - Resume previous conversation
|
|
238
|
+
- /c or /continue - Continue previous conversation
|
|
239
|
+
- /review - Review changes (supports remote interaction)
|
|
240
|
+
|
|
241
|
+
💡 Interactive commands like /review will prompt you for input via Feishu when needed.
|
|
242
|
+
|
|
243
|
+
You can also use natural language commands to control Claude Code CLI.`,
|
|
244
|
+
});
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
// /clear command
|
|
248
|
+
if (trimmed === '/clear') {
|
|
249
|
+
this.executor.resetContext();
|
|
250
|
+
this.sendResponse(messageId, {
|
|
251
|
+
success: true,
|
|
252
|
+
output: '✅ Conversation context cleared',
|
|
253
|
+
});
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
// /cd command
|
|
257
|
+
if (trimmed.startsWith('/cd')) {
|
|
258
|
+
const parts = trimmed.split(/\s+/);
|
|
259
|
+
if (parts.length < 2) {
|
|
260
|
+
this.sendResponse(messageId, {
|
|
261
|
+
success: false,
|
|
262
|
+
error: 'Usage: /cd <directory>',
|
|
263
|
+
});
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
const targetDir = parts.slice(1).join(' ');
|
|
267
|
+
try {
|
|
268
|
+
this.executor.setWorkingDirectory(targetDir);
|
|
269
|
+
const newCwd = this.executor.getCurrentWorkingDirectory();
|
|
270
|
+
this.sendResponse(messageId, {
|
|
271
|
+
success: true,
|
|
272
|
+
output: `✅ Changed working directory to: ${newCwd}`,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
this.sendResponse(messageId, {
|
|
277
|
+
success: false,
|
|
278
|
+
error: error instanceof Error
|
|
279
|
+
? error.message
|
|
280
|
+
: 'Failed to change directory',
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Expand command shortcuts
|
|
289
|
+
*/
|
|
290
|
+
expandCommandShortcuts(content) {
|
|
291
|
+
const trimmed = content.trim();
|
|
292
|
+
// Only expand when command is the entire content
|
|
293
|
+
if (trimmed === '/r' || trimmed === '/resume') {
|
|
294
|
+
return 'Please resume the previous conversation';
|
|
295
|
+
}
|
|
296
|
+
if (trimmed === '/c' || trimmed === '/continue') {
|
|
297
|
+
return 'Please continue from where we left off';
|
|
298
|
+
}
|
|
299
|
+
return content;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Execute passthrough slash command using local Claude CLI
|
|
303
|
+
* This allows users to use their custom slash commands
|
|
304
|
+
*/
|
|
305
|
+
async executeSlashCommand(messageId, command) {
|
|
306
|
+
return new Promise((resolve) => {
|
|
307
|
+
const chunks = [];
|
|
308
|
+
const errorChunks = [];
|
|
309
|
+
console.log(`[MessageHandler] Spawning Claude CLI for command: ${command}`);
|
|
310
|
+
// Spawn Claude CLI with the slash command
|
|
311
|
+
// Use --print to get output and exit
|
|
312
|
+
const child = (0, child_process_1.spawn)('claude', [command, '--print'], {
|
|
313
|
+
cwd: this.executor.getCurrentWorkingDirectory(),
|
|
314
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
315
|
+
env: {
|
|
316
|
+
...process.env,
|
|
317
|
+
CLAUDECODE: '', // Prevent nested session error
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
// Handle stdout (stream chunks)
|
|
321
|
+
child.stdout?.on('data', (data) => {
|
|
322
|
+
const chunk = data.toString();
|
|
323
|
+
chunks.push(chunk);
|
|
324
|
+
this.sendStreamChunk(messageId, chunk);
|
|
325
|
+
});
|
|
326
|
+
// Handle stderr
|
|
327
|
+
child.stderr?.on('data', (data) => {
|
|
328
|
+
const chunk = data.toString();
|
|
329
|
+
errorChunks.push(chunk);
|
|
330
|
+
console.error(`[MessageHandler] Claude stderr: ${chunk}`);
|
|
331
|
+
});
|
|
332
|
+
// Handle process exit
|
|
333
|
+
child.on('exit', (code) => {
|
|
334
|
+
console.log(`[MessageHandler] Claude process exited with code: ${code}`);
|
|
335
|
+
if (code === 0) {
|
|
336
|
+
const output = chunks.join('');
|
|
337
|
+
this.sendResponse(messageId, {
|
|
338
|
+
success: true,
|
|
339
|
+
output: output.trim() || '✅ Command executed successfully',
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
const errorOutput = errorChunks.join('') || chunks.join('');
|
|
344
|
+
this.sendResponse(messageId, {
|
|
345
|
+
success: false,
|
|
346
|
+
error: errorOutput.trim() || `Command failed with exit code ${code}`,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
resolve();
|
|
350
|
+
});
|
|
351
|
+
// Handle process error
|
|
352
|
+
child.on('error', (error) => {
|
|
353
|
+
console.error(`[MessageHandler] Failed to spawn Claude:`, error);
|
|
354
|
+
this.sendResponse(messageId, {
|
|
355
|
+
success: false,
|
|
356
|
+
error: `Failed to execute command: ${error.message}`,
|
|
357
|
+
});
|
|
358
|
+
resolve();
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Execute Claude command
|
|
364
|
+
*/
|
|
365
|
+
async executeCommand(messageId, content) {
|
|
366
|
+
try {
|
|
367
|
+
const result = await this.executor.execute(content, {
|
|
368
|
+
onStream: (chunk) => {
|
|
369
|
+
this.sendStreamChunk(messageId, chunk);
|
|
370
|
+
},
|
|
371
|
+
onToolUse: (toolUse) => {
|
|
372
|
+
this.sendToolUse(messageId, toolUse);
|
|
373
|
+
},
|
|
374
|
+
onToolResult: (toolResult) => {
|
|
375
|
+
this.sendToolResult(messageId, toolResult);
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
// Only send success status, not the output
|
|
379
|
+
// Output has already been streamed via onStream callback
|
|
380
|
+
this.sendResponse(messageId, {
|
|
381
|
+
success: result.success,
|
|
382
|
+
error: result.error,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
this.sendResponse(messageId, {
|
|
387
|
+
success: false,
|
|
388
|
+
error: error instanceof Error ? error.message : 'Execution error',
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Send streaming output chunk
|
|
394
|
+
*/
|
|
395
|
+
sendStreamChunk(messageId, chunk) {
|
|
396
|
+
try {
|
|
397
|
+
this.wsClient.send({
|
|
398
|
+
type: 'stream',
|
|
399
|
+
messageId,
|
|
400
|
+
chunk,
|
|
401
|
+
streamType: 'text',
|
|
402
|
+
openId: this.currentOpenId,
|
|
403
|
+
timestamp: Date.now(),
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
// Ignore send errors, don't affect main flow
|
|
408
|
+
console.error('Failed to send stream chunk:', error);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Send tool use event
|
|
413
|
+
*/
|
|
414
|
+
sendToolUse(messageId, toolUse) {
|
|
415
|
+
try {
|
|
416
|
+
this.wsClient.send({
|
|
417
|
+
type: 'stream',
|
|
418
|
+
messageId,
|
|
419
|
+
streamType: 'tool_use',
|
|
420
|
+
toolUse,
|
|
421
|
+
openId: this.currentOpenId,
|
|
422
|
+
timestamp: Date.now(),
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
console.error('Failed to send tool use:', error);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Send tool result event
|
|
431
|
+
*/
|
|
432
|
+
sendToolResult(messageId, toolResult) {
|
|
433
|
+
try {
|
|
434
|
+
this.wsClient.send({
|
|
435
|
+
type: 'stream',
|
|
436
|
+
messageId,
|
|
437
|
+
streamType: 'tool_result',
|
|
438
|
+
toolResult,
|
|
439
|
+
openId: this.currentOpenId,
|
|
440
|
+
timestamp: Date.now(),
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
console.error('Failed to send tool result:', error);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Send structured content for rich formatting
|
|
449
|
+
*/
|
|
450
|
+
sendStructuredContent(messageId, structuredContent) {
|
|
451
|
+
try {
|
|
452
|
+
this.wsClient.send({
|
|
453
|
+
type: 'structured',
|
|
454
|
+
messageId,
|
|
455
|
+
structuredContent,
|
|
456
|
+
openId: this.currentOpenId,
|
|
457
|
+
timestamp: Date.now(),
|
|
458
|
+
cwd: this.executor.getCurrentWorkingDirectory(),
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
console.error('Failed to send structured content:', error);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Send response
|
|
467
|
+
*/
|
|
468
|
+
sendResponse(messageId, result) {
|
|
469
|
+
try {
|
|
470
|
+
this.wsClient.send({
|
|
471
|
+
type: 'response',
|
|
472
|
+
messageId,
|
|
473
|
+
success: result.success,
|
|
474
|
+
output: result.output,
|
|
475
|
+
error: result.error,
|
|
476
|
+
sessionAbbr: result.sessionAbbr,
|
|
477
|
+
openId: this.currentOpenId,
|
|
478
|
+
timestamp: Date.now(),
|
|
479
|
+
cwd: this.executor.getCurrentWorkingDirectory(),
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
catch (error) {
|
|
483
|
+
console.error('Failed to send response:', error);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Destroy handler
|
|
488
|
+
*/
|
|
489
|
+
destroy() {
|
|
490
|
+
this.isDestroyed = true;
|
|
491
|
+
this.isExecuting = false;
|
|
492
|
+
this.notificationAdapter.unregister();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
exports.MessageHandler = MessageHandler;
|
|
496
|
+
//# sourceMappingURL=MessageHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MessageHandler.js","sourceRoot":"","sources":["../../src/client/MessageHandler.ts"],"names":[],"mappings":";;;AAIA,oCAAqD;AACrD,iDAAsC;AAYtC;;;GAGG;AACH,MAAa,cAAc;IACjB,QAAQ,CAAkB;IAC1B,QAAQ,CAA4C;IACpD,cAAc,CAAiB;IAC/B,WAAW,GAAG,KAAK,CAAC;IACpB,WAAW,GAAG,KAAK,CAAC;IACpB,aAAa,CAAU;IACvB,mBAAmB,CAA4B;IAEvD,YACE,QAAyB,EACzB,QAAmD,EACnD,cAA8B;QAE9B,IAAI,CAAC,QAAQ,GAA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client configuration
|
|
3
|
+
*/
|
|
4
|
+
export interface WebSocketClientOptions {
|
|
5
|
+
/** Reconnect interval (milliseconds), default 5000 */
|
|
6
|
+
reconnectInterval?: number;
|
|
7
|
+
/** Heartbeat interval (milliseconds), default 15000 */
|
|
8
|
+
heartbeatInterval?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Connection status
|
|
12
|
+
*/
|
|
13
|
+
export interface ConnectionStatus {
|
|
14
|
+
connected: boolean;
|
|
15
|
+
serverUrl: string;
|
|
16
|
+
deviceId: string;
|
|
17
|
+
lastHeartbeat?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* WebSocket Client
|
|
21
|
+
* Responsible for establishing and maintaining WebSocket connection with router server
|
|
22
|
+
*/
|
|
23
|
+
export declare class WebSocketClient {
|
|
24
|
+
private serverUrl;
|
|
25
|
+
private deviceId;
|
|
26
|
+
private ws;
|
|
27
|
+
private reconnectInterval;
|
|
28
|
+
private heartbeatInterval;
|
|
29
|
+
private reconnectTimer;
|
|
30
|
+
private heartbeatTimer;
|
|
31
|
+
private manualDisconnect;
|
|
32
|
+
private connected;
|
|
33
|
+
private messageHandlers;
|
|
34
|
+
private errorHandlers;
|
|
35
|
+
private closeHandlers;
|
|
36
|
+
private connectHandlers;
|
|
37
|
+
constructor(serverUrl: string, deviceId: string, options?: WebSocketClientOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Connect to server
|
|
40
|
+
*/
|
|
41
|
+
connect(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Disconnect
|
|
44
|
+
*/
|
|
45
|
+
disconnect(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Send message
|
|
48
|
+
* @param message Message object
|
|
49
|
+
*/
|
|
50
|
+
send(message: any): void;
|
|
51
|
+
/**
|
|
52
|
+
* Check if connected
|
|
53
|
+
*/
|
|
54
|
+
isConnected(): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Get connection status
|
|
57
|
+
*/
|
|
58
|
+
getStatus(): ConnectionStatus;
|
|
59
|
+
/**
|
|
60
|
+
* Register message handler
|
|
61
|
+
* @param handler Message handler function
|
|
62
|
+
*/
|
|
63
|
+
onMessage(handler: (message: any) => void): void;
|
|
64
|
+
/**
|
|
65
|
+
* Register error handler
|
|
66
|
+
* @param handler Error handler function
|
|
67
|
+
*/
|
|
68
|
+
onError(handler: (error: Error) => void): void;
|
|
69
|
+
/**
|
|
70
|
+
* Register close handler
|
|
71
|
+
* @param handler Close handler function
|
|
72
|
+
*/
|
|
73
|
+
onClose(handler: (code: number, reason: string) => void): void;
|
|
74
|
+
/**
|
|
75
|
+
* Register connect handler
|
|
76
|
+
* @param handler Connect handler function
|
|
77
|
+
*/
|
|
78
|
+
onConnect(handler: () => void): void;
|
|
79
|
+
/**
|
|
80
|
+
* Event emitter style: on method
|
|
81
|
+
* @param event Event name
|
|
82
|
+
* @param handler Event handler
|
|
83
|
+
*/
|
|
84
|
+
on(event: 'connected', handler: () => void): void;
|
|
85
|
+
on(event: 'disconnected', handler: () => void): void;
|
|
86
|
+
on(event: 'error', handler: (error: Error) => void): void;
|
|
87
|
+
on(event: 'message', handler: (message: any) => void): void;
|
|
88
|
+
/**
|
|
89
|
+
* Send device registration message
|
|
90
|
+
*/
|
|
91
|
+
private sendRegistration;
|
|
92
|
+
/**
|
|
93
|
+
* Start heartbeat
|
|
94
|
+
*/
|
|
95
|
+
private startHeartbeat;
|
|
96
|
+
/**
|
|
97
|
+
* Stop heartbeat
|
|
98
|
+
*/
|
|
99
|
+
private stopHeartbeat;
|
|
100
|
+
/**
|
|
101
|
+
* Schedule reconnection
|
|
102
|
+
*/
|
|
103
|
+
private scheduleReconnect;
|
|
104
|
+
/**
|
|
105
|
+
* Clear reconnect timer
|
|
106
|
+
*/
|
|
107
|
+
private clearReconnectTimer;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=WebSocketClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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