@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.
- package/CHANGELOG.md +597 -0
- package/LICENSE +21 -0
- package/README.md +292 -2
- package/README.zh-CN.md +307 -0
- package/SKILL.md +261 -0
- package/cordis.patch.yml +9 -0
- package/demo-prompts.md +118 -0
- package/lib/assets/echarts-core.js +43 -0
- package/lib/assets/echarts-full.js +72 -0
- package/lib/assets/mermaid.js +3591 -0
- package/lib/assets/standalone-runtime.js +1506 -0
- package/lib/assets/three.js +3844 -0
- package/lib/client.js +405 -0
- package/lib/index.js +5187 -0
- package/lib/invariant.js +19 -0
- package/lib/types/client/EChartNode.d.ts +13 -0
- package/lib/types/client/ErrorBoundary.d.ts +30 -0
- package/lib/types/client/GenuiBlock.d.ts +9 -0
- package/lib/types/client/PlotBlock.d.ts +44 -0
- package/lib/types/client/TemplateDrawer.d.ts +7 -0
- package/lib/types/client/achievement-store.d.ts +35 -0
- package/lib/types/client/achievement-toast.d.ts +2 -0
- package/lib/types/client/achievements.d.ts +63 -0
- package/lib/types/client/action-context.d.ts +7 -0
- package/lib/types/client/artifact/ExportableGenuiBlock.d.ts +6 -0
- package/lib/types/client/artifact/create.d.ts +5 -0
- package/lib/types/client/artifact/download.d.ts +9 -0
- package/lib/types/client/artifact/html.d.ts +5 -0
- package/lib/types/client/artifact/index.d.ts +8 -0
- package/lib/types/client/artifact/json.d.ts +3 -0
- package/lib/types/client/artifact/parse.d.ts +3 -0
- package/lib/types/client/artifact/portability.d.ts +4 -0
- package/lib/types/client/artifact/standalone-theme.d.ts +2 -0
- package/lib/types/client/artifact/types.d.ts +33 -0
- package/lib/types/client/asset-echarts-core.d.ts +1 -0
- package/lib/types/client/asset-echarts.d.ts +17 -0
- package/lib/types/client/asset-loader.d.ts +33 -0
- package/lib/types/client/asset-mermaid.d.ts +1 -0
- package/lib/types/client/asset-three.d.ts +1 -0
- package/lib/types/client/blocks/advanced.d.ts +101 -0
- package/lib/types/client/blocks/basic.d.ts +22 -0
- package/lib/types/client/blocks/charts.d.ts +53 -0
- package/lib/types/client/blocks/checkbox.d.ts +17 -0
- package/lib/types/client/blocks/diagram/geometry.d.ts +73 -0
- package/lib/types/client/blocks/diagram/index.d.ts +5 -0
- package/lib/types/client/blocks/diagram/layout.d.ts +41 -0
- package/lib/types/client/blocks/diagram/theme.d.ts +37 -0
- package/lib/types/client/blocks/forms.d.ts +77 -0
- package/lib/types/client/blocks/image.d.ts +6 -0
- package/lib/types/client/blocks/render-node.d.ts +10 -0
- package/lib/types/client/blocks/state.d.ts +63 -0
- package/lib/types/client/blocks/svg.d.ts +4 -0
- package/lib/types/client/dom-fence.d.ts +30 -0
- package/lib/types/client/echarts-engine.d.ts +4 -0
- package/lib/types/client/echarts-lazy.d.ts +18 -0
- package/lib/types/client/embed.d.ts +10 -0
- package/lib/types/client/fence-render.d.ts +106 -0
- package/lib/types/client/gallery.d.ts +7 -0
- package/lib/types/client/genui-runtime/diagnostics.d.ts +18 -0
- package/lib/types/client/genui-runtime/index.d.ts +1 -0
- package/lib/types/client/genui-runtime/limits.d.ts +54 -0
- package/lib/types/client/genui-runtime/normalize.d.ts +15 -0
- package/lib/types/client/genui-runtime/schema.d.ts +87 -0
- package/lib/types/client/genui-runtime/value-utils.d.ts +18 -0
- package/lib/types/client/guard.d.ts +144 -0
- package/lib/types/client/i18n/en.d.ts +307 -0
- package/lib/types/client/i18n/index.d.ts +44 -0
- package/lib/types/client/i18n/runtime.d.ts +64 -0
- package/lib/types/client/i18n/zh.d.ts +8 -0
- package/lib/types/client/index.d.ts +36 -0
- package/lib/types/client/inline.d.ts +5 -0
- package/lib/types/client/interaction-store.d.ts +48 -0
- package/lib/types/client/mermaid-core.d.ts +8 -0
- package/lib/types/client/mermaid-lazy.d.ts +9 -0
- package/lib/types/client/mermaid-safe.d.ts +52 -0
- package/lib/types/client/panel-command.d.ts +34 -0
- package/lib/types/client/panel-store.d.ts +94 -0
- package/lib/types/client/panel.d.ts +18 -0
- package/lib/types/client/parse-partial.d.ts +63 -0
- package/lib/types/client/primitive-adapter.d.ts +4 -0
- package/lib/types/client/primitive-labels.d.ts +18 -0
- package/lib/types/client/safe-math.d.ts +43 -0
- package/lib/types/client/scene3d-core.d.ts +21 -0
- package/lib/types/client/scene3d-lazy.d.ts +8 -0
- package/lib/types/client/session-resolver.d.ts +8 -0
- package/lib/types/client/source-fence.d.ts +29 -0
- package/lib/types/client/spec.d.ts +709 -0
- package/lib/types/client/standalone/asset-loader.d.ts +2 -0
- package/lib/types/client/standalone/bootstrap-types.d.ts +9 -0
- package/lib/types/client/standalone/katex-style.d.ts +1 -0
- package/lib/types/client/standalone/primitive-adapter.d.ts +54 -0
- package/lib/types/client/standalone/runtime.d.ts +2 -0
- package/lib/types/client/submission-registry.d.ts +51 -0
- package/lib/types/client/svg-fence.d.ts +2 -0
- package/lib/types/client/table-details.d.ts +20 -0
- package/lib/types/client/templates.d.ts +41 -0
- package/lib/types/client/toolview.d.ts +7 -0
- package/lib/types/client/walk-spec.d.ts +3 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/plugin/chart-contract.d.ts +9 -0
- package/lib/types/plugin/fence-feedback.d.ts +151 -0
- package/lib/types/plugin/genui-diagnostic.d.ts +13 -0
- package/lib/types/plugin/index.d.ts +35 -0
- package/lib/types/plugin/inline-content-diagnostic.d.ts +8 -0
- package/lib/types/plugin/invariant.d.ts +15 -0
- package/lib/types/plugin/tool.d.ts +27 -0
- package/lib/types/shared/fence-repair.d.ts +78 -0
- package/lib/types/shared/fence-resolve.d.ts +39 -0
- package/package.json +249 -4
- package/src/client/EChartNode.tsx +466 -0
- package/src/client/ErrorBoundary.tsx +67 -0
- package/src/client/GenuiBlock.module.css +2221 -0
- package/src/client/GenuiBlock.tsx +235 -0
- package/src/client/PlotBlock.module.css +171 -0
- package/src/client/PlotBlock.tsx +492 -0
- package/src/client/TemplateDrawer.module.css +152 -0
- package/src/client/TemplateDrawer.tsx +147 -0
- package/src/client/achievement-store.ts +151 -0
- package/src/client/achievement-toast.tsx +71 -0
- package/src/client/achievements.ts +160 -0
- package/src/client/action-context.ts +32 -0
- package/src/client/artifact/ArtifactExport.module.css +64 -0
- package/src/client/artifact/ExportableGenuiBlock.tsx +100 -0
- package/src/client/artifact/create.ts +69 -0
- package/src/client/artifact/download.ts +42 -0
- package/src/client/artifact/html.ts +157 -0
- package/src/client/artifact/index.ts +15 -0
- package/src/client/artifact/json.ts +6 -0
- package/src/client/artifact/parse.ts +32 -0
- package/src/client/artifact/portability.ts +47 -0
- package/src/client/artifact/standalone-theme.ts +94 -0
- package/src/client/artifact/types.ts +44 -0
- package/src/client/asset-echarts-core.ts +39 -0
- package/src/client/asset-echarts.ts +27 -0
- package/src/client/asset-loader.ts +96 -0
- package/src/client/asset-mermaid.ts +13 -0
- package/src/client/asset-three.ts +12 -0
- package/src/client/blocks/advanced.tsx +537 -0
- package/src/client/blocks/basic.tsx +121 -0
- package/src/client/blocks/charts.tsx +999 -0
- package/src/client/blocks/checkbox.tsx +61 -0
- package/src/client/blocks/diagram/geometry.ts +151 -0
- package/src/client/blocks/diagram/index.tsx +293 -0
- package/src/client/blocks/diagram/layout.ts +202 -0
- package/src/client/blocks/diagram/theme.ts +127 -0
- package/src/client/blocks/forms.tsx +531 -0
- package/src/client/blocks/image.tsx +28 -0
- package/src/client/blocks/render-node.tsx +494 -0
- package/src/client/blocks/state.ts +65 -0
- package/src/client/blocks/svg.tsx +66 -0
- package/src/client/dom-fence.tsx +1123 -0
- package/src/client/echarts-engine.ts +9 -0
- package/src/client/echarts-lazy.ts +41 -0
- package/src/client/embed.ts +19 -0
- package/src/client/fence-render.tsx +266 -0
- package/src/client/gallery.ts +215 -0
- package/src/client/genui-runtime/diagnostics.ts +165 -0
- package/src/client/genui-runtime/index.ts +1 -0
- package/src/client/genui-runtime/limits.ts +54 -0
- package/src/client/genui-runtime/normalize.ts +329 -0
- package/src/client/genui-runtime/schema.ts +352 -0
- package/src/client/genui-runtime/value-utils.ts +67 -0
- package/src/client/guard.ts +1965 -0
- package/src/client/i18n/en.ts +335 -0
- package/src/client/i18n/index.ts +125 -0
- package/src/client/i18n/runtime.ts +130 -0
- package/src/client/i18n/zh.ts +330 -0
- package/src/client/index.tsx +228 -0
- package/src/client/inline.ts +106 -0
- package/src/client/interaction-store.ts +127 -0
- package/src/client/mermaid-core.ts +158 -0
- package/src/client/mermaid-lazy.ts +31 -0
- package/src/client/mermaid-safe.ts +122 -0
- package/src/client/panel-command.ts +138 -0
- package/src/client/panel-store.ts +471 -0
- package/src/client/panel.tsx +287 -0
- package/src/client/parse-partial.ts +170 -0
- package/src/client/primitive-adapter.ts +13 -0
- package/src/client/primitive-labels.ts +55 -0
- package/src/client/safe-math.ts +298 -0
- package/src/client/scene3d-core.ts +195 -0
- package/src/client/scene3d-lazy.ts +26 -0
- package/src/client/session-resolver.ts +33 -0
- package/src/client/source-fence.ts +70 -0
- package/src/client/spec.ts +896 -0
- package/src/client/standalone/asset-loader.ts +9 -0
- package/src/client/standalone/bootstrap-types.ts +11 -0
- package/src/client/standalone/katex-style.ts +1 -0
- package/src/client/standalone/primitive-adapter.tsx +94 -0
- package/src/client/standalone/runtime.tsx +27 -0
- package/src/client/submission-registry.ts +150 -0
- package/src/client/svg-fence.tsx +39 -0
- package/src/client/table-details.ts +151 -0
- package/src/client/templates.ts +322 -0
- package/src/client/toolview.tsx +67 -0
- package/src/client/walk-spec.ts +43 -0
- package/src/css-modules.d.ts +5 -0
- package/src/index.ts +9 -0
- package/src/plugin/chart-contract.ts +13 -0
- package/src/plugin/fence-feedback.ts +655 -0
- package/src/plugin/genui-diagnostic.ts +112 -0
- package/src/plugin/index.ts +224 -0
- package/src/plugin/inline-content-diagnostic.ts +178 -0
- package/src/plugin/invariant.ts +30 -0
- package/src/plugin/node-shims.d.ts +31 -0
- package/src/plugin/tool.ts +424 -0
- package/src/shared/fence-repair.ts +536 -0
- package/src/shared/fence-resolve.ts +80 -0
- package/tsconfig.json +35 -0
- package/tsdown.config.ts +275 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SafeMath: a restricted math expression evaluator for GenUI plot specs.
|
|
3
|
+
*
|
|
4
|
+
* The model supplies an expression string like "sin(x) * 2 + 1". It is parsed
|
|
5
|
+
* and evaluated here WITHOUT eval / new Function: a hand-written recursive
|
|
6
|
+
* descent parser over a white-listed function/constant vocabulary. Unknown
|
|
7
|
+
* identifiers, object property access, and call syntax other than the
|
|
8
|
+
* white-listed functions are parse errors, so an adversarial expression
|
|
9
|
+
* cannot reach globals, prototypes, or side effects.
|
|
10
|
+
*
|
|
11
|
+
* Supported grammar:
|
|
12
|
+
* expr := term (('+' | '-') term)*
|
|
13
|
+
* term := unary (('*' | '/' | '%') unary)*
|
|
14
|
+
* unary := ('-' | '+') unary | power
|
|
15
|
+
* power := atom ('^' atom)?
|
|
16
|
+
* atom := number | variable | constant | func '(' expr ')' | '(' expr ')'
|
|
17
|
+
* variable: 'x' (and any single-letter variable present in vars)
|
|
18
|
+
* constant: 'pi' | 'e' | 'tau'
|
|
19
|
+
* func : sin cos tan asin acos atan sqrt cbrt exp log ln abs floor ceil round min max pow
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export interface SafeMathOptions {
|
|
23
|
+
/** Variable values; only names present here may be referenced as variables. */
|
|
24
|
+
vars?: Record<string, number>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const CONSTANTS: Record<string, number> = {
|
|
28
|
+
pi: Math.PI,
|
|
29
|
+
e: Math.E,
|
|
30
|
+
tau: Math.PI * 2,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const FUNCTIONS: Record<string, (...args: number[]) => number> = {
|
|
34
|
+
sin: Math.sin, cos: Math.cos, tan: Math.tan,
|
|
35
|
+
asin: Math.asin, acos: Math.acos, atan: Math.atan,
|
|
36
|
+
sqrt: Math.sqrt, cbrt: Math.cbrt,
|
|
37
|
+
exp: Math.exp, log: Math.log10, ln: Math.log,
|
|
38
|
+
abs: Math.abs, floor: Math.floor, ceil: Math.ceil, round: Math.round,
|
|
39
|
+
min: Math.min, max: Math.max,
|
|
40
|
+
pow: (a, b) => Math.pow(a, b),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class ParseError extends Error {
|
|
44
|
+
constructor(message: string, readonly pos: number) {
|
|
45
|
+
super(`SafeMath parse error at ${pos}: ${message}`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class SafeMathParser {
|
|
50
|
+
private i = 0
|
|
51
|
+
constructor(private readonly src: string, private readonly vars: Record<string, number>) {}
|
|
52
|
+
|
|
53
|
+
parse(): (x: number) => number {
|
|
54
|
+
const node = this.parseExpr()
|
|
55
|
+
this.skipWs()
|
|
56
|
+
if (this.i < this.src.length) throw new ParseError('unexpected trailing input', this.i)
|
|
57
|
+
return this.evalNode(node)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private evalNode(node: number | ((x: number) => number)): (x: number) => number {
|
|
61
|
+
if (typeof node === 'number') return () => node
|
|
62
|
+
return node
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private parseExpr(): number | ((x: number) => number) {
|
|
66
|
+
let left = this.parseTerm()
|
|
67
|
+
for (;;) {
|
|
68
|
+
this.skipWs()
|
|
69
|
+
const c = this.peek()
|
|
70
|
+
if (c === '+' || c === '-') {
|
|
71
|
+
this.i++
|
|
72
|
+
const right = this.parseTerm()
|
|
73
|
+
const op = c
|
|
74
|
+
const prev = left
|
|
75
|
+
left = op === '+'
|
|
76
|
+
? (x) => this.asNum(prev, x) + this.asNum(right, x)
|
|
77
|
+
: (x) => this.asNum(prev, x) - this.asNum(right, x)
|
|
78
|
+
} else return left
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private parseTerm(): number | ((x: number) => number) {
|
|
83
|
+
let left = this.parseUnary()
|
|
84
|
+
for (;;) {
|
|
85
|
+
this.skipWs()
|
|
86
|
+
const c = this.peek()
|
|
87
|
+
if (c === '*' || c === '/' || c === '%') {
|
|
88
|
+
this.i++
|
|
89
|
+
const right = this.parseUnary()
|
|
90
|
+
const op = c
|
|
91
|
+
const prev = left
|
|
92
|
+
left = op === '*'
|
|
93
|
+
? (x) => this.asNum(prev, x) * this.asNum(right, x)
|
|
94
|
+
: op === '/'
|
|
95
|
+
? (x) => this.asNum(prev, x) / this.asNum(right, x)
|
|
96
|
+
: (x) => this.asNum(prev, x) % this.asNum(right, x)
|
|
97
|
+
} else return left
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private parseUnary(): number | ((x: number) => number) {
|
|
102
|
+
this.skipWs()
|
|
103
|
+
const c = this.char()
|
|
104
|
+
if (c === '-' || c === '+') {
|
|
105
|
+
this.i++
|
|
106
|
+
const operand = this.parseUnary()
|
|
107
|
+
return c === '-' ? (x) => -this.asNum(operand, x) : (x) => this.asNum(operand, x)
|
|
108
|
+
}
|
|
109
|
+
return this.parsePower()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private parsePower(): number | ((x: number) => number) {
|
|
113
|
+
const base = this.parseAtom()
|
|
114
|
+
this.skipWs()
|
|
115
|
+
if (this.peek() === '^') {
|
|
116
|
+
this.i++
|
|
117
|
+
const exp = this.parsePower()
|
|
118
|
+
const prev = base
|
|
119
|
+
return (x) => Math.pow(this.asNum(prev, x), this.asNum(exp, x))
|
|
120
|
+
}
|
|
121
|
+
return base
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private parseAtom(): number | ((x: number) => number) {
|
|
125
|
+
this.skipWs()
|
|
126
|
+
const c = this.char()
|
|
127
|
+
if (c === '(') {
|
|
128
|
+
this.i++
|
|
129
|
+
const inner = this.parseExpr()
|
|
130
|
+
this.skipWs()
|
|
131
|
+
if (this.peek() !== ')') throw new ParseError('expected )', this.i)
|
|
132
|
+
this.i++
|
|
133
|
+
return inner
|
|
134
|
+
}
|
|
135
|
+
if (c >= '0' && c <= '9' || c === '.') return this.parseNumber()
|
|
136
|
+
if (this.isIdentStart(c)) return this.parseIdent()
|
|
137
|
+
throw new ParseError(`unexpected character '${c}'`, this.i)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private parseNumber(): number {
|
|
141
|
+
const start = this.i
|
|
142
|
+
let sawDigit = false
|
|
143
|
+
|
|
144
|
+
while (this.i < this.src.length && /[0-9]/.test(this.src[this.i]!)) {
|
|
145
|
+
sawDigit = true
|
|
146
|
+
this.i++
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (this.peek() === '.') {
|
|
150
|
+
this.i++
|
|
151
|
+
while (this.i < this.src.length && /[0-9]/.test(this.src[this.i]!)) {
|
|
152
|
+
sawDigit = true
|
|
153
|
+
this.i++
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!sawDigit) {
|
|
158
|
+
throw new ParseError(`invalid number '${this.src.slice(start, this.i)}'`, start)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (this.peek() === 'e' || this.peek() === 'E') {
|
|
162
|
+
this.i++
|
|
163
|
+
if (this.peek() === '+' || this.peek() === '-') this.i++
|
|
164
|
+
const exponentStart = this.i
|
|
165
|
+
while (this.i < this.src.length && /[0-9]/.test(this.src[this.i]!)) this.i++
|
|
166
|
+
if (this.i === exponentStart) {
|
|
167
|
+
throw new ParseError(`invalid number '${this.src.slice(start, this.i)}'`, start)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const text = this.src.slice(start, this.i)
|
|
172
|
+
const value = Number(text)
|
|
173
|
+
if (Number.isNaN(value)) throw new ParseError(`invalid number '${text}'`, start)
|
|
174
|
+
return value
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private parseIdent(): number | ((x: number) => number) {
|
|
178
|
+
const start = this.i
|
|
179
|
+
while (this.i < this.src.length && this.isIdentChar(this.src[this.i]!)) this.i++
|
|
180
|
+
const name = this.src.slice(start, this.i)
|
|
181
|
+
this.skipWs()
|
|
182
|
+
if (this.peek() === '(') {
|
|
183
|
+
// Function call.
|
|
184
|
+
const fn = FUNCTIONS[name]
|
|
185
|
+
if (fn === undefined) throw new ParseError(`unknown function '${name}'`, start)
|
|
186
|
+
this.i++
|
|
187
|
+
const args: Array<number | ((x: number) => number)> = []
|
|
188
|
+
this.skipWs()
|
|
189
|
+
if (this.peek() === ')') {
|
|
190
|
+
this.i++
|
|
191
|
+
} else {
|
|
192
|
+
for (;;) {
|
|
193
|
+
args.push(this.parseExpr())
|
|
194
|
+
this.skipWs()
|
|
195
|
+
const sep = this.peek()
|
|
196
|
+
if (sep === ',') { this.i++; continue }
|
|
197
|
+
if (sep === ')') { this.i++; break }
|
|
198
|
+
throw new ParseError('expected , or ) in call', this.i)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return (x) => fn(...args.map(a => this.asNum(a, x)))
|
|
202
|
+
}
|
|
203
|
+
// Own-property checks only: `in` would match inherited members like
|
|
204
|
+
// `constructor`/`toString` on the vars object.
|
|
205
|
+
if (Object.hasOwn(CONSTANTS, name)) return CONSTANTS[name]!
|
|
206
|
+
if (name === 'x') {
|
|
207
|
+
// The sampling variable resolves against the evaluation argument.
|
|
208
|
+
return (value: number) => value
|
|
209
|
+
}
|
|
210
|
+
if (Object.hasOwn(this.vars, name)) {
|
|
211
|
+
// A declared parameter (a/b/c…) resolves against its captured value,
|
|
212
|
+
// which the caller changes by recompiling with new vars.
|
|
213
|
+
const captured = this.vars[name]!
|
|
214
|
+
return () => captured
|
|
215
|
+
}
|
|
216
|
+
// Implicit parameters: an undeclared single-lowercase-letter identifier
|
|
217
|
+
// (a/b/k/m/n…) is a parameter that defaults to 1, so a model expression
|
|
218
|
+
// like "a*sin(b*x)" without a params declaration still renders instead
|
|
219
|
+
// of failing as "unknown identifier". This is safe: only [a-z] matches,
|
|
220
|
+
// so injection names (constructor, window, process…) still parse-error.
|
|
221
|
+
if (/^[a-z]$/.test(name)) return () => 1
|
|
222
|
+
throw new ParseError(`unknown identifier '${name}'`, start)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private asNum(node: number | ((x: number) => number), x: number): number {
|
|
226
|
+
return typeof node === 'number' ? node : node(x)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private skipWs(): void {
|
|
230
|
+
while (this.i < this.src.length && /\s/.test(this.src[this.i]!)) this.i++
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private peek(): string | undefined {
|
|
234
|
+
return this.src[this.i]
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private char(): string {
|
|
238
|
+
const c = this.peek()
|
|
239
|
+
if (c === undefined) throw new ParseError('unexpected end of input', this.i)
|
|
240
|
+
return c
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private isIdentStart(c: string | undefined): boolean {
|
|
244
|
+
return c !== undefined && /[a-zA-Z_]/.test(c)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private isIdentChar(c: string): boolean {
|
|
248
|
+
return /[a-zA-Z0-9_]/.test(c)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Compile an expression into an evaluable function.
|
|
254
|
+
* @param expr - the math expression. `x` is the sampling variable; any other
|
|
255
|
+
* name in `vars` is a parameter whose captured value the evaluator returns
|
|
256
|
+
* (recompile with new vars to change a parameter — expressions are short).
|
|
257
|
+
* @param options - parameter values; `x` is reserved and ignored here.
|
|
258
|
+
* @returns evaluator, or null when the expression is invalid.
|
|
259
|
+
*/
|
|
260
|
+
export function compileMathExpr(expr: string, options: SafeMathOptions = {}): ((x: number) => number) | null {
|
|
261
|
+
try {
|
|
262
|
+
// `x` is the sampling variable and must never be shadowed by a param.
|
|
263
|
+
const vars: Record<string, number> = { ...options.vars }
|
|
264
|
+
delete vars.x
|
|
265
|
+
return new SafeMathParser(expr, vars).parse()
|
|
266
|
+
} catch {
|
|
267
|
+
return null
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Sample an expression over a range.
|
|
273
|
+
* @param expr - math expression in x, with optional parameters.
|
|
274
|
+
* @param xMin - inclusive start.
|
|
275
|
+
* @param xMax - inclusive end.
|
|
276
|
+
* @param samples - number of samples (>= 2).
|
|
277
|
+
* @param params - parameter values (e.g. { a: 2, b: 3 }); recompile per change.
|
|
278
|
+
* @returns [x, y] pairs; non-finite samples are dropped.
|
|
279
|
+
*/
|
|
280
|
+
export function sampleExpr(
|
|
281
|
+
expr: string,
|
|
282
|
+
xMin: number,
|
|
283
|
+
xMax: number,
|
|
284
|
+
samples = 200,
|
|
285
|
+
params: Record<string, number> = {},
|
|
286
|
+
): Array<[number, number]> {
|
|
287
|
+
const fn = compileMathExpr(expr, { vars: params })
|
|
288
|
+
if (fn === null) return []
|
|
289
|
+
if (!Number.isFinite(xMin) || !Number.isFinite(xMax) || xMax <= xMin || samples < 2) return []
|
|
290
|
+
const points: Array<[number, number]> = []
|
|
291
|
+
const step = (xMax - xMin) / (samples - 1)
|
|
292
|
+
for (let i = 0; i < samples; i++) {
|
|
293
|
+
const x = xMin + step * i
|
|
294
|
+
const y = fn(x)
|
|
295
|
+
if (Number.isFinite(y)) points.push([x, y])
|
|
296
|
+
}
|
|
297
|
+
return points
|
|
298
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three.js scene renderer — the HEAVY half. This module imports three and is
|
|
3
|
+
* bundled ONLY into the lazy asset bundle `lib/assets/three.js`, loaded on
|
|
4
|
+
* demand by the scene3d-lazy loader when a spec contains a `scene3d` node;
|
|
5
|
+
* the main client bundle stays small.
|
|
6
|
+
*
|
|
7
|
+
* Security: the model only supplies white-listed primitive shapes, numeric
|
|
8
|
+
* transforms, and colors. No textures, no external URLs, no scripts, no
|
|
9
|
+
* custom shaders — everything is geometry + material colors constructed
|
|
10
|
+
* locally. The renderer mounts into a caller-owned container and returns a
|
|
11
|
+
* disposer that tears down the WebGL context.
|
|
12
|
+
* @module @changfenhuang/dsh-genui/client/scene3d-core
|
|
13
|
+
*/
|
|
14
|
+
import type { GenuiScene3D } from './spec.ts'
|
|
15
|
+
|
|
16
|
+
let threePromise: Promise<typeof import('three')> | null = null
|
|
17
|
+
|
|
18
|
+
function loadThree(): Promise<typeof import('three')> {
|
|
19
|
+
threePromise ??= import('three')
|
|
20
|
+
return threePromise
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Shape constructors we accept, mapped to a size-normalized factory. */
|
|
24
|
+
type ShapeKind = 'box' | 'sphere' | 'cone' | 'cylinder' | 'torus'
|
|
25
|
+
|
|
26
|
+
function geometryFor(
|
|
27
|
+
THREE: typeof import('three'),
|
|
28
|
+
shape: ShapeKind,
|
|
29
|
+
size: number | [number, number, number] | undefined,
|
|
30
|
+
): import('three').BufferGeometry {
|
|
31
|
+
const s = size ?? 1
|
|
32
|
+
switch (shape) {
|
|
33
|
+
case 'box': {
|
|
34
|
+
const [w, h, d] = Array.isArray(s) ? s : [s, s, s]
|
|
35
|
+
return new THREE.BoxGeometry(w, h, d)
|
|
36
|
+
}
|
|
37
|
+
case 'sphere': {
|
|
38
|
+
const r = Array.isArray(s) ? s[0] : s
|
|
39
|
+
return new THREE.SphereGeometry(r, 32, 24)
|
|
40
|
+
}
|
|
41
|
+
case 'cone': {
|
|
42
|
+
const r = Array.isArray(s) ? s[0] : s
|
|
43
|
+
return new THREE.ConeGeometry(r, Array.isArray(s) ? s[1] : r * 2, 32)
|
|
44
|
+
}
|
|
45
|
+
case 'cylinder': {
|
|
46
|
+
const r = Array.isArray(s) ? s[0] : s
|
|
47
|
+
return new THREE.CylinderGeometry(r, r, Array.isArray(s) ? s[1] : r * 2, 32)
|
|
48
|
+
}
|
|
49
|
+
case 'torus': {
|
|
50
|
+
const r = Array.isArray(s) ? s[0] : s
|
|
51
|
+
return new THREE.TorusGeometry(r, Array.isArray(s) ? s[1] : r * 0.4, 16, 48)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Mount a GenUI 3D scene into `container`.
|
|
58
|
+
* @param container - the DOM node to host the WebGL canvas.
|
|
59
|
+
* @param scene - the declarative scene spec.
|
|
60
|
+
* @returns a disposer that removes the renderer and its context.
|
|
61
|
+
*/
|
|
62
|
+
export async function mountScene(container: HTMLElement, scene: GenuiScene3D): Promise<() => void> {
|
|
63
|
+
const THREE = await loadThree()
|
|
64
|
+
const width = container.clientWidth || 420
|
|
65
|
+
const height = 240
|
|
66
|
+
|
|
67
|
+
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
|
|
68
|
+
renderer.setSize(width, height)
|
|
69
|
+
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
|
|
70
|
+
container.appendChild(renderer.domElement)
|
|
71
|
+
|
|
72
|
+
const camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 100)
|
|
73
|
+
camera.position.set(3, 2.4, 4)
|
|
74
|
+
camera.lookAt(0, 0, 0)
|
|
75
|
+
|
|
76
|
+
const threeScene = new THREE.Scene()
|
|
77
|
+
if (scene.background !== undefined) {
|
|
78
|
+
try { threeScene.background = new THREE.Color(scene.background) } catch { /* keep transparent */ }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const ambient = new THREE.AmbientLight(0xffffff, scene.ambient ?? 0.8)
|
|
82
|
+
threeScene.add(ambient)
|
|
83
|
+
const key = new THREE.DirectionalLight(0xffffff, 1.6)
|
|
84
|
+
key.position.set(4, 6, 3)
|
|
85
|
+
threeScene.add(key)
|
|
86
|
+
const rim = new THREE.DirectionalLight(0x6ea8ff, 0.6)
|
|
87
|
+
rim.position.set(-4, -2, -4)
|
|
88
|
+
threeScene.add(rim)
|
|
89
|
+
|
|
90
|
+
// Wireframe-ish ground grid for spatial reference.
|
|
91
|
+
const grid = new THREE.GridHelper(8, 16, 0x3a4152, 0x232a38)
|
|
92
|
+
threeScene.add(grid)
|
|
93
|
+
|
|
94
|
+
for (const mesh of scene.meshes) {
|
|
95
|
+
const geo = geometryFor(THREE, mesh.shape, mesh.size)
|
|
96
|
+
const color = new THREE.Color(mesh.color ?? '#6ea8ff')
|
|
97
|
+
const mat = new THREE.MeshStandardMaterial({ color, roughness: 0.35, metalness: 0.15 })
|
|
98
|
+
const obj = new THREE.Mesh(geo, mat)
|
|
99
|
+
const p = mesh.position ?? [0, 0, 0]
|
|
100
|
+
obj.position.set(p[0] ?? 0, p[1] ?? 0, p[2] ?? 0)
|
|
101
|
+
const r = mesh.rotation ?? [0, 0, 0]
|
|
102
|
+
obj.rotation.set(r[0] ?? 0, r[1] ?? 0, r[2] ?? 0)
|
|
103
|
+
const sc = mesh.scale ?? 1
|
|
104
|
+
if (typeof sc === 'number') obj.scale.setScalar(sc)
|
|
105
|
+
else obj.scale.set(sc[0] ?? 1, sc[1] ?? 1, sc[2] ?? 1)
|
|
106
|
+
threeScene.add(obj)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Orbit: simple drag-to-rotate, wheel to zoom (no external controls dep).
|
|
110
|
+
// EVENT-DRIVEN rendering: one render after init, one per orbit change —
|
|
111
|
+
// a static scene never runs a requestAnimationFrame loop, so an idle
|
|
112
|
+
// scene costs zero GPU/battery. Pointer capture keeps all drag events on
|
|
113
|
+
// the canvas (no window listeners, nothing to leak on unmount).
|
|
114
|
+
let isDragging = false
|
|
115
|
+
let prevX = 0
|
|
116
|
+
let prevY = 0
|
|
117
|
+
let theta = 0.8
|
|
118
|
+
let phi = 0.6
|
|
119
|
+
let radius = 5.5
|
|
120
|
+
const orbit = (): void => {
|
|
121
|
+
camera.position.set(
|
|
122
|
+
radius * Math.sin(phi) * Math.cos(theta),
|
|
123
|
+
radius * Math.cos(phi),
|
|
124
|
+
radius * Math.sin(phi) * Math.sin(theta),
|
|
125
|
+
)
|
|
126
|
+
camera.lookAt(0, 0, 0)
|
|
127
|
+
renderer.render(threeScene, camera)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const onPointerDown = (e: PointerEvent): void => {
|
|
131
|
+
isDragging = true
|
|
132
|
+
prevX = e.clientX
|
|
133
|
+
prevY = e.clientY
|
|
134
|
+
try { canvas.setPointerCapture(e.pointerId) } catch { /* jsdom / capture unsupported */ }
|
|
135
|
+
}
|
|
136
|
+
const onPointerMove = (e: PointerEvent): void => {
|
|
137
|
+
if (!isDragging) return
|
|
138
|
+
// Drag follows the cursor (standard orbit feel): dragging right/left
|
|
139
|
+
// sweeps the azimuth with the pointer, dragging up/down tilts the polar
|
|
140
|
+
// angle with the pointer (phi grows from +Y down toward the equator).
|
|
141
|
+
theta += (e.clientX - prevX) * 0.01
|
|
142
|
+
phi = Math.max(0.1, Math.min(Math.PI - 0.1, phi - (e.clientY - prevY) * 0.01))
|
|
143
|
+
prevX = e.clientX
|
|
144
|
+
prevY = e.clientY
|
|
145
|
+
orbit()
|
|
146
|
+
}
|
|
147
|
+
const onPointerEnd = (e: PointerEvent): void => {
|
|
148
|
+
if (!isDragging) return
|
|
149
|
+
isDragging = false
|
|
150
|
+
try { canvas.releasePointerCapture(e.pointerId) } catch { /* jsdom */ }
|
|
151
|
+
}
|
|
152
|
+
const onWheel = (e: WheelEvent): void => {
|
|
153
|
+
e.preventDefault()
|
|
154
|
+
radius = Math.max(2.5, Math.min(14, radius + e.deltaY * 0.005))
|
|
155
|
+
orbit()
|
|
156
|
+
}
|
|
157
|
+
const canvas = renderer.domElement
|
|
158
|
+
canvas.addEventListener('pointerdown', onPointerDown)
|
|
159
|
+
canvas.addEventListener('pointermove', onPointerMove)
|
|
160
|
+
canvas.addEventListener('pointerup', onPointerEnd)
|
|
161
|
+
canvas.addEventListener('pointercancel', onPointerEnd)
|
|
162
|
+
canvas.addEventListener('wheel', onWheel, { passive: false })
|
|
163
|
+
|
|
164
|
+
// Container-size sync: the panel is resizable and the window reflows, so a
|
|
165
|
+
// fixed mount-time width drifts. Event-driven like the orbit — a resize
|
|
166
|
+
// renders exactly once, an idle scene still costs nothing.
|
|
167
|
+
let lastWidth = width
|
|
168
|
+
const observer = typeof ResizeObserver !== 'undefined'
|
|
169
|
+
? new ResizeObserver(() => {
|
|
170
|
+
const w = container.clientWidth || 420
|
|
171
|
+
if (w === lastWidth) return
|
|
172
|
+
lastWidth = w
|
|
173
|
+
renderer.setSize(w, height)
|
|
174
|
+
camera.aspect = w / height
|
|
175
|
+
camera.updateProjectionMatrix()
|
|
176
|
+
renderer.render(threeScene, camera)
|
|
177
|
+
})
|
|
178
|
+
: null
|
|
179
|
+
observer?.observe(container)
|
|
180
|
+
|
|
181
|
+
orbit() // initial render; afterwards only interactions re-render.
|
|
182
|
+
|
|
183
|
+
return () => {
|
|
184
|
+
observer?.disconnect()
|
|
185
|
+
canvas.removeEventListener('pointerdown', onPointerDown)
|
|
186
|
+
canvas.removeEventListener('pointermove', onPointerMove)
|
|
187
|
+
canvas.removeEventListener('pointerup', onPointerEnd)
|
|
188
|
+
canvas.removeEventListener('pointercancel', onPointerEnd)
|
|
189
|
+
canvas.removeEventListener('wheel', onWheel)
|
|
190
|
+
renderer.dispose()
|
|
191
|
+
if (renderer.domElement.parentElement === container) {
|
|
192
|
+
container.removeChild(renderer.domElement)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime loader for the three.js scene renderer. The heavy three bundle
|
|
3
|
+
* ships as a separate asset (`lib/assets/three.js`, served by the plugin's
|
|
4
|
+
* own HTTP route) and is fetched ONLY when a spec contains a `scene3d` node.
|
|
5
|
+
* On a host that does not serve the asset the load rejects and the
|
|
6
|
+
* Scene3DNode shows its error hint.
|
|
7
|
+
* @module @changfenhuang/dsh-genui/client/scene3d-lazy
|
|
8
|
+
*/
|
|
9
|
+
import { loadGenuiAsset } from './asset-loader.ts'
|
|
10
|
+
import type { GenuiScene3D } from './spec.ts'
|
|
11
|
+
|
|
12
|
+
/** The engine surface registered by the three asset bundle. */
|
|
13
|
+
interface ThreeAssetApi {
|
|
14
|
+
mountScene: (container: HTMLElement, scene: GenuiScene3D) => Promise<() => void>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Mount a GenUI 3D scene into `container` (engine loaded on demand).
|
|
19
|
+
* @param container - the DOM node to host the WebGL canvas.
|
|
20
|
+
* @param scene - the declarative scene spec.
|
|
21
|
+
* @returns a disposer that removes the renderer and its context.
|
|
22
|
+
*/
|
|
23
|
+
export async function mountScene(container: HTMLElement, scene: GenuiScene3D): Promise<() => void> {
|
|
24
|
+
const api = await loadGenuiAsset<ThreeAssetApi>('three')
|
|
25
|
+
return api.mountScene(container, scene)
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SessionId } from '@deepseek-ai/dsh-session/types'
|
|
2
|
+
|
|
3
|
+
interface SessionListEntry {
|
|
4
|
+
id?: unknown
|
|
5
|
+
retainedBy?: unknown
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface SessionListSnapshot {
|
|
9
|
+
current?: unknown
|
|
10
|
+
byId?: unknown
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 从宿主会话列表快照中解析当前主视图会话。
|
|
15
|
+
*
|
|
16
|
+
* @param list - 宿主提供的会话列表快照
|
|
17
|
+
* @returns 当前会话标识;无法识别快照结构时返回 undefined
|
|
18
|
+
*/
|
|
19
|
+
export function resolveViewedSessionId(list: unknown): SessionId | undefined {
|
|
20
|
+
if (typeof list !== 'object' || list === null) return undefined
|
|
21
|
+
const snapshot = list as SessionListSnapshot
|
|
22
|
+
if (typeof snapshot.current === 'string') return snapshot.current as SessionId
|
|
23
|
+
if (typeof snapshot.byId !== 'object' || snapshot.byId === null || Array.isArray(snapshot.byId)) return undefined
|
|
24
|
+
|
|
25
|
+
const viewed = Object.values(snapshot.byId as Record<string, unknown>).find((entry): entry is SessionListEntry => {
|
|
26
|
+
if (typeof entry !== 'object' || entry === null) return false
|
|
27
|
+
const retainedBy = (entry as SessionListEntry).retainedBy
|
|
28
|
+
if (typeof retainedBy !== 'object' || retainedBy === null || Array.isArray(retainedBy)) return false
|
|
29
|
+
const mainView = (retainedBy as { mainView?: unknown }).mainView
|
|
30
|
+
return typeof mainView === 'number' && mainView > 0
|
|
31
|
+
})
|
|
32
|
+
return typeof viewed?.id === 'string' ? viewed.id as SessionId : undefined
|
|
33
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { fromMarkdown } from 'mdast-util-from-markdown'
|
|
2
|
+
import type { AssistantBlock, ChatNode, ChatSnapshot } from '@deepseek-ai/dsh-client-ui-chat/client'
|
|
3
|
+
|
|
4
|
+
export interface SourceFence {
|
|
5
|
+
lang: string | null
|
|
6
|
+
value: string
|
|
7
|
+
openingLineComplete: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface MarkdownNode {
|
|
11
|
+
type: string
|
|
12
|
+
lang?: string | null
|
|
13
|
+
value?: string
|
|
14
|
+
children?: MarkdownNode[]
|
|
15
|
+
position?: { start: { offset?: number } }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 按 Markdown 文档顺序读取所有代码围栏。
|
|
20
|
+
*
|
|
21
|
+
* @param markdown - assistant text block 中的 Markdown 内容
|
|
22
|
+
* @returns Markdown AST 中的代码围栏
|
|
23
|
+
*/
|
|
24
|
+
export function sourceFencesOf(markdown: string): SourceFence[] {
|
|
25
|
+
const fences: SourceFence[] = []
|
|
26
|
+
const visit = (node: MarkdownNode): void => {
|
|
27
|
+
if (node.type === 'code') {
|
|
28
|
+
const openingOffset = node.position?.start.offset
|
|
29
|
+
const openingLineEnd = openingOffset === undefined ? -1 : markdown.indexOf('\n', openingOffset)
|
|
30
|
+
fences.push({
|
|
31
|
+
lang: node.lang ?? null,
|
|
32
|
+
value: node.value as string,
|
|
33
|
+
openingLineComplete: openingLineEnd >= 0,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
for (const child of node.children ?? []) visit(child)
|
|
37
|
+
}
|
|
38
|
+
visit(fromMarkdown(markdown) as unknown as MarkdownNode)
|
|
39
|
+
return fences
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 只从 assistant 的 text blocks 中读取代码围栏。
|
|
44
|
+
*
|
|
45
|
+
* @param blocks - ChatSnapshot 提供的 assistant 内容块
|
|
46
|
+
* @returns 按内容块顺序排列的代码围栏
|
|
47
|
+
*/
|
|
48
|
+
export function sourceFencesOfAssistant(blocks: readonly AssistantBlock[]): SourceFence[] {
|
|
49
|
+
const fences: SourceFence[] = []
|
|
50
|
+
for (const block of blocks) {
|
|
51
|
+
if (block.kind === 'text') fences.push(...sourceFencesOf(block.text))
|
|
52
|
+
}
|
|
53
|
+
return fences
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 根据 assistant node key 和围栏序号读取公开 ChatSnapshot 中的 language。
|
|
58
|
+
*
|
|
59
|
+
* @param chat - 当前会话的 ChatSnapshot
|
|
60
|
+
* @param nodeKey - DOM assistant row 对应的 Chat node key
|
|
61
|
+
* @param index - assistant row 内从零开始的宿主代码块序号
|
|
62
|
+
* @returns 原始 language;null 表示围栏没有 language,undefined 表示 source 不可用
|
|
63
|
+
*/
|
|
64
|
+
export function sourceLanguageAt(chat: ChatSnapshot | undefined, nodeKey: string, index: number): string | null | undefined {
|
|
65
|
+
const node = chat?.nodes.get(nodeKey)
|
|
66
|
+
if (node?.kind !== 'assistant-step') return undefined
|
|
67
|
+
const assistantNode = node as ChatNode<'assistant-step'>
|
|
68
|
+
const fence = sourceFencesOfAssistant(assistantNode.data.blocks)[index]
|
|
69
|
+
return fence?.lang
|
|
70
|
+
}
|