@agentica/core 0.29.6 → 0.30.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.
Files changed (44) hide show
  1. package/lib/constants/AgenticaSystemPrompt.d.ts +1 -0
  2. package/lib/constants/AgenticaSystemPrompt.js +1 -0
  3. package/lib/constants/AgenticaSystemPrompt.js.map +1 -1
  4. package/lib/events/AgenticaEvent.d.ts +3 -1
  5. package/lib/events/AgenticaJsonParseErrorEvent.d.ts +8 -0
  6. package/lib/events/AgenticaJsonParseErrorEvent.js +3 -0
  7. package/lib/events/AgenticaJsonParseErrorEvent.js.map +1 -0
  8. package/lib/events/MicroAgenticaEvent.d.ts +3 -1
  9. package/lib/events/index.d.ts +1 -0
  10. package/lib/events/index.js +1 -0
  11. package/lib/events/index.js.map +1 -1
  12. package/lib/factory/events.d.ts +8 -1
  13. package/lib/factory/events.js +14 -2
  14. package/lib/factory/events.js.map +1 -1
  15. package/lib/functional/assertHttpController.js +14 -14
  16. package/lib/functional/assertHttpLlmApplication.js +14 -14
  17. package/lib/functional/validateHttpController.js +14 -14
  18. package/lib/functional/validateHttpLlmApplication.js +14 -14
  19. package/lib/index.mjs +232 -285
  20. package/lib/index.mjs.map +1 -1
  21. package/lib/json/IAgenticaEventJson.d.ts +18 -5
  22. package/lib/orchestrate/call.d.ts +1 -1
  23. package/lib/orchestrate/call.js +199 -285
  24. package/lib/orchestrate/call.js.map +1 -1
  25. package/lib/orchestrate/initialize.js +1 -1
  26. package/lib/orchestrate/initialize.js.map +1 -1
  27. package/lib/orchestrate/select.js +1 -1
  28. package/lib/orchestrate/select.js.map +1 -1
  29. package/lib/structures/IAgenticaSystemPrompt.d.ts +31 -0
  30. package/lib/structures/IMicroAgenticaSystemPrompt.d.ts +31 -0
  31. package/package.json +1 -1
  32. package/prompts/json_parse_error.md +32 -0
  33. package/src/constants/AgenticaSystemPrompt.ts +2 -0
  34. package/src/events/AgenticaEvent.ts +3 -0
  35. package/src/events/AgenticaJsonParseErrorEvent.ts +12 -0
  36. package/src/events/MicroAgenticaEvent.ts +4 -1
  37. package/src/events/index.ts +1 -0
  38. package/src/factory/events.ts +19 -1
  39. package/src/json/IAgenticaEventJson.ts +20 -4
  40. package/src/orchestrate/call.ts +273 -389
  41. package/src/orchestrate/initialize.ts +2 -2
  42. package/src/orchestrate/select.ts +2 -2
  43. package/src/structures/IAgenticaSystemPrompt.ts +32 -0
  44. package/src/structures/IMicroAgenticaSystemPrompt.ts +32 -0
@@ -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
- return station(ctx, operations, []);
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 = ctx.operations.flat.get(tc.function.name);
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.dispatch(call);
106
- const exec = yield propagate(ctx, call, 0, validateEvents);
107
- ctx.dispatch(exec);
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: call.operation.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.creatAssistantMessageEvent)({
98
+ const event = (0, events_1.createAssistantMessageEvent)({
122
99
  get: () => text,
123
100
  done: () => true,
124
101
  stream: (0, StreamUtil_1.toAsyncGenerator)(text),
@@ -127,211 +104,120 @@ function station(ctx, operations, validateEvents) {
127
104
  ctx.dispatch(event);
128
105
  }
129
106
  }
107
+ console.error("call", executes);
130
108
  return executes;
131
109
  });
132
110
  }
133
- function propagate(ctx, call, retry, validateEvents) {
111
+ function predicate(ctx, operation, toolCall, previousValidationErrors, life) {
134
112
  return __awaiter(this, void 0, void 0, function* () {
135
- switch (call.operation.protocol) {
136
- case "http": {
137
- return propagateHttp({
138
- ctx,
139
- operation: call.operation,
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
- }
113
+ // CHECK INPUT ARGUMENT
114
+ const call = parseArguments(operation, toolCall);
115
+ ctx.dispatch(call);
116
+ if (call.type === "jsonParseError") {
117
+ return correctJsonError(ctx, call, previousValidationErrors, life - 1);
155
118
  }
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);
119
+ // CHECK TYPE VALIDATION
120
+ const check = operation.function.validate(call.arguments);
166
121
  if (check.success === false) {
167
- const ve = (0, events_1.createValidateEvent)({
168
- id: props.call.id,
169
- operation: props.call.operation,
122
+ const event = (0, events_1.createValidateEvent)({
123
+ id: toolCall.id,
124
+ operation,
170
125
  result: check,
171
126
  });
172
- props.ctx.dispatch(ve);
173
- props.validateEvents.push(ve);
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
- });
127
+ ctx.dispatch(event);
128
+ return correctTypeError(ctx, call, event, [...previousValidationErrors, event], life - 1);
211
129
  }
130
+ // EXECUTE OPERATION
131
+ const execute = yield executeFunction(call, operation);
132
+ ctx.dispatch(execute);
133
+ return execute;
212
134
  });
213
135
  }
214
- function propagateClass(props) {
136
+ /* -----------------------------------------------------------
137
+ ERROR CORRECTORS
138
+ ----------------------------------------------------------- */
139
+ function correctTypeError(ctx, callEvent, validateEvent, previousValidationErrors, life) {
215
140
  return __awaiter(this, void 0, void 0, function* () {
216
- var _a, _b, _c;
217
- // ----
218
- // CLASS FUNCTION
219
- // ----
220
- // VALIDATE FIRST
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,
141
+ var _a, _b, _c, _d;
142
+ return correctError(ctx, {
143
+ giveUp: () => (0, events_1.createExecuteEvent)({
144
+ operation: callEvent.operation,
145
+ arguments: callEvent.arguments,
235
146
  value: {
236
- name: "TypeGuardError",
237
- message: "Invalid arguments.",
238
- errors: check.errors,
147
+ name: "ValidationError",
148
+ message: `Invalid arguments. The validation failed after ${AgenticaConstant_1.AgenticaConstant.RETRY} retries.`,
149
+ errors: validateEvent.result.errors,
239
150
  },
240
- }));
241
- }
242
- // EXECUTE FUNCTION
243
- try {
244
- const value = yield executeClassOperation(props.operation, props.call.arguments);
245
- return (0, events_1.createExecuteEvent)({
246
- operation: props.call.operation,
247
- arguments: props.call.arguments,
248
- value,
249
- });
250
- }
251
- catch (error) {
252
- return (0, events_1.createExecuteEvent)({
253
- operation: props.call.operation,
254
- arguments: props.call.arguments,
255
- value: error instanceof Error
256
- ? Object.assign(Object.assign({}, error), { name: error.name, message: error.message }) : error,
257
- });
258
- }
151
+ }),
152
+ operation: callEvent.operation,
153
+ messageArguments: JSON.stringify(callEvent.arguments),
154
+ messageToolParam: {
155
+ role: "tool",
156
+ content: JSON.stringify(validateEvent.result.errors),
157
+ tool_call_id: callEvent.id,
158
+ },
159
+ 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 : [
160
+ AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE,
161
+ ...(previousValidationErrors.length > 1
162
+ ? [
163
+ "",
164
+ AgenticaSystemPrompt_1.AgenticaSystemPrompt.VALIDATE_REPEATED.replace("${{HISTORICAL_ERRORS}}", JSON.stringify(previousValidationErrors.slice(0, -1).map(e => e.result.errors))),
165
+ ]
166
+ : []),
167
+ ].join("\n"),
168
+ life,
169
+ previousValidationErrors,
170
+ });
259
171
  });
260
172
  }
261
- function propagateMcp(props) {
173
+ function correctJsonError(ctx, parseErrorEvent, previousValidationErrors, life) {
262
174
  return __awaiter(this, void 0, void 0, function* () {
263
- // ----
264
- // MCP PROTOCOL
265
- // ----
266
- // @TODO: implement argument validation logic
267
- try {
268
- const value = yield executeMcpOperation(props.operation, props.call.arguments);
269
- return (0, events_1.createExecuteEvent)({
270
- operation: props.call.operation,
271
- arguments: props.call.arguments,
272
- value,
273
- });
274
- }
275
- catch (error) {
276
- return (0, events_1.createExecuteEvent)({
277
- operation: props.call.operation,
278
- arguments: props.call.arguments,
279
- value: error instanceof Error
280
- ? Object.assign(Object.assign({}, error), { name: error.name, message: error.message }) : error,
281
- });
282
- }
175
+ var _a, _b, _c, _d;
176
+ return correctError(ctx, {
177
+ giveUp: () => (0, events_1.createExecuteEvent)({
178
+ operation: parseErrorEvent.operation,
179
+ arguments: {},
180
+ value: {
181
+ name: "JsonParseError",
182
+ message: `Invalid JSON format. The parsing failed after ${AgenticaConstant_1.AgenticaConstant.RETRY} retries.`,
183
+ arguments: parseErrorEvent.arguments,
184
+ errorMessage: parseErrorEvent.errorMessage,
185
+ },
186
+ }),
187
+ operation: parseErrorEvent.operation,
188
+ messageArguments: parseErrorEvent.arguments,
189
+ messageToolParam: null,
190
+ 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),
191
+ life,
192
+ previousValidationErrors,
193
+ });
283
194
  });
284
195
  }
285
- function executeHttpOperation(operation, operationArguments) {
286
- return __awaiter(this, void 0, void 0, function* () {
287
- const controllerBaseArguments = {
288
- connection: operation.controller.connection,
289
- application: operation.controller.application,
290
- function: operation.function,
291
- };
292
- return operation.controller.execute !== undefined
293
- ? operation.controller.execute(Object.assign(Object.assign({}, controllerBaseArguments), { arguments: operationArguments }))
294
- : openapi_1.HttpLlm.propagate(Object.assign(Object.assign({}, controllerBaseArguments), { input: operationArguments }));
295
- });
196
+ function parseArguments(operation, toolCall) {
197
+ try {
198
+ const data = JSON.parse(toolCall.function.arguments);
199
+ return (0, events_1.createCallEvent)({
200
+ id: toolCall.id,
201
+ operation,
202
+ arguments: data,
203
+ });
204
+ }
205
+ catch (error) {
206
+ return (0, events_1.createJsonParseErrorEvent)({
207
+ id: toolCall.id,
208
+ operation,
209
+ arguments: toolCall.function.arguments,
210
+ errorMessage: error instanceof Error ? error.message : String(error),
211
+ });
212
+ }
296
213
  }
297
- /**
298
- * @throws {TypeError}
299
- */
300
- function executeClassOperation(operation, operationArguments) {
214
+ function correctError(ctx, props) {
301
215
  return __awaiter(this, void 0, void 0, function* () {
302
- const execute = operation.controller.execute;
303
- if (typeof execute === "function") {
304
- return yield execute({
305
- application: operation.controller.application,
306
- function: operation.function,
307
- arguments: operationArguments,
308
- });
216
+ var _a, _b, _c, _d, _e, _f, _g, _h;
217
+ if (props.life <= 0) {
218
+ return props.giveUp();
309
219
  }
310
- // As you know, it's very unstable logic.
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", {
220
+ const stream = yield ctx.request("call", {
335
221
  messages: [
336
222
  // COMMON SYSTEM PROMPT
337
223
  {
@@ -346,41 +232,29 @@ function correct(ctx, call, retry, error, validateEvents) {
346
232
  content: ctx.prompt.contents.map(histories_1.decodeUserMessageContent),
347
233
  },
348
234
  // TYPE CORRECTION
349
- ...(((_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.execute) === null
350
- ? []
351
- : [{
352
- role: "system",
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
- }]),
235
+ {
236
+ role: "system",
237
+ 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,
238
+ },
355
239
  {
356
240
  role: "assistant",
357
241
  tool_calls: [
358
242
  {
359
243
  type: "function",
360
- id: call.id,
244
+ id: (0, uuid_1.v4)(),
361
245
  function: {
362
- name: call.operation.name,
363
- arguments: JSON.stringify(call.arguments),
246
+ name: props.operation.name,
247
+ arguments: props.messageArguments,
364
248
  },
365
249
  },
366
250
  ],
367
251
  },
368
- {
369
- role: "tool",
370
- content: typeof error === "string" ? error : JSON.stringify(error),
371
- tool_call_id: call.id,
372
- },
252
+ ...(props.messageToolParam !== null
253
+ ? [props.messageToolParam]
254
+ : []),
373
255
  {
374
256
  role: "system",
375
- content: (_k = (_j = (_h = (_g = ctx.config) === null || _g === void 0 ? void 0 : _g.systemPrompt) === null || _h === void 0 ? void 0 : _h.validate) === null || _j === void 0 ? void 0 : _j.call(_h, validateEvents.slice(0, -1))) !== null && _k !== void 0 ? _k : [
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"),
257
+ content: props.systemPrompt,
384
258
  },
385
259
  ],
386
260
  // STACK FUNCTIONS
@@ -388,75 +262,115 @@ function correct(ctx, call, retry, error, validateEvents) {
388
262
  {
389
263
  type: "function",
390
264
  function: {
391
- name: call.operation.name,
392
- description: call.operation.function.description,
265
+ name: props.operation.name,
266
+ description: props.operation.function.description,
393
267
  /**
394
268
  * @TODO fix it
395
269
  * The property and value have a type mismatch, but it works.
396
270
  */
397
- parameters: (("separated" in call.operation.function
398
- && call.operation.function.separated !== undefined)
399
- ? ((_m = (_l = call.operation.function.separated) === null || _l === void 0 ? void 0 : _l.llm) !== null && _m !== void 0 ? _m : {
271
+ parameters: (("separated" in props.operation.function
272
+ && props.operation.function.separated !== undefined)
273
+ ? ((_f = (_e = props.operation.function.separated) === null || _e === void 0 ? void 0 : _e.llm) !== null && _f !== void 0 ? _f : {
400
274
  $defs: {},
401
275
  type: "object",
402
276
  properties: {},
403
277
  additionalProperties: false,
404
278
  required: [],
405
279
  })
406
- : call.operation.function.parameters),
280
+ : props.operation.function.parameters),
407
281
  },
408
282
  },
409
283
  ],
410
284
  tool_choice: {
411
285
  type: "function",
412
286
  function: {
413
- name: call.operation.name,
287
+ name: props.operation.name,
414
288
  },
415
289
  },
416
290
  // parallel_tool_calls: false,
417
291
  });
418
- const chunks = yield StreamUtil_1.StreamUtil.readAll(completionStream);
292
+ const chunks = yield StreamUtil_1.StreamUtil.readAll(stream);
419
293
  const completion = ChatGptCompletionMessageUtil_1.ChatGptCompletionMessageUtil.merge(chunks);
420
- // ----
421
- // PROCESS COMPLETION
422
- // ----
423
- const toolCall = ((_p = (_o = completion.choices[0]) === null || _o === void 0 ? void 0 : _o.message.tool_calls) !== null && _p !== void 0 ? _p : []).find(tc => tc.type === "function" && tc.function.name === call.operation.name);
424
- if (toolCall === undefined) {
425
- return null;
294
+ 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);
295
+ return toolCall === undefined
296
+ ? props.giveUp()
297
+ : predicate(ctx, props.operation, toolCall, props.previousValidationErrors, props.life);
298
+ });
299
+ }
300
+ /* -----------------------------------------------------------
301
+ FUNCTION EXECUTORS
302
+ ----------------------------------------------------------- */
303
+ function executeFunction(call, operation) {
304
+ return __awaiter(this, void 0, void 0, function* () {
305
+ try {
306
+ const value = yield (() => __awaiter(this, void 0, void 0, function* () {
307
+ switch (operation.protocol) {
308
+ case "class":
309
+ return executeClassFunction(call, operation);
310
+ case "http":
311
+ return executeHttpOperation(call, operation);
312
+ case "mcp":
313
+ return executeMcpOperation(call, operation);
314
+ default:
315
+ operation; // Ensure all cases are handled
316
+ throw new Error("Unknown protocol"); // never be happen
317
+ }
318
+ }))();
319
+ return (0, events_1.createExecuteEvent)({
320
+ operation: call.operation,
321
+ arguments: call.arguments,
322
+ value,
323
+ });
324
+ }
325
+ catch (error) {
326
+ return (0, events_1.createExecuteEvent)({
327
+ operation: call.operation,
328
+ arguments: call.arguments,
329
+ value: error instanceof Error
330
+ ? Object.assign(Object.assign({}, error), { name: error.name, message: error.message }) : error,
331
+ });
426
332
  }
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
333
  });
433
334
  }
434
- function fillHttpArguments(props) {
435
- var _a;
436
- if (props.operation.protocol !== "http") {
437
- return;
438
- }
439
- const route = props.operation.function.route();
440
- if (route.body !== null
441
- && ((_a = route.operation().requestBody) === null || _a === void 0 ? void 0 : _a.required) === true
442
- && "body" in props.arguments
443
- && isObject(props.operation.function.parameters
444
- .$defs, props.operation.function.parameters
445
- .properties
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
- }
335
+ function executeClassFunction(call, operation) {
336
+ return __awaiter(this, void 0, void 0, function* () {
337
+ const execute = operation.controller.execute;
338
+ const value = typeof execute === "function"
339
+ ? yield execute({
340
+ application: operation.controller.application,
341
+ function: operation.function,
342
+ arguments: call.arguments,
343
+ })
344
+ : yield execute[operation.function.name](call.arguments);
345
+ return value;
346
+ });
452
347
  }
453
- function isObject($defs, schema) {
454
- return (openapi_1.ChatGptTypeChecker.isObject(schema)
455
- || (openapi_1.ChatGptTypeChecker.isReference(schema)
456
- && isObject($defs, $defs[schema.$ref.split("/").at(-1)]))
457
- || (openapi_1.ChatGptTypeChecker.isAnyOf(schema)
458
- && schema.anyOf.every(schema => isObject($defs, schema)))
459
- || (openapi_1.LlmTypeCheckerV3_1.isOneOf(schema)
460
- && schema.oneOf.every(schema => isObject($defs, schema))));
348
+ function executeHttpOperation(call, operation) {
349
+ return __awaiter(this, void 0, void 0, function* () {
350
+ const execute = operation.controller.execute;
351
+ const value = typeof execute === "function"
352
+ ? yield execute({
353
+ connection: operation.controller.connection,
354
+ application: operation.controller.application,
355
+ function: operation.function,
356
+ arguments: call.arguments,
357
+ })
358
+ : yield openapi_1.HttpLlm.propagate({
359
+ connection: operation.controller.connection,
360
+ application: operation.controller.application,
361
+ function: operation.function,
362
+ input: call.arguments,
363
+ });
364
+ return value;
365
+ });
366
+ }
367
+ function executeMcpOperation(call, operation) {
368
+ return __awaiter(this, void 0, void 0, function* () {
369
+ return operation.controller.client.callTool({
370
+ method: operation.function.name,
371
+ name: operation.function.name,
372
+ arguments: call.arguments,
373
+ }).then(v => v.content);
374
+ });
461
375
  }
462
376
  //# sourceMappingURL=call.js.map