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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,466 @@
1
+ /**
2
+ * ECharts node: renders a full ECharts chart from a declarative option
3
+ * object. The echarts engine is lazy-loaded (lib/assets/echarts.js) only when
4
+ * an `echart` node appears — the main client bundle never carries the engine.
5
+ *
6
+ * The `option` field accepts a standard ECharts `EChartsCoreOption`. For
7
+ * simple use cases the `preset` + `data` shorthand builds the option
8
+ * automatically: `preset: 'bar' | 'line' | 'pie' | 'scatter' | 'area'` maps
9
+ * to a themed option template that reads the same `data`/`series` shape as
10
+ * the `chart` node, so a model can upgrade a `chart` to ECharts by changing
11
+ * `type` to `echart` and adding `preset`.
12
+ * @module @changfenhuang/dsh-genui/client/EChartNode
13
+ */
14
+ import { renderInline } from './inline.ts'
15
+ import { useEffect, useRef, useState } from 'react'
16
+ import css from './GenuiBlock.module.css'
17
+ import { createChart as lazyCreateChart, type EChartsInstance } from './echarts-lazy.ts'
18
+ import { echartEngineFor } from './echarts-engine.ts'
19
+ import { CHART_COLORS } from './blocks/charts.tsx'
20
+ import type { GenuiEChart } from './spec.ts'
21
+
22
+ /**
23
+ * Categorical fallback palette. The host defines its `--dsw-static-*` tokens on
24
+ * `body`, not on `:root`, and ECharts renders to CANVAS (so a `var(--x)` string
25
+ * is meaningless to it — every colour must be resolved to a literal first).
26
+ * Reading only `document.documentElement` therefore returned '' for every
27
+ * series colour and collapsed multi-series charts to one accent hue; these
28
+ * fixed hues keep series distinguishable on any host.
29
+ */
30
+ export const SERIES_FALLBACK = [
31
+ '#679efe', '#4ed17e', '#f5b83d', '#f2707a', '#8b7ff0', '#3fc7d4', '#b7c8fe', '#9aa3b2',
32
+ ] as const
33
+
34
+ /**
35
+ * Read a host theme token, resolved from the ELEMENT first (custom properties
36
+ * inherit, so any node inside `body` sees the host sheet) and falling back to
37
+ * body/root for detached renders.
38
+ */
39
+ function readToken(name: string, fallback: string, el?: HTMLElement | null): string {
40
+ const hosts: Array<Element | null> = [el ?? null, typeof document === 'undefined' ? null : document.body, typeof document === 'undefined' ? null : document.documentElement]
41
+ for (const host of hosts) {
42
+ if (host === null) continue
43
+ const value = getComputedStyle(host).getPropertyValue(name).trim()
44
+ if (value !== '') return value
45
+ }
46
+ return fallback
47
+ }
48
+
49
+ /** Resolve the host accent and label colors for ECharts theming. */
50
+ function themeColors(el?: HTMLElement | null): {
51
+ accent: string
52
+ labelPrimary: string
53
+ labelSecondary: string
54
+ labelTertiary: string
55
+ border: string
56
+ bgLayer1: string
57
+ } {
58
+ return {
59
+ accent: readToken('--dsw-alias-state-business-primary', '#4f8ef7', el),
60
+ labelPrimary: readToken('--dsw-alias-label-primary', '#e6e6e6', el),
61
+ labelSecondary: readToken('--dsw-alias-label-secondary', '#a0a0a0', el),
62
+ labelTertiary: readToken('--dsw-alias-label-tertiary', '#6b6b6b', el),
63
+ border: readToken('--dsw-alias-border-l1', 'rgba(255,255,255,0.12)', el),
64
+ bgLayer1: readToken('--dsw-alias-bg-layer-1', '#1a1a1e', el),
65
+ }
66
+ }
67
+
68
+ /** Build a full ECharts option from a preset + the simple data/series shape.
69
+ * `el` is the chart's own container: theme tokens are resolved against it so
70
+ * host colours are found wherever the host defines them. */
71
+ function presetOption(node: GenuiEChart, el?: HTMLElement | null): Record<string, unknown> {
72
+ const t = themeColors(el)
73
+ // Each palette slot carries its own fallback hue: if the host lacks the
74
+ // static tokens, series must still be distinguishable (the old code fell
75
+ // back to the accent for every slot, so every chart came out one colour).
76
+ const colors = node.palette !== undefined && node.palette.length > 0
77
+ ? [...node.palette]
78
+ : CHART_COLORS.map((c, i) =>
79
+ readToken(c.replace('var(', '').replace(')', ''), SERIES_FALLBACK[i % SERIES_FALLBACK.length]!, el))
80
+ const data = node.data ?? []
81
+ const series = node.series
82
+
83
+ // Shared tooltip base: renderMode 'richText' prevents ECharts from writing
84
+ // tooltip content via innerHTML — labels/formatters are model output and
85
+ // must never reach the HTML parser.
86
+ const tt = (extra: Record<string, unknown> = {}): Record<string, unknown> => ({
87
+ renderMode: 'richText',
88
+ backgroundColor: t.bgLayer1,
89
+ borderColor: t.border,
90
+ textStyle: { color: t.labelPrimary },
91
+ ...extra,
92
+ })
93
+
94
+ const base = {
95
+ color: colors,
96
+ textStyle: { color: t.labelSecondary, fontFamily: 'inherit' },
97
+ backgroundColor: 'transparent',
98
+ grid: { left: 48, right: 16, top: 24, bottom: 32 },
99
+ tooltip: tt({ trigger: 'item' }),
100
+ }
101
+
102
+ switch (node.preset) {
103
+ case 'wordCloud': {
104
+ return {
105
+ ...base,
106
+ series: [{
107
+ type: 'wordCloud',
108
+ sizeRange: [16, 64],
109
+ rotationRange: [0, 0],
110
+ gridSize: 8,
111
+ textStyle: { fontFamily: 'sans-serif' },
112
+ data: data.map((d, i) => ({ name: d.label, value: d.value, textStyle: { color: colors[i % colors.length] } })),
113
+ }],
114
+ }
115
+ }
116
+ case 'pie': {
117
+ return {
118
+ ...base,
119
+ tooltip: tt({ trigger: 'item', formatter: '{b}: {c} ({d}%)' }),
120
+ legend: { bottom: 0, textStyle: { color: t.labelTertiary } },
121
+ series: [{
122
+ type: 'pie',
123
+ radius: ['40%', '70%'],
124
+ avoidLabelOverlap: true,
125
+ itemStyle: { borderRadius: 6, borderColor: t.bgLayer1, borderWidth: 2 },
126
+ label: { color: t.labelSecondary },
127
+ data: data.map(d => ({ name: d.label, value: d.value })),
128
+ }],
129
+ }
130
+ }
131
+ case 'scatter': {
132
+ // xAxis is 'category' so string labels (e.g. 「一月」) render correctly;
133
+ // the previous `type: 'value'` xAxis could not plot non-numeric labels.
134
+ return {
135
+ ...base,
136
+ tooltip: tt({ trigger: 'item' }),
137
+ xAxis: { type: 'category', data: data.map(d => d.label), axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
138
+ yAxis: { type: 'value', axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
139
+ series: [{
140
+ type: 'scatter',
141
+ symbolSize: 10,
142
+ data: data.map(d => d.value),
143
+ }],
144
+ }
145
+ }
146
+ case 'area': {
147
+ return {
148
+ ...base,
149
+ tooltip: tt({ trigger: 'axis' }),
150
+ xAxis: { type: 'category', data: data.map(d => d.label), axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary } },
151
+ yAxis: { type: 'value', axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
152
+ series: (series ?? [{ label: '', data }]).map((s, i) => ({
153
+ name: s.label,
154
+ type: 'line',
155
+ smooth: true,
156
+ areaStyle: { opacity: 0.15 },
157
+ data: s.data.map(d => d.value),
158
+ ...optItemStyleColor(s.color, i, series),
159
+ })),
160
+ legend: series !== undefined ? { bottom: 0, textStyle: { color: t.labelTertiary } } : undefined,
161
+ }
162
+ }
163
+ case 'line': {
164
+ return {
165
+ ...base,
166
+ tooltip: tt({ trigger: 'axis' }),
167
+ xAxis: { type: 'category', data: data.map(d => d.label), axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary } },
168
+ yAxis: { type: 'value', axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
169
+ series: (series ?? [{ label: '', data }]).map((s, i) => ({
170
+ name: s.label,
171
+ type: 'line',
172
+ smooth: true,
173
+ showSymbol: true,
174
+ symbolSize: 6,
175
+ data: s.data.map(d => d.value),
176
+ ...optItemStyleColor(s.color, i, series),
177
+ })),
178
+ legend: series !== undefined ? { bottom: 0, textStyle: { color: t.labelTertiary } } : undefined,
179
+ }
180
+ }
181
+ case 'radar': {
182
+ // Indicators come from the first series' labels; each series is one
183
+ // polygon. `data` alone is treated as a single unnamed series.
184
+ const entries = series ?? [{ label: '', data }]
185
+ const indicators = (entries[0]?.data ?? []).map(d => ({ name: d.label, max: undefined as number | undefined }))
186
+ const max = Math.max(...entries.flatMap(e => e.data.map(d => Number(d.value) || 0)), 1)
187
+ return {
188
+ ...base,
189
+ tooltip: tt({ trigger: 'item' }),
190
+ legend: { bottom: 0, textStyle: { color: t.labelTertiary } },
191
+ radar: {
192
+ indicator: indicators.map(i => ({ name: i.name, max: Math.ceil(max * 1.1) })),
193
+ splitLine: { lineStyle: { color: t.border } },
194
+ splitArea: { show: false },
195
+ axisLine: { lineStyle: { color: t.border } },
196
+ axisName: { color: t.labelTertiary },
197
+ },
198
+ series: [{
199
+ type: 'radar',
200
+ symbolSize: 5,
201
+ areaStyle: { opacity: 0.12 },
202
+ data: entries.map((e, i) => ({
203
+ name: e.label,
204
+ value: e.data.map(d => Number(d.value) || 0),
205
+ ...optItemStyleColor(e.color, i, series),
206
+ })),
207
+ }],
208
+ }
209
+ }
210
+ case 'gauge': {
211
+ // One gauge per datum (usually a single KPI).
212
+ return {
213
+ ...base,
214
+ tooltip: tt({ trigger: 'item' }),
215
+ series: data.slice(0, 4).map((d, i) => ({
216
+ type: 'gauge',
217
+ startAngle: 210,
218
+ endAngle: -30,
219
+ min: 0,
220
+ max: Math.max(Number(d.value) || 0, 100),
221
+ center: data.length > 1 ? [`${(i + 0.5) * (100 / Math.min(data.length, 4))}%`, '58%'] : ['50%', '58%'],
222
+ radius: data.length > 1 ? '62%' : '82%',
223
+ progress: { show: true, width: 12 },
224
+ axisLine: { lineStyle: { width: 12, color: [[1, t.border]] } },
225
+ axisTick: { show: false },
226
+ splitLine: { show: false },
227
+ axisLabel: { show: false },
228
+ pointer: { show: false },
229
+ title: { offsetCenter: [0, '32%'], color: t.labelTertiary, fontSize: 12 },
230
+ detail: { valueAnimation: true, fontSize: 26, offsetCenter: [0, '2%'], formatter: '{value}', color: t.labelPrimary },
231
+ data: [{ value: Number(d.value) || 0, name: d.label }],
232
+ })),
233
+ }
234
+ }
235
+ case 'funnel': {
236
+ return {
237
+ ...base,
238
+ tooltip: tt({ trigger: 'item', formatter: '{b}: {c}' }),
239
+ legend: { bottom: 0, textStyle: { color: t.labelTertiary } },
240
+ series: [{
241
+ type: 'funnel',
242
+ left: '8%',
243
+ width: '84%',
244
+ top: 16,
245
+ bottom: 40,
246
+ gap: 2,
247
+ label: { position: 'inside', color: '#fff', formatter: '{b} {c}' },
248
+ itemStyle: { borderWidth: 0 },
249
+ data: data.map(d => ({ name: d.label, value: Number(d.value) || 0 })),
250
+ }],
251
+ }
252
+ }
253
+ case 'treemap': {
254
+ return {
255
+ ...base,
256
+ tooltip: tt({ trigger: 'item', formatter: '{b}: {c}' }),
257
+ series: [{
258
+ type: 'treemap',
259
+ roam: false,
260
+ nodeClick: false,
261
+ breadcrumb: { show: false },
262
+ upperLabel: { show: false },
263
+ label: { color: t.labelPrimary },
264
+ itemStyle: { borderColor: t.bgLayer1, borderWidth: 2, gapWidth: 2 },
265
+ data: data.map(d => ({ name: d.label, value: Number(d.value) || 0 })),
266
+ }],
267
+ }
268
+ }
269
+ case 'sankey': {
270
+ const links = node.links ?? []
271
+ const names = data.length > 0
272
+ ? data.map(d => d.label)
273
+ : [...new Set(links.flatMap(l => [l.from, l.to]))]
274
+ return {
275
+ ...base,
276
+ tooltip: tt({ trigger: 'item' }),
277
+ series: [{
278
+ type: 'sankey',
279
+ left: 8,
280
+ right: 8,
281
+ top: 12,
282
+ bottom: 12,
283
+ nodeGap: 12,
284
+ lineStyle: { color: 'gradient', curveness: 0.5, opacity: 0.32 },
285
+ label: { color: t.labelSecondary },
286
+ emphasis: { focus: 'adjacency' },
287
+ data: names.map(name => ({ name })),
288
+ links: links.map(l => ({ source: l.from, target: l.to, value: l.value ?? 1 })),
289
+ }],
290
+ }
291
+ }
292
+ case 'graph': {
293
+ const links = node.links ?? []
294
+ const names = data.length > 0
295
+ ? data.map(d => d.label)
296
+ : [...new Set(links.flatMap(l => [l.from, l.to]))]
297
+ // Node size follows its degree, so hubs read as hubs without extra data.
298
+ const degree = new Map<string, number>()
299
+ for (const l of links) {
300
+ degree.set(l.from, (degree.get(l.from) ?? 0) + 1)
301
+ degree.set(l.to, (degree.get(l.to) ?? 0) + 1)
302
+ }
303
+ return {
304
+ ...base,
305
+ tooltip: tt({ trigger: 'item' }),
306
+ series: [{
307
+ type: 'graph',
308
+ layout: 'force',
309
+ roam: true,
310
+ label: { show: true, color: t.labelSecondary, fontSize: 11 },
311
+ force: { repulsion: 200, edgeLength: 80 },
312
+ lineStyle: { color: t.border, curveness: 0.1 },
313
+ emphasis: { focus: 'adjacency' },
314
+ data: names.map(name => ({ name, symbolSize: 16 + (degree.get(name) ?? 0) * 5 })),
315
+ links: links.map(l => ({ source: l.from, target: l.to })),
316
+ }],
317
+ }
318
+ }
319
+ case 'heatmap': {
320
+ // Rows come from `series` (one row per entry), columns from the first
321
+ // row's labels — the same shape the other presets use.
322
+ const rows = series ?? [{ label: '', data }]
323
+ const cols = (rows[0]?.data ?? []).map(d => d.label)
324
+ const values = rows.flatMap((row, y) => row.data.map((d, x) => [x, y, Number(d.value) || 0]))
325
+ const max = Math.max(...values.map(v => v[2] as number), 1)
326
+ return {
327
+ ...base,
328
+ tooltip: tt({ trigger: 'item', position: 'top' }),
329
+ grid: { left: 64, right: 16, top: 16, bottom: 48 },
330
+ xAxis: { type: 'category', data: cols, splitArea: { show: false }, axisLabel: { color: t.labelTertiary }, axisLine: { lineStyle: { color: t.border } } },
331
+ yAxis: { type: 'category', data: rows.map(r => r.label), splitArea: { show: false }, axisLabel: { color: t.labelTertiary }, axisLine: { lineStyle: { color: t.border } } },
332
+ visualMap: { min: 0, max, calculable: true, orient: 'horizontal', left: 'center', bottom: 0, textStyle: { color: t.labelTertiary } },
333
+ series: [{ type: 'heatmap', data: values, label: { show: values.length <= 60, color: t.labelPrimary }, itemStyle: { borderColor: t.bgLayer1, borderWidth: 2 } }],
334
+ }
335
+ }
336
+ case 'bigline': {
337
+ // Long series: symbols off, area wash, inside + slider zoom.
338
+ return {
339
+ ...base,
340
+ tooltip: tt({ trigger: 'axis' }),
341
+ grid: { left: 48, right: 20, top: 24, bottom: 56 },
342
+ dataZoom: [
343
+ { type: 'inside', start: 0, end: 45 },
344
+ { type: 'slider', height: 16, bottom: 8, borderColor: t.border, textStyle: { color: t.labelTertiary } },
345
+ ],
346
+ xAxis: { type: 'category', boundaryGap: false, data: data.map(d => d.label), axisLabel: { color: t.labelTertiary }, axisLine: { lineStyle: { color: t.border } } },
347
+ yAxis: { type: 'value', axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
348
+ series: (series ?? [{ label: '', data }]).map((s2, i) => ({
349
+ name: s2.label,
350
+ type: 'line',
351
+ smooth: true,
352
+ showSymbol: false,
353
+ areaStyle: { opacity: 0.12 },
354
+ data: s2.data.map(d => d.value),
355
+ ...optItemStyleColor(s2.color, i, series),
356
+ })),
357
+ legend: series !== undefined ? { bottom: 26, textStyle: { color: t.labelTertiary } } : undefined,
358
+ }
359
+ }
360
+ default: {
361
+ // 'bar' or unspecified
362
+ return {
363
+ ...base,
364
+ tooltip: tt({ trigger: 'axis', axisPointer: { type: 'shadow' } }),
365
+ xAxis: { type: 'category', data: data.map(d => d.label), axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary } },
366
+ yAxis: { type: 'value', axisLine: { lineStyle: { color: t.border } }, axisLabel: { color: t.labelTertiary }, splitLine: { lineStyle: { color: t.border, opacity: 0.5 } } },
367
+ series: (series ?? [{ label: '', data }]).map(s => ({
368
+ name: s.label,
369
+ type: 'bar',
370
+ barMaxWidth: 40,
371
+ itemStyle: { borderRadius: [4, 4, 2, 2], ...(s.color !== undefined ? { color: s.color } : {}) },
372
+ data: s.data.map(d => d.value),
373
+ })),
374
+ legend: series !== undefined ? { bottom: 0, textStyle: { color: t.labelTertiary } } : undefined,
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ /** Per-series itemStyle.color override when the preset series declares one
381
+ * (aligns with the `chart` node which respects `series[].color`). */
382
+ function optItemStyleColor(color: string | undefined, _i: number, _series: unknown): Record<string, unknown> {
383
+ return color !== undefined ? { itemStyle: { color } } : {}
384
+ }
385
+
386
+ export function EChartNode({ node }: { node: GenuiEChart }) {
387
+ const ref = useRef<HTMLDivElement | null>(null)
388
+ const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading')
389
+ const instanceRef = useRef<EChartsInstance | null>(null)
390
+
391
+ useEffect(() => {
392
+ let alive = true
393
+ const el = ref.current
394
+ if (el === null) return
395
+
396
+ // Full `option` wins over preset shorthand.
397
+ const option = node.option ?? presetOption(node, el)
398
+
399
+ const engine = echartEngineFor(node) === 'echarts-full' ? 'full' : 'core'
400
+ void lazyCreateChart(el, option, { height: node.height ?? 300 }, engine).then((inst) => {
401
+ if (!alive) {
402
+ inst.dispose()
403
+ return
404
+ }
405
+ instanceRef.current = inst
406
+ setStatus('ready')
407
+ }).catch(() => {
408
+ if (alive) setStatus('error')
409
+ })
410
+
411
+ return () => {
412
+ alive = false
413
+ instanceRef.current?.dispose()
414
+ instanceRef.current = null
415
+ }
416
+ // eslint-disable-next-line react-hooks/exhaustive-deps
417
+ }, [])
418
+
419
+ // Resize observer: keep the chart responsive.
420
+ useEffect(() => {
421
+ if (status !== 'ready') return
422
+ const el = ref.current
423
+ if (el === null) return
424
+ const ro = new ResizeObserver(() => {
425
+ instanceRef.current?.resize()
426
+ })
427
+ ro.observe(el)
428
+ return () => { ro.disconnect() }
429
+ }, [status])
430
+
431
+ // Update option when the node changes (model re-render). `status` is in
432
+ // deps so that when the engine finishes loading (status: 'loading' →
433
+ // 'ready'), this effect re-runs and applies the LATEST option — without
434
+ // it, a spec update that arrived during engine load would be lost forever
435
+ // (the mount effect captured the old option, and this effect would have
436
+ // returned early when status was 'loading' and never re-run).
437
+ useEffect(() => {
438
+ if (status !== 'ready' || instanceRef.current === null) return
439
+ const option = node.option ?? presetOption(node, ref.current)
440
+ instanceRef.current.setOption(option, true)
441
+ // eslint-disable-next-line react-hooks/exhaustive-deps
442
+ }, [node, status])
443
+
444
+ if (status === 'error') {
445
+ return (
446
+ <div className={css.echartFallback} data-genui-echart>
447
+ <div className={css.echartErr}>ECharts 渲染失败</div>
448
+ {node.title !== undefined && <div className={css.echartHint}>{renderInline(node.title)}</div>}
449
+ </div>
450
+ )
451
+ }
452
+
453
+ return (
454
+ <div className={css.echartWrap} data-genui-echart>
455
+ {node.title !== undefined && <div className={css.echartTitle}>{renderInline(node.title)}</div>}
456
+ <div
457
+ ref={ref}
458
+ className={css.echartCanvas}
459
+ style={{ height: `${node.height ?? 300}px` }}
460
+ role="img"
461
+ aria-label={node.title ?? 'ECharts chart'}
462
+ />
463
+ {status === 'loading' && <div className={css.echartHint}>加载图表…</div>}
464
+ </div>
465
+ )
466
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Rendering error boundary: a component render failure inside one GenUI
3
+ * block must never take down the whole chat surface (pre-2026-08-09 builds
4
+ * crashed the entire conversation tree on a missing API). Every fence,
5
+ * toolview card and panel body renders under this boundary; on error the
6
+ * block degrades to a compact inline alert instead of unmounting the tree.
7
+ * @module @changfenhuang/dsh-genui/client
8
+ */
9
+
10
+ import { Component, type ErrorInfo, type ReactNode } from 'react'
11
+ import { t } from './i18n/index.ts'
12
+
13
+ interface ErrorBoundaryProps {
14
+ /** What the block is, shown in the fallback alert (fence / tool / panel). */
15
+ label?: string
16
+ children: ReactNode
17
+ }
18
+
19
+ interface ErrorBoundaryState {
20
+ error: Error | null
21
+ }
22
+
23
+ const fallbackStyle: Record<string, string | number> = {
24
+ display: 'flex',
25
+ flexDirection: 'column',
26
+ gap: '4px',
27
+ padding: '8px 12px',
28
+ margin: '4px 0',
29
+ borderRadius: '8px',
30
+ border: '1px solid rgba(127,127,127,0.35)',
31
+ background: 'rgba(127,127,127,0.08)',
32
+ color: 'inherit',
33
+ fontSize: '12px',
34
+ lineHeight: 1.5,
35
+ fontFamily: 'inherit',
36
+ }
37
+
38
+ /**
39
+ * Class boundary because function components cannot catch their own subtree
40
+ * errors. `getDerivedStateFromError` flips to the fallback render; the error
41
+ * itself is logged (and its message surfaced in the alert) so the user sees
42
+ * a hint instead of a silent white region.
43
+ */
44
+ export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
45
+ override state: ErrorBoundaryState = { error: null }
46
+
47
+ static getDerivedStateFromError(error: Error): ErrorBoundaryState {
48
+ return { error }
49
+ }
50
+
51
+ override componentDidCatch(error: Error, info: ErrorInfo): void {
52
+ console.error('[dsh-genui] render failed:', error, info.componentStack ?? '')
53
+ }
54
+
55
+ override render(): ReactNode {
56
+ const { error } = this.state
57
+ if (error === null) return this.props.children
58
+ return (
59
+ <div style={fallbackStyle} role="alert" data-genui-error>
60
+ <span style={{ fontWeight: 600 }}>
61
+ {t('err.boundary.body', { label: this.props.label ?? t('err.boundary.generic') })}
62
+ </span>
63
+ <span style={{ opacity: 0.75, overflowWrap: 'anywhere' }}>{error.message}</span>
64
+ </div>
65
+ )
66
+ }
67
+ }