Files
hermes-agent/plugins/model-providers/kimi-coding/__init__.py
Teknium 336b11584c fix: replay prior reasoning to every chat-completions route that can read it
Every model reasons and intends that reasoning to be replayed, but the
chat-completions wire echoed reasoning_content only to DeepSeek/Kimi/MiMo/
Ollama (plus a hardcoded Portal stealth model) and reasoning_details only to
OpenRouter; turn_context popped `reasoning` everywhere, so Ollama's /v1
(reads only `reasoning`) never saw any.

- message_sanitization owns one route->carrier table
  (_REASONING_CARRIER_ROUTES): strict schemas (Mistral, Groq, Cerebras,
  SambaNova) get nothing, Fireworks reasoning_content only, OpenRouter-format
  gateways (OpenRouter, Kilo, Vercel AI Gateway, Nous Portal) all three,
  Novita/MiniMax /v1/TokenHub + reasoning_details, local/Ollama/Upstage +
  reasoning, everyone else reasoning_content.
- The " " pad stays limited to the must-echo tier (DeepSeek, Kimi, MiMo);
  Ollama leaves it (no echo requirement) and the stealth hardcode is gone.
- Reactive recovery: a 4xx unknown-field rejection that NAMES a reasoning
  key this request carried is recorded per (provider, host, model) and the
  retry re-shapes api_messages without it. Generic upstream errors never
  count; a recorded key falls through, so no loop. History is untouched.
- Nous Portal reads reasoning_details again: the #118182 "replay budget"
  does not exist in the Portal and does not reproduce live.
- Gemini thought_signature survives on Gemini hosts/providers even when the
  model id is an alias.
- Vendor keep-reasoning flags (docs-only): GLM thinking.clear_thinking=false,
  DashScope preserve_thinking / clear_thinking, Kimi K2.x thinking.keep=all.
- stale_thinking_reaches_wire follows the same table, so compaction trigger
  and tail walks agree with what is sent.
2026-10-10 19:22:07 -07:00

82 lines
3.8 KiB
Python

"""Kimi / Moonshot provider profiles (chat_completions path; sk-kimi-* keys are
redirected to api.kimi.com/coding by core)."""
from typing import Any
from urllib.parse import urlparse
from agent.reasoning_effort import KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES, thinking_toggle_extras
from hermes_cli.version_info import get_version_info
from providers import register_provider
from providers.base import OMIT_TEMPERATURE, ProviderProfile
_HEADERS = {
"HTTP-Referer": "https://hermes-agent.nousresearch.com",
"X-Title": "Hermes Agent",
"User-Agent": f"HermesAgent/{get_version_info().base_version}",
# Exclude brotli: httpx's brotlicffi backend has a streaming decode bug on
# Moonshot's content-encoding: br SSE responses (#28043, #48428, #59556).
# gzip sidesteps it while still compressing the transfer.
"Accept-Encoding": "gzip",
}
def _is_confirmed_kimi_coding_url(base_url: str) -> bool:
"""True only for Kimi Code's canonical HTTPS API surfaces."""
try:
p = urlparse(base_url)
port = p.port
except ValueError:
return False
return (
p.scheme.lower() == "https" and (p.hostname or "").lower() == "api.kimi.com" and port in (None, 443)
and p.username is None and p.password is None
and p.path.rstrip("/") in {"/coding", "/coding/v1"} and not p.query and not p.fragment
)
class KimiProfile(ProviderProfile):
"""Kimi/Moonshot — temperature omitted, thinking xor reasoning_effort."""
def fetch_models(
self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
) -> list[str] | None:
"""Use Kimi Code's OpenAI-compatible surface for model discovery; the bare
``k3`` slug is only served there, so it is filtered off other endpoints."""
effective_base = (base_url or self.base_url or "").rstrip("/")
confirmed_coding_endpoint = _is_confirmed_kimi_coding_url(effective_base)
if confirmed_coding_endpoint and urlparse(effective_base).path.rstrip("/") == "/coding":
effective_base += "/v1"
models = super().fetch_models(api_key=api_key, base_url=effective_base or None, timeout=timeout)
if models is None or confirmed_coding_endpoint:
return models
return [model for model in models if model.strip().lower() != "k3"]
def build_api_kwargs_extras(
self, *, reasoning_config: dict | None = None, model: str | None = None, **context
) -> tuple[dict[str, Any], dict[str, Any]]:
"""Moonshot treats extra_body.thinking and reasoning_effort as mutually
exclusive (400 on both): send effort when requested, else the toggle. K2.x keeps
replayed prior-turn reasoning_content only with ``thinking.keep: "all"`` (default null
ignores it; platform.kimi.ai/docs/guide/use-thinking-models); K3 always keeps it."""
extra_body, top_level = thinking_toggle_extras(reasoning_config, KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES)
thinking = extra_body.get("thinking")
if isinstance(thinking, dict) and thinking.get("type") == "enabled" and "k2" in (model or "").lower():
extra_body["thinking"] = {**thinking, "keep": "all"}
return extra_body, top_level
def _kimi(name: str, aliases: tuple, env_vars: tuple, base_url: str) -> KimiProfile:
return KimiProfile(
name=name, aliases=aliases, env_vars=env_vars, base_url=base_url,
fixed_temperature=OMIT_TEMPERATURE, default_max_tokens=32000,
default_headers=dict(_HEADERS), default_aux_model="kimi-k2-turbo-preview",
)
kimi = _kimi("kimi-coding", ("kimi", "moonshot", "kimi-for-coding"), ("KIMI_API_KEY", "KIMI_CODING_API_KEY"),
"https://api.moonshot.ai/v1")
kimi_cn = _kimi("kimi-coding-cn", ("kimi-cn", "moonshot-cn"), ("KIMI_CN_API_KEY",), "https://api.moonshot.cn/v1")
register_provider(kimi)
register_provider(kimi_cn)