-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add WebSocketOptions for configurable WebSocket connections #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c1e168
feat: add WebSocketOptions for configurable WebSocket connections
twangodev 2bf07e8
style: improve formatting of WebSocketOptions kwargs conversion
twangodev c316875
test: add unit tests for WebSocketOptions and its integration in stre…
twangodev 14fc516
feat: add WebSocket options for configurable timeouts and message lim…
twangodev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """WebSocket-level options for WebSocket connections.""" | ||
|
|
||
| from typing import Any, Dict, Optional | ||
|
|
||
|
|
||
| class WebSocketOptions: | ||
| """ | ||
| Options that can be provided to configure WebSocket connections. | ||
|
|
||
| Attributes: | ||
| keepalive_ping_timeout_seconds: Maximum time to wait for a pong response | ||
| to a keepalive ping before considering the connection dead (default: 20s) | ||
| keepalive_ping_interval_seconds: Interval between keepalive pings (default: 20s) | ||
| max_message_size_bytes: Maximum size for incoming messages (default: 65,536 bytes) | ||
| queue_size: Size of the message receive queue (default: 512) | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| keepalive_ping_timeout_seconds: Optional[float] = None, | ||
| keepalive_ping_interval_seconds: Optional[float] = None, | ||
| max_message_size_bytes: Optional[int] = None, | ||
| queue_size: Optional[int] = None, | ||
| ): | ||
| self.keepalive_ping_timeout_seconds = keepalive_ping_timeout_seconds | ||
| self.keepalive_ping_interval_seconds = keepalive_ping_interval_seconds | ||
| self.max_message_size_bytes = max_message_size_bytes | ||
| self.queue_size = queue_size | ||
|
|
||
| def to_httpx_ws_kwargs(self) -> Dict[str, Any]: | ||
| """Convert to kwargs dict for httpx_ws aconnect_ws/connect_ws.""" | ||
| kwargs = {} | ||
| if self.keepalive_ping_timeout_seconds is not None: | ||
| kwargs["keepalive_ping_timeout_seconds"] = self.keepalive_ping_timeout_seconds | ||
| if self.keepalive_ping_interval_seconds is not None: | ||
| kwargs["keepalive_ping_interval_seconds"] = self.keepalive_ping_interval_seconds | ||
| if self.max_message_size_bytes is not None: | ||
| kwargs["max_message_size_bytes"] = self.max_message_size_bytes | ||
| if self.queue_size is not None: | ||
| kwargs["queue_size"] = self.queue_size | ||
| return kwargs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| from httpx_ws import AsyncWebSocketSession, WebSocketSession, aconnect_ws, connect_ws | ||
|
|
||
| from .realtime import aiter_websocket_audio, iter_websocket_audio | ||
| from ..core import AsyncClientWrapper, ClientWrapper, RequestOptions | ||
| from ..core import AsyncClientWrapper, ClientWrapper, RequestOptions, WebSocketOptions | ||
| from ..core.iterators import AsyncAudioStream, AudioStream | ||
| from ..types import ( | ||
| AudioFormat, | ||
|
|
@@ -215,6 +215,7 @@ def stream_websocket( | |
| config: TTSConfig = TTSConfig(), | ||
| model: Model = "s1", | ||
| max_workers: int = 10, | ||
| ws_options: Optional[WebSocketOptions] = None, | ||
| ) -> Iterator[bytes]: | ||
| """ | ||
| Stream text and receive audio in real-time via WebSocket. | ||
|
|
@@ -305,6 +306,9 @@ def text_generator(): | |
| speed, base=config.prosody | ||
| ) | ||
|
|
||
| # Prepare WebSocket connection kwargs | ||
| ws_kwargs = ws_options.to_httpx_ws_kwargs() if ws_options else {} | ||
|
|
||
| executor = ThreadPoolExecutor(max_workers=max_workers) | ||
|
|
||
| try: | ||
|
|
@@ -316,6 +320,7 @@ def text_generator(): | |
| "model": model, | ||
| "Authorization": f"Bearer {self._client.api_key}", | ||
| }, | ||
| **ws_kwargs, | ||
| ) as ws: | ||
|
|
||
| def sender(): | ||
|
|
@@ -502,6 +507,7 @@ async def stream_websocket( | |
| speed: Optional[float] = None, | ||
| config: TTSConfig = TTSConfig(), | ||
| model: Model = "s1", | ||
| ws_options: Optional[WebSocketOptions] = None, | ||
|
||
| ): | ||
| """ | ||
| Stream text and receive audio in real-time via WebSocket (async). | ||
|
|
@@ -591,11 +597,15 @@ async def text_generator(): | |
| speed, base=config.prosody | ||
| ) | ||
|
|
||
| # Prepare WebSocket connection kwargs | ||
| ws_kwargs = ws_options.to_httpx_ws_kwargs() if ws_options else {} | ||
|
|
||
| ws: AsyncWebSocketSession | ||
| async with aconnect_ws( | ||
| "/v1/tts/live", | ||
| client=self._client.client, | ||
| headers={"model": model, "Authorization": f"Bearer {self._client.api_key}"}, | ||
| **ws_kwargs, | ||
| ) as ws: | ||
|
|
||
| async def sender(): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ws_optionsparameter is missing from the method's docstring Args section. Please add documentation for this parameter to help users understand how to configure WebSocket connection options.