/** * The generative UI catalog: components the AI may place inside a reply, written in OpenUI Lang * (https://www.openui.com, MIT). The server turns this catalog into prompt instructions; the web * app attaches a React renderer to each entry. Only names listed here can ever be drawn, so model * output cannot inject markup or styles. * * Positional arguments follow the key order of each `props` object, so keys are never reordered. */ import { action, markReactive, tagSchemaId } from '@openuidev/lang-core'; import { z } from 'zod'; const reactive = (schema: T): T => { markReactive(schema); return schema; }; /** Any component, for container children. */ const Component = z.any(); tagSchemaId(Component, 'Component'); const Tone = z.enum(['neutral', 'accent', 'good', 'warn', 'bad']); tagSchemaId(Tone, 'Tone'); /* ------------------------------ layout ------------------------------ */ const StackProps = z.object({ children: z.array(Component), direction: z.enum(['column', 'row']).optional(), gap: z.enum(['s', 'm', 'l']).optional(), }); const CardProps = z.object({ children: z.array(Component), title: z.string().optional(), subtitle: z.string().optional(), tone: Tone.optional() }); const GridProps = z.object({ children: z.array(Component), columns: z.number().optional() }); const TabProps = z.object({ label: z.string(), children: z.array(Component) }); const TabsProps = z.object({ tabs: z.array(TabProps) }); const AccordionItemProps = z.object({ title: z.string(), children: z.array(Component) }); const AccordionProps = z.object({ items: z.array(AccordionItemProps) }); /* ------------------------------ content ------------------------------ */ const HeadingProps = z.object({ text: z.string(), size: z.enum(['s', 'm', 'l']).optional() }); const TextProps = z.object({ text: z.string(), tone: z.enum(['default', 'muted', 'small']).optional() }); const CalloutProps = z.object({ text: z.string(), tone: z.enum(['info', 'tip', 'warning', 'danger']).optional(), title: z.string().optional() }); const BadgeProps = z.object({ label: z.string(), tone: Tone.optional() }); const StatProps = z.object({ label: z.string(), value: z.union([z.string(), z.number()]), detail: z.string().optional(), tone: Tone.optional() }); const ProgressProps = z.object({ label: z.string(), value: z.number(), max: z.number().optional(), tone: Tone.optional() }); const TableProps = z.object({ columns: z.array(z.string()), rows: z.array(z.array(z.union([z.string(), z.number(), z.null()]))), caption: z.string().optional() }); const ListProps = z.object({ items: z.array(z.string()), ordered: z.boolean().optional() }); const StepProps = z.object({ title: z.string(), detail: z.string().optional() }); const StepsProps = z.object({ steps: z.array(StepProps) }); const DividerProps = z.object({}); /* ------------------------------ charts ------------------------------ */ const SeriesProps = z.object({ name: z.string(), values: z.array(z.number().nullable()) }); const SliceProps = z.object({ label: z.string(), value: z.number() }); const BarChartProps = z.object({ labels: z.array(z.string()), series: z.array(SeriesProps), title: z.string().optional(), unit: z.string().optional(), stacked: z.boolean().optional(), horizontal: z.boolean().optional(), }); const LineChartProps = z.object({ labels: z.array(z.string()), series: z.array(SeriesProps), title: z.string().optional(), unit: z.string().optional(), area: z.boolean().optional() }); const PieChartProps = z.object({ slices: z.array(SliceProps), title: z.string().optional(), unit: z.string().optional(), donut: z.boolean().optional() }); /* ------------------------------ interactive ------------------------------ */ const SliderProps = z.object({ name: z.string(), label: z.string(), min: z.number(), max: z.number(), step: z.number().optional(), value: reactive(z.number().optional()), unit: z.string().optional(), }); const NumberInputProps = z.object({ name: z.string(), label: z.string(), value: reactive(z.number().optional()), unit: z.string().optional(), step: z.number().optional() }); const OptionProps = z.object({ value: z.string(), label: z.string() }); const SelectProps = z.object({ name: z.string(), label: z.string(), options: z.array(OptionProps), value: reactive(z.string().optional()) }); const ToggleProps = z.object({ name: z.string(), label: z.string(), value: reactive(z.boolean().optional()) }); const ButtonProps = z.object({ label: z.string(), action: action().optional(), variant: z.enum(['primary', 'secondary', 'ghost']).optional() }); const LinkProps = z.object({ label: z.string(), url: z.string() }); const FollowUpsProps = z.object({ prompts: z.array(z.string()) }); const QuestionProps = z.object({ stem: z.string(), options: z.array(z.string()), correct: z.array(z.number()), rationale: z.string().optional(), multi: z.boolean().optional(), }); const FlashCardProps = z.object({ front: z.string(), back: z.string() }); const FlashcardsProps = z.object({ cards: z.array(FlashCardProps) }); export interface GenUiComponentSpec { name: string; description: string; props: z.ZodObject; } export const GENUI_COMPONENTS: GenUiComponentSpec[] = [ { name: 'Stack', description: 'Root container. Lays children out in a column (default) or a row.', props: StackProps }, { name: 'Card', description: 'A bordered panel grouping related content, with an optional title.', props: CardProps }, { name: 'Grid', description: 'Children in equal columns (2-4); collapses to one column on phones.', props: GridProps }, { name: 'Tabs', description: 'Tabbed sections; only one tab is visible at a time.', props: TabsProps }, { name: 'Tab', description: 'One tab of Tabs.', props: TabProps }, { name: 'Accordion', description: 'Collapsible sections, all closed at first.', props: AccordionProps }, { name: 'AccordionItem', description: 'One section of an Accordion.', props: AccordionItemProps }, { name: 'Heading', description: 'A short heading.', props: HeadingProps }, { name: 'Text', description: 'A paragraph. Supports inline Markdown (**bold**, *italic*, lists).', props: TextProps }, { name: 'Callout', description: 'Highlighted note: info, tip, warning (cautions) or danger (safety-critical, e.g. a contraindication).', props: CalloutProps }, { name: 'Badge', description: 'A small coloured label.', props: BadgeProps }, { name: 'Stat', description: 'A key number with a label, e.g. a score or a calculated dose.', props: StatProps }, { name: 'Progress', description: 'A labelled progress bar (value out of max, default 100).', props: ProgressProps }, { name: 'Table', description: 'A table with a header row. Good for comparisons (drug A vs B, normal vs abnormal values).', props: TableProps }, { name: 'List', description: 'A bulleted or numbered list of short items.', props: ListProps }, { name: 'Steps', description: 'A numbered sequence: procedures, pathophysiology chains, priorities.', props: StepsProps }, { name: 'Step', description: 'One step of Steps.', props: StepProps }, { name: 'Divider', description: 'A thin separator line.', props: DividerProps }, { name: 'BarChart', description: 'Bar chart: one bar per label for each series. Use for comparing amounts or scores.', props: BarChartProps }, { name: 'LineChart', description: 'Line chart over ordered labels (dates, attempts, hours). Use for trends.', props: LineChartProps }, { name: 'PieChart', description: 'Parts of a whole. Use for 2-6 slices only.', props: PieChartProps }, { name: 'Series', description: 'A named list of values, one per chart label.', props: SeriesProps }, { name: 'Slice', description: 'One slice of a PieChart.', props: SliceProps }, { name: 'Slider', description: 'A slider bound to a $variable. For calculators and what-if explorations.', props: SliderProps }, { name: 'NumberInput', description: 'A number field bound to a $variable.', props: NumberInputProps }, { name: 'Select', description: 'A dropdown bound to a $variable.', props: SelectProps }, { name: 'Option', description: 'One choice of a Select.', props: OptionProps }, { name: 'Toggle', description: 'An on/off switch bound to a $variable.', props: ToggleProps }, { name: 'Button', description: 'A button that runs an action, usually @ToAssistant("message") to ask a follow-up.', props: ButtonProps }, { name: 'Link', description: 'A link that opens a web page in a new tab.', props: LinkProps }, { name: 'FollowUps', description: 'Up to 4 suggested next questions shown as chips; clicking one sends it as the user\'s message.', props: FollowUpsProps }, { name: 'Question', description: 'A practice question the user answers inline and checks themselves. correct lists the 0-based positions of the correct options. multi=true for select-all-that-apply.', props: QuestionProps, }, { name: 'Flashcards', description: 'A small set of flip cards (2-12) to review inline.', props: FlashcardsProps }, { name: 'FlashCard', description: 'One card of Flashcards.', props: FlashCardProps }, ]; export const GENUI_GROUPS = [ { name: 'Layout', components: ['Stack', 'Card', 'Grid', 'Tabs', 'Tab', 'Accordion', 'AccordionItem', 'Divider'] }, { name: 'Content', components: ['Heading', 'Text', 'Callout', 'Badge', 'Stat', 'Progress', 'Table', 'List', 'Steps', 'Step'] }, { name: 'Charts', components: ['BarChart', 'LineChart', 'PieChart', 'Series', 'Slice'], notes: ['- Chart numbers must come from real data: tool results, the student\'s scores, or the conversation. Never invent data to fill a chart.'] }, { name: 'Interactive', components: ['Slider', 'NumberInput', 'Select', 'Option', 'Toggle', 'Button', 'Link', 'FollowUps'], notes: ['- For calculators, bind inputs to $variables and compute results with expressions, e.g. Stat("Dose", @Round($weight * $mgPerKg, 1) + " mg").'], }, { name: 'Study', components: ['Question', 'Flashcards', 'FlashCard'], notes: ['- Question options are plain strings; correct holds 0-based indexes into options.'] }, ]; export const GENUI_ROOT = 'Stack'; export const GENUI_PREAMBLE = `## Interactive blocks in replies Your replies are Markdown. When a visual or interactive element genuinely helps, embed it in the reply as a fenced code block tagged openui-lang: a chart of the student's scores, a comparison table, a process as steps, a calculator with sliders, a quick self-check question, a few flashcards, or follow-up suggestions. Text outside the fences stays ordinary Markdown, so write your explanation before and after the block. Keep simple answers simple: no block when a sentence or a Markdown list does the job. Use at most two blocks in one reply. Each block is a small, separate program written in openui-lang as described below, and nothing else goes inside the fence.`; export const GENUI_RULES = [ 'Fence each block as ```openui-lang … ``` and start it with root = Stack([...]).', 'Never put document citations like [c42] inside a block; cite in the Markdown around it.', 'Keep blocks compact: a chat column is about 600 pixels wide. Prefer Card, Table, Steps and one chart over large layouts.', 'Do not repeat the same content in the Markdown and in the block.', ]; export const GENUI_EXAMPLES = [ `Example: a weight-based dose calculator. \`\`\`openui-lang root = Stack([calc]) $weight = 70 $dose = 1 calc = Card([w, d, result, note], "Enoxaparin dose") w = Slider("weight", "Weight", 30, 150, 1, $weight, "kg") d = Slider("dose", "Dose", 0.5, 1.5, 0.5, $dose, "mg/kg") result = Stat("Dose to give", @Round($weight * $dose, 1) + " mg", "every 12 hours", "accent") note = Callout("Check platelets and renal function before giving.", "warning") \`\`\``, `Example: a self-check question and follow-ups. \`\`\`openui-lang root = Stack([q, more]) q = Question("Which finding needs immediate action in a client receiving furosemide?", ["Potassium 3.0 mEq/L", "Urine output 60 mL/h", "BP 128/80", "Weight down 0.5 kg"], [0], "Hypokalemia raises the risk of dysrhythmias, especially with digoxin.") more = FollowUps(["Quiz me on loop diuretics", "Compare furosemide and spironolactone"]) \`\`\``, ];