@robota-sdk/agent-transport-ws 3.0.0-beta.63 → 3.0.0-beta.76

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.
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Tests for WebSocket transport handler.
3
+ */
4
+
5
+ import { describe, it, expect, vi } from 'vitest';
6
+ import { createWsHandler } from '../ws-handler.js';
7
+ import type { TServerMessage } from '../ws-protocol.js';
8
+ import type {
9
+ IBackgroundJobGroupState,
10
+ IExecutionWorkspaceEvent,
11
+ IExecutionWorkspaceSnapshot,
12
+ IInteractiveSession,
13
+ TBackgroundJobGroupEvent,
14
+ TExecutionWorkspaceUpdateCause,
15
+ } from '@robota-sdk/agent-interface-transport';
16
+ import type {
17
+ IBackgroundTaskLogPage,
18
+ IBackgroundTaskState,
19
+ TBackgroundTaskEvent,
20
+ } from '@robota-sdk/agent-framework';
21
+
22
+ const backgroundTask: IBackgroundTaskState = {
23
+ id: 'task_1',
24
+ kind: 'agent',
25
+ label: 'Explore',
26
+ agentType: 'Explore',
27
+ status: 'running',
28
+ mode: 'background',
29
+ parentSessionId: 'session_1',
30
+ depth: 0,
31
+ cwd: '/repo',
32
+ updatedAt: '2026-05-01T00:00:00.000Z',
33
+ unread: false,
34
+ promptPreview: 'inspect code',
35
+ };
36
+
37
+ const backgroundTaskLogPage: IBackgroundTaskLogPage = {
38
+ taskId: 'task_1',
39
+ cursor: { offset: 0 },
40
+ lines: ['line one'],
41
+ };
42
+
43
+ const executionWorkspaceSnapshot: IExecutionWorkspaceSnapshot = {
44
+ sessionId: 'session_1',
45
+ entries: [],
46
+ updatedAt: '2026-05-01T00:00:00.000Z',
47
+ };
48
+
49
+ const backgroundJobGroup: IBackgroundJobGroupState = {
50
+ id: 'group_1',
51
+ parentSessionId: 'session_1',
52
+ waitPolicy: 'wait_all',
53
+ taskIds: ['task_1'],
54
+ status: 'running',
55
+ createdAt: '2026-05-01T00:00:00.000Z',
56
+ updatedAt: '2026-05-01T00:00:00.000Z',
57
+ results: [],
58
+ };
59
+
60
+ function createMockSession() {
61
+ const listeners = new Map<string, Set<(...args: unknown[]) => void>>();
62
+ return {
63
+ submit: vi.fn(),
64
+ abort: vi.fn(),
65
+ cancelQueue: vi.fn(),
66
+ getMessages: vi.fn().mockReturnValue([{ role: 'user', content: 'hi' }]),
67
+ getContextState: vi
68
+ .fn()
69
+ .mockReturnValue({ usedTokens: 500, maxTokens: 100000, usedPercentage: 0.5 }),
70
+ isExecuting: vi.fn().mockReturnValue(false),
71
+ getPendingPrompt: vi.fn().mockReturnValue(null),
72
+ listBackgroundTasks: vi.fn().mockReturnValue([backgroundTask]),
73
+ getBackgroundTask: vi.fn().mockReturnValue(backgroundTask),
74
+ listBackgroundJobGroups: vi.fn().mockReturnValue([backgroundJobGroup]),
75
+ getBackgroundJobGroup: vi.fn().mockReturnValue(backgroundJobGroup),
76
+ waitBackgroundJobGroup: vi.fn().mockResolvedValue({
77
+ ...backgroundJobGroup,
78
+ status: 'completed',
79
+ completedAt: '2026-05-01T00:00:01.000Z',
80
+ results: [{ taskId: 'task_1', label: 'Explore', status: 'completed', summary: 'done' }],
81
+ }),
82
+ cancelBackgroundTask: vi.fn().mockResolvedValue(undefined),
83
+ closeBackgroundTask: vi.fn().mockResolvedValue(undefined),
84
+ sendBackgroundTask: vi.fn().mockResolvedValue(undefined),
85
+ readBackgroundTaskLog: vi.fn().mockResolvedValue(backgroundTaskLogPage),
86
+ getExecutionWorkspaceSnapshot: vi.fn().mockReturnValue(executionWorkspaceSnapshot),
87
+ executeCommand: vi.fn().mockResolvedValue({ message: 'done', success: true, data: {} }),
88
+ listCommands: vi.fn().mockReturnValue([]),
89
+ on: vi.fn((event: string, handler: (...args: unknown[]) => void) => {
90
+ if (!listeners.has(event)) listeners.set(event, new Set());
91
+ listeners.get(event)!.add(handler);
92
+ }),
93
+ off: vi.fn((event: string, handler: (...args: unknown[]) => void) => {
94
+ listeners.get(event)?.delete(handler);
95
+ }),
96
+ _emit: (event: string, ...args: unknown[]) => {
97
+ listeners.get(event)?.forEach((h) => h(...args));
98
+ },
99
+ } as unknown as IInteractiveSession & { _emit: (event: string, ...args: unknown[]) => void };
100
+ }
101
+
102
+ describe('WebSocket Transport Handler', () => {
103
+ function setup() {
104
+ const session = createMockSession();
105
+ const sent: TServerMessage[] = [];
106
+ const { onMessage, cleanup } = createWsHandler({
107
+ session: session as unknown as IInteractiveSession,
108
+ send: (msg) => sent.push(msg),
109
+ });
110
+ return { session, sent, onMessage, cleanup };
111
+ }
112
+
113
+ it('submit calls session.submit()', () => {
114
+ const { onMessage, session } = setup();
115
+ onMessage(JSON.stringify({ type: 'submit', prompt: 'hello' }));
116
+ expect(
117
+ (session as unknown as { submit: ReturnType<typeof vi.fn> }).submit,
118
+ ).toHaveBeenCalledWith('hello');
119
+ });
120
+
121
+ it('abort calls session.abort()', () => {
122
+ const { onMessage, session } = setup();
123
+ onMessage(JSON.stringify({ type: 'abort' }));
124
+ expect((session as unknown as { abort: ReturnType<typeof vi.fn> }).abort).toHaveBeenCalled();
125
+ });
126
+
127
+ it('cancel-queue calls session.cancelQueue()', () => {
128
+ const { onMessage, session } = setup();
129
+ onMessage(JSON.stringify({ type: 'cancel-queue' }));
130
+ expect(
131
+ (session as unknown as { cancelQueue: ReturnType<typeof vi.fn> }).cancelQueue,
132
+ ).toHaveBeenCalled();
133
+ });
134
+
135
+ it('get-messages sends messages back', () => {
136
+ const { onMessage, sent } = setup();
137
+ onMessage(JSON.stringify({ type: 'get-messages' }));
138
+ expect(sent).toHaveLength(1);
139
+ expect(sent[0]!.type).toBe('messages');
140
+ });
141
+
142
+ it('get-context sends context state back', () => {
143
+ const { onMessage, sent } = setup();
144
+ onMessage(JSON.stringify({ type: 'get-context' }));
145
+ expect(sent).toHaveLength(1);
146
+ expect(sent[0]!.type).toBe('context');
147
+ });
148
+
149
+ it('get-executing sends executing status', () => {
150
+ const { onMessage, sent } = setup();
151
+ onMessage(JSON.stringify({ type: 'get-executing' }));
152
+ expect(sent[0]).toEqual({ type: 'executing', executing: false });
153
+ });
154
+
155
+ it('get-pending sends pending prompt', () => {
156
+ const { onMessage, sent } = setup();
157
+ onMessage(JSON.stringify({ type: 'get-pending' }));
158
+ expect(sent[0]).toEqual({ type: 'pending', pending: null });
159
+ });
160
+
161
+ it('get-background-tasks sends current background task list', () => {
162
+ const { onMessage, sent, session } = setup();
163
+ onMessage(JSON.stringify({ type: 'get-background-tasks', filter: { kind: 'agent' } }));
164
+
165
+ expect(sent[0]).toEqual({ type: 'background_tasks', tasks: [backgroundTask] });
166
+ expect(
167
+ (session as unknown as { listBackgroundTasks: ReturnType<typeof vi.fn> }).listBackgroundTasks,
168
+ ).toHaveBeenCalledWith({ kind: 'agent' });
169
+ });
170
+
171
+ it('get-background-task sends one background task snapshot', () => {
172
+ const { onMessage, sent, session } = setup();
173
+ onMessage(JSON.stringify({ type: 'get-background-task', taskId: 'task_1' }));
174
+
175
+ expect(sent[0]).toEqual({
176
+ type: 'background_task',
177
+ taskId: 'task_1',
178
+ task: backgroundTask,
179
+ });
180
+ expect(
181
+ (session as unknown as { getBackgroundTask: ReturnType<typeof vi.fn> }).getBackgroundTask,
182
+ ).toHaveBeenCalledWith('task_1');
183
+ });
184
+
185
+ it('get-background-job-groups sends group snapshots', () => {
186
+ const { onMessage, sent, session } = setup();
187
+ onMessage(JSON.stringify({ type: 'get-background-job-groups' }));
188
+
189
+ expect(sent[0]).toEqual({ type: 'background_job_groups', groups: [backgroundJobGroup] });
190
+ expect(
191
+ (session as unknown as { listBackgroundJobGroups: ReturnType<typeof vi.fn> })
192
+ .listBackgroundJobGroups,
193
+ ).toHaveBeenCalled();
194
+ });
195
+
196
+ it('get-background-job-group sends one group snapshot', () => {
197
+ const { onMessage, sent, session } = setup();
198
+ onMessage(JSON.stringify({ type: 'get-background-job-group', groupId: 'group_1' }));
199
+
200
+ expect(sent[0]).toEqual({
201
+ type: 'background_job_group',
202
+ groupId: 'group_1',
203
+ group: backgroundJobGroup,
204
+ });
205
+ expect(
206
+ (session as unknown as { getBackgroundJobGroup: ReturnType<typeof vi.fn> })
207
+ .getBackgroundJobGroup,
208
+ ).toHaveBeenCalledWith('group_1');
209
+ });
210
+
211
+ it('wait-background-job-group waits and sends the completed group', async () => {
212
+ const { onMessage, sent, session } = setup();
213
+ onMessage(JSON.stringify({ type: 'wait-background-job-group', groupId: 'group_1' }));
214
+
215
+ await new Promise((r) => setTimeout(r, 10));
216
+
217
+ expect(
218
+ (session as unknown as { waitBackgroundJobGroup: ReturnType<typeof vi.fn> })
219
+ .waitBackgroundJobGroup,
220
+ ).toHaveBeenCalledWith('group_1');
221
+ expect(sent[0]).toMatchObject({
222
+ type: 'background_job_group',
223
+ groupId: 'group_1',
224
+ group: { id: 'group_1', status: 'completed' },
225
+ });
226
+ });
227
+
228
+ it('cancel-background-task maps to session control and emits control result', async () => {
229
+ const { onMessage, sent, session } = setup();
230
+ onMessage(JSON.stringify({ type: 'cancel-background-task', taskId: 'task_1', reason: 'stop' }));
231
+
232
+ await new Promise((r) => setTimeout(r, 10));
233
+
234
+ expect(
235
+ (session as unknown as { cancelBackgroundTask: ReturnType<typeof vi.fn> })
236
+ .cancelBackgroundTask,
237
+ ).toHaveBeenCalledWith('task_1', 'stop');
238
+ expect(sent[0]).toEqual({
239
+ type: 'background_task_control_result',
240
+ action: 'cancel',
241
+ taskId: 'task_1',
242
+ success: true,
243
+ });
244
+ });
245
+
246
+ it('send-background-task maps prompt input to session control', async () => {
247
+ const { onMessage, sent, session } = setup();
248
+ onMessage(
249
+ JSON.stringify({
250
+ type: 'send-background-task',
251
+ taskId: 'task_1',
252
+ input: { prompt: 'continue' },
253
+ }),
254
+ );
255
+
256
+ await new Promise((r) => setTimeout(r, 10));
257
+
258
+ expect(
259
+ (session as unknown as { sendBackgroundTask: ReturnType<typeof vi.fn> }).sendBackgroundTask,
260
+ ).toHaveBeenCalledWith('task_1', { prompt: 'continue' });
261
+ expect(sent[0]).toEqual({
262
+ type: 'background_task_control_result',
263
+ action: 'send',
264
+ taskId: 'task_1',
265
+ success: true,
266
+ });
267
+ });
268
+
269
+ it('read-background-task-log sends log page', async () => {
270
+ const { onMessage, sent, session } = setup();
271
+ onMessage(
272
+ JSON.stringify({
273
+ type: 'read-background-task-log',
274
+ taskId: 'task_1',
275
+ cursor: { offset: 0 },
276
+ }),
277
+ );
278
+
279
+ await new Promise((r) => setTimeout(r, 10));
280
+
281
+ expect(
282
+ (session as unknown as { readBackgroundTaskLog: ReturnType<typeof vi.fn> })
283
+ .readBackgroundTaskLog,
284
+ ).toHaveBeenCalledWith('task_1', { offset: 0 });
285
+ expect(sent[0]).toEqual({
286
+ type: 'background_task_log',
287
+ taskId: 'task_1',
288
+ page: backgroundTaskLogPage,
289
+ });
290
+ });
291
+
292
+ it('command executes via session.executeCommand()', async () => {
293
+ const { onMessage, sent, session } = setup();
294
+ onMessage(JSON.stringify({ type: 'command', name: 'clear' }));
295
+ await new Promise((r) => setTimeout(r, 10));
296
+ expect(sent).toHaveLength(1);
297
+ expect(sent[0]!.type).toBe('command_result');
298
+ expect(
299
+ (session as unknown as { executeCommand: ReturnType<typeof vi.fn> }).executeCommand,
300
+ ).toHaveBeenCalledWith('clear', '');
301
+ });
302
+
303
+ it('invalid JSON sends protocol_error', () => {
304
+ const { onMessage, sent } = setup();
305
+ onMessage('not json');
306
+ expect(sent[0]).toEqual({ type: 'protocol_error', message: 'Invalid JSON' });
307
+ });
308
+
309
+ it('unknown type sends protocol_error', () => {
310
+ const { onMessage, sent } = setup();
311
+ onMessage(JSON.stringify({ type: 'unknown_type' }));
312
+ expect(sent[0]!.type).toBe('protocol_error');
313
+ });
314
+
315
+ it('submit without prompt sends protocol_error', () => {
316
+ const { onMessage, sent } = setup();
317
+ onMessage(JSON.stringify({ type: 'submit' }));
318
+ expect(sent[0]).toEqual({ type: 'protocol_error', message: 'prompt is required' });
319
+ });
320
+
321
+ it('forwards InteractiveSession events to client', () => {
322
+ const { session, sent } = setup();
323
+ (session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
324
+ 'text_delta',
325
+ 'hello',
326
+ );
327
+ expect(sent).toHaveLength(1);
328
+ expect(sent[0]).toEqual({ type: 'text_delta', delta: 'hello' });
329
+ });
330
+
331
+ it('forwards thinking event', () => {
332
+ const { session, sent } = setup();
333
+ (session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
334
+ 'thinking',
335
+ true,
336
+ );
337
+ expect(sent[0]).toEqual({ type: 'thinking', isThinking: true });
338
+ });
339
+
340
+ it('forwards background task events to the client', () => {
341
+ const { session, sent } = setup();
342
+ const event: TBackgroundTaskEvent = {
343
+ type: 'background_task_text_delta',
344
+ taskId: 'task_1',
345
+ delta: 'partial',
346
+ };
347
+ (session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
348
+ 'background_task_event',
349
+ event,
350
+ );
351
+
352
+ expect(sent[0]).toEqual({ type: 'background_task_event', event });
353
+ });
354
+
355
+ it('forwards background job group events to the client', () => {
356
+ const { session, sent } = setup();
357
+ const event: TBackgroundJobGroupEvent = {
358
+ type: 'background_job_group_completed',
359
+ group: { ...backgroundJobGroup, status: 'completed' },
360
+ };
361
+ (session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
362
+ 'background_job_group_event',
363
+ event,
364
+ );
365
+
366
+ expect(sent[0]).toEqual({ type: 'background_job_group_event', event });
367
+ });
368
+
369
+ it('forwards execution_workspace_event to the client', () => {
370
+ const { session, sent } = setup();
371
+ const event: IExecutionWorkspaceEvent = {
372
+ type: 'execution_workspace_updated',
373
+ cause: 'main_thread',
374
+ snapshot: executionWorkspaceSnapshot,
375
+ };
376
+ (session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
377
+ 'execution_workspace_event',
378
+ event,
379
+ );
380
+
381
+ expect(sent[0]).toEqual({
382
+ type: 'execution_workspace_event',
383
+ snapshot: executionWorkspaceSnapshot,
384
+ });
385
+ });
386
+
387
+ it('get-execution-workspace sends current snapshot', () => {
388
+ const { onMessage, sent, session } = setup();
389
+ onMessage(JSON.stringify({ type: 'get-execution-workspace' }));
390
+
391
+ expect(sent[0]).toEqual({
392
+ type: 'execution_workspace_event',
393
+ snapshot: executionWorkspaceSnapshot,
394
+ });
395
+ expect(
396
+ (
397
+ session as unknown as {
398
+ getExecutionWorkspaceSnapshot: ReturnType<typeof vi.fn>;
399
+ }
400
+ ).getExecutionWorkspaceSnapshot,
401
+ ).toHaveBeenCalled();
402
+ });
403
+
404
+ it('cleanup unsubscribes from all events', () => {
405
+ const { session, cleanup } = setup();
406
+ cleanup();
407
+ expect((session as unknown as { off: ReturnType<typeof vi.fn> }).off).toHaveBeenCalledTimes(11);
408
+ });
409
+ });
@@ -0,0 +1,53 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { createWsTransport } from '../ws-transport.js';
3
+ import type { IInteractiveSession } from '@robota-sdk/agent-interface-transport';
4
+
5
+ function createMockSession(): IInteractiveSession {
6
+ return {
7
+ submit: vi.fn(),
8
+ abort: vi.fn(),
9
+ cancelQueue: vi.fn(),
10
+ getMessages: vi.fn().mockReturnValue([]),
11
+ getContextState: vi
12
+ .fn()
13
+ .mockReturnValue({ usedPercentage: 0, usedTokens: 0, maxTokens: 200000 }),
14
+ isExecuting: vi.fn().mockReturnValue(false),
15
+ getPendingPrompt: vi.fn().mockReturnValue(null),
16
+ executeCommand: vi.fn().mockResolvedValue({ message: 'ok', success: true }),
17
+ listCommands: vi.fn().mockReturnValue([]),
18
+ on: vi.fn(),
19
+ off: vi.fn(),
20
+ } as unknown as IInteractiveSession;
21
+ }
22
+
23
+ describe('createWsTransport', () => {
24
+ it('returns an adapter with name "ws"', () => {
25
+ const transport = createWsTransport({ send: vi.fn() });
26
+ expect(transport.name).toBe('ws');
27
+ });
28
+
29
+ it('throws if start() is called without attach()', async () => {
30
+ const transport = createWsTransport({ send: vi.fn() });
31
+ await expect(transport.start()).rejects.toThrow('No session attached');
32
+ });
33
+
34
+ it('onMessage is null before start()', () => {
35
+ const transport = createWsTransport({ send: vi.fn() });
36
+ expect(transport.onMessage).toBeNull();
37
+ });
38
+
39
+ it('provides onMessage after attach + start', async () => {
40
+ const transport = createWsTransport({ send: vi.fn() });
41
+ transport.attach(createMockSession() as never);
42
+ await transport.start();
43
+ expect(typeof transport.onMessage).toBe('function');
44
+ });
45
+
46
+ it('clears onMessage after stop()', async () => {
47
+ const transport = createWsTransport({ send: vi.fn() });
48
+ transport.attach(createMockSession() as never);
49
+ await transport.start();
50
+ await transport.stop();
51
+ expect(transport.onMessage).toBeNull();
52
+ });
53
+ });
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { createWsHandler } from './ws-handler.js';
2
+ export type { IWsHandlerOptions } from './ws-handler.js';
3
+ export type { TClientMessage, TServerMessage } from './ws-protocol.js';
4
+ export { createWsTransport } from './ws-transport.js';
5
+ export type { IWsTransportOptions } from './ws-transport.js';
6
+ export { WsTransport } from './ws-transport-configurable.js';
7
+ export type { IWsTransportConfig } from './ws-transport-configurable.js';
8
+ export type {
9
+ IExecutionWorkspaceSnapshot,
10
+ IExecutionWorkspaceEntry,
11
+ TExecutionWorkspaceStatus,
12
+ TExecutionAttention,
13
+ } from '@robota-sdk/agent-framework';
@@ -0,0 +1,170 @@
1
+ import type { TBackgroundControlAction, TClientMessage, TServerMessage } from './ws-protocol.js';
2
+ import type { IInteractiveSession } from '@robota-sdk/agent-interface-transport';
3
+
4
+ export function handleBackgroundQueryMessage(
5
+ session: IInteractiveSession,
6
+ send: (message: TServerMessage) => void,
7
+ msg: Extract<
8
+ TClientMessage,
9
+ | { type: 'get-background-tasks' | 'get-background-task' | 'read-background-task-log' }
10
+ | {
11
+ type:
12
+ | 'get-background-job-groups'
13
+ | 'get-background-job-group'
14
+ | 'wait-background-job-group';
15
+ }
16
+ >,
17
+ ): void {
18
+ if (msg.type === 'get-background-tasks') {
19
+ send({ type: 'background_tasks', tasks: session.listBackgroundTasks(msg.filter) });
20
+ return;
21
+ }
22
+ if (msg.type === 'get-background-task') {
23
+ sendBackgroundTaskSnapshot(session, send, msg);
24
+ return;
25
+ }
26
+ if (msg.type === 'get-background-job-groups') {
27
+ send({ type: 'background_job_groups', groups: session.listBackgroundJobGroups() });
28
+ return;
29
+ }
30
+ if (msg.type === 'get-background-job-group') {
31
+ sendBackgroundJobGroupSnapshot(session, send, msg);
32
+ return;
33
+ }
34
+ if (msg.type === 'wait-background-job-group') {
35
+ sendBackgroundJobGroupWaitResult(session, send, msg);
36
+ return;
37
+ }
38
+ sendBackgroundTaskLogPage(session, send, msg);
39
+ }
40
+
41
+ export function handleBackgroundControlMessage(
42
+ session: IInteractiveSession,
43
+ send: (message: TServerMessage) => void,
44
+ msg: Extract<
45
+ TClientMessage,
46
+ { type: 'cancel-background-task' | 'close-background-task' | 'send-background-task' }
47
+ >,
48
+ ): void {
49
+ if (!msg.taskId) {
50
+ send({ type: 'protocol_error', message: 'taskId is required' });
51
+ return;
52
+ }
53
+ if (msg.type === 'cancel-background-task') {
54
+ sendBackgroundTaskControlResult(
55
+ send,
56
+ 'cancel',
57
+ msg.taskId,
58
+ session.cancelBackgroundTask(msg.taskId, msg.reason),
59
+ );
60
+ return;
61
+ }
62
+ if (msg.type === 'close-background-task') {
63
+ sendBackgroundTaskControlResult(
64
+ send,
65
+ 'close',
66
+ msg.taskId,
67
+ session.closeBackgroundTask(msg.taskId),
68
+ );
69
+ return;
70
+ }
71
+ sendBackgroundTaskInput(session, send, msg);
72
+ }
73
+
74
+ function sendBackgroundTaskSnapshot(
75
+ session: IInteractiveSession,
76
+ send: (message: TServerMessage) => void,
77
+ msg: Extract<TClientMessage, { type: 'get-background-task' }>,
78
+ ): void {
79
+ if (!msg.taskId) {
80
+ send({ type: 'protocol_error', message: 'taskId is required' });
81
+ return;
82
+ }
83
+ send({
84
+ type: 'background_task',
85
+ taskId: msg.taskId,
86
+ task: session.getBackgroundTask(msg.taskId) ?? null,
87
+ });
88
+ }
89
+
90
+ function sendBackgroundTaskLogPage(
91
+ session: IInteractiveSession,
92
+ send: (message: TServerMessage) => void,
93
+ msg: Extract<TClientMessage, { type: 'read-background-task-log' }>,
94
+ ): void {
95
+ if (!msg.taskId) {
96
+ send({ type: 'protocol_error', message: 'taskId is required' });
97
+ return;
98
+ }
99
+ session.readBackgroundTaskLog(msg.taskId, msg.cursor).then(
100
+ (page) => send({ type: 'background_task_log', taskId: msg.taskId, page }),
101
+ (error: Error) => send({ type: 'protocol_error', message: error.message }),
102
+ );
103
+ }
104
+
105
+ function sendBackgroundJobGroupSnapshot(
106
+ session: IInteractiveSession,
107
+ send: (message: TServerMessage) => void,
108
+ msg: Extract<TClientMessage, { type: 'get-background-job-group' }>,
109
+ ): void {
110
+ if (!msg.groupId) {
111
+ send({ type: 'protocol_error', message: 'groupId is required' });
112
+ return;
113
+ }
114
+ send({
115
+ type: 'background_job_group',
116
+ groupId: msg.groupId,
117
+ group: session.getBackgroundJobGroup(msg.groupId) ?? null,
118
+ });
119
+ }
120
+
121
+ function sendBackgroundJobGroupWaitResult(
122
+ session: IInteractiveSession,
123
+ send: (message: TServerMessage) => void,
124
+ msg: Extract<TClientMessage, { type: 'wait-background-job-group' }>,
125
+ ): void {
126
+ if (!msg.groupId) {
127
+ send({ type: 'protocol_error', message: 'groupId is required' });
128
+ return;
129
+ }
130
+ session.waitBackgroundJobGroup(msg.groupId).then(
131
+ (group) => send({ type: 'background_job_group', groupId: msg.groupId, group }),
132
+ (error: Error) => send({ type: 'protocol_error', message: error.message }),
133
+ );
134
+ }
135
+
136
+ function sendBackgroundTaskInput(
137
+ session: IInteractiveSession,
138
+ send: (message: TServerMessage) => void,
139
+ msg: Extract<TClientMessage, { type: 'send-background-task' }>,
140
+ ): void {
141
+ if (!msg.input) {
142
+ send({ type: 'protocol_error', message: 'input is required' });
143
+ return;
144
+ }
145
+ sendBackgroundTaskControlResult(
146
+ send,
147
+ 'send',
148
+ msg.taskId,
149
+ session.sendBackgroundTask(msg.taskId, msg.input),
150
+ );
151
+ }
152
+
153
+ function sendBackgroundTaskControlResult(
154
+ send: (message: TServerMessage) => void,
155
+ action: TBackgroundControlAction,
156
+ taskId: string,
157
+ operation: Promise<void>,
158
+ ): void {
159
+ operation.then(
160
+ () => send({ type: 'background_task_control_result', action, taskId, success: true }),
161
+ (error: Error) =>
162
+ send({
163
+ type: 'background_task_control_result',
164
+ action,
165
+ taskId,
166
+ success: false,
167
+ message: error.message,
168
+ }),
169
+ );
170
+ }