@@ -5,6 +5,7 @@ import { NEW_SESSION_TITLE } from "core/util/constants";
55import { renderChatMessage } from "core/util/messageContent" ;
66import { IIdeMessenger } from "../../context/IdeMessenger" ;
77import { selectSelectedChatModel } from "../slices/configSlice" ;
8+ import { selectSelectedProfile } from "../slices/profilesSlice" ;
89import {
910 deleteSessionMetadata ,
1011 newSession ,
@@ -13,6 +14,7 @@ import {
1314 updateSessionMetadata ,
1415} from "../slices/sessionSlice" ;
1516import { ThunkApiType } from "../store" ;
17+ import { updateSelectedModelByRole } from "../thunks/updateSelectedModelByRole" ;
1618
1719const MAX_TITLE_LENGTH = 100 ;
1820
@@ -120,26 +122,8 @@ export const loadSession = createAsyncThunk<
120122 dispatch ( newSession ( session ) ) ;
121123
122124 // Restore selected chat model from session, if present
123- const chatModelTitle = session . chatModelTitle ;
124- if ( chatModelTitle ) {
125- const state = getState ( ) ;
126- const org = state . profiles . organizations . find (
127- ( o ) => o . id === state . profiles . selectedOrganizationId ,
128- ) ;
129- const selectedProfile =
130- org ?. profiles . find ( ( p ) => p . id === state . profiles . selectedProfileId ) ??
131- null ;
132- if ( selectedProfile ) {
133- await dispatch (
134- (
135- await import ( "../thunks/updateSelectedModelByRole" )
136- ) . updateSelectedModelByRole ( {
137- role : "chat" ,
138- modelTitle : chatModelTitle ,
139- selectedProfile,
140- } ) ,
141- ) ;
142- }
125+ if ( session . chatModelTitle ) {
126+ dispatch ( selectChatModelForProfile ( session . chatModelTitle ) ) ;
143127 }
144128 } ,
145129) ;
@@ -170,26 +154,32 @@ export const loadRemoteSession = createAsyncThunk<
170154 dispatch ( newSession ( session ) ) ;
171155
172156 // Restore selected chat model from session, if present
173- const chatModelTitle = session . chatModelTitle ;
174- if ( chatModelTitle ) {
175- const state = getState ( ) ;
176- const org = state . profiles . organizations . find (
177- ( o ) => o . id === state . profiles . selectedOrganizationId ,
157+ if ( session . chatModelTitle ) {
158+ dispatch ( selectChatModelForProfile ( session . chatModelTitle ) ) ;
159+ }
160+ } ,
161+ ) ;
162+
163+ export const selectChatModelForProfile = createAsyncThunk <
164+ void ,
165+ string ,
166+ ThunkApiType
167+ > (
168+ "session/selectModelForCurrentProfile" ,
169+ async ( modelTitle , { extra, dispatch, getState } ) => {
170+ const state = getState ( ) ;
171+ const modelMatch = state . config . config ?. modelsByRole ?. chat ?. find (
172+ ( m ) => m . title === modelTitle ,
173+ ) ;
174+ const selectedProfile = selectSelectedProfile ( state ) ;
175+ if ( selectedProfile && modelMatch ) {
176+ await dispatch (
177+ updateSelectedModelByRole ( {
178+ role : "chat" ,
179+ modelTitle : modelTitle ,
180+ selectedProfile,
181+ } ) ,
178182 ) ;
179- const selectedProfile =
180- org ?. profiles . find ( ( p ) => p . id === state . profiles . selectedProfileId ) ??
181- null ;
182- if ( selectedProfile ) {
183- await dispatch (
184- (
185- await import ( "../thunks/updateSelectedModelByRole" )
186- ) . updateSelectedModelByRole ( {
187- role : "chat" ,
188- modelTitle : chatModelTitle ,
189- selectedProfile,
190- } ) ,
191- ) ;
192- }
193183 }
194184 } ,
195185) ;
@@ -220,28 +210,8 @@ export const loadLastSession = createAsyncThunk<void, void, ThunkApiType>(
220210 session = await getSession ( extra . ideMessenger , lastSessionId ) ;
221211 }
222212 dispatch ( newSession ( session ) ) ;
223-
224- // Restore selected chat model from session, if present
225- const chatModelTitle = session . chatModelTitle ;
226- if ( chatModelTitle ) {
227- const state = getState ( ) ;
228- const org = state . profiles . organizations . find (
229- ( o ) => o . id === state . profiles . selectedOrganizationId ,
230- ) ;
231- const selectedProfile =
232- org ?. profiles . find ( ( p ) => p . id === state . profiles . selectedProfileId ) ??
233- null ;
234- if ( selectedProfile ) {
235- await dispatch (
236- (
237- await import ( "../thunks/updateSelectedModelByRole" )
238- ) . updateSelectedModelByRole ( {
239- role : "chat" ,
240- modelTitle : chatModelTitle ,
241- selectedProfile,
242- } ) ,
243- ) ;
244- }
213+ if ( session . chatModelTitle ) {
214+ dispatch ( selectChatModelForProfile ( session . chatModelTitle ) ) ;
245215 }
246216 } ,
247217) ;
0 commit comments