mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-11 17:26:32 +02:00
fix(ai): persist openrouter settings on server and fix 503 error
This commit is contained in:
1 parent
643b26dcf3
commit
c41a60b430
3 files changed
+34
-18
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in new issue
Block a user