|
| 1 | +"""The provider tables, as data, for the vet protocol's ``provider_tables`` op. |
| 2 | +
|
| 3 | +``lm15-contract/tables/providers.json`` is this function's output, and |
| 4 | +``tools/audit.py`` there fails when the two differ. Ports generate their |
| 5 | +copies from that file (``playbooks/port.md`` rule 2: tables are data, copied, |
| 6 | +never re-derived). This module reads the tables the reference uses at import |
| 7 | +time and serializes them; it adds no fact of its own. |
| 8 | +
|
| 9 | +Shape rules, so a generator never guesses: |
| 10 | +
|
| 11 | +- Compat policies list only the knobs a preset sets (``None`` means |
| 12 | + "inherit", so an absent key is exactly the value), with |
| 13 | + ``model_overrides`` as ``[prefix, {knob: value}]`` pairs in match order. |
| 14 | +- Access policies, host specs and endpoint support are written whole, every |
| 15 | + field, so a port never depends on its own defaults agreeing with these. |
| 16 | +- Tuples become arrays and keep their order; mappings keep insertion order; |
| 17 | + a frozenset (``EndpointSupport.extra``) becomes a sorted array. |
| 18 | +- Field names are the reference's (snake_case); a port renames them. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import dataclasses |
| 24 | +from collections.abc import Mapping |
| 25 | +from typing import Any |
| 26 | + |
| 27 | +SCHEMA = 1 |
| 28 | + |
| 29 | + |
| 30 | +_NO_DEFAULT = object() |
| 31 | + |
| 32 | + |
| 33 | +def _default(f: dataclasses.Field) -> Any: |
| 34 | + if f.default is not dataclasses.MISSING: |
| 35 | + return f.default |
| 36 | + if f.default_factory is not dataclasses.MISSING: # type: ignore[misc] |
| 37 | + return f.default_factory() # type: ignore[misc] |
| 38 | + return _NO_DEFAULT |
| 39 | + |
| 40 | + |
| 41 | +def _plain(value: Any, *, elide: tuple[type, ...]) -> Any: |
| 42 | + if dataclasses.is_dataclass(value) and not isinstance(value, type): |
| 43 | + out: dict[str, Any] = {} |
| 44 | + for f in dataclasses.fields(value): |
| 45 | + v = getattr(value, f.name) |
| 46 | + if isinstance(value, elide) and v == _default(f): |
| 47 | + continue # a compat knob at its default inherits |
| 48 | + out[f.name] = _plain(v, elide=elide) |
| 49 | + return out |
| 50 | + if isinstance(value, Mapping): |
| 51 | + return {str(k): _plain(v, elide=elide) for k, v in value.items()} |
| 52 | + if isinstance(value, (frozenset, set)): |
| 53 | + return sorted(_plain(v, elide=elide) for v in value) |
| 54 | + if isinstance(value, (list, tuple)): |
| 55 | + return [_plain(v, elide=elide) for v in value] |
| 56 | + if value is None or isinstance(value, (str, int, float, bool)): |
| 57 | + return value |
| 58 | + raise TypeError(f"provider_tables: no JSON form for {type(value).__name__}") |
| 59 | + |
| 60 | + |
| 61 | +def provider_tables() -> dict[str, Any]: |
| 62 | + from . import compat as _compat |
| 63 | + from .login import declared as _declared |
| 64 | + from .login import flows as _flows |
| 65 | + from . import registry as _registry |
| 66 | + from . import router as _router |
| 67 | + |
| 68 | + elide = (_compat.OpenAIChatCompat, _compat.OpenAIResponsesCompat, _compat.AnthropicCompat) |
| 69 | + |
| 70 | + def plain(value: Any) -> Any: |
| 71 | + return _plain(value, elide=elide) |
| 72 | + |
| 73 | + def row(definition: Any) -> dict[str, Any]: |
| 74 | + compat = definition.compat |
| 75 | + return { |
| 76 | + "id": definition.id, |
| 77 | + "dialect": definition.dialect, |
| 78 | + "kind": "hosted" if definition.hosted else ("bound" if definition.bound else "adapter-owned"), |
| 79 | + "compat": compat if compat is None or isinstance(compat, str) else plain(compat), |
| 80 | + "access": plain(definition.access), |
| 81 | + "aliases": list(definition.aliases), |
| 82 | + "placeholder_key": definition.placeholder_key, |
| 83 | + "console_url": definition.console_url, |
| 84 | + "note": definition.note, |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + "schema": SCHEMA, |
| 89 | + "providers": [row(d) for d in _registry.PROVIDERS.values()], |
| 90 | + "declared_login": [row(d) for d in _declared.DECLARED_PROVIDERS], |
| 91 | + "compat": { |
| 92 | + "chat": plain(_compat.OPENAI_CHAT_PRESETS), |
| 93 | + "chat_base_urls": plain(_compat.OPENAI_CHAT_PRESET_BASE_URLS), |
| 94 | + "responses": plain(_compat.OPENAI_RESPONSES_PRESETS), |
| 95 | + "responses_base_urls": plain(_compat.OPENAI_RESPONSES_PRESET_BASE_URLS), |
| 96 | + "anthropic": plain(_compat.ANTHROPIC_PRESETS), |
| 97 | + "anthropic_base_urls": plain(_compat.ANTHROPIC_PRESET_BASE_URLS), |
| 98 | + "preset_aliases": plain(_compat._OPENAI_CHAT_PRESET_ALIASES), |
| 99 | + }, |
| 100 | + "routing": { |
| 101 | + "default_rules": [{"prefix": r.prefix, "provider": r.provider, "note": r.note} for r in _router.DEFAULT_RULES], |
| 102 | + "litellm_prefixes": plain(_router.LITELLM_PROVIDER_PREFIXES), |
| 103 | + }, |
| 104 | + "login": {"service_labels": plain(_flows._SERVICE_LABELS)}, |
| 105 | + } |
0 commit comments