soma-core 1.3.2 → 1.3.4

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 (281) hide show
  1. package/README.md +43 -0
  2. package/app/SomaPageClient.tsx +22 -68
  3. package/app/[...path]/page.tsx +7 -6
  4. package/app/api/[...path]/route.ts +3 -3
  5. package/app/api/soma-registry/[...path]/route.ts +67 -0
  6. package/app/components/SomaHostPage.tsx +3 -8
  7. package/app/components/SomaPageClientBoundary.tsx +13 -0
  8. package/app/layout.tsx +6 -1
  9. package/app/page.tsx +3 -3
  10. package/app/soma/manifest/route.ts +1 -0
  11. package/app/soma/page-object/route.ts +1 -1
  12. package/bin/soma-lsp.cjs +1 -4
  13. package/core/build/compiler/full-manifest-builder.ts +19 -23
  14. package/core/build/compiler/html-include-expander.ts +56 -0
  15. package/core/build/compiler/page-assets.ts +26 -0
  16. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  17. package/core/build/compiler/rollup-plugin-soma.ts +4 -13
  18. package/core/build/compiler/route-builder.ts +0 -2
  19. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  20. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
  21. package/core/build/compiler/soma-precompile.js +3581 -2387
  22. package/core/build/compiler/soma-precompile.ts +44 -2
  23. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
  24. package/core/build/compiler/soma-webpack-loader.js +6 -15
  25. package/core/build/compiler/vite-plugin-soma.ts +9 -21
  26. package/core/build/component/pack-from-dir.ts +58 -0
  27. package/core/build/component/package-loader.ts +281 -0
  28. package/core/build/eject/apply-eject.ts +133 -0
  29. package/core/build/eject/plan-eject.ts +133 -0
  30. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  31. package/core/build/lsp/extra-completion.cjs +14 -8
  32. package/core/build/lsp/lsp-navigation.cjs +50 -43
  33. package/core/build/lsp/soma-declaration.cjs +85 -0
  34. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  35. package/core/build/v2/block-compile.ts +200 -0
  36. package/core/build/v2/block-loader.ts +33 -0
  37. package/core/build/v2/html-index.ts +120 -0
  38. package/core/build/v2/migrate-v1.ts +370 -0
  39. package/core/build/v2/soma-file-loader.ts +216 -0
  40. package/core/client/adaptive/flush-scheduler.ts +96 -0
  41. package/core/client/adaptive/hysteresis.ts +56 -0
  42. package/core/client/adaptive/index.ts +23 -0
  43. package/core/client/adaptive/observer-pool.ts +90 -0
  44. package/core/client/base/SomaPageRootBase.ts +50 -0
  45. package/core/client/compiler/adaptive-compile.ts +136 -0
  46. package/core/client/compiler/manifest-compiler.ts +155 -27
  47. package/core/client/component/ai-catalog.ts +277 -0
  48. package/core/client/component/component-definition.types.ts +65 -0
  49. package/core/client/component/component-expander.ts +119 -0
  50. package/core/client/component/component-manager.ts +933 -0
  51. package/core/client/component/component-pack.ts +445 -0
  52. package/core/client/component/component-registry.ts +71 -0
  53. package/core/client/component/contract-compat.ts +132 -0
  54. package/core/client/component/example-v2.ts +132 -0
  55. package/core/client/component/pack-styles.ts +94 -0
  56. package/core/client/component/scoped-adapter.ts +85 -0
  57. package/core/client/component/verify-pack.ts +247 -0
  58. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  59. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  60. package/core/client/container/SomaContainerAdapter.ts +186 -180
  61. package/core/client/data/data-object-resolver.ts +417 -0
  62. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  63. package/core/client/diagnostics/boot-timings.ts +152 -0
  64. package/core/client/engine/asset-resolver.ts +12 -2
  65. package/core/client/engine/dom-renderer.ts +249 -24
  66. package/core/client/engine/emit-event.ts +21 -0
  67. package/core/client/engine/event-engine.ts +34 -4
  68. package/core/client/engine/on-change-engine.ts +1 -1
  69. package/core/client/engine/property-watcher.ts +1 -1
  70. package/core/client/engine/reactive-proxy.ts +11 -10
  71. package/core/client/engine/render-engine.ts +39 -4
  72. package/core/client/engine/runtime-builder.ts +23 -0
  73. package/core/client/engine/soma-bootstrap.ts +380 -7
  74. package/core/client/engine/style-binder.ts +47 -1
  75. package/core/client/observability/error-reporter.ts +173 -0
  76. package/core/client/parser/adaptive-parser.ts +264 -0
  77. package/core/client/parser/data-config-parser.ts +396 -0
  78. package/core/client/parser/pipeline-parser.ts +15 -3
  79. package/core/client/parser/style-config-parser.ts +65 -2
  80. package/core/client/parser/ui-config-parser.ts +52 -3
  81. package/core/client/parser/v2/namespace.ts +182 -0
  82. package/core/client/parser/v2/page-parser.ts +359 -0
  83. package/core/client/parser/v2/use-parser.ts +412 -0
  84. package/core/client/parser/value-lang.ts +340 -0
  85. package/core/client/pipes/pipe-registry.ts +249 -0
  86. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  87. package/core/client/registry/declaration-renderer.ts +161 -0
  88. package/core/client/registry/index.ts +28 -0
  89. package/core/client/registry/page-lifecycle.ts +171 -0
  90. package/core/client/registry/remote-page.ts +139 -0
  91. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  92. package/core/client/security/attribute-presence.ts +70 -0
  93. package/core/client/security/html-sanitizer.ts +113 -0
  94. package/core/client/security/url-guard.ts +0 -0
  95. package/core/client/theme/harmony.ts +307 -0
  96. package/core/client/theme/scene-tokens.ts +134 -0
  97. package/core/client/theme/theme-axes.ts +228 -0
  98. package/core/client/theme/theme-engine.ts +1196 -0
  99. package/core/client/theme/tone-solver.ts +118 -0
  100. package/core/client/typography/typography-base.ts +34 -0
  101. package/core/client/validation/contract-validator.ts +48 -5
  102. package/core/index.ts +262 -2
  103. package/core/server/api/IService.ts +18 -6
  104. package/core/server/api/load-host-service.ts +3 -3
  105. package/core/server/host/content-security-policy.ts +190 -0
  106. package/core/server/host/csp-nonce-metadata.ts +75 -0
  107. package/core/server/host/resolve-host-request-hook.ts +5 -0
  108. package/core/server/host/server-page-assets.ts +34 -7
  109. package/core/server/host/soma-page-compile.ts +33 -0
  110. package/core/server/index.ts +45 -1
  111. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  112. package/core/server/mock/node/cli.mjs +2 -2
  113. package/core/server/registry/api-registry-store.ts +518 -0
  114. package/core/server/registry/conditional-object-store.ts +29 -0
  115. package/core/server/registry/declaration-precompiler.ts +99 -0
  116. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  117. package/core/server/registry/index.ts +142 -0
  118. package/core/server/registry/page-registry-repository.ts +373 -0
  119. package/core/server/registry/page-registry-store.ts +932 -0
  120. package/core/server/registry/registry-error.ts +17 -0
  121. package/core/server/registry/registry-file-lock.ts +173 -0
  122. package/core/server/registry/registry-response.ts +71 -0
  123. package/core/server/registry/registry-subjects.ts +194 -0
  124. package/core/server/registry/registry-write-pipeline.ts +240 -0
  125. package/core/server/registry/request-body-limit.ts +99 -0
  126. package/core/server/registry/services/apis/Get.ts +25 -0
  127. package/core/server/registry/services/apis/List.ts +41 -0
  128. package/core/server/registry/services/apis/Register.ts +39 -0
  129. package/core/server/registry/services/apis/Unregister.ts +24 -0
  130. package/core/server/registry/services/index.ts +40 -0
  131. package/core/server/registry/services/pages/Get.ts +96 -0
  132. package/core/server/registry/services/pages/List.ts +53 -0
  133. package/core/server/registry/services/pages/Register.ts +49 -0
  134. package/core/server/registry/services/pages/Rollback.ts +36 -0
  135. package/core/server/registry/services/pages/Unregister.ts +23 -0
  136. package/core/server/registry/services/service-utils.ts +21 -0
  137. package/core/server/registry/validation/compile-gate.ts +54 -0
  138. package/core/server/registry/validation/component-existence.ts +55 -0
  139. package/core/server/registry/validation/credential-ban.ts +86 -0
  140. package/core/server/registry/validation/cross-check.ts +67 -0
  141. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  142. package/core/shared/types/adaptive.types.ts +109 -0
  143. package/core/shared/types/component-contract.types.ts +120 -0
  144. package/core/shared/types/data-config.types.ts +105 -0
  145. package/core/shared/types/domain.types.ts +6 -0
  146. package/core/shared/types/engine.types.ts +41 -35
  147. package/core/shared/types/manifest.types.ts +55 -32
  148. package/core/shared/types/scope-manifest.types.ts +96 -0
  149. package/core/shared/types/style-config.types.ts +9 -0
  150. package/core/shared/types/ui-config.types.ts +118 -98
  151. package/dist/cli/soma.js +20 -5
  152. package/dist/core/index.cjs +7811 -1380
  153. package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
  154. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  155. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  156. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  157. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
  158. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  159. package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
  160. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
  161. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  162. package/dist/types/build/component/package-loader.d.ts +40 -0
  163. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  164. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  165. package/dist/types/build/v2/block-compile.d.ts +28 -0
  166. package/dist/types/build/v2/block-loader.d.ts +9 -0
  167. package/dist/types/build/v2/html-index.d.ts +37 -0
  168. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  169. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  170. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  171. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  172. package/dist/types/client/adaptive/index.d.ts +5 -0
  173. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  174. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  175. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  176. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  177. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  178. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  179. package/dist/types/client/component/component-expander.d.ts +25 -0
  180. package/dist/types/client/component/component-manager.d.ts +160 -0
  181. package/dist/types/client/component/component-pack.d.ts +110 -0
  182. package/dist/types/client/component/component-registry.d.ts +12 -0
  183. package/dist/types/client/component/contract-compat.d.ts +16 -0
  184. package/dist/types/client/component/example-v2.d.ts +9 -0
  185. package/dist/types/client/component/pack-styles.d.ts +22 -0
  186. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  187. package/dist/types/client/component/verify-pack.d.ts +14 -0
  188. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  189. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  190. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  191. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  192. package/dist/types/client/engine/dom-renderer.d.ts +51 -1
  193. package/dist/types/client/engine/emit-event.d.ts +6 -0
  194. package/dist/types/client/engine/event-engine.d.ts +6 -0
  195. package/dist/types/client/engine/render-engine.d.ts +25 -1
  196. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  197. package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
  198. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  199. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  200. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  201. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  202. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  203. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  204. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  205. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  206. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  207. package/dist/types/client/parser/value-lang.d.ts +63 -0
  208. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  209. package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
  210. package/dist/types/client/registry/index.d.ts +10 -0
  211. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  212. package/dist/types/client/registry/remote-page.d.ts +52 -0
  213. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  214. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  215. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  216. package/dist/types/client/security/url-guard.d.ts +11 -0
  217. package/dist/types/client/theme/harmony.d.ts +77 -0
  218. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  219. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  220. package/dist/types/client/theme/theme-engine.d.ts +77 -0
  221. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  222. package/dist/types/client/typography/typography-base.d.ts +15 -0
  223. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  224. package/dist/types/index.d.ts +47 -2
  225. package/dist/types/server/api/IService.d.ts +18 -6
  226. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  227. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  228. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  229. package/dist/types/server/host/server-page-assets.d.ts +2 -0
  230. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  231. package/dist/types/server/index.d.ts +6 -1
  232. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  233. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  234. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  235. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  236. package/dist/types/server/registry/index.d.ts +29 -0
  237. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  238. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  239. package/dist/types/server/registry/registry-error.d.ts +12 -0
  240. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  241. package/dist/types/server/registry/registry-response.d.ts +18 -0
  242. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  243. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  244. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  245. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  246. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  247. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  248. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  249. package/dist/types/server/registry/services/index.d.ts +3 -0
  250. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  251. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  252. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  253. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  254. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  255. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  256. package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
  257. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  258. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  259. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  260. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  261. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  262. package/dist/types/shared/types/component-contract.types.d.ts +78 -0
  263. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  264. package/dist/types/shared/types/domain.types.d.ts +6 -0
  265. package/dist/types/shared/types/engine.types.d.ts +6 -0
  266. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  267. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  268. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  269. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  270. package/editor/vscode/package.json +1 -24
  271. package/global.d.ts +5 -11
  272. package/next.config.js +85 -28
  273. package/package.json +33 -11
  274. package/proxy.ts +62 -0
  275. package/tailwind.config.ts +0 -1
  276. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  277. package/core/client/engine/theme-variables.ts +0 -37
  278. package/dist/tsconfig.types.tsbuildinfo +0 -1
  279. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  280. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
  281. package/middleware.ts +0 -33
@@ -0,0 +1,412 @@
1
+ // v2 解析 · 组件块(REQ-005 F3)。
2
+ //
3
+ // 一个实例 = 一个块,块体是该组件的**全部**:data(三通道 + 字段字典)、
4
+ // ui(props / Row 体系)、event(actions 定义 + on 路由)。
5
+ // 不自包含就是耦合——复杂场景一来三处拼装,复杂度没有地方搬进业务组件。
6
+ //
7
+ // 产出仍是 v1 等价物:props 进 UiConfig.show 的 ExtendedShowValue.props,
8
+ // emit 路由进事件清单。soma-core 运行时对"块"一无所知,也不需要知道。
9
+
10
+ import type { ExtendedShowValue } from '../../../shared/types/ui-config.types';
11
+ import { SomaV2ParseError } from '../../../build/v2/soma-file-loader';
12
+ import {
13
+ buildBlockScope,
14
+ expandPath,
15
+ resolveApiRef,
16
+ type BlockScope,
17
+ } from './namespace';
18
+
19
+ const BLOCK_KEYS = new Set(['component', 'alias', 'when', 'data', 'ui', 'event']);
20
+ const DATA_OBJECT_KEYS = new Set(['from', 'save', 'refresh', 'reload_on', 'fields']);
21
+ const EVENT_KEYS = new Set(['actions', 'on']);
22
+ const ACTION_KEYS = new Set([
23
+ 'label', 'handler', 'change', 'confirm', 'permission', 'tone', 'when', 'disabled_reason',
24
+ ]);
25
+ const FIELD_KEYS = new Set(['name', 'type', 'values', 'permission', 'rules']);
26
+ const FIELD_TYPES = new Set([
27
+ 'text', 'int', 'number', 'money', 'date', 'enum', 'bool', 'percent',
28
+ // progress 与 percent 同数据两种读法:前者出进度条,后者出文字(见 soma-ui 类型渲染表)
29
+ 'progress',
30
+ ]);
31
+
32
+ export interface DataObjectDecl {
33
+ localName: string;
34
+ /** 全局身份:块id.名 */
35
+ fullName: string;
36
+ from?: { api: string; params: Record<string, string> };
37
+ save?: {
38
+ api: string; method: string; key?: string; trigger: string; fields?: string[];
39
+ };
40
+ refreshMs?: number;
41
+ reloadOn?: string[];
42
+ fields?: Record<string, FieldDecl>;
43
+ }
44
+
45
+ export interface FieldDecl {
46
+ name?: string;
47
+ type: string;
48
+ values?: Record<string, string>;
49
+ permission: 'r' | 'w';
50
+ rules?: Array<Record<string, unknown>>;
51
+ }
52
+
53
+ export interface ActionDecl {
54
+ id: string;
55
+ label: string;
56
+ handler?: string;
57
+ change?: string;
58
+ confirm?: string;
59
+ permission?: string;
60
+ tone?: string;
61
+ when?: string;
62
+ disabled_reason?: string;
63
+ }
64
+
65
+ export interface UseBlockAst {
66
+ blockId: string;
67
+ /** 声明的期望组件名(与 HTML 对账后的结果) */
68
+ component?: string;
69
+ /** 整块显隐 */
70
+ when?: string;
71
+ /** 数据对象(声明与引入) */
72
+ dataObjects: DataObjectDecl[];
73
+ dataImports: Array<{ localName: string; targetPath: string }>;
74
+ /** ui 段原文(Row 编译在 soma-ui 词汇层,core 只做路径还原与结构校验) */
75
+ uiRaw: Record<string, unknown>;
76
+ /** 动作定义表 */
77
+ actions: Map<string, ActionDecl>;
78
+ /** emit 路由:emit 名 → Pipeline 表达式 */
79
+ emitRoutes: Record<string, string>;
80
+ scope: BlockScope;
81
+ /** 供 v1 编译管线消费的 props 形态 */
82
+ toShowValue(): ExtendedShowValue;
83
+ }
84
+
85
+ export interface ParseUseInput {
86
+ blockId: string;
87
+ body: Record<string, unknown>;
88
+ /** HTML 中该实例的组件名(对账用) */
89
+ htmlComponent?: string;
90
+ /** 该实例可见的行变量(由 HTML 位置决定) */
91
+ rowVars?: Iterable<string>;
92
+ }
93
+
94
+ export function parseUseBlock(input: ParseUseInput): UseBlockAst {
95
+ const { blockId, body } = input;
96
+ const fail = (message: string): never => {
97
+ throw new SomaV2ParseError(message, blockId);
98
+ };
99
+
100
+ for (const key of Object.keys(body)) {
101
+ if (!BLOCK_KEYS.has(key)) {
102
+ fail(`组件块不支持键 "${key}"(可用:${[...BLOCK_KEYS].join('/')})`);
103
+ }
104
+ }
105
+
106
+ // ── component 对账:声明与 HTML 漂移即红 ────────────────────────────────
107
+ const component = body.component === undefined ? undefined : String(body.component);
108
+ if (component && input.htmlComponent && component !== input.htmlComponent) {
109
+ fail(
110
+ `component 声明为 "${component}",但 HTML 里是 "${input.htmlComponent}"`
111
+ + '——两处必须一致(改了一处忘了另一处时,页面渲染的是哪个只能靠猜)',
112
+ );
113
+ }
114
+
115
+ // ── 作用域:data 名 + alias + 行变量 ────────────────────────────────────
116
+ const dataRaw = (body.data as Record<string, unknown>) ?? {};
117
+ if (typeof dataRaw !== 'object' || Array.isArray(dataRaw)) fail('data 段必须是映射');
118
+
119
+ const scope = buildBlockScope({
120
+ blockId,
121
+ dataNames: Object.keys(dataRaw),
122
+ aliasRaw: body.alias as Record<string, unknown> | undefined,
123
+ rowVars: input.rowVars,
124
+ });
125
+
126
+ // ── event 段:先解析(data.save.trigger 要校验动作存在) ────────────────
127
+ const { actions, emitRoutes } = parseEventSection(body.event, blockId, scope, fail);
128
+
129
+ // ── data 段:声明与引入 ─────────────────────────────────────────────────
130
+ const dataObjects: DataObjectDecl[] = [];
131
+ const dataImports: Array<{ localName: string; targetPath: string }> = [];
132
+ for (const [localName, raw] of Object.entries(dataRaw)) {
133
+ if (typeof raw === 'string') {
134
+ dataImports.push({ localName, targetPath: expandPath(raw, { ...scope, dataNames: new Set() }) });
135
+ continue;
136
+ }
137
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
138
+ fail(`data.${localName} 必须是映射(声明)或字符串路径(引入)`);
139
+ }
140
+ dataObjects.push(parseDataObject(localName, raw as Record<string, unknown>, blockId, scope, actions, fail));
141
+ }
142
+
143
+ // ── ui 段 ──────────────────────────────────────────────────────────────
144
+ const uiRaw = (body.ui as Record<string, unknown>) ?? {};
145
+ if (typeof uiRaw !== 'object' || Array.isArray(uiRaw)) fail('ui 段必须是映射');
146
+
147
+ const when = body.when === undefined ? undefined : expandCondition(String(body.when), scope);
148
+
149
+ const ast: UseBlockAst = {
150
+ blockId,
151
+ ...(component ? { component } : {}),
152
+ ...(when ? { when } : {}),
153
+ dataObjects,
154
+ dataImports,
155
+ uiRaw: expandUiPaths(uiRaw, scope),
156
+ actions,
157
+ emitRoutes,
158
+ scope,
159
+ toShowValue(): ExtendedShowValue {
160
+ const value: ExtendedShowValue = { props: this.uiRaw };
161
+ if (this.when) value.when = this.when;
162
+ return value;
163
+ },
164
+ };
165
+ return ast;
166
+ }
167
+
168
+ function parseDataObject(
169
+ localName: string,
170
+ raw: Record<string, unknown>,
171
+ blockId: string,
172
+ scope: BlockScope,
173
+ actions: Map<string, ActionDecl>,
174
+ fail: (m: string) => never,
175
+ ): DataObjectDecl {
176
+ for (const key of Object.keys(raw)) {
177
+ if (!DATA_OBJECT_KEYS.has(key)) {
178
+ fail(`data.${localName} 不支持键 "${key}"(可用:${[...DATA_OBJECT_KEYS].join('/')})`);
179
+ }
180
+ }
181
+
182
+ const decl: DataObjectDecl = { localName, fullName: `${blockId}.${localName}` };
183
+
184
+ const from = raw.from as Record<string, unknown> | undefined;
185
+ if (from !== undefined) {
186
+ if (typeof from !== 'object' || Array.isArray(from)) fail(`data.${localName}.from 必须是映射`);
187
+ const api = from.api;
188
+ if (typeof api !== 'string' || api.trim() === '') fail(`data.${localName}.from 缺少 api`);
189
+ const params: Record<string, string> = {};
190
+ for (const [key, value] of Object.entries((from.params as Record<string, unknown>) ?? {})) {
191
+ params[key] = expandPath(String(value), scope);
192
+ }
193
+ decl.from = { api: resolveApiRef(String(api), scope), params };
194
+ }
195
+
196
+ const save = raw.save as Record<string, unknown> | undefined;
197
+ if (save !== undefined) {
198
+ if (typeof save !== 'object' || Array.isArray(save)) fail(`data.${localName}.save 必须是映射`);
199
+ const api = save.api;
200
+ const trigger = save.trigger;
201
+ if (typeof api !== 'string' || api.trim() === '') fail(`data.${localName}.save 缺少 api`);
202
+ if (typeof trigger !== 'string' || trigger.trim() === '') {
203
+ fail(`data.${localName}.save 缺少 trigger(由哪个动作触发回写)`);
204
+ }
205
+ if (!actions.has(String(trigger))) {
206
+ const known = [...actions.keys()].join('/') || '(本块未定义任何动作)';
207
+ fail(
208
+ `data.${localName}.save.trigger 指向未定义的动作 "${trigger}"`
209
+ + `——请在 event.actions 里定义它(现有:${known})`,
210
+ );
211
+ }
212
+ decl.save = {
213
+ api: resolveApiRef(String(api), scope),
214
+ method: String(save.method ?? 'POST').toUpperCase(),
215
+ trigger: String(trigger),
216
+ ...(save.key !== undefined ? { key: String(save.key) } : {}),
217
+ ...(Array.isArray(save.fields) ? { fields: (save.fields as unknown[]).map(String) } : {}),
218
+ };
219
+ }
220
+
221
+ if (raw.refresh !== undefined) decl.refreshMs = parseDuration(String(raw.refresh), localName, fail);
222
+
223
+ if (raw.reload_on !== undefined) {
224
+ if (!Array.isArray(raw.reload_on)) fail(`data.${localName}.reload_on 必须是数组`);
225
+ decl.reloadOn = (raw.reload_on as unknown[]).map((item) => expandPath(String(item), scope));
226
+ }
227
+
228
+ if (raw.fields !== undefined) {
229
+ const fieldsRaw = raw.fields as Record<string, unknown>;
230
+ if (typeof fieldsRaw !== 'object' || Array.isArray(fieldsRaw)) {
231
+ fail(`data.${localName}.fields 必须是映射`);
232
+ }
233
+ const fields: Record<string, FieldDecl> = {};
234
+ for (const [fieldName, fieldRaw] of Object.entries(fieldsRaw)) {
235
+ fields[fieldName] = parseField(localName, fieldName, fieldRaw, fail);
236
+ }
237
+ decl.fields = fields;
238
+ }
239
+
240
+ return decl;
241
+ }
242
+
243
+ function parseField(
244
+ objectName: string,
245
+ fieldName: string,
246
+ raw: unknown,
247
+ fail: (m: string) => never,
248
+ ): FieldDecl {
249
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
250
+ fail(`字段字典 ${objectName}.${fieldName} 必须是映射`);
251
+ }
252
+ const field = raw as Record<string, unknown>;
253
+ for (const key of Object.keys(field)) {
254
+ if (!FIELD_KEYS.has(key)) {
255
+ fail(`字段 ${objectName}.${fieldName} 不支持键 "${key}"(可用:${[...FIELD_KEYS].join('/')})`);
256
+ }
257
+ }
258
+ const type = String(field.type ?? '');
259
+ if (!FIELD_TYPES.has(type)) {
260
+ fail(
261
+ `字段 ${objectName}.${fieldName} 的 type "${type}" 非法`
262
+ + `(封闭集:${[...FIELD_TYPES].join('/')})`,
263
+ );
264
+ }
265
+ if (type === 'enum') {
266
+ const values = field.values;
267
+ if (values === null || typeof values !== 'object' || Array.isArray(values)
268
+ || Object.keys(values as object).length === 0) {
269
+ fail(`字段 ${objectName}.${fieldName} 是 enum,必须提供非空 values 映射(它同时是翻译表与选项表)`);
270
+ }
271
+ }
272
+ const permission = field.permission === undefined ? 'r' : String(field.permission);
273
+ if (permission !== 'r' && permission !== 'w') {
274
+ fail(`字段 ${objectName}.${fieldName} 的 permission 只能是 r 或 w`);
275
+ }
276
+ if (field.rules !== undefined && !Array.isArray(field.rules)) {
277
+ fail(`字段 ${objectName}.${fieldName} 的 rules 必须是数组`);
278
+ }
279
+
280
+ return {
281
+ type,
282
+ permission,
283
+ ...(field.name !== undefined ? { name: String(field.name) } : {}),
284
+ ...(field.values ? { values: field.values as Record<string, string> } : {}),
285
+ ...(field.rules ? { rules: field.rules as Array<Record<string, unknown>> } : {}),
286
+ };
287
+ }
288
+
289
+ /** `30s` → 30000;单位封闭 ms/s/m,无单位报错(不猜) */
290
+ export function parseDuration(raw: string, where: string, fail: (m: string) => never): number {
291
+ const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/.exec(raw.trim());
292
+ if (!match) {
293
+ fail(`${where} 的时长 "${raw}" 非法——形如 500ms / 30s / 5m(单位必填,不猜)`);
294
+ }
295
+ const value = Number(match![1]);
296
+ const unit = match![2];
297
+ return unit === 'ms' ? value : unit === 's' ? value * 1000 : value * 60000;
298
+ }
299
+
300
+ function parseEventSection(
301
+ raw: unknown,
302
+ blockId: string,
303
+ scope: BlockScope,
304
+ fail: (m: string) => never,
305
+ ): { actions: Map<string, ActionDecl>; emitRoutes: Record<string, string> } {
306
+ const actions = new Map<string, ActionDecl>();
307
+ const emitRoutes: Record<string, string> = {};
308
+ if (raw === undefined) return { actions, emitRoutes };
309
+
310
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
311
+ fail('event 段必须是映射(子键 actions / on)');
312
+ }
313
+ const section = raw as Record<string, unknown>;
314
+ for (const key of Object.keys(section)) {
315
+ if (!EVENT_KEYS.has(key)) {
316
+ fail(`event 段不支持子键 "${key}"(可用:actions / on)——定义与路由分开住,不靠值形状猜`);
317
+ }
318
+ }
319
+
320
+ for (const [id, actionRaw] of Object.entries((section.actions as Record<string, unknown>) ?? {})) {
321
+ if (actionRaw === null || typeof actionRaw !== 'object' || Array.isArray(actionRaw)) {
322
+ fail(`event.actions.${id} 必须是映射`);
323
+ }
324
+ const action = actionRaw as Record<string, unknown>;
325
+ for (const key of Object.keys(action)) {
326
+ if (!ACTION_KEYS.has(key)) {
327
+ fail(`动作 ${id} 不支持字段 "${key}"(可用:${[...ACTION_KEYS].join('/')})`);
328
+ }
329
+ }
330
+ if (action.label === undefined) fail(`动作 ${id} 缺少 label`);
331
+ actions.set(id, {
332
+ id,
333
+ label: String(action.label),
334
+ ...(action.handler !== undefined ? { handler: withPageDefault(String(action.handler)) } : {}),
335
+ ...(action.change !== undefined ? { change: withPageDefault(String(action.change)) } : {}),
336
+ ...(action.confirm !== undefined ? { confirm: String(action.confirm) } : {}),
337
+ ...(action.permission !== undefined ? { permission: String(action.permission) } : {}),
338
+ ...(action.tone !== undefined ? { tone: String(action.tone) } : {}),
339
+ ...(action.when !== undefined ? { when: expandCondition(String(action.when), scope) } : {}),
340
+ ...(action.disabled_reason !== undefined
341
+ ? { disabled_reason: expandPath(String(action.disabled_reason), scope) }
342
+ : {}),
343
+ });
344
+ }
345
+
346
+ for (const [emitName, expr] of Object.entries((section.on as Record<string, unknown>) ?? {})) {
347
+ if (emitName.includes('.')) {
348
+ fail(
349
+ `event.on 的键 "${emitName}" 含点号——组件块只订阅本实例的契约 emit(直接写 emit 名),`
350
+ + 'DOM 事件写在 page 块的 event.on',
351
+ );
352
+ }
353
+ emitRoutes[emitName] = withPageDefaultInPipeline(String(expr));
354
+ }
355
+
356
+ return { actions, emitRoutes };
357
+ }
358
+
359
+ /** funcPath 不含点 → 自动补 page.(v1 onChange 既有先例) */
360
+ function withPageDefault(funcPath: string): string {
361
+ return funcPath.includes('.') ? funcPath : `page.${funcPath}`;
362
+ }
363
+
364
+ /** Pipeline 表达式里的处理器段同样享受免前缀 */
365
+ function withPageDefaultInPipeline(expr: string): string {
366
+ const parts = expr.split('|').map((part) => part.trim());
367
+ const verb = parts[0];
368
+ // UI_INTERACT 的第 2 槽是 DOM 目标,第 3 槽才是可选处理器;
369
+ // NAV_STATIC 只有路由目标,不存在处理器。不能把裸锚点误改成 page.<锚点>。
370
+ const handlerIndex = verb === 'UI_INTERACT'
371
+ ? 2
372
+ : verb === 'NAV_STATIC'
373
+ ? -1
374
+ : ['UI_REFRESH', 'NAV_ACTION'].includes(verb)
375
+ ? 1
376
+ : 0;
377
+ if (handlerIndex >= 0 && parts[handlerIndex] && /^[A-Za-z_][A-Za-z0-9_]*$/.test(parts[handlerIndex])) {
378
+ parts[handlerIndex] = `page.${parts[handlerIndex]}`;
379
+ }
380
+ return parts.reduce(
381
+ (result, part, index) => index === 0 ? part : `${result} |${part ? ` ${part}` : ''}`,
382
+ '',
383
+ );
384
+ }
385
+
386
+ function expandCondition(raw: string, scope: BlockScope): string {
387
+ const text = raw.trim();
388
+ const negated = text.startsWith('!');
389
+ const bare = negated ? text.slice(1).trim() : text;
390
+ return `${negated ? '!' : ''}${expandPath(bare, scope)}`;
391
+ }
392
+
393
+ /** 递归把 ui 段里的绑定路径还原为全名(字面量与结构原样保留) */
394
+ function expandUiPaths(value: unknown, scope: BlockScope): Record<string, unknown> {
395
+ const walk = (node: unknown): unknown => {
396
+ if (typeof node === 'string') {
397
+ const segments = node.split(/\s+\|\s+/);
398
+ segments[0] = expandPath(segments[0], scope);
399
+ return segments.join(' | ');
400
+ }
401
+ if (Array.isArray(node)) return node.map(walk);
402
+ if (node !== null && typeof node === 'object') {
403
+ const out: Record<string, unknown> = {};
404
+ for (const [key, child] of Object.entries(node as Record<string, unknown>)) {
405
+ out[key] = walk(child);
406
+ }
407
+ return out;
408
+ }
409
+ return node;
410
+ };
411
+ return walk(value) as Record<string, unknown>;
412
+ }