mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-28 06:51:16 +08:00
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664