xonecas.io
53129
Pa

PAT / commit 2ee1cbc

fix: refresh the model list correctly

xonecas2025-12-17 2ee1cbcf681fa4c6f8bb513a42f1373bba08e667 parent 343b043

4 files changed

app/page.tsx+1−1
@@ -36,7 +36,7 @@ export default function Home() {
3636 };
3737
3838 const handleSendMessage = async (message: string) => {
39- await sendMessage(message, currentThreadId, createThread, selectThread, loadThreads);
39+ await sendMessage(message, currentThreadId, createThread, selectThread, loadThreads, selectedModel);
4040 };
4141
4242 // Get the model name for the current thread, or use selected model if no thread
src/components/SettingsModal.tsx+17−2
@@ -29,7 +29,22 @@ export default function SettingsModal({
2929 fetch("/api/models")
3030 .then((res) => res.json())
3131 .then((data) => {
32- setModels(data.models || []);
32+ const availableModels = data.models || [];
33+ setModels(availableModels);
34+
35+ // Validate selected model against available models
36+ if (availableModels.length > 0) {
37+ const modelExists = availableModels.some(
38+ (m: { name: string; model: string }) => m.model === selectedModel
39+ );
40+
41+ if (!modelExists) {
42+ // Selected model doesn't exist, fall back to first available
43+ const fallbackModel = availableModels[0].model;
44+ onModelChange(fallbackModel);
45+ }
46+ }
47+
3348 setIsLoadingModels(false);
3449 })
3550 .catch((error) => {
@@ -37,7 +52,7 @@ export default function SettingsModal({
3752 setIsLoadingModels(false);
3853 });
3954 }
40- }, [isOpen]);
55+ }, [isOpen, selectedModel, onModelChange]);
4156
4257 return (
4358 <Modal isOpen={isOpen} onClose={onClose}>
src/hooks/useMessages.ts+5−4
@@ -37,9 +37,10 @@ export function useMessages(threadId: number | null) {
3737 const sendMessage = async (
3838 message: string,
3939 currentThreadId: number | null,
40- onCreateThread: (title?: string, firstMessage?: string) => Promise<number | null>,
40+ onCreateThread: (title?: string, firstMessage?: string, model?: string) => Promise<number | null>,
4141 onThreadSelect: (id: number) => void,
42- onThreadsReload: () => void
42+ onThreadsReload: () => void,
43+ selectedModel: string = "gpt-oss"
4344 ) => {
4445 if (!message.trim() || isLoading) return;
4546
@@ -48,8 +49,8 @@ export function useMessages(threadId: number | null) {
4849
4950 if (!targetThreadId) {
5051 // Create a new thread if none exists
51- const model = localStorage.getItem("selectedModel") || "gpt-oss";
52- targetThreadId = await onCreateThread(message.substring(0, 100), message, model);
52+ // Use the validated selectedModel instead of reading from localStorage
53+ targetThreadId = await onCreateThread(message.substring(0, 100), message, selectedModel);
5354 if (!targetThreadId) {
5455 setIsLoading(false);
5556 return;
src/hooks/useModel.ts+38−4
@@ -3,11 +3,45 @@ import { useState, useEffect } from "react";
33 export function useModel() {
44 const [selectedModel, setSelectedModel] = useState<string>("gpt-oss");
55
6+ // Validate and sync the selected model with available models
67 useEffect(() => {
7- const savedModel = localStorage.getItem("selectedModel");
8- if (savedModel) {
9- setSelectedModel(savedModel);
10- }
8+ const validateModel = async () => {
9+ const savedModel = localStorage.getItem("selectedModel") || "gpt-oss";
10+
11+ try {
12+ const res = await fetch("/api/models");
13+ const data = await res.json();
14+ const availableModels = data.models || [];
15+
16+ if (availableModels.length === 0) {
17+ // No models available, keep default
18+ setSelectedModel("gpt-oss");
19+ localStorage.setItem("selectedModel", "gpt-oss");
20+ return;
21+ }
22+
23+ // Check if saved model exists in available models
24+ const modelExists = availableModels.some(
25+ (m: { name: string; model: string }) => m.model === savedModel
26+ );
27+
28+ if (modelExists) {
29+ // Model is valid, use it
30+ setSelectedModel(savedModel);
31+ } else {
32+ // Model doesn't exist, fall back to first available model
33+ const fallbackModel = availableModels[0].model;
34+ setSelectedModel(fallbackModel);
35+ localStorage.setItem("selectedModel", fallbackModel);
36+ }
37+ } catch (error) {
38+ console.error("Failed to validate model", error);
39+ // On error, use saved model or default
40+ setSelectedModel(savedModel);
41+ }
42+ };
43+
44+ validateModel();
1145 }, []);
1246
1347 const handleModelChange = (model: string) => {