agent-swarm-kit 0.0.1 → 1.0.0
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/LICENSE +1 -1
- package/README.md +159 -1
- package/build/.gitkeep +0 -0
- package/build/index.cjs +2341 -0
- package/build/index.mjs +2323 -0
- package/package.json +80 -12
- package/types.d.ts +470 -0
- package/index.mjs +0 -1
package/build/index.mjs
ADDED
|
@@ -0,0 +1,2323 @@
|
|
|
1
|
+
import { scoped } from 'di-scoped';
|
|
2
|
+
import { ToolRegistry, Subject, queued, not, trycatch, memoize, PubsubArrayAdapter, ttl, singleshot } from 'functools-kit';
|
|
3
|
+
import { createActivator } from 'di-kit';
|
|
4
|
+
import { omit } from 'lodash-es';
|
|
5
|
+
import xml2js from 'xml2js';
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var __assign = function() {
|
|
25
|
+
__assign = Object.assign || function __assign(t) {
|
|
26
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
27
|
+
s = arguments[i];
|
|
28
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
};
|
|
32
|
+
return __assign.apply(this, arguments);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function __generator(thisArg, body) {
|
|
46
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
47
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
48
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
49
|
+
function step(op) {
|
|
50
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
51
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
52
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
53
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
54
|
+
switch (op[0]) {
|
|
55
|
+
case 0: case 1: t = op; break;
|
|
56
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
57
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
58
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
59
|
+
default:
|
|
60
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
61
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
62
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
63
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
64
|
+
if (t[2]) _.ops.pop();
|
|
65
|
+
_.trys.pop(); continue;
|
|
66
|
+
}
|
|
67
|
+
op = body.call(thisArg, _);
|
|
68
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
69
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function __values(o) {
|
|
74
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
75
|
+
if (m) return m.call(o);
|
|
76
|
+
if (o && typeof o.length === "number") return {
|
|
77
|
+
next: function () {
|
|
78
|
+
if (o && i >= o.length) o = void 0;
|
|
79
|
+
return { value: o && o[i++], done: !o };
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function __read(o, n) {
|
|
86
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
87
|
+
if (!m) return o;
|
|
88
|
+
var i = m.call(o), r, ar = [], e;
|
|
89
|
+
try {
|
|
90
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
91
|
+
}
|
|
92
|
+
catch (error) { e = { error: error }; }
|
|
93
|
+
finally {
|
|
94
|
+
try {
|
|
95
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
96
|
+
}
|
|
97
|
+
finally { if (e) throw e.error; }
|
|
98
|
+
}
|
|
99
|
+
return ar;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function __spreadArray(to, from, pack) {
|
|
103
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
104
|
+
if (ar || !(i in from)) {
|
|
105
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
106
|
+
ar[i] = from[i];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function __asyncValues(o) {
|
|
113
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
114
|
+
var m = o[Symbol.asyncIterator], i;
|
|
115
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
116
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
117
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
121
|
+
var e = new Error(message);
|
|
122
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
var ContextService = scoped(/** @class */ (function () {
|
|
126
|
+
function class_1(context) {
|
|
127
|
+
this.context = context;
|
|
128
|
+
}
|
|
129
|
+
return class_1;
|
|
130
|
+
}()));
|
|
131
|
+
|
|
132
|
+
var LoggerService = /** @class */ (function () {
|
|
133
|
+
function LoggerService() {
|
|
134
|
+
var _this = this;
|
|
135
|
+
this._logger = {
|
|
136
|
+
log: function () {
|
|
137
|
+
},
|
|
138
|
+
debug: function () {
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
this.log = function () {
|
|
142
|
+
var _a;
|
|
143
|
+
var args = [];
|
|
144
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
145
|
+
args[_i] = arguments[_i];
|
|
146
|
+
}
|
|
147
|
+
(_a = _this._logger).log.apply(_a, __spreadArray([], __read(args), false));
|
|
148
|
+
};
|
|
149
|
+
this.debug = function () {
|
|
150
|
+
var _a;
|
|
151
|
+
var args = [];
|
|
152
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
153
|
+
args[_i] = arguments[_i];
|
|
154
|
+
}
|
|
155
|
+
(_a = _this._logger).debug.apply(_a, __spreadArray([], __read(args), false));
|
|
156
|
+
};
|
|
157
|
+
this.setLogger = function (logger) {
|
|
158
|
+
_this._logger = logger;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return LoggerService;
|
|
162
|
+
}());
|
|
163
|
+
|
|
164
|
+
var _a;
|
|
165
|
+
var init = (_a = createActivator("agent-swarm"), _a.init), inject = _a.inject, provide = _a.provide;
|
|
166
|
+
|
|
167
|
+
var baseServices$1 = {
|
|
168
|
+
loggerService: Symbol('loggerService'),
|
|
169
|
+
contextService: Symbol('contextService'),
|
|
170
|
+
};
|
|
171
|
+
var connectionServices$1 = {
|
|
172
|
+
agentConnectionService: Symbol('agentConnectionService'),
|
|
173
|
+
historyConnectionService: Symbol('historyConnectionService'),
|
|
174
|
+
swarmConnectionService: Symbol('swarmConnectionService'),
|
|
175
|
+
sessionConnectionService: Symbol('sessionConnectionService'),
|
|
176
|
+
};
|
|
177
|
+
var specServices$1 = {
|
|
178
|
+
completionSchemaService: Symbol('completionSchemaService'),
|
|
179
|
+
agentSchemaService: Symbol('agentSchemaService'),
|
|
180
|
+
swarmSchemaService: Symbol('swarmSchemaService'),
|
|
181
|
+
toolSchemaService: Symbol('toolSchemaService'),
|
|
182
|
+
};
|
|
183
|
+
var publicServices$1 = {
|
|
184
|
+
agentPublicService: Symbol('agentPublicService'),
|
|
185
|
+
historyPublicService: Symbol('historyPublicService'),
|
|
186
|
+
sessionPublicService: Symbol('sessionPublicService'),
|
|
187
|
+
swarmPublicService: Symbol('swarmPublicService'),
|
|
188
|
+
};
|
|
189
|
+
var validationServices$1 = {
|
|
190
|
+
agentValidationService: Symbol('agentValidationService'),
|
|
191
|
+
toolValidationService: Symbol('toolValidationService'),
|
|
192
|
+
sessionValidationService: Symbol('sessionValidationService'),
|
|
193
|
+
swarmValidationService: Symbol('swarmValidationService'),
|
|
194
|
+
completionValidationService: Symbol('completionValidationService'),
|
|
195
|
+
};
|
|
196
|
+
var TYPES = __assign(__assign(__assign(__assign(__assign({}, baseServices$1), specServices$1), connectionServices$1), publicServices$1), validationServices$1);
|
|
197
|
+
|
|
198
|
+
var AgentSchemaService = /** @class */ (function () {
|
|
199
|
+
function AgentSchemaService() {
|
|
200
|
+
var _this = this;
|
|
201
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
202
|
+
this.registry = new ToolRegistry("agentSchemaService");
|
|
203
|
+
this.register = function (key, value) {
|
|
204
|
+
_this.loggerService.log("agentSchemaService register", { key: key });
|
|
205
|
+
_this.registry = _this.registry.register(key, value);
|
|
206
|
+
};
|
|
207
|
+
this.get = function (key) {
|
|
208
|
+
_this.loggerService.log("agentSchemaService get", { key: key });
|
|
209
|
+
return _this.registry.get(key);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
return AgentSchemaService;
|
|
213
|
+
}());
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @description `ask for agent function` in `llama3.1:8b` to troubleshoot (need CC_OLLAMA_EMIT_TOOL_PROTOCOL to be turned off)
|
|
217
|
+
*/
|
|
218
|
+
var CC_TOOL_CALL_EXCEPTION_PROMPT = "Start the conversation";
|
|
219
|
+
/**
|
|
220
|
+
* @description When the model output is empty just say hello to the customer
|
|
221
|
+
*/
|
|
222
|
+
var CC_EMPTY_OUTPUT_PLACEHOLDERS = [
|
|
223
|
+
"Sorry, I missed that. Could you say it again?",
|
|
224
|
+
"I couldn't catch that. Would you mind repeating?",
|
|
225
|
+
"I didn’t quite hear you. Can you repeat that, please?",
|
|
226
|
+
"Pardon me, I didn’t hear that clearly. Could you repeat it?",
|
|
227
|
+
"Sorry, I didn’t catch what you said. Could you say it again?",
|
|
228
|
+
"Could you repeat that? I didn’t hear it clearly.",
|
|
229
|
+
"I missed that. Can you say it one more time?",
|
|
230
|
+
"Sorry, I didn’t get that. Could you repeat it, please?",
|
|
231
|
+
"I didn’t hear you properly. Can you say that again?",
|
|
232
|
+
"Could you please repeat that? I didn’t catch it.",
|
|
233
|
+
];
|
|
234
|
+
var CC_KEEP_MESSAGES = 5;
|
|
235
|
+
var CC_ANSWER_TIMEOUT_SECONDS = 120000;
|
|
236
|
+
var GLOBAL_CONFIG = {
|
|
237
|
+
CC_TOOL_CALL_EXCEPTION_PROMPT: CC_TOOL_CALL_EXCEPTION_PROMPT,
|
|
238
|
+
CC_EMPTY_OUTPUT_PLACEHOLDERS: CC_EMPTY_OUTPUT_PLACEHOLDERS,
|
|
239
|
+
CC_KEEP_MESSAGES: CC_KEEP_MESSAGES,
|
|
240
|
+
CC_ANSWER_TIMEOUT_SECONDS: CC_ANSWER_TIMEOUT_SECONDS,
|
|
241
|
+
};
|
|
242
|
+
var setConfig = function (config) {
|
|
243
|
+
Object.assign(GLOBAL_CONFIG, config);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
var getPlaceholder = function () {
|
|
247
|
+
return GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS[Math.floor(Math.random() * GLOBAL_CONFIG.CC_EMPTY_OUTPUT_PLACEHOLDERS.length)];
|
|
248
|
+
};
|
|
249
|
+
var ClientAgent = /** @class */ (function () {
|
|
250
|
+
function ClientAgent(params) {
|
|
251
|
+
var _this = this;
|
|
252
|
+
this.params = params;
|
|
253
|
+
this._toolCommitSubject = new Subject();
|
|
254
|
+
this._outputSubject = new Subject();
|
|
255
|
+
this._emitOuput = function (result) { return __awaiter(_this, void 0, void 0, function () {
|
|
256
|
+
var validation, result_1;
|
|
257
|
+
return __generator(this, function (_a) {
|
|
258
|
+
switch (_a.label) {
|
|
259
|
+
case 0:
|
|
260
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _emitOuput"));
|
|
261
|
+
validation = null;
|
|
262
|
+
return [4 /*yield*/, this.params.validate(result)];
|
|
263
|
+
case 1:
|
|
264
|
+
if (!(validation = _a.sent())) return [3 /*break*/, 5];
|
|
265
|
+
return [4 /*yield*/, this._resurrectModel(validation)];
|
|
266
|
+
case 2:
|
|
267
|
+
result_1 = _a.sent();
|
|
268
|
+
return [4 /*yield*/, this.params.validate(result_1)];
|
|
269
|
+
case 3:
|
|
270
|
+
if (validation = _a.sent()) {
|
|
271
|
+
throw new Error("agent-swarm-kit ClientAgent agentName=".concat(this.params.agentName, " model ressurect failed: ").concat(validation));
|
|
272
|
+
}
|
|
273
|
+
return [4 /*yield*/, this._outputSubject.next(result_1)];
|
|
274
|
+
case 4:
|
|
275
|
+
_a.sent();
|
|
276
|
+
return [2 /*return*/];
|
|
277
|
+
case 5: return [4 /*yield*/, this._outputSubject.next(result)];
|
|
278
|
+
case 6:
|
|
279
|
+
_a.sent();
|
|
280
|
+
return [2 /*return*/];
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}); };
|
|
284
|
+
this._resurrectModel = function (reason) { return __awaiter(_this, void 0, void 0, function () {
|
|
285
|
+
var message, result, validation, content;
|
|
286
|
+
return __generator(this, function (_a) {
|
|
287
|
+
switch (_a.label) {
|
|
288
|
+
case 0:
|
|
289
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel"));
|
|
290
|
+
return [4 /*yield*/, this.params.history.push({
|
|
291
|
+
role: "resque",
|
|
292
|
+
agentName: this.params.agentName,
|
|
293
|
+
content: reason || "Unknown error",
|
|
294
|
+
})];
|
|
295
|
+
case 1:
|
|
296
|
+
_a.sent();
|
|
297
|
+
return [4 /*yield*/, this.params.history.push({
|
|
298
|
+
role: "user",
|
|
299
|
+
agentName: this.params.agentName,
|
|
300
|
+
content: GLOBAL_CONFIG.CC_TOOL_CALL_EXCEPTION_PROMPT,
|
|
301
|
+
})];
|
|
302
|
+
case 2:
|
|
303
|
+
_a.sent();
|
|
304
|
+
return [4 /*yield*/, this.getCompletion()];
|
|
305
|
+
case 3:
|
|
306
|
+
message = _a.sent();
|
|
307
|
+
result = message.content;
|
|
308
|
+
validation = null;
|
|
309
|
+
return [4 /*yield*/, this.params.validate(result)];
|
|
310
|
+
case 4:
|
|
311
|
+
if (!(validation = _a.sent())) return [3 /*break*/, 6];
|
|
312
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " _resurrectModel validation error: ").concat(validation));
|
|
313
|
+
content = getPlaceholder();
|
|
314
|
+
return [4 /*yield*/, this.params.history.push({
|
|
315
|
+
agentName: this.params.agentName,
|
|
316
|
+
role: "assistant",
|
|
317
|
+
content: content,
|
|
318
|
+
})];
|
|
319
|
+
case 5:
|
|
320
|
+
_a.sent();
|
|
321
|
+
return [2 /*return*/, content];
|
|
322
|
+
case 6: return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
|
|
323
|
+
case 7:
|
|
324
|
+
_a.sent();
|
|
325
|
+
return [2 /*return*/, result];
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}); };
|
|
329
|
+
this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
330
|
+
return __generator(this, function (_a) {
|
|
331
|
+
switch (_a.label) {
|
|
332
|
+
case 0:
|
|
333
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " waitForOutput"));
|
|
334
|
+
return [4 /*yield*/, this._outputSubject.toPromise()];
|
|
335
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}); };
|
|
339
|
+
this.getCompletion = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
340
|
+
var messages;
|
|
341
|
+
var _a;
|
|
342
|
+
return __generator(this, function (_b) {
|
|
343
|
+
switch (_b.label) {
|
|
344
|
+
case 0:
|
|
345
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " getCompletion"));
|
|
346
|
+
return [4 /*yield*/, this.params.history.toArrayForAgent(this.params.prompt)];
|
|
347
|
+
case 1:
|
|
348
|
+
messages = _b.sent();
|
|
349
|
+
return [4 /*yield*/, this.params.completion.getCompletion({
|
|
350
|
+
clientId: this.params.clientId,
|
|
351
|
+
agentName: this.params.agentName,
|
|
352
|
+
messages: messages,
|
|
353
|
+
tools: (_a = this.params.tools) === null || _a === void 0 ? void 0 : _a.map(function (t) { return omit(t, "toolName", "call", "validate"); })
|
|
354
|
+
})];
|
|
355
|
+
case 2: return [2 /*return*/, _b.sent()];
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}); };
|
|
359
|
+
this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
360
|
+
return __generator(this, function (_a) {
|
|
361
|
+
switch (_a.label) {
|
|
362
|
+
case 0:
|
|
363
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitSystemMessage"));
|
|
364
|
+
return [4 /*yield*/, this.params.history.push({
|
|
365
|
+
role: "system",
|
|
366
|
+
agentName: this.params.agentName,
|
|
367
|
+
content: message.trim(),
|
|
368
|
+
})];
|
|
369
|
+
case 1:
|
|
370
|
+
_a.sent();
|
|
371
|
+
return [2 /*return*/];
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
}); };
|
|
375
|
+
this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
376
|
+
return __generator(this, function (_a) {
|
|
377
|
+
switch (_a.label) {
|
|
378
|
+
case 0:
|
|
379
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " commitToolOutput content=").concat(content));
|
|
380
|
+
return [4 /*yield*/, this.params.history.push({
|
|
381
|
+
role: "tool",
|
|
382
|
+
agentName: this.params.agentName,
|
|
383
|
+
content: content,
|
|
384
|
+
})];
|
|
385
|
+
case 1:
|
|
386
|
+
_a.sent();
|
|
387
|
+
return [4 /*yield*/, this._toolCommitSubject.next()];
|
|
388
|
+
case 2:
|
|
389
|
+
_a.sent();
|
|
390
|
+
return [2 /*return*/];
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}); };
|
|
394
|
+
this.execute = queued(function (incoming) { return __awaiter(_this, void 0, void 0, function () {
|
|
395
|
+
var message, _loop_1, this_1, _a, _b, tool, state_1, e_1_1, result, validation, result1;
|
|
396
|
+
var e_1, _c;
|
|
397
|
+
var _d;
|
|
398
|
+
return __generator(this, function (_e) {
|
|
399
|
+
switch (_e.label) {
|
|
400
|
+
case 0:
|
|
401
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute begin"), { incoming: incoming });
|
|
402
|
+
return [4 /*yield*/, this.params.history.push({
|
|
403
|
+
role: "user",
|
|
404
|
+
agentName: this.params.agentName,
|
|
405
|
+
content: incoming.trim(),
|
|
406
|
+
})];
|
|
407
|
+
case 1:
|
|
408
|
+
_e.sent();
|
|
409
|
+
return [4 /*yield*/, this.getCompletion()];
|
|
410
|
+
case 2:
|
|
411
|
+
message = _e.sent();
|
|
412
|
+
if (!message.tool_calls) return [3 /*break*/, 11];
|
|
413
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " tool call begin"));
|
|
414
|
+
_loop_1 = function (tool) {
|
|
415
|
+
var targetFn, result_2, result_3;
|
|
416
|
+
return __generator(this, function (_f) {
|
|
417
|
+
switch (_f.label) {
|
|
418
|
+
case 0:
|
|
419
|
+
targetFn = (_d = this_1.params.tools) === null || _d === void 0 ? void 0 : _d.find(function (t) { return t.function.name === tool.function.name; });
|
|
420
|
+
return [4 /*yield*/, this_1.params.history.push(__assign(__assign({}, message), { agentName: this_1.params.agentName }))];
|
|
421
|
+
case 1:
|
|
422
|
+
_f.sent();
|
|
423
|
+
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"));
|
|
425
|
+
return [4 /*yield*/, this_1._resurrectModel("No target function for ".concat(tool.function.name))];
|
|
426
|
+
case 2:
|
|
427
|
+
result_2 = _f.sent();
|
|
428
|
+
this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_2));
|
|
429
|
+
return [4 /*yield*/, this_1._emitOuput(result_2)];
|
|
430
|
+
case 3:
|
|
431
|
+
_f.sent();
|
|
432
|
+
return [2 /*return*/, { value: void 0 }];
|
|
433
|
+
case 4: return [4 /*yield*/, not(targetFn.validate(this_1.params.clientId, this_1.params.agentName, tool.function.arguments))];
|
|
434
|
+
case 5:
|
|
435
|
+
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"));
|
|
437
|
+
return [4 /*yield*/, this_1._resurrectModel("Function validation failed: name=".concat(tool.function.name, " arguments=").concat(JSON.stringify(tool.function.arguments)))];
|
|
438
|
+
case 6:
|
|
439
|
+
result_3 = _f.sent();
|
|
440
|
+
this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " execute end result=").concat(result_3));
|
|
441
|
+
return [4 /*yield*/, this_1._emitOuput(result_3)];
|
|
442
|
+
case 7:
|
|
443
|
+
_f.sent();
|
|
444
|
+
return [2 /*return*/, { value: void 0 }];
|
|
445
|
+
case 8:
|
|
446
|
+
/**
|
|
447
|
+
* @description Do not await to avoid deadlock! The tool can send the message to other agents by emulating user messages
|
|
448
|
+
*/
|
|
449
|
+
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"));
|
|
451
|
+
return [4 /*yield*/, Promise.race([
|
|
452
|
+
this_1._toolCommitSubject.toPromise(),
|
|
453
|
+
this_1._outputSubject.toPromise(),
|
|
454
|
+
])];
|
|
455
|
+
case 9:
|
|
456
|
+
_f.sent();
|
|
457
|
+
this_1.params.logger.debug("ClientAgent agentName=".concat(this_1.params.agentName, " functionName=").concat(tool.function.name, " tool call end"));
|
|
458
|
+
return [2 /*return*/];
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
};
|
|
462
|
+
this_1 = this;
|
|
463
|
+
_e.label = 3;
|
|
464
|
+
case 3:
|
|
465
|
+
_e.trys.push([3, 8, 9, 10]);
|
|
466
|
+
_a = __values(message.tool_calls), _b = _a.next();
|
|
467
|
+
_e.label = 4;
|
|
468
|
+
case 4:
|
|
469
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
470
|
+
tool = _b.value;
|
|
471
|
+
return [5 /*yield**/, _loop_1(tool)];
|
|
472
|
+
case 5:
|
|
473
|
+
state_1 = _e.sent();
|
|
474
|
+
if (typeof state_1 === "object")
|
|
475
|
+
return [2 /*return*/, state_1.value];
|
|
476
|
+
_e.label = 6;
|
|
477
|
+
case 6:
|
|
478
|
+
_b = _a.next();
|
|
479
|
+
return [3 /*break*/, 4];
|
|
480
|
+
case 7: return [3 /*break*/, 10];
|
|
481
|
+
case 8:
|
|
482
|
+
e_1_1 = _e.sent();
|
|
483
|
+
e_1 = { error: e_1_1 };
|
|
484
|
+
return [3 /*break*/, 10];
|
|
485
|
+
case 9:
|
|
486
|
+
try {
|
|
487
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
488
|
+
}
|
|
489
|
+
finally { if (e_1) throw e_1.error; }
|
|
490
|
+
return [7 /*endfinally*/];
|
|
491
|
+
case 10: return [2 /*return*/];
|
|
492
|
+
case 11:
|
|
493
|
+
if (!message.tool_calls) {
|
|
494
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute no tool calls detected"));
|
|
495
|
+
}
|
|
496
|
+
result = message.content;
|
|
497
|
+
return [4 /*yield*/, this.params.history.push(__assign(__assign({}, message), { agentName: this.params.agentName }))];
|
|
498
|
+
case 12:
|
|
499
|
+
_e.sent();
|
|
500
|
+
validation = null;
|
|
501
|
+
return [4 /*yield*/, this.params.validate(result)];
|
|
502
|
+
case 13:
|
|
503
|
+
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 });
|
|
505
|
+
return [4 /*yield*/, this._resurrectModel("Invalid model output: ".concat(result))];
|
|
506
|
+
case 14:
|
|
507
|
+
result1 = _e.sent();
|
|
508
|
+
return [4 /*yield*/, this._emitOuput(result1)];
|
|
509
|
+
case 15:
|
|
510
|
+
_e.sent();
|
|
511
|
+
return [2 /*return*/];
|
|
512
|
+
case 16:
|
|
513
|
+
this.params.logger.debug("ClientAgent agentName=".concat(this.params.agentName, " execute end result=").concat(result));
|
|
514
|
+
return [4 /*yield*/, this._emitOuput(result)];
|
|
515
|
+
case 17:
|
|
516
|
+
_e.sent();
|
|
517
|
+
return [2 /*return*/];
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}); });
|
|
521
|
+
}
|
|
522
|
+
return ClientAgent;
|
|
523
|
+
}());
|
|
524
|
+
|
|
525
|
+
var validateNoEmptyResult = function (output) { return __awaiter(void 0, void 0, void 0, function () {
|
|
526
|
+
return __generator(this, function (_a) {
|
|
527
|
+
if (!output.trim()) {
|
|
528
|
+
return [2 /*return*/, "Empty output"];
|
|
529
|
+
}
|
|
530
|
+
return [2 /*return*/, null];
|
|
531
|
+
});
|
|
532
|
+
}); };
|
|
533
|
+
|
|
534
|
+
var toolParser = new xml2js.Parser();
|
|
535
|
+
var TOOL_CALL_ENTRIES = ["tool_call", "toolcall", "tool"];
|
|
536
|
+
var DISALLOWED_SYMBOLS = [
|
|
537
|
+
"{",
|
|
538
|
+
"}",
|
|
539
|
+
];
|
|
540
|
+
/**
|
|
541
|
+
* @description Validation for not parsed XML toolcall
|
|
542
|
+
* @see https://github.com/ollama/ollama/issues/8287
|
|
543
|
+
*/
|
|
544
|
+
var validateNoToolCall = trycatch(function (output) { return __awaiter(void 0, void 0, void 0, function () {
|
|
545
|
+
var DISALLOWED_SYMBOLS_1, DISALLOWED_SYMBOLS_1_1, symbol, result, TOOL_CALL_ENTRIES_1, TOOL_CALL_ENTRIES_1_1, tag;
|
|
546
|
+
var e_1, _a, e_2, _b;
|
|
547
|
+
return __generator(this, function (_c) {
|
|
548
|
+
switch (_c.label) {
|
|
549
|
+
case 0:
|
|
550
|
+
try {
|
|
551
|
+
for (DISALLOWED_SYMBOLS_1 = __values(DISALLOWED_SYMBOLS), DISALLOWED_SYMBOLS_1_1 = DISALLOWED_SYMBOLS_1.next(); !DISALLOWED_SYMBOLS_1_1.done; DISALLOWED_SYMBOLS_1_1 = DISALLOWED_SYMBOLS_1.next()) {
|
|
552
|
+
symbol = DISALLOWED_SYMBOLS_1_1.value;
|
|
553
|
+
if (output.includes(symbol)) {
|
|
554
|
+
return [2 /*return*/, "Tool call in text output"];
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
559
|
+
finally {
|
|
560
|
+
try {
|
|
561
|
+
if (DISALLOWED_SYMBOLS_1_1 && !DISALLOWED_SYMBOLS_1_1.done && (_a = DISALLOWED_SYMBOLS_1.return)) _a.call(DISALLOWED_SYMBOLS_1);
|
|
562
|
+
}
|
|
563
|
+
finally { if (e_1) throw e_1.error; }
|
|
564
|
+
}
|
|
565
|
+
return [4 /*yield*/, toolParser.parseStringPromise(output)];
|
|
566
|
+
case 1:
|
|
567
|
+
result = _c.sent();
|
|
568
|
+
try {
|
|
569
|
+
for (TOOL_CALL_ENTRIES_1 = __values(TOOL_CALL_ENTRIES), TOOL_CALL_ENTRIES_1_1 = TOOL_CALL_ENTRIES_1.next(); !TOOL_CALL_ENTRIES_1_1.done; TOOL_CALL_ENTRIES_1_1 = TOOL_CALL_ENTRIES_1.next()) {
|
|
570
|
+
tag = TOOL_CALL_ENTRIES_1_1.value;
|
|
571
|
+
if (result[tag]) {
|
|
572
|
+
return [2 /*return*/, "Tool call in text output"];
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
577
|
+
finally {
|
|
578
|
+
try {
|
|
579
|
+
if (TOOL_CALL_ENTRIES_1_1 && !TOOL_CALL_ENTRIES_1_1.done && (_b = TOOL_CALL_ENTRIES_1.return)) _b.call(TOOL_CALL_ENTRIES_1);
|
|
580
|
+
}
|
|
581
|
+
finally { if (e_2) throw e_2.error; }
|
|
582
|
+
}
|
|
583
|
+
return [2 /*return*/, null];
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
}); }, {
|
|
587
|
+
defaultValue: null,
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
var validateDefault = function (output) { return __awaiter(void 0, void 0, void 0, function () {
|
|
591
|
+
var validation;
|
|
592
|
+
return __generator(this, function (_a) {
|
|
593
|
+
switch (_a.label) {
|
|
594
|
+
case 0:
|
|
595
|
+
validation = null;
|
|
596
|
+
return [4 /*yield*/, validateNoEmptyResult(output)];
|
|
597
|
+
case 1:
|
|
598
|
+
if (validation = _a.sent()) {
|
|
599
|
+
return [2 /*return*/, validation];
|
|
600
|
+
}
|
|
601
|
+
return [4 /*yield*/, validateNoToolCall(output)];
|
|
602
|
+
case 2:
|
|
603
|
+
if (validation = _a.sent()) {
|
|
604
|
+
return [2 /*return*/, validation];
|
|
605
|
+
}
|
|
606
|
+
return [2 /*return*/, null];
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}); };
|
|
610
|
+
|
|
611
|
+
var AgentConnectionService = /** @class */ (function () {
|
|
612
|
+
function AgentConnectionService() {
|
|
613
|
+
var _this = this;
|
|
614
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
615
|
+
this.contextService = inject(TYPES.contextService);
|
|
616
|
+
this.historyConnectionService = inject(TYPES.historyConnectionService);
|
|
617
|
+
this.agentSchemaService = inject(TYPES.agentSchemaService);
|
|
618
|
+
this.toolSchemaService = inject(TYPES.toolSchemaService);
|
|
619
|
+
this.completionSchemaService = inject(TYPES.completionSchemaService);
|
|
620
|
+
this.getAgent = memoize(function (_a) {
|
|
621
|
+
var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
|
|
622
|
+
return "".concat(clientId, "-").concat(agentName);
|
|
623
|
+
}, function (clientId, agentName) {
|
|
624
|
+
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
|
+
var completion = _this.completionSchemaService.get(completionName);
|
|
626
|
+
return new ClientAgent({
|
|
627
|
+
clientId: clientId,
|
|
628
|
+
agentName: agentName,
|
|
629
|
+
validate: validate,
|
|
630
|
+
logger: _this.loggerService,
|
|
631
|
+
history: _this.historyConnectionService.getHistory(clientId, agentName),
|
|
632
|
+
prompt: prompt,
|
|
633
|
+
tools: tools === null || tools === void 0 ? void 0 : tools.map(_this.toolSchemaService.get),
|
|
634
|
+
completion: completion,
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
this.execute = function (input) { return __awaiter(_this, void 0, void 0, function () {
|
|
638
|
+
return __generator(this, function (_a) {
|
|
639
|
+
switch (_a.label) {
|
|
640
|
+
case 0:
|
|
641
|
+
this.loggerService.log("agentConnectionService execute", {
|
|
642
|
+
input: input,
|
|
643
|
+
context: this.contextService.context,
|
|
644
|
+
});
|
|
645
|
+
return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).execute(input)];
|
|
646
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
}); };
|
|
650
|
+
this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
651
|
+
return __generator(this, function (_a) {
|
|
652
|
+
switch (_a.label) {
|
|
653
|
+
case 0:
|
|
654
|
+
this.loggerService.log("agentConnectionService waitForOutput", {
|
|
655
|
+
context: this.contextService.context,
|
|
656
|
+
});
|
|
657
|
+
return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).waitForOutput()];
|
|
658
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
}); };
|
|
662
|
+
this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
663
|
+
return __generator(this, function (_a) {
|
|
664
|
+
switch (_a.label) {
|
|
665
|
+
case 0:
|
|
666
|
+
this.loggerService.log("agentConnectionService commitToolOutput", {
|
|
667
|
+
content: content,
|
|
668
|
+
context: this.contextService.context,
|
|
669
|
+
});
|
|
670
|
+
return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(content)];
|
|
671
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
}); };
|
|
675
|
+
this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
676
|
+
return __generator(this, function (_a) {
|
|
677
|
+
switch (_a.label) {
|
|
678
|
+
case 0:
|
|
679
|
+
this.loggerService.log("agentConnectionService commitSystemMessage", {
|
|
680
|
+
message: message,
|
|
681
|
+
context: this.contextService.context,
|
|
682
|
+
});
|
|
683
|
+
return [4 /*yield*/, this.getAgent(this.contextService.context.clientId, this.contextService.context.agentName).commitToolOutput(message)];
|
|
684
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
}); };
|
|
688
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
689
|
+
return __generator(this, function (_a) {
|
|
690
|
+
this.loggerService.log("agentConnectionService dispose", {
|
|
691
|
+
context: this.contextService.context,
|
|
692
|
+
});
|
|
693
|
+
this.getAgent.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
|
|
694
|
+
return [2 /*return*/];
|
|
695
|
+
});
|
|
696
|
+
}); };
|
|
697
|
+
}
|
|
698
|
+
return AgentConnectionService;
|
|
699
|
+
}());
|
|
700
|
+
|
|
701
|
+
var ClientHistory = /** @class */ (function () {
|
|
702
|
+
function ClientHistory(params) {
|
|
703
|
+
var _this = this;
|
|
704
|
+
this.params = params;
|
|
705
|
+
this.push = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
706
|
+
return __generator(this, function (_a) {
|
|
707
|
+
switch (_a.label) {
|
|
708
|
+
case 0:
|
|
709
|
+
this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " push"), { message: message });
|
|
710
|
+
return [4 /*yield*/, this.params.items.push(message)];
|
|
711
|
+
case 1:
|
|
712
|
+
_a.sent();
|
|
713
|
+
return [2 /*return*/];
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
}); };
|
|
717
|
+
this.toArrayForRaw = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
718
|
+
var result, _a, _b, _c, item, e_1_1;
|
|
719
|
+
var _d, e_1, _e, _f;
|
|
720
|
+
return __generator(this, function (_g) {
|
|
721
|
+
switch (_g.label) {
|
|
722
|
+
case 0:
|
|
723
|
+
this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForRaw"));
|
|
724
|
+
result = [];
|
|
725
|
+
_g.label = 1;
|
|
726
|
+
case 1:
|
|
727
|
+
_g.trys.push([1, 6, 7, 12]);
|
|
728
|
+
_a = true, _b = __asyncValues(this.params.items);
|
|
729
|
+
_g.label = 2;
|
|
730
|
+
case 2: return [4 /*yield*/, _b.next()];
|
|
731
|
+
case 3:
|
|
732
|
+
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
|
|
733
|
+
_f = _c.value;
|
|
734
|
+
_a = false;
|
|
735
|
+
item = _f;
|
|
736
|
+
result.push(item);
|
|
737
|
+
_g.label = 4;
|
|
738
|
+
case 4:
|
|
739
|
+
_a = true;
|
|
740
|
+
return [3 /*break*/, 2];
|
|
741
|
+
case 5: return [3 /*break*/, 12];
|
|
742
|
+
case 6:
|
|
743
|
+
e_1_1 = _g.sent();
|
|
744
|
+
e_1 = { error: e_1_1 };
|
|
745
|
+
return [3 /*break*/, 12];
|
|
746
|
+
case 7:
|
|
747
|
+
_g.trys.push([7, , 10, 11]);
|
|
748
|
+
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
|
|
749
|
+
return [4 /*yield*/, _e.call(_b)];
|
|
750
|
+
case 8:
|
|
751
|
+
_g.sent();
|
|
752
|
+
_g.label = 9;
|
|
753
|
+
case 9: return [3 /*break*/, 11];
|
|
754
|
+
case 10:
|
|
755
|
+
if (e_1) throw e_1.error;
|
|
756
|
+
return [7 /*endfinally*/];
|
|
757
|
+
case 11: return [7 /*endfinally*/];
|
|
758
|
+
case 12: return [2 /*return*/, result];
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
}); };
|
|
762
|
+
this.toArrayForAgent = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
|
|
763
|
+
var commonMessagesRaw, systemMessagesRaw, _a, _b, _c, content, message, e_2_1, systemMessages, commonMessages, promptMessage;
|
|
764
|
+
var _this = this;
|
|
765
|
+
var _d, e_2, _e, _f;
|
|
766
|
+
return __generator(this, function (_g) {
|
|
767
|
+
switch (_g.label) {
|
|
768
|
+
case 0:
|
|
769
|
+
this.params.logger.debug("ClientHistory agentName=".concat(this.params.agentName, " toArrayForAgent"));
|
|
770
|
+
commonMessagesRaw = [];
|
|
771
|
+
systemMessagesRaw = [];
|
|
772
|
+
_g.label = 1;
|
|
773
|
+
case 1:
|
|
774
|
+
_g.trys.push([1, 6, 7, 12]);
|
|
775
|
+
_a = true, _b = __asyncValues(this.params.items);
|
|
776
|
+
_g.label = 2;
|
|
777
|
+
case 2: return [4 /*yield*/, _b.next()];
|
|
778
|
+
case 3:
|
|
779
|
+
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
|
|
780
|
+
_f = _c.value;
|
|
781
|
+
_a = false;
|
|
782
|
+
content = _f;
|
|
783
|
+
message = content;
|
|
784
|
+
if (message.role === "resque") {
|
|
785
|
+
commonMessagesRaw.splice(0, commonMessagesRaw.length);
|
|
786
|
+
systemMessagesRaw.splice(0, systemMessagesRaw.length);
|
|
787
|
+
return [3 /*break*/, 4];
|
|
788
|
+
}
|
|
789
|
+
if (message.role === "system") {
|
|
790
|
+
systemMessagesRaw.push(message);
|
|
791
|
+
}
|
|
792
|
+
else {
|
|
793
|
+
commonMessagesRaw.push(message);
|
|
794
|
+
}
|
|
795
|
+
_g.label = 4;
|
|
796
|
+
case 4:
|
|
797
|
+
_a = true;
|
|
798
|
+
return [3 /*break*/, 2];
|
|
799
|
+
case 5: return [3 /*break*/, 12];
|
|
800
|
+
case 6:
|
|
801
|
+
e_2_1 = _g.sent();
|
|
802
|
+
e_2 = { error: e_2_1 };
|
|
803
|
+
return [3 /*break*/, 12];
|
|
804
|
+
case 7:
|
|
805
|
+
_g.trys.push([7, , 10, 11]);
|
|
806
|
+
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
|
|
807
|
+
return [4 /*yield*/, _e.call(_b)];
|
|
808
|
+
case 8:
|
|
809
|
+
_g.sent();
|
|
810
|
+
_g.label = 9;
|
|
811
|
+
case 9: return [3 /*break*/, 11];
|
|
812
|
+
case 10:
|
|
813
|
+
if (e_2) throw e_2.error;
|
|
814
|
+
return [7 /*endfinally*/];
|
|
815
|
+
case 11: return [7 /*endfinally*/];
|
|
816
|
+
case 12:
|
|
817
|
+
systemMessages = systemMessagesRaw.filter(function (_a) {
|
|
818
|
+
var agentName = _a.agentName;
|
|
819
|
+
return agentName === _this.params.agentName;
|
|
820
|
+
});
|
|
821
|
+
commonMessages = commonMessagesRaw
|
|
822
|
+
.filter(function (_a) {
|
|
823
|
+
var role = _a.role, agentName = _a.agentName, tool_calls = _a.tool_calls;
|
|
824
|
+
var isOk = true;
|
|
825
|
+
if (role === "tool") {
|
|
826
|
+
isOk = isOk && agentName === _this.params.agentName;
|
|
827
|
+
}
|
|
828
|
+
if (tool_calls) {
|
|
829
|
+
isOk = isOk && agentName === _this.params.agentName;
|
|
830
|
+
}
|
|
831
|
+
return isOk;
|
|
832
|
+
})
|
|
833
|
+
.slice(-GLOBAL_CONFIG.CC_KEEP_MESSAGES);
|
|
834
|
+
promptMessage = {
|
|
835
|
+
agentName: this.params.agentName,
|
|
836
|
+
content: prompt,
|
|
837
|
+
role: "system",
|
|
838
|
+
};
|
|
839
|
+
return [2 /*return*/, __spreadArray(__spreadArray([promptMessage], __read(systemMessages), false), __read(commonMessages), false)];
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
}); };
|
|
843
|
+
}
|
|
844
|
+
return ClientHistory;
|
|
845
|
+
}());
|
|
846
|
+
|
|
847
|
+
var HistoryConnectionService = /** @class */ (function () {
|
|
848
|
+
function HistoryConnectionService() {
|
|
849
|
+
var _this = this;
|
|
850
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
851
|
+
this.contextService = inject(TYPES.contextService);
|
|
852
|
+
this.getItems = memoize(function (_a) {
|
|
853
|
+
var _b = __read(_a, 1), clientId = _b[0];
|
|
854
|
+
return clientId;
|
|
855
|
+
}, function () { return new PubsubArrayAdapter(); });
|
|
856
|
+
this.getHistory = memoize(function (_a) {
|
|
857
|
+
var _b = __read(_a, 2), clientId = _b[0], agentName = _b[1];
|
|
858
|
+
return "".concat(clientId, "-").concat(agentName);
|
|
859
|
+
}, function (clientId, agentName) {
|
|
860
|
+
return new ClientHistory({
|
|
861
|
+
clientId: clientId,
|
|
862
|
+
agentName: agentName,
|
|
863
|
+
items: _this.getItems(clientId),
|
|
864
|
+
logger: _this.loggerService,
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
this.push = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
868
|
+
return __generator(this, function (_a) {
|
|
869
|
+
switch (_a.label) {
|
|
870
|
+
case 0:
|
|
871
|
+
this.loggerService.log("historyConnectionService push", {
|
|
872
|
+
context: this.contextService.context,
|
|
873
|
+
});
|
|
874
|
+
return [4 /*yield*/, this.getHistory(this.contextService.context.clientId, this.contextService.context.agentName).push(message)];
|
|
875
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
}); };
|
|
879
|
+
this.toArrayForAgent = function (prompt) { return __awaiter(_this, void 0, void 0, function () {
|
|
880
|
+
return __generator(this, function (_a) {
|
|
881
|
+
switch (_a.label) {
|
|
882
|
+
case 0:
|
|
883
|
+
this.loggerService.log("historyConnectionService toArrayForAgent", {
|
|
884
|
+
context: this.contextService.context,
|
|
885
|
+
});
|
|
886
|
+
return [4 /*yield*/, this.getHistory(this.contextService.context.clientId, this.contextService.context.agentName).toArrayForAgent(prompt)];
|
|
887
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
}); };
|
|
891
|
+
this.toArrayForRaw = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
892
|
+
return __generator(this, function (_a) {
|
|
893
|
+
switch (_a.label) {
|
|
894
|
+
case 0:
|
|
895
|
+
this.loggerService.log("historyConnectionService toArrayForRaw", {
|
|
896
|
+
context: this.contextService.context,
|
|
897
|
+
});
|
|
898
|
+
return [4 /*yield*/, this.getHistory(this.contextService.context.clientId, this.contextService.context.agentName).toArrayForRaw()];
|
|
899
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
}); };
|
|
903
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
904
|
+
return __generator(this, function (_a) {
|
|
905
|
+
switch (_a.label) {
|
|
906
|
+
case 0:
|
|
907
|
+
this.loggerService.log("historyConnectionService dispose", {
|
|
908
|
+
context: this.contextService.context,
|
|
909
|
+
});
|
|
910
|
+
return [4 /*yield*/, this.getItems(this.contextService.context.clientId).clear()];
|
|
911
|
+
case 1:
|
|
912
|
+
_a.sent();
|
|
913
|
+
this.getItems.clear(this.contextService.context.clientId);
|
|
914
|
+
this.getHistory.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.agentName));
|
|
915
|
+
return [2 /*return*/];
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
}); };
|
|
919
|
+
}
|
|
920
|
+
return HistoryConnectionService;
|
|
921
|
+
}());
|
|
922
|
+
|
|
923
|
+
var ToolSchemaService = /** @class */ (function () {
|
|
924
|
+
function ToolSchemaService() {
|
|
925
|
+
var _this = this;
|
|
926
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
927
|
+
this.registry = new ToolRegistry("toolSchemaService");
|
|
928
|
+
this.register = function (key, value) {
|
|
929
|
+
_this.loggerService.log('toolSchemaService register');
|
|
930
|
+
_this.registry = _this.registry.register(key, value);
|
|
931
|
+
};
|
|
932
|
+
this.get = function (key) {
|
|
933
|
+
_this.loggerService.log('toolSchemaService get', { key: key });
|
|
934
|
+
return _this.registry.get(key);
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
return ToolSchemaService;
|
|
938
|
+
}());
|
|
939
|
+
|
|
940
|
+
var ClientSwarm = /** @class */ (function () {
|
|
941
|
+
function ClientSwarm(params) {
|
|
942
|
+
var _this = this;
|
|
943
|
+
this.params = params;
|
|
944
|
+
this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
945
|
+
var START_TIME, _a, agentName, output, _b;
|
|
946
|
+
var _this = this;
|
|
947
|
+
return __generator(this, function (_c) {
|
|
948
|
+
switch (_c.label) {
|
|
949
|
+
case 0:
|
|
950
|
+
this.params.logger.debug("BaseConnection waitForOutput");
|
|
951
|
+
START_TIME = Date.now();
|
|
952
|
+
_c.label = 1;
|
|
953
|
+
case 1:
|
|
954
|
+
if (Date.now() - START_TIME >= GLOBAL_CONFIG.CC_ANSWER_TIMEOUT_SECONDS) {
|
|
955
|
+
throw new Error("agent-swarm ClientSwarm waitForOutput timeout reached for ".concat(this.params.swarmName));
|
|
956
|
+
}
|
|
957
|
+
return [4 /*yield*/, Promise.race(Object.entries(this.params.agentMap).map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
958
|
+
var _c;
|
|
959
|
+
var _d = __read(_b, 2), agentName = _d[0], agent = _d[1];
|
|
960
|
+
return __generator(this, function (_e) {
|
|
961
|
+
switch (_e.label) {
|
|
962
|
+
case 0:
|
|
963
|
+
_c = [agentName];
|
|
964
|
+
return [4 /*yield*/, agent.waitForOutput()];
|
|
965
|
+
case 1: return [2 /*return*/, _c.concat([
|
|
966
|
+
_e.sent()
|
|
967
|
+
])];
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
}); }))];
|
|
971
|
+
case 2:
|
|
972
|
+
_a = __read.apply(void 0, [_c.sent(), 2]), agentName = _a[0], output = _a[1];
|
|
973
|
+
_b = agentName;
|
|
974
|
+
return [4 /*yield*/, this.getAgentName()];
|
|
975
|
+
case 3:
|
|
976
|
+
if (_b === (_c.sent())) {
|
|
977
|
+
return [2 /*return*/, output];
|
|
978
|
+
}
|
|
979
|
+
return [3 /*break*/, 1];
|
|
980
|
+
case 4: return [2 /*return*/];
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
}); };
|
|
984
|
+
this.getAgentName = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
985
|
+
return __generator(this, function (_a) {
|
|
986
|
+
this.params.logger.debug("ClientSwarm getAgentName");
|
|
987
|
+
return [2 /*return*/, this._activeAgent];
|
|
988
|
+
});
|
|
989
|
+
}); };
|
|
990
|
+
this.getAgent = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
991
|
+
var agent;
|
|
992
|
+
return __generator(this, function (_a) {
|
|
993
|
+
switch (_a.label) {
|
|
994
|
+
case 0:
|
|
995
|
+
this.params.logger.debug("ClientSwarm getAgent");
|
|
996
|
+
return [4 /*yield*/, this.getAgentName()];
|
|
997
|
+
case 1:
|
|
998
|
+
agent = _a.sent();
|
|
999
|
+
return [2 /*return*/, this.params.agentMap[agent]];
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
}); };
|
|
1003
|
+
this.setAgentName = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1004
|
+
return __generator(this, function (_a) {
|
|
1005
|
+
this.params.logger.debug("ClientSwarm setAgentName agentName=".concat(agentName));
|
|
1006
|
+
this._activeAgent = agentName;
|
|
1007
|
+
return [2 /*return*/];
|
|
1008
|
+
});
|
|
1009
|
+
}); };
|
|
1010
|
+
this._activeAgent = params.defaultAgent;
|
|
1011
|
+
}
|
|
1012
|
+
return ClientSwarm;
|
|
1013
|
+
}());
|
|
1014
|
+
|
|
1015
|
+
var SwarmConnectionService = /** @class */ (function () {
|
|
1016
|
+
function SwarmConnectionService() {
|
|
1017
|
+
var _this = this;
|
|
1018
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1019
|
+
this.contextService = inject(TYPES.contextService);
|
|
1020
|
+
this.agentConnectionService = inject(TYPES.agentConnectionService);
|
|
1021
|
+
this.swarmSchemaService = inject(TYPES.swarmSchemaService);
|
|
1022
|
+
this.getSwarm = memoize(function (_a) {
|
|
1023
|
+
var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
|
|
1024
|
+
return "".concat(clientId, "-").concat(swarmName);
|
|
1025
|
+
}, function (clientId, swarmName) {
|
|
1026
|
+
var e_1, _a;
|
|
1027
|
+
var _b = _this.swarmSchemaService.get(swarmName), agentList = _b.agentList, defaultAgent = _b.defaultAgent;
|
|
1028
|
+
var agentMap = {};
|
|
1029
|
+
try {
|
|
1030
|
+
for (var agentList_1 = __values(agentList), agentList_1_1 = agentList_1.next(); !agentList_1_1.done; agentList_1_1 = agentList_1.next()) {
|
|
1031
|
+
var agentName = agentList_1_1.value;
|
|
1032
|
+
agentMap[agentName] = _this.agentConnectionService.getAgent(clientId, agentName);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1036
|
+
finally {
|
|
1037
|
+
try {
|
|
1038
|
+
if (agentList_1_1 && !agentList_1_1.done && (_a = agentList_1.return)) _a.call(agentList_1);
|
|
1039
|
+
}
|
|
1040
|
+
finally { if (e_1) throw e_1.error; }
|
|
1041
|
+
}
|
|
1042
|
+
return new ClientSwarm({
|
|
1043
|
+
clientId: clientId,
|
|
1044
|
+
agentMap: agentMap,
|
|
1045
|
+
defaultAgent: defaultAgent,
|
|
1046
|
+
swarmName: swarmName,
|
|
1047
|
+
logger: _this.loggerService,
|
|
1048
|
+
});
|
|
1049
|
+
});
|
|
1050
|
+
this.waitForOutput = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1051
|
+
return __generator(this, function (_a) {
|
|
1052
|
+
switch (_a.label) {
|
|
1053
|
+
case 0:
|
|
1054
|
+
this.loggerService.log("swarmConnectionService waitForOutput", {
|
|
1055
|
+
context: this.contextService.context,
|
|
1056
|
+
});
|
|
1057
|
+
return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).waitForOutput()];
|
|
1058
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1059
|
+
}
|
|
1060
|
+
});
|
|
1061
|
+
}); };
|
|
1062
|
+
this.getAgentName = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1063
|
+
return __generator(this, function (_a) {
|
|
1064
|
+
switch (_a.label) {
|
|
1065
|
+
case 0:
|
|
1066
|
+
this.loggerService.log("swarmConnectionService getAgentName", {
|
|
1067
|
+
context: this.contextService.context,
|
|
1068
|
+
});
|
|
1069
|
+
return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).getAgentName()];
|
|
1070
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1071
|
+
}
|
|
1072
|
+
});
|
|
1073
|
+
}); };
|
|
1074
|
+
this.getAgent = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1075
|
+
return __generator(this, function (_a) {
|
|
1076
|
+
switch (_a.label) {
|
|
1077
|
+
case 0:
|
|
1078
|
+
this.loggerService.log("swarmConnectionService getAgent", {
|
|
1079
|
+
context: this.contextService.context,
|
|
1080
|
+
});
|
|
1081
|
+
return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).getAgent()];
|
|
1082
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
}); };
|
|
1086
|
+
this.setAgentName = function (agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1087
|
+
return __generator(this, function (_a) {
|
|
1088
|
+
switch (_a.label) {
|
|
1089
|
+
case 0:
|
|
1090
|
+
this.loggerService.log("swarmConnectionService setAgentName", {
|
|
1091
|
+
agentName: agentName,
|
|
1092
|
+
context: this.contextService.context,
|
|
1093
|
+
});
|
|
1094
|
+
return [4 /*yield*/, this.getSwarm(this.contextService.context.clientId, this.contextService.context.swarmName).setAgentName(agentName)];
|
|
1095
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}); };
|
|
1099
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1100
|
+
return __generator(this, function (_a) {
|
|
1101
|
+
this.loggerService.log("swarmConnectionService dispose", {
|
|
1102
|
+
context: this.contextService.context,
|
|
1103
|
+
});
|
|
1104
|
+
this.getSwarm.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
|
|
1105
|
+
return [2 /*return*/];
|
|
1106
|
+
});
|
|
1107
|
+
}); };
|
|
1108
|
+
}
|
|
1109
|
+
return SwarmConnectionService;
|
|
1110
|
+
}());
|
|
1111
|
+
|
|
1112
|
+
var SwarmSchemaService = /** @class */ (function () {
|
|
1113
|
+
function SwarmSchemaService() {
|
|
1114
|
+
var _this = this;
|
|
1115
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1116
|
+
this.registry = new ToolRegistry("swarmSchemaService");
|
|
1117
|
+
this.register = function (key, value) {
|
|
1118
|
+
_this.loggerService.log("swarmSchemaService register", { key: key });
|
|
1119
|
+
_this.registry = _this.registry.register(key, value);
|
|
1120
|
+
};
|
|
1121
|
+
this.get = function (key) {
|
|
1122
|
+
_this.loggerService.log("swarmSchemaService get", { key: key });
|
|
1123
|
+
return _this.registry.get(key);
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
return SwarmSchemaService;
|
|
1127
|
+
}());
|
|
1128
|
+
|
|
1129
|
+
var CompletionSchemaService = /** @class */ (function () {
|
|
1130
|
+
function CompletionSchemaService() {
|
|
1131
|
+
var _this = this;
|
|
1132
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1133
|
+
this.registry = new ToolRegistry("completionSchemaService");
|
|
1134
|
+
this.register = function (key, value) {
|
|
1135
|
+
_this.loggerService.log("completionSchemaService register", { key: key });
|
|
1136
|
+
_this.registry = _this.registry.register(key, value);
|
|
1137
|
+
};
|
|
1138
|
+
this.get = function (key) {
|
|
1139
|
+
_this.loggerService.log("completionSchemaService get", { key: key });
|
|
1140
|
+
return _this.registry.get(key);
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
return CompletionSchemaService;
|
|
1144
|
+
}());
|
|
1145
|
+
|
|
1146
|
+
var ClientSession = /** @class */ (function () {
|
|
1147
|
+
function ClientSession(params) {
|
|
1148
|
+
var _this = this;
|
|
1149
|
+
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
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
}); };
|
|
1168
|
+
this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
1169
|
+
var agent;
|
|
1170
|
+
return __generator(this, function (_a) {
|
|
1171
|
+
switch (_a.label) {
|
|
1172
|
+
case 0:
|
|
1173
|
+
this.params.logger.debug("BaseSession commitToolOutput", {
|
|
1174
|
+
content: content,
|
|
1175
|
+
});
|
|
1176
|
+
return [4 /*yield*/, this.params.swarm.getAgent()];
|
|
1177
|
+
case 1:
|
|
1178
|
+
agent = _a.sent();
|
|
1179
|
+
return [4 /*yield*/, agent.commitToolOutput(content)];
|
|
1180
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
}); };
|
|
1184
|
+
this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
1185
|
+
var agent;
|
|
1186
|
+
return __generator(this, function (_a) {
|
|
1187
|
+
switch (_a.label) {
|
|
1188
|
+
case 0:
|
|
1189
|
+
this.params.logger.debug("BaseSession commitSystemMessage", {
|
|
1190
|
+
message: message,
|
|
1191
|
+
});
|
|
1192
|
+
return [4 /*yield*/, this.params.swarm.getAgent()];
|
|
1193
|
+
case 1:
|
|
1194
|
+
agent = _a.sent();
|
|
1195
|
+
return [4 /*yield*/, agent.commitSystemMessage(message)];
|
|
1196
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
}); };
|
|
1200
|
+
this.connect = function (connector) {
|
|
1201
|
+
_this.params.logger.debug("BaseSession connect");
|
|
1202
|
+
return function (incoming) { return __awaiter(_this, void 0, void 0, function () {
|
|
1203
|
+
var _a;
|
|
1204
|
+
var _b;
|
|
1205
|
+
return __generator(this, function (_c) {
|
|
1206
|
+
switch (_c.label) {
|
|
1207
|
+
case 0:
|
|
1208
|
+
this.params.logger.debug("BaseSession connect call");
|
|
1209
|
+
_a = connector;
|
|
1210
|
+
_b = {};
|
|
1211
|
+
return [4 /*yield*/, this.execute(incoming.data)];
|
|
1212
|
+
case 1:
|
|
1213
|
+
_b.data = _c.sent();
|
|
1214
|
+
return [4 /*yield*/, this.params.swarm.getAgentName()];
|
|
1215
|
+
case 2: return [4 /*yield*/, _a.apply(void 0, [(_b.agentName = _c.sent(),
|
|
1216
|
+
_b.clientId = incoming.clientId,
|
|
1217
|
+
_b)])];
|
|
1218
|
+
case 3:
|
|
1219
|
+
_c.sent();
|
|
1220
|
+
return [2 /*return*/];
|
|
1221
|
+
}
|
|
1222
|
+
});
|
|
1223
|
+
}); };
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
return ClientSession;
|
|
1227
|
+
}());
|
|
1228
|
+
|
|
1229
|
+
var SessionConnectionService = /** @class */ (function () {
|
|
1230
|
+
function SessionConnectionService() {
|
|
1231
|
+
var _this = this;
|
|
1232
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1233
|
+
this.contextService = inject(TYPES.contextService);
|
|
1234
|
+
this.swarmConnectionService = inject(TYPES.swarmConnectionService);
|
|
1235
|
+
this.getSession = memoize(function (_a) {
|
|
1236
|
+
var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
|
|
1237
|
+
return "".concat(clientId, "-").concat(swarmName);
|
|
1238
|
+
}, function (clientId, swarmName) {
|
|
1239
|
+
return new ClientSession({
|
|
1240
|
+
clientId: clientId,
|
|
1241
|
+
logger: _this.loggerService,
|
|
1242
|
+
swarm: _this.swarmConnectionService.getSwarm(clientId, swarmName),
|
|
1243
|
+
});
|
|
1244
|
+
});
|
|
1245
|
+
this.execute = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
1246
|
+
return __generator(this, function (_a) {
|
|
1247
|
+
switch (_a.label) {
|
|
1248
|
+
case 0:
|
|
1249
|
+
this.loggerService.log("sessionConnectionService execute", {
|
|
1250
|
+
context: this.contextService.context,
|
|
1251
|
+
});
|
|
1252
|
+
return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).execute(content)];
|
|
1253
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
}); };
|
|
1257
|
+
this.connect = function (connector) {
|
|
1258
|
+
_this.loggerService.log("sessionConnectionService connect", {
|
|
1259
|
+
context: _this.contextService.context,
|
|
1260
|
+
});
|
|
1261
|
+
return _this.getSession(_this.contextService.context.clientId, _this.contextService.context.swarmName).connect(connector);
|
|
1262
|
+
};
|
|
1263
|
+
this.commitToolOutput = function (content) { return __awaiter(_this, void 0, void 0, function () {
|
|
1264
|
+
return __generator(this, function (_a) {
|
|
1265
|
+
switch (_a.label) {
|
|
1266
|
+
case 0:
|
|
1267
|
+
this.loggerService.log("sessionConnectionService commitToolOutput", {
|
|
1268
|
+
context: this.contextService.context,
|
|
1269
|
+
});
|
|
1270
|
+
return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitToolOutput(content)];
|
|
1271
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1272
|
+
}
|
|
1273
|
+
});
|
|
1274
|
+
}); };
|
|
1275
|
+
this.commitSystemMessage = function (message) { return __awaiter(_this, void 0, void 0, function () {
|
|
1276
|
+
return __generator(this, function (_a) {
|
|
1277
|
+
switch (_a.label) {
|
|
1278
|
+
case 0:
|
|
1279
|
+
this.loggerService.log("sessionConnectionService commitSystemMessage", {
|
|
1280
|
+
context: this.contextService.context,
|
|
1281
|
+
});
|
|
1282
|
+
return [4 /*yield*/, this.getSession(this.contextService.context.clientId, this.contextService.context.swarmName).commitSystemMessage(message)];
|
|
1283
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1284
|
+
}
|
|
1285
|
+
});
|
|
1286
|
+
}); };
|
|
1287
|
+
this.dispose = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1288
|
+
return __generator(this, function (_a) {
|
|
1289
|
+
this.loggerService.log("sessionConnectionService dispose", {
|
|
1290
|
+
context: this.contextService.context,
|
|
1291
|
+
});
|
|
1292
|
+
this.getSession.clear("".concat(this.contextService.context.clientId, "-").concat(this.contextService.context.swarmName));
|
|
1293
|
+
return [2 /*return*/];
|
|
1294
|
+
});
|
|
1295
|
+
}); };
|
|
1296
|
+
}
|
|
1297
|
+
return SessionConnectionService;
|
|
1298
|
+
}());
|
|
1299
|
+
|
|
1300
|
+
var AgentPublicService = /** @class */ (function () {
|
|
1301
|
+
function AgentPublicService() {
|
|
1302
|
+
var _this = this;
|
|
1303
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1304
|
+
this.agentConnectionService = inject(TYPES.agentConnectionService);
|
|
1305
|
+
this.execute = function (input, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1306
|
+
var _this = this;
|
|
1307
|
+
return __generator(this, function (_a) {
|
|
1308
|
+
switch (_a.label) {
|
|
1309
|
+
case 0:
|
|
1310
|
+
this.loggerService.log("agentPublicService execute", {
|
|
1311
|
+
input: input,
|
|
1312
|
+
clientId: clientId,
|
|
1313
|
+
agentName: agentName,
|
|
1314
|
+
});
|
|
1315
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1316
|
+
return __generator(this, function (_a) {
|
|
1317
|
+
switch (_a.label) {
|
|
1318
|
+
case 0: return [4 /*yield*/, this.agentConnectionService.execute(input)];
|
|
1319
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
}); }, {
|
|
1323
|
+
clientId: clientId,
|
|
1324
|
+
agentName: agentName,
|
|
1325
|
+
swarmName: "",
|
|
1326
|
+
})];
|
|
1327
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1328
|
+
}
|
|
1329
|
+
});
|
|
1330
|
+
}); };
|
|
1331
|
+
this.waitForOutput = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1332
|
+
var _this = this;
|
|
1333
|
+
return __generator(this, function (_a) {
|
|
1334
|
+
switch (_a.label) {
|
|
1335
|
+
case 0:
|
|
1336
|
+
this.loggerService.log("agentPublicService waitForOutput", {
|
|
1337
|
+
clientId: clientId,
|
|
1338
|
+
agentName: agentName,
|
|
1339
|
+
});
|
|
1340
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1341
|
+
return __generator(this, function (_a) {
|
|
1342
|
+
switch (_a.label) {
|
|
1343
|
+
case 0: return [4 /*yield*/, this.agentConnectionService.waitForOutput()];
|
|
1344
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1345
|
+
}
|
|
1346
|
+
});
|
|
1347
|
+
}); }, {
|
|
1348
|
+
clientId: clientId,
|
|
1349
|
+
agentName: agentName,
|
|
1350
|
+
swarmName: "",
|
|
1351
|
+
})];
|
|
1352
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1353
|
+
}
|
|
1354
|
+
});
|
|
1355
|
+
}); };
|
|
1356
|
+
this.commitToolOutput = function (content, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1357
|
+
var _this = this;
|
|
1358
|
+
return __generator(this, function (_a) {
|
|
1359
|
+
switch (_a.label) {
|
|
1360
|
+
case 0:
|
|
1361
|
+
this.loggerService.log("agentPublicService commitToolOutput", {
|
|
1362
|
+
content: content,
|
|
1363
|
+
clientId: clientId,
|
|
1364
|
+
agentName: agentName,
|
|
1365
|
+
});
|
|
1366
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1367
|
+
return __generator(this, function (_a) {
|
|
1368
|
+
switch (_a.label) {
|
|
1369
|
+
case 0: return [4 /*yield*/, this.agentConnectionService.commitToolOutput(content)];
|
|
1370
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1371
|
+
}
|
|
1372
|
+
});
|
|
1373
|
+
}); }, {
|
|
1374
|
+
clientId: clientId,
|
|
1375
|
+
agentName: agentName,
|
|
1376
|
+
swarmName: "",
|
|
1377
|
+
})];
|
|
1378
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
}); };
|
|
1382
|
+
this.commitSystemMessage = function (message, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1383
|
+
var _this = this;
|
|
1384
|
+
return __generator(this, function (_a) {
|
|
1385
|
+
switch (_a.label) {
|
|
1386
|
+
case 0:
|
|
1387
|
+
this.loggerService.log("agentPublicService commitSystemMessage", {
|
|
1388
|
+
message: message,
|
|
1389
|
+
clientId: clientId,
|
|
1390
|
+
agentName: agentName,
|
|
1391
|
+
});
|
|
1392
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1393
|
+
return __generator(this, function (_a) {
|
|
1394
|
+
switch (_a.label) {
|
|
1395
|
+
case 0: return [4 /*yield*/, this.agentConnectionService.commitSystemMessage(message)];
|
|
1396
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1399
|
+
}); }, {
|
|
1400
|
+
clientId: clientId,
|
|
1401
|
+
agentName: agentName,
|
|
1402
|
+
swarmName: "",
|
|
1403
|
+
})];
|
|
1404
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
}); };
|
|
1408
|
+
this.dispose = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1409
|
+
var _this = this;
|
|
1410
|
+
return __generator(this, function (_a) {
|
|
1411
|
+
switch (_a.label) {
|
|
1412
|
+
case 0:
|
|
1413
|
+
this.loggerService.log("agentPublicService dispose", {
|
|
1414
|
+
clientId: clientId,
|
|
1415
|
+
agentName: agentName,
|
|
1416
|
+
});
|
|
1417
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1418
|
+
return __generator(this, function (_a) {
|
|
1419
|
+
switch (_a.label) {
|
|
1420
|
+
case 0: return [4 /*yield*/, this.agentConnectionService.dispose()];
|
|
1421
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1422
|
+
}
|
|
1423
|
+
});
|
|
1424
|
+
}); }, {
|
|
1425
|
+
clientId: clientId,
|
|
1426
|
+
agentName: agentName,
|
|
1427
|
+
swarmName: "",
|
|
1428
|
+
})];
|
|
1429
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1430
|
+
}
|
|
1431
|
+
});
|
|
1432
|
+
}); };
|
|
1433
|
+
}
|
|
1434
|
+
return AgentPublicService;
|
|
1435
|
+
}());
|
|
1436
|
+
|
|
1437
|
+
var HistoryPublicService = /** @class */ (function () {
|
|
1438
|
+
function HistoryPublicService() {
|
|
1439
|
+
var _this = this;
|
|
1440
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1441
|
+
this.historyConnectionService = inject(TYPES.historyConnectionService);
|
|
1442
|
+
this.push = function (message, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1443
|
+
var _this = this;
|
|
1444
|
+
return __generator(this, function (_a) {
|
|
1445
|
+
switch (_a.label) {
|
|
1446
|
+
case 0:
|
|
1447
|
+
this.loggerService.log("historyPublicService push", {
|
|
1448
|
+
message: message,
|
|
1449
|
+
clientId: clientId,
|
|
1450
|
+
agentName: agentName,
|
|
1451
|
+
});
|
|
1452
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1453
|
+
return __generator(this, function (_a) {
|
|
1454
|
+
switch (_a.label) {
|
|
1455
|
+
case 0: return [4 /*yield*/, this.historyConnectionService.push(message)];
|
|
1456
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
}); }, {
|
|
1460
|
+
clientId: clientId,
|
|
1461
|
+
agentName: agentName,
|
|
1462
|
+
swarmName: "",
|
|
1463
|
+
})];
|
|
1464
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1467
|
+
}); };
|
|
1468
|
+
this.toArrayForAgent = function (prompt, clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1469
|
+
var _this = this;
|
|
1470
|
+
return __generator(this, function (_a) {
|
|
1471
|
+
switch (_a.label) {
|
|
1472
|
+
case 0:
|
|
1473
|
+
this.loggerService.log("historyPublicService toArrayForAgent", {
|
|
1474
|
+
prompt: prompt,
|
|
1475
|
+
clientId: clientId,
|
|
1476
|
+
agentName: agentName,
|
|
1477
|
+
});
|
|
1478
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1479
|
+
return __generator(this, function (_a) {
|
|
1480
|
+
switch (_a.label) {
|
|
1481
|
+
case 0: return [4 /*yield*/, this.historyConnectionService.toArrayForAgent(prompt)];
|
|
1482
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1483
|
+
}
|
|
1484
|
+
});
|
|
1485
|
+
}); }, {
|
|
1486
|
+
clientId: clientId,
|
|
1487
|
+
agentName: agentName,
|
|
1488
|
+
swarmName: "",
|
|
1489
|
+
})];
|
|
1490
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1491
|
+
}
|
|
1492
|
+
});
|
|
1493
|
+
}); };
|
|
1494
|
+
this.toArrayForRaw = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1495
|
+
var _this = this;
|
|
1496
|
+
return __generator(this, function (_a) {
|
|
1497
|
+
switch (_a.label) {
|
|
1498
|
+
case 0:
|
|
1499
|
+
this.loggerService.log("historyPublicService toArrayForRaw", {
|
|
1500
|
+
clientId: clientId,
|
|
1501
|
+
agentName: agentName,
|
|
1502
|
+
});
|
|
1503
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1504
|
+
return __generator(this, function (_a) {
|
|
1505
|
+
switch (_a.label) {
|
|
1506
|
+
case 0: return [4 /*yield*/, this.historyConnectionService.toArrayForRaw()];
|
|
1507
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1508
|
+
}
|
|
1509
|
+
});
|
|
1510
|
+
}); }, {
|
|
1511
|
+
clientId: clientId,
|
|
1512
|
+
agentName: agentName,
|
|
1513
|
+
swarmName: "",
|
|
1514
|
+
})];
|
|
1515
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
}); };
|
|
1519
|
+
this.dispose = function (clientId, agentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1520
|
+
var _this = this;
|
|
1521
|
+
return __generator(this, function (_a) {
|
|
1522
|
+
switch (_a.label) {
|
|
1523
|
+
case 0:
|
|
1524
|
+
this.loggerService.log("historyPublicService dispose", {
|
|
1525
|
+
clientId: clientId,
|
|
1526
|
+
agentName: agentName,
|
|
1527
|
+
});
|
|
1528
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1529
|
+
return __generator(this, function (_a) {
|
|
1530
|
+
switch (_a.label) {
|
|
1531
|
+
case 0: return [4 /*yield*/, this.historyConnectionService.dispose()];
|
|
1532
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
}); }, {
|
|
1536
|
+
clientId: clientId,
|
|
1537
|
+
agentName: agentName,
|
|
1538
|
+
swarmName: "",
|
|
1539
|
+
})];
|
|
1540
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1541
|
+
}
|
|
1542
|
+
});
|
|
1543
|
+
}); };
|
|
1544
|
+
}
|
|
1545
|
+
return HistoryPublicService;
|
|
1546
|
+
}());
|
|
1547
|
+
|
|
1548
|
+
var SessionPublicService = /** @class */ (function () {
|
|
1549
|
+
function SessionPublicService() {
|
|
1550
|
+
var _this = this;
|
|
1551
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1552
|
+
this.sessionConnectionService = inject(TYPES.sessionConnectionService);
|
|
1553
|
+
this.execute = function (content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1554
|
+
var _this = this;
|
|
1555
|
+
return __generator(this, function (_a) {
|
|
1556
|
+
switch (_a.label) {
|
|
1557
|
+
case 0:
|
|
1558
|
+
this.loggerService.log("sessionPublicService execute", {
|
|
1559
|
+
content: content,
|
|
1560
|
+
clientId: clientId,
|
|
1561
|
+
swarmName: swarmName,
|
|
1562
|
+
});
|
|
1563
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1564
|
+
return __generator(this, function (_a) {
|
|
1565
|
+
switch (_a.label) {
|
|
1566
|
+
case 0: return [4 /*yield*/, this.sessionConnectionService.execute(content)];
|
|
1567
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1568
|
+
}
|
|
1569
|
+
});
|
|
1570
|
+
}); }, {
|
|
1571
|
+
clientId: clientId,
|
|
1572
|
+
swarmName: swarmName,
|
|
1573
|
+
agentName: "",
|
|
1574
|
+
})];
|
|
1575
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
}); };
|
|
1579
|
+
this.connect = function (connector, clientId, swarmName) {
|
|
1580
|
+
_this.loggerService.log("sessionPublicService connect", {
|
|
1581
|
+
clientId: clientId,
|
|
1582
|
+
swarmName: swarmName,
|
|
1583
|
+
});
|
|
1584
|
+
return ContextService.runInContext(function () {
|
|
1585
|
+
var receive = _this.sessionConnectionService.connect(function (outgoing) { return __awaiter(_this, void 0, void 0, function () {
|
|
1586
|
+
var _this = this;
|
|
1587
|
+
return __generator(this, function (_a) {
|
|
1588
|
+
switch (_a.label) {
|
|
1589
|
+
case 0: return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1590
|
+
return __generator(this, function (_a) {
|
|
1591
|
+
switch (_a.label) {
|
|
1592
|
+
case 0: return [4 /*yield*/, connector(outgoing)];
|
|
1593
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1594
|
+
}
|
|
1595
|
+
});
|
|
1596
|
+
}); }, {
|
|
1597
|
+
clientId: clientId,
|
|
1598
|
+
swarmName: swarmName,
|
|
1599
|
+
agentName: "",
|
|
1600
|
+
})];
|
|
1601
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1602
|
+
}
|
|
1603
|
+
});
|
|
1604
|
+
}); });
|
|
1605
|
+
return function (incoming) {
|
|
1606
|
+
return ContextService.runInContext(function () {
|
|
1607
|
+
return receive(incoming);
|
|
1608
|
+
}, {
|
|
1609
|
+
clientId: clientId,
|
|
1610
|
+
swarmName: swarmName,
|
|
1611
|
+
agentName: "",
|
|
1612
|
+
});
|
|
1613
|
+
};
|
|
1614
|
+
}, {
|
|
1615
|
+
clientId: clientId,
|
|
1616
|
+
swarmName: swarmName,
|
|
1617
|
+
agentName: "",
|
|
1618
|
+
});
|
|
1619
|
+
};
|
|
1620
|
+
this.commitToolOutput = function (content, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1621
|
+
var _this = this;
|
|
1622
|
+
return __generator(this, function (_a) {
|
|
1623
|
+
switch (_a.label) {
|
|
1624
|
+
case 0:
|
|
1625
|
+
this.loggerService.log("sessionPublicService commitToolOutput", {
|
|
1626
|
+
content: content,
|
|
1627
|
+
clientId: clientId,
|
|
1628
|
+
swarmName: swarmName,
|
|
1629
|
+
});
|
|
1630
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1631
|
+
return __generator(this, function (_a) {
|
|
1632
|
+
switch (_a.label) {
|
|
1633
|
+
case 0: return [4 /*yield*/, this.sessionConnectionService.commitToolOutput(content)];
|
|
1634
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
}); }, {
|
|
1638
|
+
clientId: clientId,
|
|
1639
|
+
swarmName: swarmName,
|
|
1640
|
+
agentName: "",
|
|
1641
|
+
})];
|
|
1642
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1643
|
+
}
|
|
1644
|
+
});
|
|
1645
|
+
}); };
|
|
1646
|
+
this.commitSystemMessage = function (message, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1647
|
+
var _this = this;
|
|
1648
|
+
return __generator(this, function (_a) {
|
|
1649
|
+
switch (_a.label) {
|
|
1650
|
+
case 0:
|
|
1651
|
+
this.loggerService.log("sessionPublicService commitSystemMessage", {
|
|
1652
|
+
message: message,
|
|
1653
|
+
clientId: clientId,
|
|
1654
|
+
swarmName: swarmName,
|
|
1655
|
+
});
|
|
1656
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1657
|
+
return __generator(this, function (_a) {
|
|
1658
|
+
switch (_a.label) {
|
|
1659
|
+
case 0: return [4 /*yield*/, this.sessionConnectionService.commitSystemMessage(message)];
|
|
1660
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
}); }, {
|
|
1664
|
+
clientId: clientId,
|
|
1665
|
+
swarmName: swarmName,
|
|
1666
|
+
agentName: "",
|
|
1667
|
+
})];
|
|
1668
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1669
|
+
}
|
|
1670
|
+
});
|
|
1671
|
+
}); };
|
|
1672
|
+
this.dispose = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1673
|
+
var _this = this;
|
|
1674
|
+
return __generator(this, function (_a) {
|
|
1675
|
+
switch (_a.label) {
|
|
1676
|
+
case 0:
|
|
1677
|
+
this.loggerService.log("sessionPublicService dispose", {
|
|
1678
|
+
clientId: clientId,
|
|
1679
|
+
swarmName: swarmName,
|
|
1680
|
+
});
|
|
1681
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1682
|
+
return __generator(this, function (_a) {
|
|
1683
|
+
switch (_a.label) {
|
|
1684
|
+
case 0: return [4 /*yield*/, this.sessionConnectionService.dispose()];
|
|
1685
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1686
|
+
}
|
|
1687
|
+
});
|
|
1688
|
+
}); }, {
|
|
1689
|
+
clientId: clientId,
|
|
1690
|
+
swarmName: swarmName,
|
|
1691
|
+
agentName: "",
|
|
1692
|
+
})];
|
|
1693
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
}); };
|
|
1697
|
+
}
|
|
1698
|
+
return SessionPublicService;
|
|
1699
|
+
}());
|
|
1700
|
+
|
|
1701
|
+
var SwarmPublicService = /** @class */ (function () {
|
|
1702
|
+
function SwarmPublicService() {
|
|
1703
|
+
var _this = this;
|
|
1704
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1705
|
+
this.swarmConnectionService = inject(TYPES.swarmConnectionService);
|
|
1706
|
+
this.waitForOutput = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1707
|
+
var _this = this;
|
|
1708
|
+
return __generator(this, function (_a) {
|
|
1709
|
+
switch (_a.label) {
|
|
1710
|
+
case 0:
|
|
1711
|
+
this.loggerService.log("swarmPublicService waitForOutput", {
|
|
1712
|
+
clientId: clientId,
|
|
1713
|
+
swarmName: swarmName,
|
|
1714
|
+
});
|
|
1715
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1716
|
+
return __generator(this, function (_a) {
|
|
1717
|
+
switch (_a.label) {
|
|
1718
|
+
case 0: return [4 /*yield*/, this.swarmConnectionService.waitForOutput()];
|
|
1719
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1720
|
+
}
|
|
1721
|
+
});
|
|
1722
|
+
}); }, {
|
|
1723
|
+
clientId: clientId,
|
|
1724
|
+
swarmName: swarmName,
|
|
1725
|
+
agentName: "",
|
|
1726
|
+
})];
|
|
1727
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1728
|
+
}
|
|
1729
|
+
});
|
|
1730
|
+
}); };
|
|
1731
|
+
this.getAgentName = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1732
|
+
var _this = this;
|
|
1733
|
+
return __generator(this, function (_a) {
|
|
1734
|
+
switch (_a.label) {
|
|
1735
|
+
case 0:
|
|
1736
|
+
this.loggerService.log("swarmPublicService getAgentName", {
|
|
1737
|
+
clientId: clientId,
|
|
1738
|
+
swarmName: swarmName,
|
|
1739
|
+
});
|
|
1740
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1741
|
+
return __generator(this, function (_a) {
|
|
1742
|
+
switch (_a.label) {
|
|
1743
|
+
case 0: return [4 /*yield*/, this.swarmConnectionService.getAgentName()];
|
|
1744
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1745
|
+
}
|
|
1746
|
+
});
|
|
1747
|
+
}); }, {
|
|
1748
|
+
clientId: clientId,
|
|
1749
|
+
swarmName: swarmName,
|
|
1750
|
+
agentName: "",
|
|
1751
|
+
})];
|
|
1752
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1753
|
+
}
|
|
1754
|
+
});
|
|
1755
|
+
}); };
|
|
1756
|
+
this.getAgent = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1757
|
+
var _this = this;
|
|
1758
|
+
return __generator(this, function (_a) {
|
|
1759
|
+
switch (_a.label) {
|
|
1760
|
+
case 0:
|
|
1761
|
+
this.loggerService.log("swarmPublicService getAgent", {
|
|
1762
|
+
clientId: clientId,
|
|
1763
|
+
swarmName: swarmName,
|
|
1764
|
+
});
|
|
1765
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1766
|
+
return __generator(this, function (_a) {
|
|
1767
|
+
switch (_a.label) {
|
|
1768
|
+
case 0: return [4 /*yield*/, this.swarmConnectionService.getAgent()];
|
|
1769
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1770
|
+
}
|
|
1771
|
+
});
|
|
1772
|
+
}); }, {
|
|
1773
|
+
clientId: clientId,
|
|
1774
|
+
swarmName: swarmName,
|
|
1775
|
+
agentName: "",
|
|
1776
|
+
})];
|
|
1777
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1778
|
+
}
|
|
1779
|
+
});
|
|
1780
|
+
}); };
|
|
1781
|
+
this.setAgentName = function (agentName, clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1782
|
+
var _this = this;
|
|
1783
|
+
return __generator(this, function (_a) {
|
|
1784
|
+
switch (_a.label) {
|
|
1785
|
+
case 0:
|
|
1786
|
+
this.loggerService.log("swarmPublicService setAgentName", {
|
|
1787
|
+
agentName: agentName,
|
|
1788
|
+
clientId: clientId,
|
|
1789
|
+
swarmName: swarmName,
|
|
1790
|
+
});
|
|
1791
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1792
|
+
return __generator(this, function (_a) {
|
|
1793
|
+
switch (_a.label) {
|
|
1794
|
+
case 0: return [4 /*yield*/, this.swarmConnectionService.setAgentName(agentName)];
|
|
1795
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1796
|
+
}
|
|
1797
|
+
});
|
|
1798
|
+
}); }, {
|
|
1799
|
+
clientId: clientId,
|
|
1800
|
+
swarmName: swarmName,
|
|
1801
|
+
agentName: "",
|
|
1802
|
+
})];
|
|
1803
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1804
|
+
}
|
|
1805
|
+
});
|
|
1806
|
+
}); };
|
|
1807
|
+
this.dispose = function (clientId, swarmName) { return __awaiter(_this, void 0, void 0, function () {
|
|
1808
|
+
var _this = this;
|
|
1809
|
+
return __generator(this, function (_a) {
|
|
1810
|
+
switch (_a.label) {
|
|
1811
|
+
case 0:
|
|
1812
|
+
this.loggerService.log("swarmPublicService dispose", {
|
|
1813
|
+
clientId: clientId,
|
|
1814
|
+
swarmName: swarmName,
|
|
1815
|
+
});
|
|
1816
|
+
return [4 /*yield*/, ContextService.runInContext(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1817
|
+
return __generator(this, function (_a) {
|
|
1818
|
+
switch (_a.label) {
|
|
1819
|
+
case 0: return [4 /*yield*/, this.swarmConnectionService.dispose()];
|
|
1820
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1821
|
+
}
|
|
1822
|
+
});
|
|
1823
|
+
}); }, {
|
|
1824
|
+
clientId: clientId,
|
|
1825
|
+
swarmName: swarmName,
|
|
1826
|
+
agentName: "",
|
|
1827
|
+
})];
|
|
1828
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
}); };
|
|
1832
|
+
}
|
|
1833
|
+
return SwarmPublicService;
|
|
1834
|
+
}());
|
|
1835
|
+
|
|
1836
|
+
var AgentValidationService = /** @class */ (function () {
|
|
1837
|
+
function AgentValidationService() {
|
|
1838
|
+
var _this = this;
|
|
1839
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1840
|
+
this.toolValidationService = inject(TYPES.toolValidationService);
|
|
1841
|
+
this.completionValidationService = inject(TYPES.completionValidationService);
|
|
1842
|
+
this._agentMap = new Map();
|
|
1843
|
+
this.addAgent = function (agentName, agentSchema) {
|
|
1844
|
+
_this.loggerService.log("agentValidationService addAgent", {
|
|
1845
|
+
agentName: agentName,
|
|
1846
|
+
agentSchema: agentSchema,
|
|
1847
|
+
});
|
|
1848
|
+
if (_this._agentMap.has(agentName)) {
|
|
1849
|
+
throw new Error("agent-swarm agent ".concat(agentName, " already exist"));
|
|
1850
|
+
}
|
|
1851
|
+
_this._agentMap.set(agentName, agentSchema);
|
|
1852
|
+
};
|
|
1853
|
+
this.validate = memoize(function (_a) {
|
|
1854
|
+
var _b = __read(_a, 1), agentName = _b[0];
|
|
1855
|
+
return agentName;
|
|
1856
|
+
}, function (agentName, source) {
|
|
1857
|
+
var _a;
|
|
1858
|
+
_this.loggerService.log("agentValidationService validate", {
|
|
1859
|
+
agentName: agentName,
|
|
1860
|
+
source: source,
|
|
1861
|
+
});
|
|
1862
|
+
var agent = _this._agentMap.get(agentName);
|
|
1863
|
+
if (!agent) {
|
|
1864
|
+
throw new Error("agent-swarm agent ".concat(agentName, " not found source=").concat(source));
|
|
1865
|
+
}
|
|
1866
|
+
_this.completionValidationService.validate(agent.completion, source);
|
|
1867
|
+
(_a = agent.tools) === null || _a === void 0 ? void 0 : _a.forEach(function (toolName) {
|
|
1868
|
+
_this.toolValidationService.validate(toolName, source);
|
|
1869
|
+
});
|
|
1870
|
+
});
|
|
1871
|
+
}
|
|
1872
|
+
return AgentValidationService;
|
|
1873
|
+
}());
|
|
1874
|
+
|
|
1875
|
+
var CompletionValidationService = /** @class */ (function () {
|
|
1876
|
+
function CompletionValidationService() {
|
|
1877
|
+
var _this = this;
|
|
1878
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1879
|
+
this._completionSet = new Set();
|
|
1880
|
+
this.addCompletion = function (completionName) {
|
|
1881
|
+
_this.loggerService.log("completionValidationService addCompletion", {
|
|
1882
|
+
completionName: completionName,
|
|
1883
|
+
});
|
|
1884
|
+
if (_this._completionSet.has(completionName)) {
|
|
1885
|
+
throw new Error("agent-swarm completion ".concat(completionName, " already exist"));
|
|
1886
|
+
}
|
|
1887
|
+
_this._completionSet.add(completionName);
|
|
1888
|
+
};
|
|
1889
|
+
this.validate = memoize(function (_a) {
|
|
1890
|
+
var _b = __read(_a, 1), completionName = _b[0];
|
|
1891
|
+
return completionName;
|
|
1892
|
+
}, function (completionName, source) {
|
|
1893
|
+
_this.loggerService.log("completionValidationService validate", {
|
|
1894
|
+
completionName: completionName,
|
|
1895
|
+
source: source,
|
|
1896
|
+
});
|
|
1897
|
+
if (!_this._completionSet.has(completionName)) {
|
|
1898
|
+
throw new Error("agent-swarm completion ".concat(completionName, " not found source=").concat(source));
|
|
1899
|
+
}
|
|
1900
|
+
});
|
|
1901
|
+
}
|
|
1902
|
+
return CompletionValidationService;
|
|
1903
|
+
}());
|
|
1904
|
+
|
|
1905
|
+
var SessionValidationService = /** @class */ (function () {
|
|
1906
|
+
function SessionValidationService() {
|
|
1907
|
+
var _this = this;
|
|
1908
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1909
|
+
this._sessionMap = new Map();
|
|
1910
|
+
this.addSession = function (clientId, swarmName) {
|
|
1911
|
+
_this.loggerService.log("sessionValidationService addSession", {
|
|
1912
|
+
clientId: clientId,
|
|
1913
|
+
});
|
|
1914
|
+
if (_this._sessionMap.has(clientId)) {
|
|
1915
|
+
throw new Error("agent-swarm session clientId=".concat(clientId, " already exist"));
|
|
1916
|
+
}
|
|
1917
|
+
_this._sessionMap.set(clientId, swarmName);
|
|
1918
|
+
};
|
|
1919
|
+
this.getSessionList = function () {
|
|
1920
|
+
_this.loggerService.log("sessionValidationService getSessionList");
|
|
1921
|
+
return __spreadArray([], __read(_this._sessionMap.keys()), false);
|
|
1922
|
+
};
|
|
1923
|
+
this.getSwarm = function (clientId) {
|
|
1924
|
+
_this.loggerService.log("sessionValidationService getSwarm", {
|
|
1925
|
+
clientId: clientId,
|
|
1926
|
+
});
|
|
1927
|
+
var session = _this._sessionMap.get(clientId);
|
|
1928
|
+
if (session === undefined) {
|
|
1929
|
+
throw new Error("agent-swarm session clientId=".concat(clientId, " not found"));
|
|
1930
|
+
}
|
|
1931
|
+
return session;
|
|
1932
|
+
};
|
|
1933
|
+
this.validate = function (clientId, source) {
|
|
1934
|
+
_this.loggerService.log("sessionValidationService validate", { clientId: clientId });
|
|
1935
|
+
if (!_this._sessionMap.has(clientId)) {
|
|
1936
|
+
throw new Error("agent-swarm session clientId=".concat(clientId, " not exist source=").concat(source));
|
|
1937
|
+
}
|
|
1938
|
+
};
|
|
1939
|
+
this.removeSession = function (clientId) {
|
|
1940
|
+
_this.loggerService.log("sessionValidationService addSession", {
|
|
1941
|
+
clientId: clientId,
|
|
1942
|
+
});
|
|
1943
|
+
_this._sessionMap.delete(clientId);
|
|
1944
|
+
};
|
|
1945
|
+
}
|
|
1946
|
+
return SessionValidationService;
|
|
1947
|
+
}());
|
|
1948
|
+
|
|
1949
|
+
var SwarmValidationService = /** @class */ (function () {
|
|
1950
|
+
function SwarmValidationService() {
|
|
1951
|
+
var _this = this;
|
|
1952
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
1953
|
+
this.agentValidationService = inject(TYPES.agentValidationService);
|
|
1954
|
+
this._swarmMap = new Map();
|
|
1955
|
+
this.addSwarm = function (swarmName, swarmSchema) {
|
|
1956
|
+
_this.loggerService.log("swarmValidationService addSwarm", {
|
|
1957
|
+
swarmName: swarmName,
|
|
1958
|
+
swarmSchema: swarmSchema,
|
|
1959
|
+
});
|
|
1960
|
+
if (_this._swarmMap.has(swarmName)) {
|
|
1961
|
+
throw new Error("swarm-swarm swarm ".concat(swarmName, " already exist"));
|
|
1962
|
+
}
|
|
1963
|
+
_this._swarmMap.set(swarmName, swarmSchema);
|
|
1964
|
+
};
|
|
1965
|
+
this.getAgentList = function (swarmName) {
|
|
1966
|
+
_this.loggerService.log("swarmValidationService getAgentList", {
|
|
1967
|
+
swarmName: swarmName,
|
|
1968
|
+
});
|
|
1969
|
+
var swarm = _this._swarmMap.get(swarmName);
|
|
1970
|
+
if (!swarm) {
|
|
1971
|
+
throw new Error("agent-swarm swarm ".concat(swarmName, " not found"));
|
|
1972
|
+
}
|
|
1973
|
+
return swarm.agentList;
|
|
1974
|
+
};
|
|
1975
|
+
this.validate = memoize(function (_a) {
|
|
1976
|
+
var _b = __read(_a, 1), swarmName = _b[0];
|
|
1977
|
+
return swarmName;
|
|
1978
|
+
}, function (swarmName, source) {
|
|
1979
|
+
_this.loggerService.log("swarmValidationService validate", {
|
|
1980
|
+
swarmName: swarmName,
|
|
1981
|
+
source: source,
|
|
1982
|
+
});
|
|
1983
|
+
var swarm = _this._swarmMap.get(swarmName);
|
|
1984
|
+
if (!swarm) {
|
|
1985
|
+
throw new Error("agent-swarm swarm ".concat(swarmName, " not found"));
|
|
1986
|
+
}
|
|
1987
|
+
if (!swarm.agentList.includes(swarm.defaultAgent)) {
|
|
1988
|
+
throw new Error("agent-swarm swarm ".concat(swarmName, " default agent not in agent list"));
|
|
1989
|
+
}
|
|
1990
|
+
swarm.agentList.forEach(function (agentName) {
|
|
1991
|
+
return _this.agentValidationService.validate(agentName, source);
|
|
1992
|
+
});
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
return SwarmValidationService;
|
|
1996
|
+
}());
|
|
1997
|
+
|
|
1998
|
+
var ToolValidationService = /** @class */ (function () {
|
|
1999
|
+
function ToolValidationService() {
|
|
2000
|
+
var _this = this;
|
|
2001
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
2002
|
+
this._toolMap = new Map();
|
|
2003
|
+
this.addTool = function (toolName, toolSchema) {
|
|
2004
|
+
_this.loggerService.log("toolValidationService addTool", {
|
|
2005
|
+
toolName: toolName,
|
|
2006
|
+
toolSchema: toolSchema,
|
|
2007
|
+
});
|
|
2008
|
+
if (_this._toolMap.has(toolName)) {
|
|
2009
|
+
throw new Error("agent-swarm tool ".concat(toolName, " already exist"));
|
|
2010
|
+
}
|
|
2011
|
+
_this._toolMap.set(toolName, toolSchema);
|
|
2012
|
+
};
|
|
2013
|
+
this.validate = memoize(function (_a) {
|
|
2014
|
+
var _b = __read(_a, 1), toolName = _b[0];
|
|
2015
|
+
return toolName;
|
|
2016
|
+
}, function (toolName, source) {
|
|
2017
|
+
_this.loggerService.log("toolValidationService validate", {
|
|
2018
|
+
toolName: toolName,
|
|
2019
|
+
source: source,
|
|
2020
|
+
});
|
|
2021
|
+
if (!_this._toolMap.has(toolName)) {
|
|
2022
|
+
throw new Error("agent-swarm tool ".concat(toolName, " not found source=").concat(source));
|
|
2023
|
+
}
|
|
2024
|
+
});
|
|
2025
|
+
}
|
|
2026
|
+
return ToolValidationService;
|
|
2027
|
+
}());
|
|
2028
|
+
|
|
2029
|
+
{
|
|
2030
|
+
provide(TYPES.loggerService, function () { return new LoggerService(); });
|
|
2031
|
+
provide(TYPES.contextService, function () { return new ContextService(); });
|
|
2032
|
+
}
|
|
2033
|
+
{
|
|
2034
|
+
provide(TYPES.agentConnectionService, function () { return new AgentConnectionService(); });
|
|
2035
|
+
provide(TYPES.historyConnectionService, function () { return new HistoryConnectionService(); });
|
|
2036
|
+
provide(TYPES.swarmConnectionService, function () { return new SwarmConnectionService(); });
|
|
2037
|
+
provide(TYPES.sessionConnectionService, function () { return new SessionConnectionService(); });
|
|
2038
|
+
}
|
|
2039
|
+
{
|
|
2040
|
+
provide(TYPES.agentSchemaService, function () { return new AgentSchemaService(); });
|
|
2041
|
+
provide(TYPES.toolSchemaService, function () { return new ToolSchemaService(); });
|
|
2042
|
+
provide(TYPES.swarmSchemaService, function () { return new SwarmSchemaService(); });
|
|
2043
|
+
provide(TYPES.completionSchemaService, function () { return new CompletionSchemaService(); });
|
|
2044
|
+
}
|
|
2045
|
+
{
|
|
2046
|
+
provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
|
|
2047
|
+
provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
|
|
2048
|
+
provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
|
|
2049
|
+
provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
|
|
2050
|
+
}
|
|
2051
|
+
{
|
|
2052
|
+
provide(TYPES.agentPublicService, function () { return new AgentPublicService(); });
|
|
2053
|
+
provide(TYPES.historyPublicService, function () { return new HistoryPublicService(); });
|
|
2054
|
+
provide(TYPES.sessionPublicService, function () { return new SessionPublicService(); });
|
|
2055
|
+
provide(TYPES.swarmPublicService, function () { return new SwarmPublicService(); });
|
|
2056
|
+
}
|
|
2057
|
+
{
|
|
2058
|
+
provide(TYPES.agentValidationService, function () { return new AgentValidationService(); });
|
|
2059
|
+
provide(TYPES.completionValidationService, function () { return new CompletionValidationService(); });
|
|
2060
|
+
provide(TYPES.sessionValidationService, function () { return new SessionValidationService(); });
|
|
2061
|
+
provide(TYPES.swarmValidationService, function () { return new SwarmValidationService(); });
|
|
2062
|
+
provide(TYPES.toolValidationService, function () { return new ToolValidationService(); });
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
var baseServices = {
|
|
2066
|
+
loggerService: inject(TYPES.loggerService),
|
|
2067
|
+
contextService: inject(TYPES.contextService),
|
|
2068
|
+
};
|
|
2069
|
+
var connectionServices = {
|
|
2070
|
+
agentConnectionService: inject(TYPES.agentConnectionService),
|
|
2071
|
+
historyConnectionService: inject(TYPES.historyConnectionService),
|
|
2072
|
+
swarmConnectionService: inject(TYPES.swarmConnectionService),
|
|
2073
|
+
sessionConnectionService: inject(TYPES.sessionConnectionService),
|
|
2074
|
+
};
|
|
2075
|
+
var specServices = {
|
|
2076
|
+
agentSchemaService: inject(TYPES.agentSchemaService),
|
|
2077
|
+
toolSchemaService: inject(TYPES.toolSchemaService),
|
|
2078
|
+
swarmSchemaService: inject(TYPES.swarmSchemaService),
|
|
2079
|
+
completionSchemaService: inject(TYPES.completionSchemaService),
|
|
2080
|
+
};
|
|
2081
|
+
var publicServices = {
|
|
2082
|
+
agentPublicService: inject(TYPES.agentPublicService),
|
|
2083
|
+
historyPublicService: inject(TYPES.historyPublicService),
|
|
2084
|
+
sessionPublicService: inject(TYPES.sessionPublicService),
|
|
2085
|
+
swarmPublicService: inject(TYPES.swarmPublicService),
|
|
2086
|
+
};
|
|
2087
|
+
var validationServices = {
|
|
2088
|
+
agentValidationService: inject(TYPES.agentValidationService),
|
|
2089
|
+
toolValidationService: inject(TYPES.toolValidationService),
|
|
2090
|
+
sessionValidationService: inject(TYPES.sessionValidationService),
|
|
2091
|
+
swarmValidationService: inject(TYPES.swarmValidationService),
|
|
2092
|
+
completionValidationService: inject(TYPES.completionValidationService),
|
|
2093
|
+
};
|
|
2094
|
+
var swarm = __assign(__assign(__assign(__assign(__assign({}, baseServices), connectionServices), specServices), publicServices), validationServices);
|
|
2095
|
+
init();
|
|
2096
|
+
|
|
2097
|
+
var addAgent = function (agentSchema) {
|
|
2098
|
+
swarm.agentValidationService.addAgent(agentSchema.agentName, agentSchema);
|
|
2099
|
+
swarm.agentSchemaService.register(agentSchema.agentName, agentSchema);
|
|
2100
|
+
return agentSchema.agentName;
|
|
2101
|
+
};
|
|
2102
|
+
|
|
2103
|
+
var addCompletion = function (completionSchema) {
|
|
2104
|
+
swarm.completionValidationService.addCompletion(completionSchema.completionName);
|
|
2105
|
+
swarm.completionSchemaService.register(completionSchema.completionName, completionSchema);
|
|
2106
|
+
return completionSchema.completionName;
|
|
2107
|
+
};
|
|
2108
|
+
|
|
2109
|
+
var addSwarm = function (swarmSchema) {
|
|
2110
|
+
swarm.swarmValidationService.addSwarm(swarmSchema.swarmName, swarmSchema);
|
|
2111
|
+
swarm.swarmSchemaService.register(swarmSchema.swarmName, swarmSchema);
|
|
2112
|
+
return swarmSchema.swarmName;
|
|
2113
|
+
};
|
|
2114
|
+
|
|
2115
|
+
var addTool = function (toolSchema) {
|
|
2116
|
+
swarm.toolValidationService.addTool(toolSchema.toolName, toolSchema);
|
|
2117
|
+
swarm.toolSchemaService.register(toolSchema.toolName, toolSchema);
|
|
2118
|
+
return toolSchema.toolName;
|
|
2119
|
+
};
|
|
2120
|
+
|
|
2121
|
+
var makeConnection = function (connector, clientId, swarmName) {
|
|
2122
|
+
swarm.swarmValidationService.validate(swarmName, "makeConnection");
|
|
2123
|
+
swarm.sessionValidationService.addSession(clientId, swarmName);
|
|
2124
|
+
var send = swarm.sessionPublicService.connect(connector, clientId, swarmName);
|
|
2125
|
+
return queued(function (outgoing) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2126
|
+
return __generator(this, function (_a) {
|
|
2127
|
+
switch (_a.label) {
|
|
2128
|
+
case 0:
|
|
2129
|
+
swarm.sessionValidationService.validate(clientId, "makeConnection");
|
|
2130
|
+
return [4 /*yield*/, send(outgoing)];
|
|
2131
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2132
|
+
}
|
|
2133
|
+
});
|
|
2134
|
+
}); });
|
|
2135
|
+
};
|
|
2136
|
+
|
|
2137
|
+
var changeAgent = function (agentName, clientId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2138
|
+
var swarmName;
|
|
2139
|
+
return __generator(this, function (_a) {
|
|
2140
|
+
switch (_a.label) {
|
|
2141
|
+
case 0:
|
|
2142
|
+
swarm.sessionValidationService.validate(clientId, "changeAgent");
|
|
2143
|
+
swarm.agentValidationService.validate(agentName, "changeAgent");
|
|
2144
|
+
swarmName = swarm.sessionValidationService.getSwarm(clientId);
|
|
2145
|
+
return [4 /*yield*/, swarm.swarmPublicService.setAgentName(agentName, clientId, swarmName)];
|
|
2146
|
+
case 1:
|
|
2147
|
+
_a.sent();
|
|
2148
|
+
return [2 /*return*/];
|
|
2149
|
+
}
|
|
2150
|
+
});
|
|
2151
|
+
}); };
|
|
2152
|
+
|
|
2153
|
+
var disposeConnection = function (clientId, swarmName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2154
|
+
return __generator(this, function (_a) {
|
|
2155
|
+
switch (_a.label) {
|
|
2156
|
+
case 0:
|
|
2157
|
+
swarm.swarmValidationService.validate(swarmName, "disposeConnection");
|
|
2158
|
+
swarm.sessionValidationService.removeSession(clientId);
|
|
2159
|
+
return [4 /*yield*/, swarm.sessionPublicService.dispose(clientId, swarmName)];
|
|
2160
|
+
case 1:
|
|
2161
|
+
_a.sent();
|
|
2162
|
+
return [4 /*yield*/, swarm.swarmPublicService.dispose(clientId, swarmName)];
|
|
2163
|
+
case 2:
|
|
2164
|
+
_a.sent();
|
|
2165
|
+
return [4 /*yield*/, Promise.all([
|
|
2166
|
+
swarm.swarmValidationService
|
|
2167
|
+
.getAgentList(swarmName)
|
|
2168
|
+
.map(function (agentName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2169
|
+
return __generator(this, function (_a) {
|
|
2170
|
+
switch (_a.label) {
|
|
2171
|
+
case 0: return [4 /*yield*/, swarm.agentPublicService.dispose(clientId, agentName)];
|
|
2172
|
+
case 1:
|
|
2173
|
+
_a.sent();
|
|
2174
|
+
return [4 /*yield*/, swarm.historyPublicService.dispose(clientId, agentName)];
|
|
2175
|
+
case 2:
|
|
2176
|
+
_a.sent();
|
|
2177
|
+
return [2 /*return*/];
|
|
2178
|
+
}
|
|
2179
|
+
});
|
|
2180
|
+
}); }),
|
|
2181
|
+
])];
|
|
2182
|
+
case 3:
|
|
2183
|
+
_a.sent();
|
|
2184
|
+
return [2 /*return*/];
|
|
2185
|
+
}
|
|
2186
|
+
});
|
|
2187
|
+
}); };
|
|
2188
|
+
|
|
2189
|
+
var COMPLETE_TTL = 15 * 60 * 1000;
|
|
2190
|
+
var COMPLETE_GC = 60 * 1000;
|
|
2191
|
+
var createComplete = ttl(function (clientId, swarmName) {
|
|
2192
|
+
return queued(function (content) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2193
|
+
var result;
|
|
2194
|
+
return __generator(this, function (_a) {
|
|
2195
|
+
switch (_a.label) {
|
|
2196
|
+
case 0:
|
|
2197
|
+
swarm.swarmValidationService.validate(swarmName, "complete");
|
|
2198
|
+
swarm.sessionValidationService.addSession(clientId, swarmName);
|
|
2199
|
+
return [4 /*yield*/, swarm.sessionPublicService.execute(content, clientId, swarmName)];
|
|
2200
|
+
case 1:
|
|
2201
|
+
result = _a.sent();
|
|
2202
|
+
return [4 /*yield*/, disposeConnection(clientId, swarmName)];
|
|
2203
|
+
case 2:
|
|
2204
|
+
_a.sent();
|
|
2205
|
+
return [2 /*return*/, result];
|
|
2206
|
+
}
|
|
2207
|
+
});
|
|
2208
|
+
}); });
|
|
2209
|
+
}, {
|
|
2210
|
+
key: function (_a) {
|
|
2211
|
+
var _b = __read(_a, 2), clientId = _b[0], swarmName = _b[1];
|
|
2212
|
+
return "".concat(clientId, "-").concat(swarmName);
|
|
2213
|
+
},
|
|
2214
|
+
timeout: COMPLETE_TTL,
|
|
2215
|
+
});
|
|
2216
|
+
var createGc = singleshot(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2217
|
+
return __generator(this, function (_a) {
|
|
2218
|
+
setInterval(createComplete.gc, COMPLETE_GC);
|
|
2219
|
+
return [2 /*return*/];
|
|
2220
|
+
});
|
|
2221
|
+
}); });
|
|
2222
|
+
var complete = function (content, clientId, swarmName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2223
|
+
var run;
|
|
2224
|
+
return __generator(this, function (_a) {
|
|
2225
|
+
switch (_a.label) {
|
|
2226
|
+
case 0: return [4 /*yield*/, createComplete(clientId, swarmName)];
|
|
2227
|
+
case 1:
|
|
2228
|
+
run = _a.sent();
|
|
2229
|
+
createGc();
|
|
2230
|
+
return [4 /*yield*/, run(content)];
|
|
2231
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
2232
|
+
}
|
|
2233
|
+
});
|
|
2234
|
+
}); };
|
|
2235
|
+
|
|
2236
|
+
var session = function (clientId, swarmName) {
|
|
2237
|
+
swarm.swarmValidationService.validate(swarmName, "session");
|
|
2238
|
+
swarm.sessionValidationService.addSession(clientId, swarmName);
|
|
2239
|
+
return {
|
|
2240
|
+
complete: queued(function (content) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2241
|
+
return __generator(this, function (_a) {
|
|
2242
|
+
switch (_a.label) {
|
|
2243
|
+
case 0:
|
|
2244
|
+
swarm.sessionValidationService.validate(clientId, "session");
|
|
2245
|
+
return [4 /*yield*/, swarm.sessionPublicService.execute(content, clientId, swarmName)];
|
|
2246
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2247
|
+
}
|
|
2248
|
+
});
|
|
2249
|
+
}); }),
|
|
2250
|
+
dispose: function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
2251
|
+
return __generator(this, function (_a) {
|
|
2252
|
+
switch (_a.label) {
|
|
2253
|
+
case 0: return [4 /*yield*/, disposeConnection(clientId, swarmName)];
|
|
2254
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2255
|
+
}
|
|
2256
|
+
});
|
|
2257
|
+
}); },
|
|
2258
|
+
};
|
|
2259
|
+
};
|
|
2260
|
+
|
|
2261
|
+
var getRawHistory = function (clientId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2262
|
+
var swarmName, agentName, history;
|
|
2263
|
+
return __generator(this, function (_a) {
|
|
2264
|
+
switch (_a.label) {
|
|
2265
|
+
case 0:
|
|
2266
|
+
swarm.sessionValidationService.validate(clientId, "getRawHistory");
|
|
2267
|
+
swarmName = swarm.sessionValidationService.getSwarm(clientId);
|
|
2268
|
+
swarm.swarmValidationService.validate(swarmName, "getRawHistory");
|
|
2269
|
+
return [4 /*yield*/, swarm.swarmPublicService.getAgentName(clientId, swarmName)];
|
|
2270
|
+
case 1:
|
|
2271
|
+
agentName = _a.sent();
|
|
2272
|
+
return [4 /*yield*/, swarm.historyPublicService.toArrayForRaw(clientId, agentName)];
|
|
2273
|
+
case 2:
|
|
2274
|
+
history = _a.sent();
|
|
2275
|
+
return [2 /*return*/, __spreadArray([], __read(history), false)];
|
|
2276
|
+
}
|
|
2277
|
+
});
|
|
2278
|
+
}); };
|
|
2279
|
+
|
|
2280
|
+
var getAgentHistory = function (clientId, agentName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2281
|
+
var prompt, history;
|
|
2282
|
+
return __generator(this, function (_a) {
|
|
2283
|
+
switch (_a.label) {
|
|
2284
|
+
case 0:
|
|
2285
|
+
swarm.agentValidationService.validate(agentName, "getAgentHistory");
|
|
2286
|
+
prompt = swarm.agentSchemaService.get(agentName).prompt;
|
|
2287
|
+
return [4 /*yield*/, swarm.historyPublicService.toArrayForAgent(prompt, clientId, agentName)];
|
|
2288
|
+
case 1:
|
|
2289
|
+
history = _a.sent();
|
|
2290
|
+
return [2 /*return*/, __spreadArray([], __read(history), false)];
|
|
2291
|
+
}
|
|
2292
|
+
});
|
|
2293
|
+
}); };
|
|
2294
|
+
|
|
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
|
+
};
|
|
2301
|
+
|
|
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
|
+
};
|
|
2308
|
+
|
|
2309
|
+
var execute = function (content, clientId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2310
|
+
var swarmName;
|
|
2311
|
+
return __generator(this, function (_a) {
|
|
2312
|
+
switch (_a.label) {
|
|
2313
|
+
case 0:
|
|
2314
|
+
swarm.sessionValidationService.validate(clientId, "execute");
|
|
2315
|
+
swarmName = swarm.sessionValidationService.getSwarm(clientId);
|
|
2316
|
+
swarm.swarmValidationService.validate(swarmName, "execute");
|
|
2317
|
+
return [4 /*yield*/, swarm.sessionPublicService.execute(content, clientId, swarmName)];
|
|
2318
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2319
|
+
}
|
|
2320
|
+
});
|
|
2321
|
+
}); };
|
|
2322
|
+
|
|
2323
|
+
export { ContextService, addAgent, addCompletion, addSwarm, addTool, changeAgent, commitSystemMessage, commitToolOutput, complete, disposeConnection, execute, getAgentHistory, getRawHistory, makeConnection, session, setConfig, swarm };
|