@sparkelf/dsh-genui 0.0.0-stage → 0.11.4-patch.2

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.
Files changed (210) hide show
  1. package/CHANGELOG.md +597 -0
  2. package/LICENSE +21 -0
  3. package/README.md +292 -2
  4. package/README.zh-CN.md +307 -0
  5. package/SKILL.md +261 -0
  6. package/cordis.patch.yml +9 -0
  7. package/demo-prompts.md +118 -0
  8. package/lib/assets/echarts-core.js +43 -0
  9. package/lib/assets/echarts-full.js +72 -0
  10. package/lib/assets/mermaid.js +3591 -0
  11. package/lib/assets/standalone-runtime.js +1506 -0
  12. package/lib/assets/three.js +3844 -0
  13. package/lib/client.js +405 -0
  14. package/lib/index.js +5187 -0
  15. package/lib/invariant.js +19 -0
  16. package/lib/types/client/EChartNode.d.ts +13 -0
  17. package/lib/types/client/ErrorBoundary.d.ts +30 -0
  18. package/lib/types/client/GenuiBlock.d.ts +9 -0
  19. package/lib/types/client/PlotBlock.d.ts +44 -0
  20. package/lib/types/client/TemplateDrawer.d.ts +7 -0
  21. package/lib/types/client/achievement-store.d.ts +35 -0
  22. package/lib/types/client/achievement-toast.d.ts +2 -0
  23. package/lib/types/client/achievements.d.ts +63 -0
  24. package/lib/types/client/action-context.d.ts +7 -0
  25. package/lib/types/client/artifact/ExportableGenuiBlock.d.ts +6 -0
  26. package/lib/types/client/artifact/create.d.ts +5 -0
  27. package/lib/types/client/artifact/download.d.ts +9 -0
  28. package/lib/types/client/artifact/html.d.ts +5 -0
  29. package/lib/types/client/artifact/index.d.ts +8 -0
  30. package/lib/types/client/artifact/json.d.ts +3 -0
  31. package/lib/types/client/artifact/parse.d.ts +3 -0
  32. package/lib/types/client/artifact/portability.d.ts +4 -0
  33. package/lib/types/client/artifact/standalone-theme.d.ts +2 -0
  34. package/lib/types/client/artifact/types.d.ts +33 -0
  35. package/lib/types/client/asset-echarts-core.d.ts +1 -0
  36. package/lib/types/client/asset-echarts.d.ts +17 -0
  37. package/lib/types/client/asset-loader.d.ts +33 -0
  38. package/lib/types/client/asset-mermaid.d.ts +1 -0
  39. package/lib/types/client/asset-three.d.ts +1 -0
  40. package/lib/types/client/blocks/advanced.d.ts +101 -0
  41. package/lib/types/client/blocks/basic.d.ts +22 -0
  42. package/lib/types/client/blocks/charts.d.ts +53 -0
  43. package/lib/types/client/blocks/checkbox.d.ts +17 -0
  44. package/lib/types/client/blocks/diagram/geometry.d.ts +73 -0
  45. package/lib/types/client/blocks/diagram/index.d.ts +5 -0
  46. package/lib/types/client/blocks/diagram/layout.d.ts +41 -0
  47. package/lib/types/client/blocks/diagram/theme.d.ts +37 -0
  48. package/lib/types/client/blocks/forms.d.ts +77 -0
  49. package/lib/types/client/blocks/image.d.ts +6 -0
  50. package/lib/types/client/blocks/render-node.d.ts +10 -0
  51. package/lib/types/client/blocks/state.d.ts +63 -0
  52. package/lib/types/client/blocks/svg.d.ts +4 -0
  53. package/lib/types/client/dom-fence.d.ts +30 -0
  54. package/lib/types/client/echarts-engine.d.ts +4 -0
  55. package/lib/types/client/echarts-lazy.d.ts +18 -0
  56. package/lib/types/client/embed.d.ts +10 -0
  57. package/lib/types/client/fence-render.d.ts +106 -0
  58. package/lib/types/client/gallery.d.ts +7 -0
  59. package/lib/types/client/genui-runtime/diagnostics.d.ts +18 -0
  60. package/lib/types/client/genui-runtime/index.d.ts +1 -0
  61. package/lib/types/client/genui-runtime/limits.d.ts +54 -0
  62. package/lib/types/client/genui-runtime/normalize.d.ts +15 -0
  63. package/lib/types/client/genui-runtime/schema.d.ts +87 -0
  64. package/lib/types/client/genui-runtime/value-utils.d.ts +18 -0
  65. package/lib/types/client/guard.d.ts +144 -0
  66. package/lib/types/client/i18n/en.d.ts +307 -0
  67. package/lib/types/client/i18n/index.d.ts +44 -0
  68. package/lib/types/client/i18n/runtime.d.ts +64 -0
  69. package/lib/types/client/i18n/zh.d.ts +8 -0
  70. package/lib/types/client/index.d.ts +36 -0
  71. package/lib/types/client/inline.d.ts +5 -0
  72. package/lib/types/client/interaction-store.d.ts +48 -0
  73. package/lib/types/client/mermaid-core.d.ts +8 -0
  74. package/lib/types/client/mermaid-lazy.d.ts +9 -0
  75. package/lib/types/client/mermaid-safe.d.ts +52 -0
  76. package/lib/types/client/panel-command.d.ts +34 -0
  77. package/lib/types/client/panel-store.d.ts +94 -0
  78. package/lib/types/client/panel.d.ts +18 -0
  79. package/lib/types/client/parse-partial.d.ts +63 -0
  80. package/lib/types/client/primitive-adapter.d.ts +4 -0
  81. package/lib/types/client/primitive-labels.d.ts +18 -0
  82. package/lib/types/client/safe-math.d.ts +43 -0
  83. package/lib/types/client/scene3d-core.d.ts +21 -0
  84. package/lib/types/client/scene3d-lazy.d.ts +8 -0
  85. package/lib/types/client/session-resolver.d.ts +8 -0
  86. package/lib/types/client/source-fence.d.ts +29 -0
  87. package/lib/types/client/spec.d.ts +709 -0
  88. package/lib/types/client/standalone/asset-loader.d.ts +2 -0
  89. package/lib/types/client/standalone/bootstrap-types.d.ts +9 -0
  90. package/lib/types/client/standalone/katex-style.d.ts +1 -0
  91. package/lib/types/client/standalone/primitive-adapter.d.ts +54 -0
  92. package/lib/types/client/standalone/runtime.d.ts +2 -0
  93. package/lib/types/client/submission-registry.d.ts +51 -0
  94. package/lib/types/client/svg-fence.d.ts +2 -0
  95. package/lib/types/client/table-details.d.ts +20 -0
  96. package/lib/types/client/templates.d.ts +41 -0
  97. package/lib/types/client/toolview.d.ts +7 -0
  98. package/lib/types/client/walk-spec.d.ts +3 -0
  99. package/lib/types/index.d.ts +1 -0
  100. package/lib/types/plugin/chart-contract.d.ts +9 -0
  101. package/lib/types/plugin/fence-feedback.d.ts +151 -0
  102. package/lib/types/plugin/genui-diagnostic.d.ts +13 -0
  103. package/lib/types/plugin/index.d.ts +35 -0
  104. package/lib/types/plugin/inline-content-diagnostic.d.ts +8 -0
  105. package/lib/types/plugin/invariant.d.ts +15 -0
  106. package/lib/types/plugin/tool.d.ts +27 -0
  107. package/lib/types/shared/fence-repair.d.ts +78 -0
  108. package/lib/types/shared/fence-resolve.d.ts +39 -0
  109. package/package.json +249 -4
  110. package/src/client/EChartNode.tsx +466 -0
  111. package/src/client/ErrorBoundary.tsx +67 -0
  112. package/src/client/GenuiBlock.module.css +2221 -0
  113. package/src/client/GenuiBlock.tsx +235 -0
  114. package/src/client/PlotBlock.module.css +171 -0
  115. package/src/client/PlotBlock.tsx +492 -0
  116. package/src/client/TemplateDrawer.module.css +152 -0
  117. package/src/client/TemplateDrawer.tsx +147 -0
  118. package/src/client/achievement-store.ts +151 -0
  119. package/src/client/achievement-toast.tsx +71 -0
  120. package/src/client/achievements.ts +160 -0
  121. package/src/client/action-context.ts +32 -0
  122. package/src/client/artifact/ArtifactExport.module.css +64 -0
  123. package/src/client/artifact/ExportableGenuiBlock.tsx +100 -0
  124. package/src/client/artifact/create.ts +69 -0
  125. package/src/client/artifact/download.ts +42 -0
  126. package/src/client/artifact/html.ts +157 -0
  127. package/src/client/artifact/index.ts +15 -0
  128. package/src/client/artifact/json.ts +6 -0
  129. package/src/client/artifact/parse.ts +32 -0
  130. package/src/client/artifact/portability.ts +47 -0
  131. package/src/client/artifact/standalone-theme.ts +94 -0
  132. package/src/client/artifact/types.ts +44 -0
  133. package/src/client/asset-echarts-core.ts +39 -0
  134. package/src/client/asset-echarts.ts +27 -0
  135. package/src/client/asset-loader.ts +96 -0
  136. package/src/client/asset-mermaid.ts +13 -0
  137. package/src/client/asset-three.ts +12 -0
  138. package/src/client/blocks/advanced.tsx +537 -0
  139. package/src/client/blocks/basic.tsx +121 -0
  140. package/src/client/blocks/charts.tsx +999 -0
  141. package/src/client/blocks/checkbox.tsx +61 -0
  142. package/src/client/blocks/diagram/geometry.ts +151 -0
  143. package/src/client/blocks/diagram/index.tsx +293 -0
  144. package/src/client/blocks/diagram/layout.ts +202 -0
  145. package/src/client/blocks/diagram/theme.ts +127 -0
  146. package/src/client/blocks/forms.tsx +531 -0
  147. package/src/client/blocks/image.tsx +28 -0
  148. package/src/client/blocks/render-node.tsx +494 -0
  149. package/src/client/blocks/state.ts +65 -0
  150. package/src/client/blocks/svg.tsx +66 -0
  151. package/src/client/dom-fence.tsx +1123 -0
  152. package/src/client/echarts-engine.ts +9 -0
  153. package/src/client/echarts-lazy.ts +41 -0
  154. package/src/client/embed.ts +19 -0
  155. package/src/client/fence-render.tsx +266 -0
  156. package/src/client/gallery.ts +215 -0
  157. package/src/client/genui-runtime/diagnostics.ts +165 -0
  158. package/src/client/genui-runtime/index.ts +1 -0
  159. package/src/client/genui-runtime/limits.ts +54 -0
  160. package/src/client/genui-runtime/normalize.ts +329 -0
  161. package/src/client/genui-runtime/schema.ts +352 -0
  162. package/src/client/genui-runtime/value-utils.ts +67 -0
  163. package/src/client/guard.ts +1965 -0
  164. package/src/client/i18n/en.ts +335 -0
  165. package/src/client/i18n/index.ts +125 -0
  166. package/src/client/i18n/runtime.ts +130 -0
  167. package/src/client/i18n/zh.ts +330 -0
  168. package/src/client/index.tsx +228 -0
  169. package/src/client/inline.ts +106 -0
  170. package/src/client/interaction-store.ts +127 -0
  171. package/src/client/mermaid-core.ts +158 -0
  172. package/src/client/mermaid-lazy.ts +31 -0
  173. package/src/client/mermaid-safe.ts +122 -0
  174. package/src/client/panel-command.ts +138 -0
  175. package/src/client/panel-store.ts +471 -0
  176. package/src/client/panel.tsx +287 -0
  177. package/src/client/parse-partial.ts +170 -0
  178. package/src/client/primitive-adapter.ts +13 -0
  179. package/src/client/primitive-labels.ts +55 -0
  180. package/src/client/safe-math.ts +298 -0
  181. package/src/client/scene3d-core.ts +195 -0
  182. package/src/client/scene3d-lazy.ts +26 -0
  183. package/src/client/session-resolver.ts +33 -0
  184. package/src/client/source-fence.ts +70 -0
  185. package/src/client/spec.ts +896 -0
  186. package/src/client/standalone/asset-loader.ts +9 -0
  187. package/src/client/standalone/bootstrap-types.ts +11 -0
  188. package/src/client/standalone/katex-style.ts +1 -0
  189. package/src/client/standalone/primitive-adapter.tsx +94 -0
  190. package/src/client/standalone/runtime.tsx +27 -0
  191. package/src/client/submission-registry.ts +150 -0
  192. package/src/client/svg-fence.tsx +39 -0
  193. package/src/client/table-details.ts +151 -0
  194. package/src/client/templates.ts +322 -0
  195. package/src/client/toolview.tsx +67 -0
  196. package/src/client/walk-spec.ts +43 -0
  197. package/src/css-modules.d.ts +5 -0
  198. package/src/index.ts +9 -0
  199. package/src/plugin/chart-contract.ts +13 -0
  200. package/src/plugin/fence-feedback.ts +655 -0
  201. package/src/plugin/genui-diagnostic.ts +112 -0
  202. package/src/plugin/index.ts +224 -0
  203. package/src/plugin/inline-content-diagnostic.ts +178 -0
  204. package/src/plugin/invariant.ts +30 -0
  205. package/src/plugin/node-shims.d.ts +31 -0
  206. package/src/plugin/tool.ts +424 -0
  207. package/src/shared/fence-repair.ts +536 -0
  208. package/src/shared/fence-resolve.ts +80 -0
  209. package/tsconfig.json +35 -0
  210. package/tsdown.config.ts +275 -0
@@ -0,0 +1,165 @@
1
+ /** GenUI runtime diagnostics for aliases and unknown fields. */
2
+ import { COMPONENT_SCHEMAS, GENUI_SPEC_SCHEMA } from './schema.ts'
3
+ import { isComponentRoot } from '../spec.ts'
4
+ import type { ComponentRecordSchema, ComponentSchema } from './schema.ts'
5
+ import { isTableDetailReachable } from '../table-details.ts'
6
+
7
+ export interface GenuiDiagnostic {
8
+ readonly kind: 'alias' | 'unknown-field'
9
+ readonly path: string
10
+ readonly message: string
11
+ readonly type?: string
12
+ readonly field?: string
13
+ readonly canonical?: string
14
+ }
15
+
16
+
17
+ function record(value: unknown): Record<string, unknown> | undefined {
18
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
19
+ ? value as Record<string, unknown>
20
+ : undefined
21
+ }
22
+
23
+ function isNode(value: unknown): value is Record<string, unknown> {
24
+ const candidate = record(value)
25
+ return candidate !== undefined && typeof candidate.type === 'string'
26
+ }
27
+
28
+ function visitNativeNodes(value: unknown, path: string, visit: (node: Record<string, unknown>, path: string, schema: ComponentSchema) => void): void {
29
+ if (!isNode(value)) return
30
+ const type = value.type as string
31
+ const definition = COMPONENT_SCHEMAS[type]
32
+ if (definition === undefined) return
33
+ visit(value, path, definition)
34
+ const children = (child: unknown, childPath: string): void => visitNativeNodes(child, childPath, visit)
35
+ if ((type === 'row' || type === 'col' || type === 'grid' || type === 'card') && Array.isArray(value.items)) {
36
+ value.items.forEach((child, index) => children(child, `${path}.items[${index}]`))
37
+ } else if (type === 'list' && Array.isArray(value.items)) {
38
+ value.items.forEach((child, index) => children(child, `${path}.items[${index}]`))
39
+ } else if (type === 'tabs' && Array.isArray(value.tabs)) {
40
+ value.tabs.forEach((tab, index) => {
41
+ const holder = record(tab)
42
+ if (holder === undefined) return
43
+ if (Array.isArray(holder.items)) holder.items.forEach((child, childIndex) => children(child, `${path}.tabs[${index}].items[${childIndex}]`))
44
+ else children(holder.items, `${path}.tabs[${index}].items`)
45
+ })
46
+ } else if (type === 'accordion' && Array.isArray(value.items)) {
47
+ value.items.forEach((item, index) => {
48
+ const holder = record(item)
49
+ if (holder?.items !== undefined && Array.isArray(holder.items)) holder.items.forEach((child, childIndex) => children(child, `${path}.items[${index}].items[${childIndex}]`))
50
+ })
51
+ } else if (type === 'table' && Array.isArray(value.details)) {
52
+ const table = { columns: value.columns, rows: value.rows, types: value.types }
53
+ value.details.forEach((detail, rowIndex) => {
54
+ if (Array.isArray(detail) && isTableDetailReachable(table, rowIndex)) {
55
+ detail.forEach((child, childIndex) => children(child, `${path}.details[${rowIndex}][${childIndex}]`))
56
+ }
57
+ })
58
+ }
59
+ }
60
+
61
+ function pushUnknownField(
62
+ warnings: GenuiDiagnostic[],
63
+ path: string,
64
+ field: string,
65
+ type: string,
66
+ ): void {
67
+ warnings.push({
68
+ kind: 'unknown-field',
69
+ path: `${path}.${field}`,
70
+ message: `${path}.${field}: unknown field for '${type}'`,
71
+ type,
72
+ field,
73
+ })
74
+ }
75
+
76
+ function diagnoseRecordFields(
77
+ value: unknown,
78
+ path: string,
79
+ definition: ComponentRecordSchema,
80
+ type: string,
81
+ warnings: GenuiDiagnostic[],
82
+ ): void {
83
+ const holder = record(value)
84
+ if (holder === undefined) return
85
+ for (const field of Object.keys(holder)) {
86
+ if (field in definition.fields) continue
87
+ pushUnknownField(warnings, path, field, type)
88
+ }
89
+ for (const [field, nested] of Object.entries(definition.nested)) {
90
+ const nestedValue = holder[field]
91
+ if (Array.isArray(nestedValue)) {
92
+ nestedValue.forEach((item, index) => diagnoseRecordFields(item, `${path}.${field}[${index}]`, nested, type, warnings))
93
+ } else if (nestedValue !== undefined) {
94
+ diagnoseRecordFields(nestedValue, `${path}.${field}`, nested, type, warnings)
95
+ }
96
+ }
97
+ }
98
+
99
+ function diagnoseNestedFields(
100
+ node: Record<string, unknown>,
101
+ path: string,
102
+ definition: ComponentSchema,
103
+ warnings: GenuiDiagnostic[],
104
+ ): void {
105
+ for (const [field, nested] of Object.entries(definition.nested)) {
106
+ const nestedValue = node[field]
107
+ if (Array.isArray(nestedValue)) {
108
+ nestedValue.forEach((item, index) => diagnoseRecordFields(item, `${path}.${field}[${index}]`, nested, node.type as string, warnings))
109
+ } else if (nestedValue !== undefined) {
110
+ diagnoseRecordFields(nestedValue, `${path}.${field}`, nested, node.type as string, warnings)
111
+ }
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Diagnose unknown direct fields on native nodes.
117
+ *
118
+ * Unknown types are intentionally skipped so custom renderers retain their
119
+ * opaque extension payloads. Native unknown fields are warnings, not errors.
120
+ *
121
+ * @param value - Canonical or raw GenUI value.
122
+ * @returns Stable field diagnostics in tree order.
123
+ */
124
+ export function diagnoseUnknownGenuiFields(value: unknown): GenuiDiagnostic[] {
125
+ const warnings: GenuiDiagnostic[] = []
126
+ const root = record(value)
127
+ if (root === undefined) return warnings
128
+ const visit = (node: Record<string, unknown>, path: string, definition: ComponentSchema): void => {
129
+ for (const field of Object.keys(node)) {
130
+ if (field === 'type' || field in definition.fields) continue
131
+ pushUnknownField(warnings, path, field, node.type as string)
132
+ }
133
+ diagnoseNestedFields(node, path, definition, warnings)
134
+ if (node.type === 'list' && Array.isArray(node.items)) {
135
+ node.items.forEach((item, index) => {
136
+ if (isNode(item)) return
137
+ const holder = record(item)
138
+ if (holder === undefined) return
139
+ for (const field of Object.keys(holder)) {
140
+ if (field === 'title' || field === 'desc') continue
141
+ const itemPath = `${path}.items[${index}]`
142
+ warnings.push({ kind: 'unknown-field', path: `${itemPath}.${field}`, message: `${itemPath}.${field}: list items render 'title' and 'desc' only — '${field}' is dropped`, type: 'list', field })
143
+ }
144
+ })
145
+ }
146
+ }
147
+ if (Array.isArray(root.items) && !isComponentRoot(root)) {
148
+ for (const field of Object.keys(root)) {
149
+ if (field in GENUI_SPEC_SCHEMA.fields) continue
150
+ pushUnknownField(warnings, 'spec', field, 'spec')
151
+ }
152
+ root.items.forEach((item, index) => visitNativeNodes(item, `items[${index}]`, visit))
153
+ } else if (typeof root.type === 'string') {
154
+ // A bare component root is a documented shorthand, so its `type` belongs
155
+ // to the native node schema rather than the root specification schema —
156
+ // including data components whose `items` is their record list (#172).
157
+ visitNativeNodes(root, 'spec', visit)
158
+ } else {
159
+ for (const field of Object.keys(root)) {
160
+ if (field in GENUI_SPEC_SCHEMA.fields) continue
161
+ pushUnknownField(warnings, 'spec', field, 'spec')
162
+ }
163
+ }
164
+ return warnings
165
+ }
@@ -0,0 +1 @@
1
+ export { GENUI_LIMITS } from './limits.ts'
@@ -0,0 +1,54 @@
1
+ /** Resource limits shared by GenUI repair, validation, and rendering. */
2
+ export const GENUI_LIMITS = {
3
+ /** Maximum nesting depth of the component tree. */
4
+ maxDepth: 8,
5
+ /** Maximum total nodes across the whole spec. */
6
+ maxNodes: 200,
7
+ /** Maximum length of any plain string field. */
8
+ maxString: 2000,
9
+ /** Maximum length of a `code` body. */
10
+ maxCode: 12_000,
11
+ /** Maximum length of a mermaid source. */
12
+ maxMermaid: 8000,
13
+ /** Maximum `grid` columns. */
14
+ maxGridCols: 12,
15
+ /** Maximum `tabs` count. */
16
+ maxTabs: 12,
17
+ /** Maximum `accordion` items. */
18
+ maxAccordionItems: 24,
19
+ /** Maximum `list` items. */
20
+ maxListItems: 50,
21
+ /** Maximum `select`/`radio` options. */
22
+ maxOptions: 50,
23
+ /** Maximum `table` rows / columns. */
24
+ maxTableRows: 50,
25
+ maxTableCols: 12,
26
+ /** Maximum `chart` data points per series. */
27
+ maxChartPoints: 60,
28
+ /** Maximum `plot` series and per-series parameters. */
29
+ maxPlotSeries: 8,
30
+ maxPlotParams: 6,
31
+ /** Maximum `scene3d` meshes. */
32
+ maxMeshes: 5,
33
+ /** Maximum `quiz` options. */
34
+ maxQuizOptions: 8,
35
+ /** Maximum `steps` / `timeline` / `breadcrumb` / `keyvalue` entries. */
36
+ maxSteps: 24,
37
+ maxTimelineItems: 24,
38
+ maxBreadcrumbItems: 12,
39
+ maxKeyValuePairs: 24,
40
+ /** Maximum `file-tree` nesting. */
41
+ maxTreeDepth: 6,
42
+ /** Maximum `diagram` nodes / edges / zones / focal accents. */
43
+ maxDiagramNodes: 9,
44
+ maxDiagramEdges: 12,
45
+ maxDiagramZones: 3,
46
+ maxDiagramFocal: 2,
47
+ maxDiagramLabel: 14,
48
+ /** Maximum depth of an `echart` option object. */
49
+ maxEChartOptionDepth: 10,
50
+ /** Maximum length of any single array inside an `echart` option. */
51
+ maxEChartArrayLen: 500,
52
+ /** Maximum entries traversed while sanitizing an `echart` option. */
53
+ maxEChartOptionNodes: 2000,
54
+ } as const
@@ -0,0 +1,329 @@
1
+ /** Deterministic GenUI alias and structural normalization. */
2
+ import { COMPONENT_SCHEMAS } from './schema.ts'
3
+ import type { ComponentRecordSchema } from './schema.ts'
4
+ import { isComponentRoot } from '../spec.ts'
5
+ import type { GenuiDiagnostic } from './diagnostics.ts'
6
+ import { isTableDetailReachable } from '../table-details.ts'
7
+
8
+ function record(value: unknown): Record<string, unknown> | undefined {
9
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
10
+ ? value as Record<string, unknown>
11
+ : undefined
12
+ }
13
+
14
+ function isNode(value: unknown): value is Record<string, unknown> {
15
+ const candidate = record(value)
16
+ return candidate !== undefined && typeof candidate.type === 'string'
17
+ }
18
+
19
+ /** Fields a `stat` keeps when a metric record is folded into its own node. */
20
+ const STAT_METRIC_FIELDS = ['label', 'value', 'delta', 'spark', 'size'] as const
21
+ const LIST_ITEM_ALIASES = { label: 'title', name: 'title', description: 'desc', content: 'desc', text: 'desc', body: 'desc', detail: 'desc' } as const
22
+
23
+ /**
24
+ * Normalize one metric record of a stat group into a single-metric `stat`
25
+ * node, or null when the entry is not a metric at all.
26
+ */
27
+ function statMetricsOf(
28
+ entries: readonly unknown[],
29
+ path: string,
30
+ normalizeChild: (child: unknown, childPath: string) => unknown,
31
+ ): unknown[] | null {
32
+ if (entries.length === 0) return null
33
+ const metrics: unknown[] = []
34
+ for (let index = 0; index < entries.length; index++) {
35
+ const entry = entries[index]
36
+ const metric = record(entry)
37
+ if (metric === undefined) return null
38
+ // An entry that is already a node passes through: the model enumerated
39
+ // its own components instead of bare metrics.
40
+ if (isNode(entry)) {
41
+ metrics.push(normalizeChild(entry, `${path}.items[${index}]`))
42
+ continue
43
+ }
44
+ if (typeof metric.label !== 'string' && typeof metric.value !== 'string') return null
45
+ const stat: Record<string, unknown> = { type: 'stat' }
46
+ for (const field of STAT_METRIC_FIELDS) {
47
+ if (metric[field] !== undefined) stat[field] = metric[field]
48
+ }
49
+ metrics.push(stat)
50
+ }
51
+ return metrics
52
+ }
53
+
54
+ function normalizeAliasFields(value: Record<string, unknown>, path: string, type: string, warnings: GenuiDiagnostic[]): Record<string, unknown> {
55
+ const definition = COMPONENT_SCHEMAS[type]
56
+ if (definition === undefined) return value
57
+ const out: Record<string, unknown> = { ...value }
58
+ for (const [alias, canonical] of Object.entries(definition.aliases)) {
59
+ if (!(alias in out)) continue
60
+ const aliasPath = `${path}.${alias}`
61
+ const keptCanonical = canonical in out
62
+ if (!keptCanonical) out[canonical] = out[alias]
63
+ delete out[alias]
64
+ warnings.push({
65
+ kind: 'alias',
66
+ path: aliasPath,
67
+ message: keptCanonical
68
+ ? `${aliasPath} is ignored because canonical field '${canonical}' is present`
69
+ : `${aliasPath} normalized/adopted as '${canonical}'`,
70
+ type,
71
+ field: alias,
72
+ canonical,
73
+ })
74
+ }
75
+ for (const [field, values] of Object.entries(definition.valueAliases)) {
76
+ const written = out[field]
77
+ if (typeof written !== 'string') continue
78
+ const canonical = values[written]
79
+ if (canonical === undefined || canonical === written) continue
80
+ out[field] = canonical
81
+ warnings.push({
82
+ kind: 'alias',
83
+ path: `${path}.${field}`,
84
+ message: `${path}.${field} '${written}' normalized to '${canonical}'`,
85
+ type,
86
+ field,
87
+ canonical,
88
+ })
89
+ }
90
+ return out
91
+ }
92
+
93
+ /**
94
+ * Apply a nested record schema's field aliases to a record tree: every entry
95
+ * of an array (and every nested record collection below it) is rewritten in
96
+ * place-equivalent copies, so validation, repair, and diagnostics all see the
97
+ * canonical record fields (`keyvalue.pairs[].label → key`,
98
+ * `file-tree.items[].label → name`, issue #186).
99
+ */
100
+ function normalizeRecordTree(
101
+ value: unknown,
102
+ definition: ComponentRecordSchema,
103
+ at: string,
104
+ type: string,
105
+ warnings: GenuiDiagnostic[],
106
+ ): unknown {
107
+ if (Array.isArray(value)) {
108
+ return value.map((entry, index) => normalizeRecordTree(entry, definition, `${at}[${index}]`, type, warnings))
109
+ }
110
+ const holder = record(value)
111
+ if (holder === undefined) return value
112
+ const out = { ...holder }
113
+ for (const [alias, canonical] of Object.entries(definition.aliases)) {
114
+ if (!(alias in out) || canonical in out) continue
115
+ out[canonical] = out[alias]
116
+ delete out[alias]
117
+ warnings.push({
118
+ kind: 'alias',
119
+ path: `${at}.${alias}`,
120
+ message: `${at}.${alias} normalized/adopted as '${canonical}'`,
121
+ type,
122
+ field: alias,
123
+ canonical,
124
+ })
125
+ }
126
+ for (const [field, nested] of Object.entries(definition.nested)) {
127
+ if (out[field] !== undefined) out[field] = normalizeRecordTree(out[field], nested, `${at}.${field}`, type, warnings)
128
+ }
129
+ return out
130
+ }
131
+
132
+ /**
133
+ * file-tree parents without a `type`: a record that carries `children` is a
134
+ * directory in every model-written tree, and the renderer's collapse
135
+ * affordance depends on the marker (issue #186).
136
+ */
137
+ function defaultFileTreeDirectories(value: unknown): unknown {
138
+ if (Array.isArray(value)) return value.map(defaultFileTreeDirectories)
139
+ const holder = record(value)
140
+ if (holder === undefined) return value
141
+ const out = { ...holder }
142
+ if (out.type === undefined && Array.isArray(out.children)) out.type = 'dir'
143
+ if (out.children !== undefined) out.children = defaultFileTreeDirectories(out.children)
144
+ return out
145
+ }
146
+
147
+ function normalizeNode(value: unknown, path: string, warnings: GenuiDiagnostic[]): unknown {
148
+ if (!isNode(value)) return value
149
+ const type = value.type as string
150
+ const definition = COMPONENT_SCHEMAS[type]
151
+ // Custom nodes are opaque by contract: don't inspect or rewrite their data.
152
+ if (definition === undefined) return value
153
+ const out = normalizeAliasFields(value, path, type, warnings)
154
+ // Nested data records (keyvalue pairs, file-tree items, …) carry their own
155
+ // field aliases — apply them before validation/repair see the records.
156
+ for (const [field, nested] of Object.entries(definition.nested)) {
157
+ if (out[field] !== undefined) out[field] = normalizeRecordTree(out[field], nested, `${path}.${field}`, type, warnings)
158
+ }
159
+ const normalizeNodeValue = (child: unknown, childPath: string): unknown => normalizeNode(child, childPath, warnings)
160
+ const normalizeNodeArray = (children: unknown, childPath: string): unknown => Array.isArray(children)
161
+ ? children.map((child, index) => normalizeNodeValue(child, `${childPath}[${index}]`))
162
+ : children
163
+
164
+ if (type === 'file-tree' && out.items !== undefined) {
165
+ out.items = defaultFileTreeDirectories(out.items)
166
+ }
167
+
168
+ if (type === 'stat' && Array.isArray(out.items) && out.label === undefined && out.value === undefined) {
169
+ // Model-side generalization: one `stat` carrying a metric list
170
+ // ({type:'stat',items:[{label,value},…]}) means "these metrics, side by
171
+ // side". Normalize it into a row of single-metric stats, the documented
172
+ // multi-stat idiom (SKILL.md), instead of letting repair drop the node and
173
+ // reject the whole fence (issue #172, case A). A group that also declares
174
+ // `label`/`value` is a single stat with stray fields and stays untouched.
175
+ const metrics = statMetricsOf(out.items, path, normalizeNodeValue)
176
+ if (metrics !== null) {
177
+ warnings.push({
178
+ kind: 'alias',
179
+ path: `${path}.items`,
180
+ message: `${path}.items normalized into a row of 'stat' nodes`,
181
+ type,
182
+ field: 'items',
183
+ canonical: 'row',
184
+ })
185
+ return { type: 'row', items: metrics, ...(out.span !== undefined ? { span: out.span } : {}) }
186
+ }
187
+ }
188
+
189
+ if (type === 'row' || type === 'col' || type === 'grid' || type === 'card' || type === 'file-tree' || type === 'timeline' || type === 'breadcrumb') {
190
+ if (type !== 'file-tree' && type !== 'timeline' && type !== 'breadcrumb') out.items = normalizeNodeArray(out.items, `${path}.items`)
191
+ } else if (type === 'list' && Array.isArray(out.items)) {
192
+ // List records and nested nodes share items; aliases only belong to records.
193
+ out.items = out.items.map((child, index) => {
194
+ if (isNode(child)) return normalizeNodeValue(child, `${path}.items[${index}]`)
195
+ if (Array.isArray(child) && child.length === 1 && typeof child[0] === 'string') return child[0]
196
+ const holder = record(child)
197
+ if (holder === undefined) return child
198
+ const normalizedHolder = { ...holder }
199
+ const itemPath = `${path}.items[${index}]`
200
+ for (const [alias, canonical] of Object.entries(LIST_ITEM_ALIASES)) {
201
+ if (!(alias in normalizedHolder)) continue
202
+ const keptCanonical = canonical in normalizedHolder
203
+ if (!keptCanonical) normalizedHolder[canonical] = normalizedHolder[alias]
204
+ delete normalizedHolder[alias]
205
+ warnings.push({
206
+ kind: 'alias', path: `${itemPath}.${alias}`, type, field: alias, canonical,
207
+ message: keptCanonical
208
+ ? `${itemPath}.${alias} is ignored because canonical field '${canonical}' is present`
209
+ : `${itemPath}.${alias} normalized/adopted as '${canonical}'`,
210
+ })
211
+ }
212
+ if (!('title' in normalizedHolder) && typeof normalizedHolder.desc === 'string') {
213
+ normalizedHolder.title = normalizedHolder.desc
214
+ delete normalizedHolder.desc
215
+ warnings.push({ kind: 'alias', path: `${itemPath}.desc`, type, field: 'desc', canonical: 'title', message: `${itemPath}.desc normalized/adopted as 'title'` })
216
+ }
217
+ return normalizedHolder
218
+ })
219
+ } else if (type === 'keyvalue' && Array.isArray(out.pairs)) {
220
+ // Pair-as-array (`[[key, value], …]` or `[[key], …]`) is what a model
221
+ // writes when it treats keyvalue as a 2-column list. Canonicalize before
222
+ // validation: the record validator rejects every array entry ("must be an
223
+ // object"), and those errors take the whole fence down even though repair
224
+ // could read the data.
225
+ if (out.pairs.length > 0 && out.pairs.every(pair => Array.isArray(pair))) {
226
+ out.pairs = out.pairs.map(pair => {
227
+ const cells = pair as unknown[]
228
+ return { key: cells[0], value: cells.length > 1 ? cells[1] : '' }
229
+ })
230
+ }
231
+ } else if (type === 'tabs' && Array.isArray(out.tabs)) {
232
+ out.tabs = out.tabs.map((tab, index) => {
233
+ const holder = record(tab)
234
+ if (holder === undefined) return tab
235
+ const normalizedHolder = { ...holder }
236
+ if ('content' in normalizedHolder) {
237
+ const tabPath = `${path}.tabs[${index}].content`
238
+ const hasItems = 'items' in normalizedHolder
239
+ if (!hasItems) normalizedHolder.items = normalizedHolder.content
240
+ delete normalizedHolder.content
241
+ warnings.push({
242
+ kind: 'alias',
243
+ path: tabPath,
244
+ message: hasItems
245
+ ? `${tabPath} is ignored because canonical field 'items' is present`
246
+ : `${tabPath} normalized/adopted as 'items'`,
247
+ type,
248
+ field: 'content',
249
+ canonical: 'items',
250
+ })
251
+ }
252
+ normalizedHolder.items = Array.isArray(normalizedHolder.items)
253
+ ? normalizedHolder.items.map((child, childIndex) => normalizeNodeValue(child, `${path}.tabs[${index}].items[${childIndex}]`))
254
+ : normalizedHolder.items === undefined
255
+ ? []
256
+ : [normalizeNodeValue(normalizedHolder.items, `${path}.tabs[${index}].items[0]`)]
257
+ return normalizedHolder
258
+ })
259
+ } else if (type === 'accordion' && Array.isArray(out.items)) {
260
+ out.items = out.items.map((item, index) => {
261
+ const holder = record(item)
262
+ if (holder === undefined) return item
263
+ return { ...holder, items: Array.isArray(holder.items) ? holder.items.map((child, childIndex) => normalizeNodeValue(child, `${path}.items[${index}].items[${childIndex}]`)) : holder.items }
264
+ })
265
+ } else if (type === 'table' && Array.isArray(out.details)) {
266
+ const table = { columns: out.columns, rows: out.rows, types: out.types }
267
+ out.details = out.details.map((detail, rowIndex) => {
268
+ if (!Array.isArray(detail) || !isTableDetailReachable(table, rowIndex)) return detail
269
+ return detail.map((child, childIndex) => normalizeNodeValue(child, `${path}.details[${rowIndex}][${childIndex}]`))
270
+ })
271
+ }
272
+ return out
273
+ }
274
+
275
+ /**
276
+ * Normalize a raw GenUI value into canonical field names.
277
+ *
278
+ * Only deterministic aliases and structural aliases are changed. Resource
279
+ * limits, type repair, security filtering, and semantic validation remain in
280
+ * the guard layer. Unknown component types are returned opaque.
281
+ *
282
+ * @param value - Raw GenUI spec or bare native component.
283
+ * @returns Canonical value and stable alias diagnostics.
284
+ */
285
+ export function normalizeGenuiSpec(value: unknown): { value: unknown; warnings: GenuiDiagnostic[] } {
286
+ const warnings: GenuiDiagnostic[] = []
287
+ if (typeof value === 'string') {
288
+ // Double-encoded body (issue #186): the model escaped the whole spec into
289
+ // a JSON string. Decode exactly once — anything deeper is quoted prose,
290
+ // not a spec, and stays unrenderable.
291
+ try {
292
+ const decoded: unknown = JSON.parse(value)
293
+ if (record(decoded) !== undefined || Array.isArray(decoded)) {
294
+ value = decoded
295
+ warnings.push({
296
+ kind: 'alias',
297
+ path: 'spec',
298
+ message: 'double-encoded JSON string unwrapped into a spec value',
299
+ })
300
+ }
301
+ } catch {
302
+ // Not JSON text: leave as-is; repair rejects the root as usual.
303
+ }
304
+ }
305
+ if (Array.isArray(value) && value.length > 0) {
306
+ // A root-level component list is the envelope minus its braces (issue
307
+ // #186): adopt it as `items` instead of rejecting the whole fence. An
308
+ // empty list has nothing to render and stays an invalid root.
309
+ value = { items: value }
310
+ warnings.push({
311
+ kind: 'alias',
312
+ path: 'items',
313
+ message: 'root array normalized into an items envelope',
314
+ })
315
+ }
316
+ const root = record(value)
317
+ if (root === undefined) return { value, warnings }
318
+ const out = { ...root }
319
+ // A bare component root is normalized as a node, not as an envelope whose
320
+ // `items` are its children: for a data component (steps/list/timeline/…)
321
+ // `items` is the record list and its aliases must still apply (issue #172).
322
+ if (isComponentRoot(out)) {
323
+ return { value: normalizeNode(out, 'spec', warnings), warnings }
324
+ }
325
+ if (Array.isArray(out.items)) {
326
+ out.items = out.items.map((item, index) => normalizeNode(item, `items[${index}]`, warnings))
327
+ }
328
+ return { value: out, warnings }
329
+ }