import { app } from "../../../scripts/app.js"; import { api } from "../../../scripts/api.js"; const RELEASE_URL = "https://git.o1key.com/publisher/comfyui_o1key"; let registered = false; app.registerExtension({ name: "o1key.updateButton", setup() { if (registered || app.extensionManager.getSidebarTabs?.().some((tab) => tab.id === "o1key-update")) return; registered = true; app.extensionManager.registerSidebarTab({ id: "o1key-update", title: "更新", tooltip: "更新 O1Key 节点包", icon: "pi pi-download", type: "custom", render(container) { const root = document.createElement("div"); root.id = "o1key-update-panel"; root.style.padding = "16px"; const title = document.createElement("h3"); title.textContent = "更新 O1Key 节点包"; const description = document.createElement("p"); description.textContent = "从 O1Key 发布仓库的 main 分支获取更新。只允许安全快进,不会清理或覆盖本地文件。"; const source = document.createElement("p"); source.textContent = `发布地址:${RELEASE_URL}`; const button = document.createElement("button"); button.type = "button"; button.textContent = "检查并更新"; button.className = "p-button"; const status = document.createElement("p"); status.setAttribute("role", "status"); status.setAttribute("aria-live", "polite"); const suggestion = document.createElement("p"); function show(message, nextStep = "", error = false) { status.textContent = message; status.style.color = error ? "var(--error-color, #ef7777)" : ""; suggestion.textContent = nextStep; } button.addEventListener("click", async () => { if (button.disabled || !window.confirm(`从 ${RELEASE_URL} 的 main 分支检查并更新?本地修改不会被覆盖。`)) return; button.disabled = true; show("正在检查本地文件并连接发布仓库…", "请保持 ComfyUI 运行,等待结果。"); try { const response = await api.fetchApi("/o1key/update", { method: "POST", headers: { "X-O1Key-Update": "1" }, }); if (response.status === 404) { show("更新接口尚未加载。", "重启 ComfyUI 并刷新浏览器后再试。", true); return; } let result; try { result = await response.json(); } catch { show("无法读取更新结果。", "确认 ComfyUI 正常运行,刷新页面后重试。", true); return; } if (!response.ok) { show(result.error || "更新未完成。", result.suggestion || "检查 ComfyUI 日志后重试。", true); return; } if (typeof result.updated !== "boolean" || typeof result.version !== "string") { show("更新接口返回了无效结果。", "重启 ComfyUI 并刷新浏览器后重试。", true); return; } if (result.updated) { show( `已更新到 ${result.version}。`, result.requirements_changed ? "依赖列表已变化:先在 ComfyUI 使用的 Python 环境中安装 requirements.txt,再重启 ComfyUI。" : "请重启 ComfyUI,并刷新浏览器以加载新版本。", ); } else { show(`已是最新版本(${result.version})。`, "无需重启。"); } } catch { show("无法连接本地 ComfyUI 更新接口。", "确认 ComfyUI 正在运行,刷新页面后重试。", true); } finally { button.disabled = false; } }); root.append(title, description, source, button, status, suggestion); container.replaceChildren(root); }, }); }, });