@@ -52,6 +52,8 @@ export class ChatService {
5252 onChunk,
5353 onComplete,
5454 onError,
55+ onReasoningChunk,
56+ onModel,
5557 // Generation parameters
5658 temperature,
5759 max_tokens,
@@ -195,13 +197,14 @@ export class ChatService {
195197 onChunk ,
196198 onComplete ,
197199 onError ,
198- options . onReasoningChunk ,
200+ onReasoningChunk ,
201+ onModel ,
199202 conversationId ,
200203 abortController . signal
201204 ) ;
202205 return ;
203206 } else {
204- return this . handleNonStreamResponse ( response , onComplete , onError ) ;
207+ return this . handleNonStreamResponse ( response , onComplete , onError , onModel ) ;
205208 }
206209 } catch ( error ) {
207210 if ( error instanceof Error && error . name === 'AbortError' ) {
@@ -261,6 +264,7 @@ export class ChatService {
261264 ) => void ,
262265 onError ?: ( error : Error ) => void ,
263266 onReasoningChunk ?: ( chunk : string ) => void ,
267+ onModel ?: ( model : string ) => void ,
264268 conversationId ?: string ,
265269 abortSignal ?: AbortSignal
266270 ) : Promise < void > {
@@ -276,6 +280,7 @@ export class ChatService {
276280 let hasReceivedData = false ;
277281 let lastTimings : ChatMessageTimings | undefined ;
278282 let streamFinished = false ;
283+ let modelEmitted = false ;
279284
280285 try {
281286 let chunk = '' ;
@@ -304,6 +309,12 @@ export class ChatService {
304309 try {
305310 const parsed : ApiChatCompletionStreamChunk = JSON . parse ( data ) ;
306311
312+ const chunkModel = this . extractModelName ( parsed ) ;
313+ if ( chunkModel && ! modelEmitted ) {
314+ modelEmitted = true ;
315+ onModel ?.( chunkModel ) ;
316+ }
317+
307318 const content = parsed . choices [ 0 ] ?. delta ?. content ;
308319 const reasoningContent = parsed . choices [ 0 ] ?. delta ?. reasoning_content ;
309320 const timings = parsed . timings ;
@@ -378,7 +389,8 @@ export class ChatService {
378389 reasoningContent ?: string ,
379390 timings ?: ChatMessageTimings
380391 ) => void ,
381- onError ?: ( error : Error ) => void
392+ onError ?: ( error : Error ) => void ,
393+ onModel ?: ( model : string ) => void
382394 ) : Promise < string > {
383395 try {
384396 const responseText = await response . text ( ) ;
@@ -389,6 +401,11 @@ export class ChatService {
389401 }
390402
391403 const data : ApiChatCompletionResponse = JSON . parse ( responseText ) ;
404+ const responseModel = this . extractModelName ( data ) ;
405+ if ( responseModel ) {
406+ onModel ?.( responseModel ) ;
407+ }
408+
392409 const content = data . choices [ 0 ] ?. message ?. content || '' ;
393410 const reasoningContent = data . choices [ 0 ] ?. message ?. reasoning_content ;
394411
@@ -631,6 +648,69 @@ export class ChatService {
631648 }
632649 }
633650
651+ private extractModelName ( data : unknown ) : string | undefined {
652+ if ( ! data || typeof data !== 'object' ) {
653+ return undefined ;
654+ }
655+
656+ const record = data as Record < string , unknown > ;
657+ const normalize = ( value : unknown ) : string | undefined => {
658+ if ( typeof value !== 'string' ) {
659+ return undefined ;
660+ }
661+
662+ const trimmed = value . trim ( ) ;
663+
664+ return trimmed . length > 0 ? trimmed : undefined ;
665+ } ;
666+
667+ const rootModel = normalize ( record [ 'model' ] ) ;
668+ if ( rootModel ) {
669+ return rootModel ;
670+ }
671+
672+ const choices = record [ 'choices' ] ;
673+ if ( ! Array . isArray ( choices ) || choices . length === 0 ) {
674+ return undefined ;
675+ }
676+
677+ const firstChoice = choices [ 0 ] as Record < string , unknown > | undefined ;
678+ if ( ! firstChoice ) {
679+ return undefined ;
680+ }
681+
682+ const choiceModel = normalize ( firstChoice [ 'model' ] ) ;
683+ if ( choiceModel ) {
684+ return choiceModel ;
685+ }
686+
687+ const delta = firstChoice [ 'delta' ] as Record < string , unknown > | undefined ;
688+ if ( delta ) {
689+ const deltaModel = normalize ( delta [ 'model' ] ) ;
690+ if ( deltaModel ) {
691+ return deltaModel ;
692+ }
693+ }
694+
695+ const message = firstChoice [ 'message' ] as Record < string , unknown > | undefined ;
696+ if ( message ) {
697+ const messageModel = normalize ( message [ 'model' ] ) ;
698+ if ( messageModel ) {
699+ return messageModel ;
700+ }
701+ }
702+
703+ const metadata = firstChoice [ 'metadata' ] as Record < string , unknown > | undefined ;
704+ if ( metadata ) {
705+ const metadataModel = normalize ( metadata [ 'model' ] ) ;
706+ if ( metadataModel ) {
707+ return metadataModel ;
708+ }
709+ }
710+
711+ return undefined ;
712+ }
713+
634714 private updateProcessingState (
635715 timings ?: ChatMessageTimings ,
636716 promptProgress ?: ChatMessagePromptProgress ,
0 commit comments