@@ -269,7 +269,7 @@ describe('result.toAIStream', () => {
269269} ) ;
270270
271271describe ( 'result.pipeAIStreamToResponse' , async ( ) => {
272- it ( 'should write text deltas to a Node.js response-like object' , async ( ) => {
272+ it ( 'should write data stream parts to a Node.js response-like object' , async ( ) => {
273273 const mockResponse = createMockServerResponse ( ) ;
274274
275275 const result = await experimental_streamText ( {
@@ -315,6 +315,92 @@ describe('result.pipeAIStreamToResponse', async () => {
315315 } ) ;
316316} ) ;
317317
318+ describe ( 'result.pipeTextStreamToResponse' , async ( ) => {
319+ it ( 'should write text deltas to a Node.js response-like object' , async ( ) => {
320+ const mockResponse = createMockServerResponse ( ) ;
321+
322+ const result = await experimental_streamText ( {
323+ model : new MockLanguageModelV1 ( {
324+ doStream : async ( ) => {
325+ return {
326+ stream : convertArrayToReadableStream ( [
327+ { type : 'text-delta' , textDelta : 'Hello' } ,
328+ { type : 'text-delta' , textDelta : ', ' } ,
329+ { type : 'text-delta' , textDelta : 'world!' } ,
330+ ] ) ,
331+ rawCall : { rawPrompt : 'prompt' , rawSettings : { } } ,
332+ } ;
333+ } ,
334+ } ) ,
335+ prompt : 'test-input' ,
336+ } ) ;
337+
338+ result . pipeTextStreamToResponse ( mockResponse ) ;
339+
340+ // Wait for the stream to finish writing to the mock response
341+ await new Promise ( resolve => {
342+ const checkIfEnded = ( ) => {
343+ if ( mockResponse . ended ) {
344+ resolve ( undefined ) ;
345+ } else {
346+ setImmediate ( checkIfEnded ) ;
347+ }
348+ } ;
349+ checkIfEnded ( ) ;
350+ } ) ;
351+
352+ const decoder = new TextDecoder ( ) ;
353+
354+ assert . strictEqual ( mockResponse . statusCode , 200 ) ;
355+ assert . deepStrictEqual ( mockResponse . headers , {
356+ 'Content-Type' : 'text/plain; charset=utf-8' ,
357+ } ) ;
358+ assert . deepStrictEqual (
359+ mockResponse . writtenChunks . map ( chunk => decoder . decode ( chunk ) ) ,
360+ [ 'Hello' , ', ' , 'world!' ] ,
361+ ) ;
362+ } ) ;
363+ } ) ;
364+
365+ describe ( 'result.toAIStreamResponse' , ( ) => {
366+ it ( 'should create a Response with a stream data stream' , async ( ) => {
367+ const result = await experimental_streamText ( {
368+ model : new MockLanguageModelV1 ( {
369+ doStream : async ( { prompt, mode } ) => {
370+ return {
371+ stream : convertArrayToReadableStream ( [
372+ { type : 'text-delta' , textDelta : 'Hello' } ,
373+ { type : 'text-delta' , textDelta : ', ' } ,
374+ { type : 'text-delta' , textDelta : 'world!' } ,
375+ ] ) ,
376+ rawCall : { rawPrompt : 'prompt' , rawSettings : { } } ,
377+ } ;
378+ } ,
379+ } ) ,
380+ prompt : 'test-input' ,
381+ } ) ;
382+
383+ const response = result . toAIStreamResponse ( ) ;
384+
385+ assert . strictEqual ( response . status , 200 ) ;
386+ assert . strictEqual (
387+ response . headers . get ( 'Content-Type' ) ,
388+ 'text/plain; charset=utf-8' ,
389+ ) ;
390+
391+ // Read the chunks into an array
392+ const reader = response . body ! . getReader ( ) ;
393+ const chunks = [ ] ;
394+ while ( true ) {
395+ const { value, done } = await reader . read ( ) ;
396+ if ( done ) break ;
397+ chunks . push ( new TextDecoder ( ) . decode ( value ) ) ;
398+ }
399+
400+ assert . deepStrictEqual ( chunks , [ '0:"Hello"\n' , '0:", "\n' , '0:"world!"\n' ] ) ;
401+ } ) ;
402+ } ) ;
403+
318404describe ( 'result.toTextStreamResponse' , ( ) => {
319405 it ( 'should create a Response with a text stream' , async ( ) => {
320406 const result = await experimental_streamText ( {
0 commit comments