From 8b2fda591d4b4d9559adb573a58b8f437f0b978d Mon Sep 17 00:00:00 2001 From: Luan Citta Date: Fri, 17 Jan 2025 10:44:49 +0000 Subject: [PATCH] Added extra display and save node --- __init__.py | 2 ++ nodes/save_text_node.py | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 nodes/save_text_node.py diff --git a/__init__.py b/__init__.py index 0902879..7264109 100644 --- a/__init__.py +++ b/__init__.py @@ -7,6 +7,7 @@ from .nodes.task_node import TaskNode from .nodes.crew_node import CrewNode from .nodes.list_task_node import TaskList from .nodes.list_agent_node import AgentList +from .nodes.save_text_node import DisplayText NODE_CLASS_MAPPINGS = { "📎CrewAI Agent": AgentNode, @@ -18,6 +19,7 @@ NODE_CLASS_MAPPINGS = { "📎CrewAI LLM Hugging Face": LlmHuggingFace, "📎CrewAI LLM Chat GPT": LlmChatGpt, "📎CrewAI LLM Ollama": LlmOllama, + "📎CrewAI Text": DisplayText, } WEB_DIRECTORY = "./js" diff --git a/nodes/save_text_node.py b/nodes/save_text_node.py new file mode 100644 index 0000000..3fbde6a --- /dev/null +++ b/nodes/save_text_node.py @@ -0,0 +1,56 @@ +import os +import json +from datetime import datetime + + +class DisplayText: + def __init__(self): + self.output_dir = "outputs/text_outputs" + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "text": ("STRING", {"multiline": True}), + "filename_prefix": ("STRING", {"default": "output"}), + "save_metadata": (["disabled", "enabled"], {"default": "enabled"}), + } + } + + RETURN_TYPES = ("STRING",) # Indicates the node outputs text to the UI + FUNCTION = "save_and_display_text" + OUTPUT_NODE = True + CATEGORY = "Custom Nodes" + + def save_and_display_text(self, text, filename_prefix, save_metadata): + # Ensure output directory exists + os.makedirs(self.output_dir, exist_ok=True) + + # Generate filename with timestamp + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + filename = f"{filename_prefix}_{timestamp}.txt" + + # Save text to file + filepath = os.path.join(self.output_dir, filename) + with open(filepath, "w", encoding="utf-8") as f: + f.write(text) + + # Optionally save metadata + if save_metadata == "enabled": + metadata_path = os.path.join(self.output_dir, f"{filename}_metadata.json") + metadata = { + "timestamp": timestamp, + "filename_prefix": filename_prefix, + "text_preview": text[:50] + "..." if len(text) > 50 else text, + } + with open(metadata_path, "w", encoding="utf-8") as meta_file: + json.dump(metadata, meta_file, indent=4) + + # Return text for display in the UI + return (f"Saved Text:\n{text}",) # Display the full text or a preview + + +# Register the node in ComfyUI +NODE_CLASS_MAPPINGS = { + "SaveAndDisplayTextNode": DisplayText, +}