ai-stream-utils 0.0.2 → 0.0.3

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.
package/README.md CHANGED
@@ -271,16 +271,16 @@ function mapUIMessageStream<UI_MESSAGE extends UIMessage>(
271
271
  ): AsyncIterableStream<InferUIMessageChunk<UI_MESSAGE>>
272
272
 
273
273
  type MapUIMessageStreamFn<UI_MESSAGE extends UIMessage> = (
274
- input: ChunkMapInput<UI_MESSAGE>,
275
- context: ChunkMapContext<UI_MESSAGE>,
274
+ input: MapInput<UI_MESSAGE>,
275
+ context: MapContext<UI_MESSAGE>,
276
276
  ) => InferUIMessageChunk<UI_MESSAGE> | null;
277
277
 
278
- type ChunkMapInput<UI_MESSAGE extends UIMessage> = {
278
+ type MapInput<UI_MESSAGE extends UIMessage> = {
279
279
  chunk: InferUIMessageChunk<UI_MESSAGE>;
280
- part: PartialPart<UI_MESSAGE>;
280
+ part: InferPartialUIMessagePart<UI_MESSAGE>;
281
281
  };
282
282
 
283
- type ChunkMapContext<UI_MESSAGE extends UIMessage> = {
283
+ type MapContext<UI_MESSAGE extends UIMessage> = {
284
284
  index: number;
285
285
  chunks: InferUIMessageChunk<UI_MESSAGE>[];
286
286
  };
@@ -303,17 +303,17 @@ function flatMapUIMessageStream<UI_MESSAGE extends UIMessage, PART extends Infer
303
303
  ): AsyncIterableStream<InferUIMessageChunk<UI_MESSAGE>>
304
304
 
305
305
  type FlatMapUIMessageStreamFn<UI_MESSAGE extends UIMessage, PART = InferUIMessagePart<UI_MESSAGE>> = (
306
- input: PartFlatMapInput<UI_MESSAGE, PART>,
307
- context: PartFlatMapContext<UI_MESSAGE>,
306
+ input: FlatMapInput<UI_MESSAGE, PART>,
307
+ context: FlatMapContext<UI_MESSAGE>,
308
308
  ) => PART | null;
309
309
 
310
- type PartFlatMapInput<UI_MESSAGE extends UIMessage, PART = InferUIMessagePart<UI_MESSAGE>> = {
310
+ type FlatMapInput<UI_MESSAGE extends UIMessage, PART = InferUIMessagePart<UI_MESSAGE>> = {
311
311
  part: PART;
312
312
  };
313
313
 
314
- type PartFlatMapContext<UI_MESSAGE extends UIMessage> = {
314
+ type FlatMapContext<UI_MESSAGE extends UIMessage> = {
315
315
  index: number;
316
- parts: PartFlatMapInput<UI_MESSAGE>[];
316
+ parts: InferUIMessagePart<UI_MESSAGE>[];
317
317
  };
318
318
  ```
319
319
 
@@ -325,7 +325,7 @@ function partTypeIs<UI_MESSAGE extends UIMessage, T extends InferUIMessagePartTy
325
325
  ): FlatMapUIMessageStreamPredicate<UI_MESSAGE, Extract<InferUIMessagePart<UI_MESSAGE>, { type: T }>>
326
326
 
327
327
  type FlatMapUIMessageStreamPredicate<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE>> =
328
- (part: PartialPart<UI_MESSAGE>) => boolean;
328
+ (part: InferPartialUIMessagePart<UI_MESSAGE>) => boolean;
329
329
  ```
330
330
 
331
331
  ### `filterUIMessageStream`
@@ -337,8 +337,8 @@ function filterUIMessageStream<UI_MESSAGE extends UIMessage>(
337
337
  ): AsyncIterableStream<InferUIMessageChunk<UI_MESSAGE>>
338
338
 
339
339
  type FilterUIMessageStreamPredicate<UI_MESSAGE extends UIMessage> = (
340
- input: ChunkMapInput<UI_MESSAGE>,
341
- context: ChunkMapContext<UI_MESSAGE>,
340
+ input: MapInput<UI_MESSAGE>,
341
+ context: MapContext<UI_MESSAGE>,
342
342
  ) => boolean;
343
343
  ```
344
344
 
package/dist/index.d.mts CHANGED
@@ -10,7 +10,7 @@ type InferUIMessagePartType<UI_MESSAGE extends UIMessage> = InferUIMessagePart<U
10
10
  * A partial part reconstructed from the current chunk.
11
11
  * Contains the part type and any available data from the chunk.
12
12
  */
13
- type PartialPart<UI_MESSAGE extends UIMessage> = {
13
+ type InferPartialUIMessagePart<UI_MESSAGE extends UIMessage> = {
14
14
  /** The part type (e.g., 'text', 'reasoning', 'tool-weather', 'file') */
15
15
  type: InferUIMessagePartType<UI_MESSAGE>;
16
16
  } & Partial<InferUIMessagePart<UI_MESSAGE>>;
@@ -19,19 +19,19 @@ type PartialPart<UI_MESSAGE extends UIMessage> = {
19
19
  /**
20
20
  * Input object provided to the chunk map function.
21
21
  */
22
- type ChunkMapInput<UI_MESSAGE extends UIMessage> = {
22
+ type MapInput<UI_MESSAGE extends UIMessage> = {
23
23
  /** The current chunk */
24
24
  chunk: InferUIMessageChunk<UI_MESSAGE>;
25
25
  /**
26
26
  * A partial representation of the part this chunk belongs to.
27
27
  * Use `part.type` to determine the part type.
28
28
  */
29
- part: PartialPart<UI_MESSAGE>;
29
+ part: InferPartialUIMessagePart<UI_MESSAGE>;
30
30
  };
31
31
  /**
32
32
  * Context provided to the chunk map function (similar to Array.map callback).
33
33
  */
34
- type ChunkMapContext<UI_MESSAGE extends UIMessage> = {
34
+ type MapContext<UI_MESSAGE extends UIMessage> = {
35
35
  /** The index of the current chunk in the stream (0-based) */
36
36
  index: number;
37
37
  /** All chunks seen so far (including the current one) */
@@ -42,7 +42,7 @@ type ChunkMapContext<UI_MESSAGE extends UIMessage> = {
42
42
  * Similar to Array.map, receives the input object and context.
43
43
  * Return the chunk (possibly transformed) to include it, or null to filter it out.
44
44
  */
45
- type MapUIMessageStreamFn<UI_MESSAGE extends UIMessage> = (input: ChunkMapInput<UI_MESSAGE>, context: ChunkMapContext<UI_MESSAGE>) => InferUIMessageChunk<UI_MESSAGE> | null;
45
+ type MapUIMessageStreamFn<UI_MESSAGE extends UIMessage> = (input: MapInput<UI_MESSAGE>, context: MapContext<UI_MESSAGE>) => InferUIMessageChunk<UI_MESSAGE> | null;
46
46
  /**
47
47
  * Maps/filters a UIMessageStream at the chunk level.
48
48
  *
@@ -91,7 +91,7 @@ declare function mapUIMessageStream<UI_MESSAGE extends UIMessage>(stream: Readab
91
91
  * Filter function that receives the same input/context as mapUIMessageStream.
92
92
  * Return true to include the chunk, false to filter it out.
93
93
  */
94
- type FilterUIMessageStreamPredicate<UI_MESSAGE extends UIMessage> = (input: ChunkMapInput<UI_MESSAGE>, context: ChunkMapContext<UI_MESSAGE>) => boolean;
94
+ type FilterUIMessageStreamPredicate<UI_MESSAGE extends UIMessage> = (input: MapInput<UI_MESSAGE>, context: MapContext<UI_MESSAGE>) => boolean;
95
95
  /**
96
96
  * Creates a filter predicate that includes only the specified part types.
97
97
  *
@@ -157,18 +157,18 @@ declare function filterUIMessageStream<UI_MESSAGE extends UIMessage>(stream: Rea
157
157
  /**
158
158
  * Input object provided to the part flatMap function.
159
159
  */
160
- type PartFlatMapInput<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = {
160
+ type FlatMapInput<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = {
161
161
  /** The reconstructed part */
162
162
  part: PART;
163
163
  };
164
164
  /**
165
165
  * Context provided to the part flatMap function.
166
166
  */
167
- type PartFlatMapContext<UI_MESSAGE extends UIMessage> = {
167
+ type FlatMapContext<UI_MESSAGE extends UIMessage> = {
168
168
  /** The index of the current part in the stream (0-based) */
169
169
  index: number;
170
170
  /** All parts seen so far (including the current one) */
171
- parts: PartFlatMapInput<UI_MESSAGE>[];
171
+ parts: InferUIMessagePart<UI_MESSAGE>[];
172
172
  };
173
173
  /**
174
174
  * FlatMap function for part-level transformation.
@@ -176,13 +176,13 @@ type PartFlatMapContext<UI_MESSAGE extends UIMessage> = {
176
176
  * - The part (possibly transformed) to include it
177
177
  * - null to filter out the part
178
178
  */
179
- type FlatMapUIMessageStreamFn<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = (input: PartFlatMapInput<UI_MESSAGE, PART>, context: PartFlatMapContext<UI_MESSAGE>) => PART | null;
179
+ type FlatMapUIMessageStreamFn<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = (input: FlatMapInput<UI_MESSAGE, PART>, context: FlatMapContext<UI_MESSAGE>) => PART | null;
180
180
  /**
181
181
  * Predicate function to determine which parts should be buffered.
182
182
  * Receives a PartialPart built from the current chunk.
183
183
  * Returns true to buffer the part for transformation, false to pass through immediately.
184
184
  */
185
- type FlatMapUIMessageStreamPredicate<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = (part: PartialPart<UI_MESSAGE>) => boolean;
185
+ type FlatMapUIMessageStreamPredicate<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE> = InferUIMessagePart<UI_MESSAGE>> = (part: InferPartialUIMessagePart<UI_MESSAGE>) => boolean;
186
186
  /**
187
187
  * Creates a predicate that matches parts by their type.
188
188
  * Supports both single type and array of types with full type narrowing.
@@ -245,4 +245,4 @@ declare function partTypeIs<UI_MESSAGE extends UIMessage, PART_TYPE extends Infe
245
245
  declare function flatMapUIMessageStream<UI_MESSAGE extends UIMessage, PART extends InferUIMessagePart<UI_MESSAGE>>(stream: ReadableStream<InferUIMessageChunk<UI_MESSAGE>>, predicate: FlatMapUIMessageStreamPredicate<UI_MESSAGE, PART>, flatMapFn: FlatMapUIMessageStreamFn<UI_MESSAGE, PART>): AsyncIterableStream<InferUIMessageChunk<UI_MESSAGE>>;
246
246
  declare function flatMapUIMessageStream<UI_MESSAGE extends UIMessage>(stream: ReadableStream<InferUIMessageChunk<UI_MESSAGE>>, flatMapFn: FlatMapUIMessageStreamFn<UI_MESSAGE>): AsyncIterableStream<InferUIMessageChunk<UI_MESSAGE>>;
247
247
  //#endregion
248
- export { type ChunkMapContext, type ChunkMapInput, type FilterUIMessageStreamPredicate, type FlatMapUIMessageStreamFn, type FlatMapUIMessageStreamPredicate, type InferUIMessageData, type InferUIMessageMetadata, type InferUIMessagePart, type InferUIMessagePartType, type InferUIMessageTools, type MapUIMessageStreamFn, type PartFlatMapContext, type PartFlatMapInput, type PartialPart, excludeParts, filterUIMessageStream, flatMapUIMessageStream, includeParts, mapUIMessageStream, partTypeIs };
248
+ export { type FilterUIMessageStreamPredicate, type FlatMapContext, type FlatMapInput, type FlatMapUIMessageStreamFn, type FlatMapUIMessageStreamPredicate, type InferPartialUIMessagePart, type InferUIMessageData, type InferUIMessageMetadata, type InferUIMessagePart, type InferUIMessagePartType, type InferUIMessageTools, type MapContext, type MapInput, type MapUIMessageStreamFn, excludeParts, filterUIMessageStream, flatMapUIMessageStream, includeParts, mapUIMessageStream, partTypeIs };
package/dist/index.mjs CHANGED
@@ -552,7 +552,7 @@ function flatMapUIMessageStream(...args) {
552
552
  isBufferingCurrentPart = false;
553
553
  const part = reconstructPartFromChunks(chunks);
554
554
  const input = { part };
555
- allParts.push(input);
555
+ allParts.push(part);
556
556
  const result = flatMapFn(input, {
557
557
  index: currentIndex++,
558
558
  parts: allParts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-stream-utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "AI SDK: Filter and transform UI messages while streaming to the client",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.mjs",