From be9774943bc17e30111ccf6ec1eb8242e61f3fa1 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Sun, 12 Nov 2023 00:29:36 +0800 Subject: [PATCH] feat: #3224 auto switch to first avaliable model --- app/components/chat.tsx | 22 +++++++++++++++++++--- app/utils/hooks.ts | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index c27c3eee..48f76e8a 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -431,11 +431,27 @@ export function ChatActions(props: { // switch model const currentModel = chatStore.currentSession().mask.modelConfig.model; - const models = useAllModels() - .filter((m) => m.available) - .map((m) => m.name); + const allModels = useAllModels(); + const models = useMemo( + () => allModels.filter((m) => m.available).map((m) => m.name), + [allModels], + ); const [showModelSelector, setShowModelSelector] = useState(false); + useEffect(() => { + // if current model is not available + // switch to first available model + const isUnavaliableModel = !models.includes(currentModel); + if (isUnavaliableModel && models.length > 0) { + const nextModel = models[0] as ModelType; + chatStore.updateCurrentSession( + (session) => (session.mask.modelConfig.model = nextModel), + ); + showToast(nextModel); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [currentModel, models]); + return (
{couldStop && ( diff --git a/app/utils/hooks.ts b/app/utils/hooks.ts index f6bfae67..35d1f53a 100644 --- a/app/utils/hooks.ts +++ b/app/utils/hooks.ts @@ -8,7 +8,7 @@ export function useAllModels() { const models = useMemo(() => { return collectModels( configStore.models, - [accessStore.customModels, configStore.customModels].join(","), + [configStore.customModels, accessStore.customModels].join(","), ); }, [accessStore.customModels, configStore.customModels, configStore.models]);