@agentica/core 0.8.3 → 0.9.0-dev.20250302

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 (91) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +419 -404
  3. package/lib/Agentica.d.ts +10 -9
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/chatgpt/ChatGptAgent.d.ts +2 -1
  6. package/lib/chatgpt/ChatGptAgent.js.map +1 -1
  7. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +2 -1
  8. package/lib/chatgpt/ChatGptCallFunctionAgent.js +3 -1
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
  10. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +3 -2
  11. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +55 -2
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
  13. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +2 -1
  14. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
  15. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +2 -1
  16. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  17. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +2 -1
  18. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +63 -1
  19. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
  20. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +2 -1
  21. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +55 -2
  22. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  23. package/lib/functional/createHttpLlmApplication.js +826 -798
  24. package/lib/functional/createHttpLlmApplication.js.map +1 -1
  25. package/lib/index.mjs +987 -804
  26. package/lib/index.mjs.map +1 -1
  27. package/lib/internal/AgenticaDefaultPrompt.d.ts +2 -1
  28. package/lib/internal/AgenticaDefaultPrompt.js.map +1 -1
  29. package/lib/internal/AgenticaOperationComposer.d.ts +5 -4
  30. package/lib/internal/AgenticaOperationComposer.js +1 -1
  31. package/lib/internal/AgenticaOperationComposer.js.map +1 -1
  32. package/lib/internal/AgenticaPromptFactory.d.ts +3 -2
  33. package/lib/internal/AgenticaPromptFactory.js.map +1 -1
  34. package/lib/internal/AgenticaPromptTransformer.d.ts +5 -4
  35. package/lib/internal/AgenticaPromptTransformer.js.map +1 -1
  36. package/lib/structures/IAgenticaConfig.d.ts +4 -3
  37. package/lib/structures/IAgenticaContext.d.ts +7 -6
  38. package/lib/structures/IAgenticaController.d.ts +8 -8
  39. package/lib/structures/IAgenticaEvent.d.ts +19 -18
  40. package/lib/structures/IAgenticaExecutor.d.ts +7 -6
  41. package/lib/structures/IAgenticaOperation.d.ts +4 -4
  42. package/lib/structures/IAgenticaOperationCollection.d.ts +6 -5
  43. package/lib/structures/IAgenticaOperationSelection.d.ts +4 -4
  44. package/lib/structures/IAgenticaPrompt.d.ts +11 -11
  45. package/lib/structures/IAgenticaProps.d.ts +9 -4
  46. package/lib/structures/IAgenticaProvider.d.ts +22 -27
  47. package/lib/structures/IAgenticaSystemPrompt.d.ts +8 -7
  48. package/package.json +6 -9
  49. package/prompts/cancel.md +4 -4
  50. package/prompts/common.md +2 -2
  51. package/prompts/describe.md +6 -6
  52. package/prompts/execute.md +6 -6
  53. package/prompts/initialize.md +2 -2
  54. package/prompts/select.md +6 -6
  55. package/src/Agentica.ts +323 -318
  56. package/src/chatgpt/ChatGptAgent.ts +75 -71
  57. package/src/chatgpt/ChatGptCallFunctionAgent.ts +448 -445
  58. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +287 -283
  59. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +52 -51
  60. package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -87
  61. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -88
  62. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +319 -318
  63. package/src/functional/createHttpLlmApplication.ts +63 -63
  64. package/src/index.ts +19 -19
  65. package/src/internal/AgenticaConstant.ts +4 -4
  66. package/src/internal/AgenticaDefaultPrompt.ts +43 -39
  67. package/src/internal/AgenticaOperationComposer.ts +87 -82
  68. package/src/internal/AgenticaPromptFactory.ts +32 -30
  69. package/src/internal/AgenticaPromptTransformer.ts +86 -83
  70. package/src/internal/AgenticaTokenUsageAggregator.ts +115 -115
  71. package/src/internal/MathUtil.ts +3 -3
  72. package/src/internal/Singleton.ts +22 -22
  73. package/src/internal/__map_take.ts +15 -15
  74. package/src/structures/IAgenticaConfig.ts +123 -121
  75. package/src/structures/IAgenticaContext.ts +129 -128
  76. package/src/structures/IAgenticaController.ts +132 -130
  77. package/src/structures/IAgenticaEvent.ts +229 -224
  78. package/src/structures/IAgenticaExecutor.ts +156 -152
  79. package/src/structures/IAgenticaOperation.ts +64 -64
  80. package/src/structures/IAgenticaOperationCollection.ts +52 -50
  81. package/src/structures/IAgenticaOperationSelection.ts +69 -69
  82. package/src/structures/IAgenticaPrompt.ts +178 -173
  83. package/src/structures/IAgenticaProps.ts +70 -64
  84. package/src/structures/IAgenticaProvider.ts +39 -45
  85. package/src/structures/IAgenticaSystemPrompt.ts +124 -122
  86. package/src/structures/IAgenticaTokenUsage.ts +107 -107
  87. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -23
  88. package/src/structures/internal/__IChatFunctionReference.ts +21 -21
  89. package/src/structures/internal/__IChatInitialApplication.ts +15 -15
  90. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -24
  91. package/src/typings/AgenticaSource.ts +6 -6
@@ -1,283 +1,287 @@
1
- import { IHttpLlmFunction, ILlmApplication } from "@samchon/openapi";
2
- import OpenAI from "openai";
3
- import typia, { IValidation } from "typia";
4
- import { v4 } from "uuid";
5
-
6
- import { AgenticaConstant } from "../internal/AgenticaConstant";
7
- import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
8
- import { AgenticaPromptFactory } from "../internal/AgenticaPromptFactory";
9
- import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
10
- import { IAgenticaContext } from "../structures/IAgenticaContext";
11
- import { IAgenticaController } from "../structures/IAgenticaController";
12
- import { IAgenticaEvent } from "../structures/IAgenticaEvent";
13
- import { IAgenticaOperation } from "../structures/IAgenticaOperation";
14
- import { IAgenticaOperationSelection } from "../structures/IAgenticaOperationSelection";
15
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
16
- import { __IChatCancelFunctionsApplication } from "../structures/internal/__IChatCancelFunctionsApplication";
17
- import { __IChatFunctionReference } from "../structures/internal/__IChatFunctionReference";
18
- import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
19
-
20
- export namespace ChatGptCancelFunctionAgent {
21
- export const execute = async (
22
- ctx: IAgenticaContext,
23
- ): Promise<IAgenticaPrompt.ICancel[]> => {
24
- if (ctx.operations.divided === undefined)
25
- return step(ctx, ctx.operations.array, 0);
26
-
27
- const stacks: IAgenticaOperationSelection[][] = ctx.operations.divided.map(
28
- () => [],
29
- );
30
- const events: IAgenticaEvent[] = [];
31
- const prompts: IAgenticaPrompt.ICancel[][] = await Promise.all(
32
- ctx.operations.divided.map((operations, i) =>
33
- step(
34
- {
35
- ...ctx,
36
- stack: stacks[i]!,
37
- dispatch: async (e) => {
38
- events.push(e);
39
- },
40
- },
41
- operations,
42
- 0,
43
- ),
44
- ),
45
- );
46
-
47
- // NO FUNCTION SELECTION, SO THAT ONLY TEXT LEFT
48
- if (stacks.every((s) => s.length === 0)) return prompts[0]!;
49
- // ELITICISM
50
- else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true)
51
- return step(
52
- ctx,
53
- stacks
54
- .flat()
55
- .map(
56
- (s) =>
57
- ctx.operations.group
58
- .get(s.controller.name)!
59
- .get(s.function.name)!,
60
- ),
61
- 0,
62
- );
63
-
64
- // RE-COLLECT SELECT FUNCTION EVENTS
65
- const collection: IAgenticaPrompt.ICancel = {
66
- id: v4(),
67
- type: "cancel",
68
- operations: [],
69
- };
70
- for (const e of events)
71
- if (e.type === "select") {
72
- collection.operations.push(
73
- AgenticaPromptFactory.selection({
74
- protocol: e.operation.protocol as "http",
75
- controller: e.operation.controller as IAgenticaController.IHttp,
76
- function: e.operation.function as IHttpLlmFunction<"chatgpt">,
77
- reason: e.reason,
78
- name: e.operation.name,
79
- }),
80
- );
81
- await cancelFunction(ctx, {
82
- name: e.operation.name,
83
- reason: e.reason,
84
- });
85
- }
86
- return [collection];
87
- };
88
-
89
- export const cancelFunction = async (
90
- ctx: IAgenticaContext,
91
- reference: __IChatFunctionReference,
92
- ): Promise<IAgenticaOperationSelection | null> => {
93
- const index: number = ctx.stack.findIndex(
94
- (item) => item.name === reference.name,
95
- );
96
- if (index === -1) return null;
97
-
98
- const item: IAgenticaOperationSelection = ctx.stack[index]!;
99
- ctx.stack.splice(index, 1);
100
- await ctx.dispatch({
101
- type: "cancel",
102
- operation: item,
103
- reason: reference.reason,
104
- });
105
- return item;
106
- };
107
-
108
- const step = async (
109
- ctx: IAgenticaContext,
110
- operations: IAgenticaOperation[],
111
- retry: number,
112
- failures?: IFailure[],
113
- ): Promise<IAgenticaPrompt.ICancel[]> => {
114
- //----
115
- // EXECUTE CHATGPT API
116
- //----
117
- const completion: OpenAI.ChatCompletion = await ctx.request("cancel", {
118
- messages: [
119
- // COMMON SYSTEM PROMPT
120
- {
121
- role: "system",
122
- content: AgenticaDefaultPrompt.write(ctx.config),
123
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
124
- // CANDIDATE FUNCTIONS
125
- {
126
- role: "assistant",
127
- tool_calls: [
128
- {
129
- type: "function",
130
- id: "getApiFunctions",
131
- function: {
132
- name: "getApiFunctions",
133
- arguments: JSON.stringify({}),
134
- },
135
- },
136
- ],
137
- },
138
- {
139
- role: "tool",
140
- tool_call_id: "getApiFunctions",
141
- content: JSON.stringify(
142
- operations.map((op) => ({
143
- name: op.name,
144
- description: op.function.description,
145
- ...(op.protocol === "http"
146
- ? {
147
- method: op.function.method,
148
- path: op.function.path,
149
- tags: op.function.tags,
150
- }
151
- : {}),
152
- })),
153
- ),
154
- },
155
- // PREVIOUS HISTORIES
156
- ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
157
- // USER INPUT
158
- {
159
- role: "user",
160
- content: ctx.prompt.text,
161
- },
162
- // SYSTEM PROMPT
163
- {
164
- role: "system",
165
- content:
166
- ctx.config?.systemPrompt?.cancel?.(ctx.histories) ??
167
- AgenticaSystemPrompt.CANCEL,
168
- },
169
- // TYPE CORRECTIONS
170
- ...emendMessages(failures ?? []),
171
- ],
172
- // STACK FUNCTIONS
173
- tools: CONTAINER.functions.map(
174
- (func) =>
175
- ({
176
- type: "function",
177
- function: {
178
- name: func.name,
179
- description: func.description,
180
- parameters: func.parameters as any,
181
- },
182
- }) satisfies OpenAI.ChatCompletionTool,
183
- ),
184
- tool_choice: "auto",
185
- parallel_tool_calls: true,
186
- });
187
-
188
- //----
189
- // VALIDATION
190
- //----
191
- if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
192
- const failures: IFailure[] = [];
193
- for (const choice of completion.choices)
194
- for (const tc of choice.message.tool_calls ?? []) {
195
- if (tc.function.name !== "cancelFunctions") continue;
196
- const input: object = JSON.parse(tc.function.arguments);
197
- const validation: IValidation<__IChatFunctionReference.IProps> =
198
- typia.validate<__IChatFunctionReference.IProps>(input);
199
- if (validation.success === false)
200
- failures.push({
201
- id: tc.id,
202
- name: tc.function.name,
203
- validation,
204
- });
205
- }
206
- if (failures.length > 0) return step(ctx, operations, retry, failures);
207
- }
208
-
209
- //----
210
- // PROCESS COMPLETION
211
- //----
212
- const prompts: IAgenticaPrompt.ICancel[] = [];
213
- for (const choice of completion.choices) {
214
- // TOOL CALLING HANDLER
215
- if (choice.message.tool_calls)
216
- for (const tc of choice.message.tool_calls) {
217
- if (tc.type !== "function") continue;
218
- const input: __IChatFunctionReference.IProps = JSON.parse(
219
- tc.function.arguments,
220
- );
221
- if (typia.is(input) === false) continue;
222
- else if (tc.function.name === "cancelFunctions") {
223
- const collection: IAgenticaPrompt.ICancel = {
224
- id: tc.id,
225
- type: "cancel",
226
- operations: [],
227
- };
228
- for (const reference of input.functions) {
229
- const operation = await cancelFunction(ctx, reference);
230
- if (operation !== null) collection.operations.push(operation);
231
- }
232
- if (collection.operations.length !== 0) prompts.push(collection);
233
- }
234
- }
235
- }
236
- return prompts;
237
- };
238
-
239
- const emendMessages = (
240
- failures: IFailure[],
241
- ): OpenAI.ChatCompletionMessageParam[] =>
242
- failures
243
- .map((f) => [
244
- {
245
- role: "assistant",
246
- tool_calls: [
247
- {
248
- type: "function",
249
- id: f.id,
250
- function: {
251
- name: f.name,
252
- arguments: JSON.stringify(f.validation.data),
253
- },
254
- },
255
- ],
256
- } satisfies OpenAI.ChatCompletionAssistantMessageParam,
257
- {
258
- role: "tool",
259
- content: JSON.stringify(f.validation.errors),
260
- tool_call_id: f.id,
261
- } satisfies OpenAI.ChatCompletionToolMessageParam,
262
- {
263
- role: "system",
264
- content: [
265
- "You A.I. assistant has composed wrong typed arguments.",
266
- "",
267
- "Correct it at the next function calling.",
268
- ].join("\n"),
269
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
270
- ])
271
- .flat();
272
- }
273
-
274
- const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
275
- __IChatCancelFunctionsApplication,
276
- "chatgpt"
277
- >();
278
-
279
- interface IFailure {
280
- id: string;
281
- name: string;
282
- validation: IValidation.IFailure;
283
- }
1
+ import {
2
+ IHttpLlmFunction,
3
+ ILlmApplication,
4
+ ILlmSchema,
5
+ } from "@samchon/openapi";
6
+ import OpenAI from "openai";
7
+ import typia, { IValidation } from "typia";
8
+ import { v4 } from "uuid";
9
+
10
+ import { AgenticaConstant } from "../internal/AgenticaConstant";
11
+ import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
12
+ import { AgenticaPromptFactory } from "../internal/AgenticaPromptFactory";
13
+ import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
14
+ import { IAgenticaContext } from "../structures/IAgenticaContext";
15
+ import { IAgenticaController } from "../structures/IAgenticaController";
16
+ import { IAgenticaEvent } from "../structures/IAgenticaEvent";
17
+ import { IAgenticaOperation } from "../structures/IAgenticaOperation";
18
+ import { IAgenticaOperationSelection } from "../structures/IAgenticaOperationSelection";
19
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
20
+ import { __IChatCancelFunctionsApplication } from "../structures/internal/__IChatCancelFunctionsApplication";
21
+ import { __IChatFunctionReference } from "../structures/internal/__IChatFunctionReference";
22
+ import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
23
+
24
+ export namespace ChatGptCancelFunctionAgent {
25
+ export const execute = async <Model extends ILlmSchema.Model>(
26
+ ctx: IAgenticaContext<Model>,
27
+ ): Promise<IAgenticaPrompt.ICancel<Model>[]> => {
28
+ if (ctx.operations.divided === undefined)
29
+ return step(ctx, ctx.operations.array, 0);
30
+
31
+ const stacks: IAgenticaOperationSelection<Model>[][] =
32
+ ctx.operations.divided.map(() => []);
33
+ const events: IAgenticaEvent<Model>[] = [];
34
+ const prompts: IAgenticaPrompt.ICancel<Model>[][] = await Promise.all(
35
+ ctx.operations.divided.map((operations, i) =>
36
+ step(
37
+ {
38
+ ...ctx,
39
+ stack: stacks[i]!,
40
+ dispatch: async (e) => {
41
+ events.push(e);
42
+ },
43
+ },
44
+ operations,
45
+ 0,
46
+ ),
47
+ ),
48
+ );
49
+
50
+ // NO FUNCTION SELECTION, SO THAT ONLY TEXT LEFT
51
+ if (stacks.every((s) => s.length === 0)) return prompts[0]!;
52
+ // ELITICISM
53
+ else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true)
54
+ return step(
55
+ ctx,
56
+ stacks
57
+ .flat()
58
+ .map(
59
+ (s) =>
60
+ ctx.operations.group
61
+ .get(s.controller.name)!
62
+ .get(s.function.name)!,
63
+ ),
64
+ 0,
65
+ );
66
+
67
+ // RE-COLLECT SELECT FUNCTION EVENTS
68
+ const collection: IAgenticaPrompt.ICancel<Model> = {
69
+ id: v4(),
70
+ type: "cancel",
71
+ operations: [],
72
+ };
73
+ for (const e of events)
74
+ if (e.type === "select") {
75
+ collection.operations.push(
76
+ AgenticaPromptFactory.selection({
77
+ protocol: e.operation.protocol as "http",
78
+ controller: e.operation
79
+ .controller as IAgenticaController.IHttp<Model>,
80
+ function: e.operation.function as IHttpLlmFunction<Model>,
81
+ reason: e.reason,
82
+ name: e.operation.name,
83
+ }),
84
+ );
85
+ await cancelFunction(ctx, {
86
+ name: e.operation.name,
87
+ reason: e.reason,
88
+ });
89
+ }
90
+ return [collection];
91
+ };
92
+
93
+ export const cancelFunction = async <Model extends ILlmSchema.Model>(
94
+ ctx: IAgenticaContext<Model>,
95
+ reference: __IChatFunctionReference,
96
+ ): Promise<IAgenticaOperationSelection<Model> | null> => {
97
+ const index: number = ctx.stack.findIndex(
98
+ (item) => item.name === reference.name,
99
+ );
100
+ if (index === -1) return null;
101
+
102
+ const item: IAgenticaOperationSelection<Model> = ctx.stack[index]!;
103
+ ctx.stack.splice(index, 1);
104
+ await ctx.dispatch({
105
+ type: "cancel",
106
+ operation: item,
107
+ reason: reference.reason,
108
+ });
109
+ return item;
110
+ };
111
+
112
+ const step = async <Model extends ILlmSchema.Model>(
113
+ ctx: IAgenticaContext<Model>,
114
+ operations: IAgenticaOperation<Model>[],
115
+ retry: number,
116
+ failures?: IFailure[],
117
+ ): Promise<IAgenticaPrompt.ICancel<Model>[]> => {
118
+ //----
119
+ // EXECUTE CHATGPT API
120
+ //----
121
+ const completion: OpenAI.ChatCompletion = await ctx.request("cancel", {
122
+ messages: [
123
+ // COMMON SYSTEM PROMPT
124
+ {
125
+ role: "system",
126
+ content: AgenticaDefaultPrompt.write(ctx.config),
127
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
128
+ // CANDIDATE FUNCTIONS
129
+ {
130
+ role: "assistant",
131
+ tool_calls: [
132
+ {
133
+ type: "function",
134
+ id: "getApiFunctions",
135
+ function: {
136
+ name: "getApiFunctions",
137
+ arguments: JSON.stringify({}),
138
+ },
139
+ },
140
+ ],
141
+ },
142
+ {
143
+ role: "tool",
144
+ tool_call_id: "getApiFunctions",
145
+ content: JSON.stringify(
146
+ operations.map((op) => ({
147
+ name: op.name,
148
+ description: op.function.description,
149
+ ...(op.protocol === "http"
150
+ ? {
151
+ method: op.function.method,
152
+ path: op.function.path,
153
+ tags: op.function.tags,
154
+ }
155
+ : {}),
156
+ })),
157
+ ),
158
+ },
159
+ // PREVIOUS HISTORIES
160
+ ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
161
+ // USER INPUT
162
+ {
163
+ role: "user",
164
+ content: ctx.prompt.text,
165
+ },
166
+ // SYSTEM PROMPT
167
+ {
168
+ role: "system",
169
+ content:
170
+ ctx.config?.systemPrompt?.cancel?.(ctx.histories) ??
171
+ AgenticaSystemPrompt.CANCEL,
172
+ },
173
+ // TYPE CORRECTIONS
174
+ ...emendMessages(failures ?? []),
175
+ ],
176
+ // STACK FUNCTIONS
177
+ tools: CONTAINER.functions.map(
178
+ (func) =>
179
+ ({
180
+ type: "function",
181
+ function: {
182
+ name: func.name,
183
+ description: func.description,
184
+ parameters: func.parameters as any,
185
+ },
186
+ }) satisfies OpenAI.ChatCompletionTool,
187
+ ),
188
+ tool_choice: "auto",
189
+ parallel_tool_calls: true,
190
+ });
191
+
192
+ //----
193
+ // VALIDATION
194
+ //----
195
+ if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
196
+ const failures: IFailure[] = [];
197
+ for (const choice of completion.choices)
198
+ for (const tc of choice.message.tool_calls ?? []) {
199
+ if (tc.function.name !== "cancelFunctions") continue;
200
+ const input: object = JSON.parse(tc.function.arguments);
201
+ const validation: IValidation<__IChatFunctionReference.IProps> =
202
+ typia.validate<__IChatFunctionReference.IProps>(input);
203
+ if (validation.success === false)
204
+ failures.push({
205
+ id: tc.id,
206
+ name: tc.function.name,
207
+ validation,
208
+ });
209
+ }
210
+ if (failures.length > 0) return step(ctx, operations, retry, failures);
211
+ }
212
+
213
+ //----
214
+ // PROCESS COMPLETION
215
+ //----
216
+ const prompts: IAgenticaPrompt.ICancel<Model>[] = [];
217
+ for (const choice of completion.choices) {
218
+ // TOOL CALLING HANDLER
219
+ if (choice.message.tool_calls)
220
+ for (const tc of choice.message.tool_calls) {
221
+ if (tc.type !== "function") continue;
222
+ const input: __IChatFunctionReference.IProps = JSON.parse(
223
+ tc.function.arguments,
224
+ );
225
+ if (typia.is(input) === false) continue;
226
+ else if (tc.function.name === "cancelFunctions") {
227
+ const collection: IAgenticaPrompt.ICancel<Model> = {
228
+ id: tc.id,
229
+ type: "cancel",
230
+ operations: [],
231
+ };
232
+ for (const reference of input.functions) {
233
+ const operation = await cancelFunction(ctx, reference);
234
+ if (operation !== null) collection.operations.push(operation);
235
+ }
236
+ if (collection.operations.length !== 0) prompts.push(collection);
237
+ }
238
+ }
239
+ }
240
+ return prompts;
241
+ };
242
+
243
+ const emendMessages = (
244
+ failures: IFailure[],
245
+ ): OpenAI.ChatCompletionMessageParam[] =>
246
+ failures
247
+ .map((f) => [
248
+ {
249
+ role: "assistant",
250
+ tool_calls: [
251
+ {
252
+ type: "function",
253
+ id: f.id,
254
+ function: {
255
+ name: f.name,
256
+ arguments: JSON.stringify(f.validation.data),
257
+ },
258
+ },
259
+ ],
260
+ } satisfies OpenAI.ChatCompletionAssistantMessageParam,
261
+ {
262
+ role: "tool",
263
+ content: JSON.stringify(f.validation.errors),
264
+ tool_call_id: f.id,
265
+ } satisfies OpenAI.ChatCompletionToolMessageParam,
266
+ {
267
+ role: "system",
268
+ content: [
269
+ "You A.I. assistant has composed wrong typed arguments.",
270
+ "",
271
+ "Correct it at the next function calling.",
272
+ ].join("\n"),
273
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
274
+ ])
275
+ .flat();
276
+ }
277
+
278
+ const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
279
+ __IChatCancelFunctionsApplication,
280
+ "chatgpt"
281
+ >();
282
+
283
+ interface IFailure {
284
+ id: string;
285
+ name: string;
286
+ validation: IValidation.IFailure;
287
+ }