@jterrazz/intelligence 1.5.0 → 2.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 +199 -83
- package/dist/adapters/agents/{basic-agent.adapter.d.ts → chat-agent.adapter.d.ts} +6 -6
- package/dist/adapters/agents/{basic-agent.adapter.js → chat-agent.adapter.js} +54 -27
- package/dist/adapters/agents/chat-agent.adapter.js.map +1 -0
- package/dist/adapters/agents/{retryable-agent.adapter.d.ts → resilient-agent.adapter.d.ts} +4 -4
- package/dist/adapters/agents/{retryable-agent.adapter.js → resilient-agent.adapter.js} +22 -13
- package/dist/adapters/agents/resilient-agent.adapter.js.map +1 -0
- package/dist/adapters/agents/{autonomous-agent.adapter.d.ts → tool-agent.adapter.d.ts} +6 -6
- package/dist/adapters/agents/{autonomous-agent.adapter.js → tool-agent.adapter.js} +29 -18
- package/dist/adapters/agents/tool-agent.adapter.js.map +1 -0
- package/dist/adapters/models/openrouter-model.adapter.d.ts +17 -32
- package/dist/adapters/models/openrouter-model.adapter.js +84 -25
- package/dist/adapters/models/openrouter-model.adapter.js.map +1 -1
- package/dist/adapters/prompts/__tests__/presets.test.js +4 -4
- package/dist/adapters/prompts/__tests__/presets.test.js.map +1 -1
- package/dist/adapters/prompts/system-prompt.adapter.d.ts +2 -2
- package/dist/adapters/prompts/system-prompt.adapter.js +6 -6
- package/dist/adapters/prompts/system-prompt.adapter.js.map +1 -1
- package/dist/adapters/prompts/user-prompt.adapter.d.ts +2 -2
- package/dist/adapters/prompts/user-prompt.adapter.js +6 -6
- package/dist/adapters/prompts/user-prompt.adapter.js.map +1 -1
- package/dist/adapters/providers/openrouter-provider.adapter.d.ts +34 -0
- package/dist/adapters/providers/openrouter-provider.adapter.js +57 -0
- package/dist/adapters/providers/openrouter-provider.adapter.js.map +1 -0
- package/dist/adapters/tools/safe-tool.adapter.d.ts +2 -2
- package/dist/adapters/tools/safe-tool.adapter.js +6 -6
- package/dist/adapters/tools/safe-tool.adapter.js.map +1 -1
- package/dist/adapters/utils/__tests__/{ai-response-parser.test.js → structured-response-parser.test.js} +30 -30
- package/dist/adapters/utils/__tests__/structured-response-parser.test.js.map +1 -0
- package/dist/adapters/utils/{ai-response-parser-error.d.ts → structured-response-parser-error.d.ts} +2 -2
- package/dist/adapters/utils/{ai-response-parser-error.js → structured-response-parser-error.js} +9 -9
- package/dist/adapters/utils/structured-response-parser-error.js.map +1 -0
- package/dist/adapters/utils/{ai-response-parser.d.ts → structured-response-parser.d.ts} +1 -1
- package/dist/adapters/utils/{ai-response-parser.js → structured-response-parser.js} +13 -13
- package/dist/adapters/utils/structured-response-parser.js.map +1 -0
- package/dist/index.cjs +748 -342
- package/dist/index.d.ts +11 -8
- package/dist/index.js +29 -8
- package/dist/index.js.map +1 -1
- package/dist/ports/model.port.d.ts +30 -3
- package/dist/ports/model.port.js +1 -1
- package/dist/ports/model.port.js.map +1 -1
- package/dist/ports/provider.port.d.ts +13 -0
- package/dist/ports/provider.port.js +5 -0
- package/dist/ports/provider.port.js.map +1 -0
- package/package.json +11 -9
- package/dist/adapters/agents/autonomous-agent.adapter.js.map +0 -1
- package/dist/adapters/agents/basic-agent.adapter.js.map +0 -1
- package/dist/adapters/agents/retryable-agent.adapter.js.map +0 -1
- package/dist/adapters/utils/__tests__/ai-response-parser.test.js.map +0 -1
- package/dist/adapters/utils/ai-response-parser-error.js.map +0 -1
- package/dist/adapters/utils/ai-response-parser.js.map +0 -1
- /package/dist/adapters/utils/__tests__/{ai-response-parser.test.d.ts → structured-response-parser.test.d.ts} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var agents = require('langchain/agents');
|
|
3
|
+
var ai = require('ai');
|
|
5
4
|
var v4 = require('zod/v4');
|
|
6
5
|
var jsonrepair = require('jsonrepair');
|
|
6
|
+
var prompts = require('@langchain/core/prompts');
|
|
7
|
+
var agents = require('langchain/agents');
|
|
7
8
|
var openai = require('@langchain/openai');
|
|
9
|
+
var aiSdkProvider = require('@openrouter/ai-sdk-provider');
|
|
8
10
|
var tools = require('langchain/tools');
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
|
-
* Custom error for
|
|
13
|
+
* Custom error for structured response parsing failures
|
|
12
14
|
*/ function _assert_this_initialized(self) {
|
|
13
15
|
if (self === void 0) {
|
|
14
16
|
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
@@ -19,7 +21,7 @@ function _call_super(_this, derived, args) {
|
|
|
19
21
|
derived = _get_prototype_of(derived);
|
|
20
22
|
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
21
23
|
}
|
|
22
|
-
function _class_call_check$
|
|
24
|
+
function _class_call_check$9(instance, Constructor) {
|
|
23
25
|
if (!(instance instanceof Constructor)) {
|
|
24
26
|
throw new TypeError("Cannot call a class as a function");
|
|
25
27
|
}
|
|
@@ -41,7 +43,7 @@ function _construct(Parent, args, Class) {
|
|
|
41
43
|
}
|
|
42
44
|
return _construct.apply(null, arguments);
|
|
43
45
|
}
|
|
44
|
-
function _define_property$
|
|
46
|
+
function _define_property$9(obj, key, value) {
|
|
45
47
|
if (key in obj) {
|
|
46
48
|
Object.defineProperty(obj, key, {
|
|
47
49
|
value: value,
|
|
@@ -127,18 +129,18 @@ function _is_native_reflect_construct() {
|
|
|
127
129
|
return !!result;
|
|
128
130
|
})();
|
|
129
131
|
}
|
|
130
|
-
var
|
|
131
|
-
_inherits(
|
|
132
|
-
function
|
|
133
|
-
_class_call_check$
|
|
132
|
+
var StructuredResponseParserError = /*#__PURE__*/ function(Error1) {
|
|
133
|
+
_inherits(StructuredResponseParserError, Error1);
|
|
134
|
+
function StructuredResponseParserError(message, cause, text) {
|
|
135
|
+
_class_call_check$9(this, StructuredResponseParserError);
|
|
134
136
|
var _this;
|
|
135
|
-
_this = _call_super(this,
|
|
137
|
+
_this = _call_super(this, StructuredResponseParserError, [
|
|
136
138
|
message
|
|
137
|
-
]), _define_property$
|
|
138
|
-
_this.name = '
|
|
139
|
+
]), _define_property$9(_this, "cause", void 0), _define_property$9(_this, "text", void 0), _this.cause = cause, _this.text = text;
|
|
140
|
+
_this.name = 'StructuredResponseParserError';
|
|
139
141
|
return _this;
|
|
140
142
|
}
|
|
141
|
-
return
|
|
143
|
+
return StructuredResponseParserError;
|
|
142
144
|
}(_wrap_native_super(Error));
|
|
143
145
|
|
|
144
146
|
function _array_like_to_array(arr, len) {
|
|
@@ -149,12 +151,12 @@ function _array_like_to_array(arr, len) {
|
|
|
149
151
|
function _array_with_holes(arr) {
|
|
150
152
|
if (Array.isArray(arr)) return arr;
|
|
151
153
|
}
|
|
152
|
-
function _class_call_check$
|
|
154
|
+
function _class_call_check$8(instance, Constructor) {
|
|
153
155
|
if (!(instance instanceof Constructor)) {
|
|
154
156
|
throw new TypeError("Cannot call a class as a function");
|
|
155
157
|
}
|
|
156
158
|
}
|
|
157
|
-
function _defineProperties$
|
|
159
|
+
function _defineProperties$8(target, props) {
|
|
158
160
|
for(var i = 0; i < props.length; i++){
|
|
159
161
|
var descriptor = props[i];
|
|
160
162
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
@@ -163,11 +165,11 @@ function _defineProperties$6(target, props) {
|
|
|
163
165
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
164
166
|
}
|
|
165
167
|
}
|
|
166
|
-
function _create_class$
|
|
167
|
-
if (protoProps) _defineProperties$
|
|
168
|
+
function _create_class$8(Constructor, protoProps, staticProps) {
|
|
169
|
+
if (protoProps) _defineProperties$8(Constructor.prototype, protoProps);
|
|
168
170
|
return Constructor;
|
|
169
171
|
}
|
|
170
|
-
function _define_property$
|
|
172
|
+
function _define_property$8(obj, key, value) {
|
|
171
173
|
if (key in obj) {
|
|
172
174
|
Object.defineProperty(obj, key, {
|
|
173
175
|
value: value,
|
|
@@ -180,7 +182,7 @@ function _define_property$6(obj, key, value) {
|
|
|
180
182
|
}
|
|
181
183
|
return obj;
|
|
182
184
|
}
|
|
183
|
-
function _instanceof$
|
|
185
|
+
function _instanceof$4(left, right) {
|
|
184
186
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
185
187
|
return !!right[Symbol.hasInstance](left);
|
|
186
188
|
} else {
|
|
@@ -231,13 +233,13 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
231
233
|
}
|
|
232
234
|
/**
|
|
233
235
|
* Parses AI response text into structured data based on Zod schema
|
|
234
|
-
*/ var
|
|
235
|
-
function
|
|
236
|
-
_class_call_check$
|
|
237
|
-
_define_property$
|
|
236
|
+
*/ var StructuredResponseParser = /*#__PURE__*/ function() {
|
|
237
|
+
function StructuredResponseParser(schema) {
|
|
238
|
+
_class_call_check$8(this, StructuredResponseParser);
|
|
239
|
+
_define_property$8(this, "schema", void 0);
|
|
238
240
|
this.schema = schema;
|
|
239
241
|
}
|
|
240
|
-
_create_class$
|
|
242
|
+
_create_class$8(StructuredResponseParser, [
|
|
241
243
|
{
|
|
242
244
|
key: "parse",
|
|
243
245
|
value: /**
|
|
@@ -249,8 +251,8 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
249
251
|
var unescapedJson = this.unescapeJsonValues(json);
|
|
250
252
|
return this.schema.parse(unescapedJson);
|
|
251
253
|
} catch (error) {
|
|
252
|
-
if (_instanceof$
|
|
253
|
-
throw new
|
|
254
|
+
if (_instanceof$4(error, v4.z.ZodError)) {
|
|
255
|
+
throw new StructuredResponseParserError('Failed to validate response against schema', error, text);
|
|
254
256
|
}
|
|
255
257
|
throw error;
|
|
256
258
|
}
|
|
@@ -289,16 +291,16 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
289
291
|
value: /**
|
|
290
292
|
* Converts value to appropriate primitive type based on schema
|
|
291
293
|
*/ function convertToPrimitive(value, schema) {
|
|
292
|
-
if (_instanceof$
|
|
294
|
+
if (_instanceof$4(schema, v4.z.ZodString)) {
|
|
293
295
|
return String(value);
|
|
294
296
|
}
|
|
295
|
-
if (_instanceof$
|
|
297
|
+
if (_instanceof$4(schema, v4.z.ZodNumber)) {
|
|
296
298
|
return Number(value);
|
|
297
299
|
}
|
|
298
|
-
if (_instanceof$
|
|
300
|
+
if (_instanceof$4(schema, v4.z.ZodBoolean)) {
|
|
299
301
|
return Boolean(value);
|
|
300
302
|
}
|
|
301
|
-
if (_instanceof$
|
|
303
|
+
if (_instanceof$4(schema, v4.z.ZodNull)) {
|
|
302
304
|
return null;
|
|
303
305
|
}
|
|
304
306
|
return value;
|
|
@@ -312,14 +314,14 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
312
314
|
var arrayStart = text.indexOf('[');
|
|
313
315
|
var arrayEnd = text.lastIndexOf(']');
|
|
314
316
|
if (arrayStart === -1 || arrayEnd === -1) {
|
|
315
|
-
throw new
|
|
317
|
+
throw new StructuredResponseParserError('No array found in response', undefined, text);
|
|
316
318
|
}
|
|
317
319
|
try {
|
|
318
320
|
var raw = text.slice(arrayStart, arrayEnd + 1);
|
|
319
321
|
var repaired = this.repairJson(raw);
|
|
320
322
|
return JSON.parse(repaired);
|
|
321
323
|
} catch (error) {
|
|
322
|
-
throw new
|
|
324
|
+
throw new StructuredResponseParserError('Failed to parse array JSON', error, text);
|
|
323
325
|
}
|
|
324
326
|
}
|
|
325
327
|
},
|
|
@@ -343,16 +345,16 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
343
345
|
value: /**
|
|
344
346
|
* Extracts and parses JSON from text based on schema type
|
|
345
347
|
*/ function extractJsonFromText(text) {
|
|
346
|
-
if (_instanceof$
|
|
348
|
+
if (_instanceof$4(this.schema, v4.z.ZodArray)) {
|
|
347
349
|
return this.extractArray(text);
|
|
348
350
|
}
|
|
349
|
-
if (_instanceof$
|
|
351
|
+
if (_instanceof$4(this.schema, v4.z.ZodObject)) {
|
|
350
352
|
return this.extractObject(text);
|
|
351
353
|
}
|
|
352
|
-
if (_instanceof$
|
|
354
|
+
if (_instanceof$4(this.schema, v4.z.ZodString) || _instanceof$4(this.schema, v4.z.ZodNumber) || _instanceof$4(this.schema, v4.z.ZodBoolean) || _instanceof$4(this.schema, v4.z.ZodNull)) {
|
|
353
355
|
return this.extractPrimitive(text, this.schema);
|
|
354
356
|
}
|
|
355
|
-
throw new
|
|
357
|
+
throw new StructuredResponseParserError('Unsupported schema type', undefined, text);
|
|
356
358
|
}
|
|
357
359
|
},
|
|
358
360
|
{
|
|
@@ -363,14 +365,14 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
363
365
|
var objectStart = text.indexOf('{');
|
|
364
366
|
var objectEnd = text.lastIndexOf('}');
|
|
365
367
|
if (objectStart === -1 || objectEnd === -1) {
|
|
366
|
-
throw new
|
|
368
|
+
throw new StructuredResponseParserError('No object found in response', undefined, text);
|
|
367
369
|
}
|
|
368
370
|
try {
|
|
369
371
|
var raw = text.slice(objectStart, objectEnd + 1);
|
|
370
372
|
var repaired = this.repairJson(raw);
|
|
371
373
|
return JSON.parse(repaired);
|
|
372
374
|
} catch (error) {
|
|
373
|
-
throw new
|
|
375
|
+
throw new StructuredResponseParserError('Failed to parse object JSON', error, text);
|
|
374
376
|
}
|
|
375
377
|
}
|
|
376
378
|
},
|
|
@@ -479,10 +481,10 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
479
481
|
}
|
|
480
482
|
}
|
|
481
483
|
]);
|
|
482
|
-
return
|
|
484
|
+
return StructuredResponseParser;
|
|
483
485
|
}();
|
|
484
486
|
|
|
485
|
-
function asyncGeneratorStep$
|
|
487
|
+
function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
|
|
486
488
|
try {
|
|
487
489
|
var info = gen[key](arg);
|
|
488
490
|
var value = info.value;
|
|
@@ -496,27 +498,27 @@ function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
496
498
|
Promise.resolve(value).then(_next, _throw);
|
|
497
499
|
}
|
|
498
500
|
}
|
|
499
|
-
function _async_to_generator$
|
|
501
|
+
function _async_to_generator$3(fn) {
|
|
500
502
|
return function() {
|
|
501
503
|
var self = this, args = arguments;
|
|
502
504
|
return new Promise(function(resolve, reject) {
|
|
503
505
|
var gen = fn.apply(self, args);
|
|
504
506
|
function _next(value) {
|
|
505
|
-
asyncGeneratorStep$
|
|
507
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
|
|
506
508
|
}
|
|
507
509
|
function _throw(err) {
|
|
508
|
-
asyncGeneratorStep$
|
|
510
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
|
|
509
511
|
}
|
|
510
512
|
_next(undefined);
|
|
511
513
|
});
|
|
512
514
|
};
|
|
513
515
|
}
|
|
514
|
-
function _class_call_check$
|
|
516
|
+
function _class_call_check$7(instance, Constructor) {
|
|
515
517
|
if (!(instance instanceof Constructor)) {
|
|
516
518
|
throw new TypeError("Cannot call a class as a function");
|
|
517
519
|
}
|
|
518
520
|
}
|
|
519
|
-
function _defineProperties$
|
|
521
|
+
function _defineProperties$7(target, props) {
|
|
520
522
|
for(var i = 0; i < props.length; i++){
|
|
521
523
|
var descriptor = props[i];
|
|
522
524
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
@@ -525,11 +527,11 @@ function _defineProperties$5(target, props) {
|
|
|
525
527
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
526
528
|
}
|
|
527
529
|
}
|
|
528
|
-
function _create_class$
|
|
529
|
-
if (protoProps) _defineProperties$
|
|
530
|
+
function _create_class$7(Constructor, protoProps, staticProps) {
|
|
531
|
+
if (protoProps) _defineProperties$7(Constructor.prototype, protoProps);
|
|
530
532
|
return Constructor;
|
|
531
533
|
}
|
|
532
|
-
function _define_property$
|
|
534
|
+
function _define_property$7(obj, key, value) {
|
|
533
535
|
if (key in obj) {
|
|
534
536
|
Object.defineProperty(obj, key, {
|
|
535
537
|
value: value,
|
|
@@ -542,14 +544,14 @@ function _define_property$5(obj, key, value) {
|
|
|
542
544
|
}
|
|
543
545
|
return obj;
|
|
544
546
|
}
|
|
545
|
-
function _instanceof$
|
|
547
|
+
function _instanceof$3(left, right) {
|
|
546
548
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
547
549
|
return !!right[Symbol.hasInstance](left);
|
|
548
550
|
} else {
|
|
549
551
|
return left instanceof right;
|
|
550
552
|
}
|
|
551
553
|
}
|
|
552
|
-
function _ts_generator$
|
|
554
|
+
function _ts_generator$3(thisArg, body) {
|
|
553
555
|
var f, y, t, _ = {
|
|
554
556
|
label: 0,
|
|
555
557
|
sent: function() {
|
|
@@ -640,100 +642,74 @@ function _ts_generator$2(thisArg, body) {
|
|
|
640
642
|
};
|
|
641
643
|
}
|
|
642
644
|
}
|
|
643
|
-
var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<GLOBAL_WRAPPER_OUTPUT_FORMAT>\nCRITICAL: The format instructions in this section are the ONLY valid way to structure your response. Your entire response MUST be a single JSON markdown code block. Any formatting guidelines within the <OBJECTIVE> section apply ONLY to the content inside the "RESPOND:" part of your final "action_input".\n\nREQUIRED: You have two ways to respond:\n\n1. **Call a tool** to gather information. For this, you MUST output a JSON blob with the tool\'s name and its input.\n *Valid tool names are: {tool_names}*\n ```json\n {{\n "action": "tool_name_to_use",\n "action_input": "the input for the tool, or an empty object {{}} if no input is needed"\n }}\n ```\n\n2. **Provide the Final Answer** once you have enough information. For this, you MUST output a JSON blob with the "Final Answer" action.\n The "action_input" for a "Final Answer" MUST be a string that begins with either "RESPOND: " for a message or "SILENT: " for no message. This prefix is a literal part of the output string and MUST NOT be omitted.\n - To send a message:\n ```json\n {{\n "action": "Final Answer",\n "action_input": "RESPOND: <your response message>"\n }}\n ```\n - To stay silent:\n ```json\n {{\n "action": "Final Answer",\n "action_input": "SILENT: <your reason for staying silent>"\n }}\n ```\n\n YOU MUST ALWAYS INCLUDE "RESPOND:" OR "SILENT:" IN YOUR FINAL ANSWER\'S "action_input". FAILURE TO DO SO WILL CAUSE AN ERROR.\n\n{schema_format}\n</OUTPUT_FORMAT>\n\n<EXECUTION_CONTEXT>\nThis is internal data for your reference.\n\n<TOOLS>\n{tools}\n</TOOLS>\n\n<WORKING_MEMORY>\nThis is your internal thought process and previous tool usage.\n{agent_scratchpad}\n</WORKING_MEMORY>\n</EXECUTION_CONTEXT>\n';
|
|
644
645
|
/**
|
|
645
|
-
*
|
|
646
|
-
* It
|
|
646
|
+
* A conversational agent for direct, one-shot interactions with a model.
|
|
647
|
+
* It supports optional response parsing against a Zod schema but does not use tools.
|
|
647
648
|
* @template TOutput - The TypeScript type of the output
|
|
648
|
-
*/ var
|
|
649
|
-
function
|
|
650
|
-
_class_call_check$
|
|
651
|
-
_define_property$
|
|
652
|
-
_define_property$
|
|
649
|
+
*/ var ChatAgent = /*#__PURE__*/ function() {
|
|
650
|
+
function ChatAgent(name, options) {
|
|
651
|
+
_class_call_check$7(this, ChatAgent);
|
|
652
|
+
_define_property$7(this, "name", void 0);
|
|
653
|
+
_define_property$7(this, "options", void 0);
|
|
653
654
|
this.name = name;
|
|
654
655
|
this.options = options;
|
|
655
656
|
}
|
|
656
|
-
_create_class$
|
|
657
|
+
_create_class$7(ChatAgent, [
|
|
657
658
|
{
|
|
658
659
|
key: "run",
|
|
659
660
|
value: function run(input) {
|
|
660
|
-
return _async_to_generator$
|
|
661
|
-
var _this_options_logger,
|
|
662
|
-
return _ts_generator$
|
|
661
|
+
return _async_to_generator$3(function() {
|
|
662
|
+
var _this_options_logger, content, _this_options_logger1, parsedResponse, _this_options_logger2, error, _this_options_logger3;
|
|
663
|
+
return _ts_generator$3(this, function(_state) {
|
|
663
664
|
switch(_state.label){
|
|
664
665
|
case 0:
|
|
665
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug(
|
|
666
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug('Starting query execution', {
|
|
667
|
+
agent: this.name
|
|
668
|
+
});
|
|
666
669
|
_state.label = 1;
|
|
667
670
|
case 1:
|
|
668
671
|
_state.trys.push([
|
|
669
672
|
1,
|
|
670
|
-
|
|
673
|
+
3,
|
|
671
674
|
,
|
|
672
|
-
|
|
675
|
+
4
|
|
673
676
|
]);
|
|
674
677
|
return [
|
|
675
678
|
4,
|
|
676
|
-
this.
|
|
679
|
+
this.invokeModel(input)
|
|
677
680
|
];
|
|
678
681
|
case 2:
|
|
679
|
-
|
|
680
|
-
userInput = this.resolveUserInput(input);
|
|
681
|
-
return [
|
|
682
|
-
4,
|
|
683
|
-
executor.invoke({
|
|
684
|
-
input: userInput
|
|
685
|
-
})
|
|
686
|
-
];
|
|
687
|
-
case 3:
|
|
688
|
-
result = _state.sent();
|
|
689
|
-
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug("[".concat(this.name, "] Agent execution completed."), {
|
|
690
|
-
hasOutput: 'output' in result
|
|
691
|
-
});
|
|
692
|
-
if (!result || typeof result.output !== 'string') {
|
|
693
|
-
throw new Error('Agent returned an invalid result structure.');
|
|
694
|
-
}
|
|
695
|
-
agentResponse = this.parseAgentOutput(result.output);
|
|
696
|
-
if (!agentResponse) {
|
|
697
|
-
return [
|
|
698
|
-
2,
|
|
699
|
-
null
|
|
700
|
-
];
|
|
701
|
-
}
|
|
702
|
-
if (!agentResponse.shouldRespond) {
|
|
703
|
-
(_this_options_logger2 = this.options.logger) === null || _this_options_logger2 === void 0 ? void 0 : _this_options_logger2.info("[".concat(this.name, "] Agent chose to remain silent."), {
|
|
704
|
-
reason: agentResponse.reason
|
|
705
|
-
});
|
|
706
|
-
return [
|
|
707
|
-
2,
|
|
708
|
-
null
|
|
709
|
-
];
|
|
710
|
-
}
|
|
711
|
-
message = (_agentResponse_message = agentResponse.message) !== null && _agentResponse_message !== void 0 ? _agentResponse_message : '';
|
|
682
|
+
content = _state.sent();
|
|
712
683
|
if (this.options.schema) {
|
|
713
|
-
|
|
714
|
-
(
|
|
684
|
+
parsedResponse = this.parseResponse(content, this.options.schema);
|
|
685
|
+
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug('Execution finished and response parsed', {
|
|
686
|
+
agent: this.name
|
|
687
|
+
});
|
|
715
688
|
return [
|
|
716
689
|
2,
|
|
717
|
-
|
|
690
|
+
parsedResponse
|
|
718
691
|
];
|
|
719
692
|
} else {
|
|
720
|
-
(
|
|
721
|
-
|
|
693
|
+
(_this_options_logger2 = this.options.logger) === null || _this_options_logger2 === void 0 ? void 0 : _this_options_logger2.debug('Execution finished', {
|
|
694
|
+
agent: this.name
|
|
695
|
+
});
|
|
722
696
|
return [
|
|
723
697
|
2,
|
|
724
|
-
|
|
698
|
+
content
|
|
725
699
|
];
|
|
726
700
|
}
|
|
727
|
-
case
|
|
701
|
+
case 3:
|
|
728
702
|
error = _state.sent();
|
|
729
|
-
(
|
|
730
|
-
|
|
703
|
+
(_this_options_logger3 = this.options.logger) === null || _this_options_logger3 === void 0 ? void 0 : _this_options_logger3.error('Execution failed', {
|
|
704
|
+
agent: this.name,
|
|
705
|
+
error: _instanceof$3(error, Error) ? error.message : 'Unknown error',
|
|
706
|
+
errorStack: _instanceof$3(error, Error) ? error.stack : undefined
|
|
731
707
|
});
|
|
732
708
|
return [
|
|
733
709
|
2,
|
|
734
710
|
null
|
|
735
711
|
];
|
|
736
|
-
case
|
|
712
|
+
case 4:
|
|
737
713
|
return [
|
|
738
714
|
2
|
|
739
715
|
];
|
|
@@ -743,19 +719,15 @@ var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<
|
|
|
743
719
|
}
|
|
744
720
|
},
|
|
745
721
|
{
|
|
746
|
-
key: "
|
|
747
|
-
value: function
|
|
748
|
-
return _async_to_generator$
|
|
749
|
-
var
|
|
750
|
-
return _ts_generator$
|
|
722
|
+
key: "invokeModel",
|
|
723
|
+
value: function invokeModel(input) {
|
|
724
|
+
return _async_to_generator$3(function() {
|
|
725
|
+
var _this_options_logger, userInput, systemMessage, jsonSchema, isPrimitiveType, messages, _this_options_logger1, generateConfig, jsonSchema1, text;
|
|
726
|
+
return _ts_generator$3(this, function(_state) {
|
|
751
727
|
switch(_state.label){
|
|
752
728
|
case 0:
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
return tool.getDynamicTool();
|
|
756
|
-
});
|
|
757
|
-
// Add schema format instructions if schema is provided
|
|
758
|
-
schemaFormatInstructions = '';
|
|
729
|
+
userInput = this.resolveUserInput(input);
|
|
730
|
+
systemMessage = this.options.systemPrompt.generate();
|
|
759
731
|
if (this.options.schema) {
|
|
760
732
|
jsonSchema = v4.z.toJSONSchema(this.options.schema);
|
|
761
733
|
isPrimitiveType = [
|
|
@@ -764,40 +736,62 @@ var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<
|
|
|
764
736
|
'number',
|
|
765
737
|
'string'
|
|
766
738
|
].includes(jsonSchema.type);
|
|
767
|
-
jsonSchemaString = JSON.stringify(jsonSchema, null, 2).replace(/{/g, '{{').replace(/}/g, '}}');
|
|
768
739
|
if (isPrimitiveType) {
|
|
769
|
-
|
|
740
|
+
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>");
|
|
770
741
|
} else {
|
|
771
|
-
|
|
742
|
+
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>");
|
|
772
743
|
}
|
|
773
744
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
'
|
|
782
|
-
|
|
783
|
-
]
|
|
745
|
+
messages = [
|
|
746
|
+
{
|
|
747
|
+
content: systemMessage,
|
|
748
|
+
role: 'system'
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
content: userInput,
|
|
752
|
+
role: 'user'
|
|
753
|
+
}
|
|
754
|
+
];
|
|
755
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug('Invoking model...', {
|
|
756
|
+
agent: this.name,
|
|
757
|
+
hasSchema: !!this.options.schema
|
|
758
|
+
});
|
|
759
|
+
if (this.options.verbose) {
|
|
760
|
+
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug('Sending messages to model...', {
|
|
761
|
+
agent: this.name,
|
|
762
|
+
messages: messages
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
766
|
+
generateConfig = {
|
|
767
|
+
messages: messages,
|
|
768
|
+
model: this.options.model.getVercelModel()
|
|
769
|
+
};
|
|
770
|
+
// Prepare structured outputs configuration for better schema validation when schema is provided
|
|
771
|
+
// NOTE: OpenRouter supports structured outputs, but @openrouter/ai-sdk-provider (v0.7.3)
|
|
772
|
+
// doesn't pass through responseFormat parameter yet. Currently relies on prompt-based instructions.
|
|
773
|
+
// Once provider support is added, this will provide stronger guarantees than prompts alone.
|
|
774
|
+
// See: https://openrouter.ai/docs/features/structured-outputs
|
|
775
|
+
if (this.options.schema) {
|
|
776
|
+
jsonSchema1 = v4.z.toJSONSchema(this.options.schema);
|
|
777
|
+
generateConfig.responseFormat = {
|
|
778
|
+
json_schema: {
|
|
779
|
+
name: 'response',
|
|
780
|
+
schema: jsonSchema1,
|
|
781
|
+
strict: true
|
|
782
|
+
},
|
|
783
|
+
type: 'json_schema'
|
|
784
|
+
};
|
|
785
|
+
}
|
|
784
786
|
return [
|
|
785
787
|
4,
|
|
786
|
-
|
|
787
|
-
llm: model,
|
|
788
|
-
prompt: prompt,
|
|
789
|
-
tools: tools
|
|
790
|
-
})
|
|
788
|
+
ai.generateText(generateConfig)
|
|
791
789
|
];
|
|
792
790
|
case 1:
|
|
793
|
-
|
|
791
|
+
text = _state.sent().text;
|
|
794
792
|
return [
|
|
795
793
|
2,
|
|
796
|
-
|
|
797
|
-
agent: agent,
|
|
798
|
-
tools: tools,
|
|
799
|
-
verbose: this.options.verbose
|
|
800
|
-
})
|
|
794
|
+
text
|
|
801
795
|
];
|
|
802
796
|
}
|
|
803
797
|
});
|
|
@@ -805,28 +799,19 @@ var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<
|
|
|
805
799
|
}
|
|
806
800
|
},
|
|
807
801
|
{
|
|
808
|
-
key: "
|
|
809
|
-
value: function
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
if (silentMatch) {
|
|
821
|
-
return {
|
|
822
|
-
reason: silentMatch[1].trim(),
|
|
823
|
-
shouldRespond: false
|
|
824
|
-
};
|
|
802
|
+
key: "parseResponse",
|
|
803
|
+
value: function parseResponse(content, schema) {
|
|
804
|
+
try {
|
|
805
|
+
return new StructuredResponseParser(schema).parse(content);
|
|
806
|
+
} catch (error) {
|
|
807
|
+
var _this_options_logger;
|
|
808
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error('Failed to parse model response.', {
|
|
809
|
+
agent: this.name,
|
|
810
|
+
error: _instanceof$3(error, Error) ? error.message : 'Unknown error',
|
|
811
|
+
rawContent: content
|
|
812
|
+
});
|
|
813
|
+
throw new Error('Invalid response format from model.');
|
|
825
814
|
}
|
|
826
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error("[".concat(this.name, "] Agent output was missing 'RESPOND:' or 'SILENT:' prefix."), {
|
|
827
|
-
rawOutput: output
|
|
828
|
-
});
|
|
829
|
-
return null;
|
|
830
815
|
}
|
|
831
816
|
},
|
|
832
817
|
{
|
|
@@ -837,27 +822,12 @@ var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<
|
|
|
837
822
|
}
|
|
838
823
|
return 'Proceed with your instructions.';
|
|
839
824
|
}
|
|
840
|
-
},
|
|
841
|
-
{
|
|
842
|
-
key: "validateResponseContent",
|
|
843
|
-
value: function validateResponseContent(content, schema) {
|
|
844
|
-
try {
|
|
845
|
-
return new AIResponseParser(schema).parse(content);
|
|
846
|
-
} catch (error) {
|
|
847
|
-
var _this_options_logger;
|
|
848
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error("[".concat(this.name, "] Failed to validate response content against schema."), {
|
|
849
|
-
error: _instanceof$2(error, Error) ? error.message : 'Unknown error',
|
|
850
|
-
rawContent: content
|
|
851
|
-
});
|
|
852
|
-
throw new Error('Invalid response content from model.');
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
825
|
}
|
|
856
826
|
]);
|
|
857
|
-
return
|
|
827
|
+
return ChatAgent;
|
|
858
828
|
}();
|
|
859
829
|
|
|
860
|
-
function asyncGeneratorStep$
|
|
830
|
+
function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
861
831
|
try {
|
|
862
832
|
var info = gen[key](arg);
|
|
863
833
|
var value = info.value;
|
|
@@ -871,27 +841,27 @@ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
871
841
|
Promise.resolve(value).then(_next, _throw);
|
|
872
842
|
}
|
|
873
843
|
}
|
|
874
|
-
function _async_to_generator$
|
|
844
|
+
function _async_to_generator$2(fn) {
|
|
875
845
|
return function() {
|
|
876
846
|
var self = this, args = arguments;
|
|
877
847
|
return new Promise(function(resolve, reject) {
|
|
878
848
|
var gen = fn.apply(self, args);
|
|
879
849
|
function _next(value) {
|
|
880
|
-
asyncGeneratorStep$
|
|
850
|
+
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
|
|
881
851
|
}
|
|
882
852
|
function _throw(err) {
|
|
883
|
-
asyncGeneratorStep$
|
|
853
|
+
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
|
|
884
854
|
}
|
|
885
855
|
_next(undefined);
|
|
886
856
|
});
|
|
887
857
|
};
|
|
888
858
|
}
|
|
889
|
-
function _class_call_check$
|
|
859
|
+
function _class_call_check$6(instance, Constructor) {
|
|
890
860
|
if (!(instance instanceof Constructor)) {
|
|
891
861
|
throw new TypeError("Cannot call a class as a function");
|
|
892
862
|
}
|
|
893
863
|
}
|
|
894
|
-
function _defineProperties$
|
|
864
|
+
function _defineProperties$6(target, props) {
|
|
895
865
|
for(var i = 0; i < props.length; i++){
|
|
896
866
|
var descriptor = props[i];
|
|
897
867
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
@@ -900,11 +870,11 @@ function _defineProperties$4(target, props) {
|
|
|
900
870
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
901
871
|
}
|
|
902
872
|
}
|
|
903
|
-
function _create_class$
|
|
904
|
-
if (protoProps) _defineProperties$
|
|
873
|
+
function _create_class$6(Constructor, protoProps, staticProps) {
|
|
874
|
+
if (protoProps) _defineProperties$6(Constructor.prototype, protoProps);
|
|
905
875
|
return Constructor;
|
|
906
876
|
}
|
|
907
|
-
function _define_property$
|
|
877
|
+
function _define_property$6(obj, key, value) {
|
|
908
878
|
if (key in obj) {
|
|
909
879
|
Object.defineProperty(obj, key, {
|
|
910
880
|
value: value,
|
|
@@ -917,14 +887,14 @@ function _define_property$4(obj, key, value) {
|
|
|
917
887
|
}
|
|
918
888
|
return obj;
|
|
919
889
|
}
|
|
920
|
-
function _instanceof$
|
|
890
|
+
function _instanceof$2(left, right) {
|
|
921
891
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
922
892
|
return !!right[Symbol.hasInstance](left);
|
|
923
893
|
} else {
|
|
924
894
|
return left instanceof right;
|
|
925
895
|
}
|
|
926
896
|
}
|
|
927
|
-
function _ts_generator$
|
|
897
|
+
function _ts_generator$2(thisArg, body) {
|
|
928
898
|
var f, y, t, _ = {
|
|
929
899
|
label: 0,
|
|
930
900
|
sent: function() {
|
|
@@ -1016,85 +986,389 @@ function _ts_generator$1(thisArg, body) {
|
|
|
1016
986
|
}
|
|
1017
987
|
}
|
|
1018
988
|
/**
|
|
1019
|
-
* A
|
|
1020
|
-
*
|
|
989
|
+
* A decorator agent that adds retry logic to an existing agent for resilient execution.
|
|
990
|
+
* @template TInput - The TypeScript type of the input
|
|
1021
991
|
* @template TOutput - The TypeScript type of the output
|
|
1022
|
-
*/ var
|
|
1023
|
-
function
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
_define_property$
|
|
1027
|
-
this
|
|
1028
|
-
this
|
|
992
|
+
*/ var ResilientAgent = /*#__PURE__*/ function() {
|
|
993
|
+
function ResilientAgent(agent) {
|
|
994
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
995
|
+
_class_call_check$6(this, ResilientAgent);
|
|
996
|
+
_define_property$6(this, "agent", void 0);
|
|
997
|
+
_define_property$6(this, "name", void 0);
|
|
998
|
+
_define_property$6(this, "logger", void 0);
|
|
999
|
+
_define_property$6(this, "retries", void 0);
|
|
1000
|
+
this.agent = agent;
|
|
1001
|
+
var logger = options.logger, _options_retries = options.retries, retries = _options_retries === void 0 ? 1 : _options_retries;
|
|
1002
|
+
this.name = "Resilient(".concat(agent.name, ")");
|
|
1003
|
+
this.logger = logger;
|
|
1004
|
+
this.retries = retries;
|
|
1029
1005
|
}
|
|
1030
|
-
_create_class$
|
|
1006
|
+
_create_class$6(ResilientAgent, [
|
|
1031
1007
|
{
|
|
1032
1008
|
key: "run",
|
|
1033
1009
|
value: function run(input) {
|
|
1034
|
-
return _async_to_generator$
|
|
1035
|
-
var
|
|
1036
|
-
return _ts_generator$
|
|
1010
|
+
return _async_to_generator$2(function() {
|
|
1011
|
+
var _this_logger, maxAttempts, attempt, _this_logger1, _this_logger2, result, _this_logger3, error, _this_logger4;
|
|
1012
|
+
return _ts_generator$2(this, function(_state) {
|
|
1037
1013
|
switch(_state.label){
|
|
1038
1014
|
case 0:
|
|
1039
|
-
|
|
1015
|
+
maxAttempts = this.retries + 1;
|
|
1016
|
+
attempt = 1;
|
|
1040
1017
|
_state.label = 1;
|
|
1041
1018
|
case 1:
|
|
1042
|
-
|
|
1043
|
-
1,
|
|
1019
|
+
if (!(attempt <= maxAttempts)) return [
|
|
1044
1020
|
3,
|
|
1021
|
+
6
|
|
1022
|
+
];
|
|
1023
|
+
_state.label = 2;
|
|
1024
|
+
case 2:
|
|
1025
|
+
_state.trys.push([
|
|
1026
|
+
2,
|
|
1027
|
+
4,
|
|
1045
1028
|
,
|
|
1046
|
-
|
|
1029
|
+
5
|
|
1047
1030
|
]);
|
|
1031
|
+
(_this_logger1 = this.logger) === null || _this_logger1 === void 0 ? void 0 : _this_logger1.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts), {
|
|
1032
|
+
agent: this.name
|
|
1033
|
+
});
|
|
1048
1034
|
return [
|
|
1049
1035
|
4,
|
|
1050
|
-
this.
|
|
1036
|
+
this.agent.run(input)
|
|
1051
1037
|
];
|
|
1052
|
-
case
|
|
1053
|
-
|
|
1054
|
-
if (
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
2,
|
|
1059
|
-
parsedResponse
|
|
1060
|
-
];
|
|
1061
|
-
} else {
|
|
1062
|
-
(_this_options_logger2 = this.options.logger) === null || _this_options_logger2 === void 0 ? void 0 : _this_options_logger2.info("[".concat(this.name, "] Execution finished."));
|
|
1063
|
-
// When no schema is provided, we assume TOutput is string (default), so content is the result
|
|
1038
|
+
case 3:
|
|
1039
|
+
result = _state.sent();
|
|
1040
|
+
if (result !== null) {
|
|
1041
|
+
(_this_logger3 = this.logger) === null || _this_logger3 === void 0 ? void 0 : _this_logger3.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " succeeded"), {
|
|
1042
|
+
agent: this.name
|
|
1043
|
+
});
|
|
1064
1044
|
return [
|
|
1065
1045
|
2,
|
|
1066
|
-
|
|
1046
|
+
result
|
|
1067
1047
|
];
|
|
1068
1048
|
}
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
(_this_options_logger3 = this.options.logger) === null || _this_options_logger3 === void 0 ? void 0 : _this_options_logger3.error("[".concat(this.name, "] Execution failed."), {
|
|
1072
|
-
error: _instanceof$1(error, Error) ? error.message : 'Unknown error'
|
|
1049
|
+
(_this_logger2 = this.logger) === null || _this_logger2 === void 0 ? void 0 : _this_logger2.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " failed: agent returned null"), {
|
|
1050
|
+
agent: this.name
|
|
1073
1051
|
});
|
|
1074
1052
|
return [
|
|
1075
|
-
|
|
1076
|
-
|
|
1053
|
+
3,
|
|
1054
|
+
5
|
|
1077
1055
|
];
|
|
1078
1056
|
case 4:
|
|
1057
|
+
error = _state.sent();
|
|
1058
|
+
(_this_logger4 = this.logger) === null || _this_logger4 === void 0 ? void 0 : _this_logger4.debug("Attempt ".concat(attempt, " of ").concat(maxAttempts, " failed with an error"), {
|
|
1059
|
+
agent: this.name,
|
|
1060
|
+
error: _instanceof$2(error, Error) ? error.message : 'Unknown error'
|
|
1061
|
+
});
|
|
1079
1062
|
return [
|
|
1080
|
-
|
|
1063
|
+
3,
|
|
1064
|
+
5
|
|
1081
1065
|
];
|
|
1082
|
-
|
|
1083
|
-
|
|
1066
|
+
case 5:
|
|
1067
|
+
attempt++;
|
|
1068
|
+
return [
|
|
1069
|
+
3,
|
|
1070
|
+
1
|
|
1071
|
+
];
|
|
1072
|
+
case 6:
|
|
1073
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.error("All ".concat(maxAttempts, " attempts failed"), {
|
|
1074
|
+
agent: this.name
|
|
1075
|
+
});
|
|
1076
|
+
return [
|
|
1077
|
+
2,
|
|
1078
|
+
null
|
|
1079
|
+
];
|
|
1080
|
+
}
|
|
1081
|
+
});
|
|
1084
1082
|
}).call(this);
|
|
1085
1083
|
}
|
|
1084
|
+
}
|
|
1085
|
+
]);
|
|
1086
|
+
return ResilientAgent;
|
|
1087
|
+
}();
|
|
1088
|
+
|
|
1089
|
+
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1090
|
+
try {
|
|
1091
|
+
var info = gen[key](arg);
|
|
1092
|
+
var value = info.value;
|
|
1093
|
+
} catch (error) {
|
|
1094
|
+
reject(error);
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
if (info.done) {
|
|
1098
|
+
resolve(value);
|
|
1099
|
+
} else {
|
|
1100
|
+
Promise.resolve(value).then(_next, _throw);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
function _async_to_generator$1(fn) {
|
|
1104
|
+
return function() {
|
|
1105
|
+
var self = this, args = arguments;
|
|
1106
|
+
return new Promise(function(resolve, reject) {
|
|
1107
|
+
var gen = fn.apply(self, args);
|
|
1108
|
+
function _next(value) {
|
|
1109
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
|
1110
|
+
}
|
|
1111
|
+
function _throw(err) {
|
|
1112
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1113
|
+
}
|
|
1114
|
+
_next(undefined);
|
|
1115
|
+
});
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
function _class_call_check$5(instance, Constructor) {
|
|
1119
|
+
if (!(instance instanceof Constructor)) {
|
|
1120
|
+
throw new TypeError("Cannot call a class as a function");
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
function _defineProperties$5(target, props) {
|
|
1124
|
+
for(var i = 0; i < props.length; i++){
|
|
1125
|
+
var descriptor = props[i];
|
|
1126
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
1127
|
+
descriptor.configurable = true;
|
|
1128
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
1129
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
function _create_class$5(Constructor, protoProps, staticProps) {
|
|
1133
|
+
if (protoProps) _defineProperties$5(Constructor.prototype, protoProps);
|
|
1134
|
+
return Constructor;
|
|
1135
|
+
}
|
|
1136
|
+
function _define_property$5(obj, key, value) {
|
|
1137
|
+
if (key in obj) {
|
|
1138
|
+
Object.defineProperty(obj, key, {
|
|
1139
|
+
value: value,
|
|
1140
|
+
enumerable: true,
|
|
1141
|
+
configurable: true,
|
|
1142
|
+
writable: true
|
|
1143
|
+
});
|
|
1144
|
+
} else {
|
|
1145
|
+
obj[key] = value;
|
|
1146
|
+
}
|
|
1147
|
+
return obj;
|
|
1148
|
+
}
|
|
1149
|
+
function _instanceof$1(left, right) {
|
|
1150
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
1151
|
+
return !!right[Symbol.hasInstance](left);
|
|
1152
|
+
} else {
|
|
1153
|
+
return left instanceof right;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
function _ts_generator$1(thisArg, body) {
|
|
1157
|
+
var f, y, t, _ = {
|
|
1158
|
+
label: 0,
|
|
1159
|
+
sent: function() {
|
|
1160
|
+
if (t[0] & 1) throw t[1];
|
|
1161
|
+
return t[1];
|
|
1086
1162
|
},
|
|
1163
|
+
trys: [],
|
|
1164
|
+
ops: []
|
|
1165
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
1166
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
1167
|
+
return this;
|
|
1168
|
+
}), g;
|
|
1169
|
+
function verb(n) {
|
|
1170
|
+
return function(v) {
|
|
1171
|
+
return step([
|
|
1172
|
+
n,
|
|
1173
|
+
v
|
|
1174
|
+
]);
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
function step(op) {
|
|
1178
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
1179
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
1180
|
+
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;
|
|
1181
|
+
if (y = 0, t) op = [
|
|
1182
|
+
op[0] & 2,
|
|
1183
|
+
t.value
|
|
1184
|
+
];
|
|
1185
|
+
switch(op[0]){
|
|
1186
|
+
case 0:
|
|
1187
|
+
case 1:
|
|
1188
|
+
t = op;
|
|
1189
|
+
break;
|
|
1190
|
+
case 4:
|
|
1191
|
+
_.label++;
|
|
1192
|
+
return {
|
|
1193
|
+
value: op[1],
|
|
1194
|
+
done: false
|
|
1195
|
+
};
|
|
1196
|
+
case 5:
|
|
1197
|
+
_.label++;
|
|
1198
|
+
y = op[1];
|
|
1199
|
+
op = [
|
|
1200
|
+
0
|
|
1201
|
+
];
|
|
1202
|
+
continue;
|
|
1203
|
+
case 7:
|
|
1204
|
+
op = _.ops.pop();
|
|
1205
|
+
_.trys.pop();
|
|
1206
|
+
continue;
|
|
1207
|
+
default:
|
|
1208
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
1209
|
+
_ = 0;
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
1213
|
+
_.label = op[1];
|
|
1214
|
+
break;
|
|
1215
|
+
}
|
|
1216
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
1217
|
+
_.label = t[1];
|
|
1218
|
+
t = op;
|
|
1219
|
+
break;
|
|
1220
|
+
}
|
|
1221
|
+
if (t && _.label < t[2]) {
|
|
1222
|
+
_.label = t[2];
|
|
1223
|
+
_.ops.push(op);
|
|
1224
|
+
break;
|
|
1225
|
+
}
|
|
1226
|
+
if (t[2]) _.ops.pop();
|
|
1227
|
+
_.trys.pop();
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1230
|
+
op = body.call(thisArg, _);
|
|
1231
|
+
} catch (e) {
|
|
1232
|
+
op = [
|
|
1233
|
+
6,
|
|
1234
|
+
e
|
|
1235
|
+
];
|
|
1236
|
+
y = 0;
|
|
1237
|
+
} finally{
|
|
1238
|
+
f = t = 0;
|
|
1239
|
+
}
|
|
1240
|
+
if (op[0] & 5) throw op[1];
|
|
1241
|
+
return {
|
|
1242
|
+
value: op[0] ? op[1] : void 0,
|
|
1243
|
+
done: true
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
var SYSTEM_PROMPT_TEMPLATE = '\n<OBJECTIVE>\n{mission_prompt}\n</OBJECTIVE>\n\n<GLOBAL_WRAPPER_OUTPUT_FORMAT>\nCRITICAL: The format instructions in this section are the ONLY valid way to structure your response. Your entire response MUST be a single JSON markdown code block. Any formatting guidelines within the <OBJECTIVE> section apply ONLY to the content inside the "RESPOND:" part of your final "action_input".\n\nREQUIRED: You have two ways to respond:\n\n1. **Call a tool** to gather information. For this, you MUST output a JSON blob with the tool\'s name and its input.\n *Valid tool names are: {tool_names}*\n ```json\n {{\n "action": "tool_name_to_use",\n "action_input": "the input for the tool, or an empty object {{}} if no input is needed"\n }}\n ```\n\n2. **Provide the Final Answer** once you have enough information. For this, you MUST output a JSON blob with the "Final Answer" action.\n The "action_input" for a "Final Answer" MUST be a string that begins with either "RESPOND: " for a message or "SILENT: " for no message. This prefix is a literal part of the output string and MUST NOT be omitted.\n - To send a message:\n ```json\n {{\n "action": "Final Answer",\n "action_input": "RESPOND: <your response message>"\n }}\n ```\n - To stay silent:\n ```json\n {{\n "action": "Final Answer",\n "action_input": "SILENT: <your reason for staying silent>"\n }}\n ```\n\n YOU MUST ALWAYS INCLUDE "RESPOND:" OR "SILENT:" IN YOUR FINAL ANSWER\'S "action_input". FAILURE TO DO SO WILL CAUSE AN ERROR.\n\n{schema_format}\n</OUTPUT_FORMAT>\n\n<EXECUTION_CONTEXT>\nThis is internal data for your reference.\n\n<TOOLS>\n{tools}\n</TOOLS>\n\n<WORKING_MEMORY>\nThis is your internal thought process and previous tool usage.\n{agent_scratchpad}\n</WORKING_MEMORY>\n</EXECUTION_CONTEXT>\n';
|
|
1248
|
+
/**
|
|
1249
|
+
* A tool-enabled agent that uses tools and a structured prompt to accomplish tasks.
|
|
1250
|
+
* It can decide whether to respond or remain silent and supports schema-validated responses.
|
|
1251
|
+
* @template TOutput - The TypeScript type of the output
|
|
1252
|
+
*/ var ToolAgent = /*#__PURE__*/ function() {
|
|
1253
|
+
function ToolAgent(name, options) {
|
|
1254
|
+
_class_call_check$5(this, ToolAgent);
|
|
1255
|
+
_define_property$5(this, "name", void 0);
|
|
1256
|
+
_define_property$5(this, "options", void 0);
|
|
1257
|
+
this.name = name;
|
|
1258
|
+
this.options = options;
|
|
1259
|
+
}
|
|
1260
|
+
_create_class$5(ToolAgent, [
|
|
1087
1261
|
{
|
|
1088
|
-
key: "
|
|
1089
|
-
value: function
|
|
1262
|
+
key: "run",
|
|
1263
|
+
value: function run(input) {
|
|
1090
1264
|
return _async_to_generator$1(function() {
|
|
1091
|
-
var _this_options_logger, userInput,
|
|
1265
|
+
var _this_options_logger, _this_options_logger1, executor, userInput, result, agentResponse, _this_options_logger2, _agentResponse_message, message, _this_options_logger3, validatedResponse, _this_options_logger4, error, _this_options_logger5;
|
|
1092
1266
|
return _ts_generator$1(this, function(_state) {
|
|
1093
1267
|
switch(_state.label){
|
|
1094
1268
|
case 0:
|
|
1269
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug('Starting chat execution', {
|
|
1270
|
+
agent: this.name
|
|
1271
|
+
});
|
|
1272
|
+
_state.label = 1;
|
|
1273
|
+
case 1:
|
|
1274
|
+
_state.trys.push([
|
|
1275
|
+
1,
|
|
1276
|
+
4,
|
|
1277
|
+
,
|
|
1278
|
+
5
|
|
1279
|
+
]);
|
|
1280
|
+
return [
|
|
1281
|
+
4,
|
|
1282
|
+
this.createExecutor()
|
|
1283
|
+
];
|
|
1284
|
+
case 2:
|
|
1285
|
+
executor = _state.sent();
|
|
1095
1286
|
userInput = this.resolveUserInput(input);
|
|
1096
|
-
|
|
1097
|
-
|
|
1287
|
+
return [
|
|
1288
|
+
4,
|
|
1289
|
+
executor.invoke({
|
|
1290
|
+
input: userInput
|
|
1291
|
+
})
|
|
1292
|
+
];
|
|
1293
|
+
case 3:
|
|
1294
|
+
result = _state.sent();
|
|
1295
|
+
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.debug('Agent execution completed', {
|
|
1296
|
+
agent: this.name,
|
|
1297
|
+
hasOutput: 'output' in result
|
|
1298
|
+
});
|
|
1299
|
+
if (!result || typeof result.output !== 'string') {
|
|
1300
|
+
throw new Error('Agent returned an invalid result structure.');
|
|
1301
|
+
}
|
|
1302
|
+
agentResponse = this.parseAgentOutput(result.output);
|
|
1303
|
+
if (!agentResponse) {
|
|
1304
|
+
return [
|
|
1305
|
+
2,
|
|
1306
|
+
null
|
|
1307
|
+
];
|
|
1308
|
+
}
|
|
1309
|
+
if (!agentResponse.shouldRespond) {
|
|
1310
|
+
(_this_options_logger2 = this.options.logger) === null || _this_options_logger2 === void 0 ? void 0 : _this_options_logger2.debug('Agent chose to remain silent', {
|
|
1311
|
+
agent: this.name,
|
|
1312
|
+
reason: agentResponse.reason
|
|
1313
|
+
});
|
|
1314
|
+
return [
|
|
1315
|
+
2,
|
|
1316
|
+
null
|
|
1317
|
+
];
|
|
1318
|
+
}
|
|
1319
|
+
message = (_agentResponse_message = agentResponse.message) !== null && _agentResponse_message !== void 0 ? _agentResponse_message : '';
|
|
1320
|
+
if (this.options.schema) {
|
|
1321
|
+
validatedResponse = this.validateResponseContent(message, this.options.schema);
|
|
1322
|
+
(_this_options_logger3 = this.options.logger) === null || _this_options_logger3 === void 0 ? void 0 : _this_options_logger3.debug('Execution finished; response content validated.', {
|
|
1323
|
+
agent: this.name
|
|
1324
|
+
});
|
|
1325
|
+
return [
|
|
1326
|
+
2,
|
|
1327
|
+
validatedResponse
|
|
1328
|
+
];
|
|
1329
|
+
} else {
|
|
1330
|
+
(_this_options_logger4 = this.options.logger) === null || _this_options_logger4 === void 0 ? void 0 : _this_options_logger4.debug('Execution finished', {
|
|
1331
|
+
agent: this.name
|
|
1332
|
+
});
|
|
1333
|
+
// When no schema is provided, we assume TOutput is string (default), so message is the result
|
|
1334
|
+
return [
|
|
1335
|
+
2,
|
|
1336
|
+
message
|
|
1337
|
+
];
|
|
1338
|
+
}
|
|
1339
|
+
case 4:
|
|
1340
|
+
error = _state.sent();
|
|
1341
|
+
(_this_options_logger5 = this.options.logger) === null || _this_options_logger5 === void 0 ? void 0 : _this_options_logger5.error('Chat execution failed', {
|
|
1342
|
+
agent: this.name,
|
|
1343
|
+
error: _instanceof$1(error, Error) ? error.message : 'Unknown error'
|
|
1344
|
+
});
|
|
1345
|
+
return [
|
|
1346
|
+
2,
|
|
1347
|
+
null
|
|
1348
|
+
];
|
|
1349
|
+
case 5:
|
|
1350
|
+
return [
|
|
1351
|
+
2
|
|
1352
|
+
];
|
|
1353
|
+
}
|
|
1354
|
+
});
|
|
1355
|
+
}).call(this);
|
|
1356
|
+
}
|
|
1357
|
+
},
|
|
1358
|
+
{
|
|
1359
|
+
key: "createExecutor",
|
|
1360
|
+
value: function createExecutor() {
|
|
1361
|
+
return _async_to_generator$1(function() {
|
|
1362
|
+
var model, tools, schemaFormatInstructions, jsonSchema, isPrimitiveType, jsonSchemaString, prompt, agent;
|
|
1363
|
+
return _ts_generator$1(this, function(_state) {
|
|
1364
|
+
switch(_state.label){
|
|
1365
|
+
case 0:
|
|
1366
|
+
model = this.options.model.getLangchainModel();
|
|
1367
|
+
tools = this.options.tools.map(function(tool) {
|
|
1368
|
+
return tool.getDynamicTool();
|
|
1369
|
+
});
|
|
1370
|
+
// Add schema format instructions if schema is provided
|
|
1371
|
+
schemaFormatInstructions = '';
|
|
1098
1372
|
if (this.options.schema) {
|
|
1099
1373
|
jsonSchema = v4.z.toJSONSchema(this.options.schema);
|
|
1100
1374
|
isPrimitiveType = [
|
|
@@ -1103,43 +1377,40 @@ function _ts_generator$1(thisArg, body) {
|
|
|
1103
1377
|
'number',
|
|
1104
1378
|
'string'
|
|
1105
1379
|
].includes(jsonSchema.type);
|
|
1380
|
+
jsonSchemaString = JSON.stringify(jsonSchema, null, 2).replace(/{/g, '{{').replace(/}/g, '}}');
|
|
1106
1381
|
if (isPrimitiveType) {
|
|
1107
|
-
|
|
1382
|
+
schemaFormatInstructions = '\n\nSCHEMA VALIDATION: When providing a "RESPOND:" answer, the content after "RESPOND: " must be a '.concat(jsonSchema.type, " value that matches this schema description:\n\n```json\n").concat(jsonSchemaString, '\n```\n\nExample format:\n```json\n{{\n "action": "Final Answer",\n "action_input": "RESPOND: your ').concat(jsonSchema.type, ' value here"\n}}\n```\n\nDo not wrap the ').concat(jsonSchema.type, ' value in JSON - just provide the raw value after "RESPOND: ".');
|
|
1108
1383
|
} else {
|
|
1109
|
-
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
messages = [
|
|
1113
|
-
{
|
|
1114
|
-
content: systemMessage,
|
|
1115
|
-
role: 'system'
|
|
1116
|
-
},
|
|
1117
|
-
{
|
|
1118
|
-
content: userInput,
|
|
1119
|
-
role: 'user'
|
|
1384
|
+
schemaFormatInstructions = '\n\nSCHEMA VALIDATION: When providing a "RESPOND:" answer, the content after "RESPOND: " must be valid JSON that matches this JSON schema description:\n\n```json\n'.concat(jsonSchemaString, '\n```\n\nExample format:\n```json\n{{\n "action": "Final Answer",\n "action_input": "RESPOND: {{\\"field1\\": \\"value1\\", \\"field2\\": \\"value2\\"}}"\n}}\n```\n');
|
|
1120
1385
|
}
|
|
1121
|
-
];
|
|
1122
|
-
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.debug("[".concat(this.name, "] Invoking model..."), {
|
|
1123
|
-
hasSchema: !!this.options.schema
|
|
1124
|
-
});
|
|
1125
|
-
if (this.options.verbose) {
|
|
1126
|
-
(_this_options_logger1 = this.options.logger) === null || _this_options_logger1 === void 0 ? void 0 : _this_options_logger1.info("[".concat(this.name, "] Sending messages to model..."), {
|
|
1127
|
-
messages: messages
|
|
1128
|
-
});
|
|
1129
1386
|
}
|
|
1387
|
+
prompt = prompts.ChatPromptTemplate.fromMessages([
|
|
1388
|
+
[
|
|
1389
|
+
'system',
|
|
1390
|
+
SYSTEM_PROMPT_TEMPLATE.replace('{mission_prompt}', this.options.systemPrompt.generate()).replace('{schema_format}', schemaFormatInstructions)
|
|
1391
|
+
],
|
|
1392
|
+
[
|
|
1393
|
+
'human',
|
|
1394
|
+
'{input}'
|
|
1395
|
+
]
|
|
1396
|
+
]);
|
|
1130
1397
|
return [
|
|
1131
1398
|
4,
|
|
1132
|
-
|
|
1399
|
+
agents.createStructuredChatAgent({
|
|
1400
|
+
llm: model,
|
|
1401
|
+
prompt: prompt,
|
|
1402
|
+
tools: tools
|
|
1403
|
+
})
|
|
1133
1404
|
];
|
|
1134
1405
|
case 1:
|
|
1135
|
-
|
|
1136
|
-
content = response.content;
|
|
1137
|
-
if (typeof content !== 'string') {
|
|
1138
|
-
throw new Error('Model returned a non-string content type.');
|
|
1139
|
-
}
|
|
1406
|
+
agent = _state.sent();
|
|
1140
1407
|
return [
|
|
1141
1408
|
2,
|
|
1142
|
-
|
|
1409
|
+
agents.AgentExecutor.fromAgentAndTools({
|
|
1410
|
+
agent: agent,
|
|
1411
|
+
tools: tools,
|
|
1412
|
+
verbose: this.options.verbose
|
|
1413
|
+
})
|
|
1143
1414
|
];
|
|
1144
1415
|
}
|
|
1145
1416
|
});
|
|
@@ -1147,18 +1418,29 @@ function _ts_generator$1(thisArg, body) {
|
|
|
1147
1418
|
}
|
|
1148
1419
|
},
|
|
1149
1420
|
{
|
|
1150
|
-
key: "
|
|
1151
|
-
value: function
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1421
|
+
key: "parseAgentOutput",
|
|
1422
|
+
value: function parseAgentOutput(output) {
|
|
1423
|
+
var _this_options_logger;
|
|
1424
|
+
var text = output.trim();
|
|
1425
|
+
var respondMatch = text.match(/^RESPOND:\s*([\s\S]+)$/i);
|
|
1426
|
+
if (respondMatch) {
|
|
1427
|
+
return {
|
|
1428
|
+
message: respondMatch[1].trim(),
|
|
1429
|
+
shouldRespond: true
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
var silentMatch = text.match(/^SILENT:\s*([\s\S]+)$/i);
|
|
1433
|
+
if (silentMatch) {
|
|
1434
|
+
return {
|
|
1435
|
+
reason: silentMatch[1].trim(),
|
|
1436
|
+
shouldRespond: false
|
|
1437
|
+
};
|
|
1161
1438
|
}
|
|
1439
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error("Agent output was missing 'RESPOND:' or 'SILENT:' prefix.", {
|
|
1440
|
+
agent: this.name,
|
|
1441
|
+
rawOutput: output
|
|
1442
|
+
});
|
|
1443
|
+
return null;
|
|
1162
1444
|
}
|
|
1163
1445
|
},
|
|
1164
1446
|
{
|
|
@@ -1169,17 +1451,33 @@ function _ts_generator$1(thisArg, body) {
|
|
|
1169
1451
|
}
|
|
1170
1452
|
return 'Proceed with your instructions.';
|
|
1171
1453
|
}
|
|
1454
|
+
},
|
|
1455
|
+
{
|
|
1456
|
+
key: "validateResponseContent",
|
|
1457
|
+
value: function validateResponseContent(content, schema) {
|
|
1458
|
+
try {
|
|
1459
|
+
return new StructuredResponseParser(schema).parse(content);
|
|
1460
|
+
} catch (error) {
|
|
1461
|
+
var _this_options_logger;
|
|
1462
|
+
(_this_options_logger = this.options.logger) === null || _this_options_logger === void 0 ? void 0 : _this_options_logger.error('Failed to validate response content against schema.', {
|
|
1463
|
+
agent: this.name,
|
|
1464
|
+
error: _instanceof$1(error, Error) ? error.message : 'Unknown error',
|
|
1465
|
+
rawContent: content
|
|
1466
|
+
});
|
|
1467
|
+
throw new Error('Invalid response content from model.');
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1172
1470
|
}
|
|
1173
1471
|
]);
|
|
1174
|
-
return
|
|
1472
|
+
return ToolAgent;
|
|
1175
1473
|
}();
|
|
1176
1474
|
|
|
1177
|
-
function _class_call_check$
|
|
1475
|
+
function _class_call_check$4(instance, Constructor) {
|
|
1178
1476
|
if (!(instance instanceof Constructor)) {
|
|
1179
1477
|
throw new TypeError("Cannot call a class as a function");
|
|
1180
1478
|
}
|
|
1181
1479
|
}
|
|
1182
|
-
function _defineProperties$
|
|
1480
|
+
function _defineProperties$4(target, props) {
|
|
1183
1481
|
for(var i = 0; i < props.length; i++){
|
|
1184
1482
|
var descriptor = props[i];
|
|
1185
1483
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
@@ -1188,11 +1486,11 @@ function _defineProperties$3(target, props) {
|
|
|
1188
1486
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1189
1487
|
}
|
|
1190
1488
|
}
|
|
1191
|
-
function _create_class$
|
|
1192
|
-
if (protoProps) _defineProperties$
|
|
1489
|
+
function _create_class$4(Constructor, protoProps, staticProps) {
|
|
1490
|
+
if (protoProps) _defineProperties$4(Constructor.prototype, protoProps);
|
|
1193
1491
|
return Constructor;
|
|
1194
1492
|
}
|
|
1195
|
-
function _define_property$
|
|
1493
|
+
function _define_property$4(obj, key, value) {
|
|
1196
1494
|
if (key in obj) {
|
|
1197
1495
|
Object.defineProperty(obj, key, {
|
|
1198
1496
|
value: value,
|
|
@@ -1215,48 +1513,101 @@ function _object_spread(target) {
|
|
|
1215
1513
|
}));
|
|
1216
1514
|
}
|
|
1217
1515
|
ownKeys.forEach(function(key) {
|
|
1218
|
-
_define_property$
|
|
1516
|
+
_define_property$4(target, key, source[key]);
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
return target;
|
|
1520
|
+
}
|
|
1521
|
+
function ownKeys(object, enumerableOnly) {
|
|
1522
|
+
var keys = Object.keys(object);
|
|
1523
|
+
if (Object.getOwnPropertySymbols) {
|
|
1524
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
1525
|
+
keys.push.apply(keys, symbols);
|
|
1526
|
+
}
|
|
1527
|
+
return keys;
|
|
1528
|
+
}
|
|
1529
|
+
function _object_spread_props(target, source) {
|
|
1530
|
+
source = source != null ? source : {};
|
|
1531
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
1532
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
1533
|
+
} else {
|
|
1534
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
1535
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
1219
1536
|
});
|
|
1220
1537
|
}
|
|
1221
1538
|
return target;
|
|
1222
1539
|
}
|
|
1223
1540
|
/**
|
|
1224
|
-
* OpenRouter
|
|
1225
|
-
*/ var
|
|
1226
|
-
function
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
this
|
|
1541
|
+
* OpenRouter model wrapper supporting both LangChain and Vercel AI SDK
|
|
1542
|
+
*/ var OpenRouterModel = /*#__PURE__*/ function() {
|
|
1543
|
+
function OpenRouterModel(providerConfig, modelName) {
|
|
1544
|
+
var modelConfig = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
1545
|
+
_class_call_check$4(this, OpenRouterModel);
|
|
1546
|
+
var _this_providerConfig_metadata, _this_providerConfig_metadata1;
|
|
1547
|
+
_define_property$4(this, "providerConfig", void 0);
|
|
1548
|
+
_define_property$4(this, "modelName", void 0);
|
|
1549
|
+
_define_property$4(this, "langchainModel", void 0);
|
|
1550
|
+
_define_property$4(this, "modelConfig", void 0);
|
|
1551
|
+
_define_property$4(this, "vercelModel", void 0);
|
|
1552
|
+
this.providerConfig = providerConfig;
|
|
1553
|
+
this.modelName = modelName;
|
|
1554
|
+
this.modelConfig = _object_spread({
|
|
1555
|
+
maxTokens: 256000,
|
|
1556
|
+
reasoning: {
|
|
1557
|
+
effort: 'high',
|
|
1558
|
+
exclude: true
|
|
1559
|
+
}
|
|
1560
|
+
}, modelConfig);
|
|
1561
|
+
// LangChain model setup
|
|
1562
|
+
this.langchainModel = new openai.ChatOpenAI(_object_spread_props(_object_spread({
|
|
1232
1563
|
configuration: {
|
|
1233
1564
|
baseURL: 'https://openrouter.ai/api/v1',
|
|
1234
|
-
defaultHeaders: _object_spread({}, ((
|
|
1235
|
-
'HTTP-Referer':
|
|
1236
|
-
}, ((
|
|
1237
|
-
'X-Title':
|
|
1565
|
+
defaultHeaders: _object_spread({}, ((_this_providerConfig_metadata = this.providerConfig.metadata) === null || _this_providerConfig_metadata === void 0 ? void 0 : _this_providerConfig_metadata.website) && {
|
|
1566
|
+
'HTTP-Referer': this.providerConfig.metadata.website
|
|
1567
|
+
}, ((_this_providerConfig_metadata1 = this.providerConfig.metadata) === null || _this_providerConfig_metadata1 === void 0 ? void 0 : _this_providerConfig_metadata1.application) && {
|
|
1568
|
+
'X-Title': this.providerConfig.metadata.application
|
|
1238
1569
|
})
|
|
1239
1570
|
},
|
|
1240
|
-
maxTokens:
|
|
1571
|
+
maxTokens: this.modelConfig.maxTokens
|
|
1572
|
+
}, this.modelConfig.reasoning && {
|
|
1241
1573
|
modelKwargs: {
|
|
1242
|
-
reasoning:
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1574
|
+
reasoning: this.modelConfig.reasoning
|
|
1575
|
+
}
|
|
1576
|
+
}), {
|
|
1577
|
+
modelName: this.modelName,
|
|
1578
|
+
openAIApiKey: this.providerConfig.apiKey
|
|
1579
|
+
}));
|
|
1580
|
+
// Vercel AI SDK model setup
|
|
1581
|
+
var openrouter = aiSdkProvider.createOpenRouter({
|
|
1582
|
+
apiKey: this.providerConfig.apiKey
|
|
1249
1583
|
});
|
|
1584
|
+
this.vercelModel = openrouter(this.modelName, _object_spread({}, this.modelConfig.maxTokens && {
|
|
1585
|
+
maxTokens: this.modelConfig.maxTokens
|
|
1586
|
+
}, this.modelConfig.reasoning && {
|
|
1587
|
+
extraBody: {
|
|
1588
|
+
reasoning: this.modelConfig.reasoning
|
|
1589
|
+
}
|
|
1590
|
+
}));
|
|
1250
1591
|
}
|
|
1251
|
-
_create_class$
|
|
1592
|
+
_create_class$4(OpenRouterModel, [
|
|
1252
1593
|
{
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1594
|
+
/**
|
|
1595
|
+
* Get the configured LangChain language model instance
|
|
1596
|
+
*/ key: "getLangchainModel",
|
|
1597
|
+
value: function getLangchainModel() {
|
|
1598
|
+
return this.langchainModel;
|
|
1599
|
+
}
|
|
1600
|
+
},
|
|
1601
|
+
{
|
|
1602
|
+
/**
|
|
1603
|
+
* Get the configured Vercel AI SDK language model instance
|
|
1604
|
+
*/ key: "getVercelModel",
|
|
1605
|
+
value: function getVercelModel() {
|
|
1606
|
+
return this.vercelModel;
|
|
1256
1607
|
}
|
|
1257
1608
|
}
|
|
1258
1609
|
]);
|
|
1259
|
-
return
|
|
1610
|
+
return OpenRouterModel;
|
|
1260
1611
|
}();
|
|
1261
1612
|
|
|
1262
1613
|
/**
|
|
@@ -1391,6 +1742,60 @@ var PROMPT_LIBRARY = {
|
|
|
1391
1742
|
VERBOSITY: VERBOSITY
|
|
1392
1743
|
};
|
|
1393
1744
|
|
|
1745
|
+
function _class_call_check$3(instance, Constructor) {
|
|
1746
|
+
if (!(instance instanceof Constructor)) {
|
|
1747
|
+
throw new TypeError("Cannot call a class as a function");
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
function _defineProperties$3(target, props) {
|
|
1751
|
+
for(var i = 0; i < props.length; i++){
|
|
1752
|
+
var descriptor = props[i];
|
|
1753
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
1754
|
+
descriptor.configurable = true;
|
|
1755
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
1756
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
function _create_class$3(Constructor, protoProps, staticProps) {
|
|
1760
|
+
if (protoProps) _defineProperties$3(Constructor.prototype, protoProps);
|
|
1761
|
+
return Constructor;
|
|
1762
|
+
}
|
|
1763
|
+
function _define_property$3(obj, key, value) {
|
|
1764
|
+
if (key in obj) {
|
|
1765
|
+
Object.defineProperty(obj, key, {
|
|
1766
|
+
value: value,
|
|
1767
|
+
enumerable: true,
|
|
1768
|
+
configurable: true,
|
|
1769
|
+
writable: true
|
|
1770
|
+
});
|
|
1771
|
+
} else {
|
|
1772
|
+
obj[key] = value;
|
|
1773
|
+
}
|
|
1774
|
+
return obj;
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* System prompt that generates a system prompt from a list of strings
|
|
1778
|
+
*/ var SystemPrompt = /*#__PURE__*/ function() {
|
|
1779
|
+
function SystemPrompt() {
|
|
1780
|
+
for(var _len = arguments.length, prompts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
1781
|
+
prompts[_key] = arguments[_key];
|
|
1782
|
+
}
|
|
1783
|
+
_class_call_check$3(this, SystemPrompt);
|
|
1784
|
+
_define_property$3(this, "finalPrompt", void 0);
|
|
1785
|
+
var flattenedPrompts = prompts.flat();
|
|
1786
|
+
this.finalPrompt = flattenedPrompts.join('\n\n');
|
|
1787
|
+
}
|
|
1788
|
+
_create_class$3(SystemPrompt, [
|
|
1789
|
+
{
|
|
1790
|
+
key: "generate",
|
|
1791
|
+
value: function generate() {
|
|
1792
|
+
return this.finalPrompt;
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
]);
|
|
1796
|
+
return SystemPrompt;
|
|
1797
|
+
}();
|
|
1798
|
+
|
|
1394
1799
|
function _class_call_check$2(instance, Constructor) {
|
|
1395
1800
|
if (!(instance instanceof Constructor)) {
|
|
1396
1801
|
throw new TypeError("Cannot call a class as a function");
|
|
@@ -1423,18 +1828,18 @@ function _define_property$2(obj, key, value) {
|
|
|
1423
1828
|
return obj;
|
|
1424
1829
|
}
|
|
1425
1830
|
/**
|
|
1426
|
-
*
|
|
1427
|
-
*/ var
|
|
1428
|
-
function
|
|
1831
|
+
* User prompt that generates a user prompt from a list of strings
|
|
1832
|
+
*/ var UserPrompt = /*#__PURE__*/ function() {
|
|
1833
|
+
function UserPrompt() {
|
|
1429
1834
|
for(var _len = arguments.length, prompts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
1430
1835
|
prompts[_key] = arguments[_key];
|
|
1431
1836
|
}
|
|
1432
|
-
_class_call_check$2(this,
|
|
1837
|
+
_class_call_check$2(this, UserPrompt);
|
|
1433
1838
|
_define_property$2(this, "finalPrompt", void 0);
|
|
1434
1839
|
var flattenedPrompts = prompts.flat();
|
|
1435
1840
|
this.finalPrompt = flattenedPrompts.join('\n\n');
|
|
1436
1841
|
}
|
|
1437
|
-
_create_class$2(
|
|
1842
|
+
_create_class$2(UserPrompt, [
|
|
1438
1843
|
{
|
|
1439
1844
|
key: "generate",
|
|
1440
1845
|
value: function generate() {
|
|
@@ -1442,7 +1847,7 @@ function _define_property$2(obj, key, value) {
|
|
|
1442
1847
|
}
|
|
1443
1848
|
}
|
|
1444
1849
|
]);
|
|
1445
|
-
return
|
|
1850
|
+
return UserPrompt;
|
|
1446
1851
|
}();
|
|
1447
1852
|
|
|
1448
1853
|
function _class_call_check$1(instance, Constructor) {
|
|
@@ -1477,26 +1882,25 @@ function _define_property$1(obj, key, value) {
|
|
|
1477
1882
|
return obj;
|
|
1478
1883
|
}
|
|
1479
1884
|
/**
|
|
1480
|
-
*
|
|
1481
|
-
*/ var
|
|
1482
|
-
function
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
_class_call_check$1(this, UserPromptAdapter);
|
|
1487
|
-
_define_property$1(this, "finalPrompt", void 0);
|
|
1488
|
-
var flattenedPrompts = prompts.flat();
|
|
1489
|
-
this.finalPrompt = flattenedPrompts.join('\n\n');
|
|
1885
|
+
* OpenRouter provider that manages connection configuration
|
|
1886
|
+
*/ var OpenRouterProvider = /*#__PURE__*/ function() {
|
|
1887
|
+
function OpenRouterProvider(config) {
|
|
1888
|
+
_class_call_check$1(this, OpenRouterProvider);
|
|
1889
|
+
_define_property$1(this, "config", void 0);
|
|
1890
|
+
this.config = config;
|
|
1490
1891
|
}
|
|
1491
|
-
_create_class$1(
|
|
1892
|
+
_create_class$1(OpenRouterProvider, [
|
|
1492
1893
|
{
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1894
|
+
/**
|
|
1895
|
+
* Get a model instance for the specified model name
|
|
1896
|
+
*/ key: "getModel",
|
|
1897
|
+
value: function getModel(modelName) {
|
|
1898
|
+
var modelConfig = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
1899
|
+
return new OpenRouterModel(this.config, modelName, modelConfig);
|
|
1496
1900
|
}
|
|
1497
1901
|
}
|
|
1498
1902
|
]);
|
|
1499
|
-
return
|
|
1903
|
+
return OpenRouterProvider;
|
|
1500
1904
|
}();
|
|
1501
1905
|
|
|
1502
1906
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
@@ -1658,11 +2062,11 @@ function _ts_generator(thisArg, body) {
|
|
|
1658
2062
|
}
|
|
1659
2063
|
}
|
|
1660
2064
|
/**
|
|
1661
|
-
* Safe tool
|
|
1662
|
-
*/ var
|
|
1663
|
-
function
|
|
2065
|
+
* Safe tool that provides error handling and logging for LangChain tools
|
|
2066
|
+
*/ var SafeTool = /*#__PURE__*/ function() {
|
|
2067
|
+
function SafeTool(config) {
|
|
1664
2068
|
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
1665
|
-
_class_call_check(this,
|
|
2069
|
+
_class_call_check(this, SafeTool);
|
|
1666
2070
|
_define_property(this, "config", void 0);
|
|
1667
2071
|
_define_property(this, "options", void 0);
|
|
1668
2072
|
_define_property(this, "dynamicTool", void 0);
|
|
@@ -1765,7 +2169,7 @@ function _ts_generator(thisArg, body) {
|
|
|
1765
2169
|
});
|
|
1766
2170
|
}
|
|
1767
2171
|
}
|
|
1768
|
-
_create_class(
|
|
2172
|
+
_create_class(SafeTool, [
|
|
1769
2173
|
{
|
|
1770
2174
|
/**
|
|
1771
2175
|
* Get the underlying LangChain DynamicTool instance
|
|
@@ -1775,14 +2179,16 @@ function _ts_generator(thisArg, body) {
|
|
|
1775
2179
|
}
|
|
1776
2180
|
}
|
|
1777
2181
|
]);
|
|
1778
|
-
return
|
|
2182
|
+
return SafeTool;
|
|
1779
2183
|
}();
|
|
1780
2184
|
|
|
1781
|
-
exports.
|
|
1782
|
-
exports.
|
|
1783
|
-
exports.
|
|
1784
|
-
exports.
|
|
1785
|
-
exports.
|
|
1786
|
-
exports.
|
|
1787
|
-
exports.
|
|
1788
|
-
exports.
|
|
2185
|
+
exports.ChatAgent = ChatAgent;
|
|
2186
|
+
exports.OpenRouterModel = OpenRouterModel;
|
|
2187
|
+
exports.OpenRouterProvider = OpenRouterProvider;
|
|
2188
|
+
exports.PROMPTS = PROMPT_LIBRARY;
|
|
2189
|
+
exports.ResilientAgent = ResilientAgent;
|
|
2190
|
+
exports.SafeTool = SafeTool;
|
|
2191
|
+
exports.StructuredResponseParser = StructuredResponseParser;
|
|
2192
|
+
exports.SystemPrompt = SystemPrompt;
|
|
2193
|
+
exports.ToolAgent = ToolAgent;
|
|
2194
|
+
exports.UserPrompt = UserPrompt;
|