refactor(agent_init): reuse anthropic_adapter._TOOL_STREAMING_BETA

_FINE_GRAINED_BETA duplicated the identical beta string; import the
adapter constant lazily (same direction agent_init already imports
build_anthropic_client). Header output byte-identical.
This commit is contained in:
kshitijk4poor
2026-09-14 20:50:48 +05:30
committed by kshitij
parent e6e4213dbf
commit afeaabf629

View File

@@ -877,20 +877,19 @@ def _routed_client_kwargs(agent, fallback_model, _provider_timeout) -> Dict[str,
)
_FINE_GRAINED_BETA = "fine-grained-tool-streaming-2025-05-14"
def _apply_openai_header_policy(agent, client_kwargs: Dict[str, Any]) -> None:
"""Mutate ``client_kwargs`` (== ``agent._client_kwargs``) with header/TLS policy, in order:
OpenRouter Claude beta header → model.default_headers → custom-provider TLS/extra_headers."""
# Fine-grained tool streaming for Claude on OpenRouter: without the beta header
# Anthropic buffers the whole tool call and OpenRouter's proxy times out.
from agent.anthropic_adapter import _TOOL_STREAMING_BETA
_effective_base = str(client_kwargs.get("base_url", "")).lower()
if base_url_host_matches(_effective_base, "openrouter.ai") and "claude" in (agent.model or "").lower():
headers = client_kwargs.get("default_headers") or {}
existing_beta = headers.get("x-anthropic-beta", "")
if _FINE_GRAINED_BETA not in existing_beta:
headers["x-anthropic-beta"] = ",".join(filter(None, (existing_beta, _FINE_GRAINED_BETA)))
if _TOOL_STREAMING_BETA not in existing_beta:
headers["x-anthropic-beta"] = ",".join(filter(None, (existing_beta, _TOOL_STREAMING_BETA)))
client_kwargs["default_headers"] = headers
# model.default_headers override provider/SDK defaults (WAFs rejecting SDK headers).
agent._apply_user_default_headers()