52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class RingCentralBlockStreamer:
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
chunk_min: int = 500,
|
||
|
|
chunk_max: int = 1000,
|
||
|
|
coalesce_min: int = 200,
|
||
|
|
coalesce_idle_ms: int = 200,
|
||
|
|
):
|
||
|
|
self.chunk_min = chunk_min
|
||
|
|
self.chunk_max = chunk_max
|
||
|
|
self.coalesce_min = coalesce_min
|
||
|
|
self.coalesce_idle_ms = coalesce_idle_ms
|
||
|
|
|
||
|
|
async def stream(
|
||
|
|
self,
|
||
|
|
target_id: str,
|
||
|
|
content_stream,
|
||
|
|
outbound_adapter,
|
||
|
|
account_id: str | None = None,
|
||
|
|
) -> list[str]:
|
||
|
|
message_ids = []
|
||
|
|
buffer = ""
|
||
|
|
|
||
|
|
async for chunk in content_stream:
|
||
|
|
buffer += chunk
|
||
|
|
if len(buffer) >= self.chunk_min:
|
||
|
|
to_send = buffer[: self.chunk_max]
|
||
|
|
await outbound_adapter.send_text(
|
||
|
|
target_id,
|
||
|
|
to_send,
|
||
|
|
account_id=account_id,
|
||
|
|
)
|
||
|
|
message_ids.append(to_send)
|
||
|
|
buffer = buffer[self.chunk_max :]
|
||
|
|
|
||
|
|
if buffer.strip():
|
||
|
|
await outbound_adapter.send_text(
|
||
|
|
target_id,
|
||
|
|
buffer,
|
||
|
|
account_id=account_id,
|
||
|
|
)
|
||
|
|
message_ids.append(buffer)
|
||
|
|
|
||
|
|
return message_ids
|