@agentica/core 0.10.4 → 0.11.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 (263) hide show
  1. package/lib/Agentica.d.ts +14 -14
  2. package/lib/Agentica.js +54 -31
  3. package/lib/Agentica.js.map +1 -1
  4. package/lib/chatgpt/ChatGptAgent.d.ts +3 -3
  5. package/lib/chatgpt/ChatGptAgent.js +1 -1
  6. package/lib/chatgpt/ChatGptAgent.js.map +1 -1
  7. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +3 -3
  8. package/lib/chatgpt/ChatGptCallFunctionAgent.js +61 -63
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
  10. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +6 -6
  11. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +28 -30
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
  13. package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +8 -0
  14. package/lib/chatgpt/ChatGptCompletionMessageUtil.js +536 -0
  15. package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -0
  16. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +4 -3
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -6
  18. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
  19. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +2 -2
  20. package/lib/chatgpt/ChatGptHistoryDecoder.js +8 -8
  21. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  22. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +3 -3
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +11 -5
  24. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
  25. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +3 -3
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +39 -42
  27. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  28. package/lib/chatgpt/ChatGptUsageAggregator.d.ts +6 -0
  29. package/lib/chatgpt/ChatGptUsageAggregator.js +48 -0
  30. package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -0
  31. package/lib/context/AgenticaCancelPrompt.d.ts +16 -0
  32. package/lib/context/AgenticaCancelPrompt.js +20 -0
  33. package/lib/context/AgenticaCancelPrompt.js.map +1 -0
  34. package/lib/context/AgenticaClassOperation.d.ts +8 -0
  35. package/lib/context/AgenticaClassOperation.js +11 -0
  36. package/lib/context/AgenticaClassOperation.js.map +1 -0
  37. package/lib/{structures/IAgenticaContext.d.ts → context/AgenticaContext.d.ts} +15 -14
  38. package/lib/{structures/IAgenticaPrompt.js → context/AgenticaContext.js} +1 -1
  39. package/lib/context/AgenticaContext.js.map +1 -0
  40. package/lib/context/AgenticaHttpOperation.d.ts +8 -0
  41. package/lib/context/AgenticaHttpOperation.js +11 -0
  42. package/lib/context/AgenticaHttpOperation.js.map +1 -0
  43. package/lib/context/AgenticaOperation.d.ts +46 -0
  44. package/lib/{structures/IAgenticaContext.js → context/AgenticaOperation.js} +1 -1
  45. package/lib/context/AgenticaOperation.js.map +1 -0
  46. package/lib/context/AgenticaOperationBase.d.ts +29 -0
  47. package/lib/context/AgenticaOperationBase.js +21 -0
  48. package/lib/context/AgenticaOperationBase.js.map +1 -0
  49. package/lib/{structures/IAgenticaOperationCollection.d.ts → context/AgenticaOperationCollection.d.ts} +6 -6
  50. package/lib/{structures/IAgenticaOperationSelection.js → context/AgenticaOperationCollection.js} +1 -1
  51. package/lib/context/AgenticaOperationCollection.js.map +1 -0
  52. package/lib/context/AgenticaOperationSelection.d.ts +15 -0
  53. package/lib/context/AgenticaOperationSelection.js +17 -0
  54. package/lib/context/AgenticaOperationSelection.js.map +1 -0
  55. package/lib/context/AgenticaTokenUsage.d.ts +82 -0
  56. package/lib/context/AgenticaTokenUsage.js +97 -0
  57. package/lib/context/AgenticaTokenUsage.js.map +1 -0
  58. package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +10 -0
  59. package/lib/{internal → context/internal}/AgenticaTokenUsageAggregator.js +0 -46
  60. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -0
  61. package/lib/context/internal/__IChatCancelFunctionsApplication.js.map +1 -0
  62. package/lib/context/internal/__IChatFunctionReference.js.map +1 -0
  63. package/lib/context/internal/__IChatInitialApplication.js.map +1 -0
  64. package/lib/context/internal/__IChatSelectFunctionsApplication.js.map +1 -0
  65. package/lib/events/AgenticaCallEvent.d.ts +18 -0
  66. package/lib/events/AgenticaCallEvent.js +22 -0
  67. package/lib/events/AgenticaCallEvent.js.map +1 -0
  68. package/lib/events/AgenticaCancelEvent.d.ts +14 -0
  69. package/lib/events/AgenticaCancelEvent.js +18 -0
  70. package/lib/events/AgenticaCancelEvent.js.map +1 -0
  71. package/lib/events/AgenticaDescribeEvent.d.ts +27 -0
  72. package/lib/events/AgenticaDescribeEvent.js +40 -0
  73. package/lib/events/AgenticaDescribeEvent.js.map +1 -0
  74. package/lib/events/AgenticaEvent.d.ts +25 -0
  75. package/lib/{structures/IAgenticaEvent.js → events/AgenticaEvent.js} +1 -1
  76. package/lib/events/AgenticaEvent.js.map +1 -0
  77. package/lib/events/AgenticaEventBase.d.ts +4 -0
  78. package/lib/events/AgenticaEventBase.js +10 -0
  79. package/lib/events/AgenticaEventBase.js.map +1 -0
  80. package/lib/events/AgenticaEventSource.d.ts +1 -0
  81. package/lib/events/AgenticaEventSource.js +3 -0
  82. package/lib/events/AgenticaEventSource.js.map +1 -0
  83. package/lib/events/AgenticaExecuteEvent.d.ts +22 -0
  84. package/lib/events/AgenticaExecuteEvent.js +33 -0
  85. package/lib/events/AgenticaExecuteEvent.js.map +1 -0
  86. package/lib/events/AgenticaInitializeEvent.d.ts +6 -0
  87. package/lib/events/AgenticaInitializeEvent.js +16 -0
  88. package/lib/events/AgenticaInitializeEvent.js.map +1 -0
  89. package/lib/events/AgenticaRequestEvent.d.ts +27 -0
  90. package/lib/events/AgenticaRequestEvent.js +22 -0
  91. package/lib/events/AgenticaRequestEvent.js.map +1 -0
  92. package/lib/events/AgenticaResponseEvent.d.ts +35 -0
  93. package/lib/events/AgenticaResponseEvent.js +16 -0
  94. package/lib/events/AgenticaResponseEvent.js.map +1 -0
  95. package/lib/events/AgenticaSelectEvent.d.ts +16 -0
  96. package/lib/events/AgenticaSelectEvent.js +26 -0
  97. package/lib/events/AgenticaSelectEvent.js.map +1 -0
  98. package/lib/events/AgenticaTextEvent.d.ts +25 -0
  99. package/lib/events/AgenticaTextEvent.js +40 -0
  100. package/lib/events/AgenticaTextEvent.js.map +1 -0
  101. package/lib/index.d.ts +26 -8
  102. package/lib/index.js +29 -9
  103. package/lib/index.js.map +1 -1
  104. package/lib/index.mjs +1391 -306
  105. package/lib/index.mjs.map +1 -1
  106. package/lib/internal/AgenticaOperationComposer.d.ts +2 -2
  107. package/lib/internal/AgenticaOperationComposer.js +12 -0
  108. package/lib/internal/AgenticaOperationComposer.js.map +1 -1
  109. package/lib/internal/ByteArrayUtil.d.ts +3 -0
  110. package/lib/internal/ByteArrayUtil.js +10 -0
  111. package/lib/internal/ByteArrayUtil.js.map +1 -0
  112. package/lib/internal/MPSCUtil.d.ts +21 -0
  113. package/lib/internal/MPSCUtil.js +84 -0
  114. package/lib/internal/MPSCUtil.js.map +1 -0
  115. package/lib/internal/StreamUtil.d.ts +6 -0
  116. package/lib/internal/StreamUtil.js +65 -0
  117. package/lib/internal/StreamUtil.js.map +1 -0
  118. package/lib/json/IAgenticaEventJson.d.ts +149 -0
  119. package/lib/{structures/IAgenticaOperation.js → json/IAgenticaEventJson.js} +1 -1
  120. package/lib/json/IAgenticaEventJson.js.map +1 -0
  121. package/lib/json/IAgenticaOperationJson.d.ts +33 -0
  122. package/lib/json/IAgenticaOperationJson.js +3 -0
  123. package/lib/json/IAgenticaOperationJson.js.map +1 -0
  124. package/lib/json/IAgenticaOperationSelectionJson.d.ts +18 -0
  125. package/lib/{structures/IAgenticaOperationCollection.js → json/IAgenticaOperationSelectionJson.js} +1 -1
  126. package/lib/json/IAgenticaOperationSelectionJson.js.map +1 -0
  127. package/lib/json/IAgenticaPromptJson.d.ts +111 -0
  128. package/lib/json/IAgenticaPromptJson.js +3 -0
  129. package/lib/json/IAgenticaPromptJson.js.map +1 -0
  130. package/lib/{structures/IAgenticaTokenUsage.d.ts → json/IAgenticaTokenUsageJson.d.ts} +10 -10
  131. package/lib/json/IAgenticaTokenUsageJson.js +3 -0
  132. package/lib/json/IAgenticaTokenUsageJson.js.map +1 -0
  133. package/lib/prompts/AgenticaCancelPrompt.d.ts +16 -0
  134. package/lib/prompts/AgenticaCancelPrompt.js +20 -0
  135. package/lib/prompts/AgenticaCancelPrompt.js.map +1 -0
  136. package/lib/prompts/AgenticaDescribePrompt.d.ts +24 -0
  137. package/lib/prompts/AgenticaDescribePrompt.js +20 -0
  138. package/lib/prompts/AgenticaDescribePrompt.js.map +1 -0
  139. package/lib/prompts/AgenticaExecutePrompt.d.ts +20 -0
  140. package/lib/prompts/AgenticaExecutePrompt.js +24 -0
  141. package/lib/prompts/AgenticaExecutePrompt.js.map +1 -0
  142. package/lib/prompts/AgenticaPrompt.d.ts +7 -0
  143. package/lib/{typings/AgenticaSource.js → prompts/AgenticaPrompt.js} +1 -1
  144. package/lib/prompts/AgenticaPrompt.js.map +1 -0
  145. package/lib/prompts/AgenticaPromptBase.d.ts +22 -0
  146. package/lib/prompts/AgenticaPromptBase.js +22 -0
  147. package/lib/prompts/AgenticaPromptBase.js.map +1 -0
  148. package/lib/prompts/AgenticaSelectPrompt.d.ts +16 -0
  149. package/lib/prompts/AgenticaSelectPrompt.js +20 -0
  150. package/lib/prompts/AgenticaSelectPrompt.js.map +1 -0
  151. package/lib/prompts/AgenticaTextPrompt.d.ts +14 -0
  152. package/lib/prompts/AgenticaTextPrompt.js +20 -0
  153. package/lib/prompts/AgenticaTextPrompt.js.map +1 -0
  154. package/lib/structures/IAgenticaConfig.d.ts +3 -3
  155. package/lib/structures/IAgenticaExecutor.d.ts +12 -11
  156. package/lib/structures/IAgenticaProps.d.ts +2 -3
  157. package/lib/structures/IAgenticaSystemPrompt.d.ts +12 -11
  158. package/lib/transformers/AgenticaEventTransformer.d.ts +45 -0
  159. package/lib/transformers/AgenticaEventTransformer.js +127 -0
  160. package/lib/transformers/AgenticaEventTransformer.js.map +1 -0
  161. package/lib/transformers/AgenticaPromptTransformer.d.ts +34 -0
  162. package/lib/transformers/AgenticaPromptTransformer.js +90 -0
  163. package/lib/transformers/AgenticaPromptTransformer.js.map +1 -0
  164. package/package.json +1 -1
  165. package/src/Agentica.ts +93 -63
  166. package/src/chatgpt/ChatGptAgent.ts +8 -7
  167. package/src/chatgpt/ChatGptCallFunctionAgent.ts +90 -88
  168. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +51 -58
  169. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -0
  170. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +81 -12
  171. package/src/chatgpt/ChatGptHistoryDecoder.ts +14 -14
  172. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +21 -13
  173. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +66 -74
  174. package/src/chatgpt/ChatGptUsageAggregator.ts +62 -0
  175. package/src/context/AgenticaCancelPrompt.ts +32 -0
  176. package/src/context/AgenticaClassOperation.ts +23 -0
  177. package/src/{structures/IAgenticaContext.ts → context/AgenticaContext.ts} +17 -16
  178. package/src/context/AgenticaHttpOperation.ts +27 -0
  179. package/src/{structures/IAgenticaOperation.ts → context/AgenticaOperation.ts} +25 -22
  180. package/src/context/AgenticaOperationBase.ts +57 -0
  181. package/src/{structures/IAgenticaOperationCollection.ts → context/AgenticaOperationCollection.ts} +6 -6
  182. package/src/context/AgenticaOperationSelection.ts +27 -0
  183. package/src/context/AgenticaTokenUsage.ts +170 -0
  184. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -0
  185. package/src/events/AgenticaCallEvent.ts +36 -0
  186. package/src/events/AgenticaCancelEvent.ts +28 -0
  187. package/src/events/AgenticaDescribeEvent.ts +61 -0
  188. package/src/events/AgenticaEvent.ts +36 -0
  189. package/src/events/AgenticaEventBase.ts +7 -0
  190. package/src/{typings/AgenticaSource.ts → events/AgenticaEventSource.ts} +1 -1
  191. package/src/events/AgenticaExecuteEvent.ts +50 -0
  192. package/src/events/AgenticaInitializeEvent.ts +14 -0
  193. package/src/events/AgenticaRequestEvent.ts +45 -0
  194. package/src/events/AgenticaResponseEvent.ts +48 -0
  195. package/src/events/AgenticaSelectEvent.ts +37 -0
  196. package/src/events/AgenticaTextEvent.ts +57 -0
  197. package/src/index.ts +33 -9
  198. package/src/internal/AgenticaOperationComposer.ts +24 -15
  199. package/src/internal/ByteArrayUtil.ts +5 -0
  200. package/src/internal/MPSCUtil.ts +75 -0
  201. package/src/internal/StreamUtil.ts +64 -0
  202. package/src/json/IAgenticaEventJson.ts +178 -0
  203. package/src/json/IAgenticaOperationJson.ts +36 -0
  204. package/src/json/IAgenticaOperationSelectionJson.ts +19 -0
  205. package/src/json/IAgenticaPromptJson.ts +130 -0
  206. package/src/{structures/IAgenticaTokenUsage.ts → json/IAgenticaTokenUsageJson.ts} +10 -10
  207. package/src/prompts/AgenticaCancelPrompt.ts +32 -0
  208. package/src/prompts/AgenticaDescribePrompt.ts +41 -0
  209. package/src/prompts/AgenticaExecutePrompt.ts +52 -0
  210. package/src/prompts/AgenticaPrompt.ts +14 -0
  211. package/src/prompts/AgenticaPromptBase.ts +27 -0
  212. package/src/prompts/AgenticaSelectPrompt.ts +32 -0
  213. package/src/prompts/AgenticaTextPrompt.ts +31 -0
  214. package/src/structures/IAgenticaConfig.ts +3 -3
  215. package/src/structures/IAgenticaExecutor.ts +14 -13
  216. package/src/structures/IAgenticaProps.ts +2 -3
  217. package/src/structures/IAgenticaSystemPrompt.ts +12 -11
  218. package/src/transformers/AgenticaEventTransformer.ts +165 -0
  219. package/src/transformers/AgenticaPromptTransformer.ts +134 -0
  220. package/lib/internal/AgenticaPromptFactory.d.ts +0 -7
  221. package/lib/internal/AgenticaPromptFactory.js +0 -9
  222. package/lib/internal/AgenticaPromptFactory.js.map +0 -1
  223. package/lib/internal/AgenticaPromptTransformer.d.ts +0 -10
  224. package/lib/internal/AgenticaPromptTransformer.js +0 -58
  225. package/lib/internal/AgenticaPromptTransformer.js.map +0 -1
  226. package/lib/internal/AgenticaTokenUsageAggregator.d.ts +0 -11
  227. package/lib/internal/AgenticaTokenUsageAggregator.js.map +0 -1
  228. package/lib/structures/IAgenticaContext.js.map +0 -1
  229. package/lib/structures/IAgenticaEvent.d.ts +0 -192
  230. package/lib/structures/IAgenticaEvent.js.map +0 -1
  231. package/lib/structures/IAgenticaOperation.d.ts +0 -47
  232. package/lib/structures/IAgenticaOperation.js.map +0 -1
  233. package/lib/structures/IAgenticaOperationCollection.js.map +0 -1
  234. package/lib/structures/IAgenticaOperationSelection.d.ts +0 -50
  235. package/lib/structures/IAgenticaOperationSelection.js.map +0 -1
  236. package/lib/structures/IAgenticaPrompt.d.ts +0 -138
  237. package/lib/structures/IAgenticaPrompt.js.map +0 -1
  238. package/lib/structures/IAgenticaTokenUsage.js +0 -3
  239. package/lib/structures/IAgenticaTokenUsage.js.map +0 -1
  240. package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +0 -1
  241. package/lib/structures/internal/__IChatFunctionReference.js.map +0 -1
  242. package/lib/structures/internal/__IChatInitialApplication.js.map +0 -1
  243. package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +0 -1
  244. package/lib/typings/AgenticaSource.d.ts +0 -1
  245. package/lib/typings/AgenticaSource.js.map +0 -1
  246. package/src/internal/AgenticaPromptFactory.ts +0 -32
  247. package/src/internal/AgenticaPromptTransformer.ts +0 -86
  248. package/src/internal/AgenticaTokenUsageAggregator.ts +0 -121
  249. package/src/structures/IAgenticaEvent.ts +0 -229
  250. package/src/structures/IAgenticaOperationSelection.ts +0 -68
  251. package/src/structures/IAgenticaPrompt.ts +0 -182
  252. /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.d.ts +0 -0
  253. /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.js +0 -0
  254. /package/lib/{structures → context}/internal/__IChatFunctionReference.d.ts +0 -0
  255. /package/lib/{structures → context}/internal/__IChatFunctionReference.js +0 -0
  256. /package/lib/{structures → context}/internal/__IChatInitialApplication.d.ts +0 -0
  257. /package/lib/{structures → context}/internal/__IChatInitialApplication.js +0 -0
  258. /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.d.ts +0 -0
  259. /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.js +0 -0
  260. /package/src/{structures → context}/internal/__IChatCancelFunctionsApplication.ts +0 -0
  261. /package/src/{structures → context}/internal/__IChatFunctionReference.ts +0 -0
  262. /package/src/{structures → context}/internal/__IChatInitialApplication.ts +0 -0
  263. /package/src/{structures → context}/internal/__IChatSelectFunctionsApplication.ts +0 -0
@@ -1,37 +1,37 @@
1
- import {
2
- IHttpLlmFunction,
3
- ILlmApplication,
4
- ILlmSchema,
5
- } from "@samchon/openapi";
1
+ import { ILlmApplication, ILlmSchema } from "@samchon/openapi";
6
2
  import OpenAI from "openai";
7
3
  import typia, { IValidation } from "typia";
8
4
  import { v4 } from "uuid";
9
5
 
6
+ import { AgenticaContext } from "../context/AgenticaContext";
7
+ import { AgenticaOperation } from "../context/AgenticaOperation";
8
+ 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
+ import { AgenticaSelectEvent } from "../events/AgenticaSelectEvent";
13
+ import { AgenticaTextEvent } from "../events/AgenticaTextEvent";
10
14
  import { AgenticaConstant } from "../internal/AgenticaConstant";
11
15
  import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
12
- import { AgenticaPromptFactory } from "../internal/AgenticaPromptFactory";
13
16
  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 { __IChatFunctionReference } from "../structures/internal/__IChatFunctionReference";
21
- import { __IChatSelectFunctionsApplication } from "../structures/internal/__IChatSelectFunctionsApplication";
17
+ import { StreamUtil } from "../internal/StreamUtil";
18
+ import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
19
+ import { AgenticaSelectPrompt } from "../prompts/AgenticaSelectPrompt";
20
+ import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
21
+ import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
22
22
  import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
23
23
 
24
24
  export namespace ChatGptSelectFunctionAgent {
25
25
  export const execute = async <Model extends ILlmSchema.Model>(
26
- ctx: IAgenticaContext<Model>,
27
- ): Promise<IAgenticaPrompt<Model>[]> => {
26
+ ctx: AgenticaContext<Model>,
27
+ ): Promise<AgenticaPrompt<Model>[]> => {
28
28
  if (ctx.operations.divided === undefined)
29
29
  return step(ctx, ctx.operations.array, 0);
30
30
 
31
- const stacks: IAgenticaOperationSelection<Model>[][] =
31
+ const stacks: AgenticaOperationSelection<Model>[][] =
32
32
  ctx.operations.divided.map(() => []);
33
- const events: IAgenticaEvent<Model>[] = [];
34
- const prompts: IAgenticaPrompt<Model>[][] = await Promise.all(
33
+ const events: AgenticaEvent<Model>[] = [];
34
+ const prompts: AgenticaPrompt<Model>[][] = await Promise.all(
35
35
  ctx.operations.divided.map((operations, i) =>
36
36
  step(
37
37
  {
@@ -58,48 +58,38 @@ export namespace ChatGptSelectFunctionAgent {
58
58
  .map(
59
59
  (s) =>
60
60
  ctx.operations.group
61
- .get(s.controller.name)!
62
- .get(s.function.name)!,
61
+ .get(s.operation.controller.name)!
62
+ .get(s.operation.function.name)!,
63
63
  ),
64
64
  0,
65
65
  );
66
66
 
67
67
  // RE-COLLECT SELECT FUNCTION EVENTS
68
- const collection: IAgenticaPrompt.ISelect<Model> = {
68
+ const collection: AgenticaSelectPrompt<Model> = new AgenticaSelectPrompt({
69
69
  id: v4(),
70
- type: "select",
71
- operations: [],
72
- };
70
+ selections: [],
71
+ });
73
72
  for (const e of events)
74
73
  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
- );
74
+ collection.selections.push(e.selection);
85
75
  await selectFunction(ctx, {
86
- name: e.operation.name,
87
- reason: e.reason,
76
+ name: e.selection.operation.name,
77
+ reason: e.selection.reason,
88
78
  });
89
79
  }
90
80
  return [collection];
91
81
  };
92
82
 
93
83
  const step = async <Model extends ILlmSchema.Model>(
94
- ctx: IAgenticaContext<Model>,
95
- operations: IAgenticaOperation<Model>[],
84
+ ctx: AgenticaContext<Model>,
85
+ operations: AgenticaOperation<Model>[],
96
86
  retry: number,
97
87
  failures?: IFailure[],
98
- ): Promise<IAgenticaPrompt<Model>[]> => {
88
+ ): Promise<AgenticaPrompt<Model>[]> => {
99
89
  //----
100
90
  // EXECUTE CHATGPT API
101
91
  //----
102
- const completion: OpenAI.ChatCompletion = await ctx.request("select", {
92
+ const completionStream = await ctx.request("select", {
103
93
  messages: [
104
94
  // COMMON SYSTEM PROMPT
105
95
  {
@@ -170,6 +160,8 @@ export namespace ChatGptSelectFunctionAgent {
170
160
  parallel_tool_calls: false,
171
161
  });
172
162
 
163
+ const chunks = await StreamUtil.readAll(completionStream);
164
+ const completion = ChatGptCompletionMessageUtil.merge(chunks);
173
165
  //----
174
166
  // VALIDATION
175
167
  //----
@@ -194,7 +186,7 @@ export namespace ChatGptSelectFunctionAgent {
194
186
  //----
195
187
  // PROCESS COMPLETION
196
188
  //----
197
- const prompts: IAgenticaPrompt<Model>[] = [];
189
+ const prompts: AgenticaPrompt<Model>[] = [];
198
190
  for (const choice of completion.choices) {
199
191
  // TOOL CALLING HANDLER
200
192
  if (choice.message.tool_calls)
@@ -206,27 +198,23 @@ export namespace ChatGptSelectFunctionAgent {
206
198
  );
207
199
  if (typia.is(input) === false) continue;
208
200
  else if (tc.function.name === "selectFunctions") {
209
- const collection: IAgenticaPrompt.ISelect<Model> = {
210
- id: tc.id,
211
- type: "select",
212
- operations: [],
213
- };
201
+ const collection: AgenticaSelectPrompt<Model> =
202
+ new AgenticaSelectPrompt({
203
+ id: tc.id,
204
+ selections: [],
205
+ });
214
206
  for (const reference of input.functions) {
215
- const operation: IAgenticaOperation<Model> | null =
207
+ const operation: AgenticaOperation<Model> | null =
216
208
  await selectFunction(ctx, reference);
217
209
  if (operation !== null)
218
- collection.operations.push(
219
- AgenticaPromptFactory.selection({
220
- protocol: operation.protocol as "http",
221
- controller:
222
- operation.controller as IAgenticaController.IHttp<Model>,
223
- function: operation.function as IHttpLlmFunction<Model>,
224
- name: operation.name,
210
+ collection.selections.push(
211
+ new AgenticaOperationSelection({
212
+ operation,
225
213
  reason: reference.reason,
226
214
  }),
227
215
  );
228
216
  }
229
- if (collection.operations.length !== 0) prompts.push(collection);
217
+ if (collection.selections.length !== 0) prompts.push(collection);
230
218
  }
231
219
  }
232
220
 
@@ -235,40 +223,44 @@ export namespace ChatGptSelectFunctionAgent {
235
223
  choice.message.role === "assistant" &&
236
224
  !!choice.message.content?.length
237
225
  ) {
238
- const text: IAgenticaPrompt.IText = {
239
- type: "text",
226
+ const text: AgenticaTextPrompt = new AgenticaTextPrompt({
240
227
  role: "assistant",
241
228
  text: choice.message.content,
242
- };
229
+ });
243
230
  prompts.push(text);
244
- await ctx.dispatch(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
+ );
245
240
  }
246
241
  }
247
242
  return prompts;
248
243
  };
249
244
 
250
245
  const selectFunction = async <Model extends ILlmSchema.Model>(
251
- ctx: IAgenticaContext<Model>,
246
+ ctx: AgenticaContext<Model>,
252
247
  reference: __IChatFunctionReference,
253
- ): Promise<IAgenticaOperation<Model> | null> => {
254
- const operation: IAgenticaOperation<Model> | undefined =
248
+ ): Promise<AgenticaOperation<Model> | null> => {
249
+ const operation: AgenticaOperation<Model> | undefined =
255
250
  ctx.operations.flat.get(reference.name);
256
251
  if (operation === undefined) return null;
257
252
 
258
- ctx.stack.push(
259
- AgenticaPromptFactory.selection({
260
- protocol: operation.protocol as "http",
261
- controller: operation.controller as IAgenticaController.IHttp<Model>,
262
- function: operation.function as IHttpLlmFunction<Model>,
263
- name: reference.name,
253
+ const selection: AgenticaOperationSelection<Model> =
254
+ new AgenticaOperationSelection({
255
+ operation,
264
256
  reason: reference.reason,
257
+ });
258
+ ctx.stack.push(selection);
259
+ void ctx.dispatch(
260
+ new AgenticaSelectEvent({
261
+ selection,
265
262
  }),
266
263
  );
267
- await ctx.dispatch({
268
- type: "select",
269
- reason: reference.reason,
270
- operation,
271
- });
272
264
  return operation;
273
265
  };
274
266
 
@@ -0,0 +1,62 @@
1
+ import { CompletionUsage } from "openai/resources";
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
+ };
17
+ };
18
+
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
+ };
27
+ };
28
+
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:
36
+ (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
+ };
61
+ };
62
+ }
@@ -0,0 +1,32 @@
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
4
+ import { AgenticaPromptBase } from "../prompts/AgenticaPromptBase";
5
+ import { AgenticaOperationSelection } from "./AgenticaOperationSelection";
6
+
7
+ export class AgenticaCancelPrompt<
8
+ Model extends ILlmSchema.Model,
9
+ > extends AgenticaPromptBase<"cancel", IAgenticaPromptJson.ICancel> {
10
+ public readonly id: string;
11
+ public readonly selections: AgenticaOperationSelection<Model>[];
12
+
13
+ public constructor(props: AgenticaCancelPrompt.IProps<Model>) {
14
+ super("cancel");
15
+ this.id = props.id;
16
+ this.selections = props.selections;
17
+ }
18
+
19
+ public toJSON(): IAgenticaPromptJson.ICancel {
20
+ return {
21
+ type: this.type,
22
+ id: this.id,
23
+ selections: this.selections.map((s) => s.toJSON()),
24
+ };
25
+ }
26
+ }
27
+ export namespace AgenticaCancelPrompt {
28
+ export interface IProps<Model extends ILlmSchema.Model> {
29
+ id: string;
30
+ selections: AgenticaOperationSelection<Model>[];
31
+ }
32
+ }
@@ -0,0 +1,23 @@
1
+ import { ILlmApplication, ILlmFunction, ILlmSchema } from "@samchon/openapi";
2
+
3
+ import { AgenticaOperationBase } from "./AgenticaOperationBase";
4
+
5
+ export class AgenticaClassOperation<
6
+ Model extends ILlmSchema.Model,
7
+ > extends AgenticaOperationBase<
8
+ "class",
9
+ ILlmApplication<Model>,
10
+ ILlmFunction<Model>
11
+ > {
12
+ public constructor(props: AgenticaClassOperation.IProps<Model>) {
13
+ super(props);
14
+ }
15
+ }
16
+ export namespace AgenticaClassOperation {
17
+ export type IProps<Model extends ILlmSchema.Model> =
18
+ AgenticaOperationBase.IProps<
19
+ "class",
20
+ ILlmApplication<Model>,
21
+ ILlmFunction<Model>
22
+ >;
23
+ }
@@ -1,12 +1,13 @@
1
1
  import { ILlmSchema } from "@samchon/openapi";
2
2
  import OpenAI from "openai";
3
3
 
4
- import { AgenticaSource } from "../typings/AgenticaSource";
5
- import { IAgenticaConfig } from "./IAgenticaConfig";
6
- import { IAgenticaEvent } from "./IAgenticaEvent";
7
- import { IAgenticaOperationCollection } from "./IAgenticaOperationCollection";
8
- import { IAgenticaOperationSelection } from "./IAgenticaOperationSelection";
9
- import { IAgenticaPrompt } from "./IAgenticaPrompt";
4
+ import { AgenticaEvent } from "../events/AgenticaEvent";
5
+ import { AgenticaEventSource } from "../events/AgenticaEventSource";
6
+ import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
7
+ import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
8
+ import { IAgenticaConfig } from "../structures/IAgenticaConfig";
9
+ import { AgenticaOperationCollection } from "./AgenticaOperationCollection";
10
+ import { AgenticaOperationSelection } from "./AgenticaOperationSelection";
10
11
 
11
12
  /**
12
13
  * Context of the Nestia A.I. agent.
@@ -39,7 +40,7 @@ import { IAgenticaPrompt } from "./IAgenticaPrompt";
39
40
  *
40
41
  * @author Samchon
41
42
  */
42
- export interface IAgenticaContext<Model extends ILlmSchema.Model> {
43
+ export interface AgenticaContext<Model extends ILlmSchema.Model> {
43
44
  //----
44
45
  // APPLICATION
45
46
  //----
@@ -50,7 +51,7 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
50
51
  * groups composed by the divide and conquer rule for the
51
52
  * efficient operation selection if configured.
52
53
  */
53
- operations: IAgenticaOperationCollection<Model>;
54
+ operations: AgenticaOperationCollection<Model>;
54
55
 
55
56
  /**
56
57
  * Configuration of the agent.
@@ -68,14 +69,14 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
68
69
  /**
69
70
  * Prompt histories.
70
71
  */
71
- histories: IAgenticaPrompt<Model>[];
72
+ histories: AgenticaPrompt<Model>[];
72
73
 
73
74
  /**
74
75
  * Stacked operations.
75
76
  *
76
77
  * In other words, list of candidate operations for the LLM function calling.
77
78
  */
78
- stack: IAgenticaOperationSelection<Model>[];
79
+ stack: AgenticaOperationSelection<Model>[];
79
80
 
80
81
  /**
81
82
  * Text prompt of the user.
@@ -83,7 +84,7 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
83
84
  * Text conversation written the by user through the
84
85
  * {@link Agentica.conversate} function.
85
86
  */
86
- prompt: IAgenticaPrompt.IText<"user">;
87
+ prompt: AgenticaTextPrompt<"user">;
87
88
 
88
89
  /**
89
90
  * Whether the agent is ready.
@@ -91,7 +92,7 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
91
92
  * Returns a boolean value indicates whether the agent is ready to
92
93
  * perform the function calling.
93
94
  *
94
- * If the agent has called the {@link IAgenticaContext.initialize},
95
+ * If the agent has called the {@link AgenticaContext.initialize},
95
96
  * it returns `true`. Otherwise the {@link initialize} has never been
96
97
  * called, returns `false`.
97
98
  */
@@ -108,7 +109,7 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
108
109
  *
109
110
  * @param event Event to deliver
110
111
  */
111
- dispatch: (event: IAgenticaEvent<Model>) => Promise<void>;
112
+ dispatch: (event: AgenticaEvent<Model>) => Promise<void>;
112
113
 
113
114
  /**
114
115
  * Request to the OpenAI server.
@@ -118,9 +119,9 @@ export interface IAgenticaContext<Model extends ILlmSchema.Model> {
118
119
  * @returns Response from the OpenAI server
119
120
  */
120
121
  request: (
121
- source: AgenticaSource,
122
- body: Omit<OpenAI.ChatCompletionCreateParamsNonStreaming, "model">,
123
- ) => Promise<OpenAI.ChatCompletion>;
122
+ source: AgenticaEventSource,
123
+ body: Omit<OpenAI.ChatCompletionCreateParamsStreaming, "model" | "stream">,
124
+ ) => Promise<ReadableStream<OpenAI.Chat.Completions.ChatCompletionChunk>>;
124
125
 
125
126
  /**
126
127
  * Initialize the agent.
@@ -0,0 +1,27 @@
1
+ import {
2
+ IHttpLlmApplication,
3
+ IHttpLlmFunction,
4
+ ILlmSchema,
5
+ } from "@samchon/openapi";
6
+
7
+ import { AgenticaOperationBase } from "./AgenticaOperationBase";
8
+
9
+ export class AgenticaHttpOperation<
10
+ Model extends ILlmSchema.Model,
11
+ > extends AgenticaOperationBase<
12
+ "http",
13
+ IHttpLlmApplication<Model>,
14
+ IHttpLlmFunction<Model>
15
+ > {
16
+ public constructor(props: AgenticaHttpOperation.IProps<Model>) {
17
+ super(props);
18
+ }
19
+ }
20
+ export namespace AgenticaHttpOperation {
21
+ export type IProps<Model extends ILlmSchema.Model> =
22
+ AgenticaOperationBase.IProps<
23
+ "http",
24
+ IHttpLlmApplication<Model>,
25
+ IHttpLlmFunction<Model>
26
+ >;
27
+ }
@@ -1,11 +1,12 @@
1
1
  import { IHttpLlmFunction, ILlmFunction, ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import { IAgenticaController } from "./IAgenticaController";
3
+ import { IAgenticaOperationJson } from "../json/IAgenticaOperationJson";
4
+ import { IAgenticaController } from "../structures/IAgenticaController";
4
5
 
5
6
  /**
6
- * Operation information in the Nestia Agent.
7
+ * Operation information in the Agentica Agent.
7
8
  *
8
- * `IAgenticaOperation` is a type represents an operation that would
9
+ * `AgenticaOperation` is a type represents an operation that would
9
10
  * be selected by the A.I. chatbot of {@link Agentica} class to
10
11
  * perform the LLM (Large Language Model) function calling.
11
12
  *
@@ -17,29 +18,26 @@ import { IAgenticaController } from "./IAgenticaController";
17
18
  *
18
19
  * @author Samchon
19
20
  */
20
- export type IAgenticaOperation<Model extends ILlmSchema.Model> =
21
- | IAgenticaOperation.IHttp<Model>
22
- | IAgenticaOperation.IClass<Model>;
23
- export namespace IAgenticaOperation {
24
- /**
25
- * HTTP API operation.
26
- */
27
- export type IHttp<Model extends ILlmSchema.Model> = IBase<
28
- "http",
29
- IAgenticaController.IHttp<Model>,
30
- IHttpLlmFunction<Model>
31
- >;
32
-
33
- /**
34
- * TypeScript class operation.
35
- */
36
- export type IClass<Model extends ILlmSchema.Model> = IBase<
21
+ export type AgenticaOperation<Model extends ILlmSchema.Model> =
22
+ | AgenticaOperation.Class<Model>
23
+ | AgenticaOperation.Http<Model>;
24
+ export namespace AgenticaOperation {
25
+ export type Class<Model extends ILlmSchema.Model> = Base<
37
26
  "class",
38
27
  IAgenticaController.IClass<Model>,
39
28
  ILlmFunction<Model>
40
29
  >;
30
+ export type Http<Model extends ILlmSchema.Model> = Base<
31
+ "http",
32
+ IAgenticaController.IHttp<Model>,
33
+ IHttpLlmFunction<Model>
34
+ >;
41
35
 
42
- interface IBase<Protocol, Application, Function> {
36
+ interface Base<
37
+ Protocol extends "http" | "class",
38
+ Controller extends object,
39
+ Function extends object,
40
+ > {
43
41
  /**
44
42
  * Protocol discriminator.
45
43
  */
@@ -48,7 +46,7 @@ export namespace IAgenticaOperation {
48
46
  /**
49
47
  * Belonged controller of the target function.
50
48
  */
51
- controller: Application;
49
+ controller: Controller;
52
50
 
53
51
  /**
54
52
  * Target function to call.
@@ -59,5 +57,10 @@ export namespace IAgenticaOperation {
59
57
  * Identifier name.
60
58
  */
61
59
  name: string;
60
+
61
+ /**
62
+ * Convert to primitive JSON object.
63
+ */
64
+ toJSON(): IAgenticaOperationJson;
62
65
  }
63
66
  }
@@ -0,0 +1,57 @@
1
+ import { IAgenticaOperationJson } from "../json/IAgenticaOperationJson";
2
+
3
+ export abstract class AgenticaOperationBase<
4
+ Protocol extends "http" | "class",
5
+ Controller extends object,
6
+ Function extends object,
7
+ > {
8
+ /**
9
+ * Protocol discriminator.
10
+ */
11
+ public readonly protocol: Protocol;
12
+
13
+ /**
14
+ * Belonged controller of the target function.
15
+ */
16
+ public readonly controller: Controller;
17
+
18
+ /**
19
+ * Target function to call.
20
+ */
21
+ public readonly function: Function;
22
+
23
+ /**
24
+ * Identifier name.
25
+ */
26
+ public readonly name: string;
27
+
28
+ protected constructor(
29
+ props: AgenticaOperationBase.IProps<Protocol, Controller, Function>,
30
+ ) {
31
+ this.protocol = props.protocol;
32
+ this.controller = props.controller;
33
+ this.function = props.function;
34
+ this.name = props.name;
35
+ }
36
+
37
+ public toJSON(): IAgenticaOperationJson {
38
+ return {
39
+ protocol: this.protocol,
40
+ controller: this.controller.constructor.name,
41
+ function: this.function.constructor.name,
42
+ name: this.name,
43
+ };
44
+ }
45
+ }
46
+ export namespace AgenticaOperationBase {
47
+ export interface IProps<
48
+ Protocol extends "http" | "class",
49
+ Controller extends object,
50
+ Function extends object,
51
+ > {
52
+ protocol: Protocol;
53
+ controller: Controller;
54
+ function: Function;
55
+ name: string;
56
+ }
57
+ }
@@ -1,6 +1,6 @@
1
1
  import { ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import { IAgenticaOperation } from "./IAgenticaOperation";
3
+ import { AgenticaOperation } from "./AgenticaOperation";
4
4
 
5
5
  /**
6
6
  * Collection of operations used in the Nestia Agent.
@@ -11,11 +11,11 @@ import { IAgenticaOperation } from "./IAgenticaOperation";
11
11
  *
12
12
  * @author Samchon
13
13
  */
14
- export interface IAgenticaOperationCollection<Model extends ILlmSchema.Model> {
14
+ export interface AgenticaOperationCollection<Model extends ILlmSchema.Model> {
15
15
  /**
16
16
  * List of every operations.
17
17
  */
18
- array: IAgenticaOperation<Model>[];
18
+ array: AgenticaOperation<Model>[];
19
19
 
20
20
  /**
21
21
  * Divided operations.
@@ -32,14 +32,14 @@ export interface IAgenticaOperationCollection<Model extends ILlmSchema.Model> {
32
32
  * Otherwise, if the {@link IAgenticaConfig.capacity} has not been
33
33
  * configured, this `divided` property would be the `undefined` value.
34
34
  */
35
- divided?: IAgenticaOperation<Model>[][] | undefined;
35
+ divided?: AgenticaOperation<Model>[][] | undefined;
36
36
 
37
37
  /**
38
38
  * Flat dictionary of operations.
39
39
  *
40
40
  * Dictionary of operations with their {@link IAgenticaOperation.name}.
41
41
  */
42
- flat: Map<string, IAgenticaOperation<Model>>;
42
+ flat: Map<string, AgenticaOperation<Model>>;
43
43
 
44
44
  /**
45
45
  * Group dictionary of operations.
@@ -48,5 +48,5 @@ export interface IAgenticaOperationCollection<Model extends ILlmSchema.Model> {
48
48
  * {@link IAgenticaOperation.controller.name} and
49
49
  * {@link IAgenticaOperation.function.name}.
50
50
  */
51
- group: Map<string, Map<string, IAgenticaOperation<Model>>>;
51
+ group: Map<string, Map<string, AgenticaOperation<Model>>>;
52
52
  }