mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-08 03:01:47 +00:00
Introduces providers/ package — single source of truth for every inference provider. Adding a simple api-key provider now requires one providers/<name>.py file with zero edits anywhere else. What this PR ships: - providers/ package (ProviderProfile ABC + 33 profiles across 4 api_modes) - ProviderProfile declarative fields: name, api_mode, aliases, display_name, env_vars, base_url, models_url, auth_type, fallback_models, hostname, default_headers, fixed_temperature, default_max_tokens, default_aux_model - 4 overridable hooks: prepare_messages, build_extra_body, build_api_kwargs_extras, fetch_models - chat_completions.build_kwargs: profile path via _build_kwargs_from_profile, legacy flag path retained for lmstudio/tencent-tokenhub (which have session-aware reasoning probing that doesn't map cleanly to hooks yet) - run_agent.py: profile path for all registered providers; legacy path variable scoping fixed (all flags defined before branching) - Auto-wires: auth.PROVIDER_REGISTRY, models.CANONICAL_PROVIDERS, doctor health checks, config.OPTIONAL_ENV_VARS, model_metadata._URL_TO_PROVIDER - GeminiProfile: thinking_config translation (native + openai-compat nested) - New tests/providers/ (79 tests covering profile declarations, transport parity, hook overrides, e2e kwargs assembly) Deltas vs original PR (salvaged onto current main): - Added profiles: alibaba-coding-plan, azure-foundry, minimax-oauth (were added to main since original PR) - Skipped profiles: lmstudio, tencent-tokenhub stay on legacy path (their reasoning_effort probing has no clean hook equivalent yet) - Removed lmstudio alias from custom profile (it's a separate provider now) - Skipped openrouter/custom from PROVIDER_REGISTRY auto-extension (resolve_provider special-cases them; adding breaks runtime resolution) - runtime_provider: profile.api_mode only as fallback when URL detection finds nothing (was breaking minimax /v1 override) - Preserved main's legacy-path improvements: deepseek reasoning_content preserve, gemini Gemma skip, OpenRouter response caching, Anthropic 1M beta recovery, etc. - Kept agent/copilot_acp_client.py in place (rejected PR's relocation — main has 7 fixes landed since; relocation would revert them) - _API_KEY_PROVIDER_AUX_MODELS alias kept for backward compat with existing test imports Co-authored-by: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Closes #14418
203 lines
7.3 KiB
Python
203 lines
7.3 KiB
Python
"""Tests for the provider module registry and profiles."""
|
|
|
|
import pytest
|
|
from providers import get_provider_profile, _REGISTRY
|
|
from providers.base import ProviderProfile, OMIT_TEMPERATURE
|
|
|
|
|
|
class TestRegistry:
|
|
def test_discovery_populates_registry(self):
|
|
p = get_provider_profile("nvidia")
|
|
assert p is not None
|
|
assert p.name == "nvidia"
|
|
|
|
def test_alias_lookup(self):
|
|
assert get_provider_profile("kimi").name == "kimi-coding"
|
|
assert get_provider_profile("moonshot").name == "kimi-coding"
|
|
assert get_provider_profile("kimi-coding-cn").name == "kimi-coding-cn"
|
|
assert get_provider_profile("or").name == "openrouter"
|
|
assert get_provider_profile("nous-portal").name == "nous"
|
|
assert get_provider_profile("qwen").name == "qwen-oauth"
|
|
assert get_provider_profile("qwen-portal").name == "qwen-oauth"
|
|
|
|
def test_unknown_provider_returns_none(self):
|
|
assert get_provider_profile("nonexistent-provider") is None
|
|
|
|
def test_all_providers_have_name(self):
|
|
get_provider_profile("nvidia") # trigger discovery
|
|
for name, profile in _REGISTRY.items():
|
|
assert profile.name == name
|
|
|
|
|
|
class TestNvidiaProfile:
|
|
def test_max_tokens(self):
|
|
p = get_provider_profile("nvidia")
|
|
assert p.default_max_tokens == 16384
|
|
|
|
def test_no_special_temperature(self):
|
|
p = get_provider_profile("nvidia")
|
|
assert p.fixed_temperature is None
|
|
|
|
def test_base_url(self):
|
|
p = get_provider_profile("nvidia")
|
|
assert "nvidia.com" in p.base_url
|
|
|
|
|
|
class TestKimiProfile:
|
|
def test_temperature_omit(self):
|
|
p = get_provider_profile("kimi")
|
|
assert p.fixed_temperature is OMIT_TEMPERATURE
|
|
|
|
def test_max_tokens(self):
|
|
p = get_provider_profile("kimi")
|
|
assert p.default_max_tokens == 32000
|
|
|
|
def test_cn_separate_profile(self):
|
|
p = get_provider_profile("kimi-coding-cn")
|
|
assert p.name == "kimi-coding-cn"
|
|
assert p.env_vars == ("KIMI_CN_API_KEY",)
|
|
assert "moonshot.cn" in p.base_url
|
|
|
|
def test_cn_not_alias_of_kimi(self):
|
|
kimi = get_provider_profile("kimi-coding")
|
|
cn = get_provider_profile("kimi-coding-cn")
|
|
assert kimi is not cn
|
|
assert kimi.base_url != cn.base_url
|
|
|
|
def test_thinking_enabled(self):
|
|
p = get_provider_profile("kimi")
|
|
eb, tl = p.build_api_kwargs_extras(reasoning_config={"enabled": True, "effort": "high"})
|
|
assert eb["thinking"] == {"type": "enabled"}
|
|
assert tl["reasoning_effort"] == "high"
|
|
|
|
def test_thinking_disabled(self):
|
|
p = get_provider_profile("kimi")
|
|
eb, tl = p.build_api_kwargs_extras(reasoning_config={"enabled": False})
|
|
assert eb["thinking"] == {"type": "disabled"}
|
|
assert "reasoning_effort" not in tl
|
|
|
|
def test_reasoning_effort_default(self):
|
|
p = get_provider_profile("kimi")
|
|
eb, tl = p.build_api_kwargs_extras(reasoning_config={"enabled": True})
|
|
assert tl["reasoning_effort"] == "medium"
|
|
|
|
def test_no_config_defaults(self):
|
|
p = get_provider_profile("kimi")
|
|
eb, tl = p.build_api_kwargs_extras(reasoning_config=None)
|
|
assert eb["thinking"] == {"type": "enabled"}
|
|
assert tl["reasoning_effort"] == "medium"
|
|
|
|
|
|
class TestOpenRouterProfile:
|
|
def test_extra_body_with_prefs(self):
|
|
p = get_provider_profile("openrouter")
|
|
body = p.build_extra_body(provider_preferences={"allow": ["anthropic"]})
|
|
assert body["provider"] == {"allow": ["anthropic"]}
|
|
|
|
def test_extra_body_no_prefs(self):
|
|
p = get_provider_profile("openrouter")
|
|
body = p.build_extra_body()
|
|
assert body == {}
|
|
|
|
def test_reasoning_full_config(self):
|
|
p = get_provider_profile("openrouter")
|
|
eb, _ = p.build_api_kwargs_extras(
|
|
reasoning_config={"enabled": True, "effort": "high"},
|
|
supports_reasoning=True,
|
|
)
|
|
assert eb["reasoning"] == {"enabled": True, "effort": "high"}
|
|
|
|
def test_reasoning_disabled_still_passes(self):
|
|
"""OpenRouter passes disabled reasoning through (unlike Nous)."""
|
|
p = get_provider_profile("openrouter")
|
|
eb, _ = p.build_api_kwargs_extras(
|
|
reasoning_config={"enabled": False},
|
|
supports_reasoning=True,
|
|
)
|
|
assert eb["reasoning"] == {"enabled": False}
|
|
|
|
def test_default_reasoning(self):
|
|
p = get_provider_profile("openrouter")
|
|
eb, _ = p.build_api_kwargs_extras(supports_reasoning=True)
|
|
assert eb["reasoning"] == {"enabled": True, "effort": "medium"}
|
|
|
|
|
|
class TestNousProfile:
|
|
def test_tags(self):
|
|
p = get_provider_profile("nous")
|
|
body = p.build_extra_body()
|
|
assert body["tags"] == ["product=hermes-agent"]
|
|
|
|
def test_auth_type(self):
|
|
p = get_provider_profile("nous")
|
|
assert p.auth_type == "oauth_device_code"
|
|
|
|
def test_reasoning_enabled(self):
|
|
p = get_provider_profile("nous")
|
|
eb, _ = p.build_api_kwargs_extras(
|
|
reasoning_config={"enabled": True, "effort": "medium"},
|
|
supports_reasoning=True,
|
|
)
|
|
assert eb["reasoning"] == {"enabled": True, "effort": "medium"}
|
|
|
|
def test_reasoning_omitted_when_disabled(self):
|
|
p = get_provider_profile("nous")
|
|
eb, _ = p.build_api_kwargs_extras(
|
|
reasoning_config={"enabled": False},
|
|
supports_reasoning=True,
|
|
)
|
|
assert "reasoning" not in eb
|
|
|
|
|
|
class TestQwenProfile:
|
|
def test_max_tokens(self):
|
|
p = get_provider_profile("qwen-oauth")
|
|
assert p.default_max_tokens == 65536
|
|
|
|
def test_auth_type(self):
|
|
p = get_provider_profile("qwen-oauth")
|
|
assert p.auth_type == "oauth_external"
|
|
|
|
def test_extra_body_vl(self):
|
|
p = get_provider_profile("qwen-oauth")
|
|
body = p.build_extra_body()
|
|
assert body["vl_high_resolution_images"] is True
|
|
|
|
def test_prepare_messages_normalizes_content(self):
|
|
p = get_provider_profile("qwen-oauth")
|
|
msgs = [
|
|
{"role": "system", "content": "Be helpful"},
|
|
{"role": "user", "content": "hello"},
|
|
]
|
|
result = p.prepare_messages(msgs)
|
|
# System message: content normalized to list, cache_control on last part
|
|
assert isinstance(result[0]["content"], list)
|
|
assert result[0]["content"][-1].get("cache_control") == {"type": "ephemeral"}
|
|
assert result[0]["content"][-1]["text"] == "Be helpful"
|
|
# User message: content normalized to list
|
|
assert isinstance(result[1]["content"], list)
|
|
assert result[1]["content"][0]["text"] == "hello"
|
|
|
|
def test_metadata_top_level(self):
|
|
p = get_provider_profile("qwen-oauth")
|
|
meta = {"sessionId": "s123", "promptId": "p456"}
|
|
eb, tl = p.build_api_kwargs_extras(qwen_session_metadata=meta)
|
|
assert tl["metadata"] == meta
|
|
assert "metadata" not in eb
|
|
|
|
|
|
class TestBaseProfile:
|
|
def test_prepare_messages_passthrough(self):
|
|
p = ProviderProfile(name="test")
|
|
msgs = [{"role": "user", "content": "hi"}]
|
|
assert p.prepare_messages(msgs) is msgs
|
|
|
|
def test_build_extra_body_empty(self):
|
|
p = ProviderProfile(name="test")
|
|
assert p.build_extra_body() == {}
|
|
|
|
def test_build_api_kwargs_extras_empty(self):
|
|
p = ProviderProfile(name="test")
|
|
eb, tl = p.build_api_kwargs_extras()
|
|
assert eb == {}
|
|
assert tl == {}
|