agent-swarm-kit 1.0.61 → 1.0.63

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
@@ -123,6 +123,23 @@ function __spreadArray(to, from, pack) {
123
123
  return to.concat(ar || Array.prototype.slice.call(from));
124
124
  }
125
125
 
126
+ function __await(v) {
127
+ return this instanceof __await ? (this.v = v, this) : new __await(v);
128
+ }
129
+
130
+ function __asyncGenerator(thisArg, _arguments, generator) {
131
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
132
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
133
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
134
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
135
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
136
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
137
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
138
+ function fulfill(value) { resume("next", value); }
139
+ function reject(value) { resume("throw", value); }
140
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
141
+ }
142
+
126
143
  function __asyncValues(o) {
127
144
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
128
145
  var m = o[Symbol.asyncIterator], i;
@@ -380,6 +397,439 @@ var removeXmlTags = function (input) {
380
397
  .trim();
381
398
  };
382
399
 
400
+ /**
401
+ * Class representing a History Instance
402
+ */
403
+ var HistoryInstance = /** @class */ (function () {
404
+ /**
405
+ * Create a HistoryInstance.
406
+ * @param clientId - The client ID.
407
+ * @param callbacks - The callbacks for the history instance.
408
+ */
409
+ function HistoryInstance(clientId, callbacks) {
410
+ var _this = this;
411
+ this.clientId = clientId;
412
+ this.callbacks = callbacks;
413
+ this._array = [];
414
+ /**
415
+ * Wait for the history to initialize.
416
+ * @param agentName - The agent name.
417
+ */
418
+ this.waitForInit = functoolsKit.singleshot(function (agentName) { return __awaiter(_this, void 0, void 0, function () {
419
+ var _a;
420
+ return __generator(this, function (_b) {
421
+ switch (_b.label) {
422
+ case 0:
423
+ swarm.loggerService.log("HistoryInstance waitForInit", {
424
+ clientId: this.clientId,
425
+ agentName: agentName,
426
+ });
427
+ if (!this.callbacks.getData) return [3 /*break*/, 2];
428
+ _a = this;
429
+ return [4 /*yield*/, this.callbacks.getData(this.clientId, agentName)];
430
+ case 1:
431
+ _a._array = _b.sent();
432
+ _b.label = 2;
433
+ case 2: return [2 /*return*/];
434
+ }
435
+ });
436
+ }); });
437
+ /**
438
+ * Push a new message to the history for a given agent.
439
+ * @param value - The model message to push.
440
+ * @param agentName - The agent name.
441
+ * @returns A promise that resolves when the message is pushed.
442
+ */
443
+ this.push = function (value, agentName) { return __awaiter(_this, void 0, void 0, function () {
444
+ return __generator(this, function (_a) {
445
+ swarm.loggerService.log("HistoryInstance push", {
446
+ clientId: this.clientId,
447
+ agentName: agentName,
448
+ });
449
+ this._array.push(value);
450
+ this.callbacks.onChange &&
451
+ this.callbacks.onChange(this._array, this.clientId, agentName);
452
+ return [2 /*return*/, Promise.resolve()];
453
+ });
454
+ }); };
455
+ /**
456
+ * Dispose of the history for a given agent.
457
+ * @param agentName - The agent name or null.
458
+ * @returns A promise that resolves when the history is disposed.
459
+ */
460
+ this.dispose = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
461
+ return __generator(this, function (_a) {
462
+ swarm.loggerService.log("HistoryInstance dispose", {
463
+ clientId: this.clientId,
464
+ agentName: agentName,
465
+ });
466
+ if (agentName === null) {
467
+ this.callbacks.onDispose && this.callbacks.onDispose(this.clientId);
468
+ this._array = [];
469
+ }
470
+ return [2 /*return*/, Promise.resolve()];
471
+ });
472
+ }); };
473
+ swarm.loggerService.log("HistoryInstance CTOR", {
474
+ clientId: this.clientId,
475
+ });
476
+ if (callbacks.onInit) {
477
+ callbacks.onInit(clientId);
478
+ }
479
+ if (callbacks.filterCondition) {
480
+ this.iterate = function (agentName) {
481
+ return __asyncGenerator(this, arguments, function () {
482
+ var _a, _b, item, e_1_1, _c, _d, item, e_2_1;
483
+ var e_1, _e, e_2, _f;
484
+ return __generator(this, function (_g) {
485
+ switch (_g.label) {
486
+ case 0:
487
+ swarm.loggerService.log("HistoryInstance iterate condition", {
488
+ clientId: this.clientId,
489
+ agentName: agentName,
490
+ });
491
+ if (!this.callbacks.onRead) return [3 /*break*/, 12];
492
+ this.callbacks.onReadBegin &&
493
+ this.callbacks.onReadBegin(this.clientId, agentName);
494
+ _g.label = 1;
495
+ case 1:
496
+ _g.trys.push([1, 8, 9, 10]);
497
+ _a = __values(this._array), _b = _a.next();
498
+ _g.label = 2;
499
+ case 2:
500
+ if (!!_b.done) return [3 /*break*/, 7];
501
+ item = _b.value;
502
+ return [4 /*yield*/, __await(this.callbacks.filterCondition(item, this.clientId, agentName))];
503
+ case 3:
504
+ if (!_g.sent()) return [3 /*break*/, 6];
505
+ this.callbacks.onRead(item, this.clientId, agentName);
506
+ return [4 /*yield*/, __await(item)];
507
+ case 4: return [4 /*yield*/, _g.sent()];
508
+ case 5:
509
+ _g.sent();
510
+ _g.label = 6;
511
+ case 6:
512
+ _b = _a.next();
513
+ return [3 /*break*/, 2];
514
+ case 7: return [3 /*break*/, 10];
515
+ case 8:
516
+ e_1_1 = _g.sent();
517
+ e_1 = { error: e_1_1 };
518
+ return [3 /*break*/, 10];
519
+ case 9:
520
+ try {
521
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
522
+ }
523
+ finally { if (e_1) throw e_1.error; }
524
+ return [7 /*endfinally*/];
525
+ case 10:
526
+ this.callbacks.onReadEnd &&
527
+ this.callbacks.onReadEnd(this.clientId, agentName);
528
+ return [4 /*yield*/, __await(void 0)];
529
+ case 11: return [2 /*return*/, _g.sent()];
530
+ case 12:
531
+ this.callbacks.onReadBegin &&
532
+ this.callbacks.onReadBegin(this.clientId, agentName);
533
+ _g.label = 13;
534
+ case 13:
535
+ _g.trys.push([13, 20, 21, 22]);
536
+ _c = __values(this._array), _d = _c.next();
537
+ _g.label = 14;
538
+ case 14:
539
+ if (!!_d.done) return [3 /*break*/, 19];
540
+ item = _d.value;
541
+ return [4 /*yield*/, __await(this.callbacks.filterCondition(item, this.clientId, agentName))];
542
+ case 15:
543
+ if (!_g.sent()) return [3 /*break*/, 18];
544
+ return [4 /*yield*/, __await(item)];
545
+ case 16: return [4 /*yield*/, _g.sent()];
546
+ case 17:
547
+ _g.sent();
548
+ _g.label = 18;
549
+ case 18:
550
+ _d = _c.next();
551
+ return [3 /*break*/, 14];
552
+ case 19: return [3 /*break*/, 22];
553
+ case 20:
554
+ e_2_1 = _g.sent();
555
+ e_2 = { error: e_2_1 };
556
+ return [3 /*break*/, 22];
557
+ case 21:
558
+ try {
559
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
560
+ }
561
+ finally { if (e_2) throw e_2.error; }
562
+ return [7 /*endfinally*/];
563
+ case 22:
564
+ this.callbacks.onReadEnd &&
565
+ this.callbacks.onReadEnd(this.clientId, agentName);
566
+ return [2 /*return*/];
567
+ }
568
+ });
569
+ });
570
+ };
571
+ }
572
+ }
573
+ /**
574
+ * Iterate over the history messages for a given agent.
575
+ * @param agentName - The agent name.
576
+ * @returns An async iterable iterator of model messages.
577
+ */
578
+ HistoryInstance.prototype.iterate = function (agentName) {
579
+ return __asyncGenerator(this, arguments, function iterate_1() {
580
+ var _a, _b, item, e_3_1, _c, _d, item, e_4_1;
581
+ var e_3, _e, e_4, _f;
582
+ return __generator(this, function (_g) {
583
+ switch (_g.label) {
584
+ case 0:
585
+ swarm.loggerService.log("HistoryInstance iterate", {
586
+ clientId: this.clientId,
587
+ agentName: agentName,
588
+ });
589
+ if (!this.callbacks.onRead) return [3 /*break*/, 11];
590
+ this.callbacks.onReadBegin &&
591
+ this.callbacks.onReadBegin(this.clientId, agentName);
592
+ _g.label = 1;
593
+ case 1:
594
+ _g.trys.push([1, 7, 8, 9]);
595
+ _a = __values(this._array), _b = _a.next();
596
+ _g.label = 2;
597
+ case 2:
598
+ if (!!_b.done) return [3 /*break*/, 6];
599
+ item = _b.value;
600
+ this.callbacks.onRead(item, this.clientId, agentName);
601
+ return [4 /*yield*/, __await(item)];
602
+ case 3: return [4 /*yield*/, _g.sent()];
603
+ case 4:
604
+ _g.sent();
605
+ _g.label = 5;
606
+ case 5:
607
+ _b = _a.next();
608
+ return [3 /*break*/, 2];
609
+ case 6: return [3 /*break*/, 9];
610
+ case 7:
611
+ e_3_1 = _g.sent();
612
+ e_3 = { error: e_3_1 };
613
+ return [3 /*break*/, 9];
614
+ case 8:
615
+ try {
616
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
617
+ }
618
+ finally { if (e_3) throw e_3.error; }
619
+ return [7 /*endfinally*/];
620
+ case 9:
621
+ this.callbacks.onReadEnd &&
622
+ this.callbacks.onReadEnd(this.clientId, agentName);
623
+ return [4 /*yield*/, __await(void 0)];
624
+ case 10: return [2 /*return*/, _g.sent()];
625
+ case 11:
626
+ this.callbacks.onReadBegin &&
627
+ this.callbacks.onReadBegin(this.clientId, agentName);
628
+ _g.label = 12;
629
+ case 12:
630
+ _g.trys.push([12, 18, 19, 20]);
631
+ _c = __values(this._array), _d = _c.next();
632
+ _g.label = 13;
633
+ case 13:
634
+ if (!!_d.done) return [3 /*break*/, 17];
635
+ item = _d.value;
636
+ return [4 /*yield*/, __await(item)];
637
+ case 14: return [4 /*yield*/, _g.sent()];
638
+ case 15:
639
+ _g.sent();
640
+ _g.label = 16;
641
+ case 16:
642
+ _d = _c.next();
643
+ return [3 /*break*/, 13];
644
+ case 17: return [3 /*break*/, 20];
645
+ case 18:
646
+ e_4_1 = _g.sent();
647
+ e_4 = { error: e_4_1 };
648
+ return [3 /*break*/, 20];
649
+ case 19:
650
+ try {
651
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
652
+ }
653
+ finally { if (e_4) throw e_4.error; }
654
+ return [7 /*endfinally*/];
655
+ case 20:
656
+ this.callbacks.onReadEnd &&
657
+ this.callbacks.onReadEnd(this.clientId, agentName);
658
+ return [2 /*return*/];
659
+ }
660
+ });
661
+ });
662
+ };
663
+ return HistoryInstance;
664
+ }());
665
+ /**
666
+ * Class representing History Utilities
667
+ */
668
+ var HistoryUtils = /** @class */ (function () {
669
+ function HistoryUtils() {
670
+ var _this = this;
671
+ this.HistoryFactory = HistoryInstance;
672
+ this.HistoryCallbacks = {};
673
+ this.getHistory = functoolsKit.memoize(function (_a) {
674
+ var _b = __read(_a, 1), clientId = _b[0];
675
+ return clientId;
676
+ }, function (clientId) {
677
+ return new _this.HistoryFactory(clientId, _this.HistoryCallbacks);
678
+ });
679
+ /**
680
+ * Use a custom history adapter.
681
+ * @param Ctor - The constructor for the history instance.
682
+ */
683
+ this.useHistoryAdapter = function (Ctor) {
684
+ swarm.loggerService.log("HistoryUtils useHistoryAdapter");
685
+ _this.HistoryFactory = Ctor;
686
+ };
687
+ /**
688
+ * Use history lifecycle callbacks.
689
+ * @param Callbacks - The callbacks dictionary.
690
+ */
691
+ this.useHistoryCallbacks = function (Callbacks) {
692
+ swarm.loggerService.log("HistoryUtils useHistoryCallbacks");
693
+ _this.HistoryCallbacks = Callbacks;
694
+ };
695
+ /**
696
+ * Push a new message to the history.
697
+ * @param value - The model message to push.
698
+ * @param clientId - The client ID.
699
+ * @param agentName - The agent name.
700
+ * @returns A promise that resolves when the message is pushed.
701
+ */
702
+ this.push = function (value, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
703
+ var isInitial, history;
704
+ return __generator(this, function (_a) {
705
+ switch (_a.label) {
706
+ case 0:
707
+ swarm.loggerService.log("HistoryUtils push", {
708
+ clientId: clientId,
709
+ agentName: agentName,
710
+ value: value,
711
+ });
712
+ isInitial = this.getHistory.has(clientId);
713
+ return [4 /*yield*/, this.getHistory(clientId)];
714
+ case 1:
715
+ history = _a.sent();
716
+ return [4 /*yield*/, history.waitForInit(agentName, isInitial)];
717
+ case 2:
718
+ _a.sent();
719
+ return [4 /*yield*/, history.push(value, agentName)];
720
+ case 3: return [2 /*return*/, _a.sent()];
721
+ }
722
+ });
723
+ }); };
724
+ /**
725
+ * Dispose of the history for a given client and agent.
726
+ * @param clientId - The client ID.
727
+ * @param agentName - The agent name or null.
728
+ * @returns A promise that resolves when the history is disposed.
729
+ */
730
+ this.dispose = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
731
+ var isInitial, history;
732
+ return __generator(this, function (_a) {
733
+ switch (_a.label) {
734
+ case 0:
735
+ swarm.loggerService.log("HistoryUtils dispose", {
736
+ clientId: clientId,
737
+ agentName: agentName,
738
+ });
739
+ isInitial = this.getHistory.has(clientId);
740
+ return [4 /*yield*/, this.getHistory(clientId)];
741
+ case 1:
742
+ history = _a.sent();
743
+ return [4 /*yield*/, history.waitForInit(agentName, isInitial)];
744
+ case 2:
745
+ _a.sent();
746
+ return [4 /*yield*/, history.dispose(agentName)];
747
+ case 3:
748
+ _a.sent();
749
+ if (agentName === null) {
750
+ this.getHistory.clear(clientId);
751
+ }
752
+ return [2 /*return*/];
753
+ }
754
+ });
755
+ }); };
756
+ }
757
+ /**
758
+ * Iterate over the history messages.
759
+ * @param clientId - The client ID.
760
+ * @param agentName - The agent name.
761
+ * @returns An async iterable iterator of model messages.
762
+ */
763
+ HistoryUtils.prototype.iterate = function (clientId, agentName) {
764
+ return __asyncGenerator(this, arguments, function iterate_2() {
765
+ var isInitial, history, _a, _b, _c, item, e_5_1;
766
+ var _d, e_5, _e, _f;
767
+ return __generator(this, function (_g) {
768
+ switch (_g.label) {
769
+ case 0:
770
+ swarm.loggerService.log("HistoryUtils iterate", {
771
+ clientId: clientId,
772
+ agentName: agentName,
773
+ });
774
+ isInitial = this.getHistory.has(clientId);
775
+ return [4 /*yield*/, __await(this.getHistory(clientId))];
776
+ case 1:
777
+ history = _g.sent();
778
+ return [4 /*yield*/, __await(history.waitForInit(agentName, isInitial))];
779
+ case 2:
780
+ _g.sent();
781
+ _g.label = 3;
782
+ case 3:
783
+ _g.trys.push([3, 10, 11, 16]);
784
+ _a = true, _b = __asyncValues(history.iterate(agentName));
785
+ _g.label = 4;
786
+ case 4: return [4 /*yield*/, __await(_b.next())];
787
+ case 5:
788
+ if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 9];
789
+ _f = _c.value;
790
+ _a = false;
791
+ item = _f;
792
+ return [4 /*yield*/, __await(item)];
793
+ case 6: return [4 /*yield*/, _g.sent()];
794
+ case 7:
795
+ _g.sent();
796
+ _g.label = 8;
797
+ case 8:
798
+ _a = true;
799
+ return [3 /*break*/, 4];
800
+ case 9: return [3 /*break*/, 16];
801
+ case 10:
802
+ e_5_1 = _g.sent();
803
+ e_5 = { error: e_5_1 };
804
+ return [3 /*break*/, 16];
805
+ case 11:
806
+ _g.trys.push([11, , 14, 15]);
807
+ if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 13];
808
+ return [4 /*yield*/, __await(_e.call(_b))];
809
+ case 12:
810
+ _g.sent();
811
+ _g.label = 13;
812
+ case 13: return [3 /*break*/, 15];
813
+ case 14:
814
+ if (e_5) throw e_5.error;
815
+ return [7 /*endfinally*/];
816
+ case 15: return [7 /*endfinally*/];
817
+ case 16: return [2 /*return*/];
818
+ }
819
+ });
820
+ });
821
+ };
822
+ return HistoryUtils;
823
+ }());
824
+ /**
825
+ * Exported History Adapter instance
826
+ */
827
+ var HistoryAdapter = new HistoryUtils();
828
+ /**
829
+ * Exported History Control instance
830
+ */
831
+ var History = HistoryAdapter;
832
+
383
833
  /**
384
834
  * @description `ask for agent function` in `llama3.1:8b` to troubleshoot (need CC_OLLAMA_EMIT_TOOL_PROTOCOL to be turned off)
385
835
  */
@@ -432,9 +882,8 @@ var CC_AGENT_HISTORY_FILTER = function (agentName) {
432
882
  };
433
883
  var CC_AGENT_OUTPUT_TRANSFORM = removeXmlTags;
434
884
  var CC_KEEP_MESSAGES = 5;
435
- var CC_GET_AGENT_HISTORY = function () { return new functoolsKit.PubsubArrayAdapter(); };
885
+ var CC_GET_AGENT_HISTORY_ADAPTER = function () { return HistoryAdapter; };
436
886
  var CC_AGENT_OUTPUT_MAP = function (message) { return message; };
437
- var CC_AGENT_SEPARATE_HISTORY = false;
438
887
  var CC_AGENT_SYSTEM_PROMPT = undefined;
439
888
  var CC_STORAGE_SEARCH_SIMILARITY = 0.65;
440
889
  var CC_STORAGE_SEARCH_POOL = 5;
@@ -442,7 +891,7 @@ var GLOBAL_CONFIG = {
442
891
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
443
892
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
444
893
  CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
445
- CC_GET_AGENT_HISTORY: CC_GET_AGENT_HISTORY,
894
+ CC_GET_AGENT_HISTORY_ADAPTER: CC_GET_AGENT_HISTORY_ADAPTER,
446
895
  CC_SWARM_AGENT_CHANGED: CC_SWARM_AGENT_CHANGED,
447
896
  CC_SWARM_DEFAULT_AGENT: CC_SWARM_DEFAULT_AGENT,
448
897
  CC_AGENT_DEFAULT_VALIDATION: CC_AGENT_DEFAULT_VALIDATION,
@@ -450,7 +899,6 @@ var GLOBAL_CONFIG = {
450
899
  CC_AGENT_OUTPUT_TRANSFORM: CC_AGENT_OUTPUT_TRANSFORM,
451
900
  CC_AGENT_OUTPUT_MAP: CC_AGENT_OUTPUT_MAP,
452
901
  CC_AGENT_SYSTEM_PROMPT: CC_AGENT_SYSTEM_PROMPT,
453
- CC_AGENT_SEPARATE_HISTORY: CC_AGENT_SEPARATE_HISTORY,
454
902
  CC_AGENT_DISALLOWED_TAGS: CC_AGENT_DISALLOWED_TAGS,
455
903
  CC_AGENT_DISALLOWED_SYMBOLS: CC_AGENT_DISALLOWED_SYMBOLS,
456
904
  CC_STORAGE_SEARCH_SIMILARITY: CC_STORAGE_SEARCH_SIMILARITY,
@@ -1149,7 +1597,7 @@ var ClientHistory = /** @class */ (function () {
1149
1597
  switch (_a.label) {
1150
1598
  case 0:
1151
1599
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " push"), { message: message });
1152
- return [4 /*yield*/, this.params.items.push(message)];
1600
+ return [4 /*yield*/, this.params.items.push(message, this.params.clientId, this.params.agentName)];
1153
1601
  case 1:
1154
1602
  _a.sent();
1155
1603
  return [2 /*return*/];
@@ -1171,7 +1619,7 @@ var ClientHistory = /** @class */ (function () {
1171
1619
  _g.label = 1;
1172
1620
  case 1:
1173
1621
  _g.trys.push([1, 6, 7, 12]);
1174
- _a = true, _b = __asyncValues(this.params.items);
1622
+ _a = true, _b = __asyncValues(this.params.items.iterate(this.params.clientId, this.params.agentName));
1175
1623
  _g.label = 2;
1176
1624
  case 2: return [4 /*yield*/, _b.next()];
1177
1625
  case 3:
@@ -1225,7 +1673,7 @@ var ClientHistory = /** @class */ (function () {
1225
1673
  _h.label = 1;
1226
1674
  case 1:
1227
1675
  _h.trys.push([1, 6, 7, 12]);
1228
- _a = true, _b = __asyncValues(this.params.items);
1676
+ _a = true, _b = __asyncValues(this.params.items.iterate(this.params.clientId, this.params.agentName));
1229
1677
  _h.label = 2;
1230
1678
  case 2: return [4 /*yield*/, _b.next()];
1231
1679
  case 3:
@@ -1352,6 +1800,22 @@ var ClientHistory = /** @class */ (function () {
1352
1800
  }
1353
1801
  });
1354
1802
  }); };
1803
+ /**
1804
+ * Should call on agent dispose
1805
+ * @returns {Promise<void>}
1806
+ */
1807
+ this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
1808
+ return __generator(this, function (_a) {
1809
+ switch (_a.label) {
1810
+ case 0:
1811
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " dispose"));
1812
+ return [4 /*yield*/, this.params.items.dispose(this.params.clientId, this.params.agentName)];
1813
+ case 1:
1814
+ _a.sent();
1815
+ return [2 /*return*/];
1816
+ }
1817
+ });
1818
+ }); };
1355
1819
  this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"), {
1356
1820
  params: params,
1357
1821
  });
@@ -1370,18 +1834,6 @@ var HistoryConnectionService = /** @class */ (function () {
1370
1834
  this.loggerService = inject(TYPES.loggerService);
1371
1835
  this.contextService = inject(TYPES.contextService);
1372
1836
  this.sessionValidationService = inject(TYPES.sessionValidationService);
1373
- /**
1374
- * Retrieves items for a given client and agent.
1375
- * @param {string} clientId - The client ID.
1376
- * @param {AgentName} agentName - The agent name.
1377
- * @returns {IPubsubArray<IModelMessage>} The items.
1378
- */
1379
- this.getItems = functoolsKit.memoize(function (_a) {
1380
- var _b = __read(_a, 1), clientId = _b[0];
1381
- return clientId;
1382
- }, function (clientId, agentName) {
1383
- return GLOBAL_CONFIG.CC_GET_AGENT_HISTORY(clientId, agentName);
1384
- });
1385
1837
  /**
1386
1838
  * Retrieves the history for a given client and agent.
1387
1839
  * @param {string} clientId - The client ID.
@@ -1396,7 +1848,7 @@ var HistoryConnectionService = /** @class */ (function () {
1396
1848
  return new ClientHistory({
1397
1849
  clientId: clientId,
1398
1850
  agentName: agentName,
1399
- items: _this.getItems(clientId, agentName),
1851
+ items: GLOBAL_CONFIG.CC_GET_AGENT_HISTORY_ADAPTER(clientId, agentName),
1400
1852
  logger: _this.loggerService,
1401
1853
  });
1402
1854
  });
@@ -1466,13 +1918,9 @@ var HistoryConnectionService = /** @class */ (function () {
1466
1918
  if (!this.getHistory.has(key)) {
1467
1919
  return [2 /*return*/];
1468
1920
  }
1469
- if (!GLOBAL_CONFIG.CC_AGENT_SEPARATE_HISTORY) return [3 /*break*/, 2];
1470
- return [4 /*yield*/, this.getItems(this.contextService.context.clientId, this.contextService.context.agentName).clear()];
1921
+ return [4 /*yield*/, this.getHistory(this.contextService.context.clientId, this.contextService.context.agentName).dispose()];
1471
1922
  case 1:
1472
1923
  _a.sent();
1473
- this.getItems.clear(this.contextService.context.clientId);
1474
- _a.label = 2;
1475
- case 2:
1476
1924
  this.getHistory.clear(key);
1477
1925
  this.sessionValidationService.removeHistoryUsage(this.contextService.context.clientId, this.contextService.context.agentName);
1478
1926
  return [2 /*return*/];
@@ -3399,9 +3847,10 @@ var SessionValidationService = /** @class */ (function () {
3399
3847
  function SessionValidationService() {
3400
3848
  var _this = this;
3401
3849
  this.loggerService = inject(TYPES.loggerService);
3850
+ this._storageSwarmMap = new Map();
3402
3851
  this._historySwarmMap = new Map();
3403
- this._sessionSwarmMap = new Map();
3404
3852
  this._agentSwarmMap = new Map();
3853
+ this._sessionSwarmMap = new Map();
3405
3854
  this._sessionModeMap = new Map();
3406
3855
  /**
3407
3856
  * Adds a new session.
@@ -3460,6 +3909,26 @@ var SessionValidationService = /** @class */ (function () {
3460
3909
  _this._historySwarmMap.set(sessionId, [agentName]);
3461
3910
  }
3462
3911
  };
3912
+ /**
3913
+ * Adds a storage usage to a session.
3914
+ * @param {SessionId} sessionId - The ID of the session.
3915
+ * @param {StorageName} storageName - The name of the storage.
3916
+ */
3917
+ this.addStorageUsage = function (sessionId, storageName) {
3918
+ _this.loggerService.log("sessionValidationService addStorageUsage", {
3919
+ sessionId: sessionId,
3920
+ storageName: storageName,
3921
+ });
3922
+ if (_this._storageSwarmMap.has(sessionId)) {
3923
+ var storages = _this._storageSwarmMap.get(sessionId);
3924
+ if (!storages.includes(storageName)) {
3925
+ storages.push(storageName);
3926
+ }
3927
+ }
3928
+ else {
3929
+ _this._storageSwarmMap.set(sessionId, [storageName]);
3930
+ }
3931
+ };
3463
3932
  /**
3464
3933
  * Removes an agent usage from a session.
3465
3934
  * @param {SessionId} sessionId - The ID of the session.
@@ -3510,6 +3979,31 @@ var SessionValidationService = /** @class */ (function () {
3510
3979
  throw new Error("No agents found for sessionId=".concat(sessionId));
3511
3980
  }
3512
3981
  };
3982
+ /**
3983
+ * Removes a storage usage from a session.
3984
+ * @param {SessionId} sessionId - The ID of the session.
3985
+ * @param {StorageName} storageName - The name of the storage.
3986
+ * @throws Will throw an error if no storages are found for the session.
3987
+ */
3988
+ this.removeStorageUsage = function (sessionId, storageName) {
3989
+ _this.loggerService.log("sessionValidationService removeStorageUsage", {
3990
+ sessionId: sessionId,
3991
+ storageName: storageName,
3992
+ });
3993
+ if (_this._storageSwarmMap.has(sessionId)) {
3994
+ var agents = _this._storageSwarmMap.get(sessionId);
3995
+ var agentIndex = agents.indexOf(storageName);
3996
+ if (agentIndex !== -1) {
3997
+ agents.splice(agentIndex, 1);
3998
+ }
3999
+ if (agents.length === 0) {
4000
+ _this._storageSwarmMap.delete(sessionId);
4001
+ }
4002
+ }
4003
+ else {
4004
+ throw new Error("No agents found for sessionId=".concat(sessionId));
4005
+ }
4006
+ };
3513
4007
  /**
3514
4008
  * Gets the mode of a session.
3515
4009
  * @param {SessionId} clientId - The ID of the client.
@@ -4040,6 +4534,7 @@ var StorageConnectionService = /** @class */ (function () {
4040
4534
  this.loggerService = inject(TYPES.loggerService);
4041
4535
  this.contextService = inject(TYPES.contextService);
4042
4536
  this.storageSchemaService = inject(TYPES.storageSchemaService);
4537
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
4043
4538
  this.embeddingSchemaService = inject(TYPES.embeddingSchemaService);
4044
4539
  /**
4045
4540
  * Retrieves a storage instance based on client ID and storage name.
@@ -4051,6 +4546,7 @@ var StorageConnectionService = /** @class */ (function () {
4051
4546
  var _b = __read(_a, 2), clientId = _b[0], storageName = _b[1];
4052
4547
  return "".concat(clientId, "-").concat(storageName);
4053
4548
  }, function (clientId, storageName) {
4549
+ _this.sessionValidationService.addStorageUsage(clientId, storageName);
4054
4550
  var _a = _this.storageSchemaService.get(storageName), createIndex = _a.createIndex, getData = _a.getData, embeddingName = _a.embedding, callbacks = _a.callbacks;
4055
4551
  var _b = _this.embeddingSchemaService.get(embeddingName), calculateSimilarity = _b.calculateSimilarity, createEmbedding = _b.createEmbedding, embedding = _b.callbacks;
4056
4552
  return new ClientStorage(__assign(__assign({ clientId: clientId, storageName: storageName, embedding: embeddingName, calculateSimilarity: calculateSimilarity, createEmbedding: createEmbedding, createIndex: createIndex, getData: getData, logger: _this.loggerService }, embedding), { callbacks: callbacks }));
@@ -4208,6 +4704,7 @@ var StorageConnectionService = /** @class */ (function () {
4208
4704
  return [2 /*return*/];
4209
4705
  }
4210
4706
  this.getStorage.clear(key);
4707
+ this.sessionValidationService.removeStorageUsage(this.contextService.context.clientId, this.contextService.context.storageName);
4211
4708
  return [2 /*return*/];
4212
4709
  });
4213
4710
  }); };
@@ -5064,6 +5561,9 @@ var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0
5064
5561
  }); }),
5065
5562
  ])];
5066
5563
  case 4:
5564
+ _a.sent();
5565
+ return [4 /*yield*/, HistoryAdapter.dispose(clientId, null)];
5566
+ case 5:
5067
5567
  _a.sent();
5068
5568
  return [2 /*return*/];
5069
5569
  }
@@ -5926,14 +6426,14 @@ var StorageUtils = /** @class */ (function () {
5926
6426
  return __generator(this, function (_a) {
5927
6427
  switch (_a.label) {
5928
6428
  case 0:
5929
- swarm.loggerService.log("StorageStatic take", {
6429
+ swarm.loggerService.log("StorageUtils take", {
5930
6430
  search: payload.search,
5931
6431
  total: payload.total,
5932
6432
  clientId: payload.clientId,
5933
6433
  storageName: payload.storageName,
5934
6434
  score: payload.score,
5935
6435
  });
5936
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6436
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
5937
6437
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
5938
6438
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (take)"));
5939
6439
  }
@@ -5955,12 +6455,12 @@ var StorageUtils = /** @class */ (function () {
5955
6455
  return __generator(this, function (_a) {
5956
6456
  switch (_a.label) {
5957
6457
  case 0:
5958
- swarm.loggerService.log("StorageStatic upsert", {
6458
+ swarm.loggerService.log("StorageUtils upsert", {
5959
6459
  item: payload.item,
5960
6460
  clientId: payload.clientId,
5961
6461
  storageName: payload.storageName,
5962
6462
  });
5963
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6463
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
5964
6464
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
5965
6465
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (upsert)"));
5966
6466
  }
@@ -5981,12 +6481,12 @@ var StorageUtils = /** @class */ (function () {
5981
6481
  return __generator(this, function (_a) {
5982
6482
  switch (_a.label) {
5983
6483
  case 0:
5984
- swarm.loggerService.log("StorageStatic remove", {
6484
+ swarm.loggerService.log("StorageUtils remove", {
5985
6485
  itemId: payload.itemId,
5986
6486
  clientId: payload.clientId,
5987
6487
  storageName: payload.storageName,
5988
6488
  });
5989
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6489
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
5990
6490
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
5991
6491
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (remove)"));
5992
6492
  }
@@ -6008,12 +6508,12 @@ var StorageUtils = /** @class */ (function () {
6008
6508
  return __generator(this, function (_a) {
6009
6509
  switch (_a.label) {
6010
6510
  case 0:
6011
- swarm.loggerService.log("StorageStatic get", {
6511
+ swarm.loggerService.log("StorageUtils get", {
6012
6512
  itemId: payload.itemId,
6013
6513
  clientId: payload.clientId,
6014
6514
  storageName: payload.storageName,
6015
6515
  });
6016
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6516
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
6017
6517
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
6018
6518
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (get)"));
6019
6519
  }
@@ -6035,11 +6535,11 @@ var StorageUtils = /** @class */ (function () {
6035
6535
  return __generator(this, function (_a) {
6036
6536
  switch (_a.label) {
6037
6537
  case 0:
6038
- swarm.loggerService.log("StorageStatic list", {
6538
+ swarm.loggerService.log("StorageUtils list", {
6039
6539
  clientId: payload.clientId,
6040
6540
  storageName: payload.storageName,
6041
6541
  });
6042
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6542
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
6043
6543
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
6044
6544
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (list)"));
6045
6545
  }
@@ -6059,11 +6559,11 @@ var StorageUtils = /** @class */ (function () {
6059
6559
  return __generator(this, function (_a) {
6060
6560
  switch (_a.label) {
6061
6561
  case 0:
6062
- swarm.loggerService.log("StorageStatic clear", {
6562
+ swarm.loggerService.log("StorageUtils clear", {
6063
6563
  clientId: payload.clientId,
6064
6564
  storageName: payload.storageName,
6065
6565
  });
6066
- swarm.storageValidationService.validate(payload.storageName, "StorageStatic");
6566
+ swarm.storageValidationService.validate(payload.storageName, "StorageUtils");
6067
6567
  if (!swarm.agentValidationService.hasStorage(payload.agentName, payload.storageName)) {
6068
6568
  throw new Error("agent-swarm StorageUtils ".concat(payload.storageName, " not registered in ").concat(payload.agentName, " (clear)"));
6069
6569
  }
@@ -6078,7 +6578,10 @@ var StorageUtils = /** @class */ (function () {
6078
6578
  var Storage = new StorageUtils();
6079
6579
 
6080
6580
  exports.ContextService = ContextService;
6581
+ exports.History = History;
6081
6582
  exports.Storage = Storage;
6583
+ exports._HistoryAdapter = HistoryAdapter;
6584
+ exports._HistoryInstance = HistoryInstance;
6082
6585
  exports.addAgent = addAgent;
6083
6586
  exports.addCompletion = addCompletion;
6084
6587
  exports.addEmbedding = addEmbedding;