Lumi/plugins/lumi_ai/public/settings.js
2026-06-11 06:35:43 +02:00

72 lines
3.1 KiB
JavaScript

(() => {
const actions = document.querySelector("[data-ai-runtime-actions]");
const state = document.querySelector("[data-runtime-state]");
const downloadStatus = document.querySelector("[data-download-status]");
const testForm = document.querySelector("[data-ai-test-form]");
const testOutput = document.querySelector("[data-ai-test-output]");
if (actions) {
actions.addEventListener("click", async (event) => {
const button = event.target.closest("[data-runtime-action]");
if (!button) return;
button.disabled = true;
try {
const response = await fetch(`/plugins/lumi_ai/runtime/${button.dataset.runtimeAction}`, { method: "POST" });
const data = await response.json();
if (!response.ok) throw new Error(data.error || "Runtime action failed.");
if (data.state) state.textContent = data.state;
if (["self-test", "verify-runtime", "verify-model"].includes(button.dataset.runtimeAction)) {
const labels = { "self-test": "Runtime self-test passed.", "verify-runtime": "Runtime installation verified.", "verify-model": "Model verification passed." };
window.alert(labels[button.dataset.runtimeAction]);
}
} catch (error) {
window.alert(error.message);
} finally {
button.disabled = false;
}
});
}
const pollDownloads = async () => {
if (!downloadStatus) return;
try {
const response = await fetch("/plugins/lumi_ai/api/downloads");
if (!response.ok) return;
const jobs = Object.values(await response.json());
const active = jobs.filter((job) => !["complete", "error"].includes(job.state));
if (!jobs.length) return;
downloadStatus.hidden = false;
downloadStatus.textContent = jobs.map((job) => {
const percent = job.total ? Math.floor(job.downloaded / job.total * 100) : 0;
return `${job.id}: ${job.state}${job.total ? ` ${percent}%` : ""}${job.error ? ` - ${job.error}` : ""}`;
}).join(" | ");
if (active.length) window.setTimeout(pollDownloads, 1000);
} catch {}
};
if (testForm && testOutput) {
testForm.addEventListener("submit", async (event) => {
event.preventDefault();
const form = new FormData(testForm);
testOutput.hidden = false;
testOutput.textContent = "Running...";
try {
const response = await fetch("/plugins/lumi_ai/assistant/test", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
role: form.get("role"),
message: form.get("message"),
show_raw_prompt: form.get("show_raw_prompt") === "on",
show_raw_output: form.get("show_raw_output") === "on"
})
});
const data = await response.json();
if (!response.ok) throw new Error(data.error || "Test failed.");
if (form.get("show_raw_output") !== "on") delete data.raw_response;
testOutput.textContent = JSON.stringify(data, null, 2);
} catch (error) {
testOutput.textContent = error.message;
}
});
}
pollDownloads();
})();