Skip to content

Commit ea6b0e1

Browse files
authoredApr 12, 2024··
Expose StreamPart & utilities. (#1334)
1 parent 3fd1918 commit ea6b0e1

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed
 

Diff for: ‎.changeset/weak-bags-attack.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Expose formatStreamPart, parseStreamPart, and readDataStream helpers.

Diff for: ‎packages/core/shared/read-data-stream.ts

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ function concatChunks(chunks: Uint8Array[], totalLength: number) {
1616
return concatenatedChunks;
1717
}
1818

19+
/**
20+
Converts a ReadableStreamDefaultReader into an async generator that yields
21+
StreamPart objects.
22+
23+
@param reader
24+
Reader for the stream to read from.
25+
@param isAborted
26+
Optional function that returns true if the request has been aborted.
27+
If the function returns true, the generator will stop reading the stream.
28+
If the function is not provided, the generator will not stop reading the stream.
29+
*/
1930
export async function* readDataStream(
2031
reader: ReadableStreamDefaultReader<Uint8Array>,
2132
{

Diff for: ‎packages/core/shared/stream-parts.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ export const StreamStringPrefixes = {
328328
export const validCodes = streamParts.map(part => part.code);
329329

330330
/**
331-
* Parses a stream part from a string.
332-
*
333-
* @param line The string to parse.
334-
* @returns The parsed stream part.
335-
* @throws An error if the string cannot be parsed.
331+
Parses a stream part from a string.
332+
333+
@param line The string to parse.
334+
@returns The parsed stream part.
335+
@throws An error if the string cannot be parsed.
336336
*/
337337
export const parseStreamPart = (line: string): StreamPartType => {
338338
const firstSeparatorIndex = line.indexOf(':');
@@ -356,10 +356,10 @@ export const parseStreamPart = (line: string): StreamPartType => {
356356
};
357357

358358
/**
359-
* Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
360-
* and appends a new line.
361-
*
362-
* It ensures type-safety for the part type and value.
359+
Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
360+
and appends a new line.
361+
362+
It ensures type-safety for the part type and value.
363363
*/
364364
export function formatStreamPart<T extends keyof StreamPartValueType>(
365365
type: T,

Diff for: ‎packages/core/shared/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export * from './generate-id';
99
// TODO remove (breaking change)
1010
export { generateId as nanoid } from './generate-id';
1111

12+
// Export stream data utilities for custom stream implementations,
13+
// both on the client and server side.
14+
export type { StreamPart } from './stream-parts';
15+
export { formatStreamPart, parseStreamPart } from './stream-parts';
16+
export { readDataStream } from './read-data-stream';
17+
1218
// simple decoder signatures:
1319
function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
1420
function createChunkDecoder(

0 commit comments

Comments
 (0)
Please sign in to comment.