From b33a7f7b1c701275ddd214190030d5abe33e0730 Mon Sep 17 00:00:00 2001 From: Paul Jankowski <8bittitan@gmail.com> Date: Wed, 5 Aug 2026 12:11:58 -0400 Subject: [PATCH] fix(v5): recognize conversation depth errors (#2957) Backport of #2881.\n\nOriginal commit: f68e52251ce464e199f12b7a1ac907de61982993 Co-authored-by: Felipe Bermudez --- .changeset/deep-conversations-start.md | 5 +++ .../src/utils/__tests__/ai.test.ts | 37 +++++++++++++++++++ packages/docsearch-react/src/utils/ai.ts | 4 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 .changeset/deep-conversations-start.md diff --git a/.changeset/deep-conversations-start.md b/.changeset/deep-conversations-start.md new file mode 100644 index 00000000..2b603d3c --- /dev/null +++ b/.changeset/deep-conversations-start.md @@ -0,0 +1,5 @@ +--- +"@docsearch/react": patch +--- + +Recognize current Agent Studio conversation-depth error messages. diff --git a/packages/docsearch-react/src/utils/__tests__/ai.test.ts b/packages/docsearch-react/src/utils/__tests__/ai.test.ts index 0b8ad660..ccc83853 100644 --- a/packages/docsearch-react/src/utils/__tests__/ai.test.ts +++ b/packages/docsearch-react/src/utils/__tests__/ai.test.ts @@ -10,10 +10,47 @@ import { getSearchToolQueries, isAIToolPart, isAlgoliaMCPSearchOutputPart, + isThreadDepthError, sanitizeMessagesForRequest, getMessageContent, } from '../ai'; +describe('isThreadDepthError', () => { + it('detects AI-217 regardless of casing', () => { + expect(isThreadDepthError(new Error('AI-217: limit reached'))).toBe(true); + expect(isThreadDepthError(new Error('prefix ai-217 suffix'))).toBe(true); + }); + + it('detects conversation depth phrasing', () => { + expect( + isThreadDepthError( + new Error( + "You've hit the max conversation depth (4 messages), start a new conversation." + ) + ) + ).toBe(true); + expect( + isThreadDepthError(new Error('Maximum conversation depth reached.')) + ).toBe(true); + }); + + it('detects conversation depth in a JSON-shaped error message', () => { + expect( + isThreadDepthError( + new Error( + JSON.stringify({ message: 'Maximum conversation depth reached.' }) + ) + ) + ).toBe(true); + }); + + it('ignores unrelated errors', () => { + expect(isThreadDepthError()).toBe(false); + expect(isThreadDepthError(new Error('Network failed'))).toBe(false); + expect(isThreadDepthError(new Error('AI-214: rate limit'))).toBe(false); + }); +}); + function message(id: string, parts: AIMessagePart[]): AIMessage { return { id, diff --git a/packages/docsearch-react/src/utils/ai.ts b/packages/docsearch-react/src/utils/ai.ts index 21d377f1..945c43ca 100644 --- a/packages/docsearch-react/src/utils/ai.ts +++ b/packages/docsearch-react/src/utils/ai.ts @@ -122,11 +122,11 @@ export const getMessageContent = (message: AIMessage | null): string => .map((part) => part.text) .join('\n\n'); -/** Helper function to check if error is a thread depth error (AI-217). */ +/** Helper function to check if an error reports the conversation depth limit. */ export function isThreadDepthError(error?: Error): boolean { if (!error) return false; - return error.message?.includes('AI-217') || false; + return /(?:ai-217|conversation\s+depth)/i.test(error.message ?? ''); } export const EMPTY_TOOLS: Readonly = Object.freeze({});