@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.
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 +231 -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 +198 -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 +272 -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),
@@ -130,208 +107,116 @@ function station(ctx, operations, validateEvents) {
130
107
  return executes;
131
108
  });
132
109
  }
133
- function propagate(ctx, call, retry, validateEvents) {
110
+ function predicate(ctx, operation, toolCall, previousValidationErrors, life) {
134
111
  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
- }
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 ve = (0, events_1.createValidateEvent)({
168
- id: props.call.id,
169
- operation: props.call.operation,
121
+ const event = (0, events_1.createValidateEvent)({
122
+ id: toolCall.id,
123
+ operation,
170
124
  result: check,
171
125
  });
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
- });
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
- function propagateClass(props) {
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
- // 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,
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: "TypeGuardError",
237
- message: "Invalid arguments.",
238
- errors: check.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
- // 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
- }
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 propagateMcp(props) {
172
+ function correctJsonError(ctx, parseErrorEvent, previousValidationErrors, life) {
262
173
  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
- }
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 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
- });
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
- 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
- });
215
+ var _a, _b, _c, _d, _e, _f, _g, _h;
216
+ if (props.life <= 0) {
217
+ return props.giveUp();
309
218
  }
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", {
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
- ...(((_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
- }]),
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: call.id,
243
+ id: (0, uuid_1.v4)(),
361
244
  function: {
362
- name: call.operation.name,
363
- arguments: JSON.stringify(call.arguments),
245
+ name: props.operation.name,
246
+ arguments: props.messageArguments,
364
247
  },
365
248
  },
366
249
  ],
367
250
  },
368
- {
369
- role: "tool",
370
- content: typeof error === "string" ? error : JSON.stringify(error),
371
- tool_call_id: call.id,
372
- },
251
+ ...(props.messageToolParam !== null
252
+ ? [props.messageToolParam]
253
+ : []),
373
254
  {
374
255
  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"),
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: call.operation.name,
392
- description: call.operation.function.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 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 : {
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
- : call.operation.function.parameters),
279
+ : props.operation.function.parameters),
407
280
  },
408
281
  },
409
282
  ],
410
283
  tool_choice: {
411
284
  type: "function",
412
285
  function: {
413
- name: call.operation.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(completionStream);
291
+ const chunks = yield StreamUtil_1.StreamUtil.readAll(stream);
419
292
  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;
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 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
- }
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 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))));
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