Untitled

 avatar
unknown
plain_text
2 months ago
4.0 kB
8
Indexable
"Clean my PC" → {"action": "delete", "target": "temp_files"} from typing import Dict, Any, List

# ---------------------------
# POLICY ENGINE
# ---------------------------

class Policy:
    def __init__(self, policy_id, priority, condition, effect):
        self.policy_id = policy_id
        self.priority = priority
        self.condition = condition
        self.effect = effect

    def matches(self, action: Dict[str, Any]) -> bool:
        for key, value in self.condition.items():
            if action.get(key) != value:
                return False
        return True


class PolicyEngine:
    def __init__(self):
        self.policies: List[Policy] = []

    def add_policy(self, policy: Policy):
        self.policies.append(policy)

    def evaluate(self, action: Dict[str, Any]) -> Dict[str, Any]:
        matched = []

        for policy in self.policies:
            if policy.matches(action):
                matched.append(policy)

        if not matched:
            return {"decision": "allow"}

        # highest priority wins
        matched.sort(key=lambda p: p.priority, reverse=True)
        top = matched[0]

        return {
            "decision": top.effect,
            "policy": top.policy_id
        }


# ---------------------------
# INTENT ENGINE
# ---------------------------

class IntentEngine:
    def interpret(self, user_input: str) -> List[Dict[str, Any]]:
        """
        Converts natural language into structured actions
        (VERY simplified mock)
        """
        if "delete" in user_input.lower():
            return [{
                "action": "delete",
                "target": "user_documents"
            }]

        if "clean" in user_input.lower():
            return [{
                "action": "delete",
                "target": "temp_files"
            }]

        if "open browser" in user_input.lower():
            return [{
                "action": "open_app",
                "target": "browser"
            }]

        return [{"action": "unknown"}]


# ---------------------------
# EXECUTION LAYER
# ---------------------------

class ExecutionLayer:
    def execute(self, action: Dict[str, Any]):
        print(f"⚙️ Executing: {action}")


# ---------------------------
# SYSTEM CONTROLLER
# ---------------------------

class Windows12System:
    def __init__(self):
        self.intent_engine = IntentEngine()
        self.policy_engine = PolicyEngine()
        self.executor = ExecutionLayer()

    def handle_input(self, user_input: str):
        print(f"\n🧠 User Intent: {user_input}")

        actions = self.intent_engine.interpret(user_input)

        for action in actions:
            result = self.policy_engine.evaluate(action)

            if result["decision"] == "deny":
                print(f"❌ Blocked by policy: {result['policy']}")
            elif result["decision"] == "ask":
                print(f"⚠️ Requires user confirmation ({result['policy']})")
            else:
                self.executor.execute(action)


# ---------------------------
# SETUP SYSTEM
# ---------------------------

system = Windows12System()

# Hard Rule: Never delete user documents
system.policy_engine.add_policy(
    Policy(
        policy_id="no_delete_user_docs",
        priority=100,
        condition={"action": "delete", "target": "user_documents"},
        effect="deny"
    )
)

# Context Rule: Allow temp cleanup
system.policy_engine.add_policy(
    Policy(
        policy_id="allow_temp_cleanup",
        priority=10,
        condition={"action": "delete", "target": "temp_files"},
        effect="allow"
    )
)

# ---------------------------
# TEST CASES
# ---------------------------

system.handle_input("Delete my documents")
system.handle_input("Clean my PC")
system.handle_input("Open browser")
Editor is loading...
Leave a Comment