@robota-sdk/agent-transport-ws 3.0.0-beta.78 → 3.0.0-beta.81
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 +14 -4
- package/dist/node/index.cjs +1 -1
- package/dist/node/index.d.cts +161 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.ts +124 -175
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -1
- package/dist/node/index.js.map +1 -1
- package/package.json +36 -17
- package/src/__tests__/outbound-delivery-carrier.test.ts +195 -0
- package/src/__tests__/payload-channels.test.ts +233 -0
- package/src/__tests__/ws-payload-channel.e2e.test.ts +255 -0
- package/src/__tests__/ws-session-delivery.test.ts +86 -0
- package/src/__tests__/ws-transport-auth.test.ts +220 -0
- package/src/__tests__/ws-transport-lifecycle.test.ts +135 -0
- package/src/__tests__/ws-transport.test.ts +69 -5
- package/src/index.ts +5 -5
- package/src/payload-channels.ts +204 -0
- package/src/ws-connection-guards.ts +104 -0
- package/src/ws-message-data.ts +8 -0
- package/src/ws-session-delivery.ts +109 -0
- package/src/ws-transport-config.ts +114 -0
- package/src/ws-transport-configurable.ts +217 -40
- package/src/ws-transport.ts +60 -12
- package/src/__tests__/ws-handler.test.ts +0 -429
- package/src/ws-background-messages.ts +0 -170
- package/src/ws-handler.ts +0 -287
- package/src/ws-protocol.ts +0 -78
package/src/ws-transport.ts
CHANGED
|
@@ -1,35 +1,81 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ITransportAdapter implementation for WebSocket transport.
|
|
3
3
|
*
|
|
4
|
-
* Wraps
|
|
4
|
+
* Wraps createSessionMessageHandler into the unified ITransportAdapter interface.
|
|
5
5
|
* After start(), the consumer must wire onMessage to their WebSocket.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { createOutboundDelivery, createSessionMessageHandler } from '@robota-sdk/agent-transport';
|
|
9
9
|
|
|
10
|
-
import type {
|
|
11
|
-
import type {
|
|
10
|
+
import type { TUsageSurface } from '@robota-sdk/agent-interface-analytics';
|
|
11
|
+
import type { TDriverId } from '@robota-sdk/agent-interface-session';
|
|
12
|
+
import type {
|
|
13
|
+
ITransportAdapter,
|
|
14
|
+
ITransportLifecycleError,
|
|
15
|
+
} from '@robota-sdk/agent-interface-transport';
|
|
16
|
+
import type {
|
|
17
|
+
IProtocolSession,
|
|
18
|
+
ISessionMessageHandlerOptions,
|
|
19
|
+
TServerMessage,
|
|
20
|
+
} from '@robota-sdk/agent-transport';
|
|
12
21
|
|
|
13
22
|
export interface IWsTransportOptions {
|
|
14
23
|
/** Send a JSON message to the connected WebSocket client. */
|
|
15
24
|
send: (message: TServerMessage) => void;
|
|
25
|
+
/** Owning socket lifecycle callback for outbound session-event delivery failures. */
|
|
26
|
+
onDeliveryError?: (error: Error, event: string) => void;
|
|
27
|
+
personalUsageReporter?: ISessionMessageHandlerOptions['personalUsageReporter'];
|
|
28
|
+
usageReporter?: ISessionMessageHandlerOptions['usageReporter'];
|
|
29
|
+
storedSessionUsageReporter?: ISessionMessageHandlerOptions['storedSessionUsageReporter'];
|
|
30
|
+
driverId?: TDriverId;
|
|
31
|
+
surface?: TUsageSurface;
|
|
16
32
|
}
|
|
17
33
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
34
|
+
export interface IWsTransport extends ITransportAdapter<IProtocolSession> {
|
|
35
|
+
onMessage: ((data: string) => void) | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createWsTransport(options: IWsTransportOptions): IWsTransport {
|
|
39
|
+
let session: IProtocolSession | null = null;
|
|
22
40
|
let cleanup: (() => void) | null = null;
|
|
41
|
+
const lifecycleError = (code: ITransportLifecycleError['code']): ITransportLifecycleError =>
|
|
42
|
+
Object.assign(new Error(`WebSocket transport ${code}.`), {
|
|
43
|
+
name: 'TransportLifecycleError' as const,
|
|
44
|
+
code,
|
|
45
|
+
transportName: 'ws',
|
|
46
|
+
});
|
|
23
47
|
|
|
24
|
-
|
|
48
|
+
const transport: IWsTransport = {
|
|
25
49
|
name: 'ws',
|
|
50
|
+
lifecycle: Object.freeze({ kind: 'service' }),
|
|
26
51
|
onMessage: null,
|
|
27
|
-
attach(s:
|
|
52
|
+
attach(s: IProtocolSession) {
|
|
28
53
|
session = s;
|
|
29
54
|
},
|
|
30
55
|
async start() {
|
|
31
|
-
if (!session) throw
|
|
32
|
-
|
|
56
|
+
if (!session) throw lifecycleError('not-attached');
|
|
57
|
+
if (cleanup) throw lifecycleError('already-started');
|
|
58
|
+
// ARCH-030: this adapter is the carrier here, so it builds the connection's outbound boundary
|
|
59
|
+
// from its own injected sink and its own failure policy, then passes it down.
|
|
60
|
+
const deliver = createOutboundDelivery(options.send, (error, event) => {
|
|
61
|
+
handler.cleanup();
|
|
62
|
+
cleanup = null;
|
|
63
|
+
transport.onMessage = null;
|
|
64
|
+
options.onDeliveryError?.(error, event);
|
|
65
|
+
});
|
|
66
|
+
const handler = createSessionMessageHandler({
|
|
67
|
+
session,
|
|
68
|
+
deliver,
|
|
69
|
+
...(options.driverId ? { driverId: options.driverId } : {}),
|
|
70
|
+
...(options.surface ? { surface: options.surface } : {}),
|
|
71
|
+
...(options.personalUsageReporter
|
|
72
|
+
? { personalUsageReporter: options.personalUsageReporter }
|
|
73
|
+
: {}),
|
|
74
|
+
...(options.usageReporter ? { usageReporter: options.usageReporter } : {}),
|
|
75
|
+
...(options.storedSessionUsageReporter
|
|
76
|
+
? { storedSessionUsageReporter: options.storedSessionUsageReporter }
|
|
77
|
+
: {}),
|
|
78
|
+
});
|
|
33
79
|
cleanup = handler.cleanup;
|
|
34
80
|
this.onMessage = handler.onMessage;
|
|
35
81
|
},
|
|
@@ -37,6 +83,8 @@ export function createWsTransport(
|
|
|
37
83
|
cleanup?.();
|
|
38
84
|
cleanup = null;
|
|
39
85
|
this.onMessage = null;
|
|
86
|
+
session = null;
|
|
40
87
|
},
|
|
41
88
|
};
|
|
89
|
+
return transport;
|
|
42
90
|
}
|
|
@@ -1,429 +0,0 @@
|
|
|
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-interface-transport';
|
|
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().mockResolvedValue(undefined),
|
|
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('submit rejection surfaces a protocol_error (WS-001)', async () => {
|
|
322
|
-
const { onMessage, sent, session } = setup();
|
|
323
|
-
(session as unknown as { submit: ReturnType<typeof vi.fn> }).submit.mockRejectedValueOnce(
|
|
324
|
-
new Error('submit failed'),
|
|
325
|
-
);
|
|
326
|
-
onMessage(JSON.stringify({ type: 'submit', prompt: 'hello' }));
|
|
327
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
328
|
-
expect(sent).toEqual([{ type: 'protocol_error', message: 'submit failed' }]);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it('command rejection surfaces a protocol_error (WS-001)', async () => {
|
|
332
|
-
const { onMessage, sent, session } = setup();
|
|
333
|
-
(
|
|
334
|
-
session as unknown as { executeCommand: ReturnType<typeof vi.fn> }
|
|
335
|
-
).executeCommand.mockRejectedValueOnce(new Error('command failed'));
|
|
336
|
-
onMessage(JSON.stringify({ type: 'command', name: 'clear' }));
|
|
337
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
338
|
-
expect(sent).toEqual([{ type: 'protocol_error', message: 'command failed' }]);
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
it('forwards InteractiveSession events to client', () => {
|
|
342
|
-
const { session, sent } = setup();
|
|
343
|
-
(session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
|
|
344
|
-
'text_delta',
|
|
345
|
-
'hello',
|
|
346
|
-
);
|
|
347
|
-
expect(sent).toHaveLength(1);
|
|
348
|
-
expect(sent[0]).toEqual({ type: 'text_delta', delta: 'hello' });
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
it('forwards thinking event', () => {
|
|
352
|
-
const { session, sent } = setup();
|
|
353
|
-
(session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
|
|
354
|
-
'thinking',
|
|
355
|
-
true,
|
|
356
|
-
);
|
|
357
|
-
expect(sent[0]).toEqual({ type: 'thinking', isThinking: true });
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
it('forwards background task events to the client', () => {
|
|
361
|
-
const { session, sent } = setup();
|
|
362
|
-
const event: TBackgroundTaskEvent = {
|
|
363
|
-
type: 'background_task_text_delta',
|
|
364
|
-
taskId: 'task_1',
|
|
365
|
-
delta: 'partial',
|
|
366
|
-
};
|
|
367
|
-
(session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
|
|
368
|
-
'background_task_event',
|
|
369
|
-
event,
|
|
370
|
-
);
|
|
371
|
-
|
|
372
|
-
expect(sent[0]).toEqual({ type: 'background_task_event', event });
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
it('forwards background job group events to the client', () => {
|
|
376
|
-
const { session, sent } = setup();
|
|
377
|
-
const event: TBackgroundJobGroupEvent = {
|
|
378
|
-
type: 'background_job_group_completed',
|
|
379
|
-
group: { ...backgroundJobGroup, status: 'completed' },
|
|
380
|
-
};
|
|
381
|
-
(session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
|
|
382
|
-
'background_job_group_event',
|
|
383
|
-
event,
|
|
384
|
-
);
|
|
385
|
-
|
|
386
|
-
expect(sent[0]).toEqual({ type: 'background_job_group_event', event });
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it('forwards execution_workspace_event to the client', () => {
|
|
390
|
-
const { session, sent } = setup();
|
|
391
|
-
const event: IExecutionWorkspaceEvent = {
|
|
392
|
-
type: 'execution_workspace_updated',
|
|
393
|
-
cause: 'main_thread',
|
|
394
|
-
snapshot: executionWorkspaceSnapshot,
|
|
395
|
-
};
|
|
396
|
-
(session as unknown as { _emit: (e: string, ...args: unknown[]) => void })._emit(
|
|
397
|
-
'execution_workspace_event',
|
|
398
|
-
event,
|
|
399
|
-
);
|
|
400
|
-
|
|
401
|
-
expect(sent[0]).toEqual({
|
|
402
|
-
type: 'execution_workspace_event',
|
|
403
|
-
snapshot: executionWorkspaceSnapshot,
|
|
404
|
-
});
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
it('get-execution-workspace sends current snapshot', () => {
|
|
408
|
-
const { onMessage, sent, session } = setup();
|
|
409
|
-
onMessage(JSON.stringify({ type: 'get-execution-workspace' }));
|
|
410
|
-
|
|
411
|
-
expect(sent[0]).toEqual({
|
|
412
|
-
type: 'execution_workspace_event',
|
|
413
|
-
snapshot: executionWorkspaceSnapshot,
|
|
414
|
-
});
|
|
415
|
-
expect(
|
|
416
|
-
(
|
|
417
|
-
session as unknown as {
|
|
418
|
-
getExecutionWorkspaceSnapshot: ReturnType<typeof vi.fn>;
|
|
419
|
-
}
|
|
420
|
-
).getExecutionWorkspaceSnapshot,
|
|
421
|
-
).toHaveBeenCalled();
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
it('cleanup unsubscribes from all events', () => {
|
|
425
|
-
const { session, cleanup } = setup();
|
|
426
|
-
cleanup();
|
|
427
|
-
expect((session as unknown as { off: ReturnType<typeof vi.fn> }).off).toHaveBeenCalledTimes(11);
|
|
428
|
-
});
|
|
429
|
-
});
|
|
@@ -1,170 +0,0 @@
|
|
|
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
|
-
}
|