@jterrazz/intelligence 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +229 -0
- package/dist/adapters/agents/chat-agent.adapter.d.ts +35 -0
- package/dist/adapters/agents/chat-agent.adapter.js +388 -0
- package/dist/adapters/agents/chat-agent.adapter.js.map +1 -0
- package/dist/adapters/models/openrouter-model.adapter.d.ts +28 -0
- package/dist/adapters/models/openrouter-model.adapter.js +80 -0
- package/dist/adapters/models/openrouter-model.adapter.js.map +1 -0
- package/dist/adapters/prompts/__tests__/__snapshots__/presets.test.ts.snap +150 -0
- package/dist/adapters/prompts/__tests__/presets.test.d.ts +1 -0
- package/dist/adapters/prompts/__tests__/presets.test.js +31 -0
- package/dist/adapters/prompts/__tests__/presets.test.js.map +1 -0
- package/dist/adapters/prompts/library/categories/agent-logic.d.ts +8 -0
- package/dist/adapters/prompts/library/categories/agent-logic.js +9 -0
- package/dist/adapters/prompts/library/categories/agent-logic.js.map +1 -0
- package/dist/adapters/prompts/library/categories/agent-skills.d.ts +8 -0
- package/dist/adapters/prompts/library/categories/agent-skills.js +9 -0
- package/dist/adapters/prompts/library/categories/agent-skills.js.map +1 -0
- package/dist/adapters/prompts/library/categories/directives.d.ts +9 -0
- package/dist/adapters/prompts/library/categories/directives.js +10 -0
- package/dist/adapters/prompts/library/categories/directives.js.map +1 -0
- package/dist/adapters/prompts/library/categories/domain.d.ts +11 -0
- package/dist/adapters/prompts/library/categories/domain.js +12 -0
- package/dist/adapters/prompts/library/categories/domain.js.map +1 -0
- package/dist/adapters/prompts/library/categories/format.d.ts +9 -0
- package/dist/adapters/prompts/library/categories/format.js +10 -0
- package/dist/adapters/prompts/library/categories/format.js.map +1 -0
- package/dist/adapters/prompts/library/categories/language.d.ts +11 -0
- package/dist/adapters/prompts/library/categories/language.js +12 -0
- package/dist/adapters/prompts/library/categories/language.js.map +1 -0
- package/dist/adapters/prompts/library/categories/persona.d.ts +11 -0
- package/dist/adapters/prompts/library/categories/persona.js +12 -0
- package/dist/adapters/prompts/library/categories/persona.js.map +1 -0
- package/dist/adapters/prompts/library/categories/tone.d.ts +9 -0
- package/dist/adapters/prompts/library/categories/tone.js +10 -0
- package/dist/adapters/prompts/library/categories/tone.js.map +1 -0
- package/dist/adapters/prompts/library/categories/verbosity.d.ts +8 -0
- package/dist/adapters/prompts/library/categories/verbosity.js +9 -0
- package/dist/adapters/prompts/library/categories/verbosity.js.map +1 -0
- package/dist/adapters/prompts/library/index.d.ts +66 -0
- package/dist/adapters/prompts/library/index.js +28 -0
- package/dist/adapters/prompts/library/index.js.map +1 -0
- package/dist/adapters/prompts/library/presets.d.ts +17 -0
- package/dist/adapters/prompts/library/presets.js +51 -0
- package/dist/adapters/prompts/library/presets.js.map +1 -0
- package/dist/adapters/prompts/system-prompt.adapter.d.ts +9 -0
- package/dist/adapters/prompts/system-prompt.adapter.js +57 -0
- package/dist/adapters/prompts/system-prompt.adapter.js.map +1 -0
- package/dist/adapters/prompts/user-prompt.adapter.d.ts +9 -0
- package/dist/adapters/prompts/user-prompt.adapter.js +57 -0
- package/dist/adapters/prompts/user-prompt.adapter.js.map +1 -0
- package/dist/adapters/tools/safe-tool.adapter.d.ts +27 -0
- package/dist/adapters/tools/safe-tool.adapter.js +283 -0
- package/dist/adapters/tools/safe-tool.adapter.js.map +1 -0
- package/dist/adapters/utils/__tests__/ai-response-parser.test.d.ts +1 -0
- package/dist/adapters/utils/__tests__/ai-response-parser.test.js +249 -0
- package/dist/adapters/utils/__tests__/ai-response-parser.test.js.map +1 -0
- package/dist/adapters/utils/ai-response-parser-error.d.ts +8 -0
- package/dist/adapters/utils/ai-response-parser-error.js +136 -0
- package/dist/adapters/utils/ai-response-parser-error.js.map +1 -0
- package/dist/adapters/utils/ai-response-parser.d.ts +56 -0
- package/dist/adapters/utils/ai-response-parser.js +334 -0
- package/dist/adapters/utils/ai-response-parser.js.map +1 -0
- package/dist/index.cjs +1461 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/ports/agent.port.d.ts +11 -0
- package/dist/ports/agent.port.js +5 -0
- package/dist/ports/agent.port.js.map +1 -0
- package/dist/ports/model.port.d.ts +10 -0
- package/dist/ports/model.port.js +5 -0
- package/dist/ports/model.port.js.map +1 -0
- package/dist/ports/prompt.port.d.ts +9 -0
- package/dist/ports/prompt.port.js +5 -0
- package/dist/ports/prompt.port.js.map +1 -0
- package/dist/ports/tool.port.d.ts +11 -0
- package/dist/ports/tool.port.js +5 -0
- package/dist/ports/tool.port.js.map +1 -0
- package/package.json +35 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1461 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var prompts = require('@langchain/core/prompts');
|
|
4
|
+
var agents = require('langchain/agents');
|
|
5
|
+
var v4 = require('zod/v4');
|
|
6
|
+
var openai = require('@langchain/openai');
|
|
7
|
+
var tools = require('langchain/tools');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Custom error for AI response parsing failures
|
|
11
|
+
*/ function _assert_this_initialized(self) {
|
|
12
|
+
if (self === void 0) {
|
|
13
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
14
|
+
}
|
|
15
|
+
return self;
|
|
16
|
+
}
|
|
17
|
+
function _call_super(_this, derived, args) {
|
|
18
|
+
derived = _get_prototype_of(derived);
|
|
19
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
20
|
+
}
|
|
21
|
+
function _class_call_check$6(instance, Constructor) {
|
|
22
|
+
if (!(instance instanceof Constructor)) {
|
|
23
|
+
throw new TypeError("Cannot call a class as a function");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function _construct(Parent, args, Class) {
|
|
27
|
+
if (_is_native_reflect_construct()) {
|
|
28
|
+
_construct = Reflect.construct;
|
|
29
|
+
} else {
|
|
30
|
+
_construct = function construct(Parent, args, Class) {
|
|
31
|
+
var a = [
|
|
32
|
+
null
|
|
33
|
+
];
|
|
34
|
+
a.push.apply(a, args);
|
|
35
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
36
|
+
var instance = new Constructor();
|
|
37
|
+
if (Class) _set_prototype_of(instance, Class.prototype);
|
|
38
|
+
return instance;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return _construct.apply(null, arguments);
|
|
42
|
+
}
|
|
43
|
+
function _define_property$6(obj, key, value) {
|
|
44
|
+
if (key in obj) {
|
|
45
|
+
Object.defineProperty(obj, key, {
|
|
46
|
+
value: value,
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
obj[key] = value;
|
|
53
|
+
}
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
function _get_prototype_of(o) {
|
|
57
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
58
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
59
|
+
};
|
|
60
|
+
return _get_prototype_of(o);
|
|
61
|
+
}
|
|
62
|
+
function _inherits(subClass, superClass) {
|
|
63
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
64
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
65
|
+
}
|
|
66
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
67
|
+
constructor: {
|
|
68
|
+
value: subClass,
|
|
69
|
+
writable: true,
|
|
70
|
+
configurable: true
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
74
|
+
}
|
|
75
|
+
function _is_native_function(fn) {
|
|
76
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
77
|
+
}
|
|
78
|
+
function _possible_constructor_return(self, call) {
|
|
79
|
+
if (call && (_type_of$2(call) === "object" || typeof call === "function")) {
|
|
80
|
+
return call;
|
|
81
|
+
}
|
|
82
|
+
return _assert_this_initialized(self);
|
|
83
|
+
}
|
|
84
|
+
function _set_prototype_of(o, p) {
|
|
85
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
86
|
+
o.__proto__ = p;
|
|
87
|
+
return o;
|
|
88
|
+
};
|
|
89
|
+
return _set_prototype_of(o, p);
|
|
90
|
+
}
|
|
91
|
+
function _type_of$2(obj) {
|
|
92
|
+
"@swc/helpers - typeof";
|
|
93
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
94
|
+
}
|
|
95
|
+
function _wrap_native_super(Class) {
|
|
96
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
97
|
+
_wrap_native_super = function wrapNativeSuper(Class) {
|
|
98
|
+
if (Class === null || !_is_native_function(Class)) return Class;
|
|
99
|
+
if (typeof Class !== "function") {
|
|
100
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
101
|
+
}
|
|
102
|
+
if (typeof _cache !== "undefined") {
|
|
103
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
104
|
+
_cache.set(Class, Wrapper);
|
|
105
|
+
}
|
|
106
|
+
function Wrapper() {
|
|
107
|
+
return _construct(Class, arguments, _get_prototype_of(this).constructor);
|
|
108
|
+
}
|
|
109
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
110
|
+
constructor: {
|
|
111
|
+
value: Wrapper,
|
|
112
|
+
enumerable: false,
|
|
113
|
+
writable: true,
|
|
114
|
+
configurable: true
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return _set_prototype_of(Wrapper, Class);
|
|
118
|
+
};
|
|
119
|
+
return _wrap_native_super(Class);
|
|
120
|
+
}
|
|
121
|
+
function _is_native_reflect_construct() {
|
|
122
|
+
try {
|
|
123
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
124
|
+
} catch (_) {}
|
|
125
|
+
return (_is_native_reflect_construct = function() {
|
|
126
|
+
return !!result;
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
var AIResponseParserError = /*#__PURE__*/ function(Error1) {
|
|
130
|
+
_inherits(AIResponseParserError, Error1);
|
|
131
|
+
function AIResponseParserError(message, cause, text) {
|
|
132
|
+
_class_call_check$6(this, AIResponseParserError);
|
|
133
|
+
var _this;
|
|
134
|
+
_this = _call_super(this, AIResponseParserError, [
|
|
135
|
+
message
|
|
136
|
+
]), _define_property$6(_this, "cause", void 0), _define_property$6(_this, "text", void 0), _this.cause = cause, _this.text = text;
|
|
137
|
+
_this.name = 'AIResponseParserError';
|
|
138
|
+
return _this;
|
|
139
|
+
}
|
|
140
|
+
return AIResponseParserError;
|
|
141
|
+
}(_wrap_native_super(Error));
|
|
142
|
+
|
|
143
|
+
function _array_like_to_array(arr, len) {
|
|
144
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
145
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
146
|
+
return arr2;
|
|
147
|
+
}
|
|
148
|
+
function _array_with_holes(arr) {
|
|
149
|
+
if (Array.isArray(arr)) return arr;
|
|
150
|
+
}
|
|
151
|
+
function _class_call_check$5(instance, Constructor) {
|
|
152
|
+
if (!(instance instanceof Constructor)) {
|
|
153
|
+
throw new TypeError("Cannot call a class as a function");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function _defineProperties$5(target, props) {
|
|
157
|
+
for(var i = 0; i < props.length; i++){
|
|
158
|
+
var descriptor = props[i];
|
|
159
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
160
|
+
descriptor.configurable = true;
|
|
161
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
162
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function _create_class$5(Constructor, protoProps, staticProps) {
|
|
166
|
+
if (protoProps) _defineProperties$5(Constructor.prototype, protoProps);
|
|
167
|
+
return Constructor;
|
|
168
|
+
}
|
|
169
|
+
function _define_property$5(obj, key, value) {
|
|
170
|
+
if (key in obj) {
|
|
171
|
+
Object.defineProperty(obj, key, {
|
|
172
|
+
value: value,
|
|
173
|
+
enumerable: true,
|
|
174
|
+
configurable: true,
|
|
175
|
+
writable: true
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
obj[key] = value;
|
|
179
|
+
}
|
|
180
|
+
return obj;
|
|
181
|
+
}
|
|
182
|
+
function _instanceof$2(left, right) {
|
|
183
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
184
|
+
return !!right[Symbol.hasInstance](left);
|
|
185
|
+
} else {
|
|
186
|
+
return left instanceof right;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function _iterable_to_array_limit(arr, i) {
|
|
190
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
191
|
+
if (_i == null) return;
|
|
192
|
+
var _arr = [];
|
|
193
|
+
var _n = true;
|
|
194
|
+
var _d = false;
|
|
195
|
+
var _s, _e;
|
|
196
|
+
try {
|
|
197
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
198
|
+
_arr.push(_s.value);
|
|
199
|
+
if (i && _arr.length === i) break;
|
|
200
|
+
}
|
|
201
|
+
} catch (err) {
|
|
202
|
+
_d = true;
|
|
203
|
+
_e = err;
|
|
204
|
+
} finally{
|
|
205
|
+
try {
|
|
206
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
207
|
+
} finally{
|
|
208
|
+
if (_d) throw _e;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return _arr;
|
|
212
|
+
}
|
|
213
|
+
function _non_iterable_rest() {
|
|
214
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
215
|
+
}
|
|
216
|
+
function _sliced_to_array(arr, i) {
|
|
217
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
218
|
+
}
|
|
219
|
+
function _type_of$1(obj) {
|
|
220
|
+
"@swc/helpers - typeof";
|
|
221
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
222
|
+
}
|
|
223
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
224
|
+
if (!o) return;
|
|
225
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
226
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
227
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
228
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
229
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Parses AI response text into structured data based on Zod schema
|
|
233
|
+
*/ var AIResponseParser = /*#__PURE__*/ function() {
|
|
234
|
+
function AIResponseParser(schema) {
|
|
235
|
+
_class_call_check$5(this, AIResponseParser);
|
|
236
|
+
_define_property$5(this, "schema", void 0);
|
|
237
|
+
this.schema = schema;
|
|
238
|
+
}
|
|
239
|
+
_create_class$5(AIResponseParser, [
|
|
240
|
+
{
|
|
241
|
+
key: "parse",
|
|
242
|
+
value: /**
|
|
243
|
+
* Parses the AI response text based on the configured schema
|
|
244
|
+
*/ function parse(text) {
|
|
245
|
+
try {
|
|
246
|
+
var cleanedText = this.cleanText(text);
|
|
247
|
+
var json = this.extractJsonFromText(cleanedText);
|
|
248
|
+
var unescapedJson = this.unescapeJsonValues(json);
|
|
249
|
+
return this.schema.parse(unescapedJson);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
if (_instanceof$2(error, v4.z.ZodError)) {
|
|
252
|
+
throw new AIResponseParserError('Failed to validate response against schema', error, text);
|
|
253
|
+
}
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
key: "cleanText",
|
|
260
|
+
value: /**
|
|
261
|
+
* Cleans text and finds the largest schema-compatible structure
|
|
262
|
+
*/ function cleanText(text) {
|
|
263
|
+
var _this = this;
|
|
264
|
+
// First try to extract from markdown code blocks
|
|
265
|
+
var codeBlocks = text.match(/```(?:json)?\r?\n([^`]*?)\r?\n```/g);
|
|
266
|
+
if (codeBlocks) {
|
|
267
|
+
// Try each code block and return the largest valid one
|
|
268
|
+
var validBlocks = codeBlocks.map(function(block) {
|
|
269
|
+
return _this.extractJsonFromCodeBlock(block);
|
|
270
|
+
}).filter(function(block) {
|
|
271
|
+
return block !== null;
|
|
272
|
+
});
|
|
273
|
+
if (validBlocks.length > 0) {
|
|
274
|
+
return this.findLargestString(validBlocks);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// If no valid code blocks, try to find JSON-like structures in the text
|
|
278
|
+
var jsonMatches = this.findJsonStructures(text);
|
|
279
|
+
if (jsonMatches.length > 0) {
|
|
280
|
+
return this.findLargestString(jsonMatches);
|
|
281
|
+
}
|
|
282
|
+
// If no JSON structures found, clean and return the original text
|
|
283
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
key: "convertToPrimitive",
|
|
288
|
+
value: /**
|
|
289
|
+
* Converts value to appropriate primitive type based on schema
|
|
290
|
+
*/ function convertToPrimitive(value, schema) {
|
|
291
|
+
if (_instanceof$2(schema, v4.z.ZodString)) {
|
|
292
|
+
return String(value);
|
|
293
|
+
}
|
|
294
|
+
if (_instanceof$2(schema, v4.z.ZodNumber)) {
|
|
295
|
+
return Number(value);
|
|
296
|
+
}
|
|
297
|
+
if (_instanceof$2(schema, v4.z.ZodBoolean)) {
|
|
298
|
+
return Boolean(value);
|
|
299
|
+
}
|
|
300
|
+
if (_instanceof$2(schema, v4.z.ZodNull)) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
return value;
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
key: "extractArray",
|
|
308
|
+
value: /**
|
|
309
|
+
* Extracts array from text
|
|
310
|
+
*/ function extractArray(text) {
|
|
311
|
+
var arrayStart = text.indexOf('[');
|
|
312
|
+
var arrayEnd = text.lastIndexOf(']');
|
|
313
|
+
if (arrayStart === -1 || arrayEnd === -1) {
|
|
314
|
+
throw new AIResponseParserError('No array found in response', undefined, text);
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
return JSON.parse(text.slice(arrayStart, arrayEnd + 1));
|
|
318
|
+
} catch (error) {
|
|
319
|
+
throw new AIResponseParserError('Failed to parse array JSON', error, text);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
key: "extractJsonFromCodeBlock",
|
|
325
|
+
value: /**
|
|
326
|
+
* Extracts and validates JSON content from a code block
|
|
327
|
+
*/ function extractJsonFromCodeBlock(block) {
|
|
328
|
+
var content = block.replace(/```(?:json)?\r?\n([^`]*?)\r?\n```/, '$1').trim();
|
|
329
|
+
try {
|
|
330
|
+
// Attempt to parse as JSON to validate structure
|
|
331
|
+
JSON.parse(content);
|
|
332
|
+
return content;
|
|
333
|
+
} catch (e) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
key: "extractJsonFromText",
|
|
340
|
+
value: /**
|
|
341
|
+
* Extracts and parses JSON from text based on schema type
|
|
342
|
+
*/ function extractJsonFromText(text) {
|
|
343
|
+
if (_instanceof$2(this.schema, v4.z.ZodArray)) {
|
|
344
|
+
return this.extractArray(text);
|
|
345
|
+
}
|
|
346
|
+
if (_instanceof$2(this.schema, v4.z.ZodObject)) {
|
|
347
|
+
return this.extractObject(text);
|
|
348
|
+
}
|
|
349
|
+
if (_instanceof$2(this.schema, v4.z.ZodString) || _instanceof$2(this.schema, v4.z.ZodNumber) || _instanceof$2(this.schema, v4.z.ZodBoolean) || _instanceof$2(this.schema, v4.z.ZodNull)) {
|
|
350
|
+
return this.extractPrimitive(text, this.schema);
|
|
351
|
+
}
|
|
352
|
+
throw new AIResponseParserError('Unsupported schema type', undefined, text);
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
key: "extractObject",
|
|
357
|
+
value: /**
|
|
358
|
+
* Extracts object from text
|
|
359
|
+
*/ function extractObject(text) {
|
|
360
|
+
var objectStart = text.indexOf('{');
|
|
361
|
+
var objectEnd = text.lastIndexOf('}');
|
|
362
|
+
if (objectStart === -1 || objectEnd === -1) {
|
|
363
|
+
throw new AIResponseParserError('No object found in response', undefined, text);
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
return JSON.parse(text.slice(objectStart, objectEnd + 1));
|
|
367
|
+
} catch (error) {
|
|
368
|
+
throw new AIResponseParserError('Failed to parse object JSON', error, text);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
key: "extractPrimitive",
|
|
374
|
+
value: /**
|
|
375
|
+
* Extracts and converts primitive value from text
|
|
376
|
+
*/ function extractPrimitive(text, schema) {
|
|
377
|
+
var trimmed = text.trim();
|
|
378
|
+
// Try to parse as JSON first in case it's quoted
|
|
379
|
+
try {
|
|
380
|
+
var parsed = JSON.parse(trimmed);
|
|
381
|
+
return this.convertToPrimitive(parsed, schema);
|
|
382
|
+
} catch (e) {
|
|
383
|
+
// If not valid JSON, use the raw string
|
|
384
|
+
return this.convertToPrimitive(trimmed, schema);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
key: "findJsonStructures",
|
|
390
|
+
value: /**
|
|
391
|
+
* Finds valid JSON structures in raw text
|
|
392
|
+
*/ function findJsonStructures(text) {
|
|
393
|
+
var jsonMatches = [];
|
|
394
|
+
var depth = 0;
|
|
395
|
+
var start = -1;
|
|
396
|
+
for(var i = 0; i < text.length; i++){
|
|
397
|
+
var char = text[i];
|
|
398
|
+
if (char === '{' || char === '[') {
|
|
399
|
+
if (depth === 0) start = i;
|
|
400
|
+
depth++;
|
|
401
|
+
} else if (char === '}' || char === ']') {
|
|
402
|
+
depth--;
|
|
403
|
+
if (depth === 0 && start !== -1) {
|
|
404
|
+
var potentialJson = text.slice(start, i + 1);
|
|
405
|
+
try {
|
|
406
|
+
JSON.parse(potentialJson);
|
|
407
|
+
jsonMatches.push(potentialJson);
|
|
408
|
+
} catch (e) {
|
|
409
|
+
// Invalid JSON, ignore
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return jsonMatches;
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
key: "findLargestString",
|
|
419
|
+
value: /**
|
|
420
|
+
* Returns the largest string from an array of strings
|
|
421
|
+
*/ function findLargestString(strings) {
|
|
422
|
+
return strings.reduce(function(largest, current) {
|
|
423
|
+
return current.length > largest.length ? current : largest;
|
|
424
|
+
}, strings[0]);
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
key: "unescapeJsonValues",
|
|
429
|
+
value: /**
|
|
430
|
+
* Recursively unescapes all string values in a JSON object/array
|
|
431
|
+
*/ function unescapeJsonValues(json) {
|
|
432
|
+
var _this = this;
|
|
433
|
+
if (typeof json === 'string') {
|
|
434
|
+
return this.unescapeText(json);
|
|
435
|
+
}
|
|
436
|
+
if (Array.isArray(json)) {
|
|
437
|
+
return json.map(function(item) {
|
|
438
|
+
return _this.unescapeJsonValues(item);
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
if ((typeof json === "undefined" ? "undefined" : _type_of$1(json)) === 'object' && json !== null) {
|
|
442
|
+
return Object.fromEntries(Object.entries(json).map(function(param) {
|
|
443
|
+
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
444
|
+
return [
|
|
445
|
+
key,
|
|
446
|
+
_this.unescapeJsonValues(value)
|
|
447
|
+
];
|
|
448
|
+
}));
|
|
449
|
+
}
|
|
450
|
+
return json;
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
key: "unescapeText",
|
|
455
|
+
value: /**
|
|
456
|
+
* Unescapes common escaped characters in text
|
|
457
|
+
*/ function unescapeText(text) {
|
|
458
|
+
return text.replace(/\\"/g, '"') // Unescape quotes
|
|
459
|
+
.replace(/\\n/g, '\n') // Unescape newlines
|
|
460
|
+
.replace(/\\r/g, '\r') // Unescape carriage returns
|
|
461
|
+
.replace(/\\t/g, '\t') // Unescape tabs
|
|
462
|
+
.replace(/\\\\/g, '\\') // Unescape backslashes
|
|
463
|
+
.replace(/\\u([0-9a-fA-F]{4})/g, function(_, code) {
|
|
464
|
+
return String.fromCharCode(parseInt(code, 16));
|
|
465
|
+
}); // Unescape unicode
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
]);
|
|
469
|
+
return AIResponseParser;
|
|
470
|
+
}();
|
|
471
|
+
|
|
472
|
+
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
|
473
|
+
try {
|
|
474
|
+
var info = gen[key](arg);
|
|
475
|
+
var value = info.value;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
reject(error);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (info.done) {
|
|
481
|
+
resolve(value);
|
|
482
|
+
} else {
|
|
483
|
+
Promise.resolve(value).then(_next, _throw);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
function _async_to_generator$1(fn) {
|
|
487
|
+
return function() {
|
|
488
|
+
var self = this, args = arguments;
|
|
489
|
+
return new Promise(function(resolve, reject) {
|
|
490
|
+
var gen = fn.apply(self, args);
|
|
491
|
+
function _next(value) {
|
|
492
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
|
493
|
+
}
|
|
494
|
+
function _throw(err) {
|
|
495
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
|
496
|
+
}
|
|
497
|
+
_next(undefined);
|
|
498
|
+
});
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
function _class_call_check$4(instance, Constructor) {
|
|
502
|
+
if (!(instance instanceof Constructor)) {
|
|
503
|
+
throw new TypeError("Cannot call a class as a function");
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
function _defineProperties$4(target, props) {
|
|
507
|
+
for(var i = 0; i < props.length; i++){
|
|
508
|
+
var descriptor = props[i];
|
|
509
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
510
|
+
descriptor.configurable = true;
|
|
511
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
512
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function _create_class$4(Constructor, protoProps, staticProps) {
|
|
516
|
+
if (protoProps) _defineProperties$4(Constructor.prototype, protoProps);
|
|
517
|
+
return Constructor;
|
|
518
|
+
}
|
|
519
|
+
function _define_property$4(obj, key, value) {
|
|
520
|
+
if (key in obj) {
|
|
521
|
+
Object.defineProperty(obj, key, {
|
|
522
|
+
value: value,
|
|
523
|
+
enumerable: true,
|
|
524
|
+
configurable: true,
|
|
525
|
+
writable: true
|
|
526
|
+
});
|
|
527
|
+
} else {
|
|
528
|
+
obj[key] = value;
|
|
529
|
+
}
|
|
530
|
+
return obj;
|
|
531
|
+
}
|
|
532
|
+
function _instanceof$1(left, right) {
|
|
533
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
534
|
+
return !!right[Symbol.hasInstance](left);
|
|
535
|
+
} else {
|
|
536
|
+
return left instanceof right;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function _type_of(obj) {
|
|
540
|
+
"@swc/helpers - typeof";
|
|
541
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
542
|
+
}
|
|
543
|
+
function _ts_generator$1(thisArg, body) {
|
|
544
|
+
var f, y, t, _ = {
|
|
545
|
+
label: 0,
|
|
546
|
+
sent: function() {
|
|
547
|
+
if (t[0] & 1) throw t[1];
|
|
548
|
+
return t[1];
|
|
549
|
+
},
|
|
550
|
+
trys: [],
|
|
551
|
+
ops: []
|
|
552
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
553
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
554
|
+
return this;
|
|
555
|
+
}), g;
|
|
556
|
+
function verb(n) {
|
|
557
|
+
return function(v) {
|
|
558
|
+
return step([
|
|
559
|
+
n,
|
|
560
|
+
v
|
|
561
|
+
]);
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function step(op) {
|
|
565
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
566
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
567
|
+
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;
|
|
568
|
+
if (y = 0, t) op = [
|
|
569
|
+
op[0] & 2,
|
|
570
|
+
t.value
|
|
571
|
+
];
|
|
572
|
+
switch(op[0]){
|
|
573
|
+
case 0:
|
|
574
|
+
case 1:
|
|
575
|
+
t = op;
|
|
576
|
+
break;
|
|
577
|
+
case 4:
|
|
578
|
+
_.label++;
|
|
579
|
+
return {
|
|
580
|
+
value: op[1],
|
|
581
|
+
done: false
|
|
582
|
+
};
|
|
583
|
+
case 5:
|
|
584
|
+
_.label++;
|
|
585
|
+
y = op[1];
|
|
586
|
+
op = [
|
|
587
|
+
0
|
|
588
|
+
];
|
|
589
|
+
continue;
|
|
590
|
+
case 7:
|
|
591
|
+
op = _.ops.pop();
|
|
592
|
+
_.trys.pop();
|
|
593
|
+
continue;
|
|
594
|
+
default:
|
|
595
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
596
|
+
_ = 0;
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
600
|
+
_.label = op[1];
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
604
|
+
_.label = t[1];
|
|
605
|
+
t = op;
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
if (t && _.label < t[2]) {
|
|
609
|
+
_.label = t[2];
|
|
610
|
+
_.ops.push(op);
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
if (t[2]) _.ops.pop();
|
|
614
|
+
_.trys.pop();
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
op = body.call(thisArg, _);
|
|
618
|
+
} catch (e) {
|
|
619
|
+
op = [
|
|
620
|
+
6,
|
|
621
|
+
e
|
|
622
|
+
];
|
|
623
|
+
y = 0;
|
|
624
|
+
} finally{
|
|
625
|
+
f = t = 0;
|
|
626
|
+
}
|
|
627
|
+
if (op[0] & 5) throw op[1];
|
|
628
|
+
return {
|
|
629
|
+
value: op[0] ? op[1] : void 0,
|
|
630
|
+
done: true
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
// Schema for agent responses
|
|
635
|
+
var AgentResponseSchema = v4.z.object({
|
|
636
|
+
message: v4.z.string().optional(),
|
|
637
|
+
reason: v4.z.string().optional(),
|
|
638
|
+
shouldRespond: v4.z["boolean"]()
|
|
639
|
+
});
|
|
640
|
+
// LangChain-specific framework rules (internal to this adapter)
|
|
641
|
+
var LANGCHAIN_FRAMEWORK_RULES = '\n<AGENT_FRAMEWORK>\nYou are a chat agent piloted by the LangChain framework.\n\nYou have access to the following tools: {tools}\nTool names: {tool_names}\nAgent scratchpad: {agent_scratchpad}\n\nWhen you want to provide your final response, you MUST format it exactly like this:\n\n```json\n{{"action": "Final Answer", "action_input": {{"shouldRespond": false, "reason": "<your reason>"}}}}\n```\n\nOR \n\n```json\n{{"action": "Final Answer", "action_input": {{"shouldRespond": true, "message": "<your response message>"}}}}\n```\n\n"shouldRespond" set to true means the message will be sent to the user.\n"shouldRespond" set to false means the message will not be sent to the user.\n\nALWAYS use this exact format with markdown code blocks and the action/action_input structure.\n</AGENT_FRAMEWORK>';
|
|
642
|
+
/**
|
|
643
|
+
* Chat agent adapter that provides structured chat capabilities with optional responses
|
|
644
|
+
*/ var ChatAgentAdapter = /*#__PURE__*/ function() {
|
|
645
|
+
function ChatAgentAdapter(name, systemPrompt, options) {
|
|
646
|
+
_class_call_check$4(this, ChatAgentAdapter);
|
|
647
|
+
_define_property$4(this, "executorPromise", void 0);
|
|
648
|
+
_define_property$4(this, "logger", void 0);
|
|
649
|
+
_define_property$4(this, "name", void 0);
|
|
650
|
+
_define_property$4(this, "responseParser", void 0);
|
|
651
|
+
this.name = name;
|
|
652
|
+
this.logger = options.logger;
|
|
653
|
+
this.responseParser = new AIResponseParser(AgentResponseSchema);
|
|
654
|
+
this.executorPromise = this.createExecutor(systemPrompt, options);
|
|
655
|
+
}
|
|
656
|
+
_create_class$4(ChatAgentAdapter, [
|
|
657
|
+
{
|
|
658
|
+
key: "run",
|
|
659
|
+
value: function run(userPrompt) {
|
|
660
|
+
return _async_to_generator$1(function() {
|
|
661
|
+
var _this_logger, executor, input, result, response, error, _this_logger1;
|
|
662
|
+
return _ts_generator$1(this, function(_state) {
|
|
663
|
+
switch(_state.label){
|
|
664
|
+
case 0:
|
|
665
|
+
_state.trys.push([
|
|
666
|
+
0,
|
|
667
|
+
3,
|
|
668
|
+
,
|
|
669
|
+
4
|
|
670
|
+
]);
|
|
671
|
+
return [
|
|
672
|
+
4,
|
|
673
|
+
this.executorPromise
|
|
674
|
+
];
|
|
675
|
+
case 1:
|
|
676
|
+
executor = _state.sent();
|
|
677
|
+
input = this.resolveUserInput(userPrompt);
|
|
678
|
+
return [
|
|
679
|
+
4,
|
|
680
|
+
executor.invoke({
|
|
681
|
+
input: input
|
|
682
|
+
})
|
|
683
|
+
];
|
|
684
|
+
case 2:
|
|
685
|
+
result = _state.sent();
|
|
686
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.debug('Agent execution result', {
|
|
687
|
+
agentName: this.name,
|
|
688
|
+
hasOutput: 'output' in result,
|
|
689
|
+
hasUserPrompt: !!userPrompt,
|
|
690
|
+
outputType: _type_of(result.output)
|
|
691
|
+
});
|
|
692
|
+
if (!result || typeof result.output === 'undefined') {
|
|
693
|
+
throw new Error('Agent returned invalid result structure');
|
|
694
|
+
}
|
|
695
|
+
response = this.parseAgentResponse(result.output);
|
|
696
|
+
return [
|
|
697
|
+
2,
|
|
698
|
+
this.handleResponse(response)
|
|
699
|
+
];
|
|
700
|
+
case 3:
|
|
701
|
+
error = _state.sent();
|
|
702
|
+
(_this_logger1 = this.logger) === null || _this_logger1 === void 0 ? void 0 : _this_logger1.error('Error running chat agent', {
|
|
703
|
+
agentName: this.name,
|
|
704
|
+
error: _instanceof$1(error, Error) ? error.message : 'Unknown error',
|
|
705
|
+
userPrompt: userPrompt ? 'Prompt object' : 'none'
|
|
706
|
+
});
|
|
707
|
+
return [
|
|
708
|
+
2,
|
|
709
|
+
null
|
|
710
|
+
];
|
|
711
|
+
case 4:
|
|
712
|
+
return [
|
|
713
|
+
2
|
|
714
|
+
];
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}).call(this);
|
|
718
|
+
}
|
|
719
|
+
},
|
|
720
|
+
{
|
|
721
|
+
key: "createExecutor",
|
|
722
|
+
value: function createExecutor(systemPrompt, options) {
|
|
723
|
+
return _async_to_generator$1(function() {
|
|
724
|
+
var model, dynamicTools, systemPromptText, systemTemplate, humanTemplate, prompt, agent;
|
|
725
|
+
return _ts_generator$1(this, function(_state) {
|
|
726
|
+
switch(_state.label){
|
|
727
|
+
case 0:
|
|
728
|
+
model = options.model.getModel();
|
|
729
|
+
// Convert Tool instances to DynamicTool instances
|
|
730
|
+
dynamicTools = options.tools.map(function(tool) {
|
|
731
|
+
return tool.getDynamicTool();
|
|
732
|
+
});
|
|
733
|
+
// Combine LangChain framework rules with user-provided system prompts
|
|
734
|
+
systemPromptText = "".concat(LANGCHAIN_FRAMEWORK_RULES, "\n\n").concat(systemPrompt.generate());
|
|
735
|
+
systemTemplate = prompts.SystemMessagePromptTemplate.fromTemplate(systemPromptText);
|
|
736
|
+
humanTemplate = prompts.HumanMessagePromptTemplate.fromTemplate('{input}');
|
|
737
|
+
prompt = prompts.ChatPromptTemplate.fromMessages([
|
|
738
|
+
systemTemplate,
|
|
739
|
+
humanTemplate
|
|
740
|
+
]);
|
|
741
|
+
return [
|
|
742
|
+
4,
|
|
743
|
+
agents.createStructuredChatAgent({
|
|
744
|
+
llm: model,
|
|
745
|
+
prompt: prompt,
|
|
746
|
+
tools: dynamicTools
|
|
747
|
+
})
|
|
748
|
+
];
|
|
749
|
+
case 1:
|
|
750
|
+
agent = _state.sent();
|
|
751
|
+
return [
|
|
752
|
+
2,
|
|
753
|
+
agents.AgentExecutor.fromAgentAndTools({
|
|
754
|
+
agent: agent,
|
|
755
|
+
tools: dynamicTools
|
|
756
|
+
})
|
|
757
|
+
];
|
|
758
|
+
}
|
|
759
|
+
});
|
|
760
|
+
})();
|
|
761
|
+
}
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
key: "extractActionInput",
|
|
765
|
+
value: function extractActionInput(output) {
|
|
766
|
+
if ((typeof output === "undefined" ? "undefined" : _type_of(output)) === 'object' && output !== null) {
|
|
767
|
+
return JSON.stringify(output);
|
|
768
|
+
}
|
|
769
|
+
if (typeof output !== 'string') {
|
|
770
|
+
return String(output);
|
|
771
|
+
}
|
|
772
|
+
// Check for LangChain's action/action_input format first
|
|
773
|
+
var codeBlockMatch = output.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
774
|
+
if (codeBlockMatch) {
|
|
775
|
+
var content = codeBlockMatch[1].trim();
|
|
776
|
+
try {
|
|
777
|
+
var parsed = JSON.parse(content);
|
|
778
|
+
if (parsed.action === 'Final Answer' && parsed.action_input) {
|
|
779
|
+
return JSON.stringify(parsed.action_input);
|
|
780
|
+
}
|
|
781
|
+
} catch (e) {
|
|
782
|
+
// Fall through to return original content
|
|
783
|
+
}
|
|
784
|
+
return content;
|
|
785
|
+
}
|
|
786
|
+
// Fallback: Look for "Final Answer:" pattern
|
|
787
|
+
if (output.includes('Final Answer:')) {
|
|
788
|
+
var actionInputMatch = output.match(/Final Answer:\s*([\s\S]*?)$/i);
|
|
789
|
+
if (actionInputMatch) {
|
|
790
|
+
return actionInputMatch[1].trim();
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return output;
|
|
794
|
+
}
|
|
795
|
+
},
|
|
796
|
+
{
|
|
797
|
+
key: "handleResponse",
|
|
798
|
+
value: function handleResponse(response) {
|
|
799
|
+
var _this_logger;
|
|
800
|
+
if (response.shouldRespond && response.message) {
|
|
801
|
+
var _this_logger1;
|
|
802
|
+
(_this_logger1 = this.logger) === null || _this_logger1 === void 0 ? void 0 : _this_logger1.info('Agent responding with message', {
|
|
803
|
+
agentName: this.name
|
|
804
|
+
});
|
|
805
|
+
return response.message;
|
|
806
|
+
}
|
|
807
|
+
if (!response.shouldRespond) {
|
|
808
|
+
var _this_logger2;
|
|
809
|
+
(_this_logger2 = this.logger) === null || _this_logger2 === void 0 ? void 0 : _this_logger2.info('Agent chose not to respond', {
|
|
810
|
+
agentName: this.name,
|
|
811
|
+
reason: response.reason
|
|
812
|
+
});
|
|
813
|
+
return null;
|
|
814
|
+
}
|
|
815
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.error('Invalid agent response state', {
|
|
816
|
+
agentName: this.name,
|
|
817
|
+
response: response
|
|
818
|
+
});
|
|
819
|
+
return null;
|
|
820
|
+
}
|
|
821
|
+
},
|
|
822
|
+
{
|
|
823
|
+
key: "parseAgentResponse",
|
|
824
|
+
value: function parseAgentResponse(output) {
|
|
825
|
+
try {
|
|
826
|
+
// Handle LangChain's action/action_input format
|
|
827
|
+
var processedOutput = this.extractActionInput(output);
|
|
828
|
+
return this.responseParser.parse(processedOutput);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
var _this_logger;
|
|
831
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.error('Failed to parse agent response', {
|
|
832
|
+
agentName: this.name,
|
|
833
|
+
error: _instanceof$1(error, Error) ? error.message : 'Unknown error',
|
|
834
|
+
rawOutput: output
|
|
835
|
+
});
|
|
836
|
+
throw new Error('Invalid agent response format');
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
{
|
|
841
|
+
key: "resolveUserInput",
|
|
842
|
+
value: function resolveUserInput(userPrompt) {
|
|
843
|
+
if (!userPrompt) {
|
|
844
|
+
return 'Please analyze the current situation and respond if appropriate.';
|
|
845
|
+
}
|
|
846
|
+
return userPrompt.generate();
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
]);
|
|
850
|
+
return ChatAgentAdapter;
|
|
851
|
+
}();
|
|
852
|
+
|
|
853
|
+
function _class_call_check$3(instance, Constructor) {
|
|
854
|
+
if (!(instance instanceof Constructor)) {
|
|
855
|
+
throw new TypeError("Cannot call a class as a function");
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
function _defineProperties$3(target, props) {
|
|
859
|
+
for(var i = 0; i < props.length; i++){
|
|
860
|
+
var descriptor = props[i];
|
|
861
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
862
|
+
descriptor.configurable = true;
|
|
863
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
864
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
function _create_class$3(Constructor, protoProps, staticProps) {
|
|
868
|
+
if (protoProps) _defineProperties$3(Constructor.prototype, protoProps);
|
|
869
|
+
return Constructor;
|
|
870
|
+
}
|
|
871
|
+
function _define_property$3(obj, key, value) {
|
|
872
|
+
if (key in obj) {
|
|
873
|
+
Object.defineProperty(obj, key, {
|
|
874
|
+
value: value,
|
|
875
|
+
enumerable: true,
|
|
876
|
+
configurable: true,
|
|
877
|
+
writable: true
|
|
878
|
+
});
|
|
879
|
+
} else {
|
|
880
|
+
obj[key] = value;
|
|
881
|
+
}
|
|
882
|
+
return obj;
|
|
883
|
+
}
|
|
884
|
+
function _object_spread(target) {
|
|
885
|
+
for(var i = 1; i < arguments.length; i++){
|
|
886
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
887
|
+
var ownKeys = Object.keys(source);
|
|
888
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
889
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
890
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
891
|
+
}));
|
|
892
|
+
}
|
|
893
|
+
ownKeys.forEach(function(key) {
|
|
894
|
+
_define_property$3(target, key, source[key]);
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
return target;
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* OpenRouter adapter that provides access to various models through OpenRouter's API
|
|
901
|
+
*/ var OpenRouterModelAdapter = /*#__PURE__*/ function() {
|
|
902
|
+
function OpenRouterModelAdapter(config) {
|
|
903
|
+
_class_call_check$3(this, OpenRouterModelAdapter);
|
|
904
|
+
_define_property$3(this, "model", void 0);
|
|
905
|
+
this.model = new openai.ChatOpenAI({
|
|
906
|
+
configuration: {
|
|
907
|
+
baseURL: 'https://openrouter.ai/api/v1',
|
|
908
|
+
defaultHeaders: _object_spread({}, config.websiteUrl && {
|
|
909
|
+
'HTTP-Referer': config.websiteUrl
|
|
910
|
+
}, config.title && {
|
|
911
|
+
'X-Title': config.title
|
|
912
|
+
})
|
|
913
|
+
},
|
|
914
|
+
modelName: config.modelName,
|
|
915
|
+
openAIApiKey: config.apiKey
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
_create_class$3(OpenRouterModelAdapter, [
|
|
919
|
+
{
|
|
920
|
+
key: "getModel",
|
|
921
|
+
value: function getModel() {
|
|
922
|
+
return this.model;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
]);
|
|
926
|
+
return OpenRouterModelAdapter;
|
|
927
|
+
}();
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Specifies the agent's area of expertise.
|
|
931
|
+
* This helps focus the agent's knowledge and the context of its responses.
|
|
932
|
+
*/ var DOMAIN = {
|
|
933
|
+
ACADEMIC_RESEARCH: "\n<DOMAIN>\nYour expertise is in academic research. You are familiar with scholarly writing, peer-review processes, and formal citation methods.\n</DOMAIN>",
|
|
934
|
+
BUSINESS_STRATEGY: "\n<DOMAIN>\nYour expertise is in business strategy, including market analysis, competitive positioning, and operational planning.\n</DOMAIN>",
|
|
935
|
+
DATA_SCIENCE: "\n<DOMAIN>\nYour expertise is in data science, including statistical analysis, machine learning, and data visualization.\n</DOMAIN>",
|
|
936
|
+
GENERAL: "\n<DOMAIN>\nYou are a generalist with broad knowledge across many subjects.\n</DOMAIN>",
|
|
937
|
+
SOFTWARE_ENGINEERING: "\n<DOMAIN>\nYour expertise is in software engineering, including programming languages, system architecture, design patterns, and development best practices.\n</DOMAIN>"
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* Governs the agent's internal logic for responding and using tools.
|
|
942
|
+
*/ var AGENT_LOGIC = {
|
|
943
|
+
ALWAYS_RESPOND: "\n<AGENT_LOGIC>\nAlways provide a response to the user's input, even if it's just to state that you cannot fulfill the request.\n</AGENT_LOGIC>",
|
|
944
|
+
SELECTIVE_RESPONSE: "\n<AGENT_LOGIC>\nOnly respond when you can provide a valuable, relevant, and substantive contribution.\nIf a response is not necessary, state that you have nothing to add.\n</AGENT_LOGIC>",
|
|
945
|
+
TOOL_FIRST: "\n<AGENT_LOGIC>\nBefore formulating a response, always prioritize using your available tools to gather the most current and accurate information.\n</AGENT_LOGIC>"
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Defines the agent's primary cognitive skills and capabilities.
|
|
950
|
+
*/ var AGENT_SKILLS = {
|
|
951
|
+
CREATIVE_IDEATION: "\n<AGENT_SKILL>\nYou excel at brainstorming, generating novel ideas, and thinking outside the box.\n</AGENT_SKILL>",
|
|
952
|
+
INFORMATION_SYNTHESIS: "\n<AGENT_SKILL>\nYou are skilled at gathering and synthesizing information from multiple sources to provide a comprehensive answer.\n</AGENT_SKILL>",
|
|
953
|
+
PROBLEM_SOLVING: "\n<AGENT_SKILL>\nYou are skilled at analyzing complex problems, breaking them down into manageable parts, and proposing systematic solutions.\n</AGENT_SKILL>"
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* Core, non-negotiable rules that guide the agent's operation.
|
|
958
|
+
* These define fundamental principles like safety, ethics, and factuality.
|
|
959
|
+
*/ var DIRECTIVES = {
|
|
960
|
+
BE_ETHICAL: "\n<DIRECTIVE>\nAdhere to the highest ethical standards. Do not promote hate speech, discrimination, or violence.\nRespect user privacy and do not ask for or store personally identifiable information.\n</DIRECTIVE>",
|
|
961
|
+
BE_FACTUAL: "\n<DIRECTIVE>\nPrioritize accuracy and rely on verifiable information.\nIf you are uncertain about an answer, state your uncertainty clearly.\nDo not invent facts or statistics. When possible, cite credible sources.\n</DIRECTIVE>",
|
|
962
|
+
BE_SAFE: "\n<DIRECTIVE>\nDo not provide instructions or information that is illegal, dangerous, or harmful.\nRefuse to engage with requests that could cause real-world harm.\nPrioritize user safety and well-being in all responses.\n</DIRECTIVE>"
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Defines the structural format of the agent's output.
|
|
967
|
+
*/ var FORMAT = {
|
|
968
|
+
JSON: "\n<FORMAT>\nRespond ONLY with a valid, well-formed JSON object.\nDo not include any explanatory text or markdown formatting outside of the JSON structure.\n</FORMAT>",
|
|
969
|
+
MARKDOWN: "\n<FORMAT>\nUse Markdown for clear, structured responses.\nEmploy headings, lists, bold/italic text, and code blocks to improve readability.\n</FORMAT>",
|
|
970
|
+
PLAIN_TEXT: "\n<FORMAT>\nRespond in plain text without any special formatting, lists, or structural elements.\n</FORMAT>",
|
|
971
|
+
STEP_BY_STEP: "\n<FORMAT>\nBreak down instructions or processes into a clear, numbered, step-by-step list.\nEnsure each step is a distinct, actionable item.\n</FORMAT>"
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* Defines the natural language for the agent's responses, including proficiency level.
|
|
976
|
+
*/ var LANGUAGE = {
|
|
977
|
+
ENGLISH_NATIVE: "\n<LANGUAGE>\nRespond in natural, fluent English as a native speaker would.\nUse idiomatic expressions, varied vocabulary, and natural sentence flow.\n</LANGUAGE>",
|
|
978
|
+
ENGLISH_SIMPLE: "\n<LANGUAGE>\nUse simple, clear English that's easy to understand for non-native speakers.\nAvoid complex grammar, idioms, and sophisticated vocabulary.\n</LANGUAGE>",
|
|
979
|
+
FRENCH_NATIVE: "\n<LANGUAGE>\nR\xe9pondez en fran\xe7ais naturel et fluide comme le ferait un locuteur natif.\nUtilisez des expressions idiomatiques et un vocabulaire vari\xe9.\n</LANGUAGE>",
|
|
980
|
+
FRENCH_SIMPLE: "\n<LANGUAGE>\nUtilisez un fran\xe7ais simple et clair, facile \xe0 comprendre.\n\xc9vitez la grammaire complexe et le vocabulaire sophistiqu\xe9.\n</LANGUAGE>",
|
|
981
|
+
SPANISH_NATIVE: "\n<LANGUAGE>\nResponde en espa\xf1ol natural y fluido como lo har\xeda un hablante nativo.\nUsa expresiones idiom\xe1ticas y vocabulario variado.\n</LANGUAGE>",
|
|
982
|
+
SPANISH_SIMPLE: "\n<LANGUAGE>\nUsa espa\xf1ol simple y claro que sea f\xe1cil de entender para no nativos.\nEvita la gram\xe1tica compleja y el vocabulario sofisticado.\n</LANGUAGE>"
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Defines the "who" of the agent—its role and character.
|
|
987
|
+
* This sets the overall personality and interaction style.
|
|
988
|
+
*/ var PERSONA = {
|
|
989
|
+
COMMUNITY_ANIMATOR: "\n<PERSONA>\nYou are the Community Animator, the heart and soul of a digital community like Discord.\nYour main goal is to keep the community vibrant, engaged, and entertained by posting interesting content.\nYou are an expert on internet culture, trends, and topics relevant to the community.\n</PERSONA>",
|
|
990
|
+
CREATIVE_PARTNER: "\n<PERSONA>\nYou are a creative partner, here to help brainstorm and explore new ideas.\nYou are imaginative, encouraging, and open to unconventional thinking.\nYour goal is to inspire and collaborate.\n</PERSONA>",
|
|
991
|
+
EXPERT_ADVISOR: "\n<PERSONA>\nYou are an expert advisor in your specified domain.\nYou provide authoritative, well-reasoned guidance.\nYour tone is confident, knowledgeable, and objective.\n</PERSONA>",
|
|
992
|
+
SUPPORT_AGENT: "\n<PERSONA>\nYou are a friendly and empathetic support agent.\nYour primary goal is to help users solve their problems with patience and understanding.\nYou are a good listener and provide clear, step-by-step assistance.\n</PERSONA>",
|
|
993
|
+
TUTOR: "\n<PERSONA>\nYou are a patient and knowledgeable tutor.\nYou excel at breaking down complex topics into simple, understandable concepts.\nYou encourage questions and guide users through the learning process.\n</PERSONA>"
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Determines the emotional flavor and attitude of the agent's language.
|
|
998
|
+
*/ var TONE = {
|
|
999
|
+
EMPATHETIC: "\n<TONE>\nAdopt a warm, understanding, and supportive tone.\nAcknowledge the user's feelings and show you are listening.\n</TONE>",
|
|
1000
|
+
HUMOROUS: "\n<TONE>\nEmploy light-hearted humor, wit, and cleverness.\nKeep the mood fun and engaging, but avoid inappropriate or offensive jokes.\n</TONE>",
|
|
1001
|
+
NEUTRAL: "\n<TONE>\nMaintain an impartial, objective, and straightforward tone.\nAvoid emotional language and stick to the facts.\n</TONE>",
|
|
1002
|
+
PROFESSIONAL: "\n<TONE>\nUse a formal, respectful, and clear tone.\nStructure your communication logically and avoid slang or overly casual language.\n</TONE>"
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* Controls the level of detail and length of the agent's responses.
|
|
1007
|
+
*/ var VERBOSITY = {
|
|
1008
|
+
CONCISE: "\n<VERBOSITY>\nProvide brief, to-the-point answers.\nFocus only on the most critical information and omit background details unless requested.\n</VERBOSITY>",
|
|
1009
|
+
DETAILED: "\n<VERBOSITY>\nOffer comprehensive and thorough responses.\nInclude background information, context, examples, and potential edge cases.\n</VERBOSITY>",
|
|
1010
|
+
NORMAL: "\n<VERBOSITY>\nProvide a balanced level of detail, sufficient for a clear understanding without being overwhelming.\n</VERBOSITY>"
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Provides ready-to-use combinations of prompt parts for common use cases.
|
|
1015
|
+
*/ var PRESETS = {
|
|
1016
|
+
/**
|
|
1017
|
+
* A creative partner for brainstorming and ideation.
|
|
1018
|
+
*/ CREATIVE_BRAINSTORMER: [
|
|
1019
|
+
DIRECTIVES.BE_ETHICAL,
|
|
1020
|
+
PERSONA.CREATIVE_PARTNER,
|
|
1021
|
+
DOMAIN.GENERAL,
|
|
1022
|
+
TONE.HUMOROUS,
|
|
1023
|
+
VERBOSITY.NORMAL,
|
|
1024
|
+
FORMAT.MARKDOWN,
|
|
1025
|
+
AGENT_LOGIC.ALWAYS_RESPOND,
|
|
1026
|
+
AGENT_SKILLS.CREATIVE_IDEATION
|
|
1027
|
+
],
|
|
1028
|
+
/**
|
|
1029
|
+
* A fun and engaging community animator for platforms like Discord.
|
|
1030
|
+
*/ DISCORD_COMMUNITY_ANIMATOR: [
|
|
1031
|
+
DIRECTIVES.BE_SAFE,
|
|
1032
|
+
PERSONA.COMMUNITY_ANIMATOR,
|
|
1033
|
+
DOMAIN.GENERAL,
|
|
1034
|
+
TONE.HUMOROUS,
|
|
1035
|
+
VERBOSITY.NORMAL,
|
|
1036
|
+
FORMAT.MARKDOWN,
|
|
1037
|
+
AGENT_LOGIC.ALWAYS_RESPOND,
|
|
1038
|
+
AGENT_SKILLS.CREATIVE_IDEATION
|
|
1039
|
+
],
|
|
1040
|
+
/**
|
|
1041
|
+
* A friendly and empathetic support agent for general queries.
|
|
1042
|
+
*/ EMPATHETIC_SUPPORT_AGENT: [
|
|
1043
|
+
DIRECTIVES.BE_SAFE,
|
|
1044
|
+
DIRECTIVES.BE_ETHICAL,
|
|
1045
|
+
PERSONA.SUPPORT_AGENT,
|
|
1046
|
+
DOMAIN.GENERAL,
|
|
1047
|
+
TONE.EMPATHETIC,
|
|
1048
|
+
VERBOSITY.NORMAL,
|
|
1049
|
+
FORMAT.STEP_BY_STEP,
|
|
1050
|
+
AGENT_LOGIC.ALWAYS_RESPOND,
|
|
1051
|
+
AGENT_SKILLS.PROBLEM_SOLVING
|
|
1052
|
+
]
|
|
1053
|
+
};
|
|
1054
|
+
|
|
1055
|
+
var PROMPTS = {
|
|
1056
|
+
AGENT_LOGIC: AGENT_LOGIC,
|
|
1057
|
+
AGENT_SKILLS: AGENT_SKILLS,
|
|
1058
|
+
DIRECTIVES: DIRECTIVES,
|
|
1059
|
+
DOMAIN: DOMAIN,
|
|
1060
|
+
FORMAT: FORMAT,
|
|
1061
|
+
LANGUAGE: LANGUAGE,
|
|
1062
|
+
PERSONA: PERSONA,
|
|
1063
|
+
PRESETS: PRESETS,
|
|
1064
|
+
TONE: TONE,
|
|
1065
|
+
VERBOSITY: VERBOSITY
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1068
|
+
function _class_call_check$2(instance, Constructor) {
|
|
1069
|
+
if (!(instance instanceof Constructor)) {
|
|
1070
|
+
throw new TypeError("Cannot call a class as a function");
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
function _defineProperties$2(target, props) {
|
|
1074
|
+
for(var i = 0; i < props.length; i++){
|
|
1075
|
+
var descriptor = props[i];
|
|
1076
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
1077
|
+
descriptor.configurable = true;
|
|
1078
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
1079
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
function _create_class$2(Constructor, protoProps, staticProps) {
|
|
1083
|
+
if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
|
|
1084
|
+
return Constructor;
|
|
1085
|
+
}
|
|
1086
|
+
function _define_property$2(obj, key, value) {
|
|
1087
|
+
if (key in obj) {
|
|
1088
|
+
Object.defineProperty(obj, key, {
|
|
1089
|
+
value: value,
|
|
1090
|
+
enumerable: true,
|
|
1091
|
+
configurable: true,
|
|
1092
|
+
writable: true
|
|
1093
|
+
});
|
|
1094
|
+
} else {
|
|
1095
|
+
obj[key] = value;
|
|
1096
|
+
}
|
|
1097
|
+
return obj;
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* System prompt adapter that generates a system prompt from a list of strings
|
|
1101
|
+
*/ var SystemPromptAdapter = /*#__PURE__*/ function() {
|
|
1102
|
+
function SystemPromptAdapter() {
|
|
1103
|
+
for(var _len = arguments.length, prompts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
1104
|
+
prompts[_key] = arguments[_key];
|
|
1105
|
+
}
|
|
1106
|
+
_class_call_check$2(this, SystemPromptAdapter);
|
|
1107
|
+
_define_property$2(this, "finalPrompt", void 0);
|
|
1108
|
+
var flattenedPrompts = prompts.flat();
|
|
1109
|
+
this.finalPrompt = flattenedPrompts.join('\n\n');
|
|
1110
|
+
}
|
|
1111
|
+
_create_class$2(SystemPromptAdapter, [
|
|
1112
|
+
{
|
|
1113
|
+
key: "generate",
|
|
1114
|
+
value: function generate() {
|
|
1115
|
+
return this.finalPrompt;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
]);
|
|
1119
|
+
return SystemPromptAdapter;
|
|
1120
|
+
}();
|
|
1121
|
+
|
|
1122
|
+
function _class_call_check$1(instance, Constructor) {
|
|
1123
|
+
if (!(instance instanceof Constructor)) {
|
|
1124
|
+
throw new TypeError("Cannot call a class as a function");
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
function _defineProperties$1(target, props) {
|
|
1128
|
+
for(var i = 0; i < props.length; i++){
|
|
1129
|
+
var descriptor = props[i];
|
|
1130
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
1131
|
+
descriptor.configurable = true;
|
|
1132
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
1133
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
function _create_class$1(Constructor, protoProps, staticProps) {
|
|
1137
|
+
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
|
1138
|
+
return Constructor;
|
|
1139
|
+
}
|
|
1140
|
+
function _define_property$1(obj, key, value) {
|
|
1141
|
+
if (key in obj) {
|
|
1142
|
+
Object.defineProperty(obj, key, {
|
|
1143
|
+
value: value,
|
|
1144
|
+
enumerable: true,
|
|
1145
|
+
configurable: true,
|
|
1146
|
+
writable: true
|
|
1147
|
+
});
|
|
1148
|
+
} else {
|
|
1149
|
+
obj[key] = value;
|
|
1150
|
+
}
|
|
1151
|
+
return obj;
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* User prompt adapter that generates a user prompt from a list of strings
|
|
1155
|
+
*/ var UserPromptAdapter = /*#__PURE__*/ function() {
|
|
1156
|
+
function UserPromptAdapter() {
|
|
1157
|
+
for(var _len = arguments.length, prompts = new Array(_len), _key = 0; _key < _len; _key++){
|
|
1158
|
+
prompts[_key] = arguments[_key];
|
|
1159
|
+
}
|
|
1160
|
+
_class_call_check$1(this, UserPromptAdapter);
|
|
1161
|
+
_define_property$1(this, "finalPrompt", void 0);
|
|
1162
|
+
var flattenedPrompts = prompts.flat();
|
|
1163
|
+
this.finalPrompt = flattenedPrompts.join('\n\n');
|
|
1164
|
+
}
|
|
1165
|
+
_create_class$1(UserPromptAdapter, [
|
|
1166
|
+
{
|
|
1167
|
+
key: "generate",
|
|
1168
|
+
value: function generate() {
|
|
1169
|
+
return this.finalPrompt;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
]);
|
|
1173
|
+
return UserPromptAdapter;
|
|
1174
|
+
}();
|
|
1175
|
+
|
|
1176
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1177
|
+
try {
|
|
1178
|
+
var info = gen[key](arg);
|
|
1179
|
+
var value = info.value;
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
reject(error);
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
if (info.done) {
|
|
1185
|
+
resolve(value);
|
|
1186
|
+
} else {
|
|
1187
|
+
Promise.resolve(value).then(_next, _throw);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
function _async_to_generator(fn) {
|
|
1191
|
+
return function() {
|
|
1192
|
+
var self = this, args = arguments;
|
|
1193
|
+
return new Promise(function(resolve, reject) {
|
|
1194
|
+
var gen = fn.apply(self, args);
|
|
1195
|
+
function _next(value) {
|
|
1196
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
1197
|
+
}
|
|
1198
|
+
function _throw(err) {
|
|
1199
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1200
|
+
}
|
|
1201
|
+
_next(undefined);
|
|
1202
|
+
});
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
function _class_call_check(instance, Constructor) {
|
|
1206
|
+
if (!(instance instanceof Constructor)) {
|
|
1207
|
+
throw new TypeError("Cannot call a class as a function");
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
function _defineProperties(target, props) {
|
|
1211
|
+
for(var i = 0; i < props.length; i++){
|
|
1212
|
+
var descriptor = props[i];
|
|
1213
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
1214
|
+
descriptor.configurable = true;
|
|
1215
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
1216
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
1220
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
1221
|
+
return Constructor;
|
|
1222
|
+
}
|
|
1223
|
+
function _define_property(obj, key, value) {
|
|
1224
|
+
if (key in obj) {
|
|
1225
|
+
Object.defineProperty(obj, key, {
|
|
1226
|
+
value: value,
|
|
1227
|
+
enumerable: true,
|
|
1228
|
+
configurable: true,
|
|
1229
|
+
writable: true
|
|
1230
|
+
});
|
|
1231
|
+
} else {
|
|
1232
|
+
obj[key] = value;
|
|
1233
|
+
}
|
|
1234
|
+
return obj;
|
|
1235
|
+
}
|
|
1236
|
+
function _instanceof(left, right) {
|
|
1237
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
1238
|
+
return !!right[Symbol.hasInstance](left);
|
|
1239
|
+
} else {
|
|
1240
|
+
return left instanceof right;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
function _ts_generator(thisArg, body) {
|
|
1244
|
+
var f, y, t, _ = {
|
|
1245
|
+
label: 0,
|
|
1246
|
+
sent: function() {
|
|
1247
|
+
if (t[0] & 1) throw t[1];
|
|
1248
|
+
return t[1];
|
|
1249
|
+
},
|
|
1250
|
+
trys: [],
|
|
1251
|
+
ops: []
|
|
1252
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
1253
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
1254
|
+
return this;
|
|
1255
|
+
}), g;
|
|
1256
|
+
function verb(n) {
|
|
1257
|
+
return function(v) {
|
|
1258
|
+
return step([
|
|
1259
|
+
n,
|
|
1260
|
+
v
|
|
1261
|
+
]);
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
function step(op) {
|
|
1265
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
1266
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
1267
|
+
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;
|
|
1268
|
+
if (y = 0, t) op = [
|
|
1269
|
+
op[0] & 2,
|
|
1270
|
+
t.value
|
|
1271
|
+
];
|
|
1272
|
+
switch(op[0]){
|
|
1273
|
+
case 0:
|
|
1274
|
+
case 1:
|
|
1275
|
+
t = op;
|
|
1276
|
+
break;
|
|
1277
|
+
case 4:
|
|
1278
|
+
_.label++;
|
|
1279
|
+
return {
|
|
1280
|
+
value: op[1],
|
|
1281
|
+
done: false
|
|
1282
|
+
};
|
|
1283
|
+
case 5:
|
|
1284
|
+
_.label++;
|
|
1285
|
+
y = op[1];
|
|
1286
|
+
op = [
|
|
1287
|
+
0
|
|
1288
|
+
];
|
|
1289
|
+
continue;
|
|
1290
|
+
case 7:
|
|
1291
|
+
op = _.ops.pop();
|
|
1292
|
+
_.trys.pop();
|
|
1293
|
+
continue;
|
|
1294
|
+
default:
|
|
1295
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
1296
|
+
_ = 0;
|
|
1297
|
+
continue;
|
|
1298
|
+
}
|
|
1299
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
1300
|
+
_.label = op[1];
|
|
1301
|
+
break;
|
|
1302
|
+
}
|
|
1303
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
1304
|
+
_.label = t[1];
|
|
1305
|
+
t = op;
|
|
1306
|
+
break;
|
|
1307
|
+
}
|
|
1308
|
+
if (t && _.label < t[2]) {
|
|
1309
|
+
_.label = t[2];
|
|
1310
|
+
_.ops.push(op);
|
|
1311
|
+
break;
|
|
1312
|
+
}
|
|
1313
|
+
if (t[2]) _.ops.pop();
|
|
1314
|
+
_.trys.pop();
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
op = body.call(thisArg, _);
|
|
1318
|
+
} catch (e) {
|
|
1319
|
+
op = [
|
|
1320
|
+
6,
|
|
1321
|
+
e
|
|
1322
|
+
];
|
|
1323
|
+
y = 0;
|
|
1324
|
+
} finally{
|
|
1325
|
+
f = t = 0;
|
|
1326
|
+
}
|
|
1327
|
+
if (op[0] & 5) throw op[1];
|
|
1328
|
+
return {
|
|
1329
|
+
value: op[0] ? op[1] : void 0,
|
|
1330
|
+
done: true
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* Safe tool adapter that provides error handling and logging for LangChain tools
|
|
1336
|
+
*/ var SafeToolAdapter = /*#__PURE__*/ function() {
|
|
1337
|
+
function SafeToolAdapter(config) {
|
|
1338
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
1339
|
+
_class_call_check(this, SafeToolAdapter);
|
|
1340
|
+
_define_property(this, "config", void 0);
|
|
1341
|
+
_define_property(this, "options", void 0);
|
|
1342
|
+
_define_property(this, "dynamicTool", void 0);
|
|
1343
|
+
this.config = config;
|
|
1344
|
+
this.options = options;
|
|
1345
|
+
var logger = options.logger, schema = options.schema;
|
|
1346
|
+
if (schema) {
|
|
1347
|
+
// Use DynamicStructuredTool for parameterized tools
|
|
1348
|
+
this.dynamicTool = new tools.DynamicStructuredTool({
|
|
1349
|
+
description: config.description,
|
|
1350
|
+
func: function(args) {
|
|
1351
|
+
return _async_to_generator(function() {
|
|
1352
|
+
var error, errorMessage;
|
|
1353
|
+
return _ts_generator(this, function(_state) {
|
|
1354
|
+
switch(_state.label){
|
|
1355
|
+
case 0:
|
|
1356
|
+
_state.trys.push([
|
|
1357
|
+
0,
|
|
1358
|
+
2,
|
|
1359
|
+
,
|
|
1360
|
+
3
|
|
1361
|
+
]);
|
|
1362
|
+
return [
|
|
1363
|
+
4,
|
|
1364
|
+
config.execute(args)
|
|
1365
|
+
];
|
|
1366
|
+
case 1:
|
|
1367
|
+
return [
|
|
1368
|
+
2,
|
|
1369
|
+
_state.sent()
|
|
1370
|
+
];
|
|
1371
|
+
case 2:
|
|
1372
|
+
error = _state.sent();
|
|
1373
|
+
errorMessage = _instanceof(error, Error) ? error.message : String(error);
|
|
1374
|
+
logger === null || logger === void 0 ? void 0 : logger.error("Unexpected error in ".concat(config.name), {
|
|
1375
|
+
args: args,
|
|
1376
|
+
error: errorMessage,
|
|
1377
|
+
toolName: config.name
|
|
1378
|
+
});
|
|
1379
|
+
return [
|
|
1380
|
+
2,
|
|
1381
|
+
"Tool ".concat(config.name, " failed to execute")
|
|
1382
|
+
];
|
|
1383
|
+
case 3:
|
|
1384
|
+
return [
|
|
1385
|
+
2
|
|
1386
|
+
];
|
|
1387
|
+
}
|
|
1388
|
+
});
|
|
1389
|
+
})();
|
|
1390
|
+
},
|
|
1391
|
+
name: config.name,
|
|
1392
|
+
schema: schema
|
|
1393
|
+
});
|
|
1394
|
+
} else {
|
|
1395
|
+
// Use DynamicTool for simple tools
|
|
1396
|
+
this.dynamicTool = new tools.DynamicTool({
|
|
1397
|
+
description: config.description,
|
|
1398
|
+
func: function() {
|
|
1399
|
+
return _async_to_generator(function() {
|
|
1400
|
+
var error, errorMessage;
|
|
1401
|
+
return _ts_generator(this, function(_state) {
|
|
1402
|
+
switch(_state.label){
|
|
1403
|
+
case 0:
|
|
1404
|
+
_state.trys.push([
|
|
1405
|
+
0,
|
|
1406
|
+
2,
|
|
1407
|
+
,
|
|
1408
|
+
3
|
|
1409
|
+
]);
|
|
1410
|
+
return [
|
|
1411
|
+
4,
|
|
1412
|
+
config.execute()
|
|
1413
|
+
];
|
|
1414
|
+
case 1:
|
|
1415
|
+
return [
|
|
1416
|
+
2,
|
|
1417
|
+
_state.sent()
|
|
1418
|
+
];
|
|
1419
|
+
case 2:
|
|
1420
|
+
error = _state.sent();
|
|
1421
|
+
errorMessage = _instanceof(error, Error) ? error.message : String(error);
|
|
1422
|
+
logger === null || logger === void 0 ? void 0 : logger.error("Unexpected error in ".concat(config.name), {
|
|
1423
|
+
error: errorMessage,
|
|
1424
|
+
toolName: config.name
|
|
1425
|
+
});
|
|
1426
|
+
return [
|
|
1427
|
+
2,
|
|
1428
|
+
"Tool ".concat(config.name, " failed to execute")
|
|
1429
|
+
];
|
|
1430
|
+
case 3:
|
|
1431
|
+
return [
|
|
1432
|
+
2
|
|
1433
|
+
];
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
})();
|
|
1437
|
+
},
|
|
1438
|
+
name: config.name
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
_create_class(SafeToolAdapter, [
|
|
1443
|
+
{
|
|
1444
|
+
/**
|
|
1445
|
+
* Get the underlying LangChain DynamicTool instance
|
|
1446
|
+
*/ key: "getDynamicTool",
|
|
1447
|
+
value: function getDynamicTool() {
|
|
1448
|
+
return this.dynamicTool;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
]);
|
|
1452
|
+
return SafeToolAdapter;
|
|
1453
|
+
}();
|
|
1454
|
+
|
|
1455
|
+
exports.AIResponseParser = AIResponseParser;
|
|
1456
|
+
exports.ChatAgentAdapter = ChatAgentAdapter;
|
|
1457
|
+
exports.OpenRouterAdapter = OpenRouterModelAdapter;
|
|
1458
|
+
exports.PROMPTS = PROMPTS;
|
|
1459
|
+
exports.SafeToolAdapter = SafeToolAdapter;
|
|
1460
|
+
exports.SystemPromptAdapter = SystemPromptAdapter;
|
|
1461
|
+
exports.UserPromptAdapter = UserPromptAdapter;
|