/ examples / policy / basic_policy.py
basic_policy.py
  1  #!/usr/bin/env python3
  2  """
  3  Basic Policy Engine Example for PraisonAI Agents.
  4  
  5  This example demonstrates how to use the policy engine with Agent:
  6  1. Create an Agent with policy enforcement
  7  2. Define policies with rules
  8  3. Block dangerous operations
  9  4. Use convenience policy creators
 10  
 11  Usage:
 12      python basic_policy.py
 13  """
 14  
 15  from praisonaiagents import Agent
 16  from praisonaiagents.policy import (
 17      PolicyEngine, Policy, PolicyRule, PolicyAction,
 18      create_deny_tools_policy, create_read_only_policy
 19  )
 20  
 21  
 22  def main():
 23      print("=" * 60)
 24      print("Agent-Centric Policy Engine Demo")
 25      print("=" * 60)
 26      
 27      # Create a policy engine with rules
 28      engine = PolicyEngine()
 29      
 30      # Add policy to block dangerous tools
 31      print("\n--- Creating Policies ---")
 32      
 33      engine.add_policy(Policy(
 34          name="block_dangerous",
 35          description="Block dangerous file operations",
 36          rules=[
 37              PolicyRule(
 38                  action=PolicyAction.DENY,
 39                  resource="tool:delete_*",
 40                  reason="Delete operations are blocked"
 41              ),
 42              PolicyRule(
 43                  action=PolicyAction.DENY,
 44                  resource="tool:rm_*",
 45                  reason="Remove operations are blocked"
 46              ),
 47          ],
 48          priority=100
 49      ))
 50      print("✅ Added policy: block_dangerous")
 51      
 52      # Add read-only policy using convenience function
 53      engine.add_policy(create_read_only_policy())
 54      print("✅ Added policy: read_only")
 55      
 56      # Create an Agent with policy enforcement
 57      agent = Agent(
 58          name="SecureAgent",
 59          instructions="You are a file management assistant.",
 60          policy=engine
 61      )
 62      
 63      print("\n--- Agent with Policy Enforcement Created ---")
 64      print(f"Agent: {agent.name}")
 65      print(f"Policy engine: {agent.policy is not None}")
 66      
 67      # Test policy checks via agent's policy
 68      print("\n--- Testing Policy Checks ---")
 69      
 70      test_cases = [
 71          ("tool:read_file", "Reading a file"),
 72          ("tool:list_directory", "Listing directory"),
 73          ("tool:delete_file", "Deleting a file"),
 74          ("tool:write_file", "Writing a file"),
 75      ]
 76      
 77      for resource, description in test_cases:
 78          result = agent.policy.check(resource, {})
 79          status = "✅ ALLOWED" if result.allowed else "🚫 DENIED"
 80          reason = f" ({result.reason})" if result.reason else ""
 81          print(f"  {status}: {description}{reason}")
 82      
 83      # Using convenience functions
 84      print("\n--- Convenience Policy Creators ---")
 85      
 86      deny_tools = create_deny_tools_policy(
 87          ["execute_*", "shell_*", "system_*"],
 88          reason="System commands are blocked"
 89      )
 90      print(f"✅ create_deny_tools_policy: {len(deny_tools.rules)} rules")
 91      
 92      read_only = create_read_only_policy()
 93      print(f"✅ create_read_only_policy: {len(read_only.rules)} rules")
 94      
 95      print("\n" + "=" * 60)
 96      print("Demo Complete!")
 97      print("=" * 60)
 98  
 99  
100  if __name__ == "__main__":
101      main()