| 182 | ) |
| 183 | |
| 184 | def query(self, messages: list[dict[str, str]], **kwargs) -> dict: |
| 185 | for attempt in retry(logger=logger, abort_exceptions=self.abort_exceptions): |
| 186 | with attempt: |
| 187 | response = self._query(messages, **kwargs) |
| 188 | actions = self._parse_actions(response) |
| 189 | cost_output = self._calculate_cost(response) |
| 190 | GLOBAL_MODEL_STATS.add(cost_output["cost"]) |
| 191 | content = [] |
| 192 | for block in response.content: |
| 193 | d = block.model_dump() |
| 194 | if block.type == "tool_use": |
| 195 | d["text"] = f"```\n{block.input.get('command', json.dumps(block.input))}\n```" |
| 196 | content.append(d) |
| 197 | return { |
| 198 | "role": "assistant", |
| 199 | "content": content, |
| 200 | "extra": { |
| 201 | "actions": actions, |
| 202 | "response": response.model_dump(), |
| 203 | **cost_output, |
| 204 | "timestamp": time.time(), |
| 205 | }, |
| 206 | } |
| 207 | |
| 208 | def _parse_actions(self, response: anthropic.types.Message) -> list[dict]: |
| 209 | tool_calls = [ |