agent-swarm-kit 1.0.173 → 1.0.175

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/build/index.cjs CHANGED
@@ -495,6 +495,9 @@ const PERSIST_STORAGE_UTILS_METHOD_NAME_USE_PERSIST_STORAGE_ADAPTER = "PersistSt
495
495
  const PERSIST_STORAGE_UTILS_METHOD_NAME_GET_DATA = "PersistStorageUtils.getData";
496
496
  /** @private Constant for logging the setData method in PersistStorageUtils */
497
497
  const PERSIST_STORAGE_UTILS_METHOD_NAME_SET_DATA = "PersistStorageUtils.setData";
498
+ const PERSIST_POLICY_UTILS_METHOD_NAME_USE_PERSIST_POLICY_ADAPTER = "PersistPolicyUtils.usePersistPolicyAdapter";
499
+ const PERSIST_POLICY_UTILS_METHOD_NAME_GET_BANNED_CLIENTS = "PersistPolicyUtils.getBannedClients";
500
+ const PERSIST_POLICY_UTILS_METHOD_NAME_SET_BANNED_CLIENTS = "PersistPolicyUtils.setBannedClients";
498
501
  // Logging method names for private functions
499
502
  /** @private Constant for logging the waitForInitFn function */
500
503
  const BASE_WAIT_FOR_INIT_FN_METHOD_NAME = "PersistBase.waitForInitFn";
@@ -1551,6 +1554,87 @@ const PersistAliveAdapter = new PersistAliveUtils();
1551
1554
  * @type {IPersistAliveControl}
1552
1555
  */
1553
1556
  const PersistAlive = PersistAliveAdapter;
1557
+ /**
1558
+ * Utility class for managing policy data persistence in the swarm system.
1559
+ * Provides methods to get and set banned clients within a `SwarmName`, with a customizable adapter.
1560
+ * @implements {IPersistPolicyControl}
1561
+ */
1562
+ class PersistPolicyUtils {
1563
+ constructor() {
1564
+ /** @private Default constructor for policy data persistence, defaults to `PersistBase` */
1565
+ this.PersistPolicyFactory = PersistBase;
1566
+ /**
1567
+ * Memoized function to create or retrieve storage for a specific policy data.
1568
+ * Ensures a single persistence instance per swarm, optimizing resource use.
1569
+ * @private
1570
+ * @param swarmName - The identifier of the swarm.
1571
+ * @returns A persistence instance for the policy data, rooted at `./logs/data/policy/`.
1572
+ */
1573
+ this.getPolicyStorage = functoolsKit.memoize(([swarmName]) => `${swarmName}`, (swarmName) => Reflect.construct(this.PersistPolicyFactory, [
1574
+ swarmName,
1575
+ `./logs/data/policy/`,
1576
+ ]));
1577
+ /**
1578
+ * Retrieves the list of banned clients for a specific policy, defaulting to an empty array if unset.
1579
+ * Used to check client ban status in swarm workflows.
1580
+ * @param policyName - The identifier of the policy to check.
1581
+ * @param swarmName - The identifier of the swarm.
1582
+ * @param defaultValue - Optional default value if no banned clients are found.
1583
+ * @returns A promise resolving to an array of banned client session IDs.
1584
+ * @throws {Error} If reading from storage fails (e.g., file corruption).
1585
+ */
1586
+ this.getBannedClients = async (policyName, swarmName, defaultValue = []) => {
1587
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1588
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_GET_BANNED_CLIENTS);
1589
+ const isInitial = this.getPolicyStorage.has(swarmName);
1590
+ const stateStorage = this.getPolicyStorage(swarmName);
1591
+ await stateStorage.waitForInit(isInitial);
1592
+ if (await stateStorage.hasValue(policyName)) {
1593
+ const { bannedClients } = await stateStorage.readValue(policyName);
1594
+ return bannedClients;
1595
+ }
1596
+ return defaultValue;
1597
+ };
1598
+ /**
1599
+ * Sets the list of banned clients for a specific policy, persisting the status for future retrieval.
1600
+ * Used to manage client bans in swarm operations.
1601
+ * @param bannedClients - Array of session IDs to be banned under this policy.
1602
+ * @param policyName - The identifier of the policy to update.
1603
+ * @param swarmName - The identifier of the swarm.
1604
+ * @returns A promise that resolves when the banned clients list is persisted.
1605
+ * @throws {Error} If writing to storage fails (e.g., permissions or disk space).
1606
+ */
1607
+ this.setBannedClients = async (bannedClients, policyName, swarmName) => {
1608
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1609
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_SET_BANNED_CLIENTS);
1610
+ const isInitial = this.getPolicyStorage.has(swarmName);
1611
+ const stateStorage = this.getPolicyStorage(swarmName);
1612
+ await stateStorage.waitForInit(isInitial);
1613
+ await stateStorage.writeValue(policyName, { bannedClients });
1614
+ };
1615
+ }
1616
+ /**
1617
+ * Configures a custom constructor for policy data persistence, overriding the default `PersistBase`.
1618
+ * Enables advanced tracking (e.g., in-memory or database-backed persistence).
1619
+ * @param Ctor - The constructor to use for policy data storage, implementing `IPersistBase`.
1620
+ */
1621
+ usePersistPolicyAdapter(Ctor) {
1622
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1623
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_USE_PERSIST_POLICY_ADAPTER);
1624
+ this.PersistPolicyFactory = Ctor;
1625
+ }
1626
+ }
1627
+ /**
1628
+ * Singleton instance of `PersistPolicyUtils` for managing policy data persistence globally.
1629
+ * @type {PersistPolicyUtils}
1630
+ */
1631
+ const PersistPolicyAdapter = new PersistPolicyUtils();
1632
+ /**
1633
+ * Exported singleton for policy persistence operations, cast as the control interface.
1634
+ * Provides a global point of access for managing client bans in the swarm.
1635
+ * @type {IPersistPolicyControl}
1636
+ */
1637
+ const PersistPolicy = PersistPolicyAdapter;
1554
1638
 
1555
1639
  var _a$1, _b$1, _c, _d;
1556
1640
  /** @private Symbol for memoizing the waitForInit method in HistoryMemoryInstance */
@@ -2628,6 +2712,9 @@ const CC_LOGGER_ENABLE_CONSOLE = false;
2628
2712
  let CC_RESQUE_STRATEGY;
2629
2713
  const CC_DEFAULT_STATE_SET = () => Promise.resolve();
2630
2714
  const CC_DEFAULT_STATE_GET = ({}, {}, defaultState) => Promise.resolve(defaultState);
2715
+ const CC_DEFAULT_POLICY_GET_BAN_CLIENTS = () => [];
2716
+ const CC_DEFAULT_POLICY_GET = () => [];
2717
+ const CC_DEFAULT_POLICY_SET = () => Promise.resolve();
2631
2718
  const CC_DEFAULT_STORAGE_GET = ({}, {}, defaultValue) => Promise.resolve(defaultValue);
2632
2719
  const CC_DEFAULT_STORAGE_SET = () => Promise.resolve();
2633
2720
  const CC_NAME_TO_TITLE = nameToTitle;
@@ -2644,6 +2731,9 @@ const GLOBAL_CONFIG = {
2644
2731
  CC_EMPTY_OUTPUT_PLACEHOLDERS,
2645
2732
  CC_KEEP_MESSAGES,
2646
2733
  CC_MAX_TOOL_CALLS,
2734
+ CC_DEFAULT_POLICY_GET_BAN_CLIENTS,
2735
+ CC_DEFAULT_POLICY_GET,
2736
+ CC_DEFAULT_POLICY_SET,
2647
2737
  CC_AGENT_MAP_TOOLS,
2648
2738
  CC_GET_AGENT_HISTORY_ADAPTER,
2649
2739
  CC_GET_CLIENT_LOGGER_ADAPTER,
@@ -3547,15 +3637,15 @@ class ClientAgent {
3547
3637
  * @param {string} message - The user message to commit, trimmed before storage.
3548
3638
  * @returns {Promise<void>} Resolves when the message is committed to history and the event is emitted.
3549
3639
  */
3550
- async commitUserMessage(message) {
3640
+ async commitUserMessage(message, mode) {
3551
3641
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3552
- this.params.logger.debug(`ClientAgent agentName=${this.params.agentName} clientId=${this.params.clientId} commitUserMessage`, { message });
3642
+ this.params.logger.debug(`ClientAgent agentName=${this.params.agentName} clientId=${this.params.clientId} commitUserMessage`, { message, mode });
3553
3643
  this.params.onUserMessage &&
3554
3644
  this.params.onUserMessage(this.params.clientId, this.params.agentName, message);
3555
3645
  await this.params.history.push({
3556
3646
  role: "user",
3557
3647
  agentName: this.params.agentName,
3558
- mode: "user",
3648
+ mode,
3559
3649
  content: message.trim(),
3560
3650
  });
3561
3651
  await this.params.bus.emit(this.params.clientId, {
@@ -3954,12 +4044,13 @@ class AgentConnectionService {
3954
4044
  * @param {string} message - The user message to commit.
3955
4045
  * @returns {Promise<any>} A promise resolving to the commit result, type determined by ClientAgent’s implementation.
3956
4046
  */
3957
- this.commitUserMessage = async (message) => {
4047
+ this.commitUserMessage = async (message, mode) => {
3958
4048
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
3959
4049
  this.loggerService.info(`agentConnectionService commitUserMessage`, {
3960
4050
  message,
4051
+ mode,
3961
4052
  });
3962
- return await this.getAgent(this.methodContextService.context.clientId, this.methodContextService.context.agentName).commitUserMessage(message);
4053
+ return await this.getAgent(this.methodContextService.context.clientId, this.methodContextService.context.agentName).commitUserMessage(message, mode);
3963
4054
  };
3964
4055
  /**
3965
4056
  * Commits an agent change to prevent the next tool execution, altering the execution flow.
@@ -5229,13 +5320,14 @@ class ClientSession {
5229
5320
  * @param {string} message - The user message to commit, typically from client input.
5230
5321
  * @returns {Promise<void>} Resolves when the message is committed and the event is logged.
5231
5322
  */
5232
- async commitUserMessage(message) {
5323
+ async commitUserMessage(message, mode) {
5233
5324
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
5234
5325
  this.params.logger.debug(`ClientSession clientId=${this.params.clientId} commitUserMessage`, {
5235
5326
  message,
5327
+ mode,
5236
5328
  });
5237
5329
  const agent = await this.params.swarm.getAgent();
5238
- const result = await agent.commitUserMessage(message);
5330
+ const result = await agent.commitUserMessage(message, mode);
5239
5331
  await this.params.bus.emit(this.params.clientId, {
5240
5332
  type: "commit-user-message",
5241
5333
  source: "session-bus",
@@ -5889,12 +5981,13 @@ class SessionConnectionService {
5889
5981
  * @param {string} message - The user message to commit.
5890
5982
  * @returns {Promise<void>} A promise resolving when the user message is committed.
5891
5983
  */
5892
- this.commitUserMessage = async (message) => {
5984
+ this.commitUserMessage = async (message, mode) => {
5893
5985
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
5894
5986
  this.loggerService.info(`sessionConnectionService commitUserMessage`, {
5895
5987
  message,
5988
+ mode,
5896
5989
  });
5897
- return await this.getSession(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).commitUserMessage(message);
5990
+ return await this.getSession(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).commitUserMessage(message, mode);
5898
5991
  };
5899
5992
  /**
5900
5993
  * Commits a flush of the session’s history, clearing stored data.
@@ -6179,16 +6272,17 @@ class AgentPublicService {
6179
6272
  * @param {AgentName} agentName - The agent name for identification.
6180
6273
  * @returns {Promise<unknown>} A promise resolving to the commit result.
6181
6274
  */
6182
- this.commitUserMessage = async (message, methodName, clientId, agentName) => {
6275
+ this.commitUserMessage = async (message, mode, methodName, clientId, agentName) => {
6183
6276
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
6184
6277
  this.loggerService.info("agentPublicService commitUserMessage", {
6185
6278
  methodName,
6186
6279
  message,
6187
6280
  clientId,
6188
6281
  agentName,
6282
+ mode,
6189
6283
  });
6190
6284
  return await MethodContextService.runInContext(async () => {
6191
- return await this.agentConnectionService.commitUserMessage(message);
6285
+ return await this.agentConnectionService.commitUserMessage(message, mode);
6192
6286
  }, {
6193
6287
  methodName,
6194
6288
  clientId,
@@ -6758,16 +6852,17 @@ class SessionPublicService {
6758
6852
  * @param {SwarmName} swarmName - The swarm name for context.
6759
6853
  * @returns {Promise<void>} A promise resolving when the user message is committed.
6760
6854
  */
6761
- this.commitUserMessage = async (message, methodName, clientId, swarmName) => {
6855
+ this.commitUserMessage = async (message, mode, methodName, clientId, swarmName) => {
6762
6856
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
6763
6857
  this.loggerService.info("sessionPublicService commitUserMessage", {
6764
6858
  methodName,
6765
6859
  message,
6766
6860
  clientId,
6767
6861
  swarmName,
6862
+ mode,
6768
6863
  });
6769
6864
  return await MethodContextService.runInContext(async () => {
6770
- return await this.sessionConnectionService.commitUserMessage(message);
6865
+ return await this.sessionConnectionService.commitUserMessage(message, mode);
6771
6866
  }, {
6772
6867
  methodName,
6773
6868
  clientId,
@@ -11898,8 +11993,9 @@ class PolicySchemaService {
11898
11993
  if (typeof policySchema.policyName !== "string") {
11899
11994
  throw new Error(`agent-swarm policy schema validation failed: missing policyName`);
11900
11995
  }
11901
- if (typeof policySchema.getBannedClients !== "function") {
11902
- throw new Error(`agent-swarm policy schema validation failed: missing getBannedClients policyName=${policySchema.policyName}`);
11996
+ if (typeof policySchema.validateInput !== "function" &&
11997
+ typeof policySchema.validateOutput !== "function") {
11998
+ throw new Error(`agent-swarm policy schema validation failed: the validateInput or validateOutput must be provided at least policyName=${policySchema.policyName}`);
11903
11999
  }
11904
12000
  };
11905
12001
  /**
@@ -12505,12 +12601,18 @@ class PolicyConnectionService {
12505
12601
  * @returns {ClientPolicy} The memoized ClientPolicy instance configured for the policy.
12506
12602
  */
12507
12603
  this.getPolicy = functoolsKit.memoize(([policyName]) => `${policyName}`, (policyName) => {
12508
- const { autoBan = GLOBAL_CONFIG.CC_AUTOBAN_ENABLED_BY_DEFAULT, ...schema } = this.policySchemaService.get(policyName);
12604
+ const { autoBan = GLOBAL_CONFIG.CC_AUTOBAN_ENABLED_BY_DEFAULT, banMessage = GLOBAL_CONFIG.CC_BANHAMMER_PLACEHOLDER, persist = GLOBAL_CONFIG.CC_PERSIST_ENABLED_BY_DEFAULT, getBannedClients = persist
12605
+ ? PersistPolicyAdapter.getBannedClients
12606
+ : GLOBAL_CONFIG.CC_DEFAULT_POLICY_GET, setBannedClients = persist
12607
+ ? PersistPolicyAdapter.setBannedClients
12608
+ : GLOBAL_CONFIG.CC_DEFAULT_POLICY_SET, ...schema } = this.policySchemaService.get(policyName);
12509
12609
  return new ClientPolicy({
12510
12610
  policyName,
12511
12611
  bus: this.busService,
12512
12612
  logger: this.loggerService,
12513
- autoBan,
12613
+ autoBan, getBannedClients,
12614
+ setBannedClients,
12615
+ banMessage,
12514
12616
  ...schema,
12515
12617
  });
12516
12618
  });
@@ -13452,13 +13554,14 @@ const METHOD_NAME$B = "function.commit.commitSystemMessage";
13452
13554
  * @example
13453
13555
  * await commitUserMessage("User input message", "client-123", "AgentX");
13454
13556
  */
13455
- const commitUserMessage = beginContext(async (content, clientId, agentName) => {
13557
+ const commitUserMessage = beginContext(async (content, mode, clientId, agentName) => {
13456
13558
  // Log the operation details if logging is enabled in GLOBAL_CONFIG
13457
13559
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
13458
13560
  swarm$1.loggerService.log(METHOD_NAME$B, {
13459
13561
  content,
13460
13562
  clientId,
13461
13563
  agentName,
13564
+ mode,
13462
13565
  });
13463
13566
  // Validate the agent, session, and swarm to ensure they exist and are accessible
13464
13567
  swarm$1.agentValidationService.validate(agentName, METHOD_NAME$B);
@@ -13478,7 +13581,7 @@ const commitUserMessage = beginContext(async (content, clientId, agentName) => {
13478
13581
  return;
13479
13582
  }
13480
13583
  // Commit the user message to the agent's history via the session public service
13481
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$B, clientId, swarmName);
13584
+ await swarm$1.sessionPublicService.commitUserMessage(content, mode, METHOD_NAME$B, clientId, swarmName);
13482
13585
  });
13483
13586
 
13484
13587
  const METHOD_NAME$A = "function.commit.commitToolOutputForce";
@@ -13593,19 +13696,20 @@ const METHOD_NAME$x = "function.commit.commitSystemMessage";
13593
13696
  * @example
13594
13697
  * await commitUserMessageForce("User input message", "client-123");
13595
13698
  */
13596
- const commitUserMessageForce = beginContext(async (content, clientId) => {
13699
+ const commitUserMessageForce = beginContext(async (content, mode, clientId) => {
13597
13700
  // Log the operation details if logging is enabled in GLOBAL_CONFIG
13598
13701
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
13599
13702
  swarm$1.loggerService.log(METHOD_NAME$x, {
13600
13703
  content,
13601
13704
  clientId,
13705
+ mode,
13602
13706
  });
13603
13707
  // Validate the session and swarm to ensure they exist and are accessible
13604
13708
  swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$x);
13605
13709
  const swarmName = swarm$1.sessionValidationService.getSwarm(clientId);
13606
13710
  swarm$1.swarmValidationService.validate(swarmName, METHOD_NAME$x);
13607
13711
  // Commit the user message to the agent's history via the session public service without checking the active agent
13608
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$x, clientId, swarmName);
13712
+ await swarm$1.sessionPublicService.commitUserMessage(content, mode, METHOD_NAME$x, clientId, swarmName);
13609
13713
  });
13610
13714
 
13611
13715
  /** @private Constant defining the method name for logging and validation context */
@@ -14448,13 +14552,13 @@ makeConnection.scheduled = (connector, clientId, swarmName, { delay = SCHEDULED_
14448
14552
  await online();
14449
14553
  if (payload) {
14450
14554
  return await PayloadContextService.runInContext(async () => {
14451
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$i, clientId, swarmName);
14555
+ await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$i, clientId, swarmName);
14452
14556
  }, {
14453
14557
  clientId,
14454
14558
  payload,
14455
14559
  });
14456
14560
  }
14457
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$i, clientId, swarmName);
14561
+ await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$i, clientId, swarmName);
14458
14562
  }),
14459
14563
  delay,
14460
14564
  });
@@ -14771,13 +14875,13 @@ session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
14771
14875
  await online();
14772
14876
  if (payload) {
14773
14877
  return await PayloadContextService.runInContext(async () => {
14774
- return await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$g, clientId, swarmName);
14878
+ return await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$g, clientId, swarmName);
14775
14879
  }, {
14776
14880
  clientId,
14777
14881
  payload,
14778
14882
  });
14779
14883
  }
14780
- return await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$g, clientId, swarmName);
14884
+ return await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$g, clientId, swarmName);
14781
14885
  }),
14782
14886
  delay,
14783
14887
  });
@@ -17159,6 +17263,7 @@ const Utils = {
17159
17263
  PersistStorageUtils,
17160
17264
  PersistMemoryUtils,
17161
17265
  PersistAliveUtils,
17266
+ PersistPolicyUtils,
17162
17267
  };
17163
17268
 
17164
17269
  exports.Adapter = Adapter;
@@ -17174,6 +17279,7 @@ exports.PersistAlive = PersistAlive;
17174
17279
  exports.PersistBase = PersistBase;
17175
17280
  exports.PersistList = PersistList;
17176
17281
  exports.PersistMemory = PersistMemory;
17282
+ exports.PersistPolicy = PersistPolicy;
17177
17283
  exports.PersistState = PersistState;
17178
17284
  exports.PersistStorage = PersistStorage;
17179
17285
  exports.PersistSwarm = PersistSwarm;
package/build/index.mjs CHANGED
@@ -493,6 +493,9 @@ const PERSIST_STORAGE_UTILS_METHOD_NAME_USE_PERSIST_STORAGE_ADAPTER = "PersistSt
493
493
  const PERSIST_STORAGE_UTILS_METHOD_NAME_GET_DATA = "PersistStorageUtils.getData";
494
494
  /** @private Constant for logging the setData method in PersistStorageUtils */
495
495
  const PERSIST_STORAGE_UTILS_METHOD_NAME_SET_DATA = "PersistStorageUtils.setData";
496
+ const PERSIST_POLICY_UTILS_METHOD_NAME_USE_PERSIST_POLICY_ADAPTER = "PersistPolicyUtils.usePersistPolicyAdapter";
497
+ const PERSIST_POLICY_UTILS_METHOD_NAME_GET_BANNED_CLIENTS = "PersistPolicyUtils.getBannedClients";
498
+ const PERSIST_POLICY_UTILS_METHOD_NAME_SET_BANNED_CLIENTS = "PersistPolicyUtils.setBannedClients";
496
499
  // Logging method names for private functions
497
500
  /** @private Constant for logging the waitForInitFn function */
498
501
  const BASE_WAIT_FOR_INIT_FN_METHOD_NAME = "PersistBase.waitForInitFn";
@@ -1549,6 +1552,87 @@ const PersistAliveAdapter = new PersistAliveUtils();
1549
1552
  * @type {IPersistAliveControl}
1550
1553
  */
1551
1554
  const PersistAlive = PersistAliveAdapter;
1555
+ /**
1556
+ * Utility class for managing policy data persistence in the swarm system.
1557
+ * Provides methods to get and set banned clients within a `SwarmName`, with a customizable adapter.
1558
+ * @implements {IPersistPolicyControl}
1559
+ */
1560
+ class PersistPolicyUtils {
1561
+ constructor() {
1562
+ /** @private Default constructor for policy data persistence, defaults to `PersistBase` */
1563
+ this.PersistPolicyFactory = PersistBase;
1564
+ /**
1565
+ * Memoized function to create or retrieve storage for a specific policy data.
1566
+ * Ensures a single persistence instance per swarm, optimizing resource use.
1567
+ * @private
1568
+ * @param swarmName - The identifier of the swarm.
1569
+ * @returns A persistence instance for the policy data, rooted at `./logs/data/policy/`.
1570
+ */
1571
+ this.getPolicyStorage = memoize(([swarmName]) => `${swarmName}`, (swarmName) => Reflect.construct(this.PersistPolicyFactory, [
1572
+ swarmName,
1573
+ `./logs/data/policy/`,
1574
+ ]));
1575
+ /**
1576
+ * Retrieves the list of banned clients for a specific policy, defaulting to an empty array if unset.
1577
+ * Used to check client ban status in swarm workflows.
1578
+ * @param policyName - The identifier of the policy to check.
1579
+ * @param swarmName - The identifier of the swarm.
1580
+ * @param defaultValue - Optional default value if no banned clients are found.
1581
+ * @returns A promise resolving to an array of banned client session IDs.
1582
+ * @throws {Error} If reading from storage fails (e.g., file corruption).
1583
+ */
1584
+ this.getBannedClients = async (policyName, swarmName, defaultValue = []) => {
1585
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1586
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_GET_BANNED_CLIENTS);
1587
+ const isInitial = this.getPolicyStorage.has(swarmName);
1588
+ const stateStorage = this.getPolicyStorage(swarmName);
1589
+ await stateStorage.waitForInit(isInitial);
1590
+ if (await stateStorage.hasValue(policyName)) {
1591
+ const { bannedClients } = await stateStorage.readValue(policyName);
1592
+ return bannedClients;
1593
+ }
1594
+ return defaultValue;
1595
+ };
1596
+ /**
1597
+ * Sets the list of banned clients for a specific policy, persisting the status for future retrieval.
1598
+ * Used to manage client bans in swarm operations.
1599
+ * @param bannedClients - Array of session IDs to be banned under this policy.
1600
+ * @param policyName - The identifier of the policy to update.
1601
+ * @param swarmName - The identifier of the swarm.
1602
+ * @returns A promise that resolves when the banned clients list is persisted.
1603
+ * @throws {Error} If writing to storage fails (e.g., permissions or disk space).
1604
+ */
1605
+ this.setBannedClients = async (bannedClients, policyName, swarmName) => {
1606
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1607
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_SET_BANNED_CLIENTS);
1608
+ const isInitial = this.getPolicyStorage.has(swarmName);
1609
+ const stateStorage = this.getPolicyStorage(swarmName);
1610
+ await stateStorage.waitForInit(isInitial);
1611
+ await stateStorage.writeValue(policyName, { bannedClients });
1612
+ };
1613
+ }
1614
+ /**
1615
+ * Configures a custom constructor for policy data persistence, overriding the default `PersistBase`.
1616
+ * Enables advanced tracking (e.g., in-memory or database-backed persistence).
1617
+ * @param Ctor - The constructor to use for policy data storage, implementing `IPersistBase`.
1618
+ */
1619
+ usePersistPolicyAdapter(Ctor) {
1620
+ GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
1621
+ swarm$1.loggerService.log(PERSIST_POLICY_UTILS_METHOD_NAME_USE_PERSIST_POLICY_ADAPTER);
1622
+ this.PersistPolicyFactory = Ctor;
1623
+ }
1624
+ }
1625
+ /**
1626
+ * Singleton instance of `PersistPolicyUtils` for managing policy data persistence globally.
1627
+ * @type {PersistPolicyUtils}
1628
+ */
1629
+ const PersistPolicyAdapter = new PersistPolicyUtils();
1630
+ /**
1631
+ * Exported singleton for policy persistence operations, cast as the control interface.
1632
+ * Provides a global point of access for managing client bans in the swarm.
1633
+ * @type {IPersistPolicyControl}
1634
+ */
1635
+ const PersistPolicy = PersistPolicyAdapter;
1552
1636
 
1553
1637
  var _a$1, _b$1, _c, _d;
1554
1638
  /** @private Symbol for memoizing the waitForInit method in HistoryMemoryInstance */
@@ -2626,6 +2710,9 @@ const CC_LOGGER_ENABLE_CONSOLE = false;
2626
2710
  let CC_RESQUE_STRATEGY;
2627
2711
  const CC_DEFAULT_STATE_SET = () => Promise.resolve();
2628
2712
  const CC_DEFAULT_STATE_GET = ({}, {}, defaultState) => Promise.resolve(defaultState);
2713
+ const CC_DEFAULT_POLICY_GET_BAN_CLIENTS = () => [];
2714
+ const CC_DEFAULT_POLICY_GET = () => [];
2715
+ const CC_DEFAULT_POLICY_SET = () => Promise.resolve();
2629
2716
  const CC_DEFAULT_STORAGE_GET = ({}, {}, defaultValue) => Promise.resolve(defaultValue);
2630
2717
  const CC_DEFAULT_STORAGE_SET = () => Promise.resolve();
2631
2718
  const CC_NAME_TO_TITLE = nameToTitle;
@@ -2642,6 +2729,9 @@ const GLOBAL_CONFIG = {
2642
2729
  CC_EMPTY_OUTPUT_PLACEHOLDERS,
2643
2730
  CC_KEEP_MESSAGES,
2644
2731
  CC_MAX_TOOL_CALLS,
2732
+ CC_DEFAULT_POLICY_GET_BAN_CLIENTS,
2733
+ CC_DEFAULT_POLICY_GET,
2734
+ CC_DEFAULT_POLICY_SET,
2645
2735
  CC_AGENT_MAP_TOOLS,
2646
2736
  CC_GET_AGENT_HISTORY_ADAPTER,
2647
2737
  CC_GET_CLIENT_LOGGER_ADAPTER,
@@ -3545,15 +3635,15 @@ class ClientAgent {
3545
3635
  * @param {string} message - The user message to commit, trimmed before storage.
3546
3636
  * @returns {Promise<void>} Resolves when the message is committed to history and the event is emitted.
3547
3637
  */
3548
- async commitUserMessage(message) {
3638
+ async commitUserMessage(message, mode) {
3549
3639
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
3550
- this.params.logger.debug(`ClientAgent agentName=${this.params.agentName} clientId=${this.params.clientId} commitUserMessage`, { message });
3640
+ this.params.logger.debug(`ClientAgent agentName=${this.params.agentName} clientId=${this.params.clientId} commitUserMessage`, { message, mode });
3551
3641
  this.params.onUserMessage &&
3552
3642
  this.params.onUserMessage(this.params.clientId, this.params.agentName, message);
3553
3643
  await this.params.history.push({
3554
3644
  role: "user",
3555
3645
  agentName: this.params.agentName,
3556
- mode: "user",
3646
+ mode,
3557
3647
  content: message.trim(),
3558
3648
  });
3559
3649
  await this.params.bus.emit(this.params.clientId, {
@@ -3952,12 +4042,13 @@ class AgentConnectionService {
3952
4042
  * @param {string} message - The user message to commit.
3953
4043
  * @returns {Promise<any>} A promise resolving to the commit result, type determined by ClientAgent’s implementation.
3954
4044
  */
3955
- this.commitUserMessage = async (message) => {
4045
+ this.commitUserMessage = async (message, mode) => {
3956
4046
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
3957
4047
  this.loggerService.info(`agentConnectionService commitUserMessage`, {
3958
4048
  message,
4049
+ mode,
3959
4050
  });
3960
- return await this.getAgent(this.methodContextService.context.clientId, this.methodContextService.context.agentName).commitUserMessage(message);
4051
+ return await this.getAgent(this.methodContextService.context.clientId, this.methodContextService.context.agentName).commitUserMessage(message, mode);
3961
4052
  };
3962
4053
  /**
3963
4054
  * Commits an agent change to prevent the next tool execution, altering the execution flow.
@@ -5227,13 +5318,14 @@ class ClientSession {
5227
5318
  * @param {string} message - The user message to commit, typically from client input.
5228
5319
  * @returns {Promise<void>} Resolves when the message is committed and the event is logged.
5229
5320
  */
5230
- async commitUserMessage(message) {
5321
+ async commitUserMessage(message, mode) {
5231
5322
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
5232
5323
  this.params.logger.debug(`ClientSession clientId=${this.params.clientId} commitUserMessage`, {
5233
5324
  message,
5325
+ mode,
5234
5326
  });
5235
5327
  const agent = await this.params.swarm.getAgent();
5236
- const result = await agent.commitUserMessage(message);
5328
+ const result = await agent.commitUserMessage(message, mode);
5237
5329
  await this.params.bus.emit(this.params.clientId, {
5238
5330
  type: "commit-user-message",
5239
5331
  source: "session-bus",
@@ -5887,12 +5979,13 @@ class SessionConnectionService {
5887
5979
  * @param {string} message - The user message to commit.
5888
5980
  * @returns {Promise<void>} A promise resolving when the user message is committed.
5889
5981
  */
5890
- this.commitUserMessage = async (message) => {
5982
+ this.commitUserMessage = async (message, mode) => {
5891
5983
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
5892
5984
  this.loggerService.info(`sessionConnectionService commitUserMessage`, {
5893
5985
  message,
5986
+ mode,
5894
5987
  });
5895
- return await this.getSession(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).commitUserMessage(message);
5988
+ return await this.getSession(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).commitUserMessage(message, mode);
5896
5989
  };
5897
5990
  /**
5898
5991
  * Commits a flush of the session’s history, clearing stored data.
@@ -6177,16 +6270,17 @@ class AgentPublicService {
6177
6270
  * @param {AgentName} agentName - The agent name for identification.
6178
6271
  * @returns {Promise<unknown>} A promise resolving to the commit result.
6179
6272
  */
6180
- this.commitUserMessage = async (message, methodName, clientId, agentName) => {
6273
+ this.commitUserMessage = async (message, mode, methodName, clientId, agentName) => {
6181
6274
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
6182
6275
  this.loggerService.info("agentPublicService commitUserMessage", {
6183
6276
  methodName,
6184
6277
  message,
6185
6278
  clientId,
6186
6279
  agentName,
6280
+ mode,
6187
6281
  });
6188
6282
  return await MethodContextService.runInContext(async () => {
6189
- return await this.agentConnectionService.commitUserMessage(message);
6283
+ return await this.agentConnectionService.commitUserMessage(message, mode);
6190
6284
  }, {
6191
6285
  methodName,
6192
6286
  clientId,
@@ -6756,16 +6850,17 @@ class SessionPublicService {
6756
6850
  * @param {SwarmName} swarmName - The swarm name for context.
6757
6851
  * @returns {Promise<void>} A promise resolving when the user message is committed.
6758
6852
  */
6759
- this.commitUserMessage = async (message, methodName, clientId, swarmName) => {
6853
+ this.commitUserMessage = async (message, mode, methodName, clientId, swarmName) => {
6760
6854
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
6761
6855
  this.loggerService.info("sessionPublicService commitUserMessage", {
6762
6856
  methodName,
6763
6857
  message,
6764
6858
  clientId,
6765
6859
  swarmName,
6860
+ mode,
6766
6861
  });
6767
6862
  return await MethodContextService.runInContext(async () => {
6768
- return await this.sessionConnectionService.commitUserMessage(message);
6863
+ return await this.sessionConnectionService.commitUserMessage(message, mode);
6769
6864
  }, {
6770
6865
  methodName,
6771
6866
  clientId,
@@ -11896,8 +11991,9 @@ class PolicySchemaService {
11896
11991
  if (typeof policySchema.policyName !== "string") {
11897
11992
  throw new Error(`agent-swarm policy schema validation failed: missing policyName`);
11898
11993
  }
11899
- if (typeof policySchema.getBannedClients !== "function") {
11900
- throw new Error(`agent-swarm policy schema validation failed: missing getBannedClients policyName=${policySchema.policyName}`);
11994
+ if (typeof policySchema.validateInput !== "function" &&
11995
+ typeof policySchema.validateOutput !== "function") {
11996
+ throw new Error(`agent-swarm policy schema validation failed: the validateInput or validateOutput must be provided at least policyName=${policySchema.policyName}`);
11901
11997
  }
11902
11998
  };
11903
11999
  /**
@@ -12503,12 +12599,18 @@ class PolicyConnectionService {
12503
12599
  * @returns {ClientPolicy} The memoized ClientPolicy instance configured for the policy.
12504
12600
  */
12505
12601
  this.getPolicy = memoize(([policyName]) => `${policyName}`, (policyName) => {
12506
- const { autoBan = GLOBAL_CONFIG.CC_AUTOBAN_ENABLED_BY_DEFAULT, ...schema } = this.policySchemaService.get(policyName);
12602
+ const { autoBan = GLOBAL_CONFIG.CC_AUTOBAN_ENABLED_BY_DEFAULT, banMessage = GLOBAL_CONFIG.CC_BANHAMMER_PLACEHOLDER, persist = GLOBAL_CONFIG.CC_PERSIST_ENABLED_BY_DEFAULT, getBannedClients = persist
12603
+ ? PersistPolicyAdapter.getBannedClients
12604
+ : GLOBAL_CONFIG.CC_DEFAULT_POLICY_GET, setBannedClients = persist
12605
+ ? PersistPolicyAdapter.setBannedClients
12606
+ : GLOBAL_CONFIG.CC_DEFAULT_POLICY_SET, ...schema } = this.policySchemaService.get(policyName);
12507
12607
  return new ClientPolicy({
12508
12608
  policyName,
12509
12609
  bus: this.busService,
12510
12610
  logger: this.loggerService,
12511
- autoBan,
12611
+ autoBan, getBannedClients,
12612
+ setBannedClients,
12613
+ banMessage,
12512
12614
  ...schema,
12513
12615
  });
12514
12616
  });
@@ -13450,13 +13552,14 @@ const METHOD_NAME$B = "function.commit.commitSystemMessage";
13450
13552
  * @example
13451
13553
  * await commitUserMessage("User input message", "client-123", "AgentX");
13452
13554
  */
13453
- const commitUserMessage = beginContext(async (content, clientId, agentName) => {
13555
+ const commitUserMessage = beginContext(async (content, mode, clientId, agentName) => {
13454
13556
  // Log the operation details if logging is enabled in GLOBAL_CONFIG
13455
13557
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
13456
13558
  swarm$1.loggerService.log(METHOD_NAME$B, {
13457
13559
  content,
13458
13560
  clientId,
13459
13561
  agentName,
13562
+ mode,
13460
13563
  });
13461
13564
  // Validate the agent, session, and swarm to ensure they exist and are accessible
13462
13565
  swarm$1.agentValidationService.validate(agentName, METHOD_NAME$B);
@@ -13476,7 +13579,7 @@ const commitUserMessage = beginContext(async (content, clientId, agentName) => {
13476
13579
  return;
13477
13580
  }
13478
13581
  // Commit the user message to the agent's history via the session public service
13479
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$B, clientId, swarmName);
13582
+ await swarm$1.sessionPublicService.commitUserMessage(content, mode, METHOD_NAME$B, clientId, swarmName);
13480
13583
  });
13481
13584
 
13482
13585
  const METHOD_NAME$A = "function.commit.commitToolOutputForce";
@@ -13591,19 +13694,20 @@ const METHOD_NAME$x = "function.commit.commitSystemMessage";
13591
13694
  * @example
13592
13695
  * await commitUserMessageForce("User input message", "client-123");
13593
13696
  */
13594
- const commitUserMessageForce = beginContext(async (content, clientId) => {
13697
+ const commitUserMessageForce = beginContext(async (content, mode, clientId) => {
13595
13698
  // Log the operation details if logging is enabled in GLOBAL_CONFIG
13596
13699
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
13597
13700
  swarm$1.loggerService.log(METHOD_NAME$x, {
13598
13701
  content,
13599
13702
  clientId,
13703
+ mode,
13600
13704
  });
13601
13705
  // Validate the session and swarm to ensure they exist and are accessible
13602
13706
  swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$x);
13603
13707
  const swarmName = swarm$1.sessionValidationService.getSwarm(clientId);
13604
13708
  swarm$1.swarmValidationService.validate(swarmName, METHOD_NAME$x);
13605
13709
  // Commit the user message to the agent's history via the session public service without checking the active agent
13606
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$x, clientId, swarmName);
13710
+ await swarm$1.sessionPublicService.commitUserMessage(content, mode, METHOD_NAME$x, clientId, swarmName);
13607
13711
  });
13608
13712
 
13609
13713
  /** @private Constant defining the method name for logging and validation context */
@@ -14446,13 +14550,13 @@ makeConnection.scheduled = (connector, clientId, swarmName, { delay = SCHEDULED_
14446
14550
  await online();
14447
14551
  if (payload) {
14448
14552
  return await PayloadContextService.runInContext(async () => {
14449
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$i, clientId, swarmName);
14553
+ await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$i, clientId, swarmName);
14450
14554
  }, {
14451
14555
  clientId,
14452
14556
  payload,
14453
14557
  });
14454
14558
  }
14455
- await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$i, clientId, swarmName);
14559
+ await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$i, clientId, swarmName);
14456
14560
  }),
14457
14561
  delay,
14458
14562
  });
@@ -14769,13 +14873,13 @@ session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
14769
14873
  await online();
14770
14874
  if (payload) {
14771
14875
  return await PayloadContextService.runInContext(async () => {
14772
- return await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$g, clientId, swarmName);
14876
+ return await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$g, clientId, swarmName);
14773
14877
  }, {
14774
14878
  clientId,
14775
14879
  payload,
14776
14880
  });
14777
14881
  }
14778
- return await swarm$1.sessionPublicService.commitUserMessage(content, METHOD_NAME$g, clientId, swarmName);
14882
+ return await swarm$1.sessionPublicService.commitUserMessage(content, "user", METHOD_NAME$g, clientId, swarmName);
14779
14883
  }),
14780
14884
  delay,
14781
14885
  });
@@ -17157,6 +17261,7 @@ const Utils = {
17157
17261
  PersistStorageUtils,
17158
17262
  PersistMemoryUtils,
17159
17263
  PersistAliveUtils,
17264
+ PersistPolicyUtils,
17160
17265
  };
17161
17266
 
17162
- export { Adapter, ExecutionContextService, History, HistoryMemoryInstance, HistoryPersistInstance, Logger, LoggerInstance, MethodContextService, PayloadContextService, PersistAlive, PersistBase, PersistList, PersistMemory, PersistState, PersistStorage, PersistSwarm, Policy, Schema, SharedState, SharedStorage, State, Storage, Utils, addAgent, addCompletion, addEmbedding, addPolicy, addState, addStorage, addSwarm, addTool, beginContext, cancelOutput, cancelOutputForce, changeToAgent, changeToDefaultAgent, changeToPrevAgent, commitAssistantMessage, commitAssistantMessageForce, commitFlush, commitFlushForce, commitStopTools, commitStopToolsForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, dumpAgent, dumpClientPerformance, dumpDocs, dumpPerfomance, dumpSwarm, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getPayload, getRawHistory, getSessionContext, getSessionMode, getUserHistory, listenAgentEvent, listenAgentEventOnce, listenEvent, listenEventOnce, listenExecutionEvent, listenExecutionEventOnce, listenHistoryEvent, listenHistoryEventOnce, listenPolicyEvent, listenPolicyEventOnce, listenSessionEvent, listenSessionEventOnce, listenStateEvent, listenStateEventOnce, listenStorageEvent, listenStorageEventOnce, listenSwarmEvent, listenSwarmEventOnce, makeAutoDispose, makeConnection, markOffline, markOnline, runStateless, runStatelessForce, session, setConfig, swarm };
17267
+ export { Adapter, ExecutionContextService, History, HistoryMemoryInstance, HistoryPersistInstance, Logger, LoggerInstance, MethodContextService, PayloadContextService, PersistAlive, PersistBase, PersistList, PersistMemory, PersistPolicy, PersistState, PersistStorage, PersistSwarm, Policy, Schema, SharedState, SharedStorage, State, Storage, Utils, addAgent, addCompletion, addEmbedding, addPolicy, addState, addStorage, addSwarm, addTool, beginContext, cancelOutput, cancelOutputForce, changeToAgent, changeToDefaultAgent, changeToPrevAgent, commitAssistantMessage, commitAssistantMessageForce, commitFlush, commitFlushForce, commitStopTools, commitStopToolsForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, dumpAgent, dumpClientPerformance, dumpDocs, dumpPerfomance, dumpSwarm, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getPayload, getRawHistory, getSessionContext, getSessionMode, getUserHistory, listenAgentEvent, listenAgentEventOnce, listenEvent, listenEventOnce, listenExecutionEvent, listenExecutionEventOnce, listenHistoryEvent, listenHistoryEventOnce, listenPolicyEvent, listenPolicyEventOnce, listenSessionEvent, listenSessionEventOnce, listenStateEvent, listenStateEventOnce, listenStorageEvent, listenStorageEventOnce, listenSwarmEvent, listenSwarmEventOnce, makeAutoDispose, makeConnection, markOffline, markOnline, runStateless, runStatelessForce, session, setConfig, swarm };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.173",
3
+ "version": "1.0.175",
4
4
  "description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -664,6 +664,8 @@ interface IPolicy {
664
664
  * Defines how policies enforce rules and manage bans within the swarm.
665
665
  */
666
666
  interface IPolicySchema {
667
+ /** Optional flag to enable serialization of banned clients to persistent storage (e.g., hard drive). */
668
+ persist?: boolean;
667
669
  /** Optional description for documentation purposes, aiding in policy usage understanding. */
668
670
  docDescription?: string;
669
671
  /** The unique name of the policy within the swarm. */
@@ -687,7 +689,7 @@ interface IPolicySchema {
687
689
  * @param {SwarmName} swarmName - The unique name of the swarm.
688
690
  * @returns {SessionId[] | Promise<SessionId[]>} An array of banned session IDs, synchronously or asynchronously.
689
691
  */
690
- getBannedClients: (policyName: PolicyName, swarmName: SwarmName) => SessionId[] | Promise<SessionId[]>;
692
+ getBannedClients?: (policyName: PolicyName, swarmName: SwarmName) => SessionId[] | Promise<SessionId[]>;
691
693
  /**
692
694
  * Optional function to set the list of banned clients.
693
695
  * Overrides default ban list management if provided.
@@ -1234,7 +1236,7 @@ interface ISession {
1234
1236
  * @returns {Promise<void>} A promise that resolves when the message is committed.
1235
1237
  * @throws {Error} If committing the message fails.
1236
1238
  */
1237
- commitUserMessage: (message: string) => Promise<void>;
1239
+ commitUserMessage: (message: string, mode: ExecutionMode) => Promise<void>;
1238
1240
  /**
1239
1241
  * Commits a flush operation to clear the session's agent history.
1240
1242
  * Resets the history to an initial state.
@@ -2283,6 +2285,77 @@ declare class PersistAliveUtils implements IPersistAliveControl {
2283
2285
  * @type {IPersistAliveControl}
2284
2286
  */
2285
2287
  declare const PersistAlive: IPersistAliveControl;
2288
+ /**
2289
+ * Defines the structure for policy data persistence in the swarm system.
2290
+ * Tracks banned clients (`SessionId`) within a `SwarmName` under a specific policy.
2291
+ * @interface IPersistPolicyData
2292
+ */
2293
+ interface IPersistPolicyData {
2294
+ /** Array of session IDs that are banned under this policy */
2295
+ bannedClients: SessionId[];
2296
+ }
2297
+ /**
2298
+ * Defines control methods for customizing policy persistence operations.
2299
+ * Allows injection of a custom persistence adapter for policy data tied to `SwarmName`.
2300
+ * @interface IPersistPolicyControl
2301
+ */
2302
+ interface IPersistPolicyControl {
2303
+ /**
2304
+ * Sets a custom persistence adapter for policy data storage.
2305
+ * Overrides the default `PersistBase` implementation for specialized behavior (e.g., in-memory tracking for `SwarmName`).
2306
+ * @param {TPersistBaseCtor<SwarmName, IPersistPolicyData>} Ctor - The constructor for the policy data persistence adapter.
2307
+ */
2308
+ usePersistPolicyAdapter(Ctor: TPersistBaseCtor<SwarmName, IPersistPolicyData>): void;
2309
+ }
2310
+ /**
2311
+ * Utility class for managing policy data persistence in the swarm system.
2312
+ * Provides methods to get and set banned clients within a `SwarmName`, with a customizable adapter.
2313
+ * @implements {IPersistPolicyControl}
2314
+ */
2315
+ declare class PersistPolicyUtils implements IPersistPolicyControl {
2316
+ /** @private Default constructor for policy data persistence, defaults to `PersistBase` */
2317
+ private PersistPolicyFactory;
2318
+ /**
2319
+ * Memoized function to create or retrieve storage for a specific policy data.
2320
+ * Ensures a single persistence instance per swarm, optimizing resource use.
2321
+ * @private
2322
+ * @param swarmName - The identifier of the swarm.
2323
+ * @returns A persistence instance for the policy data, rooted at `./logs/data/policy/`.
2324
+ */
2325
+ private getPolicyStorage;
2326
+ /**
2327
+ * Configures a custom constructor for policy data persistence, overriding the default `PersistBase`.
2328
+ * Enables advanced tracking (e.g., in-memory or database-backed persistence).
2329
+ * @param Ctor - The constructor to use for policy data storage, implementing `IPersistBase`.
2330
+ */
2331
+ usePersistPolicyAdapter(Ctor: TPersistBaseCtor<SwarmName, IPersistPolicyData>): void;
2332
+ /**
2333
+ * Retrieves the list of banned clients for a specific policy, defaulting to an empty array if unset.
2334
+ * Used to check client ban status in swarm workflows.
2335
+ * @param policyName - The identifier of the policy to check.
2336
+ * @param swarmName - The identifier of the swarm.
2337
+ * @param defaultValue - Optional default value if no banned clients are found.
2338
+ * @returns A promise resolving to an array of banned client session IDs.
2339
+ * @throws {Error} If reading from storage fails (e.g., file corruption).
2340
+ */
2341
+ getBannedClients: (policyName: PolicyName, swarmName: SwarmName, defaultValue?: SessionId[]) => Promise<SessionId[]>;
2342
+ /**
2343
+ * Sets the list of banned clients for a specific policy, persisting the status for future retrieval.
2344
+ * Used to manage client bans in swarm operations.
2345
+ * @param bannedClients - Array of session IDs to be banned under this policy.
2346
+ * @param policyName - The identifier of the policy to update.
2347
+ * @param swarmName - The identifier of the swarm.
2348
+ * @returns A promise that resolves when the banned clients list is persisted.
2349
+ * @throws {Error} If writing to storage fails (e.g., permissions or disk space).
2350
+ */
2351
+ setBannedClients: (bannedClients: SessionId[], policyName: PolicyName, swarmName: SwarmName) => Promise<void>;
2352
+ }
2353
+ /**
2354
+ * Exported singleton for policy persistence operations, cast as the control interface.
2355
+ * Provides a global point of access for managing client bans in the swarm.
2356
+ * @type {IPersistPolicyControl}
2357
+ */
2358
+ declare const PersistPolicy: IPersistPolicyControl;
2286
2359
 
2287
2360
  /**
2288
2361
  * Callbacks for managing history instance lifecycle and message handling.
@@ -3036,7 +3109,7 @@ interface IAgent {
3036
3109
  * @returns {Promise<void>} A promise that resolves when the message is committed.
3037
3110
  * @throws {Error} If committing the message fails.
3038
3111
  */
3039
- commitUserMessage(message: string): Promise<void>;
3112
+ commitUserMessage(message: string, mode: ExecutionMode): Promise<void>;
3040
3113
  /**
3041
3114
  * Commits an assistant message to the agent's history without triggering a response.
3042
3115
  * @param {string} message - The assistant message content to commit.
@@ -3294,7 +3367,7 @@ declare class ClientAgent implements IAgent {
3294
3367
  * @param {string} message - The user message to commit, trimmed before storage.
3295
3368
  * @returns {Promise<void>} Resolves when the message is committed to history and the event is emitted.
3296
3369
  */
3297
- commitUserMessage(message: string): Promise<void>;
3370
+ commitUserMessage(message: string, mode: ExecutionMode): Promise<void>;
3298
3371
  /**
3299
3372
  * Commits a flush of the agent’s history, clearing it and notifying the system via BusService.
3300
3373
  * Useful for resetting agent state, coordinated with HistoryConnectionService.
@@ -3503,7 +3576,7 @@ declare class AgentConnectionService implements IAgent {
3503
3576
  * @param {string} message - The user message to commit.
3504
3577
  * @returns {Promise<any>} A promise resolving to the commit result, type determined by ClientAgent’s implementation.
3505
3578
  */
3506
- commitUserMessage: (message: string) => Promise<void>;
3579
+ commitUserMessage: (message: string, mode: ExecutionMode) => Promise<void>;
3507
3580
  /**
3508
3581
  * Commits an agent change to prevent the next tool execution, altering the execution flow.
3509
3582
  * Delegates to ClientAgent.commitAgentChange, using context from MethodContextService, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
@@ -4175,7 +4248,7 @@ declare class ClientSession implements ISession {
4175
4248
  * @param {string} message - The user message to commit, typically from client input.
4176
4249
  * @returns {Promise<void>} Resolves when the message is committed and the event is logged.
4177
4250
  */
4178
- commitUserMessage(message: string): Promise<void>;
4251
+ commitUserMessage(message: string, mode: ExecutionMode): Promise<void>;
4179
4252
  /**
4180
4253
  * Commits a flush of the agent’s history via the swarm’s agent (ClientAgent), clearing it and logging via BusService.
4181
4254
  * Useful for resetting session state, coordinated with ClientHistory via ClientAgent.
@@ -4346,7 +4419,7 @@ declare class SessionConnectionService implements ISession {
4346
4419
  * @param {string} message - The user message to commit.
4347
4420
  * @returns {Promise<void>} A promise resolving when the user message is committed.
4348
4421
  */
4349
- commitUserMessage: (message: string) => Promise<void>;
4422
+ commitUserMessage: (message: string, mode: ExecutionMode) => Promise<void>;
4350
4423
  /**
4351
4424
  * Commits a flush of the session’s history, clearing stored data.
4352
4425
  * Delegates to ClientSession.commitFlush, using context from MethodContextService, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
@@ -4501,7 +4574,7 @@ declare class AgentPublicService implements TAgentConnectionService {
4501
4574
  * @param {AgentName} agentName - The agent name for identification.
4502
4575
  * @returns {Promise<unknown>} A promise resolving to the commit result.
4503
4576
  */
4504
- commitUserMessage: (message: string, methodName: string, clientId: string, agentName: AgentName) => Promise<void>;
4577
+ commitUserMessage: (message: string, mode: ExecutionMode, methodName: string, clientId: string, agentName: AgentName) => Promise<void>;
4505
4578
  /**
4506
4579
  * Commits a flush of the agent’s history, clearing stored data.
4507
4580
  * Wraps AgentConnectionService.commitFlush with MethodContextService, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
@@ -4790,7 +4863,7 @@ declare class SessionPublicService implements TSessionConnectionService {
4790
4863
  * @param {SwarmName} swarmName - The swarm name for context.
4791
4864
  * @returns {Promise<void>} A promise resolving when the user message is committed.
4792
4865
  */
4793
- commitUserMessage: (message: string, methodName: string, clientId: string, swarmName: SwarmName) => Promise<void>;
4866
+ commitUserMessage: (message: string, mode: ExecutionMode, methodName: string, clientId: string, swarmName: SwarmName) => Promise<void>;
4794
4867
  /**
4795
4868
  * Commits a flush of the session’s history, clearing stored data.
4796
4869
  * Wraps SessionConnectionService.commitFlush with MethodContextService, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
@@ -8255,7 +8328,7 @@ declare const commitFlush: (clientId: string, agentName: string) => Promise<void
8255
8328
  * @example
8256
8329
  * await commitUserMessage("User input message", "client-123", "AgentX");
8257
8330
  */
8258
- declare const commitUserMessage: (content: string, clientId: string, agentName: string) => Promise<void>;
8331
+ declare const commitUserMessage: (content: string, mode: ExecutionMode, clientId: string, agentName: string) => Promise<void>;
8259
8332
 
8260
8333
  /**
8261
8334
  * Commits the output of a tool execution to the active agent in a swarm session without checking the active agent.
@@ -8321,7 +8394,7 @@ declare const commitFlushForce: (clientId: string) => Promise<void>;
8321
8394
  * @example
8322
8395
  * await commitUserMessageForce("User input message", "client-123");
8323
8396
  */
8324
- declare const commitUserMessageForce: (content: string, clientId: string) => Promise<void>;
8397
+ declare const commitUserMessageForce: (content: string, mode: ExecutionMode, clientId: string) => Promise<void>;
8325
8398
 
8326
8399
  /**
8327
8400
  * Commits an assistant-generated message to the active agent in the swarm system.
@@ -9920,6 +9993,33 @@ interface IGlobalConfig {
9920
9993
  * });
9921
9994
  */
9922
9995
  CC_DEFAULT_STATE_GET: <T = any>(clientId: string, stateName: StateName, defaultState: T) => Promise<T>;
9996
+ /**
9997
+ * Default function to get banned clients for the policy
9998
+ * @param {PolicyName} policyName - The policy identifier.
9999
+ * @param {SwarmName} swarmName - The swarm identifier.
10000
+ * @example
10001
+ * setConfig({
10002
+ * CC_DEFAULT_POLICY_GET_BAN_CLIENTS: async () => []
10003
+ * });
10004
+ */
10005
+ CC_DEFAULT_POLICY_GET_BAN_CLIENTS: (policyName: PolicyName, swarmName: SwarmName) => Promise<SessionId[]> | SessionId[];
10006
+ /**
10007
+ * Retrieves the list of currently banned clients under this policy.
10008
+ * @param {PolicyName} policyName - The unique name of the policy.
10009
+ * @param {SwarmName} swarmName - The unique name of the swarm.
10010
+ * @returns {SessionId[] | Promise<SessionId[]>} An array of banned session IDs, synchronously or asynchronously.
10011
+ */
10012
+ CC_DEFAULT_POLICY_GET?: (policyName: PolicyName, swarmName: SwarmName) => SessionId[] | Promise<SessionId[]>;
10013
+ /**
10014
+ * Optional function to set the list of banned clients.
10015
+ * Overrides default ban list management if provided.
10016
+ * @param {SessionId[]} clientIds - An array of session IDs to ban.
10017
+ * @param {PolicyName} policyName - The unique name of the policy.
10018
+ * @param {SwarmName} swarmName - The unique name of the swarm.
10019
+ * @returns {Promise<void> | void} A promise that resolves when the ban list is updated, or void if synchronous.
10020
+ * @throws {Error} If updating the ban list fails (e.g., due to persistence issues).
10021
+ */
10022
+ CC_DEFAULT_POLICY_SET?: (clientIds: SessionId[], policyName: PolicyName, swarmName: SwarmName) => Promise<void> | void;
9923
10023
  /**
9924
10024
  * Default function to get storage data, used in `IStorage.take` for storage retrieval.
9925
10025
  * Returns `defaultValue` by default, allowing storage retrieval to be customized via `setConfig`, though not directly in `ClientAgent`.
@@ -10503,6 +10603,7 @@ declare const Utils: {
10503
10603
  PersistStorageUtils: typeof PersistStorageUtils;
10504
10604
  PersistMemoryUtils: typeof PersistMemoryUtils;
10505
10605
  PersistAliveUtils: typeof PersistAliveUtils;
10606
+ PersistPolicyUtils: typeof PersistPolicyUtils;
10506
10607
  };
10507
10608
 
10508
- export { Adapter, type EventSource, ExecutionContextService, History, HistoryMemoryInstance, HistoryPersistInstance, type IAgentSchema, type IAgentTool, type IBaseEvent, type IBusEvent, type IBusEventContext, type ICompletionArgs, type ICompletionSchema, type ICustomEvent, type IEmbeddingSchema, type IGlobalConfig, type IHistoryAdapter, type IHistoryControl, type IHistoryInstance, type IHistoryInstanceCallbacks, type IIncomingMessage, type ILoggerAdapter, type ILoggerInstance, type ILoggerInstanceCallbacks, type IMakeConnectionConfig, type IMakeDisposeParams, type IModelMessage, type IOutgoingMessage, type IPersistBase, type IPolicySchema, type ISessionConfig, type IStateSchema, type IStorageSchema, type ISwarmSchema, type ITool, type IToolCall, Logger, LoggerInstance, MethodContextService, PayloadContextService, PersistAlive, PersistBase, PersistList, PersistMemory, PersistState, PersistStorage, PersistSwarm, Policy, type ReceiveMessageFn, Schema, type SendMessageFn, SharedState, SharedStorage, State, Storage, type THistoryInstanceCtor, type THistoryMemoryInstance, type THistoryPersistInstance, type TLoggerInstance, type TPersistBase, type TPersistBaseCtor, type TPersistList, Utils, addAgent, addCompletion, addEmbedding, addPolicy, addState, addStorage, addSwarm, addTool, beginContext, cancelOutput, cancelOutputForce, changeToAgent, changeToDefaultAgent, changeToPrevAgent, commitAssistantMessage, commitAssistantMessageForce, commitFlush, commitFlushForce, commitStopTools, commitStopToolsForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, dumpAgent, dumpClientPerformance, dumpDocs, dumpPerfomance, dumpSwarm, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getPayload, getRawHistory, getSessionContext, getSessionMode, getUserHistory, listenAgentEvent, listenAgentEventOnce, listenEvent, listenEventOnce, listenExecutionEvent, listenExecutionEventOnce, listenHistoryEvent, listenHistoryEventOnce, listenPolicyEvent, listenPolicyEventOnce, listenSessionEvent, listenSessionEventOnce, listenStateEvent, listenStateEventOnce, listenStorageEvent, listenStorageEventOnce, listenSwarmEvent, listenSwarmEventOnce, makeAutoDispose, makeConnection, markOffline, markOnline, runStateless, runStatelessForce, session, setConfig, swarm };
10609
+ export { Adapter, type EventSource, ExecutionContextService, History, HistoryMemoryInstance, HistoryPersistInstance, type IAgentSchema, type IAgentTool, type IBaseEvent, type IBusEvent, type IBusEventContext, type ICompletionArgs, type ICompletionSchema, type ICustomEvent, type IEmbeddingSchema, type IGlobalConfig, type IHistoryAdapter, type IHistoryControl, type IHistoryInstance, type IHistoryInstanceCallbacks, type IIncomingMessage, type ILoggerAdapter, type ILoggerInstance, type ILoggerInstanceCallbacks, type IMakeConnectionConfig, type IMakeDisposeParams, type IModelMessage, type IOutgoingMessage, type IPersistBase, type IPolicySchema, type ISessionConfig, type IStateSchema, type IStorageSchema, type ISwarmSchema, type ITool, type IToolCall, Logger, LoggerInstance, MethodContextService, PayloadContextService, PersistAlive, PersistBase, PersistList, PersistMemory, PersistPolicy, PersistState, PersistStorage, PersistSwarm, Policy, type ReceiveMessageFn, Schema, type SendMessageFn, SharedState, SharedStorage, State, Storage, type THistoryInstanceCtor, type THistoryMemoryInstance, type THistoryPersistInstance, type TLoggerInstance, type TPersistBase, type TPersistBaseCtor, type TPersistList, Utils, addAgent, addCompletion, addEmbedding, addPolicy, addState, addStorage, addSwarm, addTool, beginContext, cancelOutput, cancelOutputForce, changeToAgent, changeToDefaultAgent, changeToPrevAgent, commitAssistantMessage, commitAssistantMessageForce, commitFlush, commitFlushForce, commitStopTools, commitStopToolsForce, commitSystemMessage, commitSystemMessageForce, commitToolOutput, commitToolOutputForce, commitUserMessage, commitUserMessageForce, complete, disposeConnection, dumpAgent, dumpClientPerformance, dumpDocs, dumpPerfomance, dumpSwarm, emit, emitForce, event, execute, executeForce, getAgentHistory, getAgentName, getAssistantHistory, getLastAssistantMessage, getLastSystemMessage, getLastUserMessage, getPayload, getRawHistory, getSessionContext, getSessionMode, getUserHistory, listenAgentEvent, listenAgentEventOnce, listenEvent, listenEventOnce, listenExecutionEvent, listenExecutionEventOnce, listenHistoryEvent, listenHistoryEventOnce, listenPolicyEvent, listenPolicyEventOnce, listenSessionEvent, listenSessionEventOnce, listenStateEvent, listenStateEventOnce, listenStorageEvent, listenStorageEventOnce, listenSwarmEvent, listenSwarmEventOnce, makeAutoDispose, makeConnection, markOffline, markOnline, runStateless, runStatelessForce, session, setConfig, swarm };