__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """CommandCode provider profiles: ``commandcode`` (chat_completions) and
  2. ``commandcode-anthropic`` (anthropic_messages, Bearer auth — see
  3. ``agent/anthropic_adapter.py``). Same key and base URL for both."""
  4. import json
  5. import logging
  6. import urllib.request
  7. from hermes_cli.urllib_security import open_credentialed_url
  8. from providers import get_provider_profile, register_provider
  9. from providers.base import ProviderProfile, _profile_user_agent
  10. logger = logging.getLogger(__name__)
  11. _COMMANDCODE_BASE = "https://api.commandcode.ai/provider/v1"
  12. _COMMANDCODE_MODELS_URL = f"{_COMMANDCODE_BASE}/models"
  13. class CommandCodeProfile(ProviderProfile):
  14. """CommandCode — OpenAI-compatible chat completions endpoint."""
  15. def fetch_models(
  16. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
  17. ) -> list[str] | None:
  18. """Public (unauthenticated) /models endpoint. The picker passes base_url
  19. unconditionally, so only a value differing from the default is a custom endpoint."""
  20. caller_base = (base_url or "").strip().rstrip("/")
  21. custom = caller_base and caller_base != _COMMANDCODE_BASE
  22. models_url = caller_base + "/models" if custom else _COMMANDCODE_MODELS_URL
  23. try:
  24. req = urllib.request.Request(models_url)
  25. req.add_header("Accept", "application/json")
  26. req.add_header("User-Agent", _profile_user_agent())
  27. with open_credentialed_url(req, timeout=timeout) as resp:
  28. data = json.loads(resp.read().decode())
  29. return [m["id"] for m in data.get("data", []) if isinstance(m, dict) and "id" in m]
  30. except Exception as exc:
  31. logger.debug("fetch_models(commandcode): %s", exc)
  32. return None
  33. def build_api_kwargs_extras(
  34. self, *, reasoning_config: dict | None = None, model: str | None = None, **context
  35. ) -> tuple[dict, dict]:
  36. """DeepSeek ids (``deepseek/deepseek-v4-flash``) get the native DeepSeek wire
  37. controls: DeepSeek V4+ defaults to thinking when ``thinking`` is omitted, so
  38. without them ``/reasoning`` never reaches the request (#95232). Other model
  39. families stay a no-op — CommandCode declares no reasoning vocabulary for them."""
  40. m = (model or "").strip()
  41. if not m.lower().startswith("deepseek/"):
  42. return {}, {}
  43. # Registry lookup, not a module import: the deepseek shim is only a loader-injected
  44. # sys.modules entry, and the registry honours a user override of the profile.
  45. native = get_provider_profile("deepseek")
  46. if native is None:
  47. return {}, {}
  48. return native.build_api_kwargs_extras(
  49. reasoning_config=reasoning_config, model=m.split("/", 1)[1], **context,
  50. )
  51. class CommandCodeAnthropicProfile(CommandCodeProfile):
  52. """CommandCode — Anthropic Messages API-compatible endpoint."""
  53. def fetch_models(
  54. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
  55. ) -> list[str] | None:
  56. """Public /models endpoint, filtered to Anthropic-family models."""
  57. all_models = super().fetch_models(api_key=api_key, base_url=base_url, timeout=timeout)
  58. return None if all_models is None else [m for m in all_models if m.startswith("claude-")]
  59. commandcode = CommandCodeProfile(
  60. name="commandcode", aliases=("commandcode-chat",), api_mode="chat_completions",
  61. # Same key as the anthropic profile; distinct base-URL override vars so each
  62. # profile renders its own card on the desktop Keys tab (rows keyed by env var).
  63. env_vars=("COMMANDCODE_API_KEY", "COMMANDCODE_BASE_URL"),
  64. display_name="CommandCode", description="CommandCode — 20+ models via OpenAI-compatible API",
  65. signup_url="https://commandcode.ai/", base_url=_COMMANDCODE_BASE, models_url=_COMMANDCODE_MODELS_URL,
  66. fallback_models=(
  67. "deepseek/deepseek-v4-pro", "deepseek/deepseek-v4-flash", "Qwen/Qwen3.7-Max", "Qwen/Qwen3.6-Plus",
  68. "moonshotai/Kimi-K2.6", "zai-org/GLM-5.1", "MiniMaxAI/MiniMax-M2.7", "stepfun/Step-3.5-Flash",
  69. "xiaomi/mimo-v2.5-pro", "google/gemini-3.5-flash", "gpt-5.5",
  70. ),
  71. default_aux_model="deepseek/deepseek-v4-flash",
  72. )
  73. commandcode_anthropic = CommandCodeAnthropicProfile(
  74. name="commandcode-anthropic", aliases=("commandcode-claude",), api_mode="anthropic_messages",
  75. env_vars=("COMMANDCODE_API_KEY", "COMMANDCODE_ANTHROPIC_BASE_URL"),
  76. display_name="CommandCode (Anthropic)",
  77. description="CommandCode — Claude models via Anthropic Messages API",
  78. signup_url="https://commandcode.ai/", base_url=_COMMANDCODE_BASE, models_url=_COMMANDCODE_MODELS_URL,
  79. fallback_models=("claude-sonnet-4-6", "claude-opus-4-7", "claude-haiku-4-5-20251001"),
  80. default_aux_model="claude-haiku-4-5-20251001",
  81. )
  82. register_provider(commandcode)
  83. register_provider(commandcode_anthropic)