From f3f1cc12c8c9de0a1571042c197323ba71eb8b39 Mon Sep 17 00:00:00 2001 From: Brian Hackett Date: Thu, 6 Mar 2025 08:28:08 -0800 Subject: [PATCH] fix error parsing --- app/lib/.server/llm/chat-anthropic.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/lib/.server/llm/chat-anthropic.ts b/app/lib/.server/llm/chat-anthropic.ts index 504999d7..0fbe19c0 100644 --- a/app/lib/.server/llm/chat-anthropic.ts +++ b/app/lib/.server/llm/chat-anthropic.ts @@ -58,13 +58,18 @@ export interface AnthropicCall { } function maybeParseAnthropicErrorPromptTooLong(e: any) { - if (e?.error?.type === "invalid_request_error") { - const match = /prompt is too long: (\d+) tokens > (\d+) maximum/.exec(e?.error?.message); - if (match) { - const tokens = +match[1]; - const maximum = +match[2]; - return { tokens, maximum }; + try { + const { type, message } = e.error.error; + if (type === "invalid_request_error") { + const match = /prompt is too long: (\d+) tokens > (\d+) maximum/.exec(message); + if (match) { + const tokens = +match[1]; + const maximum = +match[2]; + return { tokens, maximum }; + } } + } catch (e) { + console.log("AnthropicParseError", e); } return undefined; }