@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,492 @@
1
+ /**
2
+ * PlotBlock: renders one or more math functions as an SVG line chart. The
3
+ * model supplies expressions (e.g. "sin(x)") which are evaluated by the
4
+ * SafeMath white-listed evaluator — never eval — and sampled into polylines.
5
+ * Pure SVG: linear x/y scales computed in place, no chart library.
6
+ *
7
+ * v2 interactivity: series may declare `params` (e.g. {a: 2} in "a*sin(x)")
8
+ * which render as live sliders under the chart — dragging re-samples the
9
+ * curve in place. The plot itself supports drag-to-pan and wheel-to-zoom.
10
+ */
11
+ import { renderInline } from './inline.ts'
12
+ import { memo, useEffect, useRef, useState, type ReactNode } from 'react'
13
+ import { sampleExpr } from './safe-math.ts'
14
+ import css from './PlotBlock.module.css'
15
+
16
+ export interface PlotSeriesParam {
17
+ name: string
18
+ value: number
19
+ min?: number | undefined
20
+ max?: number | undefined
21
+ step?: number | undefined
22
+ /** v1.5: animate this parameter from its initial value to this target. */
23
+ animateTo?: number | undefined
24
+ /** Animation duration in ms (default 4000). */
25
+ durationMs?: number | undefined
26
+ /** Loop the animation (default false). */
27
+ loop?: boolean | undefined
28
+ }
29
+
30
+ export interface PlotSeries {
31
+ /** Math expression in x. */
32
+ expr: string
33
+ /** Optional label shown in a legend row. */
34
+ label?: string | undefined
35
+ /** Stroke color; defaults to the accent token (multi-series auto-assign). */
36
+ color?: string | undefined
37
+ /** v2.9 draw shape: line (default), area (fill to baseline), scatter. */
38
+ kind?: 'line' | 'area' | 'scatter' | undefined
39
+ /** v2: adjustable parameters, one slider each, live re-render. */
40
+ params?: PlotSeriesParam[] | undefined
41
+ }
42
+
43
+ /** Categorical palette for multi-series plots: host static tokens only
44
+ * (design system v2 — same families as the chart/avatar palettes in
45
+ * GenuiBlock, no off-theme hexes). CSS custom properties resolve inside
46
+ * inline `style` on SVG strokes and legend swatches. */
47
+ export const PLOT_COLORS = [
48
+ 'var(--dsw-static-deepseek-400)',
49
+ 'var(--dsw-static-green-400)',
50
+ 'var(--dsw-static-amber-400)',
51
+ 'var(--dsw-static-red-400)',
52
+ 'var(--dsw-static-blue-450)',
53
+ 'var(--dsw-static-deepseek-450)',
54
+ 'var(--dsw-static-neutral-bluish-400)',
55
+ 'var(--dsw-static-deepseek-300)',
56
+ ]
57
+
58
+ /** Series color: explicit wins; multi-series auto-assign from the palette. */
59
+ const seriesColor = (i: number, n: number, c?: string): string | undefined =>
60
+ c ?? (n > 1 ? PLOT_COLORS[i % PLOT_COLORS.length] : undefined)
61
+
62
+ export interface PlotBlockProps {
63
+ /** Functions to draw, in draw order. */
64
+ series: PlotSeries[]
65
+ /** Horizontal range, inclusive. */
66
+ xMin?: number | undefined
67
+ xMax?: number | undefined
68
+ /** Vertical bounds; omitted = auto-fit to the sampled data. */
69
+ yMin?: number | undefined
70
+ yMax?: number | undefined
71
+ /** Chart title. */
72
+ title?: string | undefined
73
+ }
74
+
75
+ const WIDTH = 480
76
+ const HEIGHT = 220
77
+ const PAD_L = 34
78
+ const PAD_R = 10
79
+ const PAD_T = 14
80
+ const PAD_B = 24
81
+ const SAMPLES = 240
82
+
83
+ /** Auto-fit y-range from finite samples with a small margin. */
84
+ function fitY(points: ReadonlyArray<readonly [number, number]>): [number, number] {
85
+ let lo = Infinity
86
+ let hi = -Infinity
87
+ for (const [, y] of points) {
88
+ if (y < lo) lo = y
89
+ if (y > hi) hi = y
90
+ }
91
+ if (!Number.isFinite(lo) || !Number.isFinite(hi)) return [-1, 1]
92
+ if (lo === hi) { lo -= 1; hi += 1 }
93
+ const pad = (hi - lo) * 0.08
94
+ return [lo - pad, hi + pad]
95
+ }
96
+
97
+ function niceTicks(min: number, max: number, count = 5): number[] {
98
+ const span = max - min
99
+ if (!Number.isFinite(span) || span <= 0) return []
100
+ const step = Math.pow(10, Math.floor(Math.log10(span / count)))
101
+ const err = span / count / step
102
+ const mult = err >= 7.5 ? 10 : err >= 3.5 ? 5 : err >= 1.5 ? 2 : 1
103
+ const size = step * mult
104
+ const ticks: number[] = []
105
+ for (let v = Math.ceil(min / size) * size; v <= max + size * 1e-9; v += size) {
106
+ ticks.push(Math.round(v * 1e9) / 1e9)
107
+ }
108
+ return ticks
109
+ }
110
+
111
+ function formatTick(v: number): string {
112
+ if (Math.abs(v) >= 1e5 || (Math.abs(v) < 1e-3 && v !== 0)) return v.toExponential(1)
113
+ return String(Math.round(v * 100) / 100)
114
+ }
115
+
116
+ /** Polyline with manual points sync: React 18 does not reliably update the
117
+ * SVG `points` attribute when only the value changes (the element is diffed
118
+ * but the attribute write is skipped), so this wrapper writes it through a
119
+ * ref on every render. */
120
+ function Polyline({ points, className, color }: { points: string; className: string | undefined; color?: string | undefined }) {
121
+ // React 18 does not reliably update the SVG `points` attribute when the
122
+ // value changes on an existing element (the DOM write is skipped). Keying
123
+ // the element on a prefix of the points string forces a fresh element per
124
+ // distinct curve, which sidesteps the update entirely. The prefix must
125
+ // change whenever the curve changes: include the first and last point.
126
+ const key = points.length > 40 ? `${points.slice(0, 16)}|${points.slice(-16)}` : points
127
+ return (
128
+ <polyline
129
+ key={key}
130
+ points={points}
131
+ className={className}
132
+ style={color !== undefined ? { stroke: color } : undefined}
133
+ />
134
+ )
135
+ }
136
+
137
+ /** Render one plot: grid, axes, one polyline per series, legend, sliders. */
138
+ export const PlotBlock = memo(function PlotBlock({
139
+ series, xMin: propXMin = -5, xMax: propXMax = 5, yMin, yMax, title,
140
+ }: PlotBlockProps) {
141
+ // Pan/zoom view state: x range plus a LOCKED y range. The y range is fit
142
+ // ONCE from the initial parameters and then fixed, so dragging a parameter
143
+ // slider changes the curve's SHAPE (amplitude/slope) instead of rescaling
144
+ // the axis every time — the user sees the function move, not the numbers.
145
+ const [view, setView] = useState(() => {
146
+ // Fit y from the default parameter values (the same fallback the render
147
+ // path uses), so the initial axis already frames the default curve.
148
+ const defaults: Record<string, number> = {}
149
+ for (const [si, s] of series.entries()) {
150
+ for (const p of s.params ?? []) {
151
+ if (p !== null && p !== undefined) defaults[`${si}:${p.name}`] = p.value
152
+ }
153
+ }
154
+ const probe = series.map((s, si) => {
155
+ const p: Record<string, number> = {}
156
+ for (const param of s.params ?? []) {
157
+ if (param === null || param === undefined) continue
158
+ p[param.name] = defaults[`${si}:${param.name}`] ?? param.value
159
+ }
160
+ return sampleExpr(s.expr, propXMin, propXMax, SAMPLES, p)
161
+ })
162
+ const [autoLo, autoHi] = fitY(probe.flatMap(p => p))
163
+ return {
164
+ xMin: propXMin,
165
+ xMax: propXMax,
166
+ yMin: yMin ?? autoLo,
167
+ yMax: yMax ?? autoHi,
168
+ }
169
+ })
170
+ const dragRef = useRef<{ startX: number; startY: number; xMin: number; xMax: number } | null>(null)
171
+
172
+ // Parameter values keyed by series index + param name; slider drag updates.
173
+ // Deliberately starts EMPTY: the initializer must never read series props
174
+ // (a parent re-mount can rebuild them mid-interaction), so every value
175
+ // falls back to the param's declared default at render time. This makes
176
+ // remounts during slider drags crash-proof.
177
+ const [params, setParams] = useState<Record<string, number>>(() => ({}))
178
+
179
+ // Animation: pick the first parameter that declares animateTo. A play/pause
180
+ // button drives it from its current value to animateTo over durationMs
181
+ // using requestAnimationFrame; the curve moves in place (y axis is locked).
182
+ const animParam = (() => {
183
+ for (const [si, s] of series.entries()) {
184
+ for (const p of s.params ?? []) {
185
+ if (p !== null && p !== undefined && p.animateTo !== undefined) {
186
+ return { si, param: p }
187
+ }
188
+ }
189
+ }
190
+ return null
191
+ })()
192
+ const [playing, setPlaying] = useState(false)
193
+ const [animProgress, setAnimProgress] = useState(0)
194
+ const animRef = useRef<number | null>(null)
195
+
196
+ useEffect(() => {
197
+ if (!playing || animParam === null) return
198
+ const key = `${animParam.si}:${animParam.param.name}`
199
+ let cycleFrom = params[key] ?? animParam.param.value
200
+ const to = animParam.param.animateTo!
201
+ const duration = Math.max(0, animParam.param.durationMs ?? 4000)
202
+ let cycleStart = performance.now()
203
+ const tick = (now: number): void => {
204
+ const t = duration === 0 ? 1 : Math.min(1, Math.max(0, (now - cycleStart) / duration))
205
+ const eased = 1 - Math.pow(1 - t, 3) // easeOutCubic
206
+ const value = cycleFrom + (to - cycleFrom) * eased
207
+ setParams(prev => ({ ...prev, [key]: value }))
208
+ setAnimProgress(t)
209
+ if (t < 1) {
210
+ animRef.current = requestAnimationFrame(tick)
211
+ } else if (animParam.param.loop === true) {
212
+ cycleFrom = animParam.param.value
213
+ cycleStart = now
214
+ setParams(prev => ({ ...prev, [key]: cycleFrom }))
215
+ setAnimProgress(0)
216
+ animRef.current = requestAnimationFrame(tick)
217
+ } else {
218
+ animRef.current = null
219
+ setPlaying(false)
220
+ }
221
+ }
222
+ animRef.current = requestAnimationFrame(tick)
223
+ return () => {
224
+ if (animRef.current !== null) cancelAnimationFrame(animRef.current)
225
+ animRef.current = null
226
+ }
227
+ }, [
228
+ playing,
229
+ animParam?.si,
230
+ animParam?.param.name,
231
+ animParam?.param.value,
232
+ animParam?.param.animateTo,
233
+ animParam?.param.durationMs,
234
+ animParam?.param.loop,
235
+ ])
236
+
237
+ const xMin = view.xMin
238
+ const xMax = view.xMax
239
+ const lo = view.yMin
240
+ const hi = view.yMax
241
+ const plotW = WIDTH - PAD_L - PAD_R
242
+ const plotH = HEIGHT - PAD_T - PAD_B
243
+ const fromX = (px: number): number => xMin + ((px - PAD_L) / plotW) * (xMax - xMin)
244
+ const toX = (x: number): number => PAD_L + ((x - xMin) / (xMax - xMin)) * plotW
245
+ const toY = (y: number): number => PAD_T + (1 - (y - lo) / (hi - lo)) * plotH
246
+
247
+ // Sample with current parameter values, then convert to screen coordinates
248
+ // in one pass against the LOCKED y range — the `points` string changes with
249
+ // the parameters, so the curve's shape visibly responds to the slider.
250
+ const sampled = series.map((s, si) => {
251
+ const p: Record<string, number> = {}
252
+ for (const param of s.params ?? []) {
253
+ if (param === null || param === undefined) continue
254
+ p[param.name] = params[`${si}:${param.name}`] ?? param.value
255
+ }
256
+ const pts = sampleExpr(s.expr, xMin, xMax, SAMPLES, p)
257
+ return {
258
+ series: s,
259
+ points: pts.map(([x, y]) => `${toX(x).toFixed(2)},${toY(y).toFixed(2)}`).join(' '),
260
+ }
261
+ })
262
+
263
+ const xTicks = niceTicks(xMin, xMax)
264
+ const yTicks = niceTicks(lo, hi)
265
+ const hasData = sampled.some(p => p.points.length > 1)
266
+ const hasValidRange = Number.isFinite(xMin) && Number.isFinite(xMax) && xMax > xMin && Number.isFinite(hi) && Number.isFinite(lo) && hi > lo
267
+
268
+ // v2.9 draw shapes: line (polyline), area (fill to the locked baseline),
269
+ // scatter (dots only). Colors stay on the shared categorical palette.
270
+ const renderSeries = (s: PlotSeries, i: number, points: string): ReactNode => {
271
+ const color = seriesColor(i, sampled.length, s.color) ?? 'var(--dsw-alias-state-business-primary, #4f8ef7)'
272
+ const kind = s.kind ?? 'line'
273
+ if (kind === 'scatter') {
274
+ const coords = points === '' ? [] : points.split(' ').map(pt => {
275
+ const [x, y] = pt.split(',')
276
+ return [Number(x), Number(y)] as const
277
+ })
278
+ return (
279
+ <g key={i}>
280
+ {coords.map(([cx, cy], k) => <circle key={k} cx={cx} cy={cy} r={2.6} className={css.scatterDot} style={{ fill: color }} />)}
281
+ </g>
282
+ )
283
+ }
284
+ if (kind === 'area') {
285
+ if (points === '') return <Polyline key={i} points="" className={css.line} color={color} />
286
+ const firstX = points.slice(0, points.indexOf(' ')).split(',')[0]!
287
+ const lastX = points.slice(points.lastIndexOf(' ') + 1).split(',')[0]!
288
+ const baseY = toY(lo)
289
+ const poly = `${firstX},${baseY.toFixed(2)} ${points} ${lastX},${baseY.toFixed(2)}`
290
+ return <polygon key={i} points={poly} className={css.area} style={{ fill: color }} />
291
+ }
292
+ return <Polyline key={i} points={points} className={css.line} color={color} />
293
+ }
294
+
295
+ // Drag-to-pan and wheel-to-zoom on the SVG surface. Pan/zoom move the x
296
+ // window only; the y range stays locked so parameter changes stay visible.
297
+ const onPointerDown = (e: React.PointerEvent): void => {
298
+ dragRef.current = { startX: e.clientX, startY: e.clientY, xMin, xMax }
299
+ ;(e.target as Element).setPointerCapture?.(e.pointerId)
300
+ }
301
+ const onPointerMove = (e: React.PointerEvent): void => {
302
+ const d = dragRef.current
303
+ if (d === null) return
304
+ // A few pixels of slack: a plain click or a text-selection drag must not
305
+ // shift the domain.
306
+ if (Math.abs(e.clientX - d.startX) < 4) return
307
+ const span = d.xMax - d.xMin
308
+ const dx = ((d.startX - e.clientX) / plotW) * span
309
+ setView(prev => ({ xMin: d.xMin + dx, xMax: d.xMax + dx, yMin: prev.yMin, yMax: prev.yMax }))
310
+ }
311
+ const onPointerUp = (): void => { dragRef.current = null }
312
+
313
+ // Wheel-to-zoom must not scroll the enclosing dialog (#63): React attaches
314
+ // onWheel as a passive listener, where preventDefault() is ignored and the
315
+ // event bubbles up to the scroll container. Register natively with
316
+ // { passive: false } instead — the same pattern as the 3D scene viewer.
317
+ // Wheel zoom requires a modifier (Cmd/Ctrl). Without one the wheel is left
318
+ // alone so the CONVERSATION scrolls: an unmodified wheel over the chart used
319
+ // to be swallowed by the plot, so scrolling the page silently rescaled the
320
+ // chart instead (a reader could end up looking at a domain nobody asked for,
321
+ // with no way back). The #63 requirement still holds for the modified case —
322
+ // a native non-passive listener keeps the event from reaching the host.
323
+ const svgRef = useRef<SVGSVGElement | null>(null)
324
+ useEffect(() => {
325
+ const el = svgRef.current
326
+ if (el === null) return
327
+ const onWheel = (e: WheelEvent): void => {
328
+ if (!e.metaKey && !e.ctrlKey) return
329
+ e.preventDefault()
330
+ const span = xMax - xMin
331
+ const factor = e.deltaY > 0 ? 1.15 : 1 / 1.15
332
+ const next = span * factor
333
+ const rect = el.getBoundingClientRect()
334
+ const cx = fromX(((e.clientX - rect.left) / rect.width) * WIDTH)
335
+ const left = cx - ((cx - xMin) / span) * next
336
+ setView(prev => ({ xMin: left, xMax: left + next, yMin: prev.yMin, yMax: prev.yMax }))
337
+ }
338
+ el.addEventListener('wheel', onWheel, { passive: false })
339
+ return () => el.removeEventListener('wheel', onWheel)
340
+ }, [xMin, xMax, fromX])
341
+
342
+ const hasParams = series.some(s => (s.params?.length ?? 0) > 0)
343
+
344
+ // Zoom/pan move the x window; without a way back a reader who zoomed by
345
+ // accident was stuck with a domain nobody asked for. The header therefore
346
+ // shows the CURRENT domain whenever it differs from the initial one and
347
+ // offers a one-click return.
348
+ const initialX = useRef({ min: propXMin, max: propXMax })
349
+ const viewMoved = Math.abs(xMin - initialX.current.min) > 1e-6 || Math.abs(xMax - initialX.current.max) > 1e-6
350
+ const resetView = (): void => {
351
+ setView(prev => ({ ...prev, xMin: initialX.current.min, xMax: initialX.current.max }))
352
+ }
353
+
354
+ return (
355
+ <div className={css.block} data-genui-plot>
356
+ {title !== undefined && <div className={css.title}>{renderInline(title)}</div>}
357
+ {hasData && hasValidRange && (
358
+ <div className={css.plotTools}>
359
+ <span className={css.plotHint}>
360
+ {viewMoved
361
+ ? `x ∈ [${formatTick(xMin)}, ${formatTick(xMax)}]`
362
+ : '⌘/Ctrl + 滚轮缩放 · 拖拽平移'}
363
+ </span>
364
+ {viewMoved && (
365
+ <button type="button" className={css.plotReset} onClick={resetView}>↺ 回到初始区间</button>
366
+ )}
367
+ </div>
368
+ )}
369
+ {hasData && hasValidRange ? (
370
+ <svg
371
+ width="100%"
372
+ viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
373
+ role="img"
374
+ aria-label={title ?? 'function plot'}
375
+ className={css.surface}
376
+ ref={svgRef}
377
+ onPointerDown={onPointerDown}
378
+ onPointerMove={onPointerMove}
379
+ onPointerUp={onPointerUp}
380
+ onPointerLeave={onPointerUp}
381
+ >
382
+ {/* y grid */}
383
+ {yTicks.map(t => (
384
+ <g key={`y${t}`}>
385
+ <line x1={PAD_L} x2={WIDTH - PAD_R} y1={toY(t)} y2={toY(t)} className={css.gridLine} />
386
+ <text x={PAD_L - 6} y={toY(t) + 4} className={css.tick} textAnchor="end">{formatTick(t)}</text>
387
+ </g>
388
+ ))}
389
+ {/* x grid */}
390
+ {xTicks.map(t => (
391
+ <g key={`x${t}`}>
392
+ <line x1={toX(t)} x2={toX(t)} y1={PAD_T} y2={HEIGHT - PAD_B} className={css.gridLine} />
393
+ <text x={toX(t)} y={HEIGHT - PAD_B + 14} className={css.tick} textAnchor="middle">{formatTick(t)}</text>
394
+ </g>
395
+ ))}
396
+ {/* axes */}
397
+ <line x1={PAD_L} x2={WIDTH - PAD_R} y1={HEIGHT - PAD_B} y2={HEIGHT - PAD_B} className={css.axis} />
398
+ <line x1={PAD_L} x2={PAD_L} y1={PAD_T} y2={HEIGHT - PAD_B} className={css.axis} />
399
+ {/* series */}
400
+ {sampled.map(({ series: s, points }, i) => renderSeries(s, i, points))}
401
+ </svg>
402
+ ) : (
403
+ <div className={css.empty}>
404
+ {series.map((s, i) => <div key={i} className={css.emptyRow}>{s.expr} — 无法绘制(表达式无效或范围非法)</div>)}
405
+ </div>
406
+ )}
407
+ {hasParams && (
408
+ <div className={css.sliders}>
409
+ <div className={css.slidersHead}>
410
+ <span className={css.slidersTitle}>参数调节</span>
411
+ <button
412
+ type="button"
413
+ className={css.resetBtn}
414
+ onClick={() => {
415
+ setPlaying(false)
416
+ const reset: Record<string, number> = {}
417
+ for (const [si, s] of series.entries()) {
418
+ for (const p of s.params ?? []) {
419
+ if (p !== null && p !== undefined) reset[`${si}:${p.name}`] = p.value
420
+ }
421
+ }
422
+ setParams(reset)
423
+ setAnimProgress(0)
424
+ }}
425
+ >
426
+ ↺ 重置
427
+ </button>
428
+ </div>
429
+ {series.map((s, si) => (s.params ?? []).map(p => {
430
+ if (p === null || p === undefined) return null
431
+ const key = `${si}:${p.name}`
432
+ const value = params[key] ?? p.value
433
+ return (
434
+ <label key={key} className={css.sliderRow}>
435
+ <span className={css.sliderName}>{s.label ?? s.expr} · {p.name}</span>
436
+ <input
437
+ type="range"
438
+ className={css.slider}
439
+ min={p.min ?? 0}
440
+ max={p.max ?? 10}
441
+ step={p.step ?? 0.1}
442
+ value={value}
443
+ onChange={e => {
444
+ // Read the value synchronously: React 18 may null out
445
+ // currentTarget after the handler in concurrent renders.
446
+ const next = Number(e.currentTarget.value)
447
+ setParams(prev => ({ ...prev, [key]: next }))
448
+ }}
449
+ />
450
+ <span className={css.sliderValue}>{Math.round(value * 100) / 100}</span>
451
+ </label>
452
+ )
453
+ }))}
454
+ </div>
455
+ )}
456
+ {animParam !== null && (
457
+ <div className={css.animBar}>
458
+ <button
459
+ type="button"
460
+ className={css.playBtn}
461
+ onClick={() => {
462
+ if (playing) { setPlaying(false) }
463
+ else {
464
+ // Reset to the declared start value before playing.
465
+ setParams(prev => ({ ...prev, [`${animParam.si}:${animParam.param.name}`]: animParam.param.value }))
466
+ setAnimProgress(0)
467
+ setPlaying(true)
468
+ }
469
+ }}
470
+ >
471
+ {playing ? '⏸ 暂停' : '▶ 播放动画'}
472
+ </button>
473
+ {playing && (
474
+ <div className={css.animTrack}>
475
+ <div className={css.animFill} style={{ width: `${animProgress * 100}%` }} />
476
+ </div>
477
+ )}
478
+ </div>
479
+ )}
480
+ {(series.length > 1 || hasParams) && (
481
+ <div className={css.legend}>
482
+ {series.map((s, i) => (
483
+ <span key={i} className={css.legendItem}>
484
+ <span className={css.legendSwatch} style={{ background: seriesColor(i, series.length, s.color) ?? 'var(--dsw-alias-state-business-primary, #4f8ef7)' }} />
485
+ {s.label ?? s.expr}
486
+ </span>
487
+ ))}
488
+ </div>
489
+ )}
490
+ </div>
491
+ )
492
+ })
@@ -0,0 +1,152 @@
1
+ /* TemplateDrawer: the GenUI template center inside the session panel body.
2
+ Tokens mirror GenuiBlock.module.css (--dsw-alias-* sheets, fallbacks last). */
3
+
4
+ .wrap {
5
+ display: flex;
6
+ flex-direction: column;
7
+ gap: 12px;
8
+ padding: 12px 14px;
9
+ min-height: 0;
10
+ overflow: auto;
11
+ }
12
+
13
+ /* ---- achievements tab ---- */
14
+ .achievements {
15
+ display: flex;
16
+ flex-direction: column;
17
+ gap: 8px;
18
+ min-height: 0;
19
+ overflow: auto;
20
+ }
21
+
22
+ /* ---- category chips ---- */
23
+ .cats {
24
+ display: flex;
25
+ flex-wrap: wrap;
26
+ gap: 8px;
27
+ }
28
+
29
+ .cat {
30
+ padding: 4px 12px;
31
+ border: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.12));
32
+ border-radius: 999px;
33
+ background: transparent;
34
+ color: var(--dsw-alias-label-secondary, var(--dsl-g-font-muted-fallback, rgba(255, 255, 255, 0.6)));
35
+ font-size: 12px;
36
+ line-height: 1.5;
37
+ cursor: pointer;
38
+ }
39
+
40
+ .cat:hover {
41
+ background: var(--dsw-alias-fill-hover, rgba(255, 255, 255, 0.06));
42
+ }
43
+
44
+ .catActive {
45
+ border-color: var(--dsw-alias-state-business-primary, #4f8ef7);
46
+ color: var(--dsw-alias-state-business-primary, #4f8ef7);
47
+ background: color-mix(in srgb, var(--dsw-alias-state-business-primary, #4f8ef7) 14%, transparent);
48
+ }
49
+
50
+ /* ---- template cards ---- */
51
+ .list {
52
+ display: grid;
53
+ grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
54
+ gap: 8px;
55
+ }
56
+
57
+ .card {
58
+ display: flex;
59
+ flex-direction: column;
60
+ gap: 4px;
61
+ padding: 10px 12px;
62
+ border: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.12));
63
+ border-radius: var(--dsl-g-radius-surface, 12px);
64
+ background: var(--dsw-alias-bg-layer-2, rgba(255, 255, 255, 0.04));
65
+ color: var(--dsw-alias-label-primary);
66
+ text-align: left;
67
+ cursor: pointer;
68
+ }
69
+
70
+ .card:hover {
71
+ border-color: var(--dsw-alias-border-l2, rgba(255, 255, 255, 0.2));
72
+ }
73
+
74
+ .cardActive {
75
+ border-color: var(--dsw-alias-state-business-primary, #4f8ef7);
76
+ }
77
+
78
+ .cardName {
79
+ font-size: 13px;
80
+ font-weight: 600;
81
+ line-height: 1.5;
82
+ }
83
+
84
+ .cardMeta {
85
+ font-size: 11px;
86
+ color: var(--dsw-alias-label-tertiary, rgba(255, 255, 255, 0.45));
87
+ }
88
+
89
+ .cardDesc {
90
+ font-size: 12px;
91
+ line-height: 1.5;
92
+ color: var(--dsw-alias-label-secondary, rgba(255, 255, 255, 0.6));
93
+ }
94
+
95
+ /* ---- detail preview ---- */
96
+ .detail {
97
+ display: flex;
98
+ flex-direction: column;
99
+ gap: 8px;
100
+ border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.12));
101
+ padding-top: 12px;
102
+ }
103
+
104
+ .toolbar {
105
+ display: flex;
106
+ align-items: center;
107
+ gap: 8px;
108
+ flex-wrap: wrap;
109
+ }
110
+
111
+ .toolbarTitle {
112
+ flex: 1;
113
+ min-width: 0;
114
+ font-size: 13px;
115
+ font-weight: 600;
116
+ color: var(--dsw-alias-label-primary);
117
+ }
118
+
119
+ .try,
120
+ .copy {
121
+ padding: 5px 12px;
122
+ border-radius: var(--dsl-g-radius-control, 8px);
123
+ border: 1px solid transparent;
124
+ font-size: 12px;
125
+ line-height: 1.5;
126
+ cursor: pointer;
127
+ }
128
+
129
+ .try {
130
+ border-color: var(--dsw-alias-state-business-primary, #4f8ef7);
131
+ color: var(--dsw-alias-state-business-primary, #4f8ef7);
132
+ background: color-mix(in srgb, var(--dsw-alias-state-business-primary, #4f8ef7) 14%, transparent);
133
+ }
134
+
135
+ .try:hover {
136
+ background: color-mix(in srgb, var(--dsw-alias-state-business-primary, #4f8ef7) 22%, transparent);
137
+ }
138
+
139
+ .copy {
140
+ border-color: var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.12));
141
+ background: transparent;
142
+ color: var(--dsw-alias-label-secondary, rgba(255, 255, 255, 0.6));
143
+ }
144
+
145
+ .preview {
146
+ border: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, 0.12));
147
+ border-radius: var(--dsl-g-radius-surface, 12px);
148
+ padding: 12px;
149
+ min-height: 0;
150
+ overflow: auto;
151
+ background: var(--dsw-alias-bg-layer-1, rgba(0, 0, 0, 0.08));
152
+ }