feat(embed-studio): make agent-generated charts theme-aware
Browse filesThe iframe already supports live light/dark swap via CSS variables and
a postMessage(setTheme) hot-swap, but the LLM kept generating charts
that hardcode colours into SVG attributes (fill="#333", stroke="black",
...). Those values can't be overridden by the parent's data-theme
attribute, so the chart looked correct on whatever theme the model
guessed and broken on the other one.
Two complementary fixes:
1. Forward the current theme (light/dark) from EmbedStudio through
useEmbedChat into the request context, and surface it in the
system prompt as a "Current theme" section. The model now knows
which background it's painting against and can self-correct.
2. Rewrite the "Colors & theming" section of the embed system prompt
with a concrete CSS pattern (scoped .d3-* rules driving fill/stroke
via var(--text-color), --axis-color, --grid-color, ...) and an
explicit ban on hardcoded SVG colour attributes. Add a hint about
window.__chartRerender for JS-driven palettes.
No public API change - the new context.theme field is opt-in and the
backend gracefully falls back to no theme block when it's missing.
Co-authored-by: Cursor <cursoragent@cursor.com>
Files changed (4)
2 unmodified lines3456785 unmodified lines1415161718196 unmodified lines2627282930312 unmodified linesBANNER_SYSTEM_PROMPT,buildEmbedContext,buildDataFilesContext,} from "./embed-system-prompt.js";import { embedTools } from "./embed-tools.js";import { streamChatResponse } from "./stream-handler.js";5 unmodified linesconst isBanner = Boolean(context?.isBanner);const isScratch = Boolean(context?.isScratch);const dataFilesBlock = buildDataFilesContext(context?.dataFiles);const parts = [EMBED_SYSTEM_PROMPT];if (isBanner) parts.push(BANNER_SYSTEM_PROMPT);6 unmodified lines"underlying file and keep the document in sync.",);}if (contextBlock) parts.push(`## Current chart\n\n${contextBlock}`);if (dataFilesBlock) parts.push(`## Attached data\n\n${dataFilesBlock}`);const systemPrompt = parts.join("\n\n");2 unmodified lines34567895 unmodified lines151617181920216 unmodified lines282930313233342 unmodified linesBANNER_SYSTEM_PROMPT,buildEmbedContext,buildDataFilesContext,buildThemeContext,} from "./embed-system-prompt.js";import { embedTools } from "./embed-tools.js";import { streamChatResponse } from "./stream-handler.js";5 unmodified linesconst isBanner = Boolean(context?.isBanner);const isScratch = Boolean(context?.isScratch);const dataFilesBlock = buildDataFilesContext(context?.dataFiles);const themeBlock = buildThemeContext(context?.theme);const parts = [EMBED_SYSTEM_PROMPT];if (isBanner) parts.push(BANNER_SYSTEM_PROMPT);6 unmodified lines"underlying file and keep the document in sync.",);}if (themeBlock) parts.push(themeBlock);if (contextBlock) parts.push(`## Current chart\n\n${contextBlock}`);if (dataFilesBlock) parts.push(`## Attached data\n\n${dataFilesBlock}`);const systemPrompt = parts.join("\n\n");
79 unmodified lines808182838485868788899091929368 unmodified lines16216316416516616779 unmodified lines\`\`\`### Colors & theming (MANDATORY)- Get colors from \`window.ColorPalettes\`:- \`ColorPalettes.getColors('categorical', 8)\` - 8-color categorical palette- \`ColorPalettes.getColors('sequential', 8)\` - sequential blues- \`ColorPalettes.getColors('diverging', 9)\` - red-blue diverging- \`ColorPalettes.getPrimary()\` - current primary accent color- Use CSS variables for theming: \`--primary-color\`, \`--text-color\`, \`--muted-color\`, \`--surface-bg\`, \`--border-color\`- Axis/tick/grid: \`--axis-color\`, \`--tick-color\`, \`--grid-color\`- NEVER hardcode color arrays### Layout rules- SVG for chart primitives (marks, axes, gridlines) only68 unmodified linesreturn `<embed>\n${embedHtml}\n</embed>`;}function formatDataFileLine(file: DataFileMeta): string {const sizeKb = (file.size / 1024).toFixed(1);const rows = file.rowCount !== undefined ? ` rows=${file.rowCount}` : "";79 unmodified lines808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114268 unmodified lines21121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024179 unmodified lines\`\`\`### Colors & theming (MANDATORY)The host iframe ships with both **light** and **dark** themes, switchedlive by the user via a \`data-theme\` attribute on \`<html>\`. Your chartMUST adapt to both without re-rendering.**Categorical / sequential / diverging palettes** - get from\`window.ColorPalettes\`. The palettes are tuned to read on both lightand dark backgrounds:- \`ColorPalettes.getColors('categorical', 8)\`- \`ColorPalettes.getColors('sequential', 8)\`- \`ColorPalettes.getColors('diverging', 9)\`- \`ColorPalettes.getPrimary()\` - current primary accent color**Text / axes / grid / surfaces** - drive them with CSS variables, NEVERwith SVG \`fill=\` / \`stroke=\` / inline JS color strings. SVG presentationattributes don't parse \`var(--*)\`, so you MUST either:1. **Set them in the scoped \`<style>\` block (preferred)** - thebrowser hot-swaps them when the theme flips, no JS needed:\`\`\`css.d3-yourname { color: var(--text-color); }.d3-yourname .axis path,.d3-yourname .axis line { stroke: var(--axis-color); }.d3-yourname .axis text { fill: var(--tick-color); }.d3-yourname .grid line { stroke: var(--grid-color); }.d3-yourname .tooltip { background: var(--surface-bg); color: var(--text-color); border: 1px solid var(--border-color); }\`\`\`2. **Use \`currentColor\`** on SVG elements that should follow thecontainer's CSS \`color\`:\`\`\`jssvg.append('text').attr('fill', 'currentColor')...\`\`\`Available CSS variables (light + dark values are pre-defined):\`--text-color\`, \`--muted-color\`, \`--surface-bg\`, \`--page-bg\`,\`--border-color\`, \`--axis-color\`, \`--tick-color\`, \`--grid-color\`,\`--primary-color\`.**Forbidden patterns** (they freeze the chart on one theme):- \`fill="#333"\`, \`stroke="black"\`, \`d3.color('white')\`, \`'rgba(0,0,0,.5)'\`for axes / text / grid / tooltips.- Reading \`getComputedStyle(...)\` once at mount and baking the valueinto attributes. CSS handles it; don't reinvent it.- Conditional palettes based on \`document.documentElement.dataset.theme\`for axes/text - just use the CSS vars above.**Live theme swap**: if your chart paints categorical / sequentialcolours via JS (which is fine), define \`window.__chartRerender =function(){ /* re-run draw() */ }\` so the host can re-tint when theuser flips the theme. CSS-driven properties (text, axes, grid) updateautomatically and don't need this.NEVER hardcode categorical palettes - always go through\`window.ColorPalettes\`.### Layout rules- SVG for chart primitives (marks, axes, gridlines) only68 unmodified linesreturn `<embed>\n${embedHtml}\n</embed>`;}/*** Tell the agent which theme the user is currently looking at, so it* can sanity-check its colour choices in context (e.g. "is this axis* label readable on the current background?") and know which variant* to debug visually. The CSS variables already cover both themes, but* this also surfaces accidental hardcoded colours early - the agent* will see e.g. "current theme: dark" and notice if it just wrote* `fill="#000"`.*/export function buildThemeContext(theme?: string): string {const t = theme === "dark" ? "dark" : theme === "light" ? "light" : null;if (!t) return "";return ["## Current theme","",`The user is currently viewing the article in **${t}** mode. The`,"iframe automatically swaps CSS variables when the theme flips, so","any chart that drives axes/text/grid via the `--*` variables (see","the Colors & theming section) will adapt without any extra work.","Use this as a sanity check: if you ever feel tempted to hardcode a",`colour, ask yourself "would this still be readable in ${t === "dark" ? "light" : "dark"} mode?" - the`,"answer is almost always no.",].join("\n");}function formatDataFileLine(file: DataFileMeta): string {const sizeKb = (file.size / 1024).toFixed(1);const rows = file.rowCount !== undefined ? ` rows=${file.rowCount}` : "";
161 unmodified lines162163164165166167168161 unmodified linesactiveRef.current = activeFrame;const data = useEmbedData({ dataStore, userId });const chat = useEmbedChat({ embedStore, dataStore, src, modelRef, userId, onRename });const selectedFile = selectedDataFile ? data.getFile(selectedDataFile) : undefined;161 unmodified lines162163164165166167168161 unmodified linesactiveRef.current = activeFrame;const data = useEmbedData({ dataStore, userId });const chat = useEmbedChat({ embedStore, dataStore, src, modelRef, userId, isDark, onRename });const selectedFile = selectedDataFile ? data.getFile(selectedDataFile) : undefined;
15 unmodified lines16171819202151 unmodified lines7374757677787918 unmodified lines989910010110210319 unmodified lines12312412512612712815 unmodified linessrc: string;modelRef: React.RefObject<string>;userId: string;/*** Called when the agent picks a descriptive filename via createEmbed.* Parent is expected to update the htmlEmbed node's src attribute in51 unmodified linesreturn `embed:${src}`;}export function useEmbedChat({ embedStore, dataStore, src, modelRef, userId, onRename }: UseEmbedChatOptions) {const [input, setInput] = useState("");// srcRef is the source of truth for the active embed filename. It// starts from the `src` prop but may change mid-session when the18 unmodified lines// restored messages end with completed tool calls.const userHasSentRef = useRef(false);const getEmbedContext = useCallback(() => {if (!embedStore || !srcRef.current) return {};const currentSrc = srcRef.current;19 unmodified linesisBanner,isScratch,dataFiles,};}, [embedStore, dataStore]);15 unmodified lines1617181920212223242526272851 unmodified lines8081828384858618 unmodified lines10510610710810911011111211311411519 unmodified lines13513613713813914014115 unmodified linessrc: string;modelRef: React.RefObject<string>;userId: string;/*** Current article theme (light/dark). Forwarded to the agent so it* can sanity-check its colour choices and bake theme-aware CSS into* generated charts (instead of hardcoding axis/text colours that* only happen to look right under one theme).*/isDark?: boolean;/*** Called when the agent picks a descriptive filename via createEmbed.* Parent is expected to update the htmlEmbed node's src attribute in51 unmodified linesreturn `embed:${src}`;}export function useEmbedChat({ embedStore, dataStore, src, modelRef, userId, isDark, onRename }: UseEmbedChatOptions) {const [input, setInput] = useState("");// srcRef is the source of truth for the active embed filename. It// starts from the `src` prop but may change mid-session when the18 unmodified lines// restored messages end with completed tool calls.const userHasSentRef = useRef(false);// Track the current theme via ref so getEmbedContext always reads the// latest value without forcing a callback identity churn on toggle.const isDarkRef = useRef(!!isDark);isDarkRef.current = !!isDark;const getEmbedContext = useCallback(() => {if (!embedStore || !srcRef.current) return {};const currentSrc = srcRef.current;19 unmodified linesisBanner,isScratch,dataFiles,theme: isDarkRef.current ? "dark" : "light",};}, [embedStore, dataStore]);