Skip to content

AI Mode toggle and overview in search - #522

Merged
neuromechanist merged 22 commits into
stagingfrom
feature/issue-518-ai-mode-search
Nov 11, 2026
Merged

neuromechanist merged 22 commits into
stagingfrom
feature/issue-518-ai-mode-search

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the AI Mode toggle to the Discover search field (board D2) and the Home hero field (board D1), and the AI overview panel above Discover's results.

  • Setting. New build-time PUBLIC_OSA_OVERVIEW_URL, read in src/lib/osa-overview.ts. It must be exactly https://develop-widget.osc.earth/osa/nemar/overview or https://widget.osc.earth/osa/nemar/overview (the hosts OSA_API_HOSTS already allows in connect-src; edge.osc.earth is refused until a release adds it to the CSP). Unset, blank or invalid means no toggle, no panel, no data island. Set in deploy-test.yml (the real value) and wrangler.test.toml (reference); wrangler.toml, middleware.ts and every production setting are untouched. ADR 0032 records the decision.
  • Toggle. A button with aria-pressed, hidden until the widget has open and the endpoint is set. 44 px pill with a 15 px label and a 22 px icon in the hero; 36 px with 13.5 px and 20 px in Discover's 44 px field. The magnifier-with-sparkle is the only AI mark in a field; the Search buttons keep their plain label. On: the field gets the Download headline's teal-to-purple ring. Colours are tokens with dark values.
  • Preference. localStorage (nemar:ai-mode-search:v1, try/catch everywhere) and ai=1 in the address (replaceState); ai=1 in the URL wins and is written back. ai is now a FilterState field, so it survives paging and filter links like exact; a hidden ai input keeps it across search, filters and sort; the page's own /discover links get it client-side (they are server-rendered and edge-cached for everyone).
  • Discover panel. The page embeds the request as an escaped JSON data island, only when a search ran, nothing failed, the page is 1 and cards exist (description from dataset.description, falling back to the snippet without its <mark> tags). The browser posts it when AI Mode is on. States: off, loading (aria-busy), answer (summary with ids as links to /dataset/<id>, up to 3 picks, "Written by AI from these search results", Ask a follow-up), error (Try again). Text is set with textContent; an answer that cites an id outside the request is an error.
  • Home. Submitting while on goes to /discover?q=...&ai=1; the form markup is unchanged and the client adds the ai input only while on. On: placeholder "Describe the data you need, in plain words" and a "Try" row of three example pills that fill the field and submit.
  • Follow-up: OSAChatWidget.ask(text, {mode: 'center'}) when ask is a function, else open({mode: 'center'}); a throw is caught and warned.

Refs #518
Refs #519

The issues stay open until the staging check passes on test.nemar.org (a built deploy, from an origin the service allows), as AGENTS.md describes for work that needs verification; close them by hand then.

Tests added

  • src/lib/osa-overview.test.ts: endpoint setting (vi.stubEnv), request builder over test/fixtures/search-corrected-query.json, every limit, response parsing over test/fixtures/osa-overview-response.json (the example in OSA's docs), the call with HTTP fixtures only (200, 403, 404, 422, 429, 500, 502, network, timeout, caller abort, foreign cited id).
  • src/lib/osa-ai-mode.test.ts (extended): preference store, URL wins and is written back, off removes ai, throwing store, availability needs widget open and the endpoint, follow-up helper.
  • src/lib/ai-overview-panel.test.ts: pure state to view for off, loading, answer, error; follow-up text at most 1500 characters.
  • src/lib/filters.test.ts: ai round trip.
  • test/ai-mode-search.test.ts: source-level guards (toggle markup and sizes, no sparkle elsewhere, panel live region, tokens with dark counterparts, Home form unchanged when off, setting in the two staging files and absent from wrangler.toml).

Commands run

bun install --frozen-lockfile                        ok
bun run lint                                          ok (biome check src, no fixes)
bun run typecheck                                     0 errors, 0 warnings
bun run test                                          175 files passed, 4807 tests passed (3 files, 14 tests skipped as before)
bun run build  (with the staging PUBLIC_OSA_* env)    ok
bun run build  (no env)                               ok

Manual check

bun run dev with the staging env from the brief and PUBLIC_API_BASE_URL=https://api-test.nemar.org, in Chrome: toggling on and off (address, localStorage, panel, hidden input, /discover links), a new search while on keeps ai=1, Home submit on and off, a Try pill, and no horizontal scroll at 390 px on Home and Discover. With no PUBLIC_OSA_OVERVIEW_URL the Home form and Discover have no toggle, panel or island.

Caveats, stated plainly:

  • Update (review round 2): develop-widget now answers 200. A POST from the allowed origin (Origin: https://test.nemar.org) with a small request returned a real overview (summary, picks, cited_ids). From localhost it still answers 403, so the answer, loading and rate-limit screenshots remain intercepted ones; real-service screenshots need the staging deploy. Original note: develop-widget answered 502. A real POST of the island's request (staging ids xx099906, xx099905, and four more) to https://develop-widget.osc.earth/osa/nemar/overview with Origin: https://test.nemar.org returned {"error":"error code: 502"} (twice; direct to api.osc.earth/osa-dev/nemar/overview it asks for a key, so the route exists on the backend). The worker route is presumably not deployed yet, as OSA's docs say it answers only after a maintainer deploys it. From localhost it answers 403 (origin not allowed), which the panel shows as its error state. So the answer, loading and rate-limit screenshots below were taken with that endpoint intercepted in the browser and answered with a response in the documented shape (ids from the real staging search); the 502 and 403 states are real. Re-check on test.nemar.org once the worker serves the route.
  • Screenshots are from the dev server, not wrangler pages dev, which needs the API service binding (AGENTS.md). The build is green.

Screenshots

Light Dark
Home off, AI Mode on home off home on home on dark
Discover answer answer answer dark
Discover loading, rate limited rate limited loading dark
Discover with AI Mode off off
390 px home 390 discover 390

Notes for review

  • Also fixed: Discover scrolled sideways at 390 px before this change (the layout's single column was 1fr, whose minimum is the filter chips' width). It is now minmax(0, 1fr), its own commit, because the 390 px check could not pass without it.
  • On phones the overview now has its own grid row above the filters (880 px and below); on wide screens it sits above the results as before. The notice that search words and top results leave the site is visible text under each field while AI Mode is on.
  • total_count is the count Discover shows (equal to the API's count except when 2+ modalities or a participant range make Discover filter the best-100 window itself, where it is the filtered count).
  • The screenshots are committed under screenshots/ai-mode-search/ (JPEG, about 1 MB) so the links do not depend on the branch.
  • Production is unaffected: no setting in wrangler.toml, so no toggle. Enabling it there is a release-time decision (ADR 0032).

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

…elds, add request parser and unexpected kind (tested)
…nses, retry, logged failure kinds) and aiLinkHref (tested)
…used follow-up; live region moves outside the hidden section (tested by source guards; type-checked)
…says why no overview (source guards; type-checked)
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review round 1: outcome for each finding. Pushed as 837d503, 74b0026, 1592399, 9cd781d, 2cb8b89, 8da09e4. After the fixes: bun run lint clean, astro check 0 errors, bun run test 175 files / 4832 tests passed, astro build completes.

  1. Silent failure and mislabeled error (important): fixed in 74b0026 and 1592399. A new unexpected error kind replaces the network fallback for anything that is not an OverviewError. Every failure is now logged as [ai-overview] with its kind and status. A refused follow-up shows an inline message under the button.
  2. Live region inside the hidden section (important): fixed in 1592399. The polite region is now a visually hidden sibling that is always rendered, the drawn copy beside the heading is aria-hidden, and the comment is corrected. The guard test asserts the region is neither hidden nor inside the hidden section.
  3. State machine untested (important): fixed in 74b0026 and 1592399. The transitions moved into a DOM-free OverviewController with 11 tests over real Response fixtures: abort on off, kept answer on re-toggle, the loading guard, retry, stale-response suppression, and failure classification. readRequest became parseOverviewRequest, and the link rewrite became aiLinkHref, each tested. The client header now says what is and is not unit tested: the DOM glue, focus return and syncAiInput are covered by browser checks and source guards only. There is no Playwright config in the repo, so I did not add an e2e spec.
  4. snippetText stripped anything tag-shaped (important): fixed in 837d503. Only <mark> tags are removed, with a test for a < b and c > d, p<0.05 and <div>.
  5. Builder missed OSA limits (important): fixed in 837d503. Modalities are cut to 200 characters, filter keys over 100 are dropped, strings are cut to 200 and lists to 20 items, each with a test. The module header lists the limits.
  6. overviewRequestForPage unguarded in Discover (important): fixed in 9cd781d. The request is built in a try/catch that logs and yields no panel. With AI Mode on, page 2 and later shows a short note saying the overview reads page 1.
  7. Dead data and loose types (suggestion): fixed in 837d503 for the dead data. citedIds, shown and cached are dropped and cited_ids is no longer validated, so a stray id there cannot spoil an overview. The foreign-id check stays on the summary and picks. The discriminated-union part is rejected: PanelView is built in one tested function, panelView, so the invalid combinations are unreachable, and a union would add casts to the renderer for no behavior change.
  8. Privacy disclosure (suggestion): partly fixed in 2cb8b89. The toggle's title says the search words and the top results' titles and descriptions go to the Open Science Assistant service and an AI model provider, and ADR 0032 lists it under consequences. /privacy on this site is a redirect to the docs site, so the policy sentence cannot be added in this repo; filed as Privacy policy: name the AI Mode search overview before production enables it #523 to land before the production line goes into wrangler.toml.
  9. osa-ai-mode.ts header (suggestion): fixed in 8da09e4. The header describes both halves and lists the search toggle's five responsibilities. I did not split the file, since both halves share the widget check and whenDocumentReady.
  10. Verification gaps (suggestion): rejected as a code change, because the screenshots and the check on test.nemar.org depend on the worker serving /nemar/overview after merge. I ran lint, typecheck, the full test suite and a build locally (counts above). The re-check in light, dark and 390 px on staging, and replacing the intercepted screenshots, stay as a post-merge step.

@neuromechanist neuromechanist changed the title Discover and Home: AI Mode toggle and AI overview (E6.4, E6.5) AI Mode toggle and overview in search Nov 11, 2026
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review round 2: outcomes. Pushed to this branch; lint, typecheck (0 errors), the full suite (175 files, 4834 tests) and the build pass.

  1. Visible disclosure (important): fixed in 0c5d541. A one-paragraph notice (shared text AI_MODE_SEARCH_DISCLOSURE) now shows under the search field on Discover (SearchBar) and Home while AI Mode is on, and is hidden when it is off; the toggle keeps the title as a bonus. The source guard asserts the text, the hidden-by-default paragraph in both places and the client wiring. ADR 0032 now says the notice is visible text.
  2. Built preview on the real service (important): partly fixed, issues kept open. develop-widget now answers 200 from Origin: https://test.nemar.org (checked with a small request, a real overview came back), so the 502 caveat is gone. From localhost the service still answers 403, so retaking the answer and rate-limit screenshots from the real service, and the Discover 390 px light and Home 390 px dark shots, need the staging deploy of this PR; wrangler pages dev needs the API service binding. The PR body now says Refs #518 and Refs #519 (not Closes), per AGENTS.md for work that needs verification on staging, and the body notes the update. Close both by hand after the staging check.
  3. Overview below the filters on phones (suggestion): fixed in 64aa8eb. The overview is its own grid row (.discover__overview); at 880 px and below the areas are overview, rail, results; wider, it sits above the results beside the rail as before. Checked in the browser at the narrowest window available (606 px): the overview is above the rail and the page does not scroll sideways. The wide layout was not re-checked in a browser (the window would not go wider than 606 px); it is source-guarded. The PR body's "deferred" note is updated.
  4. Switch-off path unguarded (suggestion): fixed in f2a8355. OverviewPanel.setOn catches and console.warns, so a render bug cannot skip the other toggle steps (source guard). Squash title shortened to "AI Mode toggle and overview in search" (37 characters); the branch's own commit headlines are replaced by it at squash.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

PM verdict at f2a8355: not ready to merge yet, one check is red.

  • Criteria: the tests and source guards in the brief are all present and match (endpoint setting, request builder over the real fixture, HTTP-fixture response handling, preference store, panel state model, follow-up helper, source-level guard, setting absent from wrangler.toml). Both review rounds have an outcome comment. The branch is up to date with staging.
  • Checks: lint, typecheck, test and build pass on GitHub Actions. I also ran lint, typecheck, the test suite (175 files, 4831 tests) and bun run build (with and without the staging PUBLIC_OSA_* env) in a clean worktree at f2a8355: all pass.
  • Not green: the Cloudflare Pages preview build failed on f2a8355 (it succeeded on 004562d and 8da09e4 and on other staging PRs). The log is behind the Cloudflare login and I could not reproduce it locally, so I cannot say the cause. It does not look related to the nemar-cli merge or to this diff. Please retry the Pages build from the dashboard (or push any commit) and check the log if it fails again.
  • Also open: the brief asks for a manual check on a built preview; the check was done on the dev server and the Pages preview does not exist for this commit, so that stays a post-merge check on test.nemar.org, as the PR body says (Discover: AI Mode toggle in the search field and the AI overview panel #518 and Home: AI Mode toggle in the hero search field #519 stay open until then).

@neuromechanist
neuromechanist merged commit f36a96c into staging Nov 11, 2026
5 checks passed
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