|
| 1 | +"""Custom bidi exceptions.""" |
| 2 | +from typing import Any |
| 3 | + |
| 4 | + |
| 5 | +class BidiExceptionChain(Exception): |
| 6 | + """Chain a list of exceptions together. |
| 7 | +
|
| 8 | + Useful for chaining together exceptions raised across a multi-step workflow (e.g., the bidi `stop` methods). |
| 9 | + Note, this exception is meant to mimic ExceptionGroup released in Python 3.11. |
| 10 | +
|
| 11 | + - Docs: https://docs.python.org/3/library/exceptions.html#ExceptionGroup |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, message: str, exceptions: list[Exception]) -> None: |
| 15 | + """Chain exceptions. |
| 16 | +
|
| 17 | + Args: |
| 18 | + message: Top-level exception message. |
| 19 | + exceptions: List of exceptions to chain. |
| 20 | + """ |
| 21 | + super().__init__(self, message) |
| 22 | + |
| 23 | + exceptions.append(self) |
| 24 | + for i in range(1, len(exceptions)): |
| 25 | + exceptions[i].__context__ = exceptions[i - 1] |
| 26 | + |
| 27 | + |
| 28 | +class BidiModelTimeoutError(Exception): |
| 29 | + """Model timeout error. |
| 30 | +
|
| 31 | + Bidirectional models are often configured with a connection time limit. Nova sonic for example keeps the connection |
| 32 | + open for 8 minutes max. Upon receiving a timeout, the agent loop is configured to restart the model connection so as |
| 33 | + to create a seamless, uninterrupted experience for the user. |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, message: str, **restart_config: Any) -> None: |
| 37 | + """Initialize error. |
| 38 | +
|
| 39 | + Args: |
| 40 | + message: Timeout message from model. |
| 41 | + **restart_config: Configure restart specific behaviors in the call to model start. |
| 42 | + """ |
| 43 | + super().__init__(self, message) |
| 44 | + |
| 45 | + self.restart_config = restart_config |
0 commit comments