@agentica/core 0.12.21 → 0.13.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 (205) hide show
  1. package/README.md +50 -47
  2. package/lib/Agentica.d.ts +9 -9
  3. package/lib/Agentica.js +23 -16
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/chatgpt/ChatGptAgent.d.ts +7 -7
  6. package/lib/chatgpt/ChatGptAgent.js +17 -12
  7. package/lib/chatgpt/ChatGptAgent.js.map +1 -1
  8. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +7 -6
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.js +120 -81
  10. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
  11. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +9 -7
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +177 -145
  13. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
  14. package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +13 -8
  15. package/lib/chatgpt/ChatGptCompletionMessageUtil.js +499 -489
  16. package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -1
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +7 -6
  18. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +33 -32
  19. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
  20. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +7 -6
  21. package/lib/chatgpt/ChatGptHistoryDecoder.js +60 -56
  22. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +7 -6
  24. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +121 -116
  25. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +20 -5
  27. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +183 -150
  28. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  29. package/lib/chatgpt/ChatGptUsageAggregator.d.ts +9 -6
  30. package/lib/chatgpt/ChatGptUsageAggregator.js +46 -41
  31. package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -1
  32. package/lib/context/AgenticaCancelPrompt.d.ts +3 -3
  33. package/lib/context/AgenticaCancelPrompt.js +1 -1
  34. package/lib/context/AgenticaCancelPrompt.js.map +1 -1
  35. package/lib/context/AgenticaClassOperation.d.ts +1 -1
  36. package/lib/context/AgenticaClassOperation.js.map +1 -1
  37. package/lib/context/AgenticaContext.d.ts +9 -9
  38. package/lib/context/AgenticaHttpOperation.d.ts +1 -1
  39. package/lib/context/AgenticaHttpOperation.js.map +1 -1
  40. package/lib/context/AgenticaOperation.d.ts +4 -4
  41. package/lib/context/AgenticaOperationBase.d.ts +1 -1
  42. package/lib/context/AgenticaOperationCollection.d.ts +2 -2
  43. package/lib/context/AgenticaOperationSelection.d.ts +3 -3
  44. package/lib/context/AgenticaTokenUsage.d.ts +1 -1
  45. package/lib/context/AgenticaTokenUsage.js.map +1 -1
  46. package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +11 -10
  47. package/lib/context/internal/AgenticaTokenUsageAggregator.js +42 -43
  48. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -1
  49. package/lib/context/internal/__IChatCancelFunctionsApplication.d.ts +2 -2
  50. package/lib/context/internal/__IChatInitialApplication.d.ts +2 -2
  51. package/lib/context/internal/__IChatSelectFunctionsApplication.d.ts +2 -2
  52. package/lib/events/AgenticaCallEvent.d.ts +3 -3
  53. package/lib/events/AgenticaCancelEvent.d.ts +3 -3
  54. package/lib/events/AgenticaDescribeEvent.d.ts +3 -3
  55. package/lib/events/AgenticaDescribeEvent.js +13 -2
  56. package/lib/events/AgenticaDescribeEvent.js.map +1 -1
  57. package/lib/events/AgenticaEvent.d.ts +12 -12
  58. package/lib/events/AgenticaExecuteEvent.d.ts +7 -7
  59. package/lib/events/AgenticaInitializeEvent.d.ts +1 -1
  60. package/lib/events/AgenticaRequestEvent.d.ts +3 -3
  61. package/lib/events/AgenticaRequestEvent.js.map +1 -1
  62. package/lib/events/AgenticaResponseEvent.d.ts +2 -2
  63. package/lib/events/AgenticaResponseEvent.js.map +1 -1
  64. package/lib/events/AgenticaSelectEvent.d.ts +3 -3
  65. package/lib/events/AgenticaSelectEvent.js.map +1 -1
  66. package/lib/events/AgenticaTextEvent.d.ts +1 -1
  67. package/lib/events/AgenticaTextEvent.js +12 -1
  68. package/lib/events/AgenticaTextEvent.js.map +1 -1
  69. package/lib/functional/assertHttpLlmApplication.d.ts +4 -3
  70. package/lib/functional/assertHttpLlmApplication.js +9339 -9338
  71. package/lib/functional/assertHttpLlmApplication.js.map +1 -1
  72. package/lib/functional/validateHttpLlmApplication.d.ts +5 -4
  73. package/lib/functional/validateHttpLlmApplication.js +19 -19
  74. package/lib/functional/validateHttpLlmApplication.js.map +1 -1
  75. package/lib/index.d.ts +19 -19
  76. package/lib/index.js +21 -21
  77. package/lib/index.js.map +1 -1
  78. package/lib/index.mjs +19210 -18997
  79. package/lib/index.mjs.map +1 -1
  80. package/lib/internal/AgenticaDefaultPrompt.d.ts +6 -5
  81. package/lib/internal/AgenticaDefaultPrompt.js +39 -19
  82. package/lib/internal/AgenticaDefaultPrompt.js.map +1 -1
  83. package/lib/internal/AgenticaOperationComposer.d.ts +11 -10
  84. package/lib/internal/AgenticaOperationComposer.js +60 -59
  85. package/lib/internal/AgenticaOperationComposer.js.map +1 -1
  86. package/lib/internal/AgenticaSystemPrompt.d.ts +8 -8
  87. package/lib/internal/AgenticaSystemPrompt.js +9 -9
  88. package/lib/internal/AgenticaSystemPrompt.js.map +1 -1
  89. package/lib/internal/AsyncQueue.d.ts +15 -0
  90. package/lib/internal/AsyncQueue.js +82 -0
  91. package/lib/internal/AsyncQueue.js.map +1 -0
  92. package/lib/internal/ByteArrayUtil.d.ts +1 -1
  93. package/lib/internal/ByteArrayUtil.js +3 -2
  94. package/lib/internal/ByteArrayUtil.js.map +1 -1
  95. package/lib/internal/MPSC.d.ts +10 -0
  96. package/lib/internal/MPSC.js +49 -0
  97. package/lib/internal/MPSC.js.map +1 -0
  98. package/lib/internal/Singleton.js +6 -5
  99. package/lib/internal/Singleton.js.map +1 -1
  100. package/lib/internal/StreamUtil.d.ts +16 -6
  101. package/lib/internal/StreamUtil.js +43 -29
  102. package/lib/internal/StreamUtil.js.map +1 -1
  103. package/lib/internal/__map_take.js +5 -5
  104. package/lib/internal/__map_take.js.map +1 -1
  105. package/lib/json/IAgenticaEventJson.d.ts +7 -7
  106. package/lib/json/IAgenticaOperationSelectionJson.d.ts +1 -1
  107. package/lib/json/IAgenticaPromptJson.d.ts +2 -2
  108. package/lib/prompts/AgenticaCancelPrompt.d.ts +3 -3
  109. package/lib/prompts/AgenticaCancelPrompt.js +1 -1
  110. package/lib/prompts/AgenticaCancelPrompt.js.map +1 -1
  111. package/lib/prompts/AgenticaDescribePrompt.d.ts +3 -3
  112. package/lib/prompts/AgenticaDescribePrompt.js +1 -1
  113. package/lib/prompts/AgenticaDescribePrompt.js.map +1 -1
  114. package/lib/prompts/AgenticaExecutePrompt.d.ts +5 -5
  115. package/lib/prompts/AgenticaExecutePrompt.js.map +1 -1
  116. package/lib/prompts/AgenticaPrompt.d.ts +6 -6
  117. package/lib/prompts/AgenticaSelectPrompt.d.ts +3 -3
  118. package/lib/prompts/AgenticaSelectPrompt.js +1 -1
  119. package/lib/prompts/AgenticaSelectPrompt.js.map +1 -1
  120. package/lib/prompts/AgenticaTextPrompt.d.ts +1 -1
  121. package/lib/structures/IAgenticaConfig.d.ts +5 -5
  122. package/lib/structures/IAgenticaController.d.ts +1 -1
  123. package/lib/structures/IAgenticaExecutor.d.ts +4 -4
  124. package/lib/structures/IAgenticaProps.d.ts +5 -5
  125. package/lib/structures/IAgenticaSystemPrompt.d.ts +4 -4
  126. package/lib/structures/IAgenticaVendor.d.ts +1 -1
  127. package/lib/transformers/AgenticaEventTransformer.d.ts +55 -37
  128. package/lib/transformers/AgenticaEventTransformer.js +105 -58
  129. package/lib/transformers/AgenticaEventTransformer.js.map +1 -1
  130. package/lib/transformers/AgenticaPromptTransformer.d.ts +35 -29
  131. package/lib/transformers/AgenticaPromptTransformer.js +74 -45
  132. package/lib/transformers/AgenticaPromptTransformer.js.map +1 -1
  133. package/package.json +20 -23
  134. package/prompts/cancel.md +1 -1
  135. package/prompts/common.md +1 -1
  136. package/prompts/describe.md +1 -1
  137. package/prompts/execute.md +1 -1
  138. package/prompts/initialize.md +1 -1
  139. package/prompts/select.md +1 -1
  140. package/src/Agentica.ts +41 -31
  141. package/src/chatgpt/ChatGptAgent.ts +63 -57
  142. package/src/chatgpt/ChatGptCallFunctionAgent.ts +361 -337
  143. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +188 -164
  144. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +136 -137
  145. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +105 -101
  146. package/src/chatgpt/ChatGptHistoryDecoder.ts +69 -65
  147. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +128 -123
  148. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +215 -189
  149. package/src/chatgpt/ChatGptUsageAggregator.ts +49 -54
  150. package/src/context/AgenticaCancelPrompt.ts +4 -4
  151. package/src/context/AgenticaClassOperation.ts +5 -5
  152. package/src/context/AgenticaContext.ts +15 -15
  153. package/src/context/AgenticaHttpOperation.ts +5 -5
  154. package/src/context/AgenticaOperation.ts +4 -4
  155. package/src/context/AgenticaOperationBase.ts +1 -1
  156. package/src/context/AgenticaOperationCollection.ts +2 -2
  157. package/src/context/AgenticaOperationSelection.ts +3 -3
  158. package/src/context/AgenticaTokenUsage.ts +4 -3
  159. package/src/context/internal/AgenticaTokenUsageAggregator.ts +59 -59
  160. package/src/context/internal/__IChatCancelFunctionsApplication.ts +2 -2
  161. package/src/context/internal/__IChatInitialApplication.ts +2 -2
  162. package/src/context/internal/__IChatSelectFunctionsApplication.ts +2 -2
  163. package/src/events/AgenticaCallEvent.ts +3 -3
  164. package/src/events/AgenticaCancelEvent.ts +3 -3
  165. package/src/events/AgenticaDescribeEvent.ts +5 -5
  166. package/src/events/AgenticaEvent.ts +12 -12
  167. package/src/events/AgenticaExecuteEvent.ts +7 -7
  168. package/src/events/AgenticaInitializeEvent.ts +1 -1
  169. package/src/events/AgenticaRequestEvent.ts +3 -3
  170. package/src/events/AgenticaResponseEvent.ts +2 -2
  171. package/src/events/AgenticaSelectEvent.ts +4 -4
  172. package/src/events/AgenticaTextEvent.ts +2 -2
  173. package/src/functional/assertHttpLlmApplication.ts +9 -6
  174. package/src/functional/validateHttpLlmApplication.ts +14 -8
  175. package/src/index.ts +24 -24
  176. package/src/internal/AgenticaDefaultPrompt.ts +42 -28
  177. package/src/internal/AgenticaOperationComposer.ts +63 -61
  178. package/src/internal/AgenticaSystemPrompt.ts +15 -14
  179. package/src/internal/AsyncQueue.ts +67 -0
  180. package/src/internal/ByteArrayUtil.ts +2 -2
  181. package/src/internal/MPSC.ts +40 -0
  182. package/src/internal/Singleton.ts +8 -6
  183. package/src/internal/StreamUtil.ts +62 -53
  184. package/src/internal/__map_take.ts +5 -7
  185. package/src/json/IAgenticaEventJson.ts +7 -8
  186. package/src/json/IAgenticaOperationSelectionJson.ts +1 -1
  187. package/src/json/IAgenticaPromptJson.ts +2 -2
  188. package/src/prompts/AgenticaCancelPrompt.ts +4 -4
  189. package/src/prompts/AgenticaDescribePrompt.ts +4 -4
  190. package/src/prompts/AgenticaExecutePrompt.ts +6 -5
  191. package/src/prompts/AgenticaPrompt.ts +6 -6
  192. package/src/prompts/AgenticaSelectPrompt.ts +4 -4
  193. package/src/prompts/AgenticaTextPrompt.ts +1 -1
  194. package/src/structures/IAgenticaConfig.ts +5 -5
  195. package/src/structures/IAgenticaController.ts +14 -14
  196. package/src/structures/IAgenticaExecutor.ts +4 -4
  197. package/src/structures/IAgenticaProps.ts +5 -5
  198. package/src/structures/IAgenticaSystemPrompt.ts +4 -4
  199. package/src/structures/IAgenticaVendor.ts +1 -1
  200. package/src/transformers/AgenticaEventTransformer.ts +169 -136
  201. package/src/transformers/AgenticaPromptTransformer.ts +125 -103
  202. package/lib/internal/MPSCUtil.d.ts +0 -35
  203. package/lib/internal/MPSCUtil.js +0 -105
  204. package/lib/internal/MPSCUtil.js.map +0 -1
  205. package/src/internal/MPSCUtil.ts +0 -111
@@ -1,96 +1,105 @@
1
- import { ILlmApplication, ILlmSchema } from "@samchon/openapi";
2
- import OpenAI from "openai";
3
- import typia, { IValidation } from "typia";
4
- import { v4 } from "uuid";
1
+ import type { ILlmApplication, ILlmSchema } from "@samchon/openapi";
2
+ import type OpenAI from "openai";
3
+ import type { IValidation } from "typia";
4
+ import type { AgenticaContext } from "../context/AgenticaContext";
5
+ import type { AgenticaOperation } from "../context/AgenticaOperation";
5
6
 
6
- import { AgenticaContext } from "../context/AgenticaContext";
7
- import { AgenticaOperation } from "../context/AgenticaOperation";
7
+ import type { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
8
+ import type { __IChatSelectFunctionsApplication } from "../context/internal/__IChatSelectFunctionsApplication";
9
+ import type { AgenticaEvent } from "../events/AgenticaEvent";
10
+ import type { AgenticaPrompt } from "../prompts/AgenticaPrompt";
11
+ import typia from "typia";
12
+ import { v4 } from "uuid";
8
13
  import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
9
- import { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
10
- import { __IChatSelectFunctionsApplication } from "../context/internal/__IChatSelectFunctionsApplication";
11
- import { AgenticaEvent } from "../events/AgenticaEvent";
12
14
  import { AgenticaSelectEvent } from "../events/AgenticaSelectEvent";
13
15
  import { AgenticaTextEvent } from "../events/AgenticaTextEvent";
14
16
  import { AgenticaConstant } from "../internal/AgenticaConstant";
15
17
  import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
16
18
  import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
17
19
  import { StreamUtil } from "../internal/StreamUtil";
18
- import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
19
20
  import { AgenticaSelectPrompt } from "../prompts/AgenticaSelectPrompt";
20
21
  import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
21
22
  import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
22
23
  import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
23
24
 
24
- export namespace ChatGptSelectFunctionAgent {
25
- export const execute = async <Model extends ILlmSchema.Model>(
26
- ctx: AgenticaContext<Model>,
27
- ): Promise<AgenticaPrompt<Model>[]> => {
28
- if (ctx.operations.divided === undefined)
29
- return step(ctx, ctx.operations.array, 0);
25
+ const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
26
+ __IChatSelectFunctionsApplication,
27
+ "chatgpt"
28
+ >();
30
29
 
31
- const stacks: AgenticaOperationSelection<Model>[][] =
32
- ctx.operations.divided.map(() => []);
33
- const events: AgenticaEvent<Model>[] = [];
34
- const prompts: AgenticaPrompt<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
- },
30
+ interface IFailure {
31
+ id: string;
32
+ name: string;
33
+ validation: IValidation.IFailure;
34
+ }
35
+
36
+ export async function execute<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>): Promise<AgenticaPrompt<Model>[]> {
37
+ if (ctx.operations.divided === undefined) {
38
+ return step(ctx, ctx.operations.array, 0);
39
+ }
40
+
41
+ const stacks: AgenticaOperationSelection<Model>[][]
42
+ = ctx.operations.divided.map(() => []);
43
+ const events: AgenticaEvent<Model>[] = [];
44
+ const prompts: AgenticaPrompt<Model>[][] = await Promise.all(
45
+ ctx.operations.divided.map(async (operations, i) =>
46
+ step(
47
+ {
48
+ ...ctx,
49
+ stack: stacks[i]!,
50
+ dispatch: async (e) => {
51
+ events.push(e);
43
52
  },
44
- operations,
45
- 0,
46
- ),
53
+ },
54
+ operations,
55
+ 0,
47
56
  ),
48
- );
57
+ ),
58
+ );
49
59
 
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.operation.controller.name)!
62
- .get(s.operation.function.name)!,
63
- ),
64
- 0,
65
- );
60
+ // NO FUNCTION SELECTION, SO THAT ONLY TEXT LEFT
61
+ if (stacks.every(s => s.length === 0)) {
62
+ return prompts[0]!;
63
+ }
64
+ // ELITICISM
65
+ else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true) {
66
+ return step(
67
+ ctx,
68
+ stacks
69
+ .flat()
70
+ .map(
71
+ s =>
72
+ ctx.operations.group
73
+ .get(s.operation.controller.name)!
74
+ .get(s.operation.function.name)!,
75
+ ),
76
+ 0,
77
+ );
78
+ }
66
79
 
67
- // RE-COLLECT SELECT FUNCTION EVENTS
68
- const collection: AgenticaSelectPrompt<Model> = new AgenticaSelectPrompt({
69
- id: v4(),
70
- selections: [],
71
- });
72
- for (const e of events)
73
- if (e.type === "select") {
74
- collection.selections.push(e.selection);
75
- await selectFunction(ctx, {
76
- name: e.selection.operation.name,
77
- reason: e.selection.reason,
78
- });
79
- }
80
- return [collection];
81
- };
80
+ // RE-COLLECT SELECT FUNCTION EVENTS
81
+ const collection: AgenticaSelectPrompt<Model> = new AgenticaSelectPrompt({
82
+ id: v4(),
83
+ selections: [],
84
+ });
85
+ for (const e of events) {
86
+ if (e.type === "select") {
87
+ collection.selections.push(e.selection);
88
+ await selectFunction(ctx, {
89
+ name: e.selection.operation.name,
90
+ reason: e.selection.reason,
91
+ });
92
+ }
93
+ }
94
+ return [collection];
95
+ }
82
96
 
83
- const step = async <Model extends ILlmSchema.Model>(
84
- ctx: AgenticaContext<Model>,
85
- operations: AgenticaOperation<Model>[],
86
- retry: number,
87
- failures?: IFailure[],
88
- ): Promise<AgenticaPrompt<Model>[]> => {
89
- //----
90
- // EXECUTE CHATGPT API
91
- //----
92
- const completionStream = await ctx.request("select", {
93
- messages: [
97
+ async function step<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>, operations: AgenticaOperation<Model>[], retry: number, failures?: IFailure[]): Promise<AgenticaPrompt<Model>[]> {
98
+ // ----
99
+ // EXECUTE CHATGPT API
100
+ // ----
101
+ const completionStream = await ctx.request("select", {
102
+ messages: [
94
103
  // COMMON SYSTEM PROMPT
95
104
  {
96
105
  role: "system",
@@ -114,7 +123,7 @@ export namespace ChatGptSelectFunctionAgent {
114
123
  role: "tool",
115
124
  tool_call_id: "getApiFunctions",
116
125
  content: JSON.stringify(
117
- operations.map((op) => ({
126
+ operations.map(op => ({
118
127
  name: op.name,
119
128
  description: op.function.description,
120
129
  ...(op.protocol === "http"
@@ -138,137 +147,159 @@ export namespace ChatGptSelectFunctionAgent {
138
147
  {
139
148
  role: "system",
140
149
  content:
141
- ctx.config?.systemPrompt?.select?.(ctx.histories) ??
142
- AgenticaSystemPrompt.SELECT,
150
+ ctx.config?.systemPrompt?.select?.(ctx.histories)
151
+ ?? AgenticaSystemPrompt.SELECT,
143
152
  },
144
153
  // TYPE CORRECTIONS
145
154
  ...emendMessages(failures ?? []),
146
- ],
147
- // STACK FUNCTIONS
148
- tools: CONTAINER.functions.map(
149
- (func) =>
155
+ ],
156
+ // STACK FUNCTIONS
157
+ tools: CONTAINER.functions.map(
158
+ func =>
150
159
  ({
151
160
  type: "function",
152
161
  function: {
153
162
  name: func.name,
154
163
  description: func.description,
155
- parameters: func.parameters as any,
164
+ /**
165
+ * @TODO fix it
166
+ * The property and value have a type mismatch, but it works.
167
+ */
168
+ parameters: func.parameters as unknown as Record<string, unknown>,
156
169
  },
157
170
  }) satisfies OpenAI.ChatCompletionTool,
158
- ),
159
- tool_choice: "auto",
160
- parallel_tool_calls: false,
161
- });
171
+ ),
172
+ tool_choice: "auto",
173
+ parallel_tool_calls: false,
174
+ });
162
175
 
163
- const chunks = await StreamUtil.readAll(completionStream);
164
- const completion = ChatGptCompletionMessageUtil.merge(chunks);
165
- //----
166
- // VALIDATION
167
- //----
168
- if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
169
- const failures: IFailure[] = [];
170
- for (const choice of completion.choices)
171
- for (const tc of choice.message.tool_calls ?? []) {
172
- if (tc.function.name !== "selectFunctions") continue;
173
- const input: object = JSON.parse(tc.function.arguments);
174
- const validation: IValidation<__IChatFunctionReference.IProps> =
175
- typia.validate<__IChatFunctionReference.IProps>(input);
176
- if (validation.success === false)
177
- failures.push({
178
- id: tc.id,
179
- name: tc.function.name,
180
- validation,
181
- });
176
+ const chunks = await StreamUtil.readAll(completionStream);
177
+ const completion = ChatGptCompletionMessageUtil.merge(chunks);
178
+ // ----
179
+ // VALIDATION
180
+ // ----
181
+ if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
182
+ const failures: IFailure[] = [];
183
+ for (const choice of completion.choices) {
184
+ for (const tc of choice.message.tool_calls ?? []) {
185
+ if (tc.function.name !== "selectFunctions") {
186
+ continue;
187
+ }
188
+ const input = JSON.parse(tc.function.arguments) as object;
189
+ const validation: IValidation<__IChatFunctionReference.IProps>
190
+ = typia.validate<__IChatFunctionReference.IProps>(input);
191
+ if (validation.success === false) {
192
+ failures.push({
193
+ id: tc.id,
194
+ name: tc.function.name,
195
+ validation,
196
+ });
182
197
  }
183
- if (failures.length > 0) return step(ctx, operations, retry, failures);
198
+ }
199
+ }
200
+ if (failures.length > 0) {
201
+ return step(ctx, operations, retry, failures);
184
202
  }
203
+ }
185
204
 
186
- //----
187
- // PROCESS COMPLETION
188
- //----
189
- const prompts: AgenticaPrompt<Model>[] = [];
190
- for (const choice of completion.choices) {
191
- // TOOL CALLING HANDLER
192
- if (choice.message.tool_calls)
193
- for (const tc of choice.message.tool_calls) {
194
- if (tc.type !== "function") continue;
205
+ // ----
206
+ // PROCESS COMPLETION
207
+ // ----
208
+ const prompts: AgenticaPrompt<Model>[] = [];
209
+ for (const choice of completion.choices) {
210
+ // TOOL CALLING HANDLER
211
+ if (choice.message.tool_calls != null) {
212
+ for (const tc of choice.message.tool_calls) {
213
+ if (tc.type !== "function") {
214
+ continue;
215
+ }
195
216
 
196
- const input: __IChatFunctionReference.IProps = JSON.parse(
197
- tc.function.arguments,
198
- );
199
- if (typia.is(input) === false) continue;
200
- else if (tc.function.name === "selectFunctions") {
201
- const collection: AgenticaSelectPrompt<Model> =
202
- new AgenticaSelectPrompt({
217
+ if (tc.function.name !== "selectFunctions") {
218
+ continue;
219
+ }
220
+ const input = typia.json.isParse<__IChatFunctionReference.IProps>(tc.function.arguments);
221
+
222
+ if (input === null) {
223
+ continue;
224
+ }
225
+
226
+ const collection: AgenticaSelectPrompt<Model>
227
+ = new AgenticaSelectPrompt({
203
228
  id: tc.id,
204
229
  selections: [],
205
230
  });
206
- for (const reference of input.functions) {
207
- const operation: AgenticaOperation<Model> | null =
208
- await selectFunction(ctx, reference);
209
- if (operation !== null)
210
- collection.selections.push(
211
- new AgenticaOperationSelection({
212
- operation,
213
- reason: reference.reason,
214
- }),
215
- );
216
- }
217
- if (collection.selections.length !== 0) prompts.push(collection);
231
+ for (const reference of input.functions) {
232
+ const operation: AgenticaOperation<Model> | null
233
+ = await selectFunction(ctx, reference);
234
+
235
+ if (operation === null) {
236
+ continue;
218
237
  }
238
+
239
+ collection.selections.push(
240
+ new AgenticaOperationSelection({
241
+ operation,
242
+ reason: reference.reason,
243
+ }),
244
+ );
219
245
  }
220
246
 
221
- // ASSISTANT MESSAGE
222
- if (
223
- choice.message.role === "assistant" &&
224
- !!choice.message.content?.length
225
- ) {
226
- const text: AgenticaTextPrompt = new AgenticaTextPrompt({
227
- role: "assistant",
228
- text: choice.message.content,
229
- });
230
- prompts.push(text);
231
- await ctx.dispatch(
232
- new AgenticaTextEvent({
233
- role: "assistant",
234
- stream: StreamUtil.to(text.text),
235
- join: () => Promise.resolve(text.text),
236
- done: () => true,
237
- get: () => text.text,
238
- }),
239
- );
247
+ if (collection.selections.length !== 0) {
248
+ prompts.push(collection);
249
+ }
240
250
  }
241
251
  }
242
- return prompts;
243
- };
244
252
 
245
- const selectFunction = async <Model extends ILlmSchema.Model>(
246
- ctx: AgenticaContext<Model>,
247
- reference: __IChatFunctionReference,
248
- ): Promise<AgenticaOperation<Model> | null> => {
249
- const operation: AgenticaOperation<Model> | undefined =
250
- ctx.operations.flat.get(reference.name);
251
- if (operation === undefined) return null;
253
+ // ASSISTANT MESSAGE
254
+ if (
255
+ choice.message.role === "assistant"
256
+ && choice.message.content != null
257
+ ) {
258
+ const text: AgenticaTextPrompt = new AgenticaTextPrompt({
259
+ role: "assistant",
260
+ text: choice.message.content,
261
+ });
262
+ prompts.push(text);
263
+
264
+ await ctx.dispatch(
265
+ new AgenticaTextEvent({
266
+ role: "assistant",
267
+ stream: StreamUtil.to(text.text),
268
+ join: async () => Promise.resolve(text.text),
269
+ done: () => true,
270
+ get: () => text.text,
271
+ }),
272
+ );
273
+ }
274
+ }
275
+
276
+ return prompts;
277
+ }
278
+
279
+ async function selectFunction<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>, reference: __IChatFunctionReference): Promise<AgenticaOperation<Model> | null> {
280
+ const operation: AgenticaOperation<Model> | undefined
281
+ = ctx.operations.flat.get(reference.name);
282
+ if (operation === undefined) {
283
+ return null;
284
+ }
252
285
 
253
- const selection: AgenticaOperationSelection<Model> =
254
- new AgenticaOperationSelection({
286
+ const selection: AgenticaOperationSelection<Model>
287
+ = new AgenticaOperationSelection({
255
288
  operation,
256
289
  reason: reference.reason,
257
290
  });
258
- ctx.stack.push(selection);
259
- void ctx.dispatch(
260
- new AgenticaSelectEvent({
261
- selection,
262
- }),
263
- );
264
- return operation;
265
- };
291
+ ctx.stack.push(selection);
292
+ void ctx.dispatch(
293
+ new AgenticaSelectEvent({
294
+ selection,
295
+ }),
296
+ );
297
+ return operation;
298
+ }
266
299
 
267
- const emendMessages = (
268
- failures: IFailure[],
269
- ): OpenAI.ChatCompletionMessageParam[] =>
270
- failures
271
- .map((f) => [
300
+ function emendMessages(failures: IFailure[]): OpenAI.ChatCompletionMessageParam[] {
301
+ return failures
302
+ .map(f => [
272
303
  {
273
304
  role: "assistant",
274
305
  tool_calls: [
@@ -295,17 +326,12 @@ export namespace ChatGptSelectFunctionAgent {
295
326
  "Correct it at the next function calling.",
296
327
  ].join("\n"),
297
328
  } satisfies OpenAI.ChatCompletionSystemMessageParam,
298
- ])
299
- .flat();
329
+ ])
330
+ .flat();
300
331
  }
301
332
 
302
- const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
303
- __IChatSelectFunctionsApplication,
304
- "chatgpt"
305
- >();
306
-
307
- interface IFailure {
308
- id: string;
309
- name: string;
310
- validation: IValidation.IFailure;
311
- }
333
+ export const ChatGptSelectFunctionAgent = {
334
+ execute,
335
+ selectFunction,
336
+ emendMessages,
337
+ };
@@ -1,62 +1,57 @@
1
- import { CompletionUsage } from "openai/resources";
1
+ import type { CompletionUsage } from "openai/resources";
2
2
 
3
- export namespace ChatGptUsageAggregator {
4
- export const sumCompletionTokenDetail = (
5
- x: CompletionUsage.CompletionTokensDetails,
6
- y: CompletionUsage.CompletionTokensDetails,
7
- ): CompletionUsage.CompletionTokensDetails => {
8
- return {
9
- accepted_prediction_tokens:
10
- (x.accepted_prediction_tokens ?? 0) +
11
- (y.accepted_prediction_tokens ?? 0),
12
- reasoning_tokens: (x.reasoning_tokens ?? 0) + (y.reasoning_tokens ?? 0),
13
- rejected_prediction_tokens:
14
- (x.rejected_prediction_tokens ?? 0) +
15
- (y.rejected_prediction_tokens ?? 0),
16
- };
3
+ export function sumCompletionTokenDetail(x: CompletionUsage.CompletionTokensDetails, y: CompletionUsage.CompletionTokensDetails): CompletionUsage.CompletionTokensDetails {
4
+ return {
5
+ accepted_prediction_tokens:
6
+ (x.accepted_prediction_tokens ?? 0)
7
+ + (y.accepted_prediction_tokens ?? 0),
8
+ reasoning_tokens: (x.reasoning_tokens ?? 0) + (y.reasoning_tokens ?? 0),
9
+ rejected_prediction_tokens:
10
+ (x.rejected_prediction_tokens ?? 0)
11
+ + (y.rejected_prediction_tokens ?? 0),
17
12
  };
13
+ }
18
14
 
19
- export const sumPromptTokenDetail = (
20
- x: CompletionUsage.PromptTokensDetails,
21
- y: CompletionUsage.PromptTokensDetails,
22
- ): CompletionUsage.PromptTokensDetails => {
23
- return {
24
- audio_tokens: (x.audio_tokens ?? 0) + (y.audio_tokens ?? 0),
25
- cached_tokens: (x.cached_tokens ?? 0) + (y.cached_tokens ?? 0),
26
- };
15
+ export function sumPromptTokenDetail(x: CompletionUsage.PromptTokensDetails, y: CompletionUsage.PromptTokensDetails): CompletionUsage.PromptTokensDetails {
16
+ return {
17
+ audio_tokens: (x.audio_tokens ?? 0) + (y.audio_tokens ?? 0),
18
+ cached_tokens: (x.cached_tokens ?? 0) + (y.cached_tokens ?? 0),
27
19
  };
20
+ }
28
21
 
29
- export const sum = (
30
- x: CompletionUsage,
31
- y: CompletionUsage,
32
- ): CompletionUsage => {
33
- return {
34
- prompt_tokens: (x.prompt_tokens ?? 0) + (y.prompt_tokens ?? 0),
35
- completion_tokens:
22
+ export function sum(x: CompletionUsage, y: CompletionUsage): CompletionUsage {
23
+ return {
24
+ prompt_tokens: (x.prompt_tokens ?? 0) + (y.prompt_tokens ?? 0),
25
+ completion_tokens:
36
26
  (x.completion_tokens ?? 0) + (y.completion_tokens ?? 0),
37
- total_tokens: (x.total_tokens ?? 0) + (y.total_tokens ?? 0),
38
- completion_tokens_details: sumCompletionTokenDetail(
39
- x.completion_tokens_details ?? {
40
- accepted_prediction_tokens: 0,
41
- reasoning_tokens: 0,
42
- rejected_prediction_tokens: 0,
43
- },
44
- y.completion_tokens_details ?? {
45
- accepted_prediction_tokens: 0,
46
- reasoning_tokens: 0,
47
- rejected_prediction_tokens: 0,
48
- },
49
- ),
50
- prompt_tokens_details: sumPromptTokenDetail(
51
- x.prompt_tokens_details ?? {
52
- audio_tokens: 0,
53
- cached_tokens: 0,
54
- },
55
- y.prompt_tokens_details ?? {
56
- audio_tokens: 0,
57
- cached_tokens: 0,
58
- },
59
- ),
60
- };
27
+ total_tokens: (x.total_tokens ?? 0) + (y.total_tokens ?? 0),
28
+ completion_tokens_details: sumCompletionTokenDetail(
29
+ x.completion_tokens_details ?? {
30
+ accepted_prediction_tokens: 0,
31
+ reasoning_tokens: 0,
32
+ rejected_prediction_tokens: 0,
33
+ },
34
+ y.completion_tokens_details ?? {
35
+ accepted_prediction_tokens: 0,
36
+ reasoning_tokens: 0,
37
+ rejected_prediction_tokens: 0,
38
+ },
39
+ ),
40
+ prompt_tokens_details: sumPromptTokenDetail(
41
+ x.prompt_tokens_details ?? {
42
+ audio_tokens: 0,
43
+ cached_tokens: 0,
44
+ },
45
+ y.prompt_tokens_details ?? {
46
+ audio_tokens: 0,
47
+ cached_tokens: 0,
48
+ },
49
+ ),
61
50
  };
62
51
  }
52
+
53
+ export const ChatGptUsageAggregator = {
54
+ sum,
55
+ sumCompletionTokenDetail,
56
+ sumPromptTokenDetail,
57
+ };
@@ -1,8 +1,8 @@
1
- import { ILlmSchema } from "@samchon/openapi";
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
3
+ import type { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
4
+ import type { AgenticaOperationSelection } from "./AgenticaOperationSelection";
4
5
  import { AgenticaPromptBase } from "../prompts/AgenticaPromptBase";
5
- import { AgenticaOperationSelection } from "./AgenticaOperationSelection";
6
6
 
7
7
  export class AgenticaCancelPrompt<
8
8
  Model extends ILlmSchema.Model,
@@ -20,7 +20,7 @@ export class AgenticaCancelPrompt<
20
20
  return {
21
21
  type: this.type,
22
22
  id: this.id,
23
- selections: this.selections.map((s) => s.toJSON()),
23
+ selections: this.selections.map(s => s.toJSON()),
24
24
  };
25
25
  }
26
26
  }
@@ -1,14 +1,14 @@
1
- import { ILlmApplication, ILlmFunction, ILlmSchema } from "@samchon/openapi";
1
+ import type { ILlmApplication, ILlmFunction, ILlmSchema } from "@samchon/openapi";
2
2
 
3
3
  import { AgenticaOperationBase } from "./AgenticaOperationBase";
4
4
 
5
5
  export class AgenticaClassOperation<
6
6
  Model extends ILlmSchema.Model,
7
7
  > extends AgenticaOperationBase<
8
- "class",
9
- ILlmApplication<Model>,
10
- ILlmFunction<Model>
11
- > {
8
+ "class",
9
+ ILlmApplication<Model>,
10
+ ILlmFunction<Model>
11
+ > {
12
12
  public constructor(props: AgenticaClassOperation.IProps<Model>) {
13
13
  super(props);
14
14
  }