@@ -220,7 +220,7 @@ import {
220220 Message as DbMessage ,
221221 messageToStreamingMessage ,
222222} from './tools/chatDb/util'
223- import { FALLBACK_MODEL_OPTIONS , MODEL_RECORD } from './constants/modelSelection'
223+ import { FALLBACK_MODEL_OPTIONS , FALLBACK_MODEL_RECORD } from './constants/modelSelection'
224224import { DEFAULT_IMAGE_VERIFICATION_OPTIONS , verifyServerImage } from '../../shared/imageVerification'
225225import { sanitize } from '@aws/lsp-core/out/util/path'
226226import { ActiveUserTracker } from '../../shared/activeUserTracker'
@@ -756,7 +756,7 @@ export class AgenticChatController implements ChatHandlers {
756756
757757 // Get the first fallback model option as default
758758 const defaultModelOption = FALLBACK_MODEL_OPTIONS [ 1 ]
759- const DEFAULT_MODEL_ID = defaultModelOption ?. id || Object . keys ( MODEL_RECORD ) [ 1 ]
759+ const DEFAULT_MODEL_ID = defaultModelOption ?. id || Object . keys ( FALLBACK_MODEL_RECORD ) [ 1 ]
760760
761761 const sessionResult = this . #chatSessionManagementService. getSession ( params . tabId )
762762 const { data : session , success } = sessionResult
@@ -774,22 +774,27 @@ export class AgenticChatController implements ChatHandlers {
774774 let selectedModelId : string
775775 let modelId = this . #chatHistoryDb. getModelId ( )
776776
777+ // Helper function to get model label from FALLBACK_MODEL_RECORD
778+ const getModelLabel = ( modelKey : string ) =>
779+ FALLBACK_MODEL_RECORD [ modelKey as keyof typeof FALLBACK_MODEL_RECORD ] ?. label || modelKey
780+
781+ // Determine selected model ID based on priority
777782 if ( modelId ) {
778- // Case 1: User has previously selected a model - use that model or its mapped version
779- // Check if modelId is a key in MODEL_RECORD
780- if ( modelId in MODEL_RECORD ) {
781- // If it's a valid model key, use its mapped label
782- selectedModelId = MODEL_RECORD [ modelId as keyof typeof MODEL_RECORD ] ?. label || modelId
783- } else {
784- // Otherwise use as is (might be a direct model ID from the API)
783+ // Priority 1: Use modelId if it exists in available models from backend
784+ if ( models . some ( model => model . id === modelId ) ) {
785785 selectedModelId = modelId
786786 }
787- } else if ( defaultModelId ) {
788- // Case 2: No user selection, but API provided a default model - use server recommendation
789- selectedModelId = defaultModelId
787+ // Priority 2: Use mapped version if modelId exists in FALLBACK_MODEL_RECORD
788+ else if ( modelId in FALLBACK_MODEL_RECORD ) {
789+ selectedModelId = getModelLabel ( modelId )
790+ }
791+ // Priority 3: Fall back to default or system default
792+ else {
793+ selectedModelId = defaultModelId || getModelLabel ( DEFAULT_MODEL_ID )
794+ }
790795 } else {
791- // Case 3: Last resort - use default model's label
792- selectedModelId = MODEL_RECORD [ DEFAULT_MODEL_ID as keyof typeof MODEL_RECORD ] ?. label || DEFAULT_MODEL_ID
796+ // No user-selected model - use API default or system default
797+ selectedModelId = defaultModelId || getModelLabel ( DEFAULT_MODEL_ID )
793798 }
794799
795800 // Store the selected model in the session
0 commit comments