@robota-sdk/agent-transport-ws 3.0.0-beta.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/dist/node/index.cjs +114 -0
- package/dist/node/index.d.cts +110 -0
- package/dist/node/index.d.ts +110 -0
- package/dist/node/index.js +87 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Robota Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @robota-sdk/agent-transport-ws
|
|
2
|
+
|
|
3
|
+
Framework-agnostic WebSocket transport adapter for exposing `InteractiveSession` over real-time bidirectional connections. Works with any WebSocket implementation (Node.js `ws`, browser `WebSocket`, uWebSockets, etc.) via send/onMessage callbacks — no WebSocket library dependency.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @robota-sdk/agent-transport-ws
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createWsHandler } from '@robota-sdk/agent-transport-ws';
|
|
15
|
+
import { InteractiveSession, SystemCommandExecutor } from '@robota-sdk/agent-sdk';
|
|
16
|
+
import { WebSocketServer } from 'ws';
|
|
17
|
+
|
|
18
|
+
const wss = new WebSocketServer({ port: 8080 });
|
|
19
|
+
|
|
20
|
+
wss.on('connection', (ws) => {
|
|
21
|
+
const { onMessage, cleanup } = createWsHandler({
|
|
22
|
+
session: interactiveSession,
|
|
23
|
+
commandExecutor,
|
|
24
|
+
send: (msg) => ws.send(JSON.stringify(msg)),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
ws.on('message', (data) => onMessage(String(data)));
|
|
28
|
+
ws.on('close', cleanup);
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Message Protocol
|
|
33
|
+
|
|
34
|
+
All messages are JSON-encoded objects with a `type` field.
|
|
35
|
+
|
|
36
|
+
### Client to Server
|
|
37
|
+
|
|
38
|
+
| type | payload | maps to |
|
|
39
|
+
| --------------- | --------------------------------- | ---------------------------- |
|
|
40
|
+
| `submit` | `{ prompt: string }` | `session.submit(prompt)` |
|
|
41
|
+
| `command` | `{ name: string, args?: string }` | `commandExecutor.execute()` |
|
|
42
|
+
| `abort` | — | `session.abort()` |
|
|
43
|
+
| `cancel-queue` | — | `session.cancelQueue()` |
|
|
44
|
+
| `get-messages` | — | `session.getMessages()` |
|
|
45
|
+
| `get-context` | — | `session.getContextState()` |
|
|
46
|
+
| `get-executing` | — | `session.isExecuting()` |
|
|
47
|
+
| `get-pending` | — | `session.getPendingPrompt()` |
|
|
48
|
+
|
|
49
|
+
### Server to Client (pushed events)
|
|
50
|
+
|
|
51
|
+
| type | payload | source |
|
|
52
|
+
| ---------------- | ----------------------------------- | ------------------------ |
|
|
53
|
+
| `text_delta` | `{ delta: string }` | InteractiveSession event |
|
|
54
|
+
| `tool_start` | `{ state: IToolState }` | InteractiveSession event |
|
|
55
|
+
| `tool_end` | `{ state: IToolState }` | InteractiveSession event |
|
|
56
|
+
| `thinking` | `{ isThinking: boolean }` | InteractiveSession event |
|
|
57
|
+
| `complete` | `{ result: IExecutionResult }` | InteractiveSession event |
|
|
58
|
+
| `interrupted` | `{ result: IExecutionResult }` | InteractiveSession event |
|
|
59
|
+
| `error` | `{ message: string }` | InteractiveSession event |
|
|
60
|
+
| `command_result` | `{ name, message, success, data? }` | command response |
|
|
61
|
+
| `messages` | `{ messages: TUniversalMessage[] }` | get-messages response |
|
|
62
|
+
| `context` | `{ state: IContextWindowState }` | get-context response |
|
|
63
|
+
| `executing` | `{ executing: boolean }` | get-executing response |
|
|
64
|
+
| `pending` | `{ pending: string\|null }` | get-pending response |
|
|
65
|
+
| `protocol_error` | `{ message: string }` | invalid client message |
|
|
66
|
+
|
|
67
|
+
## Dependencies
|
|
68
|
+
|
|
69
|
+
- `@robota-sdk/agent-sdk` — `InteractiveSession`, `SystemCommandExecutor`
|
|
70
|
+
- No WebSocket library dependency (framework-agnostic)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createWsHandler: () => createWsHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/ws-handler.ts
|
|
28
|
+
function createWsHandler(options) {
|
|
29
|
+
const { session, commandExecutor, send } = options;
|
|
30
|
+
const onTextDelta = (delta) => send({ type: "text_delta", delta });
|
|
31
|
+
const onToolStart = (state) => send({ type: "tool_start", state });
|
|
32
|
+
const onToolEnd = (state) => send({ type: "tool_end", state });
|
|
33
|
+
const onThinking = (isThinking) => send({ type: "thinking", isThinking });
|
|
34
|
+
const onComplete = (result) => send({ type: "complete", result });
|
|
35
|
+
const onInterrupted = (result) => send({ type: "interrupted", result });
|
|
36
|
+
const onError = (error) => send({ type: "error", message: error.message });
|
|
37
|
+
session.on("text_delta", onTextDelta);
|
|
38
|
+
session.on("tool_start", onToolStart);
|
|
39
|
+
session.on("tool_end", onToolEnd);
|
|
40
|
+
session.on("thinking", onThinking);
|
|
41
|
+
session.on("complete", onComplete);
|
|
42
|
+
session.on("interrupted", onInterrupted);
|
|
43
|
+
session.on("error", onError);
|
|
44
|
+
const onMessage = (data) => {
|
|
45
|
+
let msg;
|
|
46
|
+
try {
|
|
47
|
+
msg = JSON.parse(data);
|
|
48
|
+
} catch {
|
|
49
|
+
send({ type: "protocol_error", message: "Invalid JSON" });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
switch (msg.type) {
|
|
53
|
+
case "submit":
|
|
54
|
+
if (!msg.prompt) {
|
|
55
|
+
send({ type: "protocol_error", message: "prompt is required" });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
session.submit(msg.prompt);
|
|
59
|
+
break;
|
|
60
|
+
case "command":
|
|
61
|
+
if (!msg.name) {
|
|
62
|
+
send({ type: "protocol_error", message: "name is required" });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
commandExecutor.execute(msg.name, session, msg.args ?? "").then((result) => {
|
|
66
|
+
send({
|
|
67
|
+
type: "command_result",
|
|
68
|
+
name: msg.name,
|
|
69
|
+
message: result?.message ?? `Unknown command: ${msg.name}`,
|
|
70
|
+
success: result?.success ?? false,
|
|
71
|
+
data: result?.data
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
break;
|
|
75
|
+
case "abort":
|
|
76
|
+
session.abort();
|
|
77
|
+
break;
|
|
78
|
+
case "cancel-queue":
|
|
79
|
+
session.cancelQueue();
|
|
80
|
+
break;
|
|
81
|
+
case "get-messages":
|
|
82
|
+
send({ type: "messages", messages: session.getMessages() });
|
|
83
|
+
break;
|
|
84
|
+
case "get-context":
|
|
85
|
+
send({ type: "context", state: session.getContextState() });
|
|
86
|
+
break;
|
|
87
|
+
case "get-executing":
|
|
88
|
+
send({ type: "executing", executing: session.isExecuting() });
|
|
89
|
+
break;
|
|
90
|
+
case "get-pending":
|
|
91
|
+
send({ type: "pending", pending: session.getPendingPrompt() });
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
send({
|
|
95
|
+
type: "protocol_error",
|
|
96
|
+
message: `Unknown message type: ${msg.type}`
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const cleanup = () => {
|
|
101
|
+
session.off("text_delta", onTextDelta);
|
|
102
|
+
session.off("tool_start", onToolStart);
|
|
103
|
+
session.off("tool_end", onToolEnd);
|
|
104
|
+
session.off("thinking", onThinking);
|
|
105
|
+
session.off("complete", onComplete);
|
|
106
|
+
session.off("interrupted", onInterrupted);
|
|
107
|
+
session.off("error", onError);
|
|
108
|
+
};
|
|
109
|
+
return { onMessage, cleanup };
|
|
110
|
+
}
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
createWsHandler
|
|
114
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { InteractiveSession, SystemCommandExecutor, IToolState, IExecutionResult } from '@robota-sdk/agent-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WebSocket transport adapter — exposes InteractiveSession over WebSocket.
|
|
5
|
+
*
|
|
6
|
+
* Framework-agnostic: works with any WebSocket implementation via
|
|
7
|
+
* send/onMessage callbacks. No dependency on ws, uWebSockets, etc.
|
|
8
|
+
*
|
|
9
|
+
* Protocol: JSON messages with { type, ...payload } structure.
|
|
10
|
+
* Server pushes InteractiveSession events to client in real-time.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Inbound message from client → server. */
|
|
14
|
+
type TClientMessage = {
|
|
15
|
+
type: 'submit';
|
|
16
|
+
prompt: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'command';
|
|
19
|
+
name: string;
|
|
20
|
+
args?: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'abort';
|
|
23
|
+
} | {
|
|
24
|
+
type: 'cancel-queue';
|
|
25
|
+
} | {
|
|
26
|
+
type: 'get-messages';
|
|
27
|
+
} | {
|
|
28
|
+
type: 'get-context';
|
|
29
|
+
} | {
|
|
30
|
+
type: 'get-executing';
|
|
31
|
+
} | {
|
|
32
|
+
type: 'get-pending';
|
|
33
|
+
};
|
|
34
|
+
/** Outbound message from server → client. */
|
|
35
|
+
type TServerMessage = {
|
|
36
|
+
type: 'text_delta';
|
|
37
|
+
delta: string;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'tool_start';
|
|
40
|
+
state: IToolState;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'tool_end';
|
|
43
|
+
state: IToolState;
|
|
44
|
+
} | {
|
|
45
|
+
type: 'thinking';
|
|
46
|
+
isThinking: boolean;
|
|
47
|
+
} | {
|
|
48
|
+
type: 'complete';
|
|
49
|
+
result: IExecutionResult;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'interrupted';
|
|
52
|
+
result: IExecutionResult;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'error';
|
|
55
|
+
message: string;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'command_result';
|
|
58
|
+
name: string;
|
|
59
|
+
message: string;
|
|
60
|
+
success: boolean;
|
|
61
|
+
data?: Record<string, unknown>;
|
|
62
|
+
} | {
|
|
63
|
+
type: 'messages';
|
|
64
|
+
messages: unknown[];
|
|
65
|
+
} | {
|
|
66
|
+
type: 'context';
|
|
67
|
+
state: unknown;
|
|
68
|
+
} | {
|
|
69
|
+
type: 'executing';
|
|
70
|
+
executing: boolean;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'pending';
|
|
73
|
+
pending: string | null;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'protocol_error';
|
|
76
|
+
message: string;
|
|
77
|
+
};
|
|
78
|
+
interface IWsHandlerOptions {
|
|
79
|
+
/** InteractiveSession to expose. */
|
|
80
|
+
session: InteractiveSession;
|
|
81
|
+
/** System command executor. */
|
|
82
|
+
commandExecutor: SystemCommandExecutor;
|
|
83
|
+
/** Send a JSON message to the client. */
|
|
84
|
+
send: (message: TServerMessage) => void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create a WebSocket message handler for an InteractiveSession.
|
|
88
|
+
*
|
|
89
|
+
* Returns:
|
|
90
|
+
* - `onMessage(data)`: call this when the WebSocket receives a message
|
|
91
|
+
* - `cleanup()`: call this when the WebSocket disconnects
|
|
92
|
+
*
|
|
93
|
+
* Usage:
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const { onMessage, cleanup } = createWsHandler({
|
|
96
|
+
* session: interactiveSession,
|
|
97
|
+
* commandExecutor,
|
|
98
|
+
* send: (msg) => ws.send(JSON.stringify(msg)),
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* ws.on('message', (data) => onMessage(String(data)));
|
|
102
|
+
* ws.on('close', cleanup);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function createWsHandler(options: IWsHandlerOptions): {
|
|
106
|
+
onMessage: (data: string) => void;
|
|
107
|
+
cleanup: () => void;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export { type IWsHandlerOptions, type TClientMessage, type TServerMessage, createWsHandler };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { InteractiveSession, SystemCommandExecutor, IToolState, IExecutionResult } from '@robota-sdk/agent-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WebSocket transport adapter — exposes InteractiveSession over WebSocket.
|
|
5
|
+
*
|
|
6
|
+
* Framework-agnostic: works with any WebSocket implementation via
|
|
7
|
+
* send/onMessage callbacks. No dependency on ws, uWebSockets, etc.
|
|
8
|
+
*
|
|
9
|
+
* Protocol: JSON messages with { type, ...payload } structure.
|
|
10
|
+
* Server pushes InteractiveSession events to client in real-time.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Inbound message from client → server. */
|
|
14
|
+
type TClientMessage = {
|
|
15
|
+
type: 'submit';
|
|
16
|
+
prompt: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'command';
|
|
19
|
+
name: string;
|
|
20
|
+
args?: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'abort';
|
|
23
|
+
} | {
|
|
24
|
+
type: 'cancel-queue';
|
|
25
|
+
} | {
|
|
26
|
+
type: 'get-messages';
|
|
27
|
+
} | {
|
|
28
|
+
type: 'get-context';
|
|
29
|
+
} | {
|
|
30
|
+
type: 'get-executing';
|
|
31
|
+
} | {
|
|
32
|
+
type: 'get-pending';
|
|
33
|
+
};
|
|
34
|
+
/** Outbound message from server → client. */
|
|
35
|
+
type TServerMessage = {
|
|
36
|
+
type: 'text_delta';
|
|
37
|
+
delta: string;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'tool_start';
|
|
40
|
+
state: IToolState;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'tool_end';
|
|
43
|
+
state: IToolState;
|
|
44
|
+
} | {
|
|
45
|
+
type: 'thinking';
|
|
46
|
+
isThinking: boolean;
|
|
47
|
+
} | {
|
|
48
|
+
type: 'complete';
|
|
49
|
+
result: IExecutionResult;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'interrupted';
|
|
52
|
+
result: IExecutionResult;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'error';
|
|
55
|
+
message: string;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'command_result';
|
|
58
|
+
name: string;
|
|
59
|
+
message: string;
|
|
60
|
+
success: boolean;
|
|
61
|
+
data?: Record<string, unknown>;
|
|
62
|
+
} | {
|
|
63
|
+
type: 'messages';
|
|
64
|
+
messages: unknown[];
|
|
65
|
+
} | {
|
|
66
|
+
type: 'context';
|
|
67
|
+
state: unknown;
|
|
68
|
+
} | {
|
|
69
|
+
type: 'executing';
|
|
70
|
+
executing: boolean;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'pending';
|
|
73
|
+
pending: string | null;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'protocol_error';
|
|
76
|
+
message: string;
|
|
77
|
+
};
|
|
78
|
+
interface IWsHandlerOptions {
|
|
79
|
+
/** InteractiveSession to expose. */
|
|
80
|
+
session: InteractiveSession;
|
|
81
|
+
/** System command executor. */
|
|
82
|
+
commandExecutor: SystemCommandExecutor;
|
|
83
|
+
/** Send a JSON message to the client. */
|
|
84
|
+
send: (message: TServerMessage) => void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create a WebSocket message handler for an InteractiveSession.
|
|
88
|
+
*
|
|
89
|
+
* Returns:
|
|
90
|
+
* - `onMessage(data)`: call this when the WebSocket receives a message
|
|
91
|
+
* - `cleanup()`: call this when the WebSocket disconnects
|
|
92
|
+
*
|
|
93
|
+
* Usage:
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const { onMessage, cleanup } = createWsHandler({
|
|
96
|
+
* session: interactiveSession,
|
|
97
|
+
* commandExecutor,
|
|
98
|
+
* send: (msg) => ws.send(JSON.stringify(msg)),
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* ws.on('message', (data) => onMessage(String(data)));
|
|
102
|
+
* ws.on('close', cleanup);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function createWsHandler(options: IWsHandlerOptions): {
|
|
106
|
+
onMessage: (data: string) => void;
|
|
107
|
+
cleanup: () => void;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export { type IWsHandlerOptions, type TClientMessage, type TServerMessage, createWsHandler };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/ws-handler.ts
|
|
2
|
+
function createWsHandler(options) {
|
|
3
|
+
const { session, commandExecutor, send } = options;
|
|
4
|
+
const onTextDelta = (delta) => send({ type: "text_delta", delta });
|
|
5
|
+
const onToolStart = (state) => send({ type: "tool_start", state });
|
|
6
|
+
const onToolEnd = (state) => send({ type: "tool_end", state });
|
|
7
|
+
const onThinking = (isThinking) => send({ type: "thinking", isThinking });
|
|
8
|
+
const onComplete = (result) => send({ type: "complete", result });
|
|
9
|
+
const onInterrupted = (result) => send({ type: "interrupted", result });
|
|
10
|
+
const onError = (error) => send({ type: "error", message: error.message });
|
|
11
|
+
session.on("text_delta", onTextDelta);
|
|
12
|
+
session.on("tool_start", onToolStart);
|
|
13
|
+
session.on("tool_end", onToolEnd);
|
|
14
|
+
session.on("thinking", onThinking);
|
|
15
|
+
session.on("complete", onComplete);
|
|
16
|
+
session.on("interrupted", onInterrupted);
|
|
17
|
+
session.on("error", onError);
|
|
18
|
+
const onMessage = (data) => {
|
|
19
|
+
let msg;
|
|
20
|
+
try {
|
|
21
|
+
msg = JSON.parse(data);
|
|
22
|
+
} catch {
|
|
23
|
+
send({ type: "protocol_error", message: "Invalid JSON" });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
switch (msg.type) {
|
|
27
|
+
case "submit":
|
|
28
|
+
if (!msg.prompt) {
|
|
29
|
+
send({ type: "protocol_error", message: "prompt is required" });
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
session.submit(msg.prompt);
|
|
33
|
+
break;
|
|
34
|
+
case "command":
|
|
35
|
+
if (!msg.name) {
|
|
36
|
+
send({ type: "protocol_error", message: "name is required" });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
commandExecutor.execute(msg.name, session, msg.args ?? "").then((result) => {
|
|
40
|
+
send({
|
|
41
|
+
type: "command_result",
|
|
42
|
+
name: msg.name,
|
|
43
|
+
message: result?.message ?? `Unknown command: ${msg.name}`,
|
|
44
|
+
success: result?.success ?? false,
|
|
45
|
+
data: result?.data
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
break;
|
|
49
|
+
case "abort":
|
|
50
|
+
session.abort();
|
|
51
|
+
break;
|
|
52
|
+
case "cancel-queue":
|
|
53
|
+
session.cancelQueue();
|
|
54
|
+
break;
|
|
55
|
+
case "get-messages":
|
|
56
|
+
send({ type: "messages", messages: session.getMessages() });
|
|
57
|
+
break;
|
|
58
|
+
case "get-context":
|
|
59
|
+
send({ type: "context", state: session.getContextState() });
|
|
60
|
+
break;
|
|
61
|
+
case "get-executing":
|
|
62
|
+
send({ type: "executing", executing: session.isExecuting() });
|
|
63
|
+
break;
|
|
64
|
+
case "get-pending":
|
|
65
|
+
send({ type: "pending", pending: session.getPendingPrompt() });
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
send({
|
|
69
|
+
type: "protocol_error",
|
|
70
|
+
message: `Unknown message type: ${msg.type}`
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const cleanup = () => {
|
|
75
|
+
session.off("text_delta", onTextDelta);
|
|
76
|
+
session.off("tool_start", onToolStart);
|
|
77
|
+
session.off("tool_end", onToolEnd);
|
|
78
|
+
session.off("thinking", onThinking);
|
|
79
|
+
session.off("complete", onComplete);
|
|
80
|
+
session.off("interrupted", onInterrupted);
|
|
81
|
+
session.off("error", onError);
|
|
82
|
+
};
|
|
83
|
+
return { onMessage, cleanup };
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
createWsHandler
|
|
87
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@robota-sdk/agent-transport-ws",
|
|
3
|
+
"version": "3.0.0-beta.43",
|
|
4
|
+
"description": "WebSocket transport adapter for exposing InteractiveSession over real-time connections",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/node/index.js",
|
|
7
|
+
"types": "dist/node/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/node/index.d.ts",
|
|
11
|
+
"node": {
|
|
12
|
+
"import": "./dist/node/index.js",
|
|
13
|
+
"require": "./dist/node/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"default": {
|
|
16
|
+
"import": "./dist/node/index.js",
|
|
17
|
+
"require": "./dist/node/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@robota-sdk/agent-sdk": "3.0.0-beta.43"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rimraf": "^5.0.5",
|
|
29
|
+
"tsup": "^8.0.1",
|
|
30
|
+
"typescript": "^5.3.3",
|
|
31
|
+
"vitest": "^1.6.1"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --out-dir dist/node --clean",
|
|
39
|
+
"test": "vitest run --passWithNoTests",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"lint": "eslint src/ --ext .ts",
|
|
42
|
+
"clean": "rimraf dist"
|
|
43
|
+
}
|
|
44
|
+
}
|