(messages: list)
| 727 | |
| 728 | |
| 729 | def agent_loop(messages: list): |
| 730 | while True: |
| 731 | response = client.messages.create( |
| 732 | model=MODEL, |
| 733 | system=SYSTEM, |
| 734 | messages=messages, |
| 735 | tools=TOOLS, |
| 736 | max_tokens=8000, |
| 737 | ) |
| 738 | messages.append({"role": "assistant", "content": response.content}) |
| 739 | if response.stop_reason != "tool_use": |
| 740 | return |
| 741 | |
| 742 | results = [] |
| 743 | for block in response.content: |
| 744 | if block.type == "tool_use": |
| 745 | handler = TOOL_HANDLERS.get(block.name) |
| 746 | try: |
| 747 | output = handler(**block.input) if handler else f"Unknown tool: {block.name}" |
| 748 | except Exception as e: |
| 749 | output = f"Error: {e}" |
| 750 | print(f"> {block.name}:") |
| 751 | print(str(output)[:200]) |
| 752 | results.append( |
| 753 | { |
| 754 | "type": "tool_result", |
| 755 | "tool_use_id": block.id, |
| 756 | "content": str(output), |
| 757 | } |
| 758 | ) |
| 759 | messages.append({"role": "user", "content": results}) |
| 760 | |
| 761 | |
| 762 | if __name__ == "__main__": |
no test coverage detected