@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,235 @@
1
+ /**
2
+ * GenuiBlock: renders a declarative GenUI spec (from a ```dsh-ui fence in an
3
+ * assistant reply) as real interactive components inline in the conversation.
4
+ * The component tree is white-listed and mapped to DOM directly — no raw HTML.
5
+ * The block shell holds the shared interaction state (answers registry,
6
+ * durable localStorage persistence, action debounce); the per-family
7
+ * components live in src/client/blocks/*.
8
+ */
9
+ import { renderInline } from './inline.ts'
10
+ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
11
+ import { useGenuiAction } from './action-context.ts'
12
+ import css from './GenuiBlock.module.css'
13
+ import { loadBlockState, saveBlockState } from './interaction-store.ts'
14
+ import { recordFence, recordInteraction } from './achievement-store.ts'
15
+ import { renderNode } from './blocks/render-node.tsx'
16
+ import type { AnswersState, GenuiBlockProps } from './blocks/state.ts'
17
+ import type { GenuiSpec } from './spec.ts'
18
+ import { compileSubmissionRegistry } from './submission-registry.ts'
19
+
20
+ export const GENUI_ACTION_DEBOUNCE_MS = 300
21
+
22
+ /**
23
+ * Wrap the harness action callback. DISCRETE gestures (button, checkbox,
24
+ * radio, switch, select, input, textarea, submit, quiz — every payload type
25
+ * but `slider`) deliver immediately: each is one user intent, and SKILL.md
26
+ * promises per-interaction `action` behavior, so collapsing them would
27
+ * silently drop interactions from the model's view. Only the CONTINUOUS
28
+ * gesture (slider drag) goes through the trailing debounce, keyed by action
29
+ * name + payload `id`, so one control's drag still collapses to its last
30
+ * value while distinct sliders sharing an action name never displace each
31
+ * other. Absent provider = v1 behavior (components are display-only,
32
+ * callback stays undefined). Pending timers are cleared on unmount so a
33
+ * drag that never fired does not leak into the next mount. Timers live in
34
+ * one stable map and read the latest handler through a ref, so provider
35
+ * updates cannot leave stale callbacks behind.
36
+ */
37
+ function useDebouncedAction(onAction: GenuiBlockProps['onAction'] | undefined): GenuiBlockProps['onAction'] {
38
+ const pending = useRef(new Map<string, ReturnType<typeof setTimeout>>())
39
+ const actionRef = useRef(onAction)
40
+ actionRef.current = onAction
41
+
42
+ useEffect(() => {
43
+ return () => {
44
+ for (const timer of pending.current.values()) clearTimeout(timer)
45
+ pending.current.clear()
46
+ }
47
+ }, [])
48
+
49
+ const debounced = useCallback((action: string, payload: Record<string, unknown>): void => {
50
+ if (payload.type !== 'slider') {
51
+ actionRef.current?.(action, payload)
52
+ return
53
+ }
54
+ const key = `${action}\u0000${typeof payload.id === 'string' ? payload.id : ''}`
55
+ const existing = pending.current.get(key)
56
+ if (existing !== undefined) clearTimeout(existing)
57
+ pending.current.set(key, setTimeout(() => {
58
+ pending.current.delete(key)
59
+ actionRef.current?.(action, payload)
60
+ }, GENUI_ACTION_DEBOUNCE_MS))
61
+ }, [])
62
+
63
+ return onAction === undefined ? undefined : debounced
64
+ }
65
+
66
+ /**
67
+ * Structural spec equality for the memo comparator: the fence path re-parses
68
+ * the body on every streaming chunk and produces a FRESH object even when the
69
+ * repaired content is unchanged (a chunk that closed no new component). The
70
+ * default shallow memo would then re-render the whole tree per chunk — up to
71
+ * ~200 full-tree renders for a max-size fence. Stringify equality makes the
72
+ * memo skip renders whose content did not actually change; the cost is one
73
+ * JSON.stringify per chunk (≤200 nodes, negligible next to a React tree
74
+ * reconciliation). `stateKey` already embeds the content fingerprint, so when
75
+ * both keys are equal and non-undefined the specs necessarily stringify
76
+ * equal — the stringify branch matters for the streaming path (stateKey
77
+ * undefined).
78
+ */
79
+ function specEquivalent(a: GenuiSpec, b: GenuiSpec): boolean {
80
+ if (a === b) return true
81
+ return JSON.stringify(a) === JSON.stringify(b)
82
+ }
83
+
84
+ /** Stateful implementation. Streaming state adopts its first durable key
85
+ * when the reply settles; switching an existing durable key starts fresh. */
86
+ function GenuiBlockInstance({ spec, stateKey, animateEntrance = true, initialState, onStateChange, onStateSnapshot }: GenuiBlockProps) {
87
+ const gap = spec.gap ?? 16
88
+ const onAction = useDebouncedAction(useGenuiAction())
89
+ const stateChangeRef = useRef(onStateChange)
90
+ stateChangeRef.current = onStateChange
91
+ const savesExternally = onStateChange !== undefined
92
+ // Grouped radios and grouped checkboxes record their local selections here;
93
+ // `submit` either grades radio-only papers locally or aggregates all form
94
+ // state into one action. Block-local state survives streaming/panel
95
+ // re-renders, and with a stateKey it also survives refresh/reopen.
96
+ const [persisted] = useState(() => initialState ?? (onStateChange !== undefined || stateKey === undefined ? null : loadBlockState(stateKey)))
97
+ const [answers, setAnswers] = useState<Record<string, string>>(persisted?.answers ?? {})
98
+ const [multiAnswers, setMultiAnswers] = useState<Record<string, string[]>>(persisted?.multiAnswers ?? {})
99
+ const [fields, setFields] = useState<Record<string, string>>(persisted?.fields ?? {})
100
+ const submissionRegistry = useMemo(() => compileSubmissionRegistry(spec), [spec])
101
+ const [locked, setLocked] = useState(persisted?.locked === true)
102
+ const [round, setRound] = useState(0)
103
+ // Secret (password) field ids: their values never persist and never join
104
+ // submit collection — the input itself stays masked and its own action
105
+ // still delivers the value on explicit user submit.
106
+ const [secretFields, setSecretFields] = useState<ReadonlySet<string>>(new Set())
107
+ const setAnswer = useCallback((group: string, choice: string) => {
108
+ setAnswers(prev => (prev[group] === choice ? prev : { ...prev, [group]: choice }))
109
+ }, [])
110
+ const setMultiAnswer = useCallback((group: string, choice: string, checked: boolean) => {
111
+ setMultiAnswers(prev => {
112
+ const current = prev[group] ?? []
113
+ const next = checked
114
+ ? current.includes(choice) ? current : [...current, choice]
115
+ : current.filter(item => item !== choice)
116
+ const hadGroup = Object.prototype.hasOwnProperty.call(prev, group)
117
+ if (hadGroup && next.length === current.length && next.every((item, index) => item === current[index])) {
118
+ return prev
119
+ }
120
+ // Keep an explicit empty array: it distinguishes "user cleared this
121
+ // group" from "group never had local state", so checked defaults do not
122
+ // reappear after a refresh.
123
+ return { ...prev, [group]: next }
124
+ })
125
+ }, [])
126
+ const setField = useCallback((id: string, value: string) => {
127
+ // Field state presence means "the user has touched this field" — a blank
128
+ // value is stored as '' instead of deleting the entry, so a user who
129
+ // CLEARS a model-provided default does not get the default back on the
130
+ // next mount. Blank values are still excluded from submit collection by
131
+ // SubmitNode's filledFields filter (and from persistence for secrets), so
132
+ // this does not change what the model receives.
133
+ setFields(prev => (prev[id] === value ? prev : { ...prev, [id]: value }))
134
+ }, [])
135
+ const registerSecretField = useCallback((id: string) => {
136
+ setSecretFields(prev => (prev.has(id) ? prev : new Set(prev).add(id)))
137
+ }, [])
138
+ const clear = useCallback(() => {
139
+ setAnswers({})
140
+ setMultiAnswers({})
141
+ setLocked(false)
142
+ setRound(r => r + 1) // radios remount (key carries the round) with clean selections
143
+ }, [])
144
+ const answersState = useMemo<AnswersState>(
145
+ () => ({
146
+ answers, multiAnswers, fields, secretFields, registry: submissionRegistry, locked, round,
147
+ setAnswer, setMultiAnswer, setField, registerSecretField, clear, setLocked,
148
+ }),
149
+ [answers, multiAnswers, fields, secretFields, submissionRegistry, locked, round, setAnswer, setMultiAnswer, setField, registerSecretField, clear],
150
+ )
151
+ const durableState = useMemo(() => {
152
+ const safeFields = Object.fromEntries(Object.entries(fields).filter(([id]) => !secretFields.has(id)))
153
+ return {
154
+ answers,
155
+ ...(Object.keys(multiAnswers).length > 0 ? { multiAnswers } : {}),
156
+ locked,
157
+ ...(Object.keys(safeFields).length > 0 ? { fields: safeFields } : {}),
158
+ }
159
+ }, [answers, multiAnswers, locked, fields, secretFields])
160
+ useEffect(() => {
161
+ onStateSnapshot?.(durableState)
162
+ }, [durableState, onStateSnapshot])
163
+ // Achievement telemetry: every emitted action counts as one interaction
164
+ // (the debounced emit fires once per real user action).
165
+ const trackedAction = useMemo(() => {
166
+ if (onAction === undefined) return undefined
167
+ return (action: string, payload: Record<string, unknown>): void => {
168
+ recordInteraction()
169
+ onAction(action, payload)
170
+ }
171
+ }, [onAction])
172
+ // Durable save (debounced 300ms — typing in a field fires per keystroke).
173
+ // Secret field values are stripped before writing: passwords never persist.
174
+ useEffect(() => {
175
+ if (savesExternally) {
176
+ stateChangeRef.current?.(durableState)
177
+ return
178
+ }
179
+ if (stateKey === undefined) return
180
+ const timer = setTimeout(() => saveBlockState(stateKey, durableState), 300)
181
+ return () => clearTimeout(timer)
182
+ }, [stateKey, durableState, savesExternally])
183
+ // Achievement telemetry (0.9.5): the store dedupes by spec fingerprint, so
184
+ // streaming re-renders and replays count once per distinct content.
185
+ useEffect(() => {
186
+ recordFence(spec)
187
+ }, [spec])
188
+ return (
189
+ <div className={css.block} data-genui>
190
+ {spec.title !== undefined && <div className={css.banner}>{renderInline(spec.title)}</div>}
191
+ <div className={css.col} style={{ gap: `${gap}px` }}>
192
+ {spec.items.map((c, i) => (
193
+ // Staggered reveal: each root item fades/slides in after its
194
+ // predecessors, so the block assembles piece by piece instead of
195
+ // popping in as one slab. Delay capped so long specs still settle
196
+ // quickly; prefers-reduced-motion disables it (see CSS).
197
+ <div
198
+ key={i}
199
+ className={css.reveal}
200
+ style={{
201
+ animationDelay: `${Math.min(i * 90, 720)}ms`,
202
+ ...(!animateEntrance || persisted !== null ? { animation: 'none' } : {}),
203
+ }}
204
+ onAnimationEnd={event => {
205
+ // Reattaching this DOM node must not replay its completed entrance.
206
+ if (event.target === event.currentTarget) event.currentTarget.style.animation = 'none'
207
+ }}
208
+ >
209
+ {renderNode(c, i, trackedAction, 0, answersState)}
210
+ </div>
211
+ ))}
212
+ </div>
213
+ </div>
214
+ )
215
+ }
216
+
217
+ /**
218
+ * Render a GenUI spec as an inline block. `stateKey` is also the durable
219
+ * component identity. A streaming instance adopts its first durable key so
220
+ * inputs and pending actions survive settling. Leaving an existing durable
221
+ * key remounts, keeping different blocks' interaction state isolated.
222
+ */
223
+ export const GenuiBlock = memo(function GenuiBlock(props: GenuiBlockProps) {
224
+ const [identity, setIdentity] = useState({ stateKey: props.stateKey, generation: 0 })
225
+ if (identity.stateKey !== props.stateKey) {
226
+ setIdentity({
227
+ stateKey: props.stateKey,
228
+ generation: identity.generation + (identity.stateKey === undefined ? 0 : 1),
229
+ })
230
+ }
231
+ return <GenuiBlockInstance key={identity.generation} {...props} />
232
+ }, (prev, next) => prev.stateKey === next.stateKey
233
+ && prev.animateEntrance === next.animateEntrance && prev.onStateChange === next.onStateChange
234
+ && prev.onStateSnapshot === next.onStateSnapshot
235
+ && specEquivalent(prev.spec, next.spec))
@@ -0,0 +1,171 @@
1
+ /* PlotBlock: SVG function plot inside a genui block. Design system v2
2
+ (0.5.0): same token vocabulary as GenuiBlock.module.css — this root
3
+ carries its own copy because the plot can also mount standalone. */
4
+
5
+ .block {
6
+ --dsl-g-radius-surface: 12px;
7
+ --dsl-g-radius-control: 8px;
8
+
9
+ --dsl-g-font-title: 13px;
10
+ --dsl-g-font-meta: 12px;
11
+ --dsl-g-font-data: 11px;
12
+
13
+ --dsl-g-gap-md: 12px;
14
+ --dsl-g-gap-sm: 8px;
15
+ --dsl-g-gap-xs: 4px;
16
+
17
+ --dsl-g-border: var(--dsw-alias-border-l1, var(--dsw-alias-border-l2, rgba(255, 255, 255, 0.12)));
18
+ --dsl-g-accent: var(--dsw-alias-state-business-primary, #4f8ef7);
19
+ --dsl-g-accent-soft: color-mix(in srgb, var(--dsl-g-accent) var(--dsl-g-tint), transparent);
20
+ --dsl-g-tint: 8%;
21
+ --dsl-g-tint-strong: 14%;
22
+ --dsl-g-font-mono: var(--ds-font-family-code, ui-monospace, 'SF Mono', monospace);
23
+
24
+ padding: 14px 16px;
25
+ background: var(--dsw-alias-bg-layer-1, var(--dsw-alias-markdown-code-block));
26
+ border: 1px solid var(--dsl-g-border);
27
+ border-radius: var(--dsl-g-radius-surface);
28
+ display: flex;
29
+ flex-direction: column;
30
+ gap: var(--dsl-g-gap-sm);
31
+ }
32
+
33
+ .title {
34
+ font-size: var(--dsl-g-font-title);
35
+ font-weight: 600;
36
+ letter-spacing: 0.01em;
37
+ color: var(--dsw-alias-label-primary);
38
+ }
39
+
40
+ .gridLine {
41
+ stroke: var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.07));
42
+ stroke-width: 1;
43
+ }
44
+
45
+ .axis {
46
+ stroke: var(--dsw-alias-label-tertiary);
47
+ stroke-width: 1;
48
+ }
49
+
50
+ .tick {
51
+ fill: var(--dsw-alias-label-tertiary);
52
+ font-size: var(--dsl-g-font-data);
53
+ font-family: var(--dsl-g-font-mono);
54
+ }
55
+
56
+ .line {
57
+ fill: none;
58
+ stroke: var(--dsl-g-accent);
59
+ stroke-width: 2;
60
+ stroke-linejoin: round;
61
+ stroke-linecap: round;
62
+ }
63
+
64
+ /* v2.9 draw shapes */
65
+ .area {
66
+ stroke: none;
67
+ fill-opacity: 0.16;
68
+ }
69
+ .scatterDot {
70
+ stroke: var(--dsw-alias-bg-base, #17171a);
71
+ stroke-width: 1;
72
+ }
73
+
74
+ .legend {
75
+ display: flex;
76
+ flex-wrap: wrap;
77
+ gap: var(--dsl-g-gap-md);
78
+ }
79
+
80
+ .legendItem {
81
+ display: inline-flex;
82
+ align-items: center;
83
+ gap: var(--dsl-g-gap-xs);
84
+ font-size: var(--dsl-g-font-meta);
85
+ color: var(--dsw-alias-label-secondary);
86
+ }
87
+
88
+ .legendSwatch {
89
+ width: 10px;
90
+ height: 3px;
91
+ border-radius: 2px;
92
+ background: var(--dsl-g-accent);
93
+ }
94
+
95
+ .empty {
96
+ padding: 14px 0;
97
+ color: var(--dsw-alias-label-tertiary);
98
+ font-size: var(--dsl-g-font-title);
99
+ display: flex;
100
+ flex-direction: column;
101
+ gap: var(--dsl-g-gap-xs);
102
+ }
103
+
104
+ .emptyRow {
105
+ font-family: var(--dsl-g-font-mono);
106
+ }
107
+
108
+ .surface { cursor: grab; touch-action: pan-y; }
109
+ /* The current x-domain only shows when it differs from the initial one, next
110
+ to the way back. Without this, an accidental zoom left the reader with a
111
+ domain nobody asked for and no visible affordance to restore it. */
112
+ .plotTools { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 0 0 4px; }
113
+ .plotHint { font-size: 12.5px; color: var(--dsw-alias-label-tertiary); font-variant-numeric: tabular-nums; }
114
+ .plotReset {
115
+ padding: 2px 9px;
116
+ border: 1px solid var(--dsw-alias-border-l2, rgba(255, 255, 255, 0.12));
117
+ border-radius: 999px;
118
+ background: transparent;
119
+ color: var(--dsw-alias-label-secondary);
120
+ font-family: inherit;
121
+ font-size: 12.5px;
122
+ cursor: pointer;
123
+ }
124
+ .plotReset:hover { color: var(--dsw-alias-label-primary); border-color: var(--dsw-alias-state-business-primary, #4f8ef7); }
125
+ .surface:active { cursor: grabbing; }
126
+
127
+ .sliders { display: flex; flex-direction: column; gap: var(--dsl-g-gap-sm); }
128
+ .sliderRow { display: flex; align-items: center; gap: var(--dsl-g-gap-sm); font-size: var(--dsl-g-font-meta); }
129
+ .sliderName { color: var(--dsw-alias-label-secondary); min-width: 90px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
130
+ .slider { flex: 1; accent-color: var(--dsl-g-accent); height: 4px; cursor: pointer; }
131
+ .sliderValue { color: var(--dsw-alias-label-primary); font-family: var(--dsl-g-font-mono); min-width: 34px; text-align: right; }
132
+
133
+ /* ---------- animation bar ---------- */
134
+
135
+ .animBar { display: flex; align-items: center; gap: var(--dsl-g-gap-md); }
136
+ .playBtn {
137
+ width: fit-content;
138
+ background: var(--dsl-g-accent-soft);
139
+ border: 1px solid color-mix(in srgb, var(--dsl-g-accent) 30%, transparent);
140
+ color: var(--dsl-g-accent);
141
+ border-radius: var(--dsl-g-radius-control);
142
+ padding: 6px 14px;
143
+ font-size: var(--dsl-g-font-title);
144
+ font-weight: 600;
145
+ font-family: inherit;
146
+ cursor: pointer;
147
+ }
148
+ .playBtn:hover { filter: brightness(1.1); }
149
+ .animTrack {
150
+ flex: 1;
151
+ height: 5px;
152
+ border-radius: 999px;
153
+ background: var(--dsw-alias-bg-layer-1);
154
+ box-shadow: inset 0 0 0 1px var(--dsl-g-border);
155
+ overflow: hidden;
156
+ }
157
+ .animFill { height: 100%; border-radius: 999px; background: var(--dsl-g-accent); }
158
+ .resetBtn {
159
+ width: fit-content;
160
+ background: transparent;
161
+ border: 1px solid var(--dsl-g-border);
162
+ color: var(--dsw-alias-label-secondary);
163
+ border-radius: var(--dsl-g-radius-control);
164
+ padding: 6px 14px;
165
+ font-size: var(--dsl-g-font-title);
166
+ font-family: inherit;
167
+ cursor: pointer;
168
+ }
169
+ .resetBtn:hover { border-color: var(--dsl-g-accent); color: var(--dsl-g-accent); }
170
+ .slidersHead { display: flex; align-items: center; justify-content: space-between; }
171
+ .slidersTitle { font-size: var(--dsl-g-font-meta); color: var(--dsw-alias-label-tertiary); }