04_agent_integration.py
1 """ 2 Example 4: Agent Integration with Context Management 3 4 This example shows how to use context management with the Agent class. 5 Context management is now unified via the context= parameter. 6 7 Benefits: 8 - Seamless integration with existing Agent workflows 9 - Automatic token tracking and optimization 10 - Configurable via context= parameter with ManagerConfig 11 """ 12 13 from praisonaiagents import Agent 14 from praisonaiagents.context import ManagerConfig 15 16 WORKSPACE = "/Users/praison/praisonai-package/src/praisonai-agents" 17 18 19 def main(): 20 print("=" * 70) 21 print("Agent Integration with Context Management") 22 print("=" * 70) 23 24 # Create an agent with context management enabled 25 print("\n1. Creating Agent with Context Management") 26 print("-" * 40) 27 28 config = ManagerConfig( 29 auto_compact=True, 30 compact_threshold=0.8, 31 strategy="smart", 32 monitor_enabled=True, 33 monitor_path="./context_debug.txt", 34 ) 35 36 agent = Agent( 37 name="CodeAssistant", 38 instructions="You are a helpful code assistant.", 39 context=config, 40 output="silent" 41 ) 42 43 print(f" Agent name: {agent.name}") 44 print(f" Context manager: {agent.context_manager is not None}") 45 if agent.context_manager: 46 print(f" Auto-compact: {agent.context_manager.config.auto_compact}") 47 print(f" Threshold: {agent.context_manager.config.compact_threshold}") 48 print(f" Strategy: {agent.context_manager.config.strategy}") 49 50 # For FastContext code search, use the module directly 51 print("\n2. Using FastContext for Code Search") 52 print("-" * 40) 53 54 try: 55 from praisonaiagents.context.fast import FastContext 56 57 fc = FastContext( 58 workspace_path=WORKSPACE, 59 model="gpt-4o-mini", 60 max_turns=4, 61 max_parallel=8, 62 timeout=30.0, 63 ) 64 65 queries = [ 66 "find the Agent class definition", 67 "search for chat method", 68 ] 69 70 for query in queries: 71 print(f"\n Query: '{query}'") 72 result = fc.search(query) 73 print(f" Files found: {result.total_files}") 74 print(f" Search time: {result.search_time_ms}ms") 75 76 except ImportError: 77 print(" FastContext not available (optional feature)") 78 except Exception as e: 79 print(f" FastContext error: {e}") 80 81 # Show configuration options 82 print("\n3. Configuration Options") 83 print("-" * 40) 84 85 print(" Agent context= parameter:") 86 print(" - context=True # Enable with defaults") 87 print(" - context=ManagerConfig(...) # Custom configuration") 88 print(" - context=False # Disabled (default)") 89 90 print("\n ManagerConfig options:") 91 print(" - auto_compact=True # Auto-optimize context") 92 print(" - compact_threshold=0.8 # Trigger at 80%") 93 print(" - strategy='smart' # Optimization strategy") 94 print(" - monitor_enabled=True # Write snapshots") 95 print(" - monitor_path='./context.txt' # Snapshot path") 96 print(" - FAST_CONTEXT_PARALLELISM") 97 print(" - FAST_CONTEXT_TIMEOUT") 98 print(" - FAST_CONTEXT_CACHE") 99 print(" - FAST_CONTEXT_CACHE_TTL") 100 101 print("\n" + "=" * 70) 102 print("Fast Context integrates seamlessly with Agent for code search!") 103 print("=" * 70) 104 105 106 if __name__ == "__main__": 107 main()