text
stringlengths
0
1.47k
export function resolveTheme(): ResolvedTheme {
const computed = getComputedStyle(document.body)
const themeVars: Record<string, string> = {}
for (const [frameName, hostToken] of TOKEN_BRIDGE) {
themeVars[frameName] = computed.getPropertyValue(hostToken)
}
const scheme = computed.colorScheme
const colorScheme = scheme.includes('dark') && !scheme.includes('light')
? 'dark'
: scheme.includes('light') && !scheme.includes('dark')
? 'light'
: document.body.hasAttribute('data-ds-dark-theme')
? 'dark'
: matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
return { themeVars, colorScheme }
}
# ===== file: src/fragment.ts =====
/**
* Pure fragment contract shared by the tool (validation at execute time), the
* browser card (meta narrowing at render time), and the specs. No I/O and no
* DOM so both halves and vitest can load it unchanged.
*
* A *fragment* is the model-authored inline-HTML body of one visualization:
* literal markup without a document skeleton. The card owns the skeleton — it
* wraps the fragment in a sandboxed iframe document with its own CSP — so a
* fragment that ships its own `<!doctype>`/`<html>`/`<head>`/`<body>` would
* nest documents and is rejected loudly instead of rendered broken.
*
* @module @nagi-ovo/dsh-visualize/fragment
*/
/**
* Wire name of the tool, the keyed toolview, and the streaming-preview match.
* Lives in this pure module so the browser half can import it without pulling
* the node-side tool implementation into the client bundle.
*/
export const VISUALIZE_TOOL_NAME = 'visualize'
/** Width intent of one visualization card. */
export type VisualizeMode = 'inline' | 'wide'
/**
* What one `visualize` call does: render a card from whole markup, or patch
* the markup of a card an earlier call already rendered.
*/
export type VisualizeAction = 'create' | 'update'
/** The `tool/result` meta descriptor persisted for replay-stable rendering. */
export interface VisualizeMeta {
/** Discriminant for consumers sharing the meta channel. */
kind: 'visualize'
/** The validated fragment body, inlined so replay never re-reads the file. */
fragment: string
/** Concise human title shown in the card header. */
title: string
/** Width intent; `wide` asks the card for the expanded inline surface. */
mode: VisualizeMode
/** Session-relative or absolute source path, kept for provenance display. */
path: string
}
/** Document-skeleton tags a fragment must not contain (case-insensitive). */
const SKELETON_TAG = /<!doctype\b|<\s*(?:html|head|body)\b/iu
/**
* Validate one fragment against the inline contract.
* @param fragment - the file content the model wrote.
* @param maxBytes - deployment size ceiling for one fragment.
* @returns the fragment's UTF-8 size in bytes.
* @throws Error naming the violated rule; the tool surfaces it as `isError`.
*/
export function validateFragment(fragment: string, maxBytes: number): number {
if (fragment.trim().length === 0) {
throw new Error('invalid visualization: the fragment file is empty')
}
const sizeBytes = byteLength(fragment)
if (sizeBytes > maxBytes) {
throw new Error(
`invalid visualization: fragment is ${sizeBytes} bytes, over the ${maxBytes}-byte limit — `
+ 'shrink the inline data first (fewer rows, coarser buckets, fewer decimals)',
)
}
const skeleton = SKELETON_TAG.exec(fragment)
if (skeleton) {
throw new Error(
`invalid visualization: fragment contains a document-skeleton tag (${JSON.stringify(skeleton[0])}) — `
+ 'write only the inline body; the host supplies <!doctype>, <html>, <head>, and <body>',
)
}
return sizeBytes
}
/**
* Characters of real card content quoted back when a patch fails to apply, so
* the model can correct `old_str` from the true bytes without re-reading the
* whole card.
*/
const PATCH_CONTEXT_CHARS = 160