anthropic.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """Anthropic Messages API transport: conversion via agent/anthropic_adapter.py, normalization here."""
  2. from typing import Any, Dict, List, Optional
  3. from agent.transports.base import ProviderTransport
  4. from agent.transports.types import NormalizedResponse, ToolCall
  5. _MCP_PREFIX = "mcp__"
  6. _THINKING_TYPES = ("thinking", "redacted_thinking")
  7. def _unprefix_oauth_tool_name(name: str) -> str:
  8. """Reverse the OAuth-wire ``mcp__`` prefix back to the registered tool name.
  9. Two originals map onto one wire name (``read_file`` / ``mcp_linear_get_issue``), so
  10. resolve by registry lookup, never rewriting a name that already resolves natively.
  11. OAuth wire aliases are checked LAST so a real tool under the wire name still wins."""
  12. from agent.anthropic_adapter import _OAUTH_TOOL_NAME_REVERSE_ALIASES
  13. from tools.registry import registry as _tool_registry
  14. bare = name[len(_MCP_PREFIX):]
  15. for candidate in (name, "mcp_" + bare, bare):
  16. if _tool_registry.get_entry(candidate):
  17. return candidate
  18. return _OAUTH_TOOL_NAME_REVERSE_ALIASES.get(bare, name)
  19. # build_kwargs params forwarded to build_anthropic_kwargs, with the defaults applied when absent.
  20. _BUILD_KWARG_DEFAULTS = {
  21. "max_tokens": 16384, "reasoning_config": None, "tool_choice": None, "is_oauth": False, "preserve_dots": False,
  22. "context_length": None, "base_url": None, "fast_mode": False, "drop_context_1m_beta": False,
  23. }
  24. class AnthropicTransport(ProviderTransport):
  25. """Transport for api_mode='anthropic_messages'."""
  26. _STOP_REASON_MAP = {
  27. "end_turn": "stop", "tool_use": "tool_calls", "max_tokens": "length", "stop_sequence": "stop",
  28. "refusal": "content_filter", "model_context_window_exceeded": "length",
  29. }
  30. @property
  31. def api_mode(self) -> str:
  32. return "anthropic_messages"
  33. def convert_messages(self, messages: list[dict[str, Any]], **kwargs) -> Any:
  34. """Convert OpenAI messages to an Anthropic (system, messages) tuple; ``base_url`` and ``model`` pick the thinking-replay policy."""
  35. from agent.anthropic_message_convert import convert_messages_to_anthropic
  36. return convert_messages_to_anthropic(messages, base_url=kwargs.get("base_url"), model=kwargs.get("model"))
  37. def convert_tools(self, tools: list[dict[str, Any]]) -> Any:
  38. """Convert OpenAI tool schemas to Anthropic input_schema format."""
  39. from agent.anthropic_message_convert import convert_tools_to_anthropic
  40. return convert_tools_to_anthropic(tools)
  41. def build_kwargs(
  42. self, model: str, messages: list[dict[str, Any]], tools: Optional[list[dict[str, Any]]] = None, **params,
  43. ) -> dict[str, Any]:
  44. """Build Anthropic messages.create() kwargs (converts messages and tools internally)."""
  45. from agent.anthropic_adapter import build_anthropic_kwargs
  46. return build_anthropic_kwargs(
  47. model=model, messages=messages, tools=tools,
  48. **{key: params.get(key, default) for key, default in _BUILD_KWARG_DEFAULTS.items()},
  49. )
  50. def normalize_response(self, response: Any, **kwargs) -> NormalizedResponse:
  51. """Parse content blocks (text/thinking/tool_use), map stop_reason, collect reasoning_details."""
  52. import json
  53. from agent.anthropic_message_convert import _sanitize_replay_block, _to_plain_data
  54. strip_tool_prefix = kwargs.get("strip_tool_prefix", False)
  55. text_parts, reasoning_parts, reasoning_details, tool_calls = [], [], [], []
  56. # Anthropic signs each thinking block against the blocks PRECEDING it; when thinking
  57. # interleaves with tool_use the parallel lists lose that order and replay -> HTTP 400.
  58. ordered_blocks = []
  59. for block in response.content:
  60. block_dict = _to_plain_data(block)
  61. # Sanitize at capture so output-only SDK fields never persist and replay (400).
  62. clean_block = _sanitize_replay_block(block_dict) if isinstance(block_dict, dict) else None
  63. if clean_block is not None:
  64. ordered_blocks.append(clean_block)
  65. if block.type == "text":
  66. text_parts.append(block.text)
  67. elif block.type in _THINKING_TYPES:
  68. if block.type == "thinking":
  69. reasoning_parts.append(block.thinking)
  70. detail = clean_block if clean_block is not None else block_dict # raw only if sanitize dropped it
  71. if isinstance(detail, dict):
  72. reasoning_details.append(detail)
  73. elif block.type == "tool_use":
  74. name = block.name
  75. if strip_tool_prefix and name.startswith(_MCP_PREFIX):
  76. name = _unprefix_oauth_tool_name(name)
  77. tool_calls.append(ToolCall(id=block.id, name=name, arguments=json.dumps(block.input)))
  78. provider_data = {"reasoning_details": reasoning_details} if reasoning_details else {}
  79. stop_details = _to_plain_data(getattr(response, "stop_details", None))
  80. if stop_details is not None:
  81. provider_data["stop_details"] = stop_details
  82. # Ordered channel only for the shape the parallel lists reconstruct wrongly.
  83. signed = any(b.get("type") in _THINKING_TYPES and (b.get("signature") or b.get("data")) for b in ordered_blocks)
  84. if signed and any(b.get("type") == "tool_use" for b in ordered_blocks):
  85. provider_data["anthropic_content_blocks"] = ordered_blocks
  86. return NormalizedResponse(
  87. content="\n".join(text_parts) if text_parts else None, tool_calls=tool_calls or None,
  88. finish_reason=self.response_finish_reason(response),
  89. reasoning="\n\n".join(reasoning_parts) if reasoning_parts else None, usage=None,
  90. provider_data=provider_data or None,
  91. )
  92. def response_finish_reason(self, response: Any) -> str:
  93. """``stop_reason`` mapped to the OpenAI vocabulary. Bedrock InvokeModel guardrail blocks keep
  94. ``stop_reason=end_turn`` and hand back the guardrail's canned text as an ordinary reply; they
  95. must surface as ``content_filter`` so the loop treats them as a refusal, not model output."""
  96. from agent.bedrock_adapter import anthropic_response_guardrail_intervened
  97. if anthropic_response_guardrail_intervened(response):
  98. return "content_filter"
  99. return self.map_finish_reason(response.stop_reason)
  100. def validate_response(self, response: Any) -> bool:
  101. """Structural check; empty content is legitimate for ``end_turn``/``refusal`` (retrying
  102. either would loop forever)."""
  103. content_blocks = getattr(response, "content", None)
  104. return isinstance(content_blocks, list) and (
  105. bool(content_blocks) or getattr(response, "stop_reason", None) in {"end_turn", "refusal"}
  106. )
  107. def extract_cache_stats(self, response: Any) -> Optional[dict[str, int]]:
  108. """Anthropic cache_read / cache_creation token counts."""
  109. usage = getattr(response, "usage", None)
  110. if usage is None:
  111. return None
  112. cached = getattr(usage, "cache_read_input_tokens", 0) or 0
  113. written = getattr(usage, "cache_creation_input_tokens", 0) or 0
  114. return {"cached_tokens": cached, "creation_tokens": written} if cached or written else None
  115. from agent.transports import register_transport
  116. register_transport("anthropic_messages", AnthropicTransport)