Stability, BFL, Runway, Luma, Replicate, fal, Ideogram, Recraft, Leonardo, MiniMax, Kling, Higgsfield: call tables checked against vendor specs/SDKs, ImageGenerator/VideoGenerator seam with capped polling, /ai/media demo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
103 lines
4.3 KiB
JavaScript
103 lines
4.3 KiB
JavaScript
// Media page: thin client over /ai/media/* (all limits are enforced server side).
|
|
(function () {
|
|
"use strict";
|
|
var $ = function (id) { return document.getElementById(id); };
|
|
var out = $("md-out"), err = $("md-error");
|
|
var providers = [];
|
|
var last = null; // last ticket returned by image / video
|
|
|
|
var routes = {
|
|
"image": function (p) { return { provider: p, prompt: "A lighthouse at dusk, oil painting", model: "", aspect_ratio: "", count: 1 }; },
|
|
"video": function (p) { return { provider: p, prompt: "Waves rolling into a harbour", model: "", duration_seconds: 5 }; },
|
|
"poll": function () { return { ticket: last || {} }; },
|
|
"wait": function () { return { ticket: last || {}, max_attempts: 10, max_wait_seconds: 40 }; },
|
|
"job": function () { return { ticket: last || {} }; },
|
|
"account": function (p) { return { provider: p }; },
|
|
"call": function (p) { return { provider: p, key: "", path: {}, query: {}, body: {} }; },
|
|
"fetch": function () { return { url: "https://example.com/result.png" }; },
|
|
"webhook": function () { return { secret: "whsec_...", webhook_id: "msg_...", timestamp: "0", signature: "v1,...", payload: "{}" }; }
|
|
};
|
|
var labels = {
|
|
"image": "image: submit a generation",
|
|
"video": "video: submit a generation",
|
|
"poll": "poll: one status check",
|
|
"wait": "wait: poll under caps",
|
|
"job": "job: cancel (DELETE)",
|
|
"account": "account: free credential check",
|
|
"call": "call: any table row",
|
|
"fetch": "fetch: download a result URL",
|
|
"webhook": "webhook: verify a Replicate signature"
|
|
};
|
|
|
|
var route = $("md-route"), prov = $("md-provider");
|
|
Object.keys(routes).forEach(function (k) {
|
|
var o = document.createElement("option");
|
|
o.value = k; o.textContent = labels[k];
|
|
route.appendChild(o);
|
|
});
|
|
|
|
function fill() {
|
|
var p = prov.value || "fal";
|
|
$("md-body").value = JSON.stringify(routes[route.value](p), null, 1);
|
|
}
|
|
route.onchange = fill;
|
|
prov.onchange = fill;
|
|
|
|
function send(confirm) {
|
|
var body;
|
|
try { body = JSON.parse($("md-body").value || "{}"); }
|
|
catch (e) { err.textContent = "Body is not valid JSON"; return; }
|
|
if (confirm) body.confirm = true;
|
|
err.textContent = "";
|
|
out.textContent = "...";
|
|
var key = route.value;
|
|
var url = "/ai/media/" + (key === "job" ? "job" : key);
|
|
fetch(url, {
|
|
method: key === "job" ? "DELETE" : "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 = ""; return; }
|
|
if (j.ticket) last = j.ticket;
|
|
out.textContent = JSON.stringify(j, null, 2);
|
|
var imgs = (j.ticket && j.ticket.assets) || j.assets || [];
|
|
imgs.forEach(function (a) {
|
|
if (a.data_base64 && /^image\//.test(a.mime || "")) {
|
|
var im = document.createElement("img");
|
|
im.src = "data:" + a.mime + ";base64," + a.data_base64;
|
|
im.style.maxWidth = "100%";
|
|
out.appendChild(document.createElement("br"));
|
|
out.appendChild(im);
|
|
}
|
|
});
|
|
});
|
|
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
|
|
}
|
|
$("md-send").onclick = function () { send(false); };
|
|
$("md-confirm").onclick = function () { send(true); };
|
|
|
|
fetch("/ai/media/status").then(function (r) { return r.json(); }).then(function (s) {
|
|
providers = s.providers || [];
|
|
providers.forEach(function (p) {
|
|
var o = document.createElement("option");
|
|
o.value = p.id;
|
|
o.textContent = p.name + (p.configured ? "" : " (no key)");
|
|
prov.appendChild(o);
|
|
});
|
|
var ready = providers.filter(function (p) { return p.configured; }).length;
|
|
$("md-config").textContent = ready
|
|
? ready + " of " + providers.length + " providers have a credential. Paid calls need confirm."
|
|
: "No provider credential found: calls answer 503 until one is configured; dry runs still work.";
|
|
var ul = $("md-unsupported");
|
|
(s.unsupported || []).forEach(function (u) {
|
|
var li = document.createElement("li");
|
|
li.textContent = u.name + ": " + u.reason;
|
|
ul.appendChild(li);
|
|
});
|
|
fill();
|
|
});
|
|
})();
|