80 lines
3.4 KiB
JavaScript
80 lines
3.4 KiB
JavaScript
// OpenAI platform page: thin client over /ai/openai/platform/* (limits server side).
|
|||
|
|
(function () {
|
||
|
|
"use strict";
|
||
|
|
var $ = function (id) { return document.getElementById(id); };
|
||
|
|
var out = $("oap-out"), err = $("oap-error");
|
||
|
|
var base = "/ai/openai/platform";
|
||
|
|
var calls = [];
|
||
|
|
|
||
|
|
function show(res) {
|
||
|
|
return res.text().then(function (t) {
|
||
|
|
var j;
|
||
|
|
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
|
||
|
|
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
|
||
|
|
else { out.textContent = JSON.stringify(j, null, 2); }
|
||
|
|
return j;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
function post(path, body) {
|
||
|
|
err.textContent = "";
|
||
|
|
out.textContent = "...";
|
||
|
|
return fetch(base + path, {
|
||
|
|
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body)
|
||
|
|
}).then(show).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
|
||
|
|
}
|
||
|
|
function json(id) {
|
||
|
|
try { return JSON.parse($(id).value || "{}"); }
|
||
|
|
catch (e) { err.textContent = "Not valid JSON: " + id.replace("oap-", ""); return null; }
|
||
|
|
}
|
||
|
|
|
||
|
|
fetch(base + "/status").then(function (r) { return r.json(); }).then(function (s) {
|
||
|
|
$("oap-config").textContent = s.configured
|
||
|
|
? "OpenAI key found (" + s.credential_env + "). " + s.calls + " calls, " + s.events + " events."
|
||
|
|
: "No OpenAI key: calls answer 503 until " + s.credential_env + " is set. Dry runs and local helpers work.";
|
||
|
|
});
|
||
|
|
|
||
|
|
function fillOps() {
|
||
|
|
var area = $("oap-area").value, sel = $("oap-op");
|
||
|
|
sel.innerHTML = "";
|
||
|
|
calls.filter(function (c) { return c.area === area; }).forEach(function (c) {
|
||
|
|
var o = document.createElement("option");
|
||
|
|
o.value = c.operation;
|
||
|
|
o.textContent = c.http + " " + c.path;
|
||
|
|
sel.appendChild(o);
|
||
|
|
});
|
||
|
|
info();
|
||
|
|
}
|
||
|
|
function info() {
|
||
|
|
var c = calls.filter(function (x) { return x.operation === $("oap-op").value; })[0];
|
||
|
|
if (!c) { $("oap-info").textContent = ""; return; }
|
||
|
|
$("oap-info").textContent = "ids: " + (c.ids.join(", ") || "none") + "; body: " +
|
||
|
|
(c.body_fields.join(", ") || "none") + "; required: " + (c.required.join(", ") || "none") +
|
||
|
|
"; query: " + (c.query.join(", ") || "none") + "; " + c.flags.join(", ");
|
||
|
|
}
|
||
|
|
fetch(base + "/calls").then(function (r) { return r.json(); }).then(function (d) {
|
||
|
|
calls = d.calls;
|
||
|
|
var areas = [];
|
||
|
|
calls.forEach(function (c) { if (areas.indexOf(c.area) < 0) areas.push(c.area); });
|
||
|
|
areas.forEach(function (a) {
|
||
|
|
var o = document.createElement("option");
|
||
|
|
o.value = a; o.textContent = a;
|
||
|
|
$("oap-area").appendChild(o);
|
||
|
|
});
|
||
|
|
fillOps();
|
||
|
|
});
|
||
|
|
$("oap-area").onchange = fillOps;
|
||
|
|
$("oap-op").onchange = info;
|
||
|
|
|
||
|
|
$("oap-send").onclick = function () {
|
||
|
|
var ids = json("oap-ids"), query = json("oap-query"), body = json("oap-body");
|
||
|
|
if (!ids || !query || !body) return;
|
||
|
|
post("/call", { operation: $("oap-op").value, ids: ids, query: query, body: body, confirm: $("oap-confirm").checked });
|
||
|
|
};
|
||
|
|
$("oap-ebuild").onclick = function () {
|
||
|
|
var f = json("oap-efields");
|
||
|
|
if (f) post("/realtime/event", { profile: $("oap-profile").value, type: $("oap-etype").value.trim(), fields: f });
|
||
|
|
};
|
||
|
|
$("oap-ecatalog").onclick = function () { post("/realtime/catalog", { profile: $("oap-profile").value }); };
|
||
|
|
$("oap-eparse").onclick = function () { post("/realtime/parse", { profile: $("oap-profile").value, text: $("oap-etext").value }); };
|
||
|
|
})();
|