From 6891aff6688f43d37c56f703aa1e1b055b0520e3 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Fri, 10 Apr 2026 13:38:26 +0200 Subject: [PATCH] feat: implement admin AI chat route with SQL execution and database schema access tools --- src/app/api/admin/ai-chat/route.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/app/api/admin/ai-chat/route.ts b/src/app/api/admin/ai-chat/route.ts index a0f21fd..4109b28 100644 --- a/src/app/api/admin/ai-chat/route.ts +++ b/src/app/api/admin/ai-chat/route.ts @@ -1,6 +1,20 @@ import { NextRequest } from "next/server"; import { auth } from "@/lib/auth"; import { getDb } from "@/db"; +import fs from "fs/promises"; +import path from "path"; + +async function getOpenRouterKey(): Promise { + if (process.env.OPENROUTER_API_KEY) return process.env.OPENROUTER_API_KEY; + try { + const configFile = path.join(process.cwd(), "data", ".db-config.json"); + const data = await fs.readFile(configFile, "utf-8"); + const config = JSON.parse(data); + return config.openRouterKey ?? null; + } catch { + return null; + } +} const OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"; @@ -188,7 +202,7 @@ export async function POST(req: NextRequest) { const { messages, model } = await req.json(); - const apiKey = process.env.OPENROUTER_API_KEY; + const apiKey = await getOpenRouterKey(); if (!apiKey) { return new Response(JSON.stringify({ error: "No OpenRouter API key configured" }), { status: 500 }); }