fix(chat): model selection persistence when switching between models (#1197)
This commit is contained in:
parent
5ab695262f
commit
dfa22bbede
2 changed files with 21 additions and 5 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import { DefaultChatTransport } from 'ai';
|
import { DefaultChatTransport } from 'ai';
|
||||||
import { useChat } from '@ai-sdk/react';
|
import { useChat } from '@ai-sdk/react';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState, useRef } from 'react';
|
||||||
import useSWR, { useSWRConfig } from 'swr';
|
import useSWR, { useSWRConfig } from 'swr';
|
||||||
import { ChatHeader } from '@/components/chat-header';
|
import { ChatHeader } from '@/components/chat-header';
|
||||||
import type { Vote } from '@/lib/db/schema';
|
import type { Vote } from '@/lib/db/schema';
|
||||||
|
|
@ -64,6 +64,12 @@ export function Chat({
|
||||||
const [input, setInput] = useState<string>('');
|
const [input, setInput] = useState<string>('');
|
||||||
const [usage, setUsage] = useState<AppUsage | undefined>(initialLastContext);
|
const [usage, setUsage] = useState<AppUsage | undefined>(initialLastContext);
|
||||||
const [showCreditCardAlert, setShowCreditCardAlert] = useState(false);
|
const [showCreditCardAlert, setShowCreditCardAlert] = useState(false);
|
||||||
|
const [currentModelId, setCurrentModelId] = useState(initialChatModel);
|
||||||
|
const currentModelIdRef = useRef(currentModelId);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
currentModelIdRef.current = currentModelId;
|
||||||
|
}, [currentModelId]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
messages,
|
messages,
|
||||||
|
|
@ -86,7 +92,7 @@ export function Chat({
|
||||||
body: {
|
body: {
|
||||||
id,
|
id,
|
||||||
message: messages.at(-1),
|
message: messages.at(-1),
|
||||||
selectedChatModel: initialChatModel,
|
selectedChatModel: currentModelIdRef.current,
|
||||||
selectedVisibilityType: visibilityType,
|
selectedVisibilityType: visibilityType,
|
||||||
...body,
|
...body,
|
||||||
},
|
},
|
||||||
|
|
@ -185,7 +191,8 @@ export function Chat({
|
||||||
setMessages={setMessages}
|
setMessages={setMessages}
|
||||||
sendMessage={sendMessage}
|
sendMessage={sendMessage}
|
||||||
selectedVisibilityType={visibilityType}
|
selectedVisibilityType={visibilityType}
|
||||||
selectedModelId={initialChatModel}
|
selectedModelId={currentModelId}
|
||||||
|
onModelChange={setCurrentModelId}
|
||||||
usage={usage}
|
usage={usage}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
@ -207,7 +214,7 @@ export function Chat({
|
||||||
votes={votes}
|
votes={votes}
|
||||||
isReadonly={isReadonly}
|
isReadonly={isReadonly}
|
||||||
selectedVisibilityType={visibilityType}
|
selectedVisibilityType={visibilityType}
|
||||||
selectedModelId={initialChatModel}
|
selectedModelId={currentModelId}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<AlertDialog
|
<AlertDialog
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ function PureMultimodalInput({
|
||||||
className,
|
className,
|
||||||
selectedVisibilityType,
|
selectedVisibilityType,
|
||||||
selectedModelId,
|
selectedModelId,
|
||||||
|
onModelChange,
|
||||||
usage,
|
usage,
|
||||||
}: {
|
}: {
|
||||||
chatId: string;
|
chatId: string;
|
||||||
|
|
@ -79,6 +80,7 @@ function PureMultimodalInput({
|
||||||
className?: string;
|
className?: string;
|
||||||
selectedVisibilityType: VisibilityType;
|
selectedVisibilityType: VisibilityType;
|
||||||
selectedModelId: string;
|
selectedModelId: string;
|
||||||
|
onModelChange?: (modelId: string) => void;
|
||||||
usage?: AppUsage;
|
usage?: AppUsage;
|
||||||
}) {
|
}) {
|
||||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
|
@ -353,7 +355,7 @@ function PureMultimodalInput({
|
||||||
status={status}
|
status={status}
|
||||||
selectedModelId={selectedModelId}
|
selectedModelId={selectedModelId}
|
||||||
/>
|
/>
|
||||||
<ModelSelectorCompact selectedModelId={selectedModelId} />
|
<ModelSelectorCompact selectedModelId={selectedModelId} onModelChange={onModelChange} />
|
||||||
</PromptInputTools>
|
</PromptInputTools>
|
||||||
|
|
||||||
{status === 'submitted' ? (
|
{status === 'submitted' ? (
|
||||||
|
|
@ -418,11 +420,17 @@ const AttachmentsButton = memo(PureAttachmentsButton);
|
||||||
|
|
||||||
function PureModelSelectorCompact({
|
function PureModelSelectorCompact({
|
||||||
selectedModelId,
|
selectedModelId,
|
||||||
|
onModelChange,
|
||||||
}: {
|
}: {
|
||||||
selectedModelId: string;
|
selectedModelId: string;
|
||||||
|
onModelChange?: (modelId: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId);
|
const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setOptimisticModelId(selectedModelId);
|
||||||
|
}, [selectedModelId]);
|
||||||
|
|
||||||
const selectedModel = chatModels.find(
|
const selectedModel = chatModels.find(
|
||||||
(model) => model.id === optimisticModelId,
|
(model) => model.id === optimisticModelId,
|
||||||
);
|
);
|
||||||
|
|
@ -434,6 +442,7 @@ function PureModelSelectorCompact({
|
||||||
const model = chatModels.find((m) => m.name === modelName);
|
const model = chatModels.find((m) => m.name === modelName);
|
||||||
if (model) {
|
if (model) {
|
||||||
setOptimisticModelId(model.id);
|
setOptimisticModelId(model.id);
|
||||||
|
onModelChange?.(model.id);
|
||||||
startTransition(() => {
|
startTransition(() => {
|
||||||
saveChatModelAsCookie(model.id);
|
saveChatModelAsCookie(model.id);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue