@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-handler.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebSocket transport adapter — exposes IInteractiveSession over WebSocket.
|
|
3
|
-
*
|
|
4
|
-
* Framework-agnostic: works with any WebSocket implementation via
|
|
5
|
-
* send/onMessage callbacks. No dependency on ws, uWebSockets, etc.
|
|
6
|
-
*
|
|
7
|
-
* Protocol: JSON messages with { type, ...payload } structure.
|
|
8
|
-
* Server pushes IInteractiveSession events to client in real-time.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
handleBackgroundControlMessage,
|
|
13
|
-
handleBackgroundQueryMessage,
|
|
14
|
-
} from './ws-background-messages.js';
|
|
15
|
-
|
|
16
|
-
import type { TClientMessage, TServerMessage } from './ws-protocol.js';
|
|
17
|
-
import type {
|
|
18
|
-
IExecutionResult,
|
|
19
|
-
IExecutionWorkspaceEvent,
|
|
20
|
-
IInteractiveSession,
|
|
21
|
-
IToolState,
|
|
22
|
-
TBackgroundJobGroupEvent,
|
|
23
|
-
} from '@robota-sdk/agent-interface-transport';
|
|
24
|
-
import type { TBackgroundTaskEvent } from '@robota-sdk/agent-interface-transport';
|
|
25
|
-
|
|
26
|
-
export interface IWsHandlerOptions {
|
|
27
|
-
/** IInteractiveSession to expose. */
|
|
28
|
-
session: IInteractiveSession;
|
|
29
|
-
/** Send a JSON message to the client. */
|
|
30
|
-
send: (message: TServerMessage) => void;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Create a WebSocket message handler for an IInteractiveSession.
|
|
35
|
-
*
|
|
36
|
-
* Returns:
|
|
37
|
-
* - `onMessage(data)`: call this when the WebSocket receives a message
|
|
38
|
-
* - `cleanup()`: call this when the WebSocket disconnects
|
|
39
|
-
*
|
|
40
|
-
* Usage:
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const { onMessage, cleanup } = createWsHandler({
|
|
43
|
-
* session: interactiveSession,
|
|
44
|
-
* send: (msg) => ws.send(JSON.stringify(msg)),
|
|
45
|
-
* });
|
|
46
|
-
*
|
|
47
|
-
* ws.on('message', (data) => onMessage(String(data)));
|
|
48
|
-
* ws.on('close', cleanup);
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
export function createWsHandler(options: IWsHandlerOptions): {
|
|
52
|
-
onMessage: (data: string) => void;
|
|
53
|
-
cleanup: () => void;
|
|
54
|
-
} {
|
|
55
|
-
const cleanup = subscribeSessionEvents(options.session, options.send);
|
|
56
|
-
const onMessage = createWsMessageHandler(options.session, options.send);
|
|
57
|
-
|
|
58
|
-
return { onMessage, cleanup };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function subscribeSessionEvents(
|
|
62
|
-
session: IInteractiveSession,
|
|
63
|
-
send: (message: TServerMessage) => void,
|
|
64
|
-
): () => void {
|
|
65
|
-
const onUserMessage = (content: string): void => send({ type: 'user_message', content });
|
|
66
|
-
const onTextDelta = (delta: string): void => send({ type: 'text_delta', delta });
|
|
67
|
-
const onToolStart = (state: IToolState): void => send({ type: 'tool_start', state });
|
|
68
|
-
const onToolEnd = (state: IToolState): void => send({ type: 'tool_end', state });
|
|
69
|
-
const onThinking = (isThinking: boolean): void => send({ type: 'thinking', isThinking });
|
|
70
|
-
const onComplete = (result: IExecutionResult): void => send({ type: 'complete', result });
|
|
71
|
-
const onInterrupted = (result: IExecutionResult): void => send({ type: 'interrupted', result });
|
|
72
|
-
const onError = (error: Error): void => send({ type: 'error', message: error.message });
|
|
73
|
-
const onBackgroundTaskEvent = (event: TBackgroundTaskEvent): void =>
|
|
74
|
-
send({ type: 'background_task_event', event });
|
|
75
|
-
const onBackgroundJobGroupEvent = (event: TBackgroundJobGroupEvent): void =>
|
|
76
|
-
send({ type: 'background_job_group_event', event });
|
|
77
|
-
const onExecutionWorkspace = (event: IExecutionWorkspaceEvent): void =>
|
|
78
|
-
send({ type: 'execution_workspace_event', snapshot: event.snapshot });
|
|
79
|
-
|
|
80
|
-
session.on('user_message', onUserMessage);
|
|
81
|
-
session.on('text_delta', onTextDelta);
|
|
82
|
-
session.on('tool_start', onToolStart);
|
|
83
|
-
session.on('tool_end', onToolEnd);
|
|
84
|
-
session.on('thinking', onThinking);
|
|
85
|
-
session.on('complete', onComplete);
|
|
86
|
-
session.on('interrupted', onInterrupted);
|
|
87
|
-
session.on('error', onError);
|
|
88
|
-
session.on('background_task_event', onBackgroundTaskEvent);
|
|
89
|
-
session.on('background_job_group_event', onBackgroundJobGroupEvent);
|
|
90
|
-
session.on('execution_workspace_event', onExecutionWorkspace);
|
|
91
|
-
|
|
92
|
-
return (): void => {
|
|
93
|
-
session.off('user_message', onUserMessage);
|
|
94
|
-
session.off('text_delta', onTextDelta);
|
|
95
|
-
session.off('tool_start', onToolStart);
|
|
96
|
-
session.off('tool_end', onToolEnd);
|
|
97
|
-
session.off('thinking', onThinking);
|
|
98
|
-
session.off('complete', onComplete);
|
|
99
|
-
session.off('interrupted', onInterrupted);
|
|
100
|
-
session.off('error', onError);
|
|
101
|
-
session.off('background_task_event', onBackgroundTaskEvent);
|
|
102
|
-
session.off('background_job_group_event', onBackgroundJobGroupEvent);
|
|
103
|
-
session.off('execution_workspace_event', onExecutionWorkspace);
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function createWsMessageHandler(
|
|
108
|
-
session: IInteractiveSession,
|
|
109
|
-
send: (message: TServerMessage) => void,
|
|
110
|
-
): (data: string) => void {
|
|
111
|
-
return (data: string): void => {
|
|
112
|
-
const msg = parseClientMessage(data, send);
|
|
113
|
-
if (!msg) return;
|
|
114
|
-
handleClientMessage(session, send, msg);
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function parseClientMessage(
|
|
119
|
-
data: string,
|
|
120
|
-
send: (message: TServerMessage) => void,
|
|
121
|
-
): TClientMessage | null {
|
|
122
|
-
try {
|
|
123
|
-
return JSON.parse(data) as TClientMessage;
|
|
124
|
-
} catch {
|
|
125
|
-
send({ type: 'protocol_error', message: 'Invalid JSON' });
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function handleClientMessage(
|
|
131
|
-
session: IInteractiveSession,
|
|
132
|
-
send: (message: TServerMessage) => void,
|
|
133
|
-
msg: TClientMessage,
|
|
134
|
-
): void {
|
|
135
|
-
if (isSessionControlMessage(msg)) {
|
|
136
|
-
handleSessionControlMessage(session, send, msg);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (isSessionQueryMessage(msg)) {
|
|
140
|
-
handleSessionQueryMessage(session, send, msg);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (isBackgroundQueryMessage(msg)) {
|
|
144
|
-
handleBackgroundQueryMessage(session, send, msg);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
if (isBackgroundControlMessage(msg)) {
|
|
148
|
-
handleBackgroundControlMessage(session, send, msg);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
send({ type: 'protocol_error', message: `Unknown message type: ${getMessageType(msg)}` });
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function getMessageType(msg: TClientMessage): string {
|
|
155
|
-
return (msg as { type: string }).type;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function isSessionControlMessage(
|
|
159
|
-
msg: TClientMessage,
|
|
160
|
-
): msg is Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }> {
|
|
161
|
-
return (
|
|
162
|
-
msg.type === 'submit' ||
|
|
163
|
-
msg.type === 'command' ||
|
|
164
|
-
msg.type === 'abort' ||
|
|
165
|
-
msg.type === 'cancel-queue'
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function isSessionQueryMessage(msg: TClientMessage): msg is Extract<
|
|
170
|
-
TClientMessage,
|
|
171
|
-
{
|
|
172
|
-
type:
|
|
173
|
-
| 'get-messages'
|
|
174
|
-
| 'get-context'
|
|
175
|
-
| 'get-executing'
|
|
176
|
-
| 'get-pending'
|
|
177
|
-
| 'get-execution-workspace';
|
|
178
|
-
}
|
|
179
|
-
> {
|
|
180
|
-
return (
|
|
181
|
-
msg.type === 'get-messages' ||
|
|
182
|
-
msg.type === 'get-context' ||
|
|
183
|
-
msg.type === 'get-executing' ||
|
|
184
|
-
msg.type === 'get-pending' ||
|
|
185
|
-
msg.type === 'get-execution-workspace'
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function isBackgroundQueryMessage(
|
|
190
|
-
msg: TClientMessage,
|
|
191
|
-
): msg is Extract<
|
|
192
|
-
TClientMessage,
|
|
193
|
-
| { type: 'get-background-tasks' | 'get-background-task' | 'read-background-task-log' }
|
|
194
|
-
| { type: 'get-background-job-groups' | 'get-background-job-group' | 'wait-background-job-group' }
|
|
195
|
-
> {
|
|
196
|
-
return (
|
|
197
|
-
msg.type === 'get-background-tasks' ||
|
|
198
|
-
msg.type === 'get-background-task' ||
|
|
199
|
-
msg.type === 'read-background-task-log' ||
|
|
200
|
-
msg.type === 'get-background-job-groups' ||
|
|
201
|
-
msg.type === 'get-background-job-group' ||
|
|
202
|
-
msg.type === 'wait-background-job-group'
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function isBackgroundControlMessage(
|
|
207
|
-
msg: TClientMessage,
|
|
208
|
-
): msg is Extract<
|
|
209
|
-
TClientMessage,
|
|
210
|
-
{ type: 'cancel-background-task' | 'close-background-task' | 'send-background-task' }
|
|
211
|
-
> {
|
|
212
|
-
return (
|
|
213
|
-
msg.type === 'cancel-background-task' ||
|
|
214
|
-
msg.type === 'close-background-task' ||
|
|
215
|
-
msg.type === 'send-background-task'
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function handleSessionControlMessage(
|
|
220
|
-
session: IInteractiveSession,
|
|
221
|
-
send: (message: TServerMessage) => void,
|
|
222
|
-
msg: Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }>,
|
|
223
|
-
): void {
|
|
224
|
-
if (msg.type === 'submit') {
|
|
225
|
-
if (!msg.prompt) {
|
|
226
|
-
send({ type: 'protocol_error', message: 'prompt is required' });
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
session.submit(msg.prompt).catch((error: Error) => {
|
|
230
|
-
send({ type: 'protocol_error', message: error.message });
|
|
231
|
-
});
|
|
232
|
-
} else if (msg.type === 'command') {
|
|
233
|
-
if (!msg.name) {
|
|
234
|
-
send({ type: 'protocol_error', message: 'name is required' });
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
session.executeCommand(msg.name, msg.args ?? '').then(
|
|
238
|
-
(result) => {
|
|
239
|
-
send({
|
|
240
|
-
type: 'command_result',
|
|
241
|
-
name: msg.name,
|
|
242
|
-
message: result?.message ?? `Unknown command: ${msg.name}`,
|
|
243
|
-
success: result?.success ?? false,
|
|
244
|
-
data: result?.data,
|
|
245
|
-
});
|
|
246
|
-
},
|
|
247
|
-
(error: Error) => {
|
|
248
|
-
send({ type: 'protocol_error', message: error.message });
|
|
249
|
-
},
|
|
250
|
-
);
|
|
251
|
-
} else if (msg.type === 'abort') {
|
|
252
|
-
session.abort();
|
|
253
|
-
} else {
|
|
254
|
-
session.cancelQueue();
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function handleSessionQueryMessage(
|
|
259
|
-
session: IInteractiveSession,
|
|
260
|
-
send: (message: TServerMessage) => void,
|
|
261
|
-
msg: Extract<
|
|
262
|
-
TClientMessage,
|
|
263
|
-
{
|
|
264
|
-
type:
|
|
265
|
-
| 'get-messages'
|
|
266
|
-
| 'get-context'
|
|
267
|
-
| 'get-executing'
|
|
268
|
-
| 'get-pending'
|
|
269
|
-
| 'get-execution-workspace';
|
|
270
|
-
}
|
|
271
|
-
>,
|
|
272
|
-
): void {
|
|
273
|
-
if (msg.type === 'get-messages') {
|
|
274
|
-
send({ type: 'messages', messages: session.getMessages() });
|
|
275
|
-
} else if (msg.type === 'get-context') {
|
|
276
|
-
send({ type: 'context', state: session.getContextState() });
|
|
277
|
-
} else if (msg.type === 'get-executing') {
|
|
278
|
-
send({ type: 'executing', executing: session.isExecuting() });
|
|
279
|
-
} else if (msg.type === 'get-execution-workspace') {
|
|
280
|
-
send({
|
|
281
|
-
type: 'execution_workspace_event',
|
|
282
|
-
snapshot: session.getExecutionWorkspaceSnapshot(),
|
|
283
|
-
});
|
|
284
|
-
} else {
|
|
285
|
-
send({ type: 'pending', pending: session.getPendingPrompt() });
|
|
286
|
-
}
|
|
287
|
-
}
|
package/src/ws-protocol.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { IInteractiveSession } from '@robota-sdk/agent-interface-transport';
|
|
2
|
-
import type {
|
|
3
|
-
IBackgroundJobGroupState,
|
|
4
|
-
ICommandResult,
|
|
5
|
-
IExecutionResult,
|
|
6
|
-
IExecutionWorkspaceSnapshot,
|
|
7
|
-
IToolState,
|
|
8
|
-
TBackgroundJobGroupEvent,
|
|
9
|
-
} from '@robota-sdk/agent-interface-transport';
|
|
10
|
-
import type {
|
|
11
|
-
IBackgroundTaskInput,
|
|
12
|
-
IBackgroundTaskListFilter,
|
|
13
|
-
IBackgroundTaskLogCursor,
|
|
14
|
-
IBackgroundTaskLogPage,
|
|
15
|
-
IBackgroundTaskState,
|
|
16
|
-
TBackgroundTaskEvent,
|
|
17
|
-
} from '@robota-sdk/agent-interface-transport';
|
|
18
|
-
|
|
19
|
-
export type TBackgroundControlAction = 'cancel' | 'close' | 'send';
|
|
20
|
-
|
|
21
|
-
/** Inbound message from client to server. */
|
|
22
|
-
export type TClientMessage =
|
|
23
|
-
| { type: 'submit'; prompt: string }
|
|
24
|
-
| { type: 'command'; name: string; args?: string }
|
|
25
|
-
| { type: 'abort' }
|
|
26
|
-
| { type: 'cancel-queue' }
|
|
27
|
-
| { type: 'get-messages' }
|
|
28
|
-
| { type: 'get-context' }
|
|
29
|
-
| { type: 'get-executing' }
|
|
30
|
-
| { type: 'get-pending' }
|
|
31
|
-
| { type: 'get-execution-workspace' }
|
|
32
|
-
| { type: 'get-background-tasks'; filter?: IBackgroundTaskListFilter }
|
|
33
|
-
| { type: 'get-background-task'; taskId: string }
|
|
34
|
-
| { type: 'get-background-job-groups' }
|
|
35
|
-
| { type: 'get-background-job-group'; groupId: string }
|
|
36
|
-
| { type: 'wait-background-job-group'; groupId: string }
|
|
37
|
-
| { type: 'cancel-background-task'; taskId: string; reason?: string }
|
|
38
|
-
| { type: 'close-background-task'; taskId: string }
|
|
39
|
-
| { type: 'send-background-task'; taskId: string; input: IBackgroundTaskInput }
|
|
40
|
-
| { type: 'read-background-task-log'; taskId: string; cursor?: IBackgroundTaskLogCursor };
|
|
41
|
-
|
|
42
|
-
/** Outbound message from server to client. */
|
|
43
|
-
export type TServerMessage =
|
|
44
|
-
| { type: 'text_delta'; delta: string }
|
|
45
|
-
| { type: 'user_message'; content: string }
|
|
46
|
-
| { type: 'tool_start'; state: IToolState }
|
|
47
|
-
| { type: 'tool_end'; state: IToolState }
|
|
48
|
-
| { type: 'thinking'; isThinking: boolean }
|
|
49
|
-
| { type: 'complete'; result: IExecutionResult }
|
|
50
|
-
| { type: 'interrupted'; result: IExecutionResult }
|
|
51
|
-
| { type: 'error'; message: string }
|
|
52
|
-
| {
|
|
53
|
-
type: 'command_result';
|
|
54
|
-
name: string;
|
|
55
|
-
message: string;
|
|
56
|
-
success: boolean;
|
|
57
|
-
data?: ICommandResult['data'];
|
|
58
|
-
}
|
|
59
|
-
| { type: 'messages'; messages: ReturnType<IInteractiveSession['getMessages']> }
|
|
60
|
-
| { type: 'context'; state: ReturnType<IInteractiveSession['getContextState']> }
|
|
61
|
-
| { type: 'executing'; executing: boolean }
|
|
62
|
-
| { type: 'pending'; pending: string | null }
|
|
63
|
-
| { type: 'execution_workspace_event'; snapshot: IExecutionWorkspaceSnapshot }
|
|
64
|
-
| { type: 'background_task_event'; event: TBackgroundTaskEvent }
|
|
65
|
-
| { type: 'background_job_group_event'; event: TBackgroundJobGroupEvent }
|
|
66
|
-
| { type: 'background_tasks'; tasks: IBackgroundTaskState[] }
|
|
67
|
-
| { type: 'background_task'; taskId: string; task: IBackgroundTaskState | null }
|
|
68
|
-
| { type: 'background_job_groups'; groups: IBackgroundJobGroupState[] }
|
|
69
|
-
| { type: 'background_job_group'; groupId: string; group: IBackgroundJobGroupState | null }
|
|
70
|
-
| { type: 'background_task_log'; taskId: string; page: IBackgroundTaskLogPage }
|
|
71
|
-
| {
|
|
72
|
-
type: 'background_task_control_result';
|
|
73
|
-
action: TBackgroundControlAction;
|
|
74
|
-
taskId: string;
|
|
75
|
-
success: boolean;
|
|
76
|
-
message?: string;
|
|
77
|
-
}
|
|
78
|
-
| { type: 'protocol_error'; message: string };
|