Files
retoorandClaude Sonnet 5.5 9e550889cc feat(clients): openai admin: organization admin API, usage, costs, audit logs, webhooks
121-call table checked against the OpenAI OpenAPI document, admin-key-only
client, destructive guards, WebhookVerifier (Standard Webhooks HMAC-SHA256),
demo routes under /ai/openai/admin, local and opt-in live tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 07:43:17 +02:00

72 lines
3.2 KiB
JavaScript

// OpenAI administration page: thin client over /ai/openai/admin/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("oaa-out"), err = $("oaa-error");
var base = "/ai/openai/admin";
var calls = [];
function post(path, body) {
err.textContent = "";
out.textContent = "...";
return fetch(base + path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (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); }
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
function json(id) {
try { return JSON.parse($(id).value || "{}"); }
catch (e) { err.textContent = id + " is not valid JSON"; return null; }
}
function current() {
for (var i = 0; i < calls.length; i++) if (calls[i].key === $("oaa-call").value) return calls[i];
return null;
}
fetch(base + "/status").then(function (r) { return r.json(); }).then(function (s) {
$("oaa-config").textContent = s.configured
? "Admin key found."
: (s.admin_key_kind_ok ? "No admin key: calls answer 503 until OPENAI_ADMIN_KEY is set."
: "OPENAI_ADMIN_KEY is not an admin key (sk-admin-...); it is refused.");
});
fetch(base + "/calls").then(function (r) { return r.json(); }).then(function (d) {
calls = d.calls;
$("oaa-call").innerHTML = calls.map(function (c) {
return "<option value=\"" + c.key + "\">" + c.method + " " + c.key + (c.destructive ? " (destructive)" : "") + "</option>";
}).join("");
$("oaa-usage-kind").innerHTML = d.usage_kinds.map(function (k) { return "<option>" + k + "</option>"; }).join("");
});
$("oaa-run").onclick = function () {
var c = current(); if (!c) return;
var query = json("oaa-query"), body = json("oaa-body");
if (!query || !body) return;
var payload = {
call: c.key,
ids: $("oaa-ids").value.split(",").map(function (s) { return s.trim(); }).filter(Boolean),
query: query
};
if (c.method !== "GET" && Object.keys(body).length) payload.body = body;
if (c.destructive) payload.confirm = $("oaa-confirm").checked;
post(c.method === "GET" ? "/read" : (c.destructive ? "/destroy" : "/write"), payload);
};
function usageBody() {
return { kind: $("oaa-usage-kind").value, start_time: Number($("oaa-start").value) || 0, bucket_width: $("oaa-bucket").value };
}
$("oaa-usage").onclick = function () { post("/usage", usageBody()); };
$("oaa-costs").onclick = function () { var b = usageBody(); delete b.kind; b.bucket_width = "1d"; post("/costs", b); };
$("oaa-audit").onclick = function () { post("/audit", { limit: 20 }); };
$("oaa-verify").onclick = function () {
var h = json("oaa-wh-headers"); if (!h) return;
post("/webhook/verify", { secret: $("oaa-secret").value, headers: h, body: $("oaa-wh-body").value });
};
})();