Skip to content

Fix Ask AI by pointing DocSearch at the Agent Studio agent - #3053

Closed
shahbaz17 wants to merge 2 commits into
mainfrom
feat/fix-ai-search
Closed

shahbaz17 wants to merge 2 commits into
mainfrom
feat/fix-ai-search

Conversation

@shahbaz17

@shahbaz17 shahbaz17 commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Description

Ask AI was failing on every question with "AI-201 Bad input".

Root cause: @docsearch/react v4 has two Ask AI transports, selected by an agentStudio flag:

agentStudio: false (default) -> https://askai.algolia.com/chat
(legacy standalone Ask AI)
agentStudio: true -> https://{appId}.algolia.net/agent-studio/1
/agents/{id}/completions (Agent Studio)

Our assistant lives in Algolia Agent Studio (the published "MetaMask Documentation Help" agent), but the flag was never set, so the site was posting to the retired legacy backend. That backend cannot resolve an Agent Studio agent ID and returns AI-201. The configured assistantId (REak1eiP5wfp) was also a stale legacy Ask AI ID rather than the Agent Studio agent ID.

This is why Ask AI worked when tested from the Algolia dashboard but not on the docs site - the dashboard was exercising Agent Studio directly.

Changes:

  1. docusaurus.config.js - askAi.assistantId updated to the Agent Studio agent ID.
  2. src/theme/SearchBar/index.tsx (new) - swizzled wrapper that injects agentStudio: true into askAi for the search modal.
  3. src/theme/Root.tsx - passes agentStudio to DocSearchSidepanel.

Why the flag isn't just in docusaurus.config.js:
@docusaurus/theme-search-algolia@3.10.2 validates themeConfig.algolia.askAi with a closed Joi schema (assistantId, indexName, apiKey, appId, searchParameters.facetFilters, suggestedQuestions). Adding agentStudio there fails the build with "algolia.askAi.agentStudio" is not allowed. SearchBar lets props override themeConfig and forwards the whole askAi object to DocSearchModal untouched, so the wrapper is the supported way to get the flag through. Both files carry comments explaining this and noting the wrapper can be deleted once Docusaurus' schema accepts agentStudio (or DocSearch v5 makes Agent Studio the default).

Note the wrapper reads askAi from themeConfig rather than from props: Navbar/Content renders with no props, and the themeConfig merge happens inside the original SearchBar, downstream of the wrapper.

Known upstream bug, not introduced here: after this fix answers stream and render correctly, but a spurious second request fires and fails with "422 Conversation must end with a user message or resolved tool results", which DocSearch paints as a red "Chat error" banner above the correct answer. Agent Studio does not set providerExecuted: true on tool events in its ai-sdk-5 compatibility stream (verified - tool-input-start, tool-input-available and tool-output-available all omit it), so DocSearch's unconditional sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls concludes the client owns the server-side search tool call and auto-resubmits a conversation ending in an assistant message, which the API correctly rejects. There is no supported option to disable that auto-resubmit and @docsearch/react@5.0.5 still sets it unconditionally, so a version bump does not help. Reported to Algolia; tracking separately rather than patching node_modules.

Verification: POST {appId}.algolia.net/agent-studio/1/agents/{agentId} /completions?stream=true&compatibilityMode=ai-sdk-5 with no request to askai.algolia.com/chat/token.

Issue(s) fixed

Fixes #

Preview

Checklist

  • If this PR updates or adds documentation content that changes or adds technical meaning, it has received an approval from an engineer or DevRel from the relevant team.
  • If this PR updates or adds documentation content, it has received an approval from a technical writer.

External contributor checklist

  • I've read the contribution guidelines.
  • I've created a new issue (or assigned myself to an existing issue) describing what this PR addresses.

Note

Low Risk
Documentation-site Algolia/Ask AI configuration and theme CSS only; no auth, data, or payment paths.

Overview
Fixes Ask AI failing with AI-201 Bad input by targeting the MetaMask Documentation Help Agent Studio agent instead of the retired askai.algolia.com backend.

Config: themeConfig.algolia.askAi.assistantId is updated to the Agent Studio agent UUID, with comments noting why agentStudio cannot live in docusaurus.config.js (Docusaurus’ closed Joi schema rejects unknown askAi keys).

Runtime wiring: A new swizzled SearchBar wrapper merges agentStudio: true into askAi for the search modal, and Root.tsx passes the same flag to DocSearchSidepanel, so both entry points hit {appId}.algolia.net/agent-studio/.../completions.

UI: _doc-search.scss adds styling for Ask AI fenced code (.DocSearch-CodeSnippet), inline code in answers, DocSearch button spacing, and a higher modal z-index above the navbar.

Reviewed by Cursor Bugbot for commit 94a9561. Bugbot is set up for automated code reviews on this repo. Configure here.

Ask AI was failing on every question with "AI-201 Bad input".

Root cause: @docsearch/react v4 has two Ask AI transports, selected by an
`agentStudio` flag:

  agentStudio: false (default) -> https://askai.algolia.com/chat
                                 (legacy standalone Ask AI)
  agentStudio: true            -> https://{appId}.algolia.net/agent-studio/1
                                 /agents/{id}/completions (Agent Studio)

Our assistant lives in Algolia Agent Studio (the published "MetaMask
Documentation Help" agent), but the flag was never set, so the site was
posting to the retired legacy backend. That backend cannot resolve an Agent
Studio agent ID and returns AI-201. The configured assistantId (REak1eiP5wfp)
was also a stale legacy Ask AI ID rather than the Agent Studio agent ID.

This is why Ask AI worked when tested from the Algolia dashboard but not on
the docs site - the dashboard was exercising Agent Studio directly.

Changes:

1. docusaurus.config.js - askAi.assistantId updated to the Agent Studio
   agent ID.
2. src/theme/SearchBar/index.tsx (new) - swizzled wrapper that injects
   `agentStudio: true` into askAi for the search modal.
3. src/theme/Root.tsx - passes `agentStudio` to DocSearchSidepanel.

Why the flag isn't just in docusaurus.config.js:
@docusaurus/theme-search-algolia@3.10.2 validates themeConfig.algolia.askAi
with a closed Joi schema (assistantId, indexName, apiKey, appId,
searchParameters.facetFilters, suggestedQuestions). Adding `agentStudio`
there fails the build with `"algolia.askAi.agentStudio" is not allowed`.
SearchBar lets props override themeConfig and forwards the whole askAi object
to DocSearchModal untouched, so the wrapper is the supported way to get the
flag through. Both files carry comments explaining this and noting the wrapper
can be deleted once Docusaurus' schema accepts `agentStudio` (or DocSearch v5
makes Agent Studio the default).

Note the wrapper reads askAi from themeConfig rather than from props:
Navbar/Content renders <SearchBar /> with no props, and the themeConfig merge
happens inside the original SearchBar, downstream of the wrapper.

Known upstream bug, not introduced here: after this fix answers stream and
render correctly, but a spurious second request fires and fails with "422
Conversation must end with a user message or resolved tool results", which
DocSearch paints as a red "Chat error" banner above the correct answer. Agent
Studio does not set `providerExecuted: true` on tool events in its ai-sdk-5
compatibility stream (verified - tool-input-start, tool-input-available and
tool-output-available all omit it), so DocSearch's unconditional
`sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls` concludes
the client owns the server-side search tool call and auto-resubmits a
conversation ending in an assistant message, which the API correctly rejects.
There is no supported option to disable that auto-resubmit and
@docsearch/react@5.0.5 still sets it unconditionally, so a version bump does
not help. Reported to Algolia; tracking separately rather than patching
node_modules.

Verification: `POST {appId}.algolia.net/agent-studio/1/agents/{agentId}
/completions?stream=true&compatibilityMode=ai-sdk-5` with no request to
askai.algolia.com/chat/token.
@shahbaz17
shahbaz17 requested review from a team as code owners September 8, 2026 15:31
@vercel

vercel Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
metamask-docs Ready Ready Preview Sep 8, 2026 4:01pm UTC

Request Review

Code blocks in Ask AI answers rendered as unstyled, low-contrast text: the
copy button overlapped the first line and long lines wrapped mid-URL.

DocSearch renders fenced blocks through its own marked renderer as:

  <div class="DocSearch-CodeSnippet">
    <button class="DocSearch-CodeSnippet-CopyButton">...</button>
    <pre><code class="language-tsx">...</code></pre>
  </div>

@docsearch/css ships no styling for that <pre>/<code> at all - only
`position: relative` on the wrapper plus the copy button chrome - so the
snippet inherited whatever the surrounding answer styles happened to set.

Snippets now match the site's own code blocks (theme/_prism-code.scss):
`--general-black-light` surface, `--inline-code-font`, `--general-gray` text,
horizontal scroll instead of wrapping, and top padding that clears the copy
button. Applied globally rather than scoped to a container so it covers both
the search modal and the Ask AI sidepanel.

Two things worth noting:

- The existing sidepanel rules force `code { color: #373739 !important }` in
  light mode, which is unreadable on the dark snippet surface. The snippet
  `code` colour is therefore set with matching specificity and !important,
  scoped under both sidepanel colour modes.
- DocSearch does not syntax highlight. The marked renderer only emits a
  `language-*` class, with no Prism/highlight.js pass, so this change covers
  contrast, typography and layout - not token colours.

Also folds in the DocSearch CSS from PR #3052 (branch `ai-fixes`), which
overlaps this branch: `.DocSearch-Button` margin/transition, and
`.DocSearch-Container` z-index to lift the modal above the fixed navbar. Kept
in _doc-search.scss rather than a separate src/theme/SearchBar/styles.css so
all DocSearch styling stays in one place.

Not copied from #3052: its `:root` `--docsearch-primary-color` /
`--docsearch-text-color` pair. This file already overrides both per colour
mode, and has those exact two lines commented out on purpose at the top;
re-adding them at `:root` would leak a purple primary to DocSearch surfaces
rendered outside `.DocSearch`. Its blanket `.DocSearch-Button-Key
{ padding: 0 }` is included but is largely inert, since
`.DocSearch-Button-Key:first-child` in custom.scss sets padding with
!important.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 94a9561. Configure here.

Comment thread src/scss/theme/_doc-search.scss
@shahbaz17

Copy link
Copy Markdown
Member Author

Closing in favor of #3052

@shahbaz17 shahbaz17 closed this Sep 10, 2026

This branch was successfully deployed

1 active deployment
Preview — 94a95617 Deployed Sep 8, 2026 by vercel[bot]
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.

1 participant