// OpenAI page: posts the chosen action to /ai/openai/. (function () { "use strict"; var $ = function (id) { return document.getElementById(id); }; var routes = { responses: ["/ai/openai/responses", function (p) { return { input: p }; }], chat: ["/ai/openai/chat", function (p) { return { prompt: p }; }], completions: ["/ai/openai/completions", function (p) { return { prompt: p }; }], embeddings: ["/ai/openai/embeddings", function (p) { return { input: p }; }], moderations: ["/ai/openai/moderations", function (p) { return { input: p }; }], models: ["/ai/openai/models", function () { return {}; }], tokens: ["/ai/openai/responses/tokens", function (p) { return { input: p }; }], images: ["/ai/openai/images", function (p) { return { prompt: p }; }], speech: ["/ai/openai/speech", function (p) { return { input: p }; }], videos: ["/ai/openai/videos", function (p) { return { prompt: p }; }] }; var MAX_OUT = 20000; fetch("/ai/openai/status").then(function (r) { return r.json(); }).then(function (s) { $("o-config").textContent = s.configured ? "Credential found. " + s.calls + " API calls declared." : "No OpenAI credential configured: calls answer 503 until OPENAI_API_KEY is set."; }).catch(function () { $("o-config").textContent = "Status unavailable."; }); $("o-run").addEventListener("click", function () { var route = routes[$("o-action").value]; var body = route[1]($("o-prompt").value); if ($("o-model").value) body.model = $("o-model").value; if ($("o-confirm").checked) body.confirm = true; $("o-error").textContent = ""; $("o-out").textContent = "..."; fetch(route[0], { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }).then(function (r) { return r.text().then(function (t) { return [r.status, t]; }); }) .then(function (x) { if (x[0] >= 400) { $("o-error").textContent = x[0] + ": " + x[1].slice(0, 500); $("o-out").textContent = ""; } else { $("o-out").textContent = x[1].slice(0, MAX_OUT); } }).catch(function () { $("o-error").textContent = "Request failed"; $("o-out").textContent = ""; }); }); }());