basic_checkpoints.py
1 #!/usr/bin/env python3 2 """ 3 Basic Checkpoints Example for PraisonAI Agents. 4 5 This example demonstrates how to use the shadow git checkpointing system with Agent: 6 1. Create an Agent with checkpoint support 7 2. Save checkpoints before making changes 8 3. List available checkpoints 9 4. Restore to a previous checkpoint 10 11 Usage: 12 python basic_checkpoints.py 13 """ 14 15 import asyncio 16 import tempfile 17 from pathlib import Path 18 19 from praisonaiagents import Agent 20 from praisonaiagents.checkpoints import CheckpointService 21 22 23 async def main(): 24 # Create a temporary workspace for demonstration 25 with tempfile.TemporaryDirectory() as workspace: 26 print("=" * 60) 27 print("Agent-Centric Checkpointing Demo") 28 print("=" * 60) 29 print(f"\nWorkspace: {workspace}") 30 31 # Create some initial files 32 test_file = Path(workspace) / "example.py" 33 test_file.write_text("# Initial content\nprint('Hello, World!')\n") 34 print(f"\n✅ Created initial file: {test_file.name}") 35 36 # Create checkpoint service (used standalone, not passed to Agent) 37 checkpoint_service = CheckpointService(workspace_dir=workspace) 38 39 # Create an Agent 40 agent = Agent( 41 name="RefactorBot", 42 instructions="You are a code refactoring assistant.", 43 ) 44 45 print("\n--- Agent with Checkpoint Support Created ---") 46 print(f"Agent: {agent.name}") 47 print(f"Checkpoint service initialized: {checkpoint_service is not None}") 48 49 try: 50 # Initialize the shadow git repository 51 print("\n--- Initializing Checkpoint Service ---") 52 await checkpoint_service.initialize() 53 print("✅ Shadow git repository initialized") 54 55 # Save first checkpoint 56 print("\n--- Saving Checkpoint 1: Initial State ---") 57 result1 = await checkpoint_service.save("Initial state") 58 print(f"✅ Checkpoint saved: {result1.checkpoint.id[:8]}") 59 print(f" Message: {result1.checkpoint.message}") 60 61 # Make some changes (simulating agent modifications) 62 print("\n--- Simulating Agent Changes ---") 63 test_file.write_text("# Refactored content\nprint('Hello, PraisonAI!')\nprint('Checkpoints are awesome!')\n") 64 print("✅ Modified example.py") 65 66 # Create a new file 67 new_file = Path(workspace) / "utils.py" 68 new_file.write_text("def helper():\n return 'I help!'\n") 69 print("✅ Created utils.py") 70 71 # Save second checkpoint 72 print("\n--- Saving Checkpoint 2: After Refactoring ---") 73 result2 = await checkpoint_service.save("Added features") 74 print(f"✅ Checkpoint saved: {result2.checkpoint.id[:8]}") 75 76 # List all checkpoints 77 print("\n--- Listing All Checkpoints ---") 78 checkpoints_list = await checkpoint_service.list_checkpoints() 79 for i, cp in enumerate(checkpoints_list, 1): 80 print(f" {i}. [{cp.id[:8]}] {cp.message}") 81 82 # Show diff 83 print("\n--- Current Diff ---") 84 diff = await checkpoint_service.diff() 85 if diff.files: 86 for f in diff.files: 87 print(f" {f.status}: {f.path}") 88 else: 89 print(" No uncommitted changes") 90 91 # Make more changes 92 test_file.write_text("# Bad changes\nprint('This will be reverted')\n") 93 print("\n✅ Made problematic changes to example.py") 94 95 # Restore to checkpoint 2 96 print(f"\n--- Restoring to Checkpoint: {result2.checkpoint.id[:8]} ---") 97 await checkpoint_service.restore(result2.checkpoint.id) 98 print("✅ Restored successfully!") 99 100 # Verify restoration 101 content = test_file.read_text() 102 print(f"\n--- Verified Content After Restore ---") 103 print(content) 104 105 except Exception as e: 106 print(f"\n❌ Error: {e}") 107 raise 108 109 print("\n" + "=" * 60) 110 print("Demo Complete!") 111 print("=" * 60) 112 113 114 if __name__ == "__main__": 115 asyncio.run(main())