(messages: list)
| 200 | # ═══════════════════════════════════════════════════════════ |
| 201 | |
| 202 | def agent_loop(messages: list): |
| 203 | while True: |
| 204 | response = client.messages.create( |
| 205 | model=MODEL, system=SYSTEM, messages=messages, |
| 206 | tools=TOOLS, max_tokens=8000, |
| 207 | ) |
| 208 | messages.append({"role": "assistant", "content": response.content}) |
| 209 | |
| 210 | if response.stop_reason != "tool_use": |
| 211 | return |
| 212 | |
| 213 | results = [] |
| 214 | for block in response.content: |
| 215 | if block.type != "tool_use": |
| 216 | continue |
| 217 | |
| 218 | print(f"\033[36m> {block.name}\033[0m") |
| 219 | |
| 220 | # s03 change: run through permission pipeline before executing |
| 221 | if not check_permission(block): |
| 222 | results.append({"type": "tool_result", "tool_use_id": block.id, |
| 223 | "content": "Permission denied."}) |
| 224 | continue |
| 225 | |
| 226 | handler = TOOL_HANDLERS.get(block.name) |
| 227 | output = handler(**block.input) if handler else f"Unknown: {block.name}" |
| 228 | print(str(output)[:200]) |
| 229 | results.append({"type": "tool_result", "tool_use_id": block.id, "content": output}) |
| 230 | |
| 231 | messages.append({"role": "user", "content": results}) |
| 232 | |
| 233 | |
| 234 | if __name__ == "__main__": |
no test coverage detected