@agentica/core 0.27.3 → 0.29.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 (92) hide show
  1. package/README.md +17 -2
  2. package/lib/Agentica.d.ts +2 -1
  3. package/lib/Agentica.js +92 -60
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/MicroAgentica.d.ts +1 -0
  6. package/lib/MicroAgentica.js +88 -60
  7. package/lib/MicroAgentica.js.map +1 -1
  8. package/lib/constants/AgenticaDefaultPrompt.js +2 -18
  9. package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
  10. package/lib/context/AgenticaContext.d.ts +1 -1
  11. package/lib/context/MicroAgenticaContext.d.ts +2 -2
  12. package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -2
  13. package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
  14. package/lib/events/AgenticaEventBase.d.ts +9 -0
  15. package/lib/factory/events.d.ts +1 -2
  16. package/lib/factory/events.js +71 -7
  17. package/lib/factory/events.js.map +1 -1
  18. package/lib/factory/histories.js +27 -9
  19. package/lib/factory/histories.js.map +1 -1
  20. package/lib/histories/AgenticaCancelHistory.d.ts +1 -2
  21. package/lib/histories/AgenticaExecuteHistory.d.ts +0 -4
  22. package/lib/histories/AgenticaHistoryBase.d.ts +9 -0
  23. package/lib/histories/AgenticaSelectHistory.d.ts +1 -2
  24. package/lib/index.mjs +415 -731
  25. package/lib/index.mjs.map +1 -1
  26. package/lib/json/IAgenticaEventJson.d.ts +9 -1
  27. package/lib/json/IAgenticaHistoryJson.d.ts +11 -14
  28. package/lib/orchestrate/call.d.ts +2 -2
  29. package/lib/orchestrate/call.js +43 -72
  30. package/lib/orchestrate/call.js.map +1 -1
  31. package/lib/orchestrate/cancel.d.ts +1 -2
  32. package/lib/orchestrate/cancel.js +14 -39
  33. package/lib/orchestrate/cancel.js.map +1 -1
  34. package/lib/orchestrate/describe.d.ts +1 -2
  35. package/lib/orchestrate/describe.js +5 -17
  36. package/lib/orchestrate/describe.js.map +1 -1
  37. package/lib/orchestrate/execute.d.ts +1 -2
  38. package/lib/orchestrate/execute.js +9 -13
  39. package/lib/orchestrate/execute.js.map +1 -1
  40. package/lib/orchestrate/initialize.d.ts +1 -2
  41. package/lib/orchestrate/initialize.js +4 -12
  42. package/lib/orchestrate/initialize.js.map +1 -1
  43. package/lib/orchestrate/internal/{cancelFunction.js → cancelFunctionFromContext.js} +7 -7
  44. package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -0
  45. package/lib/orchestrate/internal/selectFunctionFromContext.js +24 -0
  46. package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -0
  47. package/lib/orchestrate/select.d.ts +1 -2
  48. package/lib/orchestrate/select.js +21 -52
  49. package/lib/orchestrate/select.js.map +1 -1
  50. package/lib/structures/IAgenticaConfig.d.ts +1 -3
  51. package/lib/structures/IAgenticaExecutor.d.ts +6 -7
  52. package/lib/structures/IAgenticaVendor.d.ts +14 -0
  53. package/lib/structures/IMicroAgenticaExecutor.d.ts +2 -3
  54. package/lib/transformers/transformHistory.js +13 -8
  55. package/lib/transformers/transformHistory.js.map +1 -1
  56. package/lib/utils/ChatGptCompletionMessageUtil.js +5 -353
  57. package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
  58. package/package.json +9 -7
  59. package/src/Agentica.ts +127 -87
  60. package/src/MicroAgentica.ts +118 -81
  61. package/src/constants/AgenticaDefaultPrompt.ts +3 -20
  62. package/src/context/AgenticaContext.ts +1 -1
  63. package/src/context/MicroAgenticaContext.ts +2 -2
  64. package/src/context/internal/AgenticaOperationComposer.spec.ts +1 -2
  65. package/src/events/AgenticaEventBase.ts +12 -0
  66. package/src/factory/events.ts +78 -8
  67. package/src/factory/histories.ts +41 -11
  68. package/src/histories/AgenticaCancelHistory.ts +1 -2
  69. package/src/histories/AgenticaExecuteHistory.ts +0 -5
  70. package/src/histories/AgenticaHistoryBase.ts +12 -0
  71. package/src/histories/AgenticaSelectHistory.ts +1 -2
  72. package/src/json/IAgenticaEventJson.ts +11 -1
  73. package/src/json/IAgenticaHistoryJson.ts +14 -17
  74. package/src/orchestrate/call.ts +59 -109
  75. package/src/orchestrate/cancel.ts +77 -100
  76. package/src/orchestrate/describe.ts +16 -36
  77. package/src/orchestrate/execute.ts +17 -37
  78. package/src/orchestrate/initialize.ts +37 -50
  79. package/src/orchestrate/internal/{cancelFunction.ts → cancelFunctionFromContext.ts} +11 -11
  80. package/src/orchestrate/internal/{selectFunction.ts → selectFunctionFromContext.ts} +18 -13
  81. package/src/orchestrate/select.ts +113 -152
  82. package/src/structures/IAgenticaConfig.ts +1 -3
  83. package/src/structures/IAgenticaExecutor.ts +10 -8
  84. package/src/structures/IAgenticaVendor.ts +15 -0
  85. package/src/structures/IMicroAgenticaExecutor.ts +2 -3
  86. package/src/transformers/transformHistory.ts +19 -20
  87. package/src/utils/ChatGptCompletionMessageUtil.ts +5 -6
  88. package/lib/orchestrate/internal/cancelFunction.js.map +0 -1
  89. package/lib/orchestrate/internal/selectFunction.js +0 -35
  90. package/lib/orchestrate/internal/selectFunction.js.map +0 -1
  91. /package/lib/orchestrate/internal/{cancelFunction.d.ts → cancelFunctionFromContext.d.ts} +0 -0
  92. /package/lib/orchestrate/internal/{selectFunction.d.ts → selectFunctionFromContext.d.ts} +0 -0
package/src/Agentica.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
+ import type OpenAI from "openai";
3
+
4
+ import { Semaphore } from "tstl";
5
+ import { v4 } from "uuid";
2
6
 
3
7
  import type { AgenticaContext } from "./context/AgenticaContext";
4
8
  import type { AgenticaOperation } from "./context/AgenticaOperation";
5
9
  import type { AgenticaOperationCollection } from "./context/AgenticaOperationCollection";
6
10
  import type { AgenticaOperationSelection } from "./context/AgenticaOperationSelection";
11
+ import type { AgenticaEventSource } from "./events";
7
12
  import type { AgenticaEvent } from "./events/AgenticaEvent";
8
13
  import type { AgenticaRequestEvent } from "./events/AgenticaRequestEvent";
14
+ import type { AgenticaUserMessageEvent } from "./events/AgenticaUserMessageEvent";
9
15
  import type { AgenticaUserMessageContent } from "./histories";
10
16
  import type { AgenticaHistory } from "./histories/AgenticaHistory";
11
17
  import type { AgenticaUserMessageHistory } from "./histories/AgenticaUserMessageHistory";
@@ -17,7 +23,6 @@ import type { IAgenticaVendor } from "./structures/IAgenticaVendor";
17
23
  import { AgenticaTokenUsage } from "./context/AgenticaTokenUsage";
18
24
  import { AgenticaOperationComposer } from "./context/internal/AgenticaOperationComposer";
19
25
  import { AgenticaTokenUsageAggregator } from "./context/internal/AgenticaTokenUsageAggregator";
20
- import { createUserMessageHistory } from "./factory";
21
26
  import { createInitializeEvent, createRequestEvent, createUserMessageEvent } from "./factory/events";
22
27
  import { execute } from "./orchestrate/execute";
23
28
  import { transformHistory } from "./transformers/transformHistory";
@@ -62,11 +67,10 @@ export class Agentica<Model extends ILlmSchema.Model> {
62
67
  private readonly listeners_: Map<string, Set<(event: AgenticaEvent<Model>) => Promise<void> | void>>;
63
68
 
64
69
  // STATUS
70
+ private readonly executor_: (ctx: AgenticaContext<Model>) => Promise<void>;
71
+ private readonly semaphore_: Semaphore | null;
65
72
  private readonly token_usage_: AgenticaTokenUsage;
66
73
  private ready_: boolean;
67
- private readonly executor_: (
68
- ctx: AgenticaContext<Model>,
69
- ) => Promise<AgenticaHistory<Model>[]>;
70
74
 
71
75
  /* -----------------------------------------------------------
72
76
  CONSTRUCTOR
@@ -83,7 +87,7 @@ export class Agentica<Model extends ILlmSchema.Model> {
83
87
  config: props.config,
84
88
  });
85
89
 
86
- // STATUS
90
+ // STACK
87
91
  this.stack_ = [];
88
92
  this.listeners_ = new Map();
89
93
  this.histories_ = (props.histories ?? []).map(input =>
@@ -94,16 +98,21 @@ export class Agentica<Model extends ILlmSchema.Model> {
94
98
  );
95
99
 
96
100
  // STATUS
101
+ this.executor_
102
+ = typeof props.config?.executor === "function"
103
+ ? props.config.executor
104
+ : execute(props.config?.executor ?? null);
105
+ this.semaphore_ = props.vendor.semaphore != null
106
+ ? typeof props.vendor.semaphore === "object"
107
+ ? props.vendor.semaphore
108
+ : new Semaphore(props.vendor.semaphore)
109
+ : null;
97
110
  this.token_usage_ = this.props.tokenUsage !== undefined
98
111
  ? this.props.tokenUsage instanceof AgenticaTokenUsage
99
112
  ? this.props.tokenUsage
100
113
  : new AgenticaTokenUsage(this.props.tokenUsage)
101
114
  : AgenticaTokenUsage.zero();
102
115
  this.ready_ = false;
103
- this.executor_
104
- = typeof props.config?.executor === "function"
105
- ? props.config.executor
106
- : execute(props.config?.executor ?? null);
107
116
  }
108
117
 
109
118
  /**
@@ -140,7 +149,23 @@ export class Agentica<Model extends ILlmSchema.Model> {
140
149
  abortSignal?: AbortSignal;
141
150
  } = {},
142
151
  ): Promise<AgenticaHistory<Model>[]> {
143
- const prompt: AgenticaUserMessageHistory = createUserMessageHistory({
152
+ const historyGetters: Array<() => Promise<AgenticaHistory<Model>>> = [];
153
+ const dispatch = (event: AgenticaEvent<Model>): void => {
154
+ this.dispatch(event).catch(() => {});
155
+ if ("toHistory" in event) {
156
+ if ("join" in event) {
157
+ historyGetters.push(async () => {
158
+ await event.join();
159
+ return event.toHistory();
160
+ });
161
+ }
162
+ else {
163
+ historyGetters.push(async () => event.toHistory());
164
+ }
165
+ }
166
+ };
167
+
168
+ const prompt: AgenticaUserMessageEvent = createUserMessageEvent({
144
169
  contents: Array.isArray(content)
145
170
  ? content
146
171
  : typeof content === "string"
@@ -150,22 +175,22 @@ export class Agentica<Model extends ILlmSchema.Model> {
150
175
  }]
151
176
  : [content],
152
177
  });
178
+ dispatch(prompt);
153
179
 
154
- this.dispatch(
155
- createUserMessageEvent({
156
- contents: prompt.contents,
157
- }),
158
- ).catch(() => {});
159
-
160
- const newbie: AgenticaHistory<Model>[] = await this.executor_(
180
+ await this.executor_(
161
181
  this.getContext({
162
- prompt,
182
+ dispatch,
183
+ prompt: prompt.toHistory(),
163
184
  abortSignal: options.abortSignal,
164
185
  usage: this.token_usage_,
165
186
  }),
166
187
  );
167
- this.histories_.push(prompt, ...newbie);
168
- return [prompt, ...newbie];
188
+
189
+ const completed: AgenticaHistory<Model>[] = await Promise.all(
190
+ historyGetters.map(async h => h()),
191
+ );
192
+ this.histories_.push(...completed);
193
+ return completed;
169
194
  }
170
195
 
171
196
  /**
@@ -233,9 +258,78 @@ export class Agentica<Model extends ILlmSchema.Model> {
233
258
  public getContext(props: {
234
259
  prompt: AgenticaUserMessageHistory;
235
260
  usage: AgenticaTokenUsage;
261
+ dispatch: (event: AgenticaEvent<Model>) => void;
236
262
  abortSignal?: AbortSignal;
237
263
  }): AgenticaContext<Model> {
238
- const dispatch = async (event: AgenticaEvent<Model>) => this.dispatch(event);
264
+ const request = async (
265
+ source: AgenticaEventSource,
266
+ body: Omit<OpenAI.ChatCompletionCreateParamsStreaming, "model" | "stream">,
267
+ ): Promise<ReadableStream<OpenAI.Chat.Completions.ChatCompletionChunk>> => {
268
+ const event: AgenticaRequestEvent = createRequestEvent({
269
+ source,
270
+ body: {
271
+ ...body,
272
+ model: this.props.vendor.model,
273
+ stream: true,
274
+ stream_options: {
275
+ include_usage: true,
276
+ },
277
+ },
278
+ options: {
279
+ ...this.props.vendor.options,
280
+ signal: props.abortSignal,
281
+ },
282
+ });
283
+ props.dispatch(event);
284
+
285
+ // completion
286
+ const completion = await this.props.vendor.api.chat.completions.create(
287
+ event.body,
288
+ event.options,
289
+ );
290
+
291
+ const [streamForEvent, temporaryStream] = StreamUtil.transform(
292
+ completion.toReadableStream() as ReadableStream<Uint8Array>,
293
+ value =>
294
+ ChatGptCompletionMessageUtil.transformCompletionChunk(value),
295
+ ).tee();
296
+
297
+ const [streamForAggregate, streamForReturn] = temporaryStream.tee();
298
+
299
+ (async () => {
300
+ const reader = streamForAggregate.getReader();
301
+ while (true) {
302
+ const chunk = await reader.read();
303
+ if (chunk.done) {
304
+ break;
305
+ }
306
+ if (chunk.value.usage != null) {
307
+ AgenticaTokenUsageAggregator.aggregate({
308
+ kind: source,
309
+ completionUsage: chunk.value.usage,
310
+ usage: props.usage,
311
+ });
312
+ }
313
+ }
314
+ })().catch(() => {});
315
+
316
+ const [streamForStream, streamForJoin] = streamForEvent.tee();
317
+ props.dispatch({
318
+ id: v4(),
319
+ type: "response",
320
+ source,
321
+ stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
322
+ body: event.body,
323
+ options: event.options,
324
+ join: async () => {
325
+ const chunks = await StreamUtil.readAll(streamForJoin);
326
+ return ChatGptCompletionMessageUtil.merge(chunks);
327
+ },
328
+ created_at: new Date().toISOString(),
329
+ });
330
+ return streamForReturn;
331
+ };
332
+
239
333
  return {
240
334
  // APPLICATION
241
335
  operations: this.operations_,
@@ -249,75 +343,21 @@ export class Agentica<Model extends ILlmSchema.Model> {
249
343
  abortSignal: props.abortSignal,
250
344
 
251
345
  // HANDLERS
252
- dispatch: async event => this.dispatch(event),
253
- request: async (source, body) => {
254
- // request information
255
- const event: AgenticaRequestEvent = createRequestEvent({
256
- source,
257
- body: {
258
- ...body,
259
- model: this.props.vendor.model,
260
- stream: true,
261
- stream_options: {
262
- include_usage: true,
263
- },
264
- },
265
- options: {
266
- ...this.props.vendor.options,
267
- signal: props.abortSignal,
268
- },
269
- });
270
- await dispatch(event);
271
-
272
- // completion
273
- const completion = await this.props.vendor.api.chat.completions.create(
274
- event.body,
275
- event.options,
276
- );
277
-
278
- const [streamForEvent, temporaryStream] = StreamUtil.transform(
279
- completion.toReadableStream() as ReadableStream<Uint8Array>,
280
- value =>
281
- ChatGptCompletionMessageUtil.transformCompletionChunk(value),
282
- ).tee();
283
-
284
- const [streamForAggregate, streamForReturn] = temporaryStream.tee();
285
-
286
- (async () => {
287
- const reader = streamForAggregate.getReader();
288
- while (true) {
289
- const chunk = await reader.read();
290
- if (chunk.done) {
291
- break;
292
- }
293
- if (chunk.value.usage != null) {
294
- AgenticaTokenUsageAggregator.aggregate({
295
- kind: source,
296
- completionUsage: chunk.value.usage,
297
- usage: props.usage,
298
- });
299
- }
346
+ dispatch: props.dispatch,
347
+ request: this.semaphore_ === null
348
+ ? request
349
+ : async (source, body) => {
350
+ await this.semaphore_!.acquire();
351
+ try {
352
+ return await request(source, body);
300
353
  }
301
- })().catch(() => {});
302
-
303
- const [streamForStream, streamForJoin] = streamForEvent.tee();
304
- await dispatch({
305
- type: "response",
306
- source,
307
- stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
308
- body: event.body,
309
- options: event.options,
310
- join: async () => {
311
- const chunks = await StreamUtil.readAll(streamForJoin);
312
- return ChatGptCompletionMessageUtil.merge(chunks);
313
- },
314
- });
315
-
316
- return streamForReturn;
317
- },
354
+ finally {
355
+ void this.semaphore_!.release().catch(() => {});
356
+ }
357
+ },
318
358
  initialize: async () => {
319
359
  this.ready_ = true;
320
- await dispatch(createInitializeEvent());
360
+ props.dispatch(createInitializeEvent());
321
361
  },
322
362
  };
323
363
  }
@@ -1,13 +1,17 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
+ import type OpenAI from "openai";
3
+
4
+ import { Semaphore } from "tstl";
5
+ import { v4 } from "uuid";
2
6
 
3
7
  import type { AgenticaOperation } from "./context/AgenticaOperation";
4
8
  import type { AgenticaOperationCollection } from "./context/AgenticaOperationCollection";
5
9
  import type { MicroAgenticaContext } from "./context/MicroAgenticaContext";
10
+ import type { AgenticaUserMessageEvent } from "./events";
6
11
  import type { AgenticaRequestEvent } from "./events/AgenticaRequestEvent";
7
12
  import type { MicroAgenticaEvent } from "./events/MicroAgenticaEvent";
8
13
  import type { AgenticaUserMessageContent } from "./histories";
9
14
  import type { AgenticaExecuteHistory } from "./histories/AgenticaExecuteHistory";
10
- import type { AgenticaUserMessageHistory } from "./histories/AgenticaUserMessageHistory";
11
15
  import type { MicroAgenticaHistory } from "./histories/MicroAgenticaHistory";
12
16
  import type { IAgenticaController } from "./structures/IAgenticaController";
13
17
  import type { IAgenticaVendor } from "./structures/IAgenticaVendor";
@@ -17,7 +21,6 @@ import type { IMicroAgenticaProps } from "./structures/IMicroAgenticaProps";
17
21
  import { AgenticaTokenUsage } from "./context/AgenticaTokenUsage";
18
22
  import { AgenticaOperationComposer } from "./context/internal/AgenticaOperationComposer";
19
23
  import { AgenticaTokenUsageAggregator } from "./context/internal/AgenticaTokenUsageAggregator";
20
- import { createUserMessageHistory } from "./factory";
21
24
  import { createRequestEvent, createUserMessageEvent } from "./factory/events";
22
25
  import { call, describe } from "./orchestrate";
23
26
  import { transformHistory } from "./transformers/transformHistory";
@@ -64,6 +67,8 @@ export class MicroAgentica<Model extends ILlmSchema.Model> {
64
67
  Set<(event: MicroAgenticaEvent<Model>) => Promise<void>>
65
68
  >;
66
69
 
70
+ private readonly semaphore_: Semaphore | null;
71
+
67
72
  /* -----------------------------------------------------------
68
73
  CONSTRUCTOR
69
74
  ----------------------------------------------------------- */
@@ -89,6 +94,11 @@ export class MicroAgentica<Model extends ILlmSchema.Model> {
89
94
  : new AgenticaTokenUsage(this.props.tokenUsage)
90
95
  : AgenticaTokenUsage.zero();
91
96
  this.listeners_ = new Map();
97
+ this.semaphore_ = props.vendor.semaphore != null
98
+ ? typeof props.vendor.semaphore === "object"
99
+ ? props.vendor.semaphore
100
+ : new Semaphore(props.vendor.semaphore)
101
+ : null;
92
102
  }
93
103
 
94
104
  /**
@@ -119,7 +129,23 @@ export class MicroAgentica<Model extends ILlmSchema.Model> {
119
129
  public async conversate(
120
130
  content: string | AgenticaUserMessageContent | Array<AgenticaUserMessageContent>,
121
131
  ): Promise<MicroAgenticaHistory<Model>[]> {
122
- const talk = createUserMessageHistory({
132
+ const histories: Array<() => Promise<MicroAgenticaHistory<Model>>> = [];
133
+ const dispatch = (event: MicroAgenticaEvent<Model>): void => {
134
+ this.dispatch(event).catch(() => {});
135
+ if ("toHistory" in event) {
136
+ if ("join" in event) {
137
+ histories.push(async () => {
138
+ await event.join();
139
+ return event.toHistory();
140
+ });
141
+ }
142
+ else {
143
+ histories.push(async () => event.toHistory());
144
+ }
145
+ }
146
+ };
147
+
148
+ const prompt: AgenticaUserMessageEvent = createUserMessageEvent({
123
149
  contents: Array.isArray(content)
124
150
  ? content
125
151
  : typeof content === "string"
@@ -129,29 +155,26 @@ export class MicroAgentica<Model extends ILlmSchema.Model> {
129
155
  }]
130
156
  : [content],
131
157
  });
132
- this.dispatch(
133
- createUserMessageEvent({
134
- contents: talk.contents,
135
- }),
136
- ).catch(() => {});
158
+ dispatch(prompt);
137
159
 
138
160
  const ctx: MicroAgenticaContext<Model> = this.getContext({
139
- prompt: talk,
161
+ prompt,
162
+ dispatch,
140
163
  usage: this.token_usage_,
141
164
  });
142
- const histories: MicroAgenticaHistory<Model>[] = await call(
165
+ const executes: AgenticaExecuteHistory<Model>[] = await call(
143
166
  ctx,
144
167
  this.operations_.array,
145
- ) as MicroAgenticaHistory<Model>[];
146
- const executes: AgenticaExecuteHistory<Model>[] = histories.filter(p => p.type === "execute");
147
- if (executes.length
148
- && ctx.config?.executor?.describe !== null
149
- && ctx.config?.executor?.describe !== false) {
150
- histories.push(...await describe(ctx, executes));
168
+ );
169
+ if (executes.length) {
170
+ await describe(ctx, executes);
151
171
  }
152
172
 
153
- this.histories_.push(talk, ...histories);
154
- return histories;
173
+ const completed: MicroAgenticaHistory<Model>[] = await Promise.all(
174
+ histories.map(async h => h()),
175
+ );
176
+ this.histories_.push(...completed);
177
+ return completed;
155
178
  }
156
179
 
157
180
  /**
@@ -217,79 +240,93 @@ export class MicroAgentica<Model extends ILlmSchema.Model> {
217
240
  * @internal
218
241
  */
219
242
  public getContext(props: {
220
- prompt: AgenticaUserMessageHistory;
243
+ prompt: AgenticaUserMessageEvent;
221
244
  usage: AgenticaTokenUsage;
245
+ dispatch: (event: MicroAgenticaEvent<Model>) => void;
222
246
  }): MicroAgenticaContext<Model> {
223
- const dispatch = this.dispatch.bind(this);
224
- return {
225
- operations: this.operations_,
226
- config: this.props.config,
227
-
228
- histories: this.histories_,
229
- prompt: props.prompt,
230
- dispatch,
231
- request: async (source, body) => {
232
- // request information
233
- const event: AgenticaRequestEvent = createRequestEvent({
234
- source,
235
- body: {
236
- ...body,
237
- model: this.props.vendor.model,
238
- stream: true,
239
- stream_options: {
240
- include_usage: true,
241
- },
247
+ const request = async (
248
+ source: MicroAgenticaEvent.Source,
249
+ body: Omit<OpenAI.ChatCompletionCreateParamsStreaming, "model" | "stream">,
250
+ ): Promise<ReadableStream<OpenAI.Chat.Completions.ChatCompletionChunk>> => {
251
+ const event: AgenticaRequestEvent = createRequestEvent({
252
+ source,
253
+ body: {
254
+ ...body,
255
+ model: this.props.vendor.model,
256
+ stream: true,
257
+ stream_options: {
258
+ include_usage: true,
242
259
  },
243
- options: this.props.vendor.options,
244
- });
245
- await dispatch(event);
260
+ },
261
+ options: this.props.vendor.options,
262
+ });
263
+ props.dispatch(event);
246
264
 
247
- // completion
248
- const completion = await this.props.vendor.api.chat.completions.create(
249
- event.body,
250
- event.options,
251
- );
265
+ // completion
266
+ const completion = await this.props.vendor.api.chat.completions.create(
267
+ event.body,
268
+ event.options,
269
+ );
252
270
 
253
- const [streamForEvent, temporaryStream] = StreamUtil.transform(
254
- completion.toReadableStream() as ReadableStream<Uint8Array>,
255
- value =>
256
- ChatGptCompletionMessageUtil.transformCompletionChunk(value),
257
- ).tee();
271
+ const [streamForEvent, temporaryStream] = StreamUtil.transform(
272
+ completion.toReadableStream() as ReadableStream<Uint8Array>,
273
+ value =>
274
+ ChatGptCompletionMessageUtil.transformCompletionChunk(value),
275
+ ).tee();
258
276
 
259
- const [streamForAggregate, streamForReturn] = temporaryStream.tee();
277
+ const [streamForAggregate, streamForReturn] = temporaryStream.tee();
260
278
 
261
- void (async () => {
262
- const reader = streamForAggregate.getReader();
263
- while (true) {
264
- const chunk = await reader.read();
265
- if (chunk.done) {
266
- break;
267
- }
268
- if (chunk.value.usage != null) {
269
- AgenticaTokenUsageAggregator.aggregate({
270
- kind: source,
271
- completionUsage: chunk.value.usage,
272
- usage: props.usage,
273
- });
274
- }
279
+ void (async () => {
280
+ const reader = streamForAggregate.getReader();
281
+ while (true) {
282
+ const chunk = await reader.read();
283
+ if (chunk.done) {
284
+ break;
275
285
  }
276
- })();
286
+ if (chunk.value.usage != null) {
287
+ AgenticaTokenUsageAggregator.aggregate({
288
+ kind: source,
289
+ completionUsage: chunk.value.usage,
290
+ usage: props.usage,
291
+ });
292
+ }
293
+ }
294
+ })().catch(() => {});
277
295
 
278
- const [streamForStream, streamForJoin] = streamForEvent.tee();
279
- await dispatch({
280
- type: "response",
281
- source,
282
- stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
283
- body: event.body,
284
- options: event.options,
285
- join: async () => {
286
- const chunks = await StreamUtil.readAll(streamForJoin);
287
- return ChatGptCompletionMessageUtil.merge(chunks);
288
- },
289
- });
296
+ const [streamForStream, streamForJoin] = streamForEvent.tee();
297
+ props.dispatch({
298
+ id: v4(),
299
+ type: "response",
300
+ source,
301
+ stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
302
+ body: event.body,
303
+ options: event.options,
304
+ join: async () => {
305
+ const chunks = await StreamUtil.readAll(streamForJoin);
306
+ return ChatGptCompletionMessageUtil.merge(chunks);
307
+ },
308
+ created_at: new Date().toISOString(),
309
+ });
310
+ return streamForReturn;
311
+ };
312
+ return {
313
+ operations: this.operations_,
314
+ config: this.props.config,
290
315
 
291
- return streamForReturn;
292
- },
316
+ histories: this.histories_,
317
+ prompt: props.prompt,
318
+ dispatch: props.dispatch,
319
+ request: this.semaphore_ === null
320
+ ? request
321
+ : async (source, body) => {
322
+ await this.semaphore_!.acquire();
323
+ try {
324
+ return await request(source, body);
325
+ }
326
+ finally {
327
+ void this.semaphore_!.release().catch(() => {});
328
+ }
329
+ },
293
330
  };
294
331
  }
295
332
 
@@ -1,5 +1,7 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
+ import { is_node } from "tstl";
4
+
3
5
  import type { IAgenticaConfig } from "../structures/IAgenticaConfig";
4
6
  import type { IMicroAgenticaConfig } from "../structures/IMicroAgenticaConfig";
5
7
  import type { IMicroAgenticaSystemPrompt } from "../structures/IMicroAgenticaSystemPrompt";
@@ -8,30 +10,11 @@ import { Singleton } from "../utils/Singleton";
8
10
 
9
11
  import { AgenticaSystemPrompt } from "./AgenticaSystemPrompt";
10
12
 
11
- /**
12
- * @TODO maybe this code will rewrite
13
- */
14
- const isNode = new Singleton(() => {
15
- const isObject = (obj: any) => typeof obj === "object" && obj !== null;
16
- return (
17
- // eslint-disable-next-line no-restricted-globals
18
- typeof global === "object"
19
- // eslint-disable-next-line no-restricted-globals
20
- && isObject(global)
21
- // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
22
- && isObject(global.process)
23
- // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
24
- && isObject(global.process.versions)
25
- // eslint-disable-next-line node/prefer-global/process, no-restricted-globals
26
- && typeof global.process.versions.node !== "undefined"
27
- );
28
- });
29
-
30
13
  /**
31
14
  * @TODO maybe replace `process` property for lint pass
32
15
  */
33
16
  const getLocale = new Singleton(() =>
34
- isNode.get()
17
+ is_node()
35
18
  // eslint-disable-next-line node/prefer-global/process
36
19
  ? (process.env.LANG?.split(".")[0] ?? "en-US")
37
20
  : navigator.language,
@@ -115,7 +115,7 @@ export interface AgenticaContext<Model extends ILlmSchema.Model> {
115
115
  *
116
116
  * @param event Event to deliver
117
117
  */
118
- dispatch: (event: AgenticaEvent<Model>) => Promise<void>;
118
+ dispatch: (event: AgenticaEvent<Model>) => void;
119
119
 
120
120
  /**
121
121
  * Request to the OpenAI server.
@@ -75,11 +75,11 @@ export interface MicroAgenticaContext<Model extends ILlmSchema.Model> {
75
75
  * Dispatch event.
76
76
  *
77
77
  * Dispatch event so that the agent can be handle the event
78
- * through the {@link Agentica.on} function.
78
+ * through the {@link MicroAgentica.on} function.
79
79
  *
80
80
  * @param event Event to deliver
81
81
  */
82
- dispatch: (event: MicroAgenticaEvent<Model>) => Promise<void>;
82
+ dispatch: (event: MicroAgenticaEvent<Model>) => void;
83
83
 
84
84
  /**
85
85
  * Request to the OpenAI server.
@@ -87,7 +87,6 @@ async function createMockMcpController(name: string, functions: IMcpLlmFunction<
87
87
 
88
88
  describe("a AgenticaOperationComposer", () => {
89
89
  beforeAll(async () => {
90
- // eslint-disable-next-line ts/no-unsafe-call
91
90
  const server = await createServer({
92
91
  name: "calculator",
93
92
  version: "1.0.0",
@@ -97,7 +96,7 @@ describe("a AgenticaOperationComposer", () => {
97
96
 
98
97
  await Promise.all([
99
98
  client.connect(clientTransport),
100
- // eslint-disable-next-line ts/no-unsafe-call
99
+
101
100
  server.connect(serverTransport),
102
101
  ]);
103
102
  });
@@ -1,6 +1,18 @@
1
+ import type { tags } from "typia";
2
+
1
3
  export interface AgenticaEventBase<Type extends string> {
4
+ /**
5
+ * Primary key of the event.
6
+ */
7
+ id: string;
8
+
2
9
  /**
3
10
  * Discriminator type.
4
11
  */
5
12
  type: Type;
13
+
14
+ /**
15
+ * Creation timestamp of the event.
16
+ */
17
+ created_at: string & tags.Format<"date-time">;
6
18
  }