Skip to content

Commit e1b6b10

Browse files
authored
Merge pull request #371 from Mng-dev-ai/fix/model-selector-landing-page
Fix model selector not persisting chosen model across pages
2 parents 62b6ee8 + c603047 commit e1b6b10

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

frontend/src/components/chat/chat-window/ChatSessionOrchestrator.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ export function ChatSessionOrchestrator({
5050
})),
5151
);
5252

53-
const { selectedModelId, selectModel } = useModelSelection({ chatId });
53+
const lastAssistantModelId = useMemo((): string | null | undefined => {
54+
if (messagesQuery.isLoading) return null;
55+
for (let i = fetchedMessages.length - 1; i >= 0; i--) {
56+
if (fetchedMessages[i].role === 'assistant' && fetchedMessages[i].model_id) {
57+
return fetchedMessages[i].model_id;
58+
}
59+
}
60+
return undefined;
61+
}, [fetchedMessages, messagesQuery.isLoading]);
62+
63+
const { selectedModelId, selectModel } = useModelSelection({
64+
chatId,
65+
initialModelId: lastAssistantModelId,
66+
});
5467

5568
const {
5669
initialPrompt,

frontend/src/hooks/queries/useModelQueries.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ export const useModelsQuery = (options?: Partial<UseQueryOptions<Model[]>>) => {
1717
});
1818
};
1919

20-
export const useModelSelection = (options?: { enabled?: boolean; chatId?: string }) => {
20+
export const useModelSelection = (options?: {
21+
enabled?: boolean;
22+
chatId?: string;
23+
// null = still loading (defer default), undefined = no initial model (use models[0])
24+
initialModelId?: string | null;
25+
}) => {
2126
const chatId = options?.chatId ?? DEFAULT_MODEL_KEY;
27+
const initialModelId = options?.initialModelId;
2228
const { data: models = [], isLoading } = useModelsQuery({
2329
enabled: options?.enabled,
2430
});
@@ -27,10 +33,17 @@ export const useModelSelection = (options?: { enabled?: boolean; chatId?: string
2733
useEffect(() => {
2834
if (models.length === 0) return;
2935
const selectedExists = models.some((m) => m.model_id === selectedModelId);
30-
if (!selectedExists) {
31-
useModelStore.getState().selectModel(chatId, models[0].model_id);
32-
}
33-
}, [models, selectedModelId, chatId]);
36+
if (selectedExists) return;
37+
38+
// Still loading initial model from message history — wait before defaulting
39+
if (initialModelId === null) return;
40+
41+
const fallback =
42+
initialModelId && models.some((m) => m.model_id === initialModelId)
43+
? initialModelId
44+
: models[0].model_id;
45+
useModelStore.getState().selectModel(chatId, fallback);
46+
}, [models, selectedModelId, chatId, initialModelId]);
3447

3548
const selectedModel = useMemo(
3649
() => models.find((m) => m.model_id === selectedModelId) ?? null,

frontend/src/pages/LandingPage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useLayoutSidebar } from '@/components/layout/layoutState';
66
import { Input as ChatInput } from '@/components/chat/message-input/Input';
77
import { WorkspaceSelector } from '@/components/chat/WorkspaceSelector';
88
import { useChatStore } from '@/store/chatStore';
9+
import { useModelStore } from '@/store/modelStore';
910
import { useAuthStore } from '@/store/authStore';
1011
import { useInfiniteChatsQuery, useCreateChatMutation } from '@/hooks/queries/useChatQueries';
1112
import { useWorkspacesQuery } from '@/hooks/queries/useWorkspaceQueries';
@@ -131,6 +132,7 @@ export function LandingPage() {
131132
model_id: selectedModelId,
132133
workspace_id: selectedWorkspaceId,
133134
});
135+
useModelStore.getState().selectModel(newChat.id, selectedModelId);
134136
setMessage('');
135137
navigate(`/chat/${newChat.id}`, { state: { initialPrompt: trimmedPrompt } });
136138
} catch (error) {

0 commit comments

Comments
 (0)