import os
import shutil
import subprocess
from pathlib import Path
def _find_binary() -> Path:
path = shutil.which("codex-relay")
if path:
return Path(path)
local = Path(__file__).parent / "_bin" / "codex-relay"
if local.exists():
return local
raise FileNotFoundError(
"codex-relay binary not found. "
"Install with: pip install codex-relay or cargo install codex-relay"
)
def start(
port: int = 4444,
upstream: str = "https://openrouter.ai/api/v1",
api_key: str = "",
) -> subprocess.Popen:
env = os.environ.copy()
if api_key:
env["CODEX_RELAY_API_KEY"] = api_key
return subprocess.Popen(
[str(_find_binary()), "--port", str(port), "--upstream", upstream],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
__all__ = ["start"]