"""# Summary SSEInMemoryBus — in-memory event bus for SSE progress events. --- Buffer published progress events so they can be replayed (and streamed) per correlation key, such as a job, upload, or fetch-request id. """ from typing import Generic, TypeVar T = TypeVar("T") class SSEInMemoryBus(Generic[T]): """Buffer SSE progress events in memory, keyed by an event attribute. Attributes: _events (List[T]): Events published since the last clear. _key_attr (str): Event attribute used to correlate a stream. """ def __init__(self, key_attr: str = "id"): """Initialize an empty event buffer. Args: key_attr (str): Attribute name on each event holding the stream correlation key (for example ``"job_id"``). """ self._events: list[T] = [] self._key_attr = key_attr async def publish(self, event: T) -> None: """Append an event to the buffer. Args: event (T): Event to append. """ self._events.append(event) def get_events(self, key: str) -> list[T]: """Return buffered events for a correlation key. Args: key (str): Value matched against the configured ``key_attr``. Returns: List[T]: Events whose ``key_attr`` equals ``key``. """ return [e for e in self._events if getattr(e, self._key_attr, None) == key] def clear(self) -> None: """Remove all buffered events.""" self._events.clear()