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;
configurealso accepts a look and a color scheme, as ADR-0014 added the locale) and ADR-0011 (ThemeandDefaultChattake a look's name; the theme root gainsdata-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.mjsfor the loader and the iframe app,scripts/react-size.mjsfor React).
Context
A bot's look is fixed today (ADR-0008):
widget.themeandwidget.cssper bot, served as one content-hashed stylesheet,<basePath>/theme/<bot>.css;- the launcher's look inlined into
widget.js?bot=<id>; askdare('configure')accepts onlygreeting,starters,contextandlocale.
So:
- Sites with their own dark-mode switch can't make the widget follow it.
colorScheme: 'auto'follows the system, and only React'sThemetakescolorScheme. - 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:
viewForinserver/handler.tsreads?preset=&scheme=and puts them in the stylesheet's URL. - The loader already switches the chat in place for a language (
switchToinembed/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, soTheme'scolorSchemeswitches 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.
| What | main, local | Prototype, local | Added | main, CI | With it, CI (estimate) | Budget | Left (estimate) |
|---|---|---|---|---|---|---|---|
| Loader | 5,284 | 5,425 | +141 | 5,316 | 5,457 | 5,632 | 175 |
React entry (react.js) | 34,343 | 34,483 | +140 | 34,472 | 34,612 | 34,816 | 204 |
Headless (Root and useChat) | 11,966 | 12,011 | +45 | 11,996 | 12,041 | 12,288 | 247 |
Embed (EmbedRoot and useChat) | 12,734 | 12,899 | +165 | 12,759 | 12,924 | 14,336 | 1,412 |
| Iframe app | 44,400 | 44,791 | +391 | 44,632 | 45,023 | 51,200 | 6,177 |
Other costs:
- The served
widget.jsof 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.
useChatwithout 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.receivedwithoutcome: 'completed'. - There was one
/messagesrequest and one/sessionrequest: 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.themewithwidget.css, as today, and its name isdefault. A bot withoutthemesbehaves exactly as today: same stylesheet, same hash, same launcher. widget.themesadds up to 8 looks.- Names match
[a-z0-9-]{1,32};defaultis reserved. - The limit keeps
widget.jssmall: every look adds about 66 B to every page.
- Names match
- An entry is a preset name, a
defineThemeresult, or{ theme, css }. - CSS per look. An entry's
cssreplaceswidget.cssfor that look. Without one, the look useswidget.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
greetingin the sameconfigurecall, which it can already do. - Texts per look would multiply the launcher's texts in
widget.jsper language and look.
- A page that wants another welcome greeting per look passes
- Validation at startup uses the same Zod schema as
widget.theme, withcssat most 50,000 characters, aswidget.cssis today. In development, each customized look gets the contrast warningswidget.themegets today; the built-in presets stay checked in CI.
2. What a page may ask for
- The loader:
askdare('configure', { theme, colorScheme }).themeis a look's name or'default';colorSchemeis'auto','light'or'dark'.- Each is applied on its own, like
locale: a call with onlythemekeeps 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).
- Each is applied on its own, like
- React:
ThemeandDefaultChattakethemebeside the existingcolorScheme.Rootdoesn'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.
EmbedRootpasses what the embedding page asked for to theThemeinside it, so a site's own page in the iframe followsconfigurewith no code. A prop onThemewins over it.- Nothing is stored. The look isn't kept in storage or a cookie. A page that remembers a visitor's choice calls
configureon every load, as for the language. The loader putsthemeandschemein the iframe's URL, so a chat opened after aconfigurecall 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:themeandschemeare read outside gallery mode;presetstays gallery-only.- The hash covers the look's tokens, its CSS and the scope. A matching
vis 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-schemeon<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@scopeof 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.
- React 19 never removes a stylesheet it has hoisted. After default → BLDR → default, both are in
- The configuration carries every look:
themes: { <name>: { preset, variants, href } }in the iframe's configuration (embedConfig), ingetChatConfigand ingetEmbedConfig, 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.
- It is built by
- A bot's
embedPath(ADR-0018): the redirect forwardsthemeandschemewithlangandmode.
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 iframe sets the variant attributes on
- The launcher's look per theme is inlined in
widget.js. The served loader carries, for the requested bot only, alooksmap 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, besidecolorScheme.Themereads the configuration throughuseChatConfig, 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-presetand the variants of the look, and links the look's stylesheet.
- It takes the look's name through
useChat().bot.presetandbot.variantsdescribe the look on screen.Themeprovides the look through an internal context thatuseChatreads. 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.
Rootdoesn'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.tsdoesn't change:ThemePropsgains 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: thethemesoption and resolving each look; additive, about 40 lines.server/handler.ts:viewForandserveTheme, and theembedPathredirect.embed/loader.ts: its own lane.protocol/*anddocs/protocol.md:protocol/launcher.tsgains the optionallooksmap, and the bridge gains itsthememessage;docs/protocol.mddocuments?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:
- Runtime themes first, as three tasks:
- The server part (Core lane):
define-bot.ts,handler.ts,protocol/launcher.tsanddocs/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.tsonly), and React with the iframe andEmbedRoot(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.
- The server part (Core lane):
- The second provider path (OpenRouter) next, once the server part has released
define-bot.tsandhandler.ts. It can start in parallel with the loader and React tasks. askdare evalskipping unchanged cases, in parallel with 2 if it stays ineval/andserver/eval.ts, which aren't hot. If it needsdefine-bot.tsto hash a bot's instructions and settings, it goes after 2 on that file.- Interop (AI SDK UI messages, AG-UI) after 2:
protocol/*andserver/handler.ts. - 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 oncerelease/0.9has 0.8.x-p1 (which changesserver/handler.tsonmain), in parallel with c1 and b1, and holdsdefine-bot.ts,handler.tsandprotocol/launcher.tsbefore 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.tsis 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-schemeattribute that watches a class on<html>, asdata-follow-langdoes for the language. Sites mark dark mode in too many ways (class="dark",data-theme,color-scheme) to pick one. A page callsconfigure({ 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.themesindefineBot;themeandcolorSchemeinaskdare('configure');themeonThemeandDefaultChat;- the
data-ask-themeattribute on the theme root.
The
?theme=and?scheme=parameters are the route's, documented indocs/protocol.md. It is a minor changeset. -
The site's demo becomes one bot. One
demobot with five looks replacessupport,sage,halo,careandbldr, and the dock switches the look without a reload, keeping the conversation.- The site then has three bots:
demo,space(0.9-d2) anddocs. - The five bots' different titles and greetings become one set of texts; the dock can pass a greeting per look with
configure.
- The site then has three bots:
-
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 howwidget.cssapplies 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.variantsright 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
colorSchemealready does.
- It keeps
- A public
useLook()hook instead of the look inuseChat: 38 B less in the headless case, but one more export to learn, andbot.variantswould 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.cssinstead 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 withconfigure.
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:
- Looks are visual only; texts stay per bot.
- A look's
cssreplaceswidget.css. - At most 8 looks besides the default.
- The iframe's stylesheet carries both color schemes for every bot.
useChatreads the look; nouseLook()hook.- An unknown name in React shows the default look; the loader keeps the current one.
- The look stays out of every report; the console lists the names.
- 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.
- Looks are visual only (decision 1). Recommended: yes; texts stay per bot. Alternative: a few texts per look (title, greeting, launcher label).
- A look's
cssreplaceswidget.css(decision 1). Recommended: replaces. Alternative: appends. - At most 8 looks besides the default (decision 1). Recommended: 8. Alternative: 4, which covers the demo with none to spare.
- 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.
useChatreads the look (decision 5, 38 B of the headless case). Recommended: yes. Alternative: a publicuseLook()hook.- 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.
- The look stays out of every report (decision 6), and the console lists the names. Recommended: yes.
- The order in 0.9 (decision 7): the server part of themes first on
define-bot.tsandhandler.ts, then the provider path. Recommended: yes; to be reconciled with the 0.9 core plan.