@agentica/core 0.29.6 → 0.30.1
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/lib/constants/AgenticaSystemPrompt.d.ts +1 -0
- package/lib/constants/AgenticaSystemPrompt.js +1 -0
- package/lib/constants/AgenticaSystemPrompt.js.map +1 -1
- package/lib/events/AgenticaEvent.d.ts +3 -1
- package/lib/events/AgenticaJsonParseErrorEvent.d.ts +8 -0
- package/lib/events/AgenticaJsonParseErrorEvent.js +3 -0
- package/lib/events/AgenticaJsonParseErrorEvent.js.map +1 -0
- package/lib/events/MicroAgenticaEvent.d.ts +3 -1
- package/lib/events/index.d.ts +1 -0
- package/lib/events/index.js +1 -0
- package/lib/events/index.js.map +1 -1
- package/lib/factory/events.d.ts +8 -1
- package/lib/factory/events.js +14 -2
- package/lib/factory/events.js.map +1 -1
- package/lib/functional/assertHttpController.js +14 -14
- package/lib/functional/assertHttpLlmApplication.js +14 -14
- package/lib/functional/validateHttpController.js +14 -14
- package/lib/functional/validateHttpLlmApplication.js +14 -14
- package/lib/index.mjs +231 -285
- package/lib/index.mjs.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +18 -5
- package/lib/orchestrate/call.d.ts +1 -1
- package/lib/orchestrate/call.js +198 -285
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/initialize.js +1 -1
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/select.js +1 -1
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/structures/IAgenticaSystemPrompt.d.ts +31 -0
- package/lib/structures/IMicroAgenticaSystemPrompt.d.ts +31 -0
- package/package.json +1 -1
- package/prompts/json_parse_error.md +32 -0
- package/src/constants/AgenticaSystemPrompt.ts +2 -0
- package/src/events/AgenticaEvent.ts +3 -0
- package/src/events/AgenticaJsonParseErrorEvent.ts +12 -0
- package/src/events/MicroAgenticaEvent.ts +4 -1
- package/src/events/index.ts +1 -0
- package/src/factory/events.ts +19 -1
- package/src/json/IAgenticaEventJson.ts +20 -4
- package/src/orchestrate/call.ts +272 -389
- package/src/orchestrate/initialize.ts +2 -2
- package/src/orchestrate/select.ts +2 -2
- package/src/structures/IAgenticaSystemPrompt.ts +32 -0
- package/src/structures/IMicroAgenticaSystemPrompt.ts +32 -0
package/lib/orchestrate/call.js
CHANGED
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.call = call;
|
|
13
13
|
const openapi_1 = require("@samchon/openapi");
|
|
14
|
+
const uuid_1 = require("uuid");
|
|
14
15
|
const AgenticaConstant_1 = require("../constants/AgenticaConstant");
|
|
15
16
|
const AgenticaDefaultPrompt_1 = require("../constants/AgenticaDefaultPrompt");
|
|
16
17
|
const AgenticaSystemPrompt_1 = require("../constants/AgenticaSystemPrompt");
|
|
@@ -22,16 +23,8 @@ const StreamUtil_1 = require("../utils/StreamUtil");
|
|
|
22
23
|
const cancelFunctionFromContext_1 = require("./internal/cancelFunctionFromContext");
|
|
23
24
|
function call(ctx, operations) {
|
|
24
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
function station(ctx, operations, validateEvents) {
|
|
29
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
31
|
-
// ----
|
|
32
|
-
// EXECUTE CHATGPT API
|
|
33
|
-
// ----
|
|
34
|
-
const completionStream = yield ctx.request("call", {
|
|
26
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
27
|
+
const stream = yield ctx.request("call", {
|
|
35
28
|
messages: [
|
|
36
29
|
// COMMON SYSTEM PROMPT
|
|
37
30
|
{
|
|
@@ -77,38 +70,22 @@ function station(ctx, operations, validateEvents) {
|
|
|
77
70
|
tool_choice: "auto",
|
|
78
71
|
// parallel_tool_calls: false,
|
|
79
72
|
});
|
|
80
|
-
|
|
81
|
-
// PROCESS COMPLETION
|
|
82
|
-
// ----
|
|
83
|
-
const chunks = yield StreamUtil_1.StreamUtil.readAll(completionStream);
|
|
73
|
+
const chunks = yield StreamUtil_1.StreamUtil.readAll(stream);
|
|
84
74
|
const completion = ChatGptCompletionMessageUtil_1.ChatGptCompletionMessageUtil.merge(chunks);
|
|
85
75
|
const executes = [];
|
|
86
76
|
for (const choice of completion.choices) {
|
|
87
77
|
for (const tc of (_g = choice.message.tool_calls) !== null && _g !== void 0 ? _g : []) {
|
|
88
78
|
if (tc.type === "function") {
|
|
89
|
-
const operation =
|
|
79
|
+
const operation = operations.find(s => s.name === tc.function.name);
|
|
90
80
|
if (operation === undefined) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const call = (0, events_1.createCallEvent)({
|
|
94
|
-
id: tc.id,
|
|
95
|
-
operation,
|
|
96
|
-
// @TODO add type assertion!
|
|
97
|
-
arguments: JSON.parse(tc.function.arguments),
|
|
98
|
-
});
|
|
99
|
-
if (call.operation.protocol === "http") {
|
|
100
|
-
fillHttpArguments({
|
|
101
|
-
operation: call.operation,
|
|
102
|
-
arguments: call.arguments,
|
|
103
|
-
});
|
|
81
|
+
continue; // Ignore unknown tool calls
|
|
104
82
|
}
|
|
105
|
-
ctx.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
executes.push(exec);
|
|
83
|
+
const event = yield predicate(ctx, operation, tc, [], (_j = (_h = ctx.config) === null || _h === void 0 ? void 0 : _h.retry) !== null && _j !== void 0 ? _j : AgenticaConstant_1.AgenticaConstant.RETRY);
|
|
84
|
+
ctx.dispatch(event);
|
|
85
|
+
executes.push(event);
|
|
109
86
|
if ((0, isAgenticaContext_1.isAgenticaContext)(ctx)) {
|
|
110
87
|
(0, cancelFunctionFromContext_1.cancelFunctionFromContext)(ctx, {
|
|
111
|
-
name:
|
|
88
|
+
name: event.operation.name,
|
|
112
89
|
reason: "completed",
|
|
113
90
|
});
|
|
114
91
|
}
|
|
@@ -118,7 +95,7 @@ function station(ctx, operations, validateEvents) {
|
|
|
118
95
|
&& choice.message.content != null
|
|
119
96
|
&& choice.message.content.length !== 0) {
|
|
120
97
|
const text = choice.message.content;
|
|
121
|
-
const event = (0, events_1.
|
|
98
|
+
const event = (0, events_1.createAssistantMessageEvent)({
|
|
122
99
|
get: () => text,
|
|
123
100
|
done: () => true,
|
|
124
101
|
stream: (0, StreamUtil_1.toAsyncGenerator)(text),
|
|
@@ -130,208 +107,116 @@ function station(ctx, operations, validateEvents) {
|
|
|
130
107
|
return executes;
|
|
131
108
|
});
|
|
132
109
|
}
|
|
133
|
-
function
|
|
110
|
+
function predicate(ctx, operation, toolCall, previousValidationErrors, life) {
|
|
134
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
call,
|
|
141
|
-
retry,
|
|
142
|
-
validateEvents,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
case "class": {
|
|
146
|
-
return propagateClass({ ctx, operation: call.operation, call, retry, validateEvents });
|
|
147
|
-
}
|
|
148
|
-
case "mcp": {
|
|
149
|
-
return propagateMcp({ ctx, operation: call.operation, call, retry, validateEvents });
|
|
150
|
-
}
|
|
151
|
-
default: {
|
|
152
|
-
call.operation;
|
|
153
|
-
throw new Error("Unsupported protocol");
|
|
154
|
-
}
|
|
112
|
+
// CHECK INPUT ARGUMENT
|
|
113
|
+
const call = parseArguments(operation, toolCall);
|
|
114
|
+
ctx.dispatch(call);
|
|
115
|
+
if (call.type === "jsonParseError") {
|
|
116
|
+
return correctJsonError(ctx, call, previousValidationErrors, life - 1);
|
|
155
117
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
function propagateHttp(props) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
var _a, _b, _c, _d, _e;
|
|
161
|
-
// ----
|
|
162
|
-
// HTTP PROTOCOL
|
|
163
|
-
// ----
|
|
164
|
-
// NESTED VALIDATOR
|
|
165
|
-
const check = props.operation.function.validate(props.call.arguments);
|
|
118
|
+
// CHECK TYPE VALIDATION
|
|
119
|
+
const check = operation.function.validate(call.arguments);
|
|
166
120
|
if (check.success === false) {
|
|
167
|
-
const
|
|
168
|
-
id:
|
|
169
|
-
operation
|
|
121
|
+
const event = (0, events_1.createValidateEvent)({
|
|
122
|
+
id: toolCall.id,
|
|
123
|
+
operation,
|
|
170
124
|
result: check,
|
|
171
125
|
});
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (props.retry++ < ((_b = (_a = props.ctx.config) === null || _a === void 0 ? void 0 : _a.retry) !== null && _b !== void 0 ? _b : AgenticaConstant_1.AgenticaConstant.RETRY)) {
|
|
175
|
-
const trial = yield correct(props.ctx, props.call, props.retry, check.errors, props.validateEvents);
|
|
176
|
-
if (trial !== null) {
|
|
177
|
-
return trial;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
// CALL HTTP API
|
|
183
|
-
const response = yield executeHttpOperation(props.operation, props.call.arguments);
|
|
184
|
-
// CHECK STATUS
|
|
185
|
-
const success = ((response.status === 400
|
|
186
|
-
|| response.status === 404
|
|
187
|
-
|| response.status === 422)
|
|
188
|
-
&& props.retry++ < ((_d = (_c = props.ctx.config) === null || _c === void 0 ? void 0 : _c.retry) !== null && _d !== void 0 ? _d : AgenticaConstant_1.AgenticaConstant.RETRY)
|
|
189
|
-
&& typeof response.body) === false;
|
|
190
|
-
// DISPATCH EVENT
|
|
191
|
-
return ((_e = (success === false
|
|
192
|
-
? yield correct(props.ctx, props.call, props.retry, response.body, props.validateEvents)
|
|
193
|
-
: null)) !== null && _e !== void 0 ? _e : (0, events_1.createExecuteEvent)({
|
|
194
|
-
operation: props.call.operation,
|
|
195
|
-
arguments: props.call.arguments,
|
|
196
|
-
value: response,
|
|
197
|
-
}));
|
|
198
|
-
}
|
|
199
|
-
catch (error) {
|
|
200
|
-
// DISPATCH ERROR
|
|
201
|
-
return (0, events_1.createExecuteEvent)({
|
|
202
|
-
operation: props.call.operation,
|
|
203
|
-
arguments: props.call.arguments,
|
|
204
|
-
value: {
|
|
205
|
-
status: 500,
|
|
206
|
-
headers: {},
|
|
207
|
-
body: error instanceof Error
|
|
208
|
-
? Object.assign(Object.assign({}, error), { name: error.name, message: error.message }) : error,
|
|
209
|
-
},
|
|
210
|
-
});
|
|
126
|
+
ctx.dispatch(event);
|
|
127
|
+
return correctTypeError(ctx, call, event, [...previousValidationErrors, event], life - 1);
|
|
211
128
|
}
|
|
129
|
+
// EXECUTE OPERATION
|
|
130
|
+
const execute = yield executeFunction(call, operation);
|
|
131
|
+
ctx.dispatch(execute);
|
|
132
|
+
return execute;
|
|
212
133
|
});
|
|
213
134
|
}
|
|
214
|
-
|
|
135
|
+
/* -----------------------------------------------------------
|
|
136
|
+
ERROR CORRECTORS
|
|
137
|
+
----------------------------------------------------------- */
|
|
138
|
+
function correctTypeError(ctx, callEvent, validateEvent, previousValidationErrors, life) {
|
|
215
139
|
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
var _a, _b, _c;
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const check = props.operation.function.validate(props.call.arguments);
|
|
222
|
-
if (check.success === false) {
|
|
223
|
-
const ve = (0, events_1.createValidateEvent)({
|
|
224
|
-
id: props.call.id,
|
|
225
|
-
operation: props.call.operation,
|
|
226
|
-
result: check,
|
|
227
|
-
});
|
|
228
|
-
props.ctx.dispatch(ve);
|
|
229
|
-
props.validateEvents.push(ve);
|
|
230
|
-
return ((_c = (props.retry++ < ((_b = (_a = props.ctx.config) === null || _a === void 0 ? void 0 : _a.retry) !== null && _b !== void 0 ? _b : AgenticaConstant_1.AgenticaConstant.RETRY)
|
|
231
|
-
? yield correct(props.ctx, props.call, props.retry, check.errors, props.validateEvents)
|
|
232
|
-
: null)) !== null && _c !== void 0 ? _c : (0, events_1.createExecuteEvent)({
|
|
233
|
-
operation: props.call.operation,
|
|
234
|
-
arguments: props.call.arguments,
|
|
140
|
+
var _a, _b, _c, _d;
|
|
141
|
+
return correctError(ctx, {
|
|
142
|
+
giveUp: () => (0, events_1.createExecuteEvent)({
|
|
143
|
+
operation: callEvent.operation,
|
|
144
|
+
arguments: callEvent.arguments,
|
|
235
145
|
value: {
|
|
236
|
-
name: "
|
|
237
|
-
message:
|
|
238
|
-
errors:
|
|
146
|
+
name: "ValidationError",
|
|
147
|
+
message: `Invalid arguments. The validation failed after ${AgenticaConstant_1.AgenticaConstant.RETRY} retries.`,
|
|
148
|
+
errors: validateEvent.result.errors,
|
|
239
149
|
},
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
150
|
+
}),
|
|
151
|
+
operation: callEvent.operation,
|
|
152
|
+
messageArguments: JSON.stringify(callEvent.arguments),
|
|
153
|
+
messageToolParam: {
|
|
154
|
+
role: "tool",
|
|
155
|
+
content: JSON.stringify(validateEvent.result.errors),
|
|
156
|
+
tool_call_id: callEvent.id,
|
|
157
|
+
},
|
|
158
|
+
systemPrompt: (_d = (_c = (_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.validate) === null || _c === void 0 ? void 0 : _c.call(_b, previousValidationErrors.slice(0, -1))) !== null && _d !== void 0 ? _d : [
|
|
159
|
+
AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE,
|
|
160
|
+
...(previousValidationErrors.length > 1
|
|
161
|
+
? [
|
|
162
|
+
"",
|
|
163
|
+
AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE_REPEATED.replace("${{HISTORICAL_ERRORS}}", JSON.stringify(previousValidationErrors.slice(0, -1).map(e => e.result.errors))),
|
|
164
|
+
]
|
|
165
|
+
: []),
|
|
166
|
+
].join("\n"),
|
|
167
|
+
life,
|
|
168
|
+
previousValidationErrors,
|
|
169
|
+
});
|
|
259
170
|
});
|
|
260
171
|
}
|
|
261
|
-
function
|
|
172
|
+
function correctJsonError(ctx, parseErrorEvent, previousValidationErrors, life) {
|
|
262
173
|
return __awaiter(this, void 0, void 0, function* () {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
174
|
+
var _a, _b, _c, _d;
|
|
175
|
+
return correctError(ctx, {
|
|
176
|
+
giveUp: () => (0, events_1.createExecuteEvent)({
|
|
177
|
+
operation: parseErrorEvent.operation,
|
|
178
|
+
arguments: {},
|
|
179
|
+
value: {
|
|
180
|
+
name: "JsonParseError",
|
|
181
|
+
message: `Invalid JSON format. The parsing failed after ${AgenticaConstant_1.AgenticaConstant.RETRY} retries.`,
|
|
182
|
+
arguments: parseErrorEvent.arguments,
|
|
183
|
+
errorMessage: parseErrorEvent.errorMessage,
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
operation: parseErrorEvent.operation,
|
|
187
|
+
messageArguments: parseErrorEvent.arguments,
|
|
188
|
+
messageToolParam: null,
|
|
189
|
+
systemPrompt: (_d = (_c = (_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.jsonParseError) === null || _c === void 0 ? void 0 : _c.call(_b, parseErrorEvent)) !== null && _d !== void 0 ? _d : AgenticaSystemPrompt_1.AgenticaSystemPrompt.JSON_PARSE_ERROR.replace("${{ERROR_MESSAGE}}", parseErrorEvent.errorMessage),
|
|
190
|
+
life,
|
|
191
|
+
previousValidationErrors,
|
|
192
|
+
});
|
|
283
193
|
});
|
|
284
194
|
}
|
|
285
|
-
function
|
|
286
|
-
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
195
|
+
function parseArguments(operation, toolCall) {
|
|
196
|
+
try {
|
|
197
|
+
const data = JSON.parse(toolCall.function.arguments);
|
|
198
|
+
return (0, events_1.createCallEvent)({
|
|
199
|
+
id: toolCall.id,
|
|
200
|
+
operation,
|
|
201
|
+
arguments: data,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
return (0, events_1.createJsonParseErrorEvent)({
|
|
206
|
+
id: toolCall.id,
|
|
207
|
+
operation,
|
|
208
|
+
arguments: toolCall.function.arguments,
|
|
209
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
210
|
+
});
|
|
211
|
+
}
|
|
296
212
|
}
|
|
297
|
-
|
|
298
|
-
* @throws {TypeError}
|
|
299
|
-
*/
|
|
300
|
-
function executeClassOperation(operation, operationArguments) {
|
|
213
|
+
function correctError(ctx, props) {
|
|
301
214
|
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
-
|
|
303
|
-
if (
|
|
304
|
-
return
|
|
305
|
-
application: operation.controller.application,
|
|
306
|
-
function: operation.function,
|
|
307
|
-
arguments: operationArguments,
|
|
308
|
-
});
|
|
215
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
216
|
+
if (props.life <= 0) {
|
|
217
|
+
return props.giveUp();
|
|
309
218
|
}
|
|
310
|
-
|
|
311
|
-
// But this is an intended error.
|
|
312
|
-
// There are two types of errors that can occur here.
|
|
313
|
-
// One is a TypeError caused by referencing an undefined value, and the other is a TypeError caused by calling something that isn't a function.
|
|
314
|
-
// These errors are intentional, and any call to this function must be wrapped in a try-catch block.
|
|
315
|
-
// Unless there is an overall structural improvement, this function will remain as-is.
|
|
316
|
-
return execute[operation.function.name](operationArguments);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
function executeMcpOperation(operation, operationArguments) {
|
|
320
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
321
|
-
return operation.controller.client.callTool({
|
|
322
|
-
method: operation.function.name,
|
|
323
|
-
name: operation.function.name,
|
|
324
|
-
arguments: operationArguments,
|
|
325
|
-
}).then(v => v.content);
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
function correct(ctx, call, retry, error, validateEvents) {
|
|
329
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
330
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
331
|
-
// ----
|
|
332
|
-
// EXECUTE CHATGPT API
|
|
333
|
-
// ----
|
|
334
|
-
const completionStream = yield ctx.request("call", {
|
|
219
|
+
const stream = yield ctx.request("call", {
|
|
335
220
|
messages: [
|
|
336
221
|
// COMMON SYSTEM PROMPT
|
|
337
222
|
{
|
|
@@ -346,41 +231,29 @@ function correct(ctx, call, retry, error, validateEvents) {
|
|
|
346
231
|
content: ctx.prompt.contents.map(histories_1.decodeUserMessageContent),
|
|
347
232
|
},
|
|
348
233
|
// TYPE CORRECTION
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
:
|
|
352
|
-
|
|
353
|
-
content: (_f = (_e = (_d = (_c = ctx.config) === null || _c === void 0 ? void 0 : _c.systemPrompt) === null || _d === void 0 ? void 0 : _d.execute) === null || _e === void 0 ? void 0 : _e.call(_d, ctx.histories)) !== null && _f !== void 0 ? _f : AgenticaSystemPrompt_1.AgenticaSystemPrompt.EXECUTE,
|
|
354
|
-
}]),
|
|
234
|
+
{
|
|
235
|
+
role: "system",
|
|
236
|
+
content: (_d = (_c = (_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.execute) === null || _c === void 0 ? void 0 : _c.call(_b, ctx.histories)) !== null && _d !== void 0 ? _d : AgenticaSystemPrompt_1.AgenticaSystemPrompt.EXECUTE,
|
|
237
|
+
},
|
|
355
238
|
{
|
|
356
239
|
role: "assistant",
|
|
357
240
|
tool_calls: [
|
|
358
241
|
{
|
|
359
242
|
type: "function",
|
|
360
|
-
id:
|
|
243
|
+
id: (0, uuid_1.v4)(),
|
|
361
244
|
function: {
|
|
362
|
-
name:
|
|
363
|
-
arguments:
|
|
245
|
+
name: props.operation.name,
|
|
246
|
+
arguments: props.messageArguments,
|
|
364
247
|
},
|
|
365
248
|
},
|
|
366
249
|
],
|
|
367
250
|
},
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
tool_call_id: call.id,
|
|
372
|
-
},
|
|
251
|
+
...(props.messageToolParam !== null
|
|
252
|
+
? [props.messageToolParam]
|
|
253
|
+
: []),
|
|
373
254
|
{
|
|
374
255
|
role: "system",
|
|
375
|
-
content:
|
|
376
|
-
AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE,
|
|
377
|
-
...(validateEvents.length > 1
|
|
378
|
-
? [
|
|
379
|
-
"",
|
|
380
|
-
AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE_REPEATED.replace("${{HISTORICAL_ERRORS}}", JSON.stringify(validateEvents.slice(0, -1).map(e => e.result.errors))),
|
|
381
|
-
]
|
|
382
|
-
: []),
|
|
383
|
-
].join("\n"),
|
|
256
|
+
content: props.systemPrompt,
|
|
384
257
|
},
|
|
385
258
|
],
|
|
386
259
|
// STACK FUNCTIONS
|
|
@@ -388,75 +261,115 @@ function correct(ctx, call, retry, error, validateEvents) {
|
|
|
388
261
|
{
|
|
389
262
|
type: "function",
|
|
390
263
|
function: {
|
|
391
|
-
name:
|
|
392
|
-
description:
|
|
264
|
+
name: props.operation.name,
|
|
265
|
+
description: props.operation.function.description,
|
|
393
266
|
/**
|
|
394
267
|
* @TODO fix it
|
|
395
268
|
* The property and value have a type mismatch, but it works.
|
|
396
269
|
*/
|
|
397
|
-
parameters: (("separated" in
|
|
398
|
-
&&
|
|
399
|
-
? ((
|
|
270
|
+
parameters: (("separated" in props.operation.function
|
|
271
|
+
&& props.operation.function.separated !== undefined)
|
|
272
|
+
? ((_f = (_e = props.operation.function.separated) === null || _e === void 0 ? void 0 : _e.llm) !== null && _f !== void 0 ? _f : {
|
|
400
273
|
$defs: {},
|
|
401
274
|
type: "object",
|
|
402
275
|
properties: {},
|
|
403
276
|
additionalProperties: false,
|
|
404
277
|
required: [],
|
|
405
278
|
})
|
|
406
|
-
:
|
|
279
|
+
: props.operation.function.parameters),
|
|
407
280
|
},
|
|
408
281
|
},
|
|
409
282
|
],
|
|
410
283
|
tool_choice: {
|
|
411
284
|
type: "function",
|
|
412
285
|
function: {
|
|
413
|
-
name:
|
|
286
|
+
name: props.operation.name,
|
|
414
287
|
},
|
|
415
288
|
},
|
|
416
289
|
// parallel_tool_calls: false,
|
|
417
290
|
});
|
|
418
|
-
const chunks = yield StreamUtil_1.StreamUtil.readAll(
|
|
291
|
+
const chunks = yield StreamUtil_1.StreamUtil.readAll(stream);
|
|
419
292
|
const completion = ChatGptCompletionMessageUtil_1.ChatGptCompletionMessageUtil.merge(chunks);
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
293
|
+
const toolCall = (_h = (_g = completion.choices[0]) === null || _g === void 0 ? void 0 : _g.message.tool_calls) === null || _h === void 0 ? void 0 : _h.find(s => s.function.name === props.operation.name);
|
|
294
|
+
return toolCall === undefined
|
|
295
|
+
? props.giveUp()
|
|
296
|
+
: predicate(ctx, props.operation, toolCall, props.previousValidationErrors, props.life);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/* -----------------------------------------------------------
|
|
300
|
+
FUNCTION EXECUTORS
|
|
301
|
+
----------------------------------------------------------- */
|
|
302
|
+
function executeFunction(call, operation) {
|
|
303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
try {
|
|
305
|
+
const value = yield (() => __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
switch (operation.protocol) {
|
|
307
|
+
case "class":
|
|
308
|
+
return executeClassFunction(call, operation);
|
|
309
|
+
case "http":
|
|
310
|
+
return executeHttpOperation(call, operation);
|
|
311
|
+
case "mcp":
|
|
312
|
+
return executeMcpOperation(call, operation);
|
|
313
|
+
default:
|
|
314
|
+
operation; // Ensure all cases are handled
|
|
315
|
+
throw new Error("Unknown protocol"); // never be happen
|
|
316
|
+
}
|
|
317
|
+
}))();
|
|
318
|
+
return (0, events_1.createExecuteEvent)({
|
|
319
|
+
operation: call.operation,
|
|
320
|
+
arguments: call.arguments,
|
|
321
|
+
value,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
return (0, events_1.createExecuteEvent)({
|
|
326
|
+
operation: call.operation,
|
|
327
|
+
arguments: call.arguments,
|
|
328
|
+
value: error instanceof Error
|
|
329
|
+
? Object.assign(Object.assign({}, error), { name: error.name, message: error.message }) : error,
|
|
330
|
+
});
|
|
426
331
|
}
|
|
427
|
-
return propagate(ctx, (0, events_1.createCallEvent)({
|
|
428
|
-
id: toolCall.id,
|
|
429
|
-
operation: call.operation,
|
|
430
|
-
arguments: JSON.parse(toolCall.function.arguments),
|
|
431
|
-
}), retry, validateEvents);
|
|
432
332
|
});
|
|
433
333
|
}
|
|
434
|
-
function
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
.body)) {
|
|
447
|
-
props.arguments.body = {};
|
|
448
|
-
}
|
|
449
|
-
if (route.query !== null && "query" in props.arguments && props.arguments.query === undefined) {
|
|
450
|
-
props.arguments.query = {};
|
|
451
|
-
}
|
|
334
|
+
function executeClassFunction(call, operation) {
|
|
335
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
const execute = operation.controller.execute;
|
|
337
|
+
const value = typeof execute === "function"
|
|
338
|
+
? yield execute({
|
|
339
|
+
application: operation.controller.application,
|
|
340
|
+
function: operation.function,
|
|
341
|
+
arguments: call.arguments,
|
|
342
|
+
})
|
|
343
|
+
: yield execute[operation.function.name](call.arguments);
|
|
344
|
+
return value;
|
|
345
|
+
});
|
|
452
346
|
}
|
|
453
|
-
function
|
|
454
|
-
return (
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
347
|
+
function executeHttpOperation(call, operation) {
|
|
348
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
349
|
+
const execute = operation.controller.execute;
|
|
350
|
+
const value = typeof execute === "function"
|
|
351
|
+
? yield execute({
|
|
352
|
+
connection: operation.controller.connection,
|
|
353
|
+
application: operation.controller.application,
|
|
354
|
+
function: operation.function,
|
|
355
|
+
arguments: call.arguments,
|
|
356
|
+
})
|
|
357
|
+
: yield openapi_1.HttpLlm.propagate({
|
|
358
|
+
connection: operation.controller.connection,
|
|
359
|
+
application: operation.controller.application,
|
|
360
|
+
function: operation.function,
|
|
361
|
+
input: call.arguments,
|
|
362
|
+
});
|
|
363
|
+
return value;
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
function executeMcpOperation(call, operation) {
|
|
367
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
368
|
+
return operation.controller.client.callTool({
|
|
369
|
+
method: operation.function.name,
|
|
370
|
+
name: operation.function.name,
|
|
371
|
+
arguments: call.arguments,
|
|
372
|
+
}).then(v => v.content);
|
|
373
|
+
});
|
|
461
374
|
}
|
|
462
375
|
//# sourceMappingURL=call.js.map
|