@lcap/wave-sandbox-sdk 0.0.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/README.md +1154 -0
- package/dist/client.d.ts +64 -0
- package/dist/client.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +7 -0
- package/dist/modules/agent.d.ts +448 -0
- package/dist/modules/agent.js +251 -0
- package/dist/modules/exec.d.ts +27 -0
- package/dist/modules/exec.js +35 -0
- package/dist/modules/file-system.d.ts +64 -0
- package/dist/modules/file-system.js +172 -0
- package/dist/modules/port.d.ts +30 -0
- package/dist/modules/port.js +41 -0
- package/dist/modules/project.d.ts +37 -0
- package/dist/modules/project.js +64 -0
- package/dist/types/agent-types.d.ts +127 -0
- package/dist/types/agent-types.js +5 -0
- package/dist/types/index.d.ts +105 -0
- package/dist/types/index.js +10 -0
- package/dist/utils/socket.d.ts +10 -0
- package/dist/utils/socket.js +27 -0
- package/package.json +34 -0
- package/src/client.ts +152 -0
- package/src/index.ts +60 -0
- package/src/modules/agent.ts +624 -0
- package/src/modules/exec.ts +49 -0
- package/src/modules/file-system.ts +212 -0
- package/src/modules/port.ts +47 -0
- package/src/modules/project.ts +77 -0
- package/src/types/agent-types.ts +140 -0
- package/src/types/index.ts +136 -0
- package/src/utils/socket.ts +34 -0
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import type { Socket } from 'socket.io-client';
|
|
2
|
+
import type {
|
|
3
|
+
TextBlock,
|
|
4
|
+
ToolBlock,
|
|
5
|
+
ErrorBlock,
|
|
6
|
+
SubagentBlock,
|
|
7
|
+
McpServerStatus,
|
|
8
|
+
AssistantMessage,
|
|
9
|
+
} from '../types/agent-types.js';
|
|
10
|
+
import type { HttpResponse } from '../types/index.js';
|
|
11
|
+
import { createSocketHandler, onSocketEvent } from '../utils/socket.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 附件接口
|
|
15
|
+
*/
|
|
16
|
+
export interface Attachment {
|
|
17
|
+
url?: string;
|
|
18
|
+
path?: string;
|
|
19
|
+
mimeType: string;
|
|
20
|
+
content?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 发送消息选项
|
|
25
|
+
*/
|
|
26
|
+
export interface SendMessageOptions {
|
|
27
|
+
id: string;
|
|
28
|
+
sessionId: string;
|
|
29
|
+
message: string;
|
|
30
|
+
attachments?: Attachment[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 消息队列项
|
|
35
|
+
*/
|
|
36
|
+
export interface ProcessQueueItem {
|
|
37
|
+
options: SendMessageOptions;
|
|
38
|
+
assistantMessageId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 消息队列变化事件数据
|
|
43
|
+
*/
|
|
44
|
+
export interface MessageQueueChangedEvent {
|
|
45
|
+
queue: ProcessQueueItem[];
|
|
46
|
+
current: {
|
|
47
|
+
sessionId: string;
|
|
48
|
+
assistantMessageId?: string;
|
|
49
|
+
senderMessageId: string;
|
|
50
|
+
message?: AssistantMessage; // 当前正在执行的消息, 初始化链接时会有值
|
|
51
|
+
} | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 消息发送事件数据
|
|
56
|
+
*/
|
|
57
|
+
export interface MessageSendEvent {
|
|
58
|
+
sessionId: string;
|
|
59
|
+
assistantMessageId: string;
|
|
60
|
+
senderMessageId: string;
|
|
61
|
+
options: SendMessageOptions;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 消息运行事件数据
|
|
66
|
+
*/
|
|
67
|
+
export interface MessageRunningEvent {
|
|
68
|
+
sessionId: string;
|
|
69
|
+
assistantMessageId: string;
|
|
70
|
+
senderMessageId: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 消息完成事件数据
|
|
75
|
+
*/
|
|
76
|
+
export interface MessageFinishedEvent {
|
|
77
|
+
sessionId: string;
|
|
78
|
+
assistantMessageId?: string;
|
|
79
|
+
senderMessageId: string;
|
|
80
|
+
status: 'pending' | 'running' | 'finished' | 'error' | 'aborted';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 初始化事件数据
|
|
85
|
+
*/
|
|
86
|
+
export interface InitializingEvent {
|
|
87
|
+
sessionId: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 初始化完成事件数据
|
|
92
|
+
*/
|
|
93
|
+
export interface InitializedEvent {
|
|
94
|
+
sessionId: string;
|
|
95
|
+
restoreSessionId?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 消息块添加事件数据
|
|
100
|
+
*/
|
|
101
|
+
export interface MessageBlockAddedEvent {
|
|
102
|
+
sessionId: string;
|
|
103
|
+
assistantMessageId?: string;
|
|
104
|
+
senderMessageId: string;
|
|
105
|
+
data: {
|
|
106
|
+
blocks: Array<TextBlock | ToolBlock | ErrorBlock | SubagentBlock>;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 消息块更新事件数据(用于流式更新)
|
|
112
|
+
*/
|
|
113
|
+
export interface MessageBlockUpdatedEvent {
|
|
114
|
+
sessionId: string;
|
|
115
|
+
assistantMessageId?: string;
|
|
116
|
+
senderMessageId: string;
|
|
117
|
+
data: {
|
|
118
|
+
type: 'text';
|
|
119
|
+
content: string;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 工具块更新事件数据
|
|
125
|
+
*/
|
|
126
|
+
export interface MessageToolBlockUpdatedEvent {
|
|
127
|
+
sessionId: string;
|
|
128
|
+
assistantMessageId?: string;
|
|
129
|
+
senderMessageId: string;
|
|
130
|
+
data: ToolBlock;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 子代理块添加事件数据
|
|
135
|
+
*/
|
|
136
|
+
export interface MessageSubagentBlockAddEvent {
|
|
137
|
+
sessionId: string;
|
|
138
|
+
assistantMessageId?: string;
|
|
139
|
+
senderMessageId: string;
|
|
140
|
+
data: {
|
|
141
|
+
subagentId: string;
|
|
142
|
+
blocks: Array<TextBlock | ToolBlock | ErrorBlock>;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 子代理块状态更新事件数据
|
|
148
|
+
*/
|
|
149
|
+
export interface MessageSubagentBlockStatusUpdatedEvent {
|
|
150
|
+
sessionId: string;
|
|
151
|
+
assistantMessageId?: string;
|
|
152
|
+
senderMessageId: string;
|
|
153
|
+
data: {
|
|
154
|
+
subagentId: string;
|
|
155
|
+
status: 'active' | 'completed' | 'error' | 'aborted';
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 子代理块内容更新事件数据
|
|
161
|
+
*/
|
|
162
|
+
export interface MessageSubagentBlockContentUpdatedEvent {
|
|
163
|
+
sessionId: string;
|
|
164
|
+
assistantMessageId?: string;
|
|
165
|
+
senderMessageId: string;
|
|
166
|
+
data: {
|
|
167
|
+
subagentId: string;
|
|
168
|
+
type: 'text';
|
|
169
|
+
content: string;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 子代理工具块更新事件数据
|
|
175
|
+
*/
|
|
176
|
+
export interface MessageSubagentToolBlockUpdatedEvent {
|
|
177
|
+
sessionId: string;
|
|
178
|
+
assistantMessageId?: string;
|
|
179
|
+
senderMessageId: string;
|
|
180
|
+
data: {
|
|
181
|
+
subagentId: string;
|
|
182
|
+
toolBlock: ToolBlock;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 错误块添加事件数据
|
|
188
|
+
*/
|
|
189
|
+
export interface MessageErrorBlockAddedEvent {
|
|
190
|
+
sessionId: string;
|
|
191
|
+
assistantMessageId?: string;
|
|
192
|
+
senderMessageId: string;
|
|
193
|
+
data: ErrorBlock;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 命令输出消息添加事件数据
|
|
198
|
+
*/
|
|
199
|
+
export interface MessageCommandOutputMessageAddedEvent {
|
|
200
|
+
sessionId: string;
|
|
201
|
+
assistantMessageId?: string;
|
|
202
|
+
senderMessageId: string;
|
|
203
|
+
data: {
|
|
204
|
+
command: string;
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 命令输出消息更新事件数据
|
|
210
|
+
*/
|
|
211
|
+
export interface MessageCommandOutputMessageUpdatedEvent {
|
|
212
|
+
sessionId: string;
|
|
213
|
+
assistantMessageId?: string;
|
|
214
|
+
senderMessageId: string;
|
|
215
|
+
data: {
|
|
216
|
+
command: string;
|
|
217
|
+
output: string;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 命令输出消息完成事件数据
|
|
223
|
+
*/
|
|
224
|
+
export interface MessageCommandOutputMessageCompletedEvent {
|
|
225
|
+
sessionId: string;
|
|
226
|
+
assistantMessageId?: string;
|
|
227
|
+
senderMessageId: string;
|
|
228
|
+
data: {
|
|
229
|
+
command: string;
|
|
230
|
+
exitCode: number;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* MCP 服务器变化事件数据
|
|
236
|
+
*/
|
|
237
|
+
export interface McpServersChangeEvent {
|
|
238
|
+
sessionId: string;
|
|
239
|
+
servers: McpServerStatus[];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 文档解析开始事件数据
|
|
244
|
+
*/
|
|
245
|
+
export interface DocumentParseStartEvent {
|
|
246
|
+
sessionId: string;
|
|
247
|
+
senderMessageId: string;
|
|
248
|
+
index: number;
|
|
249
|
+
url: string;
|
|
250
|
+
mimeType: string;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 文档解析完成事件数据
|
|
255
|
+
*/
|
|
256
|
+
export interface DocumentParseCompleteEvent {
|
|
257
|
+
sessionId: string;
|
|
258
|
+
senderMessageId: string;
|
|
259
|
+
index: number;
|
|
260
|
+
url: string;
|
|
261
|
+
mimeType: string;
|
|
262
|
+
outputPath: string;
|
|
263
|
+
contentLength?: number;
|
|
264
|
+
pageCount?: number;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 文档解析错误事件数据
|
|
269
|
+
*/
|
|
270
|
+
export interface DocumentParseErrorEvent {
|
|
271
|
+
sessionId: string;
|
|
272
|
+
senderMessageId: string;
|
|
273
|
+
index: number;
|
|
274
|
+
url: string;
|
|
275
|
+
mimeType: string;
|
|
276
|
+
error: string;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Agent 事件名称映射
|
|
281
|
+
*/
|
|
282
|
+
export type AgentEventMap = {
|
|
283
|
+
'agent:message-send': MessageSendEvent;
|
|
284
|
+
'agent:message-running': MessageRunningEvent;
|
|
285
|
+
'agent:message-finished': MessageFinishedEvent;
|
|
286
|
+
'agent:message-queue-changed': MessageQueueChangedEvent;
|
|
287
|
+
'agent:initializing': InitializingEvent;
|
|
288
|
+
'agent:initialized': InitializedEvent;
|
|
289
|
+
'agent:message-block-added': MessageBlockAddedEvent;
|
|
290
|
+
'agent:message-block-updated': MessageBlockUpdatedEvent;
|
|
291
|
+
'agent:message-tool-block-updated': MessageToolBlockUpdatedEvent;
|
|
292
|
+
'agent:message-subagent-block-add': MessageSubagentBlockAddEvent;
|
|
293
|
+
'agent:message-subagent-block-status-updated': MessageSubagentBlockStatusUpdatedEvent;
|
|
294
|
+
'agent:message-subagent-block-content-updated': MessageSubagentBlockContentUpdatedEvent;
|
|
295
|
+
'agent:message-subagent-tool-block-updated': MessageSubagentToolBlockUpdatedEvent;
|
|
296
|
+
'agent:message-error-block-added': MessageErrorBlockAddedEvent;
|
|
297
|
+
'agent:message-command-output-message-added': MessageCommandOutputMessageAddedEvent;
|
|
298
|
+
'agent:message-command-output-message-updated': MessageCommandOutputMessageUpdatedEvent;
|
|
299
|
+
'agent:message-command-output-message-completed': MessageCommandOutputMessageCompletedEvent;
|
|
300
|
+
'agent:mcp-servers-change': McpServersChangeEvent;
|
|
301
|
+
'agent:document-parse-start': DocumentParseStartEvent;
|
|
302
|
+
'agent:document-parse-complete': DocumentParseCompleteEvent;
|
|
303
|
+
'agent:document-parse-error': DocumentParseErrorEvent;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Agent 事件名称类型
|
|
308
|
+
*/
|
|
309
|
+
export type AgentEventName = keyof AgentEventMap;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Agent 模块
|
|
313
|
+
*/
|
|
314
|
+
export class AgentModule {
|
|
315
|
+
constructor(
|
|
316
|
+
private socket: Socket,
|
|
317
|
+
private baseUrl: string,
|
|
318
|
+
private projectId?: string
|
|
319
|
+
) {}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 获取查询参数
|
|
323
|
+
*/
|
|
324
|
+
private getQueryParams(): string {
|
|
325
|
+
if (this.projectId) {
|
|
326
|
+
return `projectId=${encodeURIComponent(this.projectId)}`;
|
|
327
|
+
}
|
|
328
|
+
return '';
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 发送消息
|
|
333
|
+
* @param options 发送消息选项
|
|
334
|
+
* @returns Promise<string> 返回 assistantMessageId
|
|
335
|
+
*/
|
|
336
|
+
async sendMessage(options: SendMessageOptions): Promise<string> {
|
|
337
|
+
const result = await createSocketHandler<SendMessageOptions, { assistantMessageId: string }>(
|
|
338
|
+
this.socket,
|
|
339
|
+
'agent:sendMessage',
|
|
340
|
+
() => options
|
|
341
|
+
);
|
|
342
|
+
return result.assistantMessageId;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 中止消息
|
|
347
|
+
* @param messageId 消息 ID
|
|
348
|
+
*/
|
|
349
|
+
async abortMessage(messageId: string): Promise<void> {
|
|
350
|
+
return createSocketHandler<{ messageId: string }, void>(this.socket, 'agent:abortMessage', () => ({
|
|
351
|
+
messageId,
|
|
352
|
+
}));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 删除 session,包括日志记录和 agent 对象
|
|
357
|
+
* @param sessionId 会话 ID
|
|
358
|
+
* @throws 如果当前正在处理该 session 的消息,则抛出错误
|
|
359
|
+
*/
|
|
360
|
+
async removeSession(sessionId: string): Promise<void> {
|
|
361
|
+
return createSocketHandler<{ sessionId: string }, void>(this.socket, 'agent:removeSession', () => ({
|
|
362
|
+
sessionId,
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 获取指定 session 的消息日志文件列表
|
|
368
|
+
* @param sessionId 会话 ID
|
|
369
|
+
* @returns Promise<string[] | null> 返回文件列表(倒序,最新的在前),如果没有文件则返回 null
|
|
370
|
+
*/
|
|
371
|
+
async getSessionFiles(sessionId: string): Promise<string[] | null> {
|
|
372
|
+
const result = await createSocketHandler<{ sessionId: string }, { files: string[] } | null>(
|
|
373
|
+
this.socket,
|
|
374
|
+
'agent:getSessionFiles',
|
|
375
|
+
() => ({ sessionId })
|
|
376
|
+
);
|
|
377
|
+
return result ? result.files : null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* 获取当前 wave-agent 的完整状态
|
|
382
|
+
* @returns Promise<MessageQueueChangedEvent | null> 返回当前状态,如果没有活动状态则返回 null
|
|
383
|
+
*/
|
|
384
|
+
async getCurrentState(): Promise<MessageQueueChangedEvent | null> {
|
|
385
|
+
const queryParams = this.getQueryParams();
|
|
386
|
+
const url = `${this.baseUrl}/api/agent/currentState${queryParams ? `?${queryParams}` : ''}`;
|
|
387
|
+
const response = await fetch(url);
|
|
388
|
+
const result: HttpResponse<MessageQueueChangedEvent | null> = await response.json();
|
|
389
|
+
|
|
390
|
+
if (result.status === 'error') {
|
|
391
|
+
throw new Error(result.message);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return result.data;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* 监听消息发送事件
|
|
399
|
+
* @param callback 回调函数
|
|
400
|
+
* @returns 取消监听的函数
|
|
401
|
+
*/
|
|
402
|
+
onMessageSend(callback: (event: MessageSendEvent) => void): () => void {
|
|
403
|
+
return onSocketEvent<MessageSendEvent>(this.socket, 'agent:message-send', callback);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 监听消息运行事件
|
|
408
|
+
* @param callback 回调函数
|
|
409
|
+
* @returns 取消监听的函数
|
|
410
|
+
*/
|
|
411
|
+
onMessageRunning(callback: (event: MessageRunningEvent) => void): () => void {
|
|
412
|
+
return onSocketEvent<MessageRunningEvent>(this.socket, 'agent:message-running', callback);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 监听消息完成事件
|
|
417
|
+
* @param callback 回调函数
|
|
418
|
+
* @returns 取消监听的函数
|
|
419
|
+
*/
|
|
420
|
+
onMessageFinished(callback: (event: MessageFinishedEvent) => void): () => void {
|
|
421
|
+
return onSocketEvent<MessageFinishedEvent>(this.socket, 'agent:message-finished', callback);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* 监听消息队列变化事件
|
|
426
|
+
* @param callback 回调函数
|
|
427
|
+
* @returns 取消监听的函数
|
|
428
|
+
*/
|
|
429
|
+
onMessageQueueChanged(callback: (event: MessageQueueChangedEvent) => void): () => void {
|
|
430
|
+
return onSocketEvent<MessageQueueChangedEvent>(this.socket, 'agent:message-queue-changed', callback);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* 监听初始化事件
|
|
435
|
+
* @param callback 回调函数
|
|
436
|
+
* @returns 取消监听的函数
|
|
437
|
+
*/
|
|
438
|
+
onInitializing(callback: (event: InitializingEvent) => void): () => void {
|
|
439
|
+
return onSocketEvent<InitializingEvent>(this.socket, 'agent:initializing', callback);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* 监听初始化完成事件
|
|
444
|
+
* @param callback 回调函数
|
|
445
|
+
* @returns 取消监听的函数
|
|
446
|
+
*/
|
|
447
|
+
onInitialized(callback: (event: InitializedEvent) => void): () => void {
|
|
448
|
+
return onSocketEvent<InitializedEvent>(this.socket, 'agent:initialized', callback);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* 监听消息块添加事件
|
|
453
|
+
* @param callback 回调函数
|
|
454
|
+
* @returns 取消监听的函数
|
|
455
|
+
*/
|
|
456
|
+
onMessageBlockAdded(callback: (event: MessageBlockAddedEvent) => void): () => void {
|
|
457
|
+
return onSocketEvent<MessageBlockAddedEvent>(this.socket, 'agent:message-block-added', callback);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* 监听消息块更新事件(用于流式内容更新)
|
|
462
|
+
* @param callback 回调函数
|
|
463
|
+
* @returns 取消监听的函数
|
|
464
|
+
*/
|
|
465
|
+
onMessageBlockUpdated(callback: (event: MessageBlockUpdatedEvent) => void): () => void {
|
|
466
|
+
return onSocketEvent<MessageBlockUpdatedEvent>(this.socket, 'agent:message-block-updated', callback);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* 监听工具块更新事件
|
|
471
|
+
* @param callback 回调函数
|
|
472
|
+
* @returns 取消监听的函数
|
|
473
|
+
*/
|
|
474
|
+
onMessageToolBlockUpdated(callback: (event: MessageToolBlockUpdatedEvent) => void): () => void {
|
|
475
|
+
return onSocketEvent<MessageToolBlockUpdatedEvent>(this.socket, 'agent:message-tool-block-updated', callback);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* 监听子代理块添加事件
|
|
480
|
+
* @param callback 回调函数
|
|
481
|
+
* @returns 取消监听的函数
|
|
482
|
+
*/
|
|
483
|
+
onMessageSubagentBlockAdd(callback: (event: MessageSubagentBlockAddEvent) => void): () => void {
|
|
484
|
+
return onSocketEvent<MessageSubagentBlockAddEvent>(this.socket, 'agent:message-subagent-block-add', callback);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* 监听子代理块状态更新事件
|
|
489
|
+
* @param callback 回调函数
|
|
490
|
+
* @returns 取消监听的函数
|
|
491
|
+
*/
|
|
492
|
+
onMessageSubagentBlockStatusUpdated(callback: (event: MessageSubagentBlockStatusUpdatedEvent) => void): () => void {
|
|
493
|
+
return onSocketEvent<MessageSubagentBlockStatusUpdatedEvent>(
|
|
494
|
+
this.socket,
|
|
495
|
+
'agent:message-subagent-block-status-updated',
|
|
496
|
+
callback
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* 监听子代理块内容更新事件
|
|
502
|
+
* @param callback 回调函数
|
|
503
|
+
* @returns 取消监听的函数
|
|
504
|
+
*/
|
|
505
|
+
onMessageSubagentBlockContentUpdated(callback: (event: MessageSubagentBlockContentUpdatedEvent) => void): () => void {
|
|
506
|
+
return onSocketEvent<MessageSubagentBlockContentUpdatedEvent>(
|
|
507
|
+
this.socket,
|
|
508
|
+
'agent:message-subagent-block-content-updated',
|
|
509
|
+
callback
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* 监听子代理工具块更新事件
|
|
515
|
+
* @param callback 回调函数
|
|
516
|
+
* @returns 取消监听的函数
|
|
517
|
+
*/
|
|
518
|
+
onMessageSubagentToolBlockUpdated(callback: (event: MessageSubagentToolBlockUpdatedEvent) => void): () => void {
|
|
519
|
+
return onSocketEvent<MessageSubagentToolBlockUpdatedEvent>(
|
|
520
|
+
this.socket,
|
|
521
|
+
'agent:message-subagent-tool-block-updated',
|
|
522
|
+
callback
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* 监听错误块添加事件
|
|
528
|
+
* @param callback 回调函数
|
|
529
|
+
* @returns 取消监听的函数
|
|
530
|
+
*/
|
|
531
|
+
onMessageErrorBlockAdded(callback: (event: MessageErrorBlockAddedEvent) => void): () => void {
|
|
532
|
+
return onSocketEvent<MessageErrorBlockAddedEvent>(this.socket, 'agent:message-error-block-added', callback);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* 监听命令输出消息添加事件
|
|
537
|
+
* @param callback 回调函数
|
|
538
|
+
* @returns 取消监听的函数
|
|
539
|
+
*/
|
|
540
|
+
onMessageCommandOutputMessageAdded(callback: (event: MessageCommandOutputMessageAddedEvent) => void): () => void {
|
|
541
|
+
return onSocketEvent<MessageCommandOutputMessageAddedEvent>(
|
|
542
|
+
this.socket,
|
|
543
|
+
'agent:message-command-output-message-added',
|
|
544
|
+
callback
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* 监听命令输出消息更新事件
|
|
550
|
+
* @param callback 回调函数
|
|
551
|
+
* @returns 取消监听的函数
|
|
552
|
+
*/
|
|
553
|
+
onMessageCommandOutputMessageUpdated(callback: (event: MessageCommandOutputMessageUpdatedEvent) => void): () => void {
|
|
554
|
+
return onSocketEvent<MessageCommandOutputMessageUpdatedEvent>(
|
|
555
|
+
this.socket,
|
|
556
|
+
'agent:message-command-output-message-updated',
|
|
557
|
+
callback
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* 监听命令输出消息完成事件
|
|
563
|
+
* @param callback 回调函数
|
|
564
|
+
* @returns 取消监听的函数
|
|
565
|
+
*/
|
|
566
|
+
onMessageCommandOutputMessageCompleted(
|
|
567
|
+
callback: (event: MessageCommandOutputMessageCompletedEvent) => void
|
|
568
|
+
): () => void {
|
|
569
|
+
return onSocketEvent<MessageCommandOutputMessageCompletedEvent>(
|
|
570
|
+
this.socket,
|
|
571
|
+
'agent:message-command-output-message-completed',
|
|
572
|
+
callback
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 监听 MCP 服务器变化事件
|
|
578
|
+
* @param callback 回调函数
|
|
579
|
+
* @returns 取消监听的函数
|
|
580
|
+
*/
|
|
581
|
+
onMcpServersChange(callback: (event: McpServersChangeEvent) => void): () => void {
|
|
582
|
+
return onSocketEvent<McpServersChangeEvent>(this.socket, 'agent:mcp-servers-change', callback);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* 监听文档解析开始事件
|
|
587
|
+
* @param callback 回调函数
|
|
588
|
+
* @returns 取消监听的函数
|
|
589
|
+
*/
|
|
590
|
+
onDocumentParseStart(callback: (event: DocumentParseStartEvent) => void): () => void {
|
|
591
|
+
return onSocketEvent<DocumentParseStartEvent>(this.socket, 'agent:document-parse-start', callback);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* 监听文档解析完成事件
|
|
596
|
+
* @param callback 回调函数
|
|
597
|
+
* @returns 取消监听的函数
|
|
598
|
+
*/
|
|
599
|
+
onDocumentParseComplete(callback: (event: DocumentParseCompleteEvent) => void): () => void {
|
|
600
|
+
return onSocketEvent<DocumentParseCompleteEvent>(this.socket, 'agent:document-parse-complete', callback);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* 监听文档解析错误事件
|
|
605
|
+
* @param callback 回调函数
|
|
606
|
+
* @returns 取消监听的函数
|
|
607
|
+
*/
|
|
608
|
+
onDocumentParseError(callback: (event: DocumentParseErrorEvent) => void): () => void {
|
|
609
|
+
return onSocketEvent<DocumentParseErrorEvent>(this.socket, 'agent:document-parse-error', callback);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* 通用事件监听方法,用于监听自定义事件
|
|
614
|
+
* @param eventName 事件名称(必须以 'agent:' 开头)
|
|
615
|
+
* @param callback 回调函数
|
|
616
|
+
* @returns 取消监听的函数
|
|
617
|
+
*/
|
|
618
|
+
on<T = unknown>(eventName: string, callback: (event: T) => void): () => void {
|
|
619
|
+
if (!eventName.startsWith('agent:')) {
|
|
620
|
+
throw new Error(`事件名称必须以 'agent:' 开头,当前事件名称: ${eventName}`);
|
|
621
|
+
}
|
|
622
|
+
return onSocketEvent<T>(this.socket, eventName, callback);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Socket } from 'socket.io-client';
|
|
2
|
+
import { createSocketHandler, onSocketEvent } from '../utils/socket.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 命令执行模块
|
|
6
|
+
*/
|
|
7
|
+
export class ExecModule {
|
|
8
|
+
constructor(private socket: Socket) {}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 执行命令
|
|
12
|
+
* @param sessionId 会话 ID
|
|
13
|
+
* @param command 要执行的命令
|
|
14
|
+
* @returns Promise<number> 命令退出码
|
|
15
|
+
*/
|
|
16
|
+
async exec(sessionId: string, command: string): Promise<number> {
|
|
17
|
+
return createSocketHandler<{ sessionId: string; command: string }, number>(
|
|
18
|
+
this.socket,
|
|
19
|
+
'cmd:exec',
|
|
20
|
+
() => ({ sessionId, command })
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 中止命令执行
|
|
26
|
+
* @param sessionId 会话 ID
|
|
27
|
+
*/
|
|
28
|
+
async abort(sessionId: string): Promise<void> {
|
|
29
|
+
return createSocketHandler<{ sessionId: string }, void>(
|
|
30
|
+
this.socket,
|
|
31
|
+
'cmd:abort',
|
|
32
|
+
() => ({ sessionId })
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 监听命令输出
|
|
38
|
+
* @param sessionId 会话 ID
|
|
39
|
+
* @param callback 输出回调函数,接收 Uint8Array 数据(浏览器环境)
|
|
40
|
+
* @returns 取消监听的函数
|
|
41
|
+
*/
|
|
42
|
+
onOutput(
|
|
43
|
+
sessionId: string,
|
|
44
|
+
callback: (data: Uint8Array) => void
|
|
45
|
+
): () => void {
|
|
46
|
+
const eventName = `cmd:exec-output-${sessionId}`;
|
|
47
|
+
return onSocketEvent<Uint8Array>(this.socket, eventName, callback);
|
|
48
|
+
}
|
|
49
|
+
}
|