Add a tool to the set. If a tool with the same name already exists: - Prefer the one that is active (active=True) - If both have the same active state, use the new one (overwrite)
(self, tool: FunctionTool)
| 89 | return len(self.tools) == 0 |
| 90 | |
| 91 | def add_tool(self, tool: FunctionTool) -> None: |
| 92 | """Add a tool to the set. |
| 93 | |
| 94 | If a tool with the same name already exists: |
| 95 | - Prefer the one that is active (active=True) |
| 96 | - If both have the same active state, use the new one (overwrite) |
| 97 | """ |
| 98 | for i, existing_tool in enumerate(self.tools): |
| 99 | if existing_tool.name == tool.name: |
| 100 | # Use getattr with a default of True for compatibility with tools |
| 101 | # that may not define an `active` attribute (e.g., mocks). |
| 102 | existing_active = bool(getattr(existing_tool, "active", True)) |
| 103 | new_active = bool(getattr(tool, "active", True)) |
| 104 | # Overwrite if new tool is active, or if existing tool is not active |
| 105 | if new_active or not existing_active: |
| 106 | self.tools[i] = tool |
| 107 | return |
| 108 | self.tools.append(tool) |
| 109 | |
| 110 | def remove_tool(self, name: str) -> None: |
| 111 | """Remove a tool by its name.""" |