#!/usr/bin/env python3 """ Test OllamaAdapter XML parsing with real malformed examples. """ import asyncio import sys sys.path.insert(0, '/home/serversdown/project-lyra/cortex') from autonomy.tools.adapters.ollama_adapter import OllamaAdapter async def test_parser(): adapter = OllamaAdapter() # Test cases with actual malformed XML we've seen test_cases = [ { "name": "Malformed closing tag 1", "xml": """ execute_code python print(50 / 2) To calculate the result of dividing 50 by 2. """ }, { "name": "Malformed closing tag 2", "xml": """ execute_code python print(60 / 4) To calculate 60 divided by 4 using Python. python result = 35 / 7; result To calculate the division of 35 by 7 using Python. """ }, { "name": "Correct XML", "xml": """ execute_code python print(100 / 4) Calculate division """ }, { "name": "XML with surrounding text", "xml": """Let me help you with that. execute_code python print(20 / 4) Calculate the result The result will be shown above.""" } ] print("=" * 80) print("Testing OllamaAdapter XML Parsing") print("=" * 80) for test in test_cases: print(f"\nTest: {test['name']}") print("-" * 80) print(f"Input XML:\n{test['xml'][:200]}{'...' if len(test['xml']) > 200 else ''}") print("-" * 80) try: result = await adapter.parse_response(test['xml']) print(f"✅ Parsed successfully!") print(f" Content: {result.get('content', '')[:100]}") print(f" Tool calls found: {len(result.get('tool_calls') or [])}") if result.get('tool_calls'): for idx, tc in enumerate(result['tool_calls']): print(f" Tool {idx + 1}: {tc.get('name')} with args: {tc.get('arguments')}") except Exception as e: print(f"❌ Error: {e}") print() if __name__ == "__main__": asyncio.run(test_parser())