@@ -405,7 +405,10 @@ export function handleStreamingMode(
405405export async function handleJSONToStreamResponse (
406406 response : Response ,
407407 provider : string ,
408- responseTransformerFunction : Function
408+ responseTransformerFunction : Function ,
409+ strictOpenAiCompliance : boolean ,
410+ fn : endpointStrings ,
411+ hooksResult : HookSpan [ 'hooksResult' ]
409412) : Promise < Response > {
410413 const { readable, writable } = new TransformStream ( ) ;
411414 const writer = writable . getWriter ( ) ;
@@ -419,6 +422,12 @@ export async function handleJSONToStreamResponse(
419422 ) {
420423 const generator = responseTransformerFunction ( responseJSON , provider ) ;
421424 ( async ( ) => {
425+ if ( ! strictOpenAiCompliance ) {
426+ const hookResultChunk = constructHookResultChunk ( hooksResult , fn ) ;
427+ if ( hookResultChunk ) {
428+ await writer . write ( encoder . encode ( hookResultChunk ) ) ;
429+ }
430+ }
422431 while ( true ) {
423432 const chunk = generator . next ( ) ;
424433 if ( chunk . done ) {
@@ -434,6 +443,12 @@ export async function handleJSONToStreamResponse(
434443 provider
435444 ) ;
436445 ( async ( ) => {
446+ if ( ! strictOpenAiCompliance ) {
447+ const hookResultChunk = constructHookResultChunk ( hooksResult , fn ) ;
448+ if ( hookResultChunk ) {
449+ await writer . write ( encoder . encode ( hookResultChunk ) ) ;
450+ }
451+ }
437452 for ( const chunk of streamChunkArray ) {
438453 await writer . write ( encoder . encode ( chunk ) ) ;
439454 }
0 commit comments