Extract tool_use blocks and text blocks from the API response.
(self, response: Any)
| 94 | ) |
| 95 | |
| 96 | def _parse_response(self, response: Any) -> tuple[list[dict], list[str]]: |
| 97 | """Extract tool_use blocks and text blocks from the API response.""" |
| 98 | tool_calls: list[dict] = [] |
| 99 | text_parts: list[str] = [] |
| 100 | |
| 101 | for block in response.content: |
| 102 | if block.type == "text": |
| 103 | text_parts.append(block.text) |
| 104 | sys.stdout.write(block.text) |
| 105 | sys.stdout.flush() |
| 106 | elif block.type == "tool_use": |
| 107 | tool_calls.append({ |
| 108 | "id": block.id, |
| 109 | "name": block.name, |
| 110 | "input": block.input, |
| 111 | }) |
| 112 | sys.stdout.write(f"\n[Tool: {block.name}] ") |
| 113 | _input_preview = str(block.input) |
| 114 | if len(_input_preview) > 120: |
| 115 | _input_preview = _input_preview[:120] + "..." |
| 116 | sys.stdout.write(f"{_input_preview}\n") |
| 117 | sys.stdout.flush() |
| 118 | |
| 119 | return tool_calls, text_parts |
| 120 | |
| 121 | def _execute_tool_calls(self, tool_calls: list[dict]) -> None: |
| 122 | """Execute each tool call through the permission gate, append results.""" |