mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-13 10:17:33 +02:00
feat: implement admin AI chat route with SQL execution and database schema access tools
This commit is contained in:
1 parent
63d89e5029
commit
6891aff668
1 file changed
+15
-1
@@ -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<string | null> {
|
||||
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 });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user