agent-swarm-kit 1.0.233 → 1.0.235

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,7 +26,7 @@ Unleash the power of collaborative AI with `agent-swarm-kit`! This library empow
26
26
 
27
27
  ### First Look
28
28
 
29
- Read the [briefing article by the link](https://agent-swarm.github.io/documents/app_getting-started.html)
29
+ Read the [detailed instructions by the link](https://agent-swarm.github.io/documents/app_getting-started.html)
30
30
 
31
31
  ### Installation
32
32
 
package/build/index.cjs CHANGED
@@ -1085,7 +1085,7 @@ class PersistSwarmUtils {
1085
1085
  clientId,
1086
1086
  swarmName,
1087
1087
  });
1088
- const isInitial = this.getActiveAgentStorage.has(swarmName);
1088
+ const isInitial = !this.getActiveAgentStorage.has(swarmName);
1089
1089
  const activeAgentStorage = this.getActiveAgentStorage(swarmName);
1090
1090
  await activeAgentStorage.waitForInit(isInitial);
1091
1091
  if (await activeAgentStorage.hasValue(clientId)) {
@@ -1110,7 +1110,7 @@ class PersistSwarmUtils {
1110
1110
  agentName,
1111
1111
  swarmName,
1112
1112
  });
1113
- const isInitial = this.getActiveAgentStorage.has(swarmName);
1113
+ const isInitial = !this.getActiveAgentStorage.has(swarmName);
1114
1114
  const activeAgentStorage = this.getActiveAgentStorage(swarmName);
1115
1115
  await activeAgentStorage.waitForInit(isInitial);
1116
1116
  await activeAgentStorage.writeValue(clientId, { agentName });
@@ -1129,7 +1129,7 @@ class PersistSwarmUtils {
1129
1129
  clientId,
1130
1130
  swarmName,
1131
1131
  });
1132
- const isInitial = this.getNavigationStackStorage.has(swarmName);
1132
+ const isInitial = !this.getNavigationStackStorage.has(swarmName);
1133
1133
  const navigationStackStorage = this.getNavigationStackStorage(swarmName);
1134
1134
  await navigationStackStorage.waitForInit(isInitial);
1135
1135
  if (await navigationStackStorage.hasValue(clientId)) {
@@ -1153,7 +1153,7 @@ class PersistSwarmUtils {
1153
1153
  clientId,
1154
1154
  swarmName,
1155
1155
  });
1156
- const isInitial = this.getNavigationStackStorage.has(swarmName);
1156
+ const isInitial = !this.getNavigationStackStorage.has(swarmName);
1157
1157
  const navigationStackStorage = this.getNavigationStackStorage(swarmName);
1158
1158
  await navigationStackStorage.waitForInit(isInitial);
1159
1159
  await navigationStackStorage.writeValue(clientId, { agentStack });
@@ -1227,7 +1227,7 @@ class PersistStateUtils {
1227
1227
  clientId,
1228
1228
  stateName,
1229
1229
  });
1230
- const isInitial = this.getStateStorage.has(stateName);
1230
+ const isInitial = !this.getStateStorage.has(stateName);
1231
1231
  const stateStorage = this.getStateStorage(stateName);
1232
1232
  await stateStorage.waitForInit(isInitial);
1233
1233
  await stateStorage.writeValue(clientId, { state });
@@ -1248,7 +1248,7 @@ class PersistStateUtils {
1248
1248
  clientId,
1249
1249
  stateName,
1250
1250
  });
1251
- const isInitial = this.getStateStorage.has(stateName);
1251
+ const isInitial = !this.getStateStorage.has(stateName);
1252
1252
  const stateStorage = this.getStateStorage(stateName);
1253
1253
  await stateStorage.waitForInit(isInitial);
1254
1254
  if (await stateStorage.hasValue(clientId)) {
@@ -1316,7 +1316,7 @@ class PersistStorageUtils {
1316
1316
  clientId,
1317
1317
  storageName,
1318
1318
  });
1319
- const isInitial = this.getPersistStorage.has(storageName);
1319
+ const isInitial = !this.getPersistStorage.has(storageName);
1320
1320
  const persistStorage = this.getPersistStorage(storageName);
1321
1321
  await persistStorage.waitForInit(isInitial);
1322
1322
  if (await persistStorage.hasValue(clientId)) {
@@ -1341,7 +1341,7 @@ class PersistStorageUtils {
1341
1341
  clientId,
1342
1342
  storageName,
1343
1343
  });
1344
- const isInitial = this.getPersistStorage.has(storageName);
1344
+ const isInitial = !this.getPersistStorage.has(storageName);
1345
1345
  const persistStorage = this.getPersistStorage(storageName);
1346
1346
  await persistStorage.waitForInit(isInitial);
1347
1347
  await persistStorage.writeValue(clientId, { data });
@@ -1403,7 +1403,7 @@ class PersistMemoryUtils {
1403
1403
  swarm$1.loggerService.log(PERSIST_MEMORY_UTILS_METHOD_NAME_SET_MEMORY, {
1404
1404
  clientId,
1405
1405
  });
1406
- const isInitial = this.getMemoryStorage.has(clientId);
1406
+ const isInitial = !this.getMemoryStorage.has(clientId);
1407
1407
  const stateStorage = this.getMemoryStorage(clientId);
1408
1408
  await stateStorage.waitForInit(isInitial);
1409
1409
  await stateStorage.writeValue(clientId, { data });
@@ -1422,7 +1422,7 @@ class PersistMemoryUtils {
1422
1422
  swarm$1.loggerService.log(PERSIST_MEMORY_UTILS_METHOD_NAME_GET_MEMORY, {
1423
1423
  clientId,
1424
1424
  });
1425
- const isInitial = this.getMemoryStorage.has(clientId);
1425
+ const isInitial = !this.getMemoryStorage.has(clientId);
1426
1426
  const stateStorage = this.getMemoryStorage(clientId);
1427
1427
  await stateStorage.waitForInit(isInitial);
1428
1428
  if (await stateStorage.hasValue(clientId)) {
@@ -1508,7 +1508,7 @@ class PersistAliveUtils {
1508
1508
  swarm$1.loggerService.log(PERSIST_ALIVE_UTILS_METHOD_NAME_MARK_ONLINE, {
1509
1509
  clientId,
1510
1510
  });
1511
- const isInitial = this.getAliveStorage.has(swarmName);
1511
+ const isInitial = !this.getAliveStorage.has(swarmName);
1512
1512
  const stateStorage = this.getAliveStorage(swarmName);
1513
1513
  await stateStorage.waitForInit(isInitial);
1514
1514
  await stateStorage.writeValue(clientId, { online: true });
@@ -1525,7 +1525,7 @@ class PersistAliveUtils {
1525
1525
  swarm$1.loggerService.log(PERSIST_ALIVE_UTILS_METHOD_NAME_MARK_OFFLINE, {
1526
1526
  clientId,
1527
1527
  });
1528
- const isInitial = this.getAliveStorage.has(swarmName);
1528
+ const isInitial = !this.getAliveStorage.has(swarmName);
1529
1529
  const stateStorage = this.getAliveStorage(swarmName);
1530
1530
  await stateStorage.waitForInit(isInitial);
1531
1531
  await stateStorage.writeValue(clientId, { online: false });
@@ -1542,7 +1542,7 @@ class PersistAliveUtils {
1542
1542
  swarm$1.loggerService.log(PERSIST_ALIVE_UTILS_METHOD_NAME_GET_ONLINE, {
1543
1543
  clientId,
1544
1544
  });
1545
- const isInitial = this.getAliveStorage.has(swarmName);
1545
+ const isInitial = !this.getAliveStorage.has(swarmName);
1546
1546
  const stateStorage = this.getAliveStorage(swarmName);
1547
1547
  await stateStorage.waitForInit(isInitial);
1548
1548
  if (await stateStorage.hasValue(clientId)) {
@@ -1606,7 +1606,7 @@ class PersistPolicyUtils {
1606
1606
  this.getBannedClients = async (policyName, swarmName, defaultValue = []) => {
1607
1607
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1608
1608
  swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_GET_BANNED_CLIENTS);
1609
- const isInitial = this.getPolicyStorage.has(swarmName);
1609
+ const isInitial = !this.getPolicyStorage.has(swarmName);
1610
1610
  const stateStorage = this.getPolicyStorage(swarmName);
1611
1611
  await stateStorage.waitForInit(isInitial);
1612
1612
  if (await stateStorage.hasValue(policyName)) {
@@ -1627,7 +1627,7 @@ class PersistPolicyUtils {
1627
1627
  this.setBannedClients = async (bannedClients, policyName, swarmName) => {
1628
1628
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1629
1629
  swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_SET_BANNED_CLIENTS);
1630
- const isInitial = this.getPolicyStorage.has(swarmName);
1630
+ const isInitial = !this.getPolicyStorage.has(swarmName);
1631
1631
  const stateStorage = this.getPolicyStorage(swarmName);
1632
1632
  await stateStorage.waitForInit(isInitial);
1633
1633
  await stateStorage.writeValue(policyName, { bannedClients });
@@ -1686,7 +1686,7 @@ class PersistEmbeddingUtils {
1686
1686
  this.readEmbeddingCache = async (embeddingName, stringHash) => {
1687
1687
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1688
1688
  swarm$1.loggerService.log(PERSIST_EMBEDDING_UTILS_METHOD_NAME_READ_EMBEDDING_CACHE);
1689
- const isInitial = this.getEmbeddingStorage.has(embeddingName);
1689
+ const isInitial = !this.getEmbeddingStorage.has(embeddingName);
1690
1690
  const stateStorage = this.getEmbeddingStorage(embeddingName);
1691
1691
  await stateStorage.waitForInit(isInitial);
1692
1692
  if (await stateStorage.hasValue(stringHash)) {
@@ -1707,7 +1707,7 @@ class PersistEmbeddingUtils {
1707
1707
  this.writeEmbeddingCache = async (embeddings, embeddingName, stringHash) => {
1708
1708
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1709
1709
  swarm$1.loggerService.log(PERSIST_EMBEDDING_UTILS_METHOD_NAME_WRITE_EMBEDDING_CACHE);
1710
- const isInitial = this.getEmbeddingStorage.has(embeddingName);
1710
+ const isInitial = !this.getEmbeddingStorage.has(embeddingName);
1711
1711
  const stateStorage = this.getEmbeddingStorage(embeddingName);
1712
1712
  await stateStorage.waitForInit(isInitial);
1713
1713
  await stateStorage.writeValue(stringHash, { embeddings });
@@ -2271,7 +2271,7 @@ class HistoryUtils {
2271
2271
  agentName,
2272
2272
  value,
2273
2273
  });
2274
- const isInitial = this.getHistory.has(clientId);
2274
+ const isInitial = !this.getHistory.has(clientId);
2275
2275
  const history = await this.getHistory(clientId);
2276
2276
  await history.waitForInit(agentName, isInitial);
2277
2277
  return await history.push(value, agentName);
@@ -2288,7 +2288,7 @@ class HistoryUtils {
2288
2288
  clientId,
2289
2289
  agentName,
2290
2290
  });
2291
- const isInitial = this.getHistory.has(clientId);
2291
+ const isInitial = !this.getHistory.has(clientId);
2292
2292
  const history = await this.getHistory(clientId);
2293
2293
  await history.waitForInit(agentName, isInitial);
2294
2294
  return await history.pop(agentName);
@@ -2328,7 +2328,7 @@ class HistoryUtils {
2328
2328
  clientId,
2329
2329
  agentName,
2330
2330
  });
2331
- const isInitial = this.getHistory.has(clientId);
2331
+ const isInitial = !this.getHistory.has(clientId);
2332
2332
  const history = await this.getHistory(clientId);
2333
2333
  await history.waitForInit(agentName, isInitial);
2334
2334
  for await (const item of history.iterate(agentName)) {
@@ -2759,6 +2759,208 @@ const LoggerAdapter = new LoggerUtils();
2759
2759
  */
2760
2760
  const Logger = LoggerAdapter;
2761
2761
 
2762
+ /** @private Constant for logging the constructor in OperatorInstance */
2763
+ const OPERATOR_INSTANCE_METHOD_NAME_CTOR = "OperatorInstance.CTOR";
2764
+ /** @private Constant for logging the connectAnswer method in OperatorInstance */
2765
+ const OPERATOR_INSTANCE_METHOD_NAME_CONNECT_ANSWER = "OperatorInstance.connectAnswer";
2766
+ /** @private Constant for logging the notify method in OperatorInstance */
2767
+ const OPERATOR_INSTANCE_METHOD_NAME_NOTIFY = "OperatorInstance.notify";
2768
+ /** @private Constant for logging the answer method in OperatorInstance */
2769
+ const OPERATOR_INSTANCE_METHOD_NAME_ANSWER = "OperatorInstance.answer";
2770
+ /** @private Constant for logging the recieveMessage method in OperatorInstance */
2771
+ const OPERATOR_INSTANCE_METHOD_NAME_RECEIVE_MESSAGE = "OperatorInstance.recieveMessage";
2772
+ /** @private Constant for logging the dispose method in OperatorInstance */
2773
+ const OPERATOR_INSTANCE_METHOD_NAME_DISPOSE = "OperatorInstance.dispose";
2774
+ /** @private Constant for logging the useOperatorAdapter method in OperatorUtils */
2775
+ const METHOD_NAME_USE_OPERATOR_ADAPTER = "OperatorUtils.useOperatorAdapter";
2776
+ /** @private Constant for logging the useOperatorCallbacks method in OperatorUtils */
2777
+ const METHOD_NAME_USE_OPERATOR_CALLBACKS = "OperatorUtils.useOperatorCallbacks";
2778
+ /** @private Constant for logging the connectOperator method in OperatorUtils */
2779
+ const METHOD_NAME_CONNECT_OPERATOR = "OperatorUtils.connectOperator";
2780
+ /**
2781
+ * Operator instance implementation
2782
+ * @class OperatorInstance
2783
+ * @implements {IOperatorInstance}
2784
+ */
2785
+ const OperatorInstance = functoolsKit.makeExtendable(class {
2786
+ /**
2787
+ * Disposed flag for child class
2788
+ */
2789
+ get isDisposed() {
2790
+ return this._isDisposed;
2791
+ }
2792
+ /**
2793
+ * Creates an OperatorInstance
2794
+ * @param {string} clientId - The client identifier
2795
+ * @param {AgentName} agentName - The agent name
2796
+ * @param {Partial<IOperatorInstanceCallbacks>} callbacks - Event callbacks
2797
+ */
2798
+ constructor(clientId, agentName, callbacks) {
2799
+ this.clientId = clientId;
2800
+ this.agentName = agentName;
2801
+ this.callbacks = callbacks;
2802
+ this._answerSubject = new functoolsKit.Subject();
2803
+ this._isDisposed = false;
2804
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2805
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_CTOR, {
2806
+ clientId: this.clientId,
2807
+ agentName,
2808
+ });
2809
+ if (this.callbacks.onInit) {
2810
+ this.callbacks.onInit(clientId, agentName);
2811
+ }
2812
+ }
2813
+ /**
2814
+ * Connects an answer subscription
2815
+ * @param {(answer: string) => void} next - Answer handler callback
2816
+ */
2817
+ connectAnswer(next) {
2818
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2819
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_CONNECT_ANSWER, {
2820
+ clientId: this.clientId,
2821
+ agentName: this.agentName,
2822
+ });
2823
+ this._answerSubject.unsubscribeAll();
2824
+ this._answerSubject.subscribe(next);
2825
+ }
2826
+ /**
2827
+ * Sends a notification
2828
+ * @param {string} content - Notification content
2829
+ * @returns {Promise<void>}
2830
+ */
2831
+ async notify(content) {
2832
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2833
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_NOTIFY, {
2834
+ clientId: this.clientId,
2835
+ agentName: this.agentName,
2836
+ content,
2837
+ });
2838
+ if (this._isDisposed) {
2839
+ return;
2840
+ }
2841
+ if (this.callbacks.onNotify) {
2842
+ this.callbacks.onNotify(content, this.clientId, this.agentName);
2843
+ }
2844
+ }
2845
+ /**
2846
+ * Sends an answer
2847
+ * @param {string} content - Answer content
2848
+ * @returns {Promise<void>}
2849
+ */
2850
+ async answer(content) {
2851
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2852
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_ANSWER, {
2853
+ clientId: this.clientId,
2854
+ agentName: this.agentName,
2855
+ content,
2856
+ });
2857
+ if (this._isDisposed) {
2858
+ return;
2859
+ }
2860
+ if (this._answerSubject.hasListeners) {
2861
+ this.callbacks.onAnswer &&
2862
+ this.callbacks.onAnswer(content, this.clientId, this.agentName);
2863
+ await this._answerSubject.next(content);
2864
+ }
2865
+ else {
2866
+ await this.notify(content);
2867
+ }
2868
+ this._answerSubject.unsubscribeAll();
2869
+ }
2870
+ /**
2871
+ * Receives a message
2872
+ * @param {string} message - Message content
2873
+ * @returns {Promise<void>}
2874
+ */
2875
+ async recieveMessage(message) {
2876
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2877
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_RECEIVE_MESSAGE, {
2878
+ clientId: this.clientId,
2879
+ agentName: this.agentName,
2880
+ message,
2881
+ });
2882
+ if (this.callbacks.onMessage) {
2883
+ this.callbacks.onMessage(message, this.clientId, this.agentName);
2884
+ }
2885
+ }
2886
+ /**
2887
+ * Disposes the operator instance
2888
+ * @returns {Promise<void>}
2889
+ */
2890
+ async dispose() {
2891
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2892
+ swarm$1.loggerService.debug(OPERATOR_INSTANCE_METHOD_NAME_DISPOSE, {
2893
+ clientId: this.clientId,
2894
+ agentName: this.agentName,
2895
+ });
2896
+ if (this.callbacks.onDispose) {
2897
+ this.callbacks.onDispose(this.clientId, this.agentName);
2898
+ }
2899
+ this._answerSubject.unsubscribeAll();
2900
+ this._isDisposed = true;
2901
+ }
2902
+ });
2903
+ /**
2904
+ * Operator utilities class
2905
+ * @class OperatorUtils
2906
+ * @implements {IOperatorControl}
2907
+ */
2908
+ class OperatorUtils {
2909
+ constructor() {
2910
+ this.OperatorFactory = OperatorInstance;
2911
+ this.OperatorCallbacks = {};
2912
+ this.getOperator = functoolsKit.memoize(([clientId, agentName]) => `${clientId}-${agentName}`, (clientId, agentName) => Reflect.construct(this.OperatorFactory, [
2913
+ clientId,
2914
+ agentName,
2915
+ this.OperatorCallbacks,
2916
+ ]));
2917
+ }
2918
+ /**
2919
+ * Sets custom operator adapter constructor
2920
+ * @param {TOperatorInstanceCtor} Ctor - Operator constructor
2921
+ */
2922
+ useOperatorAdapter(Ctor) {
2923
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
2924
+ swarm$1.loggerService.log(METHOD_NAME_USE_OPERATOR_ADAPTER);
2925
+ this.OperatorFactory = Ctor;
2926
+ }
2927
+ /**
2928
+ * Sets operator callbacks
2929
+ * @param {Partial<IOperatorInstanceCallbacks>} Callbacks - Callback functions
2930
+ */
2931
+ useOperatorCallbacks(Callbacks) {
2932
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
2933
+ swarm$1.loggerService.log(METHOD_NAME_USE_OPERATOR_CALLBACKS);
2934
+ Object.assign(this.OperatorCallbacks, Callbacks);
2935
+ }
2936
+ /**
2937
+ * Connects an operator instance
2938
+ * @param {string} clientId - Client identifier
2939
+ * @param {AgentName} agentName - Agent name
2940
+ * @returns {(message: string, next: (answer: string) => void) => DisposeFn} Connection function
2941
+ */
2942
+ connectOperator(clientId, agentName) {
2943
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
2944
+ swarm$1.loggerService.log(METHOD_NAME_CONNECT_OPERATOR, {
2945
+ clientId,
2946
+ agentName,
2947
+ });
2948
+ const operator = this.getOperator(clientId, agentName);
2949
+ return (message, next) => {
2950
+ operator.connectAnswer(next);
2951
+ operator.recieveMessage(message);
2952
+ return async () => {
2953
+ this.getOperator.clear(`${clientId}-${agentName}`);
2954
+ await operator.dispose();
2955
+ };
2956
+ };
2957
+ }
2958
+ }
2959
+ /** @type {OperatorUtils} Operator adapter instance */
2960
+ const OperatorAdapter = new OperatorUtils();
2961
+ /** @type {IOperatorControl} Operator control instance */
2962
+ const Operator = OperatorAdapter;
2963
+
2762
2964
  const CC_THROW_WHEN_NAVIGATION_RECURSION = true;
2763
2965
  const CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT = "Start the conversation";
2764
2966
  const CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT = functoolsKit.str.newline("Please analyze the last tool call message and identify any errors in its syntax or parameters.", "Then, provide a corrected version of the tool call that properly follows the required format and includes all necessary parameters with appropriate values.", "Include a brief explanation of what was fixed as a text content of a new message with correct tool calls request");
@@ -2847,6 +3049,16 @@ const CC_DEFAULT_READ_EMBEDDING_CACHE = (embeddingName, stringHash) => Promise.r
2847
3049
  * @throws {Error} If writing to storage fails (e.g., permissions or disk space).
2848
3050
  */
2849
3051
  const CC_DEFAULT_WRITE_EMBEDDING_CACHE = (embeddings, embeddingName, stringHash) => Promise.resolve();
3052
+ /**
3053
+ * Default function to connect an operator for handling messages and responses.
3054
+ * Establishes a connection between a client and an agent, allowing messages to be sent
3055
+ * and answers to be received via a callback mechanism.
3056
+ *
3057
+ * @param {string} clientId - The unique identifier of the client initiating the connection.
3058
+ * @param {AgentName} agentName - The name of the agent to connect with.
3059
+ * @returns {(message: string, next: (answer: string) => void) => DisposeFn}
3060
+ */
3061
+ const CC_DEFAULT_CONNECT_OPERATOR = (clientId, agentName) => OperatorAdapter.connectOperator(clientId, agentName);
2850
3062
  const GLOBAL_CONFIG = {
2851
3063
  CC_TOOL_CALL_EXCEPTION_FLUSH_PROMPT,
2852
3064
  CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
@@ -2895,6 +3107,7 @@ const GLOBAL_CONFIG = {
2895
3107
  CC_PERSIST_EMBEDDING_CACHE,
2896
3108
  CC_DEFAULT_AGENT_TOOL_VALIDATE,
2897
3109
  CC_THROW_WHEN_NAVIGATION_RECURSION,
3110
+ CC_DEFAULT_CONNECT_OPERATOR,
2898
3111
  };
2899
3112
  GLOBAL_CONFIG.CC_RESQUE_STRATEGY = "flush";
2900
3113
  /**
@@ -3130,10 +3343,10 @@ class AgentSchemaService {
3130
3343
  if (typeof agentSchema.agentName !== "string") {
3131
3344
  throw new Error(`agent-swarm agent schema validation failed: missing agentName`);
3132
3345
  }
3133
- if (typeof agentSchema.completion !== "string") {
3346
+ if (!agentSchema.operator && typeof agentSchema.completion !== "string") {
3134
3347
  throw new Error(`agent-swarm agent schema validation failed: missing completion for agentName=${agentSchema.agentName}`);
3135
3348
  }
3136
- if (typeof agentSchema.prompt !== "string") {
3349
+ if (!agentSchema.operator && typeof agentSchema.prompt !== "string") {
3137
3350
  throw new Error(`agent-swarm agent schema validation failed: missing prompt for agentName=${agentSchema.agentName}`);
3138
3351
  }
3139
3352
  if (agentSchema.system && !Array.isArray(agentSchema.system)) {
@@ -4108,6 +4321,201 @@ class ClientAgent {
4108
4321
  }
4109
4322
  }
4110
4323
 
4324
+ /** @private Timeout duration for operator signal in milliseconds */
4325
+ const OPERATOR_SIGNAL_TIMEOUT = 90000;
4326
+ /** @private Symbol representing operator timeout */
4327
+ const OPERATOR_SIGNAL_SYMBOL = Symbol("operator-timeout");
4328
+ /**
4329
+ * Manages operator signal communication
4330
+ * @class OperatorSignal
4331
+ */
4332
+ class OperatorSignal {
4333
+ /**
4334
+ * Creates an OperatorSignal instance
4335
+ * @param {IOperatorParams} params - Operator parameters
4336
+ */
4337
+ constructor(params) {
4338
+ this.params = params;
4339
+ this._disposeRef = null;
4340
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4341
+ params.logger.debug(`OperatorSignal agentName=${params.agentName} clientId=${params.clientId} CTOR`);
4342
+ this._signalFactory = params.connectOperator(params.clientId, params.agentName);
4343
+ }
4344
+ /**
4345
+ * Sends a message through the operator signal
4346
+ * @param {string} message - Message content
4347
+ * @param {(answer: string) => void} next - Callback for response
4348
+ */
4349
+ sendMessage(message, next) {
4350
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4351
+ this.params.logger.debug(`OperatorSignal agentName=${this.params.agentName} clientId=${this.params.clientId} sendMessage`, { message });
4352
+ this._disposeRef = this._signalFactory(message, next);
4353
+ }
4354
+ /**
4355
+ * Disposes the operator signal
4356
+ * @returns {Promise<void>}
4357
+ */
4358
+ async dispose() {
4359
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4360
+ this.params.logger.debug(`OperatorSignal agentName=${this.params.agentName} clientId=${this.params.clientId} dispose`);
4361
+ if (this._disposeRef) {
4362
+ this._disposeRef = null;
4363
+ await this._disposeRef();
4364
+ }
4365
+ }
4366
+ }
4367
+ /**
4368
+ * Client operator implementation
4369
+ * @class ClientOperator
4370
+ * @implements {IAgent}
4371
+ */
4372
+ class ClientOperator {
4373
+ /**
4374
+ * Creates a ClientOperator instance
4375
+ * @param {IOperatorParams} params - Operator parameters
4376
+ */
4377
+ constructor(params) {
4378
+ this.params = params;
4379
+ this._outgoingSubject = new functoolsKit.Subject();
4380
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4381
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} CTOR`, { params });
4382
+ this._operatorSignal = new OperatorSignal(params);
4383
+ this.params.onInit && this.params.onInit(params.clientId, params.agentName);
4384
+ }
4385
+ /**
4386
+ * Runs the operator (not supported)
4387
+ * @returns {Promise<string>}
4388
+ */
4389
+ async run() {
4390
+ console.warn(`ClientOperator: run should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4391
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4392
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} run - not supported`);
4393
+ return "";
4394
+ }
4395
+ /**
4396
+ * Executes an input with specified mode
4397
+ * @param {string} input - Input content
4398
+ * @param {ExecutionMode} mode - Execution mode
4399
+ * @returns {Promise<void>}
4400
+ */
4401
+ async execute(input, mode) {
4402
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4403
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} execute begin`, { input, mode });
4404
+ if (mode === "tool") {
4405
+ console.warn(`ClientOperator: execute with tool mode should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4406
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4407
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} execute - tool mode not supported`);
4408
+ return;
4409
+ }
4410
+ this._operatorSignal.sendMessage(input, this._outgoingSubject.next);
4411
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4412
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} execute end`, { input, mode });
4413
+ }
4414
+ /**
4415
+ * Waits for operator output with timeout
4416
+ * @returns {Promise<string>} Output result or empty string on timeout
4417
+ */
4418
+ async waitForOutput() {
4419
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4420
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} waitForOutput begin`);
4421
+ const result = await Promise.race([
4422
+ this._outgoingSubject.toPromise(),
4423
+ functoolsKit.sleep(OPERATOR_SIGNAL_TIMEOUT).then(() => OPERATOR_SIGNAL_SYMBOL),
4424
+ ]);
4425
+ if (typeof result === "symbol") {
4426
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4427
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} waitForOutput timeout after ${OPERATOR_SIGNAL_TIMEOUT}ms`);
4428
+ await this._operatorSignal.dispose();
4429
+ return "";
4430
+ }
4431
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4432
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} waitForOutput end`, { result });
4433
+ return result;
4434
+ }
4435
+ /**
4436
+ * Commits tool output (not supported)
4437
+ * @returns {Promise<void>}
4438
+ */
4439
+ commitToolOutput() {
4440
+ console.warn(`ClientOperator: commitToolOutput should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4441
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4442
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitToolOutput - not supported`);
4443
+ return Promise.resolve();
4444
+ }
4445
+ /**
4446
+ * Commits system message (not supported)
4447
+ * @returns {Promise<void>}
4448
+ */
4449
+ commitSystemMessage() {
4450
+ console.warn(`ClientOperator: commitSystemMessage should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4451
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4452
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitSystemMessage - not supported`);
4453
+ return Promise.resolve();
4454
+ }
4455
+ /**
4456
+ * Commits user message
4457
+ * @param {string} content - Message content
4458
+ * @returns {Promise<void>}
4459
+ */
4460
+ commitUserMessage(content) {
4461
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4462
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitUserMessage`, { content });
4463
+ this._operatorSignal.sendMessage(content, this._outgoingSubject.next);
4464
+ return Promise.resolve();
4465
+ }
4466
+ /**
4467
+ * Commits assistant message (not supported)
4468
+ * @returns {Promise<void>}
4469
+ */
4470
+ commitAssistantMessage() {
4471
+ console.warn(`ClientOperator: commitAssistantMessage should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4472
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4473
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitAssistantMessage - not supported`);
4474
+ return Promise.resolve();
4475
+ }
4476
+ /**
4477
+ * Commits flush (not supported)
4478
+ * @returns {Promise<void>}
4479
+ */
4480
+ commitFlush() {
4481
+ console.warn(`ClientOperator: commitFlush should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4482
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4483
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitFlush - not supported`);
4484
+ return Promise.resolve();
4485
+ }
4486
+ /**
4487
+ * Commits stop tools (not supported)
4488
+ * @returns {Promise<void>}
4489
+ */
4490
+ commitStopTools() {
4491
+ console.warn(`ClientOperator: commitStopTools should not be called for clientId=${this.params.clientId} agentName=${this.params.agentName}`);
4492
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4493
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitStopTools - not supported`);
4494
+ return Promise.resolve();
4495
+ }
4496
+ /**
4497
+ * Commits agent change
4498
+ * @returns {Promise<void>}
4499
+ */
4500
+ async commitAgentChange() {
4501
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4502
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} commitAgentChange`);
4503
+ await this._operatorSignal.dispose();
4504
+ }
4505
+ /**
4506
+ * Disposes the client operator
4507
+ * @returns {Promise<void>}
4508
+ */
4509
+ async dispose() {
4510
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
4511
+ this.params.logger.debug(`ClientOperator agentName=${this.params.agentName} clientId=${this.params.clientId} dispose`);
4512
+ await this._operatorSignal.dispose();
4513
+ this._outgoingSubject.unsubscribeAll();
4514
+ this.params.onDispose &&
4515
+ this.params.onDispose(this.params.clientId, this.params.agentName);
4516
+ }
4517
+ }
4518
+
4111
4519
  /**
4112
4520
  * Service class for managing agent connections and operations in the swarm system.
4113
4521
  * Implements IAgent to provide an interface for agent instantiation, execution, message handling, and lifecycle management.
@@ -4198,8 +4606,9 @@ class AgentConnectionService {
4198
4606
  * @returns {ClientAgent} The memoized ClientAgent instance configured for the client and agent.
4199
4607
  */
4200
4608
  this.getAgent = functoolsKit.memoize(([clientId, agentName]) => `${clientId}-${agentName}`, (clientId, agentName) => {
4201
- const { prompt, system, systemStatic = system, systemDynamic, tools, transform = GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM, map = GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP, maxToolCalls = GLOBAL_CONFIG.CC_MAX_TOOL_CALLS, mapToolCalls = GLOBAL_CONFIG.CC_AGENT_MAP_TOOLS, callbacks, storages, states, completion: completionName, validate = validateDefault, } = this.agentSchemaService.get(agentName);
4609
+ const { prompt, system, operator, systemStatic = system, systemDynamic, tools, transform = GLOBAL_CONFIG.CC_AGENT_OUTPUT_TRANSFORM, map = GLOBAL_CONFIG.CC_AGENT_OUTPUT_MAP, maxToolCalls = GLOBAL_CONFIG.CC_MAX_TOOL_CALLS, mapToolCalls = GLOBAL_CONFIG.CC_AGENT_MAP_TOOLS, callbacks, storages, states, connectOperator = GLOBAL_CONFIG.CC_DEFAULT_CONNECT_OPERATOR, completion: completionName, validate = validateDefault, } = this.agentSchemaService.get(agentName);
4202
4610
  const completion = this.completionSchemaService.get(completionName);
4611
+ const history = this.historyConnectionService.getHistory(clientId, agentName);
4203
4612
  this.sessionValidationService.addAgentUsage(clientId, agentName);
4204
4613
  storages?.forEach((storageName) => this.storageConnectionService
4205
4614
  .getStorage(clientId, storageName)
@@ -4207,6 +4616,17 @@ class AgentConnectionService {
4207
4616
  states?.forEach((stateName) => this.stateConnectionService
4208
4617
  .getStateRef(clientId, stateName)
4209
4618
  .waitForInit());
4619
+ if (operator) {
4620
+ return new ClientOperator({
4621
+ clientId,
4622
+ agentName,
4623
+ bus: this.busService,
4624
+ history,
4625
+ logger: this.loggerService,
4626
+ connectOperator,
4627
+ ...callbacks,
4628
+ });
4629
+ }
4210
4630
  return new ClientAgent({
4211
4631
  clientId,
4212
4632
  agentName,
@@ -4215,7 +4635,7 @@ class AgentConnectionService {
4215
4635
  mapToolCalls,
4216
4636
  logger: this.loggerService,
4217
4637
  bus: this.busService,
4218
- history: this.historyConnectionService.getHistory(clientId, agentName),
4638
+ history,
4219
4639
  prompt,
4220
4640
  systemStatic,
4221
4641
  systemDynamic,
@@ -18865,6 +19285,8 @@ exports.HistoryPersistInstance = HistoryPersistInstance;
18865
19285
  exports.Logger = Logger;
18866
19286
  exports.LoggerInstance = LoggerInstance;
18867
19287
  exports.MethodContextService = MethodContextService;
19288
+ exports.Operator = Operator;
19289
+ exports.OperatorInstance = OperatorInstance;
18868
19290
  exports.PayloadContextService = PayloadContextService;
18869
19291
  exports.PersistAlive = PersistAlive;
18870
19292
  exports.PersistBase = PersistBase;