Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
__pycache__/
dist/
poetry.toml
venv
22 changes: 10 additions & 12 deletions src/elevenlabs/conversational_ai/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
import base64
import json
import threading
from typing import Callable, Optional, Awaitable, Union, Any, Literal, Dict, Tuple
from typing import Callable, Optional, Awaitable, Union, Any, Literal
import asyncio
from concurrent.futures import ThreadPoolExecutor
from enum import Enum
from enum import StrEnum

from websockets.sync.client import connect, Connection
from websockets.sync.client import connect, ClientConnection
from websockets.exceptions import ConnectionClosedOK

from ..base_client import BaseElevenLabs


class ClientToOrchestratorEvent(str, Enum):
class ClientToOrchestratorEvent(StrEnum):
"""Event types that can be sent from client to orchestrator."""
# Response to a ping request.
PONG = "pong"
Expand Down Expand Up @@ -44,7 +44,7 @@ def to_dict(self) -> dict:
class UserActivityClientToOrchestratorEvent:
"""Event for registering user activity (ping to prevent timeout)."""

def __init__(self) -> None:
def __init__(self):
self.type: Literal[ClientToOrchestratorEvent.USER_ACTIVITY] = ClientToOrchestratorEvent.USER_ACTIVITY

def to_dict(self) -> dict:
Expand Down Expand Up @@ -118,8 +118,8 @@ class ClientTools:
ensuring non-blocking operation of the main conversation thread.
"""

def __init__(self) -> None:
self.tools: Dict[str, Tuple[Union[Callable[[dict], Any], Callable[[dict], Awaitable[Any]]], bool]] = {}
def __init__(self):
self.tools: dict[str, tuple[Union[Callable[[dict], Any], Callable[[dict], Awaitable[Any]]], bool]] = {}
self.lock = threading.Lock()
self._loop = None
self._thread = None
Expand Down Expand Up @@ -196,9 +196,6 @@ def execute_tool(self, tool_name: str, parameters: dict, callback: Callable[[dic
"""
if not self._running.is_set():
raise RuntimeError("ClientTools event loop is not running")

if self._loop is None:
raise RuntimeError("Event loop is not available")

async def _execute_and_callback():
try:
Expand Down Expand Up @@ -251,7 +248,7 @@ class Conversation:
_should_stop: threading.Event
_conversation_id: Optional[str]
_last_interrupt_id: int
_ws: Optional[Connection]
_ws: Optional[ClientConnection]

def __init__(
self,
Expand Down Expand Up @@ -299,10 +296,11 @@ def __init__(
self.client_tools.start()

self._thread = None
self._ws: Optional[Connection] = None
self._ws: Optional[ClientConnection] = None
self._should_stop = threading.Event()
self._conversation_id = None
self._last_interrupt_id = 0
self._ws = None

def start_session(self):
"""Starts the conversation session.
Expand Down
Loading