2026-02-21 03:53:24 -08:00
|
|
|
"""Shared OpenRouter API client for Hermes tools.
|
|
|
|
|
|
|
|
|
|
Provides a single lazy-initialized AsyncOpenAI client that all tool modules
|
2026-03-11 20:02:36 -07:00
|
|
|
can share. Routes through the centralized provider router in
|
|
|
|
|
agent/auxiliary_client.py so auth, headers, and API format are handled
|
|
|
|
|
consistently.
|
2026-02-21 03:53:24 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
2026-03-11 20:02:36 -07:00
|
|
|
_client = None
|
2026-02-21 03:53:24 -08:00
|
|
|
|
|
|
|
|
|
2026-03-11 20:02:36 -07:00
|
|
|
def get_async_client():
|
|
|
|
|
"""Return a shared async OpenAI-compatible client for OpenRouter.
|
2026-02-21 03:53:24 -08:00
|
|
|
|
|
|
|
|
The client is created lazily on first call and reused thereafter.
|
2026-03-11 20:02:36 -07:00
|
|
|
Uses the centralized provider router for auth and client construction.
|
2026-02-21 03:53:24 -08:00
|
|
|
Raises ValueError if OPENROUTER_API_KEY is not set.
|
|
|
|
|
"""
|
|
|
|
|
global _client
|
|
|
|
|
if _client is None:
|
2026-03-11 20:02:36 -07:00
|
|
|
from agent.auxiliary_client import resolve_provider_client
|
|
|
|
|
client, _model = resolve_provider_client("openrouter", async_mode=True)
|
|
|
|
|
if client is None:
|
2026-02-21 03:53:24 -08:00
|
|
|
raise ValueError("OPENROUTER_API_KEY environment variable not set")
|
2026-03-11 20:02:36 -07:00
|
|
|
_client = client
|
2026-02-21 03:53:24 -08:00
|
|
|
return _client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_api_key() -> bool:
|
|
|
|
|
"""Check whether the OpenRouter API key is present."""
|
|
|
|
|
return bool(os.getenv("OPENROUTER_API_KEY"))
|