lsp_code_analysis_example.py
1 """ 2 LSP Code Intelligence Example 3 4 This example demonstrates how to use LSP-powered tools through an Agent 5 for semantic code analysis (listing symbols, finding definitions, etc.). 6 7 How to run: 8 export OPENAI_API_KEY=your_key 9 python examples/lsp/lsp_code_analysis_example.py 10 11 Prerequisites: 12 pip install praisonai praisonaiagents 13 pip install python-lsp-server # Optional, for full LSP support 14 """ 15 16 import asyncio 17 import os 18 import sys 19 20 # Add paths for development 21 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../src/praisonai')) 22 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../src/praisonai-agents')) 23 24 25 async def main(): 26 print("=" * 60) 27 print("LSP CODE INTELLIGENCE EXAMPLE") 28 print("=" * 60) 29 print() 30 31 # Import after path setup 32 from praisonai.cli.features import ( 33 create_agent_centric_tools, 34 InteractiveRuntime, 35 RuntimeConfig 36 ) 37 from praisonaiagents import Agent 38 39 # Use the praisonaiagents source as workspace for analysis 40 workspace = os.path.join( 41 os.path.dirname(__file__), 42 '../../src/praisonai-agents/praisonaiagents' 43 ) 44 workspace = os.path.abspath(workspace) 45 46 print(f"Workspace: {workspace}") 47 print() 48 49 # 1. Create runtime with LSP enabled 50 print("1. Creating InteractiveRuntime with LSP...") 51 config = RuntimeConfig( 52 workspace=workspace, 53 lsp_enabled=True, # Enable LSP for code intelligence 54 acp_enabled=False, # Disable ACP for this example 55 approval_mode="auto" 56 ) 57 runtime = InteractiveRuntime(config) 58 status = await runtime.start() 59 60 print(f" LSP enabled: {status['lsp']['enabled']}") 61 print(f" LSP ready: {runtime.lsp_ready}") 62 print(f" LSP status: {status['lsp']['status']}") 63 if not runtime.lsp_ready: 64 print(" Note: LSP not available, will use regex fallback") 65 print() 66 67 # 2. Create agent-centric tools 68 print("2. Creating agent-centric tools...") 69 tools = create_agent_centric_tools(runtime) 70 71 # Filter to show only LSP tools 72 lsp_tools = [t for t in tools if t.__name__.startswith('lsp_')] 73 print(f" LSP tools available:") 74 for tool in lsp_tools: 75 print(f" - {tool.__name__}") 76 print() 77 78 # 3. Create Agent with LSP-powered tools 79 print("3. Creating Agent with LSP tools...") 80 agent = Agent( 81 name="CodeAnalyzer", 82 instructions="""You are a code analysis assistant. 83 Use lsp_list_symbols to list functions and classes in files. 84 Use lsp_find_definition to find where symbols are defined. 85 Use lsp_find_references to find where symbols are used. 86 Provide clear, organized summaries of code structure.""", 87 tools=tools, 88 output="verbose" # Use new consolidated param 89 ) 90 print() 91 92 # 4. Test 1: List symbols in a file 93 print("4. TEST 1: List symbols in agent/agent.py") 94 print("-" * 50) 95 result1 = agent.start("List all the main classes and public methods in agent/agent.py. Give me a brief summary.") 96 print("-" * 50) 97 print(f"Summary: {str(result1)[:300]}...") 98 print() 99 100 # 5. Test 2: Find definition of a symbol 101 print("5. TEST 2: Find where 'Agent' class is defined") 102 print("-" * 50) 103 result2 = agent.start("Where is the Agent class defined? Use lsp_find_definition to find it.") 104 print("-" * 50) 105 print(f"Result: {str(result2)[:300]}...") 106 print() 107 108 # 6. Test 3: Analyze main.py 109 print("6. TEST 3: Analyze main.py structure") 110 print("-" * 50) 111 result3 = agent.start("List all the functions in main.py and briefly describe what the file does.") 112 print("-" * 50) 113 print(f"Analysis: {str(result3)[:300]}...") 114 print() 115 116 # Cleanup 117 await runtime.stop() 118 119 print("=" * 60) 120 print("EXAMPLE COMPLETE") 121 print("=" * 60) 122 print() 123 print("Key takeaways:") 124 print("- LSP tools work through the Agent (agentic LSP)") 125 print("- Falls back to regex if LSP server not available") 126 print("- Returns structured data with citations") 127 128 129 if __name__ == "__main__": 130 asyncio.run(main())