@xmachines/json-render-core 0.19.0-xm.3 → 0.20.0-xm.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.
- package/README.md +11 -2
- package/dist/{chunk-AFLK3Q4T.mjs → chunk-7V7ZCHEJ.mjs} +43 -3
- package/dist/chunk-7V7ZCHEJ.mjs.map +1 -0
- package/dist/index.d.mts +57 -6
- package/dist/index.d.ts +57 -6
- package/dist/index.js +292 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +250 -11
- package/dist/index.mjs.map +1 -1
- package/dist/{store-utils-D98Czbil.d.mts → store-utils-CGwRAVOR.d.mts} +29 -3
- package/dist/{store-utils-D98Czbil.d.ts → store-utils-CGwRAVOR.d.ts} +29 -3
- package/dist/store-utils.d.mts +1 -1
- package/dist/store-utils.d.ts +1 -1
- package/dist/store-utils.js.map +1 -1
- package/dist/store-utils.mjs +1 -1
- package/package.json +4 -4
- package/dist/chunk-AFLK3Q4T.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -230,7 +230,7 @@ Schema options:
|
|
|
230
230
|
| Export | Purpose |
|
|
231
231
|
|--------|---------|
|
|
232
232
|
| `validateSpec(spec, options?)` | Validate spec structure and return issues |
|
|
233
|
-
| `autoFixSpec(spec)` | Auto-fix common spec issues
|
|
233
|
+
| `autoFixSpec(spec, options?)` | Auto-fix common spec issues; `fixDetails` classifies each fix as lossy or lossless, `{ lossy: false }` withholds pruning |
|
|
234
234
|
| `formatSpecIssues(issues)` | Format validation issues as readable strings |
|
|
235
235
|
|
|
236
236
|
### Actions
|
|
@@ -553,7 +553,16 @@ const { valid, issues } = validateSpec(spec);
|
|
|
553
553
|
console.log(formatSpecIssues(issues));
|
|
554
554
|
|
|
555
555
|
// Auto-fix common issues (returns a corrected copy)
|
|
556
|
-
const fixed = autoFixSpec(spec);
|
|
556
|
+
const { spec: fixed, fixes, fixDetails } = autoFixSpec(spec);
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
`validateSpec` checks structure beyond the catalog schema: missing or dangling `children` and named `slots` references, malformed `visible` conditions (anything outside the documented forms evaluates to hidden at runtime, so it is rejected with code `invalid_visible`), `repeat` containers with no children (`repeat_without_children`), relative repeat paths outside an enclosing repeat (`repeat_item_outside_scope`), and `repeat.statePath` values that do not reference an array in the spec's own `state` (`repeat_state_mismatch`).
|
|
560
|
+
|
|
561
|
+
`autoFixSpec` distinguishes lossless fixes (relocating `visible`/`on`/`repeat`/`watch` out of `props`) from lossy ones (pruning `children` or named `slots` references to elements that were never defined). Each entry in `fixDetails` carries `{ message, lossy }`. Callers with a repair loop should apply lossless fixes immediately and prefer re-prompting over lossy fixes, passing `{ lossy: false }` to withhold pruning until retries are exhausted:
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
const lastAttempt = retriesUsed >= maxRetries;
|
|
565
|
+
const { spec: fixed, fixDetails } = autoFixSpec(spec, { lossy: lastAttempt });
|
|
557
566
|
```
|
|
558
567
|
|
|
559
568
|
## State Watchers
|
|
@@ -56,6 +56,28 @@ function getByPath(obj, path) {
|
|
|
56
56
|
}
|
|
57
57
|
return current;
|
|
58
58
|
}
|
|
59
|
+
function resolveRepeatStatePath(statePath, repeatBasePath) {
|
|
60
|
+
if (typeof statePath === "string") {
|
|
61
|
+
return statePath;
|
|
62
|
+
}
|
|
63
|
+
if (repeatBasePath == null) {
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
if (statePath.$item === "" || statePath.$item === "/") {
|
|
67
|
+
return repeatBasePath;
|
|
68
|
+
}
|
|
69
|
+
return joinStatePath(repeatBasePath, statePath.$item);
|
|
70
|
+
}
|
|
71
|
+
function resolveRepeatItemStatePath(statePath, index) {
|
|
72
|
+
return joinStatePath(statePath, String(index));
|
|
73
|
+
}
|
|
74
|
+
function joinStatePath(basePath, childPath) {
|
|
75
|
+
const child = childPath.startsWith("/") ? childPath.slice(1) : childPath;
|
|
76
|
+
if (basePath === "" || basePath === "/") {
|
|
77
|
+
return `/${child}`;
|
|
78
|
+
}
|
|
79
|
+
return `${basePath}/${child}`;
|
|
80
|
+
}
|
|
59
81
|
function isNumericIndex(str) {
|
|
60
82
|
return /^\d+$/.test(str);
|
|
61
83
|
}
|
|
@@ -259,7 +281,13 @@ function nestedToFlat(nested) {
|
|
|
259
281
|
let counter = 0;
|
|
260
282
|
function walk(node) {
|
|
261
283
|
const key = `el-${counter++}`;
|
|
262
|
-
const {
|
|
284
|
+
const {
|
|
285
|
+
type,
|
|
286
|
+
props,
|
|
287
|
+
children: rawChildren,
|
|
288
|
+
slots: rawSlots,
|
|
289
|
+
...rest
|
|
290
|
+
} = node;
|
|
263
291
|
const childKeys = [];
|
|
264
292
|
if (Array.isArray(rawChildren)) {
|
|
265
293
|
for (const child of rawChildren) {
|
|
@@ -268,10 +296,20 @@ function nestedToFlat(nested) {
|
|
|
268
296
|
}
|
|
269
297
|
}
|
|
270
298
|
}
|
|
299
|
+
const slots = {};
|
|
300
|
+
if (rawSlots && typeof rawSlots === "object") {
|
|
301
|
+
for (const [slotName, slotChildren] of Object.entries(rawSlots)) {
|
|
302
|
+
if (!Array.isArray(slotChildren)) continue;
|
|
303
|
+
slots[slotName] = slotChildren.flatMap(
|
|
304
|
+
(child) => child && typeof child === "object" && "type" in child ? [walk(child)] : []
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
271
308
|
const element = {
|
|
272
309
|
type: type ?? "unknown",
|
|
273
310
|
props: props ?? {},
|
|
274
|
-
children: childKeys
|
|
311
|
+
children: childKeys,
|
|
312
|
+
...Object.keys(slots).length > 0 ? { slots } : {}
|
|
275
313
|
};
|
|
276
314
|
for (const [k, v] of Object.entries(rest)) {
|
|
277
315
|
if (k !== "state" && v !== void 0) {
|
|
@@ -695,6 +733,8 @@ export {
|
|
|
695
733
|
DynamicBooleanSchema,
|
|
696
734
|
resolveDynamicValue,
|
|
697
735
|
getByPath,
|
|
736
|
+
resolveRepeatStatePath,
|
|
737
|
+
resolveRepeatItemStatePath,
|
|
698
738
|
setByPath,
|
|
699
739
|
addByPath,
|
|
700
740
|
removeByPath,
|
|
@@ -715,4 +755,4 @@ export {
|
|
|
715
755
|
createStoreAdapter,
|
|
716
756
|
flattenToPointers
|
|
717
757
|
};
|
|
718
|
-
//# sourceMappingURL=chunk-
|
|
758
|
+
//# sourceMappingURL=chunk-7V7ZCHEJ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/state-store.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type { ActionBinding } from \"./actions\";\n\n/**\n * Dynamic value - can be a literal or a `{ $state }` reference to the state model.\n *\n * Used in action params and validation args where values can either be\n * hardcoded or resolved from state at runtime.\n */\nexport type DynamicValue<T = unknown> = T | { $state: string };\n\n/**\n * Dynamic string value\n */\nexport type DynamicString = DynamicValue<string>;\n\n/**\n * Dynamic number value\n */\nexport type DynamicNumber = DynamicValue<number>;\n\n/**\n * Dynamic boolean value\n */\nexport type DynamicBoolean = DynamicValue<boolean>;\n\n/**\n * Zod schema for dynamic values\n */\nexport const DynamicValueSchema = z.union([\n z.string(),\n z.number(),\n z.boolean(),\n z.null(),\n z.object({ $state: z.string() }),\n]);\n\nexport const DynamicStringSchema = z.union([\n z.string(),\n z.object({ $state: z.string() }),\n]);\n\nexport const DynamicNumberSchema = z.union([\n z.number(),\n z.object({ $state: z.string() }),\n]);\n\nexport const DynamicBooleanSchema = z.union([\n z.boolean(),\n z.object({ $state: z.string() }),\n]);\n\nexport type RepeatStatePath = string | { $item: string };\n\n/**\n * Base UI element structure for v2\n */\nexport interface UIElement<\n T extends string = string,\n P = Record<string, unknown>,\n> {\n /** Component type from the catalog */\n type: T;\n /** Component props */\n props: P;\n /** Child element keys (flat structure) */\n children?: string[];\n slots?: Record<string, string[]>;\n /** Visibility condition */\n visible?: VisibilityCondition;\n /** Event bindings — maps event names to action bindings */\n on?: Record<string, ActionBinding | ActionBinding[]>;\n /** Repeat children once per item in a state array */\n repeat?: { statePath: RepeatStatePath; key?: string };\n /**\n * State watchers — maps JSON Pointer state paths to action bindings.\n * When the value at a watched path changes, the bound actions fire.\n * Useful for cascading dependencies (e.g. country → city option loading).\n */\n watch?: Record<string, ActionBinding | ActionBinding[]>;\n}\n\n/**\n * Element with key and parentKey for use with flatToTree.\n * When elements are in an array (not a keyed map), key and parentKey\n * are needed to establish identity and parent-child relationships.\n */\nexport interface FlatElement<\n T extends string = string,\n P = Record<string, unknown>,\n> extends UIElement<T, P> {\n /** Unique key identifying this element */\n key: string;\n /** Parent element key (null for root) */\n parentKey?: string | null;\n}\n\n/**\n * Shared comparison operators for visibility conditions.\n *\n * Use at most ONE comparison operator per condition. If multiple are\n * provided, only the first matching one is evaluated (precedence:\n * eq > neq > gt > gte > lt > lte). With no operator, truthiness is checked.\n *\n * `not` inverts the final result of whichever operator (or truthiness\n * check) is used.\n */\ntype ComparisonOperators = {\n eq?: unknown;\n neq?: unknown;\n gt?: number | { $state: string };\n gte?: number | { $state: string };\n lt?: number | { $state: string };\n lte?: number | { $state: string };\n not?: true;\n};\n\n/**\n * A single state-based condition.\n * Resolves `$state` to a value from the state model, then applies the operator.\n * Without an operator, checks truthiness.\n *\n * When `not` is `true`, the result of the entire condition is inverted.\n * For example `{ $state: \"/count\", gt: 5, not: true }` means \"NOT greater than 5\".\n */\nexport type StateCondition = { $state: string } & ComparisonOperators;\n\n/**\n * A condition that resolves `$item` to a field on the current repeat item.\n * Only meaningful inside a `repeat` scope.\n *\n * Use `\"\"` to reference the whole item, or `\"field\"` for a specific field.\n */\nexport type ItemCondition = { $item: string } & ComparisonOperators;\n\n/**\n * A condition that resolves `$index` to the current repeat array index.\n * Only meaningful inside a `repeat` scope.\n */\nexport type IndexCondition = { $index: true } & ComparisonOperators;\n\n/** A single visibility condition (state, item, or index). */\nexport type SingleCondition = StateCondition | ItemCondition | IndexCondition;\n\n/**\n * AND wrapper — all child conditions must be true.\n * This is the explicit form of the implicit array AND (`SingleCondition[]`).\n * Unlike the implicit form, `$and` supports nested `$or` and `$and` conditions.\n */\nexport type AndCondition = { $and: VisibilityCondition[] };\n\n/**\n * OR wrapper — at least one child condition must be true.\n */\nexport type OrCondition = { $or: VisibilityCondition[] };\n\n/**\n * Visibility condition types.\n * - `boolean` — always/never\n * - `SingleCondition` — single condition (`$state`, `$item`, or `$index`)\n * - `SingleCondition[]` — implicit AND (all must be true)\n * - `AndCondition` — `{ $and: [...] }`, explicit AND (all must be true)\n * - `OrCondition` — `{ $or: [...] }`, at least one must be true\n */\nexport type VisibilityCondition =\n | boolean\n | SingleCondition\n | SingleCondition[]\n | AndCondition\n | OrCondition;\n\n/**\n * Flat UI tree structure (optimized for LLM generation)\n */\nexport interface Spec {\n /** Root element key */\n root: string;\n /** Flat map of elements by key */\n elements: Record<string, UIElement>;\n /** Optional initial state to seed the state model.\n * Components using statePath will read from / write to this state. */\n state?: Record<string, unknown>;\n}\n\n/**\n * State model type\n */\nexport type StateModel = Record<string, unknown>;\n\n/**\n * An abstract store that owns state and notifies subscribers on change.\n *\n * Consumers can supply their own implementation (backed by Redux, Zustand,\n * XState, etc.) or use the built-in {@link createStateStore} for a simple\n * in-memory store.\n */\nexport interface StateStore {\n /** Read a value by JSON Pointer path. */\n get: (path: string) => unknown;\n /**\n * Write a value by JSON Pointer path and notify subscribers.\n * Equality is checked by reference (`===`), not deep comparison.\n * Callers must pass a new object/array reference for changes to be detected.\n */\n set: (path: string, value: unknown) => void;\n /**\n * Write multiple values at once and notify subscribers (single notification).\n * Each value is compared by reference (`===`); only paths whose value\n * actually changed are applied.\n */\n update: (updates: Record<string, unknown>) => void;\n /** Return the full state object (used by `useSyncExternalStore`). */\n getSnapshot: () => StateModel;\n /** Optional server snapshot for SSR (passed to `useSyncExternalStore`). Falls back to `getSnapshot` when omitted. */\n getServerSnapshot?: () => StateModel;\n /** Register a listener that is called on every state change. Returns an unsubscribe function. */\n subscribe: (listener: () => void) => () => void;\n}\n\n/**\n * Component schema definition using Zod\n */\nexport type ComponentSchema = z.ZodType<Record<string, unknown>>;\n\n/**\n * Validation mode for catalog validation\n */\nexport type ValidationMode = \"strict\" | \"warn\" | \"ignore\";\n\n/**\n * JSON patch operation types (RFC 6902)\n */\nexport type PatchOp = \"add\" | \"remove\" | \"replace\" | \"move\" | \"copy\" | \"test\";\n\n/**\n * JSON patch operation (RFC 6902)\n */\nexport interface JsonPatch {\n op: PatchOp;\n path: string;\n /** Required for add, replace, test */\n value?: unknown;\n /** Required for move, copy (source location) */\n from?: string;\n}\n\n/**\n * Resolve a dynamic value against a state model\n */\nexport function resolveDynamicValue<T>(\n value: DynamicValue<T>,\n stateModel: StateModel,\n): T | undefined {\n if (value === null || value === undefined) {\n return undefined;\n }\n\n if (typeof value === \"object\" && \"$state\" in value) {\n return getByPath(stateModel, (value as { $state: string }).$state) as\n | T\n | undefined;\n }\n\n return value as T;\n}\n\n/**\n * Unescape a JSON Pointer token per RFC 6901 Section 4.\n * ~1 is decoded to / and ~0 is decoded to ~ (order matters).\n */\nfunction unescapeJsonPointer(token: string): string {\n return token.replace(/~1/g, \"/\").replace(/~0/g, \"~\");\n}\n\n/**\n * Parse a JSON Pointer path into unescaped segments.\n */\nexport function parseJsonPointer(path: string): string[] {\n const raw = path.startsWith(\"/\") ? path.slice(1).split(\"/\") : path.split(\"/\");\n return raw.map(unescapeJsonPointer);\n}\n\n/**\n * Get a value from an object by JSON Pointer path (RFC 6901)\n */\nexport function getByPath(obj: unknown, path: string): unknown {\n if (!path || path === \"/\") {\n return obj;\n }\n\n const segments = parseJsonPointer(path);\n\n let current: unknown = obj;\n\n for (const segment of segments) {\n if (current === null || current === undefined) {\n return undefined;\n }\n\n if (Array.isArray(current)) {\n const index = parseInt(segment, 10);\n current = current[index];\n } else if (typeof current === \"object\") {\n current = (current as Record<string, unknown>)[segment];\n } else {\n return undefined;\n }\n }\n\n return current;\n}\n\nexport function resolveRepeatStatePath(\n statePath: RepeatStatePath,\n repeatBasePath?: string | null,\n): string | undefined {\n if (typeof statePath === \"string\") {\n return statePath;\n }\n\n if (repeatBasePath == null) {\n return undefined;\n }\n\n if (statePath.$item === \"\" || statePath.$item === \"/\") {\n return repeatBasePath;\n }\n\n return joinStatePath(repeatBasePath, statePath.$item);\n}\n\nexport function resolveRepeatItemStatePath(\n statePath: string,\n index: number,\n): string {\n return joinStatePath(statePath, String(index));\n}\n\nfunction joinStatePath(basePath: string, childPath: string): string {\n const child = childPath.startsWith(\"/\") ? childPath.slice(1) : childPath;\n if (basePath === \"\" || basePath === \"/\") {\n return `/${child}`;\n }\n return `${basePath}/${child}`;\n}\n\n/**\n * Check if a string is a numeric index\n */\nfunction isNumericIndex(str: string): boolean {\n return /^\\d+$/.test(str);\n}\n\n/**\n * Set a value in an object by JSON Pointer path (RFC 6901).\n * Automatically creates arrays when the path segment is a numeric index.\n */\nexport function setByPath(\n obj: Record<string, unknown>,\n path: string,\n value: unknown,\n): void {\n const segments = parseJsonPointer(path);\n\n if (segments.length === 0) return;\n\n let current: Record<string, unknown> | unknown[] = obj;\n\n for (let i = 0; i < segments.length - 1; i++) {\n const segment = segments[i]!;\n const nextSegment = segments[i + 1];\n const nextIsNumeric =\n nextSegment !== undefined &&\n (isNumericIndex(nextSegment) || nextSegment === \"-\");\n\n if (Array.isArray(current)) {\n const index = parseInt(segment, 10);\n if (current[index] === undefined || typeof current[index] !== \"object\") {\n current[index] = nextIsNumeric ? [] : {};\n }\n current = current[index] as Record<string, unknown> | unknown[];\n } else {\n if (!(segment in current) || typeof current[segment] !== \"object\") {\n current[segment] = nextIsNumeric ? [] : {};\n }\n current = current[segment] as Record<string, unknown> | unknown[];\n }\n }\n\n const lastSegment = segments[segments.length - 1]!;\n if (Array.isArray(current)) {\n if (lastSegment === \"-\") {\n current.push(value);\n } else {\n const index = parseInt(lastSegment, 10);\n current[index] = value;\n }\n } else {\n current[lastSegment] = value;\n }\n}\n\n/**\n * Add a value per RFC 6902 \"add\" semantics.\n * For objects: create-or-replace the member.\n * For arrays: insert before the given index, or append if \"-\".\n */\nexport function addByPath(\n obj: Record<string, unknown>,\n path: string,\n value: unknown,\n): void {\n const segments = parseJsonPointer(path);\n\n if (segments.length === 0) return;\n\n let current: Record<string, unknown> | unknown[] = obj;\n\n for (let i = 0; i < segments.length - 1; i++) {\n const segment = segments[i]!;\n const nextSegment = segments[i + 1];\n const nextIsNumeric =\n nextSegment !== undefined &&\n (isNumericIndex(nextSegment) || nextSegment === \"-\");\n\n if (Array.isArray(current)) {\n const index = parseInt(segment, 10);\n if (current[index] === undefined || typeof current[index] !== \"object\") {\n current[index] = nextIsNumeric ? [] : {};\n }\n current = current[index] as Record<string, unknown> | unknown[];\n } else {\n if (!(segment in current) || typeof current[segment] !== \"object\") {\n current[segment] = nextIsNumeric ? [] : {};\n }\n current = current[segment] as Record<string, unknown> | unknown[];\n }\n }\n\n const lastSegment = segments[segments.length - 1]!;\n if (Array.isArray(current)) {\n if (lastSegment === \"-\") {\n current.push(value);\n } else {\n const index = parseInt(lastSegment, 10);\n current.splice(index, 0, value);\n }\n } else {\n current[lastSegment] = value;\n }\n}\n\n/**\n * Remove a value per RFC 6902 \"remove\" semantics.\n * For objects: delete the property.\n * For arrays: splice out the element at the given index.\n */\nexport function removeByPath(obj: Record<string, unknown>, path: string): void {\n const segments = parseJsonPointer(path);\n\n if (segments.length === 0) return;\n\n let current: Record<string, unknown> | unknown[] = obj;\n\n for (let i = 0; i < segments.length - 1; i++) {\n const segment = segments[i]!;\n\n if (Array.isArray(current)) {\n const index = parseInt(segment, 10);\n if (current[index] === undefined || typeof current[index] !== \"object\") {\n return; // path does not exist\n }\n current = current[index] as Record<string, unknown> | unknown[];\n } else {\n if (!(segment in current) || typeof current[segment] !== \"object\") {\n return; // path does not exist\n }\n current = current[segment] as Record<string, unknown> | unknown[];\n }\n }\n\n const lastSegment = segments[segments.length - 1]!;\n if (Array.isArray(current)) {\n const index = parseInt(lastSegment, 10);\n if (index >= 0 && index < current.length) {\n current.splice(index, 1);\n }\n } else {\n delete current[lastSegment];\n }\n}\n\n/**\n * Deep equality check for RFC 6902 \"test\" operation.\n */\nfunction deepEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true;\n if (a === null || b === null) return false;\n if (typeof a !== typeof b) return false;\n if (typeof a !== \"object\") return false;\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) return false;\n if (a.length !== b.length) return false;\n return a.every((item, i) => deepEqual(item, b[i]));\n }\n\n const aObj = a as Record<string, unknown>;\n const bObj = b as Record<string, unknown>;\n const aKeys = Object.keys(aObj);\n const bKeys = Object.keys(bObj);\n\n if (aKeys.length !== bKeys.length) return false;\n return aKeys.every((key) => deepEqual(aObj[key], bObj[key]));\n}\n\n/**\n * Find a form value from params and/or state.\n * Useful in action handlers to locate form input values regardless of path format.\n *\n * Checks in order:\n * 1. Direct param key (if not a path reference)\n * 2. Param keys ending with the field name\n * 3. State keys ending with the field name (dot notation)\n * 4. State path using getByPath (slash notation)\n *\n * @example\n * // Find \"name\" from params or state\n * const name = findFormValue(\"name\", params, state);\n *\n * // Will find from: params.name, params[\"form.name\"], state[\"form.name\"], or getByPath(state, \"name\")\n */\nexport function findFormValue(\n fieldName: string,\n params?: Record<string, unknown>,\n state?: Record<string, unknown>,\n): unknown {\n // Check params first (but not if it looks like a state path reference)\n if (params?.[fieldName] !== undefined) {\n const val = params[fieldName];\n // If the value looks like a path reference (contains dots), skip it\n if (typeof val !== \"string\" || !val.includes(\".\")) {\n return val;\n }\n }\n\n // Check param keys that end with the field name\n if (params) {\n for (const key of Object.keys(params)) {\n if (key.endsWith(`.${fieldName}`)) {\n const val = params[key];\n if (typeof val !== \"string\" || !val.includes(\".\")) {\n return val;\n }\n }\n }\n }\n\n // Check state keys that end with the field name (handles any form naming)\n if (state) {\n for (const key of Object.keys(state)) {\n if (key === fieldName || key.endsWith(`.${fieldName}`)) {\n return state[key];\n }\n }\n\n // Try getByPath with the raw field name\n const val = getByPath(state, fieldName);\n if (val !== undefined) {\n return val;\n }\n }\n\n return undefined;\n}\n\n// =============================================================================\n// SpecStream - Streaming format for progressively building specs\n// =============================================================================\n\n/**\n * A SpecStream line - a single patch operation in the stream.\n */\nexport type SpecStreamLine = JsonPatch;\n\n/**\n * Parse a single SpecStream line into a patch operation.\n * Returns null if the line is invalid or empty.\n *\n * SpecStream is json-render's streaming format where each line is a JSON patch\n * operation that progressively builds up the final spec.\n */\nexport function parseSpecStreamLine(line: string): SpecStreamLine | null {\n const trimmed = line.trim();\n if (!trimmed || !trimmed.startsWith(\"{\")) return null;\n\n try {\n const patch = JSON.parse(trimmed) as SpecStreamLine;\n if (patch.op && patch.path !== undefined) {\n return patch;\n }\n return null;\n } catch {\n return null;\n }\n}\n\n/**\n * Apply a single RFC 6902 JSON Patch operation to an object.\n * Mutates the object in place.\n *\n * Supports all six RFC 6902 operations: add, remove, replace, move, copy, test.\n *\n * @throws {Error} If a \"test\" operation fails (value mismatch).\n */\nexport function applySpecStreamPatch<T extends Record<string, unknown>>(\n obj: T,\n patch: SpecStreamLine,\n): T {\n switch (patch.op) {\n case \"add\":\n addByPath(obj, patch.path, patch.value);\n break;\n case \"replace\":\n // RFC 6902: target must exist. For streaming tolerance we set regardless.\n setByPath(obj, patch.path, patch.value);\n break;\n case \"remove\":\n removeByPath(obj, patch.path);\n break;\n case \"move\": {\n if (!patch.from) break;\n const moveValue = getByPath(obj, patch.from);\n removeByPath(obj, patch.from);\n addByPath(obj, patch.path, moveValue);\n break;\n }\n case \"copy\": {\n if (!patch.from) break;\n const copyValue = getByPath(obj, patch.from);\n addByPath(obj, patch.path, copyValue);\n break;\n }\n case \"test\": {\n const actual = getByPath(obj, patch.path);\n if (!deepEqual(actual, patch.value)) {\n throw new Error(\n `Test operation failed: value at \"${patch.path}\" does not match`,\n );\n }\n break;\n }\n }\n return obj;\n}\n\n/**\n * Apply a single RFC 6902 JSON Patch operation to a Spec.\n * Mutates the spec in place and returns it.\n *\n * This is a typed convenience wrapper around `applySpecStreamPatch` that\n * accepts a `Spec` directly without requiring a cast to `Record<string, unknown>`.\n *\n * Note: This mutates the spec. For React state updates, spread the result\n * to create a new reference: `setSpec({ ...applySpecPatch(spec, patch) })`.\n *\n * @example\n * let spec: Spec = { root: \"\", elements: {} };\n * applySpecPatch(spec, { op: \"add\", path: \"/root\", value: \"main\" });\n */\nexport function applySpecPatch(spec: Spec, patch: SpecStreamLine): Spec {\n applySpecStreamPatch(spec as unknown as Record<string, unknown>, patch);\n return spec;\n}\n\n// =============================================================================\n// Nested-to-Flat Conversion\n// =============================================================================\n\n/**\n * A nested spec node. This is the tree format that humans naturally write —\n * each node has inline `children` as an array of child node objects rather\n * than string keys.\n */\ninterface NestedNode {\n type: string;\n props: Record<string, unknown>;\n children?: NestedNode[];\n slots?: Record<string, NestedNode[]>;\n /** Any other top-level fields (visible, on, repeat, etc.) */\n [key: string]: unknown;\n}\n\n/**\n * Convert a nested (tree-structured) spec into the flat `Spec` format used\n * by json-render renderers.\n *\n * In the nested format each node has inline `children` as an array of child\n * objects. This function walks the tree, assigns auto-generated keys\n * (`el-0`, `el-1`, ...), and produces a flat `{ root, elements, state }` spec.\n *\n * The top-level `state` field (if present on the root node) is hoisted to\n * `spec.state`.\n *\n * @example\n * ```ts\n * const nested = {\n * type: \"Card\",\n * props: { title: \"Hello\" },\n * children: [\n * { type: \"Text\", props: { content: \"World\" } },\n * ],\n * state: { count: 0 },\n * };\n * const spec = nestedToFlat(nested);\n * // {\n * // root: \"el-0\",\n * // elements: {\n * // \"el-0\": { type: \"Card\", props: { title: \"Hello\" }, children: [\"el-1\"] },\n * // \"el-1\": { type: \"Text\", props: { content: \"World\" }, children: [] },\n * // },\n * // state: { count: 0 },\n * // }\n * ```\n */\nexport function nestedToFlat(nested: Record<string, unknown>): Spec {\n const elements: Record<string, UIElement> = {};\n let counter = 0;\n\n function walk(node: Record<string, unknown>): string {\n const key = `el-${counter++}`;\n const {\n type,\n props,\n children: rawChildren,\n slots: rawSlots,\n ...rest\n } = node as NestedNode;\n\n // Recursively flatten children\n const childKeys: string[] = [];\n if (Array.isArray(rawChildren)) {\n for (const child of rawChildren) {\n if (child && typeof child === \"object\" && \"type\" in child) {\n childKeys.push(walk(child as Record<string, unknown>));\n }\n }\n }\n\n const slots: Record<string, string[]> = {};\n if (rawSlots && typeof rawSlots === \"object\") {\n for (const [slotName, slotChildren] of Object.entries(rawSlots)) {\n if (!Array.isArray(slotChildren)) continue;\n slots[slotName] = slotChildren.flatMap((child) =>\n child && typeof child === \"object\" && \"type\" in child\n ? [walk(child as Record<string, unknown>)]\n : [],\n );\n }\n }\n\n // Build the flat element, preserving extra fields (visible, on, repeat, etc.)\n // but excluding `state` which is hoisted to spec-level.\n const element: UIElement = {\n type: type ?? \"unknown\",\n props: (props as Record<string, unknown>) ?? {},\n children: childKeys,\n ...(Object.keys(slots).length > 0 ? { slots } : {}),\n };\n\n // Copy extra fields (visible, on, repeat) but not state\n for (const [k, v] of Object.entries(rest)) {\n if (k !== \"state\" && v !== undefined) {\n (element as unknown as Record<string, unknown>)[k] = v;\n }\n }\n\n elements[key] = element;\n return key;\n }\n\n const root = walk(nested);\n\n const spec: Spec = { root, elements };\n\n // Hoist state from root node if present\n if (\n nested.state &&\n typeof nested.state === \"object\" &&\n !Array.isArray(nested.state)\n ) {\n spec.state = nested.state as Record<string, unknown>;\n }\n\n return spec;\n}\n\n/**\n * Compile a SpecStream string into a JSON object.\n * Each line should be a patch operation.\n *\n * @example\n * const stream = `{\"op\":\"add\",\"path\":\"/name\",\"value\":\"Alice\"}\n * {\"op\":\"add\",\"path\":\"/age\",\"value\":30}`;\n * const result = compileSpecStream(stream);\n * // { name: \"Alice\", age: 30 }\n */\nexport function compileSpecStream<\n T extends Record<string, unknown> = Record<string, unknown>,\n>(stream: string, initial: T = {} as T): T {\n const lines = stream.split(\"\\n\");\n const result = { ...initial };\n\n for (const line of lines) {\n const patch = parseSpecStreamLine(line);\n if (patch) {\n applySpecStreamPatch(result, patch);\n }\n }\n\n return result as T;\n}\n\n/**\n * Streaming SpecStream compiler.\n * Useful for processing SpecStream data as it streams in from AI.\n *\n * @example\n * const compiler = createSpecStreamCompiler<MySpec>();\n *\n * // As chunks arrive:\n * const { result, newPatches } = compiler.push(chunk);\n * if (newPatches.length > 0) {\n * updateUI(result);\n * }\n *\n * // When done:\n * const finalResult = compiler.getResult();\n */\nexport interface SpecStreamCompiler<T> {\n /** Push a chunk of text. Returns the current result and any new patches applied. */\n push(chunk: string): { result: T; newPatches: SpecStreamLine[] };\n /** Get the current compiled result */\n getResult(): T;\n /** Get all patches that have been applied */\n getPatches(): SpecStreamLine[];\n /** Reset the compiler to initial state */\n reset(initial?: Partial<T>): void;\n}\n\n/**\n * Create a streaming SpecStream compiler.\n *\n * SpecStream is json-render's streaming format. AI outputs patch operations\n * line by line, and this compiler progressively builds the final spec.\n *\n * @example\n * const compiler = createSpecStreamCompiler<TimelineSpec>();\n *\n * // Process streaming response\n * const reader = response.body.getReader();\n * while (true) {\n * const { done, value } = await reader.read();\n * if (done) break;\n *\n * const { result, newPatches } = compiler.push(decoder.decode(value));\n * if (newPatches.length > 0) {\n * setSpec(result); // Update UI with partial result\n * }\n * }\n */\nexport function createSpecStreamCompiler<T = Record<string, unknown>>(\n initial: Partial<T> = {},\n): SpecStreamCompiler<T> {\n let result = { ...initial } as T;\n let buffer = \"\";\n const appliedPatches: SpecStreamLine[] = [];\n const processedLines = new Set<string>();\n\n return {\n push(chunk: string): { result: T; newPatches: SpecStreamLine[] } {\n buffer += chunk;\n const newPatches: SpecStreamLine[] = [];\n\n // Process complete lines\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop() || \"\"; // Keep incomplete line in buffer\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || processedLines.has(trimmed)) continue;\n processedLines.add(trimmed);\n\n const patch = parseSpecStreamLine(trimmed);\n if (patch) {\n applySpecStreamPatch(result as Record<string, unknown>, patch);\n appliedPatches.push(patch);\n newPatches.push(patch);\n }\n }\n\n // Return a shallow copy to trigger re-renders\n if (newPatches.length > 0) {\n result = { ...result };\n }\n\n return { result, newPatches };\n },\n\n getResult(): T {\n // Process any remaining buffer\n if (buffer.trim()) {\n const patch = parseSpecStreamLine(buffer);\n if (patch && !processedLines.has(buffer.trim())) {\n processedLines.add(buffer.trim());\n applySpecStreamPatch(result as Record<string, unknown>, patch);\n appliedPatches.push(patch);\n result = { ...result };\n }\n buffer = \"\";\n }\n return result;\n },\n\n getPatches(): SpecStreamLine[] {\n return [...appliedPatches];\n },\n\n reset(newInitial: Partial<T> = {}): void {\n result = { ...newInitial } as T;\n buffer = \"\";\n appliedPatches.length = 0;\n processedLines.clear();\n },\n };\n}\n\n// =============================================================================\n// Mixed Stream Parser — for chat + GenUI (text interleaved with JSONL patches)\n// =============================================================================\n\n/**\n * Callbacks for the mixed stream parser.\n */\nexport interface MixedStreamCallbacks {\n /** Called when a JSONL patch line is parsed */\n onPatch: (patch: SpecStreamLine) => void;\n /** Called when a text (non-JSONL) line is received */\n onText: (text: string) => void;\n}\n\n/**\n * A stateful parser for mixed streams that contain both text and JSONL patches.\n * Used in chat + GenUI scenarios where an LLM responds with conversational text\n * interleaved with json-render JSONL patch operations.\n */\nexport interface MixedStreamParser {\n /** Push a chunk of streamed data. Calls onPatch/onText for each complete line. */\n push(chunk: string): void;\n /** Flush any remaining buffered content. Call when the stream ends. */\n flush(): void;\n}\n\n/**\n * Create a parser for mixed text + JSONL streams.\n *\n * In chat + GenUI scenarios, an LLM streams a response that contains both\n * conversational text and json-render JSONL patch lines. This parser buffers\n * incoming chunks, splits them into lines, and classifies each line as either\n * a JSONL patch (via `parseSpecStreamLine`) or plain text.\n *\n * @example\n * const parser = createMixedStreamParser({\n * onText: (text) => appendToMessage(text),\n * onPatch: (patch) => applySpecPatch(spec, patch),\n * });\n *\n * // As chunks arrive from the stream:\n * for await (const chunk of stream) {\n * parser.push(chunk);\n * }\n * parser.flush();\n */\nexport function createMixedStreamParser(\n callbacks: MixedStreamCallbacks,\n): MixedStreamParser {\n let buffer = \"\";\n let inSpecFence = false;\n\n function processLine(line: string): void {\n const trimmed = line.trim();\n\n // Fence detection\n if (!inSpecFence && trimmed.startsWith(\"```spec\")) {\n inSpecFence = true;\n return;\n }\n if (inSpecFence && trimmed === \"```\") {\n inSpecFence = false;\n return;\n }\n\n if (!trimmed) return;\n\n if (inSpecFence) {\n const patch = parseSpecStreamLine(trimmed);\n if (patch) {\n callbacks.onPatch(patch);\n }\n return;\n }\n\n // Outside fence: heuristic mode\n const patch = parseSpecStreamLine(trimmed);\n if (patch) {\n callbacks.onPatch(patch);\n } else {\n callbacks.onText(line);\n }\n }\n\n return {\n push(chunk: string): void {\n buffer += chunk;\n\n // Process complete lines\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop() || \"\"; // Keep incomplete line in buffer\n\n for (const line of lines) {\n processLine(line);\n }\n },\n\n flush(): void {\n if (buffer.trim()) {\n processLine(buffer);\n }\n buffer = \"\";\n },\n };\n}\n\n// =============================================================================\n// AI SDK Stream Transform\n// =============================================================================\n\n/**\n * Minimal chunk shape compatible with the AI SDK's `UIMessageChunk`.\n *\n * Defined here so that `@xmachines/json-render-core` has no dependency on the `ai`\n * package. The discriminated union covers the three text-related chunk types\n * the transform inspects; all other chunk types pass through via the fallback.\n */\nexport type StreamChunk =\n | { type: \"text-start\"; id: string; [k: string]: unknown }\n | { type: \"text-delta\"; id: string; delta: string; [k: string]: unknown }\n | { type: \"text-end\"; id: string; [k: string]: unknown }\n | { type: string; [k: string]: unknown };\n\n/** The opening fence for a spec block (e.g. ` ```spec `). */\nconst SPEC_FENCE_OPEN = \"```spec\";\n/** The closing fence for a spec block. */\nconst SPEC_FENCE_CLOSE = \"```\";\n\n/**\n * Creates a `TransformStream` that intercepts AI SDK UI message stream chunks\n * and classifies text content as either prose or json-render JSONL patches.\n *\n * Two classification modes:\n *\n * 1. **Fence mode** (preferred): Lines between ` ```spec ` and ` ``` ` are\n * parsed as JSONL patches. Fence delimiters are swallowed (not emitted).\n * 2. **Heuristic mode** (backward compat): Outside of fences, lines starting\n * with `{` are buffered and tested with `parseSpecStreamLine`. Valid patches\n * are emitted as {@link SPEC_DATA_PART_TYPE} parts; everything else is\n * flushed as text.\n *\n * Non-text chunks (tool events, step markers, etc.) are passed through unchanged.\n *\n * @example\n * ```ts\n * import { createJsonRenderTransform } from \"@xmachines/json-render-core\";\n * import { createUIMessageStream, createUIMessageStreamResponse } from \"ai\";\n *\n * const stream = createUIMessageStream({\n * execute: async ({ writer }) => {\n * writer.merge(\n * result.toUIMessageStream().pipeThrough(createJsonRenderTransform()),\n * );\n * },\n * });\n * return createUIMessageStreamResponse({ stream });\n * ```\n */\nexport function createJsonRenderTransform(): TransformStream<\n StreamChunk,\n StreamChunk\n> {\n let lineBuffer = \"\";\n let currentTextId = \"\";\n // Whether the current incomplete line might be JSONL (starts with '{')\n let buffering = false;\n // Whether we are inside a ```spec fence\n let inSpecFence = false;\n // Whether we are currently inside a text block (between text-start/text-end).\n // Used to split text blocks around spec data so the AI SDK creates separate\n // text parts, preserving interleaving of prose and UI in message.parts.\n let inTextBlock = false;\n let textIdCounter = 0;\n\n /** Close the current text block if one is open. */\n function closeTextBlock(\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n if (inTextBlock) {\n controller.enqueue({ type: \"text-end\", id: currentTextId });\n inTextBlock = false;\n }\n }\n\n /** Ensure a text block is open, starting a new one if needed. */\n function ensureTextBlock(\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n if (!inTextBlock) {\n textIdCounter++;\n currentTextId = String(textIdCounter);\n controller.enqueue({ type: \"text-start\", id: currentTextId });\n inTextBlock = true;\n }\n }\n\n /** Emit a text-delta, opening a text block first if necessary. */\n function emitTextDelta(\n delta: string,\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n ensureTextBlock(controller);\n controller.enqueue({ type: \"text-delta\", id: currentTextId, delta });\n }\n\n function emitPatch(\n patch: SpecStreamLine,\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n closeTextBlock(controller);\n controller.enqueue({\n type: SPEC_DATA_PART_TYPE,\n data: { type: \"patch\", patch },\n });\n }\n\n function flushBuffer(\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n if (!lineBuffer) return;\n\n const trimmed = lineBuffer.trim();\n\n // Inside a fence, everything is spec data\n if (inSpecFence) {\n if (trimmed) {\n const patch = parseSpecStreamLine(trimmed);\n if (patch) emitPatch(patch, controller);\n // Non-patch lines inside the fence are silently dropped\n }\n lineBuffer = \"\";\n buffering = false;\n return;\n }\n\n if (trimmed) {\n const patch = parseSpecStreamLine(trimmed);\n if (patch) {\n emitPatch(patch, controller);\n } else {\n // Was buffered but isn't JSONL — flush as text\n emitTextDelta(lineBuffer, controller);\n }\n } else {\n // Whitespace-only buffer — forward as-is (preserves blank lines)\n emitTextDelta(lineBuffer, controller);\n }\n lineBuffer = \"\";\n buffering = false;\n }\n\n function processCompleteLine(\n line: string,\n controller: TransformStreamDefaultController<StreamChunk>,\n ) {\n const trimmed = line.trim();\n\n // --- Fence detection ---\n if (!inSpecFence && trimmed.startsWith(SPEC_FENCE_OPEN)) {\n inSpecFence = true;\n return; // Swallow the opening fence\n }\n if (inSpecFence && trimmed === SPEC_FENCE_CLOSE) {\n inSpecFence = false;\n return; // Swallow the closing fence\n }\n\n // Inside a fence: parse as spec data\n if (inSpecFence) {\n if (trimmed) {\n const patch = parseSpecStreamLine(trimmed);\n if (patch) emitPatch(patch, controller);\n }\n return;\n }\n\n // --- Outside fence: heuristic mode ---\n if (!trimmed) {\n // Empty line — forward for markdown paragraph breaks\n emitTextDelta(\"\\n\", controller);\n return;\n }\n\n const patch = parseSpecStreamLine(trimmed);\n if (patch) {\n emitPatch(patch, controller);\n } else {\n emitTextDelta(line + \"\\n\", controller);\n }\n }\n\n return new TransformStream<StreamChunk, StreamChunk>({\n transform(chunk, controller) {\n switch (chunk.type) {\n case \"text-start\": {\n const id = (chunk as { id: string }).id;\n const idNum = parseInt(id, 10);\n if (!isNaN(idNum) && idNum >= textIdCounter) {\n textIdCounter = idNum;\n }\n currentTextId = id;\n inTextBlock = true;\n controller.enqueue(chunk);\n break;\n }\n\n case \"text-delta\": {\n const delta = chunk as { id: string; delta: string };\n const text = delta.delta;\n\n for (let i = 0; i < text.length; i++) {\n const ch = text.charAt(i);\n\n if (ch === \"\\n\") {\n // Line complete — classify and emit\n if (buffering) {\n processCompleteLine(lineBuffer, controller);\n lineBuffer = \"\";\n buffering = false;\n } else {\n // Outside fence, emit newline; inside fence, swallow it\n if (!inSpecFence) {\n emitTextDelta(\"\\n\", controller);\n }\n }\n } else if (lineBuffer.length === 0 && !buffering) {\n // Start of a new line — decide whether to buffer or stream\n if (inSpecFence || ch === \"{\" || ch === \"`\") {\n // Buffer: inside fence (everything), or heuristic mode ({), or potential fence (`)\n buffering = true;\n lineBuffer += ch;\n } else {\n emitTextDelta(ch, controller);\n }\n } else if (buffering) {\n lineBuffer += ch;\n } else {\n emitTextDelta(ch, controller);\n }\n }\n break;\n }\n\n case \"text-end\": {\n flushBuffer(controller);\n if (inTextBlock) {\n controller.enqueue({ type: \"text-end\", id: currentTextId });\n inTextBlock = false;\n }\n break;\n }\n\n default: {\n controller.enqueue(chunk);\n break;\n }\n }\n },\n\n flush(controller) {\n flushBuffer(controller);\n closeTextBlock(controller);\n },\n });\n}\n\n/**\n * The key registered in `AppDataParts` for json-render specs.\n * The AI SDK automatically prefixes this with `\"data-\"` on the wire,\n * so the actual stream chunk type is `\"data-spec\"` (see {@link SPEC_DATA_PART_TYPE}).\n *\n * @example\n * ```ts\n * import { SPEC_DATA_PART, type SpecDataPart } from \"@xmachines/json-render-core\";\n * type AppDataParts = { [SPEC_DATA_PART]: SpecDataPart };\n * ```\n */\nexport const SPEC_DATA_PART = \"spec\" as const;\n\n/**\n * The wire-format type string as it appears in stream chunks and message parts.\n * This is `\"data-\"` + {@link SPEC_DATA_PART} — i.e. `\"data-spec\"`.\n *\n * Use this constant when filtering message parts or enqueuing stream chunks.\n */\nexport const SPEC_DATA_PART_TYPE = `data-${SPEC_DATA_PART}` as const;\n\n/**\n * Discriminated union for the payload of a {@link SPEC_DATA_PART_TYPE} SSE part.\n *\n * - `\"patch\"`: A single RFC 6902 JSON Patch operation (streaming, progressive UI).\n * - `\"flat\"`: A complete flat spec with `root`, `elements`, and optional `state`.\n * - `\"nested\"`: A complete nested spec (tree structure — schema depends on catalog).\n */\nexport type SpecDataPart =\n | { type: \"patch\"; patch: JsonPatch }\n | { type: \"flat\"; spec: Spec }\n | { type: \"nested\"; spec: Record<string, unknown> };\n\n/**\n * Convenience wrapper that pipes an AI SDK UI message stream through the\n * json-render transform, classifying text as prose or JSONL patches.\n *\n * Eliminates the need for manual `pipeThrough(createJsonRenderTransform())`\n * and the associated type cast.\n *\n * @example\n * ```ts\n * import { pipeJsonRender } from \"@xmachines/json-render-core\";\n *\n * const stream = createUIMessageStream({\n * execute: async ({ writer }) => {\n * writer.merge(pipeJsonRender(result.toUIMessageStream()));\n * },\n * });\n * return createUIMessageStreamResponse({ stream });\n * ```\n */\nexport function pipeJsonRender<T = StreamChunk>(\n stream: ReadableStream<T>,\n): ReadableStream<T> {\n return stream.pipeThrough(\n createJsonRenderTransform() as unknown as TransformStream<T, T>,\n );\n}\n","import {\n getByPath,\n parseJsonPointer,\n type StateModel,\n type StateStore,\n} from \"./types\";\n\n/**\n * Immutably set a value at a JSON Pointer path using structural sharing.\n * Only objects along the path are shallow-cloned; untouched branches keep\n * their original references.\n */\nexport function immutableSetByPath(\n root: StateModel,\n path: string,\n value: unknown,\n): StateModel {\n const segments = parseJsonPointer(path);\n if (segments.length === 0) return root;\n\n const result = { ...root };\n let current: Record<string, unknown> = result;\n\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i]!;\n const child = current[seg];\n if (Array.isArray(child)) {\n current[seg] = [...child];\n } else if (child !== null && typeof child === \"object\") {\n current[seg] = { ...(child as Record<string, unknown>) };\n } else {\n const nextSeg = segments[i + 1];\n current[seg] = nextSeg !== undefined && /^\\d+$/.test(nextSeg) ? [] : {};\n }\n current = current[seg] as Record<string, unknown>;\n }\n\n const lastSeg = segments[segments.length - 1]!;\n if (Array.isArray(current)) {\n if (lastSeg === \"-\") {\n (current as unknown[]).push(value);\n } else {\n (current as unknown[])[parseInt(lastSeg, 10)] = value;\n }\n } else {\n current[lastSeg] = value;\n }\n\n return result;\n}\n\n/**\n * Create a simple in-memory {@link StateStore}.\n *\n * This is the default store used by `StateProvider` when no external store is\n * provided. It mirrors the previous `useState`-based behaviour but is\n * framework-agnostic so it can also be used in tests or non-React contexts.\n */\nexport function createStateStore(initialState: StateModel = {}): StateStore {\n let state: StateModel = { ...initialState };\n const listeners = new Set<() => void>();\n\n function notify() {\n for (const listener of listeners) {\n listener();\n }\n }\n\n return {\n get(path: string): unknown {\n return getByPath(state, path);\n },\n\n set(path: string, value: unknown): void {\n if (getByPath(state, path) === value) return;\n state = immutableSetByPath(state, path, value);\n notify();\n },\n\n update(updates: Record<string, unknown>): void {\n let changed = false;\n let next = state;\n for (const [path, value] of Object.entries(updates)) {\n if (getByPath(next, path) !== value) {\n next = immutableSetByPath(next, path, value);\n changed = true;\n }\n }\n if (!changed) return;\n state = next;\n notify();\n },\n\n getSnapshot(): StateModel {\n return state;\n },\n\n getServerSnapshot(): StateModel {\n return state;\n },\n\n subscribe(listener: () => void): () => void {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n\n/**\n * Configuration for {@link createStoreAdapter}. Adapter authors supply these\n * three callbacks; everything else (get, set, update, no-op detection,\n * getServerSnapshot) is handled by the returned {@link StateStore}.\n */\nexport interface StoreAdapterConfig {\n /** Return the current state snapshot from the underlying store. */\n getSnapshot: () => StateModel;\n /** Write a new state snapshot to the underlying store. */\n setSnapshot: (next: StateModel) => void;\n /** Subscribe to changes in the underlying store. Return an unsubscribe fn. */\n subscribe: (listener: () => void) => () => void;\n}\n\n/**\n * Build a full {@link StateStore} from a minimal adapter config.\n *\n * Handles `get`, `set` (with no-op detection), `update` (batched, with no-op\n * detection), `getSnapshot`, `getServerSnapshot`, and `subscribe` -- so each\n * adapter only needs to wire its snapshot source, write API, and subscribe\n * mechanism.\n */\nexport function createStoreAdapter(config: StoreAdapterConfig): StateStore {\n return {\n get(path: string): unknown {\n return getByPath(config.getSnapshot(), path);\n },\n\n set(path: string, value: unknown): void {\n const current = config.getSnapshot();\n if (getByPath(current, path) === value) return;\n config.setSnapshot(immutableSetByPath(current, path, value));\n },\n\n update(updates: Record<string, unknown>): void {\n let next = config.getSnapshot();\n let changed = false;\n for (const [path, value] of Object.entries(updates)) {\n if (getByPath(next, path) !== value) {\n next = immutableSetByPath(next, path, value);\n changed = true;\n }\n }\n if (!changed) return;\n config.setSnapshot(next);\n },\n\n getSnapshot: config.getSnapshot,\n\n getServerSnapshot: config.getSnapshot,\n\n subscribe: config.subscribe,\n };\n}\n\nconst MAX_FLATTEN_DEPTH = 20;\n\n/**\n * Recursively flatten a plain object into a `Record<string, unknown>` keyed by\n * JSON Pointer paths. Only leaf values (non-plain-object) appear in the output.\n *\n * Includes circular reference protection and a depth cap to prevent stack\n * overflow on pathological inputs.\n *\n * ```ts\n * flattenToPointers({ user: { name: \"Alice\" }, count: 1 })\n * // => { \"/user/name\": \"Alice\", \"/count\": 1 }\n * ```\n */\nexport function flattenToPointers(\n obj: Record<string, unknown>,\n prefix = \"\",\n _depth = 0,\n _seen?: Set<object>,\n _warned?: { current: boolean },\n): Record<string, unknown> {\n const seen = _seen ?? new Set<object>();\n const warned = _warned ?? { current: false };\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n const pointer = `${prefix}/${key}`;\n if (\n _depth < MAX_FLATTEN_DEPTH &&\n value !== null &&\n typeof value === \"object\" &&\n !Array.isArray(value) &&\n Object.getPrototypeOf(value) === Object.prototype &&\n !seen.has(value)\n ) {\n seen.add(value);\n Object.assign(\n result,\n flattenToPointers(\n value as Record<string, unknown>,\n pointer,\n _depth + 1,\n seen,\n warned,\n ),\n );\n } else {\n if (\n process.env.NODE_ENV !== \"production\" &&\n !warned.current &&\n _depth >= MAX_FLATTEN_DEPTH &&\n value !== null &&\n typeof value === \"object\" &&\n !Array.isArray(value) &&\n Object.getPrototypeOf(value) === Object.prototype &&\n !seen.has(value as object)\n ) {\n warned.current = true;\n console.warn(\n `flattenToPointers: depth limit (${MAX_FLATTEN_DEPTH}) reached. Nested state beyond this depth will be treated as a leaf value.`,\n );\n }\n result[pointer] = value;\n }\n }\n return result;\n}\n"],"mappings":";AAAA,SAAS,SAAS;AA6BX,IAAM,qBAAqB,EAAE,MAAM;AAAA,EACxC,EAAE,OAAO;AAAA,EACT,EAAE,OAAO;AAAA,EACT,EAAE,QAAQ;AAAA,EACV,EAAE,KAAK;AAAA,EACP,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAEM,IAAM,sBAAsB,EAAE,MAAM;AAAA,EACzC,EAAE,OAAO;AAAA,EACT,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAEM,IAAM,sBAAsB,EAAE,MAAM;AAAA,EACzC,EAAE,OAAO;AAAA,EACT,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAEM,IAAM,uBAAuB,EAAE,MAAM;AAAA,EAC1C,EAAE,QAAQ;AAAA,EACV,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AACjC,CAAC;AAuMM,SAAS,oBACd,OACA,YACe;AACf,MAAI,UAAU,QAAQ,UAAU,QAAW;AACzC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,UAAU,YAAY,YAAY,OAAO;AAClD,WAAO,UAAU,YAAa,MAA6B,MAAM;AAAA,EAGnE;AAEA,SAAO;AACT;AAMA,SAAS,oBAAoB,OAAuB;AAClD,SAAO,MAAM,QAAQ,OAAO,GAAG,EAAE,QAAQ,OAAO,GAAG;AACrD;AAKO,SAAS,iBAAiB,MAAwB;AACvD,QAA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OAAO;AAAA,EACpB;AACF;AAEA,IAAM,oBAAoB;AAcnB,SAAS,kBACd,KACA,SAAS,IACT,SAAS,GACT,OACA,SACyB;AACzB,QAAM,OAAO,SAAS,oBAAI,IAAY;AACtC,QAAM,SAAS,WAAW,EAAE,SAAS,MAAM;AAC3C,QAAM,SAAkC,CAAC;AACzC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,UAAM,UAAU,GAAG,MAAM,IAAI,GAAG;AAChC,QACE,SAAS,qBACT,UAAU,QACV,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,KAAK,KACpB,OAAO,eAAe,KAAK,MAAM,OAAO,aACxC,CAAC,KAAK,IAAI,KAAK,GACf;AACA,WAAK,IAAI,KAAK;AACd,aAAO;AAAA,QACL;AAAA,QACA;AAAA,UACE;AAAA,UACA;AAAA,UACA,SAAS;AAAA,UACT;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF,OAAO;AACL,UACE,QAAQ,IAAI,aAAa,gBACzB,CAAC,OAAO,WACR,UAAU,qBACV,UAAU,QACV,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,KAAK,KACpB,OAAO,eAAe,KAAK,MAAM,OAAO,aACxC,CAAC,KAAK,IAAI,KAAe,GACzB;AACA,eAAO,UAAU;AACjB,gBAAQ;AAAA,UACN,mCAAmC,iBAAiB;AAAA,QACtD;AAAA,MACF;AACA,aAAO,OAAO,IAAI;AAAA,IACpB;AAAA,EACF;AACA,SAAO;AACT;","names":["patch"]}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as StateModel, V as VisibilityCondition, a as StateCondition, A as AndCondition, O as OrCondition, D as DynamicValue, b as Spec, J as JsonPatch } from './store-utils-
|
|
2
|
-
export {
|
|
1
|
+
import { S as StateModel, V as VisibilityCondition, a as StateCondition, A as AndCondition, O as OrCondition, D as DynamicValue, b as Spec, J as JsonPatch } from './store-utils-CGwRAVOR.mjs';
|
|
2
|
+
export { $ as Action, _ as ActionBinding, a7 as ActionBindingSchema, a0 as ActionConfirm, a9 as ActionConfirmSchema, a4 as ActionDefinition, a6 as ActionExecutionContext, a3 as ActionHandler, a2 as ActionOnError, ab as ActionOnErrorSchema, a1 as ActionOnSuccess, aa as ActionOnSuccessSchema, a8 as ActionSchema, C as ComponentSchema, e as DynamicBoolean, r as DynamicBooleanSchema, d as DynamicNumber, q as DynamicNumberSchema, c as DynamicString, p as DynamicStringSchema, o as DynamicValueSchema, F as FlatElement, f as IndexCondition, I as ItemCondition, M as MixedStreamCallbacks, l as MixedStreamParser, P as PatchOp, R as RepeatStatePath, a5 as ResolvedAction, W as SPEC_DATA_PART, X as SPEC_DATA_PART_TYPE, g as SingleCondition, n as SpecDataPart, k as SpecStreamCompiler, j as SpecStreamLine, h as StateStore, Y as StoreAdapterConfig, m as StreamChunk, U as UIElement, i as ValidationMode, ag as action, af as actionBinding, x as addByPath, G as applySpecPatch, E as applySpecStreamPatch, K as compileSpecStream, Q as createJsonRenderTransform, N as createMixedStreamParser, L as createSpecStreamCompiler, Z as createStateStore, ad as executeAction, z as findFormValue, t as getByPath, ae as interpolateString, H as nestedToFlat, B as parseSpecStreamLine, T as pipeJsonRender, y as removeByPath, ac as resolveAction, s as resolveDynamicValue, v as resolveRepeatItemStatePath, u as resolveRepeatStatePath, w as setByPath } from './store-utils-CGwRAVOR.mjs';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -8,6 +8,31 @@ import { z } from 'zod';
|
|
|
8
8
|
* Lazy because `OrCondition` can recursively contain `VisibilityCondition`.
|
|
9
9
|
*/
|
|
10
10
|
declare const VisibilityConditionSchema: z.ZodType<VisibilityCondition>;
|
|
11
|
+
/**
|
|
12
|
+
* Strict variant for spec validation: rejects unknown keys, so malformed
|
|
13
|
+
* conditions (e.g. mixing $state and $item in one object) are caught at
|
|
14
|
+
* validation time instead of silently evaluating to hidden at runtime.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* True when a condition references the repeat-item scope ($item or $index)
|
|
18
|
+
* anywhere in its tree. Renderers use this to apply a repeat container's own
|
|
19
|
+
* visible condition as a per-item filter instead of evaluating it (and
|
|
20
|
+
* failing) outside the repeat scope.
|
|
21
|
+
*/
|
|
22
|
+
declare function conditionUsesItemScope(condition: VisibilityCondition | undefined): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Splits a repeat container's visible condition into a container-level gate
|
|
25
|
+
* and a per-item filter. Top-level AND structures (arrays, $and) partition
|
|
26
|
+
* cleanly: conjuncts that reference $item/$index filter items, the rest gate
|
|
27
|
+
* the container. An $or that mixes scopes cannot be partitioned soundly and
|
|
28
|
+
* is applied entirely per item (state parts still evaluate correctly there;
|
|
29
|
+
* the container shell just cannot be hidden by it).
|
|
30
|
+
*/
|
|
31
|
+
declare function splitRepeatVisibility(condition: VisibilityCondition | undefined): {
|
|
32
|
+
container: VisibilityCondition | undefined;
|
|
33
|
+
itemFilter: VisibilityCondition | undefined;
|
|
34
|
+
};
|
|
35
|
+
declare const VisibilityConditionStrictSchema: z.ZodType<VisibilityCondition>;
|
|
11
36
|
/**
|
|
12
37
|
* Context for evaluating visibility conditions.
|
|
13
38
|
*
|
|
@@ -474,7 +499,7 @@ interface SpecIssue {
|
|
|
474
499
|
/** The element key where the issue was found (if applicable) */
|
|
475
500
|
elementKey?: string;
|
|
476
501
|
/** Machine-readable issue code for programmatic handling */
|
|
477
|
-
code: "missing_root" | "root_not_found" | "missing_child" | "visible_in_props" | "orphaned_element" | "empty_spec" | "on_in_props" | "repeat_in_props" | "watch_in_props";
|
|
502
|
+
code: "missing_root" | "root_not_found" | "missing_child" | "invalid_visible" | "repeat_without_children" | "repeat_item_outside_scope" | "repeat_state_mismatch" | "visible_in_props" | "orphaned_element" | "empty_spec" | "on_in_props" | "repeat_in_props" | "watch_in_props";
|
|
478
503
|
}
|
|
479
504
|
/**
|
|
480
505
|
* Result of spec structural validation.
|
|
@@ -524,9 +549,29 @@ declare function validateSpec(spec: Spec, options?: ValidateSpecOptions): SpecVa
|
|
|
524
549
|
*
|
|
525
550
|
* Returns the fixed spec and a list of fixes applied.
|
|
526
551
|
*/
|
|
527
|
-
|
|
552
|
+
interface SpecFix {
|
|
553
|
+
message: string;
|
|
554
|
+
/**
|
|
555
|
+
* Lossy fixes change what renders (e.g. pruning a dangling child
|
|
556
|
+
* reference); lossless fixes only relocate misplaced fields. Callers with a
|
|
557
|
+
* repair loop should prefer re-prompting over accepting lossy fixes, and
|
|
558
|
+
* use the lossy-fixed spec as a last resort.
|
|
559
|
+
*/
|
|
560
|
+
lossy: boolean;
|
|
561
|
+
}
|
|
562
|
+
interface AutoFixOptions {
|
|
563
|
+
/**
|
|
564
|
+
* Apply lossy fixes (content pruning). Default true. Callers with a repair
|
|
565
|
+
* loop should pass false while retries remain so the model regenerates the
|
|
566
|
+
* missing content, then true as a last resort.
|
|
567
|
+
*/
|
|
568
|
+
lossy?: boolean;
|
|
569
|
+
}
|
|
570
|
+
declare function autoFixSpec(spec: Spec, options?: AutoFixOptions): {
|
|
528
571
|
spec: Spec;
|
|
529
572
|
fixes: string[];
|
|
573
|
+
/** Structured fix records; fixes is the plain-message projection. */
|
|
574
|
+
fixDetails: SpecFix[];
|
|
530
575
|
};
|
|
531
576
|
/**
|
|
532
577
|
* Format validation issues into a human-readable string suitable for
|
|
@@ -803,7 +848,13 @@ type InferSpec<TDef extends SchemaDefinition, TCatalog> = TDef extends {
|
|
|
803
848
|
spec: SchemaType<"object", infer Shape>;
|
|
804
849
|
} ? InferSpecObject<Shape, TCatalog> : unknown;
|
|
805
850
|
type InferSpecObject<Shape, TCatalog> = {
|
|
806
|
-
[K in keyof Shape
|
|
851
|
+
[K in keyof Shape as Shape[K] extends {
|
|
852
|
+
optional: true;
|
|
853
|
+
} ? never : K]: InferSpecField<Shape[K], TCatalog>;
|
|
854
|
+
} & {
|
|
855
|
+
[K in keyof Shape as Shape[K] extends {
|
|
856
|
+
optional: true;
|
|
857
|
+
} ? K : never]?: InferSpecField<Shape[K], TCatalog>;
|
|
807
858
|
};
|
|
808
859
|
type InferSpecField<T, TCatalog> = T extends SchemaType<"string"> ? string : T extends SchemaType<"number"> ? number : T extends SchemaType<"boolean"> ? boolean : T extends SchemaType<"array", infer Item> ? InferSpecField<Item, TCatalog>[] : T extends SchemaType<"object", infer Shape> ? InferSpecObject<Shape, TCatalog> : T extends SchemaType<"record", infer Value> ? Record<string, InferSpecField<Value, TCatalog>> : T extends SchemaType<"ref", infer Path> ? InferRefType<Path, TCatalog> : T extends SchemaType<"propsOf", infer Path> ? InferPropsOfType<Path, TCatalog> : T extends SchemaType<"any"> ? unknown : unknown;
|
|
809
860
|
type InferRefType<Path, TCatalog> = Path extends "catalog.components" ? TCatalog extends {
|
|
@@ -890,4 +941,4 @@ declare function deepMergeSpec(base: Record<string, unknown>, patch: Record<stri
|
|
|
890
941
|
*/
|
|
891
942
|
declare function diffToPatches(oldObj: Record<string, unknown>, newObj: Record<string, unknown>, basePath?: string): JsonPatch[];
|
|
892
943
|
|
|
893
|
-
export { type ActionDispatchInfo, type ActionObserver, type ActionSettleInfo, AndCondition, type BuildEditUserPromptOptions, type BuiltInAction, type Catalog, type ComputedFunction, type DirectiveDefinition, type DirectiveRegistry, DynamicValue, type EditConfig, type EditMode, type InferActionParams, type InferCatalogActions, type InferCatalogComponents, type InferCatalogInput, type InferComponentProps, type InferSpec, JsonPatch, type JsonSchemaOptions, OrCondition, type PromptContext, type PromptOptions, type PromptTemplate, type PropExpression, type PropResolutionContext, type RenderErrorHandler, type Schema, type SchemaBuilder, type SchemaDefinition, type SchemaOptions, type SchemaType, Spec, type SpecIssue, type SpecIssueSeverity, type SpecValidationIssues, type SpecValidationResult, StateCondition, StateModel, type UserPromptOptions, type ValidateSpecOptions, type ValidationCheck, type ValidationCheckResult, ValidationCheckSchema, type ValidationConfig, ValidationConfigSchema, type ValidationContext, type ValidationFunction, type ValidationFunctionDefinition, type ValidationResult, VisibilityCondition, VisibilityConditionSchema, type VisibilityContext, autoFixSpec, buildEditInstructions, buildEditUserPrompt, buildUserPrompt, builtInValidationFunctions, check, createDirectiveRegistry, deepMergeSpec, defineCatalog, defineDirective, defineSchema, diffToPatches, evaluateVisibility, findDirective, formatSpecIssues, isDevtoolsActive, isNonEmptySpec, markDevtoolsActive, nextActionDispatchId, notifyActionDispatch, notifyActionSettle, registerActionObserver, resolveActionParam, resolveBindings, resolveElementProps, resolvePropValue, runValidation, runValidationCheck, subscribeDevtoolsActive, validateSpec, visibility };
|
|
944
|
+
export { type ActionDispatchInfo, type ActionObserver, type ActionSettleInfo, AndCondition, type BuildEditUserPromptOptions, type BuiltInAction, type Catalog, type ComputedFunction, type DirectiveDefinition, type DirectiveRegistry, DynamicValue, type EditConfig, type EditMode, type InferActionParams, type InferCatalogActions, type InferCatalogComponents, type InferCatalogInput, type InferComponentProps, type InferSpec, JsonPatch, type JsonSchemaOptions, OrCondition, type PromptContext, type PromptOptions, type PromptTemplate, type PropExpression, type PropResolutionContext, type RenderErrorHandler, type Schema, type SchemaBuilder, type SchemaDefinition, type SchemaOptions, type SchemaType, Spec, type SpecIssue, type SpecIssueSeverity, type SpecValidationIssues, type SpecValidationResult, StateCondition, StateModel, type UserPromptOptions, type ValidateSpecOptions, type ValidationCheck, type ValidationCheckResult, ValidationCheckSchema, type ValidationConfig, ValidationConfigSchema, type ValidationContext, type ValidationFunction, type ValidationFunctionDefinition, type ValidationResult, VisibilityCondition, VisibilityConditionSchema, VisibilityConditionStrictSchema, type VisibilityContext, autoFixSpec, buildEditInstructions, buildEditUserPrompt, buildUserPrompt, builtInValidationFunctions, check, conditionUsesItemScope, createDirectiveRegistry, deepMergeSpec, defineCatalog, defineDirective, defineSchema, diffToPatches, evaluateVisibility, findDirective, formatSpecIssues, isDevtoolsActive, isNonEmptySpec, markDevtoolsActive, nextActionDispatchId, notifyActionDispatch, notifyActionSettle, registerActionObserver, resolveActionParam, resolveBindings, resolveElementProps, resolvePropValue, runValidation, runValidationCheck, splitRepeatVisibility, subscribeDevtoolsActive, validateSpec, visibility };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as StateModel, V as VisibilityCondition, a as StateCondition, A as AndCondition, O as OrCondition, D as DynamicValue, b as Spec, J as JsonPatch } from './store-utils-
|
|
2
|
-
export {
|
|
1
|
+
import { S as StateModel, V as VisibilityCondition, a as StateCondition, A as AndCondition, O as OrCondition, D as DynamicValue, b as Spec, J as JsonPatch } from './store-utils-CGwRAVOR.js';
|
|
2
|
+
export { $ as Action, _ as ActionBinding, a7 as ActionBindingSchema, a0 as ActionConfirm, a9 as ActionConfirmSchema, a4 as ActionDefinition, a6 as ActionExecutionContext, a3 as ActionHandler, a2 as ActionOnError, ab as ActionOnErrorSchema, a1 as ActionOnSuccess, aa as ActionOnSuccessSchema, a8 as ActionSchema, C as ComponentSchema, e as DynamicBoolean, r as DynamicBooleanSchema, d as DynamicNumber, q as DynamicNumberSchema, c as DynamicString, p as DynamicStringSchema, o as DynamicValueSchema, F as FlatElement, f as IndexCondition, I as ItemCondition, M as MixedStreamCallbacks, l as MixedStreamParser, P as PatchOp, R as RepeatStatePath, a5 as ResolvedAction, W as SPEC_DATA_PART, X as SPEC_DATA_PART_TYPE, g as SingleCondition, n as SpecDataPart, k as SpecStreamCompiler, j as SpecStreamLine, h as StateStore, Y as StoreAdapterConfig, m as StreamChunk, U as UIElement, i as ValidationMode, ag as action, af as actionBinding, x as addByPath, G as applySpecPatch, E as applySpecStreamPatch, K as compileSpecStream, Q as createJsonRenderTransform, N as createMixedStreamParser, L as createSpecStreamCompiler, Z as createStateStore, ad as executeAction, z as findFormValue, t as getByPath, ae as interpolateString, H as nestedToFlat, B as parseSpecStreamLine, T as pipeJsonRender, y as removeByPath, ac as resolveAction, s as resolveDynamicValue, v as resolveRepeatItemStatePath, u as resolveRepeatStatePath, w as setByPath } from './store-utils-CGwRAVOR.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -8,6 +8,31 @@ import { z } from 'zod';
|
|
|
8
8
|
* Lazy because `OrCondition` can recursively contain `VisibilityCondition`.
|
|
9
9
|
*/
|
|
10
10
|
declare const VisibilityConditionSchema: z.ZodType<VisibilityCondition>;
|
|
11
|
+
/**
|
|
12
|
+
* Strict variant for spec validation: rejects unknown keys, so malformed
|
|
13
|
+
* conditions (e.g. mixing $state and $item in one object) are caught at
|
|
14
|
+
* validation time instead of silently evaluating to hidden at runtime.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* True when a condition references the repeat-item scope ($item or $index)
|
|
18
|
+
* anywhere in its tree. Renderers use this to apply a repeat container's own
|
|
19
|
+
* visible condition as a per-item filter instead of evaluating it (and
|
|
20
|
+
* failing) outside the repeat scope.
|
|
21
|
+
*/
|
|
22
|
+
declare function conditionUsesItemScope(condition: VisibilityCondition | undefined): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Splits a repeat container's visible condition into a container-level gate
|
|
25
|
+
* and a per-item filter. Top-level AND structures (arrays, $and) partition
|
|
26
|
+
* cleanly: conjuncts that reference $item/$index filter items, the rest gate
|
|
27
|
+
* the container. An $or that mixes scopes cannot be partitioned soundly and
|
|
28
|
+
* is applied entirely per item (state parts still evaluate correctly there;
|
|
29
|
+
* the container shell just cannot be hidden by it).
|
|
30
|
+
*/
|
|
31
|
+
declare function splitRepeatVisibility(condition: VisibilityCondition | undefined): {
|
|
32
|
+
container: VisibilityCondition | undefined;
|
|
33
|
+
itemFilter: VisibilityCondition | undefined;
|
|
34
|
+
};
|
|
35
|
+
declare const VisibilityConditionStrictSchema: z.ZodType<VisibilityCondition>;
|
|
11
36
|
/**
|
|
12
37
|
* Context for evaluating visibility conditions.
|
|
13
38
|
*
|
|
@@ -474,7 +499,7 @@ interface SpecIssue {
|
|
|
474
499
|
/** The element key where the issue was found (if applicable) */
|
|
475
500
|
elementKey?: string;
|
|
476
501
|
/** Machine-readable issue code for programmatic handling */
|
|
477
|
-
code: "missing_root" | "root_not_found" | "missing_child" | "visible_in_props" | "orphaned_element" | "empty_spec" | "on_in_props" | "repeat_in_props" | "watch_in_props";
|
|
502
|
+
code: "missing_root" | "root_not_found" | "missing_child" | "invalid_visible" | "repeat_without_children" | "repeat_item_outside_scope" | "repeat_state_mismatch" | "visible_in_props" | "orphaned_element" | "empty_spec" | "on_in_props" | "repeat_in_props" | "watch_in_props";
|
|
478
503
|
}
|
|
479
504
|
/**
|
|
480
505
|
* Result of spec structural validation.
|
|
@@ -524,9 +549,29 @@ declare function validateSpec(spec: Spec, options?: ValidateSpecOptions): SpecVa
|
|
|
524
549
|
*
|
|
525
550
|
* Returns the fixed spec and a list of fixes applied.
|
|
526
551
|
*/
|
|
527
|
-
|
|
552
|
+
interface SpecFix {
|
|
553
|
+
message: string;
|
|
554
|
+
/**
|
|
555
|
+
* Lossy fixes change what renders (e.g. pruning a dangling child
|
|
556
|
+
* reference); lossless fixes only relocate misplaced fields. Callers with a
|
|
557
|
+
* repair loop should prefer re-prompting over accepting lossy fixes, and
|
|
558
|
+
* use the lossy-fixed spec as a last resort.
|
|
559
|
+
*/
|
|
560
|
+
lossy: boolean;
|
|
561
|
+
}
|
|
562
|
+
interface AutoFixOptions {
|
|
563
|
+
/**
|
|
564
|
+
* Apply lossy fixes (content pruning). Default true. Callers with a repair
|
|
565
|
+
* loop should pass false while retries remain so the model regenerates the
|
|
566
|
+
* missing content, then true as a last resort.
|
|
567
|
+
*/
|
|
568
|
+
lossy?: boolean;
|
|
569
|
+
}
|
|
570
|
+
declare function autoFixSpec(spec: Spec, options?: AutoFixOptions): {
|
|
528
571
|
spec: Spec;
|
|
529
572
|
fixes: string[];
|
|
573
|
+
/** Structured fix records; fixes is the plain-message projection. */
|
|
574
|
+
fixDetails: SpecFix[];
|
|
530
575
|
};
|
|
531
576
|
/**
|
|
532
577
|
* Format validation issues into a human-readable string suitable for
|
|
@@ -803,7 +848,13 @@ type InferSpec<TDef extends SchemaDefinition, TCatalog> = TDef extends {
|
|
|
803
848
|
spec: SchemaType<"object", infer Shape>;
|
|
804
849
|
} ? InferSpecObject<Shape, TCatalog> : unknown;
|
|
805
850
|
type InferSpecObject<Shape, TCatalog> = {
|
|
806
|
-
[K in keyof Shape
|
|
851
|
+
[K in keyof Shape as Shape[K] extends {
|
|
852
|
+
optional: true;
|
|
853
|
+
} ? never : K]: InferSpecField<Shape[K], TCatalog>;
|
|
854
|
+
} & {
|
|
855
|
+
[K in keyof Shape as Shape[K] extends {
|
|
856
|
+
optional: true;
|
|
857
|
+
} ? K : never]?: InferSpecField<Shape[K], TCatalog>;
|
|
807
858
|
};
|
|
808
859
|
type InferSpecField<T, TCatalog> = T extends SchemaType<"string"> ? string : T extends SchemaType<"number"> ? number : T extends SchemaType<"boolean"> ? boolean : T extends SchemaType<"array", infer Item> ? InferSpecField<Item, TCatalog>[] : T extends SchemaType<"object", infer Shape> ? InferSpecObject<Shape, TCatalog> : T extends SchemaType<"record", infer Value> ? Record<string, InferSpecField<Value, TCatalog>> : T extends SchemaType<"ref", infer Path> ? InferRefType<Path, TCatalog> : T extends SchemaType<"propsOf", infer Path> ? InferPropsOfType<Path, TCatalog> : T extends SchemaType<"any"> ? unknown : unknown;
|
|
809
860
|
type InferRefType<Path, TCatalog> = Path extends "catalog.components" ? TCatalog extends {
|
|
@@ -890,4 +941,4 @@ declare function deepMergeSpec(base: Record<string, unknown>, patch: Record<stri
|
|
|
890
941
|
*/
|
|
891
942
|
declare function diffToPatches(oldObj: Record<string, unknown>, newObj: Record<string, unknown>, basePath?: string): JsonPatch[];
|
|
892
943
|
|
|
893
|
-
export { type ActionDispatchInfo, type ActionObserver, type ActionSettleInfo, AndCondition, type BuildEditUserPromptOptions, type BuiltInAction, type Catalog, type ComputedFunction, type DirectiveDefinition, type DirectiveRegistry, DynamicValue, type EditConfig, type EditMode, type InferActionParams, type InferCatalogActions, type InferCatalogComponents, type InferCatalogInput, type InferComponentProps, type InferSpec, JsonPatch, type JsonSchemaOptions, OrCondition, type PromptContext, type PromptOptions, type PromptTemplate, type PropExpression, type PropResolutionContext, type RenderErrorHandler, type Schema, type SchemaBuilder, type SchemaDefinition, type SchemaOptions, type SchemaType, Spec, type SpecIssue, type SpecIssueSeverity, type SpecValidationIssues, type SpecValidationResult, StateCondition, StateModel, type UserPromptOptions, type ValidateSpecOptions, type ValidationCheck, type ValidationCheckResult, ValidationCheckSchema, type ValidationConfig, ValidationConfigSchema, type ValidationContext, type ValidationFunction, type ValidationFunctionDefinition, type ValidationResult, VisibilityCondition, VisibilityConditionSchema, type VisibilityContext, autoFixSpec, buildEditInstructions, buildEditUserPrompt, buildUserPrompt, builtInValidationFunctions, check, createDirectiveRegistry, deepMergeSpec, defineCatalog, defineDirective, defineSchema, diffToPatches, evaluateVisibility, findDirective, formatSpecIssues, isDevtoolsActive, isNonEmptySpec, markDevtoolsActive, nextActionDispatchId, notifyActionDispatch, notifyActionSettle, registerActionObserver, resolveActionParam, resolveBindings, resolveElementProps, resolvePropValue, runValidation, runValidationCheck, subscribeDevtoolsActive, validateSpec, visibility };
|
|
944
|
+
export { type ActionDispatchInfo, type ActionObserver, type ActionSettleInfo, AndCondition, type BuildEditUserPromptOptions, type BuiltInAction, type Catalog, type ComputedFunction, type DirectiveDefinition, type DirectiveRegistry, DynamicValue, type EditConfig, type EditMode, type InferActionParams, type InferCatalogActions, type InferCatalogComponents, type InferCatalogInput, type InferComponentProps, type InferSpec, JsonPatch, type JsonSchemaOptions, OrCondition, type PromptContext, type PromptOptions, type PromptTemplate, type PropExpression, type PropResolutionContext, type RenderErrorHandler, type Schema, type SchemaBuilder, type SchemaDefinition, type SchemaOptions, type SchemaType, Spec, type SpecIssue, type SpecIssueSeverity, type SpecValidationIssues, type SpecValidationResult, StateCondition, StateModel, type UserPromptOptions, type ValidateSpecOptions, type ValidationCheck, type ValidationCheckResult, ValidationCheckSchema, type ValidationConfig, ValidationConfigSchema, type ValidationContext, type ValidationFunction, type ValidationFunctionDefinition, type ValidationResult, VisibilityCondition, VisibilityConditionSchema, VisibilityConditionStrictSchema, type VisibilityContext, autoFixSpec, buildEditInstructions, buildEditUserPrompt, buildUserPrompt, builtInValidationFunctions, check, conditionUsesItemScope, createDirectiveRegistry, deepMergeSpec, defineCatalog, defineDirective, defineSchema, diffToPatches, evaluateVisibility, findDirective, formatSpecIssues, isDevtoolsActive, isNonEmptySpec, markDevtoolsActive, nextActionDispatchId, notifyActionDispatch, notifyActionSettle, registerActionObserver, resolveActionParam, resolveBindings, resolveElementProps, resolvePropValue, runValidation, runValidationCheck, splitRepeatVisibility, subscribeDevtoolsActive, validateSpec, visibility };
|