agent-swarm-kit 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -103,11 +103,11 @@ app.get("/api/v1/session/:clientId", upgradeWebSocket((ctx) => {
103
103
  const { complete, dispose } = session(clientId, TEST_SWARM)
104
104
 
105
105
  return {
106
- onMessage(event, ws) {
106
+ onMessage: async (event, ws) => {
107
107
  const message = event.data.toString();
108
108
  ws.send(await complete(message));
109
109
  },
110
- onClose: () => {
110
+ onClose: async () => {
111
111
  await dispose();
112
112
  },
113
113
  }
package/build/index.cjs CHANGED
@@ -235,11 +235,13 @@ var CC_EMPTY_OUTPUT_PLACEHOLDERS = [
235
235
  ];
236
236
  var CC_KEEP_MESSAGES = 5;
237
237
  var CC_ANSWER_TIMEOUT_SECONDS = 120000;
238
+ var CC_GET_AGENT_HISTORY = function () { return new functoolsKit.PubsubArrayAdapter(); };
238
239
  var GLOBAL_CONFIG = {
239
240
  CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
240
241
  CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
241
242
  CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
242
243
  CC_ANSWER_TIMEOUT_SECONDS: CC_ANSWER_TIMEOUT_SECONDS,
244
+ CC_GET_AGENT_HISTORY: CC_GET_AGENT_HISTORY,
243
245
  };
244
246
  var setConfig = function (config) {
245
247
  Object.assign(GLOBAL_CONFIG, config);
@@ -259,7 +261,7 @@ var ClientAgent = /** @class */ (function () {
259
261
  return __generator(this, function (_a) {
260
262
  switch (_a.label) {
261
263
  case 0:
262
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _emitOuput"));
264
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _emitOuput"));
263
265
  validation = null;
264
266
  return [4 /*yield*/, this.params.validate(result)];
265
267
  case 1:
@@ -270,7 +272,7 @@ var ClientAgent = /** @class */ (function () {
270
272
  return [4 /*yield*/, this.params.validate(result_1)];
271
273
  case 3:
272
274
  if (validation = _a.sent()) {
273
- throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " model ressurect failed: ").concat(validation));
275
+ throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " model ressurect failed: ").concat(validation));
274
276
  }
275
277
  return [4 /*yield*/, this._outputSubject.next(result_1)];
276
278
  case 4:
@@ -288,7 +290,7 @@ var ClientAgent = /** @class */ (function () {
288
290
  return __generator(this, function (_a) {
289
291
  switch (_a.label) {
290
292
  case 0:
291
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel"));
293
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel"));
292
294
  return [4 /*yield*/, this.params.history.push({
293
295
  role: "resque",
294
296
  agentName: this.params.agentName,
@@ -311,7 +313,7 @@ var ClientAgent = /** @class */ (function () {
311
313
  return [4 /*yield*/, this.params.validate(result)];
312
314
  case 4:
313
315
  if (!(validation = _a.sent())) return [3 /*break*/, 6];
314
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel validation error: ").concat(validation));
316
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
315
317
  content = getPlaceholder();
316
318
  return [4 /*yield*/, this.params.history.push({
317
319
  agentName: this.params.agentName,
@@ -332,7 +334,7 @@ var ClientAgent = /** @class */ (function () {
332
334
  return __generator(this, function (_a) {
333
335
  switch (_a.label) {
334
336
  case 0:
335
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " waitForOutput"));
337
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " waitForOutput"));
336
338
  return [4 /*yield*/, this._outputSubject.toPromise()];
337
339
  case 1: return [2 /*return*/, _a.sent()];
338
340
  }
@@ -344,7 +346,7 @@ var ClientAgent = /** @class */ (function () {
344
346
  return __generator(this, function (_b) {
345
347
  switch (_b.label) {
346
348
  case 0:
347
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " getCompletion"));
349
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
348
350
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt)];
349
351
  case 1:
350
352
  messages = _b.sent();
@@ -362,7 +364,7 @@ var ClientAgent = /** @class */ (function () {
362
364
  return __generator(this, function (_a) {
363
365
  switch (_a.label) {
364
366
  case 0:
365
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitSystemMessage"));
367
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitSystemMessage"));
366
368
  return [4 /*yield*/, this.params.history.push({
367
369
  role: "system",
368
370
  agentName: this.params.agentName,
@@ -378,7 +380,7 @@ var ClientAgent = /** @class */ (function () {
378
380
  return __generator(this, function (_a) {
379
381
  switch (_a.label) {
380
382
  case 0:
381
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitToolOutput content=").concat(content));
383
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " commitToolOutput content=").concat(content));
382
384
  return [4 /*yield*/, this.params.history.push({
383
385
  role: "tool",
384
386
  agentName: this.params.agentName,
@@ -400,7 +402,7 @@ var ClientAgent = /** @class */ (function () {
400
402
  return __generator(this, function (_e) {
401
403
  switch (_e.label) {
402
404
  case 0:
403
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute begin"), { incoming: incoming });
405
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute begin"), { incoming: incoming });
404
406
  return [4 /*yield*/, this.params.history.push({
405
407
  role: "user",
406
408
  agentName: this.params.agentName,
@@ -412,7 +414,7 @@ var ClientAgent = /** @class */ (function () {
412
414
  case 2:
413
415
  message = _e.sent();
414
416
  if (!message.tool_calls) return [3 /*break*/, 11];
415
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " tool call begin"));
417
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " tool call begin"));
416
418
  _loop_1 = function (tool) {
417
419
  var targetFn, result_2, result_3;
418
420
  return __generator(this, function (_f) {
@@ -423,11 +425,11 @@ var ClientAgent = /** @class */ (function () {
423
425
  case 1:
424
426
  _f.sent();
425
427
  if (!!targetFn) return [3 /*break*/, 4];
426
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool function not found"));
428
+ 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"));
427
429
  return [4 /*yield*/, this_1._resurrectModel("No target function for ".concat(tool.function.name))];
428
430
  case 2:
429
431
  result_2 = _f.sent();
430
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_2));
432
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_2));
431
433
  return [4 /*yield*/, this_1._emitOuput(result_2)];
432
434
  case 3:
433
435
  _f.sent();
@@ -435,11 +437,11 @@ var ClientAgent = /** @class */ (function () {
435
437
  case 4: return [4 /*yield*/, functoolsKit.not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
436
438
  case 5:
437
439
  if (!_f.sent()) return [3 /*break*/, 8];
438
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool validation not passed"));
440
+ 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"));
439
441
  return [4 /*yield*/, this_1._resurrectModel("Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
440
442
  case 6:
441
443
  result_3 = _f.sent();
442
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_3));
444
+ this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " clientId=").concat(this_1.params.clientId, " execute end result=").concat(result_3));
443
445
  return [4 /*yield*/, this_1._emitOuput(result_3)];
444
446
  case 7:
445
447
  _f.sent();
@@ -449,14 +451,14 @@ var ClientAgent = /** @class */ (function () {
449
451
  * @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
450
452
  */
451
453
  targetFn.call(this_1.params.clientId, this_1.params.agentName, tool.function.arguments);
452
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool call executing"));
454
+ 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"));
453
455
  return [4 /*yield*/, Promise.race([
454
456
  this_1._toolCommitSubject.toPromise(),
455
457
  this_1._outputSubject.toPromise(),
456
458
  ])];
457
459
  case 9:
458
460
  _f.sent();
459
- this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool call end"));
461
+ 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"));
460
462
  return [2 /*return*/];
461
463
  }
462
464
  });
@@ -493,7 +495,7 @@ var ClientAgent = /** @class */ (function () {
493
495
  case 10: return [2 /*return*/];
494
496
  case 11:
495
497
  if (!message.tool_calls) {
496
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute no tool calls detected"));
498
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute no tool calls detected"));
497
499
  }
498
500
  result = message.content;
499
501
  return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
@@ -503,7 +505,7 @@ var ClientAgent = /** @class */ (function () {
503
505
  return [4 /*yield*/, this.params.validate(result)];
504
506
  case 13:
505
507
  if (!(validation = _e.sent())) return [3 /*break*/, 16];
506
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute invalid tool call detected: ").concat(validation), { result: result });
508
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute invalid tool call detected: ").concat(validation), { result: result });
507
509
  return [4 /*yield*/, this._resurrectModel("Invalid model output: ".concat(result))];
508
510
  case 14:
509
511
  result1 = _e.sent();
@@ -512,7 +514,7 @@ var ClientAgent = /** @class */ (function () {
512
514
  _e.sent();
513
515
  return [2 /*return*/];
514
516
  case 16:
515
- this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute end result=").concat(result));
517
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " execute end result=").concat(result));
516
518
  return [4 /*yield*/, this._emitOuput(result)];
517
519
  case 17:
518
520
  _e.sent();
@@ -520,6 +522,7 @@ var ClientAgent = /** @class */ (function () {
520
522
  }
521
523
  });
522
524
  }); });
525
+ this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"));
523
526
  }
524
527
  return ClientAgent;
525
528
  }());
@@ -615,6 +618,7 @@ var AgentConnectionService = /** @class */ (function () {
615
618
  var _this = this;
616
619
  this.loggerService = inject(TYPES.loggerService);
617
620
  this.contextService = inject(TYPES.contextService);
621
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
618
622
  this.historyConnectionService = inject(TYPES.historyConnectionService);
619
623
  this.agentSchemaService = inject(TYPES.agentSchemaService);
620
624
  this.toolSchemaService = inject(TYPES.toolSchemaService);
@@ -625,6 +629,7 @@ var AgentConnectionService = /** @class */ (function () {
625
629
  }, function (clientId, agentName) {
626
630
  var _a = _this.agentSchemaService.get(agentName), prompt = _a.prompt, tools = _a.tools, completionName = _a.completion, _b = _a.validate, validate = _b === void 0 ? validateDefault : _b;
627
631
  var completion = _this.completionSchemaService.get(completionName);
632
+ _this.sessionValidationService.addAgentUsage(clientId, agentName);
628
633
  return new ClientAgent({
629
634
  clientId: clientId,
630
635
  agentName: agentName,
@@ -693,6 +698,7 @@ var AgentConnectionService = /** @class */ (function () {
693
698
  context: this.contextService.context,
694
699
  });
695
700
  this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
701
+ this.sessionValidationService.removeAgentUsage(this.contextService.context.clientId, this.contextService.context.agentName);
696
702
  return [2 /*return*/];
697
703
  });
698
704
  }); };
@@ -842,6 +848,7 @@ var ClientHistory = /** @class */ (function () {
842
848
  }
843
849
  });
844
850
  }); };
851
+ this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " CTOR"));
845
852
  }
846
853
  return ClientHistory;
847
854
  }());
@@ -851,18 +858,22 @@ var HistoryConnectionService = /** @class */ (function () {
851
858
  var _this = this;
852
859
  this.loggerService = inject(TYPES.loggerService);
853
860
  this.contextService = inject(TYPES.contextService);
861
+ this.sessionValidationService = inject(TYPES.sessionValidationService);
854
862
  this.getItems = functoolsKit.memoize(function (_a) {
855
863
  var _b = __read(_a, 1), clientId = _b[0];
856
864
  return clientId;
857
- }, function () { return new functoolsKit.PubsubArrayAdapter(); });
865
+ }, function (clientId, agentName) {
866
+ return GLOBAL_CONFIG.CC_GET_AGENT_HISTORY(clientId, agentName);
867
+ });
858
868
  this.getHistory = functoolsKit.memoize(function (_a) {
859
869
  var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
860
870
  return "".concat(clientId, "-").concat(agentName);
861
871
  }, function (clientId, agentName) {
872
+ _this.sessionValidationService.addHistoryUsage(clientId, agentName);
862
873
  return new ClientHistory({
863
874
  clientId: clientId,
864
875
  agentName: agentName,
865
- items: _this.getItems(clientId),
876
+ items: _this.getItems(clientId, agentName),
866
877
  logger: _this.loggerService,
867
878
  });
868
879
  });
@@ -909,11 +920,12 @@ var HistoryConnectionService = /** @class */ (function () {
909
920
  this.loggerService.log("historyConnectionService dispose", {
910
921
  context: this.contextService.context,
911
922
  });
912
- return [4 /*yield*/, this.getItems(this.contextService.context.clientId).clear()];
923
+ return [4 /*yield*/, this.getItems(this.contextService.context.clientId, this.contextService.context.agentName).clear()];
913
924
  case 1:
914
925
  _a.sent();
915
926
  this.getItems.clear(this.contextService.context.clientId);
916
927
  this.getHistory.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
928
+ this.sessionValidationService.removeHistoryUsage(this.contextService.context.clientId, this.contextService.context.agentName);
917
929
  return [2 /*return*/];
918
930
  }
919
931
  });
@@ -943,13 +955,13 @@ var ClientSwarm = /** @class */ (function () {
943
955
  function ClientSwarm(params) {
944
956
  var _this = this;
945
957
  this.params = params;
946
- this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
958
+ this.waitForOutput = functoolsKit.queued(function () { return __awaiter(_this, void 0, void 0, function () {
947
959
  var START_TIME, _a, agentName, output, _b;
948
960
  var _this = this;
949
961
  return __generator(this, function (_c) {
950
962
  switch (_c.label) {
951
963
  case 0:
952
- this.params.logger.debug("BaseConnection waitForOutput");
964
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " waitForOutput"));
953
965
  START_TIME = Date.now();
954
966
  _c.label = 1;
955
967
  case 1:
@@ -982,10 +994,10 @@ var ClientSwarm = /** @class */ (function () {
982
994
  case 4: return [2 /*return*/];
983
995
  }
984
996
  });
985
- }); };
997
+ }); });
986
998
  this.getAgentName = function () { return __awaiter(_this, void 0, void 0, function () {
987
999
  return __generator(this, function (_a) {
988
- this.params.logger.debug("ClientSwarm getAgentName");
1000
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " getAgentName"));
989
1001
  return [2 /*return*/, this._activeAgent];
990
1002
  });
991
1003
  }); };
@@ -994,7 +1006,7 @@ var ClientSwarm = /** @class */ (function () {
994
1006
  return __generator(this, function (_a) {
995
1007
  switch (_a.label) {
996
1008
  case 0:
997
- this.params.logger.debug("ClientSwarm getAgent");
1009
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " getAgent"));
998
1010
  return [4 /*yield*/, this.getAgentName()];
999
1011
  case 1:
1000
1012
  agent = _a.sent();
@@ -1004,11 +1016,12 @@ var ClientSwarm = /** @class */ (function () {
1004
1016
  }); };
1005
1017
  this.setAgentName = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
1006
1018
  return __generator(this, function (_a) {
1007
- this.params.logger.debug("ClientSwarm setAgentName agentName=".concat(agentName));
1019
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " setAgentName agentName=").concat(agentName));
1008
1020
  this._activeAgent = agentName;
1009
1021
  return [2 /*return*/];
1010
1022
  });
1011
1023
  }); };
1024
+ this.params.logger.debug("ClientSwarm swarmName=".concat(this.params.swarmName, " clientId=").concat(this.params.clientId, " CTOR"));
1012
1025
  this._activeAgent = params.defaultAgent;
1013
1026
  }
1014
1027
  return ClientSwarm;
@@ -1149,30 +1162,48 @@ var ClientSession = /** @class */ (function () {
1149
1162
  function ClientSession(params) {
1150
1163
  var _this = this;
1151
1164
  this.params = params;
1152
- this.execute = function (message) { return __awaiter(_this, void 0, void 0, function () {
1153
- var agent, output;
1154
- return __generator(this, function (_a) {
1155
- switch (_a.label) {
1156
- case 0:
1157
- this.params.logger.debug("BaseSession execute", {
1158
- message: message,
1159
- });
1160
- return [4 /*yield*/, this.params.swarm.getAgent()];
1161
- case 1:
1162
- agent = _a.sent();
1163
- output = this.params.swarm.waitForOutput();
1164
- agent.execute(message);
1165
- return [4 /*yield*/, output];
1166
- case 2: return [2 /*return*/, _a.sent()];
1167
- }
1165
+ this._emitSubject = new functoolsKit.Subject();
1166
+ this.execute = function (message_1) {
1167
+ var args_1 = [];
1168
+ for (var _i = 1; _i < arguments.length; _i++) {
1169
+ args_1[_i - 1] = arguments[_i];
1170
+ }
1171
+ return __awaiter(_this, __spreadArray([message_1], __read(args_1), false), void 0, function (message, noEmit) {
1172
+ var agent, outputAwaiter, output, _a;
1173
+ if (noEmit === void 0) { noEmit = false; }
1174
+ return __generator(this, function (_b) {
1175
+ switch (_b.label) {
1176
+ case 0:
1177
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " execute"), {
1178
+ message: message,
1179
+ noEmit: noEmit,
1180
+ });
1181
+ return [4 /*yield*/, this.params.swarm.getAgent()];
1182
+ case 1:
1183
+ agent = _b.sent();
1184
+ outputAwaiter = this.params.swarm.waitForOutput();
1185
+ agent.execute(message);
1186
+ return [4 /*yield*/, outputAwaiter];
1187
+ case 2:
1188
+ output = _b.sent();
1189
+ _a = !noEmit;
1190
+ if (!_a) return [3 /*break*/, 4];
1191
+ return [4 /*yield*/, this._emitSubject.next(output)];
1192
+ case 3:
1193
+ _a = (_b.sent());
1194
+ _b.label = 4;
1195
+ case 4:
1196
+ return [2 /*return*/, output];
1197
+ }
1198
+ });
1168
1199
  });
1169
- }); };
1200
+ };
1170
1201
  this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
1171
1202
  var agent;
1172
1203
  return __generator(this, function (_a) {
1173
1204
  switch (_a.label) {
1174
1205
  case 0:
1175
- this.params.logger.debug("BaseSession commitToolOutput", {
1206
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitToolOutput"), {
1176
1207
  content: content,
1177
1208
  });
1178
1209
  return [4 /*yield*/, this.params.swarm.getAgent()];
@@ -1188,7 +1219,7 @@ var ClientSession = /** @class */ (function () {
1188
1219
  return __generator(this, function (_a) {
1189
1220
  switch (_a.label) {
1190
1221
  case 0:
1191
- this.params.logger.debug("BaseSession commitSystemMessage", {
1222
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " commitSystemMessage"), {
1192
1223
  message: message,
1193
1224
  });
1194
1225
  return [4 /*yield*/, this.params.swarm.getAgent()];
@@ -1200,17 +1231,35 @@ var ClientSession = /** @class */ (function () {
1200
1231
  });
1201
1232
  }); };
1202
1233
  this.connect = function (connector) {
1203
- _this.params.logger.debug("BaseSession connect");
1234
+ _this.params.logger.debug("ClientSession clientId=".concat(_this.params.clientId, " connect"));
1235
+ _this._emitSubject.subscribe(function (data) { return __awaiter(_this, void 0, void 0, function () {
1236
+ var _a;
1237
+ var _b;
1238
+ return __generator(this, function (_c) {
1239
+ switch (_c.label) {
1240
+ case 0:
1241
+ _a = connector;
1242
+ _b = {
1243
+ data: data
1244
+ };
1245
+ return [4 /*yield*/, this.params.swarm.getAgentName()];
1246
+ case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
1247
+ _b.clientId = this.params.clientId,
1248
+ _b)])];
1249
+ case 2: return [2 /*return*/, _c.sent()];
1250
+ }
1251
+ });
1252
+ }); });
1204
1253
  return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
1205
1254
  var _a;
1206
1255
  var _b;
1207
1256
  return __generator(this, function (_c) {
1208
1257
  switch (_c.label) {
1209
1258
  case 0:
1210
- this.params.logger.debug("BaseSession connect call");
1259
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " connect call"));
1211
1260
  _a = connector;
1212
1261
  _b = {};
1213
- return [4 /*yield*/, this.execute(incoming.data)];
1262
+ return [4 /*yield*/, this.execute(incoming.data, true)];
1214
1263
  case 1:
1215
1264
  _b.data = _c.sent();
1216
1265
  return [4 /*yield*/, this.params.swarm.getAgentName()];
@@ -1224,6 +1273,7 @@ var ClientSession = /** @class */ (function () {
1224
1273
  });
1225
1274
  }); };
1226
1275
  };
1276
+ this.params.logger.debug("ClientSession clientId=".concat(this.params.clientId, " CTOR"));
1227
1277
  }
1228
1278
  return ClientSession;
1229
1279
  }());
@@ -1908,25 +1958,109 @@ var SessionValidationService = /** @class */ (function () {
1908
1958
  function SessionValidationService() {
1909
1959
  var _this = this;
1910
1960
  this.loggerService = inject(TYPES.loggerService);
1911
- this._sessionMap = new Map();
1961
+ this._historySwarmMap = new Map();
1962
+ this._sessionSwarmMap = new Map();
1963
+ this._agentSwarmMap = new Map();
1912
1964
  this.addSession = function (clientId, swarmName) {
1913
1965
  _this.loggerService.log("sessionValidationService addSession", {
1914
1966
  clientId: clientId,
1915
1967
  });
1916
- if (_this._sessionMap.has(clientId)) {
1968
+ if (_this._sessionSwarmMap.has(clientId)) {
1917
1969
  throw new Error("agent-swarm session clientId=".concat(clientId, " already exist"));
1918
1970
  }
1919
- _this._sessionMap.set(clientId, swarmName);
1971
+ _this._sessionSwarmMap.set(clientId, swarmName);
1972
+ };
1973
+ this.addAgentUsage = function (sessionId, agentName) {
1974
+ _this.loggerService.log("sessionValidationService addAgentUsage", {
1975
+ sessionId: sessionId,
1976
+ agentName: agentName,
1977
+ });
1978
+ if (_this._agentSwarmMap.has(sessionId)) {
1979
+ var agents = _this._agentSwarmMap.get(sessionId);
1980
+ if (!agents.includes(agentName)) {
1981
+ agents.push(agentName);
1982
+ }
1983
+ }
1984
+ else {
1985
+ _this._agentSwarmMap.set(sessionId, [agentName]);
1986
+ }
1987
+ };
1988
+ this.addHistoryUsage = function (sessionId, agentName) {
1989
+ _this.loggerService.log("sessionValidationService addHistoryUsage", {
1990
+ sessionId: sessionId,
1991
+ agentName: agentName,
1992
+ });
1993
+ if (_this._historySwarmMap.has(sessionId)) {
1994
+ var agents = _this._historySwarmMap.get(sessionId);
1995
+ if (!agents.includes(agentName)) {
1996
+ agents.push(agentName);
1997
+ }
1998
+ }
1999
+ else {
2000
+ _this._historySwarmMap.set(sessionId, [agentName]);
2001
+ }
2002
+ };
2003
+ this.removeAgentUsage = function (sessionId, agentName) {
2004
+ _this.loggerService.log("sessionValidationService removeAgentUsage", {
2005
+ sessionId: sessionId,
2006
+ agentName: agentName,
2007
+ });
2008
+ if (_this._agentSwarmMap.has(sessionId)) {
2009
+ var agents = _this._agentSwarmMap.get(sessionId);
2010
+ var agentIndex = agents.indexOf(agentName);
2011
+ if (agentIndex !== -1) {
2012
+ agents.splice(agentIndex, 1);
2013
+ }
2014
+ if (agents.length === 0) {
2015
+ _this._agentSwarmMap.delete(sessionId);
2016
+ }
2017
+ }
2018
+ else {
2019
+ throw new Error("No agents found for sessionId=".concat(sessionId));
2020
+ }
2021
+ };
2022
+ this.removeHistoryUsage = function (sessionId, agentName) {
2023
+ _this.loggerService.log("sessionValidationService removeHistoryUsage", {
2024
+ sessionId: sessionId,
2025
+ agentName: agentName,
2026
+ });
2027
+ if (_this._historySwarmMap.has(sessionId)) {
2028
+ var agents = _this._historySwarmMap.get(sessionId);
2029
+ var agentIndex = agents.indexOf(agentName);
2030
+ if (agentIndex !== -1) {
2031
+ agents.splice(agentIndex, 1);
2032
+ }
2033
+ if (agents.length === 0) {
2034
+ _this._historySwarmMap.delete(sessionId);
2035
+ }
2036
+ }
2037
+ else {
2038
+ throw new Error("No agents found for sessionId=".concat(sessionId));
2039
+ }
1920
2040
  };
1921
2041
  this.getSessionList = function () {
1922
2042
  _this.loggerService.log("sessionValidationService getSessionList");
1923
- return __spreadArray([], __read(_this._sessionMap.keys()), false);
2043
+ return __spreadArray([], __read(_this._sessionSwarmMap.keys()), false);
2044
+ };
2045
+ this.getSessionAgentList = function (clientId) {
2046
+ var _a;
2047
+ _this.loggerService.log("sessionValidationService getSessionAgentList", {
2048
+ clientId: clientId,
2049
+ });
2050
+ return (_a = _this._agentSwarmMap.get(clientId)) !== null && _a !== void 0 ? _a : [];
2051
+ };
2052
+ this.getSessionHistoryList = function (clientId) {
2053
+ var _a;
2054
+ _this.loggerService.log("sessionValidationService getSessionHistoryList", {
2055
+ clientId: clientId,
2056
+ });
2057
+ return (_a = _this._agentSwarmMap.get(clientId)) !== null && _a !== void 0 ? _a : [];
1924
2058
  };
1925
2059
  this.getSwarm = function (clientId) {
1926
2060
  _this.loggerService.log("sessionValidationService getSwarm", {
1927
2061
  clientId: clientId,
1928
2062
  });
1929
- var session = _this._sessionMap.get(clientId);
2063
+ var session = _this._sessionSwarmMap.get(clientId);
1930
2064
  if (session === undefined) {
1931
2065
  throw new Error("agent-swarm session clientId=".concat(clientId, " not found"));
1932
2066
  }
@@ -1934,7 +2068,7 @@ var SessionValidationService = /** @class */ (function () {
1934
2068
  };
1935
2069
  this.validate = function (clientId, source) {
1936
2070
  _this.loggerService.log("sessionValidationService validate", { clientId: clientId });
1937
- if (!_this._sessionMap.has(clientId)) {
2071
+ if (!_this._sessionSwarmMap.has(clientId)) {
1938
2072
  throw new Error("agent-swarm session clientId=".concat(clientId, " not exist source=").concat(source));
1939
2073
  }
1940
2074
  };
@@ -1942,7 +2076,7 @@ var SessionValidationService = /** @class */ (function () {
1942
2076
  _this.loggerService.log("sessionValidationService addSession", {
1943
2077
  clientId: clientId,
1944
2078
  });
1945
- _this._sessionMap.delete(clientId);
2079
+ _this._sessionSwarmMap.delete(clientId);
1946
2080
  };
1947
2081
  }
1948
2082
  return SessionValidationService;
@@ -2125,12 +2259,21 @@ var makeConnection = function (connector, clientId, swarmName) {
2125
2259
  swarm.sessionValidationService.addSession(clientId, swarmName);
2126
2260
  var send = swarm.sessionPublicService.connect(connector, clientId, swarmName);
2127
2261
  return functoolsKit.queued(function (outgoing) { return __awaiter(void 0, void 0, void 0, function () {
2128
- return __generator(this, function (_a) {
2129
- switch (_a.label) {
2262
+ var _a;
2263
+ var _b;
2264
+ return __generator(this, function (_c) {
2265
+ switch (_c.label) {
2130
2266
  case 0:
2131
2267
  swarm.sessionValidationService.validate(clientId, "makeConnection");
2132
- return [4 /*yield*/, send(outgoing)];
2133
- case 1: return [2 /*return*/, _a.sent()];
2268
+ _a = send;
2269
+ _b = {
2270
+ data: outgoing
2271
+ };
2272
+ return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
2273
+ case 1: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
2274
+ _b.clientId = clientId,
2275
+ _b)])];
2276
+ case 2: return [2 /*return*/, _c.sent()];
2134
2277
  }
2135
2278
  });
2136
2279
  }); });