@@ -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
@@ -103,7 +105,10 @@ export const loadSession = createAsyncThunk<
103105 ThunkApiType
104106> (
105107 "session/load" ,
106- async ( { sessionId, saveCurrentSession : save } , { extra, dispatch } ) => {
108+ async (
109+ { sessionId, saveCurrentSession : save } ,
110+ { extra, dispatch, getState } ,
111+ ) => {
107112 if ( save ) {
108113 const result = await dispatch (
109114 saveCurrentSession ( {
@@ -115,6 +120,11 @@ export const loadSession = createAsyncThunk<
115120 }
116121 const session = await getSession ( extra . ideMessenger , sessionId ) ;
117122 dispatch ( newSession ( session ) ) ;
123+
124+ // Restore selected chat model from session, if present
125+ if ( session . chatModelTitle ) {
126+ dispatch ( selectChatModelForProfile ( session . chatModelTitle ) ) ;
127+ }
118128 } ,
119129) ;
120130
@@ -127,7 +137,10 @@ export const loadRemoteSession = createAsyncThunk<
127137 ThunkApiType
128138> (
129139 "session/loadRemote" ,
130- async ( { remoteId, saveCurrentSession : save } , { extra, dispatch } ) => {
140+ async (
141+ { remoteId, saveCurrentSession : save } ,
142+ { extra, dispatch, getState } ,
143+ ) => {
131144 if ( save ) {
132145 const result = await dispatch (
133146 saveCurrentSession ( {
@@ -139,6 +152,35 @@ export const loadRemoteSession = createAsyncThunk<
139152 }
140153 const session = await getRemoteSession ( extra . ideMessenger , remoteId ) ;
141154 dispatch ( newSession ( session ) ) ;
155+
156+ // Restore selected chat model from session, if present
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+ } ) ,
182+ ) ;
183+ }
142184 } ,
143185) ;
144186
@@ -168,6 +210,9 @@ export const loadLastSession = createAsyncThunk<void, void, ThunkApiType>(
168210 session = await getSession ( extra . ideMessenger , lastSessionId ) ;
169211 }
170212 dispatch ( newSession ( session ) ) ;
213+ if ( session . chatModelTitle ) {
214+ dispatch ( selectChatModelForProfile ( session . chatModelTitle ) ) ;
215+ }
171216 } ,
172217) ;
173218
@@ -246,11 +291,15 @@ export const saveCurrentSession = createAsyncThunk<
246291 title = NEW_SESSION_TITLE ;
247292 }
248293
294+ const selectedChatModel = selectSelectedChatModel ( state ) ;
295+
249296 const session : Session = {
250297 sessionId : state . session . id ,
251298 title,
252299 workspaceDirectory : window . workspacePaths ?. [ 0 ] || "" ,
253300 history : state . session . history ,
301+ mode : state . session . mode ,
302+ chatModelTitle : selectedChatModel ?. title ?? null ,
254303 } ;
255304
256305 const result = await dispatch ( updateSession ( session ) ) ;
0 commit comments