@agentica/core 0.7.0-dev.20250224

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +464 -0
  3. package/lib/Agentica.d.ts +124 -0
  4. package/lib/Agentica.js +271 -0
  5. package/lib/Agentica.js.map +1 -0
  6. package/lib/chatgpt/ChatGptAgent.d.ts +6 -0
  7. package/lib/chatgpt/ChatGptAgent.js +59 -0
  8. package/lib/chatgpt/ChatGptAgent.js.map +1 -0
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +5 -0
  10. package/lib/chatgpt/ChatGptCallFunctionAgent.js +362 -0
  11. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -0
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +8 -0
  13. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +359 -0
  14. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -0
  15. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +5 -0
  16. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -0
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -0
  18. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +5 -0
  19. package/lib/chatgpt/ChatGptHistoryDecoder.js +77 -0
  20. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -0
  21. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +5 -0
  22. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +1883 -0
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -0
  24. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +5 -0
  25. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +381 -0
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -0
  27. package/lib/functional/createHttpLlmApplication.d.ts +33 -0
  28. package/lib/functional/createHttpLlmApplication.js +7766 -0
  29. package/lib/functional/createHttpLlmApplication.js.map +1 -0
  30. package/lib/index.d.ts +16 -0
  31. package/lib/index.js +35 -0
  32. package/lib/index.js.map +1 -0
  33. package/lib/index.mjs +10366 -0
  34. package/lib/index.mjs.map +1 -0
  35. package/lib/internal/AgenticaConstant.d.ts +4 -0
  36. package/lib/internal/AgenticaConstant.js +9 -0
  37. package/lib/internal/AgenticaConstant.js.map +1 -0
  38. package/lib/internal/AgenticaCostAggregator.d.ts +5 -0
  39. package/lib/internal/AgenticaCostAggregator.js +30 -0
  40. package/lib/internal/AgenticaCostAggregator.js.map +1 -0
  41. package/lib/internal/AgenticaDefaultPrompt.d.ts +4 -0
  42. package/lib/internal/AgenticaDefaultPrompt.js +32 -0
  43. package/lib/internal/AgenticaDefaultPrompt.js.map +1 -0
  44. package/lib/internal/AgenticaOperationComposer.d.ts +9 -0
  45. package/lib/internal/AgenticaOperationComposer.js +58 -0
  46. package/lib/internal/AgenticaOperationComposer.js.map +1 -0
  47. package/lib/internal/AgenticaPromptFactory.d.ts +6 -0
  48. package/lib/internal/AgenticaPromptFactory.js +9 -0
  49. package/lib/internal/AgenticaPromptFactory.js.map +1 -0
  50. package/lib/internal/AgenticaPromptTransformer.d.ts +9 -0
  51. package/lib/internal/AgenticaPromptTransformer.js +58 -0
  52. package/lib/internal/AgenticaPromptTransformer.js.map +1 -0
  53. package/lib/internal/AgenticaSystemPrompt.d.ts +8 -0
  54. package/lib/internal/AgenticaSystemPrompt.js +13 -0
  55. package/lib/internal/AgenticaSystemPrompt.js.map +1 -0
  56. package/lib/internal/MathUtil.d.ts +3 -0
  57. package/lib/internal/MathUtil.js +8 -0
  58. package/lib/internal/MathUtil.js.map +1 -0
  59. package/lib/internal/Singleton.d.ts +1 -0
  60. package/lib/internal/Singleton.js +23 -0
  61. package/lib/internal/Singleton.js.map +1 -0
  62. package/lib/internal/__map_take.d.ts +1 -0
  63. package/lib/internal/__map_take.js +16 -0
  64. package/lib/internal/__map_take.js.map +1 -0
  65. package/lib/structures/IAgenticaConfig.d.ts +112 -0
  66. package/lib/structures/IAgenticaConfig.js +3 -0
  67. package/lib/structures/IAgenticaConfig.js.map +1 -0
  68. package/lib/structures/IAgenticaContext.d.ts +106 -0
  69. package/lib/structures/IAgenticaContext.js +3 -0
  70. package/lib/structures/IAgenticaContext.js.map +1 -0
  71. package/lib/structures/IAgenticaController.d.ts +110 -0
  72. package/lib/structures/IAgenticaController.js +3 -0
  73. package/lib/structures/IAgenticaController.js.map +1 -0
  74. package/lib/structures/IAgenticaEvent.d.ts +191 -0
  75. package/lib/structures/IAgenticaEvent.js +3 -0
  76. package/lib/structures/IAgenticaEvent.js.map +1 -0
  77. package/lib/structures/IAgenticaExecutor.d.ts +144 -0
  78. package/lib/structures/IAgenticaExecutor.js +3 -0
  79. package/lib/structures/IAgenticaExecutor.js.map +1 -0
  80. package/lib/structures/IAgenticaOperation.d.ts +48 -0
  81. package/lib/structures/IAgenticaOperation.js +3 -0
  82. package/lib/structures/IAgenticaOperation.js.map +1 -0
  83. package/lib/structures/IAgenticaOperationCollection.d.ts +46 -0
  84. package/lib/structures/IAgenticaOperationCollection.js +3 -0
  85. package/lib/structures/IAgenticaOperationCollection.js.map +1 -0
  86. package/lib/structures/IAgenticaOperationSelection.d.ts +51 -0
  87. package/lib/structures/IAgenticaOperationSelection.js +3 -0
  88. package/lib/structures/IAgenticaOperationSelection.js.map +1 -0
  89. package/lib/structures/IAgenticaPrompt.d.ts +139 -0
  90. package/lib/structures/IAgenticaPrompt.js +3 -0
  91. package/lib/structures/IAgenticaPrompt.js.map +1 -0
  92. package/lib/structures/IAgenticaProps.d.ts +59 -0
  93. package/lib/structures/IAgenticaProps.js +3 -0
  94. package/lib/structures/IAgenticaProps.js.map +1 -0
  95. package/lib/structures/IAgenticaProvider.d.ts +41 -0
  96. package/lib/structures/IAgenticaProvider.js +3 -0
  97. package/lib/structures/IAgenticaProvider.js.map +1 -0
  98. package/lib/structures/IAgenticaSystemPrompt.d.ts +116 -0
  99. package/lib/structures/IAgenticaSystemPrompt.js +3 -0
  100. package/lib/structures/IAgenticaSystemPrompt.js.map +1 -0
  101. package/lib/structures/IAgenticaTokenUsage.d.ts +50 -0
  102. package/lib/structures/IAgenticaTokenUsage.js +3 -0
  103. package/lib/structures/IAgenticaTokenUsage.js.map +1 -0
  104. package/lib/structures/internal/__IChatCancelFunctionsApplication.d.ts +22 -0
  105. package/lib/structures/internal/__IChatCancelFunctionsApplication.js +3 -0
  106. package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +1 -0
  107. package/lib/structures/internal/__IChatFunctionReference.d.ts +20 -0
  108. package/lib/structures/internal/__IChatFunctionReference.js +3 -0
  109. package/lib/structures/internal/__IChatFunctionReference.js.map +1 -0
  110. package/lib/structures/internal/__IChatInitialApplication.d.ts +14 -0
  111. package/lib/structures/internal/__IChatInitialApplication.js +3 -0
  112. package/lib/structures/internal/__IChatInitialApplication.js.map +1 -0
  113. package/lib/structures/internal/__IChatSelectFunctionsApplication.d.ts +23 -0
  114. package/lib/structures/internal/__IChatSelectFunctionsApplication.js +3 -0
  115. package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +1 -0
  116. package/lib/typings/AgenticaSource.d.ts +1 -0
  117. package/lib/typings/AgenticaSource.js +3 -0
  118. package/lib/typings/AgenticaSource.js.map +1 -0
  119. package/package.json +74 -0
  120. package/prompts/cancel.md +5 -0
  121. package/prompts/common.md +3 -0
  122. package/prompts/describe.md +7 -0
  123. package/prompts/execute.md +7 -0
  124. package/prompts/initialize.md +3 -0
  125. package/prompts/select.md +7 -0
  126. package/src/Agentica.ts +322 -0
  127. package/src/chatgpt/ChatGptAgent.ts +71 -0
  128. package/src/chatgpt/ChatGptCallFunctionAgent.ts +445 -0
  129. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +283 -0
  130. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +51 -0
  131. package/src/chatgpt/ChatGptHistoryDecoder.ts +86 -0
  132. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -0
  133. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +316 -0
  134. package/src/functional/createHttpLlmApplication.ts +63 -0
  135. package/src/index.ts +19 -0
  136. package/src/internal/AgenticaConstant.ts +4 -0
  137. package/src/internal/AgenticaCostAggregator.ts +35 -0
  138. package/src/internal/AgenticaDefaultPrompt.ts +39 -0
  139. package/src/internal/AgenticaOperationComposer.ts +82 -0
  140. package/src/internal/AgenticaPromptFactory.ts +30 -0
  141. package/src/internal/AgenticaPromptTransformer.ts +83 -0
  142. package/src/internal/AgenticaSystemPrompt.ts +14 -0
  143. package/src/internal/MathUtil.ts +3 -0
  144. package/src/internal/Singleton.ts +22 -0
  145. package/src/internal/__map_take.ts +15 -0
  146. package/src/structures/IAgenticaConfig.ts +121 -0
  147. package/src/structures/IAgenticaContext.ts +128 -0
  148. package/src/structures/IAgenticaController.ts +130 -0
  149. package/src/structures/IAgenticaEvent.ts +224 -0
  150. package/src/structures/IAgenticaExecutor.ts +152 -0
  151. package/src/structures/IAgenticaOperation.ts +64 -0
  152. package/src/structures/IAgenticaOperationCollection.ts +50 -0
  153. package/src/structures/IAgenticaOperationSelection.ts +69 -0
  154. package/src/structures/IAgenticaPrompt.ts +173 -0
  155. package/src/structures/IAgenticaProps.ts +64 -0
  156. package/src/structures/IAgenticaProvider.ts +45 -0
  157. package/src/structures/IAgenticaSystemPrompt.ts +122 -0
  158. package/src/structures/IAgenticaTokenUsage.ts +52 -0
  159. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -0
  160. package/src/structures/internal/__IChatFunctionReference.ts +21 -0
  161. package/src/structures/internal/__IChatInitialApplication.ts +15 -0
  162. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -0
  163. package/src/typings/AgenticaSource.ts +6 -0
@@ -0,0 +1,445 @@
1
+ import {
2
+ ChatGptTypeChecker,
3
+ HttpLlm,
4
+ IChatGptSchema,
5
+ IHttpMigrateRoute,
6
+ IHttpResponse,
7
+ } from "@samchon/openapi";
8
+ import OpenAI from "openai";
9
+ import { IValidation } from "typia";
10
+
11
+ import { AgenticaConstant } from "../internal/AgenticaConstant";
12
+ import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
13
+ import { AgenticaPromptFactory } from "../internal/AgenticaPromptFactory";
14
+ import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
15
+ import { IAgenticaContext } from "../structures/IAgenticaContext";
16
+ import { IAgenticaEvent } from "../structures/IAgenticaEvent";
17
+ import { IAgenticaOperation } from "../structures/IAgenticaOperation";
18
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
19
+ import { ChatGptCancelFunctionAgent } from "./ChatGptCancelFunctionAgent";
20
+ import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
21
+
22
+ export namespace ChatGptCallFunctionAgent {
23
+ export const execute = async (
24
+ ctx: IAgenticaContext,
25
+ ): Promise<IAgenticaPrompt[]> => {
26
+ //----
27
+ // EXECUTE CHATGPT API
28
+ //----
29
+ const completion: OpenAI.ChatCompletion = await ctx.request("execute", {
30
+ messages: [
31
+ // COMMON SYSTEM PROMPT
32
+ {
33
+ role: "system",
34
+ content: AgenticaDefaultPrompt.write(ctx.config),
35
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
36
+ // PREVIOUS HISTORIES
37
+ ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
38
+ // USER INPUT
39
+ {
40
+ role: "user",
41
+ content: ctx.prompt.text,
42
+ },
43
+ // SYSTEM PROMPT
44
+ {
45
+ role: "system",
46
+ content:
47
+ ctx.config?.systemPrompt?.execute?.(ctx.histories) ??
48
+ AgenticaSystemPrompt.EXECUTE,
49
+ },
50
+ ],
51
+ // STACKED FUNCTIONS
52
+ tools: ctx.stack.map(
53
+ (op) =>
54
+ ({
55
+ type: "function",
56
+ function: {
57
+ name: op.name,
58
+ description: op.function.description,
59
+ parameters: (op.function.separated
60
+ ? (op.function.separated.llm ??
61
+ ({
62
+ type: "object",
63
+ properties: {},
64
+ required: [],
65
+ additionalProperties: false,
66
+ $defs: {},
67
+ } satisfies IChatGptSchema.IParameters))
68
+ : op.function.parameters) as Record<string, any>,
69
+ },
70
+ }) as OpenAI.ChatCompletionTool,
71
+ ),
72
+ tool_choice: "auto",
73
+ parallel_tool_calls: false,
74
+ });
75
+
76
+ //----
77
+ // PROCESS COMPLETION
78
+ //----
79
+ const closures: Array<
80
+ () => Promise<
81
+ Array<
82
+ | IAgenticaPrompt.IExecute
83
+ | IAgenticaPrompt.ICancel
84
+ | IAgenticaPrompt.IText
85
+ >
86
+ >
87
+ > = [];
88
+ for (const choice of completion.choices) {
89
+ for (const tc of choice.message.tool_calls ?? []) {
90
+ if (tc.type === "function") {
91
+ const operation: IAgenticaOperation | undefined =
92
+ ctx.operations.flat.get(tc.function.name);
93
+ if (operation === undefined) continue;
94
+ closures.push(
95
+ async (): Promise<
96
+ [IAgenticaPrompt.IExecute, IAgenticaPrompt.ICancel]
97
+ > => {
98
+ const call: IAgenticaEvent.ICall = {
99
+ type: "call",
100
+ id: tc.id,
101
+ operation,
102
+ arguments: JSON.parse(tc.function.arguments),
103
+ };
104
+ if (call.operation.protocol === "http")
105
+ fillHttpArguments({
106
+ operation: call.operation,
107
+ arguments: call.arguments,
108
+ });
109
+ await ctx.dispatch(call);
110
+
111
+ const execute: IAgenticaPrompt.IExecute = await propagate(
112
+ ctx,
113
+ call,
114
+ 0,
115
+ );
116
+ await ctx.dispatch({
117
+ type: "execute",
118
+ id: call.id,
119
+ operation: call.operation,
120
+ arguments: execute.arguments,
121
+ value: execute.value,
122
+ });
123
+
124
+ await ChatGptCancelFunctionAgent.cancelFunction(ctx, {
125
+ name: call.operation.name,
126
+ reason: "completed",
127
+ });
128
+ await ctx.dispatch({
129
+ type: "cancel",
130
+ operation: call.operation,
131
+ reason: "complete",
132
+ });
133
+ return [
134
+ execute,
135
+ {
136
+ type: "cancel",
137
+ id: call.id,
138
+ operations: [
139
+ AgenticaPromptFactory.selection({
140
+ ...call.operation,
141
+ reason: "complete",
142
+ }),
143
+ ],
144
+ } satisfies IAgenticaPrompt.ICancel,
145
+ ] as const;
146
+ },
147
+ );
148
+ }
149
+ }
150
+ if (
151
+ choice.message.role === "assistant" &&
152
+ !!choice.message.content?.length
153
+ )
154
+ closures.push(async () => {
155
+ const value: IAgenticaPrompt.IText = {
156
+ type: "text",
157
+ role: "assistant",
158
+ text: choice.message.content!,
159
+ };
160
+ await ctx.dispatch(value);
161
+ return [value];
162
+ });
163
+ }
164
+ return (await Promise.all(closures.map((fn) => fn()))).flat();
165
+ };
166
+
167
+ const propagate = async (
168
+ ctx: IAgenticaContext,
169
+ call: IAgenticaEvent.ICall,
170
+ retry: number,
171
+ ): Promise<IAgenticaPrompt.IExecute> => {
172
+ if (call.operation.protocol === "http") {
173
+ //----
174
+ // HTTP PROTOCOL
175
+ //----
176
+ try {
177
+ // CALL HTTP API
178
+ const response: IHttpResponse = call.operation.controller.execute
179
+ ? await call.operation.controller.execute({
180
+ connection: call.operation.controller.connection,
181
+ application: call.operation.controller.application,
182
+ function: call.operation.function,
183
+ arguments: call.arguments,
184
+ })
185
+ : await HttpLlm.propagate({
186
+ connection: call.operation.controller.connection,
187
+ application: call.operation.controller.application,
188
+ function: call.operation.function,
189
+ input: call.arguments,
190
+ });
191
+ // CHECK STATUS
192
+ const success: boolean =
193
+ ((response.status === 400 ||
194
+ response.status === 404 ||
195
+ response.status === 422) &&
196
+ retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY) &&
197
+ typeof response.body) === false;
198
+ // DISPATCH EVENT
199
+ return (
200
+ (success === false
201
+ ? await correct(ctx, call, retry, response.body)
202
+ : null) ??
203
+ (await AgenticaPromptFactory.execute({
204
+ type: "execute",
205
+ protocol: "http",
206
+ controller: call.operation.controller,
207
+ function: call.operation.function,
208
+ id: call.id,
209
+ name: call.operation.name,
210
+ arguments: call.arguments,
211
+ value: response,
212
+ }))
213
+ );
214
+ } catch (error) {
215
+ // DISPATCH ERROR
216
+ return AgenticaPromptFactory.execute({
217
+ type: "execute",
218
+ protocol: "http",
219
+ controller: call.operation.controller,
220
+ function: call.operation.function,
221
+ id: call.id,
222
+ name: call.operation.name,
223
+ arguments: call.arguments,
224
+ value: {
225
+ status: 500,
226
+ headers: {},
227
+ body:
228
+ error instanceof Error
229
+ ? {
230
+ ...error,
231
+ name: error.name,
232
+ message: error.message,
233
+ }
234
+ : error,
235
+ },
236
+ });
237
+ }
238
+ } else {
239
+ //----
240
+ // CLASS FUNCTION
241
+ //----
242
+ // VALIDATE FIRST
243
+ const check: IValidation<unknown> = call.operation.function.validate(
244
+ call.arguments,
245
+ );
246
+ if (check.success === false)
247
+ return (
248
+ (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)
249
+ ? await correct(ctx, call, retry, check.errors)
250
+ : null) ??
251
+ AgenticaPromptFactory.execute({
252
+ type: "execute",
253
+ protocol: "class",
254
+ controller: call.operation.controller,
255
+ function: call.operation.function,
256
+ id: call.id,
257
+ name: call.operation.name,
258
+ arguments: call.arguments,
259
+ value: {
260
+ name: "TypeGuardError",
261
+ message: "Invalid arguments.",
262
+ errors: check.errors,
263
+ },
264
+ })
265
+ );
266
+ // EXECUTE FUNCTION
267
+ try {
268
+ const value: any =
269
+ typeof call.operation.controller.execute === "function"
270
+ ? await call.operation.controller.execute({
271
+ application: call.operation.controller.application,
272
+ function: call.operation.function,
273
+ arguments: call.arguments,
274
+ })
275
+ : await (call.operation.controller.execute as any)[
276
+ call.operation.function.name
277
+ ](call.arguments);
278
+ return AgenticaPromptFactory.execute({
279
+ type: "execute",
280
+ protocol: "class",
281
+ controller: call.operation.controller,
282
+ function: call.operation.function,
283
+ id: call.id,
284
+ name: call.operation.name,
285
+ arguments: call.arguments,
286
+ value,
287
+ });
288
+ } catch (error) {
289
+ return AgenticaPromptFactory.execute({
290
+ type: "execute",
291
+ protocol: "class",
292
+ controller: call.operation.controller,
293
+ function: call.operation.function,
294
+ id: call.id,
295
+ name: call.operation.name,
296
+ arguments: call.arguments,
297
+ value:
298
+ error instanceof Error
299
+ ? {
300
+ ...error,
301
+ name: error.name,
302
+ message: error.message,
303
+ }
304
+ : error,
305
+ });
306
+ }
307
+ }
308
+ };
309
+
310
+ const correct = async (
311
+ ctx: IAgenticaContext,
312
+ call: IAgenticaEvent.ICall,
313
+ retry: number,
314
+ error: unknown,
315
+ ): Promise<IAgenticaPrompt.IExecute | null> => {
316
+ //----
317
+ // EXECUTE CHATGPT API
318
+ //----
319
+ const completion: OpenAI.ChatCompletion = await ctx.request("execute", {
320
+ messages: [
321
+ // COMMON SYSTEM PROMPT
322
+ {
323
+ role: "system",
324
+ content: AgenticaDefaultPrompt.write(ctx.config),
325
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
326
+ // PREVIOUS HISTORIES
327
+ ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
328
+ // USER INPUT
329
+ {
330
+ role: "user",
331
+ content: ctx.prompt.text,
332
+ },
333
+ // TYPE CORRECTION
334
+ {
335
+ role: "system",
336
+ content:
337
+ ctx.config?.systemPrompt?.execute?.(ctx.histories) ??
338
+ AgenticaSystemPrompt.EXECUTE,
339
+ },
340
+ {
341
+ role: "assistant",
342
+ tool_calls: [
343
+ {
344
+ type: "function",
345
+ id: call.id,
346
+ function: {
347
+ name: call.operation.name,
348
+ arguments: JSON.stringify(call.arguments),
349
+ },
350
+ } satisfies OpenAI.ChatCompletionMessageToolCall,
351
+ ],
352
+ } satisfies OpenAI.ChatCompletionAssistantMessageParam,
353
+ {
354
+ role: "tool",
355
+ content: typeof error === "string" ? error : JSON.stringify(error),
356
+ tool_call_id: call.id,
357
+ } satisfies OpenAI.ChatCompletionToolMessageParam,
358
+ {
359
+ role: "system",
360
+ content: [
361
+ "You A.I. assistant has composed wrong arguments.",
362
+ "",
363
+ "Correct it at the next function calling.",
364
+ ].join("\n"),
365
+ },
366
+ ],
367
+ // STACK FUNCTIONS
368
+ tools: [
369
+ {
370
+ type: "function",
371
+ function: {
372
+ name: call.operation.name,
373
+ description: call.operation.function.description,
374
+ parameters: (call.operation.function.separated
375
+ ? (call.operation.function.separated?.llm ??
376
+ ({
377
+ $defs: {},
378
+ type: "object",
379
+ properties: {},
380
+ additionalProperties: false,
381
+ required: [],
382
+ } satisfies IChatGptSchema.IParameters))
383
+ : call.operation.function.parameters) as any,
384
+ },
385
+ },
386
+ ],
387
+ tool_choice: "auto",
388
+ parallel_tool_calls: false,
389
+ });
390
+
391
+ //----
392
+ // PROCESS COMPLETION
393
+ //----
394
+ const toolCall: OpenAI.ChatCompletionMessageToolCall | undefined = (
395
+ completion.choices[0]?.message.tool_calls ?? []
396
+ ).find(
397
+ (tc) =>
398
+ tc.type === "function" && tc.function.name === call.operation.name,
399
+ );
400
+ if (toolCall === undefined) return null;
401
+ return propagate(
402
+ ctx,
403
+ {
404
+ id: toolCall.id,
405
+ type: "call",
406
+ operation: call.operation,
407
+ arguments: JSON.parse(toolCall.function.arguments),
408
+ },
409
+ retry,
410
+ );
411
+ };
412
+
413
+ const fillHttpArguments = (props: {
414
+ operation: IAgenticaOperation;
415
+ arguments: object;
416
+ }): void => {
417
+ if (props.operation.protocol !== "http") return;
418
+ const route: IHttpMigrateRoute = props.operation.function.route();
419
+ if (
420
+ route.body &&
421
+ route.operation().requestBody?.required === true &&
422
+ (props.arguments as any).body === undefined &&
423
+ isObject(
424
+ props.operation.function.parameters.$defs,
425
+ props.operation.function.parameters.properties.body!,
426
+ )
427
+ )
428
+ (props.arguments as any).body = {};
429
+ if (route.query && (props.arguments as any).query === undefined)
430
+ (props.arguments as any).query = {};
431
+ };
432
+
433
+ const isObject = (
434
+ $defs: Record<string, IChatGptSchema>,
435
+ schema: IChatGptSchema,
436
+ ): boolean => {
437
+ return (
438
+ ChatGptTypeChecker.isObject(schema) ||
439
+ (ChatGptTypeChecker.isReference(schema) &&
440
+ isObject($defs, $defs[schema.$ref.split("/").at(-1)!]!)) ||
441
+ (ChatGptTypeChecker.isAnyOf(schema) &&
442
+ schema.anyOf.every((schema) => isObject($defs, schema)))
443
+ );
444
+ };
445
+ }