agent-swarm-kit 1.0.32 → 1.0.33

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
@@ -456,11 +456,16 @@ var ClientAgent = /** @class */ (function () {
456
456
  if ((validation = _a.sent())) {
457
457
  throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
458
458
  }
459
+ this.params.onOutput &&
460
+ this.params.onOutput(this.params.clientId, this.params.agentName, result_1);
459
461
  return [4 /*yield*/, this._outputSubject.next(result_1)];
460
462
  case 4:
461
463
  _a.sent();
462
464
  return [2 /*return*/];
463
- case 5: return [4 /*yield*/, this._outputSubject.next(result)];
465
+ case 5:
466
+ this.params.onOutput &&
467
+ this.params.onOutput(this.params.clientId, this.params.agentName, result);
468
+ return [4 /*yield*/, this._outputSubject.next(result)];
464
469
  case 6:
465
470
  _a.sent();
466
471
  return [2 /*return*/];
@@ -479,6 +484,8 @@ var ClientAgent = /** @class */ (function () {
479
484
  switch (_a.label) {
480
485
  case 0:
481
486
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
487
+ this.params.onResurrect &&
488
+ this.params.onResurrect(this.params.clientId, this.params.agentName, mode, reason);
482
489
  return [4 /*yield*/, this.params.history.push({
483
490
  role: "resque",
484
491
  mode: "tool",
@@ -540,7 +547,7 @@ var ClientAgent = /** @class */ (function () {
540
547
  * @returns {Promise<IModelMessage>}
541
548
  */
542
549
  this.getCompletion = function (mode) { return __awaiter(_this, void 0, void 0, function () {
543
- var messages;
550
+ var messages, args, output;
544
551
  var _a;
545
552
  return __generator(this, function (_b) {
546
553
  switch (_b.label) {
@@ -549,16 +556,21 @@ var ClientAgent = /** @class */ (function () {
549
556
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
550
557
  case 1:
551
558
  messages = _b.sent();
552
- return [4 /*yield*/, this.params.completion.getCompletion({
553
- clientId: this.params.clientId,
554
- agentName: this.params.agentName,
555
- messages: messages,
556
- mode: mode,
557
- tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
558
- return lodashEs.omit(t, "toolName", "call", "validate");
559
- }),
560
- })];
561
- case 2: return [2 /*return*/, _b.sent()];
559
+ args = {
560
+ clientId: this.params.clientId,
561
+ agentName: this.params.agentName,
562
+ messages: messages,
563
+ mode: mode,
564
+ tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
565
+ return lodashEs.omit(t, "toolName", "call", "validate", "onCall", "onValidate");
566
+ }),
567
+ };
568
+ return [4 /*yield*/, this.params.completion.getCompletion(args)];
569
+ case 2:
570
+ output = _b.sent();
571
+ this.params.completion.onComplete &&
572
+ this.params.completion.onComplete(args, output);
573
+ return [2 /*return*/, output];
562
574
  }
563
575
  });
564
576
  }); };
@@ -572,6 +584,8 @@ var ClientAgent = /** @class */ (function () {
572
584
  switch (_a.label) {
573
585
  case 0:
574
586
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitUserMessage"), { message: message });
587
+ this.params.onUserMessage &&
588
+ this.params.onUserMessage(this.params.clientId, this.params.agentName, message);
575
589
  return [4 /*yield*/, this.params.history.push({
576
590
  role: "user",
577
591
  agentName: this.params.agentName,
@@ -593,6 +607,8 @@ var ClientAgent = /** @class */ (function () {
593
607
  switch (_a.label) {
594
608
  case 0:
595
609
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitFlush"));
610
+ this.params.onFlush &&
611
+ this.params.onFlush(this.params.clientId, this.params.agentName);
596
612
  return [4 /*yield*/, this.params.history.push({
597
613
  role: "flush",
598
614
  agentName: this.params.agentName,
@@ -615,6 +631,8 @@ var ClientAgent = /** @class */ (function () {
615
631
  switch (_a.label) {
616
632
  case 0:
617
633
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitSystemMessage"), { message: message });
634
+ this.params.onSystemMessage &&
635
+ this.params.onSystemMessage(this.params.clientId, this.params.agentName, message);
618
636
  return [4 /*yield*/, this.params.history.push({
619
637
  role: "system",
620
638
  agentName: this.params.agentName,
@@ -637,6 +655,8 @@ var ClientAgent = /** @class */ (function () {
637
655
  switch (_a.label) {
638
656
  case 0:
639
657
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content });
658
+ this.params.onToolOutput &&
659
+ this.params.onToolOutput(this.params.clientId, this.params.agentName, content);
640
660
  return [4 /*yield*/, this.params.history.push({
641
661
  role: "tool",
642
662
  agentName: this.params.agentName,
@@ -665,6 +685,8 @@ var ClientAgent = /** @class */ (function () {
665
685
  switch (_e.label) {
666
686
  case 0:
667
687
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
688
+ this.params.onExecute &&
689
+ this.params.onExecute(this.params.clientId, this.params.agentName, incoming, mode);
668
690
  return [4 /*yield*/, this.params.history.push({
669
691
  role: "user",
670
692
  mode: mode,
@@ -697,7 +719,9 @@ var ClientAgent = /** @class */ (function () {
697
719
  case 3:
698
720
  _f.sent();
699
721
  return [2 /*return*/, { value: void 0 }];
700
- case 4: return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
722
+ case 4:
723
+ targetFn.onValidate && targetFn.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
724
+ return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
701
725
  case 5:
702
726
  if (!_f.sent()) return [3 /*break*/, 8];
703
727
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
@@ -710,6 +734,8 @@ var ClientAgent = /** @class */ (function () {
710
734
  _f.sent();
711
735
  return [2 /*return*/, { value: void 0 }];
712
736
  case 8:
737
+ targetFn.onCall &&
738
+ targetFn.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
713
739
  /**
714
740
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
715
741
  */
@@ -816,7 +842,7 @@ var AgentConnectionService = /** @class */ (function () {
816
842
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
817
843
  return "".concat(clientId, "-").concat(agentName);
818
844
  }, function (clientId, agentName) {
819
- var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
845
+ var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, onExecute = _a.onExecute, onOutput = _a.onOutput, onResurrect = _a.onResurrect, onSystemMessage = _a.onSystemMessage, onToolOutput = _a.onToolOutput, onUserMessage = _a.onUserMessage, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
820
846
  var completion = _this.completionSchemaService.get(completionName);
821
847
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
822
848
  return new ClientAgent({
@@ -829,6 +855,12 @@ var AgentConnectionService = /** @class */ (function () {
829
855
  system: system,
830
856
  tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get),
831
857
  completion: completion,
858
+ onExecute: onExecute,
859
+ onOutput: onOutput,
860
+ onResurrect: onResurrect,
861
+ onSystemMessage: onSystemMessage,
862
+ onToolOutput: onToolOutput,
863
+ onUserMessage: onUserMessage,
832
864
  });
833
865
  });
834
866
  /**
@@ -1459,7 +1491,7 @@ var SwarmConnectionService = /** @class */ (function () {
1459
1491
  return "".concat(clientId, "-").concat(swarmName);
1460
1492
  }, function (clientId, swarmName) {
1461
1493
  var e_1, _a;
1462
- var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent;
1494
+ var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, onAgentChanged = _b.onAgentChanged;
1463
1495
  var agentMap = {};
1464
1496
  try {
1465
1497
  for (var agentList_1 = __values(agentList), agentList_1_1 = agentList_1.next(); !agentList_1_1.done; agentList_1_1 = agentList_1.next()) {
@@ -1484,7 +1516,9 @@ var SwarmConnectionService = /** @class */ (function () {
1484
1516
  return __awaiter(this, void 0, void 0, function () {
1485
1517
  return __generator(this, function (_a) {
1486
1518
  switch (_a.label) {
1487
- case 0: return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1519
+ case 0:
1520
+ onAgentChanged && onAgentChanged(clientId, agentName, swarmName);
1521
+ return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1488
1522
  case 1:
1489
1523
  _a.sent();
1490
1524
  return [2 /*return*/];
@@ -1679,6 +1713,8 @@ var ClientSession = /** @class */ (function () {
1679
1713
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " emit"), {
1680
1714
  message: message,
1681
1715
  });
1716
+ this.params.onEmit &&
1717
+ this.params.onEmit(this.params.clientId, this.params.swarmName, message);
1682
1718
  return [4 /*yield*/, this._emitSubject.next(message)];
1683
1719
  case 1:
1684
1720
  _a.sent();
@@ -1701,6 +1737,8 @@ var ClientSession = /** @class */ (function () {
1701
1737
  message: message,
1702
1738
  mode: mode,
1703
1739
  });
1740
+ this.params.onExecute &&
1741
+ this.params.onExecute(this.params.clientId, this.params.swarmName, message, mode);
1704
1742
  return [4 /*yield*/, this.params.swarm.getAgent()];
1705
1743
  case 1:
1706
1744
  agent = _a.sent();
@@ -1801,6 +1839,8 @@ var ClientSession = /** @class */ (function () {
1801
1839
  */
1802
1840
  this.connect = function (connector) {
1803
1841
  _this.params.logger.debug("ClientSession clientId=".concat(_this.params.clientId, " connect"));
1842
+ _this.params.onConnect &&
1843
+ _this.params.onConnect(_this.params.clientId, _this.params.swarmName);
1804
1844
  _this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
1805
1845
  var _a;
1806
1846
  var _b;
@@ -1859,6 +1899,7 @@ var SessionConnectionService = /** @class */ (function () {
1859
1899
  this.loggerService = inject(TYPES.loggerService);
1860
1900
  this.contextService = inject(TYPES.contextService);
1861
1901
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
1902
+ this.swarmSchemaService = inject(TYPES.swarmSchemaService);
1862
1903
  /**
1863
1904
  * Retrieves a memoized session based on clientId and swarmName.
1864
1905
  * @param {string} clientId - The client ID.
@@ -1869,10 +1910,15 @@ var SessionConnectionService = /** @class */ (function () {
1869
1910
  var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
1870
1911
  return "".concat(clientId, "-").concat(swarmName);
1871
1912
  }, function (clientId, swarmName) {
1913
+ var _a = _this.swarmSchemaService.get(swarmName), onConnect = _a.onConnect, onEmit = _a.onEmit, onExecute = _a.onExecute;
1872
1914
  return new ClientSession({
1873
1915
  clientId: clientId,
1874
1916
  logger: _this.loggerService,
1875
1917
  swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName),
1918
+ swarmName: swarmName,
1919
+ onConnect: onConnect,
1920
+ onEmit: onEmit,
1921
+ onExecute: onExecute,
1876
1922
  });
1877
1923
  });
1878
1924
  /**
package/build/index.mjs CHANGED
@@ -454,11 +454,16 @@ var ClientAgent = /** @class */ (function () {
454
454
  if ((validation = _a.sent())) {
455
455
  throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
456
456
  }
457
+ this.params.onOutput &&
458
+ this.params.onOutput(this.params.clientId, this.params.agentName, result_1);
457
459
  return [4 /*yield*/, this._outputSubject.next(result_1)];
458
460
  case 4:
459
461
  _a.sent();
460
462
  return [2 /*return*/];
461
- case 5: return [4 /*yield*/, this._outputSubject.next(result)];
463
+ case 5:
464
+ this.params.onOutput &&
465
+ this.params.onOutput(this.params.clientId, this.params.agentName, result);
466
+ return [4 /*yield*/, this._outputSubject.next(result)];
462
467
  case 6:
463
468
  _a.sent();
464
469
  return [2 /*return*/];
@@ -477,6 +482,8 @@ var ClientAgent = /** @class */ (function () {
477
482
  switch (_a.label) {
478
483
  case 0:
479
484
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
485
+ this.params.onResurrect &&
486
+ this.params.onResurrect(this.params.clientId, this.params.agentName, mode, reason);
480
487
  return [4 /*yield*/, this.params.history.push({
481
488
  role: "resque",
482
489
  mode: "tool",
@@ -538,7 +545,7 @@ var ClientAgent = /** @class */ (function () {
538
545
  * @returns {Promise<IModelMessage>}
539
546
  */
540
547
  this.getCompletion = function (mode) { return __awaiter(_this, void 0, void 0, function () {
541
- var messages;
548
+ var messages, args, output;
542
549
  var _a;
543
550
  return __generator(this, function (_b) {
544
551
  switch (_b.label) {
@@ -547,16 +554,21 @@ var ClientAgent = /** @class */ (function () {
547
554
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
548
555
  case 1:
549
556
  messages = _b.sent();
550
- return [4 /*yield*/, this.params.completion.getCompletion({
551
- clientId: this.params.clientId,
552
- agentName: this.params.agentName,
553
- messages: messages,
554
- mode: mode,
555
- tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
556
- return omit(t, "toolName", "call", "validate");
557
- }),
558
- })];
559
- case 2: return [2 /*return*/, _b.sent()];
557
+ args = {
558
+ clientId: this.params.clientId,
559
+ agentName: this.params.agentName,
560
+ messages: messages,
561
+ mode: mode,
562
+ tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) {
563
+ return omit(t, "toolName", "call", "validate", "onCall", "onValidate");
564
+ }),
565
+ };
566
+ return [4 /*yield*/, this.params.completion.getCompletion(args)];
567
+ case 2:
568
+ output = _b.sent();
569
+ this.params.completion.onComplete &&
570
+ this.params.completion.onComplete(args, output);
571
+ return [2 /*return*/, output];
560
572
  }
561
573
  });
562
574
  }); };
@@ -570,6 +582,8 @@ var ClientAgent = /** @class */ (function () {
570
582
  switch (_a.label) {
571
583
  case 0:
572
584
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitUserMessage"), { message: message });
585
+ this.params.onUserMessage &&
586
+ this.params.onUserMessage(this.params.clientId, this.params.agentName, message);
573
587
  return [4 /*yield*/, this.params.history.push({
574
588
  role: "user",
575
589
  agentName: this.params.agentName,
@@ -591,6 +605,8 @@ var ClientAgent = /** @class */ (function () {
591
605
  switch (_a.label) {
592
606
  case 0:
593
607
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitFlush"));
608
+ this.params.onFlush &&
609
+ this.params.onFlush(this.params.clientId, this.params.agentName);
594
610
  return [4 /*yield*/, this.params.history.push({
595
611
  role: "flush",
596
612
  agentName: this.params.agentName,
@@ -613,6 +629,8 @@ var ClientAgent = /** @class */ (function () {
613
629
  switch (_a.label) {
614
630
  case 0:
615
631
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitSystemMessage"), { message: message });
632
+ this.params.onSystemMessage &&
633
+ this.params.onSystemMessage(this.params.clientId, this.params.agentName, message);
616
634
  return [4 /*yield*/, this.params.history.push({
617
635
  role: "system",
618
636
  agentName: this.params.agentName,
@@ -635,6 +653,8 @@ var ClientAgent = /** @class */ (function () {
635
653
  switch (_a.label) {
636
654
  case 0:
637
655
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput"), { content: content });
656
+ this.params.onToolOutput &&
657
+ this.params.onToolOutput(this.params.clientId, this.params.agentName, content);
638
658
  return [4 /*yield*/, this.params.history.push({
639
659
  role: "tool",
640
660
  agentName: this.params.agentName,
@@ -663,6 +683,8 @@ var ClientAgent = /** @class */ (function () {
663
683
  switch (_e.label) {
664
684
  case 0:
665
685
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming, mode: mode });
686
+ this.params.onExecute &&
687
+ this.params.onExecute(this.params.clientId, this.params.agentName, incoming, mode);
666
688
  return [4 /*yield*/, this.params.history.push({
667
689
  role: "user",
668
690
  mode: mode,
@@ -695,7 +717,9 @@ var ClientAgent = /** @class */ (function () {
695
717
  case 3:
696
718
  _f.sent();
697
719
  return [2 /*return*/, { value: void 0 }];
698
- case 4: return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
720
+ case 4:
721
+ targetFn.onValidate && targetFn.onValidate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
722
+ return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
699
723
  case 5:
700
724
  if (!_f.sent()) return [3 /*break*/, 8];
701
725
  this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool validation not passed"));
@@ -708,6 +732,8 @@ var ClientAgent = /** @class */ (function () {
708
732
  _f.sent();
709
733
  return [2 /*return*/, { value: void 0 }];
710
734
  case 8:
735
+ targetFn.onCall &&
736
+ targetFn.onCall(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
711
737
  /**
712
738
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
713
739
  */
@@ -814,7 +840,7 @@ var AgentConnectionService = /** @class */ (function () {
814
840
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
815
841
  return "".concat(clientId, "-").concat(agentName);
816
842
  }, function (clientId, agentName) {
817
- var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
843
+ var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, system = _a.system, tools = _a.tools, onExecute = _a.onExecute, onOutput = _a.onOutput, onResurrect = _a.onResurrect, onSystemMessage = _a.onSystemMessage, onToolOutput = _a.onToolOutput, onUserMessage = _a.onUserMessage, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
818
844
  var completion = _this.completionSchemaService.get(completionName);
819
845
  _this.sessionValidationService.addAgentUsage(clientId, agentName);
820
846
  return new ClientAgent({
@@ -827,6 +853,12 @@ var AgentConnectionService = /** @class */ (function () {
827
853
  system: system,
828
854
  tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get),
829
855
  completion: completion,
856
+ onExecute: onExecute,
857
+ onOutput: onOutput,
858
+ onResurrect: onResurrect,
859
+ onSystemMessage: onSystemMessage,
860
+ onToolOutput: onToolOutput,
861
+ onUserMessage: onUserMessage,
830
862
  });
831
863
  });
832
864
  /**
@@ -1457,7 +1489,7 @@ var SwarmConnectionService = /** @class */ (function () {
1457
1489
  return "".concat(clientId, "-").concat(swarmName);
1458
1490
  }, function (clientId, swarmName) {
1459
1491
  var e_1, _a;
1460
- var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent;
1492
+ var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent, onAgentChanged = _b.onAgentChanged;
1461
1493
  var agentMap = {};
1462
1494
  try {
1463
1495
  for (var agentList_1 = __values(agentList), agentList_1_1 = agentList_1.next(); !agentList_1_1.done; agentList_1_1 = agentList_1.next()) {
@@ -1482,7 +1514,9 @@ var SwarmConnectionService = /** @class */ (function () {
1482
1514
  return __awaiter(this, void 0, void 0, function () {
1483
1515
  return __generator(this, function (_a) {
1484
1516
  switch (_a.label) {
1485
- case 0: return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1517
+ case 0:
1518
+ onAgentChanged && onAgentChanged(clientId, agentName, swarmName);
1519
+ return [4 /*yield*/, GLOBAL_CONFIG.CC_SWARM_AGENT_CHANGED(clientId, agentName, swarmName)];
1486
1520
  case 1:
1487
1521
  _a.sent();
1488
1522
  return [2 /*return*/];
@@ -1677,6 +1711,8 @@ var ClientSession = /** @class */ (function () {
1677
1711
  this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " emit"), {
1678
1712
  message: message,
1679
1713
  });
1714
+ this.params.onEmit &&
1715
+ this.params.onEmit(this.params.clientId, this.params.swarmName, message);
1680
1716
  return [4 /*yield*/, this._emitSubject.next(message)];
1681
1717
  case 1:
1682
1718
  _a.sent();
@@ -1699,6 +1735,8 @@ var ClientSession = /** @class */ (function () {
1699
1735
  message: message,
1700
1736
  mode: mode,
1701
1737
  });
1738
+ this.params.onExecute &&
1739
+ this.params.onExecute(this.params.clientId, this.params.swarmName, message, mode);
1702
1740
  return [4 /*yield*/, this.params.swarm.getAgent()];
1703
1741
  case 1:
1704
1742
  agent = _a.sent();
@@ -1799,6 +1837,8 @@ var ClientSession = /** @class */ (function () {
1799
1837
  */
1800
1838
  this.connect = function (connector) {
1801
1839
  _this.params.logger.debug("ClientSession clientId=".concat(_this.params.clientId, " connect"));
1840
+ _this.params.onConnect &&
1841
+ _this.params.onConnect(_this.params.clientId, _this.params.swarmName);
1802
1842
  _this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
1803
1843
  var _a;
1804
1844
  var _b;
@@ -1857,6 +1897,7 @@ var SessionConnectionService = /** @class */ (function () {
1857
1897
  this.loggerService = inject(TYPES.loggerService);
1858
1898
  this.contextService = inject(TYPES.contextService);
1859
1899
  this.swarmConnectionService = inject(TYPES.swarmConnectionService);
1900
+ this.swarmSchemaService = inject(TYPES.swarmSchemaService);
1860
1901
  /**
1861
1902
  * Retrieves a memoized session based on clientId and swarmName.
1862
1903
  * @param {string} clientId - The client ID.
@@ -1867,10 +1908,15 @@ var SessionConnectionService = /** @class */ (function () {
1867
1908
  var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
1868
1909
  return "".concat(clientId, "-").concat(swarmName);
1869
1910
  }, function (clientId, swarmName) {
1911
+ var _a = _this.swarmSchemaService.get(swarmName), onConnect = _a.onConnect, onEmit = _a.onEmit, onExecute = _a.onExecute;
1870
1912
  return new ClientSession({
1871
1913
  clientId: clientId,
1872
1914
  logger: _this.loggerService,
1873
1915
  swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName),
1916
+ swarmName: swarmName,
1917
+ onConnect: onConnect,
1918
+ onEmit: onEmit,
1919
+ onExecute: onExecute,
1874
1920
  });
1875
1921
  });
1876
1922
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
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
@@ -1,44 +1,40 @@
1
1
  import * as di_scoped from 'di-scoped';
2
- import { ExecutionMode as ExecutionMode$1 } from 'src/interfaces/Session.interface';
3
2
  import * as functools_kit from 'functools-kit';
4
3
  import { IPubsubArray, Subject } from 'functools-kit';
5
4
 
6
5
  /**
7
- * Interface representing a model message.
6
+ * Interface representing an incoming message.
8
7
  */
9
- interface IModelMessage {
8
+ interface IIncomingMessage {
10
9
  /**
11
- * The role of the message sender.
12
- * @type {'assistant' | 'system' | 'tool' | 'user' | 'resque' | 'flush'}
10
+ * The ID of the client sending the message.
13
11
  */
14
- role: "assistant" | "system" | "tool" | "user" | "resque" | "flush";
12
+ clientId: string;
13
+ /**
14
+ * The data contained in the message.
15
+ */
16
+ data: string;
15
17
  /**
16
18
  * The name of the agent sending the message.
17
- * @type {string}
18
19
  */
19
- agentName: string;
20
+ agentName: AgentName;
21
+ }
22
+ /**
23
+ * Interface representing an outgoing message.
24
+ */
25
+ interface IOutgoingMessage {
20
26
  /**
21
- * The content of the message.
22
- * @type {string}
27
+ * The ID of the client receiving the message.
23
28
  */
24
- content: string;
29
+ clientId: string;
25
30
  /**
26
- * The source of message: tool or user
27
- * @type {ExecutionMode}
31
+ * The data contained in the message.
28
32
  */
29
- mode: ExecutionMode$1;
33
+ data: string;
30
34
  /**
31
- * Optional tool calls associated with the message.
32
- * @type {Array<{ function: { name: string; arguments: { [key: string]: any; }; }; }>}
35
+ * The name of the agent sending the message.
33
36
  */
34
- tool_calls?: {
35
- function: {
36
- name: string;
37
- arguments: {
38
- [key: string]: any;
39
- };
40
- };
41
- }[];
37
+ agentName: AgentName;
42
38
  }
43
39
 
44
40
  /**
@@ -57,163 +53,29 @@ interface ILogger {
57
53
  debug(...args: any[]): void;
58
54
  }
59
55
 
60
- /**
61
- * Interface representing the history of model messages.
62
- */
63
- interface IHistory {
56
+ interface ISwarmSession {
64
57
  /**
65
- * Pushes a message to the history.
66
- * @param {IModelMessage} message - The message to push.
67
- * @returns {Promise<void>}
68
- */
69
- push(message: IModelMessage): Promise<void>;
70
- /**
71
- * Converts the history to an array of messages for a specific agent.
72
- * @param {string} prompt - The prompt to filter messages for the agent.
73
- * @returns {Promise<IModelMessage[]>}
74
- */
75
- toArrayForAgent(prompt: string, system?: string[]): Promise<IModelMessage[]>;
76
- /**
77
- * Converts the history to an array of raw messages.
78
- * @returns {Promise<IModelMessage[]>}
79
- */
80
- toArrayForRaw(): Promise<IModelMessage[]>;
81
- }
82
- /**
83
- * Interface representing the parameters required to create a history instance.
84
- */
85
- interface IHistoryParams extends IHistorySchema {
86
- /**
87
- * The name of the agent.
88
- * @type {AgentName}
89
- */
90
- agentName: AgentName;
91
- /**
92
- * The client ID.
93
- * @type {string}
94
- */
95
- clientId: string;
96
- /**
97
- * The logger instance.
98
- * @type {ILogger}
99
- */
100
- logger: ILogger;
101
- }
102
- /**
103
- * Interface representing the schema of the history.
104
- */
105
- interface IHistorySchema {
106
- /**
107
- * The array of model messages.
108
- * @type {IPubsubArray<IModelMessage>}
109
- */
110
- items: IPubsubArray<IModelMessage>;
111
- }
112
-
113
- /**
114
- * Represents a tool call with a function name and arguments.
115
- */
116
- interface IToolCall {
117
- function: {
118
- /**
119
- * The name of the function to be called.
120
- */
121
- name: string;
122
- /**
123
- * The arguments to be passed to the function.
124
- */
125
- arguments: {
126
- [key: string]: any;
127
- };
128
- };
129
- }
130
- /**
131
- * Represents a tool with a type and function details.
132
- */
133
- interface ITool {
134
- /**
135
- * The type of the tool.
136
- */
137
- type: string;
138
- function: {
139
- /**
140
- * The name of the function.
141
- */
142
- name: string;
143
- /**
144
- * The description of the function.
145
- */
146
- description: string;
147
- /**
148
- * The parameters required by the function.
149
- */
150
- parameters: {
151
- /**
152
- * The type of the parameters.
153
- */
154
- type: string;
155
- /**
156
- * The list of required parameters.
157
- */
158
- required: string[];
159
- /**
160
- * The properties of the parameters.
161
- */
162
- properties: {
163
- [key: string]: {
164
- /**
165
- * The type of the property.
166
- */
167
- type: string;
168
- /**
169
- * The description of the property.
170
- */
171
- description: string;
172
- /**
173
- * The possible values for the property.
174
- */
175
- enum?: string[];
176
- };
177
- };
178
- };
179
- };
180
- }
181
-
182
- /**
183
- * Interface representing an incoming message.
184
- */
185
- interface IIncomingMessage {
186
- /**
187
- * The ID of the client sending the message.
188
- */
189
- clientId: string;
190
- /**
191
- * The data contained in the message.
192
- */
193
- data: string;
194
- /**
195
- * The name of the agent sending the message.
196
- */
197
- agentName: AgentName;
198
- }
199
- /**
200
- * Interface representing an outgoing message.
201
- */
202
- interface IOutgoingMessage {
203
- /**
204
- * The ID of the client receiving the message.
58
+ * Callback triggered when a client connects.
59
+ * @param clientId - The ID of the client.
60
+ * @param swarmName - The name of the swarm.
205
61
  */
206
- clientId: string;
62
+ onConnect?: (clientId: string, swarmName: SwarmName) => void;
207
63
  /**
208
- * The data contained in the message.
64
+ * Callback triggered when a command is executed.
65
+ * @param clientId - The ID of the client.
66
+ * @param swarmName - The name of the swarm.
67
+ * @param content - The content to execute.
68
+ * @param mode - The source of execution: tool or user.
209
69
  */
210
- data: string;
70
+ onExecute?: (clientId: string, swarmName: SwarmName, content: string, mode: ExecutionMode) => void;
211
71
  /**
212
- * The name of the agent sending the message.
72
+ * Callback triggered when a message is emitted.
73
+ * @param clientId - The ID of the client.
74
+ * @param swarmName - The name of the swarm.
75
+ * @param message - The message to emit.
213
76
  */
214
- agentName: AgentName;
77
+ onEmit?: (clientId: string, swarmName: SwarmName, message: string) => void;
215
78
  }
216
-
217
79
  /**
218
80
  * Parameters for initializing a swarm.
219
81
  * @interface
@@ -221,6 +83,7 @@ interface IOutgoingMessage {
221
83
  */
222
84
  interface ISwarmParams extends Omit<ISwarmSchema, keyof {
223
85
  agentList: never;
86
+ onAgentChanged: never;
224
87
  }> {
225
88
  /** Client identifier */
226
89
  clientId: string;
@@ -229,19 +92,21 @@ interface ISwarmParams extends Omit<ISwarmSchema, keyof {
229
92
  /** Map of agent names to agent instances */
230
93
  agentMap: Record<AgentName, IAgent>;
231
94
  /** Emit the callback on agent change */
232
- onAgentChanged(clientId: string, agentName: AgentName, swarmName: SwarmName): Promise<void>;
95
+ onAgentChanged: (clientId: string, agentName: AgentName, swarmName: SwarmName) => Promise<void>;
233
96
  }
234
97
  /**
235
98
  * Schema for defining a swarm.
236
99
  * @interface
237
100
  */
238
- interface ISwarmSchema {
101
+ interface ISwarmSchema extends ISwarmSession {
239
102
  /** Default agent name */
240
103
  defaultAgent: AgentName;
241
104
  /** Name of the swarm */
242
105
  swarmName: string;
243
106
  /** List of agent names */
244
107
  agentList: string[];
108
+ /** Emit the callback on agent change */
109
+ onAgentChanged?: (clientId: string, agentName: AgentName, swarmName: SwarmName) => void;
245
110
  }
246
111
  /**
247
112
  * Interface for a swarm.
@@ -284,10 +149,11 @@ type SwarmName = string;
284
149
  * Parameters required to create a session.
285
150
  * @interface
286
151
  */
287
- interface ISessionParams extends ISessionSchema {
152
+ interface ISessionParams extends ISessionSchema, ISwarmSession {
288
153
  clientId: string;
289
154
  logger: ILogger;
290
155
  swarm: ISwarm;
156
+ swarmName: SwarmName;
291
157
  }
292
158
  /**
293
159
  * Schema for session data.
@@ -374,6 +240,166 @@ type SessionMode = "session" | "makeConnection" | "complete";
374
240
  */
375
241
  type ExecutionMode = "tool" | "user";
376
242
 
243
+ /**
244
+ * Interface representing a model message.
245
+ */
246
+ interface IModelMessage {
247
+ /**
248
+ * The role of the message sender.
249
+ * @type {'assistant' | 'system' | 'tool' | 'user' | 'resque' | 'flush'}
250
+ */
251
+ role: "assistant" | "system" | "tool" | "user" | "resque" | "flush";
252
+ /**
253
+ * The name of the agent sending the message.
254
+ * @type {string}
255
+ */
256
+ agentName: string;
257
+ /**
258
+ * The content of the message.
259
+ * @type {string}
260
+ */
261
+ content: string;
262
+ /**
263
+ * The source of message: tool or user
264
+ * @type {ExecutionMode}
265
+ */
266
+ mode: ExecutionMode;
267
+ /**
268
+ * Optional tool calls associated with the message.
269
+ * @type {Array<{ function: { name: string; arguments: { [key: string]: any; }; }; }>}
270
+ */
271
+ tool_calls?: {
272
+ function: {
273
+ name: string;
274
+ arguments: {
275
+ [key: string]: any;
276
+ };
277
+ };
278
+ }[];
279
+ }
280
+
281
+ /**
282
+ * Interface representing the history of model messages.
283
+ */
284
+ interface IHistory {
285
+ /**
286
+ * Pushes a message to the history.
287
+ * @param {IModelMessage} message - The message to push.
288
+ * @returns {Promise<void>}
289
+ */
290
+ push(message: IModelMessage): Promise<void>;
291
+ /**
292
+ * Converts the history to an array of messages for a specific agent.
293
+ * @param {string} prompt - The prompt to filter messages for the agent.
294
+ * @returns {Promise<IModelMessage[]>}
295
+ */
296
+ toArrayForAgent(prompt: string, system?: string[]): Promise<IModelMessage[]>;
297
+ /**
298
+ * Converts the history to an array of raw messages.
299
+ * @returns {Promise<IModelMessage[]>}
300
+ */
301
+ toArrayForRaw(): Promise<IModelMessage[]>;
302
+ }
303
+ /**
304
+ * Interface representing the parameters required to create a history instance.
305
+ */
306
+ interface IHistoryParams extends IHistorySchema {
307
+ /**
308
+ * The name of the agent.
309
+ * @type {AgentName}
310
+ */
311
+ agentName: AgentName;
312
+ /**
313
+ * The client ID.
314
+ * @type {string}
315
+ */
316
+ clientId: string;
317
+ /**
318
+ * The logger instance.
319
+ * @type {ILogger}
320
+ */
321
+ logger: ILogger;
322
+ }
323
+ /**
324
+ * Interface representing the schema of the history.
325
+ */
326
+ interface IHistorySchema {
327
+ /**
328
+ * The array of model messages.
329
+ * @type {IPubsubArray<IModelMessage>}
330
+ */
331
+ items: IPubsubArray<IModelMessage>;
332
+ }
333
+
334
+ /**
335
+ * Represents a tool call with a function name and arguments.
336
+ */
337
+ interface IToolCall {
338
+ function: {
339
+ /**
340
+ * The name of the function to be called.
341
+ */
342
+ name: string;
343
+ /**
344
+ * The arguments to be passed to the function.
345
+ */
346
+ arguments: {
347
+ [key: string]: any;
348
+ };
349
+ };
350
+ }
351
+ /**
352
+ * Represents a tool with a type and function details.
353
+ */
354
+ interface ITool {
355
+ /**
356
+ * The type of the tool.
357
+ */
358
+ type: string;
359
+ function: {
360
+ /**
361
+ * The name of the function.
362
+ */
363
+ name: string;
364
+ /**
365
+ * The description of the function.
366
+ */
367
+ description: string;
368
+ /**
369
+ * The parameters required by the function.
370
+ */
371
+ parameters: {
372
+ /**
373
+ * The type of the parameters.
374
+ */
375
+ type: string;
376
+ /**
377
+ * The list of required parameters.
378
+ */
379
+ required: string[];
380
+ /**
381
+ * The properties of the parameters.
382
+ */
383
+ properties: {
384
+ [key: string]: {
385
+ /**
386
+ * The type of the property.
387
+ */
388
+ type: string;
389
+ /**
390
+ * The description of the property.
391
+ */
392
+ description: string;
393
+ /**
394
+ * The possible values for the property.
395
+ */
396
+ enum?: string[];
397
+ };
398
+ };
399
+ };
400
+ };
401
+ }
402
+
377
403
  /**
378
404
  * Interface representing a completion.
379
405
  */
@@ -418,6 +444,12 @@ interface ICompletionSchema {
418
444
  * @returns A promise that resolves to a model message.
419
445
  */
420
446
  getCompletion(args: ICompletionArgs): Promise<IModelMessage>;
447
+ /**
448
+ * Callback fired after complete.
449
+ * @param args - Arguments passed to complete
450
+ * @param output - Output of the model
451
+ */
452
+ onComplete?: (args: ICompletionArgs, output: IModelMessage) => void;
421
453
  }
422
454
  /**
423
455
  * Type representing the name of a completion.
@@ -447,6 +479,22 @@ interface IAgentTool<T = Record<string, unknown>> extends ITool {
447
479
  * @returns A promise that resolves to a boolean indicating whether the parameters are valid, or a boolean.
448
480
  */
449
481
  validate(clientId: string, agentName: AgentName, params: T): Promise<boolean> | boolean;
482
+ /**
483
+ * Callback triggered when the tool is called.
484
+ * @param clientId - The ID of the client.
485
+ * @param agentName - The name of the agent.
486
+ * @param params - The parameters for the tool.
487
+ * @returns A promise that resolves when the tool call is complete.
488
+ */
489
+ onCall?: (clientId: string, agentName: AgentName, params: T) => Promise<void>;
490
+ /**
491
+ * Callback triggered when the tool parameters are validated.
492
+ * @param clientId - The ID of the client.
493
+ * @param agentName - The name of the agent.
494
+ * @param params - The parameters for the tool.
495
+ * @returns A promise that resolves to a boolean indicating whether the parameters are valid.
496
+ */
497
+ onValidate?: (clientId: string, agentName: AgentName, params: T) => Promise<boolean>;
450
498
  }
451
499
  /**
452
500
  * Interface representing the parameters for an agent.
@@ -493,6 +541,56 @@ interface IAgentSchema {
493
541
  * @returns A promise that resolves to a string or null.
494
542
  */
495
543
  validate?: (output: string) => Promise<string | null>;
544
+ /**
545
+ * Callback triggered when the agent executes.
546
+ * @param clientId - The ID of the client.
547
+ * @param agentName - The name of the agent.
548
+ * @param input - The input to execute.
549
+ * @param mode - The source of execution: tool or user.
550
+ */
551
+ onExecute?: (clientId: string, agentName: AgentName, input: string, mode: ExecutionMode) => void;
552
+ /**
553
+ * Callback triggered when there is tool output.
554
+ * @param clientId - The ID of the client.
555
+ * @param agentName - The name of the agent.
556
+ * @param content - The content of the tool output.
557
+ */
558
+ onToolOutput?: (clientId: string, agentName: AgentName, content: string) => void;
559
+ /**
560
+ * Callback triggered when there is a system message.
561
+ * @param clientId - The ID of the client.
562
+ * @param agentName - The name of the agent.
563
+ * @param message - The system message.
564
+ */
565
+ onSystemMessage?: (clientId: string, agentName: AgentName, message: string) => void;
566
+ /**
567
+ * Callback triggered when there is a user message.
568
+ * @param clientId - The ID of the client.
569
+ * @param agentName - The name of the agent.
570
+ * @param message - The user message.
571
+ */
572
+ onUserMessage?: (clientId: string, agentName: AgentName, message: string) => void;
573
+ /**
574
+ * Callback triggered when the agent history is flushed.
575
+ * @param clientId - The ID of the client.
576
+ * @param agentName - The name of the agent.
577
+ */
578
+ onFlush?: (clientId: string, agentName: AgentName) => void;
579
+ /**
580
+ * Callback triggered when there is output.
581
+ * @param clientId - The ID of the client.
582
+ * @param agentName - The name of the agent.
583
+ * @param output - The output string.
584
+ */
585
+ onOutput?: (clientId: string, agentName: AgentName, output: string) => void;
586
+ /**
587
+ * Callback triggered when the agent is resurrected.
588
+ * @param clientId - The ID of the client.
589
+ * @param agentName - The name of the agent.
590
+ * @param mode - The source of execution: tool or user.
591
+ * @param reason - The reason for the resurrection.
592
+ */
593
+ onResurrect?: (clientId: string, agentName: AgentName, mode: ExecutionMode, reason?: string) => void;
496
594
  }
497
595
  /**
498
596
  * Interface representing an agent.
@@ -501,7 +599,7 @@ interface IAgent {
501
599
  /**
502
600
  * Executes the agent with the given input.
503
601
  * @param input - The input to execute.
504
- * @param mode - The source of execution: tool or user
602
+ * @param mode - The source of execution: tool or user.
505
603
  * @returns A promise that resolves when the execution is complete.
506
604
  */
507
605
  execute: (input: string, mode: ExecutionMode) => Promise<void>;
@@ -529,7 +627,7 @@ interface IAgent {
529
627
  */
530
628
  commitUserMessage(message: string): Promise<void>;
531
629
  /**
532
- * Clear the history for agent
630
+ * Clears the history for the agent.
533
631
  * @returns A promise that resolves when the flush is committed.
534
632
  */
535
633
  commitFlush(): Promise<void>;
@@ -1022,6 +1120,7 @@ declare class SessionConnectionService implements ISession {
1022
1120
  private readonly loggerService;
1023
1121
  private readonly contextService;
1024
1122
  private readonly swarmConnectionService;
1123
+ private readonly swarmSchemaService;
1025
1124
  /**
1026
1125
  * Retrieves a memoized session based on clientId and swarmName.
1027
1126
  * @param {string} clientId - The client ID.