Skip to content

fix(chat): only request contextual search when the backend supports it - #1638

Merged
BenjaminMichaelis merged 1 commit into
mainfrom
fix/chat-contextual-search-capability
Nov 15, 2026
Merged

BenjaminMichaelis merged 1 commit into
mainfrom
fix/chat-contextual-search-capability

Conversation

@BenjaminMichaelis

Copy link
Copy Markdown
Member

Problem

chat-module.js always sent enableContextualSearch: true to POST /api/chat/stream. ChatController returns 400 contextual_search_unsupported when the selected IChatCompletionService has SupportsContextualSearch == false, which is the case for LocalChatService (Ollama / OpenAI-compatible) and UnavailableChatService. As a result, local AI chat was broken in the UI (the API worked when called with enableContextualSearch: false).

Found while prototyping Playwright journey tests against the standalone Web AppHost with a fake local model.

Fix

  • AddConfiguredChatServices registers a lightweight ChatBackendCapabilities record alongside whichever backend it selects (Azure → true; local / unavailable → false).
  • _Layout.cshtml emits window.CHAT_SUPPORTS_CONTEXTUAL_SEARCH from that record.
  • chat-module.js sends enableContextualSearch only when that value is not false (undefined keeps the previous behaviour).

The layout deliberately injects the capabilities record rather than IChatCompletionService: in Azure mode, resolving the service constructs AIChatService → AISearchService → embeddings / vector store / NpgsqlDataSource, so a misconfiguration there would turn every page render into a 500 instead of only failing chat.

Tests

  • ChatContextualSearchCapabilityTests (Web): the layout renders the flag from the registered capabilities (true/false), and the default test host (no AI config) renders false. Verified these fail without the layout change.
  • ServiceCollectionExtensionsTests (Chat): for every backend-selection scenario, exactly one ChatBackendCapabilities is registered and it matches the resolved service's SupportsContextualSearch, which guards against drift.

EssentialCSharp.Web.Tests (193) and EssentialCSharp.Chat.Tests (41) pass locally. I have not manually clicked through chat in a browser with this exact build. The earlier prototype showed the UI flow works end to end once the flag is false.

The chat widget always sent enableContextualSearch: true, but the local
(Ollama/OpenAI-compatible) and unavailable backends report
SupportsContextualSearch == false, so /api/chat/stream returned 400
contextual_search_unsupported and local AI chat failed in the UI.

AddConfiguredChatServices now registers a lightweight ChatBackendCapabilities
record alongside the selected backend. The layout emits
window.CHAT_SUPPORTS_CONTEXTUAL_SEARCH from it (without resolving the backend,
which in Azure mode would build the search/embedding/Postgres graph on every
first page render), and chat-module.js only requests contextual search when it
is not false.
@BenjaminMichaelis
BenjaminMichaelis merged commit 18455d3 into main Nov 15, 2026
21 checks passed
@BenjaminMichaelis
BenjaminMichaelis deleted the fix/chat-contextual-search-capability branch November 15, 2026 03:22
BenjaminMichaelis added a commit that referenced this pull request Nov 15, 2026
## Why

Unit and `WebApplicationFactory` tests don't cover what a reader
actually does: the Vue chat widget, cookies, antiforgery, captcha wiring
and streaming. #1638 was a UI-only bug that every existing test missed.
These journeys run on each PR, before merge.

## What

Browser journeys (`dev/EssentialCSharp.Web.AppHost.Tests/Journeys/`) run
against the standalone Web AppHost, with fresh SQL Server and pgvector:

| Journey | Proves |
|---|---|
| A logged-out reader opening chat is asked to log in | Chat gating and
the login link |
| A reader can log in and log out | Identity, antiforgery, hCaptcha
(test keys), cookies |
| A logged-in question gets the model's reply | Widget →
`/api/chat/stream` → backend → rendered reply. The fake model asserts it
received the question |
| A model failure shows "AI assistant is temporarily unavailable" | The
error path. It also asserts the request reached the model, so a 429 or
an auth failure can't make it pass by accident |

- **`FakeChatModelServer`** is an in-process Kestrel stand-in for an
OpenAI-compatible `/v1/chat/completions`. It returns a deterministic
reply and returns 500 on a trigger phrase. `JourneyFixture` points the
app's `AIOptions__LocalEndpoint` at it, so no Azure, Ollama or model
download is involved.
- **`JourneyFixture`** extends `StandaloneWebFixture` and seeds
confirmed users through Identity. UI registration can't be used, because
the confirmation link is never shown. The existing smoke tests now share
this fixture, so a run boots the app once.
- **Scheduling:** the journeys run one at a time, because all requests
come from 127.0.0.1 and anonymous ones share the ~30/min global limit.
Like the existing Docker tests, they run only where Linux containers are
available, so they are skipped on the macOS and Windows legs.
- **CI:** the Ubuntu frontend job uploads `wwwroot/dist`. The .NET jobs
build with `SkipFrontendBuild`, so the widget JS was never present
before. `build-and-test` and the Ubuntu leg of `pr-build-and-test`
download it before building and install Chromium.
- **Package:** adds `TUnit.Playwright` 1.74.0, matching the other TUnit
packages.

## Bug fix found by the journeys

`chat-module.js` treated **every 503** as a captcha outage. When the AI
backend is down, the server returns 503 with `errorCode:
"chat_unavailable"`, so readers were told *"Human verification is
temporarily unavailable"*. The widget now reads `errorCode`:
- `captcha_unavailable` keeps the captcha message.
- Anything else shows *"The AI assistant is temporarily unavailable.
Please try again in a moment."* under "Error".

Known gap, not changed here: mid-stream `{type:"error",
errorCode:"chat_unavailable"}` SSE events still aren't surfaced, because
the widget only handles `content_filtered`. That is worth a small
follow-up.

## Verification (Windows + Podman)
- All 9 Docker-category tests (4 journeys + 5 smoke) pass in about 110 s
with one app boot.
- Reintroducing the #1638 bug (always `enableContextualSearch: true`)
makes the chat journey fail.
- Before the JS fix, the error journey failed on "Verification
Required", which is how the bug was found.
- With no container engine, all Docker tests skip and no browser is
launched.
- The 202 Web tests still pass.

**Caveat:** login and chat need outbound access to hcaptcha.com (browser
script and server siteverify) with the public test keys. If that proves
flaky in CI, the next step is a fake `ICaptchaService` for the journey
host.

This PR's CI run is the first time the artifact hand-off and the
Chromium install on the runner are exercised.
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