Skip to content
askdareDocumentation

    ADR-0041: Themes switched at runtime

    • Status: Accepted on 2026-12-02 by the maintainer, with every recommendation (see "Settled with the maintainer"). Built by tasks 0.9-r1a, r1b and r1c, and on the site by 0.9-r2.
    • Date: 2026-12-02
    • Amends: ADR-0008 (one bot gets several looks; configure also accepts a look and a color scheme, as ADR-0014 added the locale) and ADR-0011 (Theme and DefaultChat take a look's name; the theme root gains data-ask-theme; a bot with several looks gets one inline stylesheet per look).
    • Related: ADR-0004 (the client picks among the server's looks, never behavior), ADR-0028 (the React budget), ADR-0033 (the loader's budget), ADR-0040 (bots in a database, Proposed).
    • Evidence: a prototype on a local branch, spike/runtime-themes, never merged or pushed. Sizes are gzipped and measured with the repository's own checks (scripts/inline-assets.mjs for the loader and the iframe app, scripts/react-size.mjs for React).

    Context

    A bot's look is fixed today (ADR-0008):

    • widget.theme and widget.css per bot, served as one content-hashed stylesheet, <basePath>/theme/<bot>.css;
    • the launcher's look inlined into widget.js?bot=<id>;
    • askdare('configure') accepts only greeting, starters, context and locale.

    So:

    • Sites with their own dark-mode switch can't make the widget follow it. colorScheme: 'auto' follows the system, and only React's Theme takes colorScheme.
    • One assistant with several brands or seasonal looks needs one bot per look, each with its own conversations, budget and line in the console.
    • askdare's own demo has five bots (support, sage, halo, care, bldr) for five looks. Switching reloads the page and starts a new conversation, so the demo can't show "the same chat in a new look".

    Three things exist to build on:

    • The handler already serves a theme other than the bot's, in gallery mode: viewFor in server/handler.ts reads ?preset=&scheme= and puts them in the stylesheet's URL.
    • The loader already switches the chat in place for a language (switchTo in embed/loader.ts, ADR-0014): it posts to a connected chat, or reloads one that hasn't connected.
    • Inside a page, each color scheme is already a [data-askdare-root][data-ask-bot][data-ask-scheme] block, so Theme's colorScheme switches it (ADR-0011).

    What the prototype measured

    Bytes

    Sizes in bytes, gzipped. CI's zlib compresses slightly worse than a laptop's, so the table gives both: the local build before and after the prototype, and CI's figure for main on 2026-12-02 (run 46846585856) with the same difference added.

    Whatmain, localPrototype, localAddedmain, CIWith it, CI (estimate)BudgetLeft (estimate)
    Loader5,2845,425+1415,3165,4575,632175
    React entry (react.js)34,34334,483+14034,47234,61234,816204
    Headless (Root and useChat)11,96612,011+4511,99612,04112,288247
    Embed (EmbedRoot and useChat)12,73412,899+16512,75912,92414,3361,412
    Iframe app44,40044,791+39144,63245,02351,2006,177

    Other costs:

    • The served widget.js of a bot with five looks (the demo's, BLDR's CSS included): 6,354 B with one look, 6,620 B with five. Each extra look adds about 66 B to every page that loads the bot's launcher. The look data isn't part of the loader's budget, which is measured with the placeholder (ADR-0033).
    • A look's stylesheet: Aura 693 B (iframe) and 718 B (inside a page), Sage 577 and 602 B, BLDR with its CSS 1,052 and 1,083 B. It is fetched only when a page switches to that look, then cached as immutable.
    • The iframe's stylesheet with both color schemes (decision 3): 693 B instead of 656 B for Aura, +37 B.
    • The embed page's configuration for the five looks: +194 B in its HTML.

    Two smaller variants were measured and not chosen:

    • A loader that validates and warns in more detail took +222 B. The chosen form takes +141 B. The console warning alone is 15 B.
    • useChat without the look (decision 5) takes +7 B in the headless case instead of +45 B, but the default look's skin then needs a new public hook to know the variants.

    An answer that is streaming

    A test in the prototype (embed-root.test.tsx, on the spike branch) runs a site's own page in the widget's iframe (ADR-0018): the loader's port, EmbedRoot and DefaultChat, with a server that streams half an answer, waits, and then streams the rest.

    • The page switches to another look and to the dark scheme while the answer is half done.
    • The rest of the answer arrives in the new look, in the same message.
    • The page hears message.received with outcome: 'completed'.
    • There was one /messages request and one /session request: nothing was reloaded or sent again.

    It works because a look never touches the chat's store: messages, the draft, an answer under way and the connection stay as they are.

    The same test found a trap in React 19. A <link rel="stylesheet" precedence> that isn't loaded yet holds React's commit until it loads. While the new look's stylesheet loaded, the whole chat stopped updating: the store had the finished answer, the page still showed its first half. Theme therefore takes the look's name through useDeferredValue. React then keeps the old look on screen while the stylesheet loads, the answer keeps rendering, and the look switches once its stylesheet is ready. The test checks both steps.

    The iframe's own document (askdare's page, not EmbedRoot) was prototyped too: it sets the attributes on <html>, adds the new stylesheet after the old one, and removes the old one once the new one has loaded. That code is type-checked and built, but it was not run in a browser. The implementation task covers it with an end-to-end test.

    Decision

    1. A bot lists its looks in defineBot

    widget: {
      theme: 'aura',          // the default look, named 'default'
      css: SHARED_CSS,        // the default look's CSS
      themes: {
        sage: 'sage',                       // a preset
        care: defineTheme({ extends: 'care', accent: '#4f46e5' }),
        bldr: { theme: bldr, css: BLDR_CSS },
      },
    }
    • The default look is widget.theme with widget.css, as today, and its name is default. A bot without themes behaves exactly as today: same stylesheet, same hash, same launcher.
    • widget.themes adds up to 8 looks.
      • Names match [a-z0-9-]{1,32}; default is reserved.
      • The limit keeps widget.js small: every look adds about 66 B to every page.
    • An entry is a preset name, a defineTheme result, or { theme, css }.
    • CSS per look. An entry's css replaces widget.css for that look. Without one, the look uses widget.css. Rules shared by several looks are joined in code (css: SHARED_CSS + BLDR_CSS), so no look gets rules written for another one.
    • A look is visual only: tokens, variants and CSS. The texts, the avatar, the greeting next to the launcher and the default color scheme stay the bot's.
      • A page that wants another welcome greeting per look passes greeting in the same configure call, which it can already do.
      • Texts per look would multiply the launcher's texts in widget.js per language and look.
    • Validation at startup uses the same Zod schema as widget.theme, with css at most 50,000 characters, as widget.css is today. In development, each customized look gets the contrast warnings widget.theme gets today; the built-in presets stay checked in CI.

    2. What a page may ask for

    • The loader: askdare('configure', { theme, colorScheme }). theme is a look's name or 'default'; colorScheme is 'auto', 'light' or 'dark'.
      • Each is applied on its own, like locale: a call with only theme keeps the greeting, starters and context set before.
      • An unknown name or scheme keeps the current look and logs a console warning that names the bot. The loader has no development mode, so it warns always, as it does for a fixed locale (ADR-0014).
    • React: Theme and DefaultChat take theme beside the existing colorScheme. Root doesn't change.
      • An unknown name shows the bot's default look and warns in development. A prop is declarative, so "the current look" is what the prop says, not what an earlier render showed.
    • EmbedRoot passes what the embedding page asked for to the Theme inside it, so a site's own page in the iframe follows configure with no code. A prop on Theme wins over it.
    • Nothing is stored. The look isn't kept in storage or a cookie. A page that remembers a visitor's choice calls configure on every load, as for the language. The loader puts theme and scheme in the iframe's URL, so a chat opened after a configure call loads in that look, with no flash of the default one.

    Against ADR-0004:

    • The client picks a look by name, among those the bot lists. It never sends tokens, CSS, URLs or variants.
    • The name is checked against [a-z0-9-]{1,32} in the browser and against the bot's list on the server (the chat page, the stylesheet route). Only names from code reach a URL, an attribute or a CSS selector.
    • The look isn't sent with messages, so it can't reach the model, its instructions or its tools, and it changes no cost.

    This amends ADR-0008's allowlist: configure accepts texts, starters, page context, the locale and a look and a color scheme.

    3. Serving

    • One stylesheet per bot, look and scope, on the existing route: GET <basePath>/theme/<bot>.css?theme=<name>&v=<hash> for the iframe, and ?scope=inline&theme=<name>&v=<hash> inside a page.
      • viewFor's gallery-only parameters become general: theme and scheme are read outside gallery mode; preset stays gallery-only.
      • The hash covers the look's tokens, its CSS and the scope. A matching v is cached as immutable, any other for 60 seconds, as today.
      • An unknown name answers 404.
    • The iframe's stylesheet carries both color schemes, chosen by data-ask-scheme on <html>, as the inline one does since ADR-0011. A scheme switch is then an attribute change with no request.
      • It costs 37 B per stylesheet and changes every bot's iframe stylesheet once, so the old cached copy is dropped.
      • The colors are the same, so visual baselines should not change; the implementation runs them.
    • Inside a page, a bot with several looks keys each look's rules by name: [data-askdare-root][data-ask-bot="<bot>"][data-ask-theme="<name>"], with the look's own CSS inside @scope of that root.
      • React 19 never removes a stylesheet it has hoisted. After default → BLDR → default, both are in <head>, and without the key BLDR's rules would win by their order.
      • A bot with one look keeps today's selectors.
    • The configuration carries every look: themes: { <name>: { preset, variants, href } } in the iframe's configuration (embedConfig), in getChatConfig and in getEmbedConfig, each with the stylesheet for its scope. It is absent for a bot with one look.
      • It is built by publicConfig's callers, so the "no secrets" test covers it.
      • GET /config/<bot> doesn't change: a look doesn't depend on the language.
    • A bot's embedPath (ADR-0018): the redirect forwards theme and scheme with lang and mode.

    4. The widget

    • In place, over the existing bridge. The loader posts { v: 1, type: 'theme', theme?, scheme? } to a connected chat.
      • The iframe sets the variant attributes on <html>, adds the look's stylesheet after the old one and removes the old one once the new one has loaded. The skin re-renders with the look's variants (decision 5).
      • A chat that hasn't connected yet has nothing under way: the loader reloads it with the new ?theme=&scheme=, as for a language (ADR-0014, step 1).
      • A chat cached from before this change ignores the message, as it ignores any unknown one.
    • The launcher's look per theme is inlined in widget.js. The served loader carries, for the requested bot only, a looks map with each look's launcher style, colors, orb and radius. The texts are the bot's, so they don't multiply.
      • A switch repaints the launcher at once, including the greeting bubble, which uses the launcher's colors.
      • Fetching a look on switch was rejected. The loader makes no request to the app today, a new route would need CORS, and a dark-mode switch before the visitor opens the chat must repaint the launcher at once.
    • The loader's budget stays 5,632 B. About 175 B are left after this, by CI's zlib.

    5. React

    • The switch lives in Theme, beside colorScheme. Theme reads the configuration through useChatConfig, so it re-renders after a language switch too.
      • It takes the look's name through useDeferredValue, so a streaming answer keeps rendering while the stylesheet loads (see "What the prototype measured").
      • It sets data-ask-theme, data-ask-preset and the variants of the look, and links the look's stylesheet.
    • useChat().bot.preset and bot.variants describe the look on screen. Theme provides the look through an internal context that useChat reads. The default look's skin, its ejected copy and a headless page's own components then all render the look's markup (the welcome layout, where reply options go) with no new hook.
      • The skin may import only the public entry (ADR-0011), so the alternative was a new public hook. Reading the context costs 38 B of the headless case.
    • Root doesn't change. The headless case grows by 45 B in all.
    • It fits without making room first. Measured: +140 B in the React entry (about 204 B left by CI's zlib) and +45 B in the headless case (about 247 B left).
    • The next 0.9 feature that grows the chat makes room first (ADR-0028), since this one leaves little. The live chat's human replies and interop are the likely next ones. Candidates for making room are in ADR-0028: lazy-loading the Markdown renderer or the default look.
    • react/headless.ts doesn't change: ThemeProps gains a prop, and the context stays internal.

    6. Reports and the console

    The look is a browser-only matter.

    • It doesn't appear in messages, usage reports, the admin store, transcripts, browser events (ADR-0013) or telemetry.
      • It doesn't change answers or cost.
      • Recording it would mean the client sending it with each message, which is new per-message data for nothing the server acts on.
      • The page made the change, so it already knows it for its own analytics.
    • The console's Bots view, which shows a bot's configuration read-only, lists the bot's look names beside its color scheme.

    7. Order of work in 0.9

    The implementation touches these hot files (ROADMAP, "Lanes and hot files"):

    • bot/define-bot.ts: the themes option and resolving each look; additive, about 40 lines.
    • server/handler.ts: viewFor and serveTheme, and the embedPath redirect.
    • embed/loader.ts: its own lane.
    • protocol/* and docs/protocol.md:
      • protocol/launcher.ts gains the optional looks map, and the bridge gains its theme message;
      • docs/protocol.md documents ?theme= and ?scheme= on /embed/<bot> and /theme/<bot>.css;
      • all of it is additive; the SSE protocol doesn't change.

    It doesn't touch chat/state.ts, chat/reducer.ts, chat/store.ts, chat/events.ts, i18n/* or react/headless.ts. Its other files aren't hot: react/theme.tsx, react/context.tsx, react/default-chat.tsx, the skin, embed-root/, widget-app/, chat/host-bridge.ts, server/embed-page.ts, server/launcher.ts, theme/css.ts, next/get-chat-config.ts, next/get-embed-config.ts and server/admin-console.ts.

    Recommended order for 0.9's core:

    1. Runtime themes first, as three tasks:
      • The server part (Core lane): define-bot.ts, handler.ts, protocol/launcher.ts and docs/protocol.md, plus the non-hot server files. It is small, holds the hot files briefly, and fixes the configuration's shape for the other two.
      • Then, in parallel: the loader (Loader lane, embed/loader.ts only), and React with the iframe and EmbedRoot (no hot files).
      • It goes first because it is the smallest, the site's demo task waits on it, and it decides how much of React's room is left for the features after it.
    2. The second provider path (OpenRouter) next, once the server part has released define-bot.ts and handler.ts. It can start in parallel with the loader and React tasks.
    3. askdare eval skipping unchanged cases, in parallel with 2 if it stays in eval/ and server/eval.ts, which aren't hot. If it needs define-bot.ts to hash a bot's instructions and settings, it goes after 2 on that file.
    4. Interop (AI SDK UI messages, AG-UI) after 2: protocol/* and server/handler.ts.
    5. Handoff adapters and live chat last. They reach every hot file (protocol/*, chat/*, handler.ts, define-bot.ts, React), and the first of them that grows the chat makes room in the React entry first (decision 5).

    Nothing in 0.9 has to wait on themes except the site's demo task. If the planning session orders 0.9 differently, the only constraint from this ADR is that the server part of themes holds define-bot.ts and handler.ts for one short task.

    Fitted to the 0.9 plan (2026-12-02). The plan's Core order is c1 → b1 → r1 → h1 → h2, with r1 after b1 so b1 could make room for it first. The tasks written from this ADR keep r1 before h1 but don't make all of it wait for b1:

    • r1a, the server part, needs no room and touches neither chat/* nor React. It starts once release/0.9 has 0.8.x-p1 (which changes server/handler.ts on main), in parallel with c1 and b1, and holds define-bot.ts, handler.ts and protocol/launcher.ts before h1.
    • r1b, React with the iframe and EmbedRoot, fits without b1 (decision 5), but runs after b1 and never beside it, because both change the React entry's files and its measurement.
    • r1c, the loader, runs after r1a, beside r1b; embed/loader.ts is its alone.
    • r2, the site, follows all three.

    8. The studio

    Looks stay in code: tokens and CSS are written in defineBot or defineTheme, never typed in a console.

    • CSS can reach the network (url(), @import, fonts), and ADR-0040's decision 3 keeps anything that reaches the network in code.
    • A look's contrast is checked when the code runs.

    ADR-0040's "theme choice" in the database becomes a pick by name: a studio bot chooses its default look and the looks it offers from those the app registers in code. That is the same rule as models and tools ("code registers capabilities, the database composes bots"). ADR-0040 doesn't need to decide it again; when it is accepted, its table can point here.

    Not decided here

    • Following the page's dark mode automatically, for example with a data-follow-scheme attribute that watches a class on <html>, as data-follow-lang does for the language. Sites mark dark mode in too many ways (class="dark", data-theme, color-scheme) to pick one. A page calls configure({ colorScheme }) from its own switch. Later, if asked.
    • Texts per look (decision 1). Later, if asked; each needs the launcher's texts per look and language in widget.js.

    Consequences

    • New public API (ADR-0006):

      • widget.themes in defineBot;
      • theme and colorScheme in askdare('configure');
      • theme on Theme and DefaultChat;
      • the data-ask-theme attribute on the theme root.

      The ?theme= and ?scheme= parameters are the route's, documented in docs/protocol.md. It is a minor changeset.

    • The site's demo becomes one bot. One demo bot with five looks replaces support, sage, halo, care and bldr, and the dock switches the look without a reload, keeping the conversation.

      • The site then has three bots: demo, space (0.9-d2) and docs.
      • The five bots' different titles and greetings become one set of texts; the dock can pass a greeting per look with configure.
    • One assistant can wear several brands with one budget, one line in the console and one conversation per visitor.

    • Every bot's iframe stylesheet changes once (both schemes in it, +37 B), so cached copies are fetched again after the upgrade.

    • Every page that loads a bot with looks downloads about 66 B more per look, in widget.js.

    • A look's stylesheet is fetched the first time a page switches to it. Until it loads, the old look stays on screen.

    • React's room shrinks to about 204 B in the entry and 247 B in the headless case. The next feature that grows the chat makes room first.

    • Contrast in custom looks is checked as for custom themes today: warnings in development, not in CI.

    Alternatives considered

    • One stylesheet per bot with every look, each scoped by [data-ask-theme], so a switch needs no request.
      • Every visitor would download every look's CSS on every load.
      • In the iframe, each look's own CSS would have to go into @scope, which changes how widget.css applies there today.
    • The launcher's looks fetched on switch, from the app or from the iframe over the bridge.
      • The loader makes no requests to the app, and the iframe exists only once the visitor opens the chat.
      • A dark-mode switch on a page where the chat was never opened would leave the launcher in the old look.
    • The look in the chat's store (store.setTheme, Root's props).
      • It keeps bot.variants right without a context, but it adds the switch to the headless case, the one with the least room.
      • The look also belongs to the element that shows it, as colorScheme already does.
    • A public useLook() hook instead of the look in useChat: 38 B less in the headless case, but one more export to learn, and bot.variants would describe a look that isn't on screen.
    • A new conversation per look, by switching bots. That is what the demo does today, and what this ADR removes.
    • A look's CSS appended to widget.css instead of replacing it. Rules written for the default look would leak into every other one, and a look couldn't drop them.
    • Looks that also carry texts (title, greeting, launcher label). Closer to the demo's five bots, but it multiplies the launcher's texts per look and language in widget.js. Without it, a page sets the greeting with configure.

    Settled with the maintainer (2026-12-02)

    Accepted with every recommendation under "Open questions", relayed by the session that plans 0.9. In the maintainer's words, in short: a switch changes the look only, and the session and the conversation stay as they were. So:

    1. Looks are visual only; texts stay per bot.
    2. A look's css replaces widget.css.
    3. At most 8 looks besides the default.
    4. The iframe's stylesheet carries both color schemes for every bot.
    5. useChat reads the look; no useLook() hook.
    6. An unknown name in React shows the default look; the loader keeps the current one.
    7. The look stays out of every report; the console lists the names.
    8. The order in 0.9 as in decision 7, fitted to the plan as written there.

    Open questions (settled above)

    Each had a recommendation, and the maintainer accepted them all.

    1. Looks are visual only (decision 1). Recommended: yes; texts stay per bot. Alternative: a few texts per look (title, greeting, launcher label).
    2. A look's css replaces widget.css (decision 1). Recommended: replaces. Alternative: appends.
    3. At most 8 looks besides the default (decision 1). Recommended: 8. Alternative: 4, which covers the demo with none to spare.
    4. The iframe's stylesheet carries both schemes for every bot (decision 3). Recommended: yes, so a scheme switch needs no request. Alternative: one stylesheet per look and scheme, requested on each switch.
    5. useChat reads the look (decision 5, 38 B of the headless case). Recommended: yes. Alternative: a public useLook() hook.
    6. An unknown name in React shows the default look (decision 2), while the loader keeps the current one. Recommended: yes. Alternative: React keeps the last valid look, which needs state.
    7. The look stays out of every report (decision 6), and the console lists the names. Recommended: yes.
    8. The order in 0.9 (decision 7): the server part of themes first on define-bot.ts and handler.ts, then the provider path. Recommended: yes; to be reconciled with the 0.9 core plan.