@@ -40,7 +40,10 @@ function chunk(delta: object, extra: object = {}): string {
4040 } )
4141}
4242
43- async function streamParts ( response : Response ) {
43+ async function streamParts (
44+ response : Response ,
45+ onPart ?: ( part : LanguageModelV2StreamPart ) => void ,
46+ ) {
4447 // Bun's fetch type carries a `preconnect` member the mock doesn't need.
4548 const testFetch = ( async ( ) => response ) as unknown as typeof fetch
4649 const model = new OpenAICompatibleChatLanguageModel ( 'test-model' , {
@@ -52,7 +55,7 @@ async function streamParts(response: Response) {
5255
5356 const { stream } = await model . doStream ( {
5457 prompt : [ { role : 'user' , content : [ { type : 'text' , text : 'hi' } ] } ] ,
55- includeRawChunks : false ,
58+ includeRawChunks : onPart != null ,
5659 } )
5760
5861 const parts : LanguageModelV2StreamPart [ ] = [ ]
@@ -61,6 +64,7 @@ async function streamParts(response: Response) {
6164 const { done, value } = await reader . read ( )
6265 if ( done ) break
6366 parts . push ( value )
67+ onPart ?.( value )
6468 }
6569 return parts
6670}
@@ -79,13 +83,20 @@ describe('OpenAICompatibleChatLanguageModel doStream', () => {
7983 // force another model step. Loopback is needed to exercise real decompression.
8084 it ( 'keeps a completed response when only its gzip trailer is cut' , async ( ) => {
8185 const compressors : ReturnType < typeof createGzip > [ ] = [ ]
86+ let cutResponse : ( ) => void = ( ) => {
87+ throw new Error ( 'response has not started' )
88+ }
8289 const server = http . createServer ( ( req , res ) => {
8390 res . writeHead ( 200 , {
8491 'Content-Type' : 'text/event-stream' ,
8592 'Content-Encoding' : 'gzip' ,
8693 } )
8794 const gzip = createGzip ( { flush : zlibConstants . Z_SYNC_FLUSH } )
8895 compressors . push ( gzip )
96+ cutResponse = ( ) => {
97+ gzip . unpipe ( res )
98+ res . end ( )
99+ }
89100 gzip . pipe ( res )
90101 gzip . write ( `data: ${ chunk ( { content : 'Done.' } ) } \n\n` )
91102 if ( req . url === '/complete' ) {
@@ -104,17 +115,24 @@ describe('OpenAICompatibleChatLanguageModel doStream', () => {
104115 } ) } \n\ndata: [DONE]\n\n`,
105116 )
106117 }
107- gzip . flush ( ( ) => {
108- gzip . unpipe ( res )
109- // Give the consumer the final events before the transport tail fails.
110- setTimeout ( ( ) => res . end ( ) , 20 )
111- } )
118+ gzip . flush ( )
112119 } )
113120 await new Promise < void > ( ( resolve ) => server . listen ( 0 , '127.0.0.1' , resolve ) )
114121 const { port } = server . address ( ) as { port : number }
115122 try {
116123 const parts = await streamParts (
117124 await fetch ( `http://127.0.0.1:${ port } /complete` ) ,
125+ ( part ) => {
126+ // Raw chunks are emitted in the same synchronous transform that
127+ // records usage, after the earlier finish frame was parsed. Cut only
128+ // once the consumer observes this frame, regardless of machine load.
129+ if (
130+ part . type === 'raw' &&
131+ ( part . rawValue as { usage ?: unknown } ) ?. usage != null
132+ ) {
133+ cutResponse ( )
134+ }
135+ } ,
118136 )
119137 expect ( parts . filter ( ( part ) => part . type === 'text-delta' ) ) . toEqual ( [
120138 { type : 'text-delta' , id : 'txt-0' , delta : 'Done.' } ,
@@ -126,7 +144,9 @@ describe('OpenAICompatibleChatLanguageModel doStream', () => {
126144 // No completion status or usage: a cut body must still fail so the SDK
127145 // can continue its partial answer through the existing capped recovery.
128146 await expect (
129- streamParts ( await fetch ( `http://127.0.0.1:${ port } /partial` ) ) ,
147+ streamParts ( await fetch ( `http://127.0.0.1:${ port } /partial` ) , ( part ) => {
148+ if ( part . type === 'text-delta' ) cutResponse ( )
149+ } ) ,
130150 ) . rejects . toMatchObject ( { code : 'ZlibError' } )
131151 } finally {
132152 for ( const gzip of compressors ) gzip . destroy ( )
0 commit comments