Skip to content

feat(agentStudio): cost control generic error handling#6997

Open
pipeline1987 wants to merge 5 commits into
masterfrom
feat/agent-studio-cost-control-generic-error-handling
Open

feat(agentStudio): cost control generic error handling#6997
pipeline1987 wants to merge 5 commits into
masterfrom
feat/agent-studio-cost-control-generic-error-handling

Conversation

@pipeline1987
Copy link
Copy Markdown

@pipeline1987 pipeline1987 commented Apr 27, 2026

Summary

  • HTTP: On non-OK responses, parse JSON { "message": "…" } when present and use it for the thrown error (e.g. 403 domain blocks); otherwise keep the status-based fallback.
  • Stream: For SSE chunks with type: "error", normalize nested / double-encoded errorText via getMessageFromStreamErrorText so users see the real error / message string instead of raw JSON.
  • UI: Pass error into ChatMessages so the error banner uses error.message when the chat is in an error state.
  • ChatId: Completions are keyed by the chat id sent in the request body (id / chatId). That value was created once when the Chat instance was constructed and never changed. Clearing only removed messages locally, so the next message still used the same conversation id and the backend kept the old thread.
  • Robustness: processStream catches synchronous throws from onChunk and forwards them to onError.
  • Add errorMessage?: string to ChatMessageError and pass it from ChatMessages when status === 'error'.
  • Allow translations.errorMessage: to be a string or ({ errorMessage }) => string so apps can map/sanitize/hide backend text.
  • Default error copy stays generic: raw API messages are no longer shown unless translations opt in.
  • Extend ChatMessages: tests for default behavior, custom errorComponent props, and translation-function usage.

Result

image

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 27, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

TIP This summary will be updated as you push new changes.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 27, 2026

More templates

algoliasearch-helper

npm i https://pkg.pr.new/algolia/instantsearch/algoliasearch-helper@6997

instantsearch-ui-components

npm i https://pkg.pr.new/algolia/instantsearch/instantsearch-ui-components@6997

instantsearch.css

npm i https://pkg.pr.new/algolia/instantsearch/instantsearch.css@6997

instantsearch.js

npm i https://pkg.pr.new/algolia/instantsearch/instantsearch.js@6997

react-instantsearch

npm i https://pkg.pr.new/algolia/instantsearch/react-instantsearch@6997

react-instantsearch-core

npm i https://pkg.pr.new/algolia/instantsearch/react-instantsearch-core@6997

react-instantsearch-nextjs

npm i https://pkg.pr.new/algolia/instantsearch/react-instantsearch-nextjs@6997

react-instantsearch-router-nextjs

npm i https://pkg.pr.new/algolia/instantsearch/react-instantsearch-router-nextjs@6997

vue-instantsearch

npm i https://pkg.pr.new/algolia/instantsearch/vue-instantsearch@6997

commit: 5563b7e

@pipeline1987 pipeline1987 force-pushed the feat/agent-studio-cost-control-generic-error-handling branch from 4152b4f to 05086fe Compare April 27, 2026 17:24
Copy link
Copy Markdown
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the main question is why we need to handle recursion and looping at all and the errors aren't just top-level

Comment thread packages/instantsearch.js/src/lib/ai-lite/utils.ts Outdated
Comment thread packages/instantsearch-ui-components/src/components/chat/ChatMessages.tsx Outdated
@pipeline1987 pipeline1987 force-pushed the feat/agent-studio-cost-control-generic-error-handling branch from 77b6224 to 5addb7a Compare April 28, 2026 16:07
@pipeline1987
Copy link
Copy Markdown
Author

i think the main question is why we need to handle recursion and looping at all and the errors aren't just top-level

afaik, the agent ai runs on LangGraph as a state graph with multiple LLM/tool steps, so there is a real recursion/step budget to enforce—not a single flat request/response. With streaming, the HTTP response has already started (200 + event stream), so failures that occur mid-run cannot be turned into a new top-level HTTP status; they have to be carried inside the stream protocol instead. it follows from how streaming and graph execution interact, but I could probably ask someone to check if that could be changed.

@pipeline1987 pipeline1987 force-pushed the feat/agent-studio-cost-control-generic-error-handling branch from 5addb7a to 67be1bd Compare April 28, 2026 16:20
@pipeline1987 pipeline1987 requested a review from Haroenv April 28, 2026 16:25
@Haroenv Haroenv requested a review from Copilot May 5, 2026 09:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves Agent Studio chat “cost control” error handling end-to-end: it extracts meaningful backend error messages (HTTP + SSE), makes the stream processor more robust to sync callback failures, ensures UI components can access the underlying error message (while keeping the default UI copy generic), and resets the conversation id on clear so new messages don’t continue the old backend thread.

Changes:

  • Transport + stream: parse { message: string } from non-OK HTTP responses; normalize SSE errorText (including nested/double-encoded JSON); route sync onChunk throws to onError.
  • Chat lifecycle: rotate the conversation id on clear to start a new server-side thread.
  • UI: plumb error into ChatMessages, add errorMessage?: string to error UI props, and allow translations.errorMessage to be a function; add/extend tests.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/react-instantsearch/src/widgets/Chat.tsx Passes error into messagesProps so UI can access the last error.
packages/instantsearch.js/src/widgets/chat/chat.tsx Passes error into messagesProps in the InstantSearch.js widget wrapper.
packages/instantsearch.js/src/lib/ai-lite/utils.ts Adds getMessageFromStreamErrorText() to normalize stream error payloads.
packages/instantsearch.js/src/lib/ai-lite/transport.ts Adds HTTP error body parsing for { message } and uses it on non-OK responses.
packages/instantsearch.js/src/lib/ai-lite/stream-parser.ts Wraps onChunk to forward synchronous exceptions to onError.
packages/instantsearch.js/src/lib/ai-lite/index.ts Exports getMessageFromStreamErrorText.
packages/instantsearch.js/src/lib/ai-lite/abstract-chat.ts Makes chat id resettable and uses normalized stream error messages.
packages/instantsearch.js/src/lib/ai-lite/tests/utils.test.ts Adds tests for getMessageFromStreamErrorText.
packages/instantsearch.js/src/lib/ai-lite/tests/stream-parser.test.ts Adds test for sync-throw routing in processStream.
packages/instantsearch.js/src/connectors/chat/connectChat.ts Resets conversation id when clearing finishes.
packages/instantsearch.js/src/connectors/chat/tests/connectChat-test.ts Tests that conversation id changes after clearing.
packages/instantsearch-ui-components/src/components/chat/types.ts Extends AbstractChat type with resetConversationId.
packages/instantsearch-ui-components/src/components/chat/ChatMessages.tsx Adds error?: Error prop and passes errorMessage to error UI.
packages/instantsearch-ui-components/src/components/chat/ChatMessageError.tsx Adds errorMessage?: string prop + functional translations.errorMessage.
packages/instantsearch-ui-components/src/components/chat/Chat.tsx Passes error down into ChatMessages.
packages/instantsearch-ui-components/src/components/chat/tests/ChatMessages.test.tsx Extends tests around error message exposure + translation function behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/instantsearch-ui-components/src/components/chat/ChatMessages.tsx Outdated
Comment thread packages/instantsearch-ui-components/src/components/chat/ChatMessageError.tsx Outdated
Comment thread packages/instantsearch.js/src/lib/ai-lite/transport.ts Outdated
pipeline1987 and others added 3 commits May 5, 2026 11:09
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants