项目文件夹

文件
Ali Khokhar 2a676cc6d9 Make ProviderModelInfo the sole model-catalog contract (#1222)
## Problem

Provider model discovery consumes metadata, but providers and the cache
still expose a parallel IDs-only contract. The duplicate contract adds
adapters and lets tests bypass capability metadata.

## Changes

| Before | After |
| --- | --- |
| `BaseProvider` exposed `list_model_ids()` plus a metadata adapter. |
`BaseProvider` exposes only abstract `list_model_infos()` returning
application-owned metadata. |
| Ordinary providers parsed IDs and converted them later. | Ordinary
providers parse OpenAI-compatible catalogs directly into
`ProviderModelInfo` values. |
| OpenRouter, Cloudflare, and GitHub Models maintained redundant
IDs-only wrappers. | Provider-specific filters and capability metadata
have one return path. |
| Vertex returned paginated IDs for the base adapter to wrap. | Vertex
returns metadata after completing the same paginated discovery flow. |
| The runtime cache exposed test-only raw-ID write and prefixed-ID read
helpers. | The runtime cache accepts and returns metadata while
retaining its production admin-status ID projection. |
| Provider tests asserted the parallel IDs-only API. | Provider tests
enforce the metadata-only contract and preserve provider-specific
discovery behavior. |
| The package version was `4.11.6`. | The package version is `4.11.7`
with an updated lockfile. |

<!-- greptile_comment -->

<details open><summary><h3>Greptile Summary</h3></summary>

This PR makes provider metadata the only model-catalog contract. The
main changes are:

- Makes `list_model_infos()` the abstract provider discovery API.
- Migrates provider parsers and implementations to `ProviderModelInfo`.
- Removes IDs-only cache and parser helpers.
- Updates provider tests, architecture documentation, and package
metadata.
</details>

<h3>Confidence Score: 4/5</h3>

The catalog migration is consistent, but the release version must
reflect the incompatible API removal.

Repository provider and cache call paths use the new metadata shape
consistently. Existing external consumers of the removed contracts can
fail after a patch upgrade. The repository rules classify incompatible
API removals as a major release.

pyproject.toml and the matching package entry in uv.lock

<details><summary><h3><a href="https://www.greptile.com/trex"><img
alt="T-Rex"
src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg"
height="20" align="absmiddle"></a> T-Rex Logs</h3></summary>

**What T-Rex did**
- The external-consumer compatibility probe was run against both
revisions, confirming the base still supports the legacy provider
contract, while head fails with a TypeError due to the missing
list\_model\_infos method, and runtime metadata shows head at version
4.11.7, indicating the removal is a patch transition rather than a major
change.
- An automated test suite completed successfully with 83 tests passing
in 3.31 seconds.

<a
href="https://app.greptile.com/trex/runs/15207030/artifacts"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifactsDark.svg?v=4"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"><img
alt="View all artifacts"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"></picture></a>

<sub><a href="https://www.greptile.com/trex"><img alt="T-Rex"
src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg"
height="14" align="absmiddle"></a> Ran code and verified through
T-Rex</sub>
</details>

<details open><summary><h3>Important Files Changed</h3></summary>

| Filename | Overview |
|----------|----------|
| src/free_claude_code/providers/base.py | Replaces the IDs-only
provider API with an abstract metadata-only contract. |
| src/free_claude_code/providers/model_listing.py | Consolidates
OpenAI-compatible parsing into ProviderModelInfo results and removes
IDs-only helpers. |
| src/free_claude_code/providers/runtime/model_cache.py | Removes raw-ID
helpers while retaining metadata storage and the admin ID projection. |
| src/free_claude_code/providers/openai_chat/provider.py | Provides the
metadata discovery implementation inherited by ordinary
OpenAI-compatible providers. |
| src/free_claude_code/providers/vertex/client.py | Preserves paginated
discovery while returning metadata values. |
| pyproject.toml | Uses a patch bump for a release that removes callable
and importable contracts. |

</details>

<details open><summary><h3>Flowchart</h3></summary>

<a href="#gh-light-mode-only">

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[Provider catalog endpoint] --> B[list_model_infos]
B --> C[ProviderModelInfo set]
C --> D[Provider model discovery]
D --> E[ProviderModelCache]
E --> F[Metadata-aware catalog]
E --> G[Admin ID projection]
```

</a>
<a href="#gh-dark-mode-only">

```mermaid
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart LR
A[Provider catalog endpoint] --> B[list_model_infos]
B --> C[ProviderModelInfo set]
C --> D[Provider model discovery]
D --> E[ProviderModelCache]
E --> F[Metadata-aware catalog]
E --> G[Admin ID projection]
```

</a>
</details>

<a
href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22alishahryar1%2Ffree-claude-code%22%20on%20the%20existing%20branch%20%22ali%2Fprovider-model-info-contract%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22ali%2Fprovider-model-info-contract%22.%0A%0AFix%20the%20following%201%20code%20review%20issue.%20Work%20through%20them%20one%20at%20a%20time%2C%20proposing%20concise%20fixes.%0A%0A---%0A%0A%23%23%23%20Issue%201%20of%201%0Apyproject.toml%3A7%0A**Breaking%20Contract%20Ships%20as%20Patch**%0A%0AThis%20release%20removes%20%60BaseProvider.list_model_ids%28%29%60%20and%20cache%2Fparser%20methods%20that%20existing%20integrations%20can%20import%20or%20call.%20Such%20consumers%20will%20fail%20with%20%60TypeError%60%2C%20%60AttributeError%60%2C%20or%20%60ImportError%60%20after%20a%20patch%20upgrade%2C%20so%20this%20incompatible%20API%20change%20requires%20a%20major%20version%20bump%20under%20the%20repository's%20versioning%20rules.%0A%0A%60%60%60suggestion%0Aversion%20%3D%20%225.0.0%22%0A%60%60%60%0A%0A&repo=alishahryar1%2Ffree-claude-code&pr=1222&platform=github"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInCodexDark.svg?v=6"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInCodex.svg?v=6"><img
alt="Fix All in Codex"
src="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInCodex.svg?v=6"></picture></a>

<sub>Reviews (1): Last reviewed commit: ["Make ProviderModelInfo the
sole catalog
..."](https://github.com/alishahryar1/free-claude-code/commit/5c543eadc114201a4085d38885a27ac49153ca29)
| [Re-trigger
Greptile](https://app.greptile.com/api/retrigger?id=45869248)</sub>

> Greptile also left **1 inline comment** on this PR.

**Context used:**

- Context used - CLAUDE.md
([source](https://app.greptile.com/alishahryar1/github/Alishahryar1/free-claude-code/-/custom-context?memory=d2fd24d8-0dec-4faf-8ee4-e085e215a2f8))

<!-- /greptile_comment -->
2026-07-21 00:15:35 -07:00

173 行
5.1 KiB
Python

"""Tests for the Kimi Code subscription provider profile."""
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from free_claude_code.application.errors import InvalidRequestError
from free_claude_code.application.model_metadata import ProviderModelInfo
from free_claude_code.config.provider_catalog import KIMI_CODE_DEFAULT_BASE
from free_claude_code.core.anthropic.models import MessagesRequest
from free_claude_code.providers.base import ProviderConfig
from free_claude_code.providers.openai_chat import OpenAIChatProvider
from tests.providers.support import (
immediate_admission,
profiled_provider,
reasoning_for,
)
def _request(**overrides) -> MessagesRequest:
payload = {
"model": "k3",
"messages": [{"role": "user", "content": "Hello"}],
}
payload.update(overrides)
return MessagesRequest.model_validate(payload)
@pytest.fixture
def kimi_code_provider() -> OpenAIChatProvider:
return profiled_provider(
"kimi_code",
ProviderConfig(
api_key="test-subscription-key",
base_url=KIMI_CODE_DEFAULT_BASE,
rate_limit=10,
rate_window=60,
),
admission=immediate_admission(),
)
def test_init_uses_subscription_endpoint_and_identifies_fcc(kimi_code_provider):
assert kimi_code_provider._api_key == "test-subscription-key"
assert kimi_code_provider._base_url == "https://api.kimi.com/coding/v1"
assert kimi_code_provider._provider_name == "KIMI_CODE"
assert kimi_code_provider._client.default_headers["User-Agent"] == (
"free-claude-code"
)
def test_explicit_output_limit_uses_max_completion_tokens(kimi_code_provider):
request = _request(max_tokens=32_768)
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assert body["max_completion_tokens"] == 32_768
assert "max_tokens" not in body
def test_omitted_output_limit_preserves_kimi_default(kimi_code_provider):
request = _request()
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assert "max_tokens" not in body
assert "max_completion_tokens" not in body
assert "reasoning_effort" not in body
@pytest.mark.parametrize(
("client_effort", "upstream_effort"),
[
("minimal", "low"),
("low", "low"),
("medium", "high"),
("high", "high"),
("xhigh", "max"),
("max", "max"),
],
)
def test_named_reasoning_effort_uses_kimi_vocabulary(
kimi_code_provider,
client_effort: str,
upstream_effort: str,
) -> None:
request = _request(output_config={"effort": client_effort})
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assert body["reasoning_effort"] == upstream_effort
def test_enabled_reasoning_without_named_effort_uses_max(kimi_code_provider):
request = _request(thinking={"type": "enabled", "budget_tokens": 4_096})
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assert body["reasoning_effort"] == "max"
def test_disabled_reasoning_uses_none(kimi_code_provider):
request = _request(thinking={"type": "disabled"})
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assert body["reasoning_effort"] == "none"
def test_reasoning_history_uses_reasoning_content(kimi_code_provider):
request = _request(
messages=[
{
"role": "assistant",
"content": [
{"type": "thinking", "thinking": "Inspect the repository."},
{"type": "text", "text": "I found the relevant file."},
],
},
{"role": "user", "content": "Continue."},
]
)
body = kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
assistant = body["messages"][0]
assert assistant["reasoning_content"] == "Inspect the repository."
assert assistant["content"] == "I found the relevant file."
def test_caller_extra_body_is_rejected(kimi_code_provider):
request = _request(extra_body={"thinking": {"type": "enabled"}})
with pytest.raises(InvalidRequestError, match="Kimi Code Chat Completions"):
kimi_code_provider._build_request_body(
request, reasoning=reasoning_for(request)
)
@pytest.mark.asyncio
async def test_model_list_uses_subscription_models_endpoint(kimi_code_provider):
kimi_code_provider._client.models.list = AsyncMock(
return_value=SimpleNamespace(
data=[
SimpleNamespace(id="k3"),
SimpleNamespace(id="kimi-for-coding"),
SimpleNamespace(id="kimi-for-coding-highspeed"),
]
)
)
assert await kimi_code_provider.list_model_infos() == frozenset(
{
ProviderModelInfo("k3"),
ProviderModelInfo("kimi-for-coding"),
ProviderModelInfo("kimi-for-coding-highspeed"),
}
)
kimi_code_provider._client.models.list.assert_awaited_once_with()