mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-20 13:38:08 +02:00
debug(ai): add diagnostic logs for AI API key persistence
This commit is contained in:
1 parent
c41a60b430
commit
85215565fa
2 files changed
+12
-2
No files matched your search
@@ -19,14 +19,18 @@ 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) {
|
||||||
|
console.log("[AI] Analysis request received.");
|
||||||
const config = await getSavedDatabaseConfig();
|
const config = await getSavedDatabaseConfig();
|
||||||
const apiKey = process.env.OPENROUTER_API_KEY || config?.openRouterKey;
|
const apiKey = process.env.OPENROUTER_API_KEY || config?.openRouterKey;
|
||||||
const model = config?.openRouterModel || "google/gemini-flash-1.5";
|
const model = config?.openRouterModel || "google/gemini-flash-1.5";
|
||||||
|
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
|
console.error("[AI] OPENROUTER_API_KEY is missing (neither in env nor in config file).");
|
||||||
return NextResponse.json({ error: "OPENROUTER_API_KEY not configured. Please set it in Settings." }, { status: 503 });
|
return NextResponse.json({ error: "OPENROUTER_API_KEY not configured. Please set it in Settings." }, { status: 503 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`[AI] Using model: ${model} with API key starting with ${apiKey.substring(0, 10)}...`);
|
||||||
|
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
const parsed = AnalyzeSchema.safeParse(body);
|
const parsed = AnalyzeSchema.safeParse(body);
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
|
|||||||
@@ -41,8 +41,9 @@ export async function saveDatabaseSettings(url: string, openRouterKey?: string,
|
|||||||
// 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 });
|
||||||
|
|
||||||
|
console.log(`[Settings] Saving config to ${CONFIG_FILE}`);
|
||||||
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
||||||
console.log("Database configuration saved to:", CONFIG_FILE);
|
console.log("[Settings] Database configuration saved successfully.");
|
||||||
|
|
||||||
// Refresh the shared DB instance immediately
|
// Refresh the shared DB instance immediately
|
||||||
const { refreshDb } = await import("@/db");
|
const { refreshDb } = await import("@/db");
|
||||||
@@ -58,9 +59,14 @@ export async function saveDatabaseSettings(url: string, openRouterKey?: string,
|
|||||||
|
|
||||||
export async function getSavedDatabaseConfig(): Promise<DbConfig | null> {
|
export async function getSavedDatabaseConfig(): Promise<DbConfig | null> {
|
||||||
try {
|
try {
|
||||||
|
if (!(await fs.stat(CONFIG_FILE).catch(() => null))) {
|
||||||
|
console.log(`[Settings] Config file not found at ${CONFIG_FILE}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const data = await fs.readFile(CONFIG_FILE, "utf-8");
|
const data = await fs.readFile(CONFIG_FILE, "utf-8");
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
console.error(`[Settings] Error reading config from ${CONFIG_FILE}:`, error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user