@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,896 @@
1
+ /**
2
+ * GenUI spec language: the declarative component tree a model emits inside a
3
+ * ```dsh-ui fence in its reply, which GenuiBlock renders as real interactive
4
+ * UI inline in the conversation. The vocabulary is a white list — the renderer
5
+ * maps each node to DOM directly, with no arbitrary-HTML path (same
6
+ * untrusted-output stance as MarkdownText).
7
+ *
8
+ * v1 interactivity is client-side only: buttons, tabs, checkboxes, and inputs
9
+ * are operable, but events do NOT flow back to the model.
10
+ */
11
+ import { COMPONENT_SCHEMAS, GENUI_NATIVE_TYPES } from './genui-runtime/schema.ts'
12
+
13
+ /** One node in the component tree. */
14
+ /** Layout hints accepted by every node: `span` is how many columns the node
15
+ * occupies as a direct child of a `grid` (bento layouts). */
16
+ export interface GenuiLayoutHints {
17
+ span?: number
18
+ }
19
+
20
+ export type GenuiNode = (
21
+ | GenuiText
22
+ | GenuiRow
23
+ | GenuiCol
24
+ | GenuiGrid
25
+ | GenuiCard
26
+ | GenuiButton
27
+ | GenuiInput
28
+ | GenuiSelect
29
+ | GenuiCheckbox
30
+ | GenuiLink
31
+ | GenuiImage
32
+ | GenuiAudio
33
+ | GenuiVideo
34
+ | GenuiBadge
35
+ | GenuiStat
36
+ | GenuiHero
37
+ | GenuiProgress
38
+ | GenuiDivider
39
+ | GenuiList
40
+ | GenuiTable
41
+ | GenuiChart
42
+ | GenuiTabs
43
+ | GenuiAvatar
44
+ | GenuiSpacer
45
+ | GenuiPlot
46
+ | GenuiCallout
47
+ | GenuiSteps
48
+ | GenuiKeyValue
49
+ | GenuiDiff
50
+ | GenuiJson
51
+ | GenuiCode
52
+ | GenuiRadio
53
+ | GenuiSubmit
54
+ | GenuiSwitch
55
+ | GenuiSlider
56
+ | GenuiTextarea
57
+ | GenuiAccordion
58
+ | GenuiCopy
59
+ | GenuiMermaid
60
+ | GenuiSvg
61
+ | GenuiScene3D
62
+ | GenuiTimeline
63
+ | GenuiFileTree
64
+ | GenuiBreadcrumb
65
+ | GenuiQuiz
66
+ | GenuiDiagram
67
+ ) & GenuiLayoutHints
68
+
69
+ | GenuiEChart
70
+
71
+ export interface GenuiSpec {
72
+ /** Short title shown as the card banner. */
73
+ title?: string
74
+ /** Vertical gap between root items in px. */
75
+ gap?: number
76
+ /** Panel-only flag: `true` routes the fence to the session panel dock
77
+ * instead of the message flow (rendered by the same block, updated in
78
+ * place on every publish). */
79
+ panel?: boolean
80
+ /** Panel APPEND flag (meaningful only with `panel: true`): instead of
81
+ * replacing the session panel, merge this spec INTO the existing one —
82
+ * same-labelled tabs get their items appended, new tabs are added, plain
83
+ * item lists are appended to the tail. Lets the model grow a panel
84
+ * incrementally without resending (or truncating) the accumulated content.
85
+ * Applies only when the fence body is fully complete, so a streaming
86
+ * partial parse never double-merges. */
87
+ append?: boolean
88
+ /** Root component list. */
89
+ items: GenuiNode[]
90
+ }
91
+
92
+ /* ---------------- leaf nodes ---------------- */
93
+
94
+ export interface GenuiText {
95
+ type: 'text'
96
+ size?: 'h1' | 'h2' | 'h3' | 'body' | 'muted' | 'caption'
97
+ content: string
98
+ center?: boolean
99
+ }
100
+
101
+ export interface GenuiButton {
102
+ type: 'button'
103
+ label: string
104
+ tone?: 'primary' | 'danger' | 'success' | 'ghost'
105
+ full?: boolean
106
+ small?: boolean
107
+ icon?: string
108
+ /** v2: when set, clicking sends this action back to the model. */
109
+ action?: string
110
+ }
111
+
112
+ export interface GenuiInput {
113
+ type: 'input'
114
+ label?: string
115
+ placeholder?: string
116
+ value?: string
117
+ inputType?: 'text' | 'email' | 'password' | 'color'
118
+ /** v2: when set, interaction sends this action back to the model. */
119
+ action?: string
120
+ /**
121
+ * v2.7: stable field id — the value is persisted across refresh/re-render
122
+ * and collected by a sibling `submit` node (`fields: {id: value}`).
123
+ */
124
+ id?: string
125
+ }
126
+
127
+ export interface GenuiSelect {
128
+ type: 'select'
129
+ label?: string
130
+ options: string[]
131
+ /** v2: when set, interaction sends this action back to the model. */
132
+ action?: string
133
+ /**
134
+ * v2.8: default-selected option index (a placeholder option shows instead
135
+ * when absent, so nothing is silently pre-registered).
136
+ */
137
+ selected?: number
138
+ /**
139
+ * v2.8: stable field id — the chosen option persists across refresh and is
140
+ * collected by a sibling `submit` node (`fields: {id: value}`), like
141
+ * input/textarea ids.
142
+ */
143
+ id?: string
144
+ }
145
+
146
+ export interface GenuiCheckbox {
147
+ type: 'checkbox'
148
+ label: string
149
+ checked?: boolean
150
+ /** v2: when set, interaction sends this action back to the model. */
151
+ action?: string
152
+ /**
153
+ * Aggregation group name. When set, toggles stay local and update the
154
+ * block-wide multi-answer registry instead of firing the per-click action;
155
+ * a sibling `submit` collects the selected labels as a string array.
156
+ */
157
+ group?: string
158
+ }
159
+
160
+ export interface GenuiLink {
161
+ type: 'link'
162
+ label: string
163
+ /**
164
+ * Optional http(s)/mailto target. With `href` the node renders a REAL
165
+ * anchor (target=_blank + noopener); without it the node renders as plain
166
+ * styled text — never a dead clickable-looking control.
167
+ */
168
+ href?: string
169
+ }
170
+
171
+ /** User-controlled image from a browser-reachable URL. */
172
+ export interface GenuiImage {
173
+ type: 'image'
174
+ src: string
175
+ /** Visible caption and accessible image description. */
176
+ alt?: string
177
+ }
178
+
179
+ /** User-controlled audio from a browser-reachable URL. */
180
+ export interface GenuiAudio {
181
+ type: 'audio'
182
+ src: string
183
+ /** Visible caption and accessible player name. */
184
+ alt?: string
185
+ loop?: boolean
186
+ }
187
+
188
+ /** User-controlled video from a browser-reachable URL. */
189
+ export interface GenuiVideo {
190
+ type: 'video'
191
+ src: string
192
+ /** Visible caption and accessible player name. */
193
+ alt?: string
194
+ poster?: string
195
+ loop?: boolean
196
+ muted?: boolean
197
+ aspectRatio?: '16:9' | '4:3' | '1:1' | '9:16'
198
+ }
199
+
200
+ export interface GenuiBadge {
201
+ type: 'badge'
202
+ label: string
203
+ tone?: 'success' | 'warn' | 'danger' | 'accent'
204
+ icon?: string
205
+ }
206
+
207
+ /** Cover band: the answer's focal point. One per fence — an eyebrow label, an
208
+ * oversized metric, a title and a subtitle on a soft tinted surface. */
209
+ export interface GenuiHero {
210
+ type: 'hero'
211
+ title: string
212
+ subtitle?: string
213
+ /** Oversized metric, e.g. "99.96%" / "3.2x". */
214
+ value?: string
215
+ /** Eyebrow label above the metric. */
216
+ label?: string
217
+ delta?: string
218
+ spark?: number[]
219
+ tone?: 'accent' | 'success' | 'warning' | 'danger'
220
+ }
221
+
222
+ export interface GenuiStat {
223
+ type: 'stat'
224
+ label: string
225
+ value: string
226
+ delta?: string
227
+ /** Optional micro trend line (finite numbers, 2..60 points). */
228
+ spark?: number[]
229
+ /** `hero` renders one oversized number — use it once per fence as the anchor. */
230
+ size?: 'hero'
231
+ }
232
+
233
+ export interface GenuiProgress {
234
+ type: 'progress'
235
+ value: number
236
+ label?: string
237
+ valueLabel?: string
238
+ /** `bar` (default) draws a track; `ring` draws a circular gauge. */
239
+ variant?: 'bar' | 'ring'
240
+ /** Optional target marker on the bar track (0-100). */
241
+ target?: number
242
+ }
243
+
244
+ export interface GenuiDivider {
245
+ type: 'divider'
246
+ }
247
+
248
+ export interface GenuiAvatar {
249
+ type: 'avatar'
250
+ name: string
251
+ color?: string
252
+ }
253
+
254
+ export interface GenuiSpacer {
255
+ type: 'spacer'
256
+ }
257
+
258
+ /* ---------------- container nodes ---------------- */
259
+
260
+ export interface GenuiRow {
261
+ type: 'row'
262
+ items: GenuiNode[]
263
+ wrap?: boolean
264
+ spacer?: boolean
265
+ }
266
+
267
+ export interface GenuiCol {
268
+ type: 'col'
269
+ items: GenuiNode[]
270
+ gap?: number
271
+ }
272
+
273
+ export interface GenuiGrid {
274
+ type: 'grid'
275
+ cols: number
276
+ items: GenuiNode[]
277
+ }
278
+
279
+ export interface GenuiCard {
280
+ type: 'card'
281
+ title?: string
282
+ /** Semantic tint for the card surface (default: neutral). */
283
+ tone?: 'info' | 'success' | 'warning' | 'danger'
284
+ /** Explicit accent (hex) driving the border, title and wash. */
285
+ accent?: string
286
+ items: GenuiNode[]
287
+ }
288
+
289
+ export interface GenuiList {
290
+ type: 'list'
291
+ /** Field id of an input/select: its value filters the items locally. */
292
+ filter?: string
293
+ items: Array<string | { title: string; desc?: string } | GenuiNode>
294
+ }
295
+
296
+ export interface GenuiTable {
297
+ type: 'table'
298
+ columns: string[]
299
+ rows: Array<Array<string | number>>
300
+ /** Per-column cell type; missing = auto (numeric right-align, signed delta). */
301
+ types?: TableCellType[]
302
+ /** Append a 合计 footer row (numeric columns are summed). */
303
+ total?: boolean
304
+ /** Show 复制 Markdown / 复制 CSV chips above the table. */
305
+ export?: boolean
306
+ /** Optional master-detail payload, positionally aligned with `rows`:
307
+ * `details[i]` is what row i expands into (omit / empty = not expandable). */
308
+ details?: Array<GenuiNode[] | null>
309
+ /** Field id of an input/select: its live value filters the rows locally
310
+ * (substring match), so the table is searchable without a model round trip. */
311
+ filter?: string
312
+ /** Restrict `filter` to one column index (default: every column). */
313
+ filterColumn?: number
314
+ /** Field id of a select whose value is a column header: sorts by it locally. */
315
+ sortField?: string
316
+ }
317
+
318
+ /** How a table column's cells render.
319
+ * - `bar` / `ring`: the cell is read as 0-100
320
+ * - `spark`: the cell is a number list ("3,5,4,8") drawn as a mini trend
321
+ * - `index`: the 1-based row number (cell content is ignored)
322
+ * - `delta` / `num` / `badge` / `text`: text treatments. */
323
+ export type TableCellType = 'text' | 'num' | 'delta' | 'bar' | 'badge' | 'spark' | 'ring' | 'index' | 'group'
324
+
325
+ export interface GenuiChartDatum {
326
+ label: string
327
+ value: number
328
+ color?: string
329
+ }
330
+
331
+ export interface GenuiChart {
332
+ type: 'chart'
333
+ /** Chart shape: bars (default), line (trend), donut (share). */
334
+ kind?: 'bars' | 'line' | 'donut'
335
+ data: GenuiChartDatum[]
336
+ /** Multi-series: grouped bars, or one line per entry when kind is line. */
337
+ series?: Array<{ label: string; color?: string; data: GenuiChartDatum[] }>
338
+ /** Bars only: horizontal bars (rankings, long category labels). */
339
+ horizontal?: boolean
340
+ /** Bars + series: stack the series instead of grouping them. */
341
+ stacked?: boolean
342
+ /** Explicit categorical palette (hex) — overrides the host theme colours. */
343
+ palette?: string[]
344
+ /** Field id of an input/select: its value filters the categories locally. */
345
+ filter?: string
346
+ }
347
+
348
+ export interface GenuiTab {
349
+ label: string
350
+ items: GenuiNode[]
351
+ }
352
+
353
+ export interface GenuiTabs {
354
+ type: 'tabs'
355
+ tabs: GenuiTab[]
356
+ }
357
+
358
+ /* ---------------- v1.1 additions: plot / callout / steps / keyvalue / 收编 ---------------- */
359
+
360
+ export interface GenuiPlotSeries {
361
+ /** Math expression in x (safe evaluator: sin/cos/tan/pow/sqrt/log/exp/abs/floor/ceil/round/min/max/pi/e). */
362
+ expr: string
363
+ /** Legend label; defaults to the expression. */
364
+ label?: string
365
+ /** Stroke color. */
366
+ color?: string
367
+ /** v2.9 draw shape: line (default), area (fill to the baseline), scatter
368
+ * (dots only). */
369
+ kind?: 'line' | 'area' | 'scatter'
370
+ /** v2: adjustable parameters (e.g. {a: 2} in "a*sin(x)") — one slider each, live re-render. */
371
+ params?: Array<{
372
+ name: string
373
+ value: number
374
+ min?: number
375
+ max?: number
376
+ step?: number
377
+ /** v1.5: animate this parameter (auto-play) from `value` to `animateTo`. */
378
+ animateTo?: number
379
+ /** Animation duration in ms (default 4000). */
380
+ durationMs?: number
381
+ /** Loop the animation (default false). */
382
+ loop?: boolean
383
+ }>
384
+ }
385
+
386
+ export interface GenuiPlot {
387
+ type: 'plot'
388
+ /** Functions to draw, in draw order. */
389
+ series: GenuiPlotSeries[]
390
+ xMin?: number
391
+ xMax?: number
392
+ yMin?: number
393
+ yMax?: number
394
+ title?: string
395
+ }
396
+
397
+ export interface GenuiCallout {
398
+ type: 'callout'
399
+ tone?: 'info' | 'success' | 'warning' | 'error'
400
+ /** Short heading above the body. */
401
+ title?: string
402
+ content: string
403
+ }
404
+
405
+ export interface GenuiStep {
406
+ title: string
407
+ desc?: string
408
+ }
409
+
410
+ export interface GenuiSteps {
411
+ type: 'steps'
412
+ /** Completed steps are rendered in the accent color, the rest muted. */
413
+ current?: number
414
+ steps: GenuiStep[]
415
+ }
416
+
417
+ export interface GenuiKeyValue {
418
+ type: 'keyvalue'
419
+ pairs: Array<{ key: string; value: string }>
420
+ }
421
+
422
+ /** 收编 dsh DiffBlock: file mutations as an inline diff. */
423
+ export interface GenuiDiff {
424
+ type: 'diff'
425
+ diffs: Array<{
426
+ path: string
427
+ oldText: string | null
428
+ newText: string
429
+ }>
430
+ }
431
+
432
+ /** 收编 dsh JsonTree: a structured JSON value for inspection. */
433
+ export interface GenuiJson {
434
+ type: 'json'
435
+ /** Any JSON value to render as a tree. */
436
+ value: unknown
437
+ }
438
+
439
+ /** 收编 dsh CodeBlock: syntax-highlighted code with an explicit language. */
440
+ export interface GenuiCode {
441
+ type: 'code'
442
+ lang?: string
443
+ code: string
444
+ }
445
+
446
+ /* ---------------- v1.2: richer interactivity ---------------- */
447
+
448
+ export interface GenuiRadio {
449
+ type: 'radio'
450
+ label?: string
451
+ options: string[]
452
+ /** Default-selected option index. */
453
+ selected?: number
454
+ /** v2: when set, interaction sends this action back to the model. */
455
+ action?: string
456
+ /**
457
+ * v2.5: aggregation group name. When set, the selection is recorded into
458
+ * the block-wide answers registry instead of firing a per-click action — a
459
+ * sibling `submit` node then collects ALL groups ("交卷" pattern). Without
460
+ * `group`, the legacy per-click behavior applies.
461
+ */
462
+ group?: string
463
+ /**
464
+ * v2.6: correct option for LOCAL grading — the option index (number) or
465
+ * its label (string). When the group's radio carries this, the `submit`
466
+ * button grades IN PLACE on click (score + per-question right/wrong +
467
+ * explanations, zero model round trip).
468
+ */
469
+ answer?: number | string
470
+ /** v2.6: per-question explanation shown after local grading. */
471
+ explanation?: string
472
+ }
473
+
474
+ /** 提交节点收集当前 block 的答案和字段值;纯 radio 范围可在本地判卷。 */
475
+ export interface GenuiSubmit {
476
+ type: 'submit'
477
+ label: string
478
+ /**
479
+ * 可选 action 名称。纯 radio 范围满足本地判卷条件时无需 action;其他提交
480
+ * 通过 action 发送 `answers`、可选的 `fields`、`total` 和 `answered`。
481
+ */
482
+ action?: string
483
+ /**
484
+ * v2.6: optional action fired when the user clicks "重新作答" after a
485
+ * local grading (e.g. to tell the model the paper was redone). Absent =
486
+ * reset stays fully local.
487
+ */
488
+ resetAction?: string
489
+ /**
490
+ * `groups` 引用当前 block 中的 submission member key,来源为 radio.group、
491
+ * checkbox.group 以及 input/textarea/select/slider.id。radio 需已选择,
492
+ * checkbox 需至少选择一项,普通 field 需在 trim 后非空。`groups` 控制提交
493
+ * 所需成员与完成进度;payload 仍收集当前 block 中已填写的表单状态。
494
+ * 未设置时,只要存在至少一个已完成成员即可提交。
495
+ */
496
+ groups?: string[]
497
+ }
498
+
499
+ export interface GenuiSwitch {
500
+ type: 'switch'
501
+ label: string
502
+ checked?: boolean
503
+ /** v2: when set, interaction sends this action back to the model. */
504
+ action?: string
505
+ }
506
+
507
+ /**
508
+ * Slider: a range input for numeric form values (v2.9). The current value
509
+ * shows next to the track; dragging fires the action (debounced by the
510
+ * block) and, with an `id`, persists across refresh and joins the sibling
511
+ * submit's `fields` collection (the value is the numeric string).
512
+ */
513
+ export interface GenuiSlider {
514
+ type: 'slider'
515
+ label?: string
516
+ min?: number
517
+ max?: number
518
+ step?: number
519
+ /** Default value; defaults to `min` (or 0). */
520
+ value?: number
521
+ /** v2: when set, interaction sends this action back to the model. */
522
+ action?: string
523
+ /** v2.9: stable field id — durable value + submit collection. */
524
+ id?: string
525
+ }
526
+
527
+ export interface GenuiTextarea {
528
+ type: 'textarea'
529
+ label?: string
530
+ placeholder?: string
531
+ rows?: number
532
+ value?: string
533
+ /** v2.5: when set, blurring the field sends this action with the value. */
534
+ action?: string
535
+ /**
536
+ * v2.7: stable field id — the value is persisted across refresh/re-render
537
+ * and collected by a sibling `submit` node (`fields: {id: value}`).
538
+ */
539
+ id?: string
540
+ }
541
+
542
+ export interface GenuiAccordionItem {
543
+ title: string
544
+ items: GenuiNode[]
545
+ }
546
+
547
+ export interface GenuiAccordion {
548
+ type: 'accordion'
549
+ items: GenuiAccordionItem[]
550
+ }
551
+
552
+ export interface GenuiCopy {
553
+ type: 'copy'
554
+ label?: string
555
+ text: string
556
+ }
557
+
558
+ /* ---------------- v1.3: advanced ---------------- */
559
+
560
+ export interface GenuiSvg {
561
+ type: 'svg'
562
+ /** Standalone SVG document, displayed in an isolated image context. */
563
+ code: string
564
+ title?: string
565
+ height?: number
566
+ }
567
+
568
+ /** Mermaid diagram: flowchart/sequence/class/gantt source rendered lazily. */
569
+ export interface GenuiMermaid {
570
+ type: 'mermaid'
571
+ /** Mermaid diagram source (whitelisted diagram kinds at render time). */
572
+ code: string
573
+ }
574
+
575
+ /** One mesh in a 3D scene. */
576
+ export interface GenuiMesh {
577
+ /** Primitive geometry, white-listed. */
578
+ shape: 'box' | 'sphere' | 'cone' | 'cylinder' | 'torus'
579
+ /** Mesh color. */
580
+ color?: string
581
+ /** [x, y, z] position. */
582
+ position?: [number, number, number]
583
+ /** [x, y, z] rotation in radians. */
584
+ rotation?: [number, number, number]
585
+ /** Uniform scale, or [sx, sy, sz]. */
586
+ scale?: number | [number, number, number]
587
+ /** Box size [w, h, d] or sphere/cone/cylinder radius. */
588
+ size?: number | [number, number, number]
589
+ }
590
+
591
+ /** 3D scene rendered with three.js (lazily imported). */
592
+ export interface GenuiScene3D {
593
+ type: 'scene3d'
594
+ title?: string
595
+ meshes: GenuiMesh[]
596
+ /** Ambient light intensity (0..2). */
597
+ ambient?: number
598
+ /** Background color; default transparent. */
599
+ background?: string
600
+ }
601
+
602
+ /* ---------------- v1.4: more content structure ---------------- */
603
+ export interface GenuiTimelineItem {
604
+ /** Title of the event. */
605
+ title: string
606
+ /** Secondary description. */
607
+ desc?: string
608
+ /** Optional timestamp label. */
609
+ time?: string
610
+ }
611
+
612
+ export interface GenuiTimeline {
613
+ type: 'timeline'
614
+ items: GenuiTimelineItem[]
615
+ }
616
+
617
+ export interface GenuiFileTreeNode {
618
+ /** File or directory name. */
619
+ name: string
620
+ /** Directory when true (collapsible children). */
621
+ type?: 'file' | 'dir'
622
+ /** Child nodes (dirs only). */
623
+ children?: GenuiFileTreeNode[]
624
+ }
625
+
626
+ export interface GenuiFileTree {
627
+ type: 'file-tree'
628
+ items: GenuiFileTreeNode[]
629
+ }
630
+
631
+ export interface GenuiBreadcrumb {
632
+ type: 'breadcrumb'
633
+ items: string[]
634
+ }
635
+
636
+ /* ---------------- v1.5: teaching ---------------- */
637
+
638
+ export interface GenuiQuizOption {
639
+ label: string
640
+ /** True when this option is the correct answer. */
641
+ correct?: boolean
642
+ /** Per-option feedback shown after selection. */
643
+ feedback?: string
644
+ }
645
+
646
+ export interface GenuiQuiz {
647
+ type: 'quiz'
648
+ /** The question text. */
649
+ question: string
650
+ /** Answer options; exactly one should be correct. */
651
+ options: GenuiQuizOption[]
652
+ /** Shown after answering (either way). */
653
+ explanation?: string
654
+ /** Reset the quiz when this value changes (e.g. a question id). */
655
+ id?: string
656
+ /** v2.5: when set, answering sends this action with the chosen answer
657
+ * (`{type:'quiz', question, answer, correct}`) so the model can collect
658
+ * or grade it. */
659
+ action?: string
660
+ }
661
+
662
+ /* ---------------- v2.10: editorial diagram (diagram-design port) ---------------- */
663
+
664
+ /** Editorial diagram kinds, ported from cathrynlavery/diagram-design (27 types). */
665
+ export const DIAGRAM_KINDS = [
666
+ 'architecture', 'it-state', 'flowchart', 'sequence', 'state', 'er', 'timeline',
667
+ 'swimlane', 'quadrant', 'radar', 'loop', 'nested', 'tree', 'org-chart', 'layers',
668
+ 'venn', 'pyramid', 'bar', 'line', 'gantt', 'scatter', 'high-level', 'process',
669
+ 'medallion', 'data-flow', 'dp-integration', 'dp-security-matrix',
670
+ ] as const
671
+ export type GenuiDiagramKind = typeof DIAGRAM_KINDS[number]
672
+
673
+ /** Diagram surface variant: light / dark follow the host theme; editorial is the full skin. */
674
+ export type GenuiDiagramVariant = 'light' | 'dark' | 'editorial'
675
+
676
+ /** Node treatment classes (map to the diagram-design semantic fills/strokes). */
677
+ export type GenuiDiagramNodeType =
678
+ | 'focal' | 'backend' | 'store' | 'external' | 'input' | 'optional' | 'security'
679
+
680
+ /** One positioned node in a coordinate-mode diagram. */
681
+ export interface GenuiDiagramNode {
682
+ /** Stable node id referenced by edges. */
683
+ id: string
684
+ /** Human-readable label (Geist sans in the renderer). */
685
+ label: string
686
+ /** Optional technical sublabel (Geist mono in the renderer). */
687
+ sub?: string
688
+ /** Semantic treatment; `focal` is budgeted (max 2 per diagram). */
689
+ type?: GenuiDiagramNodeType
690
+ /** Position / size (coordinate-mode kinds only). 4px grid is enforced at render. */
691
+ x?: number
692
+ y?: number
693
+ w?: number
694
+ h?: number
695
+ /** Optional rectangular type tag (e.g. `API`). */
696
+ tag?: string
697
+ }
698
+
699
+ /** One connector between two nodes. */
700
+ export interface GenuiDiagramEdge {
701
+ /** Source node id. */
702
+ from: string
703
+ /** Target node id. */
704
+ to: string
705
+ /** Optional edge label (all-caps, <=14 chars in the renderer). */
706
+ label?: string
707
+ /** Stroke semantics: solid (default), dashed (optional/async), accent, link (HTTP/API). */
708
+ kind?: 'solid' | 'dashed' | 'accent' | 'link'
709
+ /** Route hint; `auto` (default) lets the renderer choose orthogonal routing. */
710
+ route?: 'auto' | 'orthogonal' | 'straight'
711
+ }
712
+
713
+ /** Optional brand override; every key maps to a diagram-design semantic token. */
714
+ export interface GenuiDiagramTheme {
715
+ paper?: string
716
+ 'paper-2'?: string
717
+ ink?: string
718
+ muted?: string
719
+ soft?: string
720
+ rule?: string
721
+ accent?: string
722
+ 'accent-tint'?: string
723
+ link?: string
724
+ }
725
+
726
+ /** Zone container: a labelled region grouping related nodes (max 3). */
727
+ export interface GenuiDiagramZone {
728
+ /** Zone label (mono uppercase eyebrow in the renderer). */
729
+ label: string
730
+ /** Bounding box in the same canvas space as the nodes. */
731
+ x?: number
732
+ y?: number
733
+ w?: number
734
+ h?: number
735
+ }
736
+
737
+ /**
738
+ * Editorial diagram (diagram-design port): a white-listed declarative diagram
739
+ * rendered as inline SVG by the browser. `kind` selects the layout grammar
740
+ * (coordinate kinds place nodes via x/y; rule kinds auto-layout from data),
741
+ * `variant` selects the skin, and `theme` overrides semantic tokens. The
742
+ * renderer enforces the editorial constraints — orthogonal connectors, 4px
743
+ * grid, complexity budget, focal accent budget — so the model cannot emit
744
+ * "AI slop" schematics.
745
+ */
746
+ export interface GenuiDiagram {
747
+ type: 'diagram'
748
+ /** The 27-type editorial vocabulary. */
749
+ kind: GenuiDiagramKind
750
+ /** Surface skin; default follows the host theme. */
751
+ variant?: GenuiDiagramVariant
752
+ /** Optional diagram title (Instrument Serif in the renderer). */
753
+ title?: string
754
+ /** Nodes (coordinate kinds use x/y/w/h; rule kinds may omit positions). */
755
+ nodes: GenuiDiagramNode[]
756
+ /** Connectors. */
757
+ edges?: GenuiDiagramEdge[]
758
+ /** Optional labelled regions grouping nodes (max 3). */
759
+ zones?: GenuiDiagramZone[]
760
+ /** Optional semantic-token overrides. */
761
+ theme?: GenuiDiagramTheme
762
+ }
763
+
764
+ /* ---------------- v1.6: ECharts ---------------- */
765
+
766
+ /** Preset chart kinds the `echart` node can build from `data`/`series` without
767
+ * a full ECharts option. Each maps to a themed option template. */
768
+ export type EChartPreset =
769
+ | 'bar' | 'line' | 'area' | 'pie' | 'scatter'
770
+ | 'radar' | 'gauge' | 'funnel' | 'treemap' | 'sankey' | 'graph' | 'heatmap' | 'bigline' | 'wordCloud'
771
+
772
+ /** ECharts node: renders a full ECharts chart. Two modes:
773
+ *
774
+ * 1. **Full option** (`option` set): the model provides a standard ECharts
775
+ * `EChartsCoreOption` object directly. This is the escape hatch for
776
+ * custom chart types, complex series, or advanced features (dataZoom,
777
+ * visualMap, etc.).
778
+ * 2. **Preset shorthand** (`preset` + `data`/`series`): the model provides
779
+ * the same simple `data`/`series` shape as the `chart` node, and the
780
+ * component builds a themed ECharts option automatically. This is the
781
+ * easy upgrade path: change `type: 'chart'` to `type: 'echart'` and add
782
+ * `preset`.
783
+ *
784
+ * The echarts engine is lazy-loaded (lib/assets/echarts.js) only when an
785
+ * `echart` node appears in a spec. */
786
+ export interface GenuiEChart {
787
+ type: 'echart'
788
+ /** Optional title shown above the chart. */
789
+ title?: string
790
+ /** Chart height in pixels (default 300). */
791
+ height?: number
792
+ /** Preset: builds the ECharts option from `data`/`series` when `option`
793
+ * is absent. */
794
+ preset?: EChartPreset
795
+ /** Simple data for preset mode (same shape as `chart.data`). */
796
+ data?: GenuiChartDatum[]
797
+ /** Multi-series for preset mode (same shape as `chart.series`). */
798
+ series?: Array<{ label: string; color?: string; data: GenuiChartDatum[] }>
799
+ /** Node/edge data for the `sankey` and `graph` presets. */
800
+ links?: Array<{ from: string; to: string; value?: number }>
801
+ /** Explicit categorical palette (hex) for preset mode. */
802
+ palette?: string[]
803
+ /** Full ECharts option object. When present, `preset`/`data`/`series` are
804
+ * ignored. This is a pass-through to `echarts.setOption`. */
805
+ option?: Record<string, unknown>
806
+ }
807
+
808
+ /** Parse the raw fence body as a GenuiSpec, or null when it is not one. */
809
+ export function parseGenuiSpec(raw: string): GenuiSpec | null {
810
+ const trimmed = raw.trim()
811
+ if (trimmed === '') return null
812
+ let value: unknown
813
+ try {
814
+ value = JSON.parse(trimmed)
815
+ } catch {
816
+ return null
817
+ }
818
+ if (isGenuiSpec(value)) return value
819
+ // Single-component roots are part of the documented fence vocabulary
820
+ // (e.g. {"type":"callout","tone":"info","title":"…","content":"…"} as the
821
+ // whole body) — wrap them into a one-item spec so the items-gated pipeline
822
+ // renders them. panel/append hoist onto the wrapper so panel routing keeps
823
+ // working.
824
+ return wrapSingleComponentRoot(value)
825
+ }
826
+
827
+ /**
828
+ * Is `value` a bare component root rather than an envelope spec?
829
+ *
830
+ * A root object carrying a `type` is the documented single-component
831
+ * shorthand. `items` alone cannot decide the root shape: for container
832
+ * components it holds children (row/col/grid/card/accordion) and for data
833
+ * components it holds records (steps/list/timeline/file-tree/breadcrumb), so
834
+ * both readings are components. A whitelisted `type` therefore wins over the
835
+ * envelope reading — `{"type":"steps","items":[{"title":"…"}]}` is a bare
836
+ * steps node, never a spec whose `items` are the step records (issue #172).
837
+ * A non-native `type` keeps the envelope reading, so a stray `type` field
838
+ * (`{"type":"genui","items":[…]}`) cannot swallow a real spec root.
839
+ */
840
+ export function isComponentRoot(value: unknown): boolean {
841
+ if (typeof value !== 'object' || value === null) return false
842
+ const v = value as { type?: unknown; items?: unknown }
843
+ if (typeof v.type !== 'string' || v.type === '') return false
844
+ if (!Array.isArray(v.items)) return true
845
+ return GENUI_NATIVE_TYPES.has(v.type)
846
+ }
847
+
848
+ /**
849
+ * Wrap a bare component object into a one-item spec. Returns null when
850
+ * `value` is not component-shaped (no usable `type`). Root-level spec fields
851
+ * found on the component are hoisted onto the wrapper: `panel`/`append` (so
852
+ * panel routing keeps working) and `title` when the component has no title of
853
+ * its own (`{"type":"steps","title":"…"}` means a titled block, not a steps
854
+ * field the schema would drop).
855
+ *
856
+ * The wrapper is a plain spec rather than a `col` node: a spec root is
857
+ * already rendered as a column by GenuiBlock, and leaving the wrapper free of
858
+ * a component `type` is what stops the guard from reading it as one more bare
859
+ * component root and nesting every fence inside a second column (issue #172).
860
+ */
861
+ export function wrapSingleComponentRoot(value: unknown): GenuiSpec | null {
862
+ if (typeof value !== 'object' || value === null) return null
863
+ const v = value as { type?: unknown; title?: unknown; panel?: unknown; append?: unknown }
864
+ if (typeof v.type !== 'string' || v.type === '') return null
865
+ const definition = GENUI_NATIVE_TYPES.has(v.type) ? COMPONENT_SCHEMAS[v.type] : undefined
866
+ const title = typeof v.title === 'string' && definition !== undefined && !('title' in definition.fields)
867
+ ? v.title
868
+ : undefined
869
+ const node: Record<string, unknown> = { ...(value as Record<string, unknown>) }
870
+ // The hoisted title moves to the wrapper, so the component must not keep a
871
+ // copy the schema would flag as an unknown field.
872
+ if (title !== undefined) delete node.title
873
+ return {
874
+ items: [node as unknown as GenuiNode],
875
+ ...(title !== undefined ? { title } : {}),
876
+ ...(v.panel === true ? { panel: true } : {}),
877
+ ...(v.append === true ? { append: true } : {}),
878
+ }
879
+ }
880
+
881
+ /**
882
+ * Basic structural guard: is this object a valid GenuiSpec?
883
+ *
884
+ * A bare component root is not a spec (it is wrapped instead), so
885
+ * `{"type":"steps","items":[…]}` answers false and `parseGenuiSpec` routes it
886
+ * through `wrapSingleComponentRoot` (issue #172).
887
+ */
888
+ export function isGenuiSpec(value: unknown): value is GenuiSpec {
889
+ if (typeof value !== 'object' || value === null) return false
890
+ if (isComponentRoot(value)) return false
891
+ const v = value as { items?: unknown; title?: unknown; gap?: unknown }
892
+ if (!Array.isArray(v.items)) return false
893
+ if (v.title !== undefined && typeof v.title !== 'string') return false
894
+ if (v.gap !== undefined && typeof v.gap !== 'number') return false
895
+ return true
896
+ }