agent-relay 2.1.5 → 2.1.6
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 +85 -236
- package/dist/index.cjs +75 -12
- package/package.json +18 -18
- package/packages/api-types/package.json +1 -1
- package/packages/benchmark/package.json +4 -4
- package/packages/bridge/package.json +8 -8
- package/packages/cli-tester/package.json +1 -1
- package/packages/config/package.json +2 -2
- package/packages/continuity/package.json +2 -2
- package/packages/daemon/dist/server.d.ts +5 -0
- package/packages/daemon/dist/server.d.ts.map +1 -1
- package/packages/daemon/dist/server.js +31 -0
- package/packages/daemon/dist/server.js.map +1 -1
- package/packages/daemon/package.json +12 -12
- package/packages/daemon/src/server.ts +37 -0
- package/packages/hooks/package.json +4 -4
- package/packages/mcp/dist/cloud.d.ts +7 -114
- package/packages/mcp/dist/cloud.d.ts.map +1 -1
- package/packages/mcp/dist/cloud.js +21 -431
- package/packages/mcp/dist/cloud.js.map +1 -1
- package/packages/mcp/dist/errors.d.ts +4 -22
- package/packages/mcp/dist/errors.d.ts.map +1 -1
- package/packages/mcp/dist/errors.js +4 -43
- package/packages/mcp/dist/errors.js.map +1 -1
- package/packages/mcp/dist/hybrid-client.d.ts.map +1 -1
- package/packages/mcp/dist/hybrid-client.js +7 -1
- package/packages/mcp/dist/hybrid-client.js.map +1 -1
- package/packages/mcp/package.json +4 -3
- package/packages/mcp/src/cloud.ts +29 -511
- package/packages/mcp/src/errors.ts +12 -49
- package/packages/mcp/src/hybrid-client.ts +8 -1
- package/packages/mcp/tests/discover.test.ts +72 -11
- package/packages/memory/package.json +2 -2
- package/packages/policy/package.json +2 -2
- package/packages/protocol/dist/types.d.ts +17 -1
- package/packages/protocol/dist/types.d.ts.map +1 -1
- package/packages/protocol/package.json +1 -1
- package/packages/protocol/src/types.ts +23 -0
- package/packages/resiliency/package.json +1 -1
- package/packages/sdk/dist/browser-client.d.ts +212 -0
- package/packages/sdk/dist/browser-client.d.ts.map +1 -0
- package/packages/sdk/dist/browser-client.js +750 -0
- package/packages/sdk/dist/browser-client.js.map +1 -0
- package/packages/sdk/dist/browser-framing.d.ts +46 -0
- package/packages/sdk/dist/browser-framing.d.ts.map +1 -0
- package/packages/sdk/dist/browser-framing.js +122 -0
- package/packages/sdk/dist/browser-framing.js.map +1 -0
- package/packages/sdk/dist/client.d.ts +129 -2
- package/packages/sdk/dist/client.d.ts.map +1 -1
- package/packages/sdk/dist/client.js +312 -2
- package/packages/sdk/dist/client.js.map +1 -1
- package/packages/sdk/dist/discovery.d.ts +10 -0
- package/packages/sdk/dist/discovery.d.ts.map +1 -0
- package/packages/sdk/dist/discovery.js +22 -0
- package/packages/sdk/dist/discovery.js.map +1 -0
- package/packages/sdk/dist/errors.d.ts +9 -0
- package/packages/sdk/dist/errors.d.ts.map +1 -0
- package/packages/sdk/dist/errors.js +9 -0
- package/packages/sdk/dist/errors.js.map +1 -0
- package/packages/sdk/dist/index.d.ts +18 -2
- package/packages/sdk/dist/index.d.ts.map +1 -1
- package/packages/sdk/dist/index.js +27 -1
- package/packages/sdk/dist/index.js.map +1 -1
- package/packages/sdk/dist/transports/index.d.ts +92 -0
- package/packages/sdk/dist/transports/index.d.ts.map +1 -0
- package/packages/sdk/dist/transports/index.js +129 -0
- package/packages/sdk/dist/transports/index.js.map +1 -0
- package/packages/sdk/dist/transports/socket-transport.d.ts +30 -0
- package/packages/sdk/dist/transports/socket-transport.d.ts.map +1 -0
- package/packages/sdk/dist/transports/socket-transport.js +94 -0
- package/packages/sdk/dist/transports/socket-transport.js.map +1 -0
- package/packages/sdk/dist/transports/types.d.ts +69 -0
- package/packages/sdk/dist/transports/types.d.ts.map +1 -0
- package/packages/sdk/dist/transports/types.js +10 -0
- package/packages/sdk/dist/transports/types.js.map +1 -0
- package/packages/sdk/dist/transports/websocket-transport.d.ts +55 -0
- package/packages/sdk/dist/transports/websocket-transport.d.ts.map +1 -0
- package/packages/sdk/dist/transports/websocket-transport.js +180 -0
- package/packages/sdk/dist/transports/websocket-transport.js.map +1 -0
- package/packages/sdk/package.json +28 -4
- package/packages/sdk/src/browser-client.ts +985 -0
- package/packages/sdk/src/browser-framing.test.ts +115 -0
- package/packages/sdk/src/browser-framing.ts +150 -0
- package/packages/sdk/src/client.test.ts +425 -0
- package/packages/sdk/src/client.ts +397 -3
- package/packages/sdk/src/discovery.ts +38 -0
- package/packages/sdk/src/errors.ts +17 -0
- package/packages/sdk/src/index.ts +82 -1
- package/packages/sdk/src/transports/index.ts +197 -0
- package/packages/sdk/src/transports/socket-transport.ts +115 -0
- package/packages/sdk/src/transports/types.ts +77 -0
- package/packages/sdk/src/transports/websocket-transport.ts +245 -0
- package/packages/sdk/tsconfig.json +1 -1
- package/packages/spawner/package.json +1 -1
- package/packages/state/package.json +1 -1
- package/packages/storage/package.json +2 -2
- package/packages/storage/src/jsonl-adapter.test.ts +8 -3
- package/packages/telemetry/package.json +1 -1
- package/packages/trajectory/package.json +2 -2
- package/packages/user-directory/package.json +2 -2
- package/packages/utils/dist/cjs/discovery.js +328 -0
- package/packages/utils/dist/cjs/errors.js +81 -0
- package/packages/utils/dist/discovery.d.ts +123 -0
- package/packages/utils/dist/discovery.d.ts.map +1 -0
- package/packages/utils/dist/discovery.js +439 -0
- package/packages/utils/dist/discovery.js.map +1 -0
- package/packages/utils/dist/errors.d.ts +29 -0
- package/packages/utils/dist/errors.d.ts.map +1 -0
- package/packages/utils/dist/errors.js +50 -0
- package/packages/utils/dist/errors.js.map +1 -0
- package/packages/utils/package.json +15 -2
- package/packages/utils/src/consolidation.test.ts +125 -0
- package/packages/utils/src/discovery.test.ts +196 -0
- package/packages/utils/src/discovery.ts +524 -0
- package/packages/utils/src/errors.test.ts +83 -0
- package/packages/utils/src/errors.ts +56 -0
- package/packages/wrapper/package.json +6 -6
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport module for relay client communication.
|
|
3
|
+
* @agent-relay/sdk
|
|
4
|
+
*
|
|
5
|
+
* Provides transport abstraction and implementations:
|
|
6
|
+
* - SocketTransport: Unix/TCP sockets (Node.js only)
|
|
7
|
+
* - WebSocketTransport: WebSocket connections (Node.js and Browser)
|
|
8
|
+
*
|
|
9
|
+
* Auto-detection selects the appropriate transport based on environment.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Types
|
|
13
|
+
export type {
|
|
14
|
+
Transport,
|
|
15
|
+
TransportConfig,
|
|
16
|
+
TransportEvents,
|
|
17
|
+
TransportState,
|
|
18
|
+
TransportFactory,
|
|
19
|
+
} from './types.js';
|
|
20
|
+
|
|
21
|
+
// Socket transport (Node.js)
|
|
22
|
+
export {
|
|
23
|
+
SocketTransport,
|
|
24
|
+
createSocketTransport,
|
|
25
|
+
type SocketTransportConfig,
|
|
26
|
+
} from './socket-transport.js';
|
|
27
|
+
|
|
28
|
+
// WebSocket transport (Node.js and Browser)
|
|
29
|
+
export {
|
|
30
|
+
WebSocketTransport,
|
|
31
|
+
createWebSocketTransport,
|
|
32
|
+
socketPathToWsUrl,
|
|
33
|
+
type WebSocketTransportConfig,
|
|
34
|
+
} from './websocket-transport.js';
|
|
35
|
+
|
|
36
|
+
// Re-export types from types.ts for convenience
|
|
37
|
+
import type { Transport, TransportConfig } from './types.js';
|
|
38
|
+
import { SocketTransport, type SocketTransportConfig } from './socket-transport.js';
|
|
39
|
+
import { WebSocketTransport, type WebSocketTransportConfig, socketPathToWsUrl } from './websocket-transport.js';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Environment detection result.
|
|
43
|
+
*/
|
|
44
|
+
export interface EnvironmentInfo {
|
|
45
|
+
/** Running in browser environment */
|
|
46
|
+
isBrowser: boolean;
|
|
47
|
+
/** Running in Node.js environment */
|
|
48
|
+
isNode: boolean;
|
|
49
|
+
/** WebSocket API is available */
|
|
50
|
+
hasWebSocket: boolean;
|
|
51
|
+
/** Unix sockets are available (Node.js only) */
|
|
52
|
+
hasUnixSockets: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Detect the current runtime environment.
|
|
57
|
+
*/
|
|
58
|
+
export function detectEnvironment(): EnvironmentInfo {
|
|
59
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
60
|
+
const isNode = typeof process !== 'undefined' &&
|
|
61
|
+
process.versions != null &&
|
|
62
|
+
process.versions.node != null;
|
|
63
|
+
const hasWebSocket = typeof WebSocket !== 'undefined' || isNode; // 'ws' can provide WebSocket in Node
|
|
64
|
+
const hasUnixSockets = isNode; // Only Node.js supports Unix sockets
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
isBrowser,
|
|
68
|
+
isNode,
|
|
69
|
+
hasWebSocket,
|
|
70
|
+
hasUnixSockets,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Options for creating an auto-detected transport.
|
|
76
|
+
*/
|
|
77
|
+
export interface AutoTransportOptions extends TransportConfig {
|
|
78
|
+
/** Unix socket path (for Node.js socket transport) */
|
|
79
|
+
socketPath?: string;
|
|
80
|
+
/** WebSocket URL (for WebSocket transport) */
|
|
81
|
+
wsUrl?: string;
|
|
82
|
+
/** WebSocket host (used with port to construct URL) */
|
|
83
|
+
wsHost?: string;
|
|
84
|
+
/** WebSocket port (default: 3888) */
|
|
85
|
+
wsPort?: number;
|
|
86
|
+
/** WebSocket path (default: /ws) */
|
|
87
|
+
wsPath?: string;
|
|
88
|
+
/** Use secure WebSocket (wss://) */
|
|
89
|
+
wsSecure?: boolean;
|
|
90
|
+
/** Force a specific transport type */
|
|
91
|
+
forceTransport?: 'socket' | 'websocket';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create a transport automatically based on environment and options.
|
|
96
|
+
*
|
|
97
|
+
* In browser environments, WebSocket transport is always used.
|
|
98
|
+
* In Node.js, Unix socket is preferred if socketPath is provided,
|
|
99
|
+
* otherwise WebSocket is used if wsUrl or wsHost is provided.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Transport configuration options
|
|
102
|
+
* @returns Configured transport instance
|
|
103
|
+
*
|
|
104
|
+
* @example Browser usage
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const transport = createAutoTransport({
|
|
107
|
+
* wsUrl: 'wss://relay.example.com/ws'
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example Node.js with Unix socket
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const transport = createAutoTransport({
|
|
114
|
+
* socketPath: '/tmp/agent-relay.sock'
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @example Node.js with WebSocket
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const transport = createAutoTransport({
|
|
121
|
+
* wsHost: 'localhost',
|
|
122
|
+
* wsPort: 3888
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export function createAutoTransport(options: AutoTransportOptions): Transport {
|
|
127
|
+
const env = detectEnvironment();
|
|
128
|
+
const { forceTransport, ...config } = options;
|
|
129
|
+
|
|
130
|
+
// Forced transport type
|
|
131
|
+
if (forceTransport === 'websocket') {
|
|
132
|
+
return createWsTransportFromOptions(options);
|
|
133
|
+
}
|
|
134
|
+
if (forceTransport === 'socket') {
|
|
135
|
+
if (!options.socketPath) {
|
|
136
|
+
throw new Error('socketPath is required when forcing socket transport');
|
|
137
|
+
}
|
|
138
|
+
if (!env.hasUnixSockets) {
|
|
139
|
+
throw new Error('Unix sockets are not available in this environment');
|
|
140
|
+
}
|
|
141
|
+
return new SocketTransport({
|
|
142
|
+
socketPath: options.socketPath,
|
|
143
|
+
connectTimeout: config.connectTimeout,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Auto-detect: Browser always uses WebSocket
|
|
148
|
+
if (env.isBrowser) {
|
|
149
|
+
return createWsTransportFromOptions(options);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Node.js: prefer Unix socket if path is provided
|
|
153
|
+
if (env.isNode && options.socketPath) {
|
|
154
|
+
return new SocketTransport({
|
|
155
|
+
socketPath: options.socketPath,
|
|
156
|
+
connectTimeout: config.connectTimeout,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Fall back to WebSocket
|
|
161
|
+
return createWsTransportFromOptions(options);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Create WebSocket transport from options.
|
|
166
|
+
*/
|
|
167
|
+
function createWsTransportFromOptions(options: AutoTransportOptions): WebSocketTransport {
|
|
168
|
+
let url = options.wsUrl;
|
|
169
|
+
|
|
170
|
+
if (!url) {
|
|
171
|
+
// Construct URL from components
|
|
172
|
+
const host = options.wsHost ?? 'localhost';
|
|
173
|
+
const port = options.wsPort ?? 3888;
|
|
174
|
+
const path = options.wsPath ?? '/ws';
|
|
175
|
+
const secure = options.wsSecure ?? false;
|
|
176
|
+
url = socketPathToWsUrl(host, port, path, secure);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return new WebSocketTransport({
|
|
180
|
+
url,
|
|
181
|
+
connectTimeout: options.connectTimeout,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Check if running in a browser environment.
|
|
187
|
+
*/
|
|
188
|
+
export function isBrowser(): boolean {
|
|
189
|
+
return detectEnvironment().isBrowser;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Check if running in Node.js environment.
|
|
194
|
+
*/
|
|
195
|
+
export function isNode(): boolean {
|
|
196
|
+
return detectEnvironment().isNode;
|
|
197
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unix/TCP Socket Transport for Node.js environments.
|
|
3
|
+
* @agent-relay/sdk
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import net from 'node:net';
|
|
7
|
+
import type { Transport, TransportConfig, TransportEvents, TransportState } from './types.js';
|
|
8
|
+
|
|
9
|
+
export interface SocketTransportConfig extends TransportConfig {
|
|
10
|
+
/** Unix socket path or TCP host:port */
|
|
11
|
+
socketPath: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Socket-based transport for Unix sockets and TCP connections.
|
|
16
|
+
* This is the default transport for Node.js environments.
|
|
17
|
+
*/
|
|
18
|
+
export class SocketTransport implements Transport {
|
|
19
|
+
private socket?: net.Socket;
|
|
20
|
+
private config: SocketTransportConfig;
|
|
21
|
+
private events: TransportEvents = {};
|
|
22
|
+
private _state: TransportState = 'disconnected';
|
|
23
|
+
|
|
24
|
+
constructor(config: SocketTransportConfig) {
|
|
25
|
+
this.config = {
|
|
26
|
+
connectTimeout: 5000,
|
|
27
|
+
...config,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get state(): TransportState {
|
|
32
|
+
return this._state;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setEvents(events: TransportEvents): void {
|
|
36
|
+
this.events = events;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
connect(): Promise<void> {
|
|
40
|
+
if (this._state !== 'disconnected') {
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
this._state = 'connecting';
|
|
46
|
+
|
|
47
|
+
const timeout = setTimeout(() => {
|
|
48
|
+
if (this._state === 'connecting') {
|
|
49
|
+
this.socket?.destroy();
|
|
50
|
+
this._state = 'disconnected';
|
|
51
|
+
reject(new Error('Connection timeout'));
|
|
52
|
+
}
|
|
53
|
+
}, this.config.connectTimeout);
|
|
54
|
+
|
|
55
|
+
this.socket = net.createConnection(this.config.socketPath, () => {
|
|
56
|
+
clearTimeout(timeout);
|
|
57
|
+
this._state = 'connected';
|
|
58
|
+
this.events.onConnect?.();
|
|
59
|
+
resolve();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this.socket.on('data', (data) => {
|
|
63
|
+
// Convert Buffer to Uint8Array for consistent interface
|
|
64
|
+
this.events.onData?.(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
this.socket.on('close', () => {
|
|
68
|
+
this._state = 'disconnected';
|
|
69
|
+
this.events.onClose?.();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
this.socket.on('error', (err) => {
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
if (this._state === 'connecting') {
|
|
75
|
+
this._state = 'disconnected';
|
|
76
|
+
reject(err);
|
|
77
|
+
}
|
|
78
|
+
this.events.onError?.(err);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
disconnect(): void {
|
|
84
|
+
if (!this.socket || this._state === 'disconnected') {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this._state = 'closing';
|
|
89
|
+
this.socket.end();
|
|
90
|
+
this.socket = undefined;
|
|
91
|
+
this._state = 'disconnected';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
send(data: Uint8Array | Buffer): boolean {
|
|
95
|
+
if (!this.socket || this._state !== 'connected') {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
// Ensure we're sending a Buffer
|
|
101
|
+
const buffer = data instanceof Buffer ? data : Buffer.from(data);
|
|
102
|
+
this.socket.write(buffer);
|
|
103
|
+
return true;
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Create a socket transport for Unix/TCP connections.
|
|
112
|
+
*/
|
|
113
|
+
export function createSocketTransport(socketPath: string, config?: TransportConfig): SocketTransport {
|
|
114
|
+
return new SocketTransport({ socketPath, ...config });
|
|
115
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport abstraction for relay client communication.
|
|
3
|
+
* @agent-relay/sdk
|
|
4
|
+
*
|
|
5
|
+
* Defines the interface for different transport implementations:
|
|
6
|
+
* - SocketTransport: Unix/TCP sockets (Node.js)
|
|
7
|
+
* - WebSocketTransport: WebSocket connections (Node.js and Browser)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Transport connection state.
|
|
12
|
+
*/
|
|
13
|
+
export type TransportState = 'disconnected' | 'connecting' | 'connected' | 'closing';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Transport event handlers.
|
|
17
|
+
*/
|
|
18
|
+
export interface TransportEvents {
|
|
19
|
+
/** Called when transport connects successfully */
|
|
20
|
+
onConnect?: () => void;
|
|
21
|
+
/** Called when transport receives data */
|
|
22
|
+
onData?: (data: Uint8Array) => void;
|
|
23
|
+
/** Called when transport disconnects */
|
|
24
|
+
onClose?: () => void;
|
|
25
|
+
/** Called when transport encounters an error */
|
|
26
|
+
onError?: (error: Error) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Transport configuration options.
|
|
31
|
+
*/
|
|
32
|
+
export interface TransportConfig {
|
|
33
|
+
/** Connection timeout in milliseconds */
|
|
34
|
+
connectTimeout?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Transport interface for relay client communication.
|
|
39
|
+
*
|
|
40
|
+
* Implementations must handle:
|
|
41
|
+
* - Connection lifecycle (connect, disconnect)
|
|
42
|
+
* - Binary data transmission
|
|
43
|
+
* - Event callbacks for state changes
|
|
44
|
+
*/
|
|
45
|
+
export interface Transport {
|
|
46
|
+
/** Current connection state */
|
|
47
|
+
readonly state: TransportState;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Connect to the relay endpoint.
|
|
51
|
+
* @throws Error if connection fails
|
|
52
|
+
*/
|
|
53
|
+
connect(): Promise<void>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Disconnect from the relay.
|
|
57
|
+
*/
|
|
58
|
+
disconnect(): void;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Send binary data to the relay.
|
|
62
|
+
* @param data - Data to send (Buffer or Uint8Array)
|
|
63
|
+
* @returns true if data was queued for sending
|
|
64
|
+
*/
|
|
65
|
+
send(data: Uint8Array | Buffer): boolean;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Set event handlers.
|
|
69
|
+
* @param events - Event handler callbacks
|
|
70
|
+
*/
|
|
71
|
+
setEvents(events: TransportEvents): void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Transport factory function type.
|
|
76
|
+
*/
|
|
77
|
+
export type TransportFactory = (config?: TransportConfig) => Transport;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket Transport for browser and Node.js environments.
|
|
3
|
+
* @agent-relay/sdk
|
|
4
|
+
*
|
|
5
|
+
* Uses native WebSocket in browsers and 'ws' package in Node.js.
|
|
6
|
+
* The transport expects the daemon to have a WebSocket endpoint (e.g., /ws).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Transport, TransportConfig, TransportEvents, TransportState } from './types.js';
|
|
10
|
+
|
|
11
|
+
export interface WebSocketTransportConfig extends TransportConfig {
|
|
12
|
+
/** WebSocket URL (e.g., ws://localhost:3888/ws) */
|
|
13
|
+
url: string;
|
|
14
|
+
/** Protocols to use in WebSocket handshake */
|
|
15
|
+
protocols?: string | string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Simple close event type (works in both browser and Node.js)
|
|
19
|
+
interface CloseEventLike {
|
|
20
|
+
code?: number;
|
|
21
|
+
reason?: string;
|
|
22
|
+
wasClean?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Simple message event type (works in both browser and Node.js)
|
|
26
|
+
interface MessageEventLike {
|
|
27
|
+
data: ArrayBuffer | string | Buffer;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Type for WebSocket (works in both browser and Node.js with 'ws')
|
|
31
|
+
interface WebSocketLike {
|
|
32
|
+
readyState: number;
|
|
33
|
+
CONNECTING: number;
|
|
34
|
+
OPEN: number;
|
|
35
|
+
CLOSING: number;
|
|
36
|
+
CLOSED: number;
|
|
37
|
+
send(data: ArrayBuffer | Uint8Array | string): void;
|
|
38
|
+
close(): void;
|
|
39
|
+
onopen: ((ev: Event) => void) | null;
|
|
40
|
+
onclose: ((ev: CloseEventLike) => void) | null;
|
|
41
|
+
onerror: ((ev: Event | Error) => void) | null;
|
|
42
|
+
onmessage: ((ev: MessageEventLike) => void) | null;
|
|
43
|
+
binaryType: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Constructor type for WebSocket
|
|
47
|
+
type WebSocketConstructor = new (url: string, protocols?: string | string[]) => WebSocketLike;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* WebSocket-based transport for browser and Node.js.
|
|
51
|
+
*
|
|
52
|
+
* In browsers, uses native WebSocket.
|
|
53
|
+
* In Node.js, requires 'ws' package (optional dependency).
|
|
54
|
+
*/
|
|
55
|
+
export class WebSocketTransport implements Transport {
|
|
56
|
+
private ws?: WebSocketLike;
|
|
57
|
+
private config: WebSocketTransportConfig;
|
|
58
|
+
private events: TransportEvents = {};
|
|
59
|
+
private _state: TransportState = 'disconnected';
|
|
60
|
+
private WebSocketImpl?: WebSocketConstructor;
|
|
61
|
+
|
|
62
|
+
constructor(config: WebSocketTransportConfig) {
|
|
63
|
+
this.config = {
|
|
64
|
+
connectTimeout: 5000,
|
|
65
|
+
...config,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get state(): TransportState {
|
|
70
|
+
return this._state;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
setEvents(events: TransportEvents): void {
|
|
74
|
+
this.events = events;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get WebSocket implementation (native or 'ws' package).
|
|
79
|
+
*/
|
|
80
|
+
private async getWebSocketImpl(): Promise<WebSocketConstructor> {
|
|
81
|
+
if (this.WebSocketImpl) {
|
|
82
|
+
return this.WebSocketImpl;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Check for browser environment
|
|
86
|
+
if (typeof globalThis !== 'undefined' && 'WebSocket' in globalThis) {
|
|
87
|
+
this.WebSocketImpl = (globalThis as { WebSocket: WebSocketConstructor }).WebSocket;
|
|
88
|
+
return this.WebSocketImpl;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Node.js environment - try to load 'ws' package
|
|
92
|
+
try {
|
|
93
|
+
// Dynamic import of 'ws' for Node.js
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
+
const wsModule = await import('ws' as any) as any;
|
|
96
|
+
const impl = wsModule.default || wsModule.WebSocket || wsModule;
|
|
97
|
+
if (!impl) {
|
|
98
|
+
throw new Error('ws module loaded but WebSocket constructor not found');
|
|
99
|
+
}
|
|
100
|
+
this.WebSocketImpl = impl as WebSocketConstructor;
|
|
101
|
+
return this.WebSocketImpl;
|
|
102
|
+
} catch {
|
|
103
|
+
throw new Error(
|
|
104
|
+
'WebSocket not available. In Node.js, install the "ws" package:\n' +
|
|
105
|
+
' npm install ws\n\n' +
|
|
106
|
+
'In browsers, native WebSocket should be available.'
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async connect(): Promise<void> {
|
|
112
|
+
if (this._state !== 'disconnected') {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const WebSocketImpl = await this.getWebSocketImpl();
|
|
117
|
+
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
this._state = 'connecting';
|
|
120
|
+
|
|
121
|
+
const timeout = setTimeout(() => {
|
|
122
|
+
if (this._state === 'connecting') {
|
|
123
|
+
this.ws?.close();
|
|
124
|
+
this._state = 'disconnected';
|
|
125
|
+
reject(new Error('Connection timeout'));
|
|
126
|
+
}
|
|
127
|
+
}, this.config.connectTimeout);
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
this.ws = new WebSocketImpl(this.config.url, this.config.protocols);
|
|
131
|
+
this.ws.binaryType = 'arraybuffer';
|
|
132
|
+
|
|
133
|
+
this.ws.onopen = () => {
|
|
134
|
+
clearTimeout(timeout);
|
|
135
|
+
this._state = 'connected';
|
|
136
|
+
this.events.onConnect?.();
|
|
137
|
+
resolve();
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
this.ws.onmessage = (event: MessageEventLike) => {
|
|
141
|
+
// Handle binary data
|
|
142
|
+
if (event.data instanceof ArrayBuffer) {
|
|
143
|
+
this.events.onData?.(new Uint8Array(event.data));
|
|
144
|
+
} else if (typeof event.data === 'string') {
|
|
145
|
+
// Convert string to Uint8Array (shouldn't happen with binaryType = 'arraybuffer')
|
|
146
|
+
const encoder = new TextEncoder();
|
|
147
|
+
this.events.onData?.(encoder.encode(event.data));
|
|
148
|
+
} else if (event.data && typeof (event.data as Buffer).buffer !== 'undefined') {
|
|
149
|
+
// Handle Node.js Buffer
|
|
150
|
+
const buf = event.data as Buffer;
|
|
151
|
+
this.events.onData?.(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
this.ws.onclose = () => {
|
|
156
|
+
clearTimeout(timeout);
|
|
157
|
+
this._state = 'disconnected';
|
|
158
|
+
this.events.onClose?.();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
this.ws.onerror = (event: Event | Error) => {
|
|
162
|
+
clearTimeout(timeout);
|
|
163
|
+
const error = event instanceof Error
|
|
164
|
+
? event
|
|
165
|
+
: new Error('WebSocket error');
|
|
166
|
+
if (this._state === 'connecting') {
|
|
167
|
+
this._state = 'disconnected';
|
|
168
|
+
reject(error);
|
|
169
|
+
}
|
|
170
|
+
this.events.onError?.(error);
|
|
171
|
+
};
|
|
172
|
+
} catch (err) {
|
|
173
|
+
clearTimeout(timeout);
|
|
174
|
+
this._state = 'disconnected';
|
|
175
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
disconnect(): void {
|
|
181
|
+
if (!this.ws || this._state === 'disconnected') {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
this._state = 'closing';
|
|
186
|
+
this.ws.close();
|
|
187
|
+
this.ws = undefined;
|
|
188
|
+
this._state = 'disconnected';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
send(data: Uint8Array | Buffer): boolean {
|
|
192
|
+
if (!this.ws || this._state !== 'connected') {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
// WebSocket send accepts ArrayBuffer or Uint8Array
|
|
198
|
+
// Convert Buffer to Uint8Array for browser compatibility
|
|
199
|
+
let bytes: Uint8Array;
|
|
200
|
+
if (data instanceof Uint8Array) {
|
|
201
|
+
bytes = data;
|
|
202
|
+
} else {
|
|
203
|
+
// Handle Buffer (Node.js)
|
|
204
|
+
const buf = data as Buffer;
|
|
205
|
+
bytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
206
|
+
}
|
|
207
|
+
this.ws.send(bytes);
|
|
208
|
+
return true;
|
|
209
|
+
} catch {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Create a WebSocket transport.
|
|
217
|
+
*
|
|
218
|
+
* @param url - WebSocket URL (e.g., ws://localhost:3888/ws)
|
|
219
|
+
* @param config - Additional configuration options
|
|
220
|
+
*/
|
|
221
|
+
export function createWebSocketTransport(
|
|
222
|
+
url: string,
|
|
223
|
+
config?: Omit<WebSocketTransportConfig, 'url'>
|
|
224
|
+
): WebSocketTransport {
|
|
225
|
+
return new WebSocketTransport({ url, ...config });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Convert a socket path to WebSocket URL.
|
|
230
|
+
* Useful when daemon provides both Unix socket and WebSocket endpoints.
|
|
231
|
+
*
|
|
232
|
+
* @param host - Host address (default: localhost)
|
|
233
|
+
* @param port - Port number (default: 3888)
|
|
234
|
+
* @param path - WebSocket path (default: /ws)
|
|
235
|
+
* @param secure - Use wss:// instead of ws:// (default: false)
|
|
236
|
+
*/
|
|
237
|
+
export function socketPathToWsUrl(
|
|
238
|
+
host = 'localhost',
|
|
239
|
+
port = 3888,
|
|
240
|
+
path = '/ws',
|
|
241
|
+
secure = false
|
|
242
|
+
): string {
|
|
243
|
+
const protocol = secure ? 'wss' : 'ws';
|
|
244
|
+
return `${protocol}://${host}:${port}${path}`;
|
|
245
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-relay/storage",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "Storage adapters and interfaces for Relay message/session persistence",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@agent-relay/protocol": "2.1.
|
|
59
|
+
"@agent-relay/protocol": "2.1.6"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@types/node": "^22.19.3",
|
|
@@ -218,11 +218,16 @@ describe('JsonlStorageAdapter', () => {
|
|
|
218
218
|
await writerAdapter.saveMessage(makeMessage({ id: 'watch-test', body: 'from daemon' }));
|
|
219
219
|
await writerAdapter.close();
|
|
220
220
|
|
|
221
|
-
// Wait for
|
|
222
|
-
|
|
221
|
+
// Wait for file watcher to trigger reload (fs.watch can be slow/flaky)
|
|
222
|
+
// Use polling with retries instead of a fixed timeout
|
|
223
|
+
let after: StoredMessage[] = [];
|
|
224
|
+
for (let i = 0; i < 20; i++) {
|
|
225
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
226
|
+
after = await watchingAdapter.getMessages({});
|
|
227
|
+
if (after.length > countBefore) break;
|
|
228
|
+
}
|
|
223
229
|
|
|
224
230
|
// Should now see the new message
|
|
225
|
-
const after = await watchingAdapter.getMessages({});
|
|
226
231
|
expect(after.length).toBe(countBefore + 1);
|
|
227
232
|
expect(after.some(m => m.id === 'watch-test')).toBe(true);
|
|
228
233
|
|