import threading from typing import List from .playbooks.store import playbook_store, PlaybookItem class PlaybookManager: _lock = threading.Lock() @classmethod def _all(cls) -> List[PlaybookItem]: """Return all playbooks sorted by order (position 0 is always main).""" return playbook_store.all_playbooks() @classmethod def get_main_playbook(cls) -> PlaybookItem | None: playbooks = cls._all() return playbooks[0] if playbooks else None @classmethod def get_context_playbooks(cls) -> List[PlaybookItem]: """All playbooks after the first — injected as reference context.""" playbooks = cls._all() return playbooks[1:] if len(playbooks) > 1 else [] @classmethod def get_system_prompt(cls) -> str: playbook = cls.get_main_playbook() return getattr(playbook, "instructions", "") or "" if playbook else "" @classmethod def render_prompt(cls, user_message: str, variables: dict | None = None) -> str: if not variables: return user_message result = user_message for key, value in variables.items(): result = result.replace(f"{{{{{key}}}}}", str(value)) return result # Keep old name as alias so nothing else breaks @classmethod def get_active_playbook(cls) -> PlaybookItem | None: return cls.get_main_playbook()