#!/usr/bin/env python3 """ Test script to verify that XML tool examples are correctly included in workflow system prompts. """ import asyncio import json from workflows.converter import WorkflowConverter from workflows.tool_examples import get_tools_xml_examples def test_xml_examples_integration(): """Test that XML tool examples are correctly included in workflow system prompts.""" print("๐Ÿงช Testing XML Tool Examples Integration") print("=" * 50) # Create a sample workflow with input node and tools nodes = [ { "id": "input-1", "type": "inputNode", "position": {"x": 100, "y": 100}, "data": { "label": "Workflow Input", "prompt": "Search for information about AI and create a summary file.", "trigger_type": "MANUAL", "variables": { "topic": "artificial intelligence", "output_format": "markdown" } } }, { "id": "agent-1", "type": "agentNode", "position": {"x": 400, "y": 100}, "data": { "label": "Research Agent", "instructions": "You are a research assistant that searches for information and creates summaries", "model": "anthropic/claude-3-5-sonnet-latest", "connectedTools": [ {"id": "tool-1", "name": "Web Search", "type": "web_search_tool"}, {"id": "tool-2", "name": "File Operations", "type": "sb_files_tool"} ] } }, { "id": "tool-1", "type": "toolConnectionNode", "position": {"x": 100, "y": 200}, "data": { "label": "Web Search", "nodeId": "web_search_tool", "description": "Search the web for information" } }, { "id": "tool-2", "type": "toolConnectionNode", "position": {"x": 100, "y": 300}, "data": { "label": "File Operations", "nodeId": "sb_files_tool", "description": "Create and manage files" } } ] edges = [ { "id": "e-input-1-agent-1", "source": "input-1", "target": "agent-1", "sourceHandle": "output", "targetHandle": "input" }, { "id": "e-tool-1-agent-1", "source": "tool-1", "target": "agent-1", "sourceHandle": "tool-connection", "targetHandle": "tools" }, { "id": "e-tool-2-agent-1", "source": "tool-2", "target": "agent-1", "sourceHandle": "tool-connection", "targetHandle": "tools" } ] metadata = { "name": "Research and Summary Workflow", "description": "A workflow that searches for information and creates summaries", "project_id": "test-project-123" } # Convert flow to workflow definition converter = WorkflowConverter() workflow_def = converter.convert_flow_to_workflow(nodes, edges, metadata) print(f"โœ… Workflow Definition Created:") print(f" Name: {workflow_def.name}") print(f" Steps: {len(workflow_def.steps)}") # Get the system prompt main_step = workflow_def.steps[0] system_prompt = main_step.config.get("system_prompt", "") print(f"\n๐Ÿ“ System Prompt Analysis:") print(f" System Prompt Length: {len(system_prompt)} characters") # Check that input prompt is included input_prompt = main_step.config.get("input_prompt", "") if "Search for information about AI" in system_prompt: print("โœ… Input prompt correctly included in system prompt") else: print("โŒ ERROR: Input prompt not found in system prompt!") return False # Check that tool examples section exists if "## Tool Usage Examples" in system_prompt: print("โœ… Tool Usage Examples section found in system prompt") else: print("โŒ ERROR: Tool Usage Examples section not found!") return False # Check for specific XML examples if "" in system_prompt: print("โœ… XML function_calls format found in system prompt") else: print("โŒ ERROR: XML function_calls format not found!") return False # Check for web search tool example if "web_search" in system_prompt and "" in system_prompt: print("โœ… Web search tool XML example found") else: print("โŒ ERROR: Web search tool XML example not found!") return False # Check for file operations tool example if "create_file" in system_prompt and "" in system_prompt: print("โœ… File operations tool XML example found") else: print("โŒ ERROR: File operations tool XML example not found!") return False # Test the tool examples function directly print(f"\n๐Ÿ”ง Testing Tool Examples Function:") tool_ids = ["web_search_tool", "sb_files_tool"] xml_examples = get_tools_xml_examples(tool_ids) if xml_examples: print(f"โœ… Tool examples generated successfully ({len(xml_examples)} characters)") # Check that both tools are included if "Web Search Tool" in xml_examples and "Sb Files Tool" in xml_examples: print("โœ… Both tool examples included in output") else: print("โŒ ERROR: Not all tool examples included!") return False else: print("โŒ ERROR: No tool examples generated!") return False # Print a sample of the system prompt for verification print(f"\n๐Ÿ“„ System Prompt Sample (first 500 chars):") print("-" * 50) print(system_prompt[:500] + "..." if len(system_prompt) > 500 else system_prompt) print("-" * 50) # Look for the tool examples section specifically if "## Tool Usage Examples" in system_prompt: start_idx = system_prompt.find("## Tool Usage Examples") end_idx = system_prompt.find("## Workflow Execution", start_idx) if end_idx == -1: end_idx = start_idx + 1000 # Show next 1000 chars if no end found tool_examples_section = system_prompt[start_idx:end_idx] print(f"\n๐Ÿ”ง Tool Examples Section:") print("-" * 50) print(tool_examples_section) print("-" * 50) print(f"\n๐ŸŽ‰ All Tests Passed!") print(f" โœ“ Input prompt integration") print(f" โœ“ Tool Usage Examples section") print(f" โœ“ XML function_calls format") print(f" โœ“ Web search tool example") print(f" โœ“ File operations tool example") print(f" โœ“ Tool examples function") return True def main(): """Run the test.""" try: result = test_xml_examples_integration() if result: print(f"\n๐ŸŽฏ Test Summary: SUCCESS") print(f" XML tool examples are now automatically included in workflow system prompts!") print(f" When workflows are saved, agents will know exactly how to call tools using XML format.") else: print(f"\nโŒ Test Summary: FAILED") print(f" There are issues with XML tool examples integration.") except Exception as e: print(f"โŒ Test failed with error: {e}") import traceback traceback.print_exc() if __name__ == "__main__": main()