@@ -511,4 +511,57 @@ describe('OpenAIChatCompletionsModel', () => {
511511 expect ( convertChatCompletionsStreamToResponses ) . toHaveBeenCalled ( ) ;
512512 expect ( events ) . toEqual ( [ { type : 'first' } , { type : 'second' } ] ) ;
513513 } ) ;
514+
515+ it ( 'populates usage from response_done event when initial usage is zero' , async ( ) => {
516+ // override the original implementation to add the response_done event.
517+ vi . mocked ( convertChatCompletionsStreamToResponses ) . mockImplementationOnce (
518+ async function * ( ) {
519+ yield { type : 'first' } as any ;
520+ yield { type : 'second' } as any ;
521+ yield {
522+ type : 'response_done' ,
523+ response : {
524+ usage : {
525+ inputTokens : 10 ,
526+ outputTokens : 5 ,
527+ totalTokens : 15 ,
528+ inputTokensDetails : { cached_tokens : 2 } ,
529+ outputTokensDetails : { reasoning_tokens : 3 } ,
530+ } ,
531+ } ,
532+ } as any ;
533+ } ,
534+ ) ;
535+
536+ const client = new FakeClient ( ) ;
537+ async function * fakeStream ( ) {
538+ yield { id : 'c' } as any ;
539+ }
540+ client . chat . completions . create . mockResolvedValue ( fakeStream ( ) ) ;
541+
542+ const model = new OpenAIChatCompletionsModel ( client as any , 'gpt' ) ;
543+ const req : any = {
544+ input : 'hi' ,
545+ modelSettings : { } ,
546+ tools : [ ] ,
547+ outputType : 'text' ,
548+ handoffs : [ ] ,
549+ tracing : false ,
550+ } ;
551+ const events : any [ ] = [ ] ;
552+ await withTrace ( 't' , async ( ) => {
553+ for await ( const e of model . getStreamedResponse ( req ) ) {
554+ events . push ( e ) ;
555+ }
556+ } ) ;
557+
558+ expect ( client . chat . completions . create ) . toHaveBeenCalledWith (
559+ expect . objectContaining ( { stream : true } ) ,
560+ { headers : HEADERS , signal : undefined } ,
561+ ) ;
562+ expect ( convertChatCompletionsStreamToResponses ) . toHaveBeenCalled ( ) ;
563+ const responseDone = events . find ( ( e ) => e . type === 'response_done' ) ;
564+ expect ( responseDone ) . toBeDefined ( ) ;
565+ expect ( responseDone . response . usage . totalTokens ) . toBe ( 15 ) ;
566+ } ) ;
514567} ) ;
0 commit comments