agent-swarm-kit 1.0.154 → 1.0.156

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
@@ -2339,10 +2339,15 @@ var ClientAgent = /** @class */ (function () {
2339
2339
  })];
2340
2340
  case 2:
2341
2341
  _b.sent();
2342
- return [4 /*yield*/, this.getCompletion(mode)];
2342
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 4];
2343
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2343
2344
  case 3:
2345
+ _b.sent();
2346
+ return [2 /*return*/, placeholder];
2347
+ case 4: return [4 /*yield*/, this.getCompletion(mode)];
2348
+ case 5:
2344
2349
  rawMessage = _b.sent();
2345
- if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
2350
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 8];
2346
2351
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2347
2352
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel should not emit tool_calls"));
2348
2353
  console.warn("agent-swarm _resurrectModel should not emit tool_calls for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
@@ -2352,22 +2357,22 @@ var ClientAgent = /** @class */ (function () {
2352
2357
  agentName: this.params.agentName,
2353
2358
  content: "_resurrectModel should not emit tool calls",
2354
2359
  })];
2355
- case 4:
2360
+ case 6:
2356
2361
  _b.sent();
2357
2362
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2358
- case 5:
2363
+ case 7:
2359
2364
  _b.sent();
2360
2365
  return [2 /*return*/, placeholder];
2361
- case 6: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2362
- case 7:
2366
+ case 8: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2367
+ case 9:
2363
2368
  message = _b.sent();
2364
2369
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2365
- case 8:
2370
+ case 10:
2366
2371
  result = _b.sent();
2367
2372
  validation = null;
2368
2373
  return [4 /*yield*/, this.params.validate(result)];
2369
- case 9:
2370
- if (!(validation = _b.sent())) return [3 /*break*/, 11];
2374
+ case 11:
2375
+ if (!(validation = _b.sent())) return [3 /*break*/, 13];
2371
2376
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2372
2377
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2373
2378
  console.warn("agent-swarm model resurrect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
@@ -2377,15 +2382,15 @@ var ClientAgent = /** @class */ (function () {
2377
2382
  agentName: this.params.agentName,
2378
2383
  content: "_resurrectModel failed validation: ".concat(validation),
2379
2384
  })];
2380
- case 10:
2381
- _b.sent();
2382
- return [3 /*break*/, 13];
2383
- case 11: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2384
2385
  case 12:
2385
2386
  _b.sent();
2386
- _b.label = 13;
2387
- case 13: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2387
+ return [3 /*break*/, 15];
2388
+ case 13: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2388
2389
  case 14:
2390
+ _b.sent();
2391
+ _b.label = 15;
2392
+ case 15: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2393
+ case 16:
2389
2394
  _b.sent();
2390
2395
  return [2 /*return*/, placeholder];
2391
2396
  }
@@ -2416,15 +2421,15 @@ var ClientAgent = /** @class */ (function () {
2416
2421
  ClientAgent.prototype.getCompletion = function (mode) {
2417
2422
  return __awaiter(this, void 0, void 0, function () {
2418
2423
  var messages, args, output, message, result, validation, messages_1, args_1, output_1, output_2;
2419
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2420
- return __generator(this, function (_k) {
2421
- switch (_k.label) {
2424
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
2425
+ return __generator(this, function (_r) {
2426
+ switch (_r.label) {
2422
2427
  case 0:
2423
2428
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2424
2429
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
2425
2430
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2426
2431
  case 1:
2427
- messages = _k.sent();
2432
+ messages = _r.sent();
2428
2433
  args = {
2429
2434
  clientId: this.params.clientId,
2430
2435
  agentName: this.params.agentName,
@@ -2436,22 +2441,29 @@ var ClientAgent = /** @class */ (function () {
2436
2441
  };
2437
2442
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
2438
2443
  case 2:
2439
- output = _k.sent();
2444
+ output = _r.sent();
2445
+ if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush") {
2446
+ ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
2447
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
2448
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2449
+ }
2440
2450
  return [4 /*yield*/, this.params.map(output, this.params.clientId, this.params.agentName)];
2441
2451
  case 3:
2442
- message = _k.sent();
2452
+ message = _r.sent();
2443
2453
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2444
2454
  case 4:
2445
- result = _k.sent();
2446
- if ((_b = message.tool_calls) === null || _b === void 0 ? void 0 : _b.length) {
2455
+ result = _r.sent();
2456
+ if ((_d = message.tool_calls) === null || _d === void 0 ? void 0 : _d.length) {
2457
+ ((_e = this.params.completion.callbacks) === null || _e === void 0 ? void 0 : _e.onComplete) &&
2458
+ ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete(args, output));
2447
2459
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2448
2460
  }
2449
2461
  return [4 /*yield*/, this.params.validate(result)];
2450
2462
  case 5:
2451
- validation = _k.sent();
2463
+ validation = _r.sent();
2452
2464
  if (!!validation) return [3 /*break*/, 6];
2453
- ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete) &&
2454
- ((_d = this.params.completion.callbacks) === null || _d === void 0 ? void 0 : _d.onComplete(args, output));
2465
+ ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete) &&
2466
+ ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete(args, output));
2455
2467
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2456
2468
  case 6:
2457
2469
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 10];
@@ -2463,36 +2475,38 @@ var ClientAgent = /** @class */ (function () {
2463
2475
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2464
2476
  })];
2465
2477
  case 7:
2466
- _k.sent();
2478
+ _r.sent();
2467
2479
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2468
2480
  case 8:
2469
- messages_1 = _k.sent();
2481
+ messages_1 = _r.sent();
2470
2482
  args_1 = {
2471
2483
  clientId: this.params.clientId,
2472
2484
  agentName: this.params.agentName,
2473
2485
  messages: messages_1,
2474
2486
  mode: mode,
2475
- tools: (_e = this.params.tools) === null || _e === void 0 ? void 0 : _e.map(function (t) {
2487
+ tools: (_j = this.params.tools) === null || _j === void 0 ? void 0 : _j.map(function (t) {
2476
2488
  return lodashEs.omit(t, "toolName", "docNote", "call", "validate", "callbacks");
2477
2489
  }),
2478
2490
  };
2479
2491
  return [4 /*yield*/, this.params.completion.getCompletion(args_1)];
2480
2492
  case 9:
2481
- output_1 = _k.sent();
2493
+ output_1 = _r.sent();
2494
+ ((_k = this.params.completion.callbacks) === null || _k === void 0 ? void 0 : _k.onComplete) &&
2495
+ ((_l = this.params.completion.callbacks) === null || _l === void 0 ? void 0 : _l.onComplete(args_1, output_1));
2482
2496
  return [2 /*return*/, __assign(__assign({}, output_1), { content: output_1.content || "" })];
2483
2497
  case 10:
2484
2498
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 12];
2485
2499
  console.warn("agent-swarm model using custom resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2486
2500
  return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2487
2501
  case 11:
2488
- output_2 = _k.sent();
2489
- ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete) &&
2490
- ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete(args, output_2));
2502
+ output_2 = _r.sent();
2503
+ ((_m = this.params.completion.callbacks) === null || _m === void 0 ? void 0 : _m.onComplete) &&
2504
+ ((_o = this.params.completion.callbacks) === null || _o === void 0 ? void 0 : _o.onComplete(args, output_2));
2491
2505
  return [2 /*return*/, output_2];
2492
2506
  case 12:
2493
2507
  console.warn("agent-swarm model completion pending resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2494
- ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete) &&
2495
- ((_j = this.params.completion.callbacks) === null || _j === void 0 ? void 0 : _j.onComplete(args, output));
2508
+ ((_p = this.params.completion.callbacks) === null || _p === void 0 ? void 0 : _p.onComplete) &&
2509
+ ((_q = this.params.completion.callbacks) === null || _q === void 0 ? void 0 : _q.onComplete(args, output));
2496
2510
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2497
2511
  }
2498
2512
  });
@@ -15912,6 +15926,10 @@ var SchemaUtils = /** @class */ (function () {
15912
15926
  */
15913
15927
  var Schema = new SchemaUtils();
15914
15928
 
15929
+ /**
15930
+ * @see https://github.com/ollama/ollama/blob/86a622cbdc69e9fd501764ff7565e977fc98f00a/server/model.go#L158
15931
+ */
15932
+ var TOOL_PROTOCOL_PROMPT = functoolsKit.str.newline("For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:", "<tool_call>", "{\"name\": <function-name>, \"arguments\": <args-json-object>}", "</tool_call>");
15915
15933
  var AdapterUtils = /** @class */ (function () {
15916
15934
  function AdapterUtils() {
15917
15935
  var _this = this;
@@ -15941,7 +15959,7 @@ var AdapterUtils = /** @class */ (function () {
15941
15959
  return __generator(this, function (_e) {
15942
15960
  switch (_e.label) {
15943
15961
  case 0:
15944
- LoggerAdapter.logClient(clientId, "AdapterUtils history", JSON.stringify(rawMessages));
15962
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromOpenAI completion", JSON.stringify(rawMessages));
15945
15963
  messages = rawMessages.map(function (_a) {
15946
15964
  var role = _a.role, tool_call_id = _a.tool_call_id, tool_calls = _a.tool_calls, content = _a.content;
15947
15965
  return ({
@@ -15961,6 +15979,8 @@ var AdapterUtils = /** @class */ (function () {
15961
15979
  model: model,
15962
15980
  messages: messages,
15963
15981
  tools: tools,
15982
+ temperature: 0,
15983
+ seed: 0,
15964
15984
  response_format: response_format,
15965
15985
  })];
15966
15986
  case 1:
@@ -15982,6 +16002,138 @@ var AdapterUtils = /** @class */ (function () {
15982
16002
  });
15983
16003
  }); };
15984
16004
  };
16005
+ /**
16006
+ * Creates a function to interact with LMStudio's chat completions.
16007
+ *
16008
+ * @param {any} openai - The LMStudio instance.
16009
+ * @param {string} [model="saiga_yandexgpt_8b_gguf"] - The model to use for completions.
16010
+ * @param {Object} [response_format] - The format of the response.
16011
+ * @returns {Function} - A function that takes completion arguments and returns a response from LMStudio.
16012
+ */
16013
+ this.fromLMStudio = function (openai, model, response_format) {
16014
+ if (model === void 0) { model = "saiga_yandexgpt_8b_gguf"; }
16015
+ /**
16016
+ * Handles the completion request to LMStudio.
16017
+ *
16018
+ * @param {ICompletionArgs} args - The arguments for the completion request.
16019
+ * @param {string} args.agentName - The name of the agent.
16020
+ * @param {Array} args.messages - The messages to send to LMStudio.
16021
+ * @param {string} args.mode - The mode of the completion.
16022
+ * @param {Array} args.tools - The tools to use for the completion.
16023
+ * @param {string} args.clientId - The client ID.
16024
+ * @returns {Promise<Object>} - The response from LMStudio.
16025
+ */
16026
+ return function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
16027
+ var messages, _c, _d, content, role, tool_calls;
16028
+ var agentName = _b.agentName, rawMessages = _b.messages, mode = _b.mode, tools = _b.tools, clientId = _b.clientId;
16029
+ return __generator(this, function (_e) {
16030
+ switch (_e.label) {
16031
+ case 0:
16032
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromLMStudio completion", JSON.stringify(rawMessages));
16033
+ messages = rawMessages.map(function (_a) {
16034
+ var role = _a.role, tool_call_id = _a.tool_call_id, tool_calls = _a.tool_calls, content = _a.content;
16035
+ return ({
16036
+ role: role,
16037
+ tool_call_id: tool_call_id,
16038
+ content: content,
16039
+ tool_calls: tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.map(function (_a) {
16040
+ var f = _a.function, rest = __rest(_a, ["function"]);
16041
+ return (__assign(__assign({}, rest), { function: {
16042
+ name: f.name,
16043
+ arguments: JSON.stringify(f.arguments),
16044
+ } }));
16045
+ }),
16046
+ });
16047
+ });
16048
+ return [4 /*yield*/, openai.chat.completions.create({
16049
+ model: model,
16050
+ messages: messages,
16051
+ tools: tools,
16052
+ temperature: 0,
16053
+ seed: 0,
16054
+ response_format: response_format,
16055
+ })];
16056
+ case 1:
16057
+ _c = __read.apply(void 0, [(_e.sent()).choices, 1]), _d = _c[0].message, content = _d.content, role = _d.role, tool_calls = _d.tool_calls;
16058
+ return [2 /*return*/, {
16059
+ content: content,
16060
+ mode: mode,
16061
+ agentName: agentName,
16062
+ role: role,
16063
+ tool_calls: tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.map(function (_a) {
16064
+ var f = _a.function, rest = __rest(_a, ["function"]);
16065
+ return (__assign(__assign({}, rest), { function: {
16066
+ name: f.name,
16067
+ arguments: JSON.parse(f.arguments),
16068
+ } }));
16069
+ }),
16070
+ }];
16071
+ }
16072
+ });
16073
+ }); };
16074
+ };
16075
+ /**
16076
+ * Creates a function to interact with Ollama's chat completions.
16077
+ *
16078
+ * @param {any} ollama - The Ollama instance.
16079
+ * @param {string} [model="nemotron-mini:4b"] - The model to use for completions.
16080
+ * @param {string} [tool_call_protocol=TOOL_PROTOCOL_PROMPT] - The protocol for tool calls.
16081
+ * @returns {Function} - A function that takes completion arguments and returns a response from Ollama.
16082
+ */
16083
+ this.fromOllama = function (ollama, model, tool_call_protocol) {
16084
+ if (model === void 0) { model = "nemotron-mini:4b"; }
16085
+ if (tool_call_protocol === void 0) { tool_call_protocol = TOOL_PROTOCOL_PROMPT; }
16086
+ /**
16087
+ * Handles the completion request to Ollama.
16088
+ *
16089
+ * @param {ICompletionArgs} args - The arguments for the completion request.
16090
+ * @param {string} args.agentName - The name of the agent.
16091
+ * @param {Array} args.messages - The messages to send to Ollama.
16092
+ * @param {string} args.mode - The mode of the completion.
16093
+ * @param {Array} args.tools - The tools to use for the completion.
16094
+ * @param {string} args.clientId - The client ID.
16095
+ * @returns {Promise<Object>} - The response from Ollama.
16096
+ */
16097
+ return function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
16098
+ var messages, response;
16099
+ var _c;
16100
+ var agentName = _b.agentName, rawMessages = _b.messages, mode = _b.mode, tools = _b.tools, clientId = _b.clientId;
16101
+ return __generator(this, function (_d) {
16102
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromOllama completion", JSON.stringify(rawMessages));
16103
+ messages = __spreadArray([], __read(rawMessages), false);
16104
+ messages.unshift({
16105
+ agentName: agentName,
16106
+ mode: "tool",
16107
+ role: "system",
16108
+ content: tool_call_protocol,
16109
+ });
16110
+ response = ollama.chat({
16111
+ model: model,
16112
+ keep_alive: "24h",
16113
+ options: {
16114
+ temperature: 0,
16115
+ seed: 0,
16116
+ },
16117
+ messages: messages.map(function (message) {
16118
+ var _a;
16119
+ return ({
16120
+ content: message.content,
16121
+ role: message.role,
16122
+ tool_calls: (_a = message.tool_calls) === null || _a === void 0 ? void 0 : _a.map(function (call) { return ({
16123
+ function: call.function,
16124
+ }); }),
16125
+ });
16126
+ }),
16127
+ tools: tools,
16128
+ });
16129
+ return [2 /*return*/, __assign(__assign({}, response.message), { tool_calls: (_c = response.message.tool_calls) === null || _c === void 0 ? void 0 : _c.map(function (call) { return ({
16130
+ function: call.function,
16131
+ type: "function",
16132
+ id: functoolsKit.randomString(),
16133
+ }); }), mode: mode, agentName: agentName, role: response.message.role })];
16134
+ });
16135
+ }); };
16136
+ };
15985
16137
  }
15986
16138
  return AdapterUtils;
15987
16139
  }());
package/build/index.mjs CHANGED
@@ -2337,10 +2337,15 @@ var ClientAgent = /** @class */ (function () {
2337
2337
  })];
2338
2338
  case 2:
2339
2339
  _b.sent();
2340
- return [4 /*yield*/, this.getCompletion(mode)];
2340
+ if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush")) return [3 /*break*/, 4];
2341
+ return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2341
2342
  case 3:
2343
+ _b.sent();
2344
+ return [2 /*return*/, placeholder];
2345
+ case 4: return [4 /*yield*/, this.getCompletion(mode)];
2346
+ case 5:
2342
2347
  rawMessage = _b.sent();
2343
- if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
2348
+ if (!((_a = rawMessage.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 8];
2344
2349
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2345
2350
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel should not emit tool_calls"));
2346
2351
  console.warn("agent-swarm _resurrectModel should not emit tool_calls for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY));
@@ -2350,22 +2355,22 @@ var ClientAgent = /** @class */ (function () {
2350
2355
  agentName: this.params.agentName,
2351
2356
  content: "_resurrectModel should not emit tool calls",
2352
2357
  })];
2353
- case 4:
2358
+ case 6:
2354
2359
  _b.sent();
2355
2360
  return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2356
- case 5:
2361
+ case 7:
2357
2362
  _b.sent();
2358
2363
  return [2 /*return*/, placeholder];
2359
- case 6: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2360
- case 7:
2364
+ case 8: return [4 /*yield*/, this.params.map(rawMessage, this.params.clientId, this.params.agentName)];
2365
+ case 9:
2361
2366
  message = _b.sent();
2362
2367
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2363
- case 8:
2368
+ case 10:
2364
2369
  result = _b.sent();
2365
2370
  validation = null;
2366
2371
  return [4 /*yield*/, this.params.validate(result)];
2367
- case 9:
2368
- if (!(validation = _b.sent())) return [3 /*break*/, 11];
2372
+ case 11:
2373
+ if (!(validation = _b.sent())) return [3 /*break*/, 13];
2369
2374
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2370
2375
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " _resurrectModel validation error: ").concat(validation));
2371
2376
  console.warn("agent-swarm model resurrect did not solved the problem for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
@@ -2375,15 +2380,15 @@ var ClientAgent = /** @class */ (function () {
2375
2380
  agentName: this.params.agentName,
2376
2381
  content: "_resurrectModel failed validation: ".concat(validation),
2377
2382
  })];
2378
- case 10:
2379
- _b.sent();
2380
- return [3 /*break*/, 13];
2381
- case 11: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2382
2383
  case 12:
2383
2384
  _b.sent();
2384
- _b.label = 13;
2385
- case 13: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2385
+ return [3 /*break*/, 15];
2386
+ case 13: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
2386
2387
  case 14:
2388
+ _b.sent();
2389
+ _b.label = 15;
2390
+ case 15: return [4 /*yield*/, this._resqueSubject.next(MODEL_RESQUE_SYMBOL)];
2391
+ case 16:
2387
2392
  _b.sent();
2388
2393
  return [2 /*return*/, placeholder];
2389
2394
  }
@@ -2414,15 +2419,15 @@ var ClientAgent = /** @class */ (function () {
2414
2419
  ClientAgent.prototype.getCompletion = function (mode) {
2415
2420
  return __awaiter(this, void 0, void 0, function () {
2416
2421
  var messages, args, output, message, result, validation, messages_1, args_1, output_1, output_2;
2417
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2418
- return __generator(this, function (_k) {
2419
- switch (_k.label) {
2422
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
2423
+ return __generator(this, function (_r) {
2424
+ switch (_r.label) {
2420
2425
  case 0:
2421
2426
  GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
2422
2427
  this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " getCompletion"));
2423
2428
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2424
2429
  case 1:
2425
- messages = _k.sent();
2430
+ messages = _r.sent();
2426
2431
  args = {
2427
2432
  clientId: this.params.clientId,
2428
2433
  agentName: this.params.agentName,
@@ -2434,22 +2439,29 @@ var ClientAgent = /** @class */ (function () {
2434
2439
  };
2435
2440
  return [4 /*yield*/, this.params.completion.getCompletion(args)];
2436
2441
  case 2:
2437
- output = _k.sent();
2442
+ output = _r.sent();
2443
+ if (GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "flush") {
2444
+ ((_b = this.params.completion.callbacks) === null || _b === void 0 ? void 0 : _b.onComplete) &&
2445
+ ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete(args, output));
2446
+ return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2447
+ }
2438
2448
  return [4 /*yield*/, this.params.map(output, this.params.clientId, this.params.agentName)];
2439
2449
  case 3:
2440
- message = _k.sent();
2450
+ message = _r.sent();
2441
2451
  return [4 /*yield*/, this.params.transform(message.content, this.params.clientId, this.params.agentName)];
2442
2452
  case 4:
2443
- result = _k.sent();
2444
- if ((_b = message.tool_calls) === null || _b === void 0 ? void 0 : _b.length) {
2453
+ result = _r.sent();
2454
+ if ((_d = message.tool_calls) === null || _d === void 0 ? void 0 : _d.length) {
2455
+ ((_e = this.params.completion.callbacks) === null || _e === void 0 ? void 0 : _e.onComplete) &&
2456
+ ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete(args, output));
2445
2457
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2446
2458
  }
2447
2459
  return [4 /*yield*/, this.params.validate(result)];
2448
2460
  case 5:
2449
- validation = _k.sent();
2461
+ validation = _r.sent();
2450
2462
  if (!!validation) return [3 /*break*/, 6];
2451
- ((_c = this.params.completion.callbacks) === null || _c === void 0 ? void 0 : _c.onComplete) &&
2452
- ((_d = this.params.completion.callbacks) === null || _d === void 0 ? void 0 : _d.onComplete(args, output));
2463
+ ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete) &&
2464
+ ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete(args, output));
2453
2465
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2454
2466
  case 6:
2455
2467
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "recomplete")) return [3 /*break*/, 10];
@@ -2461,36 +2473,38 @@ var ClientAgent = /** @class */ (function () {
2461
2473
  content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_RECOMPLETE_PROMPT,
2462
2474
  })];
2463
2475
  case 7:
2464
- _k.sent();
2476
+ _r.sent();
2465
2477
  return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt, this.params.system)];
2466
2478
  case 8:
2467
- messages_1 = _k.sent();
2479
+ messages_1 = _r.sent();
2468
2480
  args_1 = {
2469
2481
  clientId: this.params.clientId,
2470
2482
  agentName: this.params.agentName,
2471
2483
  messages: messages_1,
2472
2484
  mode: mode,
2473
- tools: (_e = this.params.tools) === null || _e === void 0 ? void 0 : _e.map(function (t) {
2485
+ tools: (_j = this.params.tools) === null || _j === void 0 ? void 0 : _j.map(function (t) {
2474
2486
  return omit(t, "toolName", "docNote", "call", "validate", "callbacks");
2475
2487
  }),
2476
2488
  };
2477
2489
  return [4 /*yield*/, this.params.completion.getCompletion(args_1)];
2478
2490
  case 9:
2479
- output_1 = _k.sent();
2491
+ output_1 = _r.sent();
2492
+ ((_k = this.params.completion.callbacks) === null || _k === void 0 ? void 0 : _k.onComplete) &&
2493
+ ((_l = this.params.completion.callbacks) === null || _l === void 0 ? void 0 : _l.onComplete(args_1, output_1));
2480
2494
  return [2 /*return*/, __assign(__assign({}, output_1), { content: output_1.content || "" })];
2481
2495
  case 10:
2482
2496
  if (!(GLOBAL_CONFIG.CC_RESQUE_STRATEGY === "custom")) return [3 /*break*/, 12];
2483
2497
  console.warn("agent-swarm model using custom resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " validation=").concat(validation));
2484
2498
  return [4 /*yield*/, GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_CUSTOM_FUNCTION(this.params.clientId, this.params.agentName)];
2485
2499
  case 11:
2486
- output_2 = _k.sent();
2487
- ((_f = this.params.completion.callbacks) === null || _f === void 0 ? void 0 : _f.onComplete) &&
2488
- ((_g = this.params.completion.callbacks) === null || _g === void 0 ? void 0 : _g.onComplete(args, output_2));
2500
+ output_2 = _r.sent();
2501
+ ((_m = this.params.completion.callbacks) === null || _m === void 0 ? void 0 : _m.onComplete) &&
2502
+ ((_o = this.params.completion.callbacks) === null || _o === void 0 ? void 0 : _o.onComplete(args, output_2));
2489
2503
  return [2 /*return*/, output_2];
2490
2504
  case 12:
2491
2505
  console.warn("agent-swarm model completion pending resurrect for agentName=".concat(this.params.agentName, " clientId=").concat(this.params.clientId, " strategy=").concat(GLOBAL_CONFIG.CC_RESQUE_STRATEGY, " validation=").concat(validation));
2492
- ((_h = this.params.completion.callbacks) === null || _h === void 0 ? void 0 : _h.onComplete) &&
2493
- ((_j = this.params.completion.callbacks) === null || _j === void 0 ? void 0 : _j.onComplete(args, output));
2506
+ ((_p = this.params.completion.callbacks) === null || _p === void 0 ? void 0 : _p.onComplete) &&
2507
+ ((_q = this.params.completion.callbacks) === null || _q === void 0 ? void 0 : _q.onComplete(args, output));
2494
2508
  return [2 /*return*/, __assign(__assign({}, output), { content: output.content || "" })];
2495
2509
  }
2496
2510
  });
@@ -15910,6 +15924,10 @@ var SchemaUtils = /** @class */ (function () {
15910
15924
  */
15911
15925
  var Schema = new SchemaUtils();
15912
15926
 
15927
+ /**
15928
+ * @see https://github.com/ollama/ollama/blob/86a622cbdc69e9fd501764ff7565e977fc98f00a/server/model.go#L158
15929
+ */
15930
+ var TOOL_PROTOCOL_PROMPT = str.newline("For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:", "<tool_call>", "{\"name\": <function-name>, \"arguments\": <args-json-object>}", "</tool_call>");
15913
15931
  var AdapterUtils = /** @class */ (function () {
15914
15932
  function AdapterUtils() {
15915
15933
  var _this = this;
@@ -15939,7 +15957,7 @@ var AdapterUtils = /** @class */ (function () {
15939
15957
  return __generator(this, function (_e) {
15940
15958
  switch (_e.label) {
15941
15959
  case 0:
15942
- LoggerAdapter.logClient(clientId, "AdapterUtils history", JSON.stringify(rawMessages));
15960
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromOpenAI completion", JSON.stringify(rawMessages));
15943
15961
  messages = rawMessages.map(function (_a) {
15944
15962
  var role = _a.role, tool_call_id = _a.tool_call_id, tool_calls = _a.tool_calls, content = _a.content;
15945
15963
  return ({
@@ -15959,6 +15977,8 @@ var AdapterUtils = /** @class */ (function () {
15959
15977
  model: model,
15960
15978
  messages: messages,
15961
15979
  tools: tools,
15980
+ temperature: 0,
15981
+ seed: 0,
15962
15982
  response_format: response_format,
15963
15983
  })];
15964
15984
  case 1:
@@ -15980,6 +16000,138 @@ var AdapterUtils = /** @class */ (function () {
15980
16000
  });
15981
16001
  }); };
15982
16002
  };
16003
+ /**
16004
+ * Creates a function to interact with LMStudio's chat completions.
16005
+ *
16006
+ * @param {any} openai - The LMStudio instance.
16007
+ * @param {string} [model="saiga_yandexgpt_8b_gguf"] - The model to use for completions.
16008
+ * @param {Object} [response_format] - The format of the response.
16009
+ * @returns {Function} - A function that takes completion arguments and returns a response from LMStudio.
16010
+ */
16011
+ this.fromLMStudio = function (openai, model, response_format) {
16012
+ if (model === void 0) { model = "saiga_yandexgpt_8b_gguf"; }
16013
+ /**
16014
+ * Handles the completion request to LMStudio.
16015
+ *
16016
+ * @param {ICompletionArgs} args - The arguments for the completion request.
16017
+ * @param {string} args.agentName - The name of the agent.
16018
+ * @param {Array} args.messages - The messages to send to LMStudio.
16019
+ * @param {string} args.mode - The mode of the completion.
16020
+ * @param {Array} args.tools - The tools to use for the completion.
16021
+ * @param {string} args.clientId - The client ID.
16022
+ * @returns {Promise<Object>} - The response from LMStudio.
16023
+ */
16024
+ return function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
16025
+ var messages, _c, _d, content, role, tool_calls;
16026
+ var agentName = _b.agentName, rawMessages = _b.messages, mode = _b.mode, tools = _b.tools, clientId = _b.clientId;
16027
+ return __generator(this, function (_e) {
16028
+ switch (_e.label) {
16029
+ case 0:
16030
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromLMStudio completion", JSON.stringify(rawMessages));
16031
+ messages = rawMessages.map(function (_a) {
16032
+ var role = _a.role, tool_call_id = _a.tool_call_id, tool_calls = _a.tool_calls, content = _a.content;
16033
+ return ({
16034
+ role: role,
16035
+ tool_call_id: tool_call_id,
16036
+ content: content,
16037
+ tool_calls: tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.map(function (_a) {
16038
+ var f = _a.function, rest = __rest(_a, ["function"]);
16039
+ return (__assign(__assign({}, rest), { function: {
16040
+ name: f.name,
16041
+ arguments: JSON.stringify(f.arguments),
16042
+ } }));
16043
+ }),
16044
+ });
16045
+ });
16046
+ return [4 /*yield*/, openai.chat.completions.create({
16047
+ model: model,
16048
+ messages: messages,
16049
+ tools: tools,
16050
+ temperature: 0,
16051
+ seed: 0,
16052
+ response_format: response_format,
16053
+ })];
16054
+ case 1:
16055
+ _c = __read.apply(void 0, [(_e.sent()).choices, 1]), _d = _c[0].message, content = _d.content, role = _d.role, tool_calls = _d.tool_calls;
16056
+ return [2 /*return*/, {
16057
+ content: content,
16058
+ mode: mode,
16059
+ agentName: agentName,
16060
+ role: role,
16061
+ tool_calls: tool_calls === null || tool_calls === void 0 ? void 0 : tool_calls.map(function (_a) {
16062
+ var f = _a.function, rest = __rest(_a, ["function"]);
16063
+ return (__assign(__assign({}, rest), { function: {
16064
+ name: f.name,
16065
+ arguments: JSON.parse(f.arguments),
16066
+ } }));
16067
+ }),
16068
+ }];
16069
+ }
16070
+ });
16071
+ }); };
16072
+ };
16073
+ /**
16074
+ * Creates a function to interact with Ollama's chat completions.
16075
+ *
16076
+ * @param {any} ollama - The Ollama instance.
16077
+ * @param {string} [model="nemotron-mini:4b"] - The model to use for completions.
16078
+ * @param {string} [tool_call_protocol=TOOL_PROTOCOL_PROMPT] - The protocol for tool calls.
16079
+ * @returns {Function} - A function that takes completion arguments and returns a response from Ollama.
16080
+ */
16081
+ this.fromOllama = function (ollama, model, tool_call_protocol) {
16082
+ if (model === void 0) { model = "nemotron-mini:4b"; }
16083
+ if (tool_call_protocol === void 0) { tool_call_protocol = TOOL_PROTOCOL_PROMPT; }
16084
+ /**
16085
+ * Handles the completion request to Ollama.
16086
+ *
16087
+ * @param {ICompletionArgs} args - The arguments for the completion request.
16088
+ * @param {string} args.agentName - The name of the agent.
16089
+ * @param {Array} args.messages - The messages to send to Ollama.
16090
+ * @param {string} args.mode - The mode of the completion.
16091
+ * @param {Array} args.tools - The tools to use for the completion.
16092
+ * @param {string} args.clientId - The client ID.
16093
+ * @returns {Promise<Object>} - The response from Ollama.
16094
+ */
16095
+ return function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
16096
+ var messages, response;
16097
+ var _c;
16098
+ var agentName = _b.agentName, rawMessages = _b.messages, mode = _b.mode, tools = _b.tools, clientId = _b.clientId;
16099
+ return __generator(this, function (_d) {
16100
+ LoggerAdapter.logClient(clientId, "AdapterUtils fromOllama completion", JSON.stringify(rawMessages));
16101
+ messages = __spreadArray([], __read(rawMessages), false);
16102
+ messages.unshift({
16103
+ agentName: agentName,
16104
+ mode: "tool",
16105
+ role: "system",
16106
+ content: tool_call_protocol,
16107
+ });
16108
+ response = ollama.chat({
16109
+ model: model,
16110
+ keep_alive: "24h",
16111
+ options: {
16112
+ temperature: 0,
16113
+ seed: 0,
16114
+ },
16115
+ messages: messages.map(function (message) {
16116
+ var _a;
16117
+ return ({
16118
+ content: message.content,
16119
+ role: message.role,
16120
+ tool_calls: (_a = message.tool_calls) === null || _a === void 0 ? void 0 : _a.map(function (call) { return ({
16121
+ function: call.function,
16122
+ }); }),
16123
+ });
16124
+ }),
16125
+ tools: tools,
16126
+ });
16127
+ return [2 /*return*/, __assign(__assign({}, response.message), { tool_calls: (_c = response.message.tool_calls) === null || _c === void 0 ? void 0 : _c.map(function (call) { return ({
16128
+ function: call.function,
16129
+ type: "function",
16130
+ id: randomString(),
16131
+ }); }), mode: mode, agentName: agentName, role: response.message.role })];
16132
+ });
16133
+ }); };
16134
+ };
15983
16135
  }
15984
16136
  return AdapterUtils;
15985
16137
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-swarm-kit",
3
- "version": "1.0.154",
3
+ "version": "1.0.156",
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,7 +1,6 @@
1
1
  import * as di_scoped from 'di-scoped';
2
2
  import * as functools_kit from 'functools-kit';
3
3
  import { SortedArray, Subject } from 'functools-kit';
4
- import { ICompletionArgs as ICompletionArgs$1 } from 'src/interfaces/Completion.interface';
5
4
 
6
5
  /**
7
6
  * Interface representing the context.
@@ -5730,13 +5729,39 @@ declare class AdapterUtils {
5730
5729
  */
5731
5730
  fromOpenAI: (openai: any, model?: string, response_format?: {
5732
5731
  type: string;
5733
- }) => ({ agentName, messages: rawMessages, mode, tools, clientId, }: ICompletionArgs$1) => Promise<{
5732
+ }) => ({ agentName, messages: rawMessages, mode, tools, clientId, }: ICompletionArgs) => Promise<{
5734
5733
  content: any;
5735
5734
  mode: ExecutionMode;
5736
5735
  agentName: string;
5737
5736
  role: any;
5738
5737
  tool_calls: any;
5739
5738
  }>;
5739
+ /**
5740
+ * Creates a function to interact with LMStudio's chat completions.
5741
+ *
5742
+ * @param {any} openai - The LMStudio instance.
5743
+ * @param {string} [model="saiga_yandexgpt_8b_gguf"] - The model to use for completions.
5744
+ * @param {Object} [response_format] - The format of the response.
5745
+ * @returns {Function} - A function that takes completion arguments and returns a response from LMStudio.
5746
+ */
5747
+ fromLMStudio: (openai: any, model?: string, response_format?: {
5748
+ type: string;
5749
+ }) => ({ agentName, messages: rawMessages, mode, tools, clientId, }: ICompletionArgs) => Promise<{
5750
+ content: any;
5751
+ mode: ExecutionMode;
5752
+ agentName: string;
5753
+ role: any;
5754
+ tool_calls: any;
5755
+ }>;
5756
+ /**
5757
+ * Creates a function to interact with Ollama's chat completions.
5758
+ *
5759
+ * @param {any} ollama - The Ollama instance.
5760
+ * @param {string} [model="nemotron-mini:4b"] - The model to use for completions.
5761
+ * @param {string} [tool_call_protocol=TOOL_PROTOCOL_PROMPT] - The protocol for tool calls.
5762
+ * @returns {Function} - A function that takes completion arguments and returns a response from Ollama.
5763
+ */
5764
+ fromOllama: (ollama: any, model?: string, tool_call_protocol?: string) => ({ agentName, messages: rawMessages, mode, tools, clientId, }: ICompletionArgs) => Promise<any>;
5740
5765
  }
5741
5766
  /**
5742
5767
  * An instance of AdapterUtils.