diff --git a/src/app/api/chat/sessions/[id]/route.ts b/src/app/api/chat/sessions/[id]/route.ts index 5729cd7..91d760b 100644 --- a/src/app/api/chat/sessions/[id]/route.ts +++ b/src/app/api/chat/sessions/[id]/route.ts @@ -1,5 +1,5 @@ import { NextRequest } from 'next/server'; -import { deleteSession, getSession, updateSessionWorkingDirectory, updateSessionTitle, updateSessionMode, clearSessionMessages } from '@/lib/db'; +import { deleteSession, getSession, updateSessionWorkingDirectory, updateSessionTitle, updateSessionMode, updateSessionModel, updateSessionProviderId, clearSessionMessages } from '@/lib/db'; export async function GET( _request: NextRequest, @@ -40,6 +40,12 @@ export async function PATCH( if (body.mode) { updateSessionMode(id, body.mode); } + if (body.model !== undefined) { + updateSessionModel(id, body.model); + } + if (body.provider_id !== undefined) { + updateSessionProviderId(id, body.provider_id); + } if (body.clear_messages) { clearSessionMessages(id); } diff --git a/src/components/chat/ChatView.tsx b/src/components/chat/ChatView.tsx index 24084b0..bfa197f 100644 --- a/src/components/chat/ChatView.tsx +++ b/src/components/chat/ChatView.tsx @@ -35,6 +35,14 @@ export function ChatView({ sessionId, initialMessages = [], initialHasMore = fal const [currentModel, setCurrentModel] = useState(modelName || (typeof window !== 'undefined' ? localStorage.getItem('codepilot:last-model') : null) || 'sonnet'); const [currentProviderId, setCurrentProviderId] = useState(providerId || (typeof window !== 'undefined' ? localStorage.getItem('codepilot:last-provider-id') : null) || ''); + // Sync model/provider when session data loads (props update after async fetch) + useEffect(() => { + if (modelName) setCurrentModel(modelName); + }, [modelName]); + useEffect(() => { + if (providerId) setCurrentProviderId(providerId); + }, [providerId]); + // Stream snapshot from the manager — drives all streaming UI const [streamSnapshot, setStreamSnapshot] = useState( () => getSnapshot(sessionId) @@ -79,7 +87,13 @@ export function ChatView({ sessionId, initialMessages = [], initialHasMore = fal const handleProviderModelChange = useCallback((newProviderId: string, model: string) => { setCurrentProviderId(newProviderId); setCurrentModel(model); - }, []); + // Persist immediately so switching chats preserves the selection + fetch(`/api/chat/sessions/${sessionId}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model, provider_id: newProviderId }), + }).catch(() => {}); + }, [sessionId]); // Subscribe to stream-session-manager for this session. // On unmount we only unsubscribe — we do NOT abort the stream.