Repository navigation
fix(chat): only request contextual search when the backend supports it - #1638
Merged
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
chat-module.jsalways sentenableContextualSearch: truetoPOST /api/chat/stream.ChatControllerreturns 400contextual_search_unsupportedwhen the selectedIChatCompletionServicehasSupportsContextualSearch == false, which is the case forLocalChatService(Ollama / OpenAI-compatible) andUnavailableChatService. As a result, local AI chat was broken in the UI (the API worked when called withenableContextualSearch: false).Found while prototyping Playwright journey tests against the standalone Web AppHost with a fake local model.
Fix
AddConfiguredChatServicesregisters a lightweightChatBackendCapabilitiesrecord alongside whichever backend it selects (Azure →true; local / unavailable →false)._Layout.cshtmlemitswindow.CHAT_SUPPORTS_CONTEXTUAL_SEARCHfrom that record.chat-module.jssendsenableContextualSearchonly when that value is notfalse(undefined keeps the previous behaviour).The layout deliberately injects the capabilities record rather than
IChatCompletionService: in Azure mode, resolving the service constructsAIChatService→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) rendersfalse. Verified these fail without the layout change.ServiceCollectionExtensionsTests(Chat): for every backend-selection scenario, exactly oneChatBackendCapabilitiesis registered and it matches the resolved service'sSupportsContextualSearch, which guards against drift.EssentialCSharp.Web.Tests(193) andEssentialCSharp.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 isfalse.