@agentica/core 0.12.21 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +50 -47
  2. package/lib/Agentica.d.ts +9 -9
  3. package/lib/Agentica.js +23 -16
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/chatgpt/ChatGptAgent.d.ts +7 -7
  6. package/lib/chatgpt/ChatGptAgent.js +17 -12
  7. package/lib/chatgpt/ChatGptAgent.js.map +1 -1
  8. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +7 -6
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.js +120 -81
  10. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
  11. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +9 -7
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +177 -145
  13. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
  14. package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +13 -8
  15. package/lib/chatgpt/ChatGptCompletionMessageUtil.js +499 -489
  16. package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -1
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +7 -6
  18. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +33 -32
  19. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
  20. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +7 -6
  21. package/lib/chatgpt/ChatGptHistoryDecoder.js +60 -56
  22. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +7 -6
  24. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +121 -116
  25. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +20 -5
  27. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +183 -150
  28. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  29. package/lib/chatgpt/ChatGptUsageAggregator.d.ts +9 -6
  30. package/lib/chatgpt/ChatGptUsageAggregator.js +46 -41
  31. package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -1
  32. package/lib/context/AgenticaCancelPrompt.d.ts +3 -3
  33. package/lib/context/AgenticaCancelPrompt.js +1 -1
  34. package/lib/context/AgenticaCancelPrompt.js.map +1 -1
  35. package/lib/context/AgenticaClassOperation.d.ts +1 -1
  36. package/lib/context/AgenticaClassOperation.js.map +1 -1
  37. package/lib/context/AgenticaContext.d.ts +9 -9
  38. package/lib/context/AgenticaHttpOperation.d.ts +1 -1
  39. package/lib/context/AgenticaHttpOperation.js.map +1 -1
  40. package/lib/context/AgenticaOperation.d.ts +4 -4
  41. package/lib/context/AgenticaOperationBase.d.ts +1 -1
  42. package/lib/context/AgenticaOperationCollection.d.ts +2 -2
  43. package/lib/context/AgenticaOperationSelection.d.ts +3 -3
  44. package/lib/context/AgenticaTokenUsage.d.ts +1 -1
  45. package/lib/context/AgenticaTokenUsage.js.map +1 -1
  46. package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +11 -10
  47. package/lib/context/internal/AgenticaTokenUsageAggregator.js +42 -43
  48. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -1
  49. package/lib/context/internal/__IChatCancelFunctionsApplication.d.ts +2 -2
  50. package/lib/context/internal/__IChatInitialApplication.d.ts +2 -2
  51. package/lib/context/internal/__IChatSelectFunctionsApplication.d.ts +2 -2
  52. package/lib/events/AgenticaCallEvent.d.ts +3 -3
  53. package/lib/events/AgenticaCancelEvent.d.ts +3 -3
  54. package/lib/events/AgenticaDescribeEvent.d.ts +3 -3
  55. package/lib/events/AgenticaDescribeEvent.js +13 -2
  56. package/lib/events/AgenticaDescribeEvent.js.map +1 -1
  57. package/lib/events/AgenticaEvent.d.ts +12 -12
  58. package/lib/events/AgenticaExecuteEvent.d.ts +7 -7
  59. package/lib/events/AgenticaInitializeEvent.d.ts +1 -1
  60. package/lib/events/AgenticaRequestEvent.d.ts +3 -3
  61. package/lib/events/AgenticaRequestEvent.js.map +1 -1
  62. package/lib/events/AgenticaResponseEvent.d.ts +2 -2
  63. package/lib/events/AgenticaResponseEvent.js.map +1 -1
  64. package/lib/events/AgenticaSelectEvent.d.ts +3 -3
  65. package/lib/events/AgenticaSelectEvent.js.map +1 -1
  66. package/lib/events/AgenticaTextEvent.d.ts +1 -1
  67. package/lib/events/AgenticaTextEvent.js +12 -1
  68. package/lib/events/AgenticaTextEvent.js.map +1 -1
  69. package/lib/functional/assertHttpLlmApplication.d.ts +4 -3
  70. package/lib/functional/assertHttpLlmApplication.js +9339 -9338
  71. package/lib/functional/assertHttpLlmApplication.js.map +1 -1
  72. package/lib/functional/validateHttpLlmApplication.d.ts +5 -4
  73. package/lib/functional/validateHttpLlmApplication.js +19 -19
  74. package/lib/functional/validateHttpLlmApplication.js.map +1 -1
  75. package/lib/index.d.ts +19 -19
  76. package/lib/index.js +21 -21
  77. package/lib/index.js.map +1 -1
  78. package/lib/index.mjs +19210 -18997
  79. package/lib/index.mjs.map +1 -1
  80. package/lib/internal/AgenticaDefaultPrompt.d.ts +6 -5
  81. package/lib/internal/AgenticaDefaultPrompt.js +39 -19
  82. package/lib/internal/AgenticaDefaultPrompt.js.map +1 -1
  83. package/lib/internal/AgenticaOperationComposer.d.ts +11 -10
  84. package/lib/internal/AgenticaOperationComposer.js +60 -59
  85. package/lib/internal/AgenticaOperationComposer.js.map +1 -1
  86. package/lib/internal/AgenticaSystemPrompt.d.ts +8 -8
  87. package/lib/internal/AgenticaSystemPrompt.js +9 -9
  88. package/lib/internal/AgenticaSystemPrompt.js.map +1 -1
  89. package/lib/internal/AsyncQueue.d.ts +15 -0
  90. package/lib/internal/AsyncQueue.js +82 -0
  91. package/lib/internal/AsyncQueue.js.map +1 -0
  92. package/lib/internal/ByteArrayUtil.d.ts +1 -1
  93. package/lib/internal/ByteArrayUtil.js +3 -2
  94. package/lib/internal/ByteArrayUtil.js.map +1 -1
  95. package/lib/internal/MPSC.d.ts +10 -0
  96. package/lib/internal/MPSC.js +49 -0
  97. package/lib/internal/MPSC.js.map +1 -0
  98. package/lib/internal/Singleton.js +6 -5
  99. package/lib/internal/Singleton.js.map +1 -1
  100. package/lib/internal/StreamUtil.d.ts +16 -6
  101. package/lib/internal/StreamUtil.js +43 -29
  102. package/lib/internal/StreamUtil.js.map +1 -1
  103. package/lib/internal/__map_take.js +5 -5
  104. package/lib/internal/__map_take.js.map +1 -1
  105. package/lib/json/IAgenticaEventJson.d.ts +7 -7
  106. package/lib/json/IAgenticaOperationSelectionJson.d.ts +1 -1
  107. package/lib/json/IAgenticaPromptJson.d.ts +2 -2
  108. package/lib/prompts/AgenticaCancelPrompt.d.ts +3 -3
  109. package/lib/prompts/AgenticaCancelPrompt.js +1 -1
  110. package/lib/prompts/AgenticaCancelPrompt.js.map +1 -1
  111. package/lib/prompts/AgenticaDescribePrompt.d.ts +3 -3
  112. package/lib/prompts/AgenticaDescribePrompt.js +1 -1
  113. package/lib/prompts/AgenticaDescribePrompt.js.map +1 -1
  114. package/lib/prompts/AgenticaExecutePrompt.d.ts +5 -5
  115. package/lib/prompts/AgenticaExecutePrompt.js.map +1 -1
  116. package/lib/prompts/AgenticaPrompt.d.ts +6 -6
  117. package/lib/prompts/AgenticaSelectPrompt.d.ts +3 -3
  118. package/lib/prompts/AgenticaSelectPrompt.js +1 -1
  119. package/lib/prompts/AgenticaSelectPrompt.js.map +1 -1
  120. package/lib/prompts/AgenticaTextPrompt.d.ts +1 -1
  121. package/lib/structures/IAgenticaConfig.d.ts +5 -5
  122. package/lib/structures/IAgenticaController.d.ts +1 -1
  123. package/lib/structures/IAgenticaExecutor.d.ts +4 -4
  124. package/lib/structures/IAgenticaProps.d.ts +5 -5
  125. package/lib/structures/IAgenticaSystemPrompt.d.ts +4 -4
  126. package/lib/structures/IAgenticaVendor.d.ts +1 -1
  127. package/lib/transformers/AgenticaEventTransformer.d.ts +55 -37
  128. package/lib/transformers/AgenticaEventTransformer.js +105 -58
  129. package/lib/transformers/AgenticaEventTransformer.js.map +1 -1
  130. package/lib/transformers/AgenticaPromptTransformer.d.ts +35 -29
  131. package/lib/transformers/AgenticaPromptTransformer.js +74 -45
  132. package/lib/transformers/AgenticaPromptTransformer.js.map +1 -1
  133. package/package.json +20 -23
  134. package/prompts/cancel.md +1 -1
  135. package/prompts/common.md +1 -1
  136. package/prompts/describe.md +1 -1
  137. package/prompts/execute.md +1 -1
  138. package/prompts/initialize.md +1 -1
  139. package/prompts/select.md +1 -1
  140. package/src/Agentica.ts +41 -31
  141. package/src/chatgpt/ChatGptAgent.ts +63 -57
  142. package/src/chatgpt/ChatGptCallFunctionAgent.ts +361 -337
  143. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +188 -164
  144. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +136 -137
  145. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +105 -101
  146. package/src/chatgpt/ChatGptHistoryDecoder.ts +69 -65
  147. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +128 -123
  148. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +215 -189
  149. package/src/chatgpt/ChatGptUsageAggregator.ts +49 -54
  150. package/src/context/AgenticaCancelPrompt.ts +4 -4
  151. package/src/context/AgenticaClassOperation.ts +5 -5
  152. package/src/context/AgenticaContext.ts +15 -15
  153. package/src/context/AgenticaHttpOperation.ts +5 -5
  154. package/src/context/AgenticaOperation.ts +4 -4
  155. package/src/context/AgenticaOperationBase.ts +1 -1
  156. package/src/context/AgenticaOperationCollection.ts +2 -2
  157. package/src/context/AgenticaOperationSelection.ts +3 -3
  158. package/src/context/AgenticaTokenUsage.ts +4 -3
  159. package/src/context/internal/AgenticaTokenUsageAggregator.ts +59 -59
  160. package/src/context/internal/__IChatCancelFunctionsApplication.ts +2 -2
  161. package/src/context/internal/__IChatInitialApplication.ts +2 -2
  162. package/src/context/internal/__IChatSelectFunctionsApplication.ts +2 -2
  163. package/src/events/AgenticaCallEvent.ts +3 -3
  164. package/src/events/AgenticaCancelEvent.ts +3 -3
  165. package/src/events/AgenticaDescribeEvent.ts +5 -5
  166. package/src/events/AgenticaEvent.ts +12 -12
  167. package/src/events/AgenticaExecuteEvent.ts +7 -7
  168. package/src/events/AgenticaInitializeEvent.ts +1 -1
  169. package/src/events/AgenticaRequestEvent.ts +3 -3
  170. package/src/events/AgenticaResponseEvent.ts +2 -2
  171. package/src/events/AgenticaSelectEvent.ts +4 -4
  172. package/src/events/AgenticaTextEvent.ts +2 -2
  173. package/src/functional/assertHttpLlmApplication.ts +9 -6
  174. package/src/functional/validateHttpLlmApplication.ts +14 -8
  175. package/src/index.ts +24 -24
  176. package/src/internal/AgenticaDefaultPrompt.ts +42 -28
  177. package/src/internal/AgenticaOperationComposer.ts +63 -61
  178. package/src/internal/AgenticaSystemPrompt.ts +15 -14
  179. package/src/internal/AsyncQueue.ts +67 -0
  180. package/src/internal/ByteArrayUtil.ts +2 -2
  181. package/src/internal/MPSC.ts +40 -0
  182. package/src/internal/Singleton.ts +8 -6
  183. package/src/internal/StreamUtil.ts +62 -53
  184. package/src/internal/__map_take.ts +5 -7
  185. package/src/json/IAgenticaEventJson.ts +7 -8
  186. package/src/json/IAgenticaOperationSelectionJson.ts +1 -1
  187. package/src/json/IAgenticaPromptJson.ts +2 -2
  188. package/src/prompts/AgenticaCancelPrompt.ts +4 -4
  189. package/src/prompts/AgenticaDescribePrompt.ts +4 -4
  190. package/src/prompts/AgenticaExecutePrompt.ts +6 -5
  191. package/src/prompts/AgenticaPrompt.ts +6 -6
  192. package/src/prompts/AgenticaSelectPrompt.ts +4 -4
  193. package/src/prompts/AgenticaTextPrompt.ts +1 -1
  194. package/src/structures/IAgenticaConfig.ts +5 -5
  195. package/src/structures/IAgenticaController.ts +14 -14
  196. package/src/structures/IAgenticaExecutor.ts +4 -4
  197. package/src/structures/IAgenticaProps.ts +5 -5
  198. package/src/structures/IAgenticaSystemPrompt.ts +4 -4
  199. package/src/structures/IAgenticaVendor.ts +1 -1
  200. package/src/transformers/AgenticaEventTransformer.ts +169 -136
  201. package/src/transformers/AgenticaPromptTransformer.ts +125 -103
  202. package/lib/internal/MPSCUtil.d.ts +0 -35
  203. package/lib/internal/MPSCUtil.js +0 -105
  204. package/lib/internal/MPSCUtil.js.map +0 -1
  205. package/src/internal/MPSCUtil.ts +0 -111
package/src/index.ts CHANGED
@@ -1,17 +1,4 @@
1
- // STRUCTURES
2
- export * from "./structures/IAgenticaConfig";
3
- export * from "./structures/IAgenticaController";
4
- export * from "./structures/IAgenticaExecutor";
5
- export * from "./structures/IAgenticaProps";
6
- export * from "./structures/IAgenticaVendor";
7
- export * from "./structures/IAgenticaSystemPrompt";
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
-
1
+ export * from "./Agentica";
15
2
  // CONTEXT
16
3
  export * from "./context/AgenticaContext";
17
4
  export * from "./context/AgenticaOperation";
@@ -19,19 +6,13 @@ export * from "./context/AgenticaOperationCollection";
19
6
  export * from "./context/AgenticaOperationSelection";
20
7
  export * from "./context/AgenticaTokenUsage";
21
8
 
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
9
  export * from "./events/AgenticaCallEvent";
32
10
  export * from "./events/AgenticaCancelEvent";
33
11
  export * from "./events/AgenticaDescribeEvent";
12
+ // EVENTS
13
+ export * from "./events/AgenticaEvent";
34
14
  export * from "./events/AgenticaEventSource";
15
+
35
16
  export * from "./events/AgenticaExecuteEvent";
36
17
  export * from "./events/AgenticaRequestEvent";
37
18
  export * from "./events/AgenticaResponseEvent";
@@ -41,4 +22,23 @@ export * from "./events/AgenticaTextEvent";
41
22
  // FACADE CLASS
42
23
  export * from "./functional/assertHttpLlmApplication";
43
24
  export * from "./functional/validateHttpLlmApplication";
44
- export * from "./Agentica";
25
+ export * from "./json/IAgenticaEventJson";
26
+ export * from "./json/IAgenticaOperationJson";
27
+ export * from "./json/IAgenticaOperationSelectionJson";
28
+ export * from "./json/IAgenticaPromptJson";
29
+
30
+ export * from "./json/IAgenticaTokenUsageJson";
31
+ export * from "./prompts/AgenticaCancelPrompt";
32
+ export * from "./prompts/AgenticaDescribePrompt";
33
+ export * from "./prompts/AgenticaExecutePrompt";
34
+ export * from "./prompts/AgenticaPrompt";
35
+ export * from "./prompts/AgenticaSelectPrompt";
36
+ export * from "./prompts/AgenticaTextPrompt";
37
+ // STRUCTURES
38
+ export * from "./structures/IAgenticaConfig";
39
+ export * from "./structures/IAgenticaController";
40
+ export * from "./structures/IAgenticaExecutor";
41
+
42
+ export * from "./structures/IAgenticaProps";
43
+ export * from "./structures/IAgenticaSystemPrompt";
44
+ export * from "./structures/IAgenticaVendor";
@@ -1,28 +1,34 @@
1
- import { ILlmSchema } from "@samchon/openapi";
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import { IAgenticaConfig } from "../structures/IAgenticaConfig";
3
+ import type { IAgenticaConfig } from "../structures/IAgenticaConfig";
4
4
  import { AgenticaSystemPrompt } from "./AgenticaSystemPrompt";
5
5
  import { Singleton } from "./Singleton";
6
6
 
7
- export namespace AgenticaDefaultPrompt {
8
- export const write = <Model extends ILlmSchema.Model>(
9
- config?: IAgenticaConfig<Model>,
10
- ): string => {
11
- if (config?.systemPrompt?.common)
12
- return config?.systemPrompt?.common(config);
13
-
14
- const locale: string = config?.locale ?? getLocale.get();
15
- const timezone: string = config?.timezone ?? getTimezone.get();
16
-
17
- return AgenticaSystemPrompt.COMMON.replace("${locale}", locale).replace(
18
- "${timezone}",
19
- timezone,
20
- );
21
- };
22
- }
7
+ /**
8
+ * @TODO maybe this code will rewrite
9
+ */
10
+ const isNode = new Singleton(() => {
11
+ const isObject = (obj: any) => typeof obj === "object" && obj !== null;
12
+ return (
13
+ // eslint-disable-next-line no-restricted-globals
14
+ typeof global === "object"
15
+ // eslint-disable-next-line no-restricted-globals
16
+ && isObject(global)
17
+ // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
18
+ && isObject(global.process)
19
+ // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
20
+ && isObject(global.process.versions)
21
+ // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
22
+ && typeof global.process.versions.node !== "undefined"
23
+ );
24
+ });
23
25
 
26
+ /**
27
+ * @TODO maybe replace `process` property for lint pass
28
+ */
24
29
  const getLocale = new Singleton(() =>
25
30
  isNode.get()
31
+ // eslint-disable-next-line node/prefer-global/process
26
32
  ? (process.env.LANG?.split(".")[0] ?? "en-US")
27
33
  : navigator.language,
28
34
  );
@@ -31,13 +37,21 @@ const getTimezone = new Singleton(
31
37
  () => Intl.DateTimeFormat().resolvedOptions().timeZone,
32
38
  );
33
39
 
34
- const isNode = new Singleton(() => {
35
- const isObject = (obj: any) => typeof obj === "object" && obj !== null;
36
- return (
37
- typeof global === "object" &&
38
- isObject(global) &&
39
- isObject(global.process) &&
40
- isObject(global.process.versions) &&
41
- typeof global.process.versions.node !== "undefined"
42
- );
43
- });
40
+ export function write<Model extends ILlmSchema.Model>(config?: IAgenticaConfig<Model>): string {
41
+ if (config?.systemPrompt?.common !== undefined) {
42
+ return config?.systemPrompt?.common(config);
43
+ }
44
+
45
+ const locale: string = config?.locale ?? getLocale.get();
46
+ const timezone: string = config?.timezone ?? getTimezone.get();
47
+
48
+ return AgenticaSystemPrompt.COMMON
49
+ // intended code
50
+ // eslint-disable-next-line no-template-curly-in-string
51
+ .replace("${locale}", locale)
52
+ // eslint-disable-next-line no-template-curly-in-string
53
+ .replace("${timezone}", timezone);
54
+ }
55
+ export const AgenticaDefaultPrompt = {
56
+ write,
57
+ };
@@ -1,37 +1,36 @@
1
- import { ILlmSchema } from "@samchon/openapi";
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import { AgenticaOperation } from "../context/AgenticaOperation";
4
- import { AgenticaOperationCollection } from "../context/AgenticaOperationCollection";
5
- import { IAgenticaConfig } from "../structures/IAgenticaConfig";
6
- import { IAgenticaController } from "../structures/IAgenticaController";
3
+ import type { AgenticaOperation } from "../context/AgenticaOperation";
4
+ import type { AgenticaOperationCollection } from "../context/AgenticaOperationCollection";
5
+ import type { IAgenticaConfig } from "../structures/IAgenticaConfig";
6
+ import type { IAgenticaController } from "../structures/IAgenticaController";
7
7
  import { __map_take } from "./__map_take";
8
8
 
9
- export namespace AgenticaOperationComposer {
10
- export const compose = <Model extends ILlmSchema.Model>(props: {
11
- controllers: IAgenticaController<Model>[];
12
- config?: IAgenticaConfig<Model> | undefined;
13
- }): AgenticaOperationCollection<Model> => {
14
- const unique: boolean =
15
- props.controllers.length === 1 ||
16
- (() => {
17
- const names: string[] = props.controllers
18
- .map((controller) =>
19
- controller.application.functions.map((func) => func.name),
20
- )
21
- .flat();
22
- return new Set(names).size === names.length;
23
- })();
24
- const naming = (func: string, ci: number) =>
25
- unique ? func : `_${ci}_${func}`;
9
+ export function compose<Model extends ILlmSchema.Model>(props: {
10
+ controllers: IAgenticaController<Model>[];
11
+ config?: IAgenticaConfig<Model> | undefined;
12
+ }): AgenticaOperationCollection<Model> {
13
+ const unique: boolean
14
+ = props.controllers.length === 1
15
+ || (() => {
16
+ const names: string[] = props.controllers
17
+ .map(controller =>
18
+ controller.application.functions.map(func => func.name),
19
+ )
20
+ .flat();
21
+ return new Set(names).size === names.length;
22
+ })();
23
+ const naming = (func: string, ci: number) =>
24
+ unique ? func : `_${ci}_${func}`;
26
25
 
27
- const array: AgenticaOperation<Model>[] = props.controllers
28
- .map((controller, ci) =>
29
- controller.protocol === "http"
30
- ? controller.application.functions.map(
31
- (func) =>
26
+ const array: AgenticaOperation<Model>[] = props.controllers
27
+ .map((controller, ci) =>
28
+ controller.protocol === "http"
29
+ ? controller.application.functions.map(
30
+ func =>
32
31
  ({
33
32
  protocol: "http",
34
- controller: controller,
33
+ controller,
35
34
  function: func,
36
35
  name: naming(func.name, ci),
37
36
  toJSON: () => ({
@@ -41,9 +40,9 @@ export namespace AgenticaOperationComposer {
41
40
  name: naming(func.name, ci),
42
41
  }),
43
42
  }) satisfies AgenticaOperation.Http<Model>,
44
- )
45
- : controller.application.functions.map(
46
- (func) =>
43
+ )
44
+ : controller.application.functions.map(
45
+ func =>
47
46
  ({
48
47
  protocol: "class",
49
48
  controller,
@@ -56,41 +55,44 @@ export namespace AgenticaOperationComposer {
56
55
  name: naming(func.name, ci),
57
56
  }),
58
57
  }) satisfies AgenticaOperation.Class<Model>,
59
- ),
60
- )
61
- .flat();
62
- const divided: AgenticaOperation<Model>[][] | undefined =
63
- !!props.config?.capacity && array.length > props.config.capacity
64
- ? divideOperations({
58
+ ),
59
+ )
60
+ .flat();
61
+ const divided: AgenticaOperation<Model>[][] | undefined
62
+ = props.config?.capacity !== undefined && array.length > props.config.capacity
63
+ ? divide({
65
64
  array,
66
65
  capacity: props.config.capacity,
67
66
  })
68
67
  : undefined;
69
68
 
70
- const flat: Map<string, AgenticaOperation<Model>> = new Map();
71
- const group: Map<string, Map<string, AgenticaOperation<Model>>> = new Map();
72
- for (const item of array) {
73
- flat.set(item.name, item);
74
- __map_take(group, item.controller.name, () => new Map()).set(
75
- item.name,
76
- item,
77
- );
78
- }
79
- return {
80
- array,
81
- divided,
82
- flat,
83
- group,
84
- };
69
+ const flat: Map<string, AgenticaOperation<Model>> = new Map();
70
+ const group: Map<string, Map<string, AgenticaOperation<Model>>> = new Map();
71
+ for (const item of array) {
72
+ flat.set(item.name, item);
73
+ __map_take(group, item.controller.name, () => new Map()).set(
74
+ item.name,
75
+ item,
76
+ );
77
+ }
78
+ return {
79
+ array,
80
+ divided,
81
+ flat,
82
+ group,
85
83
  };
84
+ }
86
85
 
87
- const divideOperations = <Model extends ILlmSchema.Model>(props: {
88
- array: AgenticaOperation<Model>[];
89
- capacity: number;
90
- }): AgenticaOperation<Model>[][] => {
91
- const size: number = Math.ceil(props.array.length / props.capacity);
92
- const capacity: number = Math.ceil(props.array.length / size);
93
- const replica: AgenticaOperation<Model>[] = props.array.slice();
94
- return new Array(size).fill(0).map(() => replica.splice(0, capacity));
95
- };
86
+ function divide<T>(props: {
87
+ array: T[];
88
+ capacity: number;
89
+ }): T[][] {
90
+ const size: number = Math.ceil(props.array.length / props.capacity);
91
+ const capacity: number = Math.ceil(props.array.length / size);
92
+ const replica: T[] = props.array.slice();
93
+ return Array.from({ length: size }, () => replica.splice(0, capacity));
96
94
  }
95
+
96
+ export const AgenticaOperationComposer = {
97
+ compose,
98
+ };
@@ -1,14 +1,15 @@
1
- export namespace AgenticaSystemPrompt {
2
- export const CANCEL =
3
- "You are a helpful assistant for cancelling functions which are prepared to call.\n\nUse the supplied tools to select some functions to cancel of `getApiFunctions()` returned.\n\nIf you can't find any proper function to select, don't talk, don't do anything.";
4
- export const COMMON =
5
- "At first, the user's language locale code is \"${locale}\". When you are conversating with the user or describing the function calling result, consider it and always translate to the target locale language. Never conversate with different locale language text with the user.\n\nAt second, the user's timezone is \"${timezone}\", and ISO datetime is ${datetime}. When you are conversating with the user, consider current time and user belonged timezone.";
6
- export const DESCRIBE =
7
- "You are a helpful assistant describing return values of function calls.\n\nAbove messages are the list of function call histories. When describing the return values, please do not too much shortly summarize them. Instead, provide detailed descriptions as much as.\n\nAlso, its content format must be markdown. If required, utilize the mermaid syntax for drawing some diagrams. When image contents are, just put them through the markdown image syntax.\n\nAt last, if user's language locale code is different with your description, please translate it to the user's language.";
8
- export const EXECUTE =
9
- "You are a helpful assistant for tool calling.\n\nUse the supplied tools to assist the user.\n\nIf previous messages are not enough to compose the arguments, you can ask the user to write more information. By the way, when asking the user to write more information, make the text concise and clear.\n\nFor reference, in the \"tool\" role message content, the `function` property means metadata of the API operation. In other words, it is the function schema describing its purpose, parameters and return value types. And then the `data` property is the return value from the target function calling.";
10
- export const INITIALIZE =
11
- "You are a helpful assistant.\n\nUse the supplied tools to assist the user.";
12
- export const SELECT =
13
- "You are a helpful assistant for selecting functions to call.\n\nUse the supplied tools to select some functions of `getApiFunctions()` returned.\n\nWhen selecting functions to call, pay attention to the relationship between functions. In particular, check the prerequisites between each function.\n\nIf you can't find any proper function to select, just type your own message. By the way, when typing your own message, please consider the user's language locale code. If your message is different with the user's language, please translate it to the user's.";
14
- }
1
+ /* eslint-disable no-template-curly-in-string */
2
+ export const AgenticaSystemPrompt = {
3
+ CANCEL:
4
+ "You are a helpful assistant for cancelling functions which are prepared to call.\n\nUse the supplied tools to select some functions to cancel of `getApiFunctions()` returned.\n\nIf you can't find any proper function to select, don't talk, don't do anything.",
5
+ COMMON:
6
+ "At first, the user's language locale code is \"${locale}\". When you are conversating with the user or describing the function calling result, consider it and always translate to the target locale language. Never conversate with different locale language text with the user.\n\nAt second, the user's timezone is \"${timezone}\", and ISO datetime is ${datetime}. When you are conversating with the user, consider current time and user belonged timezone.",
7
+ DESCRIBE:
8
+ "You are a helpful assistant describing return values of function calls.\n\nAbove messages are the list of function call histories. When describing the return values, please do not too much shortly summarize them. Instead, provide detailed descriptions as much as.\n\nAlso, its content format must be markdown. If required, utilize the mermaid syntax for drawing some diagrams. When image contents are, just put them through the markdown image syntax.\n\nAt last, if user's language locale code is different with your description, please translate it to the user's language.",
9
+ EXECUTE:
10
+ "You are a helpful assistant for tool calling.\n\nUse the supplied tools to assist the user.\n\nIf previous messages are not enough to compose the arguments, you can ask the user to write more information. By the way, when asking the user to write more information, make the text concise and clear.\n\nFor reference, in the \"tool\" role message content, the `function` property means metadata of the API operation. In other words, it is the function schema describing its purpose, parameters and return value types. And then the `data` property is the return value from the target function calling.",
11
+ INITIALIZE:
12
+ "You are a helpful assistant.\n\nUse the supplied tools to assist the user.",
13
+ SELECT:
14
+ "You are a helpful assistant for selecting functions to call.\n\nUse the supplied tools to select some functions of `getApiFunctions()` returned.\n\nWhen selecting functions to call, pay attention to the relationship between functions. In particular, check the prerequisites between each function.\n\nIf you can't find any proper function to select, just type your own message. By the way, when typing your own message, please consider the user's language locale code. If your message is different with the user's language, please translate it to the user's.",
15
+ };
@@ -0,0 +1,67 @@
1
+ export class AsyncQueue<T> {
2
+ private queue: T[] = [];
3
+ private resolvers: ((value: IteratorResult<T, undefined>) => void)[] = [];
4
+ private closeResolvers: (() => void)[] = [];
5
+ private emptyResolvers: (() => void)[] = [];
6
+ private closed = false;
7
+
8
+ enqueue(item: T) {
9
+ this.queue.push(item);
10
+ if (this.resolvers.length > 0) {
11
+ this.resolvers.shift()?.({ value: this.queue.shift()!, done: false });
12
+ }
13
+ }
14
+
15
+ async dequeue(): Promise<IteratorResult<T, undefined>> {
16
+ if (this.queue.length > 0) {
17
+ return { value: this.queue.shift()!, done: false };
18
+ }
19
+ if (this.closed) {
20
+ if (this.emptyResolvers.length > 0) {
21
+ this.emptyResolvers.forEach(resolve => resolve());
22
+ this.emptyResolvers = [];
23
+ }
24
+ return { value: undefined, done: true };
25
+ }
26
+ return new Promise(resolve => this.resolvers.push(resolve));
27
+ }
28
+
29
+ isEmpty() {
30
+ return this.queue.length === 0;
31
+ }
32
+
33
+ isClosed() {
34
+ return this.closed;
35
+ }
36
+
37
+ done() {
38
+ return this.isClosed() && this.isEmpty();
39
+ }
40
+
41
+ close() {
42
+ this.closed = true;
43
+ while (this.resolvers.length > 0) {
44
+ this.resolvers.shift()?.({ value: undefined, done: true });
45
+ }
46
+ this.closeResolvers.forEach(resolve => resolve());
47
+ }
48
+
49
+ async waitUntilEmpty() {
50
+ if (this.isEmpty()) {
51
+ return Promise.resolve();
52
+ }
53
+ return new Promise<void>((resolve) => {
54
+ this.emptyResolvers.push(resolve);
55
+ });
56
+ }
57
+
58
+ async waitClosed() {
59
+ if (this.isClosed()) {
60
+ return Promise.resolve();
61
+ }
62
+
63
+ return new Promise<void>((resolve) => {
64
+ this.closeResolvers.push(resolve);
65
+ });
66
+ }
67
+ }
@@ -1,5 +1,5 @@
1
1
  export namespace ByteArrayUtil {
2
- export const toUtf8 = (byteArray: Uint8Array): string => {
2
+ export function toUtf8(byteArray: Uint8Array): string {
3
3
  return new TextDecoder().decode(byteArray);
4
- };
4
+ }
5
5
  }
@@ -0,0 +1,40 @@
1
+ import { AsyncQueue } from "./AsyncQueue";
2
+
3
+ export class MPSC<T> {
4
+ private readonly queue: AsyncQueue<T>;
5
+ public readonly consumer: ReadableStream<T>;
6
+
7
+ public constructor() {
8
+ this.queue = new AsyncQueue<T>();
9
+ this.consumer = new ReadableStream<T>({
10
+ pull: async (controller) => {
11
+ const { value, done } = await this.queue.dequeue();
12
+ if (done === true) {
13
+ controller.close();
14
+ return;
15
+ }
16
+ controller.enqueue(value);
17
+ },
18
+ });
19
+ }
20
+
21
+ produce(chunk: T) {
22
+ this.queue.enqueue(chunk);
23
+ }
24
+
25
+ close() {
26
+ this.queue.close();
27
+ }
28
+
29
+ done() {
30
+ return this.queue.done();
31
+ }
32
+
33
+ async waitClosed() {
34
+ await this.queue.waitClosed();
35
+ }
36
+
37
+ async waitUntilEmpty() {
38
+ await this.queue.waitUntilEmpty();
39
+ }
40
+ }
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @internal
3
+ */
4
+ const NOT_MOUNTED_YET = {};
5
+
1
6
  /**
2
7
  * @internal
3
8
  */
@@ -11,12 +16,9 @@ export class Singleton<T, Args extends any[] = []> {
11
16
  }
12
17
 
13
18
  public get(...args: Args): T {
14
- if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_(...args);
19
+ if (this.value_ === NOT_MOUNTED_YET) {
20
+ this.value_ = this.closure_(...args);
21
+ }
15
22
  return this.value_ as T;
16
23
  }
17
24
  }
18
-
19
- /**
20
- * @internal
21
- */
22
- const NOT_MOUNTED_YET = {};
@@ -1,64 +1,73 @@
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
- };
1
+ /**
2
+ * @module StreamUtil
3
+ *
4
+ * Utility functions for streams.
5
+ */
12
6
 
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();
7
+ async function readAll<T>(stream: ReadableStream<T>): Promise<T[]> {
8
+ const reader = stream.getReader();
9
+ const result: T[] = [];
10
+ while (true) {
11
+ const { done, value } = await reader.read();
12
+ if (done) {
13
+ break;
14
+ }
15
+ result.push(value);
16
+ }
17
+ return result;
18
+ }
19
19
 
20
- let acc = (initial ?? null) as R | null | T;
20
+ async function reduce<T, R = T>(stream: ReadableStream<T>, reducer: (acc: T | R, cur: T) => R, initial?: R): Promise<R | null> {
21
+ const reader = stream.getReader();
21
22
 
22
- while (true) {
23
- const { done, value } = await reader.read();
24
- if (done) break;
25
- if (acc === null) {
26
- acc = value;
27
- continue;
28
- }
23
+ let acc = (initial ?? null) as R | null | T;
29
24
 
30
- acc = reducer(acc, value);
25
+ while (true) {
26
+ const { done, value } = await reader.read();
27
+ if (done) {
28
+ break;
29
+ }
30
+ if (acc === null) {
31
+ acc = value;
32
+ continue;
31
33
  }
32
34
 
33
- return acc as R;
34
- };
35
+ acc = reducer(acc, value);
36
+ }
35
37
 
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
- });
38
+ return acc as R;
39
+ }
40
+
41
+ function to<T>(value: T): ReadableStream<T> {
42
+ const stream = new ReadableStream<T>({
43
+ start: (controller) => {
44
+ controller.enqueue(value);
45
+ controller.close();
46
+ },
47
+ });
43
48
 
44
- return stream;
45
- };
49
+ return stream;
50
+ }
46
51
 
47
- export const transform = <T, R>(
48
- stream: ReadableStream<T>,
49
- transformer: (value: T) => R,
50
- ): ReadableStream<R> => {
51
- const reader = stream.getReader();
52
+ function transform<T, R>(stream: ReadableStream<T>, transformer: (value: T) => R): ReadableStream<R> {
53
+ const reader = stream.getReader();
52
54
 
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
- };
55
+ return new ReadableStream<R>({
56
+ pull: async (controller) => {
57
+ const { done, value } = await reader.read();
58
+ if (!done) {
59
+ controller.enqueue(transformer(value));
60
+ }
61
+ else {
62
+ controller.close();
63
+ }
64
+ },
65
+ });
64
66
  }
67
+
68
+ export const StreamUtil = {
69
+ readAll,
70
+ reduce,
71
+ to,
72
+ transform,
73
+ };
@@ -1,15 +1,13 @@
1
1
  /**
2
2
  * @internal
3
3
  */
4
- export const __map_take = <Key, T>(
5
- dict: Map<Key, T>,
6
- key: Key,
7
- generator: () => T,
8
- ): T => {
4
+ export function __map_take<Key, T>(dict: Map<Key, T>, key: Key, generator: () => T): T {
9
5
  const oldbie: T | undefined = dict.get(key);
10
- if (oldbie) return oldbie;
6
+ if (oldbie !== undefined) {
7
+ return oldbie;
8
+ }
11
9
 
12
10
  const value: T = generator();
13
11
  dict.set(key, value);
14
12
  return value;
15
- };
13
+ }