from __future__ import annotations MAX_NAME_LENGTH = 64 def validate_tool(tool: dict) -> bool: """Validate a tool/function definition.""" name = tool.get("name") or tool.get("function", {}).get("name", "") if not name or len(name) > MAX_NAME_LENGTH: return False if not all(c.isalnum() or c in "_-" for c in name): return False parameters = tool.get("parameters") or tool.get("function", {}).get("parameters", {}) if not isinstance(parameters, dict): return False return True def clean_tool(tool: dict) -> dict: """Strip to basic format for non-fully-supported providers.""" name = tool.get("name") or tool.get("function", {}).get("name", "") desc = tool.get("description") or tool.get("function", {}).get("description", "") parameters = tool.get("parameters") or tool.get("function", {}).get("parameters", {}) return { "type": "function", "function": { "name": name, "description": desc, "parameters": parameters, } }