from __future__ import annotations

"""Shared conversation state used by multiple agents."""

import asyncio
from dataclasses import dataclass, field

from agentrspy.core import Message


@dataclass(slots=True)
class SharedConversation:
    """Shared conversation state used by multiple agents."""

    messages: list[Message] = field(default_factory=list)
    lock: asyncio.Lock = field(default_factory=asyncio.Lock)

    @classmethod
    def new(cls) -> "SharedConversation":
        return cls()

    async def add(self, agent_name: str, message: Message) -> None:
        async with self.lock:
            message.metadata["agent"] = agent_name
            self.messages.append(message)

    async def get_all(self) -> list[Message]:
        async with self.lock:
            return list(self.messages)
