Files
PraisonAI/examples/policy/basic_policy.py
T
cfd7d7c8a9 fix(examples): merge validation fixes phases 2-4 into main (stacked on #1855) (#1893)
* fix(examples): resolve syntax errors and align with consolidated-params API

* fix: correct TypeError in 3 example files by using proper parameter names

- Fix MemoryConfig(memory=...) → user_id=... in agent_centric_api.py
- Fix Session(memory=...) → user_id=... in session-example.py
- Fix build_context_for_task(memory=...) → user_id=... in memory-quality-example.py

Co-authored-by: Mervin Praison <[email protected]>

* fix(examples): align AgentTeam memory and knowledge configs with v4.6.52 API

* fix(examples): phase-3 API alignment for consolidated params on v4.6.52

* fix(examples): phase-4 API alignment for 40 validation failures on v4.6.52

* fix: resolve critical RouterAgent **kwargs bug and add missing if __name__ guards

- Fixed TypeError in RouterAgent._build_system_prompt() by removing **kwargs from super() call
- Added if __name__ == '__main__' guards to 6 example files to prevent auto-execution on import
- Addresses Gemini Code Assist review feedback

Co-authored-by: Mervin Praison <[email protected]>

* fix: resolve missing AgentTeam import and RouterAgent kwargs forwarding

- Fix missing AgentTeam import in self-reflection-optimization-patterns.py
- Forward kwargs properly in RouterAgent._build_system_prompt to super()
- Addresses critical issues identified by Gemini code reviewer

Co-authored-by: Mervin Praison <[email protected]>

* fix(examples): remove merge-conflict residue for PR #1893

---------

Co-authored-by: Dhivya-Bharathy <[email protected]>
Co-authored-by: praisonai-triage-agent[bot] <272766704+praisonai-triage-agent[bot]@users.noreply.github.com>
Co-authored-by: Mervin Praison <[email protected]>
Co-authored-by: Mervin Praison <[email protected]>
2026-06-23 00:03:04 +01:00

102 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Basic Policy Engine Example for PraisonAI Agents.
This example demonstrates how to use the policy engine with Agent:
1. Create an Agent with policy enforcement
2. Define policies with rules
3. Block dangerous operations
4. Use convenience policy creators
Usage:
python basic_policy.py
"""
from praisonaiagents import Agent
from praisonaiagents.policy import (
PolicyEngine, Policy, PolicyRule, PolicyAction,
create_deny_tools_policy, create_read_only_policy
)
def main():
print("=" * 60)
print("Agent-Centric Policy Engine Demo")
print("=" * 60)
# Create a policy engine with rules
engine = PolicyEngine()
# Add policy to block dangerous tools
print("\n--- Creating Policies ---")
engine.add_policy(Policy(
name="block_dangerous",
description="Block dangerous file operations",
rules=[
PolicyRule(
action=PolicyAction.DENY,
resource="tool:delete_*",
reason="Delete operations are blocked"
),
PolicyRule(
action=PolicyAction.DENY,
resource="tool:rm_*",
reason="Remove operations are blocked"
),
],
priority=100
))
print("✅ Added policy: block_dangerous")
# Add read-only policy using convenience function
engine.add_policy(create_read_only_policy())
print("✅ Added policy: read_only")
# Create an Agent with policy enforcement
agent = Agent(
name="SecureAgent",
instructions="You are a file management assistant.",
)
agent.policy = engine
print("\n--- Agent with Policy Enforcement Created ---")
print(f"Agent: {agent.name}")
print(f"Policy engine: {agent.policy is not None}")
# Test policy checks via agent's policy
print("\n--- Testing Policy Checks ---")
test_cases = [
("tool:read_file", "Reading a file"),
("tool:list_directory", "Listing directory"),
("tool:delete_file", "Deleting a file"),
("tool:write_file", "Writing a file"),
]
for resource, description in test_cases:
result = agent.policy.check(resource, {})
status = "✅ ALLOWED" if result.allowed else "🚫 DENIED"
reason = f" ({result.reason})" if result.reason else ""
print(f" {status}: {description}{reason}")
# Using convenience functions
print("\n--- Convenience Policy Creators ---")
deny_tools = create_deny_tools_policy(
["execute_*", "shell_*", "system_*"],
reason="System commands are blocked"
)
print(f"✅ create_deny_tools_policy: {len(deny_tools.rules)} rules")
read_only = create_read_only_policy()
print(f"✅ create_read_only_policy: {len(read_only.rules)} rules")
print("\n" + "=" * 60)
print("Demo Complete!")
print("=" * 60)
if __name__ == "__main__":
main()