@producible/cereworker-gateway 26.520.1
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 +38 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +282 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -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 +23 -0
- package/dist/proxy-tools.js.map +1 -0
- package/dist/server.d.ts +42 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +373 -0
- package/dist/server.js.map +1 -0
- package/dist/session-bus.d.ts +27 -0
- package/dist/session-bus.d.ts.map +1 -0
- package/dist/session-bus.js +127 -0
- package/dist/session-bus.js.map +1 -0
- package/dist/types.d.ts +105 -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,38 @@
|
|
|
1
|
+
import { type ToolResult } from '@producible/cereworker-core';
|
|
2
|
+
import type { GatewayClientConfig } from './types.js';
|
|
3
|
+
export interface RemoteToolExecutionContext {
|
|
4
|
+
conversationId?: string;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
turnId?: string;
|
|
7
|
+
attempt?: number;
|
|
8
|
+
scopeKey?: string;
|
|
9
|
+
callId?: string;
|
|
10
|
+
}
|
|
11
|
+
export type ToolExecutor = (tool: string, args: Record<string, unknown>, context?: RemoteToolExecutionContext) => Promise<string | ToolResult>;
|
|
12
|
+
export declare class GatewayNodeClient {
|
|
13
|
+
private ws;
|
|
14
|
+
private config;
|
|
15
|
+
private executor;
|
|
16
|
+
private onEmergencyStop?;
|
|
17
|
+
private reconnectAttempt;
|
|
18
|
+
private maxReconnectAttempts;
|
|
19
|
+
private reconnectTimer;
|
|
20
|
+
private connected;
|
|
21
|
+
private stopped;
|
|
22
|
+
private startedAt;
|
|
23
|
+
private readonly bus;
|
|
24
|
+
constructor(config: GatewayClientConfig, executor: ToolExecutor);
|
|
25
|
+
setEmergencyStopHandler(handler: () => void): void;
|
|
26
|
+
connect(): Promise<void>;
|
|
27
|
+
disconnect(reason?: string): Promise<void>;
|
|
28
|
+
isConnected(): boolean;
|
|
29
|
+
private doConnect;
|
|
30
|
+
private handleFrame;
|
|
31
|
+
private handleInvoke;
|
|
32
|
+
private normalizeResult;
|
|
33
|
+
private getStatus;
|
|
34
|
+
private scheduleReconnect;
|
|
35
|
+
private sendEnvelope;
|
|
36
|
+
private sendRaw;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,KAAK,EAEV,mBAAmB,EAGpB,MAAM,YAAY,CAAC;AAKpB,MAAM,WAAW,0BAA0B;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,YAAY,GAAG,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,0BAA0B,KACjC,OAAO,CAAC,MAAM,GAAG,UAAU,CAAC,CAAC;AAElC,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;IAC/B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAkB;gBAE1B,MAAM,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY;IAY/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;IAmB3D,WAAW,IAAI,OAAO;IAItB,OAAO,CAAC,SAAS;IAgEjB,OAAO,CAAC,WAAW;YA4DL,YAAY;IAgE1B,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,SAAS;IASjB,OAAO,CAAC,iBAAiB;IAuBzB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,OAAO;CAKhB"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import { createLogger } from '@producible/cereworker-core';
|
|
3
|
+
import { SessionBusState, buildBusStatePath } from './session-bus.js';
|
|
4
|
+
const log = createLogger('gateway-client');
|
|
5
|
+
export class GatewayNodeClient {
|
|
6
|
+
ws = null;
|
|
7
|
+
config;
|
|
8
|
+
executor;
|
|
9
|
+
onEmergencyStop;
|
|
10
|
+
reconnectAttempt = 0;
|
|
11
|
+
maxReconnectAttempts = 10;
|
|
12
|
+
reconnectTimer = null;
|
|
13
|
+
connected = false;
|
|
14
|
+
stopped = false;
|
|
15
|
+
startedAt = Date.now();
|
|
16
|
+
bus;
|
|
17
|
+
constructor(config, executor) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.executor = executor;
|
|
20
|
+
this.bus = new SessionBusState(buildBusStatePath(config.stateDir, `node-${config.nodeId}`, `${config.gatewayUrl}-${config.nodeId}`));
|
|
21
|
+
}
|
|
22
|
+
setEmergencyStopHandler(handler) {
|
|
23
|
+
this.onEmergencyStop = handler;
|
|
24
|
+
}
|
|
25
|
+
async connect() {
|
|
26
|
+
this.stopped = false;
|
|
27
|
+
this.startedAt = Date.now();
|
|
28
|
+
return this.doConnect();
|
|
29
|
+
}
|
|
30
|
+
async disconnect(reason = 'client shutdown') {
|
|
31
|
+
this.stopped = true;
|
|
32
|
+
if (this.reconnectTimer) {
|
|
33
|
+
clearTimeout(this.reconnectTimer);
|
|
34
|
+
this.reconnectTimer = null;
|
|
35
|
+
}
|
|
36
|
+
if (this.ws) {
|
|
37
|
+
this.sendEnvelope('transport.disconnect', {
|
|
38
|
+
sessionId: `node:${this.config.nodeId}`,
|
|
39
|
+
source: 'node',
|
|
40
|
+
payload: { reason },
|
|
41
|
+
});
|
|
42
|
+
this.ws.close();
|
|
43
|
+
this.ws = null;
|
|
44
|
+
}
|
|
45
|
+
this.connected = false;
|
|
46
|
+
log.info('Disconnected from gateway', { reason });
|
|
47
|
+
}
|
|
48
|
+
isConnected() {
|
|
49
|
+
return this.connected;
|
|
50
|
+
}
|
|
51
|
+
doConnect() {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const ws = new WebSocket(this.config.gatewayUrl);
|
|
54
|
+
ws.on('open', () => {
|
|
55
|
+
this.ws = ws;
|
|
56
|
+
this.sendEnvelope('transport.connect', {
|
|
57
|
+
sessionId: `node:${this.config.nodeId}`,
|
|
58
|
+
source: 'node',
|
|
59
|
+
payload: {
|
|
60
|
+
nodeId: this.config.nodeId,
|
|
61
|
+
token: this.config.token,
|
|
62
|
+
capabilities: this.config.capabilities,
|
|
63
|
+
},
|
|
64
|
+
resumeFromSequence: this.bus.getResumeSequence(),
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
ws.on('message', (data) => {
|
|
68
|
+
let frame;
|
|
69
|
+
try {
|
|
70
|
+
frame = JSON.parse(data.toString());
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
log.warn('Received invalid JSON from gateway');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const isConnectedFrame = frame.eventType === 'transport.connected';
|
|
77
|
+
if (isConnectedFrame) {
|
|
78
|
+
this.bus.markConnected(frame);
|
|
79
|
+
}
|
|
80
|
+
const shouldProcess = isConnectedFrame ? true : this.bus.markReceived(frame);
|
|
81
|
+
if (frame.eventType !== 'transport.ack') {
|
|
82
|
+
this.sendEnvelope('transport.ack', {
|
|
83
|
+
sessionId: frame.sessionId,
|
|
84
|
+
conversationId: frame.conversationId,
|
|
85
|
+
source: 'node',
|
|
86
|
+
payload: { message: 'ack' },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (!shouldProcess)
|
|
90
|
+
return;
|
|
91
|
+
this.handleFrame(frame, resolve, reject);
|
|
92
|
+
});
|
|
93
|
+
ws.on('close', () => {
|
|
94
|
+
this.connected = false;
|
|
95
|
+
this.ws = null;
|
|
96
|
+
if (!this.stopped) {
|
|
97
|
+
log.warn('Connection to gateway lost, reconnecting...');
|
|
98
|
+
this.scheduleReconnect();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
ws.on('error', (err) => {
|
|
102
|
+
log.error('WebSocket error', { error: err.message });
|
|
103
|
+
if (!this.connected) {
|
|
104
|
+
reject(err);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
handleFrame(frame, onConnect, onError) {
|
|
110
|
+
switch (frame.eventType) {
|
|
111
|
+
case 'transport.connected':
|
|
112
|
+
this.connected = true;
|
|
113
|
+
this.reconnectAttempt = 0;
|
|
114
|
+
for (const pending of this.bus.replayAfter(frame.resumeFromSequence ?? 0)) {
|
|
115
|
+
this.sendRaw(pending);
|
|
116
|
+
}
|
|
117
|
+
log.info('Connected to gateway', { gatewayId: frame.payload.gatewayId, nodeId: this.config.nodeId });
|
|
118
|
+
onConnect?.();
|
|
119
|
+
break;
|
|
120
|
+
case 'transport.error':
|
|
121
|
+
log.error('Gateway error', { message: frame.payload.message });
|
|
122
|
+
if (!this.connected) {
|
|
123
|
+
onError?.(new Error(frame.payload.message));
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
case 'invoke.request':
|
|
127
|
+
void this.handleInvoke(frame);
|
|
128
|
+
break;
|
|
129
|
+
case 'transport.ping':
|
|
130
|
+
this.sendEnvelope('transport.pong', {
|
|
131
|
+
sessionId: frame.sessionId,
|
|
132
|
+
conversationId: frame.conversationId,
|
|
133
|
+
source: 'node',
|
|
134
|
+
payload: { sentAt: frame.payload.sentAt },
|
|
135
|
+
});
|
|
136
|
+
break;
|
|
137
|
+
case 'node.status.request':
|
|
138
|
+
this.sendEnvelope('node.status', {
|
|
139
|
+
sessionId: frame.sessionId,
|
|
140
|
+
conversationId: frame.conversationId,
|
|
141
|
+
source: 'node',
|
|
142
|
+
payload: {
|
|
143
|
+
requestId: frame.payload.requestId,
|
|
144
|
+
status: this.getStatus(),
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
case 'transport.emergency-stop':
|
|
149
|
+
log.warn('Emergency stop received from gateway');
|
|
150
|
+
this.onEmergencyStop?.();
|
|
151
|
+
break;
|
|
152
|
+
case 'transport.disconnect':
|
|
153
|
+
log.info('Gateway requested disconnect', { reason: frame.payload.reason });
|
|
154
|
+
this.stopped = true;
|
|
155
|
+
this.ws?.close();
|
|
156
|
+
break;
|
|
157
|
+
default:
|
|
158
|
+
log.warn('Unexpected frame from gateway', { eventType: frame.eventType });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async handleInvoke(frame) {
|
|
162
|
+
const { invocationId, tool, args, context } = frame.payload;
|
|
163
|
+
log.info('Invoke received', { invocationId, tool });
|
|
164
|
+
this.sendEnvelope('tool.started', {
|
|
165
|
+
sessionId: frame.sessionId,
|
|
166
|
+
conversationId: frame.conversationId,
|
|
167
|
+
source: 'node',
|
|
168
|
+
payload: {
|
|
169
|
+
invocationId,
|
|
170
|
+
tool,
|
|
171
|
+
args,
|
|
172
|
+
context,
|
|
173
|
+
nodeId: this.config.nodeId,
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
let result;
|
|
177
|
+
try {
|
|
178
|
+
const rawResult = await this.executor(tool, args, {
|
|
179
|
+
conversationId: frame.conversationId,
|
|
180
|
+
sessionId: frame.sessionId,
|
|
181
|
+
turnId: context?.turnId ?? frame.sessionId,
|
|
182
|
+
attempt: context?.attempt,
|
|
183
|
+
scopeKey: context?.scopeKey,
|
|
184
|
+
callId: context?.callId ?? invocationId,
|
|
185
|
+
});
|
|
186
|
+
result = this.normalizeResult(rawResult, context?.callId ?? invocationId);
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
190
|
+
result = {
|
|
191
|
+
callId: context?.callId ?? invocationId,
|
|
192
|
+
output: message,
|
|
193
|
+
isError: true,
|
|
194
|
+
metadata: {
|
|
195
|
+
runtimeError: true,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
this.sendEnvelope('tool.finished', {
|
|
200
|
+
sessionId: frame.sessionId,
|
|
201
|
+
conversationId: frame.conversationId,
|
|
202
|
+
source: 'node',
|
|
203
|
+
payload: {
|
|
204
|
+
invocationId,
|
|
205
|
+
tool,
|
|
206
|
+
args,
|
|
207
|
+
result,
|
|
208
|
+
context,
|
|
209
|
+
nodeId: this.config.nodeId,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
this.sendEnvelope('invoke.result', {
|
|
213
|
+
sessionId: frame.sessionId,
|
|
214
|
+
conversationId: frame.conversationId,
|
|
215
|
+
source: 'node',
|
|
216
|
+
payload: {
|
|
217
|
+
invocationId,
|
|
218
|
+
result,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
normalizeResult(raw, callId) {
|
|
223
|
+
if (typeof raw === 'string') {
|
|
224
|
+
return {
|
|
225
|
+
callId,
|
|
226
|
+
output: raw,
|
|
227
|
+
isError: false,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
...raw,
|
|
232
|
+
callId: raw.callId || callId,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
getStatus() {
|
|
236
|
+
return {
|
|
237
|
+
healthy: true,
|
|
238
|
+
uptime: Math.floor((Date.now() - this.startedAt) / 1000),
|
|
239
|
+
activeTools: this.config.capabilities.length,
|
|
240
|
+
autoMode: false,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
scheduleReconnect() {
|
|
244
|
+
if (this.stopped || this.reconnectAttempt >= this.maxReconnectAttempts) {
|
|
245
|
+
if (this.reconnectAttempt >= this.maxReconnectAttempts) {
|
|
246
|
+
log.error('Max reconnect attempts reached, giving up');
|
|
247
|
+
}
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const baseDelay = 1000;
|
|
251
|
+
const maxDelay = 30_000;
|
|
252
|
+
const delay = Math.min(baseDelay * Math.pow(2, this.reconnectAttempt), maxDelay);
|
|
253
|
+
const jitter = delay * (0.5 + Math.random() * 0.5);
|
|
254
|
+
this.reconnectAttempt++;
|
|
255
|
+
log.info('Reconnecting...', { attempt: this.reconnectAttempt, delayMs: Math.round(jitter) });
|
|
256
|
+
this.reconnectTimer = setTimeout(() => {
|
|
257
|
+
this.doConnect().catch((err) => {
|
|
258
|
+
log.error('Reconnect failed', { error: err.message });
|
|
259
|
+
});
|
|
260
|
+
}, jitter);
|
|
261
|
+
}
|
|
262
|
+
sendEnvelope(eventType, options) {
|
|
263
|
+
const envelope = this.bus.createEnvelope({
|
|
264
|
+
eventType,
|
|
265
|
+
senderId: this.config.nodeId,
|
|
266
|
+
instanceId: this.config.instanceId,
|
|
267
|
+
sessionId: options.sessionId,
|
|
268
|
+
conversationId: options.conversationId,
|
|
269
|
+
source: options.source,
|
|
270
|
+
payload: options.payload,
|
|
271
|
+
resumeFromSequence: options.resumeFromSequence,
|
|
272
|
+
});
|
|
273
|
+
this.sendRaw(envelope);
|
|
274
|
+
return envelope;
|
|
275
|
+
}
|
|
276
|
+
sendRaw(frame) {
|
|
277
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
278
|
+
this.ws.send(JSON.stringify(frame));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,IAAI,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAmB,MAAM,6BAA6B,CAAC;AAO5E,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAEtE,MAAM,GAAG,GAAG,YAAY,CAAC,gBAAgB,CAAC,CAAC;AAiB3C,MAAM,OAAO,iBAAiB;IACpB,EAAE,GAAqB,IAAI,CAAC;IAC5B,MAAM,CAAsB;IAC5B,QAAQ,CAAe;IACvB,eAAe,CAAc;IAC7B,gBAAgB,GAAG,CAAC,CAAC;IACrB,oBAAoB,GAAG,EAAE,CAAC;IAC1B,cAAc,GAAyC,IAAI,CAAC;IAC5D,SAAS,GAAG,KAAK,CAAC;IAClB,OAAO,GAAG,KAAK,CAAC;IAChB,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACd,GAAG,CAAkB;IAEtC,YAAY,MAA2B,EAAE,QAAsB;QAC7D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,GAAG,GAAG,IAAI,eAAe,CAC5B,iBAAiB,CACf,MAAM,CAAC,QAAQ,EACf,QAAQ,MAAM,CAAC,MAAM,EAAE,EACvB,GAAG,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,CACxC,CACF,CAAC;IACJ,CAAC;IAED,uBAAuB,CAAC,OAAmB;QACzC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,SAAS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,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;YACxD,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,MAAM,CAAC,CAAC;IACb,CAAC;IAEO,YAAY,CAClB,SAAY,EACZ,OAMC;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC;YACvC,SAAS;YACT,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM;YAC5B,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU;YAClC,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;SAC/C,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,CAAC,QAA2B,CAAC,CAAC;QAC1C,OAAO,QAA2B,CAAC;IACrC,CAAC;IAEO,OAAO,CAAC,KAAsB;QACpC,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,7 @@
|
|
|
1
|
+
export type { GatewayFrame, AnyGatewayFrame, NodeInfo, NodeStatus, GatewayServerConfig, GatewayClientConfig, SessionBusEventType, SessionBusEnvelopeMap, } from './types.js';
|
|
2
|
+
export { GatewayServer } from './server.js';
|
|
3
|
+
export { GatewayNodeClient } from './client.js';
|
|
4
|
+
export type { ToolExecutor, RemoteToolExecutionContext } from './client.js';
|
|
5
|
+
export { createProxyTools } from './proxy-tools.js';
|
|
6
|
+
export { TRANSPORT_PROTOCOL_VERSION } from './session-bus.js';
|
|
7
|
+
//# 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,eAAe,EACf,QAAQ,EACR,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,0BAA0B,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":"AAWA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,0BAA0B,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ToolDefinition } from '@producible/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,EAAwB,MAAM,6BAA6B,CAAC;AACxF,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,CAuBhC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
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, context) => {
|
|
9
|
+
return server.invoke(nodeId, cap, args, {
|
|
10
|
+
conversationId: context?.conversationId,
|
|
11
|
+
sessionId: context?.sessionKey ?? context?.turnId,
|
|
12
|
+
turnId: context?.turnId,
|
|
13
|
+
attempt: context?.attempt,
|
|
14
|
+
callId: context?.callId,
|
|
15
|
+
requestedToolName: context?.toolName,
|
|
16
|
+
scopeKey: context?.scopeKey,
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return tools;
|
|
22
|
+
}
|
|
23
|
+
//# 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,OAA8B,EAAE,EAAE;gBACtD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE;oBACtC,cAAc,EAAE,OAAO,EAAE,cAAc;oBACvC,SAAS,EAAE,OAAO,EAAE,UAAU,IAAI,OAAO,EAAE,MAAM;oBACjD,MAAM,EAAE,OAAO,EAAE,MAAM;oBACvB,OAAO,EAAE,OAAO,EAAE,OAAO;oBACzB,MAAM,EAAE,OAAO,EAAE,MAAM;oBACvB,iBAAiB,EAAE,OAAO,EAAE,QAAQ;oBACpC,QAAQ,EAAE,OAAO,EAAE,QAAQ;iBAC5B,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type ToolResult } from '@producible/cereworker-core';
|
|
2
|
+
import type { AnyGatewayFrame, GatewayServerConfig, NodeInfo } from './types.js';
|
|
3
|
+
export declare class GatewayServer {
|
|
4
|
+
private wss;
|
|
5
|
+
private httpServer;
|
|
6
|
+
private nodes;
|
|
7
|
+
private pendingInvocations;
|
|
8
|
+
private pingTimer;
|
|
9
|
+
private gatewayId;
|
|
10
|
+
private config;
|
|
11
|
+
private onNodeConnected?;
|
|
12
|
+
private onNodeDisconnected?;
|
|
13
|
+
private onEnvelope?;
|
|
14
|
+
constructor(config: GatewayServerConfig);
|
|
15
|
+
setNodeConnectedHandler(handler: (nodeId: string, capabilities: string[]) => void): void;
|
|
16
|
+
setNodeDisconnectedHandler(handler: (nodeId: string, reason: string, capabilities: string[]) => void): void;
|
|
17
|
+
setEnvelopeHandler(handler: (nodeId: string, frame: AnyGatewayFrame) => void): void;
|
|
18
|
+
start(): Promise<void>;
|
|
19
|
+
stop(): Promise<void>;
|
|
20
|
+
invoke(nodeId: string, tool: string, args: Record<string, unknown>, options?: {
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
conversationId?: string;
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
turnId?: string;
|
|
25
|
+
attempt?: number;
|
|
26
|
+
callId?: string;
|
|
27
|
+
requestedToolName?: string;
|
|
28
|
+
scopeKey?: string;
|
|
29
|
+
}): Promise<ToolResult>;
|
|
30
|
+
emergencyStopAll(): void;
|
|
31
|
+
listNodes(): NodeInfo[];
|
|
32
|
+
getNode(nodeId: string): NodeInfo | undefined;
|
|
33
|
+
getGatewayId(): string;
|
|
34
|
+
getPort(): number;
|
|
35
|
+
private createServerEnvelope;
|
|
36
|
+
private handleFrame;
|
|
37
|
+
private handleNodeDisconnect;
|
|
38
|
+
private startPingLoop;
|
|
39
|
+
private sendEnvelope;
|
|
40
|
+
private sendRaw;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,KAAK,EACV,eAAe,EAEf,mBAAmB,EACnB,QAAQ,EAIT,MAAM,YAAY,CAAC;AAmBpB,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;IAC9F,OAAO,CAAC,UAAU,CAAC,CAAmD;gBAE1D,MAAM,EAAE,mBAAmB;IA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,OAAO;CAKhB"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { WebSocketServer, WebSocket } from 'ws';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { createLogger } from '@producible/cereworker-core';
|
|
5
|
+
import { SessionBusState, buildBusStatePath } from './session-bus.js';
|
|
6
|
+
const log = createLogger('gateway-server');
|
|
7
|
+
export class GatewayServer {
|
|
8
|
+
wss = null;
|
|
9
|
+
httpServer = null;
|
|
10
|
+
nodes = new Map();
|
|
11
|
+
pendingInvocations = new Map();
|
|
12
|
+
pingTimer = null;
|
|
13
|
+
gatewayId;
|
|
14
|
+
config;
|
|
15
|
+
onNodeConnected;
|
|
16
|
+
onNodeDisconnected;
|
|
17
|
+
onEnvelope;
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.gatewayId = nanoid(12);
|
|
21
|
+
}
|
|
22
|
+
setNodeConnectedHandler(handler) {
|
|
23
|
+
this.onNodeConnected = handler;
|
|
24
|
+
}
|
|
25
|
+
setNodeDisconnectedHandler(handler) {
|
|
26
|
+
this.onNodeDisconnected = handler;
|
|
27
|
+
}
|
|
28
|
+
setEnvelopeHandler(handler) {
|
|
29
|
+
this.onEnvelope = handler;
|
|
30
|
+
}
|
|
31
|
+
async start() {
|
|
32
|
+
this.httpServer = createServer((req, res) => {
|
|
33
|
+
if (req.url === '/status' && req.method === 'GET') {
|
|
34
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
35
|
+
res.end(JSON.stringify({ gatewayId: this.gatewayId, nodes: this.listNodes() }));
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
res.writeHead(404);
|
|
39
|
+
res.end();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.wss = new WebSocketServer({ server: this.httpServer });
|
|
43
|
+
this.wss.on('connection', (ws) => {
|
|
44
|
+
let authenticated = false;
|
|
45
|
+
let nodeId = null;
|
|
46
|
+
let nodeBus = null;
|
|
47
|
+
const authTimeout = setTimeout(() => {
|
|
48
|
+
if (!authenticated) {
|
|
49
|
+
this.sendRaw(ws, this.createServerEnvelope('transport.error', {
|
|
50
|
+
sessionId: 'gateway',
|
|
51
|
+
source: 'gateway',
|
|
52
|
+
payload: { message: 'Authentication timeout' },
|
|
53
|
+
}));
|
|
54
|
+
ws.close();
|
|
55
|
+
}
|
|
56
|
+
}, 10_000);
|
|
57
|
+
ws.on('message', (data) => {
|
|
58
|
+
let frame;
|
|
59
|
+
try {
|
|
60
|
+
frame = JSON.parse(data.toString());
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
this.sendRaw(ws, this.createServerEnvelope('transport.error', {
|
|
64
|
+
sessionId: 'gateway',
|
|
65
|
+
source: 'gateway',
|
|
66
|
+
payload: { message: 'Invalid JSON' },
|
|
67
|
+
}));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!authenticated) {
|
|
71
|
+
if (frame.eventType !== 'transport.connect') {
|
|
72
|
+
this.sendRaw(ws, this.createServerEnvelope('transport.error', {
|
|
73
|
+
sessionId: 'gateway',
|
|
74
|
+
source: 'gateway',
|
|
75
|
+
payload: { message: 'Must authenticate first' },
|
|
76
|
+
}));
|
|
77
|
+
ws.close();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
clearTimeout(authTimeout);
|
|
81
|
+
const { nodeId: requestedNodeId, token, capabilities } = frame.payload;
|
|
82
|
+
if (this.config.token && token !== this.config.token) {
|
|
83
|
+
this.sendRaw(ws, this.createServerEnvelope('transport.error', {
|
|
84
|
+
sessionId: 'gateway',
|
|
85
|
+
source: 'gateway',
|
|
86
|
+
payload: { message: 'Invalid token' },
|
|
87
|
+
}));
|
|
88
|
+
ws.close();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (this.nodes.has(requestedNodeId)) {
|
|
92
|
+
this.sendRaw(ws, this.createServerEnvelope('transport.error', {
|
|
93
|
+
sessionId: 'gateway',
|
|
94
|
+
source: 'gateway',
|
|
95
|
+
payload: { message: `Node "${requestedNodeId}" already connected` },
|
|
96
|
+
}));
|
|
97
|
+
ws.close();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
nodeId = requestedNodeId;
|
|
101
|
+
nodeBus = new SessionBusState(buildBusStatePath(this.config.stateDir, `gateway-${requestedNodeId}`, requestedNodeId));
|
|
102
|
+
nodeBus.markReceived(frame);
|
|
103
|
+
authenticated = true;
|
|
104
|
+
const info = {
|
|
105
|
+
nodeId: requestedNodeId,
|
|
106
|
+
capabilities,
|
|
107
|
+
connectedAt: Date.now(),
|
|
108
|
+
lastPingAt: Date.now(),
|
|
109
|
+
status: 'connected',
|
|
110
|
+
};
|
|
111
|
+
this.nodes.set(requestedNodeId, { ws, info, bus: nodeBus });
|
|
112
|
+
this.sendEnvelope(this.nodes.get(requestedNodeId), 'transport.connected', {
|
|
113
|
+
sessionId: 'gateway',
|
|
114
|
+
source: 'gateway',
|
|
115
|
+
payload: { gatewayId: this.gatewayId },
|
|
116
|
+
resumeFromSequence: nodeBus.getHighestReceivedSequence(),
|
|
117
|
+
});
|
|
118
|
+
const replayFrom = frame.resumeFromSequence ?? 0;
|
|
119
|
+
for (const pending of nodeBus.replayAfter(replayFrom)) {
|
|
120
|
+
this.sendRaw(ws, pending);
|
|
121
|
+
}
|
|
122
|
+
log.info('Node connected', { nodeId: requestedNodeId, capabilities, replayFrom });
|
|
123
|
+
this.onNodeConnected?.(requestedNodeId, capabilities);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this.handleFrame(nodeId, frame);
|
|
127
|
+
});
|
|
128
|
+
ws.on('close', () => {
|
|
129
|
+
clearTimeout(authTimeout);
|
|
130
|
+
if (nodeId) {
|
|
131
|
+
this.handleNodeDisconnect(nodeId, 'connection closed');
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
ws.on('error', (err) => {
|
|
135
|
+
log.error('WebSocket error', { nodeId, error: err.message });
|
|
136
|
+
if (nodeId) {
|
|
137
|
+
this.handleNodeDisconnect(nodeId, `error: ${err.message}`);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
await new Promise((resolve) => {
|
|
142
|
+
this.httpServer.listen(this.config.port, () => {
|
|
143
|
+
log.info('Gateway server started', { port: this.config.port, gatewayId: this.gatewayId });
|
|
144
|
+
resolve();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
this.startPingLoop();
|
|
148
|
+
}
|
|
149
|
+
async stop() {
|
|
150
|
+
if (this.pingTimer) {
|
|
151
|
+
clearInterval(this.pingTimer);
|
|
152
|
+
this.pingTimer = null;
|
|
153
|
+
}
|
|
154
|
+
for (const [id, pending] of this.pendingInvocations) {
|
|
155
|
+
clearTimeout(pending.timer);
|
|
156
|
+
pending.reject(new Error('Gateway shutting down'));
|
|
157
|
+
this.pendingInvocations.delete(id);
|
|
158
|
+
}
|
|
159
|
+
for (const [, node] of this.nodes) {
|
|
160
|
+
this.sendEnvelope(node, 'transport.disconnect', {
|
|
161
|
+
sessionId: 'gateway',
|
|
162
|
+
source: 'gateway',
|
|
163
|
+
payload: { reason: 'gateway shutting down' },
|
|
164
|
+
});
|
|
165
|
+
node.ws.close();
|
|
166
|
+
}
|
|
167
|
+
this.nodes.clear();
|
|
168
|
+
if (this.wss) {
|
|
169
|
+
this.wss.close();
|
|
170
|
+
this.wss = null;
|
|
171
|
+
}
|
|
172
|
+
await new Promise((resolve) => {
|
|
173
|
+
if (this.httpServer) {
|
|
174
|
+
this.httpServer.close(() => resolve());
|
|
175
|
+
this.httpServer = null;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
resolve();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
log.info('Gateway server stopped');
|
|
182
|
+
}
|
|
183
|
+
async invoke(nodeId, tool, args, options) {
|
|
184
|
+
const node = this.nodes.get(nodeId);
|
|
185
|
+
if (!node || node.info.status !== 'connected') {
|
|
186
|
+
throw new Error(`Node "${nodeId}" is not connected`);
|
|
187
|
+
}
|
|
188
|
+
const invocationId = nanoid(12);
|
|
189
|
+
const timeout = options?.timeoutMs ?? this.config.invokeTimeoutMs;
|
|
190
|
+
const context = {
|
|
191
|
+
callId: options?.callId,
|
|
192
|
+
requestedToolName: options?.requestedToolName,
|
|
193
|
+
turnId: options?.turnId,
|
|
194
|
+
attempt: options?.attempt,
|
|
195
|
+
scopeKey: options?.scopeKey,
|
|
196
|
+
};
|
|
197
|
+
return new Promise((resolve, reject) => {
|
|
198
|
+
const timer = setTimeout(() => {
|
|
199
|
+
this.pendingInvocations.delete(invocationId);
|
|
200
|
+
reject(new Error(`Invoke timeout: ${tool}@${nodeId} after ${timeout}ms`));
|
|
201
|
+
}, timeout);
|
|
202
|
+
this.pendingInvocations.set(invocationId, {
|
|
203
|
+
resolve,
|
|
204
|
+
reject,
|
|
205
|
+
timer,
|
|
206
|
+
nodeId,
|
|
207
|
+
tool,
|
|
208
|
+
});
|
|
209
|
+
this.sendEnvelope(node, 'invoke.request', {
|
|
210
|
+
sessionId: options?.sessionId ?? options?.turnId ?? `node:${nodeId}`,
|
|
211
|
+
conversationId: options?.conversationId,
|
|
212
|
+
source: 'gateway',
|
|
213
|
+
payload: {
|
|
214
|
+
invocationId,
|
|
215
|
+
tool,
|
|
216
|
+
args,
|
|
217
|
+
context,
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
log.info('Invoke sent', {
|
|
221
|
+
invocationId,
|
|
222
|
+
nodeId,
|
|
223
|
+
tool,
|
|
224
|
+
sessionId: options?.sessionId ?? options?.turnId ?? `node:${nodeId}`,
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
emergencyStopAll() {
|
|
229
|
+
for (const [nodeId, node] of this.nodes) {
|
|
230
|
+
this.sendEnvelope(node, 'transport.emergency-stop', {
|
|
231
|
+
sessionId: 'gateway',
|
|
232
|
+
source: 'gateway',
|
|
233
|
+
payload: { reason: 'gateway emergency stop' },
|
|
234
|
+
});
|
|
235
|
+
log.warn('Emergency stop sent to node', { nodeId });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
listNodes() {
|
|
239
|
+
return Array.from(this.nodes.values()).map((n) => ({ ...n.info }));
|
|
240
|
+
}
|
|
241
|
+
getNode(nodeId) {
|
|
242
|
+
return this.nodes.get(nodeId)?.info;
|
|
243
|
+
}
|
|
244
|
+
getGatewayId() {
|
|
245
|
+
return this.gatewayId;
|
|
246
|
+
}
|
|
247
|
+
getPort() {
|
|
248
|
+
return this.config.port;
|
|
249
|
+
}
|
|
250
|
+
createServerEnvelope(eventType, options) {
|
|
251
|
+
const bus = new SessionBusState(null);
|
|
252
|
+
return bus.createEnvelope({
|
|
253
|
+
eventType,
|
|
254
|
+
senderId: this.gatewayId,
|
|
255
|
+
instanceId: this.config.instanceId,
|
|
256
|
+
sessionId: options.sessionId,
|
|
257
|
+
conversationId: options.conversationId,
|
|
258
|
+
source: options.source,
|
|
259
|
+
payload: options.payload,
|
|
260
|
+
resumeFromSequence: options.resumeFromSequence,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
handleFrame(nodeId, frame) {
|
|
264
|
+
const node = this.nodes.get(nodeId);
|
|
265
|
+
if (!node)
|
|
266
|
+
return;
|
|
267
|
+
const shouldProcess = node.bus.markReceived(frame);
|
|
268
|
+
if (frame.eventType !== 'transport.ack') {
|
|
269
|
+
this.sendEnvelope(node, 'transport.ack', {
|
|
270
|
+
sessionId: frame.sessionId,
|
|
271
|
+
conversationId: frame.conversationId,
|
|
272
|
+
source: 'gateway',
|
|
273
|
+
payload: { message: 'ack' },
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (!shouldProcess)
|
|
277
|
+
return;
|
|
278
|
+
switch (frame.eventType) {
|
|
279
|
+
case 'invoke.result': {
|
|
280
|
+
const pending = this.pendingInvocations.get(frame.payload.invocationId);
|
|
281
|
+
if (pending) {
|
|
282
|
+
clearTimeout(pending.timer);
|
|
283
|
+
this.pendingInvocations.delete(frame.payload.invocationId);
|
|
284
|
+
if (frame.payload.result.isError) {
|
|
285
|
+
pending.reject(new Error(frame.payload.result.output));
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
pending.resolve(frame.payload.result);
|
|
289
|
+
}
|
|
290
|
+
log.info('Invoke result received', {
|
|
291
|
+
invocationId: frame.payload.invocationId,
|
|
292
|
+
nodeId,
|
|
293
|
+
isError: frame.payload.result.isError,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
this.onEnvelope?.(nodeId, frame);
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
case 'tool.started':
|
|
300
|
+
case 'tool.finished':
|
|
301
|
+
case 'node.status':
|
|
302
|
+
if (frame.eventType === 'node.status') {
|
|
303
|
+
node.info.lastPingAt = Date.now();
|
|
304
|
+
node.info.status = 'connected';
|
|
305
|
+
}
|
|
306
|
+
this.onEnvelope?.(nodeId, frame);
|
|
307
|
+
break;
|
|
308
|
+
case 'transport.pong':
|
|
309
|
+
node.info.lastPingAt = Date.now();
|
|
310
|
+
node.info.status = 'connected';
|
|
311
|
+
break;
|
|
312
|
+
case 'transport.disconnect':
|
|
313
|
+
this.handleNodeDisconnect(nodeId, frame.payload.reason);
|
|
314
|
+
break;
|
|
315
|
+
default:
|
|
316
|
+
log.warn('Unexpected frame from node', { nodeId, eventType: frame.eventType });
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
handleNodeDisconnect(nodeId, reason) {
|
|
320
|
+
const node = this.nodes.get(nodeId);
|
|
321
|
+
if (!node)
|
|
322
|
+
return;
|
|
323
|
+
const capabilities = [...node.info.capabilities];
|
|
324
|
+
node.info.status = 'disconnected';
|
|
325
|
+
this.nodes.delete(nodeId);
|
|
326
|
+
log.info('Node disconnected', { nodeId, reason });
|
|
327
|
+
this.onNodeDisconnected?.(nodeId, reason, capabilities);
|
|
328
|
+
}
|
|
329
|
+
startPingLoop() {
|
|
330
|
+
this.pingTimer = setInterval(() => {
|
|
331
|
+
const now = Date.now();
|
|
332
|
+
const staleThreshold = this.config.pingIntervalMs * 3;
|
|
333
|
+
const disconnectThreshold = this.config.pingIntervalMs * 6;
|
|
334
|
+
for (const [nodeId, node] of this.nodes) {
|
|
335
|
+
const elapsed = now - node.info.lastPingAt;
|
|
336
|
+
if (elapsed > disconnectThreshold) {
|
|
337
|
+
log.warn('Node timed out', { nodeId, elapsedMs: elapsed });
|
|
338
|
+
this.handleNodeDisconnect(nodeId, 'ping timeout');
|
|
339
|
+
node.ws.close();
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (elapsed > staleThreshold) {
|
|
343
|
+
node.info.status = 'stale';
|
|
344
|
+
}
|
|
345
|
+
this.sendEnvelope(node, 'transport.ping', {
|
|
346
|
+
sessionId: 'gateway',
|
|
347
|
+
source: 'gateway',
|
|
348
|
+
payload: { sentAt: Date.now() },
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}, this.config.pingIntervalMs);
|
|
352
|
+
}
|
|
353
|
+
sendEnvelope(node, eventType, options) {
|
|
354
|
+
const envelope = node.bus.createEnvelope({
|
|
355
|
+
eventType,
|
|
356
|
+
senderId: this.gatewayId,
|
|
357
|
+
instanceId: this.config.instanceId,
|
|
358
|
+
sessionId: options.sessionId,
|
|
359
|
+
conversationId: options.conversationId,
|
|
360
|
+
source: options.source,
|
|
361
|
+
payload: options.payload,
|
|
362
|
+
resumeFromSequence: options.resumeFromSequence,
|
|
363
|
+
});
|
|
364
|
+
this.sendRaw(node.ws, envelope);
|
|
365
|
+
return envelope;
|
|
366
|
+
}
|
|
367
|
+
sendRaw(ws, frame) {
|
|
368
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
369
|
+
ws.send(JSON.stringify(frame));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -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5B,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;SAC/C,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,QAA2B,CAAC,CAAC;QACnD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEO,OAAO,CAAC,EAAa,EAAE,KAAsB;QACnD,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"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AnyGatewayFrame, GatewayFrame, SessionBusEnvelopeMap, SessionBusEventType } from './types.js';
|
|
2
|
+
export declare const TRANSPORT_PROTOCOL_VERSION = 1;
|
|
3
|
+
export declare function buildBusStatePath(stateDir: string | undefined, prefix: string, id: string): string | null;
|
|
4
|
+
export declare class SessionBusState {
|
|
5
|
+
private state;
|
|
6
|
+
private readonly statePath;
|
|
7
|
+
private readonly seenEnvelopeIds;
|
|
8
|
+
constructor(statePath: string | null);
|
|
9
|
+
getHighestReceivedSequence(): number;
|
|
10
|
+
getResumeSequence(): number;
|
|
11
|
+
markConnected(frame: AnyGatewayFrame): void;
|
|
12
|
+
createEnvelope<K extends SessionBusEventType>(base: {
|
|
13
|
+
eventType: K;
|
|
14
|
+
senderId: string;
|
|
15
|
+
sessionId: string;
|
|
16
|
+
conversationId?: string;
|
|
17
|
+
source: GatewayFrame<K>['source'];
|
|
18
|
+
instanceId?: string;
|
|
19
|
+
payload: SessionBusEnvelopeMap[K];
|
|
20
|
+
resumeFromSequence?: number;
|
|
21
|
+
}): GatewayFrame<K>;
|
|
22
|
+
markReceived(frame: AnyGatewayFrame): boolean;
|
|
23
|
+
ackThrough(sequence: number): void;
|
|
24
|
+
replayAfter(sequence: number): AnyGatewayFrame[];
|
|
25
|
+
private persist;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=session-bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-bus.d.ts","sourceRoot":"","sources":["../src/session-bus.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,0BAA0B,IAAI,CAAC;AAkC5C,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMzG;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,KAAK,CAA2C;IACxD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgB;IAC1C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAqB;gBAEzC,SAAS,EAAE,MAAM,GAAG,IAAI;IAapC,0BAA0B,IAAI,MAAM;IAIpC,iBAAiB,IAAI,MAAM;IAI3B,aAAa,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI;IAW3C,cAAc,CAAC,CAAC,SAAS,mBAAmB,EAAE,IAAI,EAAE;QAClD,SAAS,EAAE,CAAC,CAAC;QACb,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QAClC,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC,CAAC;QAClC,kBAAkB,CAAC,EAAE,MAAM,CAAC;KAC7B,GAAG,YAAY,CAAC,CAAC,CAAC;IA6BnB,YAAY,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO;IAqB7C,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAMlC,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,eAAe,EAAE;IAIhD,OAAO,CAAC,OAAO;CAIhB"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { basename, join, resolve } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { ensureDir, readJsonFile, writeJsonFileAtomic, } from '@producible/cereworker-core';
|
|
5
|
+
export const TRANSPORT_PROTOCOL_VERSION = 1;
|
|
6
|
+
const DEFAULT_STATE = {
|
|
7
|
+
nextSequence: 1,
|
|
8
|
+
highestReceivedSequence: 0,
|
|
9
|
+
outbound: [],
|
|
10
|
+
};
|
|
11
|
+
const MAX_OUTBOUND_ENVELOPES = 500;
|
|
12
|
+
function shouldPersistOutgoing(eventType) {
|
|
13
|
+
return ![
|
|
14
|
+
'transport.connect',
|
|
15
|
+
'transport.connected',
|
|
16
|
+
'transport.ack',
|
|
17
|
+
'transport.ping',
|
|
18
|
+
'transport.pong',
|
|
19
|
+
].includes(eventType);
|
|
20
|
+
}
|
|
21
|
+
function expandHome(path) {
|
|
22
|
+
return resolve(path.replace(/^~(?=\/|$)/, homedir()));
|
|
23
|
+
}
|
|
24
|
+
function sanitizeSegment(value) {
|
|
25
|
+
return value.replace(/[^a-zA-Z0-9._-]+/g, '-');
|
|
26
|
+
}
|
|
27
|
+
export function buildBusStatePath(stateDir, prefix, id) {
|
|
28
|
+
if (!stateDir)
|
|
29
|
+
return null;
|
|
30
|
+
const dir = expandHome(stateDir);
|
|
31
|
+
ensureDir(dir);
|
|
32
|
+
const digest = createHash('sha1').update(id).digest('hex').slice(0, 12);
|
|
33
|
+
return join(dir, `${sanitizeSegment(prefix)}-${sanitizeSegment(basename(id) || prefix)}-${digest}.json`);
|
|
34
|
+
}
|
|
35
|
+
export class SessionBusState {
|
|
36
|
+
state = { ...DEFAULT_STATE };
|
|
37
|
+
statePath;
|
|
38
|
+
seenEnvelopeIds = new Set();
|
|
39
|
+
constructor(statePath) {
|
|
40
|
+
this.statePath = statePath;
|
|
41
|
+
if (!statePath)
|
|
42
|
+
return;
|
|
43
|
+
const parsed = readJsonFile(statePath, null);
|
|
44
|
+
if (parsed) {
|
|
45
|
+
this.state = {
|
|
46
|
+
nextSequence: parsed.nextSequence || 1,
|
|
47
|
+
highestReceivedSequence: parsed.highestReceivedSequence || 0,
|
|
48
|
+
outbound: Array.isArray(parsed.outbound) ? parsed.outbound : [],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
getHighestReceivedSequence() {
|
|
53
|
+
return this.state.highestReceivedSequence;
|
|
54
|
+
}
|
|
55
|
+
getResumeSequence() {
|
|
56
|
+
return this.state.highestReceivedSequence;
|
|
57
|
+
}
|
|
58
|
+
markConnected(frame) {
|
|
59
|
+
if (typeof frame.ackedThrough === 'number') {
|
|
60
|
+
this.ackThrough(frame.ackedThrough);
|
|
61
|
+
}
|
|
62
|
+
this.seenEnvelopeIds.clear();
|
|
63
|
+
if (typeof frame.sequence === 'number') {
|
|
64
|
+
this.state.highestReceivedSequence = frame.sequence;
|
|
65
|
+
}
|
|
66
|
+
this.persist();
|
|
67
|
+
}
|
|
68
|
+
createEnvelope(base) {
|
|
69
|
+
const envelope = {
|
|
70
|
+
envelopeId: randomUUID(),
|
|
71
|
+
protocolVersion: TRANSPORT_PROTOCOL_VERSION,
|
|
72
|
+
senderId: base.senderId,
|
|
73
|
+
instanceId: base.instanceId,
|
|
74
|
+
sessionId: base.sessionId,
|
|
75
|
+
conversationId: base.conversationId,
|
|
76
|
+
source: base.source,
|
|
77
|
+
eventType: base.eventType,
|
|
78
|
+
timestamp: Date.now(),
|
|
79
|
+
payload: base.payload,
|
|
80
|
+
sequence: this.state.nextSequence++,
|
|
81
|
+
ackedThrough: this.state.highestReceivedSequence,
|
|
82
|
+
...(base.resumeFromSequence !== undefined
|
|
83
|
+
? { resumeFromSequence: base.resumeFromSequence }
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
if (shouldPersistOutgoing(base.eventType)) {
|
|
87
|
+
this.state.outbound.push(envelope);
|
|
88
|
+
if (this.state.outbound.length > MAX_OUTBOUND_ENVELOPES) {
|
|
89
|
+
this.state.outbound = this.state.outbound.slice(-MAX_OUTBOUND_ENVELOPES);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.persist();
|
|
93
|
+
return envelope;
|
|
94
|
+
}
|
|
95
|
+
markReceived(frame) {
|
|
96
|
+
if (typeof frame.ackedThrough === 'number') {
|
|
97
|
+
this.ackThrough(frame.ackedThrough);
|
|
98
|
+
}
|
|
99
|
+
if (typeof frame.sequence !== 'number') {
|
|
100
|
+
this.persist();
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (this.seenEnvelopeIds.has(frame.envelopeId) || frame.sequence <= this.state.highestReceivedSequence) {
|
|
104
|
+
this.persist();
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
this.seenEnvelopeIds.add(frame.envelopeId);
|
|
108
|
+
this.state.highestReceivedSequence = Math.max(this.state.highestReceivedSequence, frame.sequence);
|
|
109
|
+
this.persist();
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
ackThrough(sequence) {
|
|
113
|
+
if (sequence <= 0)
|
|
114
|
+
return;
|
|
115
|
+
this.state.outbound = this.state.outbound.filter((frame) => (frame.sequence ?? 0) > sequence);
|
|
116
|
+
this.persist();
|
|
117
|
+
}
|
|
118
|
+
replayAfter(sequence) {
|
|
119
|
+
return this.state.outbound.filter((frame) => (frame.sequence ?? 0) > sequence);
|
|
120
|
+
}
|
|
121
|
+
persist() {
|
|
122
|
+
if (!this.statePath)
|
|
123
|
+
return;
|
|
124
|
+
writeJsonFileAtomic(this.statePath, this.state);
|
|
125
|
+
}
|
|
126
|
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AAC;IAED,WAAW,CAAC,QAAgB;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC;IACjF,CAAC;IAEO,OAAO;QACb,IAAI,CAAC,IAAI,CAAC,SAAS;YAAE,OAAO;QAC5B,mBAAmB,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IAClD,CAAC;CACF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { ToolResult, TransportEnvelope } from '@producible/cereworker-core';
|
|
2
|
+
export interface NodeStatus {
|
|
3
|
+
healthy: boolean;
|
|
4
|
+
uptime: number;
|
|
5
|
+
activeTools: number;
|
|
6
|
+
autoMode: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface SessionInvokeContext {
|
|
9
|
+
callId?: string;
|
|
10
|
+
requestedToolName?: string;
|
|
11
|
+
turnId?: string;
|
|
12
|
+
attempt?: number;
|
|
13
|
+
scopeKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface SessionBusEnvelopeMap {
|
|
16
|
+
'transport.connect': {
|
|
17
|
+
nodeId: string;
|
|
18
|
+
token: string;
|
|
19
|
+
capabilities: string[];
|
|
20
|
+
};
|
|
21
|
+
'transport.connected': {
|
|
22
|
+
gatewayId: string;
|
|
23
|
+
};
|
|
24
|
+
'transport.error': {
|
|
25
|
+
message: string;
|
|
26
|
+
};
|
|
27
|
+
'transport.ack': {
|
|
28
|
+
message?: string;
|
|
29
|
+
};
|
|
30
|
+
'transport.ping': {
|
|
31
|
+
sentAt?: number;
|
|
32
|
+
};
|
|
33
|
+
'transport.pong': {
|
|
34
|
+
sentAt?: number;
|
|
35
|
+
};
|
|
36
|
+
'transport.disconnect': {
|
|
37
|
+
reason: string;
|
|
38
|
+
};
|
|
39
|
+
'transport.emergency-stop': {
|
|
40
|
+
reason?: string;
|
|
41
|
+
};
|
|
42
|
+
'node.status.request': {
|
|
43
|
+
requestId: string;
|
|
44
|
+
};
|
|
45
|
+
'node.status': {
|
|
46
|
+
requestId?: string;
|
|
47
|
+
status: NodeStatus;
|
|
48
|
+
};
|
|
49
|
+
'invoke.request': {
|
|
50
|
+
invocationId: string;
|
|
51
|
+
tool: string;
|
|
52
|
+
args: Record<string, unknown>;
|
|
53
|
+
context?: SessionInvokeContext;
|
|
54
|
+
};
|
|
55
|
+
'invoke.result': {
|
|
56
|
+
invocationId: string;
|
|
57
|
+
result: ToolResult;
|
|
58
|
+
};
|
|
59
|
+
'tool.started': {
|
|
60
|
+
invocationId: string;
|
|
61
|
+
tool: string;
|
|
62
|
+
args: Record<string, unknown>;
|
|
63
|
+
context?: SessionInvokeContext;
|
|
64
|
+
nodeId?: string;
|
|
65
|
+
};
|
|
66
|
+
'tool.finished': {
|
|
67
|
+
invocationId: string;
|
|
68
|
+
tool: string;
|
|
69
|
+
args: Record<string, unknown>;
|
|
70
|
+
result: ToolResult;
|
|
71
|
+
context?: SessionInvokeContext;
|
|
72
|
+
nodeId?: string;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export type SessionBusEventType = keyof SessionBusEnvelopeMap;
|
|
76
|
+
export type GatewayFrame<K extends SessionBusEventType = SessionBusEventType> = TransportEnvelope<SessionBusEnvelopeMap[K]> & {
|
|
77
|
+
eventType: K;
|
|
78
|
+
};
|
|
79
|
+
export type AnyGatewayFrame = {
|
|
80
|
+
[K in SessionBusEventType]: GatewayFrame<K>;
|
|
81
|
+
}[SessionBusEventType];
|
|
82
|
+
export interface NodeInfo {
|
|
83
|
+
nodeId: string;
|
|
84
|
+
capabilities: string[];
|
|
85
|
+
connectedAt: number;
|
|
86
|
+
lastPingAt: number;
|
|
87
|
+
status: 'connected' | 'stale' | 'disconnected';
|
|
88
|
+
}
|
|
89
|
+
export interface GatewayServerConfig {
|
|
90
|
+
port: number;
|
|
91
|
+
token?: string;
|
|
92
|
+
invokeTimeoutMs: number;
|
|
93
|
+
pingIntervalMs: number;
|
|
94
|
+
stateDir?: string;
|
|
95
|
+
instanceId?: string;
|
|
96
|
+
}
|
|
97
|
+
export interface GatewayClientConfig {
|
|
98
|
+
gatewayUrl: string;
|
|
99
|
+
nodeId: string;
|
|
100
|
+
token: string;
|
|
101
|
+
capabilities: string[];
|
|
102
|
+
stateDir?: string;
|
|
103
|
+
instanceId?: string;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEjF,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,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,qBAAqB;IACpC,mBAAmB,EAAE;QACnB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,YAAY,EAAE,MAAM,EAAE,CAAC;KACxB,CAAC;IACF,qBAAqB,EAAE;QACrB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,iBAAiB,EAAE;QACjB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,eAAe,EAAE;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;I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AE9D,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,mBAAmB,GAAG,mBAAmB,IAAI,iBAAiB,CAC/F,qBAAqB,CAAC,CAAC,CAAC,CACzB,GAAG;IACF,SAAS,EAAE,CAAC,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;KAC3B,CAAC,IAAI,mBAAmB,GAAG,YAAY,CAAC,CAAC,CAAC;CAC5C,CAAC,mBAAmB,CAAC,CAAC;AAEvB,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,WAAW,GAAG,OAAO,GAAG,cAAc,CAAC;CAChD;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB"}
|
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": "@producible/cereworker-gateway",
|
|
3
|
+
"version": "26.520.1",
|
|
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
|
+
"@producible/cereworker-core": "26.520.1"
|
|
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
|
+
}
|