Skip to content

Commit

Permalink
Add test for StreamTextResult.toAIStream. (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Apr 11, 2024
1 parent 0e538c3 commit 2ac59d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
28 changes: 28 additions & 0 deletions packages/core/core/generate-text/stream-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert';
import { z } from 'zod';
import { convertArrayToReadableStream } from '../test/convert-array-to-readable-stream';
import { convertAsyncIterableToArray } from '../test/convert-async-iterable-to-array';
import { convertReadableStreamToArray } from '../test/convert-readable-stream-to-array';
import { MockLanguageModelV1 } from '../test/mock-language-model-v1';
import { experimental_streamText } from './stream-text';

Expand Down Expand Up @@ -232,6 +233,33 @@ describe('result.fullStream', () => {
});
});

describe('result.toAIStream', () => {
it('should transform textStream through callbacks and data transformers', async () => {
const result = await experimental_streamText({
model: new MockLanguageModelV1({
doStream: async ({ prompt, mode }) => {
return {
stream: convertArrayToReadableStream([
{ type: 'text-delta', textDelta: 'Hello' },
{ type: 'text-delta', textDelta: ', ' },
{ type: 'text-delta', textDelta: 'world!' },
]),
rawCall: { rawPrompt: 'prompt', rawSettings: {} },
};
},
}),
prompt: 'test-input',
});

assert.deepStrictEqual(
await convertReadableStreamToArray(
result.toAIStream().pipeThrough(new TextDecoderStream()),
),
['0:"Hello"\n', '0:", "\n', '0:"world!"\n'],
);
});
});

describe('result.toTextStreamResponse', () => {
it('should create a Response with a text stream', async () => {
const result = await experimental_streamText({
Expand Down
4 changes: 1 addition & 3 deletions packages/core/core/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
AIStreamCallbacksAndOptions,
createCallbacksTransformer,
createStreamDataTransformer,
readableFromAsyncIterable,
} from '../../streams';
import { CallSettings } from '../prompt/call-settings';
import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
Expand Down Expand Up @@ -214,8 +213,7 @@ Stream callbacks that will be called when the stream emits events.
@returns an `AIStream` object.
*/
toAIStream(callbacks?: AIStreamCallbacksAndOptions) {
// TODO add support for tool calls
return readableFromAsyncIterable(this.textStream)
return this.textStream
.pipeThrough(createCallbacksTransformer(callbacks))
.pipeThrough(createStreamDataTransformer());
}
Expand Down
14 changes: 14 additions & 0 deletions packages/core/core/test/convert-readable-stream-to-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export async function convertReadableStreamToArray<T>(
stream: ReadableStream<T>,
): Promise<T[]> {
const reader = stream.getReader();
const result: T[] = [];

while (true) {
const { done, value } = await reader.read();
if (done) break;
result.push(value);
}

return result;
}

0 comments on commit 2ac59d9

Please sign in to comment.