from __future__ import annotations import json import os from pathlib import Path def get_plugin_root() -> Path: return Path(__file__).resolve().parents[1] def get_default_config_path() -> Path: return get_plugin_root() / "config.json" def load_config(config_path: str | Path | None = None) -> dict: path = Path(config_path) if config_path is not None else get_default_config_path() if not path.exists(): return {} with path.open("r", encoding="utf-8") as handle: data = json.load(handle) return data if isinstance(data, dict) else {} def resolve_api_key(input_api_key: str | None, config_path: str | Path | None = None) -> str: if input_api_key and input_api_key.strip(): return input_api_key.strip() env_api_key = os.getenv("KIE_API_KEY", "").strip() if env_api_key: return env_api_key config_api_key = str(load_config(config_path).get("api_key", "")).strip() if config_api_key: return config_api_key raise ValueError( "Kie API key is required. Provide api_key on the node, set KIE_API_KEY, or add api_key to config.json." )