@agentica/core 0.15.6 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/lib/Agentica.d.ts +21 -21
  2. package/lib/Agentica.js +27 -27
  3. package/lib/Agentica.js.map +1 -1
  4. package/lib/MicroAgentica.d.ts +115 -0
  5. package/lib/MicroAgentica.js +279 -0
  6. package/lib/MicroAgentica.js.map +1 -0
  7. package/lib/constants/AgenticaDefaultPrompt.d.ts +2 -1
  8. package/lib/constants/AgenticaDefaultPrompt.js +7 -5
  9. package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
  10. package/lib/context/AgenticaCancelPrompt.d.ts +3 -3
  11. package/lib/context/AgenticaContext.d.ts +12 -12
  12. package/lib/context/AgenticaOperationCollection.d.ts +1 -1
  13. package/lib/context/MicroAgenticaContext.d.ts +75 -0
  14. package/lib/{prompts/AgenticaCancelPrompt.js → context/MicroAgenticaContext.js} +1 -1
  15. package/lib/context/MicroAgenticaContext.js.map +1 -0
  16. package/lib/context/internal/AgenticaOperationComposer.d.ts +2 -1
  17. package/lib/context/internal/AgenticaOperationComposer.js +3 -2
  18. package/lib/context/internal/AgenticaOperationComposer.js.map +1 -1
  19. package/lib/context/internal/isAgenticaContext.d.ts +1 -0
  20. package/lib/context/internal/isAgenticaContext.js +10 -0
  21. package/lib/context/internal/isAgenticaContext.js.map +1 -0
  22. package/lib/events/AgenticaDescribeEvent.d.ts +4 -4
  23. package/lib/events/AgenticaEvent.d.ts +1 -0
  24. package/lib/events/AgenticaExecuteEvent.d.ts +5 -5
  25. package/lib/events/AgenticaSelectEvent.d.ts +2 -2
  26. package/lib/events/AgenticaTextEvent.d.ts +2 -2
  27. package/lib/events/MicroAgenticaEvent.d.ts +32 -0
  28. package/lib/{prompts/AgenticaTextPrompt.js → events/MicroAgenticaEvent.js} +1 -1
  29. package/lib/events/MicroAgenticaEvent.js.map +1 -0
  30. package/lib/factory/events.d.ts +2 -2
  31. package/lib/factory/events.js +5 -5
  32. package/lib/factory/events.js.map +1 -1
  33. package/lib/factory/histories.d.ts +33 -0
  34. package/lib/factory/{prompts.js → histories.js} +13 -13
  35. package/lib/factory/histories.js.map +1 -0
  36. package/lib/factory/index.d.ts +1 -1
  37. package/lib/factory/index.js +1 -1
  38. package/lib/factory/index.js.map +1 -1
  39. package/lib/functional/assertHttpLlmApplication.js +577 -387
  40. package/lib/functional/assertHttpLlmApplication.js.map +1 -1
  41. package/lib/functional/validateHttpLlmApplication.js +500 -348
  42. package/lib/functional/validateHttpLlmApplication.js.map +1 -1
  43. package/lib/histories/AgenticaCancelHistory.d.ts +8 -0
  44. package/lib/{prompts/AgenticaExecutePrompt.js → histories/AgenticaCancelHistory.js} +1 -1
  45. package/lib/histories/AgenticaCancelHistory.js.map +1 -0
  46. package/lib/histories/AgenticaDescribeHistory.d.ts +16 -0
  47. package/lib/histories/AgenticaDescribeHistory.js +3 -0
  48. package/lib/histories/AgenticaDescribeHistory.js.map +1 -0
  49. package/lib/{prompts/AgenticaExecutePrompt.d.ts → histories/AgenticaExecuteHistory.d.ts} +5 -5
  50. package/lib/{prompts/AgenticaDescribePrompt.js → histories/AgenticaExecuteHistory.js} +1 -1
  51. package/lib/histories/AgenticaExecuteHistory.js.map +1 -0
  52. package/lib/histories/AgenticaHistory.d.ts +17 -0
  53. package/lib/{prompts/AgenticaPrompt.js → histories/AgenticaHistory.js} +1 -1
  54. package/lib/histories/AgenticaHistory.js.map +1 -0
  55. package/lib/{prompts/AgenticaPromptBase.d.ts → histories/AgenticaHistoryBase.d.ts} +4 -4
  56. package/lib/{json/IAgenticaPromptJson.js → histories/AgenticaHistoryBase.js} +1 -1
  57. package/lib/histories/AgenticaHistoryBase.js.map +1 -0
  58. package/lib/histories/AgenticaSelectHistory.d.ts +8 -0
  59. package/lib/histories/AgenticaSelectHistory.js +3 -0
  60. package/lib/histories/AgenticaSelectHistory.js.map +1 -0
  61. package/lib/histories/AgenticaTextHistory.d.ts +6 -0
  62. package/lib/{prompts/AgenticaPromptBase.js → histories/AgenticaTextHistory.js} +1 -1
  63. package/lib/histories/AgenticaTextHistory.js.map +1 -0
  64. package/lib/histories/MicroAgenticaHistory.d.ts +13 -0
  65. package/lib/histories/MicroAgenticaHistory.js +3 -0
  66. package/lib/histories/MicroAgenticaHistory.js.map +1 -0
  67. package/lib/index.d.ts +12 -7
  68. package/lib/index.js +12 -7
  69. package/lib/index.js.map +1 -1
  70. package/lib/index.mjs +1393 -902
  71. package/lib/index.mjs.map +1 -1
  72. package/lib/json/IAgenticaEventJson.d.ts +6 -5
  73. package/lib/json/{IAgenticaPromptJson.d.ts → IAgenticaHistoryJson.d.ts} +8 -8
  74. package/lib/json/IAgenticaHistoryJson.js +3 -0
  75. package/lib/json/IAgenticaHistoryJson.js.map +1 -0
  76. package/lib/json/IAgenticaOperationSelectionJson.d.ts +7 -1
  77. package/lib/json/IMicroAgenticaEventJson.d.ts +13 -0
  78. package/lib/json/IMicroAgenticaEventJson.js +3 -0
  79. package/lib/json/IMicroAgenticaEventJson.js.map +1 -0
  80. package/lib/json/IMicroAgenticaHistoryJson.d.ts +19 -0
  81. package/lib/json/IMicroAgenticaHistoryJson.js +3 -0
  82. package/lib/json/IMicroAgenticaHistoryJson.js.map +1 -0
  83. package/lib/orchestrate/call.d.ts +4 -2
  84. package/lib/orchestrate/call.js +50 -41
  85. package/lib/orchestrate/call.js.map +1 -1
  86. package/lib/orchestrate/cancel.js +4 -4
  87. package/lib/orchestrate/cancel.js.map +1 -1
  88. package/lib/orchestrate/describe.d.ts +4 -3
  89. package/lib/orchestrate/describe.js +3 -3
  90. package/lib/orchestrate/describe.js.map +1 -1
  91. package/lib/orchestrate/execute.d.ts +2 -2
  92. package/lib/orchestrate/execute.js +1 -1
  93. package/lib/orchestrate/execute.js.map +1 -1
  94. package/lib/orchestrate/initialize.d.ts +2 -2
  95. package/lib/orchestrate/initialize.js +3 -3
  96. package/lib/orchestrate/initialize.js.map +1 -1
  97. package/lib/orchestrate/internal/selectFunction.d.ts +1 -5
  98. package/lib/orchestrate/internal/selectFunction.js +3 -0
  99. package/lib/orchestrate/internal/selectFunction.js.map +1 -1
  100. package/lib/orchestrate/select.d.ts +2 -2
  101. package/lib/orchestrate/select.js +5 -5
  102. package/lib/orchestrate/select.js.map +1 -1
  103. package/lib/structures/IAgenticaConfig.d.ts +6 -5
  104. package/lib/structures/IAgenticaController.d.ts +1 -1
  105. package/lib/structures/IAgenticaExecutor.d.ts +11 -9
  106. package/lib/structures/IAgenticaProps.d.ts +3 -3
  107. package/lib/structures/IAgenticaSystemPrompt.d.ts +13 -13
  108. package/lib/structures/IAgenticaVendor.d.ts +1 -1
  109. package/lib/structures/IMicroAgenticaConfig.d.ts +74 -0
  110. package/lib/structures/IMicroAgenticaConfig.js +3 -0
  111. package/lib/structures/IMicroAgenticaConfig.js.map +1 -0
  112. package/lib/structures/IMicroAgenticaExecutor.d.ts +57 -0
  113. package/lib/structures/IMicroAgenticaExecutor.js +3 -0
  114. package/lib/structures/IMicroAgenticaExecutor.js.map +1 -0
  115. package/lib/structures/IMicroAgenticaProps.d.ts +63 -0
  116. package/lib/structures/IMicroAgenticaProps.js +3 -0
  117. package/lib/structures/IMicroAgenticaProps.js.map +1 -0
  118. package/lib/structures/IMicroAgenticaSystemPrompt.d.ts +66 -0
  119. package/lib/structures/IMicroAgenticaSystemPrompt.js +3 -0
  120. package/lib/structures/IMicroAgenticaSystemPrompt.js.map +1 -0
  121. package/lib/transformers/AgenticaPromptTransformer.d.ts +19 -19
  122. package/lib/transformers/AgenticaPromptTransformer.js +27 -27
  123. package/lib/transformers/AgenticaPromptTransformer.js.map +1 -1
  124. package/package.json +7 -7
  125. package/src/Agentica.ts +34 -34
  126. package/src/MicroAgentica.ts +337 -0
  127. package/src/constants/AgenticaDefaultPrompt.ts +7 -3
  128. package/src/context/AgenticaCancelPrompt.ts +3 -3
  129. package/src/context/AgenticaContext.ts +12 -12
  130. package/src/context/AgenticaOperationCollection.ts +1 -1
  131. package/src/context/MicroAgenticaContext.ts +95 -0
  132. package/src/context/internal/AgenticaOperationComposer.ts +6 -3
  133. package/src/context/internal/isAgenticaContext.ts +13 -0
  134. package/src/events/AgenticaDescribeEvent.ts +4 -4
  135. package/src/events/AgenticaEvent.ts +6 -0
  136. package/src/events/AgenticaExecuteEvent.ts +5 -5
  137. package/src/events/AgenticaSelectEvent.ts +2 -2
  138. package/src/events/AgenticaTextEvent.ts +2 -2
  139. package/src/events/MicroAgenticaEvent.ts +41 -0
  140. package/src/factory/events.ts +8 -8
  141. package/src/factory/{prompts.ts → histories.ts} +18 -18
  142. package/src/factory/index.ts +1 -1
  143. package/src/histories/AgenticaCancelHistory.ts +13 -0
  144. package/src/histories/AgenticaDescribeHistory.ts +22 -0
  145. package/src/{prompts/AgenticaExecutePrompt.ts → histories/AgenticaExecuteHistory.ts} +7 -7
  146. package/src/histories/AgenticaHistory.ts +25 -0
  147. package/src/{prompts/AgenticaPromptBase.ts → histories/AgenticaHistoryBase.ts} +4 -4
  148. package/src/histories/AgenticaSelectHistory.ts +13 -0
  149. package/src/histories/AgenticaTextHistory.ts +10 -0
  150. package/src/histories/MicroAgenticaHistory.ts +18 -0
  151. package/src/index.ts +18 -15
  152. package/src/json/IAgenticaEventJson.ts +6 -5
  153. package/src/json/{IAgenticaPromptJson.ts → IAgenticaHistoryJson.ts} +13 -13
  154. package/src/json/IAgenticaOperationSelectionJson.ts +8 -1
  155. package/src/json/IMicroAgenticaEventJson.ts +21 -0
  156. package/src/json/IMicroAgenticaHistoryJson.ts +23 -0
  157. package/src/orchestrate/call.ts +83 -59
  158. package/src/orchestrate/cancel.ts +4 -4
  159. package/src/orchestrate/describe.ts +11 -7
  160. package/src/orchestrate/execute.ts +7 -7
  161. package/src/orchestrate/initialize.ts +6 -6
  162. package/src/orchestrate/internal/selectFunction.ts +3 -0
  163. package/src/orchestrate/select.ts +13 -13
  164. package/src/structures/IAgenticaConfig.ts +6 -5
  165. package/src/structures/IAgenticaController.ts +1 -1
  166. package/src/structures/IAgenticaExecutor.ts +12 -10
  167. package/src/structures/IAgenticaProps.ts +3 -3
  168. package/src/structures/IAgenticaSystemPrompt.ts +13 -13
  169. package/src/structures/IAgenticaVendor.ts +1 -1
  170. package/src/structures/IMicroAgenticaConfig.ts +82 -0
  171. package/src/structures/IMicroAgenticaExecutor.ts +63 -0
  172. package/src/structures/IMicroAgenticaProps.ts +70 -0
  173. package/src/structures/IMicroAgenticaSystemPrompt.ts +71 -0
  174. package/src/transformers/AgenticaPromptTransformer.ts +46 -46
  175. package/lib/factory/prompts.d.ts +0 -33
  176. package/lib/factory/prompts.js.map +0 -1
  177. package/lib/json/IAgenticaPromptJson.js.map +0 -1
  178. package/lib/prompts/AgenticaCancelPrompt.d.ts +0 -8
  179. package/lib/prompts/AgenticaCancelPrompt.js.map +0 -1
  180. package/lib/prompts/AgenticaDescribePrompt.d.ts +0 -16
  181. package/lib/prompts/AgenticaDescribePrompt.js.map +0 -1
  182. package/lib/prompts/AgenticaExecutePrompt.js.map +0 -1
  183. package/lib/prompts/AgenticaPrompt.d.ts +0 -17
  184. package/lib/prompts/AgenticaPrompt.js.map +0 -1
  185. package/lib/prompts/AgenticaPromptBase.js.map +0 -1
  186. package/lib/prompts/AgenticaSelectPrompt.d.ts +0 -8
  187. package/lib/prompts/AgenticaSelectPrompt.js +0 -3
  188. package/lib/prompts/AgenticaSelectPrompt.js.map +0 -1
  189. package/lib/prompts/AgenticaTextPrompt.d.ts +0 -6
  190. package/lib/prompts/AgenticaTextPrompt.js.map +0 -1
  191. package/src/prompts/AgenticaCancelPrompt.ts +0 -13
  192. package/src/prompts/AgenticaDescribePrompt.ts +0 -22
  193. package/src/prompts/AgenticaPrompt.ts +0 -25
  194. package/src/prompts/AgenticaSelectPrompt.ts +0 -13
  195. package/src/prompts/AgenticaTextPrompt.ts +0 -10
@@ -0,0 +1,337 @@
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import type { AgenticaOperationCollection } from "./context/AgenticaOperationCollection";
4
+ import type { MicroAgenticaContext } from "./context/MicroAgenticaContext";
5
+ import type { AgenticaRequestEvent } from "./events/AgenticaRequestEvent";
6
+ import type { MicroAgenticaEvent } from "./events/MicroAgenticaEvent";
7
+ import type { AgenticaExecuteHistory } from "./histories/AgenticaExecuteHistory";
8
+ import type { AgenticaTextHistory } from "./histories/AgenticaTextHistory";
9
+ import type { MicroAgenticaHistory } from "./histories/MicroAgenticaHistory";
10
+ import type { IAgenticaController } from "./structures/IAgenticaController";
11
+ import type { IAgenticaVendor } from "./structures/IAgenticaVendor";
12
+ import type { IMicroAgenticaConfig } from "./structures/IMicroAgenticaConfig";
13
+ import type { IMicroAgenticaProps } from "./structures/IMicroAgenticaProps";
14
+
15
+ import { AgenticaTokenUsage } from "./context/AgenticaTokenUsage";
16
+ import { AgenticaOperationComposer } from "./context/internal/AgenticaOperationComposer";
17
+ import { AgenticaTokenUsageAggregator } from "./context/internal/AgenticaTokenUsageAggregator";
18
+ import { createRequestEvent, createTextEvent } from "./factory/events";
19
+ import { createTextHistory } from "./factory/histories";
20
+ import { call, describe } from "./orchestrate";
21
+ import { AgenticaPromptTransformer } from "./transformers/AgenticaPromptTransformer";
22
+ import { __map_take } from "./utils/__map_take";
23
+ import { ChatGptCompletionMessageUtil } from "./utils/ChatGptCompletionMessageUtil";
24
+ import { StreamUtil } from "./utils/StreamUtil";
25
+
26
+ /**
27
+ * Micro AI chatbot.
28
+ *
29
+ * `MicroAgentica` is a facade class for the micro AI chatbot agent
30
+ * which performs LLM (Large Language Model) function calling from the
31
+ * {@link conversate user's conversation} and manages the
32
+ * {@link getHistories prompt histories}.
33
+ *
34
+ * Different between `MicroAgentica` and {@link Agentica} is that
35
+ * `MicroAgentica` does not have function selecting filter. It directly
36
+ * list up every functions to the agent. Besides, {@link Agentica} has
37
+ * a function selecting mechanism to reduce the number of functions to
38
+ * be listed up to the agent.
39
+ *
40
+ * Therefore, if you have a lot of functions to call, you must not
41
+ * use this `MicroAgentica` class. Use this `MicroAgentica` class only
42
+ * when you have a few functions to call.
43
+ *
44
+ * - [Multi-agent orchestration of `@agentica`](https://wrtnlabs.io/agentica/docs/concepts/function-calling/#orchestration-strategy)
45
+ * - Internal agents of `MicroAgentica`
46
+ * - executor
47
+ * - describier
48
+ * - Internal agents of {@link Agentica}
49
+ * - initializer
50
+ * - **selector**
51
+ * - executor
52
+ * - describer
53
+ *
54
+ * @author Samchon
55
+ */
56
+ export class MicroAgentica<Model extends ILlmSchema.Model> {
57
+ private readonly operations_: AgenticaOperationCollection<Model>;
58
+ private readonly histories_: MicroAgenticaHistory<Model>[];
59
+ private readonly listeners_: Map<string, Set<(event: MicroAgenticaEvent<Model>) => Promise<void>>>;
60
+ private readonly token_usage_: AgenticaTokenUsage;
61
+
62
+ /* -----------------------------------------------------------
63
+ CONSTRUCTOR
64
+ ----------------------------------------------------------- */
65
+ /**
66
+ * Initializer Constructor.
67
+ *
68
+ * @param props Properties to construct the micro agent
69
+ */
70
+ public constructor(private readonly props: IMicroAgenticaProps<Model>) {
71
+ this.operations_ = AgenticaOperationComposer.compose({
72
+ controllers: props.controllers,
73
+ config: props.config,
74
+ });
75
+ this.histories_ = (props.histories ?? []).map(input =>
76
+ AgenticaPromptTransformer.transform({
77
+ operations: this.operations_.group,
78
+ history: input,
79
+ }),
80
+ ) as MicroAgenticaHistory<Model>[];
81
+ this.listeners_ = new Map();
82
+ this.token_usage_ = AgenticaTokenUsage.zero();
83
+ }
84
+
85
+ /**
86
+ * @internal
87
+ */
88
+ public clone(): MicroAgentica<Model> {
89
+ return new MicroAgentica<Model>({
90
+ ...this.props,
91
+ histories: this.props.histories?.slice(),
92
+ });
93
+ }
94
+
95
+ /* -----------------------------------------------------------
96
+ ACCESSORS
97
+ ----------------------------------------------------------- */
98
+ /**
99
+ * Conversate with the micro agent.
100
+ *
101
+ * User talks to the AI chatbot with the given content.
102
+ *
103
+ * When the user's conversation implies the AI chatbot to execute a
104
+ * function calling, the returned chat prompts will contain the
105
+ * function callinng information like {@link AgenticaExecuteHistory}
106
+ *
107
+ * @param content The content to talk
108
+ * @returns List of newly created histories
109
+ */
110
+ public async conversate(content: string): Promise<MicroAgenticaHistory<Model>[]> {
111
+ const talk: AgenticaTextHistory<"user"> = createTextHistory<"user">({
112
+ role: "user",
113
+ text: content,
114
+ });
115
+ await this.dispatch(
116
+ createTextEvent({
117
+ role: "user",
118
+ stream: StreamUtil.to(content),
119
+ done: () => true,
120
+ get: () => content,
121
+ join: async () => Promise.resolve(content),
122
+ }),
123
+ );
124
+
125
+ const ctx: MicroAgenticaContext<Model> = this.getContext({
126
+ prompt: talk,
127
+ usage: this.token_usage_,
128
+ });
129
+ const histories: MicroAgenticaHistory<Model>[] = await call(
130
+ ctx,
131
+ this.operations_.array,
132
+ ) as MicroAgenticaHistory<Model>[];
133
+ const executes: AgenticaExecuteHistory<Model>[] = histories.filter(p => p.type === "execute");
134
+ if (executes.length) {
135
+ histories.push(...await describe(ctx, executes));
136
+ }
137
+
138
+ this.histories_.push(talk, ...histories);
139
+ return histories;
140
+ }
141
+
142
+ /**
143
+ * Get configuration.
144
+ */
145
+ public getConfig(): IMicroAgenticaConfig<Model> | undefined {
146
+ return this.props.config;
147
+ }
148
+
149
+ /**
150
+ * Get LLM vendor.
151
+ */
152
+ public getVendor(): IAgenticaVendor {
153
+ return this.props.vendor;
154
+ }
155
+
156
+ /**
157
+ * Get controllers.
158
+ *
159
+ * Get list of controllers, which are the collection of functions that
160
+ * the agent can execute.
161
+ */
162
+ public getControllers(): ReadonlyArray<IAgenticaController<Model>> {
163
+ return this.props.controllers;
164
+ }
165
+
166
+ /**
167
+ * Get the chatbot's histories.
168
+ *
169
+ * Get list of chat histories that the chatbot has been conversated.
170
+ *
171
+ * @returns List of chat histories
172
+ */
173
+ public getHistories(): MicroAgenticaHistory<Model>[] {
174
+ return this.histories_;
175
+ }
176
+
177
+ /**
178
+ * Get token usage of the AI chatbot.
179
+ *
180
+ * Entire token usage of the AI chatbot during the conversating
181
+ * with the user by {@link conversate} method callings.
182
+ *
183
+ * @returns Cost of the AI chatbot
184
+ */
185
+ public getTokenUsage(): AgenticaTokenUsage {
186
+ return this.token_usage_;
187
+ }
188
+
189
+ /**
190
+ * @internal
191
+ */
192
+ public getContext(props: {
193
+ prompt: AgenticaTextHistory<"user">;
194
+ usage: AgenticaTokenUsage;
195
+ }): MicroAgenticaContext<Model> {
196
+ const dispatch = this.dispatch.bind(this);
197
+ return {
198
+ operations: this.operations_,
199
+ config: this.props.config,
200
+
201
+ histories: this.histories_,
202
+ prompt: props.prompt,
203
+ dispatch,
204
+ request: async (source, body) => {
205
+ // request information
206
+ const event: AgenticaRequestEvent = createRequestEvent({
207
+ source,
208
+ body: {
209
+ ...body,
210
+ model: this.props.vendor.model,
211
+ stream: true,
212
+ stream_options: {
213
+ include_usage: true,
214
+ },
215
+ },
216
+ options: this.props.vendor.options,
217
+ });
218
+ await dispatch(event);
219
+
220
+ // completion
221
+ const completion = await this.props.vendor.api.chat.completions.create(
222
+ event.body,
223
+ event.options,
224
+ );
225
+
226
+ const [streamForEvent, temporaryStream] = StreamUtil.transform(
227
+ completion.toReadableStream() as ReadableStream<Uint8Array>,
228
+ value =>
229
+ ChatGptCompletionMessageUtil.transformCompletionChunk(value),
230
+ ).tee();
231
+
232
+ const [streamForAggregate, streamForReturn] = temporaryStream.tee();
233
+
234
+ void (async () => {
235
+ const reader = streamForAggregate.getReader();
236
+ while (true) {
237
+ const chunk = await reader.read();
238
+ if (chunk.done) {
239
+ break;
240
+ }
241
+ if (chunk.value.usage != null) {
242
+ AgenticaTokenUsageAggregator.aggregate({
243
+ kind: source,
244
+ completionUsage: chunk.value.usage,
245
+ usage: props.usage,
246
+ });
247
+ }
248
+ }
249
+ })();
250
+
251
+ const [streamForStream, streamForJoin] = streamForEvent.tee();
252
+ await dispatch({
253
+ type: "response",
254
+ source,
255
+ stream: streamForStream,
256
+ body: event.body,
257
+ options: event.options,
258
+ join: async () => {
259
+ const chunks = await StreamUtil.readAll(streamForJoin);
260
+ return ChatGptCompletionMessageUtil.merge(chunks);
261
+ },
262
+ });
263
+
264
+ return streamForReturn;
265
+ },
266
+ };
267
+ }
268
+
269
+ /* -----------------------------------------------------------
270
+ EVENT HANDLERS
271
+ ----------------------------------------------------------- */
272
+ /**
273
+ * Add an event listener.
274
+ *
275
+ * Add an event listener to be called whenever the event is emitted.
276
+ *
277
+ * @param type Type of event
278
+ * @param listener Callback function to be called whenever the event is emitted
279
+ */
280
+ public on<Type extends MicroAgenticaEvent.Type>(
281
+ type: Type,
282
+ listener: (
283
+ event: MicroAgenticaEvent.Mapper<Model>[Type],
284
+ ) => void | Promise<void>,
285
+ ): this {
286
+ /**
287
+ * @TODO remove `as`
288
+ */
289
+ __map_take(this.listeners_, type, () => new Set()).add(listener as (event: MicroAgenticaEvent<Model>) => Promise<void>);
290
+ return this;
291
+ }
292
+
293
+ /**
294
+ * Erase an event listener.
295
+ *
296
+ * Erase an event listener to stop calling the callback function.
297
+ *
298
+ * @param type Type of event
299
+ * @param listener Callback function to erase
300
+ */
301
+ public off<Type extends MicroAgenticaEvent.Type>(
302
+ type: Type,
303
+ listener: (
304
+ event: MicroAgenticaEvent.Mapper<Model>[Type],
305
+ ) => void | Promise<void>,
306
+ ): this {
307
+ const set = this.listeners_.get(type);
308
+ if (set !== undefined) {
309
+ /**
310
+ * @TODO remove `as`
311
+ */
312
+ set.delete(listener as (event: MicroAgenticaEvent<Model>) => Promise<void>);
313
+ if (set.size === 0) {
314
+ this.listeners_.delete(type);
315
+ }
316
+ }
317
+ return this;
318
+ }
319
+
320
+ private async dispatch<Event extends MicroAgenticaEvent<Model>>(
321
+ event: Event,
322
+ ): Promise<void> {
323
+ const set = this.listeners_.get(event.type);
324
+ if (set !== undefined) {
325
+ await Promise.all(
326
+ Array.from(set).map(async (listener) => {
327
+ try {
328
+ await listener(event);
329
+ }
330
+ catch {
331
+ /* empty */
332
+ }
333
+ }),
334
+ );
335
+ }
336
+ }
337
+ }
@@ -1,6 +1,8 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
3
  import type { IAgenticaConfig } from "../structures/IAgenticaConfig";
4
+ import type { IMicroAgenticaConfig } from "../structures/IMicroAgenticaConfig";
5
+ import type { IMicroAgenticaSystemPrompt } from "../structures/IMicroAgenticaSystemPrompt";
4
6
 
5
7
  import { Singleton } from "../utils/Singleton";
6
8
 
@@ -39,9 +41,9 @@ const getTimezone = new Singleton(
39
41
  () => Intl.DateTimeFormat().resolvedOptions().timeZone,
40
42
  );
41
43
 
42
- export function write<Model extends ILlmSchema.Model>(config?: IAgenticaConfig<Model>): string {
44
+ export function write<Model extends ILlmSchema.Model>(config?: IAgenticaConfig<Model> | IMicroAgenticaConfig<Model>): string {
43
45
  if (config?.systemPrompt?.common !== undefined) {
44
- return config?.systemPrompt?.common(config);
46
+ return (config.systemPrompt as IMicroAgenticaSystemPrompt<Model>).common!(config as unknown as IMicroAgenticaConfig<Model>);
45
47
  }
46
48
 
47
49
  const locale: string = config?.locale ?? getLocale.get();
@@ -52,7 +54,9 @@ export function write<Model extends ILlmSchema.Model>(config?: IAgenticaConfig<M
52
54
  // eslint-disable-next-line no-template-curly-in-string
53
55
  .replace("${locale}", locale)
54
56
  // eslint-disable-next-line no-template-curly-in-string
55
- .replace("${timezone}", timezone);
57
+ .replace("${timezone}", timezone)
58
+ // eslint-disable-next-line no-template-curly-in-string
59
+ .replace("${datetime}", new Date().toISOString());
56
60
  }
57
61
  export const AgenticaDefaultPrompt = {
58
62
  write,
@@ -1,13 +1,13 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
- import type { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
4
- import type { AgenticaPromptBase } from "../prompts/AgenticaPromptBase";
3
+ import type { AgenticaHistoryBase } from "../histories/AgenticaHistoryBase";
4
+ import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
5
5
 
6
6
  import type { AgenticaOperationSelection } from "./AgenticaOperationSelection";
7
7
 
8
8
  export interface AgenticaCancelPrompt<
9
9
  Model extends ILlmSchema.Model,
10
- > extends AgenticaPromptBase<"cancel", IAgenticaPromptJson.ICancel> {
10
+ > extends AgenticaHistoryBase<"cancel", IAgenticaHistoryJson.ICancel> {
11
11
  id: string;
12
12
  selections: AgenticaOperationSelection<Model>[];
13
13
  }
@@ -3,28 +3,28 @@ import type OpenAI from "openai";
3
3
 
4
4
  import type { AgenticaEvent } from "../events/AgenticaEvent";
5
5
  import type { AgenticaEventSource } from "../events/AgenticaEventSource";
6
- import type { AgenticaPrompt } from "../prompts/AgenticaPrompt";
7
- import type { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
6
+ import type { AgenticaHistory } from "../histories/AgenticaHistory";
7
+ import type { AgenticaTextHistory } from "../histories/AgenticaTextHistory";
8
8
  import type { IAgenticaConfig } from "../structures/IAgenticaConfig";
9
9
 
10
10
  import type { AgenticaOperationCollection } from "./AgenticaOperationCollection";
11
11
  import type { AgenticaOperationSelection } from "./AgenticaOperationSelection";
12
12
 
13
13
  /**
14
- * Context of the Nestia A.I. agent.
14
+ * Context of the Agentic AI agent.
15
15
  *
16
- * `IAgenticaContext` is a structure defining the context of the
16
+ * `AgenticaContext` is a structure defining the context of the
17
17
  * internal agents composing the {@link Agentica}, like function
18
18
  * selector, executor, and describer, and so on. For example, if an
19
19
  * agent has been configured to utilize the OpenAI, the context will
20
20
  * be delivered to the below components.
21
21
  *
22
- * - {@link ChatGptAgent}
23
- * - {@link ChatGptInitializeFunctionAgent}
24
- * - {@link ChatGptSelectFunctionAgent}
25
- * - {@link ChatGptExecuteFunctionAgent}
26
- * - {@link ChatGptDescribeFunctionAgent}
27
- * - {@link ChatGptCancelFunctionAgent}
22
+ * - {@link orchestrate.execute}
23
+ * - {@link orchestrate.initialize}
24
+ * - {@link orchestrate.select}
25
+ * - {@link orchestrate.call}
26
+ * - {@link orchestrate.describe}
27
+ * - {@link orchestrate.cancel}
28
28
  *
29
29
  * Also, as its name is context, it contains every information that
30
30
  * is required to interact with the AI vendor like OpenAI. It
@@ -70,7 +70,7 @@ export interface AgenticaContext<Model extends ILlmSchema.Model> {
70
70
  /**
71
71
  * Prompt histories.
72
72
  */
73
- histories: AgenticaPrompt<Model>[];
73
+ histories: AgenticaHistory<Model>[];
74
74
 
75
75
  /**
76
76
  * Stacked operations.
@@ -85,7 +85,7 @@ export interface AgenticaContext<Model extends ILlmSchema.Model> {
85
85
  * Text conversation written the by user through the
86
86
  * {@link Agentica.conversate} function.
87
87
  */
88
- prompt: AgenticaTextPrompt<"user">;
88
+ prompt: AgenticaTextHistory<"user">;
89
89
 
90
90
  /**
91
91
  * Whether the agent is ready.
@@ -3,7 +3,7 @@ import type { ILlmSchema } from "@samchon/openapi";
3
3
  import type { AgenticaOperation } from "./AgenticaOperation";
4
4
 
5
5
  /**
6
- * Collection of operations used in the Nestia Agent.
6
+ * Collection of operations used in the Agentica Agent.
7
7
  *
8
8
  * `IAgenticaOperationCollection` is an interface type representing
9
9
  * a collection of operations for several purposes used in the
@@ -0,0 +1,95 @@
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
+ import type OpenAI from "openai";
3
+
4
+ import type { MicroAgenticaEvent } from "../events/MicroAgenticaEvent";
5
+ import type { AgenticaTextHistory } from "../histories/AgenticaTextHistory";
6
+ import type { MicroAgenticaHistory } from "../histories/MicroAgenticaHistory";
7
+ import type { IMicroAgenticaConfig } from "../structures/IMicroAgenticaConfig";
8
+
9
+ import type { AgenticaOperationCollection } from "./AgenticaOperationCollection";
10
+
11
+ /**
12
+ * Context of the Micro Agentic AI agent.
13
+ *
14
+ * `MicroAgenticaContext` is a structure defining the context of the
15
+ * internal agents composing the {@link MicroAgentica}.
16
+ *
17
+ * It contains every information that is required to interact with
18
+ * the AI vendor like OpenAI. It contains every operations for LLM
19
+ * function calling, and configuration used for the agent construction.
20
+ * And it contains the prompt histories, and facade controller
21
+ * functions for interacting with the {@link MicroAgentica} like
22
+ * {@link dispatch}.
23
+ *
24
+ * In such reasons, if you're planning to customize some internal
25
+ * agents, or add new agents with new process routine, you have to
26
+ * understand this context structure. Otherwise you don't have any
27
+ * plan to customize the internal agents, this context information is
28
+ * not important for you.
29
+ *
30
+ * @author Samchon
31
+ */
32
+ export interface MicroAgenticaContext<Model extends ILlmSchema.Model> {
33
+ // ----
34
+ // APPLICATION
35
+ // ----
36
+ /**
37
+ * Collection of operations.
38
+ *
39
+ * Collection of operations from every controllers, and their
40
+ * groups composed by the divide and conquer rule for the
41
+ * efficient operation selection if configured.
42
+ */
43
+ operations: AgenticaOperationCollection<Model>;
44
+
45
+ /**
46
+ * Configuration of the agent.
47
+ *
48
+ * Configuration of the agent, that is used when constructing the
49
+ * {@link Agentica} instance.
50
+ *
51
+ * @todo Write detaily after supporting the agent customization feature
52
+ */
53
+ config: IMicroAgenticaConfig<Model> | undefined;
54
+
55
+ // ----
56
+ // STATES
57
+ // ----
58
+ /**
59
+ * Prompt histories.
60
+ */
61
+ histories: MicroAgenticaHistory<Model>[];
62
+
63
+ /**
64
+ * Text prompt of the user.
65
+ *
66
+ * Text conversation written the by user through the
67
+ * {@link Agentica.conversate} function.
68
+ */
69
+ prompt: AgenticaTextHistory<"user">;
70
+
71
+ // ----
72
+ // HANDLERS
73
+ // ----
74
+ /**
75
+ * Dispatch event.
76
+ *
77
+ * Dispatch event so that the agent can be handle the event
78
+ * through the {@link Agentica.on} function.
79
+ *
80
+ * @param event Event to deliver
81
+ */
82
+ dispatch: (event: MicroAgenticaEvent<Model>) => Promise<void>;
83
+
84
+ /**
85
+ * Request to the OpenAI server.
86
+ *
87
+ * @param source The source agent of the agent
88
+ * @param body The request body to the OpenAI server
89
+ * @returns Response from the OpenAI server
90
+ */
91
+ request: (
92
+ source: MicroAgenticaEvent.Source,
93
+ body: Omit<OpenAI.ChatCompletionCreateParamsStreaming, "model" | "stream">,
94
+ ) => Promise<ReadableStream<OpenAI.Chat.Completions.ChatCompletionChunk>>;
95
+ }
@@ -2,6 +2,7 @@ import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
3
  import type { IAgenticaConfig } from "../../structures/IAgenticaConfig";
4
4
  import type { IAgenticaController } from "../../structures/IAgenticaController";
5
+ import type { IMicroAgenticaConfig } from "../../structures/IMicroAgenticaConfig";
5
6
  import type { AgenticaOperation } from "../AgenticaOperation";
6
7
  import type { AgenticaOperationCollection } from "../AgenticaOperationCollection";
7
8
 
@@ -9,7 +10,7 @@ import { __map_take } from "../../utils/__map_take";
9
10
 
10
11
  export function compose<Model extends ILlmSchema.Model>(props: {
11
12
  controllers: IAgenticaController<Model>[];
12
- config?: IAgenticaConfig<Model> | undefined;
13
+ config?: IAgenticaConfig<Model> | IMicroAgenticaConfig<Model> | undefined;
13
14
  }): AgenticaOperationCollection<Model> {
14
15
  const unique: boolean
15
16
  = props.controllers.length === 1
@@ -59,11 +60,13 @@ export function compose<Model extends ILlmSchema.Model>(props: {
59
60
  ),
60
61
  )
61
62
  .flat();
63
+
64
+ const capacity: number | undefined = (props.config as IAgenticaConfig<Model>)?.capacity;
62
65
  const divided: AgenticaOperation<Model>[][] | undefined
63
- = props.config?.capacity !== undefined && array.length > props.config.capacity
66
+ = capacity !== undefined && array.length > capacity
64
67
  ? divide({
65
68
  array,
66
- capacity: props.config.capacity,
69
+ capacity,
67
70
  })
68
71
  : undefined;
69
72
 
@@ -0,0 +1,13 @@
1
+ import type { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import type { AgenticaContext } from "../AgenticaContext";
4
+ import type { MicroAgenticaContext } from "../MicroAgenticaContext";
5
+
6
+ /**
7
+ * @internal
8
+ */
9
+ export function isAgenticaContext<Model extends ILlmSchema.Model>(
10
+ ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
11
+ ): ctx is AgenticaContext<Model> {
12
+ return typeof (ctx as AgenticaContext<Model>).initialize === "function";
13
+ }
@@ -1,18 +1,18 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
+ import type { AgenticaDescribeHistory } from "../histories/AgenticaDescribeHistory";
4
+ import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
3
5
  import type { IAgenticaEventJson } from "../json/IAgenticaEventJson";
4
- import type { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
5
- import type { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
6
6
 
7
7
  import type { AgenticaEventBase } from "./AgenticaEventBase";
8
8
 
9
9
  export interface AgenticaDescribeEvent<
10
10
  Model extends ILlmSchema.Model,
11
11
  > extends AgenticaEventBase<"describe"> {
12
- executes: AgenticaExecutePrompt<Model>[];
12
+ executes: AgenticaExecuteHistory<Model>[];
13
13
  stream: ReadableStream<string>;
14
14
 
15
15
  join: () => Promise<string>;
16
16
  toJSON: () => IAgenticaEventJson.IDescribe;
17
- toPrompt: () => AgenticaDescribePrompt<Model>;
17
+ toHistory: () => AgenticaDescribeHistory<Model>;
18
18
  }
@@ -46,4 +46,10 @@ export namespace AgenticaEvent {
46
46
  text: AgenticaTextEvent;
47
47
  validate: AgenticaValidateEvent<Model>;
48
48
  }
49
+ export type Source =
50
+ | "initialize"
51
+ | "select"
52
+ | "cancel"
53
+ | "call"
54
+ | "describe";
49
55
  }
@@ -1,8 +1,8 @@
1
1
  import type { IHttpResponse, ILlmSchema } from "@samchon/openapi";
2
2
 
3
3
  import type { AgenticaOperation } from "../context/AgenticaOperation";
4
+ import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
4
5
  import type { IAgenticaEventJson } from "../json/IAgenticaEventJson";
5
- import type { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
6
6
 
7
7
  import type { AgenticaEventBase } from "./AgenticaEventBase";
8
8
 
@@ -14,7 +14,7 @@ export namespace AgenticaExecuteEvent {
14
14
  extends Base<
15
15
  "class",
16
16
  AgenticaOperation.Class<Model>,
17
- AgenticaExecutePrompt.Class<Model>,
17
+ AgenticaExecuteHistory.Class<Model>,
18
18
  unknown
19
19
  > {}
20
20
 
@@ -22,14 +22,14 @@ export namespace AgenticaExecuteEvent {
22
22
  extends Base<
23
23
  "http",
24
24
  AgenticaOperation.Http<Model>,
25
- AgenticaExecutePrompt.Http<Model>,
25
+ AgenticaExecuteHistory.Http<Model>,
26
26
  IHttpResponse
27
27
  > {}
28
28
 
29
29
  interface Base<
30
30
  Protocol extends "http" | "class",
31
31
  Operation extends AgenticaOperation<any>,
32
- Prompt extends AgenticaExecutePrompt<any>,
32
+ History extends AgenticaExecuteHistory<any>,
33
33
  Value,
34
34
  > extends AgenticaEventBase<"execute"> {
35
35
  protocol: Protocol;
@@ -39,6 +39,6 @@ export namespace AgenticaExecuteEvent {
39
39
  value: Value;
40
40
 
41
41
  toJSON: () => IAgenticaEventJson.IExecute;
42
- toPrompt: () => Prompt;
42
+ toHistory: () => History;
43
43
  }
44
44
  }