Add OpenAI key save and test controls

This commit is contained in:
rajchales-monito
2026-07-29 21:27:32 +02:00
parent d1cd58c586
commit 9f9f90df2e
4 changed files with 71 additions and 4 deletions
+22
View File
@@ -86,6 +86,7 @@ async function adminApi(req, res, url) {
if (url.pathname === "/api/admin/events" && req.method === "GET") return eventStream(req, res, url, "admin");
if (url.pathname === "/api/admin/site" && req.method === "PATCH") return updateSite(req, res);
if (url.pathname === "/api/admin/ai/models" && req.method === "GET") return adminAiModels(res);
if (url.pathname === "/api/admin/ai/test" && req.method === "POST") return adminAiTest(res);
if (url.pathname === "/api/admin/conversations" && req.method === "GET") return adminConversations(res);
if (url.pathname === "/api/admin/visitors" && req.method === "GET") return adminVisitors(res);
if (url.pathname.match(/^\/api\/admin\/visitors\/[^/]+$/) && req.method === "PATCH") return updateVisitorProfile(req, res, url);
@@ -471,6 +472,27 @@ async function adminAiModels(res) {
}
}
async function adminAiTest(res) {
const settings = getTranslationSettings();
const apiKey = settings.openaiApiKey;
if (!apiKey) return json(res, 400, { ok: false, error: "missing_openai_api_key" });
try {
const response = await fetch("https://api.openai.com/v1/models", {
headers: { "Authorization": `Bearer ${apiKey}` }
});
if (!response.ok) return json(res, 400, { ok: false, error: `openai_http_${response.status}` });
const data = await response.json();
return json(res, 200, {
ok: true,
key: maskApiKey(apiKey),
modelCount: Array.isArray(data.data) ? data.data.length : 0
});
} catch (error) {
console.error("openai_test_failed", error.message);
return json(res, 502, { ok: false, error: "openai_test_failed" });
}
}
async function updateConversationStatus(req, res, url) {
const body = await readJson(req);
const status = ["new", "open", "resolved", "spam"].includes(body.status) ? body.status : null;