dsh-vscode-mode 0.39.0 → 0.41.0

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 (51) hide show
  1. package/README.md +11 -8
  2. package/lib/client.js +83 -64
  3. package/lib/client.js.map +1 -1
  4. package/lib/index.js +4765 -1886
  5. package/lib/index.js.map +1 -1
  6. package/package.json +1 -1
  7. package/skills/dsh-vscodemode-test-check/SKILL.md +82 -0
  8. package/skills/dsh-vscodemode-ui-kit/SKILL.md +84 -0
  9. package/skills/dsh-vscodemode-ui-kit/fixtures/selftest/spec.json +65 -0
  10. package/skills/dsh-vscodemode-ui-kit/references/detect.md +90 -0
  11. package/skills/dsh-vscodemode-ui-kit/references/lib.md +48 -0
  12. package/skills/dsh-vscodemode-ui-kit/references/questions.md +68 -0
  13. package/skills/dsh-vscodemode-ui-kit/references/spec.md +99 -0
  14. package/skills/dsh-vscodemode-ui-kit/references/verify.md +37 -0
  15. package/skills/dsh-vscodemode-ui-kit/runtime/build.mjs +348 -0
  16. package/skills/dsh-vscodemode-ui-kit/runtime/css/framework.css +398 -0
  17. package/skills/dsh-vscodemode-ui-kit/runtime/js/01-core.js +182 -0
  18. package/skills/dsh-vscodemode-ui-kit/runtime/js/02-state.js +96 -0
  19. package/skills/dsh-vscodemode-ui-kit/runtime/js/03-tree.js +144 -0
  20. package/skills/dsh-vscodemode-ui-kit/runtime/js/04-canvas.js +194 -0
  21. package/skills/dsh-vscodemode-ui-kit/runtime/js/05-inspector.js +241 -0
  22. package/skills/dsh-vscodemode-ui-kit/runtime/js/06-diff.js +52 -0
  23. package/skills/dsh-vscodemode-ui-kit/runtime/js/07-reply.js +71 -0
  24. package/skills/dsh-vscodemode-ui-kit/runtime/js/08-main.js +147 -0
  25. package/skills/dsh-vscodemode-ui-kit/runtime/spec.schema.json +79 -0
  26. package/src/attachmentBytes.ts +84 -0
  27. package/src/client/index.ts +6 -0
  28. package/src/client/openReceipt.ts +5 -2
  29. package/src/client/sidebar/panels/TestCheckPanel.ts +485 -0
  30. package/src/client/sidebar/panels/useTestCheckData.ts +414 -0
  31. package/src/client/sidebar/types.ts +2 -0
  32. package/src/client/styles/editor.css +78 -0
  33. package/src/client/testCheck/TestCheckChip.ts +181 -0
  34. package/src/client/testCheck/TestCheckPanel.ts +339 -0
  35. package/src/client/testCheckPanelState.ts +173 -0
  36. package/src/client/ui/EditorView.ts +69 -6
  37. package/src/htmlPlanAttachments.ts +4 -1
  38. package/src/htmlPlanCommand.ts +4 -62
  39. package/src/index.ts +4 -0
  40. package/src/rpc.ts +3 -0
  41. package/src/shared/rpc.ts +44 -0
  42. package/src/shared/testCheck.ts +759 -0
  43. package/src/shared/testCheckHtml.ts +276 -0
  44. package/src/shared/testCheckPolicyDefault.ts +37 -0
  45. package/src/shared/testCheckSnippets.ts +197 -0
  46. package/src/testCheckCommand.ts +287 -0
  47. package/src/testCheckGuidance.ts +79 -0
  48. package/src/testCheckMode.ts +654 -0
  49. package/src/testCheckPolicy.ts +141 -0
  50. package/src/testCheckRpc.ts +196 -0
  51. package/src/testCheckStore.ts +822 -0
@@ -0,0 +1,348 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ui-kit build:spec + runtime → 单文件 HTML。
4
+ *
5
+ * node build.mjs --spec <spec.json> --out <plan.html> [--lib <库根>]… [--check] [--quiet]
6
+ * node build.mjs --selftest
7
+ *
8
+ * 规则:有错就不写文件;写之前先算 hash;素材一律内联成 data URI。
9
+ */
10
+ import { readFileSync, writeFileSync, existsSync, statSync, readdirSync } from 'node:fs'
11
+ import { dirname, resolve, join, extname } from 'node:path'
12
+ import { fileURLToPath } from 'node:url'
13
+
14
+ const HERE = dirname(fileURLToPath(import.meta.url))
15
+ const SKILL_ROOT = resolve(HERE, '..')
16
+ const CSS_DIR = join(HERE, 'css')
17
+ const JS_DIR = join(HERE, 'js')
18
+
19
+ export const RUNTIME_VERSION = '1.0.0'
20
+ const KINDS = new Set(['group', 'text', 'image', 'sprite', 'button', 'list', 'prefab'])
21
+ const MIME = {
22
+ '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
23
+ '.webp': 'image/webp', '.gif': 'image/gif',
24
+ }
25
+ const MAX_IMAGE = 6 * 1024 * 1024
26
+ const MAX_PAGE = 12 * 1024 * 1024
27
+
28
+ /* ── 规范化与 hash ───────────────────────────────────────────── */
29
+
30
+ export function canonical(v) {
31
+ if (v === null || typeof v !== 'object') return JSON.stringify(v)
32
+ if (Array.isArray(v)) return '[' + v.map(canonical).join(',') + ']'
33
+ const keys = Object.keys(v).filter((k) => v[k] !== undefined).sort()
34
+ return '{' + keys.map((k) => JSON.stringify(k) + ':' + canonical(v[k])).join(',') + '}'
35
+ }
36
+
37
+ export function fnv1a(str) {
38
+ let h = 0x811c9dc5
39
+ for (let i = 0; i < str.length; i++) {
40
+ h ^= str.charCodeAt(i)
41
+ h = Math.imul(h, 0x01000193) >>> 0
42
+ }
43
+ return h.toString(16).padStart(8, '0')
44
+ }
45
+
46
+ export function hashSpec(spec) {
47
+ const { hash, ...rest } = spec
48
+ return fnv1a(canonical(rest))
49
+ }
50
+
51
+ /* ── 校验 ───────────────────────────────────────────────────── */
52
+
53
+ export function validateSpec(spec, specDir) {
54
+ const errors = []
55
+ const warnings = []
56
+ if (!spec || typeof spec !== 'object') return { errors: ['spec 不是对象'], warnings }
57
+
58
+ if (spec.v !== 1) errors.push(`spec.v 必须是 1,当前 ${JSON.stringify(spec.v)}`)
59
+ const page = spec.page || 'design'
60
+ if (page !== 'design' && page !== 'library') errors.push('page 只能是 design 或 library')
61
+
62
+ const screen = spec.screen || {}
63
+ if (!(screen.w > 0 && screen.h > 0)) errors.push('screen.w / screen.h 必须是正数')
64
+
65
+ if (page === 'library') return { errors, warnings }
66
+
67
+ const nodes = Array.isArray(spec.nodes) ? spec.nodes : []
68
+ if (!nodes.length) warnings.push('nodes 为空,页面只有效果图')
69
+
70
+ const byId = new Map()
71
+ nodes.forEach((n, i) => {
72
+ const at = `nodes[${i}]${n && n.id ? ` ${n.id}` : ''}`
73
+ if (!n || typeof n !== 'object') { errors.push(`${at}: 不是对象`); return }
74
+ if (!n.id || typeof n.id !== 'string') { errors.push(`${at}: 缺 id`); return }
75
+ if (byId.has(n.id)) errors.push(`${at}: id 重复`)
76
+ byId.set(n.id, n)
77
+ if (!KINDS.has(n.kind)) errors.push(`${at}: kind 未知(${n.kind})`)
78
+ if (!Array.isArray(n.rect) || n.rect.length !== 4 ||
79
+ n.rect.some((v) => typeof v !== 'number' || !Number.isFinite(v))) {
80
+ errors.push(`${at}: rect 必须是 4 个数字`)
81
+ } else if (n.rect[2] <= 0 || n.rect[3] <= 0) {
82
+ errors.push(`${at}: rect 的 w / h 必须大于 0`)
83
+ }
84
+ if (n.kind === 'text' && (!n.text || typeof n.text.s !== 'string')) {
85
+ errors.push(`${at}: text 节点缺 text.s`)
86
+ }
87
+ if (n.opaque && n.kind !== 'prefab') errors.push(`${at}: opaque 只能用在 kind:"prefab"`)
88
+ if (n.ref && typeof n.ref === 'string' && !/^lib:/.test(n.ref)) {
89
+ errors.push(`${at}: ref 必须以 lib: 开头`)
90
+ }
91
+ })
92
+
93
+ nodes.forEach((n) => {
94
+ if (!n || typeof n !== 'object' || n.parent == null) return
95
+ if (!byId.has(n.parent)) errors.push(`${n.id}: parent 不存在(${n.parent})`)
96
+ })
97
+
98
+ for (const n of nodes) {
99
+ if (!n || typeof n !== 'object' || !n.id) continue
100
+ let p = n.parent
101
+ let depth = 0
102
+ while (p != null) {
103
+ if (p === n.id) { errors.push(`${n.id}: parent 链成环`); break }
104
+ const pn = byId.get(p)
105
+ if (!pn) break
106
+ p = pn.parent
107
+ if (++depth > 64) { errors.push(`${n.id}: parent 链过深`); break }
108
+ }
109
+ }
110
+
111
+ for (const n of nodes) {
112
+ if (!n || typeof n !== 'object' || !n.opaque) continue
113
+ const kids = nodes.filter((k) => k && k.parent === n.id)
114
+ if (kids.length) errors.push(`${n.id}: opaque 节点内不能有子节点(${kids.map((k) => k.id).join(', ')})`)
115
+ }
116
+
117
+ if (spec.mock && spec.mock.src && !/^data:/.test(spec.mock.src) && specDir) {
118
+ const p = resolve(specDir, spec.mock.src)
119
+ if (!existsSync(p)) errors.push(`mock.src 找不到:${spec.mock.src}`)
120
+ }
121
+
122
+ for (const n of nodes) {
123
+ if (!n || typeof n !== 'object') continue
124
+ if (typeof n.src === 'string' && n.src && !/^data:/.test(n.src) && specDir) {
125
+ if (!existsSync(resolve(specDir, n.src))) errors.push(`${n.id}: src 找不到(${n.src})`)
126
+ }
127
+ if (n.ref && !n.libOk) warnings.push(`${n.id}: 引用了库条目 ${n.ref},未提供库根,按未知处理`)
128
+ }
129
+
130
+ return { errors, warnings }
131
+ }
132
+
133
+ /* ── 素材内联 ───────────────────────────────────────────────── */
134
+
135
+ function toDataUri(absPath, label, warnings) {
136
+ if (!existsSync(absPath)) return null
137
+ const size = statSync(absPath).size
138
+ if (size > MAX_IMAGE) warnings.push(`${label} 体积 ${(size / 1048576).toFixed(1)}MB,超过 6MB 内联上限,先压图`)
139
+ const mime = MIME[extname(absPath).toLowerCase()] || 'application/octet-stream'
140
+ return `data:${mime};base64,${readFileSync(absPath).toString('base64')}`
141
+ }
142
+
143
+ export function inlineAssets(spec, specDir, warnings = []) {
144
+ const out = JSON.parse(JSON.stringify(spec))
145
+ if (out.mock && typeof out.mock.src === 'string' && out.mock.src && !/^data:/.test(out.mock.src)) {
146
+ const uri = toDataUri(resolve(specDir, out.mock.src), 'mock 图', warnings)
147
+ if (uri) out.mock.src = uri
148
+ }
149
+ for (const n of out.nodes || []) {
150
+ if (typeof n.src === 'string' && n.src && !/^data:/.test(n.src)) {
151
+ const uri = toDataUri(resolve(specDir, n.src), `${n.id} 的图`, warnings)
152
+ if (uri) n.src = uri
153
+ }
154
+ }
155
+ return out
156
+ }
157
+
158
+ /* ── 页面壳 ─────────────────────────────────────────────────── */
159
+
160
+ function esc(s) {
161
+ return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]))
162
+ }
163
+
164
+ function specJsonForPage(spec) {
165
+ return JSON.stringify(spec).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029')
166
+ }
167
+
168
+ function readRuntime() {
169
+ const read = (dir, ext, sep) => {
170
+ if (!existsSync(dir)) return ''
171
+ return readdirSync(dir)
172
+ .filter((f) => f.endsWith(ext))
173
+ .sort()
174
+ .map((f) => readFileSync(join(dir, f), 'utf8'))
175
+ .join(sep)
176
+ }
177
+ // JS 用 ";\n" 拼,避免上一个文件的结尾跟下一个文件开头的 (function(){}) 粘连
178
+ return { css: read(CSS_DIR, '.css', '\n'), js: read(JS_DIR, '.js', '\n;\n') }
179
+ }
180
+
181
+ export function renderHtml({ spec, css, js }) {
182
+ const screen = spec.screen || {}
183
+ const title = `${screen.name || 'untitled'} · ui-kit`
184
+ return `<!doctype html>
185
+ <html lang="zh-CN">
186
+ <head>
187
+ <meta charset="utf-8">
188
+ <meta name="viewport" content="width=device-width,initial-scale=1">
189
+ <title>${esc(title)}</title>
190
+ <style data-htmlplan>${css}</style>
191
+ </head>
192
+ <body>
193
+ <script id="ui-spec" type="application/json">${specJsonForPage(spec)}</script>
194
+ <div id="app"></div>
195
+ <script data-htmlplan>${js}</script>
196
+ </body>
197
+ </html>
198
+ `
199
+ }
200
+
201
+ /* ── 构建 ───────────────────────────────────────────────────── */
202
+
203
+ export function build(specPath, opts = {}) {
204
+ const abs = resolve(specPath)
205
+ if (!existsSync(abs)) throw new Error(`spec 不存在:${specPath}`)
206
+ const specDir = dirname(abs)
207
+ const raw = JSON.parse(readFileSync(abs, 'utf8'))
208
+
209
+ const warnings = []
210
+ const { errors } = validateSpec(raw, specDir)
211
+ const hash = hashSpec(raw)
212
+ const spec = inlineAssets({ ...raw, hash }, specDir, warnings)
213
+ const { css, js } = readRuntime()
214
+ if (!css) errors.push('runtime/css 为空')
215
+ if (!js) errors.push('runtime/js 为空')
216
+
217
+ if (errors.length) {
218
+ const err = new Error(errors.join('\n'))
219
+ err.errors = errors
220
+ err.warnings = warnings
221
+ throw err
222
+ }
223
+
224
+ const html = renderHtml({ spec, css, js })
225
+ const bytes = Buffer.byteLength(html, 'utf8')
226
+ if (bytes > (opts.maxBytes || MAX_PAGE)) {
227
+ warnings.push(`页面 ${(bytes / 1048576).toFixed(2)}MB,超过上限 ${(MAX_PAGE / 1048576).toFixed(0)}MB,压图或改局部图`)
228
+ }
229
+ return { html, bytes, hash, warnings, spec }
230
+ }
231
+
232
+ /* ── 自检 ───────────────────────────────────────────────────── */
233
+
234
+ export function selftest(opts = {}) {
235
+ const specPath = join(SKILL_ROOT, 'fixtures', 'selftest', 'spec.json')
236
+ const outPath = join(SKILL_ROOT, 'fixtures', 'selftest', 'plan.selftest.html')
237
+ const checks = []
238
+ const add = (name, ok, detail = '') => checks.push({ name, ok, detail })
239
+
240
+ let built
241
+ try {
242
+ built = build(specPath, { maxBytes: 2 * 1024 * 1024 })
243
+ } catch (e) {
244
+ if (!opts.quiet) {
245
+ console.error('❌ 自检构建失败:')
246
+ for (const line of e.errors || [e.message]) console.error(' ✖ ' + line)
247
+ }
248
+ return { ok: false, checks: [{ name: '构建', ok: false, detail: e.message }] }
249
+ }
250
+
251
+ const { html, hash, spec } = built
252
+ writeFileSync(outPath, html, 'utf8')
253
+
254
+ add('产物有 ui-spec 数据块', /<script id="ui-spec" type="application\/json">/.test(html))
255
+ add('没有外链样式', !/<link\b[^>]*href=/i.test(html))
256
+ add('没有外链脚本', !/<script\b[^>]*\bsrc=/i.test(html))
257
+ add('没有 doc- 标签', !/<doc-[a-z]+/i.test(html))
258
+ add('没有 http(s) 外链', !/\b(src|href)\s*=\s*["']https?:/i.test(html))
259
+ add('带计划签通行标记', /<style data-htmlplan>/.test(html) && /<script data-htmlplan>/.test(html))
260
+
261
+ let parsed = null
262
+ try {
263
+ const m = html.match(/<script id="ui-spec" type="application\/json">([\s\S]*?)<\/script>/)
264
+ parsed = JSON.parse(m[1].replace(/\\u003c/g, '<'))
265
+ add('ui-spec 能被 JSON.parse', true)
266
+ } catch (e) {
267
+ add('ui-spec 能被 JSON.parse', false, e.message)
268
+ }
269
+ if (parsed) {
270
+ add('页面里的 hash 与重新计算一致', parsed.hash === hashSpec(parsed), `page=${parsed.hash} calc=${hashSpec(parsed)}`)
271
+ add('节点数与 spec 一致', (parsed.nodes || []).length === (spec.nodes || []).length)
272
+ }
273
+
274
+ add('体积在 2MB 内', built.bytes < 2 * 1024 * 1024, `${(built.bytes / 1024).toFixed(0)}KB`)
275
+ add('渲染器与样式都在页内', html.includes('ui-tree') && html.includes('ui-canvas'))
276
+ add('hash 是 8 位十六进制', /^[0-9a-f]{8}$/.test(hash), hash)
277
+
278
+ const ok = checks.every((c) => c.ok)
279
+ if (!opts.quiet) {
280
+ for (const c of checks) console.log(`${c.ok ? '✅' : '❌'} ${c.name}${c.detail ? ' — ' + c.detail : ''}`)
281
+ console.log(ok ? `\n自检通过。产物:${outPath}` : '\n自检未通过。')
282
+ }
283
+ return { ok, checks, outPath, bytes: built.bytes }
284
+ }
285
+
286
+ /* ── CLI ────────────────────────────────────────────────────── */
287
+
288
+ function parseArgs(argv) {
289
+ const a = { libs: [] }
290
+ for (let i = 0; i < argv.length; i++) {
291
+ const k = argv[i]
292
+ const next = () => {
293
+ const v = argv[++i]
294
+ if (v == null) throw new Error(`${k} 缺参数`)
295
+ return v
296
+ }
297
+ if (k === '--spec') a.spec = next()
298
+ else if (k === '--out') a.out = next()
299
+ else if (k === '--lib') a.libs.push(next())
300
+ else if (k === '--check') a.check = true
301
+ else if (k === '--selftest') a.selftest = true
302
+ else if (k === '--quiet') a.quiet = true
303
+ else if (k === '--max-bytes') a.maxBytes = Number(next())
304
+ else if (k === '-h' || k === '--help') a.help = true
305
+ else throw new Error(`未知参数:${k}`)
306
+ }
307
+ return a
308
+ }
309
+
310
+ function usage() {
311
+ console.log(`ui-kit build ${RUNTIME_VERSION}
312
+ --spec <spec.json> --out <plan.html> [--lib <库根>]… [--check] [--quiet] [--max-bytes N]
313
+ --selftest 用 fixtures/selftest 生成一页并断言
314
+ 没有 --out 时只打印检查结果,不写文件。`)
315
+ }
316
+
317
+ function main() {
318
+ let a
319
+ try {
320
+ a = parseArgs(process.argv.slice(2))
321
+ } catch (e) {
322
+ console.error(e.message)
323
+ process.exit(2)
324
+ }
325
+ if (a.help) return usage()
326
+ if (a.selftest) return void (selftest({ quiet: a.quiet }).ok ? process.exit(0) : process.exit(1))
327
+ if (!a.spec) return void (usage(), process.exit(2))
328
+
329
+ let built
330
+ try {
331
+ built = build(a.spec, { maxBytes: a.maxBytes })
332
+ } catch (e) {
333
+ console.error('构建失败:')
334
+ for (const line of (e.errors || [e.message])) console.error(' ✖ ' + line)
335
+ for (const w of e.warnings || []) console.error(' ⚠ ' + w)
336
+ process.exit(1)
337
+ }
338
+
339
+ if (a.out && !a.check) {
340
+ writeFileSync(resolve(a.out), built.html, 'utf8')
341
+ if (!a.quiet) console.log(`写出 ${a.out} · ${(built.bytes / 1024).toFixed(0)}KB · hash ${built.hash}`)
342
+ } else if (!a.quiet) {
343
+ console.log(`校验通过 · hash ${built.hash} · ${(built.bytes / 1024).toFixed(0)}KB(未写文件)`)
344
+ }
345
+ for (const w of built.warnings) console.error('⚠ ' + w)
346
+ }
347
+
348
+ if (process.argv[1] && resolve(process.argv[1]) === resolve(fileURLToPath(import.meta.url))) main()