HIGH: - Fix JS TypeError on empty API response: validate keys/types are arrays before using them; add HTTP status check (resp.ok) - Fix BEGIN IMMEDIATE conflict: set isolation_level=None (autocommit) on SQLite connection so explicit transactions work without implicit ones MEDIUM: - Fix import_json_file non-atomic: wrap entire operation in BEGIN/COMMIT with ROLLBACK on error — no more partial imports - Fix crash on non-dict batch_data items: skip non-dict elements - Fix comma-in-key corruption: store keys/types as JSON arrays in hidden widgets instead of comma-delimited strings (backward-compat fallback) - Fix blocking I/O in API routes: change async def to def so FastAPI auto-threads the synchronous SQLite calls LOW: - Fix missing ?. on app.graph.setDirtyCanvas in refreshDynamicOutputs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
184 lines
8.2 KiB
JavaScript
184 lines
8.2 KiB
JavaScript
import { app } from "../../scripts/app.js";
|
|
import { api } from "../../scripts/api.js";
|
|
|
|
app.registerExtension({
|
|
name: "json.manager.project.dynamic",
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
|
if (nodeData.name !== "ProjectLoaderDynamic") return;
|
|
|
|
const origOnNodeCreated = nodeType.prototype.onNodeCreated;
|
|
nodeType.prototype.onNodeCreated = function () {
|
|
origOnNodeCreated?.apply(this, arguments);
|
|
|
|
// Hide internal widgets (managed by JS)
|
|
for (const name of ["output_keys", "output_types"]) {
|
|
const w = this.widgets?.find(w => w.name === name);
|
|
if (w) { w.type = "hidden"; w.computeSize = () => [0, -4]; }
|
|
}
|
|
|
|
// Do NOT remove default outputs synchronously here.
|
|
// During graph loading, ComfyUI creates all nodes (firing onNodeCreated)
|
|
// before configuring them. Other nodes (e.g. Kijai Set/Get) may resolve
|
|
// links to our outputs during their configure step. If we remove outputs
|
|
// here, those nodes find no output slot and error out.
|
|
//
|
|
// Instead, defer cleanup: for loaded workflows onConfigure sets _configured
|
|
// before this runs; for new nodes the defaults are cleaned up.
|
|
this._configured = false;
|
|
|
|
// Add Refresh button
|
|
this.addWidget("button", "Refresh Outputs", null, () => {
|
|
this.refreshDynamicOutputs();
|
|
});
|
|
|
|
queueMicrotask(() => {
|
|
if (!this._configured) {
|
|
// New node (not loading) — remove the 32 Python default outputs
|
|
while (this.outputs.length > 0) {
|
|
this.removeOutput(0);
|
|
}
|
|
this.setSize(this.computeSize());
|
|
app.graph?.setDirtyCanvas(true, true);
|
|
}
|
|
});
|
|
};
|
|
|
|
nodeType.prototype.refreshDynamicOutputs = async function () {
|
|
const urlWidget = this.widgets?.find(w => w.name === "manager_url");
|
|
const projectWidget = this.widgets?.find(w => w.name === "project_name");
|
|
const fileWidget = this.widgets?.find(w => w.name === "file_name");
|
|
const seqWidget = this.widgets?.find(w => w.name === "sequence_number");
|
|
|
|
if (!urlWidget?.value || !projectWidget?.value || !fileWidget?.value) return;
|
|
|
|
try {
|
|
const resp = await api.fetchApi(
|
|
`/json_manager/get_project_keys?url=${encodeURIComponent(urlWidget.value)}&project=${encodeURIComponent(projectWidget.value)}&file=${encodeURIComponent(fileWidget.value)}&seq=${seqWidget?.value || 1}`
|
|
);
|
|
|
|
if (!resp.ok) {
|
|
console.warn("[ProjectLoaderDynamic] HTTP error", resp.status, "— keeping existing outputs");
|
|
return;
|
|
}
|
|
|
|
const data = await resp.json();
|
|
const keys = data.keys;
|
|
const types = data.types;
|
|
|
|
// If the API returned an error or missing data, keep existing outputs and links intact
|
|
if (data.error || !Array.isArray(keys) || !Array.isArray(types)) {
|
|
console.warn("[ProjectLoaderDynamic] API error or missing data, keeping existing outputs:", data.error || "no keys/types");
|
|
return;
|
|
}
|
|
|
|
// Store keys and types in hidden widgets for persistence (JSON-encoded)
|
|
const okWidget = this.widgets?.find(w => w.name === "output_keys");
|
|
if (okWidget) okWidget.value = JSON.stringify(keys);
|
|
const otWidget = this.widgets?.find(w => w.name === "output_types");
|
|
if (otWidget) otWidget.value = JSON.stringify(types);
|
|
|
|
// Build a map of current output names to slot indices
|
|
const oldSlots = {};
|
|
for (let i = 0; i < this.outputs.length; i++) {
|
|
oldSlots[this.outputs[i].name] = i;
|
|
}
|
|
|
|
// Build new outputs, reusing existing slots to preserve links
|
|
const newOutputs = [];
|
|
for (let k = 0; k < keys.length; k++) {
|
|
const key = keys[k];
|
|
const type = types[k] || "*";
|
|
if (key in oldSlots) {
|
|
const slot = this.outputs[oldSlots[key]];
|
|
slot.type = type;
|
|
newOutputs.push(slot);
|
|
delete oldSlots[key];
|
|
} else {
|
|
newOutputs.push({ name: key, type: type, links: null });
|
|
}
|
|
}
|
|
|
|
// Disconnect links on slots that are being removed
|
|
for (const name in oldSlots) {
|
|
const idx = oldSlots[name];
|
|
if (this.outputs[idx]?.links?.length) {
|
|
for (const linkId of [...this.outputs[idx].links]) {
|
|
this.graph?.removeLink(linkId);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reassign the outputs array and fix link slot indices
|
|
this.outputs = newOutputs;
|
|
if (this.graph) {
|
|
for (let i = 0; i < this.outputs.length; i++) {
|
|
const links = this.outputs[i].links;
|
|
if (!links) continue;
|
|
for (const linkId of links) {
|
|
const link = this.graph.links[linkId];
|
|
if (link) link.origin_slot = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
this.setSize(this.computeSize());
|
|
app.graph?.setDirtyCanvas(true, true);
|
|
} catch (e) {
|
|
console.error("[ProjectLoaderDynamic] Refresh failed:", e);
|
|
}
|
|
};
|
|
|
|
// Restore state on workflow load
|
|
const origOnConfigure = nodeType.prototype.onConfigure;
|
|
nodeType.prototype.onConfigure = function (info) {
|
|
origOnConfigure?.apply(this, arguments);
|
|
this._configured = true;
|
|
|
|
// Hide internal widgets
|
|
for (const name of ["output_keys", "output_types"]) {
|
|
const w = this.widgets?.find(w => w.name === name);
|
|
if (w) { w.type = "hidden"; w.computeSize = () => [0, -4]; }
|
|
}
|
|
|
|
const okWidget = this.widgets?.find(w => w.name === "output_keys");
|
|
const otWidget = this.widgets?.find(w => w.name === "output_types");
|
|
|
|
// Parse keys/types — try JSON array first, fall back to comma-split
|
|
let keys = [];
|
|
if (okWidget?.value) {
|
|
try { keys = JSON.parse(okWidget.value); } catch (_) {
|
|
keys = okWidget.value.split(",").map(k => k.trim()).filter(Boolean);
|
|
}
|
|
}
|
|
let types = [];
|
|
if (otWidget?.value) {
|
|
try { types = JSON.parse(otWidget.value); } catch (_) {
|
|
types = otWidget.value.split(",");
|
|
}
|
|
}
|
|
|
|
if (keys.length > 0) {
|
|
// On load, LiteGraph already restored serialized outputs with links.
|
|
// Rename and set types to match stored state (preserves links).
|
|
for (let i = 0; i < this.outputs.length && i < keys.length; i++) {
|
|
this.outputs[i].name = keys[i].trim();
|
|
if (types[i]) this.outputs[i].type = types[i];
|
|
}
|
|
|
|
// Remove any extra outputs beyond the key count
|
|
while (this.outputs.length > keys.length) {
|
|
this.removeOutput(this.outputs.length - 1);
|
|
}
|
|
} else if (this.outputs.length > 0) {
|
|
// Widget values empty but serialized outputs exist — sync widgets
|
|
// from the outputs LiteGraph already restored (fallback).
|
|
if (okWidget) okWidget.value = JSON.stringify(this.outputs.map(o => o.name));
|
|
if (otWidget) otWidget.value = JSON.stringify(this.outputs.map(o => o.type));
|
|
}
|
|
|
|
this.setSize(this.computeSize());
|
|
};
|
|
},
|
|
});
|