17 Commits
Author SHA1 Message Date
kantan-kanto 1d68bc85ec Update documentation and version for 1.3.0 2026-09-29 06:58:00 +09:00
kantan-kanto 7a0dd36c1f Always unload the local Jev model after each run and remove keep_model_loaded 2026-09-28 22:12:05 +09:00
kantan-kanto e874207f33 Fix uniform Jev probabilities with upstream llama-cpp-python by reading logits from the context 2026-09-28 21:34:24 +09:00
kantan-kanto f84abc9e8a Add TypeSafe API backend to MultiOutputScript (Jev) with the key in api_key.txt 2026-09-28 18:52:05 +09:00
kantan-kanto 6949a6f236 Size the default Jev aspect ratio from megapixels in in_value_1 2026-09-26 19:00:01 +09:00
kantan-kanto 40b2a5fa92 Add jev.probabilities and an eight-way aspect ratio default Jev script 2026-09-26 18:17:30 +09:00
kantan-kanto c65a9a8f4a Show the Jev choice in ot1 in the default Jev script 2026-09-26 18:06:39 +09:00
kantan-kanto d1975cc511 Fix Jev model listing for models on another drive 2026-09-26 18:02:28 +09:00
kantan-kanto a977b97d29 Replace TypeSafe API with local GGUF decisions via llama.cpp in MultiOutputScript (Jev) 2026-09-26 17:54:57 +09:00
kantan-kanto 288275c100 Load Jev API key from api_key.txt beside the node 2026-09-26 17:24:52 +09:00
kantan-kanto af8f8cc25d Add MultiOutputScript (Jev) node with jev namespace for scripts 2026-09-26 17:06:14 +09:00
kantan-kanto a3a9fc5b63 Document README examples and add project metadata. 2026-06-20 19:50:01 +09:00
kantan-kanto b3933446bd Replace dynamic script execution with a safe AST interpreter. 2026-05-31 18:18:53 +09:00
kantan-kanto d61c2f3806 Add an application recipe for converting LLM dialogue transcripts into dialogue_segments_json for TTS workflows. 2026-05-16 18:23:32 +09:00
kantan-kanto 7207aa0d7f feat: add safe-mode AST checks and execution timeout for MultiOutputScript 2026-04-06 13:54:35 +09:00
kantan-kanto bc82df9fec docs: update README and pyproject metadata for v1.1.0 2026-04-03 20:41:47 +09:00
kantan-kanto 7d4270182b feat: add centi node and update README for v1.1.0 2026-04-02 21:07:36 +09:00
9 changed files with 1549 additions and 95 deletions
+4 -1
View File
@@ -32,6 +32,9 @@ env/
*.swo
*~
# API Keys
api_key.txt
# Model files (too large for git)
*.gguf
*.safetensors
@@ -49,4 +52,4 @@ Thumbs.db
output/
temp/
experiments/
experiments/
+66
View File
@@ -0,0 +1,66 @@
# Changelog
All notable changes to ComfyUI-ScriptFlow will be documented in this file.
## [Unreleased]
## [1.3.0] - 2026-09-29
- Added `MultiOutputScript (Jev)` node (`utils`)
- Same inputs, outputs, and script rules as `MultiOutputScript`, plus a `jev` namespace that answers typed questions about text
- `jev.yes(state, question[, threshold])`, `jev.noul(state, question)`, `jev.choice(state, question, options)`, `jev.probabilities(state, question, options)`, and `jev.score(state, question, levels)`
- Using a `jev.noul()` probability directly as a condition raises an error instead of always being true
- Identical questions within a run are asked only once; `max_jev_calls` limits API requests or model evaluations per run
- Default script picks one of eight aspect ratios for the prompt in `in_text_1`, sizes it to the megapixels in `in_value_1`, and lists every ratio's probability in `out_text_1`
- Added two backends selected by the `model` input; scripts are the same for both
- `TypeSafe API`: asks TypeSafe AI's Jev through the optional `typesafe-sdk` package, with the API key read from `api_key.txt` in the node's installation directory on every run
- Local GGUF models as a fallback when the Jev API is not available: models under `models/LLM` and `models/text_encoders`, including paths registered in `extra_model_paths.yaml` on other drives
- The local backend reads the decision from next-token probabilities of lettered options in one forward pass through the optional `llama-cpp-python` package, adapted from SemIf (OpenJev, MIT License); no text is generated and nothing is sent over the network
- The local model is loaded on the first `jev` question of a run and always unloaded when the run ends, freeing VRAM on CUDA, Intel XPU, and other ComfyUI devices
- Kept `MultiOutputScript` and `centi` unchanged
- `jev` is not available in `MultiOutputScript`, which still makes no network requests and loads no models
- Existing nodes do not require `typesafe-sdk` or `llama-cpp-python`
- Added `api_key.txt` to `.gitignore`
- Moved release notes from `README.md` to `CHANGELOG.md`
- Replaced the README demo with a `MultiOutputScript (Jev)` demo at the top of the README
## [1.2.0] - 2026-05-31
- Replaced direct Python execution with a safe AST interpreter for ComfyUI Registry compatibility.
- Added Python-subset support for helper functions, loops, dictionaries, lists, string methods, f-strings, `random`, `datetime`, and `math`.
- Replaced trace-based timeout handling with step and loop limits.
- Kept existing `MultiOutputScript` inputs and outputs unchanged.
## [1.1.1] - 2026-04-06
- Added safe-mode AST validation before script execution.
- Blocked unsafe syntax (`Import`, `ImportFrom`, `Global`, `Nonlocal`, `ClassDef`, `Try`, `With`, `AsyncWith`).
- Blocked unsafe dynamic execution, file/input access, namespace inspection, and dynamic attribute mutation calls.
- Added execution timeout guard (`1.5s` default) to stop runaway scripts.
- Updated security notes for trusted-workflow usage.
## [1.1.0] - 2026-04-03
Added `centi` node (`utils`).
- Minimal connector-only utility node.
- Three optional integer inputs: `int_1`, `int_2`, `int_3`.
- Three float outputs: `float_1`, `float_2`, `float_3`.
- Each connected input is converted by `int_n / 100`; unconnected inputs return `None`.
## [1.0.1] - 2026-04-01
- Improved documentation and project metadata.
## [1.0.0] - 2026-03-31
- Initial release
+13
View File
@@ -0,0 +1,13 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This project is licensed under the GNU General Public License v3.0 or later
(SPDX: GPL-3.0-or-later).
For the full license text, see:
https://www.gnu.org/licenses/gpl-3.0.txt
+344 -28
View File
@@ -1,11 +1,27 @@
# ComfyUI-ScriptFlow
**Version:** 1.0.1
**Version:** 1.3.0
**License:** GPL-3.0
A general-purpose calculation node that executes user-written Python code and returns multiple text and numeric outputs based on the inputs.
Safe Python-like script node for ComfyUI with a System One decision model.
Ask yes/no, choice, and score questions via TypeSafe AI's Jev API, with local GGUF LLMs as a fallback, and use the answer directly in an `if` statement.
This custom node accepts numbers (int/float) and text (string) as inputs, runs calculations and logical operations internally, and returns multiple values through separate output ports. It consolidates workflows that previously required combining several basic nodes into a single node.
![MultiOutputScript (Jev) demo](images/MultiOutputScript_Jev.gif)
`MultiOutputScript (Jev)` picks an aspect ratio for a text prompt through the TypeSafe API, lists every ratio's probability, and passes the width and height to an image node.
A simplified version of the demo: connect a text prompt to `in_text_1`, and the answer picks the width and height.
```python
if jev.yes(it1, "Does a portrait orientation suit this prompt?"):
ov1, ov2 = 832, 1280
else:
ov1, ov2 = 1280, 832
```
A System One model answers with probabilities in a single fast pass instead of generating text, so the answer can drive workflow logic directly.
Multiple inputs and outputs let one node handle math, logic, text editing, conditional routing, resolution selection, filename generation, and more — work that previously took several basic nodes. Scripts run in a restricted interpreter with no file or OS access.
## Installation
@@ -16,11 +32,33 @@ This custom node accepts numbers (int/float) and text (string) as inputs, runs c
```
3. Restart ComfyUI.
The `MultiOutputScript` and `centi` nodes need no extra packages. `MultiOutputScript (Jev)` needs the setup below for the backend you use.
### For `MultiOutputScript (Jev)`
#### TypeSafe API
1. Install the SDK in the Python environment that runs ComfyUI:
```bash
pip install typesafe-sdk
```
2. Create `api_key.txt` in the actual installation directory of this custom node and add your TypeSafe API key (single line, no quotes):
```
ComfyUI/custom_nodes/<your-installation-folder>/api_key.txt
```
The key is read from this file on every run; the `TYPESAFE_API_KEY` environment variable is not used. `api_key.txt` is listed in `.gitignore`; never commit it.
#### Local models (fallback)
- Install [llama-cpp-python](https://github.com/JamePeng/llama-cpp-python) with GPU support for your platform in the Python environment that runs ComfyUI. Tested with the JamePeng fork and the upstream [abetlen/llama-cpp-python](https://github.com/abetlen/llama-cpp-python) 0.3.35. A CPU-only build works but is much slower.
- Put an instruction-tuned GGUF model with a chat template in `ComfyUI/models/LLM` (or `models/text_encoders`). Tested with Qwen3.5-9B Q8_0 and Gemma-4-E4B Q8.
## Key Features
- Multiple inputs: freely combine numeric and text inputs.
- Calculations and logic: supports arithmetic, comparison, and simple conditional branching.
- Multiple outputs: pass results to downstream nodes via separate output ports.
- Safer execution: restricted environment (no file/OS access).
- AI decisions: `jev.yes` / `jev.choice` / `jev.score` in `MultiOutputScript (Jev)`, with probabilities, through TypeSafe AI's Jev API or a local GGUF model as a fallback.
- Safer execution: scripts run in an AST interpreter with restricted syntax and no file/OS access. Only the Jev node's `jev` functions reach a model or the TypeSafe API.
## Use Cases
- In Wan2.2 I2V workflows, automatically select a recommended resolution
@@ -28,14 +66,16 @@ This custom node accepts numbers (int/float) and text (string) as inputs, runs c
- Batch-generate file names (e.g., local LLM prompt history, output logs).
- Apply conditional logic to control workflow behavior (e.g., switch parameters or routes based on inputs).
- Parse and transform LLM outputs into structured values for downstream nodes.
Dynamically generate numeric parameters (e.g., seeds, thresholds, scaling values) using custom calculations.
- Dynamically generate numeric parameters (e.g., seeds, thresholds, scaling values) using custom calculations.
- Pick an aspect ratio and size for a text prompt before generation, with the probability of every ratio (Jev node default script).
- Route workflows by the meaning of text, e.g. check whether a prompt describes a night scene or classify its main subject.
## Node
**Name:** `MultiOutputScript`
## Nodes
### `MultiOutputScript`
**Category:** `utils`
### UI Ports (Current)
Inputs (connectable):
Inputs (optional, connectable):
- `in_text_1` (accepts any input; validated as `str` at runtime)
- `in_text_2` (accepts any input; validated as `str` at runtime)
- `in_text_3` (accepts any input; validated as `str` at runtime)
@@ -54,6 +94,7 @@ Outputs:
Notes on numeric outputs:
- Outputs are `INT`.
- If you need `FLOAT` values from integer inputs, use the `centi` node.
- If a value is a float, the fractional part is truncated on output.
- If you prefer rounding or ceiling, handle it in the script (e.g., `round(...)`, `math.ceil(...)`).
@@ -73,44 +114,127 @@ w, h = iv1, iv2
ov1, ov2 = (512, 384) if w >= h else (384, 512)
```
### `MultiOutputScript (Jev)`
**Category:** `utils`
Same inputs, outputs, and script rules as `MultiOutputScript`, plus a `jev` namespace that asks [Jev](https://typesafe.ai), TypeSafe AI's decision model, typed questions about text and lets scripts branch on the answer.
- **TypeSafe API** (primary): sends the questions to Jev over the internet.
- **Local GGUF model** (fallback): for when the Jev API is not available, or when text must stay on your machine. It approximates Jev-style decisions with a general instruction-tuned model, reading the answer from next-token probabilities of lettered options in one forward pass, following [SemIf (OpenJev)](https://github.com/TheoLeeCJ/SemIf-OpenJev). No text is generated and nothing is sent over the network. Answers and probabilities differ from Jev's.
Scripts are the same for both. See [Installation](#installation) for the setup of each backend.
Extra inputs:
- `model`: `TypeSafe API`, or a local GGUF model (`mmproj-*` files are hidden).
- `max_jev_calls` (INT, default 8): maximum API requests or model evaluations per run. Identical questions within a run are asked only once.
Functions (`state` is a string, dict, or list; text only):
- `jev.yes(state, question[, threshold])` → `bool` (yes-probability ≥ `threshold`, default `0.5`)
- `jev.noul(state, question)` → yes-probability (`0.0`–`1.0`). Compare it with a threshold; using it directly as a condition raises an error.
- `jev.choice(state, question, options)` → the most likely option (`str`). `options` is a list of labels, or a dict of labels to descriptions (2–16 options for local models).
- `jev.probabilities(state, question, options)` → `dict` of each option to its probability (sums to `1.0`). Same `options` as `jev.choice`; asking both with the same arguments asks the model once.
- `jev.score(state, question, levels)` → expected level (`float`, `0` to `len(levels) - 1`) for ordered level descriptions (2–16 levels for local models)
```python
# it1: a prompt text
if jev.yes(it1, "Does this prompt describe a night scene?", 0.7):
ov1 = 30
else:
ov1 = 20
kind = jev.choice(it1, "Which orientation suits this prompt?", ["portrait", "landscape"])
ov2, ov3 = (384, 512) if kind == "portrait" else (512, 384)
probs = jev.probabilities(it1, "Which orientation suits this prompt?", ["portrait", "landscape"])
ot1 = f"{kind} (portrait: {probs['portrait']:.3f}, landscape: {probs['landscape']:.3f})"
```
The node's default script picks one of eight aspect ratios for the prompt in `it1`, sizes it to the megapixels in `in_value_1` (default `1.0` when unconnected; width and height rounded to multiples of 64), outputs the width and height to `out_value_1` / `out_value_2`, and lists every ratio's probability in `out_text_1`.
Each function asks one question. With the TypeSafe API it maps to one `system_one` request of the [TypeSafe Python SDK](https://pypi.org/project/typesafe-sdk/):
| ScriptFlow | TypeSafe SDK |
| --- | --- |
| `jev.noul(state, q)` | `client.system_one(state=state, questions={"q": Noul(instructions=q)}).answers["q"].noul` |
| `jev.yes(state, q, t)` | `... .answers["q"].noul >= t` |
| `jev.choice(state, q, options)` | `client.system_one(state=state, questions={"q": Choice(instructions=q, criteria=options)}).answers["q"].choice` |
| `jev.probabilities(state, q, options)` | `... .answers["q"].probabilities` (same request as `jev.choice`) |
| `jev.score(state, q, levels)` | `client.system_one(state=state, questions={"q": Score(instructions=q, criteria=levels)}).answers["q"].score` |
A list of `options` is sent as `criteria={label: None, ...}`. `confidence`, the Score level probabilities, and Noul `criteria` are not exposed.
Notes:
- The TypeSafe API sends `state` and questions over the internet. Do not use it with text you cannot share externally.
- A local question takes one forward pass (about 1–1.5 s with a 4B–9B model on GPU). The model is loaded on the first question of each run (several seconds) and unloaded when the run ends to free VRAM for the rest of the workflow.
- Local probabilities are uncalibrated and depend on the model; test thresholds on your own inputs.
- `MultiOutputScript` itself never loads a model or makes network requests; `jev` is not available there.
- This project is independent and not affiliated with TypeSafe AI.
### `centi`
**Category:** `utils`
Inputs (optional, connectable):
- `int_1` (INT)
- `int_2` (INT)
- `int_3` (INT)
Outputs:
- `float_1` (FLOAT) = `int_1 / 100`
- `float_2` (FLOAT) = `int_2 / 100`
- `float_3` (FLOAT) = `int_3 / 100`
Notes:
- If `int_n` is not connected, the corresponding `float_n` output is `None`.
- The node is intentionally minimal and connector-only.
---
## Execution Environment
The script is executed using:
```python
exec(code, globals_dict, locals_dict)
```
The script is parsed with Python AST and evaluated by ScriptFlow's safe interpreter. It supports common workflow logic such as assignment, arithmetic, branching, loops, user-defined helper functions, dictionaries, lists, strings, and selected methods.
### Allowed builtins
### Allowed functions
- `len`, `min`, `max`, `sum`, `abs`, `round`
- `int`, `float`, `str`, `bool`
- `sorted`, `reversed`
- `enumerate`, `range`, `zip`, `map`, `filter`
- `enumerate`, `range`, `zip`
- `any`, `all`, `pow`, `divmod`
- `list`, `dict`, `tuple`
### Allowed modules
- `random`
- `datetime`
- `math`
### Allowed namespaces
- `random`: `random`, `randint`, `uniform`, `choice`
- `datetime`: `datetime.now`, `date.today`, date/time fields such as `year`, `month`, `day`, `hour`, `minute`, `second`, plus `isoformat` and `strftime` (`strftime` requires v1.2.0 or later)
- `math`: `ceil`, `floor`, `sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `log`, `log10`, `exp`, `pow`, `fabs`, `isfinite`, `pi`, `e`, `tau`
### Allowed methods
- `str`: `replace`, `find`, `strip`, `split`, `splitlines`, `startswith`, `endswith`, `join`, `lower`, `upper`
- `list`: `append`
### Not allowed
- `import` statements
- file access (`open`, etc.)
- OS operations (`os`, `sys`)
- re-running `eval`/`exec`
- dynamic Python execution
- blocked syntax in safe mode: `Import`, `ImportFrom`, `Global`, `Nonlocal`, `ClassDef`, `Try`, `With`, `AsyncWith`, `Lambda`, `Delete`, `Yield`, `Await`
- blocked calls in safe mode: dynamic execution, file/input access, runtime namespace inspection, and dynamic attribute mutation
### Execution Notes
- Using `random` or `datetime` makes outputs non-deterministic.
- `datetime.strftime(...)` is supported in v1.2.0 or later for concise timestamp formatting:
```python
now = datetime.datetime.now()
ot1 = now.strftime("%Y%m%d%H%M%S")
```
- For versions earlier than v1.2.0, format the same timestamp manually:
```python
now = datetime.datetime.now()
ot1 = f'{now.year:04d}{now.month:02d}{now.day:02d}{now.hour:02d}{now.minute:02d}{now.second:02d}'
```
- Type mismatches raise an error and stop execution.
- Safe mode validates AST before evaluation and stops scripts that exceed the step or loop limit.
- Use only trusted scripts/workflows. Do not run untrusted code from unknown sources.
## Examples
### Demo
![Demo](images/MultiOutputScript.gif)
Demonstrates running the sample workflow with a landscape image and a portrait image.
### Example Workflow
```
@@ -118,6 +242,189 @@ examples/
├─ example_workflow.json
```
## Application Recipes
### Convert `LLM Dialogue Cycle` transcript text to `dialogue_segments_json`
<details>
<summary>Show ComfyUI-LLM-Session transcript conversion recipe</summary>
`ComfyUI-LLM-Session` returns the full result of `LLM Dialogue Cycle` as `transcript_text`.
You can connect that text to `MultiOutputScript.in_text_1` and paste the script below into
the `code` field to generate a speaker-tagged JSON text for downstream dialogue TTS nodes.
Recommended workflow:
```text
LLM Dialogue Cycle
-> transcript_text
-> MultiOutputScript.in_text_1
-> MultiOutputScript.out_text_1
-> dialogue_segments_json input of a TTS node
```
Paste this code into `MultiOutputScript.code`:
```python
# Convert ComfyUI-LLM-Session "LLM Dialogue Cycle" transcript_text
# into dialogue_segments_json for TTS.
# Only text inside Japanese corner quotes 「...」 is used as spoken dialogue.
#
# Input:
# it1: transcript_text
#
# Output:
# ot1: JSON text with schema "kantan.dialogue.v1"
#
# Expected transcript lines:
# [2026-05-16T12:00:00] USER -> A: initial prompt
# [2026-05-16T12:00:01] A: 彼女は少し考えた。「こんにちは。」
# [2026-05-16T12:00:02] B: 彼は笑った。「やあ。」「調子はどう?」
#
# The parser also accepts the unicode arrow form:
# USER → A:
def json_escape(value):
s = str(value)
s = s.replace("\\", "\\\\")
s = s.replace('"', '\\"')
s = s.replace("\n", "\\n")
s = s.replace("\r", "\\r")
s = s.replace("\t", "\\t")
return s
def extract_spoken_text(value):
source = str(value)
parts = []
position = 0
while position < len(source):
start = source.find("「", position)
if start < 0:
position = len(source)
else:
end = source.find("」", start + 1)
if end < 0:
position = len(source)
else:
spoken = source[start + 1:end].strip()
if spoken:
parts.append(spoken)
position = end + 1
return "\n".join(parts)
transcript = str(it1 or "")
lines = transcript.splitlines()
utterances = []
current = None
for raw_line in lines:
line = str(raw_line)
speaker = None
text = None
timestamp = ""
if line.startswith("[") and "] " in line:
close_index = line.find("] ")
timestamp = line[1:close_index]
rest = line[close_index + 2:]
if rest.startswith("A:"):
speaker = "A"
text = rest[2:].strip()
elif rest.startswith("B:"):
speaker = "B"
text = rest[2:].strip()
elif rest.startswith("USER -> A:"):
speaker = "USER"
text = rest[len("USER -> A:"):].strip()
elif rest.startswith("USER → A:"):
speaker = "USER"
text = rest[len("USER → A:"):].strip()
if speaker == "A" or speaker == "B":
if current is not None:
utterances.append(current)
current = {
"speaker": speaker,
"text": text,
"timestamp": timestamp,
}
else:
if current is not None:
if current["text"]:
current["text"] = current["text"] + "\n" + line
else:
current["text"] = line
if current is not None:
utterances.append(current)
items = []
index = 1
for u in utterances:
spoken_text = extract_spoken_text(u["text"])
if not spoken_text:
continue
item = (
'{"index":'
+ str(index)
+ ',"speaker":"'
+ json_escape(u["speaker"])
+ '","text":"'
+ json_escape(spoken_text)
+ '","timestamp":"'
+ json_escape(u["timestamp"])
+ '"}'
)
items.append(item)
index = index + 1
ot1 = (
'{"schema":"kantan.dialogue.v1",'
+ '"source":"ComfyUI-LLM-Session LLM Dialogue Cycle",'
+ '"utterances":['
+ ",".join(items)
+ "]}"
)
```
The resulting `out_text_1` is a JSON string like this:
```json
{
"schema": "kantan.dialogue.v1",
"source": "ComfyUI-LLM-Session LLM Dialogue Cycle",
"utterances": [
{
"index": 1,
"speaker": "A",
"text": "こんにちは。",
"timestamp": "2026-05-16T12:00:01"
},
{
"index": 2,
"speaker": "B",
"text": "やあ。\n調子はどう?",
"timestamp": "2026-05-16T12:00:02"
}
]
}
```
Notes:
- `USER -> A` / `USER → A` is intentionally skipped so TTS reads only character dialogue.
- Multiline model output is kept inside the previous `A` or `B` utterance.
- Only text inside `「...」` is emitted to `utterances[].text`; narration and inner thoughts outside quotes are skipped.
- If one utterance contains multiple quoted lines, they are joined with newlines.
- Utterances without `「...」` are omitted from the TTS script.
- `out_text_2`, `out_text_3`, and numeric outputs are unused by this recipe.
</details>
## License
This project is licensed under the **GNU General Public License v3.0**.
@@ -134,10 +441,19 @@ You should have received a copy of the GNU General Public License along with thi
## Support
- **Issues**: Report bugs or request features via GitHub Issues
- **Documentation**: See [CHANGELOG.md](CHANGELOG.md) for version history
- **Examples**: Check [examples/](examples/) for workflow templates
---
## Release Notes
### 1.0.0
- Initial release
## Changelog
See [CHANGELOG.md](CHANGELOG.md) for detailed version history.
### Current Version: 1.3.0
- Added `MultiOutputScript (Jev)` node with a `jev` namespace for typed decisions in scripts
- Added `jev.yes`, `jev.noul`, `jev.choice`, `jev.probabilities`, and `jev.score`
- Added TypeSafe API backend with the API key read from `api_key.txt`
- Added local GGUF backend through llama-cpp-python, adapted from SemIf (OpenJev)
- Added a default script that picks one of eight aspect ratios and sizes it from megapixels
- Kept `MultiOutputScript` and `centi` unchanged
+899 -63
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

+221
View File
@@ -0,0 +1,221 @@
# ComfyUI-ScriptFlow
# Copyright (C) 2026 kantan-kanto (https://github.com/kantan-kanto)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The decision readout is adapted from SemIf (formerly OpenJev),
# Copyright (c) 2026 TheoLeeCJ, MIT License: https://github.com/TheoLeeCJ/SemIf-OpenJev
from __future__ import annotations
import gc
import json
import os
from typing import Any
import comfy.model_management
import folder_paths
import numpy as np
try:
from llama_cpp import Llama, llama_get_logits_ith, llama_get_memory, llama_memory_clear
from llama_cpp.llama_chat_format import Jinja2ChatFormatter
except ImportError:
Llama = None
try:
from typesafe_sdk import Choice, Noul, Score, TypeSafeClient
except ImportError:
TypeSafeClient = None
_LETTERS = "ABCDEFGHIJKLMNOP"
_DIRECT_SYSTEM = (
"Apply the supplied criterion to the supplied evidence. Choose exactly one listed option. "
"Respond with only its uppercase letter, with no explanation or reasoning."
)
_N_CTX = 8192
_loaded_model: tuple[str, Any, Any] | None = None
class JevBackend:
"""Caches answers and limits model calls for a single script run."""
def __init__(self, engine: Any, max_calls: int):
self.engine = engine
self.max_calls = max_calls
self.calls = 0
self.cache: dict[str, Any] = {}
def noul(self, state: Any, question: Any) -> float:
return self._ask(state, "noul", question, None)[0]
def choice(self, state: Any, question: Any, options: list | dict) -> str:
probabilities = self.probabilities(state, question, options)
return max(probabilities, key=probabilities.get)
def probabilities(self, state: Any, question: Any, options: list | dict) -> dict[str, float]:
if isinstance(options, list):
options = {str(option): None for option in options}
return dict(zip(options, self._ask(state, "choice", question, options)))
def score(self, state: Any, question: Any, levels: list) -> float:
probabilities = self._ask(state, "score", question, list(levels))
return sum(level * p for level, p in enumerate(probabilities))
def _ask(self, state: Any, kind: str, question: Any, criteria: Any) -> list[float]:
key = json.dumps([state, kind, question, criteria], sort_keys=True, ensure_ascii=False)
if key in self.cache:
return self.cache[key]
if self.calls >= self.max_calls:
raise RuntimeError(f"Script exceeded max_jev_calls ({self.max_calls})")
self.calls += 1
probabilities = self.engine.ask(state, kind, question, criteria)
self.cache[key] = probabilities
return probabilities
class LocalJev:
"""Answers with a local GGUF model. Noul is asked as Yes/No options."""
def __init__(self, model_path: str):
self.model_path = model_path
def ask(self, state: Any, kind: str, question: Any, criteria: Any) -> list[float]:
if kind == "noul":
options = ["Yes", "No"]
elif kind == "choice":
options = [label if description is None else f"{label}: {description}" for label, description in criteria.items()]
else:
options = criteria
return _option_probabilities(self.model_path, state, question, options)
class TypeSafeJev:
"""Answers with TypeSafe's Jev API. Returns probabilities in the order of the options."""
def __init__(self, client: Any):
self.client = client
def ask(self, state: Any, kind: str, question: Any, criteria: Any) -> list[float]:
if kind == "noul":
p = self._answer(state, Noul(instructions=question)).noul
return [p, 1.0 - p]
if kind == "choice":
probabilities = self._answer(state, Choice(instructions=question, criteria=criteria)).probabilities
return [probabilities[label] for label in criteria]
probabilities = self._answer(state, Score(instructions=question, criteria=criteria)).probabilities
return [probabilities[level] for level in range(len(criteria))]
def _answer(self, state: Any, question: Any) -> Any:
return self.client.system_one(state=state, questions={"q": question}).answers["q"]
def _option_probabilities(model_path: str, state: Any, question: Any, options: list) -> list[float]:
"""Reads one decision from the next-token logits of the option letters, like SemIf's direct path."""
if not 2 <= len(options) <= len(_LETTERS):
raise ValueError(f"Jev questions need 2-{len(_LETTERS)} options, got {len(options)}")
llm, formatter = load_local_model(model_path)
payload = {
"evidence": state,
"criterion": question,
"options": [{"letter": letter, "description": option} for letter, option in zip(_LETTERS, options)],
}
messages = [
{"role": "system", "content": _DIRECT_SYSTEM},
{"role": "user", "content": json.dumps(payload, ensure_ascii=False)},
]
prompt = formatter(messages=messages, enable_thinking=False).prompt
tokens = llm.tokenize(prompt.encode("utf-8"), add_bos=False, special=True)
if len(tokens) >= llm.n_ctx():
raise ValueError(f"Jev prompt is {len(tokens)} tokens, over the {llm.n_ctx()} token context")
slots = []
for letter in _LETTERS[: len(options)]:
encoded = llm.tokenize(letter.encode("utf-8"), add_bos=False, special=False)
if len(encoded) != 1:
raise ValueError(f"Model tokenizer does not encode {letter!r} as one token")
slots.append(encoded[0])
# reset() alone keeps the recurrent state of hybrid models such as Qwen3.5
llama_memory_clear(llama_get_memory(llm.ctx), True)
llm.reset()
llm.eval(tokens)
# Llama.scores is not filled without logits_all in upstream llama-cpp-python, so read the context directly
vocabulary = np.ctypeslib.as_array(llama_get_logits_ith(llm.ctx, -1), shape=(llm.n_vocab(),))
logits = vocabulary[slots].astype(np.float64)
weights = np.exp(logits - logits.max())
return (weights / weights.sum()).tolist()
def _model_dirs() -> list[str]:
dirs = []
for key in ("LLM", "llm", "text_encoders"):
try:
dirs.extend(folder_paths.get_folder_paths(key))
except KeyError:
pass
dirs.append(os.path.join(folder_paths.models_dir, "LLM"))
return list(dict.fromkeys(os.path.normpath(d) for d in dirs))
def list_local_models() -> dict[str, str]:
"""GGUF language models under models/LLM and models/text_encoders, keyed by display name."""
models = {}
for root in _model_dirs():
if not os.path.isdir(root):
continue
for current_dir, _, files in os.walk(root):
for file_name in files:
if file_name.endswith(".gguf") and not file_name.startswith("mmproj"):
path = os.path.join(current_dir, file_name)
models[_display_name(path)] = path
return dict(sorted(models.items(), key=lambda item: item[0].lower()))
def _display_name(path: str) -> str:
# models registered through extra_model_paths.yaml may be outside models_dir or on another drive
try:
rel = os.path.relpath(path, folder_paths.models_dir)
except ValueError:
return path
return path if rel.startswith("..") else rel.replace("\\", "/")
def load_local_model(model_path: str) -> tuple[Any, Any]:
global _loaded_model
if _loaded_model is not None and _loaded_model[0] == model_path:
return _loaded_model[1], _loaded_model[2]
if Llama is None:
raise RuntimeError("llama-cpp-python is not installed. See the README for installation.")
unload_local_model()
device = comfy.model_management.get_torch_device()
comfy.model_management.free_memory(os.path.getsize(model_path) * 1.2, device)
llm = Llama(model_path=model_path, n_ctx=_N_CTX, n_gpu_layers=-1, verbose=False)
template = llm.metadata.get("tokenizer.chat_template")
if not template:
llm.close()
raise ValueError(f"GGUF has no chat template: {model_path}")
bos, eos = llm.token_bos(), llm.token_eos()
formatter = Jinja2ChatFormatter(
template=template,
bos_token=llm.detokenize([bos], special=True).decode("utf-8") if bos != -1 else "",
eos_token=llm.detokenize([eos], special=True).decode("utf-8") if eos != -1 else "",
)
_loaded_model = (model_path, llm, formatter)
return llm, formatter
def unload_local_model() -> None:
# Same cleanup as ComfyUI-LLM-Session's Unload LLM Model: drop the model, collect, then empty the device cache
global _loaded_model
if _loaded_model is None:
return
llm = _loaded_model[1]
_loaded_model = None
llm.close()
del llm
gc.collect()
comfy.model_management.soft_empty_cache()
+2 -3
View File
@@ -1,7 +1,7 @@
[project]
name = "ComfyUI-ScriptFlow"
description = "Python script node for ComfyUI supporting math, calc, and logic with multi output. Handle conditional workflows, auto-select resolution, parse LLM outputs, and generate filenames — all in one flexible node."
version = "1.0.1"
description = "Safe Python-like script node with a System One decision model: ask yes/no, choice, and score questions via TypeSafe AI's Jev API, with local GGUF LLMs as a fallback. Multiple inputs and outputs let one node handle math, logic, text editing, conditional routing, resolution selection, filename generation, and more."
version = "1.3.0"
license = {file = "LICENSE"}
# classifiers = [
# # For OS-independent nodes (works on all operating systems)
@@ -34,4 +34,3 @@ DisplayName = "ComfyUI-ScriptFlow"
# Icon = ""
includes = []
# "requires-comfyui" = ">=1.0.0" # ComfyUI version compatibility