@agentica/core 0.10.1-dev.20250302 → 0.10.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 (46) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +419 -419
  3. package/package.json +1 -1
  4. package/prompts/cancel.md +4 -4
  5. package/prompts/common.md +2 -2
  6. package/prompts/describe.md +6 -6
  7. package/prompts/execute.md +6 -6
  8. package/prompts/initialize.md +2 -2
  9. package/prompts/select.md +6 -6
  10. package/src/Agentica.ts +323 -323
  11. package/src/chatgpt/ChatGptAgent.ts +75 -75
  12. package/src/chatgpt/ChatGptCallFunctionAgent.ts +464 -464
  13. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +287 -287
  14. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +52 -52
  15. package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -88
  16. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -88
  17. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +319 -319
  18. package/src/functional/createHttpLlmApplication.ts +63 -63
  19. package/src/index.ts +19 -19
  20. package/src/internal/AgenticaConstant.ts +4 -4
  21. package/src/internal/AgenticaDefaultPrompt.ts +43 -43
  22. package/src/internal/AgenticaOperationComposer.ts +87 -87
  23. package/src/internal/AgenticaPromptFactory.ts +32 -32
  24. package/src/internal/AgenticaPromptTransformer.ts +86 -86
  25. package/src/internal/AgenticaTokenUsageAggregator.ts +115 -115
  26. package/src/internal/MathUtil.ts +3 -3
  27. package/src/internal/Singleton.ts +22 -22
  28. package/src/internal/__map_take.ts +15 -15
  29. package/src/structures/IAgenticaConfig.ts +123 -123
  30. package/src/structures/IAgenticaContext.ts +129 -129
  31. package/src/structures/IAgenticaController.ts +133 -133
  32. package/src/structures/IAgenticaEvent.ts +229 -229
  33. package/src/structures/IAgenticaExecutor.ts +156 -156
  34. package/src/structures/IAgenticaOperation.ts +63 -63
  35. package/src/structures/IAgenticaOperationCollection.ts +52 -52
  36. package/src/structures/IAgenticaOperationSelection.ts +68 -68
  37. package/src/structures/IAgenticaPrompt.ts +182 -182
  38. package/src/structures/IAgenticaProps.ts +70 -70
  39. package/src/structures/IAgenticaSystemPrompt.ts +124 -124
  40. package/src/structures/IAgenticaTokenUsage.ts +107 -107
  41. package/src/structures/IAgenticaVendor.ts +39 -39
  42. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -23
  43. package/src/structures/internal/__IChatFunctionReference.ts +21 -21
  44. package/src/structures/internal/__IChatInitialApplication.ts +15 -15
  45. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -24
  46. package/src/typings/AgenticaSource.ts +6 -6
package/src/Agentica.ts CHANGED
@@ -1,323 +1,323 @@
1
- import { ILlmSchema } from "@samchon/openapi";
2
- import OpenAI from "openai";
3
-
4
- import { ChatGptAgent } from "./chatgpt/ChatGptAgent";
5
- import { AgenticaOperationComposer } from "./internal/AgenticaOperationComposer";
6
- import { AgenticaPromptTransformer } from "./internal/AgenticaPromptTransformer";
7
- import { AgenticaTokenUsageAggregator } from "./internal/AgenticaTokenUsageAggregator";
8
- import { __map_take } from "./internal/__map_take";
9
- import { IAgenticaConfig } from "./structures/IAgenticaConfig";
10
- import { IAgenticaContext } from "./structures/IAgenticaContext";
11
- import { IAgenticaController } from "./structures/IAgenticaController";
12
- import { IAgenticaEvent } from "./structures/IAgenticaEvent";
13
- import { IAgenticaOperation } from "./structures/IAgenticaOperation";
14
- import { IAgenticaOperationCollection } from "./structures/IAgenticaOperationCollection";
15
- import { IAgenticaOperationSelection } from "./structures/IAgenticaOperationSelection";
16
- import { IAgenticaPrompt } from "./structures/IAgenticaPrompt";
17
- import { IAgenticaProps } from "./structures/IAgenticaProps";
18
- import { IAgenticaTokenUsage } from "./structures/IAgenticaTokenUsage";
19
- import { IAgenticaVendor } from "./structures/IAgenticaVendor";
20
-
21
- /**
22
- * Nestia A.I. chatbot agent.
23
- *
24
- * `Agentica` is a facade class for the super A.I. chatbot agent
25
- * which performs the {@link conversate user's conversation function}
26
- * with LLM (Large Language Model) function calling and manages the
27
- * {@link getPromptHistories prompt histories}.
28
- *
29
- * To understand and compose the `Agentica` class exactly, reference
30
- * below types concentrating on the documentation comments please.
31
- * Especially, you have to be careful about the {@link IAgenticaProps}
32
- * type which is used in the {@link constructor} function.
33
- *
34
- * - Constructors
35
- * - {@link IAgenticaProps}
36
- * - {@link IAgenticaVendor}
37
- * - {@link IAgenticaController}
38
- * - {@link IAgenticaConfig}
39
- * - {@link IAgenticaSystemPrompt}
40
- * - Accessors
41
- * - {@link IAgenticaOperation}
42
- * - {@link IAgenticaPrompt}
43
- * - {@link IAgenticaEvent}
44
- * - {@link IAgenticaTokenUsage}
45
- *
46
- * @author Samchon
47
- */
48
- export class Agentica<Model extends ILlmSchema.Model> {
49
- // THE OPERATIONS
50
- private readonly operations_: IAgenticaOperationCollection<Model>;
51
-
52
- // STACK
53
- private readonly stack_: IAgenticaOperationSelection<Model>[];
54
- private readonly prompt_histories_: IAgenticaPrompt<Model>[];
55
- private readonly listeners_: Map<string, Set<Function>>;
56
-
57
- // STATUS
58
- private readonly token_usage_: IAgenticaTokenUsage;
59
- private ready_: boolean;
60
- private readonly executor_: (
61
- ctx: IAgenticaContext<Model>,
62
- ) => Promise<IAgenticaPrompt<Model>[]>;
63
-
64
- /* -----------------------------------------------------------
65
- CONSTRUCTOR
66
- ----------------------------------------------------------- */
67
- /**
68
- * Initializer constructor.
69
- *
70
- * @param props Properties to construct the agent
71
- */
72
- public constructor(private readonly props: IAgenticaProps<Model>) {
73
- // OPERATIONS
74
- this.operations_ = AgenticaOperationComposer.compose({
75
- controllers: props.controllers,
76
- config: props.config,
77
- });
78
-
79
- // STATUS
80
- this.stack_ = [];
81
- this.listeners_ = new Map();
82
- this.prompt_histories_ = (props.histories ?? []).map((input) =>
83
- AgenticaPromptTransformer.transform({
84
- operations: this.operations_.group,
85
- input,
86
- }),
87
- );
88
-
89
- // STATUS
90
- this.token_usage_ = AgenticaTokenUsageAggregator.zero();
91
- this.ready_ = false;
92
- this.executor_ =
93
- typeof props.config?.executor === "function"
94
- ? props.config.executor
95
- : ChatGptAgent.execute(props.config?.executor ?? null);
96
- }
97
-
98
- /**
99
- * @internal
100
- */
101
- public clone(): Agentica<Model> {
102
- return new Agentica({
103
- ...this.props,
104
- histories: this.props.histories?.slice(),
105
- });
106
- }
107
-
108
- /* -----------------------------------------------------------
109
- ACCESSORS
110
- ----------------------------------------------------------- */
111
- /**
112
- * Conversate with the A.I. chatbot.
113
- *
114
- * User talks to the A.I. chatbot with the content.
115
- *
116
- * When the user's conversation implies the A.I. chatbot to execute a
117
- * function calling, the returned chat prompts will contain the
118
- * function calling information like {@link IAgenticaPrompt.IExecute}.
119
- *
120
- * @param content The content to talk
121
- * @returns List of newly created chat prompts
122
- */
123
- public async conversate(content: string): Promise<IAgenticaPrompt<Model>[]> {
124
- const prompt: IAgenticaPrompt.IText<"user"> = {
125
- type: "text",
126
- role: "user",
127
- text: content,
128
- };
129
- await this.dispatch(prompt);
130
-
131
- const newbie: IAgenticaPrompt<Model>[] = await this.executor_(
132
- this.getContext({
133
- prompt,
134
- usage: this.token_usage_,
135
- }),
136
- );
137
- this.prompt_histories_.push(prompt, ...newbie);
138
- return [prompt, ...newbie];
139
- }
140
-
141
- /**
142
- * Get configuration.
143
- */
144
- public getConfig(): IAgenticaConfig<Model> | undefined {
145
- return this.props.config;
146
- }
147
-
148
- /**
149
- * Get LLM vendor.
150
- */
151
- public getVendor(): IAgenticaVendor {
152
- return this.props.vendor;
153
- }
154
-
155
- /**
156
- * Get controllers.
157
- *
158
- * Get list of controllers, which are the collection of functions that
159
- * the "Super A.I. Chatbot" can execute.
160
- */
161
- public getControllers(): ReadonlyArray<IAgenticaController<Model>> {
162
- return this.props.controllers;
163
- }
164
-
165
- /**
166
- * Get operations.
167
- *
168
- * Get list of operations, which has capsuled the pair of controller
169
- * and function from the {@link getControllers controllers}.
170
- *
171
- * @returns
172
- */
173
- public getOperations(): ReadonlyArray<IAgenticaOperation<Model>> {
174
- return this.operations_.array;
175
- }
176
-
177
- /**
178
- * Get the chatbot's prompt histories.
179
- *
180
- * Get list of chat prompts that the chatbot has been conversated.
181
- *
182
- * @returns List of chat prompts
183
- */
184
- public getPromptHistories(): IAgenticaPrompt<Model>[] {
185
- return this.prompt_histories_;
186
- }
187
-
188
- /**
189
- * Get token usage of the A.I. chatbot.
190
- *
191
- * Entire token usage of the A.I. chatbot during the conversating
192
- * with the user by {@link conversate} method callings.
193
- *
194
- * @returns Cost of the A.I. chatbot
195
- */
196
- public getTokenUsage(): IAgenticaTokenUsage {
197
- return this.token_usage_;
198
- }
199
-
200
- /**
201
- * @internal
202
- */
203
- public getContext(props: {
204
- prompt: IAgenticaPrompt.IText<"user">;
205
- usage: IAgenticaTokenUsage;
206
- }): IAgenticaContext<Model> {
207
- const dispatch = (event: IAgenticaEvent<Model>) => this.dispatch(event);
208
- return {
209
- // APPLICATION
210
- operations: this.operations_,
211
- config: this.props.config,
212
-
213
- // STATES
214
- histories: this.prompt_histories_,
215
- stack: this.stack_,
216
- ready: () => this.ready_,
217
- prompt: props.prompt,
218
-
219
- // HANDLERS
220
- dispatch: (event) => this.dispatch(event),
221
- request: async (kind, body) => {
222
- // request information
223
- const event: IAgenticaEvent.IRequest = {
224
- type: "request",
225
- source: kind,
226
- body: {
227
- ...body,
228
- model: this.props.vendor.model,
229
- },
230
- options: this.props.vendor.options,
231
- };
232
- await dispatch(event);
233
-
234
- // completion
235
- const completion: OpenAI.ChatCompletion =
236
- await this.props.vendor.api.chat.completions.create(
237
- event.body,
238
- event.options,
239
- );
240
- AgenticaTokenUsageAggregator.aggregate({
241
- kind,
242
- completion,
243
- usage: props.usage,
244
- });
245
- await dispatch({
246
- type: "response",
247
- source: kind,
248
- body: event.body,
249
- options: event.options,
250
- value: completion,
251
- });
252
- return completion;
253
- },
254
- initialize: async () => {
255
- this.ready_ = true;
256
- await dispatch({
257
- type: "initialize",
258
- });
259
- },
260
- };
261
- }
262
-
263
- /* -----------------------------------------------------------
264
- EVENT HANDLERS
265
- ----------------------------------------------------------- */
266
- /**
267
- * Add an event listener.
268
- *
269
- * Add an event listener to be called whenever the event is emitted.
270
- *
271
- * @param type Type of event
272
- * @param listener Callback function to be called whenever the event is emitted
273
- */
274
- public on<Type extends IAgenticaEvent.Type>(
275
- type: Type,
276
- listener: (
277
- event: IAgenticaEvent.Mapper<Model>[Type],
278
- ) => void | Promise<void>,
279
- ): this {
280
- __map_take(this.listeners_, type, () => new Set()).add(listener);
281
- return this;
282
- }
283
-
284
- /**
285
- * Erase an event listener.
286
- *
287
- * Erase an event listener to stop calling the callback function.
288
- *
289
- * @param type Type of event
290
- * @param listener Callback function to erase
291
- */
292
- public off<Type extends IAgenticaEvent.Type>(
293
- type: Type,
294
- listener: (
295
- event: IAgenticaEvent.Mapper<Model>[Type],
296
- ) => void | Promise<void>,
297
- ): this {
298
- const set = this.listeners_.get(type);
299
- if (set) {
300
- set.delete(listener);
301
- if (set.size === 0) this.listeners_.delete(type);
302
- }
303
- return this;
304
- }
305
-
306
- private async dispatch<Event extends IAgenticaEvent<Model>>(
307
- event: Event,
308
- ): Promise<void> {
309
- const set = this.listeners_.get(event.type);
310
- if (set) {
311
- await Promise.all(
312
- Array.from(set).map(async (listener) => {
313
- try {
314
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
315
- await listener(event);
316
- } catch {
317
- /* empty */
318
- }
319
- }),
320
- );
321
- }
322
- }
323
- }
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+ import OpenAI from "openai";
3
+
4
+ import { ChatGptAgent } from "./chatgpt/ChatGptAgent";
5
+ import { AgenticaOperationComposer } from "./internal/AgenticaOperationComposer";
6
+ import { AgenticaPromptTransformer } from "./internal/AgenticaPromptTransformer";
7
+ import { AgenticaTokenUsageAggregator } from "./internal/AgenticaTokenUsageAggregator";
8
+ import { __map_take } from "./internal/__map_take";
9
+ import { IAgenticaConfig } from "./structures/IAgenticaConfig";
10
+ import { IAgenticaContext } from "./structures/IAgenticaContext";
11
+ import { IAgenticaController } from "./structures/IAgenticaController";
12
+ import { IAgenticaEvent } from "./structures/IAgenticaEvent";
13
+ import { IAgenticaOperation } from "./structures/IAgenticaOperation";
14
+ import { IAgenticaOperationCollection } from "./structures/IAgenticaOperationCollection";
15
+ import { IAgenticaOperationSelection } from "./structures/IAgenticaOperationSelection";
16
+ import { IAgenticaPrompt } from "./structures/IAgenticaPrompt";
17
+ import { IAgenticaProps } from "./structures/IAgenticaProps";
18
+ import { IAgenticaTokenUsage } from "./structures/IAgenticaTokenUsage";
19
+ import { IAgenticaVendor } from "./structures/IAgenticaVendor";
20
+
21
+ /**
22
+ * Nestia A.I. chatbot agent.
23
+ *
24
+ * `Agentica` is a facade class for the super A.I. chatbot agent
25
+ * which performs the {@link conversate user's conversation function}
26
+ * with LLM (Large Language Model) function calling and manages the
27
+ * {@link getPromptHistories prompt histories}.
28
+ *
29
+ * To understand and compose the `Agentica` class exactly, reference
30
+ * below types concentrating on the documentation comments please.
31
+ * Especially, you have to be careful about the {@link IAgenticaProps}
32
+ * type which is used in the {@link constructor} function.
33
+ *
34
+ * - Constructors
35
+ * - {@link IAgenticaProps}
36
+ * - {@link IAgenticaVendor}
37
+ * - {@link IAgenticaController}
38
+ * - {@link IAgenticaConfig}
39
+ * - {@link IAgenticaSystemPrompt}
40
+ * - Accessors
41
+ * - {@link IAgenticaOperation}
42
+ * - {@link IAgenticaPrompt}
43
+ * - {@link IAgenticaEvent}
44
+ * - {@link IAgenticaTokenUsage}
45
+ *
46
+ * @author Samchon
47
+ */
48
+ export class Agentica<Model extends ILlmSchema.Model> {
49
+ // THE OPERATIONS
50
+ private readonly operations_: IAgenticaOperationCollection<Model>;
51
+
52
+ // STACK
53
+ private readonly stack_: IAgenticaOperationSelection<Model>[];
54
+ private readonly prompt_histories_: IAgenticaPrompt<Model>[];
55
+ private readonly listeners_: Map<string, Set<Function>>;
56
+
57
+ // STATUS
58
+ private readonly token_usage_: IAgenticaTokenUsage;
59
+ private ready_: boolean;
60
+ private readonly executor_: (
61
+ ctx: IAgenticaContext<Model>,
62
+ ) => Promise<IAgenticaPrompt<Model>[]>;
63
+
64
+ /* -----------------------------------------------------------
65
+ CONSTRUCTOR
66
+ ----------------------------------------------------------- */
67
+ /**
68
+ * Initializer constructor.
69
+ *
70
+ * @param props Properties to construct the agent
71
+ */
72
+ public constructor(private readonly props: IAgenticaProps<Model>) {
73
+ // OPERATIONS
74
+ this.operations_ = AgenticaOperationComposer.compose({
75
+ controllers: props.controllers,
76
+ config: props.config,
77
+ });
78
+
79
+ // STATUS
80
+ this.stack_ = [];
81
+ this.listeners_ = new Map();
82
+ this.prompt_histories_ = (props.histories ?? []).map((input) =>
83
+ AgenticaPromptTransformer.transform({
84
+ operations: this.operations_.group,
85
+ input,
86
+ }),
87
+ );
88
+
89
+ // STATUS
90
+ this.token_usage_ = AgenticaTokenUsageAggregator.zero();
91
+ this.ready_ = false;
92
+ this.executor_ =
93
+ typeof props.config?.executor === "function"
94
+ ? props.config.executor
95
+ : ChatGptAgent.execute(props.config?.executor ?? null);
96
+ }
97
+
98
+ /**
99
+ * @internal
100
+ */
101
+ public clone(): Agentica<Model> {
102
+ return new Agentica({
103
+ ...this.props,
104
+ histories: this.props.histories?.slice(),
105
+ });
106
+ }
107
+
108
+ /* -----------------------------------------------------------
109
+ ACCESSORS
110
+ ----------------------------------------------------------- */
111
+ /**
112
+ * Conversate with the A.I. chatbot.
113
+ *
114
+ * User talks to the A.I. chatbot with the content.
115
+ *
116
+ * When the user's conversation implies the A.I. chatbot to execute a
117
+ * function calling, the returned chat prompts will contain the
118
+ * function calling information like {@link IAgenticaPrompt.IExecute}.
119
+ *
120
+ * @param content The content to talk
121
+ * @returns List of newly created chat prompts
122
+ */
123
+ public async conversate(content: string): Promise<IAgenticaPrompt<Model>[]> {
124
+ const prompt: IAgenticaPrompt.IText<"user"> = {
125
+ type: "text",
126
+ role: "user",
127
+ text: content,
128
+ };
129
+ await this.dispatch(prompt);
130
+
131
+ const newbie: IAgenticaPrompt<Model>[] = await this.executor_(
132
+ this.getContext({
133
+ prompt,
134
+ usage: this.token_usage_,
135
+ }),
136
+ );
137
+ this.prompt_histories_.push(prompt, ...newbie);
138
+ return [prompt, ...newbie];
139
+ }
140
+
141
+ /**
142
+ * Get configuration.
143
+ */
144
+ public getConfig(): IAgenticaConfig<Model> | undefined {
145
+ return this.props.config;
146
+ }
147
+
148
+ /**
149
+ * Get LLM vendor.
150
+ */
151
+ public getVendor(): IAgenticaVendor {
152
+ return this.props.vendor;
153
+ }
154
+
155
+ /**
156
+ * Get controllers.
157
+ *
158
+ * Get list of controllers, which are the collection of functions that
159
+ * the "Super A.I. Chatbot" can execute.
160
+ */
161
+ public getControllers(): ReadonlyArray<IAgenticaController<Model>> {
162
+ return this.props.controllers;
163
+ }
164
+
165
+ /**
166
+ * Get operations.
167
+ *
168
+ * Get list of operations, which has capsuled the pair of controller
169
+ * and function from the {@link getControllers controllers}.
170
+ *
171
+ * @returns
172
+ */
173
+ public getOperations(): ReadonlyArray<IAgenticaOperation<Model>> {
174
+ return this.operations_.array;
175
+ }
176
+
177
+ /**
178
+ * Get the chatbot's prompt histories.
179
+ *
180
+ * Get list of chat prompts that the chatbot has been conversated.
181
+ *
182
+ * @returns List of chat prompts
183
+ */
184
+ public getPromptHistories(): IAgenticaPrompt<Model>[] {
185
+ return this.prompt_histories_;
186
+ }
187
+
188
+ /**
189
+ * Get token usage of the A.I. chatbot.
190
+ *
191
+ * Entire token usage of the A.I. chatbot during the conversating
192
+ * with the user by {@link conversate} method callings.
193
+ *
194
+ * @returns Cost of the A.I. chatbot
195
+ */
196
+ public getTokenUsage(): IAgenticaTokenUsage {
197
+ return this.token_usage_;
198
+ }
199
+
200
+ /**
201
+ * @internal
202
+ */
203
+ public getContext(props: {
204
+ prompt: IAgenticaPrompt.IText<"user">;
205
+ usage: IAgenticaTokenUsage;
206
+ }): IAgenticaContext<Model> {
207
+ const dispatch = (event: IAgenticaEvent<Model>) => this.dispatch(event);
208
+ return {
209
+ // APPLICATION
210
+ operations: this.operations_,
211
+ config: this.props.config,
212
+
213
+ // STATES
214
+ histories: this.prompt_histories_,
215
+ stack: this.stack_,
216
+ ready: () => this.ready_,
217
+ prompt: props.prompt,
218
+
219
+ // HANDLERS
220
+ dispatch: (event) => this.dispatch(event),
221
+ request: async (kind, body) => {
222
+ // request information
223
+ const event: IAgenticaEvent.IRequest = {
224
+ type: "request",
225
+ source: kind,
226
+ body: {
227
+ ...body,
228
+ model: this.props.vendor.model,
229
+ },
230
+ options: this.props.vendor.options,
231
+ };
232
+ await dispatch(event);
233
+
234
+ // completion
235
+ const completion: OpenAI.ChatCompletion =
236
+ await this.props.vendor.api.chat.completions.create(
237
+ event.body,
238
+ event.options,
239
+ );
240
+ AgenticaTokenUsageAggregator.aggregate({
241
+ kind,
242
+ completion,
243
+ usage: props.usage,
244
+ });
245
+ await dispatch({
246
+ type: "response",
247
+ source: kind,
248
+ body: event.body,
249
+ options: event.options,
250
+ value: completion,
251
+ });
252
+ return completion;
253
+ },
254
+ initialize: async () => {
255
+ this.ready_ = true;
256
+ await dispatch({
257
+ type: "initialize",
258
+ });
259
+ },
260
+ };
261
+ }
262
+
263
+ /* -----------------------------------------------------------
264
+ EVENT HANDLERS
265
+ ----------------------------------------------------------- */
266
+ /**
267
+ * Add an event listener.
268
+ *
269
+ * Add an event listener to be called whenever the event is emitted.
270
+ *
271
+ * @param type Type of event
272
+ * @param listener Callback function to be called whenever the event is emitted
273
+ */
274
+ public on<Type extends IAgenticaEvent.Type>(
275
+ type: Type,
276
+ listener: (
277
+ event: IAgenticaEvent.Mapper<Model>[Type],
278
+ ) => void | Promise<void>,
279
+ ): this {
280
+ __map_take(this.listeners_, type, () => new Set()).add(listener);
281
+ return this;
282
+ }
283
+
284
+ /**
285
+ * Erase an event listener.
286
+ *
287
+ * Erase an event listener to stop calling the callback function.
288
+ *
289
+ * @param type Type of event
290
+ * @param listener Callback function to erase
291
+ */
292
+ public off<Type extends IAgenticaEvent.Type>(
293
+ type: Type,
294
+ listener: (
295
+ event: IAgenticaEvent.Mapper<Model>[Type],
296
+ ) => void | Promise<void>,
297
+ ): this {
298
+ const set = this.listeners_.get(type);
299
+ if (set) {
300
+ set.delete(listener);
301
+ if (set.size === 0) this.listeners_.delete(type);
302
+ }
303
+ return this;
304
+ }
305
+
306
+ private async dispatch<Event extends IAgenticaEvent<Model>>(
307
+ event: Event,
308
+ ): Promise<void> {
309
+ const set = this.listeners_.get(event.type);
310
+ if (set) {
311
+ await Promise.all(
312
+ Array.from(set).map(async (listener) => {
313
+ try {
314
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
315
+ await listener(event);
316
+ } catch {
317
+ /* empty */
318
+ }
319
+ }),
320
+ );
321
+ }
322
+ }
323
+ }