@agentica/core 0.32.3 → 0.32.4

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.
@@ -149,7 +149,7 @@ describe("streamUtil", () => {
149
149
  const stringResult = await StreamUtil.reduce<number, string>(
150
150
  stringStream,
151
151
  (acc, cur) => acc + cur.toString(),
152
- "",
152
+ { initial: ""},
153
153
  );
154
154
 
155
155
  expect(stringResult).toBe("123");
@@ -160,7 +160,7 @@ describe("streamUtil", () => {
160
160
  const sumResult = await StreamUtil.reduce<number, number>(
161
161
  sumStream,
162
162
  (acc, cur) => acc + cur,
163
- 0,
163
+ { initial: 0 },
164
164
  );
165
165
 
166
166
  expect(sumResult).toBe(15);
@@ -171,6 +171,7 @@ describe("streamUtil", () => {
171
171
  const noInitialResult = await StreamUtil.reduce<number>(
172
172
  noInitialStream,
173
173
  (acc, cur) => acc + cur,
174
+ { initial: 0 },
174
175
  );
175
176
 
176
177
  expect(noInitialResult).toBe(10);
@@ -181,7 +182,7 @@ describe("streamUtil", () => {
181
182
  const emptyResult = await StreamUtil.reduce<number, string>(
182
183
  emptyStream,
183
184
  (acc, cur) => acc + cur.toString(),
184
- "initial value",
185
+ { initial: "initial value" },
185
186
  );
186
187
 
187
188
  expect(emptyResult).toBe("initial value");
@@ -192,6 +193,7 @@ describe("streamUtil", () => {
192
193
  const emptyNoInitialResult = await StreamUtil.reduce<number>(
193
194
  emptyNoInitialStream,
194
195
  (acc, cur) => acc + cur,
196
+ { initial: 0 },
195
197
  );
196
198
 
197
199
  expect(emptyNoInitialResult).toBeNull();
@@ -202,7 +204,7 @@ describe("streamUtil", () => {
202
204
  const stringResult = await StreamUtil.reduce<number, string>(
203
205
  stringStream,
204
206
  (acc, cur) => acc + cur.toString(),
205
- "",
207
+ { initial: "" },
206
208
  );
207
209
 
208
210
  expect(stringResult).toBe("123");
@@ -213,6 +215,7 @@ describe("streamUtil", () => {
213
215
  const noInitialResult = await StreamUtil.reduce<number>(
214
216
  noInitialStream,
215
217
  (acc, cur) => acc + cur,
218
+ { initial: 0 },
216
219
  );
217
220
 
218
221
  expect(noInitialResult).toBe(10);
@@ -229,7 +232,7 @@ describe("streamUtil", () => {
229
232
  }
230
233
  return [...acc, `item${cur}`];
231
234
  },
232
- [],
235
+ { initial: [] },
233
236
  );
234
237
 
235
238
  expect(transformResult).toEqual(["item1", "item2", "item3"]);
@@ -240,7 +243,7 @@ describe("streamUtil", () => {
240
243
  const emptyResult = await StreamUtil.reduce<number, string>(
241
244
  emptyStream,
242
245
  (acc, cur) => acc + cur.toString(),
243
- "initial",
246
+ { initial: "initial" },
244
247
  );
245
248
 
246
249
  expect(emptyResult).toBe("initial");
@@ -256,7 +259,7 @@ describe("streamUtil", () => {
256
259
  const delayResult = await StreamUtil.reduce<number, number>(
257
260
  delayStream,
258
261
  (acc, cur) => acc + cur,
259
- 0,
262
+ { initial: 0 },
260
263
  );
261
264
 
262
265
  expect(delayResult).toBe(6);
@@ -274,7 +277,7 @@ describe("streamUtil", () => {
274
277
  }
275
278
  return acc + cur;
276
279
  },
277
- 0,
280
+ { initial: 0 },
278
281
  ),
279
282
  ).rejects.toThrow("Test error");
280
283
  });
@@ -285,7 +288,7 @@ describe("streamUtil", () => {
285
288
  const result = await StreamUtil.reduce<number | null | undefined, number>(
286
289
  stream,
287
290
  (acc, cur) => (acc ?? 0) + (cur ?? 0),
288
- 0,
291
+ { initial: 0 },
289
292
  );
290
293
 
291
294
  expect(result).toBe(9); // 1 + 0 + 3 + 0 + 5 = 9
@@ -4,12 +4,12 @@
4
4
  * Utility functions for streams.
5
5
  */
6
6
 
7
- async function readAll<T>(stream: ReadableStream<T>): Promise<T[]> {
7
+ async function readAll<T>(stream: ReadableStream<T>, abortSignal?: AbortSignal): Promise<T[]> {
8
8
  const reader = stream.getReader();
9
9
  const result: T[] = [];
10
10
  while (true) {
11
11
  const { done, value } = await reader.read();
12
- if (done) {
12
+ if (done || abortSignal?.aborted === true) {
13
13
  break;
14
14
  }
15
15
  result.push(value);
@@ -17,12 +17,16 @@ async function readAll<T>(stream: ReadableStream<T>): Promise<T[]> {
17
17
  return result;
18
18
  }
19
19
 
20
- async function reduce<T, R = T>(stream: ReadableStream<T>, reducer: (acc: T | R, cur: T) => R, initial?: R): Promise<R | null> {
20
+ async function reduce<T, R = T>(stream: ReadableStream<T>, reducer: (acc: T | R, cur: T) => R, options: { initial?: R, abortSignal?: AbortSignal }): Promise<R | null> {
21
21
  const reader = stream.getReader();
22
22
  const iterator = streamDefaultReaderToAsyncGenerator(reader);
23
- let acc = (initial ?? null) as R | null | T;
23
+ let acc = (options.initial ?? null) as R | null | T;
24
24
 
25
25
  for await (const value of iterator) {
26
+ if (options.abortSignal?.aborted === true) {
27
+ break;
28
+ }
29
+
26
30
  if (acc === null) {
27
31
  acc = value;
28
32
  continue;
@@ -49,28 +53,28 @@ export async function* toAsyncGenerator<T>(value: T): AsyncGenerator<T, undefine
49
53
  yield value;
50
54
  }
51
55
 
52
- export async function* streamDefaultReaderToAsyncGenerator<T>(reader: ReadableStreamDefaultReader<T>): AsyncGenerator<Awaited<T>, undefined, undefined> {
56
+ export async function* streamDefaultReaderToAsyncGenerator<T>(reader: ReadableStreamDefaultReader<T>, abortSignal?: AbortSignal): AsyncGenerator<Awaited<T>, undefined, undefined> {
53
57
  while (true) {
54
58
  const { done, value } = await reader.read();
55
- if (done) {
59
+ if (done || abortSignal?.aborted === true) {
56
60
  break;
57
61
  }
58
62
  yield value;
59
63
  }
60
64
  }
61
65
 
62
- function transform<T, R>(stream: ReadableStream<T>, transformer: (value: T) => R): ReadableStream<R> {
66
+ function transform<T, R>(stream: ReadableStream<T>, transformer: (value: T) => R, abortSignal?: AbortSignal): ReadableStream<R> {
63
67
  const reader = stream.getReader();
64
68
 
65
69
  return new ReadableStream<R>({
66
70
  pull: async (controller) => {
67
71
  const { done, value } = await reader.read();
68
- if (!done) {
69
- controller.enqueue(transformer(value));
70
- }
71
- else {
72
+ if (done === true || abortSignal?.aborted === true) {
72
73
  controller.close();
74
+ return;
73
75
  }
76
+
77
+ controller.enqueue(transformer(value));
74
78
  },
75
79
  });
76
80
  }
@@ -61,6 +61,7 @@ export const getChatCompletionWithStreamingFunction = <Model extends ILlmSchema.
61
61
  completion.toReadableStream() as ReadableStream<Uint8Array>,
62
62
  value =>
63
63
  ChatGptCompletionMessageUtil.transformCompletionChunk(value),
64
+ props.abortSignal,
64
65
  ).tee();
65
66
 
66
67
  const [streamForAggregate, streamForReturn] = temporaryStream.tee();
@@ -69,7 +70,7 @@ export const getChatCompletionWithStreamingFunction = <Model extends ILlmSchema.
69
70
  const reader = streamForAggregate.getReader();
70
71
  while (true) {
71
72
  const chunk = await reader.read();
72
- if (chunk.done) {
73
+ if (chunk.done || props.abortSignal?.aborted === true) {
73
74
  break;
74
75
  }
75
76
  if (chunk.value.usage != null) {
@@ -87,11 +88,11 @@ export const getChatCompletionWithStreamingFunction = <Model extends ILlmSchema.
87
88
  id: v4(),
88
89
  type: "response",
89
90
  source,
90
- stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
91
+ stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader(), props.abortSignal),
91
92
  body: event.body,
92
93
  options: event.options,
93
94
  join: async () => {
94
- const chunks = await StreamUtil.readAll(streamForJoin);
95
+ const chunks = await StreamUtil.readAll(streamForJoin, props.abortSignal);
95
96
  return ChatGptCompletionMessageUtil.merge(chunks);
96
97
  },
97
98
  created_at: new Date().toISOString(),