diff --git a/json_prompt_modifier.py b/json_prompt_modifier.py index 7f9369b..51f070e 100644 --- a/json_prompt_modifier.py +++ b/json_prompt_modifier.py @@ -1,4 +1,5 @@ import json +from server import PromptServer class JSONPromptModifier: def __init__(self): @@ -8,30 +9,53 @@ class JSONPromptModifier: def INPUT_TYPES(s): return { "required": { - # 核心文本框:存储和编辑 JSON 内容 "json_text": ("STRING", {"multiline": True, "default": "{}"}), }, + "optional": { + "vqa_input": ("STRING", {"forceInput": True}), + }, + "hidden": {"unique_id": "UNIQUE_ID"}, } RETURN_TYPES = ("STRING", "JSON") RETURN_NAMES = ("text", "json") FUNCTION = "process_json" CATEGORY = "JSON工具" + OUTPUT_NODE = True - def process_json(self, json_text): - # 尝试解析 JSON,如果格式错误则返回空字典,防止工作流崩溃 + def process_json(self, json_text, vqa_input=None, unique_id=None): + display_text = json_text + + # --- 修复逻辑:处理可能的列表输入 --- + if vqa_input is not None: + # 如果输入是列表 ['prompt text'],则取第一项 + if isinstance(vqa_input, list): + if len(vqa_input) > 0: + vqa_input = str(vqa_input[0]) + else: + vqa_input = "" + + # 确保是字符串后再进行 strip + vqa_input = str(vqa_input).strip() + + if vqa_input != "": + # 发送给前端显示 + PromptServer.instance.send_sync("json_modifier_update_text", { + "node_id": unique_id, + "text": vqa_input + }) + # 如果此时对话框是空的或是默认值,后端直接采用输入值 + if json_text == "{}" or json_text.strip() == "": + display_text = vqa_input + + # 尝试解析 JSON 输出 try: - json_object = json.loads(json_text) - except Exception as e: - print(f"JSON解析警告: {e}") + # 这里的解析对象使用 display_text,确保后端输出与前端显示同步 + json_object = json.loads(display_text) + except Exception: json_object = {} - return (json_text, json_object) + return (display_text, json_object) -NODE_CLASS_MAPPINGS = { - "JSON-Prompt-Modifier": JSONPromptModifier -} - -NODE_DISPLAY_NAME_MAPPINGS = { - "JSON-Prompt-Modifier": "JSON Prompt Modifier 🛠️" -} +NODE_CLASS_MAPPINGS = {"JSON-Prompt-Modifier": JSONPromptModifier} +NODE_DISPLAY_NAME_MAPPINGS = {"JSON-Prompt-Modifier": "JSON Prompt Modifier 🛠️"}