@@ -639,65 +639,35 @@ export class ChatService {
639639 }
640640
641641 private extractModelName ( data : unknown ) : string | undefined {
642- if ( ! data || typeof data !== 'object' ) {
643- return undefined ;
644- }
645-
646- const record = data as Record < string , unknown > ;
647- const normalize = ( value : unknown ) : string | undefined => {
648- if ( typeof value !== 'string' ) {
649- return undefined ;
650- }
651-
652- const trimmed = value . trim ( ) ;
642+ const asRecord = ( value : unknown ) : Record < string , unknown > | undefined => {
643+ return typeof value === 'object' && value !== null
644+ ? ( value as Record < string , unknown > )
645+ : undefined ;
646+ } ;
653647
654- return trimmed . length > 0 ? trimmed : undefined ;
648+ const getTrimmedString = ( value : unknown ) : string | undefined => {
649+ return typeof value === 'string' && value . trim ( ) ? value . trim ( ) : undefined ;
655650 } ;
656651
657- const rootModel = normalize ( record [ 'model' ] ) ;
658- if ( rootModel ) {
659- return rootModel ;
660- }
652+ const root = asRecord ( data ) ;
653+ if ( ! root ) return undefined ;
661654
662- const choices = record [ 'choices' ] ;
663- if ( ! Array . isArray ( choices ) || choices . length === 0 ) {
664- return undefined ;
665- }
655+ // 1) root (some implementations provide `model` at the top level)
656+ const rootModel = getTrimmedString ( root . model ) ;
657+ if ( rootModel ) return rootModel ;
666658
667- const firstChoice = choices [ 0 ] as Record < string , unknown > | undefined ;
668- if ( ! firstChoice ) {
669- return undefined ;
670- }
659+ // 2) streaming choice (delta) or final response (message)
660+ const firstChoice = Array . isArray ( root . choices ) ? asRecord ( root . choices [ 0 ] ) : undefined ;
661+ if ( ! firstChoice ) return undefined ;
671662
672- const choiceModel = normalize ( firstChoice [ 'model' ] ) ;
673- if ( choiceModel ) {
674- return choiceModel ;
675- }
663+ // priority: delta.model (first chunk) else message.model (final response)
664+ const deltaModel = getTrimmedString ( asRecord ( firstChoice . delta ) ?. model ) ;
665+ if ( deltaModel ) return deltaModel ;
676666
677- const delta = firstChoice [ 'delta' ] as Record < string , unknown > | undefined ;
678- if ( delta ) {
679- const deltaModel = normalize ( delta [ 'model' ] ) ;
680- if ( deltaModel ) {
681- return deltaModel ;
682- }
683- }
684-
685- const message = firstChoice [ 'message' ] as Record < string , unknown > | undefined ;
686- if ( message ) {
687- const messageModel = normalize ( message [ 'model' ] ) ;
688- if ( messageModel ) {
689- return messageModel ;
690- }
691- }
692-
693- const metadata = firstChoice [ 'metadata' ] as Record < string , unknown > | undefined ;
694- if ( metadata ) {
695- const metadataModel = normalize ( metadata [ 'model' ] ) ;
696- if ( metadataModel ) {
697- return metadataModel ;
698- }
699- }
667+ const messageModel = getTrimmedString ( asRecord ( firstChoice . message ) ?. model ) ;
668+ if ( messageModel ) return messageModel ;
700669
670+ // avoid guessing from non-standard locations (metadata, etc.)
701671 return undefined ;
702672 }
703673
0 commit comments