@agentica/core 0.7.0-dev.20250224

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +464 -0
  3. package/lib/Agentica.d.ts +124 -0
  4. package/lib/Agentica.js +271 -0
  5. package/lib/Agentica.js.map +1 -0
  6. package/lib/chatgpt/ChatGptAgent.d.ts +6 -0
  7. package/lib/chatgpt/ChatGptAgent.js +59 -0
  8. package/lib/chatgpt/ChatGptAgent.js.map +1 -0
  9. package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +5 -0
  10. package/lib/chatgpt/ChatGptCallFunctionAgent.js +362 -0
  11. package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -0
  12. package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +8 -0
  13. package/lib/chatgpt/ChatGptCancelFunctionAgent.js +359 -0
  14. package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -0
  15. package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +5 -0
  16. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -0
  17. package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -0
  18. package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +5 -0
  19. package/lib/chatgpt/ChatGptHistoryDecoder.js +77 -0
  20. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -0
  21. package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +5 -0
  22. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +1883 -0
  23. package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -0
  24. package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +5 -0
  25. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +381 -0
  26. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -0
  27. package/lib/functional/createHttpLlmApplication.d.ts +33 -0
  28. package/lib/functional/createHttpLlmApplication.js +7766 -0
  29. package/lib/functional/createHttpLlmApplication.js.map +1 -0
  30. package/lib/index.d.ts +16 -0
  31. package/lib/index.js +35 -0
  32. package/lib/index.js.map +1 -0
  33. package/lib/index.mjs +10366 -0
  34. package/lib/index.mjs.map +1 -0
  35. package/lib/internal/AgenticaConstant.d.ts +4 -0
  36. package/lib/internal/AgenticaConstant.js +9 -0
  37. package/lib/internal/AgenticaConstant.js.map +1 -0
  38. package/lib/internal/AgenticaCostAggregator.d.ts +5 -0
  39. package/lib/internal/AgenticaCostAggregator.js +30 -0
  40. package/lib/internal/AgenticaCostAggregator.js.map +1 -0
  41. package/lib/internal/AgenticaDefaultPrompt.d.ts +4 -0
  42. package/lib/internal/AgenticaDefaultPrompt.js +32 -0
  43. package/lib/internal/AgenticaDefaultPrompt.js.map +1 -0
  44. package/lib/internal/AgenticaOperationComposer.d.ts +9 -0
  45. package/lib/internal/AgenticaOperationComposer.js +58 -0
  46. package/lib/internal/AgenticaOperationComposer.js.map +1 -0
  47. package/lib/internal/AgenticaPromptFactory.d.ts +6 -0
  48. package/lib/internal/AgenticaPromptFactory.js +9 -0
  49. package/lib/internal/AgenticaPromptFactory.js.map +1 -0
  50. package/lib/internal/AgenticaPromptTransformer.d.ts +9 -0
  51. package/lib/internal/AgenticaPromptTransformer.js +58 -0
  52. package/lib/internal/AgenticaPromptTransformer.js.map +1 -0
  53. package/lib/internal/AgenticaSystemPrompt.d.ts +8 -0
  54. package/lib/internal/AgenticaSystemPrompt.js +13 -0
  55. package/lib/internal/AgenticaSystemPrompt.js.map +1 -0
  56. package/lib/internal/MathUtil.d.ts +3 -0
  57. package/lib/internal/MathUtil.js +8 -0
  58. package/lib/internal/MathUtil.js.map +1 -0
  59. package/lib/internal/Singleton.d.ts +1 -0
  60. package/lib/internal/Singleton.js +23 -0
  61. package/lib/internal/Singleton.js.map +1 -0
  62. package/lib/internal/__map_take.d.ts +1 -0
  63. package/lib/internal/__map_take.js +16 -0
  64. package/lib/internal/__map_take.js.map +1 -0
  65. package/lib/structures/IAgenticaConfig.d.ts +112 -0
  66. package/lib/structures/IAgenticaConfig.js +3 -0
  67. package/lib/structures/IAgenticaConfig.js.map +1 -0
  68. package/lib/structures/IAgenticaContext.d.ts +106 -0
  69. package/lib/structures/IAgenticaContext.js +3 -0
  70. package/lib/structures/IAgenticaContext.js.map +1 -0
  71. package/lib/structures/IAgenticaController.d.ts +110 -0
  72. package/lib/structures/IAgenticaController.js +3 -0
  73. package/lib/structures/IAgenticaController.js.map +1 -0
  74. package/lib/structures/IAgenticaEvent.d.ts +191 -0
  75. package/lib/structures/IAgenticaEvent.js +3 -0
  76. package/lib/structures/IAgenticaEvent.js.map +1 -0
  77. package/lib/structures/IAgenticaExecutor.d.ts +144 -0
  78. package/lib/structures/IAgenticaExecutor.js +3 -0
  79. package/lib/structures/IAgenticaExecutor.js.map +1 -0
  80. package/lib/structures/IAgenticaOperation.d.ts +48 -0
  81. package/lib/structures/IAgenticaOperation.js +3 -0
  82. package/lib/structures/IAgenticaOperation.js.map +1 -0
  83. package/lib/structures/IAgenticaOperationCollection.d.ts +46 -0
  84. package/lib/structures/IAgenticaOperationCollection.js +3 -0
  85. package/lib/structures/IAgenticaOperationCollection.js.map +1 -0
  86. package/lib/structures/IAgenticaOperationSelection.d.ts +51 -0
  87. package/lib/structures/IAgenticaOperationSelection.js +3 -0
  88. package/lib/structures/IAgenticaOperationSelection.js.map +1 -0
  89. package/lib/structures/IAgenticaPrompt.d.ts +139 -0
  90. package/lib/structures/IAgenticaPrompt.js +3 -0
  91. package/lib/structures/IAgenticaPrompt.js.map +1 -0
  92. package/lib/structures/IAgenticaProps.d.ts +59 -0
  93. package/lib/structures/IAgenticaProps.js +3 -0
  94. package/lib/structures/IAgenticaProps.js.map +1 -0
  95. package/lib/structures/IAgenticaProvider.d.ts +41 -0
  96. package/lib/structures/IAgenticaProvider.js +3 -0
  97. package/lib/structures/IAgenticaProvider.js.map +1 -0
  98. package/lib/structures/IAgenticaSystemPrompt.d.ts +116 -0
  99. package/lib/structures/IAgenticaSystemPrompt.js +3 -0
  100. package/lib/structures/IAgenticaSystemPrompt.js.map +1 -0
  101. package/lib/structures/IAgenticaTokenUsage.d.ts +50 -0
  102. package/lib/structures/IAgenticaTokenUsage.js +3 -0
  103. package/lib/structures/IAgenticaTokenUsage.js.map +1 -0
  104. package/lib/structures/internal/__IChatCancelFunctionsApplication.d.ts +22 -0
  105. package/lib/structures/internal/__IChatCancelFunctionsApplication.js +3 -0
  106. package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +1 -0
  107. package/lib/structures/internal/__IChatFunctionReference.d.ts +20 -0
  108. package/lib/structures/internal/__IChatFunctionReference.js +3 -0
  109. package/lib/structures/internal/__IChatFunctionReference.js.map +1 -0
  110. package/lib/structures/internal/__IChatInitialApplication.d.ts +14 -0
  111. package/lib/structures/internal/__IChatInitialApplication.js +3 -0
  112. package/lib/structures/internal/__IChatInitialApplication.js.map +1 -0
  113. package/lib/structures/internal/__IChatSelectFunctionsApplication.d.ts +23 -0
  114. package/lib/structures/internal/__IChatSelectFunctionsApplication.js +3 -0
  115. package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +1 -0
  116. package/lib/typings/AgenticaSource.d.ts +1 -0
  117. package/lib/typings/AgenticaSource.js +3 -0
  118. package/lib/typings/AgenticaSource.js.map +1 -0
  119. package/package.json +74 -0
  120. package/prompts/cancel.md +5 -0
  121. package/prompts/common.md +3 -0
  122. package/prompts/describe.md +7 -0
  123. package/prompts/execute.md +7 -0
  124. package/prompts/initialize.md +3 -0
  125. package/prompts/select.md +7 -0
  126. package/src/Agentica.ts +322 -0
  127. package/src/chatgpt/ChatGptAgent.ts +71 -0
  128. package/src/chatgpt/ChatGptCallFunctionAgent.ts +445 -0
  129. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +283 -0
  130. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +51 -0
  131. package/src/chatgpt/ChatGptHistoryDecoder.ts +86 -0
  132. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -0
  133. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +316 -0
  134. package/src/functional/createHttpLlmApplication.ts +63 -0
  135. package/src/index.ts +19 -0
  136. package/src/internal/AgenticaConstant.ts +4 -0
  137. package/src/internal/AgenticaCostAggregator.ts +35 -0
  138. package/src/internal/AgenticaDefaultPrompt.ts +39 -0
  139. package/src/internal/AgenticaOperationComposer.ts +82 -0
  140. package/src/internal/AgenticaPromptFactory.ts +30 -0
  141. package/src/internal/AgenticaPromptTransformer.ts +83 -0
  142. package/src/internal/AgenticaSystemPrompt.ts +14 -0
  143. package/src/internal/MathUtil.ts +3 -0
  144. package/src/internal/Singleton.ts +22 -0
  145. package/src/internal/__map_take.ts +15 -0
  146. package/src/structures/IAgenticaConfig.ts +121 -0
  147. package/src/structures/IAgenticaContext.ts +128 -0
  148. package/src/structures/IAgenticaController.ts +130 -0
  149. package/src/structures/IAgenticaEvent.ts +224 -0
  150. package/src/structures/IAgenticaExecutor.ts +152 -0
  151. package/src/structures/IAgenticaOperation.ts +64 -0
  152. package/src/structures/IAgenticaOperationCollection.ts +50 -0
  153. package/src/structures/IAgenticaOperationSelection.ts +69 -0
  154. package/src/structures/IAgenticaPrompt.ts +173 -0
  155. package/src/structures/IAgenticaProps.ts +64 -0
  156. package/src/structures/IAgenticaProvider.ts +45 -0
  157. package/src/structures/IAgenticaSystemPrompt.ts +122 -0
  158. package/src/structures/IAgenticaTokenUsage.ts +52 -0
  159. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -0
  160. package/src/structures/internal/__IChatFunctionReference.ts +21 -0
  161. package/src/structures/internal/__IChatInitialApplication.ts +15 -0
  162. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -0
  163. package/src/typings/AgenticaSource.ts +6 -0
@@ -0,0 +1,23 @@
1
+ import { __IChatFunctionReference } from "./__IChatFunctionReference";
2
+ export interface __IChatSelectFunctionsApplication {
3
+ /**
4
+ * Select proper API functions to call.
5
+ *
6
+ * If you A.I. agent has found some proper API functions to call
7
+ * from the conversation with user, please select the API functions
8
+ * just by calling this function.
9
+ *
10
+ * When user wants to call a same function multiply, you A.I. agent must
11
+ * list up it multiply in the `functions` property. Otherwise the user has
12
+ * requested to call many different functions, you A.I. agent have to assign
13
+ * them all into the `functions` property.
14
+ *
15
+ * Also, if you A.I. agent can't specify a specific function to call due to lack
16
+ * of specificity or homogeneity of candidate functions, just assign all of them
17
+ * by in the` functions` property` too. Instead, when you A.I. agent can specify
18
+ * a specific function to call, the others would be eliminated.
19
+ *
20
+ * @param props Properties of the function
21
+ */
22
+ selectFunctions(props: __IChatFunctionReference.IProps): Promise<void>;
23
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=__IChatSelectFunctionsApplication.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"__IChatSelectFunctionsApplication.js","sourceRoot":"","sources":["../../../src/structures/internal/__IChatSelectFunctionsApplication.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export type AgenticaSource = "initialize" | "select" | "cancel" | "execute" | "describe";
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=AgenticaSource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AgenticaSource.js","sourceRoot":"","sources":["../../src/typings/AgenticaSource.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@agentica/core",
3
+ "version": "0.7.0-dev.20250224",
4
+ "main": "lib/index.js",
5
+ "description": "Agentic AI Library specialized in LLM Function Calling",
6
+ "scripts": {
7
+ "prepare": "ts-patch install && pnpm build:prompt",
8
+ "build": "rimraf lib && pnpm build:prompt && tsc && rollup -c",
9
+ "build:prompt": "ts-node build/prompt.ts",
10
+ "dev": "rimraf lib && tsc --watch",
11
+ "eslint": "eslint ./**/*.ts",
12
+ "test": "pnpm build:test && node bin/test/index.js"
13
+ },
14
+ "author": "Wrtn Technologies",
15
+ "homepage": "https://wrtnlabs.io/agentica",
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/wrtnlabs/agentica"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/wrtnlabs/agentica/issues"
23
+ },
24
+ "keywords": [
25
+ "openai",
26
+ "chatgpt",
27
+ "anthropic",
28
+ "claude",
29
+ "ai",
30
+ "chatbot",
31
+ "nestia",
32
+ "swagger",
33
+ "openapi"
34
+ ],
35
+ "files": [
36
+ "README.md",
37
+ "LICENSE",
38
+ "package.json",
39
+ "lib",
40
+ "prompts",
41
+ "src"
42
+ ],
43
+ "devDependencies": {
44
+ "@eslint/js": "^9.17.0",
45
+ "@nestia/e2e": "^0.8.2",
46
+ "@rollup/plugin-terser": "^0.4.4",
47
+ "@rollup/plugin-typescript": "^12.1.2",
48
+ "@samchon/openapi": "^2.4.2",
49
+ "@trivago/prettier-plugin-sort-imports": "^5.2.2",
50
+ "@types/node": "^22.13.4",
51
+ "@types/uuid": "^10.0.0",
52
+ "eslint": "^9.17.0",
53
+ "openai": "^4.80.0",
54
+ "prettier": "^3.5.0",
55
+ "rimraf": "^6.0.1",
56
+ "rollup": "^4.34.8",
57
+ "ts-node": "^10.9.2",
58
+ "ts-patch": "^3.3.0",
59
+ "typescript": "~5.7.3",
60
+ "typia": "^7.6.4"
61
+ },
62
+ "dependencies": {
63
+ "@samchon/openapi": "^2.4.3",
64
+ "typia": "^7.6.4",
65
+ "uuid": "^11.0.4"
66
+ },
67
+ "peerDependencies": {
68
+ "@samchon/openapi": ">= 2.4.3",
69
+ "openai": ">= 4.80.0",
70
+ "typia": ">= 7.6.4"
71
+ },
72
+ "module": "lib/index.mjs",
73
+ "typings": "lib/index.d.ts"
74
+ }
@@ -0,0 +1,5 @@
1
+ You are a helpful assistant for cancelling functions which are prepared to call.
2
+
3
+ Use the supplied tools to select some functions to cancel of `getApiFunctions()` returned.
4
+
5
+ If you can't find any proper function to select, don't talk, don't do anything.
@@ -0,0 +1,3 @@
1
+ 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.
2
+
3
+ At 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.
@@ -0,0 +1,7 @@
1
+ You are a helpful assistant describing return values of function calls.
2
+
3
+ Above 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.
4
+
5
+ Also, 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.
6
+
7
+ At last, if user's language locale code is different with your description, please translate it to the user's language.
@@ -0,0 +1,7 @@
1
+ You are a helpful assistant for tool calling.
2
+
3
+ Use the supplied tools to assist the user.
4
+
5
+ If 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.
6
+
7
+ For 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.
@@ -0,0 +1,3 @@
1
+ You are a helpful assistant.
2
+
3
+ Use the supplied tools to assist the user.
@@ -0,0 +1,7 @@
1
+ You are a helpful assistant for selecting functions to call.
2
+
3
+ Use the supplied tools to select some functions of `getApiFunctions()` returned.
4
+
5
+ When selecting functions to call, pay attention to the relationship between functions. In particular, check the prerequisites between each function.
6
+
7
+ If 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.
@@ -0,0 +1,322 @@
1
+ import OpenAI from "openai";
2
+
3
+ import { ChatGptAgent } from "./chatgpt/ChatGptAgent";
4
+ import { AgenticaCostAggregator } from "./internal/AgenticaCostAggregator";
5
+ import { AgenticaOperationComposer } from "./internal/AgenticaOperationComposer";
6
+ import { AgenticaPromptTransformer } from "./internal/AgenticaPromptTransformer";
7
+ import { __map_take } from "./internal/__map_take";
8
+ import { IAgenticaConfig } from "./structures/IAgenticaConfig";
9
+ import { IAgenticaContext } from "./structures/IAgenticaContext";
10
+ import { IAgenticaController } from "./structures/IAgenticaController";
11
+ import { IAgenticaEvent } from "./structures/IAgenticaEvent";
12
+ import { IAgenticaOperation } from "./structures/IAgenticaOperation";
13
+ import { IAgenticaOperationCollection } from "./structures/IAgenticaOperationCollection";
14
+ import { IAgenticaOperationSelection } from "./structures/IAgenticaOperationSelection";
15
+ import { IAgenticaPrompt } from "./structures/IAgenticaPrompt";
16
+ import { IAgenticaProps } from "./structures/IAgenticaProps";
17
+ import { IAgenticaProvider } from "./structures/IAgenticaProvider";
18
+ import { IAgenticaTokenUsage } from "./structures/IAgenticaTokenUsage";
19
+
20
+ /**
21
+ * Nestia A.I. chatbot agent.
22
+ *
23
+ * `Agentica` is a facade class for the super A.I. chatbot agent
24
+ * which performs the {@link converstate user's conversation function}
25
+ * with LLM (Large Language Model) function calling and manages the
26
+ * {@link getPromptHistories prompt histories}.
27
+ *
28
+ * To understand and compose the `Agentica` class exactly, reference
29
+ * below types concentrating on the documentation comments please.
30
+ * Especially, you have to be careful about the {@link IAgenticaProps}
31
+ * type which is used in the {@link constructor} function.
32
+ *
33
+ * - Constructors
34
+ * - {@link IAgenticaProps}
35
+ * - {@link IAgenticaProvider}
36
+ * - {@link IAgenticaController}
37
+ * - {@link IAgenticaConfig}
38
+ * - {@link IAgenticaSystemPrompt}
39
+ * - Accessors
40
+ * - {@link IAgenticaOperation}
41
+ * - {@link IAgenticaPrompt}
42
+ * - {@link IAgenticaEvent}
43
+ * - {@link IAgenticaTokenUsage}
44
+ *
45
+ * @author Samchon
46
+ */
47
+ export class Agentica {
48
+ // THE OPERATIONS
49
+ private readonly operations_: IAgenticaOperationCollection;
50
+
51
+ // STACK
52
+ private readonly stack_: IAgenticaOperationSelection[];
53
+ private readonly prompt_histories_: IAgenticaPrompt[];
54
+ private readonly listeners_: Map<string, Set<Function>>;
55
+
56
+ // STATUS
57
+ private readonly token_usage_: IAgenticaTokenUsage;
58
+ private ready_: boolean;
59
+ private readonly executor_: (
60
+ ctx: IAgenticaContext,
61
+ ) => Promise<IAgenticaPrompt[]>;
62
+
63
+ /* -----------------------------------------------------------
64
+ CONSTRUCTOR
65
+ ----------------------------------------------------------- */
66
+ /**
67
+ * Initializer constructor.
68
+ *
69
+ * @param props Properties to construct the agent
70
+ */
71
+ public constructor(private readonly props: IAgenticaProps) {
72
+ // OPERATIONS
73
+ this.operations_ = AgenticaOperationComposer.compose({
74
+ controllers: props.controllers,
75
+ config: props.config,
76
+ });
77
+
78
+ // STATUS
79
+ this.stack_ = [];
80
+ this.listeners_ = new Map();
81
+ this.prompt_histories_ = (props.histories ?? []).map((input) =>
82
+ AgenticaPromptTransformer.transform({
83
+ operations: this.operations_.group,
84
+ input,
85
+ }),
86
+ );
87
+
88
+ // STATUS
89
+ this.token_usage_ = {
90
+ total: 0,
91
+ prompt: {
92
+ total: 0,
93
+ audio: 0,
94
+ cached: 0,
95
+ },
96
+ completion: {
97
+ total: 0,
98
+ accepted_prediction: 0,
99
+ audio: 0,
100
+ reasoning: 0,
101
+ rejected_prediction: 0,
102
+ },
103
+ };
104
+ this.ready_ = false;
105
+ this.executor_ =
106
+ typeof props.config?.executor === "function"
107
+ ? props.config.executor
108
+ : ChatGptAgent.execute(props.config?.executor ?? null);
109
+ }
110
+
111
+ /**
112
+ * @internal
113
+ */
114
+ public clone(): Agentica {
115
+ return new Agentica({
116
+ ...this.props,
117
+ histories: this.props.histories?.slice(),
118
+ });
119
+ }
120
+
121
+ /* -----------------------------------------------------------
122
+ ACCESSORS
123
+ ----------------------------------------------------------- */
124
+ /**
125
+ * Conversate with the A.I. chatbot.
126
+ *
127
+ * User talks to the A.I. chatbot with the content.
128
+ *
129
+ * When the user's conversation implies the A.I. chatbot to execute a
130
+ * function calling, the returned chat prompts will contain the
131
+ * function calling information like {@link IAgenticaPrompt.IExecute}.
132
+ *
133
+ * @param content The content to talk
134
+ * @returns List of newly created chat prompts
135
+ */
136
+ public async conversate(content: string): Promise<IAgenticaPrompt[]> {
137
+ const prompt: IAgenticaPrompt.IText<"user"> = {
138
+ type: "text",
139
+ role: "user",
140
+ text: content,
141
+ };
142
+ await this.dispatch(prompt);
143
+
144
+ const newbie: IAgenticaPrompt[] = await this.executor_(
145
+ this.getContext({
146
+ prompt,
147
+ usage: this.token_usage_,
148
+ }),
149
+ );
150
+ this.prompt_histories_.push(prompt, ...newbie);
151
+ return [prompt, ...newbie];
152
+ }
153
+
154
+ /**
155
+ * Get configuration.
156
+ */
157
+ public getConfig(): IAgenticaConfig | undefined {
158
+ return this.props.config;
159
+ }
160
+
161
+ /**
162
+ * Get LLM Provider.
163
+ */
164
+ public getProvider(): IAgenticaProvider {
165
+ return this.props.provider;
166
+ }
167
+
168
+ /**
169
+ * Get controllers.
170
+ *
171
+ * Get list of controllers, which are the collection of functions that
172
+ * the "Super A.I. Chatbot" can execute.
173
+ */
174
+ public getControllers(): ReadonlyArray<IAgenticaController> {
175
+ return this.props.controllers;
176
+ }
177
+
178
+ /**
179
+ * Get operations.
180
+ *
181
+ * Get list of operations, which has capsuled the pair of controller
182
+ * and function from the {@link getControllers controllers}.
183
+ *
184
+ * @returns
185
+ */
186
+ public getOperations(): ReadonlyArray<IAgenticaOperation> {
187
+ return this.operations_.array;
188
+ }
189
+
190
+ /**
191
+ * Get the chatbot's prompt histories.
192
+ *
193
+ * Get list of chat prompts that the chatbot has been conversated.
194
+ *
195
+ * @returns List of chat prompts
196
+ */
197
+ public getPromptHistories(): IAgenticaPrompt[] {
198
+ return this.prompt_histories_;
199
+ }
200
+
201
+ /**
202
+ * Get token usage of the A.I. chatbot.
203
+ *
204
+ * Entire token usage of the A.I. chatbot during the conversating
205
+ * with the user by {@link conversate} method callings.
206
+ *
207
+ * @returns Cost of the A.I. chatbot
208
+ */
209
+ public getTokenUsage(): IAgenticaTokenUsage {
210
+ return this.token_usage_;
211
+ }
212
+
213
+ /**
214
+ * @internal
215
+ */
216
+ public getContext(props: {
217
+ prompt: IAgenticaPrompt.IText<"user">;
218
+ usage: IAgenticaTokenUsage;
219
+ }): IAgenticaContext {
220
+ const dispatch = (event: IAgenticaEvent) => this.dispatch(event);
221
+ return {
222
+ // APPLICATION
223
+ operations: this.operations_,
224
+ config: this.props.config,
225
+
226
+ // STATES
227
+ histories: this.prompt_histories_,
228
+ stack: this.stack_,
229
+ ready: () => this.ready_,
230
+ prompt: props.prompt,
231
+
232
+ // HANDLERS
233
+ dispatch,
234
+ request: async (source, body) => {
235
+ // request information
236
+ const event: IAgenticaEvent.IRequest = {
237
+ type: "request",
238
+ source,
239
+ body: {
240
+ ...body,
241
+ model: this.props.provider.model,
242
+ },
243
+ options: this.props.provider.options,
244
+ };
245
+ await dispatch(event);
246
+
247
+ // completion
248
+ const value: OpenAI.ChatCompletion =
249
+ await this.props.provider.api.chat.completions.create(
250
+ event.body,
251
+ event.options,
252
+ );
253
+ AgenticaCostAggregator.aggregate(props.usage, value);
254
+ await dispatch({
255
+ type: "response",
256
+ source,
257
+ body: event.body,
258
+ options: event.options,
259
+ value,
260
+ });
261
+ return value;
262
+ },
263
+ initialize: async () => {
264
+ this.ready_ = true;
265
+ await dispatch({
266
+ type: "initialize",
267
+ });
268
+ },
269
+ };
270
+ }
271
+
272
+ /* -----------------------------------------------------------
273
+ EVENT HANDLERS
274
+ ----------------------------------------------------------- */
275
+ /**
276
+ * Add an event listener.
277
+ *
278
+ * Add an event listener to be called whenever the event is emitted.
279
+ *
280
+ * @param type Type of event
281
+ * @param listener Callback function to be called whenever the event is emitted
282
+ */
283
+ public on<Type extends IAgenticaEvent.Type>(
284
+ type: Type,
285
+ listener: (event: IAgenticaEvent.Mapper[Type]) => void | Promise<void>,
286
+ ): void {
287
+ __map_take(this.listeners_, type, () => new Set()).add(listener);
288
+ }
289
+
290
+ /**
291
+ * Erase an event listener.
292
+ *
293
+ * Erase an event listener to stop calling the callback function.
294
+ *
295
+ * @param type Type of event
296
+ * @param listener Callback function to erase
297
+ */
298
+ public off<Type extends IAgenticaEvent.Type>(
299
+ type: Type,
300
+ listener: (event: IAgenticaEvent.Mapper[Type]) => void | Promise<void>,
301
+ ): void {
302
+ const set: Set<Function> | undefined = this.listeners_.get(type);
303
+ if (set) {
304
+ set.delete(listener);
305
+ if (set.size === 0) this.listeners_.delete(type);
306
+ }
307
+ }
308
+
309
+ private async dispatch<Event extends IAgenticaEvent>(
310
+ event: Event,
311
+ ): Promise<void> {
312
+ const set: Set<Function> | undefined = this.listeners_.get(event.type);
313
+ if (set)
314
+ await Promise.all(
315
+ Array.from(set).map(async (listener) => {
316
+ try {
317
+ await listener(event);
318
+ } catch {}
319
+ }),
320
+ );
321
+ }
322
+ }
@@ -0,0 +1,71 @@
1
+ import { IAgenticaContext } from "../structures/IAgenticaContext";
2
+ import { IAgenticaExecutor } from "../structures/IAgenticaExecutor";
3
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
4
+ import { ChatGptCallFunctionAgent } from "./ChatGptCallFunctionAgent";
5
+ import { ChatGptCancelFunctionAgent } from "./ChatGptCancelFunctionAgent";
6
+ import { ChatGptDescribeFunctionAgent } from "./ChatGptDescribeFunctionAgent";
7
+ import { ChatGptInitializeFunctionAgent } from "./ChatGptInitializeFunctionAgent";
8
+ import { ChatGptSelectFunctionAgent } from "./ChatGptSelectFunctionAgent";
9
+
10
+ export namespace ChatGptAgent {
11
+ export const execute =
12
+ (executor: Partial<IAgenticaExecutor> | null) =>
13
+ async (ctx: IAgenticaContext): Promise<IAgenticaPrompt[]> => {
14
+ const histories: IAgenticaPrompt[] = [];
15
+
16
+ // FUNCTIONS ARE NOT LISTED YET
17
+ if (ctx.ready() === false) {
18
+ if (executor?.initialize === null) await ctx.initialize();
19
+ else {
20
+ histories.push(
21
+ ...(await (
22
+ executor?.initialize ?? ChatGptInitializeFunctionAgent.execute
23
+ )(ctx)),
24
+ );
25
+ if (ctx.ready() === false) return histories;
26
+ }
27
+ }
28
+
29
+ // CANCEL CANDIDATE FUNCTIONS
30
+ if (ctx.stack.length !== 0)
31
+ histories.push(
32
+ ...(await (executor?.cancel ?? ChatGptCancelFunctionAgent.execute)(
33
+ ctx,
34
+ )),
35
+ );
36
+
37
+ // SELECT CANDIDATE FUNCTIONS
38
+ histories.push(
39
+ ...(await (executor?.select ?? ChatGptSelectFunctionAgent.execute)(
40
+ ctx,
41
+ )),
42
+ );
43
+ if (ctx.stack.length === 0) return histories;
44
+
45
+ // FUNCTION CALLING LOOP
46
+ while (true) {
47
+ // EXECUTE FUNCTIONS
48
+ const prompts: IAgenticaPrompt[] = await (
49
+ executor?.call ?? ChatGptCallFunctionAgent.execute
50
+ )(ctx);
51
+ histories.push(...prompts);
52
+
53
+ // EXPLAIN RETURN VALUES
54
+ const executes: IAgenticaPrompt.IExecute[] = prompts.filter(
55
+ (prompt) => prompt.type === "execute",
56
+ );
57
+ for (const e of executes)
58
+ await ChatGptCancelFunctionAgent.cancelFunction(ctx, {
59
+ reason: "completed",
60
+ name: e.function.name,
61
+ });
62
+ histories.push(
63
+ ...(await (
64
+ executor?.describe ?? ChatGptDescribeFunctionAgent.execute
65
+ )(ctx, executes)),
66
+ );
67
+ if (executes.length === 0 || ctx.stack.length === 0) break;
68
+ }
69
+ return histories;
70
+ };
71
+ }