mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-23 23:18:10 +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(() => {
|
useEffect(() => {
|
||||||
setIsMounted(true);
|
setIsMounted(true);
|
||||||
|
reloadFromServer();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const fetchModels = useCallback(async (key: string) => {
|
const fetchModels = useCallback(async (key: string) => {
|
||||||
@@ -95,18 +96,27 @@ export default function SettingsPage() {
|
|||||||
|
|
||||||
const reloadFromServer = async () => {
|
const reloadFromServer = async () => {
|
||||||
const config = await getSavedDatabaseConfig();
|
const config = await getSavedDatabaseConfig();
|
||||||
if (config && config.url) {
|
if (config) {
|
||||||
// Parser l'URL pour remettre dans le store
|
if (config.url) {
|
||||||
try {
|
// Parser l'URL pour remettre dans le store
|
||||||
const url = new URL(config.url.replace("postgres://", "http://")); // URL parser helper
|
try {
|
||||||
setHost(url.hostname);
|
const url = new URL(config.url.replace("postgres://", "http://")); // URL parser helper
|
||||||
setPort(url.port || "5432");
|
setHost(url.hostname);
|
||||||
setDatabase(url.pathname.slice(1).split("?")[0]);
|
setPort(url.port || "5432");
|
||||||
setUser(url.username);
|
setDatabase(url.pathname.slice(1).split("?")[0]);
|
||||||
setPassword(decodeURIComponent(url.password));
|
setUser(url.username);
|
||||||
setSsl(config.url.includes("sslmode=require"));
|
setPassword(decodeURIComponent(url.password));
|
||||||
} catch (e) {
|
setSsl(config.url.includes("sslmode=require"));
|
||||||
console.error("Failed to parse saved URL", e);
|
} 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 () => {
|
const handleSave = async () => {
|
||||||
setSaveStatus("saving");
|
setSaveStatus("saving");
|
||||||
const url = getDatabaseUrl();
|
const url = getDatabaseUrl();
|
||||||
const res = await saveDatabaseSettings(url);
|
const res = await saveDatabaseSettings(url, apiKey, selectedModel);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
setSaveStatus("saved");
|
setSaveStatus("saved");
|
||||||
setTimeout(() => setSaveStatus("idle"), 2500);
|
setTimeout(() => setSaveStatus("idle"), 2500);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { getSavedDatabaseConfig } from "@/features/settings/actions";
|
||||||
|
|
||||||
const AnalyzeSchema = z.object({
|
const AnalyzeSchema = z.object({
|
||||||
codein: z.string(),
|
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]"`;
|
Format: "[Recommandation]: [Justification courte basée sur les données]"`;
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
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) {
|
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();
|
const body = await req.json();
|
||||||
@@ -53,7 +57,7 @@ Quelle gamme recommandes-tu et pourquoi ?`;
|
|||||||
"X-Title": "CollectFlow AI Copilot",
|
"X-Title": "CollectFlow AI Copilot",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: "google/gemini-flash-1.5",
|
model,
|
||||||
messages: [
|
messages: [
|
||||||
{ role: "system", content: SYSTEM_PROMPT },
|
{ role: "system", content: SYSTEM_PROMPT },
|
||||||
{ role: "user", content: userMessage },
|
{ role: "user", content: userMessage },
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ const CONFIG_FILE = path.join(DATA_DIR, ".db-config.json");
|
|||||||
|
|
||||||
export interface DbConfig {
|
export interface DbConfig {
|
||||||
url: string;
|
url: string;
|
||||||
|
openRouterKey?: string;
|
||||||
|
openRouterModel?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function testDatabaseConnection(url: 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 {
|
try {
|
||||||
const config: DbConfig = { url };
|
const config: DbConfig = { url, openRouterKey, openRouterModel };
|
||||||
|
|
||||||
// S'assurer que le dossier data existe
|
// S'assurer que le dossier data existe
|
||||||
await fs.mkdir(DATA_DIR, { recursive: true });
|
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||||
|
|||||||
Reference in new issue
Block a user