From 8ebe1a515b892abd7b69530fcb888ef8fe09b159 Mon Sep 17 00:00:00 2001 From: Timothy Carambat Date: Tue, 16 Apr 2024 16:42:06 -0700 Subject: [PATCH] Gracefully handle bad agent auth (#1115) use provider that is set --- server/utils/agents/aibitat/plugins/websocket.js | 8 +++++++- server/utils/agents/aibitat/providers/anthropic.js | 6 +++++- server/utils/agents/aibitat/providers/openai.js | 7 +++++-- server/utils/agents/index.js | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/server/utils/agents/aibitat/plugins/websocket.js b/server/utils/agents/aibitat/plugins/websocket.js index af691ca5..b6154984 100644 --- a/server/utils/agents/aibitat/plugins/websocket.js +++ b/server/utils/agents/aibitat/plugins/websocket.js @@ -48,7 +48,13 @@ const websocket = { name: this.name, setup(aibitat) { aibitat.onError(async (error) => { - console.error(chalk.red(` error: ${error?.message}`)); + if (!!error?.message) { + console.error(chalk.red(` error: ${error.message}`)); + aibitat.introspect( + `Error encountered while running: ${error.message}` + ); + } + if (error instanceof RetryError) { console.error(chalk.red(` retrying in 60 seconds...`)); setTimeout(() => { diff --git a/server/utils/agents/aibitat/providers/anthropic.js b/server/utils/agents/aibitat/providers/anthropic.js index d160d9ab..5189dc2e 100644 --- a/server/utils/agents/aibitat/providers/anthropic.js +++ b/server/utils/agents/aibitat/providers/anthropic.js @@ -117,10 +117,14 @@ class AnthropicProvider extends Provider { cost, }; } catch (error) { + // If invalid Auth error we need to abort because no amount of waiting + // will make auth better. + if (error instanceof Anthropic.AuthenticationError) throw error; + if ( error instanceof Anthropic.RateLimitError || error instanceof Anthropic.InternalServerError || - error instanceof Anthropic.APIError + error instanceof Anthropic.APIError // Also will catch AuthenticationError!!! ) { throw new RetryError(error.message); } diff --git a/server/utils/agents/aibitat/providers/openai.js b/server/utils/agents/aibitat/providers/openai.js index 82cd7741..4458afe8 100644 --- a/server/utils/agents/aibitat/providers/openai.js +++ b/server/utils/agents/aibitat/providers/openai.js @@ -102,11 +102,14 @@ class OpenAIProvider extends Provider { cost, }; } catch (error) { - console.log(error); + // If invalid Auth error we need to abort because no amount of waiting + // will make auth better. + if (error instanceof OpenAI.AuthenticationError) throw error; + if ( error instanceof OpenAI.RateLimitError || error instanceof OpenAI.InternalServerError || - error instanceof OpenAI.APIError + error instanceof OpenAI.APIError // Also will catch AuthenticationError!!! ) { throw new RetryError(error.message); } diff --git a/server/utils/agents/index.js b/server/utils/agents/index.js index ff66c982..dd42a6b9 100644 --- a/server/utils/agents/index.js +++ b/server/utils/agents/index.js @@ -157,8 +157,8 @@ class AgentHandler { } ) { this.aibitat = new AIbitat({ - provider: "openai", - model: "gpt-3.5-turbo", + provider: this.provider ?? "openai", + model: this.model ?? "gpt-3.5-turbo", chats: await this.#chatHistory(20), handlerProps: { invocation: this.invocation,