-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathprompt_search_list.py
More file actions
232 lines (201 loc) · 7.73 KB
/
prompt_search_list.py
File metadata and controls
232 lines (201 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
PromptSearchList: A ComfyUI node that searches prompts and outputs results as a list.
This node enables batch processing workflows by outputting prompt texts with
OUTPUT_IS_LIST=True, allowing direct connection to nodes that accept list inputs.
"""
import re
from typing import Any, Dict, List, Tuple
try:
from .utils.logging_config import get_logger
except ImportError:
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from utils.logging_config import get_logger
try:
from comfy.comfy_types import IO, ComfyNodeABC, InputTypeDict
except ImportError:
class ComfyNodeABC:
pass
class IO:
STRING = "STRING"
INT = "INT"
InputTypeDict = dict
try:
from .database.operations import PromptDatabase
except ImportError:
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from database.operations import PromptDatabase
class PromptSearchList(ComfyNodeABC):
"""
A ComfyUI node that searches the prompt database and outputs matching
prompts as a list for batch processing workflows.
Features:
- Search by text content
- Filter by category, tags, and minimum rating
- Outputs as OUTPUT_IS_LIST for batch node compatibility
- Read-only operation (no database writes)
"""
def __init__(self):
self.logger = get_logger("prompt_search_list.node")
self.logger.debug("Initializing PromptSearchList node")
self.db = PromptDatabase()
@classmethod
def INPUT_TYPES(cls) -> InputTypeDict:
return {
"required": {},
"optional": {
"text": (
IO.STRING,
{
"default": "",
"tooltip": "Search text to match against prompt content",
},
),
"category": (
IO.STRING,
{
"default": "",
"tooltip": "Filter by specific category",
},
),
"tags": (
IO.STRING,
{
"default": "",
"tooltip": "Comma-separated list of tags to filter by (partial match)",
},
),
"min_rating": (
IO.INT,
{
"default": 0,
"min": 0,
"max": 5,
"tooltip": "Minimum rating (0-5) to include in results",
},
),
"limit": (
IO.INT,
{
"default": 50,
"min": 1,
"max": 1000,
"tooltip": "Maximum number of results to return",
},
),
"skip_multipart": (
"BOOLEAN",
{
"default": True,
"tooltip": "Skip prompts containing Clip_1/Clip_2/etc. multi-part markers",
},
),
},
}
RETURN_TYPES = (IO.STRING, IO.STRING, IO.INT)
RETURN_NAMES = ("prompts", "preview", "count")
OUTPUT_IS_LIST = (True, False, False)
OUTPUT_TOOLTIPS = (
"List of prompt texts matching the search criteria.",
"Preview of all found prompts (for display).",
"Number of prompts found.",
)
FUNCTION = "search"
OUTPUT_NODE = True
CATEGORY = "🫶 ComfyAssets/🧠 Prompts"
DESCRIPTION = (
"Searches the prompt database and outputs matching prompts as a list. "
"Use this node to retrieve stored prompts for batch processing workflows. "
"'prompts' output sends each prompt individually to downstream nodes (batch). "
"'preview' output shows all found prompts as one text block. "
"'count' output shows how many prompts were found."
)
def search(
self,
text: str = "",
category: str = "",
tags: str = "",
min_rating: int = 0,
limit: int = 50,
skip_multipart: bool = True,
):
"""
Search for prompts matching the given criteria.
Args:
text: Search text to match against prompt content
category: Filter by specific category
tags: Comma-separated list of tags to filter by
min_rating: Minimum rating (0-5) to include
limit: Maximum number of results to return
Returns:
Dict with 'ui' display info and 'result' tuple
"""
try:
# Parse tags from comma-separated string
tags_list = None
if tags and tags.strip():
tags_list = [tag.strip() for tag in tags.split(",") if tag.strip()]
# Perform search with partial tag matching for discovery
results = self.db.search_prompts(
text=text.strip() if text and text.strip() else None,
category=category.strip() if category and category.strip() else None,
tags=tags_list,
tag_partial=True,
rating_min=min_rating if min_rating > 0 else None,
limit=limit,
)
# Extract prompt text, collapsing newlines to spaces so downstream
# nodes that split on \n (e.g. StringOutputList) treat each DB
# entry as a single prompt.
prompt_texts = [
" ".join(r["text"].split()) for r in results if r.get("text")
]
# Filter out multi-part prompts (Clip_1/Clip_2 markers)
if skip_multipart:
prompt_texts = [
p for p in prompt_texts if not re.search(r"Clip_\d+", p)
]
# Filter out prompts that are only LoRA tags with no actual content.
# Uses subtraction instead of repeated groups to avoid backtracking.
prompt_texts = [
p for p in prompt_texts if re.sub(r"<lora:[^>]+>", "", p).strip()
]
count = len(prompt_texts)
# Build a preview: numbered list of truncated prompts
preview_lines = []
for i, p in enumerate(prompt_texts, 1):
truncated = p[:120] + "..." if len(p) > 120 else p
preview_lines.append(f"[{i}] {truncated}")
preview = "\n".join(preview_lines) if preview_lines else "No results found"
# Must return at least one element — ComfyUI's slice_dict
# indexes into OUTPUT_IS_LIST outputs and crashes on empty lists.
if not prompt_texts:
self.logger.info("Search returned no results")
return {
"ui": {"text": ["No results found"]},
"result": ([""], preview, count),
}
return {
"ui": {"text": [f"Found {count} prompts"]},
"result": (prompt_texts, preview, count),
}
except Exception as e:
self.logger.error(f"Search error: {e}", exc_info=True)
return {
"ui": {"text": [f"Search error: {e}"]},
"result": ([""], f"Error: {e}", 0),
}
@classmethod
def IS_CHANGED(
cls, text="", category="", tags="", min_rating=0, limit=50, skip_multipart=True
):
"""
Always re-execute to get fresh results from the database.
The database contents may have changed since the last execution,
so we always return a unique value to trigger re-execution.
"""
import time
return time.time()