bakbak-python 1.0.0rc2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bakbak/__init__.py +77 -0
- bakbak/_base_client.py +642 -0
- bakbak/_client.py +219 -0
- bakbak/_constants.py +43 -0
- bakbak/_exceptions.py +410 -0
- bakbak/_models.py +93 -0
- bakbak/_response.py +51 -0
- bakbak/_retry.py +52 -0
- bakbak/_sse.py +68 -0
- bakbak/_streaming.py +432 -0
- bakbak/_types.py +79 -0
- bakbak/_utils.py +62 -0
- bakbak/_version.py +3 -0
- bakbak/_ws/__init__.py +1 -0
- bakbak/_ws/_async.py +657 -0
- bakbak/_ws/_common.py +176 -0
- bakbak/_ws/_core.py +819 -0
- bakbak/_ws/_sync.py +649 -0
- bakbak/_ws/events.py +67 -0
- bakbak/audio.py +75 -0
- bakbak/py.typed +0 -0
- bakbak/resources/__init__.py +6 -0
- bakbak/resources/tts.py +339 -0
- bakbak/resources/voices.py +139 -0
- bakbak/text.py +463 -0
- bakbak/types.py +100 -0
- bakbak/websocket.py +23 -0
- bakbak_python-1.0.0rc2.dist-info/METADATA +416 -0
- bakbak_python-1.0.0rc2.dist-info/RECORD +31 -0
- bakbak_python-1.0.0rc2.dist-info/WHEEL +4 -0
- bakbak_python-1.0.0rc2.dist-info/licenses/LICENSE +202 -0
bakbak/_ws/_async.py
ADDED
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
"""The asyncio WebSocket client: ``AsyncTTSWebSocket`` and its contexts.
|
|
2
|
+
|
|
3
|
+
One reader task per connection routes every message through the core, which
|
|
4
|
+
also keeps the connection answering the server's pings. Requests are written
|
|
5
|
+
under one send lock, so a context's messages leave in the order they were sent.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# The driver is the client's own internals.
|
|
9
|
+
# pyright: reportPrivateUsage=false
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import asyncio
|
|
14
|
+
import time
|
|
15
|
+
from collections.abc import (
|
|
16
|
+
AsyncGenerator,
|
|
17
|
+
AsyncIterable,
|
|
18
|
+
AsyncIterator,
|
|
19
|
+
Callable,
|
|
20
|
+
Coroutine,
|
|
21
|
+
Generator,
|
|
22
|
+
Iterable,
|
|
23
|
+
Mapping,
|
|
24
|
+
)
|
|
25
|
+
from types import TracebackType
|
|
26
|
+
from typing import TYPE_CHECKING, Any
|
|
27
|
+
|
|
28
|
+
from typing_extensions import Self, Unpack, override
|
|
29
|
+
from websockets.asyncio.client import ClientConnection, connect
|
|
30
|
+
from websockets.exceptions import ConnectionClosed
|
|
31
|
+
|
|
32
|
+
from .._constants import WS_CLOSE_TIMEOUT, WS_MAX_MESSAGE_BYTES
|
|
33
|
+
from .._exceptions import APIStatusError, BakbakError
|
|
34
|
+
from .._retry import retry_delay
|
|
35
|
+
from .._utils import logger, redact_url, user_agent
|
|
36
|
+
from ..text import Segmenter
|
|
37
|
+
from ..types import RequestDefaults, SendParams
|
|
38
|
+
from ._common import (
|
|
39
|
+
WEBSOCKET_LOGGER,
|
|
40
|
+
build_request,
|
|
41
|
+
check_context_id,
|
|
42
|
+
connect_error,
|
|
43
|
+
open_failure,
|
|
44
|
+
text_delta,
|
|
45
|
+
)
|
|
46
|
+
from ._core import Connection, Core, Outcome, Request, State
|
|
47
|
+
from .events import Chunk, Failed, RequestResult, WebSocketEvent
|
|
48
|
+
|
|
49
|
+
if TYPE_CHECKING:
|
|
50
|
+
from .._client import AsyncBakbak
|
|
51
|
+
|
|
52
|
+
__all__ = ["AsyncContext", "AsyncRequestHandle", "AsyncTTSWebSocket"]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class _Connect(connect):
|
|
56
|
+
"""``connect`` that never follows a redirect: the API never redirects, and
|
|
57
|
+
a redirect would carry ``X-API-Key`` to another origin (§8). A 3xx is
|
|
58
|
+
raised as ``InvalidStatus`` and becomes an ``APIStatusError``."""
|
|
59
|
+
|
|
60
|
+
@override
|
|
61
|
+
def process_redirect(self, exc: Exception) -> Exception | str:
|
|
62
|
+
return exc
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class _Signal:
|
|
66
|
+
"""An ``asyncio.Event`` used as a wake-up: check, then ``wait()``."""
|
|
67
|
+
|
|
68
|
+
__slots__ = ("_event",)
|
|
69
|
+
|
|
70
|
+
def __init__(self) -> None:
|
|
71
|
+
self._event = asyncio.Event()
|
|
72
|
+
|
|
73
|
+
def notify(self) -> None:
|
|
74
|
+
self._event.set()
|
|
75
|
+
|
|
76
|
+
async def wait(self) -> None:
|
|
77
|
+
# Callers check their condition first; nothing can run between that
|
|
78
|
+
# check and this clear, so no wake-up is lost.
|
|
79
|
+
self._event.clear()
|
|
80
|
+
await self._event.wait()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _signal(core_signal: object) -> _Signal:
|
|
84
|
+
assert isinstance(core_signal, _Signal)
|
|
85
|
+
return core_signal
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
_STOP = object()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
async def _iterate(
|
|
92
|
+
tokens: Iterable[object] | AsyncIterable[object],
|
|
93
|
+
) -> AsyncGenerator[object, None]:
|
|
94
|
+
if isinstance(tokens, AsyncIterable):
|
|
95
|
+
async for item in tokens:
|
|
96
|
+
yield item
|
|
97
|
+
else:
|
|
98
|
+
for item in tokens:
|
|
99
|
+
yield item
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class AsyncRequestHandle:
|
|
103
|
+
"""One request sent on a context.
|
|
104
|
+
|
|
105
|
+
``await handle`` (or ``await handle.result()``) waits for its outcome and
|
|
106
|
+
raises its error if it failed. ``async for audio in handle`` yields its
|
|
107
|
+
audio; that reads the context's events up to the end of this request, so
|
|
108
|
+
use either the handles or ``ctx.events()``/``ctx.audio()``, not both.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
__slots__ = ("_context", "_request")
|
|
112
|
+
|
|
113
|
+
def __init__(self, request: Request, context: AsyncContext) -> None:
|
|
114
|
+
self._request = request
|
|
115
|
+
self._context = context
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def context_id(self) -> str:
|
|
119
|
+
return self._context.context_id
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def request_id(self) -> str | None:
|
|
123
|
+
"""The server's id for the request, once its first message arrived."""
|
|
124
|
+
return self._request.request_id
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def audio_bytes(self) -> int:
|
|
128
|
+
"""Audio delivered so far."""
|
|
129
|
+
return self._request.audio_bytes
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def done(self) -> bool:
|
|
133
|
+
"""Whether the request has an outcome."""
|
|
134
|
+
return self._request.state is State.DONE
|
|
135
|
+
|
|
136
|
+
async def result(self, timeout: float | None = None) -> RequestResult:
|
|
137
|
+
"""Wait for the outcome.
|
|
138
|
+
|
|
139
|
+
Raises:
|
|
140
|
+
BakbakError: the request's error, if it failed.
|
|
141
|
+
TimeoutError: if ``timeout`` seconds pass first.
|
|
142
|
+
"""
|
|
143
|
+
request = self._request
|
|
144
|
+
|
|
145
|
+
async def finished() -> None:
|
|
146
|
+
while request.state is not State.DONE:
|
|
147
|
+
await _signal(request.signal).wait()
|
|
148
|
+
|
|
149
|
+
try:
|
|
150
|
+
await asyncio.wait_for(finished(), timeout)
|
|
151
|
+
except asyncio.TimeoutError:
|
|
152
|
+
raise TimeoutError("The request did not finish in time.") from None
|
|
153
|
+
if request.outcome is Outcome.FAILED and request.error is not None:
|
|
154
|
+
raise request.error
|
|
155
|
+
return RequestResult(
|
|
156
|
+
request.request_id, request.audio_bytes, cancelled=request.outcome is Outcome.CANCELLED
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def __await__(self) -> Generator[Any, None, RequestResult]:
|
|
160
|
+
return self.result().__await__()
|
|
161
|
+
|
|
162
|
+
def __aiter__(self) -> AsyncIterator[bytes]:
|
|
163
|
+
return self._audio()
|
|
164
|
+
|
|
165
|
+
async def _audio(self) -> AsyncIterator[bytes]:
|
|
166
|
+
core = self._context._ws._core
|
|
167
|
+
request = self._request
|
|
168
|
+
while True:
|
|
169
|
+
event, finished = core.pop_request_event(request)
|
|
170
|
+
if event is not None:
|
|
171
|
+
if isinstance(event, Chunk):
|
|
172
|
+
yield event.audio # pyright: ignore[reportUnknownMemberType]
|
|
173
|
+
elif isinstance(event, Failed):
|
|
174
|
+
raise event.error
|
|
175
|
+
else:
|
|
176
|
+
return
|
|
177
|
+
continue
|
|
178
|
+
if finished:
|
|
179
|
+
if request.outcome is Outcome.FAILED and request.error is not None:
|
|
180
|
+
raise request.error
|
|
181
|
+
return
|
|
182
|
+
await _signal(request.context.signal).wait()
|
|
183
|
+
|
|
184
|
+
@override
|
|
185
|
+
def __repr__(self) -> str:
|
|
186
|
+
return (
|
|
187
|
+
f"AsyncRequestHandle(context_id={self.context_id!r}, request_id={self.request_id!r}, "
|
|
188
|
+
f"done={self.done!r}, audio_bytes={self.audio_bytes})"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class AsyncContext:
|
|
193
|
+
"""A sequence of requests whose audio plays in order (BEHAVIOR.md §6.2).
|
|
194
|
+
|
|
195
|
+
Requests on one context run one after another; different contexts run at
|
|
196
|
+
the same time. Get one from ``ws.context()``.
|
|
197
|
+
"""
|
|
198
|
+
|
|
199
|
+
__slots__ = ("_ctx", "_defaults", "_ws", "context_id")
|
|
200
|
+
|
|
201
|
+
def __init__(self, ws: AsyncTTSWebSocket, context_id: str, defaults: dict[str, object]) -> None:
|
|
202
|
+
self._ws = ws
|
|
203
|
+
self.context_id = context_id
|
|
204
|
+
self._defaults = defaults
|
|
205
|
+
self._ctx = ws._core.context(context_id)
|
|
206
|
+
|
|
207
|
+
async def send(
|
|
208
|
+
self, *, extra_body: dict[str, object] | None = None, **params: Unpack[SendParams]
|
|
209
|
+
) -> AsyncRequestHandle:
|
|
210
|
+
"""Send one request. Fields left out come from the context's and the
|
|
211
|
+
WebSocket's defaults; ``extra_body`` adds fields the SDK does not
|
|
212
|
+
model yet.
|
|
213
|
+
|
|
214
|
+
It waits only while the connection's limits are reached (64 unfinished
|
|
215
|
+
requests, or 16 busy contexts); a server-side failure does not raise
|
|
216
|
+
here but becomes the request's outcome.
|
|
217
|
+
"""
|
|
218
|
+
handle = await self._send(params, extra_body, None)
|
|
219
|
+
assert handle is not None
|
|
220
|
+
return handle
|
|
221
|
+
|
|
222
|
+
async def _send(
|
|
223
|
+
self,
|
|
224
|
+
params: Mapping[str, object],
|
|
225
|
+
extra_body: Mapping[str, object] | None,
|
|
226
|
+
epoch: int | None,
|
|
227
|
+
) -> AsyncRequestHandle | None:
|
|
228
|
+
"""Send one request; with ``epoch``, send nothing (and return None) if
|
|
229
|
+
the context was cancelled since that epoch."""
|
|
230
|
+
ws = self._ws
|
|
231
|
+
core = ws._core
|
|
232
|
+
ctx = self._ctx
|
|
233
|
+
wire, width = build_request(
|
|
234
|
+
{**ws._defaults, **self._defaults}, params, extra_body, self.context_id
|
|
235
|
+
)
|
|
236
|
+
while True:
|
|
237
|
+
if core.closed:
|
|
238
|
+
raise BakbakError("This WebSocket is closed.")
|
|
239
|
+
if ctx.input_ended:
|
|
240
|
+
raise BakbakError("end_input() was called on this context; use a new context.")
|
|
241
|
+
if epoch is not None and ctx.cancel_epoch != epoch:
|
|
242
|
+
return None
|
|
243
|
+
if not core.at_capacity(ctx):
|
|
244
|
+
break
|
|
245
|
+
await _signal(core.capacity).wait()
|
|
246
|
+
request = core.submit(ctx, wire, width, lambda r: AsyncRequestHandle(r, self))
|
|
247
|
+
try:
|
|
248
|
+
# The flush runs in its own task: cancelling this call cannot cut a
|
|
249
|
+
# batch of writes short, so the core always matches the wire.
|
|
250
|
+
await asyncio.shield(ws._spawn(ws._flush()))
|
|
251
|
+
except asyncio.CancelledError:
|
|
252
|
+
# The caller gave up before getting the handle. If the request was
|
|
253
|
+
# not written yet, take it back: nothing it abandoned is sent or
|
|
254
|
+
# billed. If it was, its outcome still reaches the context.
|
|
255
|
+
core.withdraw(request)
|
|
256
|
+
raise
|
|
257
|
+
handle: AsyncRequestHandle = request.handle
|
|
258
|
+
return handle
|
|
259
|
+
|
|
260
|
+
async def send_stream(
|
|
261
|
+
self,
|
|
262
|
+
tokens: Iterable[object] | AsyncIterable[object],
|
|
263
|
+
*,
|
|
264
|
+
end_input: bool = True,
|
|
265
|
+
segmenter: Segmenter | None = None,
|
|
266
|
+
**params: Unpack[RequestDefaults],
|
|
267
|
+
) -> list[AsyncRequestHandle]:
|
|
268
|
+
"""Send text that arrives in pieces, such as LLM tokens, sentence by
|
|
269
|
+
sentence as each sentence is final.
|
|
270
|
+
|
|
271
|
+
``tokens`` may be an iterable or an async iterable of ``str``, or of
|
|
272
|
+
stream events from an LLM SDK (OpenAI, Anthropic or Gemini style; the
|
|
273
|
+
text delta is found by duck typing). A sync iterable is read on the
|
|
274
|
+
event loop, so it should not block. Calls ``end_input()`` at the end
|
|
275
|
+
unless ``end_input=False``, also when ``tokens`` raises.
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
segmenter: a configured :class:`bakbak.text.Segmenter`, e.g. with
|
|
279
|
+
``eager_first_clause=True`` for an earlier first request.
|
|
280
|
+
"""
|
|
281
|
+
splitter = segmenter or Segmenter()
|
|
282
|
+
handles: list[AsyncRequestHandle] = []
|
|
283
|
+
epoch = self._ctx.cancel_epoch
|
|
284
|
+
items = _iterate(tokens)
|
|
285
|
+
racing = isinstance(tokens, AsyncIterable)
|
|
286
|
+
try:
|
|
287
|
+
while True:
|
|
288
|
+
item = await self._next_item(items, epoch, racing)
|
|
289
|
+
if item is _STOP:
|
|
290
|
+
break
|
|
291
|
+
text = text_delta(item)
|
|
292
|
+
if not text:
|
|
293
|
+
continue
|
|
294
|
+
for sentence in splitter.push(text):
|
|
295
|
+
handle = await self._send({**params, "text": sentence}, None, epoch)
|
|
296
|
+
if handle is None:
|
|
297
|
+
return handles
|
|
298
|
+
handles.append(handle)
|
|
299
|
+
for sentence in splitter.flush():
|
|
300
|
+
handle = await self._send({**params, "text": sentence}, None, epoch)
|
|
301
|
+
if handle is None:
|
|
302
|
+
return handles
|
|
303
|
+
handles.append(handle)
|
|
304
|
+
finally:
|
|
305
|
+
await items.aclose()
|
|
306
|
+
if end_input:
|
|
307
|
+
self.end_input()
|
|
308
|
+
return handles
|
|
309
|
+
|
|
310
|
+
async def _next_item(
|
|
311
|
+
self, items: AsyncGenerator[object, None], epoch: int, racing: bool
|
|
312
|
+
) -> object:
|
|
313
|
+
"""The next token, or ``_STOP`` when the tokens end or the context is
|
|
314
|
+
cancelled: a cancel stops ``send_stream`` even while it waits for a
|
|
315
|
+
slow LLM (§6.3)."""
|
|
316
|
+
ctx = self._ctx
|
|
317
|
+
if ctx.cancel_epoch != epoch:
|
|
318
|
+
return _STOP
|
|
319
|
+
if not racing:
|
|
320
|
+
try:
|
|
321
|
+
return await items.__anext__()
|
|
322
|
+
except StopAsyncIteration:
|
|
323
|
+
return _STOP
|
|
324
|
+
getter = asyncio.ensure_future(items.__anext__())
|
|
325
|
+
watcher = asyncio.ensure_future(self._cancelled_since(epoch))
|
|
326
|
+
try:
|
|
327
|
+
await asyncio.wait({getter, watcher}, return_when=asyncio.FIRST_COMPLETED)
|
|
328
|
+
finally:
|
|
329
|
+
watcher.cancel()
|
|
330
|
+
if not getter.done():
|
|
331
|
+
# Stop waiting for the token, and let the generator finish
|
|
332
|
+
# before it is closed.
|
|
333
|
+
getter.cancel()
|
|
334
|
+
await asyncio.gather(getter, return_exceptions=True)
|
|
335
|
+
if ctx.cancel_epoch != epoch or getter.cancelled():
|
|
336
|
+
return _STOP
|
|
337
|
+
try:
|
|
338
|
+
return getter.result()
|
|
339
|
+
except StopAsyncIteration:
|
|
340
|
+
return _STOP
|
|
341
|
+
|
|
342
|
+
async def _cancelled_since(self, epoch: int) -> None:
|
|
343
|
+
core = self._ws._core
|
|
344
|
+
while self._ctx.cancel_epoch == epoch and not core.closed:
|
|
345
|
+
await _signal(self._ctx.signal).wait()
|
|
346
|
+
|
|
347
|
+
def end_input(self) -> None:
|
|
348
|
+
"""No more requests: ``events()`` and ``audio()`` end once every
|
|
349
|
+
request has an outcome."""
|
|
350
|
+
self._ws._core.end_input(self._ctx)
|
|
351
|
+
|
|
352
|
+
async def cancel(self) -> None:
|
|
353
|
+
"""Cancel every unfinished request of this context.
|
|
354
|
+
|
|
355
|
+
Their audio stops at once (what arrives before the server's
|
|
356
|
+
acknowledgement is discarded) and each ends as ``Cancelled``. Returns
|
|
357
|
+
when the server has acknowledged; the context is then ready for new
|
|
358
|
+
requests.
|
|
359
|
+
"""
|
|
360
|
+
ws = self._ws
|
|
361
|
+
core = ws._core
|
|
362
|
+
conn = core.cancel(self._ctx)
|
|
363
|
+
if conn is not None:
|
|
364
|
+
# The cancel must reach the wire even if this task is cancelled
|
|
365
|
+
# (barge-in often lands while an error is being handled).
|
|
366
|
+
await asyncio.shield(ws._spawn(ws._write(conn, core.cancel_message(self._ctx))))
|
|
367
|
+
while self._ctx.cancels_pending and not core.closed:
|
|
368
|
+
await _signal(self._ctx.cancel_signal).wait()
|
|
369
|
+
|
|
370
|
+
async def events(self) -> AsyncIterator[WebSocketEvent[AsyncRequestHandle]]:
|
|
371
|
+
"""Yield ``Chunk``, ``Done``, ``Failed`` and ``Cancelled`` in order.
|
|
372
|
+
|
|
373
|
+
Ends after ``end_input()`` once every request has its outcome."""
|
|
374
|
+
core = self._ws._core
|
|
375
|
+
ctx = self._ctx
|
|
376
|
+
while True:
|
|
377
|
+
event = core.pop_event(ctx)
|
|
378
|
+
if event is not None:
|
|
379
|
+
yield event
|
|
380
|
+
continue
|
|
381
|
+
if core.events_finished(ctx):
|
|
382
|
+
return
|
|
383
|
+
await _signal(ctx.signal).wait()
|
|
384
|
+
|
|
385
|
+
async def audio(self) -> AsyncIterator[bytes]:
|
|
386
|
+
"""Yield only the audio, and raise the first failure."""
|
|
387
|
+
async for event in self.events():
|
|
388
|
+
if isinstance(event, Chunk):
|
|
389
|
+
yield event.audio
|
|
390
|
+
elif isinstance(event, Failed):
|
|
391
|
+
raise event.error
|
|
392
|
+
|
|
393
|
+
@override
|
|
394
|
+
def __repr__(self) -> str:
|
|
395
|
+
return f"AsyncContext(context_id={self.context_id!r})"
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
class AsyncTTSWebSocket:
|
|
399
|
+
"""Many streaming requests over one WebSocket (BEHAVIOR.md §6).
|
|
400
|
+
|
|
401
|
+
``await client.tts.websocket()`` (or ``await ws.connect()``, or
|
|
402
|
+
``async with``) opens it ahead of time so the first request pays no
|
|
403
|
+
handshake; otherwise the first send opens it. Reconnects, resends and
|
|
404
|
+
holds are handled for you; every unfinished request gets an outcome when a
|
|
405
|
+
connection is lost.
|
|
406
|
+
"""
|
|
407
|
+
|
|
408
|
+
def __init__(
|
|
409
|
+
self,
|
|
410
|
+
client: AsyncBakbak,
|
|
411
|
+
defaults: RequestDefaults,
|
|
412
|
+
on_error: Callable[[APIStatusError], None] | None,
|
|
413
|
+
) -> None:
|
|
414
|
+
self._client = client
|
|
415
|
+
self._defaults: dict[str, object] = dict(defaults)
|
|
416
|
+
self._on_error = on_error
|
|
417
|
+
self._core = Core(self, max_retries=client.max_retries, read_timeout=client.timeout.read)
|
|
418
|
+
self._send_lock = asyncio.Lock()
|
|
419
|
+
self._connect_lock = asyncio.Lock()
|
|
420
|
+
self._tasks: set[asyncio.Task[None]] = set()
|
|
421
|
+
self._watchdog: asyncio.Task[None] | None = None
|
|
422
|
+
|
|
423
|
+
# Public API
|
|
424
|
+
|
|
425
|
+
@property
|
|
426
|
+
def url(self) -> str:
|
|
427
|
+
"""The WebSocket URL (it never contains the API key)."""
|
|
428
|
+
return self._client.websocket_url
|
|
429
|
+
|
|
430
|
+
@property
|
|
431
|
+
def connected(self) -> bool:
|
|
432
|
+
"""Whether a connection is open and accepting requests."""
|
|
433
|
+
return self._core.usable()
|
|
434
|
+
|
|
435
|
+
def __await__(self) -> Generator[Any, None, Self]:
|
|
436
|
+
return self.connect().__await__()
|
|
437
|
+
|
|
438
|
+
async def __aenter__(self) -> Self:
|
|
439
|
+
return await self.connect()
|
|
440
|
+
|
|
441
|
+
async def __aexit__(
|
|
442
|
+
self,
|
|
443
|
+
exc_type: type[BaseException] | None,
|
|
444
|
+
exc: BaseException | None,
|
|
445
|
+
tb: TracebackType | None,
|
|
446
|
+
) -> None:
|
|
447
|
+
await self.close()
|
|
448
|
+
|
|
449
|
+
async def connect(self) -> Self:
|
|
450
|
+
"""Open the connection now, so the first request pays no handshake.
|
|
451
|
+
|
|
452
|
+
Raises:
|
|
453
|
+
AuthenticationError: the key was refused (or another
|
|
454
|
+
``APIStatusError`` for another refusal).
|
|
455
|
+
APIConnectionError: the server could not be reached.
|
|
456
|
+
"""
|
|
457
|
+
if self._core.closed:
|
|
458
|
+
raise BakbakError("This WebSocket is closed.")
|
|
459
|
+
async with self._connect_lock:
|
|
460
|
+
if not self._core.usable():
|
|
461
|
+
await self._open()
|
|
462
|
+
return self
|
|
463
|
+
|
|
464
|
+
def context(
|
|
465
|
+
self, context_id: str | None = None, **defaults: Unpack[RequestDefaults]
|
|
466
|
+
) -> AsyncContext:
|
|
467
|
+
"""A context: requests on it play in order.
|
|
468
|
+
|
|
469
|
+
Args:
|
|
470
|
+
context_id: 1 to 128 characters of ``A-Z a-z 0-9 . _ -``; a random
|
|
471
|
+
``ctx_…`` id when left out.
|
|
472
|
+
**defaults: request fields for this context, over the WebSocket's.
|
|
473
|
+
"""
|
|
474
|
+
if self._core.closed:
|
|
475
|
+
raise BakbakError("This WebSocket is closed.")
|
|
476
|
+
return AsyncContext(self, check_context_id(context_id), dict(defaults))
|
|
477
|
+
|
|
478
|
+
async def close(self) -> None:
|
|
479
|
+
"""Close the WebSocket. Unfinished requests fail with
|
|
480
|
+
``WebSocketClosedError``."""
|
|
481
|
+
connections = self._core.close()
|
|
482
|
+
await asyncio.gather(*(self._close_transport(c.transport) for c in connections))
|
|
483
|
+
current = asyncio.current_task()
|
|
484
|
+
pending = [t for t in self._tasks if t is not current]
|
|
485
|
+
for task in pending:
|
|
486
|
+
task.cancel()
|
|
487
|
+
if pending:
|
|
488
|
+
await asyncio.gather(*pending, return_exceptions=True)
|
|
489
|
+
|
|
490
|
+
@override
|
|
491
|
+
def __repr__(self) -> str:
|
|
492
|
+
return f"AsyncTTSWebSocket(url={self.url!r}, connected={self.connected!r})"
|
|
493
|
+
|
|
494
|
+
# Driver: called by the core, never blocking
|
|
495
|
+
|
|
496
|
+
def new_signal(self) -> _Signal:
|
|
497
|
+
return _Signal()
|
|
498
|
+
|
|
499
|
+
def schedule_flush(self) -> None:
|
|
500
|
+
self._spawn(self._flush())
|
|
501
|
+
|
|
502
|
+
def schedule_retry(self, request: Request, delay: float) -> None:
|
|
503
|
+
self._spawn(self._retry_later(request, delay))
|
|
504
|
+
|
|
505
|
+
def schedule_reconnect(self, delay: float, *, force: bool) -> None:
|
|
506
|
+
self._spawn(self._reconnect_later(delay, force))
|
|
507
|
+
|
|
508
|
+
def abort(self, connection: Connection) -> None:
|
|
509
|
+
self._spawn(self._close_transport(connection.transport))
|
|
510
|
+
|
|
511
|
+
def notice(self, error: APIStatusError) -> None:
|
|
512
|
+
if self._on_error is None:
|
|
513
|
+
logger.debug("WebSocket error about no request: %s", error.message)
|
|
514
|
+
return
|
|
515
|
+
try:
|
|
516
|
+
self._on_error(error)
|
|
517
|
+
except Exception:
|
|
518
|
+
logger.debug("the on_error callback raised", exc_info=True)
|
|
519
|
+
|
|
520
|
+
# Internals
|
|
521
|
+
|
|
522
|
+
def _spawn(self, coro: Coroutine[Any, Any, None]) -> asyncio.Task[None]:
|
|
523
|
+
task = asyncio.get_running_loop().create_task(coro)
|
|
524
|
+
self._tasks.add(task)
|
|
525
|
+
task.add_done_callback(self._tasks.discard)
|
|
526
|
+
return task
|
|
527
|
+
|
|
528
|
+
async def _open(self) -> None:
|
|
529
|
+
"""Open a connection, retrying what can be retried."""
|
|
530
|
+
url = self.url
|
|
531
|
+
retries = 0
|
|
532
|
+
while True:
|
|
533
|
+
started = time.monotonic()
|
|
534
|
+
try:
|
|
535
|
+
transport = await _Connect(
|
|
536
|
+
url,
|
|
537
|
+
additional_headers=self._client.websocket_headers(),
|
|
538
|
+
user_agent_header=user_agent(),
|
|
539
|
+
compression=None,
|
|
540
|
+
open_timeout=self._client.timeout.connect,
|
|
541
|
+
close_timeout=WS_CLOSE_TIMEOUT,
|
|
542
|
+
max_size=WS_MAX_MESSAGE_BYTES,
|
|
543
|
+
logger=WEBSOCKET_LOGGER,
|
|
544
|
+
)
|
|
545
|
+
except Exception as exc:
|
|
546
|
+
mapped = connect_error(exc)
|
|
547
|
+
if mapped is None:
|
|
548
|
+
raise
|
|
549
|
+
error, retry_after = mapped
|
|
550
|
+
else:
|
|
551
|
+
break
|
|
552
|
+
# Raised outside the except block: the error carries no chained
|
|
553
|
+
# exception from the WebSocket library.
|
|
554
|
+
if not getattr(error, "retryable", False) or retries >= self._core.max_retries:
|
|
555
|
+
raise error
|
|
556
|
+
retries += 1
|
|
557
|
+
delay = retry_delay(retries, retry_after)
|
|
558
|
+
logger.debug("WebSocket connect failed (%s); retrying in %.2f s", error.message, delay)
|
|
559
|
+
await self._client._sleep(delay)
|
|
560
|
+
logger.debug(
|
|
561
|
+
"WebSocket connected to %s in %.0f ms",
|
|
562
|
+
redact_url(url),
|
|
563
|
+
(time.monotonic() - started) * 1000,
|
|
564
|
+
)
|
|
565
|
+
if self._core.closed: # closed while the handshake ran
|
|
566
|
+
await self._close_transport(transport)
|
|
567
|
+
return
|
|
568
|
+
conn = self._core.opened(transport)
|
|
569
|
+
self._spawn(self._read(conn))
|
|
570
|
+
self._ensure_watchdog()
|
|
571
|
+
|
|
572
|
+
async def _flush(self, force_connect: bool = False) -> None:
|
|
573
|
+
"""Open a connection if a request needs one, then send what can go."""
|
|
574
|
+
core = self._core
|
|
575
|
+
|
|
576
|
+
def needed() -> bool:
|
|
577
|
+
return (
|
|
578
|
+
not core.closed and not core.usable() and (force_connect or core.needs_connection())
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
if needed():
|
|
582
|
+
async with self._connect_lock:
|
|
583
|
+
if needed():
|
|
584
|
+
try:
|
|
585
|
+
await self._open()
|
|
586
|
+
except Exception as exc:
|
|
587
|
+
core.fail_waiting(open_failure(exc))
|
|
588
|
+
return
|
|
589
|
+
async with self._send_lock:
|
|
590
|
+
for conn, wire in core.take_sendable():
|
|
591
|
+
if not await self._send(conn, wire):
|
|
592
|
+
break
|
|
593
|
+
|
|
594
|
+
async def _write(self, conn: Connection, wire: str) -> None:
|
|
595
|
+
async with self._send_lock:
|
|
596
|
+
await self._send(conn, wire)
|
|
597
|
+
|
|
598
|
+
async def _send(self, conn: Connection, wire: str) -> bool:
|
|
599
|
+
transport: ClientConnection = conn.transport
|
|
600
|
+
try:
|
|
601
|
+
await transport.send(wire)
|
|
602
|
+
except Exception:
|
|
603
|
+
# The reader sees the close and resends or fails what was in flight.
|
|
604
|
+
logger.debug("WebSocket send failed on connection %d", conn.id, exc_info=True)
|
|
605
|
+
await self._close_transport(transport)
|
|
606
|
+
return False
|
|
607
|
+
return True
|
|
608
|
+
|
|
609
|
+
async def _read(self, conn: Connection) -> None:
|
|
610
|
+
transport: ClientConnection = conn.transport
|
|
611
|
+
try:
|
|
612
|
+
async for message in transport:
|
|
613
|
+
if isinstance(message, str):
|
|
614
|
+
self._core.on_message(conn, message)
|
|
615
|
+
except ConnectionClosed:
|
|
616
|
+
pass
|
|
617
|
+
except Exception:
|
|
618
|
+
logger.debug("WebSocket reader failed on connection %d", conn.id, exc_info=True)
|
|
619
|
+
finally:
|
|
620
|
+
if transport.close_code is None:
|
|
621
|
+
await self._close_transport(transport)
|
|
622
|
+
self._core.on_close(conn, transport.close_code or 1006, transport.close_reason or "")
|
|
623
|
+
|
|
624
|
+
async def _close_transport(self, transport: ClientConnection) -> None:
|
|
625
|
+
"""Close within about a second, even if the peer never answers."""
|
|
626
|
+
try:
|
|
627
|
+
await asyncio.wait_for(transport.close(), WS_CLOSE_TIMEOUT + 0.25)
|
|
628
|
+
except Exception:
|
|
629
|
+
logger.debug("closing a WebSocket failed; aborting it", exc_info=True)
|
|
630
|
+
transport.transport.abort()
|
|
631
|
+
|
|
632
|
+
async def _retry_later(self, request: Request, delay: float) -> None:
|
|
633
|
+
await self._client._sleep(delay)
|
|
634
|
+
self._core.retry_due(request)
|
|
635
|
+
|
|
636
|
+
async def _reconnect_later(self, delay: float, force: bool) -> None:
|
|
637
|
+
if delay:
|
|
638
|
+
await self._client._sleep(delay)
|
|
639
|
+
await self._flush(force_connect=force)
|
|
640
|
+
|
|
641
|
+
def _ensure_watchdog(self) -> None:
|
|
642
|
+
if self._core.read_timeout is None:
|
|
643
|
+
return
|
|
644
|
+
if self._watchdog is None or self._watchdog.done():
|
|
645
|
+
self._watchdog = asyncio.get_running_loop().create_task(self._watch())
|
|
646
|
+
self._tasks.add(self._watchdog)
|
|
647
|
+
self._watchdog.add_done_callback(self._tasks.discard)
|
|
648
|
+
|
|
649
|
+
async def _watch(self) -> None:
|
|
650
|
+
"""Fail requests that hear nothing for the read timeout."""
|
|
651
|
+
core = self._core
|
|
652
|
+
idle = core.read_timeout or 60.0
|
|
653
|
+
while not core.closed and core.connections:
|
|
654
|
+
wait, sends = core.check_timeouts()
|
|
655
|
+
for conn, wire in sends:
|
|
656
|
+
await self._write(conn, wire)
|
|
657
|
+
await asyncio.sleep(idle if wait is None else min(wait, idle))
|