@@ -79,9 +79,7 @@ class ModelsStore {
7979
8080 const models : ModelOption [ ] = response . data . map ( ( item , index ) => {
8181 const details = response . models ?. [ index ] ;
82- const rawCapabilities = Array . isArray ( details ?. capabilities )
83- ? [ ...( details ?. capabilities ?? [ ] ) ]
84- : [ ] ;
82+ const rawCapabilities = Array . isArray ( details ?. capabilities ) ? details ?. capabilities : [ ] ;
8583 const displayNameSource =
8684 details ?. name && details . name . trim ( ) . length > 0 ? details . name : item . id ;
8785 const displayName = this . toDisplayName ( displayNameSource ) ;
@@ -99,36 +97,17 @@ class ModelsStore {
9997
10098 this . _models = models ;
10199
102- const persisted = this . readPersistedSelection ( ) ;
103- let nextSelectionId = this . _selectedModelId ?? persisted ?. id ?? null ;
104- let nextSelectionName = this . _selectedModelName ?? persisted ?. model ?? null ;
105- if ( nextSelectionId ) {
106- const match = models . find ( ( model ) => model . id === nextSelectionId ) ;
107- if ( match ) {
108- nextSelectionId = match . id ;
109- nextSelectionName = match . model ;
110- } else if ( models [ 0 ] ) {
111- nextSelectionId = models [ 0 ] . id ;
112- nextSelectionName = models [ 0 ] . model ;
113- } else {
114- nextSelectionId = null ;
115- nextSelectionName = null ;
116- }
117- } else if ( models [ 0 ] ) {
118- nextSelectionId = models [ 0 ] . id ;
119- nextSelectionName = models [ 0 ] . model ;
120- }
100+ const selection = this . determineInitialSelection ( models ) ;
121101
122- this . _selectedModelId = nextSelectionId ;
123- this . _selectedModelName = nextSelectionName ;
102+ this . _selectedModelId = selection . id ;
103+ this . _selectedModelName = selection . model ;
124104 this . persistSelection (
125- nextSelectionId && nextSelectionName
126- ? { id : nextSelectionId , model : nextSelectionName }
127- : null
105+ selection . id && selection . model ? { id : selection . id , model : selection . model } : null
128106 ) ;
129107 } catch ( error ) {
130108 this . _models = [ ] ;
131109 this . _error = error instanceof Error ? error . message : 'Failed to load models' ;
110+
132111 throw error ;
133112 } finally {
134113 this . _loading = false ;
@@ -167,6 +146,38 @@ class ModelsStore {
167146 return candidate && candidate . trim ( ) . length > 0 ? candidate : id ;
168147 }
169148
149+ /**
150+ * Determines which model should be selected after fetching the models list.
151+ * Priority: current selection > persisted selection > first available model > none
152+ */
153+ private determineInitialSelection ( models : ModelOption [ ] ) : {
154+ id : string | null ;
155+ model : string | null ;
156+ } {
157+ const persisted = this . readPersistedSelection ( ) ;
158+ let nextSelectionId = this . _selectedModelId ?? persisted ?. id ?? null ;
159+ let nextSelectionName = this . _selectedModelName ?? persisted ?. model ?? null ;
160+
161+ if ( nextSelectionId ) {
162+ const match = models . find ( ( m ) => m . id === nextSelectionId ) ;
163+ if ( match ) {
164+ nextSelectionId = match . id ;
165+ nextSelectionName = match . model ;
166+ } else if ( models [ 0 ] ) {
167+ nextSelectionId = models [ 0 ] . id ;
168+ nextSelectionName = models [ 0 ] . model ;
169+ } else {
170+ nextSelectionId = null ;
171+ nextSelectionName = null ;
172+ }
173+ } else if ( models [ 0 ] ) {
174+ nextSelectionId = models [ 0 ] . id ;
175+ nextSelectionName = models [ 0 ] . model ;
176+ }
177+
178+ return { id : nextSelectionId , model : nextSelectionName } ;
179+ }
180+
170181 private readPersistedSelection ( ) : PersistedModelSelection | null {
171182 if ( ! browser ) {
172183 return null ;
@@ -183,6 +194,7 @@ class ModelsStore {
183194 const id = parsed . id ;
184195 const model =
185196 typeof parsed . model === 'string' && parsed . model . length > 0 ? parsed . model : id ;
197+
186198 return { id, model } ;
187199 }
188200 } catch ( error ) {
0 commit comments