-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream.py
More file actions
93 lines (74 loc) · 2.75 KB
/
stream.py
File metadata and controls
93 lines (74 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
import base64
from typing import TYPE_CHECKING, AsyncIterable, BinaryIO, Iterable, Union
if TYPE_CHECKING:
from .rpc import AsyncRpcClient
Streamable = Union[AsyncIterable[bytes], Iterable[bytes], BinaryIO]
class AsyncStreamWriter:
"""Manages writing a stream to the server."""
def __init__(self, rpc: AsyncRpcClient, stream_id: int):
self._rpc = rpc
self._stream_id = stream_id
async def start(self) -> None:
"""Send $sandbox.stream.start message."""
await self._rpc.send_notification(
"$sandbox.stream.start", {"streamId": self._stream_id}
)
async def enqueue(self, data: bytes) -> None:
"""Send $sandbox.stream.enqueue message with base64-encoded data."""
await self._rpc.send_notification(
"$sandbox.stream.enqueue",
{
"streamId": self._stream_id,
"data": base64.b64encode(data).decode("ascii"),
},
)
async def end(self) -> None:
"""Send $sandbox.stream.end message."""
await self._rpc.send_notification(
"$sandbox.stream.end", {"streamId": self._stream_id}
)
async def error(self, message: str) -> None:
"""Send $sandbox.stream.error message."""
await self._rpc.send_notification(
"$sandbox.stream.error", {"streamId": self._stream_id, "error": message}
)
async def stream_data(
rpc: AsyncRpcClient, data: Streamable, chunk_size: int = 64 * 1024
) -> int:
"""
Stream data to server. Returns stream_id.
Sends: start → enqueue(s) → end
On error: sends error message then re-raises
"""
stream_id = rpc.next_stream_id()
writer = AsyncStreamWriter(rpc, stream_id)
try:
await writer.start()
if hasattr(data, "read"):
# File-like object
while True:
chunk = data.read(chunk_size) # type: ignore[union-attr]
if not chunk:
break
await writer.enqueue(chunk)
elif hasattr(data, "__aiter__"):
# Async iterable
async for chunk in data: # type: ignore[union-attr]
await writer.enqueue(chunk)
else:
# Sync iterable
for chunk in data: # type: ignore[union-attr]
await writer.enqueue(chunk)
await writer.end()
return stream_id
except Exception as e:
await writer.error(str(e))
raise
def is_streamable(obj: object) -> bool:
"""Check if object is streamable (not bytes)."""
return (
hasattr(obj, "read")
or hasattr(obj, "__aiter__")
or (hasattr(obj, "__iter__") and not isinstance(obj, (bytes, str, dict, list)))
)