diff --git a/.gitignore b/.gitignore index 0c81f1c..b8749dd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ env/ *.swo *~ +# API Keys +api_key.txt + # Model files (too large for git) *.gguf *.safetensors diff --git a/README.md b/README.md index 7d53929..e14f6a1 100644 --- a/README.md +++ b/README.md @@ -80,10 +80,15 @@ ov1, ov2 = (512, 384) if w >= h else (384, 512) 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. Setup: -```bash -pip install typesafe-sdk -export TYPESAFE_API_KEY="your-api-key" -``` +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//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. Extra input: - `max_jev_calls` (INT, default 8): maximum Jev API requests per run. Identical questions within a run are asked only once. diff --git a/__init__.py b/__init__.py index 9a46fc6..6dd5ffa 100644 --- a/__init__.py +++ b/__init__.py @@ -14,6 +14,7 @@ import ast import datetime import math import random +from pathlib import Path from typing import Any, Dict from .jev_backend import JevBackend, TypeSafeClient @@ -22,6 +23,7 @@ from .jev_backend import JevBackend, TypeSafeClient _DEFAULT_MAX_STEPS = 10000 _DEFAULT_MAX_CALL_DEPTH = 32 _MAX_RANGE_SIZE = 10000 +_API_KEY_PATH = Path(__file__).resolve().parent / "api_key.txt" _BLOCKED_AST_NODES = ( ast.Import, @@ -889,7 +891,16 @@ class MultiOutputScriptJev(MultiOutputScript): ): if TypeSafeClient is None: raise RuntimeError("typesafe-sdk is not installed. Run: pip install typesafe-sdk") - with TypeSafeClient() as client: + if not _API_KEY_PATH.exists(): + raise EnvironmentError( + f"API key file not found: {_API_KEY_PATH}\nPlease create this file with your TypeSafe API key." + ) + api_key = _API_KEY_PATH.read_text(encoding="utf-8").strip() + if not api_key: + raise EnvironmentError( + f'API key is not set in "{_API_KEY_PATH}"\nPlease add your TypeSafe API key to this file.' + ) + with TypeSafeClient(api_key=api_key) as client: return _run_script( code, JevBackend(client, max_jev_calls),