import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
app.registerExtension({
name: "ImpactFrames.ListModels",
async setup() {
// Register node before it's created
app.ui.settings.addSetting({
id: "ImpactFrames.ListModels.AutoRefresh",
name: "Auto-refresh model list",
type: "boolean",
defaultValue: false,
onChange: () => {}
});
},
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeType.comfyClass === "ListModelsNode") {
// Store original onNodeCreated if it exists
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function() {
// Call original onNodeCreated if it exists
if (onNodeCreated) {
onNodeCreated.apply(this, arguments);
}
const updateModelList = () => {
// Get current values
const llmProvider = this.widgets.find(w => w.name === "llm_provider").value;
const baseIp = this.widgets.find(w => w.name === "base_ip").value;
const port = this.widgets.find(w => w.name === "port").value;
const apiKey = this.widgets.find(w => w.name === "external_api_key").value;
// Set refresh widget to trigger update
const refreshWidget = this.widgets.find(w => w.name === "refresh");
if (refreshWidget) {
refreshWidget.value = true;
app.graph.runStep(); // Trigger node execution
refreshWidget.value = false; // Reset for next use
}
};
// Add the refresh button to widgets
const refreshButton = document.createElement("button");
refreshButton.textContent = "🔄 Refresh Models";
refreshButton.className = "if-lm-refresh-button";
refreshButton.onclick = updateModelList;
// Add button styles
const style = document.createElement("style");
style.textContent = `
.if-lm-refresh-button {
background-color: #2a2a2a;
border: 1px solid #3c3c3c;
color: #dadada;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
margin: 5px;
transition: background-color 0.3s;
}
.if-lm-refresh-button:hover {
background-color: #3c3c3c;
}
`;
document.head.appendChild(style);
// Add button to node
this.onDrawBackground = function(ctx) {
if (this.flags.collapsed) return;
// Draw button only if node is not collapsed
if (!this.buttons) {
this.buttons = [];
this.buttons.push(refreshButton);
// Add the button to the node's HTML element
this.htmlElement = document.createElement("div");
this.htmlElement.style.position = "relative";
this.htmlElement.appendChild(refreshButton);
document.body.appendChild(this.htmlElement);
}
// Position the button at the bottom of the node
if (this.htmlElement) {
const nodeRect = this.getBoundingClientRect();
this.htmlElement.style.left = `${nodeRect.x + 10}px`;
this.htmlElement.style.top = `${nodeRect.y + nodeRect.height - 30}px`;
}
};
// Clean up when node is removed
this.onRemoved = function() {
if (this.htmlElement && this.htmlElement.parentElement) {
this.htmlElement.parentElement.removeChild(this.htmlElement);
}
};
// Auto-refresh on provider change
const providerWidget = this.widgets.find(w => w.name === "llm_provider");
if (providerWidget) {
const originalCallback = providerWidget.callback;
providerWidget.callback = function() {
if (originalCallback) {
originalCallback.apply(this, arguments);
}
// Check auto-refresh setting
if (app.ui.settings.getSettingValue("ImpactFrames.ListModels.AutoRefresh", false)) {
updateModelList();
}
};
}
};
}
}
});