Replace assert-based type narrowing with local variables and guards
Use local variable narrowing and if-guards instead of assert statements for type safety, since asserts are stripped with python -O. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1150,8 +1150,7 @@ class LLMAssistantAggregator(LLMContextAggregator):
|
|||||||
|
|
||||||
thought = concatenate_aggregated_text(self._thought_aggregation)
|
thought = concatenate_aggregated_text(self._thought_aggregation)
|
||||||
|
|
||||||
if self._thought_append_to_context:
|
if self._thought_append_to_context and self._thought_llm is not None:
|
||||||
assert self._thought_llm is not None, "llm is required when append_to_context is True"
|
|
||||||
self._context.add_message(
|
self._context.add_message(
|
||||||
LLMSpecificMessage(
|
LLMSpecificMessage(
|
||||||
llm=self._thought_llm,
|
llm=self._thought_llm,
|
||||||
|
|||||||
@@ -701,15 +701,16 @@ class BaseOutputTransport(FrameProcessor):
|
|||||||
# Notify the bot stopped speaking upstream if necessary.
|
# Notify the bot stopped speaking upstream if necessary.
|
||||||
await self._bot_stopped_speaking()
|
await self._bot_stopped_speaking()
|
||||||
|
|
||||||
async def with_mixer(vad_stop_secs: float) -> AsyncGenerator[Frame, None]:
|
async def with_mixer(
|
||||||
assert self._mixer is not None
|
mixer: BaseAudioMixer, vad_stop_secs: float
|
||||||
|
) -> AsyncGenerator[Frame, None]:
|
||||||
last_frame_time = 0
|
last_frame_time = 0
|
||||||
silence = b"\x00" * self._audio_chunk_size
|
silence = b"\x00" * self._audio_chunk_size
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
frame = self._audio_queue.get_nowait()
|
frame = self._audio_queue.get_nowait()
|
||||||
if isinstance(frame, OutputAudioRawFrame):
|
if isinstance(frame, OutputAudioRawFrame):
|
||||||
frame.audio = await self._mixer.mix(frame.audio)
|
frame.audio = await mixer.mix(frame.audio)
|
||||||
last_frame_time = time.time()
|
last_frame_time = time.time()
|
||||||
yield frame
|
yield frame
|
||||||
self._audio_queue.task_done()
|
self._audio_queue.task_done()
|
||||||
@@ -720,7 +721,7 @@ class BaseOutputTransport(FrameProcessor):
|
|||||||
await self._bot_stopped_speaking()
|
await self._bot_stopped_speaking()
|
||||||
# Generate an audio frame with only the mixer's part.
|
# Generate an audio frame with only the mixer's part.
|
||||||
frame = OutputAudioRawFrame(
|
frame = OutputAudioRawFrame(
|
||||||
audio=await self._mixer.mix(silence),
|
audio=await mixer.mix(silence),
|
||||||
sample_rate=self._sample_rate,
|
sample_rate=self._sample_rate,
|
||||||
num_channels=self._params.audio_out_channels,
|
num_channels=self._params.audio_out_channels,
|
||||||
)
|
)
|
||||||
@@ -732,7 +733,7 @@ class BaseOutputTransport(FrameProcessor):
|
|||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
if self._mixer:
|
if self._mixer:
|
||||||
return with_mixer(BOT_VAD_STOP_SECS)
|
return with_mixer(self._mixer, BOT_VAD_STOP_SECS)
|
||||||
else:
|
else:
|
||||||
return without_mixer(BOT_VAD_STOP_SECS)
|
return without_mixer(BOT_VAD_STOP_SECS)
|
||||||
|
|
||||||
|
|||||||
@@ -141,8 +141,8 @@ class WebsocketClientSession:
|
|||||||
self._client_task_handler(),
|
self._client_task_handler(),
|
||||||
f"{self._transport_name}::WebsocketClientSession::_client_task_handler",
|
f"{self._transport_name}::WebsocketClientSession::_client_task_handler",
|
||||||
)
|
)
|
||||||
assert self._websocket is not None
|
if self._websocket:
|
||||||
await self._callbacks.on_connected(self._websocket)
|
await self._callbacks.on_connected(self._websocket)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
logger.error(f"Timeout connecting to {self._uri}")
|
logger.error(f"Timeout connecting to {self._uri}")
|
||||||
|
|
||||||
@@ -193,17 +193,18 @@ class WebsocketClientSession:
|
|||||||
|
|
||||||
async def _client_task_handler(self):
|
async def _client_task_handler(self):
|
||||||
"""Handle incoming messages from the WebSocket connection."""
|
"""Handle incoming messages from the WebSocket connection."""
|
||||||
|
if not self._websocket:
|
||||||
|
return
|
||||||
|
|
||||||
|
websocket = self._websocket
|
||||||
try:
|
try:
|
||||||
assert self._websocket is not None
|
|
||||||
websocket = self._websocket
|
|
||||||
# Handle incoming messages
|
# Handle incoming messages
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
await self._callbacks.on_message(websocket, message)
|
await self._callbacks.on_message(websocket, message)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} exception receiving data: {e.__class__.__name__} ({e})")
|
logger.error(f"{self} exception receiving data: {e.__class__.__name__} ({e})")
|
||||||
|
|
||||||
if self._websocket:
|
await self._callbacks.on_disconnected(websocket)
|
||||||
await self._callbacks.on_disconnected(self._websocket)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation of the WebSocket client session."""
|
"""String representation of the WebSocket client session."""
|
||||||
|
|||||||
Reference in New Issue
Block a user