diff --git a/app/agent/llm/catalog.py b/app/agent/llm/catalog.py index 50da380d7..8194564f0 100644 --- a/app/agent/llm/catalog.py +++ b/app/agent/llm/catalog.py @@ -431,6 +431,14 @@ class _ProviderCatalog: api_key_hint="填写 Obscurify.ai API Key(sk-obs1_ 开头),在 https://obscurify.ai/?utm_source=moviepilot 创建账户后,于 Account 页面点击 Show API Key 获取。", description="Obscurify.ai 注重隐私的 OpenAI-compatible 聚合模型平台:账户为随机 16 位账号,无需邮箱,服务端不保存提示词与回复。", ), + openai_provider( + provider_id="cheapaimodel", + name="cheapaimodel", + default_base_url="https://api.cheapaimodel.ai/v1", + sort_order=60, + api_key_hint="填写 cheapaimodel API Key(sk- 开头),可在 https://console.cheapaimodel.ai/keys 创建。", + description="cheapaimodel OpenAI-compatible 模型网关,提供 DeepSeek、GLM、Kimi 与 Grok 等模型,按 token 从预充值美元余额扣费。kimi-k3 仅接受 temperature 1 或 0.6。", + ), ProviderSpec( id="github-copilot", name="GitHub Copilot", @@ -448,7 +456,7 @@ class _ProviderCatalog: ), model_list_strategy="github_copilot", description="通过 GitHub Copilot 订阅接入。", - sort_order=60, + sort_order=61, ), catalog_openai_provider( provider_id="github-models", diff --git a/tests/test_llm_provider_cheapaimodel.py b/tests/test_llm_provider_cheapaimodel.py new file mode 100644 index 000000000..575ad6fd9 --- /dev/null +++ b/tests/test_llm_provider_cheapaimodel.py @@ -0,0 +1,60 @@ +"""cheapaimodel 预设通过共享 OpenAI 兼容通道发现模型的边界回归。""" + +from unittest.mock import AsyncMock + +import httpx +import pytest + +from app.agent.llm.provider import LLMProviderManager +from app.foundation.singleton import Singleton + + +@pytest.fixture +def manager(monkeypatch): + """隔离管理器单例及目录缓存,防止测试影响其他 provider。""" + key = (LLMProviderManager, (), frozenset()) + monkeypatch.setattr(Singleton, "_instances", {k: v for k, v in Singleton._instances.items() if k != key}) + instance = LLMProviderManager() + monkeypatch.setattr(instance, "get_models_dev_data", AsyncMock(return_value={})) + return instance + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "base_url, expected_url", + [ + (None, "https://api.cheapaimodel.ai/v1/models"), + ("https://relay.example/v1", "https://relay.example/v1/models"), + ], +) +async def test_cheapaimodel_discovers_authenticated_models(manager, monkeypatch, base_url, expected_url): + """预设及覆盖地址都传递密钥,原样保留模型 ID,不推断缺失的上下文元数据。""" + requests = [] + + def respond(request): + requests.append(request) + return httpx.Response( + 200, + json={ + "object": "list", + "data": [ + {"id": "deepseek-v4-1-flash", "object": "model", "created": 0}, + {"id": "glm-5.3", "object": "model", "created": 0}, + ], + }, + ) + + monkeypatch.setattr( + manager, + "_build_sdk_http_client", + lambda use_proxy: httpx.AsyncClient(transport=httpx.MockTransport(respond)), + ) + + models = await manager.list_models("cheapaimodel", api_key="test-cheapaimodel-key", base_url=base_url) + + assert len(requests) == 1 + assert requests[0].method == "GET" + assert str(requests[0].url) == expected_url + assert requests[0].headers["Authorization"] == "Bearer test-cheapaimodel-key" + assert [model["id"] for model in models] == ["deepseek-v4-1-flash", "glm-5.3"] + assert all(model["source"] == "provider" and model["context_tokens"] is None for model in models)