@kadi.build/core 0.0.1-alpha.1 → 0.0.1-alpha.10
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 +1387 -214
- package/dist/abilities/AbilityCache.d.ts +242 -0
- package/dist/abilities/AbilityCache.d.ts.map +1 -0
- package/dist/abilities/AbilityCache.js +285 -0
- package/dist/abilities/AbilityCache.js.map +1 -0
- package/dist/abilities/AbilityContext.d.ts +215 -0
- package/dist/abilities/AbilityContext.d.ts.map +1 -0
- package/dist/abilities/AbilityContext.js +36 -0
- package/dist/abilities/AbilityContext.js.map +1 -0
- package/dist/abilities/AbilityLoader.d.ts +177 -0
- package/dist/abilities/AbilityLoader.d.ts.map +1 -0
- package/dist/abilities/AbilityLoader.js +277 -0
- package/dist/abilities/AbilityLoader.js.map +1 -0
- package/dist/abilities/AbilityProxy.d.ts +463 -0
- package/dist/abilities/AbilityProxy.d.ts.map +1 -0
- package/dist/abilities/AbilityProxy.js +511 -0
- package/dist/abilities/AbilityProxy.js.map +1 -0
- package/dist/abilities/AbilityValidator.d.ts +172 -0
- package/dist/abilities/AbilityValidator.d.ts.map +1 -0
- package/dist/abilities/AbilityValidator.js +253 -0
- package/dist/abilities/AbilityValidator.js.map +1 -0
- package/dist/abilities/index.d.ts +26 -0
- package/dist/abilities/index.d.ts.map +1 -0
- package/dist/abilities/index.js +23 -0
- package/dist/abilities/index.js.map +1 -0
- package/dist/abilities/types.d.ts +156 -0
- package/dist/abilities/types.d.ts.map +1 -0
- package/dist/abilities/types.js +10 -0
- package/dist/abilities/types.js.map +1 -0
- package/dist/api/index.d.ts +92 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +124 -0
- package/dist/api/index.js.map +1 -0
- package/dist/broker/BrokerConnection.d.ts +253 -0
- package/dist/broker/BrokerConnection.d.ts.map +1 -0
- package/dist/broker/BrokerConnection.js +434 -0
- package/dist/broker/BrokerConnection.js.map +1 -0
- package/dist/broker/BrokerConnectionManager.d.ts +216 -0
- package/dist/broker/BrokerConnectionManager.d.ts.map +1 -0
- package/dist/broker/BrokerConnectionManager.js +305 -0
- package/dist/broker/BrokerConnectionManager.js.map +1 -0
- package/dist/broker/BrokerProtocol.d.ts +280 -0
- package/dist/broker/BrokerProtocol.d.ts.map +1 -0
- package/dist/broker/BrokerProtocol.js +466 -0
- package/dist/broker/BrokerProtocol.js.map +1 -0
- package/dist/broker/index.d.ts +9 -0
- package/dist/broker/index.d.ts.map +1 -0
- package/dist/broker/index.js +9 -0
- package/dist/broker/index.js.map +1 -0
- package/dist/client/KadiClient.d.ts +270 -0
- package/dist/client/KadiClient.d.ts.map +1 -0
- package/dist/client/KadiClient.js +492 -0
- package/dist/client/KadiClient.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -0
- package/dist/config/ConfigLoader.d.ts +138 -0
- package/dist/config/ConfigLoader.d.ts.map +1 -0
- package/dist/config/ConfigLoader.js +226 -0
- package/dist/config/ConfigLoader.js.map +1 -0
- package/dist/config/ConfigResolver.d.ts +135 -0
- package/dist/config/ConfigResolver.d.ts.map +1 -0
- package/dist/config/ConfigResolver.js +282 -0
- package/dist/config/ConfigResolver.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +8 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/events/EventHub.d.ts +172 -0
- package/dist/events/EventHub.d.ts.map +1 -0
- package/dist/events/EventHub.js +333 -0
- package/dist/events/EventHub.js.map +1 -0
- package/dist/events/index.d.ts +7 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +7 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +63 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/index.d.ts +33 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +33 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/schemas/index.d.ts +19 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +25 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/kadi-extensions.d.ts +231 -0
- package/dist/schemas/kadi-extensions.d.ts.map +1 -0
- package/dist/schemas/kadi-extensions.js +14 -0
- package/dist/schemas/kadi-extensions.js.map +1 -0
- package/dist/schemas/mcp/schema.d.ts +1399 -0
- package/dist/schemas/mcp/schema.d.ts.map +1 -0
- package/dist/schemas/mcp/schema.js +53 -0
- package/dist/schemas/mcp/schema.js.map +1 -0
- package/dist/schemas/mcp/version.d.ts +37 -0
- package/dist/schemas/mcp/version.d.ts.map +1 -0
- package/dist/schemas/mcp/version.js +39 -0
- package/dist/schemas/mcp/version.js.map +1 -0
- package/dist/schemas/schema-builders.d.ts +178 -0
- package/dist/schemas/schema-builders.d.ts.map +1 -0
- package/dist/schemas/schema-builders.js +258 -0
- package/dist/schemas/schema-builders.js.map +1 -0
- package/dist/tools/ToolRegistry.d.ts +256 -0
- package/dist/tools/ToolRegistry.d.ts.map +1 -0
- package/dist/tools/ToolRegistry.js +340 -0
- package/dist/tools/ToolRegistry.js.map +1 -0
- package/dist/tools/index.d.ts +7 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/transports/BrokerTransport.d.ts +151 -0
- package/dist/transports/BrokerTransport.d.ts.map +1 -0
- package/dist/transports/BrokerTransport.js +261 -0
- package/dist/transports/BrokerTransport.js.map +1 -0
- package/dist/transports/NativeTransport.d.ts +149 -0
- package/dist/transports/NativeTransport.d.ts.map +1 -0
- package/dist/transports/NativeTransport.js +302 -0
- package/dist/transports/NativeTransport.js.map +1 -0
- package/dist/transports/StdioTransport.d.ts +172 -0
- package/dist/transports/StdioTransport.d.ts.map +1 -0
- package/dist/transports/StdioTransport.js +410 -0
- package/dist/transports/StdioTransport.js.map +1 -0
- package/dist/transports/index.d.ts +10 -0
- package/dist/transports/index.d.ts.map +1 -0
- package/dist/transports/index.js +9 -0
- package/dist/transports/index.js.map +1 -0
- package/dist/types/broker.d.ts +301 -0
- package/dist/types/broker.d.ts.map +1 -0
- package/dist/types/broker.js +46 -0
- package/dist/types/broker.js.map +1 -0
- package/dist/types/config.d.ts +325 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +17 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/errors.d.ts +178 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +165 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/events.d.ts +210 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +8 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +32 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/protocol.d.ts +48 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +11 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/types/tools.d.ts +67 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +16 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/types/transport.d.ts +250 -0
- package/dist/types/transport.d.ts.map +1 -0
- package/dist/types/transport.js +18 -0
- package/dist/types/transport.js.map +1 -0
- package/dist/validation/SchemaValidator.d.ts +208 -0
- package/dist/validation/SchemaValidator.d.ts.map +1 -0
- package/dist/validation/SchemaValidator.js +411 -0
- package/dist/validation/SchemaValidator.js.map +1 -0
- package/dist/validation/index.d.ts +11 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +10 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +69 -5
- package/agent.json +0 -18
- package/broker.js +0 -214
- package/index.js +0 -370
- package/ipc.js +0 -220
- package/ipcInterfaces/pythonAbilityIPC.py +0 -177
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Broker Connection
|
|
3
|
+
*
|
|
4
|
+
* Wraps a single WebSocket connection to a KADI broker.
|
|
5
|
+
* Handles connection lifecycle, message sending/receiving, and state management.
|
|
6
|
+
*
|
|
7
|
+
* @module broker/BrokerConnection
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import WebSocket from 'ws';
|
|
11
|
+
import { ConnectionState } from '../types/index.js';
|
|
12
|
+
import { KadiError, ErrorCode } from '../types/index.js';
|
|
13
|
+
/**
|
|
14
|
+
* Broker Connection
|
|
15
|
+
*
|
|
16
|
+
* Manages a single WebSocket connection to a broker.
|
|
17
|
+
* Handles low-level connection lifecycle and message framing.
|
|
18
|
+
*
|
|
19
|
+
* Does NOT handle:
|
|
20
|
+
* - Protocol logic (handshake, auth) - that's BrokerProtocol's job
|
|
21
|
+
* - Multiple connections - that's BrokerConnectionManager's job
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const connection = new BrokerConnection({
|
|
26
|
+
* url: 'ws://localhost:8080',
|
|
27
|
+
* name: 'local',
|
|
28
|
+
* connectionTimeout: 10000
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* connection.on('connected', () => {
|
|
32
|
+
* console.log('Connected to broker');
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* connection.on('message', (message) => {
|
|
36
|
+
* console.log('Received:', message);
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* await connection.connect();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export class BrokerConnection extends EventEmitter {
|
|
43
|
+
/**
|
|
44
|
+
* Broker configuration
|
|
45
|
+
*/
|
|
46
|
+
config;
|
|
47
|
+
/**
|
|
48
|
+
* WebSocket instance
|
|
49
|
+
*/
|
|
50
|
+
ws = null;
|
|
51
|
+
/**
|
|
52
|
+
* Current connection state
|
|
53
|
+
*/
|
|
54
|
+
_state = ConnectionState.DISCONNECTED;
|
|
55
|
+
/**
|
|
56
|
+
* Assigned agent ID (set after handshake)
|
|
57
|
+
*/
|
|
58
|
+
_agentId = null;
|
|
59
|
+
/**
|
|
60
|
+
* Heartbeat timer
|
|
61
|
+
*/
|
|
62
|
+
heartbeatTimer = null;
|
|
63
|
+
/**
|
|
64
|
+
* Connection timestamp
|
|
65
|
+
*/
|
|
66
|
+
_connectedAt = null;
|
|
67
|
+
/**
|
|
68
|
+
* Last heartbeat timestamp
|
|
69
|
+
*/
|
|
70
|
+
_lastHeartbeat = null;
|
|
71
|
+
/**
|
|
72
|
+
* Pending requests (for matching responses)
|
|
73
|
+
*/
|
|
74
|
+
pendingRequests = new Map();
|
|
75
|
+
/**
|
|
76
|
+
* Request ID counter
|
|
77
|
+
*/
|
|
78
|
+
requestIdCounter = 0;
|
|
79
|
+
/**
|
|
80
|
+
* Create a new BrokerConnection
|
|
81
|
+
*
|
|
82
|
+
* @param config - Broker configuration
|
|
83
|
+
*/
|
|
84
|
+
constructor(config) {
|
|
85
|
+
super();
|
|
86
|
+
this.config = {
|
|
87
|
+
...config,
|
|
88
|
+
connectionTimeout: config.connectionTimeout ?? 10000,
|
|
89
|
+
requestTimeout: config.requestTimeout ?? 30000,
|
|
90
|
+
heartbeatInterval: config.heartbeatInterval ?? 30000,
|
|
91
|
+
autoReconnect: config.autoReconnect ?? true,
|
|
92
|
+
maxReconnectAttempts: config.maxReconnectAttempts ?? 5
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Connect to the broker
|
|
97
|
+
*
|
|
98
|
+
* @throws {KadiError} If connection fails
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* await connection.connect();
|
|
103
|
+
* console.log('Connected!');
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
async connect() {
|
|
107
|
+
if (this._state !== ConnectionState.DISCONNECTED) {
|
|
108
|
+
throw new KadiError('Connection already in progress or established', ErrorCode.CONNECTION_FAILED, 400, { currentState: this._state, broker: this.config.name });
|
|
109
|
+
}
|
|
110
|
+
this.setState(ConnectionState.CONNECTING);
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
const timeout = setTimeout(() => {
|
|
113
|
+
this.cleanup();
|
|
114
|
+
reject(new KadiError(`Connection timeout after ${this.config.connectionTimeout}ms`, ErrorCode.CONNECTION_TIMEOUT, 408, { broker: this.config.name, url: this.config.url }));
|
|
115
|
+
}, this.config.connectionTimeout);
|
|
116
|
+
try {
|
|
117
|
+
this.ws = new WebSocket(this.config.url);
|
|
118
|
+
this.ws.on('open', () => {
|
|
119
|
+
clearTimeout(timeout);
|
|
120
|
+
this._connectedAt = Date.now();
|
|
121
|
+
this.setState(ConnectionState.CONNECTED);
|
|
122
|
+
this.setupHeartbeat();
|
|
123
|
+
this.emit('connected');
|
|
124
|
+
resolve();
|
|
125
|
+
});
|
|
126
|
+
this.ws.on('error', (error) => {
|
|
127
|
+
clearTimeout(timeout);
|
|
128
|
+
this.emit('error', error);
|
|
129
|
+
reject(new KadiError(`WebSocket error: ${error.message}`, ErrorCode.CONNECTION_FAILED, 500, { broker: this.config.name, url: this.config.url, originalError: error.message }));
|
|
130
|
+
});
|
|
131
|
+
this.ws.on('close', (code, reason) => {
|
|
132
|
+
clearTimeout(timeout);
|
|
133
|
+
this.handleClose(code, reason.toString());
|
|
134
|
+
});
|
|
135
|
+
this.ws.on('message', (data) => {
|
|
136
|
+
this.handleMessage(data);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
clearTimeout(timeout);
|
|
141
|
+
this.cleanup();
|
|
142
|
+
reject(KadiError.from(error, ErrorCode.CONNECTION_FAILED));
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Disconnect from the broker
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* await connection.disconnect();
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
async disconnect() {
|
|
155
|
+
this.cleanup();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Send a JSON-RPC request and wait for response
|
|
159
|
+
*
|
|
160
|
+
* @template T - Response result type
|
|
161
|
+
* @param request - JSON-RPC request
|
|
162
|
+
* @returns Promise resolving to the response result
|
|
163
|
+
*
|
|
164
|
+
* @throws {KadiError} If request fails or times out
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const result = await connection.sendRequest({
|
|
169
|
+
* jsonrpc: '2.0',
|
|
170
|
+
* method: 'kadi.handshake',
|
|
171
|
+
* params: { name: 'my-agent' },
|
|
172
|
+
* id: 1
|
|
173
|
+
* });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
async sendRequest(request) {
|
|
177
|
+
if (!this.isConnected) {
|
|
178
|
+
throw new KadiError('Not connected to broker', ErrorCode.BROKER_NOT_CONNECTED, 503, { broker: this.config.name });
|
|
179
|
+
}
|
|
180
|
+
// Generate ID if not provided
|
|
181
|
+
if (request.id === undefined) {
|
|
182
|
+
request.id = ++this.requestIdCounter;
|
|
183
|
+
}
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
const timeout = setTimeout(() => {
|
|
186
|
+
this.pendingRequests.delete(request.id);
|
|
187
|
+
reject(new KadiError(`Request timeout after ${this.config.requestTimeout}ms`, ErrorCode.BROKER_TIMEOUT, 408, { broker: this.config.name, method: request.method, requestId: request.id }));
|
|
188
|
+
}, this.config.requestTimeout);
|
|
189
|
+
this.pendingRequests.set(request.id, {
|
|
190
|
+
id: request.id,
|
|
191
|
+
resolve: resolve,
|
|
192
|
+
reject,
|
|
193
|
+
timeout,
|
|
194
|
+
timestamp: Date.now()
|
|
195
|
+
});
|
|
196
|
+
this.send(request);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Send a JSON-RPC notification (no response expected)
|
|
201
|
+
*
|
|
202
|
+
* @param notification - JSON-RPC notification
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* connection.sendNotification({
|
|
207
|
+
* jsonrpc: '2.0',
|
|
208
|
+
* method: 'kadi.heartbeat',
|
|
209
|
+
* params: { timestamp: Date.now() }
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
sendNotification(notification) {
|
|
214
|
+
if (!this.isConnected) {
|
|
215
|
+
throw new KadiError('Not connected to broker', ErrorCode.BROKER_NOT_CONNECTED, 503, { broker: this.config.name });
|
|
216
|
+
}
|
|
217
|
+
this.send(notification);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Send a tool result back to broker as JSON-RPC response
|
|
221
|
+
*
|
|
222
|
+
* Used when this client acts as a tool provider and needs to respond to invocations.
|
|
223
|
+
* The requestId must match the incoming request's id field for proper routing.
|
|
224
|
+
*
|
|
225
|
+
* @param requestId - Request ID to respond to (CRITICAL: must match incoming request id)
|
|
226
|
+
* @param result - Result data to send back
|
|
227
|
+
*/
|
|
228
|
+
sendResponse(requestId, result) {
|
|
229
|
+
if (!requestId) {
|
|
230
|
+
// No ID = broker can't route response, so silently drop
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!this.isConnected) {
|
|
234
|
+
throw new KadiError('Not connected to broker', ErrorCode.BROKER_NOT_CONNECTED, 503, { broker: this.config.name });
|
|
235
|
+
}
|
|
236
|
+
// Send proper JSON-RPC response with matching id
|
|
237
|
+
const response = {
|
|
238
|
+
jsonrpc: '2.0',
|
|
239
|
+
result,
|
|
240
|
+
id: requestId
|
|
241
|
+
};
|
|
242
|
+
this.send(response);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Send a tool error back to broker as JSON-RPC error response
|
|
246
|
+
*
|
|
247
|
+
* Used when tool execution fails and we need to report the error back.
|
|
248
|
+
* The requestId must match the incoming request's id field for proper routing.
|
|
249
|
+
*
|
|
250
|
+
* @param requestId - Request ID to respond to (CRITICAL: must match incoming request id)
|
|
251
|
+
* @param code - Error code (e.g., ErrorCode.TOOL_NOT_FOUND)
|
|
252
|
+
* @param message - Human-readable error message
|
|
253
|
+
* @param data - Optional additional error context
|
|
254
|
+
*/
|
|
255
|
+
sendError(requestId, code, message, data) {
|
|
256
|
+
if (!requestId) {
|
|
257
|
+
// No ID = broker can't route error, so silently drop
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (!this.isConnected) {
|
|
261
|
+
throw new KadiError('Not connected to broker', ErrorCode.BROKER_NOT_CONNECTED, 503, { broker: this.config.name });
|
|
262
|
+
}
|
|
263
|
+
// Send proper JSON-RPC error response with matching id
|
|
264
|
+
const response = {
|
|
265
|
+
jsonrpc: '2.0',
|
|
266
|
+
error: {
|
|
267
|
+
code: -32000, // Application error code range
|
|
268
|
+
message: `${code}: ${message}`,
|
|
269
|
+
data
|
|
270
|
+
},
|
|
271
|
+
id: requestId
|
|
272
|
+
};
|
|
273
|
+
this.send(response);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Send raw data to broker
|
|
277
|
+
*
|
|
278
|
+
* @param data - Data to send (request, response, or notification)
|
|
279
|
+
*/
|
|
280
|
+
send(data) {
|
|
281
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
282
|
+
throw new KadiError('WebSocket not open', ErrorCode.BROKER_NOT_CONNECTED, 503, { broker: this.config.name });
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const message = JSON.stringify(data);
|
|
286
|
+
this.ws.send(message);
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
throw new KadiError(`Failed to send message: ${error instanceof Error ? error.message : String(error)}`, ErrorCode.TRANSPORT_SEND_FAILED, 500, { broker: this.config.name });
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Handle incoming WebSocket message
|
|
294
|
+
*
|
|
295
|
+
* @param data - Raw message data
|
|
296
|
+
*/
|
|
297
|
+
handleMessage(data) {
|
|
298
|
+
try {
|
|
299
|
+
const message = JSON.parse(data.toString());
|
|
300
|
+
// Check if this is a response to a pending request
|
|
301
|
+
if ('id' in message && message.id !== undefined) {
|
|
302
|
+
const pending = this.pendingRequests.get(message.id);
|
|
303
|
+
if (pending) {
|
|
304
|
+
clearTimeout(pending.timeout);
|
|
305
|
+
this.pendingRequests.delete(message.id);
|
|
306
|
+
const response = message;
|
|
307
|
+
if (response.error) {
|
|
308
|
+
pending.reject(new KadiError(response.error.message, ErrorCode.BROKER_RESPONSE_ERROR, 500, { broker: this.config.name, errorCode: response.error.code, errorData: response.error.data }));
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
pending.resolve(response.result);
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Emit as general message (notification or unsolicited response)
|
|
317
|
+
this.emit('message', message);
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
this.emit('error', new KadiError(`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`, ErrorCode.INVALID_MESSAGE_FORMAT, 400, { broker: this.config.name }));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Handle WebSocket close event
|
|
325
|
+
*
|
|
326
|
+
* @param code - Close code
|
|
327
|
+
* @param reason - Close reason
|
|
328
|
+
*/
|
|
329
|
+
handleClose(code, reason) {
|
|
330
|
+
this.setState(ConnectionState.DISCONNECTED);
|
|
331
|
+
this.cleanup();
|
|
332
|
+
this.emit('disconnected', code, reason);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Setup heartbeat timer
|
|
336
|
+
*/
|
|
337
|
+
setupHeartbeat() {
|
|
338
|
+
if (this.heartbeatTimer) {
|
|
339
|
+
clearInterval(this.heartbeatTimer);
|
|
340
|
+
}
|
|
341
|
+
if (this.config.heartbeatInterval && this.config.heartbeatInterval > 0) {
|
|
342
|
+
this.heartbeatTimer = setInterval(() => {
|
|
343
|
+
this._lastHeartbeat = Date.now();
|
|
344
|
+
// Heartbeat logic handled by BrokerProtocol
|
|
345
|
+
}, this.config.heartbeatInterval);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Cleanup connection resources
|
|
350
|
+
*/
|
|
351
|
+
cleanup() {
|
|
352
|
+
if (this.heartbeatTimer) {
|
|
353
|
+
clearInterval(this.heartbeatTimer);
|
|
354
|
+
this.heartbeatTimer = null;
|
|
355
|
+
}
|
|
356
|
+
// Reject all pending requests
|
|
357
|
+
for (const [id, pending] of this.pendingRequests) {
|
|
358
|
+
clearTimeout(pending.timeout);
|
|
359
|
+
pending.reject(new KadiError('Connection closed', ErrorCode.CONNECTION_CLOSED, 503, { broker: this.config.name, requestId: id }));
|
|
360
|
+
}
|
|
361
|
+
this.pendingRequests.clear();
|
|
362
|
+
if (this.ws) {
|
|
363
|
+
this.ws.removeAllListeners();
|
|
364
|
+
if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
|
|
365
|
+
this.ws.close();
|
|
366
|
+
}
|
|
367
|
+
this.ws = null;
|
|
368
|
+
}
|
|
369
|
+
this._connectedAt = null;
|
|
370
|
+
this._lastHeartbeat = null;
|
|
371
|
+
this.setState(ConnectionState.DISCONNECTED);
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Set connection state and emit event
|
|
375
|
+
*
|
|
376
|
+
* @param newState - New state
|
|
377
|
+
*/
|
|
378
|
+
setState(newState) {
|
|
379
|
+
const oldState = this._state;
|
|
380
|
+
if (oldState !== newState) {
|
|
381
|
+
this._state = newState;
|
|
382
|
+
this.emit('stateChange', oldState, newState);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Get broker name
|
|
387
|
+
*/
|
|
388
|
+
get name() {
|
|
389
|
+
return this.config.name;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Get broker URL
|
|
393
|
+
*/
|
|
394
|
+
get url() {
|
|
395
|
+
return this.config.url;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Get current connection state
|
|
399
|
+
*/
|
|
400
|
+
get state() {
|
|
401
|
+
return this._state;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Check if connected
|
|
405
|
+
*/
|
|
406
|
+
get isConnected() {
|
|
407
|
+
return this._state === ConnectionState.CONNECTED || this._state === ConnectionState.AUTHENTICATED;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Get assigned agent ID
|
|
411
|
+
*/
|
|
412
|
+
get agentId() {
|
|
413
|
+
return this._agentId;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Set agent ID (called after handshake)
|
|
417
|
+
*/
|
|
418
|
+
set agentId(id) {
|
|
419
|
+
this._agentId = id;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Get connection timestamp
|
|
423
|
+
*/
|
|
424
|
+
get connectedAt() {
|
|
425
|
+
return this._connectedAt;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Get last heartbeat timestamp
|
|
429
|
+
*/
|
|
430
|
+
get lastHeartbeat() {
|
|
431
|
+
return this._lastHeartbeat;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
//# sourceMappingURL=BrokerConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerConnection.js","sourceRoot":"","sources":["../../src/broker/BrokerConnection.ts"],"names":[],"mappings":"AAAA;;;;;;;G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IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Broker Connection Manager
|
|
3
|
+
*
|
|
4
|
+
* Manages multiple broker connections.
|
|
5
|
+
* Handles connection lifecycle, failover, and routing.
|
|
6
|
+
*
|
|
7
|
+
* @module broker/BrokerConnectionManager
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import type { BrokerConfig } from '../types/index.js';
|
|
11
|
+
import { BrokerConnection } from './BrokerConnection.js';
|
|
12
|
+
/**
|
|
13
|
+
* Broker Connection Manager
|
|
14
|
+
*
|
|
15
|
+
* Single responsibility: Manage multiple broker connections.
|
|
16
|
+
* Does NOT handle:
|
|
17
|
+
* - Protocol logic (that's BrokerProtocol's job)
|
|
18
|
+
* - Tool registration/invocation (that's ToolRegistry/Client's job)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const manager = new BrokerConnectionManager();
|
|
23
|
+
*
|
|
24
|
+
* // Add brokers
|
|
25
|
+
* await manager.connect({
|
|
26
|
+
* url: 'ws://localhost:8080',
|
|
27
|
+
* name: 'local'
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* await manager.connect({
|
|
31
|
+
* url: 'wss://prod.example.com',
|
|
32
|
+
* name: 'production'
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Get a connection
|
|
36
|
+
* const connection = manager.getConnection('local');
|
|
37
|
+
*
|
|
38
|
+
* // Get default connection
|
|
39
|
+
* const defaultConn = manager.getDefaultConnection();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class BrokerConnectionManager extends EventEmitter {
|
|
43
|
+
/**
|
|
44
|
+
* Active broker connections (name -> connection)
|
|
45
|
+
*/
|
|
46
|
+
private connections;
|
|
47
|
+
/**
|
|
48
|
+
* Default broker name
|
|
49
|
+
*/
|
|
50
|
+
private defaultBrokerName;
|
|
51
|
+
/**
|
|
52
|
+
* Connect to a broker
|
|
53
|
+
*
|
|
54
|
+
* @param config - Broker configuration
|
|
55
|
+
* @param setAsDefault - Set this as the default broker
|
|
56
|
+
* @returns The established connection
|
|
57
|
+
*
|
|
58
|
+
* @throws {KadiError} If connection fails
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const connection = await manager.connect({
|
|
63
|
+
* url: 'ws://localhost:8080',
|
|
64
|
+
* name: 'local'
|
|
65
|
+
* }, true);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
connect(config: BrokerConfig, setAsDefault?: boolean): Promise<BrokerConnection>;
|
|
69
|
+
/**
|
|
70
|
+
* Connect to multiple brokers
|
|
71
|
+
*
|
|
72
|
+
* @param configs - Array of broker configurations
|
|
73
|
+
* @param defaultBrokerName - Name of the default broker
|
|
74
|
+
* @returns Array of established connections
|
|
75
|
+
*
|
|
76
|
+
* @throws {KadiError} If any connection fails
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* await manager.connectMultiple([
|
|
81
|
+
* { url: 'ws://localhost:8080', name: 'local' },
|
|
82
|
+
* { url: 'wss://prod.example.com', name: 'prod' }
|
|
83
|
+
* ], 'local');
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
connectMultiple(configs: BrokerConfig[], defaultBrokerName?: string): Promise<BrokerConnection[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Disconnect from a broker
|
|
89
|
+
*
|
|
90
|
+
* @param brokerName - Name of broker to disconnect from
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* await manager.disconnect('local');
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
disconnect(brokerName: string): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Disconnect from all brokers
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* await manager.disconnectAll();
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
disconnectAll(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Get a broker connection by name
|
|
109
|
+
*
|
|
110
|
+
* @param brokerName - Name of the broker
|
|
111
|
+
* @returns The connection or undefined if not found
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const conn = manager.getConnection('local');
|
|
116
|
+
* if (conn) {
|
|
117
|
+
* await conn.sendRequest(...);
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
getConnection(brokerName: string): BrokerConnection | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Get the default broker connection
|
|
124
|
+
*
|
|
125
|
+
* @returns The default connection
|
|
126
|
+
* @throws {KadiError} If no default broker is set
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const conn = manager.getDefaultConnection();
|
|
131
|
+
* await conn.sendRequest(...);
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
getDefaultConnection(): BrokerConnection;
|
|
135
|
+
/**
|
|
136
|
+
* Get all broker connections
|
|
137
|
+
*
|
|
138
|
+
* @returns Array of all connections
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const connections = manager.getAllConnections();
|
|
143
|
+
* console.log(`Connected to ${connections.length} brokers`);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
getAllConnections(): BrokerConnection[];
|
|
147
|
+
/**
|
|
148
|
+
* Get all connected broker connections
|
|
149
|
+
*
|
|
150
|
+
* @returns Array of connected connections
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const connected = manager.getConnectedBrokers();
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
getConnectedBrokers(): BrokerConnection[];
|
|
158
|
+
/**
|
|
159
|
+
* Check if connected to a specific broker
|
|
160
|
+
*
|
|
161
|
+
* @param brokerName - Broker name
|
|
162
|
+
* @returns true if connected
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* if (manager.isConnected('local')) {
|
|
167
|
+
* // Use local broker
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
isConnected(brokerName: string): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Check if connected to any broker
|
|
174
|
+
*
|
|
175
|
+
* @returns true if connected to at least one broker
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* if (!manager.hasConnections()) {
|
|
180
|
+
* throw new Error('Not connected to any broker');
|
|
181
|
+
* }
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
hasConnections(): boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Set the default broker
|
|
187
|
+
*
|
|
188
|
+
* @param brokerName - Name of broker to set as default
|
|
189
|
+
* @throws {KadiError} If broker doesn't exist
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* manager.setDefaultBroker('production');
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
setDefaultBroker(brokerName: string): void;
|
|
197
|
+
/**
|
|
198
|
+
* Get default broker name
|
|
199
|
+
*
|
|
200
|
+
* @returns Default broker name or null if not set
|
|
201
|
+
*/
|
|
202
|
+
getDefaultBrokerName(): string | null;
|
|
203
|
+
/**
|
|
204
|
+
* Get number of active connections
|
|
205
|
+
*
|
|
206
|
+
* @returns Connection count
|
|
207
|
+
*/
|
|
208
|
+
get connectionCount(): number;
|
|
209
|
+
/**
|
|
210
|
+
* Get broker names
|
|
211
|
+
*
|
|
212
|
+
* @returns Array of broker names
|
|
213
|
+
*/
|
|
214
|
+
getBrokerNames(): string[];
|
|
215
|
+
}
|
|
216
|
+
//# sourceMappingURL=BrokerConnectionManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerConnectionManager.d.ts","sourceRoot":"","sources":["../../src/broker/BrokerConnectionManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,uBAAwB,SAAQ,YAAY;IACvD;;OAEG;IACH,OAAO,CAAC,WAAW,CAAuC;IAE1D;;OAEG;IACH,OAAO,CAAC,iBAAiB,CAAuB;IAEhD;;;;;;;;;;;;;;;;OAgBG;IACG,OAAO,CAAC,MAAM,EAAE,YAAY,EAAE,YAAY,UAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA6CpF;;;;;;;;;;;;;;;;OAgBG;IACG,eAAe,CACnB,OAAO,EAAE,YAAY,EAAE,EACvB,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,gBAAgB,EAAE,CAAC;IAY9B;;;;;;;;;OASG;IACG,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBnD;;;;;;;OAOG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAQpC;;;;;;;;;;;;;OAaG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAI/D;;;;;;;;;;;OAWG;IACH,o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