@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
@@ -0,0 +1,57 @@
1
+ import { IAgenticaEventJson } from "../json/IAgenticaEventJson";
2
+ import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
3
+ import { AgenticaEventBase } from "./AgenticaEventBase";
4
+
5
+ export class AgenticaTextEvent extends AgenticaEventBase<"text"> {
6
+ public readonly role: "assistant" | "user";
7
+ public readonly stream: ReadableStream<string>;
8
+ public get text(): string {
9
+ return this.get_();
10
+ }
11
+ public get done(): boolean {
12
+ return this.done_();
13
+ }
14
+
15
+ public constructor(props: AgenticaTextEvent.IProps) {
16
+ super("text");
17
+ this.role = props.role;
18
+ this.stream = props.stream;
19
+
20
+ this.done_ = props.done;
21
+ this.get_ = props.get;
22
+ this.join_ = props.join;
23
+ }
24
+
25
+ public join(): Promise<string> {
26
+ return this.join_();
27
+ }
28
+
29
+ public toJSON(): IAgenticaEventJson.IText {
30
+ return {
31
+ type: "text",
32
+ role: this.role,
33
+ text: this.text,
34
+ done: this.done,
35
+ };
36
+ }
37
+
38
+ public toPrompt(): AgenticaTextPrompt {
39
+ return new AgenticaTextPrompt({
40
+ role: this.role,
41
+ text: this.text,
42
+ });
43
+ }
44
+
45
+ private readonly done_: () => boolean;
46
+ private readonly get_: () => string;
47
+ private readonly join_: () => Promise<string>;
48
+ }
49
+ export namespace AgenticaTextEvent {
50
+ export interface IProps {
51
+ role: "assistant" | "user";
52
+ stream: ReadableStream<string>;
53
+ done: () => boolean;
54
+ get: () => string;
55
+ join: () => Promise<string>;
56
+ }
57
+ }
package/src/index.ts CHANGED
@@ -1,18 +1,42 @@
1
- // TYPES
1
+ // STRUCTURES
2
2
  export * from "./structures/IAgenticaConfig";
3
- export * from "./structures/IAgenticaContext";
4
3
  export * from "./structures/IAgenticaController";
5
- export * from "./structures/IAgenticaEvent";
6
4
  export * from "./structures/IAgenticaExecutor";
7
- export * from "./structures/IAgenticaOperation";
8
- export * from "./structures/IAgenticaOperationCollection";
9
- export * from "./structures/IAgenticaOperationSelection";
10
- export * from "./structures/IAgenticaPrompt";
11
5
  export * from "./structures/IAgenticaProps";
12
6
  export * from "./structures/IAgenticaVendor";
13
7
  export * from "./structures/IAgenticaSystemPrompt";
14
- export * from "./structures/IAgenticaTokenUsage";
15
- export * from "./typings/AgenticaSource";
8
+
9
+ export * from "./json/IAgenticaEventJson";
10
+ export * from "./json/IAgenticaOperationJson";
11
+ export * from "./json/IAgenticaOperationSelectionJson";
12
+ export * from "./json/IAgenticaPromptJson";
13
+ export * from "./json/IAgenticaTokenUsageJson";
14
+
15
+ // CONTEXT
16
+ export * from "./context/AgenticaContext";
17
+ export * from "./context/AgenticaOperation";
18
+ export * from "./context/AgenticaOperationCollection";
19
+ export * from "./context/AgenticaOperationSelection";
20
+ export * from "./context/AgenticaTokenUsage";
21
+
22
+ export * from "./prompts/AgenticaPrompt";
23
+ export * from "./prompts/AgenticaCancelPrompt";
24
+ export * from "./prompts/AgenticaDescribePrompt";
25
+ export * from "./prompts/AgenticaExecutePrompt";
26
+ export * from "./prompts/AgenticaSelectPrompt";
27
+ export * from "./prompts/AgenticaTextPrompt";
28
+
29
+ // EVENTS
30
+ export * from "./events/AgenticaEvent";
31
+ export * from "./events/AgenticaCallEvent";
32
+ export * from "./events/AgenticaCancelEvent";
33
+ export * from "./events/AgenticaDescribeEvent";
34
+ export * from "./events/AgenticaEventSource";
35
+ export * from "./events/AgenticaExecuteEvent";
36
+ export * from "./events/AgenticaRequestEvent";
37
+ export * from "./events/AgenticaResponseEvent";
38
+ export * from "./events/AgenticaSelectEvent";
39
+ export * from "./events/AgenticaTextEvent";
16
40
 
17
41
  // FACADE CLASS
18
42
  export * from "./functional/createHttpLlmApplication";
@@ -1,16 +1,16 @@
1
1
  import { ILlmSchema } from "@samchon/openapi";
2
2
 
3
+ import { AgenticaOperation } from "../context/AgenticaOperation";
4
+ import { AgenticaOperationCollection } from "../context/AgenticaOperationCollection";
3
5
  import { IAgenticaConfig } from "../structures/IAgenticaConfig";
4
6
  import { IAgenticaController } from "../structures/IAgenticaController";
5
- import { IAgenticaOperation } from "../structures/IAgenticaOperation";
6
- import { IAgenticaOperationCollection } from "../structures/IAgenticaOperationCollection";
7
7
  import { __map_take } from "./__map_take";
8
8
 
9
9
  export namespace AgenticaOperationComposer {
10
10
  export const compose = <Model extends ILlmSchema.Model>(props: {
11
11
  controllers: IAgenticaController<Model>[];
12
12
  config?: IAgenticaConfig<Model> | undefined;
13
- }): IAgenticaOperationCollection<Model> => {
13
+ }): AgenticaOperationCollection<Model> => {
14
14
  const unique: boolean =
15
15
  props.controllers.length === 1 ||
16
16
  (() => {
@@ -24,7 +24,7 @@ export namespace AgenticaOperationComposer {
24
24
  const naming = (func: string, ci: number) =>
25
25
  unique ? func : `_${ci}_${func}`;
26
26
 
27
- const array: IAgenticaOperation<Model>[] = props.controllers
27
+ const array: AgenticaOperation<Model>[] = props.controllers
28
28
  .map((controller, ci) =>
29
29
  controller.protocol === "http"
30
30
  ? controller.application.functions.map(
@@ -34,7 +34,13 @@ export namespace AgenticaOperationComposer {
34
34
  controller: controller,
35
35
  function: func,
36
36
  name: naming(func.name, ci),
37
- }) satisfies IAgenticaOperation.IHttp<Model>,
37
+ toJSON: () => ({
38
+ protocol: "http",
39
+ controller: controller.name,
40
+ function: func.name,
41
+ name: naming(func.name, ci),
42
+ }),
43
+ }) satisfies AgenticaOperation.Http<Model>,
38
44
  )
39
45
  : controller.application.functions.map(
40
46
  (func) =>
@@ -43,11 +49,17 @@ export namespace AgenticaOperationComposer {
43
49
  controller,
44
50
  function: func,
45
51
  name: naming(func.name, ci),
46
- }) satisfies IAgenticaOperation.IClass<Model>,
52
+ toJSON: () => ({
53
+ protocol: "class",
54
+ controller: controller.name,
55
+ function: func.name,
56
+ name: naming(func.name, ci),
57
+ }),
58
+ }) satisfies AgenticaOperation.Class<Model>,
47
59
  ),
48
60
  )
49
61
  .flat();
50
- const divided: IAgenticaOperation<Model>[][] | undefined =
62
+ const divided: AgenticaOperation<Model>[][] | undefined =
51
63
  !!props.config?.capacity && array.length > props.config.capacity
52
64
  ? divideOperations({
53
65
  array,
@@ -55,11 +67,8 @@ export namespace AgenticaOperationComposer {
55
67
  })
56
68
  : undefined;
57
69
 
58
- const flat: Map<string, IAgenticaOperation<Model>> = new Map();
59
- const group: Map<
60
- string,
61
- Map<string, IAgenticaOperation<Model>>
62
- > = new Map();
70
+ const flat: Map<string, AgenticaOperation<Model>> = new Map();
71
+ const group: Map<string, Map<string, AgenticaOperation<Model>>> = new Map();
63
72
  for (const item of array) {
64
73
  flat.set(item.name, item);
65
74
  __map_take(group, item.controller.name, () => new Map()).set(
@@ -76,12 +85,12 @@ export namespace AgenticaOperationComposer {
76
85
  };
77
86
 
78
87
  const divideOperations = <Model extends ILlmSchema.Model>(props: {
79
- array: IAgenticaOperation<Model>[];
88
+ array: AgenticaOperation<Model>[];
80
89
  capacity: number;
81
- }): IAgenticaOperation<Model>[][] => {
90
+ }): AgenticaOperation<Model>[][] => {
82
91
  const size: number = Math.ceil(props.array.length / props.capacity);
83
92
  const capacity: number = Math.ceil(props.array.length / size);
84
- const replica: IAgenticaOperation<Model>[] = props.array.slice();
93
+ const replica: AgenticaOperation<Model>[] = props.array.slice();
85
94
  return new Array(size).fill(0).map(() => replica.splice(0, capacity));
86
95
  };
87
96
  }
@@ -0,0 +1,5 @@
1
+ export namespace ByteArrayUtil {
2
+ export const toUtf8 = (byteArray: Uint8Array): string => {
3
+ return new TextDecoder().decode(byteArray);
4
+ };
5
+ }
@@ -0,0 +1,75 @@
1
+ export namespace MPSCUtil {
2
+ export interface Output<T> {
3
+ consumer: ReadableStream<T>;
4
+ produce: (chunk: T) => void;
5
+ close: () => void;
6
+ waitClose: () => Promise<void>;
7
+ done: () => boolean;
8
+ }
9
+
10
+ export const create = <T>(): Output<T> => {
11
+ const queue = new AsyncQueue<T>();
12
+ const consumer = new ReadableStream<T>({
13
+ async pull(controller) {
14
+ const { value, done } = await queue.dequeue();
15
+ if (done) {
16
+ controller.close();
17
+ } else {
18
+ controller.enqueue(value);
19
+ }
20
+ },
21
+ });
22
+ return {
23
+ consumer,
24
+ produce: (chunk: T) => queue.enqueue(chunk),
25
+ close: () => queue.close(),
26
+ done: () => queue.done(),
27
+ waitClose: () => queue.waitClose(),
28
+ };
29
+ };
30
+
31
+ export class AsyncQueue<T> {
32
+ private queue: T[] = [];
33
+ private resolvers: ((value: IteratorResult<T, undefined>) => void)[] = [];
34
+ private closeResolvers: (() => void)[] = [];
35
+
36
+ private closed = false;
37
+
38
+ enqueue(item: T) {
39
+ this.queue.push(item);
40
+ if (this.resolvers.length > 0) {
41
+ this.resolvers.shift()?.({ value: this.queue.shift()!, done: false });
42
+ }
43
+ }
44
+
45
+ async dequeue(): Promise<IteratorResult<T, undefined>> {
46
+ if (this.queue.length > 0) {
47
+ return { value: this.queue.shift()!, done: false };
48
+ }
49
+ if (this.closed) return { value: undefined, done: true };
50
+ return new Promise((resolve) => this.resolvers.push(resolve));
51
+ }
52
+
53
+ done() {
54
+ return this.closed;
55
+ }
56
+
57
+ close() {
58
+ this.closed = true;
59
+ while (this.resolvers.length > 0) {
60
+ this.resolvers.shift()?.({ value: undefined, done: true });
61
+ }
62
+ this.closeResolvers.forEach((resolve) => resolve());
63
+ }
64
+
65
+ waitClose() {
66
+ if (this.closed) {
67
+ return Promise.resolve();
68
+ }
69
+
70
+ return new Promise<void>((resolve) => {
71
+ this.closeResolvers.push(resolve);
72
+ });
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,64 @@
1
+ export namespace StreamUtil {
2
+ export const readAll = async <T>(stream: ReadableStream<T>): Promise<T[]> => {
3
+ const reader = stream.getReader();
4
+ const result: T[] = [];
5
+ while (true) {
6
+ const { done, value } = await reader.read();
7
+ if (done) break;
8
+ result.push(value);
9
+ }
10
+ return result;
11
+ };
12
+
13
+ export const reduce = async <T, R = T>(
14
+ stream: ReadableStream<T>,
15
+ reducer: (acc: T | R, cur: T) => R,
16
+ initial?: R,
17
+ ): Promise<R | null> => {
18
+ const reader = stream.getReader();
19
+
20
+ let acc = (initial ?? null) as R | null | T;
21
+
22
+ while (true) {
23
+ const { done, value } = await reader.read();
24
+ if (done) break;
25
+ if (acc === null) {
26
+ acc = value;
27
+ continue;
28
+ }
29
+
30
+ acc = reducer(acc, value);
31
+ }
32
+
33
+ return acc as R;
34
+ };
35
+
36
+ export const to = <T>(value: T): ReadableStream<T> => {
37
+ const stream = new ReadableStream<T>({
38
+ start: (controller) => {
39
+ controller.enqueue(value);
40
+ controller.close();
41
+ },
42
+ });
43
+
44
+ return stream;
45
+ };
46
+
47
+ export const transform = <T, R>(
48
+ stream: ReadableStream<T>,
49
+ transformer: (value: T) => R,
50
+ ): ReadableStream<R> => {
51
+ const reader = stream.getReader();
52
+
53
+ return new ReadableStream<R>({
54
+ pull: async (controller) => {
55
+ const { done, value } = await reader.read();
56
+ if (!done) {
57
+ controller.enqueue(transformer(value));
58
+ } else {
59
+ controller.close();
60
+ }
61
+ },
62
+ });
63
+ };
64
+ }
@@ -0,0 +1,178 @@
1
+ import OpenAI from "openai";
2
+
3
+ import { AgenticaEventSource } from "../events/AgenticaEventSource";
4
+ import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
5
+ import { IAgenticaOperationSelectionJson } from "./IAgenticaOperationSelectionJson";
6
+ import { IAgenticaPromptJson } from "./IAgenticaPromptJson";
7
+
8
+ /**
9
+ * Nestia A.I. chatbot event.
10
+ *
11
+ * `IAgenticaEventJson` is an union type of all possible events that can
12
+ * be emitted by the A.I. chatbot of the {@link Agentica} class. You
13
+ * can discriminate the subtype by checking the {@link type} property.
14
+ *
15
+ * @author Samchon
16
+ */
17
+ export type IAgenticaEventJson =
18
+ | IAgenticaEventJson.ICall
19
+ | IAgenticaEventJson.ICancel
20
+ | IAgenticaEventJson.IDescribe
21
+ | IAgenticaEventJson.IExecute
22
+ | IAgenticaEventJson.IInitialize
23
+ | IAgenticaEventJson.IRequest
24
+ | IAgenticaEventJson.ISelect
25
+ | IAgenticaEventJson.IText;
26
+ export namespace IAgenticaEventJson {
27
+ export type Type = IAgenticaEventJson["type"];
28
+ export type Mapper = {
29
+ initialize: IInitialize;
30
+ select: ISelect;
31
+ cancel: ICancel;
32
+ call: ICall;
33
+ execute: IExecute;
34
+ describe: IDescribe;
35
+ text: IText;
36
+ request: IRequest;
37
+ };
38
+
39
+ /**
40
+ * Event of initializing the chatbot.
41
+ */
42
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
43
+ export interface IInitialize extends IBase<"initialize"> {}
44
+
45
+ /**
46
+ * Event of selecting a function to call.
47
+ */
48
+ export interface ISelect extends IBase<"select"> {
49
+ selection: IAgenticaOperationSelectionJson;
50
+ }
51
+
52
+ /**
53
+ * Event of canceling a function calling.
54
+ */
55
+ export interface ICancel extends IBase<"cancel"> {
56
+ selection: IAgenticaOperationSelectionJson;
57
+ }
58
+
59
+ /**
60
+ * Event of calling a function.
61
+ */
62
+ export interface ICall extends IBase<"call"> {
63
+ /**
64
+ * ID of the tool calling.
65
+ */
66
+ id: string;
67
+
68
+ /**
69
+ * Target operation to call.
70
+ */
71
+ operation: IAgenticaOperationJson;
72
+
73
+ /**
74
+ * Arguments of the function calling.
75
+ *
76
+ * If you modify this {@link arguments} property, it actually modifies
77
+ * the backend server's request. Therefore, be careful when you're
78
+ * trying to modify this property.
79
+ */
80
+ arguments: Record<string, any>;
81
+ }
82
+
83
+ /**
84
+ * Event of function calling execution.
85
+ */
86
+ export interface IExecute extends IBase<"execute"> {
87
+ /**
88
+ * ID of the tool calling.
89
+ */
90
+ id: string;
91
+
92
+ /**
93
+ * Target operation had called.
94
+ */
95
+ operation: IAgenticaOperationJson;
96
+
97
+ /**
98
+ * Arguments of the function calling.
99
+ */
100
+ arguments: object;
101
+
102
+ /**
103
+ * Return value.
104
+ */
105
+ value: any;
106
+ }
107
+
108
+ /**
109
+ * Event of description.
110
+ *
111
+ * Event describing return values of LLM function callings.
112
+ */
113
+ export interface IDescribe extends IBase<"describe"> {
114
+ /**
115
+ * Executions of the LLM function calling.
116
+ *
117
+ * This prompt describes the return value of them.
118
+ */
119
+ executes: IAgenticaPromptJson.IExecute[];
120
+
121
+ /**
122
+ * Description text.
123
+ */
124
+ text: string;
125
+
126
+ /**
127
+ * Whether the streaming is completed or not.
128
+ */
129
+ done: boolean;
130
+ }
131
+
132
+ /**
133
+ * Event of text message.
134
+ */
135
+ export interface IText extends IBase<"text"> {
136
+ /**
137
+ * Role of the orator.
138
+ */
139
+ role: "assistant" | "user";
140
+
141
+ /**
142
+ * Conversation text.
143
+ */
144
+ text: string;
145
+
146
+ /**
147
+ * Whether the streaming is completed or not.
148
+ */
149
+ done: boolean;
150
+ }
151
+
152
+ /**
153
+ * Request event of LLM vendor API.
154
+ */
155
+ export interface IRequest extends IBase<"request"> {
156
+ /**
157
+ * The source agent of the request.
158
+ */
159
+ source: AgenticaEventSource;
160
+
161
+ /**
162
+ * Request body.
163
+ */
164
+ body: OpenAI.ChatCompletionCreateParamsStreaming;
165
+
166
+ /**
167
+ * Options for the request.
168
+ */
169
+ options?: OpenAI.RequestOptions | undefined;
170
+ }
171
+
172
+ interface IBase<Type extends string> {
173
+ /**
174
+ * Discriminator type.
175
+ */
176
+ type: Type;
177
+ }
178
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Operation information in the Agentica Agent.
3
+ *
4
+ * `IAgenticaOperation` is a type represents an operation that would
5
+ * be selected by the A.I. chatbot of {@link Agentica} class to
6
+ * perform the LLM (Large Language Model) function calling.
7
+ *
8
+ * Also, it is an union type that is discriminated by the {@link protocol}
9
+ * property. If the protocol value is `http`, it means that the HTTP API
10
+ * operation would be called by the A.I. chatbot. Otherwise, if the protocol
11
+ * value is `class`, it means that the operation has come from a
12
+ * TypeScript class.
13
+ *
14
+ * @author Samchon
15
+ */
16
+ export interface IAgenticaOperationJson {
17
+ /**
18
+ * Protocol discriminator.
19
+ */
20
+ protocol: "class" | "http";
21
+
22
+ /**
23
+ * Belonged controller of the target function.
24
+ */
25
+ controller: string;
26
+
27
+ /**
28
+ * Target function to call.
29
+ */
30
+ function: string;
31
+
32
+ /**
33
+ * Identifier name.
34
+ */
35
+ name: string;
36
+ }
@@ -0,0 +1,19 @@
1
+ import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
2
+
3
+ /**
4
+ * Nestia agent operation selection.
5
+ *
6
+ * `IAgenticaOperationSelection` is a type represents an operation
7
+ * which has been selected by the A.I. chatbot of {@link Agentica}
8
+ * class for the LLM (Large Language Model) function calling with
9
+ * detailed {@link reason} of the selection (or cancellation).
10
+ *
11
+ * Also, `IAgenticaOperationSelection` is an union type that can
12
+ * specify a subtype by checking the {@link protocol} property.
13
+ *
14
+ * @author Samchon
15
+ */
16
+ export interface IAgenticaOperationSelectionJson {
17
+ reason: string;
18
+ operation: IAgenticaOperationJson;
19
+ }