25 lines
1023 B
Python
25 lines
1023 B
Python
from __future__ import annotations
|
|
|
|
from app.core.exceptions import AppError
|
|
from app.modules.api.infrastructure.streaming.sse_encoder import SseEncoder
|
|
from app.modules.api.infrastructure.streaming.sse_event_channel import SseEventChannel
|
|
from app.schemas.common import ModuleName
|
|
|
|
|
|
class StreamService:
|
|
def __init__(self, channel: SseEventChannel, request_exists, encoder: SseEncoder | None = None) -> None:
|
|
self._channel = channel
|
|
self._request_exists = request_exists
|
|
self._encoder = encoder or SseEncoder()
|
|
|
|
async def subscribe(self, request_id: str):
|
|
if not self._request_exists(request_id):
|
|
raise AppError("request_not_found", f"Agent request not found: {request_id}", ModuleName.BACKEND)
|
|
return await self._channel.subscribe(request_id, replay=True)
|
|
|
|
async def unsubscribe(self, request_id: str, queue) -> None:
|
|
await self._channel.unsubscribe(request_id, queue)
|
|
|
|
def encode(self, event) -> str:
|
|
return self._encoder.encode(event)
|