diff --git a/src/app/(dashboard)/settings/page.tsx b/src/app/(dashboard)/settings/page.tsx index daf3849..438f8ee 100644 --- a/src/app/(dashboard)/settings/page.tsx +++ b/src/app/(dashboard)/settings/page.tsx @@ -73,6 +73,7 @@ export default function SettingsPage() { useEffect(() => { setIsMounted(true); + reloadFromServer(); }, []); const fetchModels = useCallback(async (key: string) => { @@ -95,18 +96,27 @@ export default function SettingsPage() { const reloadFromServer = async () => { const config = await getSavedDatabaseConfig(); - if (config && config.url) { - // Parser l'URL pour remettre dans le store - try { - const url = new URL(config.url.replace("postgres://", "http://")); // URL parser helper - setHost(url.hostname); - setPort(url.port || "5432"); - setDatabase(url.pathname.slice(1).split("?")[0]); - setUser(url.username); - setPassword(decodeURIComponent(url.password)); - setSsl(config.url.includes("sslmode=require")); - } catch (e) { - console.error("Failed to parse saved URL", e); + if (config) { + if (config.url) { + // Parser l'URL pour remettre dans le store + try { + const url = new URL(config.url.replace("postgres://", "http://")); // URL parser helper + setHost(url.hostname); + setPort(url.port || "5432"); + setDatabase(url.pathname.slice(1).split("?")[0]); + setUser(url.username); + setPassword(decodeURIComponent(url.password)); + setSsl(config.url.includes("sslmode=require")); + } catch (e) { + console.error("Failed to parse saved URL", e); + } + } + if (config.openRouterKey) { + setApiKey(config.openRouterKey); + fetchModels(config.openRouterKey); + } + if (config.openRouterModel) { + setSelectedModel(config.openRouterModel); } } }; @@ -127,7 +137,7 @@ export default function SettingsPage() { const handleSave = async () => { setSaveStatus("saving"); const url = getDatabaseUrl(); - const res = await saveDatabaseSettings(url); + const res = await saveDatabaseSettings(url, apiKey, selectedModel); if (res.success) { setSaveStatus("saved"); setTimeout(() => setSaveStatus("idle"), 2500); diff --git a/src/app/api/ai/analyze/route.ts b/src/app/api/ai/analyze/route.ts index 7a41b87..ba79bd3 100644 --- a/src/app/api/ai/analyze/route.ts +++ b/src/app/api/ai/analyze/route.ts @@ -1,5 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; +import { getSavedDatabaseConfig } from "@/features/settings/actions"; const AnalyzeSchema = z.object({ codein: z.string(), @@ -18,9 +19,12 @@ Ta réponse doit être en 1-2 phrases maximum, en français, directe et actionna Format: "[Recommandation]: [Justification courte basée sur les données]"`; export async function POST(req: NextRequest) { - const apiKey = process.env.OPENROUTER_API_KEY; + const config = await getSavedDatabaseConfig(); + const apiKey = process.env.OPENROUTER_API_KEY || config?.openRouterKey; + const model = config?.openRouterModel || "google/gemini-flash-1.5"; + if (!apiKey) { - return NextResponse.json({ error: "OPENROUTER_API_KEY not configured" }, { status: 503 }); + return NextResponse.json({ error: "OPENROUTER_API_KEY not configured. Please set it in Settings." }, { status: 503 }); } const body = await req.json(); @@ -53,7 +57,7 @@ Quelle gamme recommandes-tu et pourquoi ?`; "X-Title": "CollectFlow AI Copilot", }, body: JSON.stringify({ - model: "google/gemini-flash-1.5", + model, messages: [ { role: "system", content: SYSTEM_PROMPT }, { role: "user", content: userMessage }, diff --git a/src/features/settings/actions.ts b/src/features/settings/actions.ts index 1449af1..e2de9e3 100644 --- a/src/features/settings/actions.ts +++ b/src/features/settings/actions.ts @@ -9,6 +9,8 @@ const CONFIG_FILE = path.join(DATA_DIR, ".db-config.json"); export interface DbConfig { url: string; + openRouterKey?: string; + openRouterModel?: string; } export async function testDatabaseConnection(url: string) { @@ -32,9 +34,9 @@ export async function testDatabaseConnection(url: string) { } } -export async function saveDatabaseSettings(url: string) { +export async function saveDatabaseSettings(url: string, openRouterKey?: string, openRouterModel?: string) { try { - const config: DbConfig = { url }; + const config: DbConfig = { url, openRouterKey, openRouterModel }; // S'assurer que le dossier data existe await fs.mkdir(DATA_DIR, { recursive: true });