| import json |
| import os |
| import logging |
| from pathlib import Path |
|
|
| logger = logging.getLogger("friday.protocols") |
|
|
| PROTOCOLS_PATH = Path(__file__).parent.parent.parent / "data" / "learned_protocols.json" |
|
|
| def get_protocols() -> dict: |
| """Load learned protocols from disk.""" |
| if not PROTOCOLS_PATH.exists(): |
| |
| os.makedirs(PROTOCOLS_PATH.parent, exist_ok=True) |
| default = { |
| "user_preferences": [], |
| "interaction_style": "default", |
| "learned_facts": {}, |
| "capabilities": {}, |
| "forbidden_topics": ["personal credentials", "passwords"] |
| } |
| save_protocols(default) |
| return default |
| |
| try: |
| with open(PROTOCOLS_PATH, "r") as f: |
| return json.load(f) |
| except Exception as e: |
| logger.error(f"Failed to load protocols: {e}") |
| return {} |
|
|
| def save_protocols(protocols: dict): |
| """Save learned protocols to disk.""" |
| try: |
| os.makedirs(PROTOCOLS_PATH.parent, exist_ok=True) |
| with open(PROTOCOLS_PATH, "w") as f: |
| json.dump(protocols, f, indent=4) |
| logger.info("Learned protocols updated.") |
| except Exception as e: |
| logger.error(f"Failed to save protocols: {e}") |
|
|
| def update_protocol(category: str, detail: str): |
| """Update a specific protocol category.""" |
| protocols = get_protocols() |
| if category not in protocols: |
| protocols[category] = [] |
| |
| if isinstance(protocols[category], list): |
| if detail not in protocols[category]: |
| protocols[category].append(detail) |
| elif isinstance(protocols[category], dict): |
| |
| if ":" in detail: |
| k, v = detail.split(":", 1) |
| protocols[category][k.strip()] = v.strip() |
| |
| save_protocols(protocols) |
|
|
| def get_protocol_prompt() -> str: |
| """Generate a prompt snippet from learned protocols.""" |
| protocols = get_protocols() |
| if not protocols: |
| return "" |
| |
| prompt = "\n[LEARNED ADAPTIVE PROTOCOLS]\n" |
| if protocols.get("user_preferences"): |
| prompt += "- Preferences: " + ", ".join(protocols["user_preferences"]) + "\n" |
| if protocols.get("interaction_style") != "default": |
| prompt += f"- Style Override: {protocols['interaction_style']}\n" |
| for k, v in protocols.get("learned_facts", {}).items(): |
| prompt += f"- Known Fact: {k} is {v}\n" |
| |
| return prompt + "\n" |
|
|