diff --git a/components/chat.tsx b/components/chat.tsx index 86d51a8..b4998ad 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -2,7 +2,7 @@ import { DefaultChatTransport } from 'ai'; import { useChat } from '@ai-sdk/react'; -import { useEffect, useState } from 'react'; +import { useEffect, useState, useRef } from 'react'; import useSWR, { useSWRConfig } from 'swr'; import { ChatHeader } from '@/components/chat-header'; import type { Vote } from '@/lib/db/schema'; @@ -64,6 +64,12 @@ export function Chat({ const [input, setInput] = useState(''); const [usage, setUsage] = useState(initialLastContext); const [showCreditCardAlert, setShowCreditCardAlert] = useState(false); + const [currentModelId, setCurrentModelId] = useState(initialChatModel); + const currentModelIdRef = useRef(currentModelId); + + useEffect(() => { + currentModelIdRef.current = currentModelId; + }, [currentModelId]); const { messages, @@ -86,7 +92,7 @@ export function Chat({ body: { id, message: messages.at(-1), - selectedChatModel: initialChatModel, + selectedChatModel: currentModelIdRef.current, selectedVisibilityType: visibilityType, ...body, }, @@ -185,7 +191,8 @@ export function Chat({ setMessages={setMessages} sendMessage={sendMessage} selectedVisibilityType={visibilityType} - selectedModelId={initialChatModel} + selectedModelId={currentModelId} + onModelChange={setCurrentModelId} usage={usage} /> )} @@ -207,7 +214,7 @@ export function Chat({ votes={votes} isReadonly={isReadonly} selectedVisibilityType={visibilityType} - selectedModelId={initialChatModel} + selectedModelId={currentModelId} /> void; usage?: AppUsage; }) { const textareaRef = useRef(null); @@ -353,7 +355,7 @@ function PureMultimodalInput({ status={status} selectedModelId={selectedModelId} /> - + {status === 'submitted' ? ( @@ -418,11 +420,17 @@ const AttachmentsButton = memo(PureAttachmentsButton); function PureModelSelectorCompact({ selectedModelId, + onModelChange, }: { selectedModelId: string; + onModelChange?: (modelId: string) => void; }) { const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId); + useEffect(() => { + setOptimisticModelId(selectedModelId); + }, [selectedModelId]); + const selectedModel = chatModels.find( (model) => model.id === optimisticModelId, ); @@ -434,6 +442,7 @@ function PureModelSelectorCompact({ const model = chatModels.find((m) => m.name === modelName); if (model) { setOptimisticModelId(model.id); + onModelChange?.(model.id); startTransition(() => { saveChatModelAsCookie(model.id); });