-
Notifications
You must be signed in to change notification settings - Fork 173
elevenlabs check if json.error exists and throws error #787
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/agents-plugin-elevenlabs': patch | ||
| --- | ||
|
|
||
| check for errors in elevenlabs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-FileCopyrightText: 2025 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * Base error class for ElevenLabs-specific exceptions | ||
| */ | ||
| export class ElevenLabsError extends Error { | ||
| public readonly statusCode?: number; | ||
| public readonly body?: unknown; | ||
|
|
||
| constructor({ | ||
| message, | ||
| statusCode, | ||
| body, | ||
| }: { | ||
| message?: string; | ||
| statusCode?: number; | ||
| body?: unknown; | ||
| }) { | ||
| super(buildMessage({ message, statusCode, body })); | ||
| Object.setPrototypeOf(this, ElevenLabsError.prototype); | ||
| this.statusCode = statusCode; | ||
| this.body = body; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Error thrown when a request to ElevenLabs times out | ||
| */ | ||
| export class ElevenLabsTimeoutError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| Object.setPrototypeOf(this, ElevenLabsTimeoutError.prototype); | ||
toubatbrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Error thrown when WebSocket connection fails | ||
| */ | ||
| export class ElevenLabsConnectionError extends ElevenLabsError { | ||
| public readonly retries: number; | ||
|
|
||
| constructor({ | ||
| message, | ||
| retries, | ||
| statusCode, | ||
| body, | ||
| }: { | ||
| message?: string; | ||
| retries: number; | ||
| statusCode?: number; | ||
| body?: unknown; | ||
| }) { | ||
| super({ message, statusCode, body }); | ||
| Object.setPrototypeOf(this, ElevenLabsConnectionError.prototype); | ||
toubatbrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.retries = retries; | ||
| } | ||
| } | ||
|
|
||
| function buildMessage({ | ||
| message, | ||
| statusCode, | ||
| body, | ||
| }: { | ||
| message: string | undefined; | ||
| statusCode: number | undefined; | ||
| body: unknown | undefined; | ||
| }): string { | ||
| const lines: string[] = []; | ||
| if (message != null) { | ||
| lines.push(message); | ||
| } | ||
|
|
||
| if (statusCode != null) { | ||
| lines.push(`Status code: ${statusCode.toString()}`); | ||
| } | ||
|
|
||
| if (body != null) { | ||
| lines.push(`Body: ${JSON.stringify(body, undefined, 2)}`); | ||
| } | ||
|
|
||
| return lines.join('\n'); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| import type { AudioFrame } from '@livekit/rtc-node'; | ||
| import { URL } from 'node:url'; | ||
| import { type RawData, WebSocket } from 'ws'; | ||
| import { ElevenLabsConnectionError, ElevenLabsError } from './errors.js'; | ||
| import type { TTSEncoding, TTSModels } from './models.js'; | ||
|
|
||
| const DEFAULT_INACTIVITY_TIMEOUT = 300; | ||
|
|
@@ -213,7 +214,10 @@ export class SynthesizeStream extends tts.SynthesizeStream { | |
| break; | ||
| } catch (e) { | ||
| if (retries >= maxRetry) { | ||
| throw new Error(`failed to connect to ElevenLabs after ${retries} attempts: ${e}`); | ||
| throw new ElevenLabsConnectionError({ | ||
| message: `Failed to connect to ElevenLabs after ${retries} attempts: ${e}`, | ||
| retries, | ||
| }); | ||
| } | ||
|
|
||
| const delay = Math.min(retries * 5, 5); | ||
|
|
@@ -298,6 +302,12 @@ export class SynthesizeStream extends tts.SynthesizeStream { | |
| }); | ||
| }).then((msg) => { | ||
| const json = JSON.parse(msg.toString()); | ||
| if (json.error) { | ||
| throw new ElevenLabsError({ | ||
| message: json.error, | ||
| body: json, | ||
| }); | ||
| } | ||
| // remove the "audio" field from the json object when printing | ||
| if ('audio' in json && json.audio !== null) { | ||
| const data = new Int8Array(Buffer.from(json.audio, 'base64')); | ||
|
|
@@ -324,6 +334,7 @@ export class SynthesizeStream extends tts.SynthesizeStream { | |
| // skip log error for normal websocket close | ||
| if (err instanceof Error && !err.message.includes('WebSocket closed')) { | ||
| this.#logger.error({ err }, 'Error in listenTask from ElevenLabs WebSocket'); | ||
| throw err; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could throw only on ElevenLabsError, but I can't see a reason why other errors should be "silently ignored"?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to reuse classes defined here: https://github.com/livekit/agents-js/blob/34116a240ab20081107c3f47ea7a5391d5077afc/agents/src/_exceptions.ts? |
||
| } | ||
| break; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
What's this line for?
Uh oh!
There was an error while loading. Please reload this page.
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.
It's a pattern I use all the time to make it easy to check of the instance of this error...e.g.
error instanceOf ElevenlabsError, without this construct this doesn't evaluate to true.Maybe this is not needed nowadays anymore,guess it depends on the transpile&runtime.
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.
Sounds good! Maybe add a 1 line comments to make it better understandable.