@json-render/core 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/visibility.ts","../src/props.ts","../src/actions.ts","../src/validation.ts","../src/spec-validator.ts","../src/schema.ts","../src/prompt.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type {\n VisibilityCondition,\n StateCondition,\n ItemCondition,\n IndexCondition,\n SingleCondition,\n AndCondition,\n OrCondition,\n StateModel,\n} from \"./types\";\nimport { getByPath } from \"./types\";\n\n// =============================================================================\n// Schemas\n// =============================================================================\n\n/**\n * Schema for a single state condition.\n */\nconst numericOrStateRef = z.union([\n z.number(),\n z.object({ $state: z.string() }),\n]);\n\nconst comparisonOps = {\n eq: z.unknown().optional(),\n neq: z.unknown().optional(),\n gt: numericOrStateRef.optional(),\n gte: numericOrStateRef.optional(),\n lt: numericOrStateRef.optional(),\n lte: numericOrStateRef.optional(),\n not: z.literal(true).optional(),\n};\n\nconst StateConditionSchema = z.object({\n $state: z.string(),\n ...comparisonOps,\n});\n\nconst ItemConditionSchema = z.object({\n $item: z.string(),\n ...comparisonOps,\n});\n\nconst IndexConditionSchema = z.object({\n $index: z.literal(true),\n ...comparisonOps,\n});\n\nconst SingleConditionSchema = z.union([\n StateConditionSchema,\n ItemConditionSchema,\n IndexConditionSchema,\n]);\n\n/**\n * Visibility condition schema.\n *\n * Lazy because `OrCondition` can recursively contain `VisibilityCondition`.\n */\nexport const VisibilityConditionSchema: z.ZodType<VisibilityCondition> = z.lazy(\n () =>\n z.union([\n z.boolean(),\n SingleConditionSchema,\n z.array(SingleConditionSchema),\n z.object({ $and: z.array(VisibilityConditionSchema) }),\n z.object({ $or: z.array(VisibilityConditionSchema) }),\n ]),\n);\n\n// =============================================================================\n// Context\n// =============================================================================\n\n/**\n * Context for evaluating visibility conditions.\n *\n * `repeatItem` and `repeatIndex` are only present inside a `repeat` scope\n * and enable `$item` / `$index` conditions.\n */\nexport interface VisibilityContext {\n stateModel: StateModel;\n /** The current repeat item (set inside a repeat scope). */\n repeatItem?: unknown;\n /** The current repeat array index (set inside a repeat scope). */\n repeatIndex?: number;\n}\n\n// =============================================================================\n// Evaluation\n// =============================================================================\n\n/**\n * Resolve a comparison value. If it's a `{ $state }` reference, look it up;\n * otherwise return the literal.\n */\nfunction resolveComparisonValue(\n value: unknown,\n ctx: VisibilityContext,\n): unknown {\n if (typeof value === \"object\" && value !== null) {\n if (\n \"$state\" in value &&\n typeof (value as Record<string, unknown>).$state === \"string\"\n ) {\n return getByPath(ctx.stateModel, (value as { $state: string }).$state);\n }\n }\n return value;\n}\n\n/**\n * Type guards for condition sources.\n */\nfunction isItemCondition(cond: SingleCondition): cond is ItemCondition {\n return \"$item\" in cond;\n}\n\nfunction isIndexCondition(cond: SingleCondition): cond is IndexCondition {\n return \"$index\" in cond;\n}\n\n/**\n * Resolve the left-hand-side value of a condition based on its source.\n */\nfunction resolveConditionValue(\n cond: SingleCondition,\n ctx: VisibilityContext,\n): unknown {\n if (isIndexCondition(cond)) {\n return ctx.repeatIndex;\n }\n if (isItemCondition(cond)) {\n if (ctx.repeatItem === undefined) return undefined;\n return cond.$item === \"\"\n ? ctx.repeatItem\n : getByPath(ctx.repeatItem, cond.$item);\n }\n // StateCondition\n return getByPath(ctx.stateModel, (cond as StateCondition).$state);\n}\n\n/**\n * Evaluate a single condition against the context.\n *\n * When `not` is `true`, the final result is inverted — this applies to\n * whichever operator is present (or to the truthiness check if no operator\n * is given). For example:\n * - `{ $state: \"/x\", not: true }` → `!Boolean(value)`\n * - `{ $state: \"/x\", gt: 5, not: true }` → `!(value > 5)`\n */\nfunction evaluateCondition(\n cond: SingleCondition,\n ctx: VisibilityContext,\n): boolean {\n const value = resolveConditionValue(cond, ctx);\n let result: boolean;\n\n // Equality\n if (cond.eq !== undefined) {\n const rhs = resolveComparisonValue(cond.eq, ctx);\n result = value === rhs;\n }\n // Inequality\n else if (cond.neq !== undefined) {\n const rhs = resolveComparisonValue(cond.neq, ctx);\n result = value !== rhs;\n }\n // Greater than\n else if (cond.gt !== undefined) {\n const rhs = resolveComparisonValue(cond.gt, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value > rhs\n : false;\n }\n // Greater than or equal\n else if (cond.gte !== undefined) {\n const rhs = resolveComparisonValue(cond.gte, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value >= rhs\n : false;\n }\n // Less than\n else if (cond.lt !== undefined) {\n const rhs = resolveComparisonValue(cond.lt, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value < rhs\n : false;\n }\n // Less than or equal\n else if (cond.lte !== undefined) {\n const rhs = resolveComparisonValue(cond.lte, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value <= rhs\n : false;\n }\n // Truthiness (no operator)\n else {\n result = Boolean(value);\n }\n\n // `not` inverts the result of any condition\n return cond.not === true ? !result : result;\n}\n\n/**\n * Type guard for AndCondition\n */\nfunction isAndCondition(\n condition: VisibilityCondition,\n): condition is AndCondition {\n return (\n typeof condition === \"object\" &&\n condition !== null &&\n !Array.isArray(condition) &&\n \"$and\" in condition\n );\n}\n\n/**\n * Type guard for OrCondition\n */\nfunction isOrCondition(\n condition: VisibilityCondition,\n): condition is OrCondition {\n return (\n typeof condition === \"object\" &&\n condition !== null &&\n !Array.isArray(condition) &&\n \"$or\" in condition\n );\n}\n\n/**\n * Evaluate a visibility condition.\n *\n * - `undefined` → visible\n * - `boolean` → that value\n * - `SingleCondition` → evaluate single condition\n * - `SingleCondition[]` → implicit AND (all must be true)\n * - `AndCondition` → `{ $and: [...] }`, explicit AND\n * - `OrCondition` → `{ $or: [...] }`, at least one must be true\n */\nexport function evaluateVisibility(\n condition: VisibilityCondition | undefined,\n ctx: VisibilityContext,\n): boolean {\n // No condition = visible\n if (condition === undefined) {\n return true;\n }\n\n // Boolean literal\n if (typeof condition === \"boolean\") {\n return condition;\n }\n\n // Array = implicit AND\n if (Array.isArray(condition)) {\n return condition.every((c) => evaluateCondition(c, ctx));\n }\n\n // Explicit AND condition\n if (isAndCondition(condition)) {\n return condition.$and.every((child) => evaluateVisibility(child, ctx));\n }\n\n // OR condition\n if (isOrCondition(condition)) {\n return condition.$or.some((child) => evaluateVisibility(child, ctx));\n }\n\n // Single condition\n return evaluateCondition(condition, ctx);\n}\n\n// =============================================================================\n// Helpers\n// =============================================================================\n\n/**\n * Helper to create visibility conditions.\n */\nexport const visibility = {\n /** Always visible */\n always: true as const,\n\n /** Never visible */\n never: false as const,\n\n /** Visible when state path is truthy */\n when: (path: string): StateCondition => ({ $state: path }),\n\n /** Visible when state path is falsy */\n unless: (path: string): StateCondition => ({ $state: path, not: true }),\n\n /** Equality check */\n eq: (path: string, value: unknown): StateCondition => ({\n $state: path,\n eq: value,\n }),\n\n /** Not equal check */\n neq: (path: string, value: unknown): StateCondition => ({\n $state: path,\n neq: value,\n }),\n\n /** Greater than */\n gt: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n gt: value,\n }),\n\n /** Greater than or equal */\n gte: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n gte: value,\n }),\n\n /** Less than */\n lt: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n lt: value,\n }),\n\n /** Less than or equal */\n lte: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n lte: value,\n }),\n\n /** AND multiple conditions */\n and: (...conditions: VisibilityCondition[]): AndCondition => ({\n $and: conditions,\n }),\n\n /** OR multiple conditions */\n or: (...conditions: VisibilityCondition[]): OrCondition => ({\n $or: conditions,\n }),\n};\n","import type { VisibilityCondition, StateModel } from \"./types\";\nimport { getByPath } from \"./types\";\nimport { evaluateVisibility, type VisibilityContext } from \"./visibility\";\n\n// =============================================================================\n// Prop Expression Types\n// =============================================================================\n\n/**\n * A prop expression that resolves to a value based on state.\n *\n * - `{ $state: string }` reads a value from the global state model\n * - `{ $item: string }` reads a field from the current repeat item\n * (relative path into the item object; use `\"\"` for the whole item)\n * - `{ $index: true }` returns the current repeat array index. Uses `true`\n * as a sentinel flag because the index is a scalar with no sub-path to\n * navigate — unlike `$item` which needs a path into the item object.\n * - `{ $bindState: string }` two-way binding to a global state path —\n * resolves to the value at the path (like `$state`) AND exposes the\n * resolved path so the component can write back.\n * - `{ $bindItem: string }` two-way binding to a field on the current\n * repeat item — resolves via `repeatBasePath + path` and exposes the\n * absolute state path for write-back.\n * - `{ $cond, $then, $else }` conditionally picks a value\n * - `{ $computed: string, args?: Record<string, PropExpression> }` calls a\n * registered function with resolved args and returns the result\n * - `{ $template: string }` interpolates `${/path}` references in the\n * string with values from the state model\n * - Any other value is a literal (passthrough)\n */\nexport type PropExpression<T = unknown> =\n | T\n | { $state: string }\n | { $item: string }\n | { $index: true }\n | { $bindState: string }\n | { $bindItem: string }\n | {\n $cond: VisibilityCondition;\n $then: PropExpression<T>;\n $else: PropExpression<T>;\n }\n | { $computed: string; args?: Record<string, unknown> }\n | { $template: string };\n\n/**\n * Function signature for `$computed` expressions.\n * Receives a record of resolved argument values and returns a computed result.\n */\nexport type ComputedFunction = (args: Record<string, unknown>) => unknown;\n\n/**\n * Context for resolving prop expressions.\n * Extends {@link VisibilityContext} with an optional `repeatBasePath` used\n * to resolve `$bindItem` paths to absolute state paths.\n */\nexport interface PropResolutionContext extends VisibilityContext {\n /** Absolute state path to the current repeat item (e.g. \"/todos/0\"). Set inside repeat scopes. */\n repeatBasePath?: string;\n /** Named functions available for `$computed` expressions. */\n functions?: Record<string, ComputedFunction>;\n}\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\nfunction isStateExpression(value: unknown): value is { $state: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$state\" in value &&\n typeof (value as Record<string, unknown>).$state === \"string\"\n );\n}\n\nfunction isItemExpression(value: unknown): value is { $item: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$item\" in value &&\n typeof (value as Record<string, unknown>).$item === \"string\"\n );\n}\n\nfunction isIndexExpression(value: unknown): value is { $index: true } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$index\" in value &&\n (value as Record<string, unknown>).$index === true\n );\n}\n\nfunction isBindStateExpression(\n value: unknown,\n): value is { $bindState: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$bindState\" in value &&\n typeof (value as Record<string, unknown>).$bindState === \"string\"\n );\n}\n\nfunction isBindItemExpression(value: unknown): value is { $bindItem: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$bindItem\" in value &&\n typeof (value as Record<string, unknown>).$bindItem === \"string\"\n );\n}\n\nfunction isCondExpression(\n value: unknown,\n): value is { $cond: VisibilityCondition; $then: unknown; $else: unknown } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$cond\" in value &&\n \"$then\" in value &&\n \"$else\" in value\n );\n}\n\nfunction isComputedExpression(\n value: unknown,\n): value is { $computed: string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$computed\" in value &&\n typeof (value as Record<string, unknown>).$computed === \"string\"\n );\n}\n\nfunction isTemplateExpression(value: unknown): value is { $template: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$template\" in value &&\n typeof (value as Record<string, unknown>).$template === \"string\"\n );\n}\n\n// Module-level set to avoid spamming console.warn on every render for the same\n// unknown $computed function name. Once the set reaches WARNED_COMPUTED_MAX,\n// new names are no longer deduplicated (warnings still fire) but the set stops\n// growing, preventing unbounded memory use in long-lived processes (e.g. SSR).\nconst WARNED_COMPUTED_MAX = 100;\nconst warnedComputedFns = new Set<string>();\n\n/** @internal Test-only: clear the deduplication set for $computed warnings. */\nexport function _resetWarnedComputedFns(): void {\n warnedComputedFns.clear();\n}\n\n// Same deduplication pattern for $template paths that don't start with \"/\".\nconst WARNED_TEMPLATE_MAX = 100;\nconst warnedTemplatePaths = new Set<string>();\n\n/** @internal Test-only: clear the deduplication set for $template warnings. */\nexport function _resetWarnedTemplatePaths(): void {\n warnedTemplatePaths.clear();\n}\n\n// =============================================================================\n// Prop Expression Resolution\n// =============================================================================\n\n// =============================================================================\n// $bindItem path resolution helper\n// =============================================================================\n\n/**\n * Resolve a `$bindItem` path into an absolute state path using the repeat\n * scope's base path.\n *\n * `\"\"` resolves to `repeatBasePath` (the whole item).\n * `\"field\"` resolves to `repeatBasePath + \"/field\"`.\n *\n * Returns `undefined` when no `repeatBasePath` is available (i.e. `$bindItem`\n * is used outside a repeat scope).\n */\nfunction resolveBindItemPath(\n itemPath: string,\n ctx: PropResolutionContext,\n): string | undefined {\n if (ctx.repeatBasePath == null) {\n console.warn(`$bindItem used outside repeat scope: \"${itemPath}\"`);\n return undefined;\n }\n if (itemPath === \"\") return ctx.repeatBasePath;\n return ctx.repeatBasePath + \"/\" + itemPath;\n}\n\n// =============================================================================\n// Prop Expression Resolution\n// =============================================================================\n\n/**\n * Resolve a single prop value that may contain expressions.\n * Handles $state, $item, $index, $bindState, $bindItem, and $cond/$then/$else in a single pass.\n */\nexport function resolvePropValue(\n value: unknown,\n ctx: PropResolutionContext,\n): unknown {\n if (value === null || value === undefined) {\n return value;\n }\n\n // $state: read from global state model\n if (isStateExpression(value)) {\n return getByPath(ctx.stateModel, value.$state);\n }\n\n // $item: read from current repeat item\n if (isItemExpression(value)) {\n if (ctx.repeatItem === undefined) return undefined;\n // \"\" means the whole item, \"field\" means a field on the item\n return value.$item === \"\"\n ? ctx.repeatItem\n : getByPath(ctx.repeatItem, value.$item);\n }\n\n // $index: return current repeat array index\n if (isIndexExpression(value)) {\n return ctx.repeatIndex;\n }\n\n // $bindState: two-way binding to global state path\n if (isBindStateExpression(value)) {\n return getByPath(ctx.stateModel, value.$bindState);\n }\n\n // $bindItem: two-way binding to repeat item field\n if (isBindItemExpression(value)) {\n const resolvedPath = resolveBindItemPath(value.$bindItem, ctx);\n if (resolvedPath === undefined) return undefined;\n return getByPath(ctx.stateModel, resolvedPath);\n }\n\n // $cond/$then/$else: evaluate condition and pick branch\n if (isCondExpression(value)) {\n const result = evaluateVisibility(value.$cond, ctx);\n return resolvePropValue(result ? value.$then : value.$else, ctx);\n }\n\n // $computed: call a registered function with resolved args\n if (isComputedExpression(value)) {\n const fn = ctx.functions?.[value.$computed];\n if (!fn) {\n if (!warnedComputedFns.has(value.$computed)) {\n if (warnedComputedFns.size < WARNED_COMPUTED_MAX) {\n warnedComputedFns.add(value.$computed);\n }\n console.warn(`Unknown $computed function: \"${value.$computed}\"`);\n }\n return undefined;\n }\n const resolvedArgs: Record<string, unknown> = {};\n if (value.args) {\n for (const [key, arg] of Object.entries(value.args)) {\n resolvedArgs[key] = resolvePropValue(arg, ctx);\n }\n }\n return fn(resolvedArgs);\n }\n\n // $template: interpolate ${/path} references with state values\n if (isTemplateExpression(value)) {\n return value.$template.replace(\n /\\$\\{([^}]+)\\}/g,\n (_match, rawPath: string) => {\n let path = rawPath;\n if (!path.startsWith(\"/\")) {\n if (!warnedTemplatePaths.has(path)) {\n if (warnedTemplatePaths.size < WARNED_TEMPLATE_MAX) {\n warnedTemplatePaths.add(path);\n }\n console.warn(\n `$template path \"${path}\" should be a JSON Pointer starting with \"/\". Automatically resolving as \"/${path}\".`,\n );\n }\n path = \"/\" + path;\n }\n const resolved = getByPath(ctx.stateModel, path);\n return resolved != null ? String(resolved) : \"\";\n },\n );\n }\n\n // Arrays: resolve each element\n if (Array.isArray(value)) {\n return value.map((item) => resolvePropValue(item, ctx));\n }\n\n // Plain objects (not expressions): resolve each value recursively\n if (typeof value === \"object\") {\n const resolved: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(value as Record<string, unknown>)) {\n resolved[key] = resolvePropValue(val, ctx);\n }\n return resolved;\n }\n\n // Primitive literal: passthrough\n return value;\n}\n\n/**\n * Resolve all prop values in an element's props object.\n * Returns a new props object with all expressions resolved.\n */\nexport function resolveElementProps(\n props: Record<string, unknown>,\n ctx: PropResolutionContext,\n): Record<string, unknown> {\n const resolved: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(props)) {\n resolved[key] = resolvePropValue(value, ctx);\n }\n return resolved;\n}\n\n/**\n * Scan an element's raw props for `$bindState` / `$bindItem` expressions\n * and return a map of prop name → resolved absolute state path.\n *\n * This is called **before** `resolveElementProps` so the component can\n * receive both the resolved value (in `props`) and the write-back path\n * (in `bindings`).\n *\n * @example\n * ```ts\n * const rawProps = { value: { $bindState: \"/form/email\" }, label: \"Email\" };\n * const bindings = resolveBindings(rawProps, ctx);\n * // bindings = { value: \"/form/email\" }\n * ```\n */\nexport function resolveBindings(\n props: Record<string, unknown>,\n ctx: PropResolutionContext,\n): Record<string, string> | undefined {\n let bindings: Record<string, string> | undefined;\n for (const [key, value] of Object.entries(props)) {\n if (isBindStateExpression(value)) {\n if (!bindings) bindings = {};\n bindings[key] = value.$bindState;\n } else if (isBindItemExpression(value)) {\n const resolved = resolveBindItemPath(value.$bindItem, ctx);\n if (resolved !== undefined) {\n if (!bindings) bindings = {};\n bindings[key] = resolved;\n }\n }\n }\n return bindings;\n}\n\n/**\n * Resolve a single action parameter value.\n *\n * Like {@link resolvePropValue} but with special handling for path-valued\n * params: `{ $item: \"field\" }` resolves to an **absolute state path**\n * (e.g. `/todos/0/field`) instead of the field's value, so the path can\n * be passed to `setState` / `pushState` / `removeState`.\n *\n * - `{ $item: \"field\" }` → absolute state path via `repeatBasePath`\n * - `{ $index: true }` → current repeat index (number)\n * - Everything else delegates to `resolvePropValue` ($state, $cond, literals).\n */\nexport function resolveActionParam(\n value: unknown,\n ctx: PropResolutionContext,\n): unknown {\n if (isItemExpression(value)) {\n return resolveBindItemPath(value.$item, ctx);\n }\n if (isIndexExpression(value)) {\n return ctx.repeatIndex;\n }\n return resolvePropValue(value, ctx);\n}\n","import { z } from \"zod\";\nimport type { DynamicValue, StateModel } from \"./types\";\nimport { DynamicValueSchema, resolveDynamicValue } from \"./types\";\n\n/**\n * Confirmation dialog configuration\n */\nexport interface ActionConfirm {\n title: string;\n message: string;\n confirmLabel?: string;\n cancelLabel?: string;\n variant?: \"default\" | \"danger\";\n}\n\n/**\n * Action success handler\n */\nexport type ActionOnSuccess =\n | { navigate: string }\n | { set: Record<string, unknown> }\n | { action: string };\n\n/**\n * Action error handler\n */\nexport type ActionOnError =\n | { set: Record<string, unknown> }\n | { action: string };\n\n/**\n * Action binding — maps an event to an action invocation.\n *\n * Used inside the `on` field of a UIElement:\n * ```json\n * { \"on\": { \"press\": { \"action\": \"setState\", \"params\": { \"statePath\": \"/x\", \"value\": 1 } } } }\n * ```\n */\nexport interface ActionBinding {\n /** Action name (must be in catalog) */\n action: string;\n /** Parameters to pass to the action handler */\n params?: Record<string, DynamicValue>;\n /** Confirmation dialog before execution */\n confirm?: ActionConfirm;\n /** Handler after successful execution */\n onSuccess?: ActionOnSuccess;\n /** Handler after failed execution */\n onError?: ActionOnError;\n /** Whether to prevent default browser behavior (e.g. navigation on links) */\n preventDefault?: boolean;\n}\n\n/**\n * @deprecated Use ActionBinding instead\n */\nexport type Action = ActionBinding;\n\n/**\n * Schema for action confirmation\n */\nexport const ActionConfirmSchema = z.object({\n title: z.string(),\n message: z.string(),\n confirmLabel: z.string().optional(),\n cancelLabel: z.string().optional(),\n variant: z.enum([\"default\", \"danger\"]).optional(),\n});\n\n/**\n * Schema for success handlers\n */\nexport const ActionOnSuccessSchema = z.union([\n z.object({ navigate: z.string() }),\n z.object({ set: z.record(z.string(), z.unknown()) }),\n z.object({ action: z.string() }),\n]);\n\n/**\n * Schema for error handlers\n */\nexport const ActionOnErrorSchema = z.union([\n z.object({ set: z.record(z.string(), z.unknown()) }),\n z.object({ action: z.string() }),\n]);\n\n/**\n * Full action binding schema\n */\nexport const ActionBindingSchema = z.object({\n action: z.string(),\n params: z.record(z.string(), DynamicValueSchema).optional(),\n confirm: ActionConfirmSchema.optional(),\n onSuccess: ActionOnSuccessSchema.optional(),\n onError: ActionOnErrorSchema.optional(),\n preventDefault: z.boolean().optional(),\n});\n\n/**\n * @deprecated Use ActionBindingSchema instead\n */\nexport const ActionSchema = ActionBindingSchema;\n\n/**\n * Action handler function signature\n */\nexport type ActionHandler<\n TParams = Record<string, unknown>,\n TResult = unknown,\n> = (params: TParams) => Promise<TResult> | TResult;\n\n/**\n * Action definition in catalog\n */\nexport interface ActionDefinition<TParams = Record<string, unknown>> {\n /** Zod schema for params validation */\n params?: z.ZodType<TParams>;\n /** Description for AI */\n description?: string;\n}\n\n/**\n * Resolved action with all dynamic values resolved\n */\nexport interface ResolvedAction {\n action: string;\n params: Record<string, unknown>;\n confirm?: ActionConfirm;\n onSuccess?: ActionOnSuccess;\n onError?: ActionOnError;\n}\n\n/**\n * Resolve all dynamic values in an action binding\n */\nexport function resolveAction(\n binding: ActionBinding,\n stateModel: StateModel,\n): ResolvedAction {\n const resolvedParams: Record<string, unknown> = {};\n\n if (binding.params) {\n for (const [key, value] of Object.entries(binding.params)) {\n resolvedParams[key] = resolveDynamicValue(value, stateModel);\n }\n }\n\n // Interpolate confirmation message if present\n let confirm = binding.confirm;\n if (confirm) {\n confirm = {\n ...confirm,\n message: interpolateString(confirm.message, stateModel),\n title: interpolateString(confirm.title, stateModel),\n };\n }\n\n return {\n action: binding.action,\n params: resolvedParams,\n confirm,\n onSuccess: binding.onSuccess,\n onError: binding.onError,\n };\n}\n\n/**\n * Interpolate ${path} expressions in a string\n */\nexport function interpolateString(\n template: string,\n stateModel: StateModel,\n): string {\n return template.replace(/\\$\\{([^}]+)\\}/g, (_, path) => {\n const value = resolveDynamicValue({ $state: path }, stateModel);\n return String(value ?? \"\");\n });\n}\n\n/**\n * Context for action execution\n */\nexport interface ActionExecutionContext {\n /** The resolved action */\n action: ResolvedAction;\n /** The action handler from the host */\n handler: ActionHandler;\n /** Function to update state model */\n setState: (path: string, value: unknown) => void;\n /** Function to navigate */\n navigate?: (path: string) => void;\n /** Function to execute another action */\n executeAction?: (name: string) => Promise<void>;\n}\n\n/**\n * Execute an action with all callbacks\n */\nexport async function executeAction(\n ctx: ActionExecutionContext,\n): Promise<void> {\n const { action, handler, setState, navigate, executeAction } = ctx;\n\n try {\n await handler(action.params);\n\n // Handle success\n if (action.onSuccess) {\n if (\"navigate\" in action.onSuccess && navigate) {\n navigate(action.onSuccess.navigate);\n } else if (\"set\" in action.onSuccess) {\n for (const [path, value] of Object.entries(action.onSuccess.set)) {\n setState(path, value);\n }\n } else if (\"action\" in action.onSuccess && executeAction) {\n await executeAction(action.onSuccess.action);\n }\n }\n } catch (error) {\n // Handle error\n if (action.onError) {\n if (\"set\" in action.onError) {\n for (const [path, value] of Object.entries(action.onError.set)) {\n // Replace $error.message with actual error\n const resolvedValue =\n typeof value === \"string\" && value === \"$error.message\"\n ? (error as Error).message\n : value;\n setState(path, resolvedValue);\n }\n } else if (\"action\" in action.onError && executeAction) {\n await executeAction(action.onError.action);\n }\n } else {\n throw error;\n }\n }\n}\n\n/**\n * Helper to create action bindings\n */\nexport const actionBinding = {\n /** Create a simple action binding */\n simple: (\n actionName: string,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n }),\n\n /** Create an action binding with confirmation */\n withConfirm: (\n actionName: string,\n confirm: ActionConfirm,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n confirm,\n }),\n\n /** Create an action binding with success handler */\n withSuccess: (\n actionName: string,\n onSuccess: ActionOnSuccess,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n onSuccess,\n }),\n};\n\n/**\n * @deprecated Use actionBinding instead\n */\nexport const action = actionBinding;\n","import { z } from \"zod\";\nimport type { DynamicValue, StateModel, VisibilityCondition } from \"./types\";\nimport { DynamicValueSchema, resolveDynamicValue } from \"./types\";\nimport { VisibilityConditionSchema, evaluateVisibility } from \"./visibility\";\nimport { resolvePropValue } from \"./props\";\n\n/**\n * Validation check definition\n */\nexport interface ValidationCheck {\n /** Validation type (built-in or from catalog) */\n type: string;\n /** Additional arguments for the validation */\n args?: Record<string, DynamicValue>;\n /** Error message to display if check fails */\n message: string;\n}\n\n/**\n * Validation configuration for a field\n */\nexport interface ValidationConfig {\n /** Array of checks to run */\n checks?: ValidationCheck[];\n /** When to run validation */\n validateOn?: \"change\" | \"blur\" | \"submit\";\n /** Condition for when validation is enabled */\n enabled?: VisibilityCondition;\n}\n\n/**\n * Schema for validation check\n */\nexport const ValidationCheckSchema = z.object({\n type: z.string(),\n args: z.record(z.string(), DynamicValueSchema).optional(),\n message: z.string(),\n});\n\n/**\n * Schema for validation config\n */\nexport const ValidationConfigSchema = z.object({\n checks: z.array(ValidationCheckSchema).optional(),\n validateOn: z.enum([\"change\", \"blur\", \"submit\"]).optional(),\n enabled: VisibilityConditionSchema.optional(),\n});\n\n/**\n * Validation function signature\n */\nexport type ValidationFunction = (\n value: unknown,\n args?: Record<string, unknown>,\n) => boolean;\n\n/**\n * Validation function definition in catalog\n */\nexport interface ValidationFunctionDefinition {\n /** The validation function */\n validate: ValidationFunction;\n /** Description for AI */\n description?: string;\n}\n\nconst matchesImpl: ValidationFunction = (\n value: unknown,\n args?: Record<string, unknown>,\n) => {\n const other = args?.other;\n return value === other;\n};\n\n/**\n * Built-in validation functions\n */\nexport const builtInValidationFunctions: Record<string, ValidationFunction> = {\n /**\n * Check if value is not null, undefined, or empty string\n */\n required: (value: unknown) => {\n if (value === null || value === undefined) return false;\n if (typeof value === \"string\") return value.trim().length > 0;\n if (Array.isArray(value)) return value.length > 0;\n return true;\n },\n\n /**\n * Check if value is a valid email address\n */\n email: (value: unknown) => {\n if (typeof value !== \"string\") return false;\n return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(value);\n },\n\n /**\n * Check minimum string length\n */\n minLength: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const min = args?.min;\n if (typeof min !== \"number\") return false;\n return value.length >= min;\n },\n\n /**\n * Check maximum string length\n */\n maxLength: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const max = args?.max;\n if (typeof max !== \"number\") return false;\n return value.length <= max;\n },\n\n /**\n * Check if string matches a regex pattern\n */\n pattern: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const pattern = args?.pattern;\n if (typeof pattern !== \"string\") return false;\n try {\n return new RegExp(pattern).test(value);\n } catch {\n return false;\n }\n },\n\n /**\n * Check minimum numeric value\n */\n min: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"number\") return false;\n const min = args?.min;\n if (typeof min !== \"number\") return false;\n return value >= min;\n },\n\n /**\n * Check maximum numeric value\n */\n max: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"number\") return false;\n const max = args?.max;\n if (typeof max !== \"number\") return false;\n return value <= max;\n },\n\n /**\n * Check if value is a number\n */\n numeric: (value: unknown) => {\n if (typeof value === \"number\") return !isNaN(value);\n if (typeof value === \"string\") return !isNaN(parseFloat(value));\n return false;\n },\n\n /**\n * Check if value is a valid URL\n */\n url: (value: unknown) => {\n if (typeof value !== \"string\") return false;\n try {\n new URL(value);\n return true;\n } catch {\n return false;\n }\n },\n\n /**\n * Check if value matches another field\n */\n matches: matchesImpl,\n\n /**\n * Alias for matches with a more descriptive name for cross-field equality\n */\n equalTo: matchesImpl,\n\n /**\n * Check if value is less than another field's value.\n * Supports numbers, strings (useful for ISO date comparison), and\n * cross-type numeric coercion (e.g. string \"3\" vs number 5).\n */\n lessThan: (value: unknown, args?: Record<string, unknown>) => {\n const other = args?.other;\n if (value == null || other == null || value === \"\" || other === \"\")\n return false;\n if (typeof value === \"number\" && typeof other === \"number\")\n return value < other;\n if (typeof value === \"string\" && typeof other === \"string\")\n return value < other;\n const numVal = Number(value);\n const numOther = Number(other);\n if (!isNaN(numVal) && !isNaN(numOther)) return numVal < numOther;\n return false;\n },\n\n /**\n * Check if value is greater than another field's value.\n * Supports numbers, strings (useful for ISO date comparison), and\n * cross-type numeric coercion (e.g. string \"7\" vs number 5).\n */\n greaterThan: (value: unknown, args?: Record<string, unknown>) => {\n const other = args?.other;\n if (value == null || other == null || value === \"\" || other === \"\")\n return false;\n if (typeof value === \"number\" && typeof other === \"number\")\n return value > other;\n if (typeof value === \"string\" && typeof other === \"string\")\n return value > other;\n const numVal = Number(value);\n const numOther = Number(other);\n if (!isNaN(numVal) && !isNaN(numOther)) return numVal > numOther;\n return false;\n },\n\n /**\n * Required only when a condition is met.\n * Uses JS truthiness: 0, false, \"\", null, and undefined are all\n * treated as \"condition not met\" (field not required), matching\n * the visibility system's bare-condition semantics.\n */\n requiredIf: (value: unknown, args?: Record<string, unknown>) => {\n const condition = args?.field;\n if (!condition) return true;\n if (value === null || value === undefined) return false;\n if (typeof value === \"string\") return value.trim().length > 0;\n if (Array.isArray(value)) return value.length > 0;\n return true;\n },\n};\n\n/**\n * Validation result for a single check\n */\nexport interface ValidationCheckResult {\n type: string;\n valid: boolean;\n message: string;\n}\n\n/**\n * Full validation result for a field\n */\nexport interface ValidationResult {\n valid: boolean;\n errors: string[];\n checks: ValidationCheckResult[];\n}\n\n/**\n * Context for running validation\n */\nexport interface ValidationContext {\n /** Current value to validate */\n value: unknown;\n /** Full data model for resolving paths */\n stateModel: StateModel;\n /** Custom validation functions from catalog */\n customFunctions?: Record<string, ValidationFunction>;\n}\n\n/**\n * Run a single validation check\n */\nexport function runValidationCheck(\n check: ValidationCheck,\n ctx: ValidationContext,\n): ValidationCheckResult {\n const { value, stateModel, customFunctions } = ctx;\n\n // Resolve args using resolvePropValue so nested $state refs (and any other\n // prop expressions) are handled consistently with the rest of the system.\n const resolvedArgs: Record<string, unknown> = {};\n if (check.args) {\n for (const [key, argValue] of Object.entries(check.args)) {\n resolvedArgs[key] = resolvePropValue(argValue, { stateModel });\n }\n }\n\n // Find the validation function\n const validationFn =\n builtInValidationFunctions[check.type] ?? customFunctions?.[check.type];\n\n if (!validationFn) {\n console.warn(`Unknown validation function: ${check.type}`);\n return {\n type: check.type,\n valid: true, // Don't fail on unknown functions\n message: check.message,\n };\n }\n\n const valid = validationFn(value, resolvedArgs);\n\n return {\n type: check.type,\n valid,\n message: check.message,\n };\n}\n\n/**\n * Run all validation checks for a field\n */\nexport function runValidation(\n config: ValidationConfig,\n ctx: ValidationContext,\n): ValidationResult {\n const checks: ValidationCheckResult[] = [];\n const errors: string[] = [];\n\n // Check if validation is enabled\n if (config.enabled) {\n const enabled = evaluateVisibility(config.enabled, {\n stateModel: ctx.stateModel,\n });\n if (!enabled) {\n return { valid: true, errors: [], checks: [] };\n }\n }\n\n // Run each check\n if (config.checks) {\n for (const check of config.checks) {\n const result = runValidationCheck(check, ctx);\n checks.push(result);\n if (!result.valid) {\n errors.push(result.message);\n }\n }\n }\n\n return {\n valid: errors.length === 0,\n errors,\n checks,\n };\n}\n\n/**\n * Helper to create validation checks\n */\nexport const check = {\n required: (message = \"This field is required\"): ValidationCheck => ({\n type: \"required\",\n message,\n }),\n\n email: (message = \"Invalid email address\"): ValidationCheck => ({\n type: \"email\",\n message,\n }),\n\n minLength: (min: number, message?: string): ValidationCheck => ({\n type: \"minLength\",\n args: { min },\n message: message ?? `Must be at least ${min} characters`,\n }),\n\n maxLength: (max: number, message?: string): ValidationCheck => ({\n type: \"maxLength\",\n args: { max },\n message: message ?? `Must be at most ${max} characters`,\n }),\n\n pattern: (pattern: string, message = \"Invalid format\"): ValidationCheck => ({\n type: \"pattern\",\n args: { pattern },\n message,\n }),\n\n min: (min: number, message?: string): ValidationCheck => ({\n type: \"min\",\n args: { min },\n message: message ?? `Must be at least ${min}`,\n }),\n\n max: (max: number, message?: string): ValidationCheck => ({\n type: \"max\",\n args: { max },\n message: message ?? `Must be at most ${max}`,\n }),\n\n url: (message = \"Invalid URL\"): ValidationCheck => ({\n type: \"url\",\n message,\n }),\n\n numeric: (message = \"Must be a number\"): ValidationCheck => ({\n type: \"numeric\",\n message,\n }),\n\n matches: (\n otherPath: string,\n message = \"Fields must match\",\n ): ValidationCheck => ({\n type: \"matches\",\n args: { other: { $state: otherPath } },\n message,\n }),\n\n equalTo: (\n otherPath: string,\n message = \"Fields must match\",\n ): ValidationCheck => ({\n type: \"equalTo\",\n args: { other: { $state: otherPath } },\n message,\n }),\n\n lessThan: (otherPath: string, message?: string): ValidationCheck => ({\n type: \"lessThan\",\n args: { other: { $state: otherPath } },\n message: message ?? \"Must be less than the compared field\",\n }),\n\n greaterThan: (otherPath: string, message?: string): ValidationCheck => ({\n type: \"greaterThan\",\n args: { other: { $state: otherPath } },\n message: message ?? \"Must be greater than the compared field\",\n }),\n\n requiredIf: (\n fieldPath: string,\n message = \"This field is required\",\n ): ValidationCheck => ({\n type: \"requiredIf\",\n args: { field: { $state: fieldPath } },\n message,\n }),\n};\n","import type { Spec, UIElement } from \"./types\";\n\n// =============================================================================\n// Spec Structural Validation\n// =============================================================================\n\n/**\n * Severity level for validation issues.\n */\nexport type SpecIssueSeverity = \"error\" | \"warning\";\n\n/**\n * A single validation issue found in a spec.\n */\nexport interface SpecIssue {\n /** Severity: errors should be fixed, warnings are informational */\n severity: SpecIssueSeverity;\n /** Human-readable description of the issue */\n message: string;\n /** The element key where the issue was found (if applicable) */\n elementKey?: string;\n /** Machine-readable issue code for programmatic handling */\n code:\n | \"missing_root\"\n | \"root_not_found\"\n | \"missing_child\"\n | \"visible_in_props\"\n | \"orphaned_element\"\n | \"empty_spec\"\n | \"on_in_props\"\n | \"repeat_in_props\"\n | \"watch_in_props\";\n}\n\n/**\n * Result of spec structural validation.\n */\nexport interface SpecValidationIssues {\n /** Whether the spec passed validation (no errors; warnings are OK) */\n valid: boolean;\n /** List of issues found */\n issues: SpecIssue[];\n}\n\n/**\n * Options for validateSpec.\n */\nexport interface ValidateSpecOptions {\n /**\n * Whether to check for orphaned elements (elements not reachable from root).\n * Defaults to false since orphans are harmless (just unused).\n */\n checkOrphans?: boolean;\n}\n\n/**\n * Validate a spec for structural integrity.\n *\n * Checks for common AI-generation errors:\n * - Missing or empty root\n * - Root element not found in elements map\n * - Children referencing non-existent elements\n * - `visible` placed inside `props` instead of on the element\n * - Orphaned elements (optional)\n *\n * @example\n * ```ts\n * const result = validateSpec(spec);\n * if (!result.valid) {\n * console.log(\"Spec errors:\", result.issues);\n * }\n * ```\n */\nexport function validateSpec(\n spec: Spec,\n options: ValidateSpecOptions = {},\n): SpecValidationIssues {\n const { checkOrphans = false } = options;\n const issues: SpecIssue[] = [];\n\n // 1. Check root\n if (!spec.root) {\n issues.push({\n severity: \"error\",\n message: \"Spec has no root element defined.\",\n code: \"missing_root\",\n });\n return { valid: false, issues };\n }\n\n if (!spec.elements[spec.root]) {\n issues.push({\n severity: \"error\",\n message: `Root element \"${spec.root}\" not found in elements map.`,\n code: \"root_not_found\",\n });\n }\n\n // 2. Check for empty spec\n if (Object.keys(spec.elements).length === 0) {\n issues.push({\n severity: \"error\",\n message: \"Spec has no elements.\",\n code: \"empty_spec\",\n });\n return { valid: false, issues };\n }\n\n // 3. Check each element\n for (const [key, element] of Object.entries(spec.elements)) {\n // 3a. Missing children\n if (element.children) {\n for (const childKey of element.children) {\n if (!spec.elements[childKey]) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" references child \"${childKey}\" which does not exist in the elements map.`,\n elementKey: key,\n code: \"missing_child\",\n });\n }\n }\n }\n\n // 3b. `visible` inside props\n const props = element.props as Record<string, unknown> | undefined;\n if (props && \"visible\" in props && props.visible !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"visible\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"visible_in_props\",\n });\n }\n\n // 3c. `on` inside props (should be a top-level field)\n if (props && \"on\" in props && props.on !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"on\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"on_in_props\",\n });\n }\n\n // 3d. `repeat` inside props (should be a top-level field)\n if (props && \"repeat\" in props && props.repeat !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"repeat\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"repeat_in_props\",\n });\n }\n\n // 3e. `watch` inside props (should be a top-level field)\n if (props && \"watch\" in props && props.watch !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"watch\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"watch_in_props\",\n });\n }\n }\n\n // 4. Orphaned elements (optional)\n if (checkOrphans) {\n const reachable = new Set<string>();\n const walk = (key: string) => {\n if (reachable.has(key)) return;\n reachable.add(key);\n const el = spec.elements[key];\n if (el?.children) {\n for (const childKey of el.children) {\n if (spec.elements[childKey]) {\n walk(childKey);\n }\n }\n }\n };\n if (spec.elements[spec.root]) {\n walk(spec.root);\n }\n\n for (const key of Object.keys(spec.elements)) {\n if (!reachable.has(key)) {\n issues.push({\n severity: \"warning\",\n message: `Element \"${key}\" is not reachable from root \"${spec.root}\".`,\n elementKey: key,\n code: \"orphaned_element\",\n });\n }\n }\n }\n\n const hasErrors = issues.some((i) => i.severity === \"error\");\n return { valid: !hasErrors, issues };\n}\n\n/**\n * Auto-fix common spec issues in-place and return a corrected copy.\n *\n * Currently fixes:\n * - `visible` inside `props` → moved to element level\n * - `on` inside `props` → moved to element level\n * - `repeat` inside `props` → moved to element level\n *\n * Returns the fixed spec and a list of fixes applied.\n */\nexport function autoFixSpec(spec: Spec): {\n spec: Spec;\n fixes: string[];\n} {\n const fixes: string[] = [];\n const fixedElements: Record<string, UIElement> = {};\n\n for (const [key, element] of Object.entries(spec.elements)) {\n const props = element.props as Record<string, unknown> | undefined;\n let fixed = element;\n\n if (props && \"visible\" in props && props.visible !== undefined) {\n // Move visible from props to element level\n const { visible, ...restProps } = fixed.props as Record<string, unknown>;\n fixed = {\n ...fixed,\n props: restProps,\n visible: visible as UIElement[\"visible\"],\n };\n fixes.push(`Moved \"visible\" from props to element level on \"${key}\".`);\n }\n\n let currentProps = fixed.props as Record<string, unknown> | undefined;\n if (currentProps && \"on\" in currentProps && currentProps.on !== undefined) {\n // Move on from props to element level\n const { on, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n on: on as UIElement[\"on\"],\n };\n fixes.push(`Moved \"on\" from props to element level on \"${key}\".`);\n }\n\n currentProps = fixed.props as Record<string, unknown> | undefined;\n if (\n currentProps &&\n \"repeat\" in currentProps &&\n currentProps.repeat !== undefined\n ) {\n // Move repeat from props to element level\n const { repeat, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n repeat: repeat as UIElement[\"repeat\"],\n };\n fixes.push(`Moved \"repeat\" from props to element level on \"${key}\".`);\n }\n\n currentProps = fixed.props as Record<string, unknown> | undefined;\n if (\n currentProps &&\n \"watch\" in currentProps &&\n currentProps.watch !== undefined\n ) {\n const { watch, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n watch: watch as UIElement[\"watch\"],\n };\n fixes.push(`Moved \"watch\" from props to element level on \"${key}\".`);\n }\n\n fixedElements[key] = fixed;\n }\n\n return {\n spec: { root: spec.root, elements: fixedElements, state: spec.state },\n fixes,\n };\n}\n\n/**\n * Format validation issues into a human-readable string suitable for\n * inclusion in a repair prompt sent back to the AI.\n */\nexport function formatSpecIssues(issues: SpecIssue[]): string {\n const errors = issues.filter((i) => i.severity === \"error\");\n if (errors.length === 0) return \"\";\n\n const lines = [\"The generated UI spec has the following errors:\"];\n for (const issue of errors) {\n lines.push(`- ${issue.message}`);\n }\n return lines.join(\"\\n\");\n}\n","import { z } from \"zod\";\n\n/**\n * Schema builder primitives\n */\nexport interface SchemaBuilder {\n /** String type */\n string(): SchemaType<\"string\">;\n /** Number type */\n number(): SchemaType<\"number\">;\n /** Boolean type */\n boolean(): SchemaType<\"boolean\">;\n /** Array of type */\n array<T extends SchemaType>(item: T): SchemaType<\"array\", T>;\n /** Object with shape */\n object<T extends Record<string, SchemaType>>(\n shape: T,\n ): SchemaType<\"object\", T>;\n /** Record/map with value type */\n record<T extends SchemaType>(value: T): SchemaType<\"record\", T>;\n /** Any type */\n any(): SchemaType<\"any\">;\n /** Placeholder for user-provided Zod schema */\n zod(): SchemaType<\"zod\">;\n /** Reference to catalog key (e.g., 'catalog.components') */\n ref(path: string): SchemaType<\"ref\", string>;\n /** Props from referenced catalog entry */\n propsOf(path: string): SchemaType<\"propsOf\", string>;\n /** Map of named entries with shared shape */\n map<T extends Record<string, SchemaType>>(\n entryShape: T,\n ): SchemaType<\"map\", T>;\n /** Optional modifier */\n optional(): { optional: true };\n}\n\n/**\n * Schema type representation\n */\nexport interface SchemaType<TKind extends string = string, TInner = unknown> {\n kind: TKind;\n inner?: TInner;\n optional?: boolean;\n}\n\n/**\n * Schema definition shape\n */\nexport interface SchemaDefinition<\n TSpec extends SchemaType = SchemaType,\n TCatalog extends SchemaType = SchemaType,\n> {\n /** What the AI-generated spec looks like */\n spec: TSpec;\n /** What the catalog must provide */\n catalog: TCatalog;\n}\n\n/**\n * Schema instance with methods\n */\nexport interface Schema<TDef extends SchemaDefinition = SchemaDefinition> {\n /** The schema definition */\n readonly definition: TDef;\n /** Custom prompt template for this schema */\n readonly promptTemplate?: PromptTemplate;\n /** Default rules baked into the schema (injected before customRules) */\n readonly defaultRules?: string[];\n /** Built-in actions always available at runtime (injected into prompts automatically) */\n readonly builtInActions?: BuiltInAction[];\n /** Create a catalog from this schema */\n createCatalog<TCatalog extends InferCatalogInput<TDef[\"catalog\"]>>(\n catalog: TCatalog,\n ): Catalog<TDef, TCatalog>;\n}\n\n/**\n * Catalog instance with methods\n */\nexport interface Catalog<\n TDef extends SchemaDefinition = SchemaDefinition,\n TCatalog = unknown,\n> {\n /** The schema this catalog is based on */\n readonly schema: Schema<TDef>;\n /** The catalog data */\n readonly data: TCatalog;\n /** Component names */\n readonly componentNames: string[];\n /** Action names */\n readonly actionNames: string[];\n /** Generate system prompt for AI */\n prompt(options?: PromptOptions): string;\n /** Export as JSON Schema for structured outputs */\n jsonSchema(): object;\n /** Validate a spec against this catalog */\n validate(spec: unknown): SpecValidationResult<InferSpec<TDef, TCatalog>>;\n /** Get the Zod schema for the spec */\n zodSchema(): z.ZodType<InferSpec<TDef, TCatalog>>;\n /** Type helper for the spec type */\n readonly _specType: InferSpec<TDef, TCatalog>;\n}\n\n/**\n * Prompt generation options\n */\nexport interface PromptOptions {\n /** Custom system message intro */\n system?: string;\n /** Additional rules to append */\n customRules?: string[];\n /**\n * Output mode for the generated prompt.\n *\n * - `\"generate\"` (default): The LLM should output only JSONL patches (no prose).\n * - `\"chat\"`: The LLM should respond conversationally first, then output JSONL patches.\n * Includes rules about interleaving text with JSONL and not wrapping in code fences.\n */\n mode?: \"generate\" | \"chat\";\n}\n\n/**\n * Context provided to prompt templates\n */\nexport interface PromptContext<TCatalog = unknown> {\n /** The catalog data */\n catalog: TCatalog;\n /** Component names from the catalog */\n componentNames: string[];\n /** Action names from the catalog (if any) */\n actionNames: string[];\n /** Prompt options provided by the user */\n options: PromptOptions;\n /** Helper to format a Zod type as a human-readable string */\n formatZodType: (schema: z.ZodType) => string;\n}\n\n/**\n * Prompt template function type\n */\nexport type PromptTemplate<TCatalog = unknown> = (\n context: PromptContext<TCatalog>,\n) => string;\n\n/**\n * A built-in action that is always available regardless of catalog configuration.\n * These are handled by the runtime (e.g. ActionProvider) and injected into prompts\n * automatically so the LLM knows about them.\n */\nexport interface BuiltInAction {\n /** Action name (e.g. \"setState\") */\n name: string;\n /** Human-readable description for the LLM */\n description: string;\n}\n\n/**\n * Schema options\n */\nexport interface SchemaOptions<TCatalog = unknown> {\n /** Custom prompt template for this schema */\n promptTemplate?: PromptTemplate<TCatalog>;\n /** Default rules baked into the schema (injected before customRules in prompts) */\n defaultRules?: string[];\n /**\n * Built-in actions that are always available regardless of catalog configuration.\n * These are injected into prompts automatically so the LLM knows about them,\n * but they don't require handlers in defineRegistry because the runtime\n * (e.g. ActionProvider) handles them directly.\n */\n builtInActions?: BuiltInAction[];\n}\n\n/**\n * Spec validation result\n */\nexport interface SpecValidationResult<T> {\n success: boolean;\n data?: T;\n error?: z.ZodError;\n}\n\n// =============================================================================\n// Catalog Type Inference Helpers\n// =============================================================================\n\n/**\n * Extract the components map type from a catalog\n * @example type Components = InferCatalogComponents<typeof myCatalog>;\n */\nexport type InferCatalogComponents<C extends Catalog> =\n C extends Catalog<SchemaDefinition, infer TCatalog>\n ? TCatalog extends { components: infer Comps }\n ? Comps\n : never\n : never;\n\n/**\n * Extract the actions map type from a catalog\n * @example type Actions = InferCatalogActions<typeof myCatalog>;\n */\nexport type InferCatalogActions<C extends Catalog> =\n C extends Catalog<SchemaDefinition, infer TCatalog>\n ? TCatalog extends { actions: infer Acts }\n ? Acts\n : never\n : never;\n\n/**\n * Infer component props from a catalog by component name\n * @example type ButtonProps = InferComponentProps<typeof myCatalog, 'Button'>;\n */\nexport type InferComponentProps<\n C extends Catalog,\n K extends keyof InferCatalogComponents<C>,\n> = InferCatalogComponents<C>[K] extends { props: z.ZodType<infer P> }\n ? P\n : never;\n\n/**\n * Infer action params from a catalog by action name\n * @example type ViewCustomersParams = InferActionParams<typeof myCatalog, 'viewCustomers'>;\n */\nexport type InferActionParams<\n C extends Catalog,\n K extends keyof InferCatalogActions<C>,\n> = InferCatalogActions<C>[K] extends { params: z.ZodType<infer P> }\n ? P\n : never;\n\n// =============================================================================\n// Internal Type Inference Helpers\n// =============================================================================\n\nexport type InferCatalogInput<T> =\n T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferCatalogField<Shape[K]> }\n : never;\n\ntype InferCatalogField<T> =\n T extends SchemaType<\"map\", infer EntryShape>\n ? Record<\n string,\n // Only 'props' is required, rest are optional\n InferMapEntryRequired<EntryShape> &\n Partial<InferMapEntryOptional<EntryShape>>\n >\n : T extends SchemaType<\"zod\">\n ? z.ZodType\n : T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferCatalogField<Item>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferCatalogField<Shape[K]> }\n : unknown;\n\n// Extract required fields (props is always required)\ntype InferMapEntryRequired<T> = {\n [K in keyof T as K extends \"props\" ? K : never]: InferMapEntryField<T[K]>;\n};\n\n// Extract optional fields (everything except props)\ntype InferMapEntryOptional<T> = {\n [K in keyof T as K extends \"props\" ? never : K]: InferMapEntryField<T[K]>;\n};\n\ntype InferMapEntryField<T> =\n T extends SchemaType<\"zod\">\n ? z.ZodType\n : T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferMapEntryField<Item>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferMapEntryField<Shape[K]> }\n : unknown;\n\n// Spec inference (simplified - will be expanded)\nexport type InferSpec<TDef extends SchemaDefinition, TCatalog> = TDef extends {\n spec: SchemaType<\"object\", infer Shape>;\n}\n ? InferSpecObject<Shape, TCatalog>\n : unknown;\n\ntype InferSpecObject<Shape, TCatalog> = {\n [K in keyof Shape]: InferSpecField<Shape[K], TCatalog>;\n};\n\ntype InferSpecField<T, TCatalog> =\n T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferSpecField<Item, TCatalog>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? InferSpecObject<Shape, TCatalog>\n : T extends SchemaType<\"record\", infer Value>\n ? Record<string, InferSpecField<Value, TCatalog>>\n : T extends SchemaType<\"ref\", infer Path>\n ? InferRefType<Path, TCatalog>\n : T extends SchemaType<\"propsOf\", infer Path>\n ? InferPropsOfType<Path, TCatalog>\n : T extends SchemaType<\"any\">\n ? unknown\n : unknown;\n\ntype InferRefType<Path, TCatalog> = Path extends \"catalog.components\"\n ? TCatalog extends { components: infer C }\n ? keyof C\n : string\n : Path extends \"catalog.actions\"\n ? TCatalog extends { actions: infer A }\n ? keyof A\n : string\n : string;\n\ntype InferPropsOfType<Path, TCatalog> = Path extends \"catalog.components\"\n ? TCatalog extends { components: infer C }\n ? C extends Record<string, { props: z.ZodType<infer P> }>\n ? P\n : Record<string, unknown>\n : Record<string, unknown>\n : Record<string, unknown>;\n\n/**\n * Create the schema builder\n */\nfunction createBuilder(): SchemaBuilder {\n return {\n string: () => ({ kind: \"string\" }),\n number: () => ({ kind: \"number\" }),\n boolean: () => ({ kind: \"boolean\" }),\n array: (item) => ({ kind: \"array\", inner: item }),\n object: (shape) => ({ kind: \"object\", inner: shape }),\n record: (value) => ({ kind: \"record\", inner: value }),\n any: () => ({ kind: \"any\" }),\n zod: () => ({ kind: \"zod\" }),\n ref: (path) => ({ kind: \"ref\", inner: path }),\n propsOf: (path) => ({ kind: \"propsOf\", inner: path }),\n map: (entryShape) => ({ kind: \"map\", inner: entryShape }),\n optional: () => ({ optional: true }),\n };\n}\n\n/**\n * Define a schema using the builder pattern\n */\nexport function defineSchema<TDef extends SchemaDefinition>(\n builder: (s: SchemaBuilder) => TDef,\n options?: SchemaOptions,\n): Schema<TDef> {\n const s = createBuilder();\n const definition = builder(s);\n\n return {\n definition,\n promptTemplate: options?.promptTemplate,\n defaultRules: options?.defaultRules,\n builtInActions: options?.builtInActions,\n createCatalog<TCatalog extends InferCatalogInput<TDef[\"catalog\"]>>(\n catalog: TCatalog,\n ): Catalog<TDef, TCatalog> {\n return createCatalogFromSchema(this as Schema<TDef>, catalog);\n },\n };\n}\n\n/**\n * Create a catalog from a schema (internal)\n */\nfunction createCatalogFromSchema<TDef extends SchemaDefinition, TCatalog>(\n schema: Schema<TDef>,\n catalogData: TCatalog,\n): Catalog<TDef, TCatalog> {\n // Extract component and action names\n const components = (catalogData as Record<string, unknown>).components as\n | Record<string, unknown>\n | undefined;\n const actions = (catalogData as Record<string, unknown>).actions as\n | Record<string, unknown>\n | undefined;\n\n const componentNames = components ? Object.keys(components) : [];\n const actionNames = actions ? Object.keys(actions) : [];\n\n // Build the Zod schema for validation\n const zodSchema = buildZodSchemaFromDefinition(\n schema.definition,\n catalogData,\n );\n\n return {\n schema,\n data: catalogData,\n componentNames,\n actionNames,\n\n prompt(options: PromptOptions = {}): string {\n return generatePrompt(this, options);\n },\n\n jsonSchema(): object {\n return zodToJsonSchema(zodSchema);\n },\n\n validate(spec: unknown): SpecValidationResult<InferSpec<TDef, TCatalog>> {\n const result = zodSchema.safeParse(spec);\n if (result.success) {\n return {\n success: true,\n data: result.data as InferSpec<TDef, TCatalog>,\n };\n }\n return { success: false, error: result.error };\n },\n\n zodSchema(): z.ZodType<InferSpec<TDef, TCatalog>> {\n return zodSchema as z.ZodType<InferSpec<TDef, TCatalog>>;\n },\n\n get _specType(): InferSpec<TDef, TCatalog> {\n throw new Error(\"_specType is only for type inference\");\n },\n };\n}\n\n/**\n * Build Zod schema from schema definition\n */\nfunction buildZodSchemaFromDefinition(\n definition: SchemaDefinition,\n catalogData: unknown,\n): z.ZodType {\n return buildZodType(definition.spec, catalogData);\n}\n\nfunction buildZodType(schemaType: SchemaType, catalogData: unknown): z.ZodType {\n switch (schemaType.kind) {\n case \"string\":\n return z.string();\n case \"number\":\n return z.number();\n case \"boolean\":\n return z.boolean();\n case \"any\":\n return z.any();\n case \"array\": {\n const inner = buildZodType(schemaType.inner as SchemaType, catalogData);\n return z.array(inner);\n }\n case \"object\": {\n const shape = schemaType.inner as Record<string, SchemaType>;\n const zodShape: Record<string, z.ZodType> = {};\n for (const [key, value] of Object.entries(shape)) {\n let zodType = buildZodType(value, catalogData);\n if (value.optional) {\n zodType = zodType.optional();\n }\n zodShape[key] = zodType;\n }\n return z.object(zodShape);\n }\n case \"record\": {\n const inner = buildZodType(schemaType.inner as SchemaType, catalogData);\n return z.record(z.string(), inner);\n }\n case \"ref\": {\n // Reference to catalog key - create enum of valid keys\n const path = schemaType.inner as string;\n const keys = getKeysFromPath(path, catalogData);\n if (keys.length === 0) {\n return z.string();\n }\n if (keys.length === 1) {\n return z.literal(keys[0]!);\n }\n return z.enum(keys as [string, ...string[]]);\n }\n case \"propsOf\": {\n // Props from catalog entry - create union of all props schemas\n const path = schemaType.inner as string;\n const propsSchemas = getPropsFromPath(path, catalogData);\n if (propsSchemas.length === 0) {\n return z.record(z.string(), z.unknown());\n }\n if (propsSchemas.length === 1) {\n return propsSchemas[0]!;\n }\n // For propsOf, we need to be lenient since type determines which props apply\n return z.record(z.string(), z.unknown());\n }\n default:\n return z.unknown();\n }\n}\n\nfunction getKeysFromPath(path: string, catalogData: unknown): string[] {\n const parts = path.split(\".\");\n let current: unknown = { catalog: catalogData };\n for (const part of parts) {\n if (current && typeof current === \"object\") {\n current = (current as Record<string, unknown>)[part];\n } else {\n return [];\n }\n }\n if (current && typeof current === \"object\") {\n return Object.keys(current);\n }\n return [];\n}\n\nfunction getPropsFromPath(path: string, catalogData: unknown): z.ZodType[] {\n const parts = path.split(\".\");\n let current: unknown = { catalog: catalogData };\n for (const part of parts) {\n if (current && typeof current === \"object\") {\n current = (current as Record<string, unknown>)[part];\n } else {\n return [];\n }\n }\n if (current && typeof current === \"object\") {\n return Object.values(current as Record<string, { props?: z.ZodType }>)\n .map((entry) => entry.props)\n .filter((props): props is z.ZodType => props !== undefined);\n }\n return [];\n}\n\n/**\n * Generate system prompt from catalog\n */\nfunction generatePrompt<TDef extends SchemaDefinition, TCatalog>(\n catalog: Catalog<TDef, TCatalog>,\n options: PromptOptions,\n): string {\n // Check if schema has a custom prompt template\n if (catalog.schema.promptTemplate) {\n const context: PromptContext<TCatalog> = {\n catalog: catalog.data,\n componentNames: catalog.componentNames,\n actionNames: catalog.actionNames,\n options,\n formatZodType,\n };\n return catalog.schema.promptTemplate(context);\n }\n\n // Default JSONL element-tree format (for @json-render/react and similar)\n const {\n system = \"You are a UI generator that outputs JSON.\",\n customRules = [],\n mode = \"generate\",\n } = options;\n\n const lines: string[] = [];\n lines.push(system);\n lines.push(\"\");\n\n // Output format section - explain JSONL streaming patch format\n if (mode === \"chat\") {\n lines.push(\"OUTPUT FORMAT (text + JSONL, RFC 6902 JSON Patch):\");\n lines.push(\n \"You respond conversationally. When generating UI, first write a brief explanation (1-3 sentences), then output JSONL patch lines wrapped in a ```spec code fence.\",\n );\n lines.push(\n \"The JSONL lines use RFC 6902 JSON Patch operations to build a UI tree. Always wrap them in a ```spec fence block:\",\n );\n lines.push(\" ```spec\");\n lines.push(' {\"op\":\"add\",\"path\":\"/root\",\"value\":\"main\"}');\n lines.push(\n ' {\"op\":\"add\",\"path\":\"/elements/main\",\"value\":{\"type\":\"Card\",\"props\":{\"title\":\"Hello\"},\"children\":[]}}',\n );\n lines.push(\" ```\");\n lines.push(\n \"If the user's message does not require a UI (e.g. a greeting or clarifying question), respond with text only — no JSONL.\",\n );\n } else {\n lines.push(\"OUTPUT FORMAT (JSONL, RFC 6902 JSON Patch):\");\n lines.push(\n \"Output JSONL (one JSON object per line) using RFC 6902 JSON Patch operations to build a UI tree.\",\n );\n }\n lines.push(\n \"Each line is a JSON patch operation (add, remove, replace). Start with /root, then stream /elements and /state patches interleaved so the UI fills in progressively as it streams.\",\n );\n lines.push(\"\");\n lines.push(\"Example output (each line is a separate JSON object):\");\n lines.push(\"\");\n\n // Build example using actual catalog component names and props to avoid hallucinations\n const allComponents = (catalog.data as Record<string, unknown>).components as\n | Record<string, CatalogComponentDef>\n | undefined;\n const cn = catalog.componentNames;\n const comp1 = cn[0] || \"Component\";\n const comp2 = cn.length > 1 ? cn[1]! : comp1;\n const comp1Def = allComponents?.[comp1];\n const comp2Def = allComponents?.[comp2];\n const comp1Props = comp1Def ? getExampleProps(comp1Def) : {};\n const comp2Props = comp2Def ? getExampleProps(comp2Def) : {};\n\n // Find a string prop on comp2 to demonstrate $state dynamic bindings\n const dynamicPropName = comp2Def?.props\n ? findFirstStringProp(comp2Def.props)\n : null;\n const dynamicProps = dynamicPropName\n ? { ...comp2Props, [dynamicPropName]: { $item: \"title\" } }\n : comp2Props;\n\n const exampleOutput = [\n JSON.stringify({ op: \"add\", path: \"/root\", value: \"main\" }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/main\",\n value: {\n type: comp1,\n props: comp1Props,\n children: [\"child-1\", \"list\"],\n },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/child-1\",\n value: { type: comp2, props: comp2Props, children: [] },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/list\",\n value: {\n type: comp1,\n props: comp1Props,\n repeat: { statePath: \"/items\", key: \"id\" },\n children: [\"item\"],\n },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/item\",\n value: { type: comp2, props: dynamicProps, children: [] },\n }),\n JSON.stringify({ op: \"add\", path: \"/state/items\", value: [] }),\n JSON.stringify({\n op: \"add\",\n path: \"/state/items/0\",\n value: { id: \"1\", title: \"First Item\" },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/state/items/1\",\n value: { id: \"2\", title: \"Second Item\" },\n }),\n ].join(\"\\n\");\n\n lines.push(`${exampleOutput}\n\nNote: state patches appear right after the elements that use them, so the UI fills in as it streams. ONLY use component types from the AVAILABLE COMPONENTS list below.`);\n lines.push(\"\");\n\n // Initial state section\n lines.push(\"INITIAL STATE:\");\n lines.push(\n \"Specs include a /state field to seed the state model. Components with { $bindState } or { $bindItem } read from and write to this state, and $state expressions read from it.\",\n );\n lines.push(\n \"CRITICAL: You MUST include state patches whenever your UI displays data via $state, $bindState, $bindItem, $item, or $index expressions, or uses repeat to iterate over arrays. Without state, these references resolve to nothing and repeat lists render zero items.\",\n );\n lines.push(\n \"Output state patches right after the elements that reference them, so the UI fills in progressively as it streams.\",\n );\n lines.push(\n \"Stream state progressively - output one patch per array item instead of one giant blob:\",\n );\n lines.push(\n ' For arrays: {\"op\":\"add\",\"path\":\"/state/posts/0\",\"value\":{\"id\":\"1\",\"title\":\"First Post\",...}} then /state/posts/1, /state/posts/2, etc.',\n );\n lines.push(\n ' For scalars: {\"op\":\"add\",\"path\":\"/state/newTodoText\",\"value\":\"\"}',\n );\n lines.push(\n ' Initialize the array first if needed: {\"op\":\"add\",\"path\":\"/state/posts\",\"value\":[]}',\n );\n lines.push(\n 'When content comes from the state model, use { \"$state\": \"/some/path\" } dynamic props to display it instead of hardcoding the same value in both state and props. The state model is the single source of truth.',\n );\n lines.push(\n \"Include realistic sample data in state. For blogs: 3-4 posts with titles, excerpts, authors, dates. For product lists: 3-5 items with names, prices, descriptions. Never leave arrays empty.\",\n );\n lines.push(\"\");\n lines.push(\"DYNAMIC LISTS (repeat field):\");\n lines.push(\n 'Any element can have a top-level \"repeat\" field to render its children once per item in a state array: { \"repeat\": { \"statePath\": \"/arrayPath\", \"key\": \"id\" } }.',\n );\n lines.push(\n 'The element itself renders once (as the container), and its children are expanded once per array item. \"statePath\" is the state array path. \"key\" is an optional field name on each item for stable React keys.',\n );\n lines.push(\n `Example: ${JSON.stringify({ type: comp1, props: comp1Props, repeat: { statePath: \"/todos\", key: \"id\" }, children: [\"todo-item\"] })}`,\n );\n lines.push(\n 'Inside children of a repeated element, use { \"$item\": \"field\" } to read a field from the current item, and { \"$index\": true } to get the current array index. For two-way binding to an item field use { \"$bindItem\": \"completed\" } on the appropriate prop.',\n );\n lines.push(\n \"ALWAYS use the repeat field for lists backed by state arrays. NEVER hardcode individual elements for each array item.\",\n );\n lines.push(\n 'IMPORTANT: \"repeat\" is a top-level field on the element (sibling of type/props/children), NOT inside props.',\n );\n lines.push(\"\");\n lines.push(\"ARRAY STATE ACTIONS:\");\n lines.push(\n 'Use action \"pushState\" to append items to arrays. Params: { statePath: \"/arrayPath\", value: { ...item }, clearStatePath: \"/inputPath\" }.',\n );\n lines.push(\n 'Values inside pushState can contain { \"$state\": \"/statePath\" } references to read current state (e.g. the text from an input field).',\n );\n lines.push(\n 'Use \"$id\" inside a pushState value to auto-generate a unique ID.',\n );\n lines.push(\n 'Example: on: { \"press\": { \"action\": \"pushState\", \"params\": { \"statePath\": \"/todos\", \"value\": { \"id\": \"$id\", \"title\": { \"$state\": \"/newTodoText\" }, \"completed\": false }, \"clearStatePath\": \"/newTodoText\" } } }',\n );\n lines.push(\n 'Use action \"removeState\" to remove items from arrays by index. Params: { statePath: \"/arrayPath\", index: N }. Inside a repeated element\\'s children, use { \"$index\": true } for the current item index. Action params support the same expressions as props: { \"$item\": \"field\" } resolves to the absolute state path, { \"$index\": true } resolves to the index number, and { \"$state\": \"/path\" } reads a value from state.',\n );\n lines.push(\n \"For lists where users can add/remove items (todos, carts, etc.), use pushState and removeState instead of hardcoding with setState.\",\n );\n lines.push(\"\");\n lines.push(\n 'IMPORTANT: State paths use RFC 6901 JSON Pointer syntax (e.g. \"/todos/0/title\"). Do NOT use JavaScript-style dot notation (e.g. \"/todos.length\" is WRONG). To generate unique IDs for new items, use \"$id\" instead of trying to read array length.',\n );\n lines.push(\"\");\n\n // Components section — reuse the typed reference from example generation\n const components = allComponents;\n\n if (components) {\n lines.push(`AVAILABLE COMPONENTS (${catalog.componentNames.length}):`);\n lines.push(\"\");\n\n for (const [name, def] of Object.entries(components)) {\n const propsStr = def.props ? formatZodType(def.props) : \"{}\";\n const hasChildren = def.slots && def.slots.length > 0;\n const childrenStr = hasChildren ? \" [accepts children]\" : \"\";\n const eventsStr =\n def.events && def.events.length > 0\n ? ` [events: ${def.events.join(\", \")}]`\n : \"\";\n const descStr = def.description ? ` - ${def.description}` : \"\";\n lines.push(`- ${name}: ${propsStr}${descStr}${childrenStr}${eventsStr}`);\n }\n lines.push(\"\");\n }\n\n // Actions section\n const actions = (catalog.data as Record<string, unknown>).actions as\n | Record<string, { params?: z.ZodType; description?: string }>\n | undefined;\n\n const builtInActions = catalog.schema.builtInActions ?? [];\n const hasCustomActions = actions && catalog.actionNames.length > 0;\n const hasBuiltInActions = builtInActions.length > 0;\n\n if (hasCustomActions || hasBuiltInActions) {\n lines.push(\"AVAILABLE ACTIONS:\");\n lines.push(\"\");\n\n // Built-in actions (handled by runtime, always available)\n for (const action of builtInActions) {\n lines.push(`- ${action.name}: ${action.description} [built-in]`);\n }\n\n // Custom actions (declared in catalog, require handlers)\n if (hasCustomActions) {\n for (const [name, def] of Object.entries(actions)) {\n lines.push(`- ${name}${def.description ? `: ${def.description}` : \"\"}`);\n }\n }\n\n lines.push(\"\");\n }\n\n // Events section\n lines.push(\"EVENTS (the `on` field):\");\n lines.push(\n \"Elements can have an optional `on` field to bind events to actions. The `on` field is a top-level field on the element (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n 'Each key in `on` is an event name (from the component\\'s supported events), and the value is an action binding: `{ \"action\": \"<actionName>\", \"params\": { ... } }`.',\n );\n lines.push(\"\");\n lines.push(\"Example:\");\n lines.push(\n ` ${JSON.stringify({ type: comp1, props: comp1Props, on: { press: { action: \"setState\", params: { statePath: \"/saved\", value: true } } }, children: [] })}`,\n );\n lines.push(\"\");\n lines.push(\n 'Action params can use dynamic references to read from state: { \"$state\": \"/statePath\" }.',\n );\n lines.push(\n \"IMPORTANT: Do NOT put action/actionParams inside props. Always use the `on` field for event bindings.\",\n );\n lines.push(\"\");\n\n // Visibility conditions\n lines.push(\"VISIBILITY CONDITIONS:\");\n lines.push(\n \"Elements can have an optional `visible` field to conditionally show/hide based on state. IMPORTANT: `visible` is a top-level field on the element object (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n `Correct: ${JSON.stringify({ type: comp1, props: comp1Props, visible: { $state: \"/activeTab\", eq: \"home\" }, children: [\"...\"] })}`,\n );\n lines.push(\n '- `{ \"$state\": \"/path\" }` - visible when state at path is truthy',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"not\": true }` - visible when state at path is falsy',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"eq\": \"value\" }` - visible when state equals value',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"neq\": \"value\" }` - visible when state does not equal value',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"gt\": N }` / `gte` / `lt` / `lte` - numeric comparisons',\n );\n lines.push(\n \"- Use ONE operator per condition (eq, neq, gt, gte, lt, lte). Do not combine multiple operators.\",\n );\n lines.push('- Any condition can add `\"not\": true` to invert its result');\n lines.push(\n \"- `[condition, condition]` - all conditions must be true (implicit AND)\",\n );\n lines.push(\n '- `{ \"$and\": [condition, condition] }` - explicit AND (use when nesting inside $or)',\n );\n lines.push(\n '- `{ \"$or\": [condition, condition] }` - at least one must be true (OR)',\n );\n lines.push(\"- `true` / `false` - always visible/hidden\");\n lines.push(\"\");\n lines.push(\n \"Use a component with on.press bound to setState to update state and drive visibility.\",\n );\n lines.push(\n `Example: A ${comp1} with on: { \"press\": { \"action\": \"setState\", \"params\": { \"statePath\": \"/activeTab\", \"value\": \"home\" } } } sets state, then a container with visible: { \"$state\": \"/activeTab\", \"eq\": \"home\" } shows only when that tab is active.`,\n );\n lines.push(\"\");\n lines.push(\n 'For tab patterns where the first/default tab should be visible when no tab is selected yet, use $or to handle both cases: visible: { \"$or\": [{ \"$state\": \"/activeTab\", \"eq\": \"home\" }, { \"$state\": \"/activeTab\", \"not\": true }] }. This ensures the first tab is visible both when explicitly selected AND when /activeTab is not yet set.',\n );\n lines.push(\"\");\n\n // Dynamic prop expressions\n lines.push(\"DYNAMIC PROPS:\");\n lines.push(\n \"Any prop value can be a dynamic expression that resolves based on state. Three forms are supported:\",\n );\n lines.push(\"\");\n lines.push(\n '1. Read-only state: `{ \"$state\": \"/statePath\" }` - resolves to the value at that state path (one-way read).',\n );\n lines.push(\n ' Example: `\"color\": { \"$state\": \"/theme/primary\" }` reads the color from state.',\n );\n lines.push(\"\");\n lines.push(\n '2. Two-way binding: `{ \"$bindState\": \"/statePath\" }` - resolves to the value at the state path AND enables write-back. Use on form input props (value, checked, pressed, etc.).',\n );\n lines.push(\n ' Example: `\"value\": { \"$bindState\": \"/form/email\" }` binds the input value to /form/email.',\n );\n lines.push(\n ' Inside repeat scopes: `\"checked\": { \"$bindItem\": \"completed\" }` binds to the current item\\'s completed field.',\n );\n lines.push(\"\");\n lines.push(\n '3. Conditional: `{ \"$cond\": <condition>, \"$then\": <value>, \"$else\": <value> }` - evaluates the condition (same syntax as visibility conditions) and picks the matching value.',\n );\n lines.push(\n ' Example: `\"color\": { \"$cond\": { \"$state\": \"/activeTab\", \"eq\": \"home\" }, \"$then\": \"#007AFF\", \"$else\": \"#8E8E93\" }`',\n );\n lines.push(\"\");\n lines.push(\n \"Use $bindState for form inputs (text fields, checkboxes, selects, sliders, etc.) and $state for read-only data display. Inside repeat scopes, use $bindItem for form inputs bound to the current item. Use dynamic props instead of duplicating elements with opposing visible conditions when only prop values differ.\",\n );\n lines.push(\"\");\n lines.push(\n '4. Template: `{ \"$template\": \"Hello, ${/name}!\" }` - interpolates `${/path}` references in the string with values from the state model.',\n );\n lines.push(\n ' Example: `\"label\": { \"$template\": \"Items: ${/cart/count} | Total: ${/cart/total}\" }` renders \"Items: 3 | Total: 42.00\" when /cart/count is 3 and /cart/total is 42.00.',\n );\n lines.push(\"\");\n\n // $computed section — only emit when catalog defines functions\n const catalogFunctions = (catalog.data as Record<string, unknown>).functions;\n if (catalogFunctions && Object.keys(catalogFunctions).length > 0) {\n lines.push(\n '5. Computed: `{ \"$computed\": \"<functionName>\", \"args\": { \"key\": <expression> } }` - calls a registered function with resolved args and returns the result.',\n );\n lines.push(\n ' Example: `\"value\": { \"$computed\": \"fullName\", \"args\": { \"first\": { \"$state\": \"/form/firstName\" }, \"last\": { \"$state\": \"/form/lastName\" } } }`',\n );\n lines.push(\" Available functions:\");\n for (const name of Object.keys(\n catalogFunctions as Record<string, unknown>,\n )) {\n lines.push(` - ${name}`);\n }\n lines.push(\"\");\n }\n\n // Validation section — only emit when at least one component has a `checks` prop\n const hasChecksComponents = allComponents\n ? Object.entries(allComponents).some(([, def]) => {\n if (!def.props) return false;\n const formatted = formatZodType(def.props);\n return formatted.includes(\"checks\");\n })\n : false;\n\n if (hasChecksComponents) {\n lines.push(\"VALIDATION:\");\n lines.push(\n \"Form components that accept a `checks` prop support client-side validation.\",\n );\n lines.push(\n 'Each check is an object: { \"type\": \"<name>\", \"message\": \"...\", \"args\": { ... } }',\n );\n lines.push(\"\");\n lines.push(\"Built-in validation types:\");\n lines.push(\" - required — value must be non-empty\");\n lines.push(\" - email — valid email format\");\n lines.push(' - minLength — minimum string length (args: { \"min\": N })');\n lines.push(' - maxLength — maximum string length (args: { \"max\": N })');\n lines.push(' - pattern — match a regex (args: { \"pattern\": \"regex\" })');\n lines.push(' - min — minimum numeric value (args: { \"min\": N })');\n lines.push(' - max — maximum numeric value (args: { \"max\": N })');\n lines.push(\" - numeric — value must be a number\");\n lines.push(\" - url — valid URL format\");\n lines.push(\n ' - matches — must equal another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - equalTo — alias for matches (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - lessThan — value must be less than another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - greaterThan — value must be greater than another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - requiredIf — required only when another field is truthy (args: { \"field\": { \"$state\": \"/path\" } })',\n );\n lines.push(\"\");\n lines.push(\"Example:\");\n lines.push(\n ' \"checks\": [{ \"type\": \"required\", \"message\": \"Email is required\" }, { \"type\": \"email\", \"message\": \"Invalid email\" }]',\n );\n lines.push(\"\");\n lines.push(\n \"IMPORTANT: When using checks, the component must also have a { $bindState } or { $bindItem } on its value/checked prop for two-way binding.\",\n );\n lines.push(\n \"Always include validation checks on form inputs for a good user experience (e.g. required, email, minLength).\",\n );\n lines.push(\"\");\n }\n\n // State watchers section — only emit when actions are available (watchers\n // trigger actions, so the section is irrelevant without them).\n if (hasCustomActions || hasBuiltInActions) {\n lines.push(\"STATE WATCHERS:\");\n lines.push(\n \"Elements can have an optional `watch` field to react to state changes and trigger actions. The `watch` field is a top-level field on the element (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n \"Maps state paths (JSON Pointers) to action bindings. When the value at a watched path changes, the bound actions fire automatically.\",\n );\n lines.push(\"\");\n lines.push(\n \"Example (cascading select — country changes trigger city loading):\",\n );\n lines.push(\n ` ${JSON.stringify({ type: \"Select\", props: { value: { $bindState: \"/form/country\" }, options: [\"US\", \"Canada\", \"UK\"] }, watch: { \"/form/country\": { action: \"loadCities\", params: { country: { $state: \"/form/country\" } } } }, children: [] })}`,\n );\n lines.push(\"\");\n lines.push(\n \"Use `watch` for cascading dependencies where changing one field should trigger side effects (loading data, resetting dependent fields, computing derived values).\",\n );\n lines.push(\n \"IMPORTANT: `watch` is a top-level field on the element (sibling of type/props/children), NOT inside props. Watchers only fire when the value changes, not on initial render.\",\n );\n lines.push(\"\");\n }\n\n // Rules\n lines.push(\"RULES:\");\n const baseRules =\n mode === \"chat\"\n ? [\n \"When generating UI, wrap all JSONL patches in a ```spec code fence - one JSON object per line inside the fence\",\n \"Write a brief conversational response before any JSONL output\",\n 'First set root: {\"op\":\"add\",\"path\":\"/root\",\"value\":\"<root-key>\"}',\n 'Then add each element: {\"op\":\"add\",\"path\":\"/elements/<key>\",\"value\":{...}}',\n \"Output /state patches right after the elements that use them, one per array item for progressive loading. REQUIRED whenever using $state, $bindState, $bindItem, $item, $index, or repeat.\",\n \"ONLY use components listed above\",\n \"Each element value needs: type, props, children (array of child keys)\",\n \"Use unique keys for the element map entries (e.g., 'header', 'metric-1', 'chart-revenue')\",\n ]\n : [\n \"Output ONLY JSONL patches - one JSON object per line, no markdown, no code fences\",\n 'First set root: {\"op\":\"add\",\"path\":\"/root\",\"value\":\"<root-key>\"}',\n 'Then add each element: {\"op\":\"add\",\"path\":\"/elements/<key>\",\"value\":{...}}',\n \"Output /state patches right after the elements that use them, one per array item for progressive loading. REQUIRED whenever using $state, $bindState, $bindItem, $item, $index, or repeat.\",\n \"ONLY use components listed above\",\n \"Each element value needs: type, props, children (array of child keys)\",\n \"Use unique keys for the element map entries (e.g., 'header', 'metric-1', 'chart-revenue')\",\n ];\n const schemaRules = catalog.schema.defaultRules ?? [];\n const allRules = [...baseRules, ...schemaRules, ...customRules];\n allRules.forEach((rule, i) => {\n lines.push(`${i + 1}. ${rule}`);\n });\n\n return lines.join(\"\\n\");\n}\n\n// =============================================================================\n// Example Value Generation from Zod Schemas\n// =============================================================================\n\n/**\n * Component definition shape as it appears in catalog data\n */\ninterface CatalogComponentDef {\n props?: z.ZodType;\n description?: string;\n slots?: string[];\n events?: string[];\n example?: Record<string, unknown>;\n}\n\n/**\n * Get example props for a catalog component.\n * Uses the explicit `example` field if provided, otherwise generates from Zod schema.\n */\nfunction getExampleProps(def: CatalogComponentDef): Record<string, unknown> {\n if (def.example && Object.keys(def.example).length > 0) {\n return def.example;\n }\n if (def.props) {\n return generateExamplePropsFromZod(def.props);\n }\n return {};\n}\n\n/**\n * Generate example prop values from a Zod object schema.\n * Only includes required fields to keep examples concise.\n */\nfunction generateExamplePropsFromZod(\n schema: z.ZodType,\n): Record<string, unknown> {\n if (!schema || !schema._def) return {};\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n if (typeName !== \"ZodObject\" && typeName !== \"object\") return {};\n\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return {};\n\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(shape)) {\n const innerTypeName = getZodTypeName(value);\n // Skip optional props to keep examples concise\n if (\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"nullable\"\n ) {\n continue;\n }\n result[key] = generateExampleValue(value);\n }\n return result;\n}\n\n/**\n * Generate a single example value from a Zod type.\n */\nfunction generateExampleValue(schema: z.ZodType): unknown {\n if (!schema || !schema._def) return \"...\";\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n switch (typeName) {\n case \"ZodString\":\n case \"string\":\n return \"example\";\n case \"ZodNumber\":\n case \"number\":\n return 0;\n case \"ZodBoolean\":\n case \"boolean\":\n return true;\n case \"ZodLiteral\":\n case \"literal\":\n return def.value;\n case \"ZodEnum\":\n case \"enum\": {\n if (Array.isArray(def.values) && def.values.length > 0)\n return def.values[0];\n if (def.entries && typeof def.entries === \"object\") {\n const values = Object.values(def.entries as Record<string, string>);\n return values.length > 0 ? values[0] : \"example\";\n }\n return \"example\";\n }\n case \"ZodOptional\":\n case \"optional\":\n case \"ZodNullable\":\n case \"nullable\":\n case \"ZodDefault\":\n case \"default\": {\n const inner = (def.innerType as z.ZodType) ?? (def.wrapped as z.ZodType);\n return inner ? generateExampleValue(inner) : null;\n }\n case \"ZodArray\":\n case \"array\":\n return [];\n case \"ZodObject\":\n case \"object\":\n return generateExamplePropsFromZod(schema);\n case \"ZodUnion\":\n case \"union\": {\n const options = def.options as z.ZodType[] | undefined;\n return options && options.length > 0\n ? generateExampleValue(options[0]!)\n : \"...\";\n }\n default:\n return \"...\";\n }\n}\n\n/**\n * Find the name of the first required string prop in a Zod object schema.\n * Used to demonstrate $state dynamic bindings in examples.\n */\nfunction findFirstStringProp(schema?: z.ZodType): string | null {\n if (!schema || !schema._def) return null;\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n if (typeName !== \"ZodObject\" && typeName !== \"object\") return null;\n\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return null;\n\n for (const [key, value] of Object.entries(shape)) {\n const innerTypeName = getZodTypeName(value);\n // Skip optional props\n if (\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"nullable\"\n ) {\n continue;\n }\n // Unwrap to check the actual type\n if (innerTypeName === \"ZodString\" || innerTypeName === \"string\") {\n return key;\n }\n }\n return null;\n}\n\n// =============================================================================\n// Zod Introspection Helpers\n// =============================================================================\n\n/**\n * Get Zod type name from schema (handles different Zod versions)\n */\nfunction getZodTypeName(schema: z.ZodType): string {\n if (!schema || !schema._def) return \"\";\n const def = schema._def as unknown as Record<string, unknown>;\n // Zod 4+ uses _def.type, older versions use _def.typeName\n return (def.typeName as string) ?? (def.type as string) ?? \"\";\n}\n\n/**\n * Format a Zod type into a human-readable string\n */\nfunction formatZodType(schema: z.ZodType): string {\n if (!schema || !schema._def) return \"unknown\";\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n switch (typeName) {\n case \"ZodString\":\n case \"string\":\n return \"string\";\n case \"ZodNumber\":\n case \"number\":\n return \"number\";\n case \"ZodBoolean\":\n case \"boolean\":\n return \"boolean\";\n case \"ZodLiteral\":\n case \"literal\":\n return JSON.stringify(def.value);\n case \"ZodEnum\":\n case \"enum\": {\n // Zod 3 uses values array, Zod 4 uses entries object\n let values: string[];\n if (Array.isArray(def.values)) {\n values = def.values as string[];\n } else if (def.entries && typeof def.entries === \"object\") {\n values = Object.values(def.entries as Record<string, string>);\n } else {\n return \"enum\";\n }\n return values.map((v) => `\"${v}\"`).join(\" | \");\n }\n case \"ZodArray\":\n case \"array\": {\n // safely resolve inner type for Zod arrays\n const inner = (\n typeof def.element === \"object\"\n ? def.element\n : typeof def.type === \"object\"\n ? def.type\n : undefined\n ) as z.ZodType | undefined;\n return inner ? `Array<${formatZodType(inner)}>` : \"Array<unknown>\";\n }\n case \"ZodObject\":\n case \"object\": {\n // Shape can be a function (Zod 3) or direct object (Zod 4)\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return \"object\";\n const props = Object.entries(shape)\n .map(([key, value]) => {\n const innerTypeName = getZodTypeName(value);\n const isOptional =\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"nullable\";\n return `${key}${isOptional ? \"?\" : \"\"}: ${formatZodType(value)}`;\n })\n .join(\", \");\n return `{ ${props} }`;\n }\n case \"ZodOptional\":\n case \"optional\":\n case \"ZodNullable\":\n case \"nullable\": {\n const inner = (def.innerType as z.ZodType) ?? (def.wrapped as z.ZodType);\n return inner ? formatZodType(inner) : \"unknown\";\n }\n case \"ZodUnion\":\n case \"union\": {\n const options = def.options as z.ZodType[] | undefined;\n return options\n ? options.map((opt) => formatZodType(opt)).join(\" | \")\n : \"unknown\";\n }\n default:\n return \"unknown\";\n }\n}\n\n/**\n * Convert Zod schema to JSON Schema\n */\nfunction zodToJsonSchema(schema: z.ZodType): object {\n // Simplified JSON Schema conversion\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = (def.typeName as string) ?? \"\";\n\n switch (typeName) {\n case \"ZodString\":\n return { type: \"string\" };\n case \"ZodNumber\":\n return { type: \"number\" };\n case \"ZodBoolean\":\n return { type: \"boolean\" };\n case \"ZodLiteral\":\n return { const: def.value };\n case \"ZodEnum\":\n return { enum: def.values };\n case \"ZodArray\": {\n const inner = def.type as z.ZodType | undefined;\n return {\n type: \"array\",\n items: inner ? zodToJsonSchema(inner) : {},\n };\n }\n case \"ZodObject\": {\n const shape = (def.shape as () => Record<string, z.ZodType>)?.();\n if (!shape) return { type: \"object\" };\n const properties: Record<string, object> = {};\n const required: string[] = [];\n for (const [key, value] of Object.entries(shape)) {\n properties[key] = zodToJsonSchema(value);\n const innerDef = value._def as unknown as Record<string, unknown>;\n if (\n innerDef.typeName !== \"ZodOptional\" &&\n innerDef.typeName !== \"ZodNullable\"\n ) {\n required.push(key);\n }\n }\n return {\n type: \"object\",\n properties,\n required: required.length > 0 ? required : undefined,\n additionalProperties: false,\n };\n }\n case \"ZodRecord\": {\n const valueType = def.valueType as z.ZodType | undefined;\n return {\n type: \"object\",\n additionalProperties: valueType ? zodToJsonSchema(valueType) : true,\n };\n }\n case \"ZodOptional\":\n case \"ZodNullable\": {\n const inner = def.innerType as z.ZodType | undefined;\n return inner ? zodToJsonSchema(inner) : {};\n }\n case \"ZodUnion\": {\n const options = def.options as z.ZodType[] | undefined;\n return options ? { anyOf: options.map(zodToJsonSchema) } : {};\n }\n case \"ZodAny\":\n return {};\n default:\n return {};\n }\n}\n\n/**\n * Shorthand: Define a catalog directly from a schema\n */\nexport function defineCatalog<\n TDef extends SchemaDefinition,\n TCatalog extends InferCatalogInput<TDef[\"catalog\"]>,\n>(schema: Schema<TDef>, catalog: TCatalog): Catalog<TDef, TCatalog> {\n return schema.createCatalog(catalog);\n}\n","import type { Spec } from \"./types\";\n\n/**\n * Options for building a user prompt.\n */\nexport interface UserPromptOptions {\n /** The user's text prompt */\n prompt: string;\n /** Existing spec to refine (triggers patch-only mode) */\n currentSpec?: Spec | null;\n /** Runtime state context to include */\n state?: Record<string, unknown> | null;\n /** Maximum length for the user's text prompt (applied before wrapping) */\n maxPromptLength?: number;\n}\n\n/**\n * Check whether a spec is non-empty (has a root and at least one element).\n */\nfunction isNonEmptySpec(spec: unknown): spec is Spec {\n if (!spec || typeof spec !== \"object\") return false;\n const s = spec as Record<string, unknown>;\n return (\n typeof s.root === \"string\" &&\n typeof s.elements === \"object\" &&\n s.elements !== null &&\n Object.keys(s.elements as object).length > 0\n );\n}\n\nconst PATCH_INSTRUCTIONS = `IMPORTANT: The current UI is already loaded. Output ONLY the patches needed to make the requested change:\n- To add a new element: {\"op\":\"add\",\"path\":\"/elements/new-key\",\"value\":{...}}\n- To modify an existing element: {\"op\":\"replace\",\"path\":\"/elements/existing-key\",\"value\":{...}}\n- To remove an element: {\"op\":\"remove\",\"path\":\"/elements/old-key\"}\n- To update the root: {\"op\":\"replace\",\"path\":\"/root\",\"value\":\"new-root-key\"}\n- To add children: update the parent element with new children array\n\nDO NOT output patches for elements that don't need to change. Only output what's necessary for the requested modification.`;\n\n/**\n * Build a user prompt for AI generation.\n *\n * Handles common patterns that every consuming app needs:\n * - Truncating the user's prompt to a max length\n * - Including the current spec for refinement (patch-only mode)\n * - Including runtime state context\n *\n * @example\n * ```ts\n * // Fresh generation\n * buildUserPrompt({ prompt: \"create a todo app\" })\n *\n * // Refinement with existing spec\n * buildUserPrompt({ prompt: \"add a dark mode toggle\", currentSpec: spec })\n *\n * // With state context\n * buildUserPrompt({ prompt: \"show my data\", state: { todos: [] } })\n * ```\n */\nexport function buildUserPrompt(options: UserPromptOptions): string {\n const { prompt, currentSpec, state, maxPromptLength } = options;\n\n // Sanitize and optionally truncate the user's text\n let userText = String(prompt || \"\");\n if (maxPromptLength !== undefined && maxPromptLength > 0) {\n userText = userText.slice(0, maxPromptLength);\n }\n\n // --- Refinement mode: currentSpec is provided ---\n if (isNonEmptySpec(currentSpec)) {\n const parts: string[] = [];\n\n parts.push(\n `CURRENT UI STATE (already loaded, DO NOT recreate existing elements):`,\n );\n parts.push(JSON.stringify(currentSpec, null, 2));\n parts.push(\"\");\n parts.push(`USER REQUEST: ${userText}`);\n\n // Append state context if provided\n if (state && Object.keys(state).length > 0) {\n parts.push(\"\");\n parts.push(`AVAILABLE STATE:\\n${JSON.stringify(state, null, 2)}`);\n }\n\n parts.push(\"\");\n parts.push(PATCH_INSTRUCTIONS);\n\n return parts.join(\"\\n\");\n }\n\n // --- Fresh generation mode ---\n const parts: string[] = [userText];\n\n if (state && Object.keys(state).length > 0) {\n parts.push(`\\nAVAILABLE STATE:\\n${JSON.stringify(state, null, 2)}`);\n }\n\n parts.push(\n `\\nRemember: Output /root first, then interleave /elements and /state patches so the UI fills in progressively as it streams. Output each state patch right after the elements that use it, one per array item.`,\n );\n\n return parts.join(\"\\n\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS;AAoBlB,IAAM,oBAAoB,EAAE,MAAM;AAAA,EAChC,EAAE,OAAO;AAAA,EACT,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAED,IAAM,gBAAgB;AAAA,EACpB,IAAI,EAAE,QAAQ,EAAE,SAAS;AAAA,EACzB,KAAK,EAAE,QAAQ,EAAE,SAAS;AAAA,EAC1B,IAAI,kBAAkB,SAAS;AAAA,EAC/B,K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AAE,QAAkB,EAAE,SAAS;AAE/C;AAEA,IAAM,qBAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6BpB,SAAS,gBAAgB,SAAoC;AAClE,QAAM,EAAE,QAAQ,aAAa,OAAO,gBAAgB,IAAI;AAGxD,MAAI,WAAW,OAAO,UAAU,EAAE;AAClC,MAAI,oBAAoB,UAAa,kBAAkB,GAAG;AACxD,eAAW,SAAS,MAAM,GAAG,eAAe;AAAA,EAC9C;AAGA,MAAI,eAAe,WAAW,GAAG;AAC/B,UAAMC,SAAkB,CAAC;AAEzB,IAAAA,OAAM;AAAA,MACJ;AAAA,IACF;AACA,IAAAA,OAAM,KAAK,KAAK,UAAU,aAAa,MAAM,CAAC,CAAC;AAC/C,IAAAA,OAAM,KAAK,EAAE;AACb,IAAAA,OAAM,KAAK,iBAAiB,QAAQ,EAAE;AAGtC,QAAI,SAAS,OAAO,KAAK,KAAK,EAAE,SAAS,GAAG;AAC1C,MAAAA,OAAM,KAAK,EAAE;AACb,MAAAA,OAAM,KAAK;AAAA,EAAqB,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC,EAAE;AAAA,IAClE;AAEA,IAAAA,OAAM,KAAK,EAAE;AACb,IAAAA,OAAM,KAAK,kBAAkB;AAE7B,WAAOA,OAAM,KAAK,IAAI;AAAA,EACxB;AAGA,QAAM,QAAkB,CAAC,QAAQ;AAEjC,MAAI,SAAS,OAAO,KAAK,KAAK,EAAE,SAAS,GAAG;AAC1C,UAAM,KAAK;AAAA;AAAA,EAAuB,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC,EAAE;AAAA,EACpE;AAEA,QAAM;AAAA,IACJ;AAAA;AAAA,EACF;AAEA,SAAO,MAAM,KAAK,IAAI;AACxB;","names":["z","z","action","executeAction","z","z","check","z","action","parts"]}
1
+ {"version":3,"sources":["../src/visibility.ts","../src/props.ts","../src/actions.ts","../src/validation.ts","../src/spec-validator.ts","../src/schema.ts","../src/prompt.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type {\n VisibilityCondition,\n StateCondition,\n ItemCondition,\n IndexCondition,\n SingleCondition,\n AndCondition,\n OrCondition,\n StateModel,\n} from \"./types\";\nimport { getByPath } from \"./types\";\n\n// =============================================================================\n// Schemas\n// =============================================================================\n\n/**\n * Schema for a single state condition.\n */\nconst numericOrStateRef = z.union([\n z.number(),\n z.object({ $state: z.string() }),\n]);\n\nconst comparisonOps = {\n eq: z.unknown().optional(),\n neq: z.unknown().optional(),\n gt: numericOrStateRef.optional(),\n gte: numericOrStateRef.optional(),\n lt: numericOrStateRef.optional(),\n lte: numericOrStateRef.optional(),\n not: z.literal(true).optional(),\n};\n\nconst StateConditionSchema = z.object({\n $state: z.string(),\n ...comparisonOps,\n});\n\nconst ItemConditionSchema = z.object({\n $item: z.string(),\n ...comparisonOps,\n});\n\nconst IndexConditionSchema = z.object({\n $index: z.literal(true),\n ...comparisonOps,\n});\n\nconst SingleConditionSchema = z.union([\n StateConditionSchema,\n ItemConditionSchema,\n IndexConditionSchema,\n]);\n\n/**\n * Visibility condition schema.\n *\n * Lazy because `OrCondition` can recursively contain `VisibilityCondition`.\n */\nexport const VisibilityConditionSchema: z.ZodType<VisibilityCondition> = z.lazy(\n () =>\n z.union([\n z.boolean(),\n SingleConditionSchema,\n z.array(SingleConditionSchema),\n z.object({ $and: z.array(VisibilityConditionSchema) }),\n z.object({ $or: z.array(VisibilityConditionSchema) }),\n ]),\n);\n\n// =============================================================================\n// Context\n// =============================================================================\n\n/**\n * Context for evaluating visibility conditions.\n *\n * `repeatItem` and `repeatIndex` are only present inside a `repeat` scope\n * and enable `$item` / `$index` conditions.\n */\nexport interface VisibilityContext {\n stateModel: StateModel;\n /** The current repeat item (set inside a repeat scope). */\n repeatItem?: unknown;\n /** The current repeat array index (set inside a repeat scope). */\n repeatIndex?: number;\n}\n\n// =============================================================================\n// Evaluation\n// =============================================================================\n\n/**\n * Resolve a comparison value. If it's a `{ $state }` reference, look it up;\n * otherwise return the literal.\n */\nfunction resolveComparisonValue(\n value: unknown,\n ctx: VisibilityContext,\n): unknown {\n if (typeof value === \"object\" && value !== null) {\n if (\n \"$state\" in value &&\n typeof (value as Record<string, unknown>).$state === \"string\"\n ) {\n return getByPath(ctx.stateModel, (value as { $state: string }).$state);\n }\n }\n return value;\n}\n\n/**\n * Type guards for condition sources.\n */\nfunction isItemCondition(cond: SingleCondition): cond is ItemCondition {\n return \"$item\" in cond;\n}\n\nfunction isIndexCondition(cond: SingleCondition): cond is IndexCondition {\n return \"$index\" in cond;\n}\n\n/**\n * Resolve the left-hand-side value of a condition based on its source.\n */\nfunction resolveConditionValue(\n cond: SingleCondition,\n ctx: VisibilityContext,\n): unknown {\n if (isIndexCondition(cond)) {\n return ctx.repeatIndex;\n }\n if (isItemCondition(cond)) {\n if (ctx.repeatItem === undefined) return undefined;\n return cond.$item === \"\"\n ? ctx.repeatItem\n : getByPath(ctx.repeatItem, cond.$item);\n }\n // StateCondition\n return getByPath(ctx.stateModel, (cond as StateCondition).$state);\n}\n\n/**\n * Evaluate a single condition against the context.\n *\n * When `not` is `true`, the final result is inverted — this applies to\n * whichever operator is present (or to the truthiness check if no operator\n * is given). For example:\n * - `{ $state: \"/x\", not: true }` → `!Boolean(value)`\n * - `{ $state: \"/x\", gt: 5, not: true }` → `!(value > 5)`\n */\nfunction evaluateCondition(\n cond: SingleCondition,\n ctx: VisibilityContext,\n): boolean {\n const value = resolveConditionValue(cond, ctx);\n let result: boolean;\n\n // Equality\n if (cond.eq !== undefined) {\n const rhs = resolveComparisonValue(cond.eq, ctx);\n result = value === rhs;\n }\n // Inequality\n else if (cond.neq !== undefined) {\n const rhs = resolveComparisonValue(cond.neq, ctx);\n result = value !== rhs;\n }\n // Greater than\n else if (cond.gt !== undefined) {\n const rhs = resolveComparisonValue(cond.gt, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value > rhs\n : false;\n }\n // Greater than or equal\n else if (cond.gte !== undefined) {\n const rhs = resolveComparisonValue(cond.gte, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value >= rhs\n : false;\n }\n // Less than\n else if (cond.lt !== undefined) {\n const rhs = resolveComparisonValue(cond.lt, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value < rhs\n : false;\n }\n // Less than or equal\n else if (cond.lte !== undefined) {\n const rhs = resolveComparisonValue(cond.lte, ctx);\n result =\n typeof value === \"number\" && typeof rhs === \"number\"\n ? value <= rhs\n : false;\n }\n // Truthiness (no operator)\n else {\n result = Boolean(value);\n }\n\n // `not` inverts the result of any condition\n return cond.not === true ? !result : result;\n}\n\n/**\n * Type guard for AndCondition\n */\nfunction isAndCondition(\n condition: VisibilityCondition,\n): condition is AndCondition {\n return (\n typeof condition === \"object\" &&\n condition !== null &&\n !Array.isArray(condition) &&\n \"$and\" in condition\n );\n}\n\n/**\n * Type guard for OrCondition\n */\nfunction isOrCondition(\n condition: VisibilityCondition,\n): condition is OrCondition {\n return (\n typeof condition === \"object\" &&\n condition !== null &&\n !Array.isArray(condition) &&\n \"$or\" in condition\n );\n}\n\n/**\n * Evaluate a visibility condition.\n *\n * - `undefined` → visible\n * - `boolean` → that value\n * - `SingleCondition` → evaluate single condition\n * - `SingleCondition[]` → implicit AND (all must be true)\n * - `AndCondition` → `{ $and: [...] }`, explicit AND\n * - `OrCondition` → `{ $or: [...] }`, at least one must be true\n */\nexport function evaluateVisibility(\n condition: VisibilityCondition | undefined,\n ctx: VisibilityContext,\n): boolean {\n // No condition = visible\n if (condition === undefined) {\n return true;\n }\n\n // Boolean literal\n if (typeof condition === \"boolean\") {\n return condition;\n }\n\n // Array = implicit AND\n if (Array.isArray(condition)) {\n return condition.every((c) => evaluateCondition(c, ctx));\n }\n\n // Explicit AND condition\n if (isAndCondition(condition)) {\n return condition.$and.every((child) => evaluateVisibility(child, ctx));\n }\n\n // OR condition\n if (isOrCondition(condition)) {\n return condition.$or.some((child) => evaluateVisibility(child, ctx));\n }\n\n // Single condition\n return evaluateCondition(condition, ctx);\n}\n\n// =============================================================================\n// Helpers\n// =============================================================================\n\n/**\n * Helper to create visibility conditions.\n */\nexport const visibility = {\n /** Always visible */\n always: true as const,\n\n /** Never visible */\n never: false as const,\n\n /** Visible when state path is truthy */\n when: (path: string): StateCondition => ({ $state: path }),\n\n /** Visible when state path is falsy */\n unless: (path: string): StateCondition => ({ $state: path, not: true }),\n\n /** Equality check */\n eq: (path: string, value: unknown): StateCondition => ({\n $state: path,\n eq: value,\n }),\n\n /** Not equal check */\n neq: (path: string, value: unknown): StateCondition => ({\n $state: path,\n neq: value,\n }),\n\n /** Greater than */\n gt: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n gt: value,\n }),\n\n /** Greater than or equal */\n gte: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n gte: value,\n }),\n\n /** Less than */\n lt: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n lt: value,\n }),\n\n /** Less than or equal */\n lte: (path: string, value: number | { $state: string }): StateCondition => ({\n $state: path,\n lte: value,\n }),\n\n /** AND multiple conditions */\n and: (...conditions: VisibilityCondition[]): AndCondition => ({\n $and: conditions,\n }),\n\n /** OR multiple conditions */\n or: (...conditions: VisibilityCondition[]): OrCondition => ({\n $or: conditions,\n }),\n};\n","import type { VisibilityCondition, StateModel } from \"./types\";\nimport { getByPath } from \"./types\";\nimport { evaluateVisibility, type VisibilityContext } from \"./visibility\";\n\n// =============================================================================\n// Prop Expression Types\n// =============================================================================\n\n/**\n * A prop expression that resolves to a value based on state.\n *\n * - `{ $state: string }` reads a value from the global state model\n * - `{ $item: string }` reads a field from the current repeat item\n * (relative path into the item object; use `\"\"` for the whole item)\n * - `{ $index: true }` returns the current repeat array index. Uses `true`\n * as a sentinel flag because the index is a scalar with no sub-path to\n * navigate — unlike `$item` which needs a path into the item object.\n * - `{ $bindState: string }` two-way binding to a global state path —\n * resolves to the value at the path (like `$state`) AND exposes the\n * resolved path so the component can write back.\n * - `{ $bindItem: string }` two-way binding to a field on the current\n * repeat item — resolves via `repeatBasePath + path` and exposes the\n * absolute state path for write-back.\n * - `{ $cond, $then, $else }` conditionally picks a value\n * - `{ $computed: string, args?: Record<string, PropExpression> }` calls a\n * registered function with resolved args and returns the result\n * - `{ $template: string }` interpolates `${/path}` references in the\n * string with values from the state model\n * - Any other value is a literal (passthrough)\n */\nexport type PropExpression<T = unknown> =\n | T\n | { $state: string }\n | { $item: string }\n | { $index: true }\n | { $bindState: string }\n | { $bindItem: string }\n | {\n $cond: VisibilityCondition;\n $then: PropExpression<T>;\n $else: PropExpression<T>;\n }\n | { $computed: string; args?: Record<string, unknown> }\n | { $template: string };\n\n/**\n * Function signature for `$computed` expressions.\n * Receives a record of resolved argument values and returns a computed result.\n */\nexport type ComputedFunction = (args: Record<string, unknown>) => unknown;\n\n/**\n * Context for resolving prop expressions.\n * Extends {@link VisibilityContext} with an optional `repeatBasePath` used\n * to resolve `$bindItem` paths to absolute state paths.\n */\nexport interface PropResolutionContext extends VisibilityContext {\n /** Absolute state path to the current repeat item (e.g. \"/todos/0\"). Set inside repeat scopes. */\n repeatBasePath?: string;\n /** Named functions available for `$computed` expressions. */\n functions?: Record<string, ComputedFunction>;\n}\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\nfunction isStateExpression(value: unknown): value is { $state: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$state\" in value &&\n typeof (value as Record<string, unknown>).$state === \"string\"\n );\n}\n\nfunction isItemExpression(value: unknown): value is { $item: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$item\" in value &&\n typeof (value as Record<string, unknown>).$item === \"string\"\n );\n}\n\nfunction isIndexExpression(value: unknown): value is { $index: true } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$index\" in value &&\n (value as Record<string, unknown>).$index === true\n );\n}\n\nfunction isBindStateExpression(\n value: unknown,\n): value is { $bindState: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$bindState\" in value &&\n typeof (value as Record<string, unknown>).$bindState === \"string\"\n );\n}\n\nfunction isBindItemExpression(value: unknown): value is { $bindItem: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$bindItem\" in value &&\n typeof (value as Record<string, unknown>).$bindItem === \"string\"\n );\n}\n\nfunction isCondExpression(\n value: unknown,\n): value is { $cond: VisibilityCondition; $then: unknown; $else: unknown } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$cond\" in value &&\n \"$then\" in value &&\n \"$else\" in value\n );\n}\n\nfunction isComputedExpression(\n value: unknown,\n): value is { $computed: string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$computed\" in value &&\n typeof (value as Record<string, unknown>).$computed === \"string\"\n );\n}\n\nfunction isTemplateExpression(value: unknown): value is { $template: string } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"$template\" in value &&\n typeof (value as Record<string, unknown>).$template === \"string\"\n );\n}\n\n// Module-level set to avoid spamming console.warn on every render for the same\n// unknown $computed function name. Once the set reaches WARNED_COMPUTED_MAX,\n// new names are no longer deduplicated (warnings still fire) but the set stops\n// growing, preventing unbounded memory use in long-lived processes (e.g. SSR).\nconst WARNED_COMPUTED_MAX = 100;\nconst warnedComputedFns = new Set<string>();\n\n/** @internal Test-only: clear the deduplication set for $computed warnings. */\nexport function _resetWarnedComputedFns(): void {\n warnedComputedFns.clear();\n}\n\n// Same deduplication pattern for $template paths that don't start with \"/\".\nconst WARNED_TEMPLATE_MAX = 100;\nconst warnedTemplatePaths = new Set<string>();\n\n/** @internal Test-only: clear the deduplication set for $template warnings. */\nexport function _resetWarnedTemplatePaths(): void {\n warnedTemplatePaths.clear();\n}\n\n// =============================================================================\n// Prop Expression Resolution\n// =============================================================================\n\n// =============================================================================\n// $bindItem path resolution helper\n// =============================================================================\n\n/**\n * Resolve a `$bindItem` path into an absolute state path using the repeat\n * scope's base path.\n *\n * `\"\"` resolves to `repeatBasePath` (the whole item).\n * `\"field\"` resolves to `repeatBasePath + \"/field\"`.\n *\n * Returns `undefined` when no `repeatBasePath` is available (i.e. `$bindItem`\n * is used outside a repeat scope).\n */\nfunction resolveBindItemPath(\n itemPath: string,\n ctx: PropResolutionContext,\n): string | undefined {\n if (ctx.repeatBasePath == null) {\n console.warn(`$bindItem used outside repeat scope: \"${itemPath}\"`);\n return undefined;\n }\n if (itemPath === \"\") return ctx.repeatBasePath;\n return ctx.repeatBasePath + \"/\" + itemPath;\n}\n\n// =============================================================================\n// Prop Expression Resolution\n// =============================================================================\n\n/**\n * Resolve a single prop value that may contain expressions.\n * Handles $state, $item, $index, $bindState, $bindItem, and $cond/$then/$else in a single pass.\n */\nexport function resolvePropValue(\n value: unknown,\n ctx: PropResolutionContext,\n): unknown {\n if (value === null || value === undefined) {\n return value;\n }\n\n // $state: read from global state model\n if (isStateExpression(value)) {\n return getByPath(ctx.stateModel, value.$state);\n }\n\n // $item: read from current repeat item\n if (isItemExpression(value)) {\n if (ctx.repeatItem === undefined) return undefined;\n // \"\" means the whole item, \"field\" means a field on the item\n return value.$item === \"\"\n ? ctx.repeatItem\n : getByPath(ctx.repeatItem, value.$item);\n }\n\n // $index: return current repeat array index\n if (isIndexExpression(value)) {\n return ctx.repeatIndex;\n }\n\n // $bindState: two-way binding to global state path\n if (isBindStateExpression(value)) {\n return getByPath(ctx.stateModel, value.$bindState);\n }\n\n // $bindItem: two-way binding to repeat item field\n if (isBindItemExpression(value)) {\n const resolvedPath = resolveBindItemPath(value.$bindItem, ctx);\n if (resolvedPath === undefined) return undefined;\n return getByPath(ctx.stateModel, resolvedPath);\n }\n\n // $cond/$then/$else: evaluate condition and pick branch\n if (isCondExpression(value)) {\n const result = evaluateVisibility(value.$cond, ctx);\n return resolvePropValue(result ? value.$then : value.$else, ctx);\n }\n\n // $computed: call a registered function with resolved args\n if (isComputedExpression(value)) {\n const fn = ctx.functions?.[value.$computed];\n if (!fn) {\n if (!warnedComputedFns.has(value.$computed)) {\n if (warnedComputedFns.size < WARNED_COMPUTED_MAX) {\n warnedComputedFns.add(value.$computed);\n }\n console.warn(`Unknown $computed function: \"${value.$computed}\"`);\n }\n return undefined;\n }\n const resolvedArgs: Record<string, unknown> = {};\n if (value.args) {\n for (const [key, arg] of Object.entries(value.args)) {\n resolvedArgs[key] = resolvePropValue(arg, ctx);\n }\n }\n return fn(resolvedArgs);\n }\n\n // $template: interpolate ${/path} references with state values\n if (isTemplateExpression(value)) {\n return value.$template.replace(\n /\\$\\{([^}]+)\\}/g,\n (_match, rawPath: string) => {\n let path = rawPath;\n if (!path.startsWith(\"/\")) {\n if (!warnedTemplatePaths.has(path)) {\n if (warnedTemplatePaths.size < WARNED_TEMPLATE_MAX) {\n warnedTemplatePaths.add(path);\n }\n console.warn(\n `$template path \"${path}\" should be a JSON Pointer starting with \"/\". Automatically resolving as \"/${path}\".`,\n );\n }\n path = \"/\" + path;\n }\n const resolved = getByPath(ctx.stateModel, path);\n return resolved != null ? String(resolved) : \"\";\n },\n );\n }\n\n // Arrays: resolve each element\n if (Array.isArray(value)) {\n return value.map((item) => resolvePropValue(item, ctx));\n }\n\n // Plain objects (not expressions): resolve each value recursively\n if (typeof value === \"object\") {\n const resolved: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(value as Record<string, unknown>)) {\n resolved[key] = resolvePropValue(val, ctx);\n }\n return resolved;\n }\n\n // Primitive literal: passthrough\n return value;\n}\n\n/**\n * Resolve all prop values in an element's props object.\n * Returns a new props object with all expressions resolved.\n */\nexport function resolveElementProps(\n props: Record<string, unknown>,\n ctx: PropResolutionContext,\n): Record<string, unknown> {\n const resolved: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(props)) {\n resolved[key] = resolvePropValue(value, ctx);\n }\n return resolved;\n}\n\n/**\n * Scan an element's raw props for `$bindState` / `$bindItem` expressions\n * and return a map of prop name → resolved absolute state path.\n *\n * This is called **before** `resolveElementProps` so the component can\n * receive both the resolved value (in `props`) and the write-back path\n * (in `bindings`).\n *\n * @example\n * ```ts\n * const rawProps = { value: { $bindState: \"/form/email\" }, label: \"Email\" };\n * const bindings = resolveBindings(rawProps, ctx);\n * // bindings = { value: \"/form/email\" }\n * ```\n */\nexport function resolveBindings(\n props: Record<string, unknown>,\n ctx: PropResolutionContext,\n): Record<string, string> | undefined {\n let bindings: Record<string, string> | undefined;\n for (const [key, value] of Object.entries(props)) {\n if (isBindStateExpression(value)) {\n if (!bindings) bindings = {};\n bindings[key] = value.$bindState;\n } else if (isBindItemExpression(value)) {\n const resolved = resolveBindItemPath(value.$bindItem, ctx);\n if (resolved !== undefined) {\n if (!bindings) bindings = {};\n bindings[key] = resolved;\n }\n }\n }\n return bindings;\n}\n\n/**\n * Resolve a single action parameter value.\n *\n * Like {@link resolvePropValue} but with special handling for path-valued\n * params: `{ $item: \"field\" }` resolves to an **absolute state path**\n * (e.g. `/todos/0/field`) instead of the field's value, so the path can\n * be passed to `setState` / `pushState` / `removeState`.\n *\n * - `{ $item: \"field\" }` → absolute state path via `repeatBasePath`\n * - `{ $index: true }` → current repeat index (number)\n * - Everything else delegates to `resolvePropValue` ($state, $cond, literals).\n */\nexport function resolveActionParam(\n value: unknown,\n ctx: PropResolutionContext,\n): unknown {\n if (isItemExpression(value)) {\n return resolveBindItemPath(value.$item, ctx);\n }\n if (isIndexExpression(value)) {\n return ctx.repeatIndex;\n }\n return resolvePropValue(value, ctx);\n}\n","import { z } from \"zod\";\nimport type { DynamicValue, StateModel } from \"./types\";\nimport { DynamicValueSchema, resolveDynamicValue } from \"./types\";\n\n/**\n * Confirmation dialog configuration\n */\nexport interface ActionConfirm {\n title: string;\n message: string;\n confirmLabel?: string;\n cancelLabel?: string;\n variant?: \"default\" | \"danger\";\n}\n\n/**\n * Action success handler\n */\nexport type ActionOnSuccess =\n | { navigate: string }\n | { set: Record<string, unknown> }\n | { action: string };\n\n/**\n * Action error handler\n */\nexport type ActionOnError =\n | { set: Record<string, unknown> }\n | { action: string };\n\n/**\n * Action binding — maps an event to an action invocation.\n *\n * Used inside the `on` field of a UIElement:\n * ```json\n * { \"on\": { \"press\": { \"action\": \"setState\", \"params\": { \"statePath\": \"/x\", \"value\": 1 } } } }\n * ```\n */\nexport interface ActionBinding {\n /** Action name (must be in catalog) */\n action: string;\n /** Parameters to pass to the action handler */\n params?: Record<string, DynamicValue>;\n /** Confirmation dialog before execution */\n confirm?: ActionConfirm;\n /** Handler after successful execution */\n onSuccess?: ActionOnSuccess;\n /** Handler after failed execution */\n onError?: ActionOnError;\n /** Whether to prevent default browser behavior (e.g. navigation on links) */\n preventDefault?: boolean;\n}\n\n/**\n * @deprecated Use ActionBinding instead\n */\nexport type Action = ActionBinding;\n\n/**\n * Schema for action confirmation\n */\nexport const ActionConfirmSchema = z.object({\n title: z.string(),\n message: z.string(),\n confirmLabel: z.string().optional(),\n cancelLabel: z.string().optional(),\n variant: z.enum([\"default\", \"danger\"]).optional(),\n});\n\n/**\n * Schema for success handlers\n */\nexport const ActionOnSuccessSchema = z.union([\n z.object({ navigate: z.string() }),\n z.object({ set: z.record(z.string(), z.unknown()) }),\n z.object({ action: z.string() }),\n]);\n\n/**\n * Schema for error handlers\n */\nexport const ActionOnErrorSchema = z.union([\n z.object({ set: z.record(z.string(), z.unknown()) }),\n z.object({ action: z.string() }),\n]);\n\n/**\n * Full action binding schema\n */\nexport const ActionBindingSchema = z.object({\n action: z.string(),\n params: z.record(z.string(), DynamicValueSchema).optional(),\n confirm: ActionConfirmSchema.optional(),\n onSuccess: ActionOnSuccessSchema.optional(),\n onError: ActionOnErrorSchema.optional(),\n preventDefault: z.boolean().optional(),\n});\n\n/**\n * @deprecated Use ActionBindingSchema instead\n */\nexport const ActionSchema = ActionBindingSchema;\n\n/**\n * Action handler function signature\n */\nexport type ActionHandler<\n TParams = Record<string, unknown>,\n TResult = unknown,\n> = (params: TParams) => Promise<TResult> | TResult;\n\n/**\n * Action definition in catalog\n */\nexport interface ActionDefinition<TParams = Record<string, unknown>> {\n /** Zod schema for params validation */\n params?: z.ZodType<TParams>;\n /** Description for AI */\n description?: string;\n}\n\n/**\n * Resolved action with all dynamic values resolved\n */\nexport interface ResolvedAction {\n action: string;\n params: Record<string, unknown>;\n confirm?: ActionConfirm;\n onSuccess?: ActionOnSuccess;\n onError?: ActionOnError;\n}\n\n/**\n * Resolve all dynamic values in an action binding\n */\nexport function resolveAction(\n binding: ActionBinding,\n stateModel: StateModel,\n): ResolvedAction {\n const resolvedParams: Record<string, unknown> = {};\n\n if (binding.params) {\n for (const [key, value] of Object.entries(binding.params)) {\n resolvedParams[key] = resolveDynamicValue(value, stateModel);\n }\n }\n\n // Interpolate confirmation message if present\n let confirm = binding.confirm;\n if (confirm) {\n confirm = {\n ...confirm,\n message: interpolateString(confirm.message, stateModel),\n title: interpolateString(confirm.title, stateModel),\n };\n }\n\n return {\n action: binding.action,\n params: resolvedParams,\n confirm,\n onSuccess: binding.onSuccess,\n onError: binding.onError,\n };\n}\n\n/**\n * Interpolate ${path} expressions in a string\n */\nexport function interpolateString(\n template: string,\n stateModel: StateModel,\n): string {\n return template.replace(/\\$\\{([^}]+)\\}/g, (_, path) => {\n const value = resolveDynamicValue({ $state: path }, stateModel);\n return String(value ?? \"\");\n });\n}\n\n/**\n * Context for action execution\n */\nexport interface ActionExecutionContext {\n /** The resolved action */\n action: ResolvedAction;\n /** The action handler from the host */\n handler: ActionHandler;\n /** Function to update state model */\n setState: (path: string, value: unknown) => void;\n /** Function to navigate */\n navigate?: (path: string) => void;\n /** Function to execute another action */\n executeAction?: (name: string) => Promise<void>;\n}\n\n/**\n * Execute an action with all callbacks\n */\nexport async function executeAction(\n ctx: ActionExecutionContext,\n): Promise<void> {\n const { action, handler, setState, navigate, executeAction } = ctx;\n\n try {\n await handler(action.params);\n\n // Handle success\n if (action.onSuccess) {\n if (\"navigate\" in action.onSuccess && navigate) {\n navigate(action.onSuccess.navigate);\n } else if (\"set\" in action.onSuccess) {\n for (const [path, value] of Object.entries(action.onSuccess.set)) {\n setState(path, value);\n }\n } else if (\"action\" in action.onSuccess && executeAction) {\n await executeAction(action.onSuccess.action);\n }\n }\n } catch (error) {\n // Handle error\n if (action.onError) {\n if (\"set\" in action.onError) {\n for (const [path, value] of Object.entries(action.onError.set)) {\n // Replace $error.message with actual error\n const resolvedValue =\n typeof value === \"string\" && value === \"$error.message\"\n ? (error as Error).message\n : value;\n setState(path, resolvedValue);\n }\n } else if (\"action\" in action.onError && executeAction) {\n await executeAction(action.onError.action);\n }\n } else {\n throw error;\n }\n }\n}\n\n/**\n * Helper to create action bindings\n */\nexport const actionBinding = {\n /** Create a simple action binding */\n simple: (\n actionName: string,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n }),\n\n /** Create an action binding with confirmation */\n withConfirm: (\n actionName: string,\n confirm: ActionConfirm,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n confirm,\n }),\n\n /** Create an action binding with success handler */\n withSuccess: (\n actionName: string,\n onSuccess: ActionOnSuccess,\n params?: Record<string, DynamicValue>,\n ): ActionBinding => ({\n action: actionName,\n params,\n onSuccess,\n }),\n};\n\n/**\n * @deprecated Use actionBinding instead\n */\nexport const action = actionBinding;\n","import { z } from \"zod\";\nimport type { DynamicValue, StateModel, VisibilityCondition } from \"./types\";\nimport { DynamicValueSchema, resolveDynamicValue } from \"./types\";\nimport { VisibilityConditionSchema, evaluateVisibility } from \"./visibility\";\nimport { resolvePropValue } from \"./props\";\n\n/**\n * Validation check definition\n */\nexport interface ValidationCheck {\n /** Validation type (built-in or from catalog) */\n type: string;\n /** Additional arguments for the validation */\n args?: Record<string, DynamicValue>;\n /** Error message to display if check fails */\n message: string;\n}\n\n/**\n * Validation configuration for a field\n */\nexport interface ValidationConfig {\n /** Array of checks to run */\n checks?: ValidationCheck[];\n /** When to run validation */\n validateOn?: \"change\" | \"blur\" | \"submit\";\n /** Condition for when validation is enabled */\n enabled?: VisibilityCondition;\n}\n\n/**\n * Schema for validation check\n */\nexport const ValidationCheckSchema = z.object({\n type: z.string(),\n args: z.record(z.string(), DynamicValueSchema).optional(),\n message: z.string(),\n});\n\n/**\n * Schema for validation config\n */\nexport const ValidationConfigSchema = z.object({\n checks: z.array(ValidationCheckSchema).optional(),\n validateOn: z.enum([\"change\", \"blur\", \"submit\"]).optional(),\n enabled: VisibilityConditionSchema.optional(),\n});\n\n/**\n * Validation function signature\n */\nexport type ValidationFunction = (\n value: unknown,\n args?: Record<string, unknown>,\n) => boolean;\n\n/**\n * Validation function definition in catalog\n */\nexport interface ValidationFunctionDefinition {\n /** The validation function */\n validate: ValidationFunction;\n /** Description for AI */\n description?: string;\n}\n\nconst matchesImpl: ValidationFunction = (\n value: unknown,\n args?: Record<string, unknown>,\n) => {\n const other = args?.other;\n return value === other;\n};\n\n/**\n * Built-in validation functions\n */\nexport const builtInValidationFunctions: Record<string, ValidationFunction> = {\n /**\n * Check if value is not null, undefined, or empty string\n */\n required: (value: unknown) => {\n if (value === null || value === undefined) return false;\n if (typeof value === \"string\") return value.trim().length > 0;\n if (Array.isArray(value)) return value.length > 0;\n return true;\n },\n\n /**\n * Check if value is a valid email address\n */\n email: (value: unknown) => {\n if (typeof value !== \"string\") return false;\n return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(value);\n },\n\n /**\n * Check minimum string length\n */\n minLength: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const min = args?.min;\n if (typeof min !== \"number\") return false;\n return value.length >= min;\n },\n\n /**\n * Check maximum string length\n */\n maxLength: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const max = args?.max;\n if (typeof max !== \"number\") return false;\n return value.length <= max;\n },\n\n /**\n * Check if string matches a regex pattern\n */\n pattern: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"string\") return false;\n const pattern = args?.pattern;\n if (typeof pattern !== \"string\") return false;\n try {\n return new RegExp(pattern).test(value);\n } catch {\n return false;\n }\n },\n\n /**\n * Check minimum numeric value\n */\n min: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"number\") return false;\n const min = args?.min;\n if (typeof min !== \"number\") return false;\n return value >= min;\n },\n\n /**\n * Check maximum numeric value\n */\n max: (value: unknown, args?: Record<string, unknown>) => {\n if (typeof value !== \"number\") return false;\n const max = args?.max;\n if (typeof max !== \"number\") return false;\n return value <= max;\n },\n\n /**\n * Check if value is a number\n */\n numeric: (value: unknown) => {\n if (typeof value === \"number\") return !isNaN(value);\n if (typeof value === \"string\") return !isNaN(parseFloat(value));\n return false;\n },\n\n /**\n * Check if value is a valid URL\n */\n url: (value: unknown) => {\n if (typeof value !== \"string\") return false;\n try {\n new URL(value);\n return true;\n } catch {\n return false;\n }\n },\n\n /**\n * Check if value matches another field\n */\n matches: matchesImpl,\n\n /**\n * Alias for matches with a more descriptive name for cross-field equality\n */\n equalTo: matchesImpl,\n\n /**\n * Check if value is less than another field's value.\n * Supports numbers, strings (useful for ISO date comparison), and\n * cross-type numeric coercion (e.g. string \"3\" vs number 5).\n */\n lessThan: (value: unknown, args?: Record<string, unknown>) => {\n const other = args?.other;\n if (value == null || other == null || value === \"\" || other === \"\")\n return false;\n if (typeof value === \"number\" && typeof other === \"number\")\n return value < other;\n if (typeof value === \"string\" && typeof other === \"string\")\n return value < other;\n const numVal = Number(value);\n const numOther = Number(other);\n if (!isNaN(numVal) && !isNaN(numOther)) return numVal < numOther;\n return false;\n },\n\n /**\n * Check if value is greater than another field's value.\n * Supports numbers, strings (useful for ISO date comparison), and\n * cross-type numeric coercion (e.g. string \"7\" vs number 5).\n */\n greaterThan: (value: unknown, args?: Record<string, unknown>) => {\n const other = args?.other;\n if (value == null || other == null || value === \"\" || other === \"\")\n return false;\n if (typeof value === \"number\" && typeof other === \"number\")\n return value > other;\n if (typeof value === \"string\" && typeof other === \"string\")\n return value > other;\n const numVal = Number(value);\n const numOther = Number(other);\n if (!isNaN(numVal) && !isNaN(numOther)) return numVal > numOther;\n return false;\n },\n\n /**\n * Required only when a condition is met.\n * Uses JS truthiness: 0, false, \"\", null, and undefined are all\n * treated as \"condition not met\" (field not required), matching\n * the visibility system's bare-condition semantics.\n */\n requiredIf: (value: unknown, args?: Record<string, unknown>) => {\n const condition = args?.field;\n if (!condition) return true;\n if (value === null || value === undefined) return false;\n if (typeof value === \"string\") return value.trim().length > 0;\n if (Array.isArray(value)) return value.length > 0;\n return true;\n },\n};\n\n/**\n * Validation result for a single check\n */\nexport interface ValidationCheckResult {\n type: string;\n valid: boolean;\n message: string;\n}\n\n/**\n * Full validation result for a field\n */\nexport interface ValidationResult {\n valid: boolean;\n errors: string[];\n checks: ValidationCheckResult[];\n}\n\n/**\n * Context for running validation\n */\nexport interface ValidationContext {\n /** Current value to validate */\n value: unknown;\n /** Full data model for resolving paths */\n stateModel: StateModel;\n /** Custom validation functions from catalog */\n customFunctions?: Record<string, ValidationFunction>;\n}\n\n/**\n * Run a single validation check\n */\nexport function runValidationCheck(\n check: ValidationCheck,\n ctx: ValidationContext,\n): ValidationCheckResult {\n const { value, stateModel, customFunctions } = ctx;\n\n // Resolve args using resolvePropValue so nested $state refs (and any other\n // prop expressions) are handled consistently with the rest of the system.\n const resolvedArgs: Record<string, unknown> = {};\n if (check.args) {\n for (const [key, argValue] of Object.entries(check.args)) {\n resolvedArgs[key] = resolvePropValue(argValue, { stateModel });\n }\n }\n\n // Find the validation function\n const validationFn =\n builtInValidationFunctions[check.type] ?? customFunctions?.[check.type];\n\n if (!validationFn) {\n console.warn(`Unknown validation function: ${check.type}`);\n return {\n type: check.type,\n valid: true, // Don't fail on unknown functions\n message: check.message,\n };\n }\n\n const valid = validationFn(value, resolvedArgs);\n\n return {\n type: check.type,\n valid,\n message: check.message,\n };\n}\n\n/**\n * Run all validation checks for a field\n */\nexport function runValidation(\n config: ValidationConfig,\n ctx: ValidationContext,\n): ValidationResult {\n const checks: ValidationCheckResult[] = [];\n const errors: string[] = [];\n\n // Check if validation is enabled\n if (config.enabled) {\n const enabled = evaluateVisibility(config.enabled, {\n stateModel: ctx.stateModel,\n });\n if (!enabled) {\n return { valid: true, errors: [], checks: [] };\n }\n }\n\n // Run each check\n if (config.checks) {\n for (const check of config.checks) {\n const result = runValidationCheck(check, ctx);\n checks.push(result);\n if (!result.valid) {\n errors.push(result.message);\n }\n }\n }\n\n return {\n valid: errors.length === 0,\n errors,\n checks,\n };\n}\n\n/**\n * Helper to create validation checks\n */\nexport const check = {\n required: (message = \"This field is required\"): ValidationCheck => ({\n type: \"required\",\n message,\n }),\n\n email: (message = \"Invalid email address\"): ValidationCheck => ({\n type: \"email\",\n message,\n }),\n\n minLength: (min: number, message?: string): ValidationCheck => ({\n type: \"minLength\",\n args: { min },\n message: message ?? `Must be at least ${min} characters`,\n }),\n\n maxLength: (max: number, message?: string): ValidationCheck => ({\n type: \"maxLength\",\n args: { max },\n message: message ?? `Must be at most ${max} characters`,\n }),\n\n pattern: (pattern: string, message = \"Invalid format\"): ValidationCheck => ({\n type: \"pattern\",\n args: { pattern },\n message,\n }),\n\n min: (min: number, message?: string): ValidationCheck => ({\n type: \"min\",\n args: { min },\n message: message ?? `Must be at least ${min}`,\n }),\n\n max: (max: number, message?: string): ValidationCheck => ({\n type: \"max\",\n args: { max },\n message: message ?? `Must be at most ${max}`,\n }),\n\n url: (message = \"Invalid URL\"): ValidationCheck => ({\n type: \"url\",\n message,\n }),\n\n numeric: (message = \"Must be a number\"): ValidationCheck => ({\n type: \"numeric\",\n message,\n }),\n\n matches: (\n otherPath: string,\n message = \"Fields must match\",\n ): ValidationCheck => ({\n type: \"matches\",\n args: { other: { $state: otherPath } },\n message,\n }),\n\n equalTo: (\n otherPath: string,\n message = \"Fields must match\",\n ): ValidationCheck => ({\n type: \"equalTo\",\n args: { other: { $state: otherPath } },\n message,\n }),\n\n lessThan: (otherPath: string, message?: string): ValidationCheck => ({\n type: \"lessThan\",\n args: { other: { $state: otherPath } },\n message: message ?? \"Must be less than the compared field\",\n }),\n\n greaterThan: (otherPath: string, message?: string): ValidationCheck => ({\n type: \"greaterThan\",\n args: { other: { $state: otherPath } },\n message: message ?? \"Must be greater than the compared field\",\n }),\n\n requiredIf: (\n fieldPath: string,\n message = \"This field is required\",\n ): ValidationCheck => ({\n type: \"requiredIf\",\n args: { field: { $state: fieldPath } },\n message,\n }),\n};\n","import type { Spec, UIElement } from \"./types\";\n\n// =============================================================================\n// Spec Structural Validation\n// =============================================================================\n\n/**\n * Severity level for validation issues.\n */\nexport type SpecIssueSeverity = \"error\" | \"warning\";\n\n/**\n * A single validation issue found in a spec.\n */\nexport interface SpecIssue {\n /** Severity: errors should be fixed, warnings are informational */\n severity: SpecIssueSeverity;\n /** Human-readable description of the issue */\n message: string;\n /** The element key where the issue was found (if applicable) */\n elementKey?: string;\n /** Machine-readable issue code for programmatic handling */\n code:\n | \"missing_root\"\n | \"root_not_found\"\n | \"missing_child\"\n | \"visible_in_props\"\n | \"orphaned_element\"\n | \"empty_spec\"\n | \"on_in_props\"\n | \"repeat_in_props\"\n | \"watch_in_props\";\n}\n\n/**\n * Result of spec structural validation.\n */\nexport interface SpecValidationIssues {\n /** Whether the spec passed validation (no errors; warnings are OK) */\n valid: boolean;\n /** List of issues found */\n issues: SpecIssue[];\n}\n\n/**\n * Options for validateSpec.\n */\nexport interface ValidateSpecOptions {\n /**\n * Whether to check for orphaned elements (elements not reachable from root).\n * Defaults to false since orphans are harmless (just unused).\n */\n checkOrphans?: boolean;\n}\n\n/**\n * Validate a spec for structural integrity.\n *\n * Checks for common AI-generation errors:\n * - Missing or empty root\n * - Root element not found in elements map\n * - Children referencing non-existent elements\n * - `visible` placed inside `props` instead of on the element\n * - Orphaned elements (optional)\n *\n * @example\n * ```ts\n * const result = validateSpec(spec);\n * if (!result.valid) {\n * console.log(\"Spec errors:\", result.issues);\n * }\n * ```\n */\nexport function validateSpec(\n spec: Spec,\n options: ValidateSpecOptions = {},\n): SpecValidationIssues {\n const { checkOrphans = false } = options;\n const issues: SpecIssue[] = [];\n\n // 1. Check root\n if (!spec.root) {\n issues.push({\n severity: \"error\",\n message: \"Spec has no root element defined.\",\n code: \"missing_root\",\n });\n return { valid: false, issues };\n }\n\n if (!spec.elements[spec.root]) {\n issues.push({\n severity: \"error\",\n message: `Root element \"${spec.root}\" not found in elements map.`,\n code: \"root_not_found\",\n });\n }\n\n // 2. Check for empty spec\n if (Object.keys(spec.elements).length === 0) {\n issues.push({\n severity: \"error\",\n message: \"Spec has no elements.\",\n code: \"empty_spec\",\n });\n return { valid: false, issues };\n }\n\n // 3. Check each element\n for (const [key, element] of Object.entries(spec.elements)) {\n // 3a. Missing children\n if (element.children) {\n for (const childKey of element.children) {\n if (!spec.elements[childKey]) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" references child \"${childKey}\" which does not exist in the elements map.`,\n elementKey: key,\n code: \"missing_child\",\n });\n }\n }\n }\n\n // 3b. `visible` inside props\n const props = element.props as Record<string, unknown> | undefined;\n if (props && \"visible\" in props && props.visible !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"visible\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"visible_in_props\",\n });\n }\n\n // 3c. `on` inside props (should be a top-level field)\n if (props && \"on\" in props && props.on !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"on\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"on_in_props\",\n });\n }\n\n // 3d. `repeat` inside props (should be a top-level field)\n if (props && \"repeat\" in props && props.repeat !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"repeat\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"repeat_in_props\",\n });\n }\n\n // 3e. `watch` inside props (should be a top-level field)\n if (props && \"watch\" in props && props.watch !== undefined) {\n issues.push({\n severity: \"error\",\n message: `Element \"${key}\" has \"watch\" inside \"props\". It should be a top-level field on the element (sibling of type/props/children).`,\n elementKey: key,\n code: \"watch_in_props\",\n });\n }\n }\n\n // 4. Orphaned elements (optional)\n if (checkOrphans) {\n const reachable = new Set<string>();\n const walk = (key: string) => {\n if (reachable.has(key)) return;\n reachable.add(key);\n const el = spec.elements[key];\n if (el?.children) {\n for (const childKey of el.children) {\n if (spec.elements[childKey]) {\n walk(childKey);\n }\n }\n }\n };\n if (spec.elements[spec.root]) {\n walk(spec.root);\n }\n\n for (const key of Object.keys(spec.elements)) {\n if (!reachable.has(key)) {\n issues.push({\n severity: \"warning\",\n message: `Element \"${key}\" is not reachable from root \"${spec.root}\".`,\n elementKey: key,\n code: \"orphaned_element\",\n });\n }\n }\n }\n\n const hasErrors = issues.some((i) => i.severity === \"error\");\n return { valid: !hasErrors, issues };\n}\n\n/**\n * Auto-fix common spec issues in-place and return a corrected copy.\n *\n * Currently fixes:\n * - `visible` inside `props` → moved to element level\n * - `on` inside `props` → moved to element level\n * - `repeat` inside `props` → moved to element level\n *\n * Returns the fixed spec and a list of fixes applied.\n */\nexport function autoFixSpec(spec: Spec): {\n spec: Spec;\n fixes: string[];\n} {\n const fixes: string[] = [];\n const fixedElements: Record<string, UIElement> = {};\n\n for (const [key, element] of Object.entries(spec.elements)) {\n const props = element.props as Record<string, unknown> | undefined;\n let fixed = element;\n\n if (props && \"visible\" in props && props.visible !== undefined) {\n // Move visible from props to element level\n const { visible, ...restProps } = fixed.props as Record<string, unknown>;\n fixed = {\n ...fixed,\n props: restProps,\n visible: visible as UIElement[\"visible\"],\n };\n fixes.push(`Moved \"visible\" from props to element level on \"${key}\".`);\n }\n\n let currentProps = fixed.props as Record<string, unknown> | undefined;\n if (currentProps && \"on\" in currentProps && currentProps.on !== undefined) {\n // Move on from props to element level\n const { on, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n on: on as UIElement[\"on\"],\n };\n fixes.push(`Moved \"on\" from props to element level on \"${key}\".`);\n }\n\n currentProps = fixed.props as Record<string, unknown> | undefined;\n if (\n currentProps &&\n \"repeat\" in currentProps &&\n currentProps.repeat !== undefined\n ) {\n // Move repeat from props to element level\n const { repeat, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n repeat: repeat as UIElement[\"repeat\"],\n };\n fixes.push(`Moved \"repeat\" from props to element level on \"${key}\".`);\n }\n\n currentProps = fixed.props as Record<string, unknown> | undefined;\n if (\n currentProps &&\n \"watch\" in currentProps &&\n currentProps.watch !== undefined\n ) {\n const { watch, ...restProps } = currentProps;\n fixed = {\n ...fixed,\n props: restProps,\n watch: watch as UIElement[\"watch\"],\n };\n fixes.push(`Moved \"watch\" from props to element level on \"${key}\".`);\n }\n\n fixedElements[key] = fixed;\n }\n\n return {\n spec: { root: spec.root, elements: fixedElements, state: spec.state },\n fixes,\n };\n}\n\n/**\n * Format validation issues into a human-readable string suitable for\n * inclusion in a repair prompt sent back to the AI.\n */\nexport function formatSpecIssues(issues: SpecIssue[]): string {\n const errors = issues.filter((i) => i.severity === \"error\");\n if (errors.length === 0) return \"\";\n\n const lines = [\"The generated UI spec has the following errors:\"];\n for (const issue of errors) {\n lines.push(`- ${issue.message}`);\n }\n return lines.join(\"\\n\");\n}\n","import { z } from \"zod\";\n\n/**\n * Schema builder primitives\n */\nexport interface SchemaBuilder {\n /** String type */\n string(): SchemaType<\"string\">;\n /** Number type */\n number(): SchemaType<\"number\">;\n /** Boolean type */\n boolean(): SchemaType<\"boolean\">;\n /** Array of type */\n array<T extends SchemaType>(item: T): SchemaType<\"array\", T>;\n /** Object with shape */\n object<T extends Record<string, SchemaType>>(\n shape: T,\n ): SchemaType<\"object\", T>;\n /** Record/map with value type */\n record<T extends SchemaType>(value: T): SchemaType<\"record\", T>;\n /** Any type */\n any(): SchemaType<\"any\">;\n /** Placeholder for user-provided Zod schema */\n zod(): SchemaType<\"zod\">;\n /** Reference to catalog key (e.g., 'catalog.components') */\n ref(path: string): SchemaType<\"ref\", string>;\n /** Props from referenced catalog entry */\n propsOf(path: string): SchemaType<\"propsOf\", string>;\n /** Map of named entries with shared shape */\n map<T extends Record<string, SchemaType>>(\n entryShape: T,\n ): SchemaType<\"map\", T>;\n /** Optional modifier */\n optional(): { optional: true };\n}\n\n/**\n * Schema type representation\n */\nexport interface SchemaType<TKind extends string = string, TInner = unknown> {\n kind: TKind;\n inner?: TInner;\n optional?: boolean;\n}\n\n/**\n * Schema definition shape\n */\nexport interface SchemaDefinition<\n TSpec extends SchemaType = SchemaType,\n TCatalog extends SchemaType = SchemaType,\n> {\n /** What the AI-generated spec looks like */\n spec: TSpec;\n /** What the catalog must provide */\n catalog: TCatalog;\n}\n\n/**\n * Schema instance with methods\n */\nexport interface Schema<TDef extends SchemaDefinition = SchemaDefinition> {\n /** The schema definition */\n readonly definition: TDef;\n /** Custom prompt template for this schema */\n readonly promptTemplate?: PromptTemplate;\n /** Default rules baked into the schema (injected before customRules) */\n readonly defaultRules?: string[];\n /** Built-in actions always available at runtime (injected into prompts automatically) */\n readonly builtInActions?: BuiltInAction[];\n /** Create a catalog from this schema */\n createCatalog<TCatalog extends InferCatalogInput<TDef[\"catalog\"]>>(\n catalog: TCatalog,\n ): Catalog<TDef, TCatalog>;\n}\n\n/**\n * Catalog instance with methods\n */\nexport interface Catalog<\n TDef extends SchemaDefinition = SchemaDefinition,\n TCatalog = unknown,\n> {\n /** The schema this catalog is based on */\n readonly schema: Schema<TDef>;\n /** The catalog data */\n readonly data: TCatalog;\n /** Component names */\n readonly componentNames: string[];\n /** Action names */\n readonly actionNames: string[];\n /** Generate system prompt for AI */\n prompt(options?: PromptOptions): string;\n /** Export as JSON Schema for structured outputs */\n jsonSchema(): object;\n /** Validate a spec against this catalog */\n validate(spec: unknown): SpecValidationResult<InferSpec<TDef, TCatalog>>;\n /** Get the Zod schema for the spec */\n zodSchema(): z.ZodType<InferSpec<TDef, TCatalog>>;\n /** Type helper for the spec type */\n readonly _specType: InferSpec<TDef, TCatalog>;\n}\n\n/**\n * Prompt generation options\n */\nexport interface PromptOptions {\n /** Custom system message intro */\n system?: string;\n /** Additional rules to append */\n customRules?: string[];\n /**\n * Output mode for the generated prompt.\n *\n * - `\"standalone\"` (default): The LLM should output only JSONL patches (no prose).\n * - `\"inline\"`: The LLM should respond conversationally first, then output JSONL patches.\n * Includes rules about interleaving text with JSONL and not wrapping in code fences.\n *\n * @deprecated `\"generate\"` — use `\"standalone\"` instead.\n * @deprecated `\"chat\"` — use `\"inline\"` instead.\n */\n mode?: \"standalone\" | \"inline\" | \"generate\" | \"chat\";\n}\n\n/**\n * Context provided to prompt templates\n */\nexport interface PromptContext<TCatalog = unknown> {\n /** The catalog data */\n catalog: TCatalog;\n /** Component names from the catalog */\n componentNames: string[];\n /** Action names from the catalog (if any) */\n actionNames: string[];\n /** Prompt options provided by the user */\n options: PromptOptions;\n /** Helper to format a Zod type as a human-readable string */\n formatZodType: (schema: z.ZodType) => string;\n}\n\n/**\n * Prompt template function type\n */\nexport type PromptTemplate<TCatalog = unknown> = (\n context: PromptContext<TCatalog>,\n) => string;\n\n/**\n * A built-in action that is always available regardless of catalog configuration.\n * These are handled by the runtime (e.g. ActionProvider) and injected into prompts\n * automatically so the LLM knows about them.\n */\nexport interface BuiltInAction {\n /** Action name (e.g. \"setState\") */\n name: string;\n /** Human-readable description for the LLM */\n description: string;\n}\n\n/**\n * Schema options\n */\nexport interface SchemaOptions<TCatalog = unknown> {\n /** Custom prompt template for this schema */\n promptTemplate?: PromptTemplate<TCatalog>;\n /** Default rules baked into the schema (injected before customRules in prompts) */\n defaultRules?: string[];\n /**\n * Built-in actions that are always available regardless of catalog configuration.\n * These are injected into prompts automatically so the LLM knows about them,\n * but they don't require handlers in defineRegistry because the runtime\n * (e.g. ActionProvider) handles them directly.\n */\n builtInActions?: BuiltInAction[];\n}\n\n/**\n * Spec validation result\n */\nexport interface SpecValidationResult<T> {\n success: boolean;\n data?: T;\n error?: z.ZodError;\n}\n\n// =============================================================================\n// Catalog Type Inference Helpers\n// =============================================================================\n\n/**\n * Extract the components map type from a catalog\n * @example type Components = InferCatalogComponents<typeof myCatalog>;\n */\nexport type InferCatalogComponents<C extends Catalog> =\n C extends Catalog<SchemaDefinition, infer TCatalog>\n ? TCatalog extends { components: infer Comps }\n ? Comps\n : never\n : never;\n\n/**\n * Extract the actions map type from a catalog\n * @example type Actions = InferCatalogActions<typeof myCatalog>;\n */\nexport type InferCatalogActions<C extends Catalog> =\n C extends Catalog<SchemaDefinition, infer TCatalog>\n ? TCatalog extends { actions: infer Acts }\n ? Acts\n : never\n : never;\n\n/**\n * Infer component props from a catalog by component name\n * @example type ButtonProps = InferComponentProps<typeof myCatalog, 'Button'>;\n */\nexport type InferComponentProps<\n C extends Catalog,\n K extends keyof InferCatalogComponents<C>,\n> = InferCatalogComponents<C>[K] extends { props: z.ZodType<infer P> }\n ? P\n : never;\n\n/**\n * Infer action params from a catalog by action name\n * @example type ViewCustomersParams = InferActionParams<typeof myCatalog, 'viewCustomers'>;\n */\nexport type InferActionParams<\n C extends Catalog,\n K extends keyof InferCatalogActions<C>,\n> = InferCatalogActions<C>[K] extends { params: z.ZodType<infer P> }\n ? P\n : never;\n\n// =============================================================================\n// Internal Type Inference Helpers\n// =============================================================================\n\nexport type InferCatalogInput<T> =\n T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferCatalogField<Shape[K]> }\n : never;\n\ntype InferCatalogField<T> =\n T extends SchemaType<\"map\", infer EntryShape>\n ? Record<\n string,\n // Only 'props' is required, rest are optional\n InferMapEntryRequired<EntryShape> &\n Partial<InferMapEntryOptional<EntryShape>>\n >\n : T extends SchemaType<\"zod\">\n ? z.ZodType\n : T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferCatalogField<Item>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferCatalogField<Shape[K]> }\n : unknown;\n\n// Extract required fields (props is always required)\ntype InferMapEntryRequired<T> = {\n [K in keyof T as K extends \"props\" ? K : never]: InferMapEntryField<T[K]>;\n};\n\n// Extract optional fields (everything except props)\ntype InferMapEntryOptional<T> = {\n [K in keyof T as K extends \"props\" ? never : K]: InferMapEntryField<T[K]>;\n};\n\ntype InferMapEntryField<T> =\n T extends SchemaType<\"zod\">\n ? z.ZodType\n : T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferMapEntryField<Item>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? { [K in keyof Shape]: InferMapEntryField<Shape[K]> }\n : unknown;\n\n// Spec inference (simplified - will be expanded)\nexport type InferSpec<TDef extends SchemaDefinition, TCatalog> = TDef extends {\n spec: SchemaType<\"object\", infer Shape>;\n}\n ? InferSpecObject<Shape, TCatalog>\n : unknown;\n\ntype InferSpecObject<Shape, TCatalog> = {\n [K in keyof Shape]: InferSpecField<Shape[K], TCatalog>;\n};\n\ntype InferSpecField<T, TCatalog> =\n T extends SchemaType<\"string\">\n ? string\n : T extends SchemaType<\"number\">\n ? number\n : T extends SchemaType<\"boolean\">\n ? boolean\n : T extends SchemaType<\"array\", infer Item>\n ? InferSpecField<Item, TCatalog>[]\n : T extends SchemaType<\"object\", infer Shape>\n ? InferSpecObject<Shape, TCatalog>\n : T extends SchemaType<\"record\", infer Value>\n ? Record<string, InferSpecField<Value, TCatalog>>\n : T extends SchemaType<\"ref\", infer Path>\n ? InferRefType<Path, TCatalog>\n : T extends SchemaType<\"propsOf\", infer Path>\n ? InferPropsOfType<Path, TCatalog>\n : T extends SchemaType<\"any\">\n ? unknown\n : unknown;\n\ntype InferRefType<Path, TCatalog> = Path extends \"catalog.components\"\n ? TCatalog extends { components: infer C }\n ? keyof C\n : string\n : Path extends \"catalog.actions\"\n ? TCatalog extends { actions: infer A }\n ? keyof A\n : string\n : string;\n\ntype InferPropsOfType<Path, TCatalog> = Path extends \"catalog.components\"\n ? TCatalog extends { components: infer C }\n ? C extends Record<string, { props: z.ZodType<infer P> }>\n ? P\n : Record<string, unknown>\n : Record<string, unknown>\n : Record<string, unknown>;\n\n/**\n * Create the schema builder\n */\nfunction createBuilder(): SchemaBuilder {\n return {\n string: () => ({ kind: \"string\" }),\n number: () => ({ kind: \"number\" }),\n boolean: () => ({ kind: \"boolean\" }),\n array: (item) => ({ kind: \"array\", inner: item }),\n object: (shape) => ({ kind: \"object\", inner: shape }),\n record: (value) => ({ kind: \"record\", inner: value }),\n any: () => ({ kind: \"any\" }),\n zod: () => ({ kind: \"zod\" }),\n ref: (path) => ({ kind: \"ref\", inner: path }),\n propsOf: (path) => ({ kind: \"propsOf\", inner: path }),\n map: (entryShape) => ({ kind: \"map\", inner: entryShape }),\n optional: () => ({ optional: true }),\n };\n}\n\n/**\n * Define a schema using the builder pattern\n */\nexport function defineSchema<TDef extends SchemaDefinition>(\n builder: (s: SchemaBuilder) => TDef,\n options?: SchemaOptions,\n): Schema<TDef> {\n const s = createBuilder();\n const definition = builder(s);\n\n return {\n definition,\n promptTemplate: options?.promptTemplate,\n defaultRules: options?.defaultRules,\n builtInActions: options?.builtInActions,\n createCatalog<TCatalog extends InferCatalogInput<TDef[\"catalog\"]>>(\n catalog: TCatalog,\n ): Catalog<TDef, TCatalog> {\n return createCatalogFromSchema(this as Schema<TDef>, catalog);\n },\n };\n}\n\n/**\n * Create a catalog from a schema (internal)\n */\nfunction createCatalogFromSchema<TDef extends SchemaDefinition, TCatalog>(\n schema: Schema<TDef>,\n catalogData: TCatalog,\n): Catalog<TDef, TCatalog> {\n // Extract component and action names\n const components = (catalogData as Record<string, unknown>).components as\n | Record<string, unknown>\n | undefined;\n const actions = (catalogData as Record<string, unknown>).actions as\n | Record<string, unknown>\n | undefined;\n\n const componentNames = components ? Object.keys(components) : [];\n const actionNames = actions ? Object.keys(actions) : [];\n\n // Build the Zod schema for validation\n const zodSchema = buildZodSchemaFromDefinition(\n schema.definition,\n catalogData,\n );\n\n return {\n schema,\n data: catalogData,\n componentNames,\n actionNames,\n\n prompt(options: PromptOptions = {}): string {\n return generatePrompt(this, options);\n },\n\n jsonSchema(): object {\n return zodToJsonSchema(zodSchema);\n },\n\n validate(spec: unknown): SpecValidationResult<InferSpec<TDef, TCatalog>> {\n const result = zodSchema.safeParse(spec);\n if (result.success) {\n return {\n success: true,\n data: result.data as InferSpec<TDef, TCatalog>,\n };\n }\n return { success: false, error: result.error };\n },\n\n zodSchema(): z.ZodType<InferSpec<TDef, TCatalog>> {\n return zodSchema as z.ZodType<InferSpec<TDef, TCatalog>>;\n },\n\n get _specType(): InferSpec<TDef, TCatalog> {\n throw new Error(\"_specType is only for type inference\");\n },\n };\n}\n\n/**\n * Build Zod schema from schema definition\n */\nfunction buildZodSchemaFromDefinition(\n definition: SchemaDefinition,\n catalogData: unknown,\n): z.ZodType {\n return buildZodType(definition.spec, catalogData);\n}\n\nfunction buildZodType(schemaType: SchemaType, catalogData: unknown): z.ZodType {\n switch (schemaType.kind) {\n case \"string\":\n return z.string();\n case \"number\":\n return z.number();\n case \"boolean\":\n return z.boolean();\n case \"any\":\n return z.any();\n case \"array\": {\n const inner = buildZodType(schemaType.inner as SchemaType, catalogData);\n return z.array(inner);\n }\n case \"object\": {\n const shape = schemaType.inner as Record<string, SchemaType>;\n const zodShape: Record<string, z.ZodType> = {};\n for (const [key, value] of Object.entries(shape)) {\n let zodType = buildZodType(value, catalogData);\n if (value.optional) {\n zodType = zodType.optional();\n }\n zodShape[key] = zodType;\n }\n return z.object(zodShape);\n }\n case \"record\": {\n const inner = buildZodType(schemaType.inner as SchemaType, catalogData);\n return z.record(z.string(), inner);\n }\n case \"ref\": {\n // Reference to catalog key - create enum of valid keys\n const path = schemaType.inner as string;\n const keys = getKeysFromPath(path, catalogData);\n if (keys.length === 0) {\n return z.string();\n }\n if (keys.length === 1) {\n return z.literal(keys[0]!);\n }\n return z.enum(keys as [string, ...string[]]);\n }\n case \"propsOf\": {\n // Props from catalog entry - create union of all props schemas\n const path = schemaType.inner as string;\n const propsSchemas = getPropsFromPath(path, catalogData);\n if (propsSchemas.length === 0) {\n return z.record(z.string(), z.unknown());\n }\n if (propsSchemas.length === 1) {\n return propsSchemas[0]!;\n }\n // For propsOf, we need to be lenient since type determines which props apply\n return z.record(z.string(), z.unknown());\n }\n default:\n return z.unknown();\n }\n}\n\nfunction getKeysFromPath(path: string, catalogData: unknown): string[] {\n const parts = path.split(\".\");\n let current: unknown = { catalog: catalogData };\n for (const part of parts) {\n if (current && typeof current === \"object\") {\n current = (current as Record<string, unknown>)[part];\n } else {\n return [];\n }\n }\n if (current && typeof current === \"object\") {\n return Object.keys(current);\n }\n return [];\n}\n\nfunction getPropsFromPath(path: string, catalogData: unknown): z.ZodType[] {\n const parts = path.split(\".\");\n let current: unknown = { catalog: catalogData };\n for (const part of parts) {\n if (current && typeof current === \"object\") {\n current = (current as Record<string, unknown>)[part];\n } else {\n return [];\n }\n }\n if (current && typeof current === \"object\") {\n return Object.values(current as Record<string, { props?: z.ZodType }>)\n .map((entry) => entry.props)\n .filter((props): props is z.ZodType => props !== undefined);\n }\n return [];\n}\n\n/**\n * Generate system prompt from catalog\n */\nfunction generatePrompt<TDef extends SchemaDefinition, TCatalog>(\n catalog: Catalog<TDef, TCatalog>,\n options: PromptOptions,\n): string {\n // Check if schema has a custom prompt template\n if (catalog.schema.promptTemplate) {\n const context: PromptContext<TCatalog> = {\n catalog: catalog.data,\n componentNames: catalog.componentNames,\n actionNames: catalog.actionNames,\n options,\n formatZodType,\n };\n return catalog.schema.promptTemplate(context);\n }\n\n // Default JSONL element-tree format (for @json-render/react and similar)\n const {\n system = \"You are a UI generator that outputs JSON.\",\n customRules = [],\n mode: rawMode = \"standalone\",\n } = options;\n\n const mode: \"standalone\" | \"inline\" =\n rawMode === \"chat\"\n ? (console.warn(\n '[json-render] mode \"chat\" is deprecated, use \"inline\" instead',\n ),\n \"inline\")\n : rawMode === \"generate\"\n ? (console.warn(\n '[json-render] mode \"generate\" is deprecated, use \"standalone\" instead',\n ),\n \"standalone\")\n : rawMode;\n\n const lines: string[] = [];\n lines.push(system);\n lines.push(\"\");\n\n // Output format section - explain JSONL streaming patch format\n if (mode === \"inline\") {\n lines.push(\"OUTPUT FORMAT (text + JSONL, RFC 6902 JSON Patch):\");\n lines.push(\n \"You respond conversationally. When generating UI, first write a brief explanation (1-3 sentences), then output JSONL patch lines wrapped in a ```spec code fence.\",\n );\n lines.push(\n \"The JSONL lines use RFC 6902 JSON Patch operations to build a UI tree. Always wrap them in a ```spec fence block:\",\n );\n lines.push(\" ```spec\");\n lines.push(' {\"op\":\"add\",\"path\":\"/root\",\"value\":\"main\"}');\n lines.push(\n ' {\"op\":\"add\",\"path\":\"/elements/main\",\"value\":{\"type\":\"Card\",\"props\":{\"title\":\"Hello\"},\"children\":[]}}',\n );\n lines.push(\" ```\");\n lines.push(\n \"If the user's message does not require a UI (e.g. a greeting or clarifying question), respond with text only — no JSONL.\",\n );\n } else {\n lines.push(\"OUTPUT FORMAT (JSONL, RFC 6902 JSON Patch):\");\n lines.push(\n \"Output JSONL (one JSON object per line) using RFC 6902 JSON Patch operations to build a UI tree.\",\n );\n }\n lines.push(\n \"Each line is a JSON patch operation (add, remove, replace). Start with /root, then stream /elements and /state patches interleaved so the UI fills in progressively as it streams.\",\n );\n lines.push(\"\");\n lines.push(\"Example output (each line is a separate JSON object):\");\n lines.push(\"\");\n\n // Build example using actual catalog component names and props to avoid hallucinations\n const allComponents = (catalog.data as Record<string, unknown>).components as\n | Record<string, CatalogComponentDef>\n | undefined;\n const cn = catalog.componentNames;\n const comp1 = cn[0] || \"Component\";\n const comp2 = cn.length > 1 ? cn[1]! : comp1;\n const comp1Def = allComponents?.[comp1];\n const comp2Def = allComponents?.[comp2];\n const comp1Props = comp1Def ? getExampleProps(comp1Def) : {};\n const comp2Props = comp2Def ? getExampleProps(comp2Def) : {};\n\n // Find a string prop on comp2 to demonstrate $state dynamic bindings\n const dynamicPropName = comp2Def?.props\n ? findFirstStringProp(comp2Def.props)\n : null;\n const dynamicProps = dynamicPropName\n ? { ...comp2Props, [dynamicPropName]: { $item: \"title\" } }\n : comp2Props;\n\n const exampleOutput = [\n JSON.stringify({ op: \"add\", path: \"/root\", value: \"main\" }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/main\",\n value: {\n type: comp1,\n props: comp1Props,\n children: [\"child-1\", \"list\"],\n },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/child-1\",\n value: { type: comp2, props: comp2Props, children: [] },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/list\",\n value: {\n type: comp1,\n props: comp1Props,\n repeat: { statePath: \"/items\", key: \"id\" },\n children: [\"item\"],\n },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/elements/item\",\n value: { type: comp2, props: dynamicProps, children: [] },\n }),\n JSON.stringify({ op: \"add\", path: \"/state/items\", value: [] }),\n JSON.stringify({\n op: \"add\",\n path: \"/state/items/0\",\n value: { id: \"1\", title: \"First Item\" },\n }),\n JSON.stringify({\n op: \"add\",\n path: \"/state/items/1\",\n value: { id: \"2\", title: \"Second Item\" },\n }),\n ].join(\"\\n\");\n\n lines.push(`${exampleOutput}\n\nNote: state patches appear right after the elements that use them, so the UI fills in as it streams. ONLY use component types from the AVAILABLE COMPONENTS list below.`);\n lines.push(\"\");\n\n // Initial state section\n lines.push(\"INITIAL STATE:\");\n lines.push(\n \"Specs include a /state field to seed the state model. Components with { $bindState } or { $bindItem } read from and write to this state, and $state expressions read from it.\",\n );\n lines.push(\n \"CRITICAL: You MUST include state patches whenever your UI displays data via $state, $bindState, $bindItem, $item, or $index expressions, or uses repeat to iterate over arrays. Without state, these references resolve to nothing and repeat lists render zero items.\",\n );\n lines.push(\n \"Output state patches right after the elements that reference them, so the UI fills in progressively as it streams.\",\n );\n lines.push(\n \"Stream state progressively - output one patch per array item instead of one giant blob:\",\n );\n lines.push(\n ' For arrays: {\"op\":\"add\",\"path\":\"/state/posts/0\",\"value\":{\"id\":\"1\",\"title\":\"First Post\",...}} then /state/posts/1, /state/posts/2, etc.',\n );\n lines.push(\n ' For scalars: {\"op\":\"add\",\"path\":\"/state/newTodoText\",\"value\":\"\"}',\n );\n lines.push(\n ' Initialize the array first if needed: {\"op\":\"add\",\"path\":\"/state/posts\",\"value\":[]}',\n );\n lines.push(\n 'When content comes from the state model, use { \"$state\": \"/some/path\" } dynamic props to display it instead of hardcoding the same value in both state and props. The state model is the single source of truth.',\n );\n lines.push(\n \"Include realistic sample data in state. For blogs: 3-4 posts with titles, excerpts, authors, dates. For product lists: 3-5 items with names, prices, descriptions. Never leave arrays empty.\",\n );\n lines.push(\"\");\n lines.push(\"DYNAMIC LISTS (repeat field):\");\n lines.push(\n 'Any element can have a top-level \"repeat\" field to render its children once per item in a state array: { \"repeat\": { \"statePath\": \"/arrayPath\", \"key\": \"id\" } }.',\n );\n lines.push(\n 'The element itself renders once (as the container), and its children are expanded once per array item. \"statePath\" is the state array path. \"key\" is an optional field name on each item for stable React keys.',\n );\n lines.push(\n `Example: ${JSON.stringify({ type: comp1, props: comp1Props, repeat: { statePath: \"/todos\", key: \"id\" }, children: [\"todo-item\"] })}`,\n );\n lines.push(\n 'Inside children of a repeated element, use { \"$item\": \"field\" } to read a field from the current item, and { \"$index\": true } to get the current array index. For two-way binding to an item field use { \"$bindItem\": \"completed\" } on the appropriate prop.',\n );\n lines.push(\n \"ALWAYS use the repeat field for lists backed by state arrays. NEVER hardcode individual elements for each array item.\",\n );\n lines.push(\n 'IMPORTANT: \"repeat\" is a top-level field on the element (sibling of type/props/children), NOT inside props.',\n );\n lines.push(\"\");\n lines.push(\"ARRAY STATE ACTIONS:\");\n lines.push(\n 'Use action \"pushState\" to append items to arrays. Params: { statePath: \"/arrayPath\", value: { ...item }, clearStatePath: \"/inputPath\" }.',\n );\n lines.push(\n 'Values inside pushState can contain { \"$state\": \"/statePath\" } references to read current state (e.g. the text from an input field).',\n );\n lines.push(\n 'Use \"$id\" inside a pushState value to auto-generate a unique ID.',\n );\n lines.push(\n 'Example: on: { \"press\": { \"action\": \"pushState\", \"params\": { \"statePath\": \"/todos\", \"value\": { \"id\": \"$id\", \"title\": { \"$state\": \"/newTodoText\" }, \"completed\": false }, \"clearStatePath\": \"/newTodoText\" } } }',\n );\n lines.push(\n 'Use action \"removeState\" to remove items from arrays by index. Params: { statePath: \"/arrayPath\", index: N }. Inside a repeated element\\'s children, use { \"$index\": true } for the current item index. Action params support the same expressions as props: { \"$item\": \"field\" } resolves to the absolute state path, { \"$index\": true } resolves to the index number, and { \"$state\": \"/path\" } reads a value from state.',\n );\n lines.push(\n \"For lists where users can add/remove items (todos, carts, etc.), use pushState and removeState instead of hardcoding with setState.\",\n );\n lines.push(\"\");\n lines.push(\n 'IMPORTANT: State paths use RFC 6901 JSON Pointer syntax (e.g. \"/todos/0/title\"). Do NOT use JavaScript-style dot notation (e.g. \"/todos.length\" is WRONG). To generate unique IDs for new items, use \"$id\" instead of trying to read array length.',\n );\n lines.push(\"\");\n\n // Components section — reuse the typed reference from example generation\n const components = allComponents;\n\n if (components) {\n lines.push(`AVAILABLE COMPONENTS (${catalog.componentNames.length}):`);\n lines.push(\"\");\n\n for (const [name, def] of Object.entries(components)) {\n const propsStr = def.props ? formatZodType(def.props) : \"{}\";\n const hasChildren = def.slots && def.slots.length > 0;\n const childrenStr = hasChildren ? \" [accepts children]\" : \"\";\n const eventsStr =\n def.events && def.events.length > 0\n ? ` [events: ${def.events.join(\", \")}]`\n : \"\";\n const descStr = def.description ? ` - ${def.description}` : \"\";\n lines.push(`- ${name}: ${propsStr}${descStr}${childrenStr}${eventsStr}`);\n }\n lines.push(\"\");\n }\n\n // Actions section\n const actions = (catalog.data as Record<string, unknown>).actions as\n | Record<string, { params?: z.ZodType; description?: string }>\n | undefined;\n\n const builtInActions = catalog.schema.builtInActions ?? [];\n const hasCustomActions = actions && catalog.actionNames.length > 0;\n const hasBuiltInActions = builtInActions.length > 0;\n\n if (hasCustomActions || hasBuiltInActions) {\n lines.push(\"AVAILABLE ACTIONS:\");\n lines.push(\"\");\n\n // Built-in actions (handled by runtime, always available)\n for (const action of builtInActions) {\n lines.push(`- ${action.name}: ${action.description} [built-in]`);\n }\n\n // Custom actions (declared in catalog, require handlers)\n if (hasCustomActions) {\n for (const [name, def] of Object.entries(actions)) {\n lines.push(`- ${name}${def.description ? `: ${def.description}` : \"\"}`);\n }\n }\n\n lines.push(\"\");\n }\n\n // Events section\n lines.push(\"EVENTS (the `on` field):\");\n lines.push(\n \"Elements can have an optional `on` field to bind events to actions. The `on` field is a top-level field on the element (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n 'Each key in `on` is an event name (from the component\\'s supported events), and the value is an action binding: `{ \"action\": \"<actionName>\", \"params\": { ... } }`.',\n );\n lines.push(\"\");\n lines.push(\"Example:\");\n lines.push(\n ` ${JSON.stringify({ type: comp1, props: comp1Props, on: { press: { action: \"setState\", params: { statePath: \"/saved\", value: true } } }, children: [] })}`,\n );\n lines.push(\"\");\n lines.push(\n 'Action params can use dynamic references to read from state: { \"$state\": \"/statePath\" }.',\n );\n lines.push(\n \"IMPORTANT: Do NOT put action/actionParams inside props. Always use the `on` field for event bindings.\",\n );\n lines.push(\"\");\n\n // Visibility conditions\n lines.push(\"VISIBILITY CONDITIONS:\");\n lines.push(\n \"Elements can have an optional `visible` field to conditionally show/hide based on state. IMPORTANT: `visible` is a top-level field on the element object (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n `Correct: ${JSON.stringify({ type: comp1, props: comp1Props, visible: { $state: \"/activeTab\", eq: \"home\" }, children: [\"...\"] })}`,\n );\n lines.push(\n '- `{ \"$state\": \"/path\" }` - visible when state at path is truthy',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"not\": true }` - visible when state at path is falsy',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"eq\": \"value\" }` - visible when state equals value',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"neq\": \"value\" }` - visible when state does not equal value',\n );\n lines.push(\n '- `{ \"$state\": \"/path\", \"gt\": N }` / `gte` / `lt` / `lte` - numeric comparisons',\n );\n lines.push(\n \"- Use ONE operator per condition (eq, neq, gt, gte, lt, lte). Do not combine multiple operators.\",\n );\n lines.push('- Any condition can add `\"not\": true` to invert its result');\n lines.push(\n \"- `[condition, condition]` - all conditions must be true (implicit AND)\",\n );\n lines.push(\n '- `{ \"$and\": [condition, condition] }` - explicit AND (use when nesting inside $or)',\n );\n lines.push(\n '- `{ \"$or\": [condition, condition] }` - at least one must be true (OR)',\n );\n lines.push(\"- `true` / `false` - always visible/hidden\");\n lines.push(\"\");\n lines.push(\n \"Use a component with on.press bound to setState to update state and drive visibility.\",\n );\n lines.push(\n `Example: A ${comp1} with on: { \"press\": { \"action\": \"setState\", \"params\": { \"statePath\": \"/activeTab\", \"value\": \"home\" } } } sets state, then a container with visible: { \"$state\": \"/activeTab\", \"eq\": \"home\" } shows only when that tab is active.`,\n );\n lines.push(\"\");\n lines.push(\n 'For tab patterns where the first/default tab should be visible when no tab is selected yet, use $or to handle both cases: visible: { \"$or\": [{ \"$state\": \"/activeTab\", \"eq\": \"home\" }, { \"$state\": \"/activeTab\", \"not\": true }] }. This ensures the first tab is visible both when explicitly selected AND when /activeTab is not yet set.',\n );\n lines.push(\"\");\n\n // Dynamic prop expressions\n lines.push(\"DYNAMIC PROPS:\");\n lines.push(\n \"Any prop value can be a dynamic expression that resolves based on state. Three forms are supported:\",\n );\n lines.push(\"\");\n lines.push(\n '1. Read-only state: `{ \"$state\": \"/statePath\" }` - resolves to the value at that state path (one-way read).',\n );\n lines.push(\n ' Example: `\"color\": { \"$state\": \"/theme/primary\" }` reads the color from state.',\n );\n lines.push(\"\");\n lines.push(\n '2. Two-way binding: `{ \"$bindState\": \"/statePath\" }` - resolves to the value at the state path AND enables write-back. Use on form input props (value, checked, pressed, etc.).',\n );\n lines.push(\n ' Example: `\"value\": { \"$bindState\": \"/form/email\" }` binds the input value to /form/email.',\n );\n lines.push(\n ' Inside repeat scopes: `\"checked\": { \"$bindItem\": \"completed\" }` binds to the current item\\'s completed field.',\n );\n lines.push(\"\");\n lines.push(\n '3. Conditional: `{ \"$cond\": <condition>, \"$then\": <value>, \"$else\": <value> }` - evaluates the condition (same syntax as visibility conditions) and picks the matching value.',\n );\n lines.push(\n ' Example: `\"color\": { \"$cond\": { \"$state\": \"/activeTab\", \"eq\": \"home\" }, \"$then\": \"#007AFF\", \"$else\": \"#8E8E93\" }`',\n );\n lines.push(\"\");\n lines.push(\n \"Use $bindState for form inputs (text fields, checkboxes, selects, sliders, etc.) and $state for read-only data display. Inside repeat scopes, use $bindItem for form inputs bound to the current item. Use dynamic props instead of duplicating elements with opposing visible conditions when only prop values differ.\",\n );\n lines.push(\"\");\n lines.push(\n '4. Template: `{ \"$template\": \"Hello, ${/name}!\" }` - interpolates `${/path}` references in the string with values from the state model.',\n );\n lines.push(\n ' Example: `\"label\": { \"$template\": \"Items: ${/cart/count} | Total: ${/cart/total}\" }` renders \"Items: 3 | Total: 42.00\" when /cart/count is 3 and /cart/total is 42.00.',\n );\n lines.push(\"\");\n\n // $computed section — only emit when catalog defines functions\n const catalogFunctions = (catalog.data as Record<string, unknown>).functions;\n if (catalogFunctions && Object.keys(catalogFunctions).length > 0) {\n lines.push(\n '5. Computed: `{ \"$computed\": \"<functionName>\", \"args\": { \"key\": <expression> } }` - calls a registered function with resolved args and returns the result.',\n );\n lines.push(\n ' Example: `\"value\": { \"$computed\": \"fullName\", \"args\": { \"first\": { \"$state\": \"/form/firstName\" }, \"last\": { \"$state\": \"/form/lastName\" } } }`',\n );\n lines.push(\" Available functions:\");\n for (const name of Object.keys(\n catalogFunctions as Record<string, unknown>,\n )) {\n lines.push(` - ${name}`);\n }\n lines.push(\"\");\n }\n\n // Validation section — only emit when at least one component has a `checks` prop\n const hasChecksComponents = allComponents\n ? Object.entries(allComponents).some(([, def]) => {\n if (!def.props) return false;\n const formatted = formatZodType(def.props);\n return formatted.includes(\"checks\");\n })\n : false;\n\n if (hasChecksComponents) {\n lines.push(\"VALIDATION:\");\n lines.push(\n \"Form components that accept a `checks` prop support client-side validation.\",\n );\n lines.push(\n 'Each check is an object: { \"type\": \"<name>\", \"message\": \"...\", \"args\": { ... } }',\n );\n lines.push(\"\");\n lines.push(\"Built-in validation types:\");\n lines.push(\" - required — value must be non-empty\");\n lines.push(\" - email — valid email format\");\n lines.push(' - minLength — minimum string length (args: { \"min\": N })');\n lines.push(' - maxLength — maximum string length (args: { \"max\": N })');\n lines.push(' - pattern — match a regex (args: { \"pattern\": \"regex\" })');\n lines.push(' - min — minimum numeric value (args: { \"min\": N })');\n lines.push(' - max — maximum numeric value (args: { \"max\": N })');\n lines.push(\" - numeric — value must be a number\");\n lines.push(\" - url — valid URL format\");\n lines.push(\n ' - matches — must equal another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - equalTo — alias for matches (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - lessThan — value must be less than another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - greaterThan — value must be greater than another field (args: { \"other\": { \"$state\": \"/path\" } })',\n );\n lines.push(\n ' - requiredIf — required only when another field is truthy (args: { \"field\": { \"$state\": \"/path\" } })',\n );\n lines.push(\"\");\n lines.push(\"Example:\");\n lines.push(\n ' \"checks\": [{ \"type\": \"required\", \"message\": \"Email is required\" }, { \"type\": \"email\", \"message\": \"Invalid email\" }]',\n );\n lines.push(\"\");\n lines.push(\n \"IMPORTANT: When using checks, the component must also have a { $bindState } or { $bindItem } on its value/checked prop for two-way binding.\",\n );\n lines.push(\n \"Always include validation checks on form inputs for a good user experience (e.g. required, email, minLength).\",\n );\n lines.push(\"\");\n }\n\n // State watchers section — only emit when actions are available (watchers\n // trigger actions, so the section is irrelevant without them).\n if (hasCustomActions || hasBuiltInActions) {\n lines.push(\"STATE WATCHERS:\");\n lines.push(\n \"Elements can have an optional `watch` field to react to state changes and trigger actions. The `watch` field is a top-level field on the element (sibling of type/props/children), NOT inside props.\",\n );\n lines.push(\n \"Maps state paths (JSON Pointers) to action bindings. When the value at a watched path changes, the bound actions fire automatically.\",\n );\n lines.push(\"\");\n lines.push(\n \"Example (cascading select — country changes trigger city loading):\",\n );\n lines.push(\n ` ${JSON.stringify({ type: \"Select\", props: { value: { $bindState: \"/form/country\" }, options: [\"US\", \"Canada\", \"UK\"] }, watch: { \"/form/country\": { action: \"loadCities\", params: { country: { $state: \"/form/country\" } } } }, children: [] })}`,\n );\n lines.push(\"\");\n lines.push(\n \"Use `watch` for cascading dependencies where changing one field should trigger side effects (loading data, resetting dependent fields, computing derived values).\",\n );\n lines.push(\n \"IMPORTANT: `watch` is a top-level field on the element (sibling of type/props/children), NOT inside props. Watchers only fire when the value changes, not on initial render.\",\n );\n lines.push(\"\");\n }\n\n // Rules\n lines.push(\"RULES:\");\n const baseRules =\n mode === \"inline\"\n ? [\n \"When generating UI, wrap all JSONL patches in a ```spec code fence - one JSON object per line inside the fence\",\n \"Write a brief conversational response before any JSONL output\",\n 'First set root: {\"op\":\"add\",\"path\":\"/root\",\"value\":\"<root-key>\"}',\n 'Then add each element: {\"op\":\"add\",\"path\":\"/elements/<key>\",\"value\":{...}}',\n \"Output /state patches right after the elements that use them, one per array item for progressive loading. REQUIRED whenever using $state, $bindState, $bindItem, $item, $index, or repeat.\",\n \"ONLY use components listed above\",\n \"Each element value needs: type, props, children (array of child keys)\",\n \"Use unique keys for the element map entries (e.g., 'header', 'metric-1', 'chart-revenue')\",\n ]\n : [\n \"Output ONLY JSONL patches - one JSON object per line, no markdown, no code fences\",\n 'First set root: {\"op\":\"add\",\"path\":\"/root\",\"value\":\"<root-key>\"}',\n 'Then add each element: {\"op\":\"add\",\"path\":\"/elements/<key>\",\"value\":{...}}',\n \"Output /state patches right after the elements that use them, one per array item for progressive loading. REQUIRED whenever using $state, $bindState, $bindItem, $item, $index, or repeat.\",\n \"ONLY use components listed above\",\n \"Each element value needs: type, props, children (array of child keys)\",\n \"Use unique keys for the element map entries (e.g., 'header', 'metric-1', 'chart-revenue')\",\n ];\n const schemaRules = catalog.schema.defaultRules ?? [];\n const allRules = [...baseRules, ...schemaRules, ...customRules];\n allRules.forEach((rule, i) => {\n lines.push(`${i + 1}. ${rule}`);\n });\n\n return lines.join(\"\\n\");\n}\n\n// =============================================================================\n// Example Value Generation from Zod Schemas\n// =============================================================================\n\n/**\n * Component definition shape as it appears in catalog data\n */\ninterface CatalogComponentDef {\n props?: z.ZodType;\n description?: string;\n slots?: string[];\n events?: string[];\n example?: Record<string, unknown>;\n}\n\n/**\n * Get example props for a catalog component.\n * Uses the explicit `example` field if provided, otherwise generates from Zod schema.\n */\nfunction getExampleProps(def: CatalogComponentDef): Record<string, unknown> {\n if (def.example && Object.keys(def.example).length > 0) {\n return def.example;\n }\n if (def.props) {\n return generateExamplePropsFromZod(def.props);\n }\n return {};\n}\n\n/**\n * Generate example prop values from a Zod object schema.\n * Only includes required fields to keep examples concise.\n */\nfunction generateExamplePropsFromZod(\n schema: z.ZodType,\n): Record<string, unknown> {\n if (!schema || !schema._def) return {};\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n if (typeName !== \"ZodObject\" && typeName !== \"object\") return {};\n\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return {};\n\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(shape)) {\n const innerTypeName = getZodTypeName(value);\n // Skip optional props to keep examples concise\n if (\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"nullable\"\n ) {\n continue;\n }\n result[key] = generateExampleValue(value);\n }\n return result;\n}\n\n/**\n * Generate a single example value from a Zod type.\n */\nfunction generateExampleValue(schema: z.ZodType): unknown {\n if (!schema || !schema._def) return \"...\";\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n switch (typeName) {\n case \"ZodString\":\n case \"string\":\n return \"example\";\n case \"ZodNumber\":\n case \"number\":\n return 0;\n case \"ZodBoolean\":\n case \"boolean\":\n return true;\n case \"ZodLiteral\":\n case \"literal\":\n return def.value;\n case \"ZodEnum\":\n case \"enum\": {\n if (Array.isArray(def.values) && def.values.length > 0)\n return def.values[0];\n if (def.entries && typeof def.entries === \"object\") {\n const values = Object.values(def.entries as Record<string, string>);\n return values.length > 0 ? values[0] : \"example\";\n }\n return \"example\";\n }\n case \"ZodOptional\":\n case \"optional\":\n case \"ZodNullable\":\n case \"nullable\":\n case \"ZodDefault\":\n case \"default\": {\n const inner = (def.innerType as z.ZodType) ?? (def.wrapped as z.ZodType);\n return inner ? generateExampleValue(inner) : null;\n }\n case \"ZodArray\":\n case \"array\":\n return [];\n case \"ZodObject\":\n case \"object\":\n return generateExamplePropsFromZod(schema);\n case \"ZodUnion\":\n case \"union\": {\n const options = def.options as z.ZodType[] | undefined;\n return options && options.length > 0\n ? generateExampleValue(options[0]!)\n : \"...\";\n }\n default:\n return \"...\";\n }\n}\n\n/**\n * Find the name of the first required string prop in a Zod object schema.\n * Used to demonstrate $state dynamic bindings in examples.\n */\nfunction findFirstStringProp(schema?: z.ZodType): string | null {\n if (!schema || !schema._def) return null;\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n if (typeName !== \"ZodObject\" && typeName !== \"object\") return null;\n\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return null;\n\n for (const [key, value] of Object.entries(shape)) {\n const innerTypeName = getZodTypeName(value);\n // Skip optional props\n if (\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"nullable\"\n ) {\n continue;\n }\n // Unwrap to check the actual type\n if (innerTypeName === \"ZodString\" || innerTypeName === \"string\") {\n return key;\n }\n }\n return null;\n}\n\n// =============================================================================\n// Zod Introspection Helpers\n// =============================================================================\n\n/**\n * Get Zod type name from schema (handles different Zod versions)\n */\nfunction getZodTypeName(schema: z.ZodType): string {\n if (!schema || !schema._def) return \"\";\n const def = schema._def as unknown as Record<string, unknown>;\n // Zod 4+ uses _def.type, older versions use _def.typeName\n return (def.typeName as string) ?? (def.type as string) ?? \"\";\n}\n\n/**\n * Format a Zod type into a human-readable string\n */\nfunction formatZodType(schema: z.ZodType): string {\n if (!schema || !schema._def) return \"unknown\";\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = getZodTypeName(schema);\n\n switch (typeName) {\n case \"ZodString\":\n case \"string\":\n return \"string\";\n case \"ZodNumber\":\n case \"number\":\n return \"number\";\n case \"ZodBoolean\":\n case \"boolean\":\n return \"boolean\";\n case \"ZodLiteral\":\n case \"literal\":\n return JSON.stringify(def.value);\n case \"ZodEnum\":\n case \"enum\": {\n // Zod 3 uses values array, Zod 4 uses entries object\n let values: string[];\n if (Array.isArray(def.values)) {\n values = def.values as string[];\n } else if (def.entries && typeof def.entries === \"object\") {\n values = Object.values(def.entries as Record<string, string>);\n } else {\n return \"enum\";\n }\n return values.map((v) => `\"${v}\"`).join(\" | \");\n }\n case \"ZodArray\":\n case \"array\": {\n // safely resolve inner type for Zod arrays\n const inner = (\n typeof def.element === \"object\"\n ? def.element\n : typeof def.type === \"object\"\n ? def.type\n : undefined\n ) as z.ZodType | undefined;\n return inner ? `Array<${formatZodType(inner)}>` : \"Array<unknown>\";\n }\n case \"ZodObject\":\n case \"object\": {\n // Shape can be a function (Zod 3) or direct object (Zod 4)\n const shape =\n typeof def.shape === \"function\"\n ? (def.shape as () => Record<string, z.ZodType>)()\n : (def.shape as Record<string, z.ZodType>);\n if (!shape) return \"object\";\n const props = Object.entries(shape)\n .map(([key, value]) => {\n const innerTypeName = getZodTypeName(value);\n const isOptional =\n innerTypeName === \"ZodOptional\" ||\n innerTypeName === \"ZodNullable\" ||\n innerTypeName === \"optional\" ||\n innerTypeName === \"nullable\";\n return `${key}${isOptional ? \"?\" : \"\"}: ${formatZodType(value)}`;\n })\n .join(\", \");\n return `{ ${props} }`;\n }\n case \"ZodOptional\":\n case \"optional\":\n case \"ZodNullable\":\n case \"nullable\": {\n const inner = (def.innerType as z.ZodType) ?? (def.wrapped as z.ZodType);\n return inner ? formatZodType(inner) : \"unknown\";\n }\n case \"ZodUnion\":\n case \"union\": {\n const options = def.options as z.ZodType[] | undefined;\n return options\n ? options.map((opt) => formatZodType(opt)).join(\" | \")\n : \"unknown\";\n }\n default:\n return \"unknown\";\n }\n}\n\n/**\n * Convert Zod schema to JSON Schema\n */\nfunction zodToJsonSchema(schema: z.ZodType): object {\n // Simplified JSON Schema conversion\n const def = schema._def as unknown as Record<string, unknown>;\n const typeName = (def.typeName as string) ?? \"\";\n\n switch (typeName) {\n case \"ZodString\":\n return { type: \"string\" };\n case \"ZodNumber\":\n return { type: \"number\" };\n case \"ZodBoolean\":\n return { type: \"boolean\" };\n case \"ZodLiteral\":\n return { const: def.value };\n case \"ZodEnum\":\n return { enum: def.values };\n case \"ZodArray\": {\n const inner = def.type as z.ZodType | undefined;\n return {\n type: \"array\",\n items: inner ? zodToJsonSchema(inner) : {},\n };\n }\n case \"ZodObject\": {\n const shape = (def.shape as () => Record<string, z.ZodType>)?.();\n if (!shape) return { type: \"object\" };\n const properties: Record<string, object> = {};\n const required: string[] = [];\n for (const [key, value] of Object.entries(shape)) {\n properties[key] = zodToJsonSchema(value);\n const innerDef = value._def as unknown as Record<string, unknown>;\n if (\n innerDef.typeName !== \"ZodOptional\" &&\n innerDef.typeName !== \"ZodNullable\"\n ) {\n required.push(key);\n }\n }\n return {\n type: \"object\",\n properties,\n required: required.length > 0 ? required : undefined,\n additionalProperties: false,\n };\n }\n case \"ZodRecord\": {\n const valueType = def.valueType as z.ZodType | undefined;\n return {\n type: \"object\",\n additionalProperties: valueType ? zodToJsonSchema(valueType) : true,\n };\n }\n case \"ZodOptional\":\n case \"ZodNullable\": {\n const inner = def.innerType as z.ZodType | undefined;\n return inner ? zodToJsonSchema(inner) : {};\n }\n case \"ZodUnion\": {\n const options = def.options as z.ZodType[] | undefined;\n return options ? { anyOf: options.map(zodToJsonSchema) } : {};\n }\n case \"ZodAny\":\n return {};\n default:\n return {};\n }\n}\n\n/**\n * Shorthand: Define a catalog directly from a schema\n */\nexport function defineCatalog<\n TDef extends SchemaDefinition,\n TCatalog extends InferCatalogInput<TDef[\"catalog\"]>,\n>(schema: Schema<TDef>, catalog: TCatalog): Catalog<TDef, TCatalog> {\n return schema.createCatalog(catalog);\n}\n","import type { Spec } from \"./types\";\n\n/**\n * Options for building a user prompt.\n */\nexport interface UserPromptOptions {\n /** The user's text prompt */\n prompt: string;\n /** Existing spec to refine (triggers patch-only mode) */\n currentSpec?: Spec | null;\n /** Runtime state context to include */\n state?: Record<string, unknown> | null;\n /** Maximum length for the user's text prompt (applied before wrapping) */\n maxPromptLength?: number;\n}\n\n/**\n * Check whether a spec is non-empty (has a root and at least one element).\n */\nfunction isNonEmptySpec(spec: unknown): spec is Spec {\n if (!spec || typeof spec !== \"object\") return false;\n const s = spec as Record<string, unknown>;\n return (\n typeof s.root === \"string\" &&\n typeof s.elements === \"object\" &&\n s.elements !== null &&\n Object.keys(s.elements as object).length > 0\n );\n}\n\nconst PATCH_INSTRUCTIONS = `IMPORTANT: The current UI is already loaded. Output ONLY the patches needed to make the requested change:\n- To add a new element: {\"op\":\"add\",\"path\":\"/elements/new-key\",\"value\":{...}}\n- To modify an existing element: {\"op\":\"replace\",\"path\":\"/elements/existing-key\",\"value\":{...}}\n- To remove an element: {\"op\":\"remove\",\"path\":\"/elements/old-key\"}\n- To update the root: {\"op\":\"replace\",\"path\":\"/root\",\"value\":\"new-root-key\"}\n- To add children: update the parent element with new children array\n\nDO NOT output patches for elements that don't need to change. Only output what's necessary for the requested modification.`;\n\n/**\n * Build a user prompt for AI generation.\n *\n * Handles common patterns that every consuming app needs:\n * - Truncating the user's prompt to a max length\n * - Including the current spec for refinement (patch-only mode)\n * - Including runtime state context\n *\n * @example\n * ```ts\n * // Fresh generation\n * buildUserPrompt({ prompt: \"create a todo app\" })\n *\n * // Refinement with existing spec\n * buildUserPrompt({ prompt: \"add a dark mode toggle\", currentSpec: spec })\n *\n * // With state context\n * buildUserPrompt({ prompt: \"show my data\", state: { todos: [] } })\n * ```\n */\nexport function buildUserPrompt(options: UserPromptOptions): string {\n const { prompt, currentSpec, state, maxPromptLength } = options;\n\n // Sanitize and optionally truncate the user's text\n let userText = String(prompt || \"\");\n if (maxPromptLength !== undefined && maxPromptLength > 0) {\n userText = userText.slice(0, maxPromptLength);\n }\n\n // --- Refinement mode: currentSpec is provided ---\n if (isNonEmptySpec(currentSpec)) {\n const parts: string[] = [];\n\n parts.push(\n `CURRENT UI STATE (already loaded, DO NOT recreate existing elements):`,\n );\n parts.push(JSON.stringify(currentSpec, null, 2));\n parts.push(\"\");\n parts.push(`USER REQUEST: ${userText}`);\n\n // Append state context if provided\n if (state && Object.keys(state).length > 0) {\n parts.push(\"\");\n parts.push(`AVAILABLE STATE:\\n${JSON.stringify(state, null, 2)}`);\n }\n\n parts.push(\"\");\n parts.push(PATCH_INSTRUCTIONS);\n\n return parts.join(\"\\n\");\n }\n\n // --- Fresh generation mode ---\n const parts: string[] = [userText];\n\n if (state && Object.keys(state).length > 0) {\n parts.push(`\\nAVAILABLE STATE:\\n${JSON.stringify(state, null, 2)}`);\n }\n\n parts.push(\n `\\nRemember: Output /root first, then interleave /elements and /state patches so the UI fills in progressively as it streams. Output each state patch right after the elements that use it, one per array item.`,\n );\n\n return parts.join(\"\\n\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS;AAoBlB,IAAM,oBAAoB,EAAE,MAAM;AAAA,EAChC,EAAE,OAAO;AAAA,EACT,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAED,IAAM,gBAAgB;AAAA,EACpB,IAAI,EAAE,QAAQ,EAAE,SAAS;AAAA,EACzB,KAAK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