MCPcopy Create free account
hub / github.com/CodeClash-ai/CodeClash / AnthropicModel

Class AnthropicModel

codeclash/agents/mini_anthropic_model.py:104–287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102
103
104class AnthropicModel:
105 abort_exceptions: list[type[Exception]] = [
106 anthropic.BadRequestError,
107 anthropic.AuthenticationError,
108 anthropic.PermissionDeniedError,
109 anthropic.NotFoundError,
110 KeyboardInterrupt,
111 ]
112
113 def __init__(self, *, config_class: type = AnthropicModelConfig, **kwargs):
114 self.config = config_class(**kwargs)
115 # Resolve endpoint-specific values from the environment so they stay out of configs.
116 if self.config.model_name_env:
117 model_name = os.getenv(self.config.model_name_env, "")
118 if not model_name:
119 raise ValueError(f"Set the {self.config.model_name_env} environment variable to the model name.")
120 self.config.model_name = model_name
121 if self.config.base_url_env:
122 base_url = os.getenv(self.config.base_url_env, "")
123 if not base_url:
124 raise ValueError(f"Set the {self.config.base_url_env} environment variable to the API base URL.")
125 self.config.base_url = base_url
126 api_key = os.getenv(self.config.api_key_env, "")
127 if not api_key:
128 raise ValueError(f"API key not found. Set the {self.config.api_key_env} environment variable.")
129 client_kwargs: dict[str, Any] = {"api_key": api_key}
130 if self.config.base_url:
131 client_kwargs["base_url"] = self.config.base_url
132 self.client = anthropic.Anthropic(**client_kwargs)
133
134 @staticmethod
135 def _set_cache_control_on_last_message(messages: list[dict]) -> list[dict]:
136 """Add cache_control to the last block of the last message."""
137 import copy
138
139 messages = copy.deepcopy(messages)
140 if not messages:
141 return messages
142 last = messages[-1]
143 content = last["content"]
144 if content is None:
145 last["cache_control"] = {"type": "ephemeral"}
146 elif isinstance(content, str):
147 last["content"] = [{"type": "text", "text": content, "cache_control": {"type": "ephemeral"}}]
148 else:
149 content[-1]["cache_control"] = {"type": "ephemeral"}
150 return messages
151
152 @staticmethod
153 def _strip_display_text(content: list[dict] | str) -> list[dict] | str:
154 """Remove the 'text' key we add to non-text blocks for interactive display
155 and 'caller' (None) that model_dump() emits from newer SDK versions."""
156 if not isinstance(content, list):
157 return content
158 return [
159 {
160 k: v
161 for k, v in block.items()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected