@jterrazz/intelligence 1.6.1 → 3.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/README.md +69 -165
- package/dist/index.cjs +649 -1625
- package/dist/index.d.ts +5 -12
- package/dist/index.js +5 -12
- package/dist/index.js.map +1 -1
- package/dist/middleware/__tests__/logging.middleware.test.js +390 -0
- package/dist/middleware/__tests__/logging.middleware.test.js.map +1 -0
- package/dist/middleware/logging.middleware.d.ts +21 -0
- package/dist/middleware/logging.middleware.js +296 -0
- package/dist/middleware/logging.middleware.js.map +1 -0
- package/dist/parsing/__tests__/create-schema-prompt.test.js +53 -0
- package/dist/parsing/__tests__/create-schema-prompt.test.js.map +1 -0
- package/dist/parsing/__tests__/parse-object.test.d.ts +1 -0
- package/dist/parsing/__tests__/parse-object.test.js +193 -0
- package/dist/parsing/__tests__/parse-object.test.js.map +1 -0
- package/dist/parsing/__tests__/parse-text.test.d.ts +1 -0
- package/dist/parsing/__tests__/parse-text.test.js +159 -0
- package/dist/parsing/__tests__/parse-text.test.js.map +1 -0
- package/dist/parsing/create-schema-prompt.d.ts +28 -0
- package/dist/parsing/create-schema-prompt.js +42 -0
- package/dist/parsing/create-schema-prompt.js.map +1 -0
- package/dist/parsing/parse-object.d.ts +33 -0
- package/dist/parsing/parse-object.js +360 -0
- package/dist/parsing/parse-object.js.map +1 -0
- package/dist/parsing/parse-text.d.ts +14 -0
- package/dist/parsing/parse-text.js +80 -0
- package/dist/parsing/parse-text.js.map +1 -0
- package/dist/providers/openrouter.provider.d.ts +36 -0
- package/dist/providers/openrouter.provider.js +58 -0
- package/dist/providers/openrouter.provider.js.map +1 -0
- package/package.json +15 -14
- package/dist/adapters/agents/autonomous-agent.adapter.d.ts +0 -30
- package/dist/adapters/agents/autonomous-agent.adapter.js +0 -400
- package/dist/adapters/agents/autonomous-agent.adapter.js.map +0 -1
- package/dist/adapters/agents/basic-agent.adapter.d.ts +0 -27
- package/dist/adapters/agents/basic-agent.adapter.js +0 -339
- package/dist/adapters/agents/basic-agent.adapter.js.map +0 -1
- package/dist/adapters/agents/retryable-agent.adapter.d.ts +0 -20
- package/dist/adapters/agents/retryable-agent.adapter.js +0 -263
- package/dist/adapters/agents/retryable-agent.adapter.js.map +0 -1
- package/dist/adapters/models/openrouter-model.adapter.d.ts +0 -38
- package/dist/adapters/models/openrouter-model.adapter.js +0 -89
- package/dist/adapters/models/openrouter-model.adapter.js.map +0 -1
- package/dist/adapters/prompts/__tests__/__snapshots__/presets.test.ts.snap +0 -120
- package/dist/adapters/prompts/__tests__/presets.test.js +0 -31
- package/dist/adapters/prompts/__tests__/presets.test.js.map +0 -1
- package/dist/adapters/prompts/library/categories/domain.d.ts +0 -11
- package/dist/adapters/prompts/library/categories/domain.js +0 -12
- package/dist/adapters/prompts/library/categories/domain.js.map +0 -1
- package/dist/adapters/prompts/library/categories/format.d.ts +0 -10
- package/dist/adapters/prompts/library/categories/format.js +0 -11
- package/dist/adapters/prompts/library/categories/format.js.map +0 -1
- package/dist/adapters/prompts/library/categories/foundations.d.ts +0 -12
- package/dist/adapters/prompts/library/categories/foundations.js +0 -13
- package/dist/adapters/prompts/library/categories/foundations.js.map +0 -1
- package/dist/adapters/prompts/library/categories/language.d.ts +0 -11
- package/dist/adapters/prompts/library/categories/language.js +0 -12
- package/dist/adapters/prompts/library/categories/language.js.map +0 -1
- package/dist/adapters/prompts/library/categories/persona.d.ts +0 -13
- package/dist/adapters/prompts/library/categories/persona.js +0 -14
- package/dist/adapters/prompts/library/categories/persona.js.map +0 -1
- package/dist/adapters/prompts/library/categories/response.d.ts +0 -9
- package/dist/adapters/prompts/library/categories/response.js +0 -10
- package/dist/adapters/prompts/library/categories/response.js.map +0 -1
- package/dist/adapters/prompts/library/categories/tone.d.ts +0 -9
- package/dist/adapters/prompts/library/categories/tone.js +0 -10
- package/dist/adapters/prompts/library/categories/tone.js.map +0 -1
- package/dist/adapters/prompts/library/categories/verbosity.d.ts +0 -8
- package/dist/adapters/prompts/library/categories/verbosity.js +0 -9
- package/dist/adapters/prompts/library/categories/verbosity.js.map +0 -1
- package/dist/adapters/prompts/library/index.d.ts +0 -68
- package/dist/adapters/prompts/library/index.js +0 -26
- package/dist/adapters/prompts/library/index.js.map +0 -1
- package/dist/adapters/prompts/library/presets.d.ts +0 -17
- package/dist/adapters/prompts/library/presets.js +0 -45
- package/dist/adapters/prompts/library/presets.js.map +0 -1
- package/dist/adapters/prompts/system-prompt.adapter.d.ts +0 -9
- package/dist/adapters/prompts/system-prompt.adapter.js +0 -57
- package/dist/adapters/prompts/system-prompt.adapter.js.map +0 -1
- package/dist/adapters/prompts/user-prompt.adapter.d.ts +0 -9
- package/dist/adapters/prompts/user-prompt.adapter.js +0 -57
- package/dist/adapters/prompts/user-prompt.adapter.js.map +0 -1
- package/dist/adapters/tools/safe-tool.adapter.d.ts +0 -27
- package/dist/adapters/tools/safe-tool.adapter.js +0 -283
- package/dist/adapters/tools/safe-tool.adapter.js.map +0 -1
- package/dist/adapters/utils/__tests__/ai-response-parser.test.js +0 -289
- package/dist/adapters/utils/__tests__/ai-response-parser.test.js.map +0 -1
- package/dist/adapters/utils/ai-response-parser-error.d.ts +0 -8
- package/dist/adapters/utils/ai-response-parser-error.js +0 -136
- package/dist/adapters/utils/ai-response-parser-error.js.map +0 -1
- package/dist/adapters/utils/ai-response-parser.d.ts +0 -60
- package/dist/adapters/utils/ai-response-parser.js +0 -347
- package/dist/adapters/utils/ai-response-parser.js.map +0 -1
- package/dist/ports/agent.port.d.ts +0 -17
- package/dist/ports/agent.port.js +0 -7
- package/dist/ports/agent.port.js.map +0 -1
- package/dist/ports/model.port.d.ts +0 -10
- package/dist/ports/model.port.js +0 -5
- package/dist/ports/model.port.js.map +0 -1
- package/dist/ports/prompt.port.d.ts +0 -9
- package/dist/ports/prompt.port.js +0 -5
- package/dist/ports/prompt.port.js.map +0 -1
- package/dist/ports/tool.port.d.ts +0 -11
- package/dist/ports/tool.port.js +0 -5
- package/dist/ports/tool.port.js.map +0 -1
- /package/dist/{adapters/prompts/__tests__/presets.test.d.ts → middleware/__tests__/logging.middleware.test.d.ts} +0 -0
- /package/dist/{adapters/utils/__tests__/ai-response-parser.test.d.ts → parsing/__tests__/create-schema-prompt.test.d.ts} +0 -0
|
@@ -1,339 +0,0 @@
|
|
|
1
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
-
try {
|
|
3
|
-
var info = gen[key](arg);
|
|
4
|
-
var value = info.value;
|
|
5
|
-
} catch (error) {
|
|
6
|
-
reject(error);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
if (info.done) {
|
|
10
|
-
resolve(value);
|
|
11
|
-
} else {
|
|
12
|
-
Promise.resolve(value).then(_next, _throw);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _async_to_generator(fn) {
|
|
16
|
-
return function() {
|
|
17
|
-
var self = this, args = arguments;
|
|
18
|
-
return new Promise(function(resolve, reject) {
|
|
19
|
-
var gen = fn.apply(self, args);
|
|
20
|
-
function _next(value) {
|
|
21
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
-
}
|
|
23
|
-
function _throw(err) {
|
|
24
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
-
}
|
|
26
|
-
_next(undefined);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function _class_call_check(instance, Constructor) {
|
|
31
|
-
if (!(instance instanceof Constructor)) {
|
|
32
|
-
throw new TypeError("Cannot call a class as a function");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function _defineProperties(target, props) {
|
|
36
|
-
for(var i = 0; i < props.length; i++){
|
|
37
|
-
var descriptor = props[i];
|
|
38
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
39
|
-
descriptor.configurable = true;
|
|
40
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
41
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function _create_class(Constructor, protoProps, staticProps) {
|
|
45
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
46
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
47
|
-
return Constructor;
|
|
48
|
-
}
|
|
49
|
-
function _define_property(obj, key, value) {
|
|
50
|
-
if (key in obj) {
|
|
51
|
-
Object.defineProperty(obj, key, {
|
|
52
|
-
value: value,
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true
|
|
56
|
-
});
|
|
57
|
-
} else {
|
|
58
|
-
obj[key] = value;
|
|
59
|
-
}
|
|
60
|
-
return obj;
|
|
61
|
-
}
|
|
62
|
-
function _instanceof(left, right) {
|
|
63
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
64
|
-
return !!right[Symbol.hasInstance](left);
|
|
65
|
-
} else {
|
|
66
|
-
return left instanceof right;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function _ts_generator(thisArg, body) {
|
|
70
|
-
var f, y, t, _ = {
|
|
71
|
-
label: 0,
|
|
72
|
-
sent: function() {
|
|
73
|
-
if (t[0] & 1) throw t[1];
|
|
74
|
-
return t[1];
|
|
75
|
-
},
|
|
76
|
-
trys: [],
|
|
77
|
-
ops: []
|
|
78
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
79
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
80
|
-
return this;
|
|
81
|
-
}), g;
|
|
82
|
-
function verb(n) {
|
|
83
|
-
return function(v) {
|
|
84
|
-
return step([
|
|
85
|
-
n,
|
|
86
|
-
v
|
|
87
|
-
]);
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function step(op) {
|
|
91
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
92
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
93
|
-
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;
|
|
94
|
-
if (y = 0, t) op = [
|
|
95
|
-
op[0] & 2,
|
|
96
|
-
t.value
|
|
97
|
-
];
|
|
98
|
-
switch(op[0]){
|
|
99
|
-
case 0:
|
|
100
|
-
case 1:
|
|
101
|
-
t = op;
|
|
102
|
-
break;
|
|
103
|
-
case 4:
|
|
104
|
-
_.label++;
|
|
105
|
-
return {
|
|
106
|
-
value: op[1],
|
|
107
|
-
done: false
|
|
108
|
-
};
|
|
109
|
-
case 5:
|
|
110
|
-
_.label++;
|
|
111
|
-
y = op[1];
|
|
112
|
-
op = [
|
|
113
|
-
0
|
|
114
|
-
];
|
|
115
|
-
continue;
|
|
116
|
-
case 7:
|
|
117
|
-
op = _.ops.pop();
|
|
118
|
-
_.trys.pop();
|
|
119
|
-
continue;
|
|
120
|
-
default:
|
|
121
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
122
|
-
_ = 0;
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
126
|
-
_.label = op[1];
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
130
|
-
_.label = t[1];
|
|
131
|
-
t = op;
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
if (t && _.label < t[2]) {
|
|
135
|
-
_.label = t[2];
|
|
136
|
-
_.ops.push(op);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
if (t[2]) _.ops.pop();
|
|
140
|
-
_.trys.pop();
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
op = body.call(thisArg, _);
|
|
144
|
-
} catch (e) {
|
|
145
|
-
op = [
|
|
146
|
-
6,
|
|
147
|
-
e
|
|
148
|
-
];
|
|
149
|
-
y = 0;
|
|
150
|
-
} finally{
|
|
151
|
-
f = t = 0;
|
|
152
|
-
}
|
|
153
|
-
if (op[0] & 5) throw op[1];
|
|
154
|
-
return {
|
|
155
|
-
value: op[0] ? op[1] : void 0,
|
|
156
|
-
done: true
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
import { z } from 'zod/v4';
|
|
161
|
-
import { AIResponseParser } from '../utils/ai-response-parser.js';
|
|
162
|
-
/**
|
|
163
|
-
* A basic agent for direct, one-shot interactions with a model.
|
|
164
|
-
* It supports optional response parsing against a Zod schema but does not use tools.
|
|
165
|
-
* @template TOutput - The TypeScript type of the output
|
|
166
|
-
*/ export var BasicAgentAdapter = /*#__PURE__*/ function() {
|
|
167
|
-
"use strict";
|
|
168
|
-
function BasicAgentAdapter(name, options) {
|
|
169
|
-
_class_call_check(this, BasicAgentAdapter);
|
|
170
|
-
_define_property(this, "name", void 0);
|
|
171
|
-
_define_property(this, "options", void 0);
|
|
172
|
-
this.name = name;
|
|
173
|
-
this.options = options;
|
|
174
|
-
}
|
|
175
|
-
_create_class(BasicAgentAdapter, [
|
|
176
|
-
{
|
|
177
|
-
key: "run",
|
|
178
|
-
value: function run(input) {
|
|
179
|
-
return _async_to_generator(function() {
|
|
180
|
-
var _this_options_logger, content, _this_options_logger1, parsedResponse, _this_options_logger2, error, _this_options_logger3;
|
|
181
|
-
return _ts_generator(this, function(_state) {
|
|
182
|
-
switch(_state.label){
|
|
183
|
-
case 0:
|
|
184
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug('Starting query execution', {
|
|
185
|
-
agent: this.name
|
|
186
|
-
});
|
|
187
|
-
_state.label = 1;
|
|
188
|
-
case 1:
|
|
189
|
-
_state.trys.push([
|
|
190
|
-
1,
|
|
191
|
-
3,
|
|
192
|
-
,
|
|
193
|
-
4
|
|
194
|
-
]);
|
|
195
|
-
return [
|
|
196
|
-
4,
|
|
197
|
-
this.invokeModel(input)
|
|
198
|
-
];
|
|
199
|
-
case 2:
|
|
200
|
-
content = _state.sent();
|
|
201
|
-
if (this.options.schema) {
|
|
202
|
-
;
|
|
203
|
-
parsedResponse = this.parseResponse(content, this.options.schema);
|
|
204
|
-
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug('Execution finished and response parsed', {
|
|
205
|
-
agent: this.name
|
|
206
|
-
});
|
|
207
|
-
return [
|
|
208
|
-
2,
|
|
209
|
-
parsedResponse
|
|
210
|
-
];
|
|
211
|
-
} else {
|
|
212
|
-
;
|
|
213
|
-
(_this_options_logger2 = this.options.logger) === null || _this_options_logger2 === void 0 ? void 0 : _this_options_logger2.debug('Execution finished', {
|
|
214
|
-
agent: this.name
|
|
215
|
-
});
|
|
216
|
-
// When no schema is provided, we assume TOutput is string (default), so content is the result
|
|
217
|
-
return [
|
|
218
|
-
2,
|
|
219
|
-
content
|
|
220
|
-
];
|
|
221
|
-
}
|
|
222
|
-
return [
|
|
223
|
-
3,
|
|
224
|
-
4
|
|
225
|
-
];
|
|
226
|
-
case 3:
|
|
227
|
-
error = _state.sent();
|
|
228
|
-
(_this_options_logger3 = this.options.logger) === null || _this_options_logger3 === void 0 ? void 0 : _this_options_logger3.error('Execution failed', {
|
|
229
|
-
agent: this.name,
|
|
230
|
-
error: _instanceof(error, Error) ? error.message : 'Unknown error'
|
|
231
|
-
});
|
|
232
|
-
return [
|
|
233
|
-
2,
|
|
234
|
-
null
|
|
235
|
-
];
|
|
236
|
-
case 4:
|
|
237
|
-
return [
|
|
238
|
-
2
|
|
239
|
-
];
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
}).call(this);
|
|
243
|
-
}
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
key: "invokeModel",
|
|
247
|
-
value: function invokeModel(input) {
|
|
248
|
-
return _async_to_generator(function() {
|
|
249
|
-
var _this_options_logger, userInput, systemMessage, jsonSchema, isPrimitiveType, messages, _this_options_logger1, response, content;
|
|
250
|
-
return _ts_generator(this, function(_state) {
|
|
251
|
-
switch(_state.label){
|
|
252
|
-
case 0:
|
|
253
|
-
userInput = this.resolveUserInput(input);
|
|
254
|
-
systemMessage = this.options.systemPrompt.generate();
|
|
255
|
-
// Add schema definition to system prompt if schema is provided
|
|
256
|
-
if (this.options.schema) {
|
|
257
|
-
jsonSchema = z.toJSONSchema(this.options.schema);
|
|
258
|
-
isPrimitiveType = [
|
|
259
|
-
'boolean',
|
|
260
|
-
'integer',
|
|
261
|
-
'number',
|
|
262
|
-
'string'
|
|
263
|
-
].includes(jsonSchema.type);
|
|
264
|
-
if (isPrimitiveType) {
|
|
265
|
-
systemMessage += "\n\n<OUTPUT_FORMAT>\nYou must respond with a ".concat(jsonSchema.type, " value that matches this schema:\n\n```json\n").concat(JSON.stringify(jsonSchema, null, 2), "\n```\n\nYour response should be only the ").concat(jsonSchema.type, " value, without any JSON wrapping or additional text.\n</OUTPUT_FORMAT>");
|
|
266
|
-
} else {
|
|
267
|
-
systemMessage += "\n\n<OUTPUT_FORMAT>\nYou must respond with valid JSON that matches this JSON schema description:\n\n```json\n".concat(JSON.stringify(jsonSchema, null, 2), "\n```\n\nYour response must be parseable JSON that validates against this schema. Do not include any text outside the JSON.\n</OUTPUT_FORMAT>");
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
messages = [
|
|
271
|
-
{
|
|
272
|
-
content: systemMessage,
|
|
273
|
-
role: 'system'
|
|
274
|
-
},
|
|
275
|
-
{
|
|
276
|
-
content: userInput,
|
|
277
|
-
role: 'user'
|
|
278
|
-
}
|
|
279
|
-
];
|
|
280
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug('Invoking model...', {
|
|
281
|
-
agent: this.name,
|
|
282
|
-
hasSchema: !!this.options.schema
|
|
283
|
-
});
|
|
284
|
-
if (this.options.verbose) {
|
|
285
|
-
;
|
|
286
|
-
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug('Sending messages to model...', {
|
|
287
|
-
agent: this.name,
|
|
288
|
-
messages: messages
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
return [
|
|
292
|
-
4,
|
|
293
|
-
this.options.model.getModel().invoke(messages)
|
|
294
|
-
];
|
|
295
|
-
case 1:
|
|
296
|
-
response = _state.sent();
|
|
297
|
-
content = response.content;
|
|
298
|
-
if (typeof content !== 'string') {
|
|
299
|
-
throw new Error('Model returned a non-string content type.');
|
|
300
|
-
}
|
|
301
|
-
return [
|
|
302
|
-
2,
|
|
303
|
-
content
|
|
304
|
-
];
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}).call(this);
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
key: "parseResponse",
|
|
312
|
-
value: function parseResponse(content, schema) {
|
|
313
|
-
try {
|
|
314
|
-
return new AIResponseParser(schema).parse(content);
|
|
315
|
-
} catch (error) {
|
|
316
|
-
var _this_options_logger;
|
|
317
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error('Failed to parse model response.', {
|
|
318
|
-
agent: this.name,
|
|
319
|
-
error: _instanceof(error, Error) ? error.message : 'Unknown error',
|
|
320
|
-
rawContent: content
|
|
321
|
-
});
|
|
322
|
-
throw new Error('Invalid response format from model.');
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
{
|
|
327
|
-
key: "resolveUserInput",
|
|
328
|
-
value: function resolveUserInput(input) {
|
|
329
|
-
if (input) {
|
|
330
|
-
return input.generate();
|
|
331
|
-
}
|
|
332
|
-
return 'Proceed with your instructions.';
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
]);
|
|
336
|
-
return BasicAgentAdapter;
|
|
337
|
-
}();
|
|
338
|
-
|
|
339
|
-
//# sourceMappingURL=basic-agent.adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/agents/basic-agent.adapter.ts"],"sourcesContent":["import { type LoggerPort } from '@jterrazz/logger';\nimport { z } from 'zod/v4';\n\nimport { type AgentPort } from '../../ports/agent.port.js';\nimport type { ModelPort } from '../../ports/model.port.js';\nimport type { PromptPort } from '../../ports/prompt.port.js';\n\nimport { AIResponseParser } from '../utils/ai-response-parser.js';\n\nimport type { SystemPromptAdapter } from '../prompts/system-prompt.adapter.js';\n\nexport interface BasicAgentOptions<TOutput = string> {\n logger?: LoggerPort;\n model: ModelPort;\n schema?: z.ZodSchema<TOutput>;\n systemPrompt: SystemPromptAdapter;\n verbose?: boolean;\n}\n\n/**\n * A basic agent for direct, one-shot interactions with a model.\n * It supports optional response parsing against a Zod schema but does not use tools.\n * @template TOutput - The TypeScript type of the output\n */\nexport class BasicAgentAdapter<TOutput = string> implements AgentPort<PromptPort, TOutput> {\n constructor(\n public readonly name: string,\n private readonly options: BasicAgentOptions<TOutput>,\n ) {}\n\n async run(input?: PromptPort): Promise<null | TOutput> {\n this.options.logger?.debug('Starting query execution', { agent: this.name });\n\n try {\n const content = await this.invokeModel(input);\n\n if (this.options.schema) {\n const parsedResponse = this.parseResponse(content, this.options.schema);\n this.options.logger?.debug('Execution finished and response parsed', {\n agent: this.name,\n });\n return parsedResponse;\n } else {\n this.options.logger?.debug('Execution finished', { agent: this.name });\n // When no schema is provided, we assume TOutput is string (default), so content is the result\n return content as TOutput;\n }\n } catch (error) {\n this.options.logger?.error('Execution failed', {\n agent: this.name,\n error: error instanceof Error ? error.message : 'Unknown error',\n });\n return null;\n }\n }\n\n private async invokeModel(input?: PromptPort): Promise<string> {\n const userInput = this.resolveUserInput(input);\n let systemMessage = this.options.systemPrompt.generate();\n\n // Add schema definition to system prompt if schema is provided\n if (this.options.schema) {\n const jsonSchema = z.toJSONSchema(this.options.schema);\n const isPrimitiveType = ['boolean', 'integer', 'number', 'string'].includes(\n jsonSchema.type as string,\n );\n\n if (isPrimitiveType) {\n systemMessage += `\\n\\n<OUTPUT_FORMAT>\nYou must respond with a ${jsonSchema.type} value that matches this schema:\n\n\\`\\`\\`json\n${JSON.stringify(jsonSchema, null, 2)}\n\\`\\`\\`\n\nYour response should be only the ${jsonSchema.type} value, without any JSON wrapping or additional text.\n</OUTPUT_FORMAT>`;\n } else {\n systemMessage += `\\n\\n<OUTPUT_FORMAT>\nYou must respond with valid JSON that matches this JSON schema description:\n\n\\`\\`\\`json\n${JSON.stringify(jsonSchema, null, 2)}\n\\`\\`\\`\n\nYour response must be parseable JSON that validates against this schema. Do not include any text outside the JSON.\n</OUTPUT_FORMAT>`;\n }\n }\n\n const messages = [\n { content: systemMessage, role: 'system' as const },\n { content: userInput, role: 'user' as const },\n ];\n\n this.options.logger?.debug('Invoking model...', {\n agent: this.name,\n hasSchema: !!this.options.schema,\n });\n\n if (this.options.verbose) {\n this.options.logger?.debug('Sending messages to model...', {\n agent: this.name,\n messages,\n });\n }\n\n const response = await this.options.model.getModel().invoke(messages);\n const { content } = response;\n\n if (typeof content !== 'string') {\n throw new Error('Model returned a non-string content type.');\n }\n\n return content;\n }\n\n private parseResponse<TResponse>(content: string, schema: z.ZodSchema<TResponse>): TResponse {\n try {\n return new AIResponseParser(schema).parse(content);\n } catch (error) {\n this.options.logger?.error('Failed to parse model response.', {\n agent: this.name,\n error: error instanceof Error ? error.message : 'Unknown error',\n rawContent: content,\n });\n throw new Error('Invalid response format from model.');\n }\n }\n\n private resolveUserInput(input?: PromptPort): string {\n if (input) {\n return input.generate();\n }\n return 'Proceed with your instructions.';\n }\n}\n"],"names":["z","AIResponseParser","BasicAgentAdapter","name","options","run","input","content","parsedResponse","error","logger","debug","agent","invokeModel","schema","parseResponse","Error","message","userInput","systemMessage","jsonSchema","isPrimitiveType","messages","response","resolveUserInput","systemPrompt","generate","toJSONSchema","includes","type","JSON","stringify","role","hasSchema","verbose","model","getModel","invoke","parse","rawContent"],"mappings":"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"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { type LoggerPort } from '@jterrazz/logger';
|
|
2
|
-
import { type AgentPort } from '../../ports/agent.port.js';
|
|
3
|
-
import type { PromptPort } from '../../ports/prompt.port.js';
|
|
4
|
-
export interface RetryableAgentAdapterOptions {
|
|
5
|
-
logger?: LoggerPort;
|
|
6
|
-
retries?: number;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* A decorator agent that adds retry logic to an existing agent.
|
|
10
|
-
* @template TInput - The TypeScript type of the input
|
|
11
|
-
* @template TOutput - The TypeScript type of the output
|
|
12
|
-
*/
|
|
13
|
-
export declare class RetryableAgentAdapter<TInput = PromptPort, TOutput = string> implements AgentPort<TInput, TOutput> {
|
|
14
|
-
private readonly agent;
|
|
15
|
-
readonly name: string;
|
|
16
|
-
private readonly logger?;
|
|
17
|
-
private readonly retries;
|
|
18
|
-
constructor(agent: AgentPort<TInput, TOutput>, options?: RetryableAgentAdapterOptions);
|
|
19
|
-
run(input?: TInput): Promise<null | TOutput>;
|
|
20
|
-
}
|
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
-
try {
|
|
3
|
-
var info = gen[key](arg);
|
|
4
|
-
var value = info.value;
|
|
5
|
-
} catch (error) {
|
|
6
|
-
reject(error);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
if (info.done) {
|
|
10
|
-
resolve(value);
|
|
11
|
-
} else {
|
|
12
|
-
Promise.resolve(value).then(_next, _throw);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _async_to_generator(fn) {
|
|
16
|
-
return function() {
|
|
17
|
-
var self = this, args = arguments;
|
|
18
|
-
return new Promise(function(resolve, reject) {
|
|
19
|
-
var gen = fn.apply(self, args);
|
|
20
|
-
function _next(value) {
|
|
21
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
-
}
|
|
23
|
-
function _throw(err) {
|
|
24
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
-
}
|
|
26
|
-
_next(undefined);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function _class_call_check(instance, Constructor) {
|
|
31
|
-
if (!(instance instanceof Constructor)) {
|
|
32
|
-
throw new TypeError("Cannot call a class as a function");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function _defineProperties(target, props) {
|
|
36
|
-
for(var i = 0; i < props.length; i++){
|
|
37
|
-
var descriptor = props[i];
|
|
38
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
39
|
-
descriptor.configurable = true;
|
|
40
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
41
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function _create_class(Constructor, protoProps, staticProps) {
|
|
45
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
46
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
47
|
-
return Constructor;
|
|
48
|
-
}
|
|
49
|
-
function _define_property(obj, key, value) {
|
|
50
|
-
if (key in obj) {
|
|
51
|
-
Object.defineProperty(obj, key, {
|
|
52
|
-
value: value,
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true
|
|
56
|
-
});
|
|
57
|
-
} else {
|
|
58
|
-
obj[key] = value;
|
|
59
|
-
}
|
|
60
|
-
return obj;
|
|
61
|
-
}
|
|
62
|
-
function _instanceof(left, right) {
|
|
63
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
64
|
-
return !!right[Symbol.hasInstance](left);
|
|
65
|
-
} else {
|
|
66
|
-
return left instanceof right;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function _ts_generator(thisArg, body) {
|
|
70
|
-
var f, y, t, _ = {
|
|
71
|
-
label: 0,
|
|
72
|
-
sent: function() {
|
|
73
|
-
if (t[0] & 1) throw t[1];
|
|
74
|
-
return t[1];
|
|
75
|
-
},
|
|
76
|
-
trys: [],
|
|
77
|
-
ops: []
|
|
78
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
79
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
80
|
-
return this;
|
|
81
|
-
}), g;
|
|
82
|
-
function verb(n) {
|
|
83
|
-
return function(v) {
|
|
84
|
-
return step([
|
|
85
|
-
n,
|
|
86
|
-
v
|
|
87
|
-
]);
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function step(op) {
|
|
91
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
92
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
93
|
-
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;
|
|
94
|
-
if (y = 0, t) op = [
|
|
95
|
-
op[0] & 2,
|
|
96
|
-
t.value
|
|
97
|
-
];
|
|
98
|
-
switch(op[0]){
|
|
99
|
-
case 0:
|
|
100
|
-
case 1:
|
|
101
|
-
t = op;
|
|
102
|
-
break;
|
|
103
|
-
case 4:
|
|
104
|
-
_.label++;
|
|
105
|
-
return {
|
|
106
|
-
value: op[1],
|
|
107
|
-
done: false
|
|
108
|
-
};
|
|
109
|
-
case 5:
|
|
110
|
-
_.label++;
|
|
111
|
-
y = op[1];
|
|
112
|
-
op = [
|
|
113
|
-
0
|
|
114
|
-
];
|
|
115
|
-
continue;
|
|
116
|
-
case 7:
|
|
117
|
-
op = _.ops.pop();
|
|
118
|
-
_.trys.pop();
|
|
119
|
-
continue;
|
|
120
|
-
default:
|
|
121
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
122
|
-
_ = 0;
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
126
|
-
_.label = op[1];
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
130
|
-
_.label = t[1];
|
|
131
|
-
t = op;
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
if (t && _.label < t[2]) {
|
|
135
|
-
_.label = t[2];
|
|
136
|
-
_.ops.push(op);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
if (t[2]) _.ops.pop();
|
|
140
|
-
_.trys.pop();
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
op = body.call(thisArg, _);
|
|
144
|
-
} catch (e) {
|
|
145
|
-
op = [
|
|
146
|
-
6,
|
|
147
|
-
e
|
|
148
|
-
];
|
|
149
|
-
y = 0;
|
|
150
|
-
} finally{
|
|
151
|
-
f = t = 0;
|
|
152
|
-
}
|
|
153
|
-
if (op[0] & 5) throw op[1];
|
|
154
|
-
return {
|
|
155
|
-
value: op[0] ? op[1] : void 0,
|
|
156
|
-
done: true
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* A decorator agent that adds retry logic to an existing agent.
|
|
162
|
-
* @template TInput - The TypeScript type of the input
|
|
163
|
-
* @template TOutput - The TypeScript type of the output
|
|
164
|
-
*/ export var RetryableAgentAdapter = /*#__PURE__*/ function() {
|
|
165
|
-
"use strict";
|
|
166
|
-
function RetryableAgentAdapter(agent) {
|
|
167
|
-
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
168
|
-
_class_call_check(this, RetryableAgentAdapter);
|
|
169
|
-
_define_property(this, "agent", void 0);
|
|
170
|
-
_define_property(this, "name", void 0);
|
|
171
|
-
_define_property(this, "logger", void 0);
|
|
172
|
-
_define_property(this, "retries", void 0);
|
|
173
|
-
this.agent = agent;
|
|
174
|
-
var logger = options.logger, _options_retries = options.retries, retries = _options_retries === void 0 ? 1 : _options_retries;
|
|
175
|
-
this.name = "Retryable(".concat(agent.name, ")");
|
|
176
|
-
this.logger = logger;
|
|
177
|
-
this.retries = retries;
|
|
178
|
-
}
|
|
179
|
-
_create_class(RetryableAgentAdapter, [
|
|
180
|
-
{
|
|
181
|
-
key: "run",
|
|
182
|
-
value: function run(input) {
|
|
183
|
-
return _async_to_generator(function() {
|
|
184
|
-
var _this_logger, maxAttempts, attempt, _this_logger1, _this_logger2, result, _this_logger3, error, _this_logger4;
|
|
185
|
-
return _ts_generator(this, function(_state) {
|
|
186
|
-
switch(_state.label){
|
|
187
|
-
case 0:
|
|
188
|
-
maxAttempts = this.retries + 1;
|
|
189
|
-
attempt = 1;
|
|
190
|
-
_state.label = 1;
|
|
191
|
-
case 1:
|
|
192
|
-
if (!(attempt <= maxAttempts)) return [
|
|
193
|
-
3,
|
|
194
|
-
6
|
|
195
|
-
];
|
|
196
|
-
_state.label = 2;
|
|
197
|
-
case 2:
|
|
198
|
-
_state.trys.push([
|
|
199
|
-
2,
|
|
200
|
-
4,
|
|
201
|
-
,
|
|
202
|
-
5
|
|
203
|
-
]);
|
|
204
|
-
(_this_logger1 = this.logger) === null || _this_logger1 === void 0 ? void 0 : _this_logger1.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts), {
|
|
205
|
-
agent: this.name
|
|
206
|
-
});
|
|
207
|
-
return [
|
|
208
|
-
4,
|
|
209
|
-
this.agent.run(input)
|
|
210
|
-
];
|
|
211
|
-
case 3:
|
|
212
|
-
result = _state.sent();
|
|
213
|
-
if (result !== null) {
|
|
214
|
-
;
|
|
215
|
-
(_this_logger3 = this.logger) === null || _this_logger3 === void 0 ? void 0 : _this_logger3.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " succeeded"), {
|
|
216
|
-
agent: this.name
|
|
217
|
-
});
|
|
218
|
-
return [
|
|
219
|
-
2,
|
|
220
|
-
result
|
|
221
|
-
];
|
|
222
|
-
}
|
|
223
|
-
(_this_logger2 = this.logger) === null || _this_logger2 === void 0 ? void 0 : _this_logger2.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " failed: agent returned null"), {
|
|
224
|
-
agent: this.name
|
|
225
|
-
});
|
|
226
|
-
return [
|
|
227
|
-
3,
|
|
228
|
-
5
|
|
229
|
-
];
|
|
230
|
-
case 4:
|
|
231
|
-
error = _state.sent();
|
|
232
|
-
(_this_logger4 = this.logger) === null || _this_logger4 === void 0 ? void 0 : _this_logger4.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " failed with an error"), {
|
|
233
|
-
agent: this.name,
|
|
234
|
-
error: _instanceof(error, Error) ? error.message : 'Unknown error'
|
|
235
|
-
});
|
|
236
|
-
return [
|
|
237
|
-
3,
|
|
238
|
-
5
|
|
239
|
-
];
|
|
240
|
-
case 5:
|
|
241
|
-
attempt++;
|
|
242
|
-
return [
|
|
243
|
-
3,
|
|
244
|
-
1
|
|
245
|
-
];
|
|
246
|
-
case 6:
|
|
247
|
-
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.error("All ".concat(maxAttempts, " attempts failed"), {
|
|
248
|
-
agent: this.name
|
|
249
|
-
});
|
|
250
|
-
return [
|
|
251
|
-
2,
|
|
252
|
-
null
|
|
253
|
-
];
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
}).call(this);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
]);
|
|
260
|
-
return RetryableAgentAdapter;
|
|
261
|
-
}();
|
|
262
|
-
|
|
263
|
-
//# sourceMappingURL=retryable-agent.adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/agents/retryable-agent.adapter.ts"],"sourcesContent":["import { type LoggerPort } from '@jterrazz/logger';\n\nimport { type AgentPort } from '../../ports/agent.port.js';\nimport type { PromptPort } from '../../ports/prompt.port.js';\n\nexport interface RetryableAgentAdapterOptions {\n logger?: LoggerPort;\n retries?: number;\n}\n\n/**\n * A decorator agent that adds retry logic to an existing agent.\n * @template TInput - The TypeScript type of the input\n * @template TOutput - The TypeScript type of the output\n */\nexport class RetryableAgentAdapter<TInput = PromptPort, TOutput = string>\n implements AgentPort<TInput, TOutput>\n{\n public readonly name: string;\n private readonly logger?: LoggerPort;\n private readonly retries: number;\n\n constructor(\n private readonly agent: AgentPort<TInput, TOutput>,\n options: RetryableAgentAdapterOptions = {},\n ) {\n const { logger, retries = 1 } = options;\n this.name = `Retryable(${agent.name})`;\n this.logger = logger;\n this.retries = retries;\n }\n\n async run(input?: TInput): Promise<null | TOutput> {\n const maxAttempts = this.retries + 1;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n this.logger?.debug(`Attempt ${attempt} of ${maxAttempts}`, { agent: this.name });\n const result = await this.agent.run(input);\n\n if (result !== null) {\n this.logger?.debug(`Attempt ${attempt} of ${maxAttempts} succeeded`, {\n agent: this.name,\n });\n return result;\n }\n\n this.logger?.debug(\n `Attempt ${attempt} of ${maxAttempts} failed: agent returned null`,\n { agent: this.name },\n );\n } catch (error) {\n this.logger?.debug(`Attempt ${attempt} of ${maxAttempts} failed with an error`, {\n agent: this.name,\n error: error instanceof Error ? error.message : 'Unknown error',\n });\n }\n }\n\n this.logger?.error(`All ${maxAttempts} attempts failed`, { agent: this.name });\n return null;\n }\n}\n"],"names":["RetryableAgentAdapter","agent","options","name","logger","retries","run","input","maxAttempts","attempt","result","error","debug","Error","message"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA;;;;CAIC,GACD,OAAO,IAAA,AAAMA,sCAAN;;aAAMA,sBAQL,AAAiBC,KAAiC;YAClDC,UAAAA,iEAAwC,CAAC;gCATpCF;;QAGT,uBAAgBG,QAAhB,KAAA;QACA,uBAAiBC,UAAjB,KAAA;QACA,uBAAiBC,WAAjB,KAAA;aAGqBJ,QAAAA;QAGjB,IAAQG,SAAwBF,QAAxBE,2BAAwBF,QAAhBG,SAAAA,wCAAU;QAC1B,IAAI,CAACF,IAAI,GAAG,AAAC,aAAuB,OAAXF,MAAME,IAAI,EAAC;QACpC,IAAI,CAACC,MAAM,GAAGA;QACd,IAAI,CAACC,OAAO,GAAGA;;kBAdVL;;YAiBHM,KAAAA;mBAAN,SAAMA,IAAIC,KAAc;;wBA2BpB,cA1BMC,aAEGC,SAED,eAUA,eATMC,QAGF,eAUCC,OACL;;;;gCAnBFH,cAAc,IAAI,CAACH,OAAO,GAAG;gCAE1BI,UAAU;;;qCAAGA,CAAAA,WAAWD,WAAU;;;;;;;;;;;;iCAEnC,gBAAA,IAAI,CAACJ,MAAM,cAAX,oCAAA,cAAaQ,KAAK,CAAC,AAAC,WAAwBJ,OAAdC,SAAQ,QAAkB,OAAZD,cAAe;oCAAEP,OAAO,IAAI,CAACE,IAAI;gCAAC;gCAC/D;;oCAAM,IAAI,CAACF,KAAK,CAACK,GAAG,CAACC;;;gCAA9BG,SAAS;gCAEf,IAAIA,WAAW,MAAM;;qCACjB,gBAAA,IAAI,CAACN,MAAM,cAAX,oCAAA,cAAaQ,KAAK,CAAC,AAAC,WAAwBJ,OAAdC,SAAQ,QAAkB,OAAZD,aAAY,eAAa;wCACjEP,OAAO,IAAI,CAACE,IAAI;oCACpB;oCACA;;wCAAOO;;gCACX;iCAEA,gBAAA,IAAI,CAACN,MAAM,cAAX,oCAAA,cAAaQ,KAAK,CACd,AAAC,WAAwBJ,OAAdC,SAAQ,QAAkB,OAAZD,aAAY,iCACrC;oCAAEP,OAAO,IAAI,CAACE,IAAI;gCAAC;;;;;;gCAElBQ;iCACL,gBAAA,IAAI,CAACP,MAAM,cAAX,oCAAA,cAAaQ,KAAK,CAAC,AAAC,WAAwBJ,OAAdC,SAAQ,QAAkB,OAAZD,aAAY,0BAAwB;oCAC5EP,OAAO,IAAI,CAACE,IAAI;oCAChBQ,OAAOA,AAAK,YAALA,OAAiBE,SAAQF,MAAMG,OAAO,GAAG;gCACpD;;;;;;gCApBsCL;;;;;;iCAwB9C,eAAA,IAAI,CAACL,MAAM,cAAX,mCAAA,aAAaO,KAAK,CAAC,AAAC,OAAkB,OAAZH,aAAY,qBAAmB;oCAAEP,OAAO,IAAI,CAACE,IAAI;gCAAC;gCAC5E;;oCAAO;;;;gBACX;;;;WA9CSH;IA+CZ"}
|