@agentica/core 0.10.3 → 0.11.0

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 (265) hide show
  1. package/README.md +2 -2
  2. package/lib/Agentica.d.ts +14 -14
  3. package/lib/Agentica.js +54 -29
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/chatgpt/ChatGptAgent.d.ts +3 -3
  6. package/lib/chatgpt/ChatGptAgent.js +1 -1
  7. package/lib/chatgpt/ChatGptAgent.js.map +1 -1
  8. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +3 -3
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.js +61 -63
  10. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
  11. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +6 -6
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +28 -30
  13. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
  14. package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +8 -0
  15. package/lib/chatgpt/ChatGptCompletionMessageUtil.js +536 -0
  16. package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -0
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +4 -3
  18. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -6
  19. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
  20. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +2 -2
  21. package/lib/chatgpt/ChatGptHistoryDecoder.js +8 -8
  22. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +3 -3
  24. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +11 -5
  25. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +3 -3
  27. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +39 -42
  28. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  29. package/lib/chatgpt/ChatGptUsageAggregator.d.ts +6 -0
  30. package/lib/chatgpt/ChatGptUsageAggregator.js +48 -0
  31. package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -0
  32. package/lib/context/AgenticaCancelPrompt.d.ts +16 -0
  33. package/lib/context/AgenticaCancelPrompt.js +20 -0
  34. package/lib/context/AgenticaCancelPrompt.js.map +1 -0
  35. package/lib/context/AgenticaClassOperation.d.ts +8 -0
  36. package/lib/context/AgenticaClassOperation.js +11 -0
  37. package/lib/context/AgenticaClassOperation.js.map +1 -0
  38. package/lib/{structures/IAgenticaContext.d.ts → context/AgenticaContext.d.ts} +15 -14
  39. package/lib/{structures/IAgenticaPrompt.js → context/AgenticaContext.js} +1 -1
  40. package/lib/context/AgenticaContext.js.map +1 -0
  41. package/lib/context/AgenticaHttpOperation.d.ts +8 -0
  42. package/lib/context/AgenticaHttpOperation.js +11 -0
  43. package/lib/context/AgenticaHttpOperation.js.map +1 -0
  44. package/lib/context/AgenticaOperation.d.ts +46 -0
  45. package/lib/{structures/IAgenticaContext.js → context/AgenticaOperation.js} +1 -1
  46. package/lib/context/AgenticaOperation.js.map +1 -0
  47. package/lib/context/AgenticaOperationBase.d.ts +29 -0
  48. package/lib/context/AgenticaOperationBase.js +21 -0
  49. package/lib/context/AgenticaOperationBase.js.map +1 -0
  50. package/lib/{structures/IAgenticaOperationCollection.d.ts → context/AgenticaOperationCollection.d.ts} +6 -6
  51. package/lib/{structures/IAgenticaOperationSelection.js → context/AgenticaOperationCollection.js} +1 -1
  52. package/lib/context/AgenticaOperationCollection.js.map +1 -0
  53. package/lib/context/AgenticaOperationSelection.d.ts +15 -0
  54. package/lib/context/AgenticaOperationSelection.js +17 -0
  55. package/lib/context/AgenticaOperationSelection.js.map +1 -0
  56. package/lib/context/AgenticaTokenUsage.d.ts +82 -0
  57. package/lib/context/AgenticaTokenUsage.js +97 -0
  58. package/lib/context/AgenticaTokenUsage.js.map +1 -0
  59. package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +10 -0
  60. package/lib/context/internal/AgenticaTokenUsageAggregator.js +47 -0
  61. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -0
  62. package/lib/context/internal/__IChatCancelFunctionsApplication.js.map +1 -0
  63. package/lib/context/internal/__IChatFunctionReference.js.map +1 -0
  64. package/lib/context/internal/__IChatInitialApplication.js.map +1 -0
  65. package/lib/context/internal/__IChatSelectFunctionsApplication.js.map +1 -0
  66. package/lib/events/AgenticaCallEvent.d.ts +18 -0
  67. package/lib/events/AgenticaCallEvent.js +22 -0
  68. package/lib/events/AgenticaCallEvent.js.map +1 -0
  69. package/lib/events/AgenticaCancelEvent.d.ts +14 -0
  70. package/lib/events/AgenticaCancelEvent.js +18 -0
  71. package/lib/events/AgenticaCancelEvent.js.map +1 -0
  72. package/lib/events/AgenticaDescribeEvent.d.ts +27 -0
  73. package/lib/events/AgenticaDescribeEvent.js +40 -0
  74. package/lib/events/AgenticaDescribeEvent.js.map +1 -0
  75. package/lib/events/AgenticaEvent.d.ts +25 -0
  76. package/lib/{structures/IAgenticaEvent.js → events/AgenticaEvent.js} +1 -1
  77. package/lib/events/AgenticaEvent.js.map +1 -0
  78. package/lib/events/AgenticaEventBase.d.ts +4 -0
  79. package/lib/events/AgenticaEventBase.js +10 -0
  80. package/lib/events/AgenticaEventBase.js.map +1 -0
  81. package/lib/events/AgenticaEventSource.d.ts +1 -0
  82. package/lib/events/AgenticaEventSource.js +3 -0
  83. package/lib/events/AgenticaEventSource.js.map +1 -0
  84. package/lib/events/AgenticaExecuteEvent.d.ts +22 -0
  85. package/lib/events/AgenticaExecuteEvent.js +33 -0
  86. package/lib/events/AgenticaExecuteEvent.js.map +1 -0
  87. package/lib/events/AgenticaInitializeEvent.d.ts +6 -0
  88. package/lib/events/AgenticaInitializeEvent.js +16 -0
  89. package/lib/events/AgenticaInitializeEvent.js.map +1 -0
  90. package/lib/events/AgenticaRequestEvent.d.ts +27 -0
  91. package/lib/events/AgenticaRequestEvent.js +22 -0
  92. package/lib/events/AgenticaRequestEvent.js.map +1 -0
  93. package/lib/events/AgenticaResponseEvent.d.ts +35 -0
  94. package/lib/events/AgenticaResponseEvent.js +16 -0
  95. package/lib/events/AgenticaResponseEvent.js.map +1 -0
  96. package/lib/events/AgenticaSelectEvent.d.ts +16 -0
  97. package/lib/events/AgenticaSelectEvent.js +26 -0
  98. package/lib/events/AgenticaSelectEvent.js.map +1 -0
  99. package/lib/events/AgenticaTextEvent.d.ts +25 -0
  100. package/lib/events/AgenticaTextEvent.js +40 -0
  101. package/lib/events/AgenticaTextEvent.js.map +1 -0
  102. package/lib/index.d.ts +26 -8
  103. package/lib/index.js +29 -9
  104. package/lib/index.js.map +1 -1
  105. package/lib/index.mjs +1391 -304
  106. package/lib/index.mjs.map +1 -1
  107. package/lib/internal/AgenticaOperationComposer.d.ts +2 -2
  108. package/lib/internal/AgenticaOperationComposer.js +12 -0
  109. package/lib/internal/AgenticaOperationComposer.js.map +1 -1
  110. package/lib/internal/ByteArrayUtil.d.ts +3 -0
  111. package/lib/internal/ByteArrayUtil.js +10 -0
  112. package/lib/internal/ByteArrayUtil.js.map +1 -0
  113. package/lib/internal/MPSCUtil.d.ts +21 -0
  114. package/lib/internal/MPSCUtil.js +84 -0
  115. package/lib/internal/MPSCUtil.js.map +1 -0
  116. package/lib/internal/StreamUtil.d.ts +6 -0
  117. package/lib/internal/StreamUtil.js +65 -0
  118. package/lib/internal/StreamUtil.js.map +1 -0
  119. package/lib/json/IAgenticaEventJson.d.ts +149 -0
  120. package/lib/{structures/IAgenticaOperation.js → json/IAgenticaEventJson.js} +1 -1
  121. package/lib/json/IAgenticaEventJson.js.map +1 -0
  122. package/lib/json/IAgenticaOperationJson.d.ts +33 -0
  123. package/lib/json/IAgenticaOperationJson.js +3 -0
  124. package/lib/json/IAgenticaOperationJson.js.map +1 -0
  125. package/lib/json/IAgenticaOperationSelectionJson.d.ts +18 -0
  126. package/lib/{structures/IAgenticaOperationCollection.js → json/IAgenticaOperationSelectionJson.js} +1 -1
  127. package/lib/json/IAgenticaOperationSelectionJson.js.map +1 -0
  128. package/lib/json/IAgenticaPromptJson.d.ts +111 -0
  129. package/lib/json/IAgenticaPromptJson.js +3 -0
  130. package/lib/json/IAgenticaPromptJson.js.map +1 -0
  131. package/lib/{structures/IAgenticaTokenUsage.d.ts → json/IAgenticaTokenUsageJson.d.ts} +10 -10
  132. package/lib/json/IAgenticaTokenUsageJson.js +3 -0
  133. package/lib/json/IAgenticaTokenUsageJson.js.map +1 -0
  134. package/lib/prompts/AgenticaCancelPrompt.d.ts +16 -0
  135. package/lib/prompts/AgenticaCancelPrompt.js +20 -0
  136. package/lib/prompts/AgenticaCancelPrompt.js.map +1 -0
  137. package/lib/prompts/AgenticaDescribePrompt.d.ts +24 -0
  138. package/lib/prompts/AgenticaDescribePrompt.js +20 -0
  139. package/lib/prompts/AgenticaDescribePrompt.js.map +1 -0
  140. package/lib/prompts/AgenticaExecutePrompt.d.ts +20 -0
  141. package/lib/prompts/AgenticaExecutePrompt.js +24 -0
  142. package/lib/prompts/AgenticaExecutePrompt.js.map +1 -0
  143. package/lib/prompts/AgenticaPrompt.d.ts +7 -0
  144. package/lib/{typings/AgenticaSource.js → prompts/AgenticaPrompt.js} +1 -1
  145. package/lib/prompts/AgenticaPrompt.js.map +1 -0
  146. package/lib/prompts/AgenticaPromptBase.d.ts +22 -0
  147. package/lib/prompts/AgenticaPromptBase.js +22 -0
  148. package/lib/prompts/AgenticaPromptBase.js.map +1 -0
  149. package/lib/prompts/AgenticaSelectPrompt.d.ts +16 -0
  150. package/lib/prompts/AgenticaSelectPrompt.js +20 -0
  151. package/lib/prompts/AgenticaSelectPrompt.js.map +1 -0
  152. package/lib/prompts/AgenticaTextPrompt.d.ts +14 -0
  153. package/lib/prompts/AgenticaTextPrompt.js +20 -0
  154. package/lib/prompts/AgenticaTextPrompt.js.map +1 -0
  155. package/lib/structures/IAgenticaConfig.d.ts +3 -3
  156. package/lib/structures/IAgenticaExecutor.d.ts +12 -11
  157. package/lib/structures/IAgenticaProps.d.ts +2 -3
  158. package/lib/structures/IAgenticaSystemPrompt.d.ts +12 -11
  159. package/lib/transformers/AgenticaEventTransformer.d.ts +45 -0
  160. package/lib/transformers/AgenticaEventTransformer.js +127 -0
  161. package/lib/transformers/AgenticaEventTransformer.js.map +1 -0
  162. package/lib/transformers/AgenticaPromptTransformer.d.ts +34 -0
  163. package/lib/transformers/AgenticaPromptTransformer.js +90 -0
  164. package/lib/transformers/AgenticaPromptTransformer.js.map +1 -0
  165. package/package.json +1 -1
  166. package/src/Agentica.ts +94 -61
  167. package/src/chatgpt/ChatGptAgent.ts +8 -7
  168. package/src/chatgpt/ChatGptCallFunctionAgent.ts +90 -88
  169. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +51 -58
  170. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -0
  171. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +81 -12
  172. package/src/chatgpt/ChatGptHistoryDecoder.ts +14 -14
  173. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +21 -13
  174. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +66 -74
  175. package/src/chatgpt/ChatGptUsageAggregator.ts +62 -0
  176. package/src/context/AgenticaCancelPrompt.ts +32 -0
  177. package/src/context/AgenticaClassOperation.ts +23 -0
  178. package/src/{structures/IAgenticaContext.ts → context/AgenticaContext.ts} +17 -16
  179. package/src/context/AgenticaHttpOperation.ts +27 -0
  180. package/src/{structures/IAgenticaOperation.ts → context/AgenticaOperation.ts} +25 -22
  181. package/src/context/AgenticaOperationBase.ts +57 -0
  182. package/src/{structures/IAgenticaOperationCollection.ts → context/AgenticaOperationCollection.ts} +6 -6
  183. package/src/context/AgenticaOperationSelection.ts +27 -0
  184. package/src/context/AgenticaTokenUsage.ts +170 -0
  185. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -0
  186. package/src/events/AgenticaCallEvent.ts +36 -0
  187. package/src/events/AgenticaCancelEvent.ts +28 -0
  188. package/src/events/AgenticaDescribeEvent.ts +61 -0
  189. package/src/events/AgenticaEvent.ts +36 -0
  190. package/src/events/AgenticaEventBase.ts +7 -0
  191. package/src/{typings/AgenticaSource.ts → events/AgenticaEventSource.ts} +1 -1
  192. package/src/events/AgenticaExecuteEvent.ts +50 -0
  193. package/src/events/AgenticaInitializeEvent.ts +14 -0
  194. package/src/events/AgenticaRequestEvent.ts +45 -0
  195. package/src/events/AgenticaResponseEvent.ts +48 -0
  196. package/src/events/AgenticaSelectEvent.ts +37 -0
  197. package/src/events/AgenticaTextEvent.ts +57 -0
  198. package/src/index.ts +33 -9
  199. package/src/internal/AgenticaOperationComposer.ts +24 -15
  200. package/src/internal/ByteArrayUtil.ts +5 -0
  201. package/src/internal/MPSCUtil.ts +75 -0
  202. package/src/internal/StreamUtil.ts +64 -0
  203. package/src/json/IAgenticaEventJson.ts +178 -0
  204. package/src/json/IAgenticaOperationJson.ts +36 -0
  205. package/src/json/IAgenticaOperationSelectionJson.ts +19 -0
  206. package/src/json/IAgenticaPromptJson.ts +130 -0
  207. package/src/{structures/IAgenticaTokenUsage.ts → json/IAgenticaTokenUsageJson.ts} +10 -10
  208. package/src/prompts/AgenticaCancelPrompt.ts +32 -0
  209. package/src/prompts/AgenticaDescribePrompt.ts +41 -0
  210. package/src/prompts/AgenticaExecutePrompt.ts +52 -0
  211. package/src/prompts/AgenticaPrompt.ts +14 -0
  212. package/src/prompts/AgenticaPromptBase.ts +27 -0
  213. package/src/prompts/AgenticaSelectPrompt.ts +32 -0
  214. package/src/prompts/AgenticaTextPrompt.ts +31 -0
  215. package/src/structures/IAgenticaConfig.ts +3 -3
  216. package/src/structures/IAgenticaExecutor.ts +14 -13
  217. package/src/structures/IAgenticaProps.ts +2 -3
  218. package/src/structures/IAgenticaSystemPrompt.ts +12 -11
  219. package/src/transformers/AgenticaEventTransformer.ts +165 -0
  220. package/src/transformers/AgenticaPromptTransformer.ts +134 -0
  221. package/lib/internal/AgenticaPromptFactory.d.ts +0 -7
  222. package/lib/internal/AgenticaPromptFactory.js +0 -9
  223. package/lib/internal/AgenticaPromptFactory.js.map +0 -1
  224. package/lib/internal/AgenticaPromptTransformer.d.ts +0 -10
  225. package/lib/internal/AgenticaPromptTransformer.js +0 -58
  226. package/lib/internal/AgenticaPromptTransformer.js.map +0 -1
  227. package/lib/internal/AgenticaTokenUsageAggregator.d.ts +0 -11
  228. package/lib/internal/AgenticaTokenUsageAggregator.js +0 -92
  229. package/lib/internal/AgenticaTokenUsageAggregator.js.map +0 -1
  230. package/lib/structures/IAgenticaContext.js.map +0 -1
  231. package/lib/structures/IAgenticaEvent.d.ts +0 -192
  232. package/lib/structures/IAgenticaEvent.js.map +0 -1
  233. package/lib/structures/IAgenticaOperation.d.ts +0 -47
  234. package/lib/structures/IAgenticaOperation.js.map +0 -1
  235. package/lib/structures/IAgenticaOperationCollection.js.map +0 -1
  236. package/lib/structures/IAgenticaOperationSelection.d.ts +0 -50
  237. package/lib/structures/IAgenticaOperationSelection.js.map +0 -1
  238. package/lib/structures/IAgenticaPrompt.d.ts +0 -138
  239. package/lib/structures/IAgenticaPrompt.js.map +0 -1
  240. package/lib/structures/IAgenticaTokenUsage.js +0 -3
  241. package/lib/structures/IAgenticaTokenUsage.js.map +0 -1
  242. package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +0 -1
  243. package/lib/structures/internal/__IChatFunctionReference.js.map +0 -1
  244. package/lib/structures/internal/__IChatInitialApplication.js.map +0 -1
  245. package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +0 -1
  246. package/lib/typings/AgenticaSource.d.ts +0 -1
  247. package/lib/typings/AgenticaSource.js.map +0 -1
  248. package/src/internal/AgenticaPromptFactory.ts +0 -32
  249. package/src/internal/AgenticaPromptTransformer.ts +0 -86
  250. package/src/internal/AgenticaTokenUsageAggregator.ts +0 -115
  251. package/src/structures/IAgenticaEvent.ts +0 -229
  252. package/src/structures/IAgenticaOperationSelection.ts +0 -68
  253. package/src/structures/IAgenticaPrompt.ts +0 -182
  254. /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.d.ts +0 -0
  255. /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.js +0 -0
  256. /package/lib/{structures → context}/internal/__IChatFunctionReference.d.ts +0 -0
  257. /package/lib/{structures → context}/internal/__IChatFunctionReference.js +0 -0
  258. /package/lib/{structures → context}/internal/__IChatInitialApplication.d.ts +0 -0
  259. /package/lib/{structures → context}/internal/__IChatInitialApplication.js +0 -0
  260. /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.d.ts +0 -0
  261. /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.js +0 -0
  262. /package/src/{structures → context}/internal/__IChatCancelFunctionsApplication.ts +0 -0
  263. /package/src/{structures → context}/internal/__IChatFunctionReference.ts +0 -0
  264. /package/src/{structures → context}/internal/__IChatInitialApplication.ts +0 -0
  265. /package/src/{structures → context}/internal/__IChatSelectFunctionsApplication.ts +0 -0
@@ -1,37 +1,34 @@
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 { AgenticaCancelPrompt } from "../context/AgenticaCancelPrompt";
7
+ import { AgenticaContext } from "../context/AgenticaContext";
8
+ import { AgenticaOperation } from "../context/AgenticaOperation";
9
+ import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
10
+ import { __IChatCancelFunctionsApplication } from "../context/internal/__IChatCancelFunctionsApplication";
11
+ import { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
12
+ import { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
13
+ import { AgenticaEvent } from "../events/AgenticaEvent";
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 { __IChatCancelFunctionsApplication } from "../structures/internal/__IChatCancelFunctionsApplication";
21
- import { __IChatFunctionReference } from "../structures/internal/__IChatFunctionReference";
17
+ import { StreamUtil } from "../internal/StreamUtil";
18
+ import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
22
19
  import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
23
20
 
24
21
  export namespace ChatGptCancelFunctionAgent {
25
22
  export const execute = async <Model extends ILlmSchema.Model>(
26
- ctx: IAgenticaContext<Model>,
27
- ): Promise<IAgenticaPrompt.ICancel<Model>[]> => {
23
+ ctx: AgenticaContext<Model>,
24
+ ): Promise<AgenticaCancelPrompt<Model>[]> => {
28
25
  if (ctx.operations.divided === undefined)
29
26
  return step(ctx, ctx.operations.array, 0);
30
27
 
31
- const stacks: IAgenticaOperationSelection<Model>[][] =
28
+ const stacks: AgenticaOperationSelection<Model>[][] =
32
29
  ctx.operations.divided.map(() => []);
33
- const events: IAgenticaEvent<Model>[] = [];
34
- const prompts: IAgenticaPrompt.ICancel<Model>[][] = await Promise.all(
30
+ const events: AgenticaEvent<Model>[] = [];
31
+ const prompts: AgenticaCancelPrompt<Model>[][] = await Promise.all(
35
32
  ctx.operations.divided.map((operations, i) =>
36
33
  step(
37
34
  {
@@ -58,67 +55,60 @@ export namespace ChatGptCancelFunctionAgent {
58
55
  .map(
59
56
  (s) =>
60
57
  ctx.operations.group
61
- .get(s.controller.name)!
62
- .get(s.function.name)!,
58
+ .get(s.operation.controller.name)!
59
+ .get(s.operation.function.name)!,
63
60
  ),
64
61
  0,
65
62
  );
66
63
 
67
64
  // RE-COLLECT SELECT FUNCTION EVENTS
68
- const collection: IAgenticaPrompt.ICancel<Model> = {
65
+ const collection: AgenticaCancelPrompt<Model> = new AgenticaCancelPrompt({
69
66
  id: v4(),
70
- type: "cancel",
71
- operations: [],
72
- };
67
+ selections: [],
68
+ });
73
69
  for (const e of events)
74
70
  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
- );
71
+ collection.selections.push(e.selection);
85
72
  await cancelFunction(ctx, {
86
- name: e.operation.name,
87
- reason: e.reason,
73
+ name: e.selection.operation.name,
74
+ reason: e.selection.reason,
88
75
  });
89
76
  }
90
77
  return [collection];
91
78
  };
92
79
 
93
80
  export const cancelFunction = async <Model extends ILlmSchema.Model>(
94
- ctx: IAgenticaContext<Model>,
81
+ ctx: AgenticaContext<Model>,
95
82
  reference: __IChatFunctionReference,
96
- ): Promise<IAgenticaOperationSelection<Model> | null> => {
83
+ ): Promise<AgenticaOperationSelection<Model> | null> => {
97
84
  const index: number = ctx.stack.findIndex(
98
- (item) => item.name === reference.name,
85
+ (item) => item.operation.name === reference.name,
99
86
  );
100
87
  if (index === -1) return null;
101
88
 
102
- const item: IAgenticaOperationSelection<Model> = ctx.stack[index]!;
89
+ const item: AgenticaOperationSelection<Model> = ctx.stack[index]!;
103
90
  ctx.stack.splice(index, 1);
104
- await ctx.dispatch({
105
- type: "cancel",
106
- operation: item,
107
- reason: reference.reason,
108
- });
91
+ await ctx.dispatch(
92
+ new AgenticaCancelEvent({
93
+ selection: new AgenticaOperationSelection({
94
+ operation: item.operation,
95
+ reason: reference.reason,
96
+ }),
97
+ }),
98
+ );
109
99
  return item;
110
100
  };
111
101
 
112
102
  const step = async <Model extends ILlmSchema.Model>(
113
- ctx: IAgenticaContext<Model>,
114
- operations: IAgenticaOperation<Model>[],
103
+ ctx: AgenticaContext<Model>,
104
+ operations: AgenticaOperation<Model>[],
115
105
  retry: number,
116
106
  failures?: IFailure[],
117
- ): Promise<IAgenticaPrompt.ICancel<Model>[]> => {
107
+ ): Promise<AgenticaCancelPrompt<Model>[]> => {
118
108
  //----
119
109
  // EXECUTE CHATGPT API
120
110
  //----
121
- const completion: OpenAI.ChatCompletion = await ctx.request("cancel", {
111
+ const completionStream = await ctx.request("cancel", {
122
112
  messages: [
123
113
  // COMMON SYSTEM PROMPT
124
114
  {
@@ -189,6 +179,9 @@ export namespace ChatGptCancelFunctionAgent {
189
179
  parallel_tool_calls: true,
190
180
  });
191
181
 
182
+ const chunks = await StreamUtil.readAll(completionStream);
183
+ const completion = ChatGptCompletionMessageUtil.merge(chunks);
184
+
192
185
  //----
193
186
  // VALIDATION
194
187
  //----
@@ -213,7 +206,7 @@ export namespace ChatGptCancelFunctionAgent {
213
206
  //----
214
207
  // PROCESS COMPLETION
215
208
  //----
216
- const prompts: IAgenticaPrompt.ICancel<Model>[] = [];
209
+ const prompts: AgenticaCancelPrompt<Model>[] = [];
217
210
  for (const choice of completion.choices) {
218
211
  // TOOL CALLING HANDLER
219
212
  if (choice.message.tool_calls)
@@ -224,16 +217,16 @@ export namespace ChatGptCancelFunctionAgent {
224
217
  );
225
218
  if (typia.is(input) === false) continue;
226
219
  else if (tc.function.name === "cancelFunctions") {
227
- const collection: IAgenticaPrompt.ICancel<Model> = {
228
- id: tc.id,
229
- type: "cancel",
230
- operations: [],
231
- };
220
+ const collection: AgenticaCancelPrompt<Model> =
221
+ new AgenticaCancelPrompt({
222
+ id: tc.id,
223
+ selections: [],
224
+ });
232
225
  for (const reference of input.functions) {
233
226
  const operation = await cancelFunction(ctx, reference);
234
- if (operation !== null) collection.operations.push(operation);
227
+ if (operation !== null) collection.selections.push(operation);
235
228
  }
236
- if (collection.operations.length !== 0) prompts.push(collection);
229
+ if (collection.selections.length !== 0) prompts.push(collection);
237
230
  }
238
231
  }
239
232
  }
@@ -0,0 +1,166 @@
1
+ import {
2
+ ChatCompletion,
3
+ ChatCompletionChunk,
4
+ ChatCompletionMessage,
5
+ ChatCompletionMessageToolCall,
6
+ } from "openai/resources";
7
+ import { json } from "typia";
8
+
9
+ import { ByteArrayUtil } from "../internal/ByteArrayUtil";
10
+ import { ChatGptUsageAggregator } from "./ChatGptUsageAggregator";
11
+
12
+ export namespace ChatGptCompletionMessageUtil {
13
+ export const transformCompletionChunk = (
14
+ source: string | Uint8Array,
15
+ ): ChatCompletionChunk => {
16
+ const str =
17
+ source instanceof Uint8Array ? ByteArrayUtil.toUtf8(source) : source;
18
+ return json.assertParse<ChatCompletionChunk>(str);
19
+ };
20
+
21
+ export const accumulate = (
22
+ origin: ChatCompletion,
23
+ chunk: ChatCompletionChunk,
24
+ ): ChatCompletion => {
25
+ const choices = origin.choices;
26
+ chunk.choices.forEach((choice) => {
27
+ const accChoice = choices[choice.index];
28
+ if (accChoice) {
29
+ choices[choice.index] = mergeChoice(accChoice, choice);
30
+ return;
31
+ }
32
+
33
+ choices[choice.index] = {
34
+ index: choice.index,
35
+
36
+ finish_reason:
37
+ choice.finish_reason ??
38
+ (null as unknown as ChatCompletion.Choice["finish_reason"]),
39
+
40
+ logprobs: choice.logprobs ?? null,
41
+ message: {
42
+ tool_calls: choice.delta.tool_calls
43
+ ? choice.delta.tool_calls.reduce((acc, cur) => {
44
+ acc[cur.index] = {
45
+ id: cur.id ?? "",
46
+ type: "function",
47
+ function: {
48
+ name: cur.function?.name ?? "",
49
+ arguments: cur.function?.arguments ?? "",
50
+ },
51
+ };
52
+ return acc;
53
+ }, [] as ChatCompletionMessageToolCall[])
54
+ : undefined,
55
+ content: choice.delta.content ?? null,
56
+ refusal: choice.delta.refusal ?? null,
57
+ role: "assistant",
58
+ } satisfies ChatCompletionMessage,
59
+ };
60
+ });
61
+
62
+ const usage = (() => {
63
+ if (!chunk.usage) {
64
+ return origin.usage;
65
+ }
66
+
67
+ if (!origin.usage) {
68
+ return chunk.usage;
69
+ }
70
+
71
+ return ChatGptUsageAggregator.sum(origin.usage, chunk.usage);
72
+ })();
73
+
74
+ return {
75
+ ...origin,
76
+ choices,
77
+ usage,
78
+ };
79
+ };
80
+
81
+ export const merge = (chunks: ChatCompletionChunk[]): ChatCompletion => {
82
+ const firstChunk = chunks[0];
83
+ if (!firstChunk) throw new Error("No chunks received");
84
+
85
+ return chunks.reduce(ChatGptCompletionMessageUtil.accumulate, {
86
+ id: firstChunk.id,
87
+ choices: [],
88
+ created: firstChunk.created,
89
+ model: firstChunk.model,
90
+ object: "chat.completion",
91
+ usage: undefined,
92
+ service_tier: firstChunk.service_tier,
93
+ system_fingerprint: firstChunk.system_fingerprint,
94
+ } as ChatCompletion);
95
+ };
96
+
97
+ export const mergeChoice = (
98
+ acc: ChatCompletion.Choice,
99
+ cur: ChatCompletionChunk.Choice,
100
+ ): ChatCompletion.Choice => {
101
+ if (!acc.finish_reason && cur.finish_reason) {
102
+ acc.finish_reason = cur.finish_reason;
103
+ }
104
+
105
+ if (!acc.logprobs && cur.logprobs) {
106
+ acc.logprobs = cur.logprobs;
107
+ }
108
+
109
+ if (cur.delta.content) {
110
+ if (!acc.message.content) {
111
+ acc.message.content = cur.delta.content;
112
+ } else {
113
+ acc.message.content += cur.delta.content;
114
+ }
115
+ }
116
+
117
+ if (cur.delta.refusal) {
118
+ if (!acc.message.refusal) {
119
+ acc.message.refusal = cur.delta.refusal;
120
+ } else {
121
+ acc.message.refusal += cur.delta.refusal;
122
+ }
123
+ }
124
+
125
+ if (cur.delta.tool_calls) {
126
+ acc.message.tool_calls ??= [];
127
+ const toolCalls = acc.message.tool_calls;
128
+
129
+ cur.delta.tool_calls.forEach((toolCall) => {
130
+ const existingToolCall = toolCalls[toolCall.index];
131
+ if (existingToolCall) {
132
+ toolCalls[toolCall.index] = mergeToolCalls(
133
+ existingToolCall,
134
+ toolCall,
135
+ );
136
+ return;
137
+ }
138
+
139
+ toolCalls[toolCall.index] = {
140
+ id: toolCall.id ?? "",
141
+ type: "function",
142
+ function: {
143
+ name: toolCall.function?.name ?? "",
144
+ arguments: toolCall.function?.arguments ?? "",
145
+ },
146
+ };
147
+ });
148
+ }
149
+
150
+ return acc;
151
+ };
152
+
153
+ export const mergeToolCalls = (
154
+ acc: ChatCompletionMessageToolCall,
155
+ cur: ChatCompletionChunk.Choice.Delta.ToolCall,
156
+ ): ChatCompletionMessageToolCall => {
157
+ if (cur.function) {
158
+ acc.function.arguments += cur.function.arguments ?? "";
159
+ acc.function.name += cur.function.name ?? "";
160
+ }
161
+
162
+ acc.id += cur.id ?? "";
163
+
164
+ return acc;
165
+ };
166
+ }
@@ -1,19 +1,24 @@
1
1
  import { ILlmSchema } from "@samchon/openapi";
2
2
  import OpenAI from "openai";
3
3
 
4
+ import { AgenticaContext } from "../context/AgenticaContext";
5
+ import { AgenticaDescribeEvent } from "../events/AgenticaDescribeEvent";
4
6
  import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
5
7
  import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
6
- import { IAgenticaContext } from "../structures/IAgenticaContext";
7
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
8
+ import { MPSCUtil } from "../internal/MPSCUtil";
9
+ import { StreamUtil } from "../internal/StreamUtil";
10
+ import { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
11
+ import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
12
+ import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
8
13
  import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
9
14
 
10
15
  export namespace ChatGptDescribeFunctionAgent {
11
16
  export const execute = async <Model extends ILlmSchema.Model>(
12
- ctx: IAgenticaContext<Model>,
13
- histories: IAgenticaPrompt.IExecute<Model>[],
14
- ): Promise<IAgenticaPrompt.IDescribe<Model>[]> => {
17
+ ctx: AgenticaContext<Model>,
18
+ histories: AgenticaExecutePrompt<Model>[],
19
+ ): Promise<AgenticaDescribePrompt<Model>[]> => {
15
20
  if (histories.length === 0) return [];
16
- const completion: OpenAI.ChatCompletion = await ctx.request("describe", {
21
+ const completionStream = await ctx.request("describe", {
17
22
  messages: [
18
23
  // COMMON SYSTEM PROMPT
19
24
  {
@@ -31,7 +36,73 @@ export namespace ChatGptDescribeFunctionAgent {
31
36
  },
32
37
  ],
33
38
  });
34
- const descriptions: IAgenticaPrompt.IDescribe<Model>[] = completion.choices
39
+
40
+ const describeContext: ({
41
+ content: string;
42
+ } & ReturnType<typeof MPSCUtil.create<string>>)[] = [];
43
+
44
+ const completion = await StreamUtil.reduce<
45
+ OpenAI.ChatCompletionChunk,
46
+ Promise<OpenAI.ChatCompletion>
47
+ >(completionStream, async (accPromise, chunk) => {
48
+ const acc = await accPromise;
49
+ const registerContext = (
50
+ choices: OpenAI.ChatCompletionChunk.Choice[],
51
+ ) => {
52
+ for (const choice of choices) {
53
+ if (choice.finish_reason) {
54
+ describeContext[choice.index]!.close();
55
+ continue;
56
+ }
57
+ if (!choice.delta.content) {
58
+ continue;
59
+ }
60
+
61
+ if (describeContext[choice.index]) {
62
+ describeContext[choice.index]!.content += choice.delta.content;
63
+ describeContext[choice.index]!.produce(choice.delta.content);
64
+ continue;
65
+ }
66
+
67
+ const { consumer, produce, close, waitClose, done } =
68
+ MPSCUtil.create<string>();
69
+
70
+ describeContext[choice.index] = {
71
+ content: choice.delta.content,
72
+ consumer,
73
+ produce,
74
+ close,
75
+ waitClose,
76
+ done,
77
+ };
78
+ produce(choice.delta.content);
79
+
80
+ void ctx.dispatch(
81
+ new AgenticaDescribeEvent({
82
+ executes: histories,
83
+ stream: consumer,
84
+ done,
85
+ get: () => describeContext[choice.index]?.content ?? "",
86
+ join: async () => {
87
+ await waitClose();
88
+ return describeContext[choice.index]!.content;
89
+ },
90
+ }),
91
+ );
92
+ }
93
+ };
94
+
95
+ if (acc.object === "chat.completion.chunk") {
96
+ registerContext([acc, chunk].flatMap((acc) => acc.choices));
97
+ return ChatGptCompletionMessageUtil.merge([acc, chunk]);
98
+ }
99
+
100
+ registerContext(chunk.choices);
101
+ return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
102
+ });
103
+
104
+ if (!completion) throw new Error("No completion received");
105
+ const descriptions: AgenticaDescribePrompt<Model>[] = completion.choices
35
106
  .map((choice) =>
36
107
  choice.message.role === "assistant" && !!choice.message.content?.length
37
108
  ? choice.message.content
@@ -40,13 +111,11 @@ export namespace ChatGptDescribeFunctionAgent {
40
111
  .filter((str) => str !== null)
41
112
  .map(
42
113
  (content) =>
43
- ({
44
- type: "describe",
45
- executions: histories,
114
+ new AgenticaDescribePrompt({
115
+ executes: histories,
46
116
  text: content,
47
- }) satisfies IAgenticaPrompt.IDescribe<Model>,
117
+ }),
48
118
  );
49
- for (const describe of descriptions) await ctx.dispatch(describe);
50
119
  return descriptions;
51
120
  };
52
121
  }
@@ -1,11 +1,11 @@
1
1
  import { ILlmSchema } from "@samchon/openapi";
2
2
  import OpenAI from "openai";
3
3
 
4
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
4
+ import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
5
5
 
6
6
  export namespace ChatGptHistoryDecoder {
7
7
  export const decode = <Model extends ILlmSchema.Model>(
8
- history: IAgenticaPrompt<Model>,
8
+ history: AgenticaPrompt<Model>,
9
9
  ): OpenAI.ChatCompletionMessageParam[] => {
10
10
  // NO NEED TO DECODE DESCRIBE
11
11
  if (history.type === "describe") return [];
@@ -27,9 +27,9 @@ export namespace ChatGptHistoryDecoder {
27
27
  function: {
28
28
  name: `${history.type}Functions`,
29
29
  arguments: JSON.stringify({
30
- functions: history.operations.map((t) => ({
31
- name: t.function.name,
32
- reason: t.reason,
30
+ functions: history.selections.map((s) => ({
31
+ name: s.operation.function.name,
32
+ reason: s.reason,
33
33
  })),
34
34
  }),
35
35
  },
@@ -51,7 +51,7 @@ export namespace ChatGptHistoryDecoder {
51
51
  type: "function",
52
52
  id: history.id,
53
53
  function: {
54
- name: history.function.name,
54
+ name: history.operation.name,
55
55
  arguments: JSON.stringify(history.arguments),
56
56
  },
57
57
  },
@@ -62,18 +62,18 @@ export namespace ChatGptHistoryDecoder {
62
62
  tool_call_id: history.id,
63
63
  content: JSON.stringify({
64
64
  function: {
65
- protocol: history.protocol,
66
- description: history.function.description,
67
- parameters: history.function.parameters,
68
- output: history.function.output,
69
- ...(history.protocol === "http"
65
+ protocol: history.operation.protocol,
66
+ description: history.operation.function.description,
67
+ parameters: history.operation.function.parameters,
68
+ output: history.operation.function.output,
69
+ ...(history.operation.protocol === "http"
70
70
  ? {
71
- method: history.function.method,
72
- path: history.function.path,
71
+ method: history.operation.function.method,
72
+ path: history.operation.function.path,
73
73
  }
74
74
  : {}),
75
75
  },
76
- ...(history.protocol === "http"
76
+ ...(history.operation.protocol === "http"
77
77
  ? {
78
78
  status: history.value.status,
79
79
  data: history.value.body,
@@ -2,21 +2,24 @@ import { ILlmFunction, ILlmSchema } from "@samchon/openapi";
2
2
  import OpenAI from "openai";
3
3
  import typia from "typia";
4
4
 
5
+ import { AgenticaContext } from "../context/AgenticaContext";
6
+ import { __IChatInitialApplication } from "../context/internal/__IChatInitialApplication";
5
7
  import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
6
8
  import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
7
- import { IAgenticaContext } from "../structures/IAgenticaContext";
8
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
9
- import { __IChatInitialApplication } from "../structures/internal/__IChatInitialApplication";
9
+ import { StreamUtil } from "../internal/StreamUtil";
10
+ import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
11
+ import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
12
+ import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
10
13
  import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
11
14
 
12
15
  export namespace ChatGptInitializeFunctionAgent {
13
16
  export const execute = async <Model extends ILlmSchema.Model>(
14
- ctx: IAgenticaContext<Model>,
15
- ): Promise<IAgenticaPrompt<Model>[]> => {
17
+ ctx: AgenticaContext<Model>,
18
+ ): Promise<AgenticaPrompt<Model>[]> => {
16
19
  //----
17
20
  // EXECUTE CHATGPT API
18
21
  //----
19
- const completion: OpenAI.ChatCompletion = await ctx.request("initialize", {
22
+ const completionStream = await ctx.request("initialize", {
20
23
  messages: [
21
24
  // COMMON SYSTEM PROMPT
22
25
  {
@@ -53,20 +56,25 @@ export namespace ChatGptInitializeFunctionAgent {
53
56
  parallel_tool_calls: false,
54
57
  });
55
58
 
59
+ const chunks = await StreamUtil.readAll(completionStream);
60
+ const completion = ChatGptCompletionMessageUtil.merge(chunks);
56
61
  //----
57
62
  // PROCESS COMPLETION
58
63
  //----
59
- const prompts: IAgenticaPrompt<Model>[] = [];
64
+ const prompts: AgenticaPrompt<Model>[] = [];
60
65
  for (const choice of completion.choices) {
61
66
  if (
62
67
  choice.message.role === "assistant" &&
63
68
  !!choice.message.content?.length
64
- )
65
- prompts.push({
66
- type: "text",
67
- role: "assistant",
68
- text: choice.message.content,
69
- });
69
+ ) {
70
+ // @TODO this logic should call the dispatch function
71
+ prompts.push(
72
+ new AgenticaTextPrompt({
73
+ role: "assistant",
74
+ text: choice.message.content,
75
+ }),
76
+ );
77
+ }
70
78
  }
71
79
  if (
72
80
  completion.choices.some(