fix(openrouter): clean up registry loading

This commit is contained in:
Torbjørn Lindahl
2026-04-01 12:07:53 +02:00
parent cc8f6380d6
commit 4c4421b28f
2 changed files with 54 additions and 34 deletions

View File

@@ -22,18 +22,10 @@ class OpenRouterModelRegistry(CapabilityModelRegistry):
LIVE_DEFAULT_FILENAME = "openrouter_models_live.json"
def __init__(self, config_path: str | None = None, live_config_path: str | None = None) -> None:
self._live_use_resources = False
self._live_resource = None
self._live_config_path: Path | None = None
self._live_default_path = Path(__file__).resolve().parents[3] / "conf" / self.LIVE_DEFAULT_FILENAME
super().__init__(
env_var_name="OPENROUTER_MODELS_CONFIG_PATH",
default_filename="openrouter_models.json",
provider=ProviderType.OPENROUTER,
friendly_prefix="OpenRouter ({model})",
config_path=config_path,
)
if live_config_path:
self._live_config_path = Path(live_config_path)
else:
@@ -44,13 +36,19 @@ class OpenRouterModelRegistry(CapabilityModelRegistry):
try:
resource = importlib.resources.files("conf").joinpath(self.LIVE_DEFAULT_FILENAME)
if hasattr(resource, "read_text"):
self._live_use_resources = True
self._live_resource = resource
else:
raise AttributeError("resource accessor not available")
except Exception:
self._live_config_path = self._live_default_path
self.reload()
super().__init__(
env_var_name="OPENROUTER_MODELS_CONFIG_PATH",
default_filename="openrouter_models.json",
provider=ProviderType.OPENROUTER,
friendly_prefix="OpenRouter ({model})",
config_path=config_path,
)
def reload(self) -> None:
live_data = self._load_live_config_data()
@@ -62,13 +60,12 @@ class OpenRouterModelRegistry(CapabilityModelRegistry):
self._build_maps(configs)
def _load_live_config_data(self) -> dict:
if self._live_use_resources:
if self._live_resource is not None:
try:
resource = importlib.resources.files("conf").joinpath(self.LIVE_DEFAULT_FILENAME)
if hasattr(resource, "read_text"):
config_text = resource.read_text(encoding="utf-8")
if hasattr(self._live_resource, "read_text"):
config_text = self._live_resource.read_text(encoding="utf-8")
else:
with resource.open("r", encoding="utf-8") as handle:
with self._live_resource.open("r", encoding="utf-8") as handle:
config_text = handle.read()
data = json.loads(config_text)
except FileNotFoundError: