@cereworker/gateway 0.6.0
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/dist/client.d.ts +26 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +168 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/proxy-tools.d.ts +4 -0
- package/dist/proxy-tools.d.ts.map +1 -0
- package/dist/proxy-tools.js +15 -0
- package/dist/proxy-tools.js.map +1 -0
- package/dist/server.d.ts +28 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +249 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Producible
|
|
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/dist/client.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { GatewayClientConfig } from './types.js';
|
|
2
|
+
export type ToolExecutor = (tool: string, args: Record<string, unknown>) => Promise<string>;
|
|
3
|
+
export declare class GatewayNodeClient {
|
|
4
|
+
private ws;
|
|
5
|
+
private config;
|
|
6
|
+
private executor;
|
|
7
|
+
private onEmergencyStop?;
|
|
8
|
+
private reconnectAttempt;
|
|
9
|
+
private maxReconnectAttempts;
|
|
10
|
+
private reconnectTimer;
|
|
11
|
+
private connected;
|
|
12
|
+
private stopped;
|
|
13
|
+
private startedAt;
|
|
14
|
+
constructor(config: GatewayClientConfig, executor: ToolExecutor);
|
|
15
|
+
setEmergencyStopHandler(handler: () => void): void;
|
|
16
|
+
connect(): Promise<void>;
|
|
17
|
+
disconnect(reason?: string): Promise<void>;
|
|
18
|
+
isConnected(): boolean;
|
|
19
|
+
private doConnect;
|
|
20
|
+
private handleFrame;
|
|
21
|
+
private handleInvoke;
|
|
22
|
+
private getStatus;
|
|
23
|
+
private scheduleReconnect;
|
|
24
|
+
private send;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,mBAAmB,EAAc,MAAM,YAAY,CAAC;AAIhF,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAE5F,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,QAAQ,CAAe;IAC/B,OAAO,CAAC,eAAe,CAAC,CAAa;IACrC,OAAO,CAAC,gBAAgB,CAAK;IAC7B,OAAO,CAAC,oBAAoB,CAAM;IAClC,OAAO,CAAC,cAAc,CAA8C;IACpE,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAc;gBAEnB,MAAM,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY;IAK/D,uBAAuB,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI;IAI5C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAMxB,UAAU,CAAC,MAAM,SAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAe3D,WAAW,IAAI,OAAO;IAItB,OAAO,CAAC,SAAS;IA8CjB,OAAO,CAAC,WAAW;YAgDL,YAAY;IAW1B,OAAO,CAAC,SAAS;IASjB,OAAO,CAAC,iBAAiB;IAuBzB,OAAO,CAAC,IAAI;CAKb"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import { createLogger } from '@cereworker/core';
|
|
3
|
+
const log = createLogger('gateway-client');
|
|
4
|
+
export class GatewayNodeClient {
|
|
5
|
+
ws = null;
|
|
6
|
+
config;
|
|
7
|
+
executor;
|
|
8
|
+
onEmergencyStop;
|
|
9
|
+
reconnectAttempt = 0;
|
|
10
|
+
maxReconnectAttempts = 10;
|
|
11
|
+
reconnectTimer = null;
|
|
12
|
+
connected = false;
|
|
13
|
+
stopped = false;
|
|
14
|
+
startedAt = Date.now();
|
|
15
|
+
constructor(config, executor) {
|
|
16
|
+
this.config = config;
|
|
17
|
+
this.executor = executor;
|
|
18
|
+
}
|
|
19
|
+
setEmergencyStopHandler(handler) {
|
|
20
|
+
this.onEmergencyStop = handler;
|
|
21
|
+
}
|
|
22
|
+
async connect() {
|
|
23
|
+
this.stopped = false;
|
|
24
|
+
this.startedAt = Date.now();
|
|
25
|
+
return this.doConnect();
|
|
26
|
+
}
|
|
27
|
+
async disconnect(reason = 'client shutdown') {
|
|
28
|
+
this.stopped = true;
|
|
29
|
+
if (this.reconnectTimer) {
|
|
30
|
+
clearTimeout(this.reconnectTimer);
|
|
31
|
+
this.reconnectTimer = null;
|
|
32
|
+
}
|
|
33
|
+
if (this.ws) {
|
|
34
|
+
this.send({ type: 'disconnect', reason });
|
|
35
|
+
this.ws.close();
|
|
36
|
+
this.ws = null;
|
|
37
|
+
}
|
|
38
|
+
this.connected = false;
|
|
39
|
+
log.info('Disconnected from gateway', { reason });
|
|
40
|
+
}
|
|
41
|
+
isConnected() {
|
|
42
|
+
return this.connected;
|
|
43
|
+
}
|
|
44
|
+
doConnect() {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const ws = new WebSocket(this.config.gatewayUrl);
|
|
47
|
+
ws.on('open', () => {
|
|
48
|
+
this.ws = ws;
|
|
49
|
+
this.send({
|
|
50
|
+
type: 'connect',
|
|
51
|
+
nodeId: this.config.nodeId,
|
|
52
|
+
token: this.config.token,
|
|
53
|
+
capabilities: this.config.capabilities,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
ws.on('message', (data) => {
|
|
57
|
+
let frame;
|
|
58
|
+
try {
|
|
59
|
+
frame = JSON.parse(data.toString());
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
log.warn('Received invalid JSON from gateway');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.handleFrame(frame, resolve, reject);
|
|
66
|
+
});
|
|
67
|
+
ws.on('close', () => {
|
|
68
|
+
const wasConnected = this.connected;
|
|
69
|
+
this.connected = false;
|
|
70
|
+
this.ws = null;
|
|
71
|
+
if (!this.stopped) {
|
|
72
|
+
log.warn('Connection to gateway lost, reconnecting...');
|
|
73
|
+
this.scheduleReconnect();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
ws.on('error', (err) => {
|
|
77
|
+
log.error('WebSocket error', { error: err.message });
|
|
78
|
+
if (!this.connected) {
|
|
79
|
+
reject(err);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
handleFrame(frame, onConnect, onError) {
|
|
85
|
+
switch (frame.type) {
|
|
86
|
+
case 'connected':
|
|
87
|
+
this.connected = true;
|
|
88
|
+
this.reconnectAttempt = 0;
|
|
89
|
+
log.info('Connected to gateway', { gatewayId: frame.gatewayId, nodeId: this.config.nodeId });
|
|
90
|
+
onConnect?.();
|
|
91
|
+
break;
|
|
92
|
+
case 'error':
|
|
93
|
+
log.error('Gateway error', { message: frame.message });
|
|
94
|
+
if (!this.connected) {
|
|
95
|
+
onError?.(new Error(frame.message));
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case 'invoke':
|
|
99
|
+
this.handleInvoke(frame.id, frame.tool, frame.args);
|
|
100
|
+
break;
|
|
101
|
+
case 'ping':
|
|
102
|
+
this.send({ type: 'pong' });
|
|
103
|
+
break;
|
|
104
|
+
case 'status-request':
|
|
105
|
+
this.send({
|
|
106
|
+
type: 'status-response',
|
|
107
|
+
id: frame.id,
|
|
108
|
+
status: this.getStatus(),
|
|
109
|
+
});
|
|
110
|
+
break;
|
|
111
|
+
case 'emergency-stop':
|
|
112
|
+
log.warn('Emergency stop received from gateway');
|
|
113
|
+
this.onEmergencyStop?.();
|
|
114
|
+
break;
|
|
115
|
+
case 'disconnect':
|
|
116
|
+
log.info('Gateway requested disconnect', { reason: frame.reason });
|
|
117
|
+
this.stopped = true;
|
|
118
|
+
this.ws?.close();
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
log.warn('Unexpected frame from gateway', { type: frame.type });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async handleInvoke(id, tool, args) {
|
|
125
|
+
log.info('Invoke received', { id, tool });
|
|
126
|
+
try {
|
|
127
|
+
const output = await this.executor(tool, args);
|
|
128
|
+
this.send({ type: 'invoke-result', id, output, isError: false });
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
132
|
+
this.send({ type: 'invoke-result', id, output: message, isError: true });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
getStatus() {
|
|
136
|
+
return {
|
|
137
|
+
healthy: true,
|
|
138
|
+
uptime: Math.floor((Date.now() - this.startedAt) / 1000),
|
|
139
|
+
activeTools: this.config.capabilities.length,
|
|
140
|
+
autoMode: false,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
scheduleReconnect() {
|
|
144
|
+
if (this.stopped || this.reconnectAttempt >= this.maxReconnectAttempts) {
|
|
145
|
+
if (this.reconnectAttempt >= this.maxReconnectAttempts) {
|
|
146
|
+
log.error('Max reconnect attempts reached, giving up');
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const baseDelay = 1000;
|
|
151
|
+
const maxDelay = 30_000;
|
|
152
|
+
const delay = Math.min(baseDelay * Math.pow(2, this.reconnectAttempt), maxDelay);
|
|
153
|
+
const jitter = delay * (0.5 + Math.random() * 0.5);
|
|
154
|
+
this.reconnectAttempt++;
|
|
155
|
+
log.info('Reconnecting...', { attempt: this.reconnectAttempt, delayMs: Math.round(jitter) });
|
|
156
|
+
this.reconnectTimer = setTimeout(() => {
|
|
157
|
+
this.doConnect().catch((err) => {
|
|
158
|
+
log.error('Reconnect failed', { error: err.message });
|
|
159
|
+
});
|
|
160
|
+
}, jitter);
|
|
161
|
+
}
|
|
162
|
+
send(frame) {
|
|
163
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
164
|
+
this.ws.send(JSON.stringify(frame));
|
|
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B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,EAAE,QAAQ,CAAC,CAAC;QACjF,MAAM,MAAM,GAAG,KAAK,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;QAEnD,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,GAAG,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,gBAAgB,EAAE,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAE7F,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,GAAG,EAAE;YACpC,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;gBAC7B,GAAG,CAAC,KAAK,CAAC,kBAAkB,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;YACxD,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,MAAM,CAAC,CAAC;IACb,CAAC;IAEO,IAAI,CAAC,KAAmB;QAC9B,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC,UAAU,KAAK,SAAS,CAAC,IAAI,EAAE,CAAC;YACrD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { GatewayFrame, NodeInfo, NodeStatus, GatewayServerConfig, GatewayClientConfig, } from './types.js';
|
|
2
|
+
export { GatewayServer } from './server.js';
|
|
3
|
+
export { GatewayNodeClient } from './client.js';
|
|
4
|
+
export type { ToolExecutor } from './client.js';
|
|
5
|
+
export { createProxyTools } from './proxy-tools.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ToolDefinition } from '@cereworker/core';
|
|
2
|
+
import type { GatewayServer } from './server.js';
|
|
3
|
+
export declare function createProxyTools(server: GatewayServer, nodeId: string, capabilities: string[]): Record<string, ToolDefinition>;
|
|
4
|
+
//# sourceMappingURL=proxy-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-tools.d.ts","sourceRoot":"","sources":["../src/proxy-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,EAAE,GACrB,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAehC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function createProxyTools(server, nodeId, capabilities) {
|
|
2
|
+
const tools = {};
|
|
3
|
+
for (const cap of capabilities) {
|
|
4
|
+
const toolName = `${cap}@${nodeId}`;
|
|
5
|
+
tools[toolName] = {
|
|
6
|
+
description: `Execute "${cap}" on remote node "${nodeId}"`,
|
|
7
|
+
parameters: {},
|
|
8
|
+
execute: async (args) => {
|
|
9
|
+
return server.invoke(nodeId, cap, args);
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
return tools;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=proxy-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-tools.js","sourceRoot":"","sources":["../src/proxy-tools.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,gBAAgB,CAC9B,MAAqB,EACrB,MAAc,EACd,YAAsB;IAEtB,MAAM,KAAK,GAAmC,EAAE,CAAC;IAEjD,KAAK,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;QAC/B,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,MAAM,EAAE,CAAC;QACpC,KAAK,CAAC,QAAQ,CAAC,GAAG;YAChB,WAAW,EAAE,YAAY,GAAG,qBAAqB,MAAM,GAAG;YAC1D,UAAU,EAAE,EAAE;YACd,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;gBACtB,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;YAC1C,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { GatewayServerConfig, NodeInfo } from './types.js';
|
|
2
|
+
export declare class GatewayServer {
|
|
3
|
+
private wss;
|
|
4
|
+
private httpServer;
|
|
5
|
+
private nodes;
|
|
6
|
+
private pendingInvocations;
|
|
7
|
+
private pingTimer;
|
|
8
|
+
private gatewayId;
|
|
9
|
+
private config;
|
|
10
|
+
private onNodeConnected?;
|
|
11
|
+
private onNodeDisconnected?;
|
|
12
|
+
constructor(config: GatewayServerConfig);
|
|
13
|
+
setNodeConnectedHandler(handler: (nodeId: string, capabilities: string[]) => void): void;
|
|
14
|
+
setNodeDisconnectedHandler(handler: (nodeId: string, reason: string, capabilities: string[]) => void): void;
|
|
15
|
+
start(): Promise<void>;
|
|
16
|
+
stop(): Promise<void>;
|
|
17
|
+
invoke(nodeId: string, tool: string, args: Record<string, unknown>, timeoutMs?: number): Promise<string>;
|
|
18
|
+
emergencyStopAll(): void;
|
|
19
|
+
listNodes(): NodeInfo[];
|
|
20
|
+
getNode(nodeId: string): NodeInfo | undefined;
|
|
21
|
+
getGatewayId(): string;
|
|
22
|
+
getPort(): number;
|
|
23
|
+
private handleFrame;
|
|
24
|
+
private handleNodeDisconnect;
|
|
25
|
+
private startPingLoop;
|
|
26
|
+
private send;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAgB,mBAAmB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAe9E,qBAAa,aAAa;IACxB,OAAO,CAAC,GAAG,CAAgC;IAC3C,OAAO,CAAC,UAAU,CAAgD;IAClE,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,kBAAkB,CAAwC;IAClE,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAsB;IAEpC,OAAO,CAAC,eAAe,CAAC,CAAmD;IAC3E,OAAO,CAAC,kBAAkB,CAAC,CAAmE;gBAElF,MAAM,EAAE,mBAAmB;IAKvC,uBAAuB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,IAAI,GAAG,IAAI;IAIxF,0BAA0B,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,IAAI,GAAG,IAAI;IAIrG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAqGtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAsCrB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAqB9G,gBAAgB,IAAI,IAAI;IAOxB,SAAS,IAAI,QAAQ,EAAE;IAIvB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;IAI7C,YAAY,IAAI,MAAM;IAItB,OAAO,IAAI,MAAM;IAIjB,OAAO,CAAC,WAAW;IAuCnB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,aAAa;IAsBrB,OAAO,CAAC,IAAI;CAKb"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { WebSocketServer, WebSocket } from 'ws';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { createLogger } from '@cereworker/core';
|
|
5
|
+
const log = createLogger('gateway-server');
|
|
6
|
+
export class GatewayServer {
|
|
7
|
+
wss = null;
|
|
8
|
+
httpServer = null;
|
|
9
|
+
nodes = new Map();
|
|
10
|
+
pendingInvocations = new Map();
|
|
11
|
+
pingTimer = null;
|
|
12
|
+
gatewayId;
|
|
13
|
+
config;
|
|
14
|
+
onNodeConnected;
|
|
15
|
+
onNodeDisconnected;
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.config = config;
|
|
18
|
+
this.gatewayId = nanoid(12);
|
|
19
|
+
}
|
|
20
|
+
setNodeConnectedHandler(handler) {
|
|
21
|
+
this.onNodeConnected = handler;
|
|
22
|
+
}
|
|
23
|
+
setNodeDisconnectedHandler(handler) {
|
|
24
|
+
this.onNodeDisconnected = handler;
|
|
25
|
+
}
|
|
26
|
+
async start() {
|
|
27
|
+
this.httpServer = createServer((req, res) => {
|
|
28
|
+
if (req.url === '/status' && req.method === 'GET') {
|
|
29
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
30
|
+
res.end(JSON.stringify({ gatewayId: this.gatewayId, nodes: this.listNodes() }));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
res.writeHead(404);
|
|
34
|
+
res.end();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
this.wss = new WebSocketServer({ server: this.httpServer });
|
|
38
|
+
this.wss.on('connection', (ws) => {
|
|
39
|
+
let authenticated = false;
|
|
40
|
+
let nodeId = null;
|
|
41
|
+
const authTimeout = setTimeout(() => {
|
|
42
|
+
if (!authenticated) {
|
|
43
|
+
this.send(ws, { type: 'error', message: 'Authentication timeout' });
|
|
44
|
+
ws.close();
|
|
45
|
+
}
|
|
46
|
+
}, 10_000);
|
|
47
|
+
ws.on('message', (data) => {
|
|
48
|
+
let frame;
|
|
49
|
+
try {
|
|
50
|
+
frame = JSON.parse(data.toString());
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
this.send(ws, { type: 'error', message: 'Invalid JSON' });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!authenticated) {
|
|
57
|
+
if (frame.type !== 'connect') {
|
|
58
|
+
this.send(ws, { type: 'error', message: 'Must authenticate first' });
|
|
59
|
+
ws.close();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
clearTimeout(authTimeout);
|
|
63
|
+
if (this.config.token && frame.token !== this.config.token) {
|
|
64
|
+
this.send(ws, { type: 'error', message: 'Invalid token' });
|
|
65
|
+
ws.close();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (this.nodes.has(frame.nodeId)) {
|
|
69
|
+
this.send(ws, { type: 'error', message: `Node "${frame.nodeId}" already connected` });
|
|
70
|
+
ws.close();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
nodeId = frame.nodeId;
|
|
74
|
+
authenticated = true;
|
|
75
|
+
const info = {
|
|
76
|
+
nodeId: frame.nodeId,
|
|
77
|
+
capabilities: frame.capabilities,
|
|
78
|
+
connectedAt: Date.now(),
|
|
79
|
+
lastPingAt: Date.now(),
|
|
80
|
+
status: 'connected',
|
|
81
|
+
};
|
|
82
|
+
this.nodes.set(frame.nodeId, { ws, info });
|
|
83
|
+
this.send(ws, { type: 'connected', gatewayId: this.gatewayId });
|
|
84
|
+
log.info('Node connected', { nodeId: frame.nodeId, capabilities: frame.capabilities });
|
|
85
|
+
this.onNodeConnected?.(frame.nodeId, frame.capabilities);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.handleFrame(nodeId, frame);
|
|
89
|
+
});
|
|
90
|
+
ws.on('close', () => {
|
|
91
|
+
clearTimeout(authTimeout);
|
|
92
|
+
if (nodeId) {
|
|
93
|
+
this.handleNodeDisconnect(nodeId, 'connection closed');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
ws.on('error', (err) => {
|
|
97
|
+
log.error('WebSocket error', { nodeId, error: err.message });
|
|
98
|
+
if (nodeId) {
|
|
99
|
+
this.handleNodeDisconnect(nodeId, `error: ${err.message}`);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
await new Promise((resolve) => {
|
|
104
|
+
this.httpServer.listen(this.config.port, () => {
|
|
105
|
+
log.info('Gateway server started', { port: this.config.port, gatewayId: this.gatewayId });
|
|
106
|
+
resolve();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
this.startPingLoop();
|
|
110
|
+
}
|
|
111
|
+
async stop() {
|
|
112
|
+
if (this.pingTimer) {
|
|
113
|
+
clearInterval(this.pingTimer);
|
|
114
|
+
this.pingTimer = null;
|
|
115
|
+
}
|
|
116
|
+
// Reject all pending invocations
|
|
117
|
+
for (const [id, pending] of this.pendingInvocations) {
|
|
118
|
+
clearTimeout(pending.timer);
|
|
119
|
+
pending.reject(new Error('Gateway shutting down'));
|
|
120
|
+
}
|
|
121
|
+
this.pendingInvocations.clear();
|
|
122
|
+
// Close all node connections
|
|
123
|
+
for (const [nodeId, node] of this.nodes) {
|
|
124
|
+
this.send(node.ws, { type: 'disconnect', reason: 'gateway shutting down' });
|
|
125
|
+
node.ws.close();
|
|
126
|
+
}
|
|
127
|
+
this.nodes.clear();
|
|
128
|
+
// Close servers
|
|
129
|
+
if (this.wss) {
|
|
130
|
+
this.wss.close();
|
|
131
|
+
this.wss = null;
|
|
132
|
+
}
|
|
133
|
+
await new Promise((resolve) => {
|
|
134
|
+
if (this.httpServer) {
|
|
135
|
+
this.httpServer.close(() => resolve());
|
|
136
|
+
this.httpServer = null;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
resolve();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
log.info('Gateway server stopped');
|
|
143
|
+
}
|
|
144
|
+
async invoke(nodeId, tool, args, timeoutMs) {
|
|
145
|
+
const node = this.nodes.get(nodeId);
|
|
146
|
+
if (!node || node.info.status !== 'connected') {
|
|
147
|
+
throw new Error(`Node "${nodeId}" is not connected`);
|
|
148
|
+
}
|
|
149
|
+
const id = nanoid(12);
|
|
150
|
+
const timeout = timeoutMs ?? this.config.invokeTimeoutMs;
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const timer = setTimeout(() => {
|
|
153
|
+
this.pendingInvocations.delete(id);
|
|
154
|
+
reject(new Error(`Invoke timeout: ${tool}@${nodeId} after ${timeout}ms`));
|
|
155
|
+
}, timeout);
|
|
156
|
+
this.pendingInvocations.set(id, { resolve, reject, timer });
|
|
157
|
+
this.send(node.ws, { type: 'invoke', id, tool, args });
|
|
158
|
+
log.info('Invoke sent', { id, nodeId, tool });
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
emergencyStopAll() {
|
|
162
|
+
for (const [nodeId, node] of this.nodes) {
|
|
163
|
+
this.send(node.ws, { type: 'emergency-stop' });
|
|
164
|
+
log.warn('Emergency stop sent to node', { nodeId });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
listNodes() {
|
|
168
|
+
return Array.from(this.nodes.values()).map((n) => ({ ...n.info }));
|
|
169
|
+
}
|
|
170
|
+
getNode(nodeId) {
|
|
171
|
+
return this.nodes.get(nodeId)?.info;
|
|
172
|
+
}
|
|
173
|
+
getGatewayId() {
|
|
174
|
+
return this.gatewayId;
|
|
175
|
+
}
|
|
176
|
+
getPort() {
|
|
177
|
+
return this.config.port;
|
|
178
|
+
}
|
|
179
|
+
handleFrame(nodeId, frame) {
|
|
180
|
+
const node = this.nodes.get(nodeId);
|
|
181
|
+
if (!node)
|
|
182
|
+
return;
|
|
183
|
+
switch (frame.type) {
|
|
184
|
+
case 'invoke-result': {
|
|
185
|
+
const pending = this.pendingInvocations.get(frame.id);
|
|
186
|
+
if (pending) {
|
|
187
|
+
clearTimeout(pending.timer);
|
|
188
|
+
this.pendingInvocations.delete(frame.id);
|
|
189
|
+
if (frame.isError) {
|
|
190
|
+
pending.reject(new Error(frame.output));
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
pending.resolve(frame.output);
|
|
194
|
+
}
|
|
195
|
+
log.info('Invoke result received', { id: frame.id, nodeId, isError: frame.isError });
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case 'pong':
|
|
200
|
+
node.info.lastPingAt = Date.now();
|
|
201
|
+
node.info.status = 'connected';
|
|
202
|
+
break;
|
|
203
|
+
case 'status-response':
|
|
204
|
+
// Status responses are informational; log them
|
|
205
|
+
log.info('Node status', { nodeId, status: frame.status });
|
|
206
|
+
break;
|
|
207
|
+
case 'disconnect':
|
|
208
|
+
this.handleNodeDisconnect(nodeId, frame.reason);
|
|
209
|
+
break;
|
|
210
|
+
default:
|
|
211
|
+
log.warn('Unexpected frame from node', { nodeId, type: frame.type });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
handleNodeDisconnect(nodeId, reason) {
|
|
215
|
+
const node = this.nodes.get(nodeId);
|
|
216
|
+
if (!node)
|
|
217
|
+
return;
|
|
218
|
+
const capabilities = [...node.info.capabilities];
|
|
219
|
+
node.info.status = 'disconnected';
|
|
220
|
+
this.nodes.delete(nodeId);
|
|
221
|
+
log.info('Node disconnected', { nodeId, reason });
|
|
222
|
+
this.onNodeDisconnected?.(nodeId, reason, capabilities);
|
|
223
|
+
}
|
|
224
|
+
startPingLoop() {
|
|
225
|
+
this.pingTimer = setInterval(() => {
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
const staleThreshold = this.config.pingIntervalMs * 3;
|
|
228
|
+
const disconnectThreshold = this.config.pingIntervalMs * 6;
|
|
229
|
+
for (const [nodeId, node] of this.nodes) {
|
|
230
|
+
const elapsed = now - node.info.lastPingAt;
|
|
231
|
+
if (elapsed > disconnectThreshold) {
|
|
232
|
+
log.warn('Node timed out', { nodeId, elapsedMs: elapsed });
|
|
233
|
+
this.handleNodeDisconnect(nodeId, 'ping timeout');
|
|
234
|
+
node.ws.close();
|
|
235
|
+
}
|
|
236
|
+
else if (elapsed > staleThreshold) {
|
|
237
|
+
node.info.status = 'stale';
|
|
238
|
+
}
|
|
239
|
+
this.send(node.ws, { type: 'ping' });
|
|
240
|
+
}
|
|
241
|
+
}, this.config.pingIntervalMs);
|
|
242
|
+
}
|
|
243
|
+
send(ws, frame) {
|
|
244
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
245
|
+
ws.send(JSON.stringify(frame));
|
|
246
|
+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AAK,EAAE,CAAC;gBAClB,CAAC;qBAAM,IAAI,OAAO,GAAG,cAAc,EAAE,CAAC;oBACpC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;gBAC7B,CAAC;gBAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;YACvC,CAAC;QACH,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IACjC,CAAC;IAEO,IAAI,CAAC,EAAa,EAAE,KAAmB;QAC7C,IAAI,EAAE,CAAC,UAAU,KAAK,SAAS,CAAC,IAAI,EAAE,CAAC;YACrC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;CACF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export interface NodeStatus {
|
|
2
|
+
healthy: boolean;
|
|
3
|
+
uptime: number;
|
|
4
|
+
activeTools: number;
|
|
5
|
+
autoMode: boolean;
|
|
6
|
+
}
|
|
7
|
+
export type GatewayFrame = {
|
|
8
|
+
type: 'connect';
|
|
9
|
+
nodeId: string;
|
|
10
|
+
token: string;
|
|
11
|
+
capabilities: string[];
|
|
12
|
+
} | {
|
|
13
|
+
type: 'connected';
|
|
14
|
+
gatewayId: string;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'error';
|
|
17
|
+
message: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'invoke';
|
|
20
|
+
id: string;
|
|
21
|
+
tool: string;
|
|
22
|
+
args: Record<string, unknown>;
|
|
23
|
+
} | {
|
|
24
|
+
type: 'invoke-result';
|
|
25
|
+
id: string;
|
|
26
|
+
output: string;
|
|
27
|
+
isError: boolean;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'ping';
|
|
30
|
+
} | {
|
|
31
|
+
type: 'pong';
|
|
32
|
+
} | {
|
|
33
|
+
type: 'status-request';
|
|
34
|
+
id: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'status-response';
|
|
37
|
+
id: string;
|
|
38
|
+
status: NodeStatus;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'emergency-stop';
|
|
41
|
+
} | {
|
|
42
|
+
type: 'disconnect';
|
|
43
|
+
reason: string;
|
|
44
|
+
};
|
|
45
|
+
export interface NodeInfo {
|
|
46
|
+
nodeId: string;
|
|
47
|
+
capabilities: string[];
|
|
48
|
+
connectedAt: number;
|
|
49
|
+
lastPingAt: number;
|
|
50
|
+
status: 'connected' | 'stale' | 'disconnected';
|
|
51
|
+
}
|
|
52
|
+
export interface GatewayServerConfig {
|
|
53
|
+
port: number;
|
|
54
|
+
token?: string;
|
|
55
|
+
invokeTimeoutMs: number;
|
|
56
|
+
pingIntervalMs: number;
|
|
57
|
+
}
|
|
58
|
+
export interface GatewayClientConfig {
|
|
59
|
+
gatewayUrl: string;
|
|
60
|
+
nodeId: string;
|
|
61
|
+
token: string;
|
|
62
|
+
capabilities: string[];
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,MAAM,YAAY,GACpB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,EAAE,CAAA;CAAE,GAC1E;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,OAAO,CA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AE,CAAC;CACxB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cereworker/gateway",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/Producible/CereWorker.git",
|
|
16
|
+
"directory": "packages/gateway"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"nanoid": "^5.0.9",
|
|
20
|
+
"ws": "^8.18.0",
|
|
21
|
+
"@cereworker/core": "0.6.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/ws": "^8.5.14",
|
|
25
|
+
"typescript": "^5.7.3"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"lint": "eslint src/",
|
|
31
|
+
"clean": "rm -rf dist"
|
|
32
|
+
}
|
|
33
|
+
}
|