@@ -1,5 +1,6 @@
11 import { NextResponse } from "next/server";
22 import { getCache, setCache } from "@/src/lib/cache";
3+import { getFirstAvailableModel } from "@/src/lib/ollama/models";
34
45 const SETTINGS_CACHE_KEY = "app_settings";
56
@@ -11,9 +12,18 @@ type Settings = {
1112 export async function GET() {
1213 try {
1314 const settings = await getCache<Settings>(SETTINGS_CACHE_KEY);
14- return NextResponse.json({
15- settings: settings || { maxPromptLength: "none", selectedModel: "gpt-oss" },
16- });
15+ if (settings) {
16+ return NextResponse.json({ settings });
17+ }
18+
19+ // If no settings exist, get the first available model
20+ const firstModel = await getFirstAvailableModel();
21+ const defaultSettings: Settings = {
22+ maxPromptLength: "none",
23+ selectedModel: firstModel || undefined,
24+ };
25+
26+ return NextResponse.json({ settings: defaultSettings });
1727 } catch {
1828 return NextResponse.json({ error: "Failed to get settings" }, { status: 500 });
1929 }
@@ -23,7 +33,7 @@ export async function POST(request: Request) {
2333 try {
2434 const { maxPromptLength, selectedModel } = await request.json();
2535
26- const settings: Partial<Settings> = {};
36+ let settings: Partial<Settings> = {};
2737
2838 // Validate and set maxPromptLength if provided
2939 if (maxPromptLength !== undefined) {
@@ -41,16 +51,21 @@ export async function POST(request: Request) {
4151 settings.selectedModel = selectedModel;
4252 }
4353
44- // Get existing settings and merge
54+ // Get existing settings and merge, prefering the ones in the request
4555 const existingSettings = await getCache<Settings>(SETTINGS_CACHE_KEY);
46- const mergedSettings: Settings = {
47- maxPromptLength: settings.maxPromptLength ?? existingSettings?.maxPromptLength ?? "none",
48- selectedModel: settings.selectedModel ?? existingSettings?.selectedModel ?? "gpt-oss",
49- };
56+ if (existingSettings) {
57+ settings = {...existingSettings, ...settings}
58+ }
5059
51- await setCache(SETTINGS_CACHE_KEY, mergedSettings);
60+ if (!settings.selectedModel) {
61+ const firstModel = await getFirstAvailableModel();
62+ settings.selectedModel = firstModel
63+ }
64+
65+ // Update the cache with new settings.
66+ await setCache(SETTINGS_CACHE_KEY, settings);
5267
53- return NextResponse.json({ settings: mergedSettings });
68+ return NextResponse.json({ settings: settings });
5469 } catch {
5570 return NextResponse.json({ error: "Failed to set settings" }, { status: 500 });
5671 }