agent-swarm-kit 1.0.70 → 1.0.71

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/types.d.ts CHANGED
@@ -53,1005 +53,1058 @@ interface ILogger {
53
53
  debug(...args: any[]): void;
54
54
  }
55
55
 
56
- interface ISwarmSessionCallbacks {
56
+ /**
57
+ * Type representing an array of numbers as embeddings.
58
+ */
59
+ type Embeddings = number[];
60
+ /**
61
+ * Interface for embedding callbacks.
62
+ */
63
+ interface IEmbeddingCallbacks {
57
64
  /**
58
- * Callback triggered when a client connects.
59
- * @param clientId - The ID of the client.
60
- * @param swarmName - The name of the swarm.
65
+ * Callback for when an embedding is created.
66
+ * @param text - The text used to create the embedding.
67
+ * @param embeddings - The generated embeddings.
68
+ * @param clientId - The client ID associated with the embedding.
69
+ * @param embeddingName - The name of the embedding.
61
70
  */
62
- onConnect?: (clientId: string, swarmName: SwarmName) => void;
71
+ onCreate(text: string, embeddings: Embeddings, clientId: string, embeddingName: EmbeddingName): void;
63
72
  /**
64
- * Callback triggered when a command is executed.
65
- * @param clientId - The ID of the client.
66
- * @param swarmName - The name of the swarm.
67
- * @param content - The content to execute.
68
- * @param mode - The source of execution: tool or user.
73
+ * Callback for when embeddings are compared.
74
+ * @param text1 - The first text used in the comparison.
75
+ * @param text2 - The second text used in the comparison.
76
+ * @param similarity - The similarity score between the embeddings.
77
+ * @param clientId - The client ID associated with the comparison.
78
+ * @param embeddingName - The name of the embedding.
69
79
  */
70
- onExecute?: (clientId: string, swarmName: SwarmName, content: string, mode: ExecutionMode) => void;
80
+ onCompare(text1: string, text2: string, similarity: number, clientId: string, embeddingName: EmbeddingName): void;
81
+ }
82
+ /**
83
+ * Interface representing the schema for embeddings.
84
+ */
85
+ interface IEmbeddingSchema {
71
86
  /**
72
- * Callback triggered when a message is emitted.
73
- * @param clientId - The ID of the client.
74
- * @param swarmName - The name of the swarm.
75
- * @param message - The message to emit.
87
+ * The name of the embedding.
76
88
  */
77
- onEmit?: (clientId: string, swarmName: SwarmName, message: string) => void;
89
+ embeddingName: EmbeddingName;
78
90
  /**
79
- * Callback triggered when a session being connected
80
- * @param clientId - The ID of the client.
81
- * @param swarmName - The name of the swarm.
91
+ * Creates an embedding from the given text.
92
+ * @param text - The text to create the embedding from.
93
+ * @returns A promise that resolves to the generated embeddings.
82
94
  */
83
- onInit?: (clientId: string, swarmName: SwarmName) => void;
95
+ createEmbedding(text: string): Promise<Embeddings>;
84
96
  /**
85
- * Callback triggered when a session being disponnected
86
- * @param clientId - The ID of the client.
87
- * @param swarmName - The name of the swarm.
97
+ * Calculates the similarity between two embeddings.
98
+ * @param a - The first embedding.
99
+ * @param b - The second embedding.
100
+ * @returns A promise that resolves to the similarity score.
88
101
  */
89
- onDispose?: (clientId: string, swarmName: SwarmName) => void;
90
- }
91
- /**
92
- * Lifecycle callbacks of initialized swarm
93
- */
94
- interface ISwarmCallbacks extends ISwarmSessionCallbacks {
95
- /** Emit the callback on agent change */
96
- onAgentChanged: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
102
+ calculateSimilarity(a: Embeddings, b: Embeddings): Promise<number>;
103
+ /**
104
+ * Optional callbacks for embedding events.
105
+ */
106
+ callbacks?: Partial<IEmbeddingCallbacks>;
97
107
  }
98
108
  /**
99
- * Parameters for initializing a swarm.
100
- * @interface
101
- * @extends {Omit<ISwarmSchema, 'agentList'>}
109
+ * Type representing the name of an embedding.
102
110
  */
103
- interface ISwarmParams extends Omit<ISwarmSchema, keyof {
104
- agentList: never;
105
- onAgentChanged: never;
106
- }>, ISwarmCallbacks {
107
- /** Client identifier */
108
- clientId: string;
109
- /** Logger instance */
110
- logger: ILogger;
111
- /** Map of agent names to agent instances */
112
- agentMap: Record<AgentName, IAgent>;
113
- }
111
+ type EmbeddingName = string;
112
+
113
+ type StorageId = string | number;
114
114
  /**
115
- * Schema for defining a swarm.
116
- * @interface
115
+ * Interface representing the data stored in the storage.
117
116
  */
118
- interface ISwarmSchema {
119
- /** Default agent name */
120
- defaultAgent: AgentName;
121
- /** Name of the swarm */
122
- swarmName: string;
123
- /** List of agent names */
124
- agentList: string[];
125
- /** Lifecycle callbacks*/
126
- callbacks?: Partial<ISwarmCallbacks>;
117
+ interface IStorageData {
118
+ id: StorageId;
127
119
  }
128
120
  /**
129
- * Interface for a swarm.
130
- * @interface
121
+ * Interface representing the schema of the storage.
122
+ * @template T - Type of the storage data.
131
123
  */
132
- interface ISwarm {
124
+ interface IStorageSchema<T extends IStorageData = IStorageData> {
133
125
  /**
134
- * Waits for the output from the swarm.
135
- * @returns {Promise<string>} The output from the swarm.
126
+ * All agents will share the same ClientStorage instance
136
127
  */
137
- waitForOutput(): Promise<string>;
128
+ shared?: boolean;
138
129
  /**
139
- * Gets the name of the agent.
140
- * @returns {Promise<AgentName>} The name of the agent.
130
+ * Function to get data from the storage.
131
+ * @param clientId - The client ID.
132
+ * @param storageName - The name of the storage.
133
+ * @returns A promise that resolves to an array of storage data or an array of storage data.
141
134
  */
142
- getAgentName(): Promise<AgentName>;
135
+ getData?: (clientId: string, storageName: StorageName) => Promise<T[]> | T[];
143
136
  /**
144
- * Gets the agent instance.
145
- * @returns {Promise<IAgent>} The agent instance.
137
+ * Function to create an index for an item.
138
+ * @param item - The item to index.
139
+ * @returns A promise that resolves to a string or a string.
146
140
  */
147
- getAgent(): Promise<IAgent>;
141
+ createIndex(item: T): Promise<string> | string;
148
142
  /**
149
- * Sets the reference to an agent.
150
- * @param {AgentName} agentName - The name of the agent.
151
- * @param {IAgent} agent - The agent instance.
152
- * @returns {Promise<void>}
143
+ * The name of the embedding.
153
144
  */
154
- setAgentRef(agentName: AgentName, agent: IAgent): Promise<void>;
145
+ embedding: EmbeddingName;
155
146
  /**
156
- * Sets the name of the agent.
157
- * @param {AgentName} agentName - The name of the agent.
158
- * @returns {Promise<void>}
147
+ * The name of the storage.
159
148
  */
160
- setAgentName(agentName: AgentName): Promise<void>;
161
- }
162
- /** Type alias for swarm name */
163
- type SwarmName = string;
164
-
165
- /**
166
- * Parameters required to create a session.
167
- * @interface
168
- */
169
- interface ISessionParams extends ISessionSchema, ISwarmSessionCallbacks {
170
- clientId: string;
171
- logger: ILogger;
172
- swarm: ISwarm;
173
- swarmName: SwarmName;
174
- }
175
- /**
176
- * Schema for session data.
177
- * @interface
178
- */
179
- interface ISessionSchema {
149
+ storageName: StorageName;
150
+ /**
151
+ * Optional callbacks for storage events.
152
+ */
153
+ callbacks?: Partial<IStorageCallbacks<T>>;
180
154
  }
181
155
  /**
182
- * Function type for sending messages.
183
- * @typedef {function} SendMessageFn
184
- * @param {IOutgoingMessage} outgoing - The outgoing message.
185
- * @returns {Promise<void> | void}
186
- */
187
- type SendMessageFn$1 = (outgoing: IOutgoingMessage) => Promise<void> | void;
188
- /**
189
- * Function type for receiving messages.
190
- * @typedef {function} ReceiveMessageFn
191
- * @param {IIncomingMessage} incoming - The incoming message.
192
- * @returns {Promise<void> | void}
193
- */
194
- type ReceiveMessageFn = (incoming: IIncomingMessage) => Promise<void> | void;
195
- /**
196
- * Interface for a session.
197
- * @interface
156
+ * Interface representing the callbacks for storage events.
157
+ * @template T - Type of the storage data.
198
158
  */
199
- interface ISession {
200
- /**
201
- * Emit a message.
202
- * @param {string} message - The message to emit.
203
- * @returns {Promise<void>}
204
- */
205
- emit(message: string): Promise<void>;
206
- /**
207
- * Execute a command.
208
- * @param {string} content - The content to execute.
209
- * @param {string} mode - The source of execution: tool or user
210
- * @returns {Promise<string>}
211
- */
212
- execute(content: string, mode: ExecutionMode): Promise<string>;
213
- /**
214
- * Connect to a message sender.
215
- * @param {SendMessageFn} connector - The function to send messages.
216
- * @returns {ReceiveMessageFn}
217
- */
218
- connect(connector: SendMessageFn$1): ReceiveMessageFn;
159
+ interface IStorageCallbacks<T extends IStorageData = IStorageData> {
219
160
  /**
220
- * Commit tool output.
221
- * @param {string} toolId - The `tool_call_id` for openai history
222
- * @param {string} content - The content to commit.
223
- * @returns {Promise<void>}
161
+ * Callback function for update events.
162
+ * @param items - The updated items.
163
+ * @param clientId - The client ID.
164
+ * @param storageName - The name of the storage.
224
165
  */
225
- commitToolOutput(toolId: string, content: string): Promise<void>;
166
+ onUpdate: (items: T[], clientId: string, storageName: StorageName) => void;
226
167
  /**
227
- * Commit user message without answer
228
- * @param {string} message - The message to commit.
229
- * @returns {Promise<void>}
168
+ * Callback function for search events.
169
+ * @param search - The search query.
170
+ * @param index - The sorted array of storage data.
171
+ * @param clientId - The client ID.
172
+ * @param storageName - The name of the storage.
230
173
  */
231
- commitUserMessage: (message: string) => Promise<void>;
174
+ onSearch: (search: string, index: SortedArray<T>, clientId: string, storageName: StorageName) => void;
232
175
  /**
233
- * Commit flush of agent history
234
- * @returns {Promise<void>}
176
+ * Callback function for init
177
+ * @param clientId - The client ID.
178
+ * @param storageName - The name of the storage.
235
179
  */
236
- commitFlush: () => Promise<void>;
180
+ onInit: (clientId: string, storageName: StorageName) => void;
237
181
  /**
238
- * Commit a system message.
239
- * @param {string} message - The message to commit.
240
- * @returns {Promise<void>}
182
+ * Callback function for dispose
183
+ * @param clientId - The client ID.
184
+ * @param storageName - The name of the storage.
241
185
  */
242
- commitSystemMessage(message: string): Promise<void>;
186
+ onDispose: (clientId: string, storageName: StorageName) => void;
243
187
  }
244
188
  /**
245
- * Type for session ID.
246
- * @typedef {string} SessionId
247
- */
248
- type SessionId = string;
249
- /**
250
- * Type for session mode.
251
- * @typedef {"session" | "makeConnection" | "complete"} SessionMode
252
- */
253
- type SessionMode = "session" | "makeConnection" | "complete";
254
- /**
255
- * Tools can emit user messages to trigger user friendly responses.
256
- * Should be ignored for `getUserHistory`
257
- * @typedef {"tool" | "user"} ExecutionMode
258
- */
259
- type ExecutionMode = "tool" | "user";
260
-
261
- /**
262
- * Represents a tool call with a function name and arguments.
189
+ * Interface representing the parameters for storage.
190
+ * @template T - Type of the storage data.
263
191
  */
264
- interface IToolCall {
192
+ interface IStorageParams<T extends IStorageData = IStorageData> extends IStorageSchema<T>, Partial<IEmbeddingCallbacks> {
265
193
  /**
266
- * The ID of the tool call.
194
+ * The client ID.
267
195
  */
268
- id: string;
196
+ clientId: string;
269
197
  /**
270
- * The type of the tool. Currently, only `function` is supported.
198
+ * Function to calculate similarity.
271
199
  */
272
- type: 'function';
200
+ calculateSimilarity: IEmbeddingSchema["calculateSimilarity"];
273
201
  /**
274
- * The function that the model called.
202
+ * Function to create an embedding.
275
203
  */
276
- function: {
277
- /**
278
- * The name of the function to be called.
279
- */
280
- name: string;
281
- /**
282
- * The arguments to be passed to the function.
283
- */
284
- arguments: {
285
- [key: string]: any;
286
- };
287
- };
288
- }
289
- /**
290
- * Represents a tool with a type and function details.
291
- */
292
- interface ITool {
204
+ createEmbedding: IEmbeddingSchema["createEmbedding"];
293
205
  /**
294
- * The type of the tool.
206
+ * The name of the storage.
295
207
  */
296
- type: string;
297
- function: {
298
- /**
299
- * The name of the function.
300
- */
301
- name: string;
302
- /**
303
- * The description of the function.
304
- */
305
- description: string;
306
- /**
307
- * The parameters required by the function.
308
- */
309
- parameters: {
310
- /**
311
- * The type of the parameters.
312
- */
313
- type: string;
314
- /**
315
- * The list of required parameters.
316
- */
317
- required: string[];
318
- /**
319
- * The properties of the parameters.
320
- */
321
- properties: {
322
- [key: string]: {
323
- /**
324
- * The type of the property.
325
- */
326
- type: string;
327
- /**
328
- * The description of the property.
329
- */
330
- description: string;
331
- /**
332
- * The possible values for the property.
333
- */
334
- enum?: string[];
335
- };
336
- };
337
- };
338
- };
208
+ storageName: StorageName;
209
+ /**
210
+ * Logger instance.
211
+ */
212
+ logger: ILogger;
213
+ /** The bus instance. */
214
+ bus: IBus;
339
215
  }
340
-
341
216
  /**
342
- * Interface representing a model message.
217
+ * Interface representing the storage.
218
+ * @template T - Type of the storage data.
343
219
  */
344
- interface IModelMessage {
220
+ interface IStorage<T extends IStorageData = IStorageData> {
345
221
  /**
346
- * The role of the message sender.
347
- * @type {'assistant' | 'system' | 'tool' | 'user' | 'resque' | 'flush'}
222
+ * Function to take items from the storage.
223
+ * @param search - The search query.
224
+ * @param total - The total number of items to take.
225
+ * @param score - Optional score parameter.
226
+ * @returns A promise that resolves to an array of storage data.
348
227
  */
349
- role: "assistant" | "system" | "tool" | "user" | "resque" | "flush";
228
+ take(search: string, total: number, score?: number): Promise<T[]>;
350
229
  /**
351
- * The name of the agent sending the message.
352
- * @type {string}
230
+ * Function to upsert an item in the storage.
231
+ * @param item - The item to upsert.
232
+ * @returns A promise that resolves when the operation is complete.
353
233
  */
354
- agentName: string;
234
+ upsert(item: T): Promise<void>;
355
235
  /**
356
- * The content of the message.
357
- * @type {string}
236
+ * Function to remove an item from the storage.
237
+ * @param itemId - The ID of the item to remove.
238
+ * @returns A promise that resolves when the operation is complete.
358
239
  */
359
- content: string;
240
+ remove(itemId: IStorageData["id"]): Promise<void>;
360
241
  /**
361
- * The source of message: tool or user
362
- * @type {ExecutionMode}
242
+ * Function to get an item from the storage.
243
+ * @param itemId - The ID of the item to get.
244
+ * @returns A promise that resolves to the item or null if not found.
363
245
  */
364
- mode: ExecutionMode;
246
+ get(itemId: IStorageData["id"]): Promise<T | null>;
365
247
  /**
366
- * Optional tool calls associated with the message.
367
- * @type {Array<{ function: { name: string; arguments: { [key: string]: any; }; }; }>}
248
+ * Function to list items from the storage.
249
+ * @param filter - Optional filter function.
250
+ * @returns A promise that resolves to an array of storage data.
368
251
  */
369
- tool_calls?: IToolCall[];
252
+ list(filter?: (item: T) => boolean): Promise<T[]>;
370
253
  /**
371
- * Tool call that this message is responding to.
254
+ * Function to clear the storage.
255
+ * @returns A promise that resolves when the operation is complete.
372
256
  */
373
- tool_call_id?: string;
257
+ clear(): Promise<void>;
374
258
  }
259
+ /**
260
+ * Type representing the name of the storage.
261
+ */
262
+ type StorageName = string;
375
263
 
264
+ type IStateData = any;
376
265
  /**
377
- * Interface for History Adapter Callbacks
266
+ * Middleware function for state management.
267
+ * @template T - The type of the state data.
268
+ * @param {T} state - The current state.
269
+ * @param {string} clientId - The client ID.
270
+ * @param {StateName} stateName - The name of the state.
271
+ * @returns {Promise<T>} - The updated state.
378
272
  */
379
- interface IHistoryInstanceCallbacks {
273
+ interface IStateMiddleware<T extends IStateData = IStateData> {
274
+ (state: T, clientId: string, stateName: StateName): Promise<T>;
275
+ }
276
+ /**
277
+ * Callbacks for state lifecycle events.
278
+ * @template T - The type of the state data.
279
+ */
280
+ interface IStateCallbacks<T extends IStateData = IStateData> {
380
281
  /**
381
- * Filter condition for history messages.
382
- * @param message - The model message.
383
- * @param clientId - The client ID.
384
- * @param agentName - The agent name.
385
- * @returns A promise that resolves to a boolean indicating whether the message passes the filter.
282
+ * Called when the state is initialized.
283
+ * @param {string} clientId - The client ID.
284
+ * @param {StateName} stateName - The name of the state.
386
285
  */
387
- filterCondition?: (message: IModelMessage, clientId: string, agentName: AgentName) => Promise<boolean> | boolean;
286
+ onInit: (clientId: string, stateName: StateName) => void;
388
287
  /**
389
- * Get data for the history.
390
- * @param clientId - The client ID.
391
- * @param agentName - The agent name.
392
- * @returns A promise that resolves to an array of model messages.
288
+ * Called when the state is disposed.
289
+ * @param {string} clientId - The client ID.
290
+ * @param {StateName} stateName - The name of the state.
393
291
  */
394
- getData: (clientId: string, agentName: AgentName) => Promise<IModelMessage[]> | IModelMessage[];
292
+ onDispose: (clientId: string, stateName: StateName) => void;
395
293
  /**
396
- * Callback for when the history changes.
397
- * @param data - The array of model messages.
398
- * @param clientId - The client ID.
399
- * @param agentName - The agent name.
294
+ * Called when the state is loaded.
295
+ * @param {T} state - The current state.
296
+ * @param {string} clientId - The client ID.
297
+ * @param {StateName} stateName - The name of the state.
400
298
  */
401
- onChange: (data: IModelMessage[], clientId: string, agentName: AgentName) => void;
299
+ onLoad: (state: T, clientId: string, stateName: StateName) => void;
402
300
  /**
403
- * Callback for when the history get the new message
404
- * @param data - The array of model messages.
405
- * @param clientId - The client ID.
406
- * @param agentName - The agent name.
301
+ * Called when the state is read.
302
+ * @param {T} state - The current state.
303
+ * @param {string} clientId - The client ID.
304
+ * @param {StateName} stateName - The name of the state.
407
305
  */
408
- onPush: (data: IModelMessage, clientId: string, agentName: AgentName) => void;
306
+ onRead: (state: T, clientId: string, stateName: StateName) => void;
409
307
  /**
410
- * Callback for when the history is read. Will be called for each message
411
- * @param message - The model message.
412
- * @param clientId - The client ID.
413
- * @param agentName - The agent name.
308
+ * Called when the state is written.
309
+ * @param {T} state - The current state.
310
+ * @param {string} clientId - The client ID.
311
+ * @param {StateName} stateName - The name of the state.
414
312
  */
415
- onRead: (message: IModelMessage, clientId: string, agentName: AgentName) => void;
313
+ onWrite: (state: T, clientId: string, stateName: StateName) => void;
314
+ }
315
+ /**
316
+ * Schema for state management.
317
+ * @template T - The type of the state data.
318
+ */
319
+ interface IStateSchema<T extends IStateData = IStateData> {
320
+ /**
321
+ * The agents can share the state
322
+ */
323
+ shared?: boolean;
416
324
  /**
417
- * Callback for when the read is begin
418
- * @param clientId - The client ID.
419
- * @param agentName - The agent name.
325
+ * The name of the state.
420
326
  */
421
- onReadBegin: (clientId: string, agentName: AgentName) => void;
327
+ stateName: StateName;
422
328
  /**
423
- * Callback for when the read is end
424
- * @param clientId - The client ID.
425
- * @param agentName - The agent name.
329
+ * Gets the state.
330
+ * @param {string} clientId - The client ID.
331
+ * @param {StateName} stateName - The name of the state.
332
+ * @returns {T | Promise<T>} - The current state.
426
333
  */
427
- onReadEnd: (clientId: string, agentName: AgentName) => void;
334
+ getState: (clientId: string, stateName: StateName) => T | Promise<T>;
428
335
  /**
429
- * Callback for when the history is disposed.
430
- * @param clientId - The client ID.
336
+ * Sets the state.
337
+ * @param {T} state - The new state.
338
+ * @param {string} clientId - The client ID.
339
+ * @param {StateName} stateName - The name of the state.
340
+ * @returns {Promise<void> | void} - A promise that resolves when the state is set.
431
341
  */
432
- onDispose: (clientId: string) => void;
342
+ setState?: (state: T, clientId: string, stateName: StateName) => Promise<void> | void;
433
343
  /**
434
- * Callback for when the history is initialized.
435
- * @param clientId - The client ID.
344
+ * Middleware functions for state management.
436
345
  */
437
- onInit: (clientId: string) => void;
346
+ middlewares?: IStateMiddleware<T>[];
438
347
  /**
439
- * Callback to obtain history ref
440
- * @param clientId - The client ID.
348
+ * Callbacks for state lifecycle events.
441
349
  */
442
- onRef: (history: HistoryInstance) => void;
350
+ callbacks?: Partial<IStateCallbacks<T>>;
443
351
  }
444
352
  /**
445
- * Interface for History Adapter
353
+ * Parameters for state management.
354
+ * @template T - The type of the state data.
446
355
  */
447
- interface IHistoryAdapter {
448
- /**
449
- * Iterate over the history messages.
450
- * @param clientId - The client ID.
451
- * @param agentName - The agent name.
452
- * @returns An async iterable iterator of model messages.
453
- */
454
- iterate(clientId: string, agentName: AgentName): AsyncIterableIterator<IModelMessage>;
356
+ interface IStateParams<T extends IStateData = IStateData> extends IStateSchema<T> {
455
357
  /**
456
- * Push a new message to the history.
457
- * @param value - The model message to push.
458
- * @param clientId - The client ID.
459
- * @param agentName - The agent name.
460
- * @returns A promise that resolves when the message is pushed.
358
+ * The client ID.
461
359
  */
462
- push: (value: IModelMessage, clientId: string, agentName: AgentName) => Promise<void>;
360
+ clientId: string;
463
361
  /**
464
- * Dispose of the history for a given client and agent.
465
- * @param clientId - The client ID.
466
- * @param agentName - The agent name or null.
467
- * @returns A promise that resolves when the history is disposed.
362
+ * The logger instance.
468
363
  */
469
- dispose: (clientId: string, agentName: AgentName | null) => Promise<void>;
364
+ logger: ILogger;
365
+ /** The bus instance. */
366
+ bus: IBus;
470
367
  }
471
368
  /**
472
- * Interface for History Control
369
+ * State management interface.
370
+ * @template T - The type of the state data.
473
371
  */
474
- interface IHistoryControl {
372
+ interface IState<T extends IStateData = IStateData> {
475
373
  /**
476
- * Use a custom history adapter.
477
- * @param Ctor - The constructor for the history instance.
374
+ * Gets the state.
375
+ * @returns {Promise<T>} - The current state.
478
376
  */
479
- useHistoryAdapter(Ctor: THistoryInstanceCtor): void;
377
+ getState: () => Promise<T>;
480
378
  /**
481
- * Use history lifecycle callbacks.
482
- * @param Callbacks - The callbacks dictionary.
379
+ * Sets the state.
380
+ * @param {(prevState: T) => Promise<T>} dispatchFn - The function to dispatch the new state.
381
+ * @returns {Promise<T>} - The updated state.
483
382
  */
484
- useHistoryCallbacks: (Callbacks: Partial<IHistoryInstanceCallbacks>) => void;
383
+ setState: (dispatchFn: (prevState: T) => Promise<T>) => Promise<T>;
485
384
  }
486
385
  /**
487
- * Interface for History Instance
386
+ * The name of the state.
488
387
  */
489
- interface IHistoryInstance {
388
+ type StateName = string;
389
+
390
+ /**
391
+ * Interface representing the base context for an event.
392
+ */
393
+ interface IBaseEventContext {
490
394
  /**
491
- * Iterate over the history messages for a given agent.
492
- * @param agentName - The agent name.
493
- * @returns An async iterable iterator of model messages.
395
+ * The name of the agent.
494
396
  */
495
- iterate(agentName: AgentName): AsyncIterableIterator<IModelMessage>;
397
+ agentName: AgentName;
496
398
  /**
497
- * Wait for the history to initialize.
498
- * @param agentName - The agent name.
499
- * @param init - Whether the history is initializing.
500
- * @returns A promise that resolves when the history is initialized.
399
+ * The name of the swarm.
501
400
  */
502
- waitForInit(agentName: AgentName, init: boolean): Promise<void>;
401
+ swarmName: SwarmName;
503
402
  /**
504
- * Push a new message to the history for a given agent.
505
- * @param value - The model message to push.
506
- * @param agentName - The agent name.
507
- * @returns A promise that resolves when the message is pushed.
403
+ * The name of the storage.
508
404
  */
509
- push(value: IModelMessage, agentName: AgentName): Promise<void>;
405
+ storageName: StorageName;
510
406
  /**
511
- * Dispose of the history for a given agent.
512
- * @param agentName - The agent name or null.
513
- * @returns A promise that resolves when the history is disposed.
407
+ * The name of the state.
514
408
  */
515
- dispose(agentName: AgentName | null): Promise<void>;
409
+ stateName: StateName;
516
410
  }
517
411
  /**
518
- * Type for History Instance Constructor
412
+ * Type representing the possible sources of an event.
519
413
  */
520
- type THistoryInstanceCtor = new (clientId: string, ...args: unknown[]) => IHistoryInstance;
414
+ type EventSource = string;
521
415
  /**
522
- * Class representing a History Instance
416
+ * Interface representing the base structure of an event.
523
417
  */
524
- declare class HistoryInstance implements IHistoryInstance {
525
- readonly clientId: string;
526
- readonly callbacks: Partial<IHistoryInstanceCallbacks>;
527
- private _array;
418
+ interface IBaseEvent {
528
419
  /**
529
- * Wait for the history to initialize.
530
- * @param agentName - The agent name.
420
+ * The source of the event.
531
421
  */
532
- waitForInit: ((agentName: AgentName) => Promise<void>) & functools_kit.ISingleshotClearable;
422
+ source: EventSource;
423
+ }
424
+
425
+ interface IBus {
426
+ emit<T extends IBaseEvent>(clientId: string, event: T): Promise<void>;
427
+ }
428
+
429
+ interface ISwarmSessionCallbacks {
533
430
  /**
534
- * Create a HistoryInstance.
535
- * @param clientId - The client ID.
536
- * @param callbacks - The callbacks for the history instance.
431
+ * Callback triggered when a client connects.
432
+ * @param clientId - The ID of the client.
433
+ * @param swarmName - The name of the swarm.
537
434
  */
538
- constructor(clientId: string, callbacks: Partial<IHistoryInstanceCallbacks>);
435
+ onConnect?: (clientId: string, swarmName: SwarmName) => void;
539
436
  /**
540
- * Iterate over the history messages for a given agent.
541
- * @param agentName - The agent name.
542
- * @returns An async iterable iterator of model messages.
437
+ * Callback triggered when a command is executed.
438
+ * @param clientId - The ID of the client.
439
+ * @param swarmName - The name of the swarm.
440
+ * @param content - The content to execute.
441
+ * @param mode - The source of execution: tool or user.
543
442
  */
544
- iterate(agentName: AgentName): AsyncIterableIterator<IModelMessage>;
443
+ onExecute?: (clientId: string, swarmName: SwarmName, content: string, mode: ExecutionMode) => void;
545
444
  /**
546
- * Push a new message to the history for a given agent.
547
- * @param value - The model message to push.
548
- * @param agentName - The agent name.
549
- * @returns A promise that resolves when the message is pushed.
445
+ * Callback triggered when a message is emitted.
446
+ * @param clientId - The ID of the client.
447
+ * @param swarmName - The name of the swarm.
448
+ * @param message - The message to emit.
550
449
  */
551
- push: (value: IModelMessage, agentName: AgentName) => Promise<void>;
450
+ onEmit?: (clientId: string, swarmName: SwarmName, message: string) => void;
552
451
  /**
553
- * Dispose of the history for a given agent.
554
- * @param agentName - The agent name or null.
555
- * @returns A promise that resolves when the history is disposed.
452
+ * Callback triggered when a session being connected
453
+ * @param clientId - The ID of the client.
454
+ * @param swarmName - The name of the swarm.
556
455
  */
557
- dispose: (agentName: AgentName | null) => Promise<void>;
456
+ onInit?: (clientId: string, swarmName: SwarmName) => void;
457
+ /**
458
+ * Callback triggered when a session being disponnected
459
+ * @param clientId - The ID of the client.
460
+ * @param swarmName - The name of the swarm.
461
+ */
462
+ onDispose?: (clientId: string, swarmName: SwarmName) => void;
558
463
  }
559
464
  /**
560
- * Class representing History Utilities
465
+ * Lifecycle callbacks of initialized swarm
561
466
  */
562
- declare class HistoryUtils implements IHistoryAdapter, IHistoryControl {
563
- private HistoryFactory;
564
- private HistoryCallbacks;
565
- private getHistory;
467
+ interface ISwarmCallbacks extends ISwarmSessionCallbacks {
468
+ /** Emit the callback on agent change */
469
+ onAgentChanged: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
470
+ }
471
+ /**
472
+ * Parameters for initializing a swarm.
473
+ * @interface
474
+ * @extends {Omit<ISwarmSchema, 'agentList'>}
475
+ */
476
+ interface ISwarmParams extends Omit<ISwarmSchema, keyof {
477
+ agentList: never;
478
+ onAgentChanged: never;
479
+ }>, ISwarmCallbacks {
480
+ /** Client identifier */
481
+ clientId: string;
482
+ /** Logger instance */
483
+ logger: ILogger;
484
+ /** The bus instance. */
485
+ bus: IBus;
486
+ /** Map of agent names to agent instances */
487
+ agentMap: Record<AgentName, IAgent>;
488
+ }
489
+ /**
490
+ * Schema for defining a swarm.
491
+ * @interface
492
+ */
493
+ interface ISwarmSchema {
494
+ /** Default agent name */
495
+ defaultAgent: AgentName;
496
+ /** Name of the swarm */
497
+ swarmName: string;
498
+ /** List of agent names */
499
+ agentList: string[];
500
+ /** Lifecycle callbacks*/
501
+ callbacks?: Partial<ISwarmCallbacks>;
502
+ }
503
+ /**
504
+ * Interface for a swarm.
505
+ * @interface
506
+ */
507
+ interface ISwarm {
566
508
  /**
567
- * Use a custom history adapter.
568
- * @param Ctor - The constructor for the history instance.
509
+ * Will return empty string in waitForOutput
510
+ * @returns {Promise<void>}
569
511
  */
570
- useHistoryAdapter: (Ctor: THistoryInstanceCtor) => void;
512
+ cancelOutput(): Promise<void>;
571
513
  /**
572
- * Use history lifecycle callbacks.
573
- * @param Callbacks - The callbacks dictionary.
514
+ * Waits for the output from the swarm.
515
+ * @returns {Promise<string>} The output from the swarm.
574
516
  */
575
- useHistoryCallbacks: (Callbacks: Partial<IHistoryInstanceCallbacks>) => void;
517
+ waitForOutput(): Promise<string>;
576
518
  /**
577
- * Iterate over the history messages.
578
- * @param clientId - The client ID.
579
- * @param agentName - The agent name.
580
- * @returns An async iterable iterator of model messages.
519
+ * Gets the name of the agent.
520
+ * @returns {Promise<AgentName>} The name of the agent.
581
521
  */
582
- iterate(clientId: string, agentName: AgentName): AsyncIterableIterator<IModelMessage>;
522
+ getAgentName(): Promise<AgentName>;
583
523
  /**
584
- * Push a new message to the history.
585
- * @param value - The model message to push.
586
- * @param clientId - The client ID.
587
- * @param agentName - The agent name.
588
- * @returns A promise that resolves when the message is pushed.
524
+ * Gets the agent instance.
525
+ * @returns {Promise<IAgent>} The agent instance.
589
526
  */
590
- push: (value: IModelMessage, clientId: string, agentName: AgentName) => Promise<void>;
527
+ getAgent(): Promise<IAgent>;
591
528
  /**
592
- * Dispose of the history for a given client and agent.
593
- * @param clientId - The client ID.
594
- * @param agentName - The agent name or null.
595
- * @returns A promise that resolves when the history is disposed.
529
+ * Sets the reference to an agent.
530
+ * @param {AgentName} agentName - The name of the agent.
531
+ * @param {IAgent} agent - The agent instance.
532
+ * @returns {Promise<void>}
596
533
  */
597
- dispose: (clientId: string, agentName: AgentName | null) => Promise<void>;
534
+ setAgentRef(agentName: AgentName, agent: IAgent): Promise<void>;
535
+ /**
536
+ * Sets the name of the agent.
537
+ * @param {AgentName} agentName - The name of the agent.
538
+ * @returns {Promise<void>}
539
+ */
540
+ setAgentName(agentName: AgentName): Promise<void>;
598
541
  }
542
+ /** Type alias for swarm name */
543
+ type SwarmName = string;
544
+
599
545
  /**
600
- * Exported History Adapter instance
546
+ * Parameters required to create a session.
547
+ * @interface
601
548
  */
602
- declare const HistoryAdapter: HistoryUtils;
549
+ interface ISessionParams extends ISessionSchema, ISwarmSessionCallbacks {
550
+ clientId: string;
551
+ logger: ILogger;
552
+ bus: IBus;
553
+ swarm: ISwarm;
554
+ swarmName: SwarmName;
555
+ }
603
556
  /**
604
- * Exported History Control instance
557
+ * Schema for session data.
558
+ * @interface
605
559
  */
606
- declare const History: IHistoryControl;
607
-
560
+ interface ISessionSchema {
561
+ }
608
562
  /**
609
- * Interface representing the history of model messages.
563
+ * Function type for sending messages.
564
+ * @typedef {function} SendMessageFn
565
+ * @param {IOutgoingMessage} outgoing - The outgoing message.
566
+ * @returns {Promise<void> | void}
610
567
  */
611
- interface IHistory {
568
+ type SendMessageFn$1 = (outgoing: IOutgoingMessage) => Promise<void> | void;
569
+ /**
570
+ * Function type for receiving messages.
571
+ * @typedef {function} ReceiveMessageFn
572
+ * @param {IIncomingMessage} incoming - The incoming message.
573
+ * @returns {Promise<void> | void}
574
+ */
575
+ type ReceiveMessageFn = (incoming: IIncomingMessage) => Promise<void> | void;
576
+ /**
577
+ * Interface for a session.
578
+ * @interface
579
+ */
580
+ interface ISession {
612
581
  /**
613
- * Pushes a message to the history.
614
- * @param {IModelMessage} message - The message to push.
582
+ * Emit a message.
583
+ * @param {string} message - The message to emit.
615
584
  * @returns {Promise<void>}
616
585
  */
617
- push(message: IModelMessage): Promise<void>;
586
+ emit(message: string): Promise<void>;
618
587
  /**
619
- * Converts the history to an array of messages for a specific agent.
620
- * @param {string} prompt - The prompt to filter messages for the agent.
621
- * @returns {Promise<IModelMessage[]>}
588
+ * Execute a command.
589
+ * @param {string} content - The content to execute.
590
+ * @param {string} mode - The source of execution: tool or user
591
+ * @returns {Promise<string>}
622
592
  */
623
- toArrayForAgent(prompt: string, system?: string[]): Promise<IModelMessage[]>;
593
+ execute(content: string, mode: ExecutionMode): Promise<string>;
624
594
  /**
625
- * Converts the history to an array of raw messages.
626
- * @returns {Promise<IModelMessage[]>}
595
+ * Connect to a message sender.
596
+ * @param {SendMessageFn} connector - The function to send messages.
597
+ * @returns {ReceiveMessageFn}
627
598
  */
628
- toArrayForRaw(): Promise<IModelMessage[]>;
629
- }
630
- /**
631
- * Interface representing the parameters required to create a history instance.
632
- */
633
- interface IHistoryParams extends IHistorySchema {
599
+ connect(connector: SendMessageFn$1): ReceiveMessageFn;
634
600
  /**
635
- * The name of the agent.
636
- * @type {AgentName}
601
+ * Commit tool output.
602
+ * @param {string} toolId - The `tool_call_id` for openai history
603
+ * @param {string} content - The content to commit.
604
+ * @returns {Promise<void>}
637
605
  */
638
- agentName: AgentName;
606
+ commitToolOutput(toolId: string, content: string): Promise<void>;
639
607
  /**
640
- * The client ID.
641
- * @type {string}
608
+ * Commit user message without answer
609
+ * @param {string} message - The message to commit.
610
+ * @returns {Promise<void>}
642
611
  */
643
- clientId: string;
612
+ commitUserMessage: (message: string) => Promise<void>;
644
613
  /**
645
- * The logger instance.
646
- * @type {ILogger}
614
+ * Commit flush of agent history
615
+ * @returns {Promise<void>}
647
616
  */
648
- logger: ILogger;
649
- }
650
- /**
651
- * Interface representing the schema of the history.
652
- */
653
- interface IHistorySchema {
617
+ commitFlush: () => Promise<void>;
654
618
  /**
655
- * The adapter for array of model messages.
656
- * @type {IHistoryAdapter}
619
+ * Commit a system message.
620
+ * @param {string} message - The message to commit.
621
+ * @returns {Promise<void>}
657
622
  */
658
- items: IHistoryAdapter;
623
+ commitSystemMessage(message: string): Promise<void>;
659
624
  }
660
-
661
625
  /**
662
- * Interface representing a completion.
626
+ * Type for session ID.
627
+ * @typedef {string} SessionId
663
628
  */
664
- interface ICompletion extends ICompletionSchema {
665
- }
629
+ type SessionId = string;
666
630
  /**
667
- * Arguments required to get a completion.
631
+ * Type for session mode.
632
+ * @typedef {"session" | "makeConnection" | "complete"} SessionMode
668
633
  */
669
- interface ICompletionArgs {
670
- /**
671
- * Client ID.
672
- */
673
- clientId: string;
674
- /**
675
- * Name of the agent.
676
- */
677
- agentName: AgentName;
634
+ type SessionMode = "session" | "makeConnection" | "complete";
635
+ /**
636
+ * Tools can emit user messages to trigger user friendly responses.
637
+ * Should be ignored for `getUserHistory`
638
+ * @typedef {"tool" | "user"} ExecutionMode
639
+ */
640
+ type ExecutionMode = "tool" | "user";
641
+
642
+ /**
643
+ * Represents a tool call with a function name and arguments.
644
+ */
645
+ interface IToolCall {
678
646
  /**
679
- * The source of the last message: tool or user
647
+ * The ID of the tool call.
680
648
  */
681
- mode: ExecutionMode;
649
+ id: string;
682
650
  /**
683
- * Array of model messages.
651
+ * The type of the tool. Currently, only `function` is supported.
684
652
  */
685
- messages: IModelMessage[];
653
+ type: 'function';
686
654
  /**
687
- * Optional array of tools.
655
+ * The function that the model called.
688
656
  */
689
- tools?: ITool[];
657
+ function: {
658
+ /**
659
+ * The name of the function to be called.
660
+ */
661
+ name: string;
662
+ /**
663
+ * The arguments to be passed to the function.
664
+ */
665
+ arguments: {
666
+ [key: string]: any;
667
+ };
668
+ };
690
669
  }
691
670
  /**
692
- * Completion lifecycle callbacks
671
+ * Represents a tool with a type and function details.
693
672
  */
694
- interface ICompletionCallbacks {
673
+ interface ITool {
695
674
  /**
696
- * Callback fired after complete.
697
- * @param args - Arguments passed to complete
698
- * @param output - Output of the model
675
+ * The type of the tool.
699
676
  */
700
- onComplete?: (args: ICompletionArgs, output: IModelMessage) => void;
677
+ type: string;
678
+ function: {
679
+ /**
680
+ * The name of the function.
681
+ */
682
+ name: string;
683
+ /**
684
+ * The description of the function.
685
+ */
686
+ description: string;
687
+ /**
688
+ * The parameters required by the function.
689
+ */
690
+ parameters: {
691
+ /**
692
+ * The type of the parameters.
693
+ */
694
+ type: string;
695
+ /**
696
+ * The list of required parameters.
697
+ */
698
+ required: string[];
699
+ /**
700
+ * The properties of the parameters.
701
+ */
702
+ properties: {
703
+ [key: string]: {
704
+ /**
705
+ * The type of the property.
706
+ */
707
+ type: string;
708
+ /**
709
+ * The description of the property.
710
+ */
711
+ description: string;
712
+ /**
713
+ * The possible values for the property.
714
+ */
715
+ enum?: string[];
716
+ };
717
+ };
718
+ };
719
+ };
701
720
  }
721
+
702
722
  /**
703
- * Schema for a completion.
723
+ * Interface representing a model message.
704
724
  */
705
- interface ICompletionSchema {
725
+ interface IModelMessage {
706
726
  /**
707
- * Name of the completion.
727
+ * The role of the message sender.
728
+ * @type {'assistant' | 'system' | 'tool' | 'user' | 'resque' | 'flush'}
708
729
  */
709
- completionName: CompletionName;
730
+ role: "assistant" | "system" | "tool" | "user" | "resque" | "flush";
710
731
  /**
711
- * Method to get a completion.
712
- * @param args - Arguments required to get a completion.
713
- * @returns A promise that resolves to a model message.
732
+ * The name of the agent sending the message.
733
+ * @type {string}
714
734
  */
715
- getCompletion(args: ICompletionArgs): Promise<IModelMessage>;
735
+ agentName: string;
716
736
  /**
717
- * Completion lifecycle callbacks
737
+ * The content of the message.
738
+ * @type {string}
718
739
  */
719
- callbacks?: Partial<ICompletionCallbacks>;
720
- }
721
- /**
722
- * Type representing the name of a completion.
723
- */
724
- type CompletionName = string;
725
-
726
- /**
727
- * Type representing an array of numbers as embeddings.
728
- */
729
- type Embeddings = number[];
730
- /**
731
- * Interface for embedding callbacks.
732
- */
733
- interface IEmbeddingCallbacks {
740
+ content: string;
734
741
  /**
735
- * Callback for when an embedding is created.
736
- * @param text - The text used to create the embedding.
737
- * @param embeddings - The generated embeddings.
738
- * @param clientId - The client ID associated with the embedding.
739
- * @param embeddingName - The name of the embedding.
742
+ * The source of message: tool or user
743
+ * @type {ExecutionMode}
740
744
  */
741
- onCreate(text: string, embeddings: Embeddings, clientId: string, embeddingName: EmbeddingName): void;
745
+ mode: ExecutionMode;
746
+ /**
747
+ * Optional tool calls associated with the message.
748
+ * @type {Array<{ function: { name: string; arguments: { [key: string]: any; }; }; }>}
749
+ */
750
+ tool_calls?: IToolCall[];
742
751
  /**
743
- * Callback for when embeddings are compared.
744
- * @param text1 - The first text used in the comparison.
745
- * @param text2 - The second text used in the comparison.
746
- * @param similarity - The similarity score between the embeddings.
747
- * @param clientId - The client ID associated with the comparison.
748
- * @param embeddingName - The name of the embedding.
752
+ * Tool call that this message is responding to.
749
753
  */
750
- onCompare(text1: string, text2: string, similarity: number, clientId: string, embeddingName: EmbeddingName): void;
754
+ tool_call_id?: string;
751
755
  }
756
+
752
757
  /**
753
- * Interface representing the schema for embeddings.
758
+ * Interface for History Adapter Callbacks
754
759
  */
755
- interface IEmbeddingSchema {
760
+ interface IHistoryInstanceCallbacks {
756
761
  /**
757
- * The name of the embedding.
762
+ * Filter condition for history messages.
763
+ * @param message - The model message.
764
+ * @param clientId - The client ID.
765
+ * @param agentName - The agent name.
766
+ * @returns A promise that resolves to a boolean indicating whether the message passes the filter.
758
767
  */
759
- embeddingName: EmbeddingName;
768
+ filterCondition?: (message: IModelMessage, clientId: string, agentName: AgentName) => Promise<boolean> | boolean;
760
769
  /**
761
- * Creates an embedding from the given text.
762
- * @param text - The text to create the embedding from.
763
- * @returns A promise that resolves to the generated embeddings.
770
+ * Get data for the history.
771
+ * @param clientId - The client ID.
772
+ * @param agentName - The agent name.
773
+ * @returns A promise that resolves to an array of model messages.
764
774
  */
765
- createEmbedding(text: string): Promise<Embeddings>;
775
+ getData: (clientId: string, agentName: AgentName) => Promise<IModelMessage[]> | IModelMessage[];
766
776
  /**
767
- * Calculates the similarity between two embeddings.
768
- * @param a - The first embedding.
769
- * @param b - The second embedding.
770
- * @returns A promise that resolves to the similarity score.
777
+ * Callback for when the history changes.
778
+ * @param data - The array of model messages.
779
+ * @param clientId - The client ID.
780
+ * @param agentName - The agent name.
771
781
  */
772
- calculateSimilarity(a: Embeddings, b: Embeddings): Promise<number>;
782
+ onChange: (data: IModelMessage[], clientId: string, agentName: AgentName) => void;
773
783
  /**
774
- * Optional callbacks for embedding events.
784
+ * Callback for when the history get the new message
785
+ * @param data - The array of model messages.
786
+ * @param clientId - The client ID.
787
+ * @param agentName - The agent name.
775
788
  */
776
- callbacks?: Partial<IEmbeddingCallbacks>;
777
- }
778
- /**
779
- * Type representing the name of an embedding.
780
- */
781
- type EmbeddingName = string;
782
-
783
- type StorageId = string | number;
784
- /**
785
- * Interface representing the data stored in the storage.
786
- */
787
- interface IStorageData {
788
- id: StorageId;
789
- }
790
- /**
791
- * Interface representing the schema of the storage.
792
- * @template T - Type of the storage data.
793
- */
794
- interface IStorageSchema<T extends IStorageData = IStorageData> {
789
+ onPush: (data: IModelMessage, clientId: string, agentName: AgentName) => void;
795
790
  /**
796
- * All agents will share the same ClientStorage instance
791
+ * Callback for when the history is read. Will be called for each message
792
+ * @param message - The model message.
793
+ * @param clientId - The client ID.
794
+ * @param agentName - The agent name.
797
795
  */
798
- shared?: boolean;
796
+ onRead: (message: IModelMessage, clientId: string, agentName: AgentName) => void;
799
797
  /**
800
- * Function to get data from the storage.
798
+ * Callback for when the read is begin
801
799
  * @param clientId - The client ID.
802
- * @param storageName - The name of the storage.
803
- * @returns A promise that resolves to an array of storage data or an array of storage data.
800
+ * @param agentName - The agent name.
804
801
  */
805
- getData?: (clientId: string, storageName: StorageName) => Promise<T[]> | T[];
802
+ onReadBegin: (clientId: string, agentName: AgentName) => void;
806
803
  /**
807
- * Function to create an index for an item.
808
- * @param item - The item to index.
809
- * @returns A promise that resolves to a string or a string.
804
+ * Callback for when the read is end
805
+ * @param clientId - The client ID.
806
+ * @param agentName - The agent name.
810
807
  */
811
- createIndex(item: T): Promise<string> | string;
808
+ onReadEnd: (clientId: string, agentName: AgentName) => void;
812
809
  /**
813
- * The name of the embedding.
810
+ * Callback for when the history is disposed.
811
+ * @param clientId - The client ID.
814
812
  */
815
- embedding: EmbeddingName;
813
+ onDispose: (clientId: string) => void;
816
814
  /**
817
- * The name of the storage.
815
+ * Callback for when the history is initialized.
816
+ * @param clientId - The client ID.
818
817
  */
819
- storageName: StorageName;
818
+ onInit: (clientId: string) => void;
820
819
  /**
821
- * Optional callbacks for storage events.
820
+ * Callback to obtain history ref
821
+ * @param clientId - The client ID.
822
822
  */
823
- callbacks?: Partial<IStorageCallbacks<T>>;
823
+ onRef: (history: HistoryInstance) => void;
824
824
  }
825
825
  /**
826
- * Interface representing the callbacks for storage events.
827
- * @template T - Type of the storage data.
826
+ * Interface for History Adapter
828
827
  */
829
- interface IStorageCallbacks<T extends IStorageData = IStorageData> {
830
- /**
831
- * Callback function for update events.
832
- * @param items - The updated items.
833
- * @param clientId - The client ID.
834
- * @param storageName - The name of the storage.
835
- */
836
- onUpdate: (items: T[], clientId: string, storageName: StorageName) => void;
828
+ interface IHistoryAdapter {
837
829
  /**
838
- * Callback function for search events.
839
- * @param search - The search query.
840
- * @param index - The sorted array of storage data.
830
+ * Iterate over the history messages.
841
831
  * @param clientId - The client ID.
842
- * @param storageName - The name of the storage.
832
+ * @param agentName - The agent name.
833
+ * @returns An async iterable iterator of model messages.
843
834
  */
844
- onSearch: (search: string, index: SortedArray<T>, clientId: string, storageName: StorageName) => void;
835
+ iterate(clientId: string, agentName: AgentName): AsyncIterableIterator<IModelMessage>;
845
836
  /**
846
- * Callback function for init
837
+ * Push a new message to the history.
838
+ * @param value - The model message to push.
847
839
  * @param clientId - The client ID.
848
- * @param storageName - The name of the storage.
840
+ * @param agentName - The agent name.
841
+ * @returns A promise that resolves when the message is pushed.
849
842
  */
850
- onInit: (clientId: string, storageName: StorageName) => void;
843
+ push: (value: IModelMessage, clientId: string, agentName: AgentName) => Promise<void>;
851
844
  /**
852
- * Callback function for dispose
845
+ * Dispose of the history for a given client and agent.
853
846
  * @param clientId - The client ID.
854
- * @param storageName - The name of the storage.
847
+ * @param agentName - The agent name or null.
848
+ * @returns A promise that resolves when the history is disposed.
855
849
  */
856
- onDispose: (clientId: string, storageName: StorageName) => void;
850
+ dispose: (clientId: string, agentName: AgentName | null) => Promise<void>;
857
851
  }
858
852
  /**
859
- * Interface representing the parameters for storage.
860
- * @template T - Type of the storage data.
853
+ * Interface for History Control
861
854
  */
862
- interface IStorageParams<T extends IStorageData = IStorageData> extends IStorageSchema<T>, Partial<IEmbeddingCallbacks> {
855
+ interface IHistoryControl {
863
856
  /**
864
- * The client ID.
857
+ * Use a custom history adapter.
858
+ * @param Ctor - The constructor for the history instance.
865
859
  */
866
- clientId: string;
860
+ useHistoryAdapter(Ctor: THistoryInstanceCtor): void;
867
861
  /**
868
- * Function to calculate similarity.
862
+ * Use history lifecycle callbacks.
863
+ * @param Callbacks - The callbacks dictionary.
869
864
  */
870
- calculateSimilarity: IEmbeddingSchema["calculateSimilarity"];
865
+ useHistoryCallbacks: (Callbacks: Partial<IHistoryInstanceCallbacks>) => void;
866
+ }
867
+ /**
868
+ * Interface for History Instance
869
+ */
870
+ interface IHistoryInstance {
871
871
  /**
872
- * Function to create an embedding.
872
+ * Iterate over the history messages for a given agent.
873
+ * @param agentName - The agent name.
874
+ * @returns An async iterable iterator of model messages.
873
875
  */
874
- createEmbedding: IEmbeddingSchema["createEmbedding"];
876
+ iterate(agentName: AgentName): AsyncIterableIterator<IModelMessage>;
875
877
  /**
876
- * The name of the storage.
878
+ * Wait for the history to initialize.
879
+ * @param agentName - The agent name.
880
+ * @param init - Whether the history is initializing.
881
+ * @returns A promise that resolves when the history is initialized.
877
882
  */
878
- storageName: StorageName;
883
+ waitForInit(agentName: AgentName, init: boolean): Promise<void>;
879
884
  /**
880
- * Logger instance.
885
+ * Push a new message to the history for a given agent.
886
+ * @param value - The model message to push.
887
+ * @param agentName - The agent name.
888
+ * @returns A promise that resolves when the message is pushed.
881
889
  */
882
- logger: ILogger;
890
+ push(value: IModelMessage, agentName: AgentName): Promise<void>;
891
+ /**
892
+ * Dispose of the history for a given agent.
893
+ * @param agentName - The agent name or null.
894
+ * @returns A promise that resolves when the history is disposed.
895
+ */
896
+ dispose(agentName: AgentName | null): Promise<void>;
883
897
  }
884
898
  /**
885
- * Interface representing the storage.
886
- * @template T - Type of the storage data.
899
+ * Type for History Instance Constructor
887
900
  */
888
- interface IStorage<T extends IStorageData = IStorageData> {
901
+ type THistoryInstanceCtor = new (clientId: string, ...args: unknown[]) => IHistoryInstance;
902
+ /**
903
+ * Class representing a History Instance
904
+ */
905
+ declare class HistoryInstance implements IHistoryInstance {
906
+ readonly clientId: string;
907
+ readonly callbacks: Partial<IHistoryInstanceCallbacks>;
908
+ private _array;
889
909
  /**
890
- * Function to take items from the storage.
891
- * @param search - The search query.
892
- * @param total - The total number of items to take.
893
- * @param score - Optional score parameter.
894
- * @returns A promise that resolves to an array of storage data.
910
+ * Wait for the history to initialize.
911
+ * @param agentName - The agent name.
895
912
  */
896
- take(search: string, total: number, score?: number): Promise<T[]>;
913
+ waitForInit: ((agentName: AgentName) => Promise<void>) & functools_kit.ISingleshotClearable;
897
914
  /**
898
- * Function to upsert an item in the storage.
899
- * @param item - The item to upsert.
900
- * @returns A promise that resolves when the operation is complete.
915
+ * Create a HistoryInstance.
916
+ * @param clientId - The client ID.
917
+ * @param callbacks - The callbacks for the history instance.
901
918
  */
902
- upsert(item: T): Promise<void>;
919
+ constructor(clientId: string, callbacks: Partial<IHistoryInstanceCallbacks>);
903
920
  /**
904
- * Function to remove an item from the storage.
905
- * @param itemId - The ID of the item to remove.
906
- * @returns A promise that resolves when the operation is complete.
921
+ * Iterate over the history messages for a given agent.
922
+ * @param agentName - The agent name.
923
+ * @returns An async iterable iterator of model messages.
907
924
  */
908
- remove(itemId: IStorageData["id"]): Promise<void>;
925
+ iterate(agentName: AgentName): AsyncIterableIterator<IModelMessage>;
909
926
  /**
910
- * Function to get an item from the storage.
911
- * @param itemId - The ID of the item to get.
912
- * @returns A promise that resolves to the item or null if not found.
927
+ * Push a new message to the history for a given agent.
928
+ * @param value - The model message to push.
929
+ * @param agentName - The agent name.
930
+ * @returns A promise that resolves when the message is pushed.
931
+ */
932
+ push: (value: IModelMessage, agentName: AgentName) => Promise<void>;
933
+ /**
934
+ * Dispose of the history for a given agent.
935
+ * @param agentName - The agent name or null.
936
+ * @returns A promise that resolves when the history is disposed.
937
+ */
938
+ dispose: (agentName: AgentName | null) => Promise<void>;
939
+ }
940
+ /**
941
+ * Class representing History Utilities
942
+ */
943
+ declare class HistoryUtils implements IHistoryAdapter, IHistoryControl {
944
+ private HistoryFactory;
945
+ private HistoryCallbacks;
946
+ private getHistory;
947
+ /**
948
+ * Use a custom history adapter.
949
+ * @param Ctor - The constructor for the history instance.
950
+ */
951
+ useHistoryAdapter: (Ctor: THistoryInstanceCtor) => void;
952
+ /**
953
+ * Use history lifecycle callbacks.
954
+ * @param Callbacks - The callbacks dictionary.
955
+ */
956
+ useHistoryCallbacks: (Callbacks: Partial<IHistoryInstanceCallbacks>) => void;
957
+ /**
958
+ * Iterate over the history messages.
959
+ * @param clientId - The client ID.
960
+ * @param agentName - The agent name.
961
+ * @returns An async iterable iterator of model messages.
913
962
  */
914
- get(itemId: IStorageData["id"]): Promise<T | null>;
963
+ iterate(clientId: string, agentName: AgentName): AsyncIterableIterator<IModelMessage>;
915
964
  /**
916
- * Function to list items from the storage.
917
- * @param filter - Optional filter function.
918
- * @returns A promise that resolves to an array of storage data.
965
+ * Push a new message to the history.
966
+ * @param value - The model message to push.
967
+ * @param clientId - The client ID.
968
+ * @param agentName - The agent name.
969
+ * @returns A promise that resolves when the message is pushed.
919
970
  */
920
- list(filter?: (item: T) => boolean): Promise<T[]>;
971
+ push: (value: IModelMessage, clientId: string, agentName: AgentName) => Promise<void>;
921
972
  /**
922
- * Function to clear the storage.
923
- * @returns A promise that resolves when the operation is complete.
973
+ * Dispose of the history for a given client and agent.
974
+ * @param clientId - The client ID.
975
+ * @param agentName - The agent name or null.
976
+ * @returns A promise that resolves when the history is disposed.
924
977
  */
925
- clear(): Promise<void>;
978
+ dispose: (clientId: string, agentName: AgentName | null) => Promise<void>;
926
979
  }
927
980
  /**
928
- * Type representing the name of the storage.
981
+ * Exported History Adapter instance
929
982
  */
930
- type StorageName = string;
931
-
932
- type IStateData = any;
983
+ declare const HistoryAdapter: HistoryUtils;
933
984
  /**
934
- * Middleware function for state management.
935
- * @template T - The type of the state data.
936
- * @param {T} state - The current state.
937
- * @param {string} clientId - The client ID.
938
- * @param {StateName} stateName - The name of the state.
939
- * @returns {Promise<T>} - The updated state.
985
+ * Exported History Control instance
940
986
  */
941
- interface IStateMiddleware<T extends IStateData = IStateData> {
942
- (state: T, clientId: string, stateName: StateName): Promise<T>;
943
- }
987
+ declare const History: IHistoryControl;
988
+
944
989
  /**
945
- * Callbacks for state lifecycle events.
946
- * @template T - The type of the state data.
990
+ * Interface representing the history of model messages.
947
991
  */
948
- interface IStateCallbacks<T extends IStateData = IStateData> {
992
+ interface IHistory {
949
993
  /**
950
- * Called when the state is initialized.
951
- * @param {string} clientId - The client ID.
952
- * @param {StateName} stateName - The name of the state.
994
+ * Pushes a message to the history.
995
+ * @param {IModelMessage} message - The message to push.
996
+ * @returns {Promise<void>}
953
997
  */
954
- onInit: (clientId: string, stateName: StateName) => void;
998
+ push(message: IModelMessage): Promise<void>;
955
999
  /**
956
- * Called when the state is disposed.
957
- * @param {string} clientId - The client ID.
958
- * @param {StateName} stateName - The name of the state.
1000
+ * Converts the history to an array of messages for a specific agent.
1001
+ * @param {string} prompt - The prompt to filter messages for the agent.
1002
+ * @returns {Promise<IModelMessage[]>}
959
1003
  */
960
- onDispose: (clientId: string, stateName: StateName) => void;
1004
+ toArrayForAgent(prompt: string, system?: string[]): Promise<IModelMessage[]>;
961
1005
  /**
962
- * Called when the state is loaded.
963
- * @param {T} state - The current state.
964
- * @param {string} clientId - The client ID.
965
- * @param {StateName} stateName - The name of the state.
1006
+ * Converts the history to an array of raw messages.
1007
+ * @returns {Promise<IModelMessage[]>}
966
1008
  */
967
- onLoad: (state: T, clientId: string, stateName: StateName) => void;
1009
+ toArrayForRaw(): Promise<IModelMessage[]>;
1010
+ }
1011
+ /**
1012
+ * Interface representing the parameters required to create a history instance.
1013
+ */
1014
+ interface IHistoryParams extends IHistorySchema {
968
1015
  /**
969
- * Called when the state is read.
970
- * @param {T} state - The current state.
971
- * @param {string} clientId - The client ID.
972
- * @param {StateName} stateName - The name of the state.
1016
+ * The name of the agent.
1017
+ * @type {AgentName}
973
1018
  */
974
- onRead: (state: T, clientId: string, stateName: StateName) => void;
1019
+ agentName: AgentName;
975
1020
  /**
976
- * Called when the state is written.
977
- * @param {T} state - The current state.
978
- * @param {string} clientId - The client ID.
979
- * @param {StateName} stateName - The name of the state.
1021
+ * The client ID.
1022
+ * @type {string}
980
1023
  */
981
- onWrite: (state: T, clientId: string, stateName: StateName) => void;
1024
+ clientId: string;
1025
+ /**
1026
+ * The logger instance.
1027
+ * @type {ILogger}
1028
+ */
1029
+ logger: ILogger;
1030
+ /** The bus instance. */
1031
+ bus: IBus;
982
1032
  }
983
1033
  /**
984
- * Schema for state management.
985
- * @template T - The type of the state data.
1034
+ * Interface representing the schema of the history.
986
1035
  */
987
- interface IStateSchema<T extends IStateData = IStateData> {
1036
+ interface IHistorySchema {
988
1037
  /**
989
- * The agents can share the state
1038
+ * The adapter for array of model messages.
1039
+ * @type {IHistoryAdapter}
990
1040
  */
991
- shared?: boolean;
1041
+ items: IHistoryAdapter;
1042
+ }
1043
+
1044
+ /**
1045
+ * Interface representing a completion.
1046
+ */
1047
+ interface ICompletion extends ICompletionSchema {
1048
+ }
1049
+ /**
1050
+ * Arguments required to get a completion.
1051
+ */
1052
+ interface ICompletionArgs {
992
1053
  /**
993
- * The name of the state.
1054
+ * Client ID.
994
1055
  */
995
- stateName: StateName;
1056
+ clientId: string;
996
1057
  /**
997
- * Gets the state.
998
- * @param {string} clientId - The client ID.
999
- * @param {StateName} stateName - The name of the state.
1000
- * @returns {T | Promise<T>} - The current state.
1058
+ * Name of the agent.
1001
1059
  */
1002
- getState: (clientId: string, stateName: StateName) => T | Promise<T>;
1060
+ agentName: AgentName;
1003
1061
  /**
1004
- * Sets the state.
1005
- * @param {T} state - The new state.
1006
- * @param {string} clientId - The client ID.
1007
- * @param {StateName} stateName - The name of the state.
1008
- * @returns {Promise<void> | void} - A promise that resolves when the state is set.
1062
+ * The source of the last message: tool or user
1009
1063
  */
1010
- setState?: (state: T, clientId: string, stateName: StateName) => Promise<void> | void;
1064
+ mode: ExecutionMode;
1011
1065
  /**
1012
- * Middleware functions for state management.
1066
+ * Array of model messages.
1013
1067
  */
1014
- middlewares?: IStateMiddleware<T>[];
1068
+ messages: IModelMessage[];
1015
1069
  /**
1016
- * Callbacks for state lifecycle events.
1070
+ * Optional array of tools.
1017
1071
  */
1018
- callbacks?: Partial<IStateCallbacks<T>>;
1072
+ tools?: ITool[];
1019
1073
  }
1020
1074
  /**
1021
- * Parameters for state management.
1022
- * @template T - The type of the state data.
1075
+ * Completion lifecycle callbacks
1023
1076
  */
1024
- interface IStateParams<T extends IStateData = IStateData> extends IStateSchema<T> {
1025
- /**
1026
- * The client ID.
1027
- */
1028
- clientId: string;
1077
+ interface ICompletionCallbacks {
1029
1078
  /**
1030
- * The logger instance.
1079
+ * Callback fired after complete.
1080
+ * @param args - Arguments passed to complete
1081
+ * @param output - Output of the model
1031
1082
  */
1032
- logger: ILogger;
1083
+ onComplete?: (args: ICompletionArgs, output: IModelMessage) => void;
1033
1084
  }
1034
1085
  /**
1035
- * State management interface.
1036
- * @template T - The type of the state data.
1086
+ * Schema for a completion.
1037
1087
  */
1038
- interface IState<T extends IStateData = IStateData> {
1088
+ interface ICompletionSchema {
1039
1089
  /**
1040
- * Gets the state.
1041
- * @returns {Promise<T>} - The current state.
1090
+ * Name of the completion.
1042
1091
  */
1043
- getState: () => Promise<T>;
1092
+ completionName: CompletionName;
1044
1093
  /**
1045
- * Sets the state.
1046
- * @param {(prevState: T) => Promise<T>} dispatchFn - The function to dispatch the new state.
1047
- * @returns {Promise<T>} - The updated state.
1094
+ * Method to get a completion.
1095
+ * @param args - Arguments required to get a completion.
1096
+ * @returns A promise that resolves to a model message.
1048
1097
  */
1049
- setState: (dispatchFn: (prevState: T) => Promise<T>) => Promise<T>;
1098
+ getCompletion(args: ICompletionArgs): Promise<IModelMessage>;
1099
+ /**
1100
+ * Completion lifecycle callbacks
1101
+ */
1102
+ callbacks?: Partial<ICompletionCallbacks>;
1050
1103
  }
1051
1104
  /**
1052
- * The name of the state.
1105
+ * Type representing the name of a completion.
1053
1106
  */
1054
- type StateName = string;
1107
+ type CompletionName = string;
1055
1108
 
1056
1109
  /**
1057
1110
  * Interface representing lifecycle callbacks of a tool
@@ -1144,6 +1197,8 @@ interface IAgentParams extends Omit<IAgentSchema, keyof {
1144
1197
  clientId: string;
1145
1198
  /** The logger instance. */
1146
1199
  logger: ILogger;
1200
+ /** The bus instance. */
1201
+ bus: IBus;
1147
1202
  /** The history instance. */
1148
1203
  history: IHistory;
1149
1204
  /** The completion instance. */
@@ -1445,6 +1500,7 @@ declare class ClientAgent implements IAgent {
1445
1500
  */
1446
1501
  declare class AgentConnectionService implements IAgent {
1447
1502
  private readonly loggerService;
1503
+ private readonly busService;
1448
1504
  private readonly contextService;
1449
1505
  private readonly sessionValidationService;
1450
1506
  private readonly historyConnectionService;
@@ -1552,6 +1608,7 @@ declare class ClientHistory implements IHistory {
1552
1608
  */
1553
1609
  declare class HistoryConnectionService implements IHistory {
1554
1610
  private readonly loggerService;
1611
+ private readonly busService;
1555
1612
  private readonly contextService;
1556
1613
  private readonly sessionValidationService;
1557
1614
  /**
@@ -1632,16 +1689,21 @@ declare class ClientSwarm implements ISwarm {
1632
1689
  readonly params: ISwarmParams;
1633
1690
  private _agentChangedSubject;
1634
1691
  private _activeAgent;
1692
+ private _cancelOutputSubject;
1635
1693
  get _agentList(): [string, IAgent][];
1636
1694
  /**
1637
1695
  * Creates an instance of ClientSwarm.
1638
1696
  * @param {ISwarmParams} params - The parameters for the swarm.
1639
1697
  */
1640
1698
  constructor(params: ISwarmParams);
1699
+ /**
1700
+ * Cancel the await of output by emit of empty string
1701
+ * @returns {Promise<string>} - The output from the active agent.
1702
+ */
1703
+ cancelOutput: () => Promise<void>;
1641
1704
  /**
1642
1705
  * Waits for output from the active agent.
1643
1706
  * @returns {Promise<string>} - The output from the active agent.
1644
- * @throws {Error} - If the timeout is reached.
1645
1707
  */
1646
1708
  waitForOutput: () => Promise<string>;
1647
1709
  /**
@@ -1674,6 +1736,7 @@ declare class ClientSwarm implements ISwarm {
1674
1736
  */
1675
1737
  declare class SwarmConnectionService implements ISwarm {
1676
1738
  private readonly loggerService;
1739
+ private readonly busService;
1677
1740
  private readonly contextService;
1678
1741
  private readonly agentConnectionService;
1679
1742
  private readonly swarmSchemaService;
@@ -1684,6 +1747,11 @@ declare class SwarmConnectionService implements ISwarm {
1684
1747
  * @returns {ClientSwarm} The client swarm instance.
1685
1748
  */
1686
1749
  getSwarm: ((clientId: string, swarmName: string) => ClientSwarm) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientSwarm>;
1750
+ /**
1751
+ * Cancel the await of output by emit of empty string
1752
+ * @returns {Promise<void>}
1753
+ */
1754
+ cancelOutput: () => Promise<void>;
1687
1755
  /**
1688
1756
  * Waits for the output from the swarm.
1689
1757
  * @returns {Promise<any>} The output from the swarm.
@@ -1826,6 +1894,7 @@ declare class ClientSession implements ISession {
1826
1894
  */
1827
1895
  declare class SessionConnectionService implements ISession {
1828
1896
  private readonly loggerService;
1897
+ private readonly busService;
1829
1898
  private readonly contextService;
1830
1899
  private readonly swarmConnectionService;
1831
1900
  private readonly swarmSchemaService;
@@ -2110,6 +2179,13 @@ type TSwarmConnectionService = {
2110
2179
  declare class SwarmPublicService implements TSwarmConnectionService {
2111
2180
  private readonly loggerService;
2112
2181
  private readonly swarmConnectionService;
2182
+ /**
2183
+ * Cancel the await of output by emit of empty string
2184
+ * @param {string} clientId - The client ID.
2185
+ * @param {SwarmName} swarmName - The swarm name.
2186
+ * @returns {Promise<void>}
2187
+ */
2188
+ cancelOutput: (clientId: string, swarmName: SwarmName) => Promise<void>;
2113
2189
  /**
2114
2190
  * Waits for output from the swarm.
2115
2191
  * @param {string} clientId - The client ID.
@@ -2511,6 +2587,7 @@ declare class ClientStorage<T extends IStorageData = IStorageData> implements IS
2511
2587
  */
2512
2588
  declare class StorageConnectionService implements IStorage {
2513
2589
  private readonly loggerService;
2590
+ private readonly busService;
2514
2591
  private readonly contextService;
2515
2592
  private readonly storageSchemaService;
2516
2593
  private readonly sessionValidationService;
@@ -2722,6 +2799,7 @@ declare class ClientState<State extends IStateData = IStateData> implements ISta
2722
2799
  */
2723
2800
  declare class StateConnectionService<T extends IStateData = IStateData> implements IState<T> {
2724
2801
  private readonly loggerService;
2802
+ private readonly busService;
2725
2803
  private readonly contextService;
2726
2804
  private readonly stateSchemaService;
2727
2805
  private readonly sessionValidationService;
@@ -2813,6 +2891,41 @@ declare class StateSchemaService {
2813
2891
  get: (key: StateName) => IStateSchema;
2814
2892
  }
2815
2893
 
2894
+ declare class BusService implements IBus {
2895
+ private readonly loggerService;
2896
+ private _eventSourceSet;
2897
+ private getEventSubject;
2898
+ /**
2899
+ * Subscribes to events for a specific client and source.
2900
+ * @param {string} clientId - The client ID.
2901
+ * @param {EventSource} source - The event source.
2902
+ * @param {(event: T) => void} fn - The callback function to handle the event.
2903
+ * @returns {Subscription} The subscription object.
2904
+ */
2905
+ subscribe: <T extends IBaseEvent>(clientId: string, source: EventSource, fn: (event: T) => void) => () => void;
2906
+ /**
2907
+ * Subscribes to a single event for a specific client and source.
2908
+ * @param {string} clientId - The client ID.
2909
+ * @param {EventSource} source - The event source.
2910
+ * @param {(event: T) => boolean} filterFn - The filter function to determine if the event should be handled.
2911
+ * @param {(event: T) => void} fn - The callback function to handle the event.
2912
+ * @returns {Subscription} The subscription object.
2913
+ */
2914
+ once: <T extends IBaseEvent>(clientId: string, source: EventSource, filterFn: (event: T) => boolean, fn: (event: T) => void) => () => void;
2915
+ /**
2916
+ * Emits an event for a specific client.
2917
+ * @param {string} clientId - The client ID.
2918
+ * @param {T} event - The event to emit.
2919
+ * @returns {Promise<void>} A promise that resolves when the event has been emitted.
2920
+ */
2921
+ emit: <T extends IBaseEvent>(clientId: string, event: T) => Promise<void>;
2922
+ /**
2923
+ * Disposes of all event subscriptions for a specific client.
2924
+ * @param {string} clientId - The client ID.
2925
+ */
2926
+ dispose: (clientId: string) => void;
2927
+ }
2928
+
2816
2929
  declare const swarm: {
2817
2930
  agentValidationService: AgentValidationService;
2818
2931
  toolValidationService: ToolValidationService;
@@ -2840,6 +2953,7 @@ declare const swarm: {
2840
2953
  sessionConnectionService: SessionConnectionService;
2841
2954
  storageConnectionService: StorageConnectionService;
2842
2955
  stateConnectionService: StateConnectionService<any>;
2956
+ busService: BusService;
2843
2957
  loggerService: LoggerService;
2844
2958
  contextService: {
2845
2959
  readonly context: IContext;
@@ -3174,6 +3288,16 @@ declare const emitForce: (content: string, clientId: string) => Promise<void>;
3174
3288
  */
3175
3289
  declare const executeForce: (content: string, clientId: string) => Promise<string>;
3176
3290
 
3291
+ /**
3292
+ * Listens for an event on the swarm bus service and executes a callback function when the event is received.
3293
+ *
3294
+ * @template T - The type of the data payload.
3295
+ * @param {string} clientId - The ID of the client to listen for events from.
3296
+ * @param {(data: T) => void} fn - The callback function to execute when the event is received. The data payload is passed as an argument to this function.
3297
+ * @returns {void} - Returns nothing.
3298
+ */
3299
+ declare const listenEvent: <T extends unknown = any>(clientId: string, topicName: string, fn: (data: T) => void) => () => void;
3300
+
3177
3301
  /**
3178
3302
  * Retrieves the last message sent by the user from the client's message history.
3179
3303
  *
@@ -3255,6 +3379,88 @@ declare const makeAutoDispose: (clientId: string, swarmName: SwarmName, { timeou
3255
3379
  destroy(): void;
3256
3380
  };
3257
3381
 
3382
+ /**
3383
+ * Emits an event to the swarm bus service.
3384
+ *
3385
+ * @template T - The type of the payload.
3386
+ * @param {string} clientId - The ID of the client emitting the event.
3387
+ * @param {T} payload - The payload of the event.
3388
+ * @returns {boolean} - Returns true if the event was successfully emitted.
3389
+ */
3390
+ declare const event: <T extends unknown = any>(clientId: string, topicName: string, payload: T) => Promise<void>;
3391
+
3392
+ /**
3393
+ * Cancel the await of output by emit of empty string
3394
+ *
3395
+ * @param {string} clientId - The ID of the client.
3396
+ * @param {string} agentName - The name of the agent.
3397
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
3398
+ */
3399
+ declare const cancelOutput: (clientId: string, agentName: string) => Promise<void>;
3400
+
3401
+ /**
3402
+ * Cancel the await of output by emit of empty string without checking active agent
3403
+ *
3404
+ * @param {string} clientId - The ID of the client.
3405
+ * @param {string} agentName - The name of the agent.
3406
+ * @returns {Promise<void>} - A promise that resolves when the output is canceled
3407
+ */
3408
+ declare const cancelOutputForce: (clientId: string) => Promise<void>;
3409
+
3410
+ /**
3411
+ * Hook to subscribe to agent events for a specific client.
3412
+ *
3413
+ * @param {string} clientId - The ID of the client to subscribe to events for.
3414
+ * @param {function} fn - The callback function to handle the event.
3415
+ * @returns {function} - A function to unsubscribe from the event.
3416
+ */
3417
+ declare const listenAgentEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3418
+
3419
+ /**
3420
+ * Hook to subscribe to history events for a specific client.
3421
+ *
3422
+ * @param {string} clientId - The ID of the client to subscribe to.
3423
+ * @param {(event: IBaseEvent) => void} fn - The callback function to handle the event.
3424
+ * @returns {Function} - The unsubscribe function.
3425
+ */
3426
+ declare const listenHistoryEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3427
+
3428
+ /**
3429
+ * Hook to subscribe to session events for a specific client.
3430
+ *
3431
+ * @param {string} clientId - The ID of the client to subscribe to session events for.
3432
+ * @param {function} fn - The callback function to handle the session events.
3433
+ * @returns {function} - The unsubscribe function to stop listening to session events.
3434
+ */
3435
+ declare const listenSessionEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3436
+
3437
+ /**
3438
+ * Hook to subscribe to state events for a specific client.
3439
+ *
3440
+ * @param {string} clientId - The ID of the client to subscribe to.
3441
+ * @param {function} fn - The callback function to handle the event.
3442
+ * @returns {function} - The unsubscribe function to stop listening to the events.
3443
+ */
3444
+ declare const listenStateEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3445
+
3446
+ /**
3447
+ * Hook to subscribe to storage events for a specific client.
3448
+ *
3449
+ * @param {string} clientId - The ID of the client to subscribe to storage events for.
3450
+ * @param {function} fn - The callback function to handle the storage event.
3451
+ * @returns {function} - A function to unsubscribe from the storage events.
3452
+ */
3453
+ declare const listenStorageEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3454
+
3455
+ /**
3456
+ * Hook to subscribe to swarm events for a specific client.
3457
+ *
3458
+ * @param {string} clientId - The ID of the client to subscribe to events for.
3459
+ * @param {(event: IBaseEvent) => void} fn - The callback function to handle the event.
3460
+ * @returns {Function} - A function to unsubscribe from the event.
3461
+ */
3462
+ declare const listenSwarmEvent: (clientId: string, fn: (event: IBaseEvent) => void) => () => void;
3463
+
3258
3464
  declare const GLOBAL_CONFIG: {
3259
3465
  CC_TOOL_CALL_EXCEPTION_PROMPT: string;
3260
3466
  CC_EMPTY_OUTPUT_PLACEHOLDERS: string[];
@@ -3416,4 +3622,4 @@ declare class StateUtils implements TState {
3416
3622
  */
3417
3623
  declare const State: StateUtils;
3418
3624
 
3419
- export { ContextService, History, HistoryAdapter, HistoryInstance, type IAgentSchema, type IAgentTool, type ICompletionArgs, type ICompletionSchema, type IEmbeddingSchema, type IHistoryAdapter, type IHistoryInstance, type IHistoryInstanceCallbacks, type IIncomingMessage, type IMakeConnectionConfig, type IMakeDisposeParams, type IModelMessage, type IOutgoingMessage, type ISessionConfig, type IStateSchema, type IStorageSchema, type ISwarmSchema, type ITool, type IToolCall, type ReceiveMessageFn, type SendMessageFn$1 as SendMessageFn, State, Storage, addAgent, addCompletion, addEmbedding, addState, addStorage, addSwarm, addTool, changeAgent, commitFlush, commitFlushForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, emit, emitForce, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getRawHistory, getSessionMode, getUserHistory, makeAutoDispose, makeConnection, session, setConfig, swarm };
3625
+ export { ContextService, type EventSource, History, HistoryAdapter, HistoryInstance, type IAgentSchema, type IAgentTool, type IBaseEvent, type IBaseEventContext, type ICompletionArgs, type ICompletionSchema, type IEmbeddingSchema, type IHistoryAdapter, type IHistoryInstance, type IHistoryInstanceCallbacks, type IIncomingMessage, type IMakeConnectionConfig, type IMakeDisposeParams, type IModelMessage, type IOutgoingMessage, type ISessionConfig, type IStateSchema, type IStorageSchema, type ISwarmSchema, type ITool, type IToolCall, type ReceiveMessageFn, type SendMessageFn$1 as SendMessageFn, State, Storage, addAgent, addCompletion, addEmbedding, addState, addStorage, addSwarm, addTool, cancelOutput, cancelOutputForce, changeAgent, commitFlush, commitFlushForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getRawHistory, getSessionMode, getUserHistory, listenAgentEvent, listenEvent, listenHistoryEvent, listenSessionEvent, listenStateEvent, listenStorageEvent, listenSwarmEvent, makeAutoDispose, makeConnection, session, setConfig, swarm };