videogen/apps/api/app/integrations/railyard/provider_clients.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

94 lines
3.0 KiB
Python

"""The only module that imports model-provider SDKs (Wave P PM1.5, the Relay boundary).
Every LLM call reaches a model through Relay on Claude credits. Code that
needs an OpenAI-shaped client, the Gemini client, DashScope or LiteLLM gets
it here; with ``RAILYARD_LIVE=1`` (production) a client for an LLM that does
not point at Relay is refused with :class:`DirectProviderBlocked`, and the
caller falls back to its deterministic path. Speech providers (text to
speech) are not LLM calls and stay allowed with ``purpose=SPEECH``.
With ``RAILYARD_LIVE`` unset (development, self-hosting) every client is built
exactly as before. ``tests/reelforge/test_wp_pm1_5_railyard.py`` fails when any
module under ``app/`` imports a provider SDK outside this file and
``app/domains/railyard/``.
"""
from __future__ import annotations
import os
from typing import Any, Optional
from openai.types.chat import ChatCompletion
from .contract import railyard_live, relay_url
LLM = "llm"
SPEECH = "speech"
class DirectProviderBlocked(RuntimeError):
"""A direct LLM client was asked for while Relay is the only allowed path."""
def relay_only() -> bool:
return railyard_live(os.environ) or (os.environ.get("MENIVOR_LLM_RELAY_ONLY") or "").strip().lower() in {
"1", "true", "yes", "on"}
def _points_at_relay(base_url: Optional[str]) -> bool:
relay = relay_url(os.environ)
return bool(relay and base_url and str(base_url).rstrip("/").startswith(relay))
def check(purpose: str, base_url: Optional[str], provider: str) -> None:
if purpose == LLM and relay_only() and not _points_at_relay(base_url):
raise DirectProviderBlocked(
f"{provider}: LLM calls go through Relay in production (RAILYARD_LIVE=1); "
"set RELAY_URL or use the deterministic fallback"
)
def OpenAI(*, api_key: Any = None, base_url: Optional[str] = None, purpose: str = LLM, **kwargs: Any) -> Any: # noqa: N802
check(purpose, base_url, "openai-compatible")
from openai import OpenAI as _OpenAI
return _OpenAI(api_key=api_key, base_url=base_url, **kwargs)
def AzureOpenAI(**kwargs: Any) -> Any: # noqa: N802
check(kwargs.pop("purpose", LLM), None, "azure")
from openai import AzureOpenAI as _AzureOpenAI
return _AzureOpenAI(**kwargs)
def genai_modules(*, purpose: str = LLM, base_url: Optional[str] = None) -> tuple[Any, Any]:
"""``(google.genai, google.genai.types)``."""
check(purpose, base_url, "gemini")
from google import genai
from google.genai import types
return genai, types
def dashscope_modules() -> tuple[Any, Any]:
"""``(dashscope, GenerationResponse)``."""
check(LLM, None, "qwen")
import dashscope
from dashscope.api_entities.dashscope_response import GenerationResponse
return dashscope, GenerationResponse
def litellm_module() -> Any:
check(LLM, None, "litellm")
import litellm
return litellm
__all__ = [
"LLM", "SPEECH", "AzureOpenAI", "ChatCompletion", "DirectProviderBlocked", "OpenAI", "check",
"dashscope_modules", "genai_modules", "litellm_module", "relay_only",
]