persist session-specific mode and chat model selection across tab switches and session loads

This commit is contained in:
ferenci84 2025-11-09 12:23:18 +01:00
parent 6bf5ee15d6
commit a3b5ee840f
6 changed files with 104 additions and 75 deletions

4
core/index.d.ts vendored
View file

@ -272,6 +272,10 @@ export interface Session {
title: string;
workspaceDirectory: string;
history: ChatHistoryItem[];
/** Optional: per-session UI mode (chat/agent/plan/background) */
mode?: MessageModes;
/** Optional: title of the selected chat model for this session */
chatModelTitle?: string | null;
}
export interface BaseSessionMetadata {

View file

@ -107,7 +107,12 @@ export class HistoryManager {
title: session.title,
workspaceDirectory: session.workspaceDirectory,
history: session.history,
};
// Optional fields persisted when present
...(session.mode ? { mode: session.mode } : {}),
...(typeof session.chatModelTitle === "undefined"
? {}
: { chatModelTitle: session.chatModelTitle }),
} as Session;
fs.writeFileSync(
getSessionFilePath(session.sessionId),
JSON.stringify(orderedSession, undefined, 2),

View file

@ -11,16 +11,12 @@ import {
removeTab,
setActiveTab,
setTabs,
setTabMode,
setTabModel,
} from "../../redux/slices/tabsSlice";
import { AppDispatch, RootState } from "../../redux/store";
import { loadSession, saveCurrentSession } from "../../redux/thunks/session";
import { varWithFallback } from "../../styles/theme";
import { useAuth } from "../../context/Auth";
import { selectSelectedChatModel } from "../../redux/slices/configSlice";
import { updateSelectedModelByRole } from "../../redux/thunks/updateSelectedModelByRole";
import { setMode } from "../../redux/slices/sessionSlice";
// Haven't set up theme colors for tabs yet
// Will keep it simple and choose from existing ones. Comments show vars we could use
@ -144,11 +140,6 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
(state: RootState) => state.session.history.length > 0,
);
const tabs = useSelector((state: RootState) => state.tabs.tabs);
const selectedModel = useAppSelector(selectSelectedChatModel);
const { selectedProfile } = useAuth();
const mode = useAppSelector((state: RootState) => state.session.mode);
const activeTab = tabs.find((tab) => tab.isActive);
const activeTabId = activeTab?.id;
// Simple UUID generator for our needs
const generateId = useCallback(() => {
@ -167,22 +158,6 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
);
}, [currentSessionId, currentSessionTitle]);
// Persist selected model into the active tab in Redux
useEffect(() => {
if (activeTabId && selectedModel?.title) {
dispatch(
setTabModel({ id: activeTabId, modelTitle: selectedModel.title }),
);
}
}, [activeTabId, selectedModel?.title, mode, dispatch]);
// Persist the currently selected mode into the active tab
useEffect(() => {
if (activeTabId && mode) {
dispatch(setTabMode({ id: activeTabId, mode }));
}
}, [activeTabId, mode, dispatch]);
const handleNewTab = async () => {
// Save current session before creating new one
if (hasHistory) {
@ -199,8 +174,6 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
title: `Chat ${tabs.length + 1}`,
isActive: true,
sessionId: undefined,
modelTitle: selectedModel?.title,
mode,
}),
);
};
@ -226,22 +199,6 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
}
dispatch(setActiveTab(id));
// restore mode for this tab (if set)
if (targetTab.mode) {
dispatch(setMode(targetTab.mode));
}
// restore model for this tab
if (targetTab.modelTitle && selectedProfile) {
void dispatch(
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: targetTab.modelTitle,
}),
);
}
};
const handleTabClose = async (id: string) => {

View file

@ -693,6 +693,9 @@ export const sessionSlice = createSlice({
state.history = payload.history as any;
state.title = payload.title;
state.id = payload.sessionId;
if (payload.mode) {
state.mode = payload.mode as MessageModes;
}
} else {
state.history = [];
state.title = NEW_SESSION_TITLE;

View file

@ -1,13 +1,10 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { MessageModes } from "core";
export interface Tab {
id: string;
title: string;
isActive: boolean;
sessionId?: string;
modelTitle?: string; // store per-tab model
mode?: MessageModes; // store per-tab mode ('chat' | 'plan' | 'agent')
}
interface TabsState {
@ -20,8 +17,6 @@ export const INITIAL_TABS_STATE: TabsState = {
id: Date.now().toString(36) + Math.random().toString(36).substring(2),
title: "Chat 1",
isActive: true,
modelTitle: undefined,
mode: "chat",
},
],
};
@ -42,26 +37,6 @@ export const tabsSlice = createSlice({
tab.id === id ? { ...tab, ...updates } : tab,
);
},
// Set the model title for a specific tab
setTabModel: (
state,
action: PayloadAction<{ id: string; modelTitle: string }>,
) => {
const { id, modelTitle } = action.payload;
state.tabs = state.tabs.map((tab) =>
tab.id === id ? { ...tab, modelTitle } : tab,
);
},
// Set the mode for a specific tab
setTabMode: (
state,
action: PayloadAction<{ id: string; mode: MessageModes }>,
) => {
const { id, mode } = action.payload;
state.tabs = state.tabs.map((tab) =>
tab.id === id ? { ...tab, mode } : tab,
);
},
addTab: (state, action: PayloadAction<Tab>) => {
state.tabs = state.tabs
.map((tab) => ({
@ -147,7 +122,6 @@ export const tabsSlice = createSlice({
title: currentSessionTitle,
isActive: true,
sessionId: currentSessionId,
modelTitle: undefined,
});
}
},
@ -157,8 +131,6 @@ export const tabsSlice = createSlice({
export const {
setTabs,
updateTab,
setTabModel,
setTabMode,
addTab,
removeTab,
setActiveTab,

View file

@ -103,7 +103,10 @@ export const loadSession = createAsyncThunk<
ThunkApiType
>(
"session/load",
async ({ sessionId, saveCurrentSession: save }, { extra, dispatch }) => {
async (
{ sessionId, saveCurrentSession: save },
{ extra, dispatch, getState },
) => {
if (save) {
const result = await dispatch(
saveCurrentSession({
@ -115,6 +118,32 @@ export const loadSession = createAsyncThunk<
}
const session = await getSession(extra.ideMessenger, sessionId);
dispatch(newSession(session));
// Restore selected chat model from session, if present
const chatModelTitle = (session as any).chatModelTitle as
| string
| null
| undefined;
if (chatModelTitle) {
const state = getState();
const org = state.profiles.organizations.find(
(o) => o.id === state.profiles.selectedOrganizationId,
);
const selectedProfile =
org?.profiles.find((p) => p.id === state.profiles.selectedProfileId) ??
null;
if (selectedProfile) {
await dispatch(
(
await import("../thunks/updateSelectedModelByRole")
).updateSelectedModelByRole({
role: "chat",
modelTitle: chatModelTitle,
selectedProfile,
}) as any,
);
}
}
},
);
@ -127,7 +156,10 @@ export const loadRemoteSession = createAsyncThunk<
ThunkApiType
>(
"session/loadRemote",
async ({ remoteId, saveCurrentSession: save }, { extra, dispatch }) => {
async (
{ remoteId, saveCurrentSession: save },
{ extra, dispatch, getState },
) => {
if (save) {
const result = await dispatch(
saveCurrentSession({
@ -139,6 +171,32 @@ export const loadRemoteSession = createAsyncThunk<
}
const session = await getRemoteSession(extra.ideMessenger, remoteId);
dispatch(newSession(session));
// Restore selected chat model from session, if present
const chatModelTitle = (session as any).chatModelTitle as
| string
| null
| undefined;
if (chatModelTitle) {
const state = getState();
const org = state.profiles.organizations.find(
(o) => o.id === state.profiles.selectedOrganizationId,
);
const selectedProfile =
org?.profiles.find((p) => p.id === state.profiles.selectedProfileId) ??
null;
if (selectedProfile) {
await dispatch(
(
await import("../thunks/updateSelectedModelByRole")
).updateSelectedModelByRole({
role: "chat",
modelTitle: chatModelTitle,
selectedProfile,
}) as any,
);
}
}
},
);
@ -168,6 +226,32 @@ export const loadLastSession = createAsyncThunk<void, void, ThunkApiType>(
session = await getSession(extra.ideMessenger, lastSessionId);
}
dispatch(newSession(session));
// Restore selected chat model from session, if present
const chatModelTitle = (session as any).chatModelTitle as
| string
| null
| undefined;
if (chatModelTitle) {
const state = getState();
const org = state.profiles.organizations.find(
(o) => o.id === state.profiles.selectedOrganizationId,
);
const selectedProfile =
org?.profiles.find((p) => p.id === state.profiles.selectedProfileId) ??
null;
if (selectedProfile) {
await dispatch(
(
await import("../thunks/updateSelectedModelByRole")
).updateSelectedModelByRole({
role: "chat",
modelTitle: chatModelTitle,
selectedProfile,
}) as any,
);
}
}
},
);
@ -246,12 +330,16 @@ export const saveCurrentSession = createAsyncThunk<
title = NEW_SESSION_TITLE;
}
const selectedChatModel = selectSelectedChatModel(state);
const session: Session = {
sessionId: state.session.id,
title,
workspaceDirectory: window.workspacePaths?.[0] || "",
history: state.session.history,
};
mode: state.session.mode,
chatModelTitle: selectedChatModel?.title ?? null,
} as Session;
const result = await dispatch(updateSession(session));
unwrapResult(result);