File size: 1,788 Bytes
49fa921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Publish the application model inventory for Codex clients."""

from pathlib import Path

from free_claude_code.api.model_catalog import build_models_list_response
from free_claude_code.application.ports import RequestRuntimePort
from free_claude_code.cli.launchers.codex_model_catalog import (
    build_codex_model_catalog,
    write_codex_model_catalog,
)
from free_claude_code.config.paths import codex_model_catalog_path


class CodexModelCatalogPublisher:
    """Own synchronization of the stable Codex model catalog file."""

    def __init__(self, catalog_path: Path | None = None) -> None:
        self._catalog_path = catalog_path

    def ensure_exists(self, runtime: RequestRuntimePort) -> None:
        """Publish a startup catalog only when no prior catalog exists."""

        catalog_path = self._resolved_catalog_path()
        if catalog_path.exists():
            return
        self._publish(runtime, catalog_path)

    def publish(self, runtime: RequestRuntimePort) -> None:
        """Publish the complete current application model inventory."""

        self._publish(runtime, self._resolved_catalog_path())

    def _publish(
        self,
        runtime: RequestRuntimePort,
        catalog_path: Path,
    ) -> None:
        models_response = build_models_list_response(
            runtime.current_settings(),
            runtime,
        )
        catalog = build_codex_model_catalog(models_response.model_dump())
        models = catalog.get("models")
        if not isinstance(models, list) or not models:
            raise ValueError("Codex model catalog contains no routable models.")
        write_codex_model_catalog(catalog_path, catalog)

    def _resolved_catalog_path(self) -> Path:
        return self._catalog_path or codex_model_catalog_path()