| 1 | import os |
| 2 | import importlib |
| 3 | import json |
| 4 | from src.swarm.tool import Tool |
| 5 | from src.swarm.assistants import Assistant |
| 6 | |
| 7 | def validate_tool(tool_definition): |
| 8 | # Validate the tool using its schema |
| 9 | Tool(**tool_definition) # Uncomment if you have a schema to validate tools |
| 10 | print(f"Validating tool: {tool_definition['function']['name']}") |
| 11 | |
| 12 | def validate_all_tools(engine): |
| 13 | tools_path = os.path.join(os.getcwd(), 'configs/tools') |
| 14 | for tool_dir in os.listdir(tools_path): |
| 15 | if '__pycache__' in tool_dir: |
| 16 | continue |
| 17 | tool_dir_path = os.path.join(tools_path, tool_dir) |
| 18 | if os.path.isdir(tool_dir_path): |
| 19 | # Validate tool.json |
| 20 | tool_json_path = os.path.join(tool_dir_path, 'tool.json') |
| 21 | handler_path = os.path.join(tool_dir_path, 'handler.py') |
| 22 | if os.path.isfile(tool_json_path) and os.path.isfile(handler_path): |
| 23 | with open(tool_json_path, 'r') as file: |
| 24 | tool_def = json.load(file) |
| 25 | tool_name_from_json = tool_def['function']['name'] |
| 26 | |
| 27 | # Check if the folder name matches the tool name in tool.json |
| 28 | if tool_name_from_json != tool_dir: |
| 29 | print(f"Mismatch in tool folder name and tool name in JSON for {tool_dir}") |
| 30 | else: |
| 31 | print(f"{tool_dir}/tool.json tool name matches folder name.") |
| 32 | |
| 33 | # Check if the function name in handler.py matches the tool name |
| 34 | spec = importlib.util.spec_from_file_location(f"{tool_dir}_handler", handler_path) |
| 35 | tool_module = importlib.util.module_from_spec(spec) |
| 36 | spec.loader.exec_module(tool_module) |
| 37 | |
| 38 | # Verify if the function exists in handler.py and matches the name |
| 39 | if hasattr(tool_module, tool_dir): |
| 40 | print(f"{tool_dir}/handler.py contains a matching function name.") |
| 41 | else: |
| 42 | print(f"{tool_dir}/handler.py does not contain a function '{tool_dir}'.") |
| 43 | |
| 44 | else: |
| 45 | if not os.path.isfile(tool_json_path): |
| 46 | print(f"Missing tool.json in {tool_dir} tool folder.") |
| 47 | if not os.path.isfile(handler_path): |
| 48 | print(f"Missing handler.py in {tool_dir} tool folder.") |
| 49 | print('\n') |
| 50 | |
| 51 | # Function to validate all assistants |
| 52 | def validate_all_assistants(): |
| 53 | assistants_path = os.path.join(os.getcwd(), 'configs/assistants') |
| 54 | for root, dirs, files in os.walk(assistants_path): |
| 55 | for file in files: |
| 56 | if file.endswith('assistant.json'): |
| 57 | file_path = os.path.join(root, file) |
| 58 | with open(file_path, 'r') as file: |
| 59 | assistant_data = json.load(file)[0] # Access the first dictionary in the list |
| 60 | try: |
| 61 | Assistant(**assistant_data) |
| 62 | print(f"{os.path.basename(root)} assistant validated!") |
| 63 | except: |
| 64 | Assistant(**assistant_data) |
| 65 | print(f"Assistant validation failed!") |
| 66 | print('\n') |