(() => { const root = document.querySelector('[data-lumi-page="lumi-transcription"]'); if (!root) return; const button = root.querySelector("[data-create-pairing]"); const status = root.querySelector("[data-status]"); button?.addEventListener("click", async () => { button.disabled = true; status.textContent = "Creating a one-time pairing package…"; try { const response = await fetch("/plugins/lumi_transcription/api/companion/download", { method: "POST", headers: { Accept: "application/json" } }); if (!response.ok) throw new Error((await response.json()).error || "Pairing package could not be created."); const blob = await response.blob(); const disposition = response.headers.get("content-disposition") || ""; const filename = /filename="?([^";]+)"?/i.exec(disposition)?.[1] || "Lumi-Companion-paired.zip"; const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = filename; link.click(); setTimeout(() => URL.revokeObjectURL(link.href), 1000); status.textContent = "Companion package created. Extract and start it within 15 minutes; the included pairing key works once."; } catch (error) { status.textContent = error.message; } finally { button.disabled = false; } }); root.addEventListener("click", async (event) => { const download = event.target.closest("[data-download-model]"); const load = event.target.closest("[data-load-model]"); if (!download && !load) return; const modelId = download?.dataset.downloadModel || load?.dataset.loadModel; if (download && !window.confirm(`Download ${download.dataset.modelLabel} (${download.dataset.modelSize} MiB) to the Lumi host?`)) return; const action = download ? "download" : "load"; const target = download || load; target.disabled = true; status.textContent = download ? "Downloading and verifying the model…" : "Loading the model into whisper.cpp…"; try { const response = await fetch(`/plugins/lumi_transcription/api/models/${encodeURIComponent(modelId)}/${action}`, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json" }, body: download ? JSON.stringify({ confirmed: true }) : "{}" }); const result = await response.json(); if (!response.ok) throw new Error(result.error || `Model ${action} failed.`); status.textContent = download ? "Model verified. You can load it now." : "Model loaded. Run the host benchmark before live use."; setTimeout(() => window.location.reload(), 700); } catch (error) { status.textContent = error.message; target.disabled = false; } }); })();