-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor (ai/core): simplify createAsyncIterableStream (#4138)
- Loading branch information
Showing
5 changed files
with
150 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
convertArrayToReadableStream, | ||
convertAsyncIterableToArray, | ||
convertReadableStreamToArray, | ||
} from '@ai-sdk/provider-utils/test'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { createAsyncIterableStream } from './async-iterable-stream'; | ||
|
||
describe('createAsyncIterableStream()', () => { | ||
it('should read all chunks from a non-empty stream using async iteration', async () => { | ||
const testData = ['Hello', 'World', 'Stream']; | ||
|
||
const source = convertArrayToReadableStream(testData); | ||
const asyncIterableStream = createAsyncIterableStream(source); | ||
|
||
expect(await convertAsyncIterableToArray(asyncIterableStream)).toEqual( | ||
testData, | ||
); | ||
}); | ||
|
||
it('should handle an empty stream gracefully', async () => { | ||
const source = convertArrayToReadableStream<string>([]); | ||
const asyncIterableStream = createAsyncIterableStream(source); | ||
|
||
expect(await convertAsyncIterableToArray(asyncIterableStream)).toEqual([]); | ||
}); | ||
|
||
it('should maintain ReadableStream functionality', async () => { | ||
const testData = ['Hello', 'World']; | ||
|
||
const source = convertArrayToReadableStream(testData); | ||
const asyncIterableStream = createAsyncIterableStream(source); | ||
|
||
expect(await convertReadableStreamToArray(asyncIterableStream)).toEqual( | ||
testData, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
export type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>; | ||
|
||
export function createAsyncIterableStream<S, T>( | ||
source: ReadableStream<S>, | ||
transformer: Transformer<S, T>, | ||
export function createAsyncIterableStream<T>( | ||
source: ReadableStream<T>, | ||
): AsyncIterableStream<T> { | ||
const transformedStream: any = source.pipeThrough( | ||
new TransformStream(transformer), | ||
); | ||
const stream = source.pipeThrough(new TransformStream<T, T>()); | ||
|
||
transformedStream[Symbol.asyncIterator] = () => { | ||
const reader = transformedStream.getReader(); | ||
(stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => { | ||
const reader = stream.getReader(); | ||
return { | ||
async next(): Promise<IteratorResult<string>> { | ||
async next(): Promise<IteratorResult<T>> { | ||
const { done, value } = await reader.read(); | ||
return done ? { done: true, value: undefined } : { done: false, value }; | ||
}, | ||
}; | ||
}; | ||
|
||
return transformedStream; | ||
return stream as AsyncIterableStream<T>; | ||
} |