Files

27 lines
1.1 KiB
Python

import json
import pathlib
def catalog_models(folder: pathlib.Path) -> list[tuple[str, object]]:
files = []
for path in folder.glob("*.json"):
try:
with open(path, encoding="utf-8") as handle:
files.append(json.load(handle))
except (OSError, ValueError):
continue
return newest_catalog_models(files)
# A model catalog file: {"fetchedAt": 1790618085238, "catalog": {"config": {"models": [{"id": "claude-opus-5-5", "name": "Opus 5.5", ...}]}}}
def newest_catalog_models(files: list[object]) -> list[tuple[str, object]]:
"""The (id, name) of each model in the newest of the parsed catalog files."""
newest: dict = {}
for data in files:
if isinstance(data, dict) and isinstance(data.get("fetchedAt"), (int, float)) and data["fetchedAt"] > newest.get("fetchedAt", -1):
newest = data
models: object = newest
for key in ("catalog", "config", "models"):
models = models.get(key) if isinstance(models, dict) else None
return [(entry["id"], entry.get("name", entry["id"])) for entry in models if isinstance(entry, dict) and isinstance(entry.get("id"), str)] if isinstance(models, list) else []