from typing import List, Dict, Any


class Memory:
    """
    Shared memory store across all agents in a Bridge.
    Agents can read/write context so they know what others did.
    """

    def __init__(self):
        self._messages: List[Dict] = []       # Full conversation history
        self._store: Dict[str, Any] = {}      # Key-value store for custom data

    def add(self, role: str, content: str, agent: str = ""):
        self._messages.append({
            "role": role,
            "content": content,
            "agent": agent
        })

    def get_messages(self) -> List[Dict]:
        """Returns messages in LLM API format (role + content only)."""
        return [{"role": m["role"], "content": m["content"]} for m in self._messages]

    def get_full(self) -> List[Dict]:
        """Returns full messages including agent metadata."""
        return self._messages.copy()

    def set(self, key: str, value: Any):
        """Store custom data."""
        self._store[key] = value

    def get(self, key: str, default=None) -> Any:
        """Retrieve custom data."""
        return self._store.get(key, default)

    def clear(self):
        self._messages = []
        self._store = {}

    def __len__(self):
        return len(self._messages)
