From 6f349c948df36aefc0dbd6db5facbd3a13a5ec30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=91=E6=9E=97=5FCyril=20Pilgrim?= Date: Mon, 9 Feb 2026 23:02:50 +0800 Subject: [PATCH] fix(json-builder): remove base_json, make keys manual text inputs Changes: - Removed unnecessary base_json parameter - Keys are now editable text widgets (not forceInput) - Values remain as input slots for upstream connections - Frontend JS updated to add text widgets for keys --- nodes/json_builder.py | 22 +++++---------- web/js/json_builder.js | 64 +++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/nodes/json_builder.py b/nodes/json_builder.py index 1026477..977078c 100644 --- a/nodes/json_builder.py +++ b/nodes/json_builder.py @@ -15,12 +15,11 @@ class JSONBuilder: return { "required": { "inputcount": ("INT", {"default": 2, "min": 1, "max": 100, "step": 1}), + "key_1": ("STRING", {"default": ""}), + "key_2": ("STRING", {"default": ""}), }, "optional": { - "base_json": ("STRING", {"forceInput": True}), - "key_1": ("STRING", {"default": ""}), "value_1": ("STRING", {"default": "", "forceInput": True}), - "key_2": ("STRING", {"default": ""}), "value_2": ("STRING", {"default": "", "forceInput": True}), } } @@ -31,21 +30,14 @@ class JSONBuilder: CATEGORY = "🚦ComfyUI_LLMs_Toolkit/JSON" DESCRIPTION = """ Build JSON from key-value pairs. -- Adjust **inputcount** and click Update to add more inputs. -- Connect **base_json** to chain multiple builders. +- Keys are manual text inputs +- Values are connected from upstream nodes +- Adjust **inputcount** and click Update inputs to add more pairs """ - def build(self, inputcount: int, base_json: str = None, **kwargs) -> Tuple[str]: + def build(self, inputcount: int, **kwargs) -> Tuple[str]: """Build JSON from key-value pairs.""" - - # Start with base JSON if provided - if base_json: - try: - result = json.loads(base_json) - except json.JSONDecodeError: - result = {} - else: - result = {} + result = {} # Add key-value pairs for i in range(1, inputcount + 1): diff --git a/web/js/json_builder.js b/web/js/json_builder.js index 3b6488e..c28e9a6 100644 --- a/web/js/json_builder.js +++ b/web/js/json_builder.js @@ -6,45 +6,57 @@ app.registerExtension({ if (nodeData.name === "JSONBuilder") { nodeType.prototype.onNodeCreated = function () { this.addWidget("button", "Update inputs", null, () => { - if (!this.inputs) { - this.inputs = []; - } - const inputcountWidget = this.widgets.find(w => w.name === "inputcount"); if (!inputcountWidget) return; - + const target_count = inputcountWidget.value; - - // Count current key/value input pairs - const current_keys = this.inputs.filter(input => - input.name && input.name.startsWith("key_") + + // Count current key widgets (they are in widgets, not inputs) + const current_keys = this.widgets.filter(w => + w.name && w.name.startsWith("key_") ).length; - + + // Count current value inputs + const current_values = this.inputs ? this.inputs.filter(input => + input.name && input.name.startsWith("value_") + ).length : 0; + if (target_count === current_keys) return; // already correct - + if (target_count < current_keys) { - // Remove excess inputs (remove from end, pairs at a time) - const pairs_to_remove = current_keys - target_count; - for (let i = 0; i < pairs_to_remove; i++) { - // Find and remove value_N first, then key_N - const idx_to_remove = current_keys - i; - const value_input_idx = this.inputs.findIndex( - input => input.name === `value_${idx_to_remove}` + // Remove excess widgets and inputs + const to_remove = current_keys - target_count; + for (let i = 0; i < to_remove; i++) { + const idx = current_keys - i; + + // Remove value input + if (this.inputs) { + const value_idx = this.inputs.findIndex( + input => input.name === `value_${idx}` + ); + if (value_idx !== -1) this.removeInput(value_idx); + } + + // Remove key widget + const key_widget_idx = this.widgets.findIndex( + w => w.name === `key_${idx}` ); - if (value_input_idx !== -1) this.removeInput(value_input_idx); - - const key_input_idx = this.inputs.findIndex( - input => input.name === `key_${idx_to_remove}` - ); - if (key_input_idx !== -1) this.removeInput(key_input_idx); + if (key_widget_idx !== -1) { + this.widgets.splice(key_widget_idx, 1); + } } } else { - // Add new inputs + // Add new widgets and inputs for (let i = current_keys + 1; i <= target_count; i++) { - this.addInput(`key_${i}`, "STRING"); + // Add key as editable text widget + this.addWidget("text", `key_${i}`, "", () => { }, {}); + // Add value as input slot this.addInput(`value_${i}`, "STRING"); } } + + // Resize node to fit new widgets + this.setSize(this.computeSize()); }); }; }