Skip to content

Adding Cancellation requests to websockets#7

Open
jay2jp wants to merge 5 commits intocartesia-ai:mainfrom
Cambio-money:main
Open

Adding Cancellation requests to websockets#7
jay2jp wants to merge 5 commits intocartesia-ai:mainfrom
Cambio-money:main

Conversation

@jay2jp
Copy link

@jay2jp jay2jp commented Oct 29, 2024

WebSocket Cancellation Implementation

Added cancellation functionality to the WebSocket implementation, following the Cartesia API documentation.

Changes

  1. Added new cancel method to _WebSocket class that implements the cancellation API described in:
    https://docs.cartesia.ai/api-reference/tts/working-with-web-sockets/contexts#cancelling-requests

  2. Created separate request handling for cancellation to maintain API clarity:

    • Created new _construct_tts_request_cancel function to handle cancel requests
    • Kept cancellation separate from send method to preserve required parameters (model_id, transcript, output_format)
    • This aligns with the API's OR condition described in https://docs.cartesia.ai/api-reference/tts/tts#send

Implementation Notes

  • Only implemented for _WebSocket class as it's our primary websocket implementation
  • Did not implement for _TTSContext class due to limited experience with that component
  • All changes have been tested in _WebSocket class and are working as expected

Documentation

i have added lines towards the end of the read me showing how this works

Copy link
Member

@kbrgl kbrgl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jay2jp! Thanks for your contribution! Your PR looks good. Can you add the cancel function to the TTSContext as well as the Async equivalents (AsyncWebsocket, AsyncTTSContext)?

It would look something like this:

def cancel(self):  # Add this method to the _TTSContext class.
        """Cancel the context. This will close the context and stop any ongoing audio generation."""
        self._websocket.cancel(self._context_id)

The PR will also have to pass CI checks before it can be merged.

self.connect()

if context_id is None:
#context_id = str(uuid.uuid4())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove comment


request_body = _construct_tts_request_cancel(context_id=context_id)
self.websocket.send(json.dumps(request_body))
return True No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add final newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants