@@ -652,65 +652,35 @@ export class ChatService {
652652 }
653653
654654 private extractModelName ( data : unknown ) : string | undefined {
655- if ( ! data || typeof data !== 'object' ) {
656- return undefined ;
657- }
658-
659- const record = data as Record < string , unknown > ;
660- const normalize = ( value : unknown ) : string | undefined => {
661- if ( typeof value !== 'string' ) {
662- return undefined ;
663- }
664-
665- const trimmed = value . trim ( ) ;
655+ const asRecord = ( value : unknown ) : Record < string , unknown > | undefined => {
656+ return typeof value === 'object' && value !== null
657+ ? ( value as Record < string , unknown > )
658+ : undefined ;
659+ } ;
666660
667- return trimmed . length > 0 ? trimmed : undefined ;
661+ const getTrimmedString = ( value : unknown ) : string | undefined => {
662+ return typeof value === 'string' && value . trim ( ) ? value . trim ( ) : undefined ;
668663 } ;
669664
670- const rootModel = normalize ( record [ 'model' ] ) ;
671- if ( rootModel ) {
672- return rootModel ;
673- }
665+ const root = asRecord ( data ) ;
666+ if ( ! root ) return undefined ;
674667
675- const choices = record [ 'choices' ] ;
676- if ( ! Array . isArray ( choices ) || choices . length === 0 ) {
677- return undefined ;
678- }
668+ // 1) root (some implementations provide `model` at the top level)
669+ const rootModel = getTrimmedString ( root . model ) ;
670+ if ( rootModel ) return rootModel ;
679671
680- const firstChoice = choices [ 0 ] as Record < string , unknown > | undefined ;
681- if ( ! firstChoice ) {
682- return undefined ;
683- }
672+ // 2) streaming choice (delta) or final response (message)
673+ const firstChoice = Array . isArray ( root . choices ) ? asRecord ( root . choices [ 0 ] ) : undefined ;
674+ if ( ! firstChoice ) return undefined ;
684675
685- const choiceModel = normalize ( firstChoice [ 'model' ] ) ;
686- if ( choiceModel ) {
687- return choiceModel ;
688- }
676+ // priority: delta.model (first chunk) else message.model (final response)
677+ const deltaModel = getTrimmedString ( asRecord ( firstChoice . delta ) ?. model ) ;
678+ if ( deltaModel ) return deltaModel ;
689679
690- const delta = firstChoice [ 'delta' ] as Record < string , unknown > | undefined ;
691- if ( delta ) {
692- const deltaModel = normalize ( delta [ 'model' ] ) ;
693- if ( deltaModel ) {
694- return deltaModel ;
695- }
696- }
697-
698- const message = firstChoice [ 'message' ] as Record < string , unknown > | undefined ;
699- if ( message ) {
700- const messageModel = normalize ( message [ 'model' ] ) ;
701- if ( messageModel ) {
702- return messageModel ;
703- }
704- }
705-
706- const metadata = firstChoice [ 'metadata' ] as Record < string , unknown > | undefined ;
707- if ( metadata ) {
708- const metadataModel = normalize ( metadata [ 'model' ] ) ;
709- if ( metadataModel ) {
710- return metadataModel ;
711- }
712- }
680+ const messageModel = getTrimmedString ( asRecord ( firstChoice . message ) ?. model ) ;
681+ if ( messageModel ) return messageModel ;
713682
683+ // avoid guessing from non-standard locations (metadata, etc.)
714684 return undefined ;
715685 }
716686
0 commit comments