@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.
Files changed (179) hide show
  1. package/README.md +1387 -214
  2. package/dist/abilities/AbilityCache.d.ts +242 -0
  3. package/dist/abilities/AbilityCache.d.ts.map +1 -0
  4. package/dist/abilities/AbilityCache.js +285 -0
  5. package/dist/abilities/AbilityCache.js.map +1 -0
  6. package/dist/abilities/AbilityContext.d.ts +215 -0
  7. package/dist/abilities/AbilityContext.d.ts.map +1 -0
  8. package/dist/abilities/AbilityContext.js +36 -0
  9. package/dist/abilities/AbilityContext.js.map +1 -0
  10. package/dist/abilities/AbilityLoader.d.ts +177 -0
  11. package/dist/abilities/AbilityLoader.d.ts.map +1 -0
  12. package/dist/abilities/AbilityLoader.js +277 -0
  13. package/dist/abilities/AbilityLoader.js.map +1 -0
  14. package/dist/abilities/AbilityProxy.d.ts +463 -0
  15. package/dist/abilities/AbilityProxy.d.ts.map +1 -0
  16. package/dist/abilities/AbilityProxy.js +511 -0
  17. package/dist/abilities/AbilityProxy.js.map +1 -0
  18. package/dist/abilities/AbilityValidator.d.ts +172 -0
  19. package/dist/abilities/AbilityValidator.d.ts.map +1 -0
  20. package/dist/abilities/AbilityValidator.js +253 -0
  21. package/dist/abilities/AbilityValidator.js.map +1 -0
  22. package/dist/abilities/index.d.ts +26 -0
  23. package/dist/abilities/index.d.ts.map +1 -0
  24. package/dist/abilities/index.js +23 -0
  25. package/dist/abilities/index.js.map +1 -0
  26. package/dist/abilities/types.d.ts +156 -0
  27. package/dist/abilities/types.d.ts.map +1 -0
  28. package/dist/abilities/types.js +10 -0
  29. package/dist/abilities/types.js.map +1 -0
  30. package/dist/api/index.d.ts +92 -0
  31. package/dist/api/index.d.ts.map +1 -0
  32. package/dist/api/index.js +124 -0
  33. package/dist/api/index.js.map +1 -0
  34. package/dist/broker/BrokerConnection.d.ts +253 -0
  35. package/dist/broker/BrokerConnection.d.ts.map +1 -0
  36. package/dist/broker/BrokerConnection.js +434 -0
  37. package/dist/broker/BrokerConnection.js.map +1 -0
  38. package/dist/broker/BrokerConnectionManager.d.ts +216 -0
  39. package/dist/broker/BrokerConnectionManager.d.ts.map +1 -0
  40. package/dist/broker/BrokerConnectionManager.js +305 -0
  41. package/dist/broker/BrokerConnectionManager.js.map +1 -0
  42. package/dist/broker/BrokerProtocol.d.ts +280 -0
  43. package/dist/broker/BrokerProtocol.d.ts.map +1 -0
  44. package/dist/broker/BrokerProtocol.js +466 -0
  45. package/dist/broker/BrokerProtocol.js.map +1 -0
  46. package/dist/broker/index.d.ts +9 -0
  47. package/dist/broker/index.d.ts.map +1 -0
  48. package/dist/broker/index.js +9 -0
  49. package/dist/broker/index.js.map +1 -0
  50. package/dist/client/KadiClient.d.ts +270 -0
  51. package/dist/client/KadiClient.d.ts.map +1 -0
  52. package/dist/client/KadiClient.js +492 -0
  53. package/dist/client/KadiClient.js.map +1 -0
  54. package/dist/client/index.d.ts +7 -0
  55. package/dist/client/index.d.ts.map +1 -0
  56. package/dist/client/index.js +7 -0
  57. package/dist/client/index.js.map +1 -0
  58. package/dist/config/ConfigLoader.d.ts +138 -0
  59. package/dist/config/ConfigLoader.d.ts.map +1 -0
  60. package/dist/config/ConfigLoader.js +226 -0
  61. package/dist/config/ConfigLoader.js.map +1 -0
  62. package/dist/config/ConfigResolver.d.ts +135 -0
  63. package/dist/config/ConfigResolver.d.ts.map +1 -0
  64. package/dist/config/ConfigResolver.js +282 -0
  65. package/dist/config/ConfigResolver.js.map +1 -0
  66. package/dist/config/index.d.ts +8 -0
  67. package/dist/config/index.d.ts.map +1 -0
  68. package/dist/config/index.js +8 -0
  69. package/dist/config/index.js.map +1 -0
  70. package/dist/errors/index.d.ts +9 -0
  71. package/dist/errors/index.d.ts.map +1 -0
  72. package/dist/errors/index.js +8 -0
  73. package/dist/errors/index.js.map +1 -0
  74. package/dist/events/EventHub.d.ts +172 -0
  75. package/dist/events/EventHub.d.ts.map +1 -0
  76. package/dist/events/EventHub.js +333 -0
  77. package/dist/events/EventHub.js.map +1 -0
  78. package/dist/events/index.d.ts +7 -0
  79. package/dist/events/index.d.ts.map +1 -0
  80. package/dist/events/index.js +7 -0
  81. package/dist/events/index.js.map +1 -0
  82. package/dist/index.d.ts +49 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/index.js +63 -0
  85. package/dist/index.js.map +1 -0
  86. package/dist/messages/index.d.ts +33 -0
  87. package/dist/messages/index.d.ts.map +1 -0
  88. package/dist/messages/index.js +33 -0
  89. package/dist/messages/index.js.map +1 -0
  90. package/dist/schemas/index.d.ts +19 -0
  91. package/dist/schemas/index.d.ts.map +1 -0
  92. package/dist/schemas/index.js +25 -0
  93. package/dist/schemas/index.js.map +1 -0
  94. package/dist/schemas/kadi-extensions.d.ts +231 -0
  95. package/dist/schemas/kadi-extensions.d.ts.map +1 -0
  96. package/dist/schemas/kadi-extensions.js +14 -0
  97. package/dist/schemas/kadi-extensions.js.map +1 -0
  98. package/dist/schemas/mcp/schema.d.ts +1399 -0
  99. package/dist/schemas/mcp/schema.d.ts.map +1 -0
  100. package/dist/schemas/mcp/schema.js +53 -0
  101. package/dist/schemas/mcp/schema.js.map +1 -0
  102. package/dist/schemas/mcp/version.d.ts +37 -0
  103. package/dist/schemas/mcp/version.d.ts.map +1 -0
  104. package/dist/schemas/mcp/version.js +39 -0
  105. package/dist/schemas/mcp/version.js.map +1 -0
  106. package/dist/schemas/schema-builders.d.ts +178 -0
  107. package/dist/schemas/schema-builders.d.ts.map +1 -0
  108. package/dist/schemas/schema-builders.js +258 -0
  109. package/dist/schemas/schema-builders.js.map +1 -0
  110. package/dist/tools/ToolRegistry.d.ts +256 -0
  111. package/dist/tools/ToolRegistry.d.ts.map +1 -0
  112. package/dist/tools/ToolRegistry.js +340 -0
  113. package/dist/tools/ToolRegistry.js.map +1 -0
  114. package/dist/tools/index.d.ts +7 -0
  115. package/dist/tools/index.d.ts.map +1 -0
  116. package/dist/tools/index.js +7 -0
  117. package/dist/tools/index.js.map +1 -0
  118. package/dist/transports/BrokerTransport.d.ts +151 -0
  119. package/dist/transports/BrokerTransport.d.ts.map +1 -0
  120. package/dist/transports/BrokerTransport.js +261 -0
  121. package/dist/transports/BrokerTransport.js.map +1 -0
  122. package/dist/transports/NativeTransport.d.ts +149 -0
  123. package/dist/transports/NativeTransport.d.ts.map +1 -0
  124. package/dist/transports/NativeTransport.js +302 -0
  125. package/dist/transports/NativeTransport.js.map +1 -0
  126. package/dist/transports/StdioTransport.d.ts +172 -0
  127. package/dist/transports/StdioTransport.d.ts.map +1 -0
  128. package/dist/transports/StdioTransport.js +410 -0
  129. package/dist/transports/StdioTransport.js.map +1 -0
  130. package/dist/transports/index.d.ts +10 -0
  131. package/dist/transports/index.d.ts.map +1 -0
  132. package/dist/transports/index.js +9 -0
  133. package/dist/transports/index.js.map +1 -0
  134. package/dist/types/broker.d.ts +301 -0
  135. package/dist/types/broker.d.ts.map +1 -0
  136. package/dist/types/broker.js +46 -0
  137. package/dist/types/broker.js.map +1 -0
  138. package/dist/types/config.d.ts +325 -0
  139. package/dist/types/config.d.ts.map +1 -0
  140. package/dist/types/config.js +17 -0
  141. package/dist/types/config.js.map +1 -0
  142. package/dist/types/errors.d.ts +178 -0
  143. package/dist/types/errors.d.ts.map +1 -0
  144. package/dist/types/errors.js +165 -0
  145. package/dist/types/errors.js.map +1 -0
  146. package/dist/types/events.d.ts +210 -0
  147. package/dist/types/events.d.ts.map +1 -0
  148. package/dist/types/events.js +8 -0
  149. package/dist/types/events.js.map +1 -0
  150. package/dist/types/index.d.ts +32 -0
  151. package/dist/types/index.d.ts.map +1 -0
  152. package/dist/types/index.js +21 -0
  153. package/dist/types/index.js.map +1 -0
  154. package/dist/types/protocol.d.ts +48 -0
  155. package/dist/types/protocol.d.ts.map +1 -0
  156. package/dist/types/protocol.js +11 -0
  157. package/dist/types/protocol.js.map +1 -0
  158. package/dist/types/tools.d.ts +67 -0
  159. package/dist/types/tools.d.ts.map +1 -0
  160. package/dist/types/tools.js +16 -0
  161. package/dist/types/tools.js.map +1 -0
  162. package/dist/types/transport.d.ts +250 -0
  163. package/dist/types/transport.d.ts.map +1 -0
  164. package/dist/types/transport.js +18 -0
  165. package/dist/types/transport.js.map +1 -0
  166. package/dist/validation/SchemaValidator.d.ts +208 -0
  167. package/dist/validation/SchemaValidator.d.ts.map +1 -0
  168. package/dist/validation/SchemaValidator.js +411 -0
  169. package/dist/validation/SchemaValidator.js.map +1 -0
  170. package/dist/validation/index.d.ts +11 -0
  171. package/dist/validation/index.d.ts.map +1 -0
  172. package/dist/validation/index.js +10 -0
  173. package/dist/validation/index.js.map +1 -0
  174. package/package.json +69 -5
  175. package/agent.json +0 -18
  176. package/broker.js +0 -214
  177. package/index.js +0 -370
  178. package/ipc.js +0 -220
  179. package/ipcInterfaces/pythonAbilityIPC.py +0 -177
@@ -0,0 +1,305 @@
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 { KadiError, ErrorCode } 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 class BrokerConnectionManager extends EventEmitter {
43
+ /**
44
+ * Active broker connections (name -> connection)
45
+ */
46
+ connections = new Map();
47
+ /**
48
+ * Default broker name
49
+ */
50
+ defaultBrokerName = null;
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
+ async connect(config, setAsDefault = false) {
69
+ // Check if already connected
70
+ if (this.connections.has(config.name)) {
71
+ const existing = this.connections.get(config.name);
72
+ if (existing.isConnected) {
73
+ return existing;
74
+ }
75
+ // Remove stale connection
76
+ this.connections.delete(config.name);
77
+ }
78
+ // Create new connection
79
+ const connection = new BrokerConnection(config);
80
+ // Set up event forwarding
81
+ connection.on('connected', () => {
82
+ this.emit('brokerConnected', config.name);
83
+ });
84
+ connection.on('disconnected', (code, reason) => {
85
+ this.emit('brokerDisconnected', config.name, code, reason);
86
+ });
87
+ connection.on('error', (error) => {
88
+ this.emit('brokerError', config.name, error);
89
+ });
90
+ connection.on('message', (message) => {
91
+ this.emit('brokerMessage', config.name, message);
92
+ });
93
+ // Connect
94
+ await connection.connect();
95
+ // Store connection
96
+ this.connections.set(config.name, connection);
97
+ // Set as default if requested or if it's the first connection
98
+ if (setAsDefault || this.connections.size === 1) {
99
+ this.defaultBrokerName = config.name;
100
+ }
101
+ return connection;
102
+ }
103
+ /**
104
+ * Connect to multiple brokers
105
+ *
106
+ * @param configs - Array of broker configurations
107
+ * @param defaultBrokerName - Name of the default broker
108
+ * @returns Array of established connections
109
+ *
110
+ * @throws {KadiError} If any connection fails
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * await manager.connectMultiple([
115
+ * { url: 'ws://localhost:8080', name: 'local' },
116
+ * { url: 'wss://prod.example.com', name: 'prod' }
117
+ * ], 'local');
118
+ * ```
119
+ */
120
+ async connectMultiple(configs, defaultBrokerName) {
121
+ const connections = [];
122
+ for (const config of configs) {
123
+ const isDefault = config.name === defaultBrokerName;
124
+ const connection = await this.connect(config, isDefault);
125
+ connections.push(connection);
126
+ }
127
+ return connections;
128
+ }
129
+ /**
130
+ * Disconnect from a broker
131
+ *
132
+ * @param brokerName - Name of broker to disconnect from
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * await manager.disconnect('local');
137
+ * ```
138
+ */
139
+ async disconnect(brokerName) {
140
+ const connection = this.connections.get(brokerName);
141
+ if (!connection) {
142
+ return;
143
+ }
144
+ await connection.disconnect();
145
+ this.connections.delete(brokerName);
146
+ // Clear default if this was it
147
+ if (this.defaultBrokerName === brokerName) {
148
+ this.defaultBrokerName = this.connections.size > 0
149
+ ? Array.from(this.connections.keys())[0] ?? null
150
+ : null;
151
+ }
152
+ }
153
+ /**
154
+ * Disconnect from all brokers
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * await manager.disconnectAll();
159
+ * ```
160
+ */
161
+ async disconnectAll() {
162
+ const disconnectPromises = Array.from(this.connections.keys()).map(name => this.disconnect(name));
163
+ await Promise.all(disconnectPromises);
164
+ }
165
+ /**
166
+ * Get a broker connection by name
167
+ *
168
+ * @param brokerName - Name of the broker
169
+ * @returns The connection or undefined if not found
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * const conn = manager.getConnection('local');
174
+ * if (conn) {
175
+ * await conn.sendRequest(...);
176
+ * }
177
+ * ```
178
+ */
179
+ getConnection(brokerName) {
180
+ return this.connections.get(brokerName);
181
+ }
182
+ /**
183
+ * Get the default broker connection
184
+ *
185
+ * @returns The default connection
186
+ * @throws {KadiError} If no default broker is set
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * const conn = manager.getDefaultConnection();
191
+ * await conn.sendRequest(...);
192
+ * ```
193
+ */
194
+ getDefaultConnection() {
195
+ if (!this.defaultBrokerName) {
196
+ throw new KadiError('No default broker set', ErrorCode.BROKER_NOT_CONNECTED, 503);
197
+ }
198
+ const connection = this.connections.get(this.defaultBrokerName);
199
+ if (!connection) {
200
+ throw new KadiError(`Default broker '${this.defaultBrokerName}' not found`, ErrorCode.BROKER_NOT_CONNECTED, 503, { defaultBroker: this.defaultBrokerName });
201
+ }
202
+ return connection;
203
+ }
204
+ /**
205
+ * Get all broker connections
206
+ *
207
+ * @returns Array of all connections
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * const connections = manager.getAllConnections();
212
+ * console.log(`Connected to ${connections.length} brokers`);
213
+ * ```
214
+ */
215
+ getAllConnections() {
216
+ return Array.from(this.connections.values());
217
+ }
218
+ /**
219
+ * Get all connected broker connections
220
+ *
221
+ * @returns Array of connected connections
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const connected = manager.getConnectedBrokers();
226
+ * ```
227
+ */
228
+ getConnectedBrokers() {
229
+ return this.getAllConnections().filter(conn => conn.isConnected);
230
+ }
231
+ /**
232
+ * Check if connected to a specific broker
233
+ *
234
+ * @param brokerName - Broker name
235
+ * @returns true if connected
236
+ *
237
+ * @example
238
+ * ```typescript
239
+ * if (manager.isConnected('local')) {
240
+ * // Use local broker
241
+ * }
242
+ * ```
243
+ */
244
+ isConnected(brokerName) {
245
+ const connection = this.connections.get(brokerName);
246
+ return connection?.isConnected ?? false;
247
+ }
248
+ /**
249
+ * Check if connected to any broker
250
+ *
251
+ * @returns true if connected to at least one broker
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * if (!manager.hasConnections()) {
256
+ * throw new Error('Not connected to any broker');
257
+ * }
258
+ * ```
259
+ */
260
+ hasConnections() {
261
+ return this.getConnectedBrokers().length > 0;
262
+ }
263
+ /**
264
+ * Set the default broker
265
+ *
266
+ * @param brokerName - Name of broker to set as default
267
+ * @throws {KadiError} If broker doesn't exist
268
+ *
269
+ * @example
270
+ * ```typescript
271
+ * manager.setDefaultBroker('production');
272
+ * ```
273
+ */
274
+ setDefaultBroker(brokerName) {
275
+ if (!this.connections.has(brokerName)) {
276
+ throw new KadiError(`Broker '${brokerName}' not found`, ErrorCode.BROKER_NOT_CONNECTED, 404, { brokerName, available: Array.from(this.connections.keys()) });
277
+ }
278
+ this.defaultBrokerName = brokerName;
279
+ }
280
+ /**
281
+ * Get default broker name
282
+ *
283
+ * @returns Default broker name or null if not set
284
+ */
285
+ getDefaultBrokerName() {
286
+ return this.defaultBrokerName;
287
+ }
288
+ /**
289
+ * Get number of active connections
290
+ *
291
+ * @returns Connection count
292
+ */
293
+ get connectionCount() {
294
+ return this.connections.size;
295
+ }
296
+ /**
297
+ * Get broker names
298
+ *
299
+ * @returns Array of broker names
300
+ */
301
+ getBrokerNames() {
302
+ return Array.from(this.connections.keys());
303
+ }
304
+ }
305
+ //# sourceMappingURL=BrokerConnectionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BrokerConnectionManager.js","sourceRoot":"","sources":["../../src/broker/BrokerConnectionManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,OAAO,uBAAwB,SAAQ,YAAY;IACvD;;OAEG;IACK,WAAW,GAAG,IAAI,GAAG,EAA4B,CAAC;IAE1D;;OAEG;IACK,iBAAiB,GAAkB,IAAI,CAAC;IAEhD;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,OAAO,CAAC,MAAoB,EAAE,YAAY,GAAG,KAAK;QACtD,6BAA6B;QAC7B,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;Y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;;;;;;;;;;;OAYG;IACH,WAAW,CAAC,UAAkB;QAC5B,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACpD,OAAO,UAAU,EAAE,WAAW,IAAI,KAAK,CAAC;IAC1C,CAAC;IAED;;;;;;;;;;;OAWG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,mBAAmB,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;;;;OAUG;IACH,gBAAgB,CAAC,UAAkB;QACjC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;YACtC,MAAM,IAAI,SAAS,CACjB,WAAW,UAAU,aAAa,EAClC,SAAS,CAAC,oBAAoB,EAC9B,GAAG,EACH,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,CAC/D,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,iBAAiB,GAAG,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACH,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACH,cAAc;QACZ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,280 @@
1
+ /**
2
+ * Broker Protocol
3
+ *
4
+ * Implements the KADI broker protocol on top of BrokerConnection.
5
+ * Handles handshake, authentication, heartbeat, and capability registration.
6
+ *
7
+ * @module broker/BrokerProtocol
8
+ */
9
+ import type { HandshakeParams, HandshakeResult, RegisterCapabilitiesParams, ToolDefinition, ToolInvokeParams, EventSubscribeParams, EventPublishParams } from '../types/index.js';
10
+ import type { BrokerConnection } from './BrokerConnection.js';
11
+ /**
12
+ * Protocol version
13
+ */
14
+ export declare const PROTOCOL_VERSION = "2.0.0";
15
+ /**
16
+ * Broker Protocol
17
+ *
18
+ * Implements KADI broker protocol operations.
19
+ * Sits on top of BrokerConnection and provides high-level protocol methods.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const protocol = new BrokerProtocol(connection);
24
+ *
25
+ * // Perform handshake
26
+ * const result = await protocol.handshake({
27
+ * name: 'my-agent',
28
+ * version: '1.0.0',
29
+ * protocolVersion: PROTOCOL_VERSION,
30
+ * networks: ['global']
31
+ * });
32
+ *
33
+ * // Register capabilities
34
+ * await protocol.registerCapabilities({
35
+ * tools: [
36
+ * { name: 'add', description: 'Add numbers' }
37
+ * ]
38
+ * });
39
+ * ```
40
+ */
41
+ export declare class BrokerProtocol {
42
+ /**
43
+ * Underlying broker connection
44
+ */
45
+ private readonly connection;
46
+ /**
47
+ * Heartbeat interval timer
48
+ */
49
+ private heartbeatTimer;
50
+ /**
51
+ * Ed25519 keypair for authentication
52
+ */
53
+ private publicKey?;
54
+ private privateKey?;
55
+ /**
56
+ * Create a new BrokerProtocol
57
+ *
58
+ * @param connection - Broker connection to use
59
+ */
60
+ constructor(connection: BrokerConnection);
61
+ /**
62
+ * Perform handshake with broker
63
+ *
64
+ * First step in establishing a session with the broker.
65
+ * The broker will assign an agent ID and confirm protocol compatibility.
66
+ *
67
+ * @param params - Handshake parameters
68
+ * @returns Handshake result with agent ID and broker info
69
+ *
70
+ * @throws {KadiError} If handshake fails
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const result = await protocol.handshake({
75
+ * name: 'calculator',
76
+ * version: '1.0.0',
77
+ * protocolVersion: PROTOCOL_VERSION,
78
+ * networks: ['global']
79
+ * });
80
+ *
81
+ * console.log('Assigned agent ID:', result.agentId);
82
+ * ```
83
+ */
84
+ handshake(params: HandshakeParams): Promise<HandshakeResult>;
85
+ /**
86
+ * Authenticate with broker using Ed25519 signature
87
+ *
88
+ * @param nonce - Challenge nonce from broker
89
+ * @returns Assigned agent ID
90
+ * @private
91
+ */
92
+ private authenticate;
93
+ /**
94
+ * Start heartbeat to keep connection alive
95
+ *
96
+ * Sends periodic heartbeat messages to the broker.
97
+ *
98
+ * @param intervalMs - Heartbeat interval in milliseconds
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * protocol.startHeartbeat(30000); // Every 30 seconds
103
+ * ```
104
+ */
105
+ startHeartbeat(intervalMs: number): void;
106
+ /**
107
+ * Stop heartbeat
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * protocol.stopHeartbeat();
112
+ * ```
113
+ */
114
+ stopHeartbeat(): void;
115
+ /**
116
+ * Send a single heartbeat
117
+ *
118
+ * @returns Promise that resolves when heartbeat is acknowledged
119
+ */
120
+ private sendHeartbeat;
121
+ /**
122
+ * Register capabilities (tools) with the broker
123
+ *
124
+ * Tells the broker what tools this agent provides.
125
+ * Other agents can then discover and invoke these tools.
126
+ *
127
+ * @param params - Capability registration parameters
128
+ *
129
+ * @throws {KadiError} If registration fails
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * await protocol.registerCapabilities({
134
+ * tools: [
135
+ * {
136
+ * name: 'calculate',
137
+ * description: 'Perform calculations',
138
+ * inputSchema: { type: 'object', ... },
139
+ * outputSchema: { type: 'object', ... }
140
+ * }
141
+ * ],
142
+ * networks: ['global']
143
+ * });
144
+ * ```
145
+ */
146
+ registerCapabilities(params: RegisterCapabilitiesParams): Promise<void>;
147
+ /**
148
+ * Unregister capabilities from the broker
149
+ *
150
+ * @param toolNames - Names of tools to unregister (empty = unregister all)
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * // Unregister specific tools
155
+ * await protocol.unregisterCapabilities(['old-tool']);
156
+ *
157
+ * // Unregister all
158
+ * await protocol.unregisterCapabilities([]);
159
+ * ```
160
+ */
161
+ unregisterCapabilities(_toolNames?: string[]): Promise<void>;
162
+ /**
163
+ * Discover agents on the network
164
+ *
165
+ * NOTE: Broker returns all tools with providers, we transform to agent-centric view client-side.
166
+ * Future enhancement: Add broker support for agent filtering to reduce network overhead.
167
+ *
168
+ * @param networks - Networks to search in
169
+ * @returns Array of discovered agents
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * const agents = await protocol.discoverAgents(['global']);
174
+ * console.log('Found agents:', agents);
175
+ * ```
176
+ */
177
+ discoverAgents(networks: string[]): Promise<Array<{
178
+ name: string;
179
+ capabilities: string[];
180
+ }>>;
181
+ /**
182
+ * Query capabilities of a specific agent
183
+ *
184
+ * NOTE: Broker doesn't support filtering by agentName yet, so we filter client-side.
185
+ * Future enhancement: Add broker support for agent filtering.
186
+ *
187
+ * @param agentName - Name of the agent
188
+ * @param networks - Networks to search in
189
+ * @returns Array of tool definitions
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * const capabilities = await protocol.queryCapabilities('calculator', ['global']);
194
+ * console.log('Calculator tools:', capabilities);
195
+ * ```
196
+ */
197
+ queryCapabilities(agentName: string, networks: string[]): Promise<ToolDefinition[]>;
198
+ /**
199
+ * Invoke a tool on a remote agent
200
+ *
201
+ * @template T - Result type
202
+ * @param params - Tool invocation parameters
203
+ * @returns Tool invocation result
204
+ *
205
+ * @throws {KadiError} If invocation fails
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * const result = await protocol.invokeTool<{ result: number }>({
210
+ * targetAgent: 'calculator',
211
+ * toolName: 'add',
212
+ * toolInput: { a: 5, b: 3 },
213
+ * timeout: 30000
214
+ * });
215
+ *
216
+ * console.log('Result:', result.result); // 8
217
+ * ```
218
+ */
219
+ invokeTool<T = unknown>(params: ToolInvokeParams): Promise<T>;
220
+ /**
221
+ * Subscribe to events on specific channels
222
+ *
223
+ * NOTE: Currently subscribes to first channel only - broker supports one pattern per subscription.
224
+ * For multiple patterns, call this method multiple times.
225
+ *
226
+ * @param params - Event subscription parameters
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * await protocol.subscribeToEvents({
231
+ * channels: ['user.*'],
232
+ * networkId: 'global'
233
+ * });
234
+ * ```
235
+ */
236
+ subscribeToEvents(params: EventSubscribeParams): Promise<void>;
237
+ /**
238
+ * Unsubscribe from event channels
239
+ *
240
+ * NOTE: Currently unsubscribes from first channel only - broker supports one pattern per unsubscribe.
241
+ * For multiple patterns, call this method multiple times.
242
+ *
243
+ * @param params - Event unsubscribe parameters
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * await protocol.unsubscribeFromEvents({
248
+ * channels: ['user.*'],
249
+ * networkId: 'global'
250
+ * });
251
+ * ```
252
+ */
253
+ unsubscribeFromEvents(params: EventSubscribeParams): Promise<void>;
254
+ /**
255
+ * Publish an event to a channel
256
+ *
257
+ * @param params - Event publish parameters
258
+ *
259
+ * @example
260
+ * ```typescript
261
+ * await protocol.publishEvent({
262
+ * channel: 'user.login',
263
+ * data: { userId: '123', timestamp: Date.now() },
264
+ * networkId: 'global',
265
+ * hints: { latestOnly: true }
266
+ * });
267
+ * ```
268
+ */
269
+ publishEvent(params: EventPublishParams): Promise<void>;
270
+ /**
271
+ * Cleanup protocol resources
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * protocol.cleanup();
276
+ * ```
277
+ */
278
+ cleanup(): void;
279
+ }
280
+ //# sourceMappingURL=BrokerProtocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BrokerProtocol.d.ts","sourceRoot":"","sources":["../../src/broker/BrokerProtocol.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,gBAAgB,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;GAy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