Skip to content

Commit b2f565e

Browse files
committed
fix: followup on persisted chat model pr
1 parent ea6e871 commit b2f565e

File tree

3 files changed

+41
-69
lines changed

3 files changed

+41
-69
lines changed

core/util/history.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from "fs";
22

3-
import { Session, BaseSessionMetadata } from "../index.js";
3+
import { BaseSessionMetadata, Session } from "../index.js";
44
import { ListHistoryOptions } from "../protocol/core.js";
55

66
import { NEW_SESSION_TITLE } from "./constants.js";
@@ -107,12 +107,14 @@ export class HistoryManager {
107107
title: session.title,
108108
workspaceDirectory: session.workspaceDirectory,
109109
history: session.history,
110-
// Optional fields persisted when present
111-
...(session.mode ? { mode: session.mode } : {}),
112-
...(typeof session.chatModelTitle === "undefined"
113-
? {}
114-
: { chatModelTitle: session.chatModelTitle }),
115-
} as Session;
110+
};
111+
if (session.mode) {
112+
orderedSession.mode = session.mode;
113+
}
114+
if (session.chatModelTitle !== undefined) {
115+
orderedSession.chatModelTitle = session.chatModelTitle;
116+
}
117+
116118
fs.writeFileSync(
117119
getSessionFilePath(session.sessionId),
118120
JSON.stringify(orderedSession, undefined, 2),

gui/src/redux/slices/sessionSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export const sessionSlice = createSlice({
694694
state.title = payload.title;
695695
state.id = payload.sessionId;
696696
if (payload.mode) {
697-
state.mode = payload.mode as MessageModes;
697+
state.mode = payload.mode;
698698
}
699699
} else {
700700
state.history = [];

gui/src/redux/thunks/session.ts

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { NEW_SESSION_TITLE } from "core/util/constants";
55
import { renderChatMessage } from "core/util/messageContent";
66
import { IIdeMessenger } from "../../context/IdeMessenger";
77
import { selectSelectedChatModel } from "../slices/configSlice";
8+
import { selectSelectedProfile } from "../slices/profilesSlice";
89
import {
910
deleteSessionMetadata,
1011
newSession,
@@ -13,6 +14,7 @@ import {
1314
updateSessionMetadata,
1415
} from "../slices/sessionSlice";
1516
import { ThunkApiType } from "../store";
17+
import { updateSelectedModelByRole } from "../thunks/updateSelectedModelByRole";
1618

1719
const 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

Comments
 (0)