(self, tool_name: str, tool_input: Dict[str, Any])
| 76 | } |
| 77 | |
| 78 | def run(self, tool_name: str, tool_input: Dict[str, Any]) -> Any: |
| 79 | if tool_name in self._tools_map: |
| 80 | tool = self._tools_map[tool_name] |
| 81 | |
| 82 | # Process input parameters: if tool_input is a list containing a single dictionary, extract the dictionary |
| 83 | if ( |
| 84 | isinstance(tool_input, list) |
| 85 | and len(tool_input) == 1 |
| 86 | and isinstance(tool_input[0], dict) |
| 87 | ): |
| 88 | tool_input = tool_input[0] |
| 89 | |
| 90 | # Ensure tool_input is dictionary type |
| 91 | if not isinstance(tool_input, dict): |
| 92 | return { |
| 93 | "error": f"Tool parameter format error: expected dict, got {type(tool_input).__name__}", |
| 94 | "message": "Tool parameters must be in dictionary format", |
| 95 | "received_type": type(tool_input).__name__, |
| 96 | } |
| 97 | |
| 98 | return tool.execute(**tool_input) |
| 99 | else: |
| 100 | # Log unknown tool call but don't throw exception, return warning message |
| 101 | import logging |
| 102 | from difflib import get_close_matches |
| 103 | |
| 104 | logger = logging.getLogger(__name__) |
| 105 | |
| 106 | # Provide similar tool name suggestions |
| 107 | available_tools = list(self._tools_map.keys()) |
| 108 | suggestions = get_close_matches(tool_name, available_tools, n=3, cutoff=0.6) |
| 109 | suggestion_text = f"Suggested tools: {', '.join(suggestions)}" if suggestions else "" |
| 110 | |
| 111 | error_msg = f"Unknown tool: {tool_name}. {suggestion_text}" |
| 112 | available_tools_text = f"Available tools: {', '.join(available_tools[:10])}" + ( |
| 113 | "..." if len(available_tools) > 10 else "" |
| 114 | ) |
| 115 | return { |
| 116 | "error": error_msg, |
| 117 | "message": "This tool does not exist, please use system-provided tools", |
| 118 | "available_tools": available_tools_text, |
| 119 | "suggestions": suggestions, |
| 120 | } |
| 121 | |
| 122 | async def batch_run_tools_async(self, tool_calls: List[Dict[str, Any]]) -> Any: |
| 123 | results = [] |
no test coverage detected