agent-swarm-kit 1.0.7 → 1.0.9

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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { scoped } from 'di-scoped';
2
- import { ToolRegistry, Subject, queued, not, trycatch, memoize, PubsubArrayAdapter, ttl, singleshot } from 'functools-kit';
2
+ import { ToolRegistry, PubsubArrayAdapter, Subject, queued, not, trycatch, memoize, ttl, singleshot } from 'functools-kit';
3
3
  import { createActivator } from 'di-kit';
4
4
  import { omit } from 'lodash-es';
5
5
  import xml2js from 'xml2js';
@@ -233,11 +233,13 @@ var CC_EMPTY_OUTPUT_PLACEHOLDERS = [
233
233
  ];
234
234
  var CC_KEEP_MESSAGES = 5;
235
235
  var CC_ANSWER_TIMEOUT_SECONDS = 120000;
236
+ var CC_GET_AGENT_HISTORY = function () { return new PubsubArrayAdapter(); };
236
237
  var GLOBAL_CONFIG = {
237
238
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
238
239
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
239
240
  CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
240
241
  CC_ANSWER_TIMEOUT_SECONDS: CC_ANSWER_TIMEOUT_SECONDS,
242
+ CC_GET_AGENT_HISTORY: CC_GET_AGENT_HISTORY,
241
243
  };
242
244
  var setConfig = function (config) {
243
245
  Object.assign(GLOBAL_CONFIG, config);
@@ -257,7 +259,7 @@ var ClientAgent = /** @class */ (function () {
257
259
  return __generator(this, function (_a) {
258
260
  switch (_a.label) {
259
261
  case 0:
260
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _emitOuput"));
262
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"));
261
263
  validation = null;
262
264
  return [4 /*yield*/, this.params.validate(result)];
263
265
  case 1:
@@ -268,7 +270,7 @@ var ClientAgent = /** @class */ (function () {
268
270
  return [4 /*yield*/, this.params.validate(result_1)];
269
271
  case 3:
270
272
  if (validation = _a.sent()) {
271
- throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " model ressurect failed: ").concat(validation));
273
+ throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
272
274
  }
273
275
  return [4 /*yield*/, this._outputSubject.next(result_1)];
274
276
  case 4:
@@ -286,7 +288,7 @@ var ClientAgent = /** @class */ (function () {
286
288
  return __generator(this, function (_a) {
287
289
  switch (_a.label) {
288
290
  case 0:
289
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel"));
291
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
290
292
  return [4 /*yield*/, this.params.history.push({
291
293
  role: "resque",
292
294
  agentName: this.params.agentName,
@@ -309,7 +311,7 @@ var ClientAgent = /** @class */ (function () {
309
311
  return [4 /*yield*/, this.params.validate(result)];
310
312
  case 4:
311
313
  if (!(validation = _a.sent())) return [3 /*break*/, 6];
312
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel validation error: ").concat(validation));
314
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
313
315
  content = getPlaceholder();
314
316
  return [4 /*yield*/, this.params.history.push({
315
317
  agentName: this.params.agentName,
@@ -330,7 +332,7 @@ var ClientAgent = /** @class */ (function () {
330
332
  return __generator(this, function (_a) {
331
333
  switch (_a.label) {
332
334
  case 0:
333
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " waitForOutput"));
335
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " waitForOutput"));
334
336
  return [4 /*yield*/, this._outputSubject.toPromise()];
335
337
  case 1: return [2 /*return*/, _a.sent()];
336
338
  }
@@ -342,7 +344,7 @@ var ClientAgent = /** @class */ (function () {
342
344
  return __generator(this, function (_b) {
343
345
  switch (_b.label) {
344
346
  case 0:
345
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " getCompletion"));
347
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
346
348
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt)];
347
349
  case 1:
348
350
  messages = _b.sent();
@@ -360,7 +362,7 @@ var ClientAgent = /** @class */ (function () {
360
362
  return __generator(this, function (_a) {
361
363
  switch (_a.label) {
362
364
  case 0:
363
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitSystemMessage"));
365
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitSystemMessage"));
364
366
  return [4 /*yield*/, this.params.history.push({
365
367
  role: "system",
366
368
  agentName: this.params.agentName,
@@ -376,7 +378,7 @@ var ClientAgent = /** @class */ (function () {
376
378
  return __generator(this, function (_a) {
377
379
  switch (_a.label) {
378
380
  case 0:
379
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitToolOutput content=").concat(content));
381
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput content=").concat(content));
380
382
  return [4 /*yield*/, this.params.history.push({
381
383
  role: "tool",
382
384
  agentName: this.params.agentName,
@@ -398,7 +400,7 @@ var ClientAgent = /** @class */ (function () {
398
400
  return __generator(this, function (_e) {
399
401
  switch (_e.label) {
400
402
  case 0:
401
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute begin"), { incoming: incoming });
403
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming });
402
404
  return [4 /*yield*/, this.params.history.push({
403
405
  role: "user",
404
406
  agentName: this.params.agentName,
@@ -410,7 +412,7 @@ var ClientAgent = /** @class */ (function () {
410
412
  case 2:
411
413
  message = _e.sent();
412
414
  if (!message.tool_calls) return [3 /*break*/, 11];
413
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " tool call begin"));
415
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
414
416
  _loop_1 = function (tool) {
415
417
  var targetFn, result_2, result_3;
416
418
  return __generator(this, function (_f) {
@@ -421,11 +423,11 @@ var ClientAgent = /** @class */ (function () {
421
423
  case 1:
422
424
  _f.sent();
423
425
  if (!!targetFn) return [3 /*break*/, 4];
424
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool function not found"));
426
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool function not found"));
425
427
  return [4 /*yield*/, this_1._resurrectModel("No target function for ".concat(tool.function.name))];
426
428
  case 2:
427
429
  result_2 = _f.sent();
428
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_2));
430
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
429
431
  return [4 /*yield*/, this_1._emitOuput(result_2)];
430
432
  case 3:
431
433
  _f.sent();
@@ -433,11 +435,11 @@ var ClientAgent = /** @class */ (function () {
433
435
  case 4: return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
434
436
  case 5:
435
437
  if (!_f.sent()) return [3 /*break*/, 8];
436
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool validation not passed"));
438
+ 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"));
437
439
  return [4 /*yield*/, this_1._resurrectModel("Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
438
440
  case 6:
439
441
  result_3 = _f.sent();
440
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_3));
442
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
441
443
  return [4 /*yield*/, this_1._emitOuput(result_3)];
442
444
  case 7:
443
445
  _f.sent();
@@ -447,14 +449,14 @@ var ClientAgent = /** @class */ (function () {
447
449
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
448
450
  */
449
451
  targetFn.call(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
450
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool call executing"));
452
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call executing"));
451
453
  return [4 /*yield*/, Promise.race([
452
454
  this_1._toolCommitSubject.toPromise(),
453
455
  this_1._outputSubject.toPromise(),
454
456
  ])];
455
457
  case 9:
456
458
  _f.sent();
457
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool call end"));
459
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " functionName=").concat(tool.function.name, " tool call end"));
458
460
  return [2 /*return*/];
459
461
  }
460
462
  });
@@ -491,7 +493,7 @@ var ClientAgent = /** @class */ (function () {
491
493
  case 10: return [2 /*return*/];
492
494
  case 11:
493
495
  if (!message.tool_calls) {
494
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute no tool calls detected"));
496
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
495
497
  }
496
498
  result = message.content;
497
499
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
@@ -501,7 +503,7 @@ var ClientAgent = /** @class */ (function () {
501
503
  return [4 /*yield*/, this.params.validate(result)];
502
504
  case 13:
503
505
  if (!(validation = _e.sent())) return [3 /*break*/, 16];
504
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute invalid tool call detected: ").concat(validation), { result: result });
506
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
505
507
  return [4 /*yield*/, this._resurrectModel("Invalid model output: ".concat(result))];
506
508
  case 14:
507
509
  result1 = _e.sent();
@@ -510,7 +512,7 @@ var ClientAgent = /** @class */ (function () {
510
512
  _e.sent();
511
513
  return [2 /*return*/];
512
514
  case 16:
513
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute end result=").concat(result));
515
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
514
516
  return [4 /*yield*/, this._emitOuput(result)];
515
517
  case 17:
516
518
  _e.sent();
@@ -518,6 +520,7 @@ var ClientAgent = /** @class */ (function () {
518
520
  }
519
521
  });
520
522
  }); });
523
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"));
521
524
  }
522
525
  return ClientAgent;
523
526
  }());
@@ -613,6 +616,7 @@ var AgentConnectionService = /** @class */ (function () {
613
616
  var _this = this;
614
617
  this.loggerService = inject(TYPES.loggerService);
615
618
  this.contextService = inject(TYPES.contextService);
619
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
616
620
  this.historyConnectionService = inject(TYPES.historyConnectionService);
617
621
  this.agentSchemaService = inject(TYPES.agentSchemaService);
618
622
  this.toolSchemaService = inject(TYPES.toolSchemaService);
@@ -623,6 +627,7 @@ var AgentConnectionService = /** @class */ (function () {
623
627
  }, function (clientId, agentName) {
624
628
  var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, tools = _a.tools, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
625
629
  var completion = _this.completionSchemaService.get(completionName);
630
+ _this.sessionValidationService.addAgentUsage(clientId, agentName);
626
631
  return new ClientAgent({
627
632
  clientId: clientId,
628
633
  agentName: agentName,
@@ -691,6 +696,7 @@ var AgentConnectionService = /** @class */ (function () {
691
696
  context: this.contextService.context,
692
697
  });
693
698
  this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
699
+ this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
694
700
  return [2 /*return*/];
695
701
  });
696
702
  }); };
@@ -840,6 +846,7 @@ var ClientHistory = /** @class */ (function () {
840
846
  }
841
847
  });
842
848
  }); };
849
+ this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"));
843
850
  }
844
851
  return ClientHistory;
845
852
  }());
@@ -849,18 +856,22 @@ var HistoryConnectionService = /** @class */ (function () {
849
856
  var _this = this;
850
857
  this.loggerService = inject(TYPES.loggerService);
851
858
  this.contextService = inject(TYPES.contextService);
859
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
852
860
  this.getItems = memoize(function (_a) {
853
861
  var _b = __read(_a, 1), clientId = _b[0];
854
862
  return clientId;
855
- }, function () { return new PubsubArrayAdapter(); });
863
+ }, function (clientId, agentName) {
864
+ return GLOBAL_CONFIG.CC_GET_AGENT_HISTORY(clientId, agentName);
865
+ });
856
866
  this.getHistory = memoize(function (_a) {
857
867
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
858
868
  return "".concat(clientId, "-").concat(agentName);
859
869
  }, function (clientId, agentName) {
870
+ _this.sessionValidationService.addHistoryUsage(clientId, agentName);
860
871
  return new ClientHistory({
861
872
  clientId: clientId,
862
873
  agentName: agentName,
863
- items: _this.getItems(clientId),
874
+ items: _this.getItems(clientId, agentName),
864
875
  logger: _this.loggerService,
865
876
  });
866
877
  });
@@ -907,11 +918,12 @@ var HistoryConnectionService = /** @class */ (function () {
907
918
  this.loggerService.log("historyConnectionService dispose", {
908
919
  context: this.contextService.context,
909
920
  });
910
- return [4 /*yield*/, this.getItems(this.contextService.context.clientId).clear()];
921
+ return [4 /*yield*/, this.getItems(this.contextService.context.clientId, this.contextService.context.agentName).clear()];
911
922
  case 1:
912
923
  _a.sent();
913
924
  this.getItems.clear(this.contextService.context.clientId);
914
925
  this.getHistory.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
926
+ this.sessionValidationService.removeHistoryUsage(this.contextService.context.clientId, this.contextService.context.agentName);
915
927
  return [2 /*return*/];
916
928
  }
917
929
  });
@@ -941,13 +953,13 @@ var ClientSwarm = /** @class */ (function () {
941
953
  function ClientSwarm(params) {
942
954
  var _this = this;
943
955
  this.params = params;
944
- this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
956
+ this.waitForOutput = queued(function () { return __awaiter(_this, void 0, void 0, function () {
945
957
  var START_TIME, _a, agentName, output, _b;
946
958
  var _this = this;
947
959
  return __generator(this, function (_c) {
948
960
  switch (_c.label) {
949
961
  case 0:
950
- this.params.logger.debug("BaseConnection waitForOutput");
962
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForOutput"));
951
963
  START_TIME = Date.now();
952
964
  _c.label = 1;
953
965
  case 1:
@@ -980,10 +992,10 @@ var ClientSwarm = /** @class */ (function () {
980
992
  case 4: return [2 /*return*/];
981
993
  }
982
994
  });
983
- }); };
995
+ }); });
984
996
  this.getAgentName = function () { return __awaiter(_this, void 0, void 0, function () {
985
997
  return __generator(this, function (_a) {
986
- this.params.logger.debug("ClientSwarm getAgentName");
998
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " getAgentName"));
987
999
  return [2 /*return*/, this._activeAgent];
988
1000
  });
989
1001
  }); };
@@ -992,7 +1004,7 @@ var ClientSwarm = /** @class */ (function () {
992
1004
  return __generator(this, function (_a) {
993
1005
  switch (_a.label) {
994
1006
  case 0:
995
- this.params.logger.debug("ClientSwarm getAgent");
1007
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " getAgent"));
996
1008
  return [4 /*yield*/, this.getAgentName()];
997
1009
  case 1:
998
1010
  agent = _a.sent();
@@ -1002,11 +1014,12 @@ var ClientSwarm = /** @class */ (function () {
1002
1014
  }); };
1003
1015
  this.setAgentName = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
1004
1016
  return __generator(this, function (_a) {
1005
- this.params.logger.debug("ClientSwarm setAgentName agentName=".concat(agentName));
1017
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " setAgentName agentName=").concat(agentName));
1006
1018
  this._activeAgent = agentName;
1007
1019
  return [2 /*return*/];
1008
1020
  });
1009
1021
  }); };
1022
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " CTOR"));
1010
1023
  this._activeAgent = params.defaultAgent;
1011
1024
  }
1012
1025
  return ClientSwarm;
@@ -1147,30 +1160,48 @@ var ClientSession = /** @class */ (function () {
1147
1160
  function ClientSession(params) {
1148
1161
  var _this = this;
1149
1162
  this.params = params;
1150
- this.execute = function (message) { return __awaiter(_this, void 0, void 0, function () {
1151
- var agent, output;
1152
- return __generator(this, function (_a) {
1153
- switch (_a.label) {
1154
- case 0:
1155
- this.params.logger.debug("BaseSession execute", {
1156
- message: message,
1157
- });
1158
- return [4 /*yield*/, this.params.swarm.getAgent()];
1159
- case 1:
1160
- agent = _a.sent();
1161
- output = this.params.swarm.waitForOutput();
1162
- agent.execute(message);
1163
- return [4 /*yield*/, output];
1164
- case 2: return [2 /*return*/, _a.sent()];
1165
- }
1163
+ this._emitSubject = new Subject();
1164
+ this.execute = function (message_1) {
1165
+ var args_1 = [];
1166
+ for (var _i = 1; _i < arguments.length; _i++) {
1167
+ args_1[_i - 1] = arguments[_i];
1168
+ }
1169
+ return __awaiter(_this, __spreadArray([message_1], __read(args_1), false), void 0, function (message, noEmit) {
1170
+ var agent, outputAwaiter, output, _a;
1171
+ if (noEmit === void 0) { noEmit = false; }
1172
+ return __generator(this, function (_b) {
1173
+ switch (_b.label) {
1174
+ case 0:
1175
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " execute"), {
1176
+ message: message,
1177
+ noEmit: noEmit,
1178
+ });
1179
+ return [4 /*yield*/, this.params.swarm.getAgent()];
1180
+ case 1:
1181
+ agent = _b.sent();
1182
+ outputAwaiter = this.params.swarm.waitForOutput();
1183
+ agent.execute(message);
1184
+ return [4 /*yield*/, outputAwaiter];
1185
+ case 2:
1186
+ output = _b.sent();
1187
+ _a = !noEmit;
1188
+ if (!_a) return [3 /*break*/, 4];
1189
+ return [4 /*yield*/, this._emitSubject.next(output)];
1190
+ case 3:
1191
+ _a = (_b.sent());
1192
+ _b.label = 4;
1193
+ case 4:
1194
+ return [2 /*return*/, output];
1195
+ }
1196
+ });
1166
1197
  });
1167
- }); };
1198
+ };
1168
1199
  this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
1169
1200
  var agent;
1170
1201
  return __generator(this, function (_a) {
1171
1202
  switch (_a.label) {
1172
1203
  case 0:
1173
- this.params.logger.debug("BaseSession commitToolOutput", {
1204
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitToolOutput"), {
1174
1205
  content: content,
1175
1206
  });
1176
1207
  return [4 /*yield*/, this.params.swarm.getAgent()];
@@ -1186,7 +1217,7 @@ var ClientSession = /** @class */ (function () {
1186
1217
  return __generator(this, function (_a) {
1187
1218
  switch (_a.label) {
1188
1219
  case 0:
1189
- this.params.logger.debug("BaseSession commitSystemMessage", {
1220
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitSystemMessage"), {
1190
1221
  message: message,
1191
1222
  });
1192
1223
  return [4 /*yield*/, this.params.swarm.getAgent()];
@@ -1198,17 +1229,35 @@ var ClientSession = /** @class */ (function () {
1198
1229
  });
1199
1230
  }); };
1200
1231
  this.connect = function (connector) {
1201
- _this.params.logger.debug("BaseSession connect");
1232
+ _this.params.logger.debug("ClientSession clientId=".concat(_this.params.clientId, " connect"));
1233
+ _this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
1234
+ var _a;
1235
+ var _b;
1236
+ return __generator(this, function (_c) {
1237
+ switch (_c.label) {
1238
+ case 0:
1239
+ _a = connector;
1240
+ _b = {
1241
+ data: data
1242
+ };
1243
+ return [4 /*yield*/, this.params.swarm.getAgentName()];
1244
+ case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
1245
+ _b.clientId = this.params.clientId,
1246
+ _b)])];
1247
+ case 2: return [2 /*return*/, _c.sent()];
1248
+ }
1249
+ });
1250
+ }); });
1202
1251
  return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
1203
1252
  var _a;
1204
1253
  var _b;
1205
1254
  return __generator(this, function (_c) {
1206
1255
  switch (_c.label) {
1207
1256
  case 0:
1208
- this.params.logger.debug("BaseSession connect call");
1257
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " connect call"));
1209
1258
  _a = connector;
1210
1259
  _b = {};
1211
- return [4 /*yield*/, this.execute(incoming.data)];
1260
+ return [4 /*yield*/, this.execute(incoming.data, true)];
1212
1261
  case 1:
1213
1262
  _b.data = _c.sent();
1214
1263
  return [4 /*yield*/, this.params.swarm.getAgentName()];
@@ -1222,6 +1271,7 @@ var ClientSession = /** @class */ (function () {
1222
1271
  });
1223
1272
  }); };
1224
1273
  };
1274
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"));
1225
1275
  }
1226
1276
  return ClientSession;
1227
1277
  }());
@@ -1906,25 +1956,109 @@ var SessionValidationService = /** @class */ (function () {
1906
1956
  function SessionValidationService() {
1907
1957
  var _this = this;
1908
1958
  this.loggerService = inject(TYPES.loggerService);
1909
- this._sessionMap = new Map();
1959
+ this._historySwarmMap = new Map();
1960
+ this._sessionSwarmMap = new Map();
1961
+ this._agentSwarmMap = new Map();
1910
1962
  this.addSession = function (clientId, swarmName) {
1911
1963
  _this.loggerService.log("sessionValidationService addSession", {
1912
1964
  clientId: clientId,
1913
1965
  });
1914
- if (_this._sessionMap.has(clientId)) {
1966
+ if (_this._sessionSwarmMap.has(clientId)) {
1915
1967
  throw new Error("agent-swarm session clientId=".concat(clientId, " already exist"));
1916
1968
  }
1917
- _this._sessionMap.set(clientId, swarmName);
1969
+ _this._sessionSwarmMap.set(clientId, swarmName);
1970
+ };
1971
+ this.addAgentUsage = function (sessionId, agentName) {
1972
+ _this.loggerService.log("sessionValidationService addAgentUsage", {
1973
+ sessionId: sessionId,
1974
+ agentName: agentName,
1975
+ });
1976
+ if (_this._agentSwarmMap.has(sessionId)) {
1977
+ var agents = _this._agentSwarmMap.get(sessionId);
1978
+ if (!agents.includes(agentName)) {
1979
+ agents.push(agentName);
1980
+ }
1981
+ }
1982
+ else {
1983
+ _this._agentSwarmMap.set(sessionId, [agentName]);
1984
+ }
1985
+ };
1986
+ this.addHistoryUsage = function (sessionId, agentName) {
1987
+ _this.loggerService.log("sessionValidationService addHistoryUsage", {
1988
+ sessionId: sessionId,
1989
+ agentName: agentName,
1990
+ });
1991
+ if (_this._historySwarmMap.has(sessionId)) {
1992
+ var agents = _this._historySwarmMap.get(sessionId);
1993
+ if (!agents.includes(agentName)) {
1994
+ agents.push(agentName);
1995
+ }
1996
+ }
1997
+ else {
1998
+ _this._historySwarmMap.set(sessionId, [agentName]);
1999
+ }
2000
+ };
2001
+ this.removeAgentUsage = function (sessionId, agentName) {
2002
+ _this.loggerService.log("sessionValidationService removeAgentUsage", {
2003
+ sessionId: sessionId,
2004
+ agentName: agentName,
2005
+ });
2006
+ if (_this._agentSwarmMap.has(sessionId)) {
2007
+ var agents = _this._agentSwarmMap.get(sessionId);
2008
+ var agentIndex = agents.indexOf(agentName);
2009
+ if (agentIndex !== -1) {
2010
+ agents.splice(agentIndex, 1);
2011
+ }
2012
+ if (agents.length === 0) {
2013
+ _this._agentSwarmMap.delete(sessionId);
2014
+ }
2015
+ }
2016
+ else {
2017
+ throw new Error("No agents found for sessionId=".concat(sessionId));
2018
+ }
2019
+ };
2020
+ this.removeHistoryUsage = function (sessionId, agentName) {
2021
+ _this.loggerService.log("sessionValidationService removeHistoryUsage", {
2022
+ sessionId: sessionId,
2023
+ agentName: agentName,
2024
+ });
2025
+ if (_this._historySwarmMap.has(sessionId)) {
2026
+ var agents = _this._historySwarmMap.get(sessionId);
2027
+ var agentIndex = agents.indexOf(agentName);
2028
+ if (agentIndex !== -1) {
2029
+ agents.splice(agentIndex, 1);
2030
+ }
2031
+ if (agents.length === 0) {
2032
+ _this._historySwarmMap.delete(sessionId);
2033
+ }
2034
+ }
2035
+ else {
2036
+ throw new Error("No agents found for sessionId=".concat(sessionId));
2037
+ }
1918
2038
  };
1919
2039
  this.getSessionList = function () {
1920
2040
  _this.loggerService.log("sessionValidationService getSessionList");
1921
- return __spreadArray([], __read(_this._sessionMap.keys()), false);
2041
+ return __spreadArray([], __read(_this._sessionSwarmMap.keys()), false);
2042
+ };
2043
+ this.getSessionAgentList = function (clientId) {
2044
+ var _a;
2045
+ _this.loggerService.log("sessionValidationService getSessionAgentList", {
2046
+ clientId: clientId,
2047
+ });
2048
+ return (_a = _this._agentSwarmMap.get(clientId)) !== null && _a !== void 0 ? _a : [];
2049
+ };
2050
+ this.getSessionHistoryList = function (clientId) {
2051
+ var _a;
2052
+ _this.loggerService.log("sessionValidationService getSessionHistoryList", {
2053
+ clientId: clientId,
2054
+ });
2055
+ return (_a = _this._agentSwarmMap.get(clientId)) !== null && _a !== void 0 ? _a : [];
1922
2056
  };
1923
2057
  this.getSwarm = function (clientId) {
1924
2058
  _this.loggerService.log("sessionValidationService getSwarm", {
1925
2059
  clientId: clientId,
1926
2060
  });
1927
- var session = _this._sessionMap.get(clientId);
2061
+ var session = _this._sessionSwarmMap.get(clientId);
1928
2062
  if (session === undefined) {
1929
2063
  throw new Error("agent-swarm session clientId=".concat(clientId, " not found"));
1930
2064
  }
@@ -1932,7 +2066,7 @@ var SessionValidationService = /** @class */ (function () {
1932
2066
  };
1933
2067
  this.validate = function (clientId, source) {
1934
2068
  _this.loggerService.log("sessionValidationService validate", { clientId: clientId });
1935
- if (!_this._sessionMap.has(clientId)) {
2069
+ if (!_this._sessionSwarmMap.has(clientId)) {
1936
2070
  throw new Error("agent-swarm session clientId=".concat(clientId, " not exist source=").concat(source));
1937
2071
  }
1938
2072
  };
@@ -1940,7 +2074,7 @@ var SessionValidationService = /** @class */ (function () {
1940
2074
  _this.loggerService.log("sessionValidationService addSession", {
1941
2075
  clientId: clientId,
1942
2076
  });
1943
- _this._sessionMap.delete(clientId);
2077
+ _this._sessionSwarmMap.delete(clientId);
1944
2078
  };
1945
2079
  }
1946
2080
  return SessionValidationService;
@@ -2123,12 +2257,21 @@ var makeConnection = function (connector, clientId, swarmName) {
2123
2257
  swarm.sessionValidationService.addSession(clientId, swarmName);
2124
2258
  var send = swarm.sessionPublicService.connect(connector, clientId, swarmName);
2125
2259
  return queued(function (outgoing) { return __awaiter(void 0, void 0, void 0, function () {
2126
- return __generator(this, function (_a) {
2127
- switch (_a.label) {
2260
+ var _a;
2261
+ var _b;
2262
+ return __generator(this, function (_c) {
2263
+ switch (_c.label) {
2128
2264
  case 0:
2129
2265
  swarm.sessionValidationService.validate(clientId, "makeConnection");
2130
- return [4 /*yield*/, send(outgoing)];
2131
- case 1: return [2 /*return*/, _a.sent()];
2266
+ _a = send;
2267
+ _b = {
2268
+ data: outgoing
2269
+ };
2270
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
2271
+ case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
2272
+ _b.clientId = clientId,
2273
+ _b)])];
2274
+ case 2: return [2 /*return*/, _c.sent()];
2132
2275
  }
2133
2276
  });
2134
2277
  }); });
@@ -2292,30 +2435,81 @@ var getAgentHistory = function (clientId, agentName) { return __awaiter(void 0,
2292
2435
  });
2293
2436
  }); };
2294
2437
 
2295
- var commitToolOutput = function (content, clientId) {
2296
- swarm.sessionValidationService.validate(clientId, "commitToolOutput");
2297
- var swarmName = swarm.sessionValidationService.getSwarm(clientId);
2298
- swarm.swarmValidationService.validate(swarmName, "commitToolOutput");
2299
- swarm.sessionPublicService.commitToolOutput(content, clientId, swarmName);
2300
- };
2438
+ var commitToolOutput = function (content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
2439
+ var swarmName, currentAgentName;
2440
+ return __generator(this, function (_a) {
2441
+ switch (_a.label) {
2442
+ case 0:
2443
+ swarm.agentValidationService.validate(agentName, "commitSystemMessage");
2444
+ swarm.sessionValidationService.validate(clientId, "commitToolOutput");
2445
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
2446
+ swarm.swarmValidationService.validate(swarmName, "commitToolOutput");
2447
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
2448
+ case 1:
2449
+ currentAgentName = _a.sent();
2450
+ if (currentAgentName !== agentName) {
2451
+ swarm.loggerService.log('function "commitToolOutput" skipped due to the agent change', {
2452
+ currentAgentName: currentAgentName,
2453
+ agentName: agentName,
2454
+ });
2455
+ return [2 /*return*/];
2456
+ }
2457
+ return [4 /*yield*/, swarm.sessionPublicService.commitToolOutput(content, clientId, swarmName)];
2458
+ case 2:
2459
+ _a.sent();
2460
+ return [2 /*return*/];
2461
+ }
2462
+ });
2463
+ }); };
2301
2464
 
2302
- var commitSystemMessage = function (content, clientId) {
2303
- swarm.sessionValidationService.validate(clientId, "commitSystemMessage");
2304
- var swarmName = swarm.sessionValidationService.getSwarm(clientId);
2305
- swarm.swarmValidationService.validate(swarmName, "commitSystemMessage");
2306
- swarm.sessionPublicService.commitSystemMessage(content, clientId, swarmName);
2307
- };
2465
+ var commitSystemMessage = function (content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
2466
+ var swarmName, currentAgentName;
2467
+ return __generator(this, function (_a) {
2468
+ switch (_a.label) {
2469
+ case 0:
2470
+ swarm.agentValidationService.validate(agentName, "commitSystemMessage");
2471
+ swarm.sessionValidationService.validate(clientId, "commitSystemMessage");
2472
+ swarmName = swarm.sessionValidationService.getSwarm(clientId);
2473
+ swarm.swarmValidationService.validate(swarmName, "commitSystemMessage");
2474
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
2475
+ case 1:
2476
+ currentAgentName = _a.sent();
2477
+ if (currentAgentName !== agentName) {
2478
+ swarm.loggerService.log('function "commitSystemMessage" skipped due to the agent change', {
2479
+ currentAgentName: currentAgentName,
2480
+ agentName: agentName,
2481
+ });
2482
+ return [2 /*return*/];
2483
+ }
2484
+ return [4 /*yield*/, swarm.sessionPublicService.commitSystemMessage(content, clientId, swarmName)];
2485
+ case 2:
2486
+ _a.sent();
2487
+ return [2 /*return*/];
2488
+ }
2489
+ });
2490
+ }); };
2308
2491
 
2309
- var execute = function (content, clientId) { return __awaiter(void 0, void 0, void 0, function () {
2310
- var swarmName;
2492
+ var execute = function (content, clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
2493
+ var swarmName, currentAgentName;
2311
2494
  return __generator(this, function (_a) {
2312
2495
  switch (_a.label) {
2313
2496
  case 0:
2497
+ swarm.agentValidationService.validate(agentName, "commitSystemMessage");
2314
2498
  swarm.sessionValidationService.validate(clientId, "execute");
2315
2499
  swarmName = swarm.sessionValidationService.getSwarm(clientId);
2316
2500
  swarm.swarmValidationService.validate(swarmName, "execute");
2501
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
2502
+ case 1:
2503
+ currentAgentName = _a.sent();
2504
+ if (currentAgentName !== agentName) {
2505
+ swarm.loggerService.log('function "execute" skipped due to the agent change', {
2506
+ currentAgentName: currentAgentName,
2507
+ agentName: agentName,
2508
+ });
2509
+ return [2 /*return*/];
2510
+ }
2317
2511
  return [4 /*yield*/, swarm.sessionPublicService.execute(content, clientId, swarmName)];
2318
- case 1: return [2 /*return*/, _a.sent()];
2512
+ case 2: return [2 /*return*/, _a.sent()];
2319
2513
  }
2320
2514
  });
2321
2515
  }); };