Files
skill-shujkimagegen-api/scripts/shujk_imagegen.py
T

115 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run the system imagegen CLI through the server6 direct Sub2API endpoint."""
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path
import subprocess
import sys
from typing import Any
DEFAULT_ORIGIN = "https://sub2apicn.shujk.top"
DEFAULT_BASE_URL = f"{DEFAULT_ORIGIN}/v1"
DEFAULT_SYSTEM_IMAGEGEN = (
Path(os.environ.get("CODEX_HOME", Path.home() / ".codex"))
/ "skills/.system/imagegen/scripts/image_gen.py"
)
def die(message: str) -> None:
print(f"Error: {message}", file=sys.stderr)
raise SystemExit(1)
def load_auth(path: Path) -> str:
if not path.exists():
die(f"Codex auth file not found: {path}")
try:
data = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
die(f"Codex auth file is not valid JSON: {exc}")
if not isinstance(data, dict):
die("Codex auth file must contain a JSON object.")
api_key = data.get("OPENAI_API_KEY")
if not isinstance(api_key, str) or not api_key.strip():
die("OPENAI_API_KEY is missing from Codex auth.")
return api_key.strip()
def print_redacted_config(
*,
auth_path: Path,
system_script: Path,
) -> None:
api_key = load_auth(auth_path)
payload: dict[str, Any] = {
"auth_path": str(auth_path),
"direct_origin": DEFAULT_ORIGIN,
"openai_base_url": DEFAULT_BASE_URL,
"openai_api_key": f"<redacted len={len(api_key)}>",
"system_imagegen_script": str(system_script),
"system_imagegen_exists": system_script.exists(),
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run system imagegen CLI through the server6 direct Sub2API endpoint.",
allow_abbrev=False,
)
parser.add_argument(
"--auth-file",
type=Path,
default=Path.home() / ".codex/auth.json",
help="Path to Codex auth JSON. Defaults to ~/.codex/auth.json.",
)
parser.add_argument(
"--system-imagegen-script",
type=Path,
default=DEFAULT_SYSTEM_IMAGEGEN,
help="Path to the system imagegen CLI script.",
)
parser.add_argument(
"--print-config-redacted",
action="store_true",
help="Print resolved non-secret config and exit.",
)
parser.add_argument(
"imagegen_args",
nargs=argparse.REMAINDER,
help="Arguments passed through to the system imagegen CLI.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
auth_path = args.auth_file.expanduser()
system_script = args.system_imagegen_script.expanduser()
if args.print_config_redacted:
print_redacted_config(auth_path=auth_path, system_script=system_script)
return 0
if not args.imagegen_args:
die("Missing imagegen CLI arguments. Try: generate --prompt ... --out output.png")
if not system_script.exists():
die(f"System imagegen CLI not found: {system_script}")
env = os.environ.copy()
env["OPENAI_API_KEY"] = load_auth(auth_path)
env["OPENAI_BASE_URL"] = DEFAULT_BASE_URL
command = [sys.executable, str(system_script), *args.imagegen_args]
completed = subprocess.run(command, env=env, check=False)
return completed.returncode
if __name__ == "__main__":
raise SystemExit(main())