|
| 1 | +import { app } from "../../scripts/app.js"; |
| 2 | +import { api } from "../../scripts/api.js"; |
| 3 | + |
| 4 | +/* |
| 5 | + * EA_LMStudio Model Refresh Extension |
| 6 | + * |
| 7 | + * Intercepts the refresh_models toggle to fetch an updated model list from |
| 8 | + * the LM Studio server and update the dropdown widgets in-place. |
| 9 | + * |
| 10 | + * Compatibility: |
| 11 | + * - Legacy LiteGraph frontend: full support via widget.options.values |
| 12 | + * - Nodes 2.0 / Vue frontend: the server-side cache is always updated, |
| 13 | + * so a browser refresh (F5) will pick up new models even if the |
| 14 | + * in-place widget update doesn't propagate in a future Vue renderer. |
| 15 | + */ |
| 16 | + |
| 17 | +app.registerExtension({ |
| 18 | + name: "EA_LMStudio.ModelRefresh", |
| 19 | + |
| 20 | + async nodeCreated(node) { |
| 21 | + if (node.comfyClass !== "EA_LMStudio") return; |
| 22 | + |
| 23 | + const refreshWidget = node.widgets?.find(w => w.name === "refresh_models"); |
| 24 | + if (!refreshWidget) return; |
| 25 | + |
| 26 | + const originalCallback = refreshWidget.callback; |
| 27 | + |
| 28 | + refreshWidget.callback = async function (value) { |
| 29 | + if (!value) { |
| 30 | + if (originalCallback) originalCallback.call(this, value); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const resp = await api.fetchApi("/ea_lmstudio/refresh_models", { |
| 36 | + method: "POST", |
| 37 | + }); |
| 38 | + const data = await resp.json(); |
| 39 | + |
| 40 | + if (data.success && data.models) { |
| 41 | + const choices = ["-- Custom (enter below) --", ...data.models]; |
| 42 | + |
| 43 | + for (const widgetName of ["model_selection", "draft_model_selection"]) { |
| 44 | + const w = node.widgets?.find(ww => ww.name === widgetName); |
| 45 | + if (w && w.options) { |
| 46 | + // Replace values array (breaks shared reference intentionally |
| 47 | + // so other node instances also get the update) |
| 48 | + w.options.values = choices; |
| 49 | + if (!choices.includes(w.value)) { |
| 50 | + w.value = choices[0]; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + console.log( |
| 56 | + `[EA_LMStudio] Refreshed models: ${data.models.length} found` |
| 57 | + ); |
| 58 | + } else { |
| 59 | + console.warn( |
| 60 | + `[EA_LMStudio] Model refresh failed: ${data.message || "unknown error"}` |
| 61 | + ); |
| 62 | + } |
| 63 | + } catch (err) { |
| 64 | + console.error("[EA_LMStudio] Failed to refresh models:", err); |
| 65 | + } |
| 66 | + |
| 67 | + // Toggle back off so it acts like a one-shot button |
| 68 | + refreshWidget.value = false; |
| 69 | + |
| 70 | + if (originalCallback) originalCallback.call(this, false); |
| 71 | + }; |
| 72 | + }, |
| 73 | +}); |
0 commit comments