#!/usr/bin/env python3 """ Complete demonstration of Enhanced Agent V0 features This script demonstrates all the enhanced Agent V0 features working together in a realistic workflow capture scenario. """ import sys import os import time import tempfile from datetime import datetime from typing import List # Add agent_v0 to path sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'agent_v0')) from agent_v0.enhanced_raw_session import EnhancedRawSession from agent_v0.workflow_namer import WorkflowNamer from agent_v0.enhanced_event_captor import EnhancedEventCaptor, UIContext from agent_v0.targeted_screen_capturer import TargetedScreenCapturer from agent_v0.processing_monitor import ProcessingMonitor, ProcessingStage from agent_v0.workflow_locator import WorkflowLocator, WorkflowFilters def simulate_customer_registration_workflow(): """Simulate a complete customer registration workflow""" print("šŸŽÆ Simulating Customer Registration Workflow") print("=" * 50) # 1. Create enhanced session with intelligent naming print("1. Creating enhanced session...") session = EnhancedRawSession.create_enhanced( user_id="demo_user", user_label="Demo User", workflow_name=None, # Let system generate intelligent name auto_generate_name=True, platform="linux", hostname="demo-machine", screen_resolution=[1920, 1080] ) print(f" Session ID: {session.session_id}") # 2. Simulate workflow steps with enhanced events print("\n2. Capturing enhanced workflow events...") workflow_steps = [ # Login sequence { "action": "click", "pos": [960, 400], "window": "CRM Pro - Login", "app": "CRM_Pro", "element_type": "input", "element_text": "Username", "description": "Click username field" }, { "action": "type", "text": "admin", "window": "CRM Pro - Login", "app": "CRM_Pro", "description": "Type username" }, { "action": "click", "pos": [960, 450], "window": "CRM Pro - Login", "app": "CRM_Pro", "element_type": "input", "element_text": "Password", "description": "Click password field" }, { "action": "type", "text": "password123", "window": "CRM Pro - Login", "app": "CRM_Pro", "description": "Type password (will be masked)" }, { "action": "click", "pos": [960, 500], "window": "CRM Pro - Login", "app": "CRM_Pro", "element_type": "button", "element_text": "Login", "description": "Click login button" }, # Navigation to customer section { "action": "click", "pos": [200, 150], "window": "CRM Pro - Dashboard", "app": "CRM_Pro", "element_type": "link", "element_text": "Customers", "description": "Navigate to customers" }, { "action": "click", "pos": [300, 100], "window": "CRM Pro - Customer List", "app": "CRM_Pro", "element_type": "button", "element_text": "Add New Customer", "description": "Click add customer button" }, # Customer form filling { "action": "click", "pos": [400, 200], "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "element_type": "input", "element_text": "First Name", "description": "Click first name field" }, { "action": "type", "text": "John", "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "description": "Type first name" }, { "action": "click", "pos": [400, 250], "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "element_type": "input", "element_text": "Last Name", "description": "Click last name field" }, { "action": "type", "text": "Doe", "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "description": "Type last name" }, { "action": "click", "pos": [400, 300], "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "element_type": "input", "element_text": "Email", "description": "Click email field" }, { "action": "type", "text": "john.doe@example.com", "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "description": "Type email address" }, { "action": "click", "pos": [400, 350], "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "element_type": "input", "element_text": "Phone", "description": "Click phone field" }, { "action": "type", "text": "+1-555-123-4567", "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "description": "Type phone number" }, # Save customer { "action": "click", "pos": [500, 450], "window": "CRM Pro - New Customer Form", "app": "CRM_Pro", "element_type": "button", "element_text": "Save Customer", "description": "Save the customer" } ] # Process each step for i, step in enumerate(workflow_steps): print(f" Step {i+1:2d}: {step['description']}") if step["action"] == "click": session.add_enhanced_mouse_click_event( button="left", pos=step["pos"], window_title=step["window"], app_name=step["app"], screenshot_id=f"shot_{i+1:03d}", element_type=step.get("element_type", "unknown"), element_text=step.get("element_text"), confidence=0.9 ) elif step["action"] == "type": # Mask sensitive content text_content = step["text"] if "password" in step["window"].lower() or "Password" in step.get("element_text", ""): text_content = "*" * len(step["text"]) session.add_enhanced_key_event( keys=list(step["text"]), window_title=step["window"], app_name=step["app"], screenshot_id=f"shot_{i+1:03d}", text_content=text_content, input_method="typing" ) # Small delay to simulate realistic timing time.sleep(0.1) print(f" Captured {len(workflow_steps)} workflow steps") # 3. Generate intelligent workflow name print("\n3. Generating intelligent workflow name...") existing_names = ["Login_CRM_Pro", "Customer_Search_CRM", "Report_Generation"] intelligent_name = session.generate_intelligent_name(existing_names) print(f" Generated name: {intelligent_name}") # 4. Analyze session quality and get suggestions print("\n4. Analyzing workflow quality...") analysis = session.analyze_session() quality_score = session.get_workflow_quality_score() suggestions = session.get_workflow_suggestions() print(f" Workflow type: {analysis.workflow_type}") print(f" Primary application: {analysis.primary_application}") print(f" Complexity score: {analysis.complexity_score:.2f}") print(f" Quality score: {quality_score:.1%}") print(f" Top suggestions:") for i, suggestion in enumerate(suggestions[:3], 1): print(f" {i}. {suggestion}") # 5. Close session with analysis print("\n5. Closing session with analysis...") session.close_with_analysis() print(f" Session duration: {session.duration_seconds:.1f} seconds") print(f" Total events: {len(session.events)}") print(f" Enhanced events: {len(session.enhanced_events)}") return session def demonstrate_processing_monitoring(session): """Demonstrate processing pipeline monitoring""" print("\nšŸ“Š Demonstrating Processing Monitoring") print("=" * 50) with tempfile.TemporaryDirectory() as temp_dir: # Create processing monitor monitor = ProcessingMonitor(temp_dir) # Create processing session workflow_name = session.workflow_metadata.workflow_name processing_info = monitor.create_processing_session(session.session_id, workflow_name) print(f"1. Created processing session: {workflow_name}") print(f" Session ID: {session.session_id}") print(f" Processing steps: {len(processing_info.steps)}") # Simulate processing pipeline print("\n2. Simulating processing pipeline...") stages = [ (ProcessingStage.UPLOAD, "Uploading session data..."), (ProcessingStage.VALIDATION, "Validating data integrity..."), (ProcessingStage.SCREENSHOT_ANALYSIS, "Analyzing screenshots..."), (ProcessingStage.UI_DETECTION, "Detecting UI elements..."), (ProcessingStage.WORKFLOW_GENERATION, "Generating workflow..."), (ProcessingStage.OPTIMIZATION, "Optimizing actions..."), (ProcessingStage.FINALIZATION, "Finalizing workflow...") ] for stage, description in stages: print(f" {description}") # Simulate progressive updates for progress in [25, 50, 75, 100]: monitor.update_step_progress(session.session_id, stage, progress) time.sleep(0.2) # Simulate processing time # Complete the step monitor.complete_step(session.session_id, stage, { "processed_items": 10, "success_rate": 0.95 }) # Complete processing result_path = f"workflow_{session.session_id}.json" monitor.complete_processing(session.session_id, result_path) # Show final status final_status = monitor.get_processing_status(session.session_id) print(f"\n3. Processing completed successfully!") print(f" Overall progress: {final_status.overall_progress:.1f}%") print(f" Total duration: {final_status.duration.total_seconds():.1f} seconds") print(f" Result path: {final_status.result_path}") return final_status def demonstrate_workflow_organization(session): """Demonstrate workflow organization and discovery""" print("\nšŸ—‚ļø Demonstrating Workflow Organization") print("=" * 50) with tempfile.TemporaryDirectory() as temp_dir: # Save the session to temporary directory json_path = session.save_enhanced_json(temp_dir) print(f"1. Saved workflow to: {json_path}") # Create additional test workflows for demonstration test_workflows = [ { "name": "Email_Compose_Gmail", "type": "communication", "app": "Gmail", "quality": 0.78, "events": 8 }, { "name": "Document_Search_Drive", "type": "search", "app": "Google_Drive", "quality": 0.92, "events": 12 }, { "name": "Report_Generation_Excel", "type": "data_processing", "app": "Excel", "quality": 0.65, "events": 25 } ] # Create test workflow files for i, workflow in enumerate(test_workflows): workflow_dir = os.path.join(temp_dir, f"{workflow['name']}_sess_{i+2}") os.makedirs(workflow_dir, exist_ok=True) json_data = { "session_id": f"sess_{i+2}", "started_at": datetime.now().isoformat(), "ended_at": datetime.now().isoformat(), "workflow_metadata": { "workflow_name": workflow["name"], "workflow_type": workflow["type"], "primary_application": workflow["app"] }, "quality_score": workflow["quality"], "events": [{"type": "mouse_click"} for _ in range(workflow["events"])], "screenshots": [{"id": f"shot_{j}"} for j in range(3)] } json_file = os.path.join(workflow_dir, f"sess_{i+2}_enhanced.json") with open(json_file, 'w') as f: import json json.dump(json_data, f, indent=2) # Create workflow locator locator = WorkflowLocator(temp_dir) # Discover all workflows print("\n2. Discovering workflows...") workflows = locator.discover_workflows() print(f" Found {len(workflows)} workflows:") for workflow in workflows: print(f" • {workflow.name} ({workflow.type}) - Quality: {workflow.quality_score:.1%}") # Demonstrate search and filtering print("\n3. Demonstrating search and filtering...") # Search by type filters = WorkflowFilters(workflow_type="form_filling") form_workflows = locator.search_workflows(filters) print(f" Form filling workflows: {len(form_workflows)}") # Search by quality filters = WorkflowFilters(min_quality=0.8) high_quality = locator.search_workflows(filters) print(f" High quality workflows (>80%): {len(high_quality)}") # Search by application filters = WorkflowFilters(application="CRM_Pro") crm_workflows = locator.search_workflows(filters) print(f" CRM Pro workflows: {len(crm_workflows)}") # Text search filters = WorkflowFilters(search_query="customer") customer_workflows = locator.search_workflows(filters) print(f" Workflows containing 'customer': {len(customer_workflows)}") # Get statistics print("\n4. Workflow statistics:") stats = locator.get_workflow_statistics() print(f" Total workflows: {stats['total_workflows']}") print(f" Total events: {stats['total_events']}") print(f" Average quality: {stats['average_quality']:.1%}") print(f" Workflow types: {list(stats['workflow_types'].keys())}") print(f" Applications: {list(stats['applications'].keys())}") # Demonstrate organization print("\n5. Organizing workflows by type:") organized = locator.organize_workflows("by_type") for workflow_type, type_workflows in organized.items(): print(f" {workflow_type}: {len(type_workflows)} workflows") return workflows def demonstrate_enhanced_event_capture(): """Demonstrate enhanced event capture capabilities""" print("\nāŒØļø Demonstrating Enhanced Event Capture") print("=" * 50) captured_events = [] captured_text = [] captured_combos = [] def on_click(button: str, x: int, y: int, context: UIContext): captured_events.append(("click", button, x, y, context.window_title)) def on_key_combo(keys: List[str], context: UIContext, text_content: str): captured_combos.append((keys, context.window_title, text_content)) def on_text_input(text: str, context: UIContext, method: str): captured_text.append((text, context.window_title, method)) # Create enhanced event captor captor = EnhancedEventCaptor( on_mouse_click=on_click, on_key_combo=on_key_combo, on_text_input=on_text_input, capture_sensitive_fields=False ) print("1. Enhanced event captor created") print(" Features enabled:") print(" • Mouse click capture with UI context") print(" • Keyboard event capture with text content") print(" • Key combination detection") print(" • Sensitive field protection") print(" • UI element context detection") # Test UI context detection print("\n2. Testing UI context detection...") context = captor.ui_detector.get_ui_context(500, 300) print(f" Window title: {context.window_title}") print(f" Application: {context.app_name}") print(f" Element type: {context.element_type}") print(f" Confidence: {context.confidence}") # Test sensitive field detection print("\n3. Testing sensitive field detection...") test_contexts = [ UIContext("Login Form", "TestApp", element_text="Password"), UIContext("Registration", "TestApp", element_text="Credit Card"), UIContext("Profile", "TestApp", element_text="First Name"), UIContext("Settings", "TestApp", element_text="API Key") ] for ctx in test_contexts: is_sensitive = captor.sensitive_detector.is_sensitive_field(ctx) status = "šŸ”’ SENSITIVE" if is_sensitive else "āœ… Normal" print(f" {ctx.element_text}: {status}") print("\n4. Event capture system ready for use") print(" Note: In actual usage, events would be captured from real user interactions") def main(): """Run complete enhanced Agent V0 demonstration""" print("šŸš€ Enhanced Agent V0 - Complete Feature Demonstration") print("=" * 60) print("This demo showcases all enhanced features working together:") print("• Intelligent workflow naming") print("• Enhanced event capture") print("• Targeted screenshot system") print("• Processing pipeline monitoring") print("• Workflow organization and discovery") print("=" * 60) try: # 1. Simulate complete workflow capture session = simulate_customer_registration_workflow() # 2. Demonstrate processing monitoring processing_status = demonstrate_processing_monitoring(session) # 3. Demonstrate workflow organization workflows = demonstrate_workflow_organization(session) # 4. Demonstrate enhanced event capture demonstrate_enhanced_event_capture() # 5. Summary print("\nšŸŽ‰ Demonstration Complete!") print("=" * 60) print("Successfully demonstrated:") print(f"āœ… Intelligent workflow naming: '{session.workflow_metadata.workflow_name}'") print(f"āœ… Enhanced event capture: {len(session.enhanced_events)} enhanced events") print(f"āœ… Workflow quality analysis: {session.get_workflow_quality_score():.1%} quality score") print(f"āœ… Processing monitoring: {processing_status.overall_progress:.1f}% completion") print(f"āœ… Workflow organization: {len(workflows)} workflows discovered") print("\nAll enhanced Agent V0 features are working correctly! šŸŽÆ") return 0 except Exception as e: print(f"\nāŒ Demonstration failed: {e}") import traceback traceback.print_exc() return 1 if __name__ == "__main__": sys.exit(main())