@agentica/core 0.43.3 → 0.44.0-dev.20260313-2

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 (205) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +218 -218
  3. package/lib/constants/AgenticaSystemPrompt.js +1 -1
  4. package/lib/constants/AgenticaSystemPrompt.js.map +1 -1
  5. package/lib/context/AgenticaOperation.d.ts +3 -2
  6. package/lib/context/internal/AgenticaOperationComposer.js.map +1 -1
  7. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -1
  8. package/lib/context/internal/__IChatInitialApplication.d.ts +1 -1
  9. package/lib/errors/AgenticaJsonParseError.d.ts +5 -8
  10. package/lib/errors/AgenticaJsonParseError.js +11 -6
  11. package/lib/errors/AgenticaJsonParseError.js.map +1 -1
  12. package/lib/errors/AgenticaValidationError.d.ts +1 -1
  13. package/lib/events/AgenticaExecuteEvent.d.ts +1 -1
  14. package/lib/events/AgenticaJsonParseErrorEvent.d.ts +2 -2
  15. package/lib/factory/events.d.ts +2 -3
  16. package/lib/factory/events.js +1 -2
  17. package/lib/factory/events.js.map +1 -1
  18. package/lib/functional/assertHttpController.d.ts +2 -3
  19. package/lib/functional/assertHttpController.js +3939 -2656
  20. package/lib/functional/assertHttpController.js.map +1 -1
  21. package/lib/functional/assertHttpLlmApplication.d.ts +3 -3
  22. package/lib/functional/assertHttpLlmApplication.js +3939 -2656
  23. package/lib/functional/assertHttpLlmApplication.js.map +1 -1
  24. package/lib/functional/assertMcpController.d.ts +2 -2
  25. package/lib/functional/assertMcpController.js +202 -629
  26. package/lib/functional/assertMcpController.js.map +1 -1
  27. package/lib/functional/createMcpLlmApplication.d.ts +6 -0
  28. package/lib/functional/createMcpLlmApplication.js +56 -0
  29. package/lib/functional/createMcpLlmApplication.js.map +1 -0
  30. package/lib/functional/validateHttpController.d.ts +3 -3
  31. package/lib/functional/validateHttpController.js +3367 -2268
  32. package/lib/functional/validateHttpController.js.map +1 -1
  33. package/lib/functional/validateHttpLlmApplication.d.ts +3 -4
  34. package/lib/functional/validateHttpLlmApplication.js +3367 -2268
  35. package/lib/functional/validateHttpLlmApplication.js.map +1 -1
  36. package/lib/functional/validateMcpController.d.ts +2 -2
  37. package/lib/functional/validateMcpController.js +388 -1161
  38. package/lib/functional/validateMcpController.js.map +1 -1
  39. package/lib/histories/AgenticaExecuteHistory.d.ts +1 -1
  40. package/lib/index.mjs +21256 -18946
  41. package/lib/index.mjs.map +1 -1
  42. package/lib/orchestrate/call.js +43 -56
  43. package/lib/orchestrate/call.js.map +1 -1
  44. package/lib/orchestrate/cancel.js +9 -66
  45. package/lib/orchestrate/cancel.js.map +1 -1
  46. package/lib/orchestrate/initialize.js +4 -947
  47. package/lib/orchestrate/initialize.js.map +1 -1
  48. package/lib/orchestrate/select.js +11 -68
  49. package/lib/orchestrate/select.js.map +1 -1
  50. package/lib/structures/IAgenticaController.d.ts +143 -151
  51. package/lib/structures/IMcpTool.d.ts +52 -0
  52. package/lib/structures/IMcpTool.js +3 -0
  53. package/lib/structures/IMcpTool.js.map +1 -0
  54. package/lib/utils/ChatGptCompletionMessageUtil.js +16 -5
  55. package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
  56. package/lib/utils/ChatGptCompletionMessageUtil.spec.js +0 -5
  57. package/lib/utils/ChatGptCompletionMessageUtil.spec.js.map +1 -1
  58. package/package.json +7 -9
  59. package/prompts/cancel.md +5 -5
  60. package/prompts/common.md +3 -3
  61. package/prompts/describe.md +7 -7
  62. package/prompts/execute.md +122 -122
  63. package/prompts/initialize.md +3 -3
  64. package/prompts/json_parse_error.md +35 -33
  65. package/prompts/select.md +7 -7
  66. package/prompts/validate.md +123 -123
  67. package/prompts/validate_repeated.md +31 -31
  68. package/src/Agentica.ts +367 -367
  69. package/src/MicroAgentica.ts +357 -357
  70. package/src/constants/AgenticaConstant.ts +4 -4
  71. package/src/constants/AgenticaDefaultPrompt.ts +44 -44
  72. package/src/constants/AgenticaSystemPrompt.ts +1 -1
  73. package/src/constants/index.ts +2 -2
  74. package/src/context/AgenticaContext.ts +136 -136
  75. package/src/context/AgenticaContextRequestResult.ts +14 -14
  76. package/src/context/AgenticaOperation.ts +73 -72
  77. package/src/context/AgenticaOperationCollection.ts +49 -49
  78. package/src/context/AgenticaOperationSelection.ts +9 -9
  79. package/src/context/AgenticaTokenUsage.ts +186 -186
  80. package/src/context/MicroAgenticaContext.ts +99 -99
  81. package/src/context/index.ts +5 -5
  82. package/src/context/internal/AgenticaOperationComposer.ts +177 -177
  83. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -66
  84. package/src/context/internal/__IChatCancelFunctionsApplication.ts +23 -23
  85. package/src/context/internal/__IChatFunctionReference.ts +21 -21
  86. package/src/context/internal/__IChatInitialApplication.ts +15 -15
  87. package/src/context/internal/__IChatSelectFunctionsApplication.ts +24 -24
  88. package/src/context/internal/isAgenticaContext.ts +11 -11
  89. package/src/errors/AgenticaJsonParseError.ts +52 -47
  90. package/src/errors/AgenticaValidationError.ts +49 -49
  91. package/src/errors/index.ts +2 -2
  92. package/src/events/AgenticaAssistantMessageEvent.ts +12 -12
  93. package/src/events/AgenticaCallEvent.ts +27 -27
  94. package/src/events/AgenticaCancelEvent.ts +9 -9
  95. package/src/events/AgenticaDescribeEvent.ts +14 -14
  96. package/src/events/AgenticaEvent.ts +59 -59
  97. package/src/events/AgenticaEvent.type.ts +19 -19
  98. package/src/events/AgenticaEventBase.ts +18 -18
  99. package/src/events/AgenticaEventSource.ts +6 -6
  100. package/src/events/AgenticaExecuteEvent.ts +45 -45
  101. package/src/events/AgenticaInitializeEvent.ts +7 -7
  102. package/src/events/AgenticaJsonParseErrorEvent.ts +16 -15
  103. package/src/events/AgenticaRequestEvent.ts +27 -27
  104. package/src/events/AgenticaResponseEvent.ts +32 -32
  105. package/src/events/AgenticaSelectEvent.ts +11 -11
  106. package/src/events/AgenticaUserMessageEvent.ts +12 -12
  107. package/src/events/AgenticaValidateEvent.ts +32 -32
  108. package/src/events/MicroAgenticaEvent.ts +45 -45
  109. package/src/events/index.ts +15 -15
  110. package/src/factory/events.ts +357 -359
  111. package/src/factory/histories.ts +348 -348
  112. package/src/factory/index.ts +3 -3
  113. package/src/factory/operations.ts +16 -16
  114. package/src/functional/assertHttpController.ts +106 -104
  115. package/src/functional/assertHttpLlmApplication.ts +52 -57
  116. package/src/functional/assertMcpController.ts +47 -44
  117. package/src/functional/createMcpLlmApplication.ts +72 -0
  118. package/src/functional/index.ts +7 -7
  119. package/src/functional/validateHttpController.ts +113 -110
  120. package/src/functional/validateHttpLlmApplication.ts +65 -70
  121. package/src/functional/validateMcpController.ts +53 -50
  122. package/src/histories/AgenticaAssistantMessageHistory.ts +10 -10
  123. package/src/histories/AgenticaCancelHistory.ts +8 -8
  124. package/src/histories/AgenticaDescribeHistory.ts +18 -18
  125. package/src/histories/AgenticaExecuteHistory.ts +64 -64
  126. package/src/histories/AgenticaHistory.ts +28 -28
  127. package/src/histories/AgenticaHistoryBase.ts +35 -35
  128. package/src/histories/AgenticaSelectHistory.ts +8 -8
  129. package/src/histories/AgenticaSystemMessageHistory.ts +10 -10
  130. package/src/histories/AgenticaUserMessageHistory.ts +11 -11
  131. package/src/histories/MicroAgenticaHistory.ts +19 -19
  132. package/src/histories/contents/AgenticaUserMessageAudioContent.ts +21 -21
  133. package/src/histories/contents/AgenticaUserMessageContent.ts +19 -19
  134. package/src/histories/contents/AgenticaUserMessageContentBase.ts +6 -6
  135. package/src/histories/contents/AgenticaUserMessageFileContent.ts +25 -25
  136. package/src/histories/contents/AgenticaUserMessageImageContent.ts +33 -33
  137. package/src/histories/contents/AgenticaUserMessageTextContent.ts +15 -15
  138. package/src/histories/contents/index.ts +5 -5
  139. package/src/histories/index.ts +10 -10
  140. package/src/index.ts +15 -15
  141. package/src/json/IAgenticaEventJson.ts +265 -265
  142. package/src/json/IAgenticaEventJson.type.ts +19 -19
  143. package/src/json/IAgenticaHistoryJson.ts +165 -165
  144. package/src/json/IAgenticaHistoryJson.type.ts +19 -19
  145. package/src/json/IAgenticaOperationJson.ts +36 -36
  146. package/src/json/IAgenticaOperationSelectionJson.ts +26 -26
  147. package/src/json/IAgenticaTokenUsageJson.ts +107 -107
  148. package/src/json/IMicroAgenticaEventJson.ts +22 -22
  149. package/src/json/IMicroAgenticaHistoryJson.ts +25 -25
  150. package/src/json/index.ts +7 -7
  151. package/src/orchestrate/call.ts +542 -558
  152. package/src/orchestrate/cancel.ts +265 -269
  153. package/src/orchestrate/describe.ts +66 -66
  154. package/src/orchestrate/execute.ts +61 -61
  155. package/src/orchestrate/index.ts +6 -6
  156. package/src/orchestrate/initialize.ts +102 -102
  157. package/src/orchestrate/internal/cancelFunctionFromContext.ts +33 -33
  158. package/src/orchestrate/internal/selectFunctionFromContext.ts +34 -34
  159. package/src/orchestrate/select.ts +320 -322
  160. package/src/structures/IAgenticaConfig.ts +83 -83
  161. package/src/structures/IAgenticaConfigBase.ts +87 -87
  162. package/src/structures/IAgenticaController.ts +143 -151
  163. package/src/structures/IAgenticaExecutor.ts +167 -167
  164. package/src/structures/IAgenticaProps.ts +78 -78
  165. package/src/structures/IAgenticaSystemPrompt.ts +236 -236
  166. package/src/structures/IAgenticaVendor.ts +54 -54
  167. package/src/structures/IMcpTool.ts +60 -0
  168. package/src/structures/IMicroAgenticaConfig.ts +56 -56
  169. package/src/structures/IMicroAgenticaExecutor.ts +67 -67
  170. package/src/structures/IMicroAgenticaProps.ts +77 -77
  171. package/src/structures/IMicroAgenticaSystemPrompt.ts +169 -169
  172. package/src/structures/index.ts +10 -10
  173. package/src/transformers/transformHistory.ts +172 -172
  174. package/src/utils/AssistantMessageEmptyError.ts +20 -20
  175. package/src/utils/AsyncQueue.spec.ts +355 -355
  176. package/src/utils/AsyncQueue.ts +95 -95
  177. package/src/utils/ByteArrayUtil.ts +5 -5
  178. package/src/utils/ChatGptCompletionMessageUtil.spec.ts +314 -320
  179. package/src/utils/ChatGptCompletionMessageUtil.ts +210 -195
  180. package/src/utils/ChatGptCompletionStreamingUtil.spec.ts +909 -909
  181. package/src/utils/ChatGptCompletionStreamingUtil.ts +91 -91
  182. package/src/utils/ChatGptTokenUsageAggregator.spec.ts +226 -226
  183. package/src/utils/ChatGptTokenUsageAggregator.ts +57 -57
  184. package/src/utils/MPSC.spec.ts +276 -276
  185. package/src/utils/MPSC.ts +42 -42
  186. package/src/utils/Singleton.spec.ts +138 -138
  187. package/src/utils/Singleton.ts +42 -42
  188. package/src/utils/StreamUtil.spec.ts +512 -512
  189. package/src/utils/StreamUtil.ts +87 -87
  190. package/src/utils/__map_take.spec.ts +140 -140
  191. package/src/utils/__map_take.ts +13 -13
  192. package/src/utils/__retry.spec.ts +198 -198
  193. package/src/utils/__retry.ts +18 -18
  194. package/src/utils/assertExecuteFailure.ts +16 -16
  195. package/src/utils/index.ts +4 -4
  196. package/src/utils/request.ts +140 -140
  197. package/src/utils/types.ts +50 -50
  198. package/lib/context/internal/AgenticaOperationComposer.spec.d.ts +0 -1
  199. package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -353
  200. package/lib/context/internal/AgenticaOperationComposer.spec.js.map +0 -1
  201. package/lib/utils/JsonUtil.d.ts +0 -8
  202. package/lib/utils/JsonUtil.js +0 -349
  203. package/lib/utils/JsonUtil.js.map +0 -1
  204. package/src/context/internal/AgenticaOperationComposer.spec.ts +0 -401
  205. package/src/utils/JsonUtil.ts +0 -460
@@ -1,322 +1,320 @@
1
- import type { ILlmApplication } from "@samchon/openapi";
2
- import type OpenAI from "openai";
3
- import type { IValidation } from "typia";
4
-
5
- import typia from "typia";
6
-
7
- import type { AgenticaContext } from "../context/AgenticaContext";
8
- import type { AgenticaOperation } from "../context/AgenticaOperation";
9
- import type { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
10
- import type { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
11
- import type { __IChatSelectFunctionsApplication } from "../context/internal/__IChatSelectFunctionsApplication";
12
- import type { AgenticaAssistantMessageEvent, AgenticaSelectEvent } from "../events";
13
- import type { AgenticaEvent } from "../events/AgenticaEvent";
14
-
15
- import { AgenticaConstant } from "../constants/AgenticaConstant";
16
- import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
17
- import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
18
- import { createAssistantMessageEvent } from "../factory/events";
19
- import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
20
- import { __get_retry } from "../utils/__retry";
21
- import { AssistantMessageEmptyError, AssistantMessageEmptyWithReasoningError } from "../utils/AssistantMessageEmptyError";
22
- import { reduceStreamingWithDispatch } from "../utils/ChatGptCompletionStreamingUtil";
23
- import { JsonUtil } from "../utils/JsonUtil";
24
- import { toAsyncGenerator } from "../utils/StreamUtil";
25
-
26
- import { selectFunctionFromContext } from "./internal/selectFunctionFromContext";
27
-
28
- const CONTAINER: ILlmApplication = typia.llm.application<
29
- __IChatSelectFunctionsApplication
30
- >();
31
-
32
- interface IFailure {
33
- id: string;
34
- name: string;
35
- validation: IValidation.IFailure;
36
- }
37
-
38
- export async function select(
39
- ctx: AgenticaContext,
40
- ): Promise<void> {
41
- if (ctx.operations.divided === undefined) {
42
- return step(ctx, ctx.operations.array, 0);
43
- }
44
-
45
- const stacks: AgenticaOperationSelection[][]
46
- = ctx.operations.divided.map(() => []);
47
- const events: AgenticaEvent[] = [];
48
- await Promise.all(
49
- ctx.operations.divided.map(async (operations, i) =>
50
- step(
51
- {
52
- ...ctx,
53
- stack: stacks[i]!,
54
- dispatch: async (e) => {
55
- events.push(e);
56
- },
57
- },
58
- operations,
59
- 0,
60
- ),
61
- ),
62
- );
63
-
64
- // ELITICISM
65
- if (
66
- (ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true
67
- && stacks.some(s => s.length !== 0)
68
- ) {
69
- return step(
70
- ctx,
71
- stacks
72
- .flat()
73
- .map(
74
- s =>
75
- ctx.operations.group
76
- .get(s.operation.controller.name)!
77
- .get(s.operation.function.name)!,
78
- ),
79
- 0,
80
- );
81
- }
82
- else {
83
- const selected: AgenticaSelectEvent[]
84
- = events.filter(e => e.type === "select");
85
- (selected.length !== 0 ? selected : events)
86
- .forEach((e) => {
87
- void ctx.dispatch(e).catch(() => {});
88
- });
89
- }
90
- }
91
-
92
- async function step(
93
- ctx: AgenticaContext,
94
- operations: AgenticaOperation[],
95
- retry: number,
96
- failures?: IFailure[],
97
- ): Promise<void> {
98
- const _retryFn = __get_retry(1);
99
- const retryFn = async (fn: (prevError?: unknown) => Promise<OpenAI.ChatCompletion>) => {
100
- return _retryFn(fn).catch((e) => {
101
- if (e instanceof AssistantMessageEmptyError) {
102
- return Symbol("emptyAssistantMessage");
103
- }
104
- throw e;
105
- });
106
- };
107
- // ----
108
- // EXECUTE CHATGPT API
109
- // ----
110
- const completion = await retryFn(async (prevError) => {
111
- const result = await ctx.request("select", {
112
- messages: [
113
-
114
- // CANDIDATE FUNCTIONS
115
- {
116
- role: "assistant",
117
- tool_calls: [
118
- {
119
- type: "function",
120
- id: "getApiFunctions",
121
- function: {
122
- name: "getApiFunctions",
123
- arguments: JSON.stringify({}),
124
- },
125
- },
126
- ],
127
- },
128
- {
129
- role: "tool",
130
- tool_call_id: "getApiFunctions",
131
- content: JSON.stringify(
132
- operations.map(op => ({
133
- name: op.name,
134
- description: op.function.description,
135
- ...(op.protocol === "http"
136
- ? {
137
- method: op.function.method,
138
- path: op.function.path,
139
- tags: op.function.tags,
140
- }
141
- : {}),
142
- })),
143
- ),
144
- },
145
- // PREVIOUS HISTORIES
146
- ...ctx.histories.map(decodeHistory).flat(),
147
- // USER INPUT
148
- {
149
- role: "user",
150
- content: ctx.prompt.contents.map(decodeUserMessageContent),
151
- },
152
- // PREVIOUS ERROR
153
- ...(prevError instanceof AssistantMessageEmptyWithReasoningError
154
- ? [
155
- {
156
- role: "assistant",
157
- content: prevError.reasoning,
158
- } satisfies OpenAI.ChatCompletionMessageParam,
159
- ]
160
- : []),
161
- // SYSTEM PROMPT
162
- {
163
- role: "system",
164
- content:
165
- ctx.config?.systemPrompt?.select?.(ctx.histories)
166
- ?? AgenticaSystemPrompt.SELECT,
167
- },
168
- // TYPE CORRECTIONS
169
- ...emendMessages(failures ?? []),
170
- // COMMON SYSTEM PROMPT
171
- {
172
- role: "system",
173
- content: AgenticaDefaultPrompt.write(ctx.config),
174
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
175
- ],
176
- // STACK FUNCTIONS
177
- tools: [{
178
- type: "function",
179
- function: {
180
- name: CONTAINER.functions[0]!.name,
181
- description: CONTAINER.functions[0]!.description,
182
- /**
183
- * @TODO fix it
184
- * The property and value have a type mismatch, but it works.
185
- */
186
- parameters: CONTAINER.functions[0]!.parameters as unknown as Record<string, unknown>,
187
- },
188
- } satisfies OpenAI.ChatCompletionTool],
189
- tool_choice: retry === 0
190
- ? "auto"
191
- : "required",
192
- // parallel_tool_calls: false,
193
- });
194
-
195
- if (result.type === "none-stream") {
196
- const completion = result.value;
197
- const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
198
- if (allAssistantMessagesEmpty) {
199
- const firstChoice = completion.choices?.at(0);
200
- if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
201
- throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
202
- }
203
- throw new AssistantMessageEmptyError();
204
- }
205
- return completion;
206
- }
207
-
208
- const completion = await reduceStreamingWithDispatch(result.value, (props) => {
209
- const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent(props);
210
- void ctx.dispatch(event).catch(() => {});
211
- }, ctx.abortSignal);
212
- const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
213
- if (allAssistantMessagesEmpty) {
214
- const firstChoice = completion.choices?.at(0);
215
- if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
216
- throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
217
- }
218
- throw new AssistantMessageEmptyError();
219
- }
220
- return completion;
221
- });
222
-
223
- if (typeof completion === "symbol") {
224
- const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent({
225
- stream: toAsyncGenerator(""),
226
- done: () => true,
227
- get: () => "",
228
- join: async () => {
229
- return "";
230
- },
231
- });
232
- void ctx.dispatch(event).catch(() => {});
233
- return;
234
- }
235
- // ----
236
- // VALIDATION
237
- // ----
238
- if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
239
- const failures: IFailure[] = [];
240
- for (const choice of (completion.choices ?? [])) {
241
- for (const tc of choice.message.tool_calls ?? []) {
242
- if (tc.type !== "function" || tc.function.name !== "selectFunctions") {
243
- continue;
244
- }
245
- const input: object = JsonUtil.parse(tc.function.arguments) as object;
246
- const validation: IValidation<__IChatFunctionReference.IProps>
247
- = typia.validate<__IChatFunctionReference.IProps>(input);
248
- if (validation.success === false) {
249
- failures.push({
250
- id: tc.id,
251
- name: tc.function.name,
252
- validation,
253
- });
254
- }
255
- }
256
- }
257
- if (failures.length > 0) {
258
- return step(ctx, operations, retry, failures);
259
- }
260
- }
261
-
262
- // ----
263
- // PROCESS COMPLETION
264
- // ----
265
- for (const choice of (completion.choices ?? [])) {
266
- // FUNCTION CALLING
267
- if (choice.message.tool_calls != null) {
268
- for (const tc of choice.message.tool_calls) {
269
- if (tc.type !== "function") {
270
- continue;
271
- }
272
- else if (tc.function.name !== "selectFunctions") {
273
- continue;
274
- }
275
-
276
- const input: __IChatFunctionReference.IProps | null
277
- = typia.json.isParse<__IChatFunctionReference.IProps>(
278
- tc.function.arguments,
279
- );
280
- if (input === null) {
281
- continue;
282
- }
283
- for (const reference of input.functions) {
284
- selectFunctionFromContext(ctx, reference);
285
- }
286
- }
287
- }
288
- }
289
- }
290
-
291
- function emendMessages(failures: IFailure[]): OpenAI.ChatCompletionMessageParam[] {
292
- return failures
293
- .map(f => [
294
- {
295
- role: "assistant",
296
- tool_calls: [
297
- {
298
- type: "function",
299
- id: f.id,
300
- function: {
301
- name: f.name,
302
- arguments: JSON.stringify(f.validation.data),
303
- },
304
- },
305
- ],
306
- } satisfies OpenAI.ChatCompletionAssistantMessageParam,
307
- {
308
- role: "tool",
309
- content: JSON.stringify(f.validation.errors),
310
- tool_call_id: f.id,
311
- } satisfies OpenAI.ChatCompletionToolMessageParam,
312
- {
313
- role: "system",
314
- content: [
315
- "You A.I. assistant has composed wrong typed arguments.",
316
- "",
317
- "Correct it at the next function calling.",
318
- ].join("\n"),
319
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
320
- ])
321
- .flat();
322
- }
1
+ import type OpenAI from "openai";
2
+ import type { ILlmFunction, IValidation } from "typia";
3
+
4
+ import typia from "typia";
5
+
6
+ import type { AgenticaContext } from "../context/AgenticaContext";
7
+ import type { AgenticaOperation } from "../context/AgenticaOperation";
8
+ import type { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
9
+ import type { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
10
+ import type { __IChatSelectFunctionsApplication } from "../context/internal/__IChatSelectFunctionsApplication";
11
+ import type { AgenticaAssistantMessageEvent, AgenticaSelectEvent } from "../events";
12
+ import type { AgenticaEvent } from "../events/AgenticaEvent";
13
+
14
+ import { AgenticaConstant } from "../constants/AgenticaConstant";
15
+ import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
16
+ import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
17
+ import { createAssistantMessageEvent } from "../factory/events";
18
+ import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
19
+ import { __get_retry } from "../utils/__retry";
20
+ import { AssistantMessageEmptyError, AssistantMessageEmptyWithReasoningError } from "../utils/AssistantMessageEmptyError";
21
+ import { reduceStreamingWithDispatch } from "../utils/ChatGptCompletionStreamingUtil";
22
+ import { toAsyncGenerator } from "../utils/StreamUtil";
23
+
24
+ import { selectFunctionFromContext } from "./internal/selectFunctionFromContext";
25
+
26
+ const FUNCTION: ILlmFunction = typia.llm.application<
27
+ __IChatSelectFunctionsApplication
28
+ >().functions[0]!;
29
+
30
+ interface IFailure {
31
+ id: string;
32
+ name: string;
33
+ validation: IValidation.IFailure;
34
+ }
35
+
36
+ export async function select(
37
+ ctx: AgenticaContext,
38
+ ): Promise<void> {
39
+ if (ctx.operations.divided === undefined) {
40
+ return step(ctx, ctx.operations.array, 0);
41
+ }
42
+
43
+ const stacks: AgenticaOperationSelection[][]
44
+ = ctx.operations.divided.map(() => []);
45
+ const events: AgenticaEvent[] = [];
46
+ await Promise.all(
47
+ ctx.operations.divided.map(async (operations, i) =>
48
+ step(
49
+ {
50
+ ...ctx,
51
+ stack: stacks[i]!,
52
+ dispatch: async (e) => {
53
+ events.push(e);
54
+ },
55
+ },
56
+ operations,
57
+ 0,
58
+ ),
59
+ ),
60
+ );
61
+
62
+ // ELITICISM
63
+ if (
64
+ (ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true
65
+ && stacks.some(s => s.length !== 0)
66
+ ) {
67
+ return step(
68
+ ctx,
69
+ stacks
70
+ .flat()
71
+ .map(
72
+ s =>
73
+ ctx.operations.group
74
+ .get(s.operation.controller.name)!
75
+ .get(s.operation.function.name)!,
76
+ ),
77
+ 0,
78
+ );
79
+ }
80
+ else {
81
+ const selected: AgenticaSelectEvent[]
82
+ = events.filter(e => e.type === "select");
83
+ (selected.length !== 0 ? selected : events)
84
+ .forEach((e) => {
85
+ void ctx.dispatch(e).catch(() => {});
86
+ });
87
+ }
88
+ }
89
+
90
+ async function step(
91
+ ctx: AgenticaContext,
92
+ operations: AgenticaOperation[],
93
+ retry: number,
94
+ failures?: IFailure[],
95
+ ): Promise<void> {
96
+ const _retryFn = __get_retry(1);
97
+ const retryFn = async (fn: (prevError?: unknown) => Promise<OpenAI.ChatCompletion>) => {
98
+ return _retryFn(fn).catch((e) => {
99
+ if (e instanceof AssistantMessageEmptyError) {
100
+ return Symbol("emptyAssistantMessage");
101
+ }
102
+ throw e;
103
+ });
104
+ };
105
+ // ----
106
+ // EXECUTE CHATGPT API
107
+ // ----
108
+ const completion = await retryFn(async (prevError) => {
109
+ const result = await ctx.request("select", {
110
+ messages: [
111
+
112
+ // CANDIDATE FUNCTIONS
113
+ {
114
+ role: "assistant",
115
+ tool_calls: [
116
+ {
117
+ type: "function",
118
+ id: "getApiFunctions",
119
+ function: {
120
+ name: "getApiFunctions",
121
+ arguments: JSON.stringify({}),
122
+ },
123
+ },
124
+ ],
125
+ },
126
+ {
127
+ role: "tool",
128
+ tool_call_id: "getApiFunctions",
129
+ content: JSON.stringify(
130
+ operations.map(op => ({
131
+ name: op.name,
132
+ description: op.function.description,
133
+ ...(op.protocol === "http"
134
+ ? {
135
+ method: op.function.method,
136
+ path: op.function.path,
137
+ tags: op.function.tags,
138
+ }
139
+ : {}),
140
+ })),
141
+ ),
142
+ },
143
+ // PREVIOUS HISTORIES
144
+ ...ctx.histories.map(decodeHistory).flat(),
145
+ // USER INPUT
146
+ {
147
+ role: "user",
148
+ content: ctx.prompt.contents.map(decodeUserMessageContent),
149
+ },
150
+ // PREVIOUS ERROR
151
+ ...(prevError instanceof AssistantMessageEmptyWithReasoningError
152
+ ? [
153
+ {
154
+ role: "assistant",
155
+ content: prevError.reasoning,
156
+ } satisfies OpenAI.ChatCompletionMessageParam,
157
+ ]
158
+ : []),
159
+ // SYSTEM PROMPT
160
+ {
161
+ role: "system",
162
+ content:
163
+ ctx.config?.systemPrompt?.select?.(ctx.histories)
164
+ ?? AgenticaSystemPrompt.SELECT,
165
+ },
166
+ // TYPE CORRECTIONS
167
+ ...emendMessages(failures ?? []),
168
+ // COMMON SYSTEM PROMPT
169
+ {
170
+ role: "system",
171
+ content: AgenticaDefaultPrompt.write(ctx.config),
172
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
173
+ ],
174
+ // STACK FUNCTIONS
175
+ tools: [{
176
+ type: "function",
177
+ function: {
178
+ name: FUNCTION.name,
179
+ description: FUNCTION.description,
180
+ /**
181
+ * @TODO fix it
182
+ * The property and value have a type mismatch, but it works.
183
+ */
184
+ parameters: FUNCTION.parameters as unknown as Record<string, unknown>,
185
+ },
186
+ } satisfies OpenAI.ChatCompletionTool],
187
+ tool_choice: retry === 0
188
+ ? "auto"
189
+ : "required",
190
+ // parallel_tool_calls: false,
191
+ });
192
+
193
+ if (result.type === "none-stream") {
194
+ const completion = result.value;
195
+ const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
196
+ if (allAssistantMessagesEmpty) {
197
+ const firstChoice = completion.choices?.[0];
198
+ if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
199
+ throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
200
+ }
201
+ throw new AssistantMessageEmptyError();
202
+ }
203
+ return completion;
204
+ }
205
+
206
+ const completion = await reduceStreamingWithDispatch(result.value, (props) => {
207
+ const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent(props);
208
+ void ctx.dispatch(event).catch(() => {});
209
+ }, ctx.abortSignal);
210
+ const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
211
+ if (allAssistantMessagesEmpty) {
212
+ const firstChoice = completion.choices?.[0];
213
+ if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
214
+ throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
215
+ }
216
+ throw new AssistantMessageEmptyError();
217
+ }
218
+ return completion;
219
+ });
220
+
221
+ if (typeof completion === "symbol") {
222
+ const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent({
223
+ stream: toAsyncGenerator(""),
224
+ done: () => true,
225
+ get: () => "",
226
+ join: async () => {
227
+ return "";
228
+ },
229
+ });
230
+ void ctx.dispatch(event).catch(() => {});
231
+ return;
232
+ }
233
+ // ----
234
+ // VALIDATION
235
+ // ----
236
+ if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
237
+ const failures: IFailure[] = [];
238
+ for (const choice of (completion.choices ?? [])) {
239
+ for (const tc of choice.message.tool_calls ?? []) {
240
+ if (tc.type !== "function" || tc.function.name !== "selectFunctions") {
241
+ continue;
242
+ }
243
+ const input: object = FUNCTION.parse(tc.function.arguments) as object;
244
+ const validation: IValidation<__IChatFunctionReference.IProps>
245
+ = FUNCTION.validate(input) as IValidation<__IChatFunctionReference.IProps>;
246
+ if (validation.success === false) {
247
+ failures.push({
248
+ id: tc.id,
249
+ name: tc.function.name,
250
+ validation,
251
+ });
252
+ }
253
+ }
254
+ }
255
+ if (failures.length > 0) {
256
+ return step(ctx, operations, retry, failures);
257
+ }
258
+ }
259
+
260
+ // ----
261
+ // PROCESS COMPLETION
262
+ // ----
263
+ for (const choice of (completion.choices ?? [])) {
264
+ // FUNCTION CALLING
265
+ if (choice.message.tool_calls != null) {
266
+ for (const tc of choice.message.tool_calls) {
267
+ if (tc.type !== "function") {
268
+ continue;
269
+ }
270
+ else if (tc.function.name !== "selectFunctions") {
271
+ continue;
272
+ }
273
+
274
+ const input: __IChatFunctionReference.IProps | null
275
+ = typia.json.isParse<__IChatFunctionReference.IProps>(
276
+ tc.function.arguments,
277
+ );
278
+ if (input === null) {
279
+ continue;
280
+ }
281
+ for (const reference of input.functions) {
282
+ selectFunctionFromContext(ctx, reference);
283
+ }
284
+ }
285
+ }
286
+ }
287
+ }
288
+
289
+ function emendMessages(failures: IFailure[]): OpenAI.ChatCompletionMessageParam[] {
290
+ return failures
291
+ .map(f => [
292
+ {
293
+ role: "assistant",
294
+ tool_calls: [
295
+ {
296
+ type: "function",
297
+ id: f.id,
298
+ function: {
299
+ name: f.name,
300
+ arguments: JSON.stringify(f.validation.data),
301
+ },
302
+ },
303
+ ],
304
+ } satisfies OpenAI.ChatCompletionAssistantMessageParam,
305
+ {
306
+ role: "tool",
307
+ content: JSON.stringify(f.validation.errors),
308
+ tool_call_id: f.id,
309
+ } satisfies OpenAI.ChatCompletionToolMessageParam,
310
+ {
311
+ role: "system",
312
+ content: [
313
+ "You A.I. assistant has composed wrong typed arguments.",
314
+ "",
315
+ "Correct it at the next function calling.",
316
+ ].join("\n"),
317
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
318
+ ])
319
+ .flat();
320
+ }