Files
moloapp/example/assets/ai_xai_core.js
T

54 lines
2.0 KiB
JavaScript
Raw Normal View History

// xAI Grok page: thin client over /ai/xai/* (all limits are enforced server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("xai-out"), err = $("xai-error");
function post(path, body) {
err.textContent = "";
out.textContent = "...";
return fetch(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 = (j.text !== undefined && !j.dry_run) ? j.text + "\n\n" + JSON.stringify(j, null, 2)
: JSON.stringify(j, null, 2);
}
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
fetch("/ai/xai/status").then(function (r) { return r.json(); }).then(function (s) {
$("xai-config").textContent = s.configured
? "XAI_API_KEY found. " + s.calls + " calls are covered."
: "No XAI_API_KEY: calls answer 503 until one is configured.";
});
$("xai-chat").onclick = function () {
var tools = $("xai-tools").value;
post("/ai/xai/chat", {
model: $("xai-model").value.trim(),
prompt: $("xai-prompt").value,
effort: $("xai-effort").value,
tools: tools ? tools.split(",") : []
});
};
$("xai-models").onclick = function () { post("/ai/xai/models", { kind: "language" }); };
$("xai-account").onclick = function () { post("/ai/xai/account", { action: "api_key" }); };
$("xai-send").onclick = function () {
var body;
try { body = JSON.parse($("xai-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
if ($("xai-confirm").checked) body.confirm = true;
post("/ai/xai/" + $("xai-route").value, body);
};
})();