"""Realtime SSE infrastructure."""

from collections.abc import AsyncGenerator

from fastapi.responses import StreamingResponse

# Shared SSE response headers — applied by both chat and domain-event endpoints.
SSE_HEADERS: dict[str, str] = {
    "Cache-Control": "no-cache",
    "X-Accel-Buffering": "no",
}


def sse_response(
    generator: AsyncGenerator[str, None],
    extra_headers: dict[str, str] | None = None,
) -> StreamingResponse:
    """Create a StreamingResponse with consistent SSE headers."""
    headers = {**SSE_HEADERS, **(extra_headers or {})}
    return StreamingResponse(
        generator,
        media_type="text/event-stream",
        headers=headers,
    )
