soma-core 1.4.1 → 1.5.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 (221) hide show
  1. package/bin/component.cjs +240 -0
  2. package/bin/eject.cjs +174 -0
  3. package/bin/migrate.cjs +362 -0
  4. package/core/build/blocks/block-compile.ts +143 -17
  5. package/core/build/blocks/migrate-flat.ts +118 -25
  6. package/core/build/compiler/page-declaration-compiler.ts +149 -5
  7. package/core/build/compiler/route-builder.ts +0 -4
  8. package/core/build/compiler/soma-precompile.js +443 -30
  9. package/core/build/component/component-graph.ts +28 -105
  10. package/core/build/component/package-loader.ts +50 -4
  11. package/core/build/eject/component-eject.ts +8 -2
  12. package/core/build/layout/constraint-solver.ts +131 -16
  13. package/core/build/layout/css-emitter.ts +15 -9
  14. package/core/build/ooui/artifact/artifact.types.ts +275 -0
  15. package/core/build/ooui/artifact/emit-artifact.ts +269 -0
  16. package/core/build/ooui/artifact/emit-html.ts +204 -0
  17. package/core/build/ooui/artifact/index.ts +53 -0
  18. package/core/build/ooui/artifact/spatial-tree.ts +268 -0
  19. package/core/build/ooui/artifact/utility-class.ts +83 -0
  20. package/core/build/ooui/composition/candidate.types.ts +323 -0
  21. package/core/build/ooui/composition/control-model.ts +114 -0
  22. package/core/build/ooui/composition/registry.ts +520 -0
  23. package/core/build/ooui/composition/select.ts +232 -0
  24. package/core/build/ooui/diagnostics/codes.ts +230 -0
  25. package/core/build/ooui/diagnostics/error.ts +235 -0
  26. package/core/build/ooui/diagnostics/format.ts +106 -0
  27. package/core/build/ooui/diagnostics/index.ts +62 -0
  28. package/core/build/ooui/diagnostics/issue.types.ts +112 -0
  29. package/core/build/ooui/engine-slot.ts +184 -0
  30. package/core/build/ooui/index.ts +1427 -0
  31. package/core/build/ooui/intent/derive-intent.ts +450 -0
  32. package/core/build/ooui/intent/field-rules.ts +330 -0
  33. package/core/build/ooui/intent/intent.types.ts +476 -0
  34. package/core/build/ooui/intent/verb-events.ts +285 -0
  35. package/core/build/ooui/page/budget.ts +503 -0
  36. package/core/build/ooui/page/compose-page.ts +736 -0
  37. package/core/build/ooui/page/index.ts +66 -0
  38. package/core/build/ooui/page/page.types.ts +402 -0
  39. package/core/build/ooui/page/region-row.ts +241 -0
  40. package/core/build/ooui/semantic/expand-page-surface.ts +180 -0
  41. package/core/build/ooui/semantic/kernel.types.ts +187 -0
  42. package/core/build/ooui/semantic/normalize.ts +784 -0
  43. package/core/build/ooui/semantic/origin.types.ts +211 -0
  44. package/core/build/ooui/semantic/role-alias.ts +154 -0
  45. package/core/build/ooui/semantic/shorthand.ts +233 -0
  46. package/core/build/ooui/semantic/surface.types.ts +78 -0
  47. package/core/build/ooui/semantic/validate-request.ts +267 -0
  48. package/core/build/ooui/spatial/emit-utility.ts +480 -0
  49. package/core/build/ooui/spatial/ir.types.ts +443 -0
  50. package/core/build/ooui/spatial/registry.ts +565 -0
  51. package/core/build/ooui/spatial/solve.ts +769 -0
  52. package/core/build/ooui/spatial/utility.types.ts +242 -0
  53. package/core/build/ooui/trace/index.ts +40 -0
  54. package/core/build/ooui/trace/render.ts +190 -0
  55. package/core/build/ooui/trace/source.types.ts +69 -0
  56. package/core/build/ooui/trace/trace.types.ts +326 -0
  57. package/core/build/pack/pack-init.ts +19 -14
  58. package/core/client/base/SomaHttpClient.ts +34 -1
  59. package/core/client/component/ai-catalog.ts +42 -0
  60. package/core/client/component/example-blocks.ts +1 -7
  61. package/core/client/component/theme-component-tokens.ts +12 -15
  62. package/core/client/component/verify-pack.ts +120 -1
  63. package/core/client/container/SomaContainerAdapter.ts +6 -2
  64. package/core/client/data/data-object-resolver.ts +27 -8
  65. package/core/client/diagnostics/telemetry-context.ts +0 -4
  66. package/core/client/engine/asset-resolver.ts +0 -1
  67. package/core/client/engine/event-engine.ts +7 -3
  68. package/core/client/engine/soma-bootstrap.ts +292 -34
  69. package/core/client/engine/style-binder.ts +0 -1
  70. package/core/client/infra/http/IHttpClient.ts +0 -6
  71. package/core/client/infra/http/SomaHttpService.ts +5 -2
  72. package/core/client/infra/notify/DomNotifyService.ts +1 -1
  73. package/core/client/infra/router/router-path.ts +0 -6
  74. package/core/client/parser/annotation-extractor.ts +1 -1
  75. package/core/client/parser/blocks/namespace.ts +30 -5
  76. package/core/client/parser/blocks/page-parser.ts +44 -1
  77. package/core/client/parser/blocks/use-parser.ts +266 -46
  78. package/core/client/parser/data-config-parser.ts +71 -3
  79. package/core/client/parser/layout-parser.ts +4 -3
  80. package/core/client/parser/reaction-parser.ts +0 -1
  81. package/core/client/parser/ui-config-parser.ts +212 -74
  82. package/core/client/theme/scene-tokens.ts +9 -3
  83. package/core/client/theme/theme-axes.ts +74 -16
  84. package/core/client/theme/theme-engine.ts +280 -69
  85. package/core/client/theme/tone-solver.ts +9 -8
  86. package/core/index.ts +38 -3
  87. package/core/server/api/load-host-service.ts +8 -2
  88. package/core/server/declaration-safety.ts +11 -0
  89. package/core/server/host/page-response-request.ts +0 -2
  90. package/core/server/host/published-page-artifact-cache.ts +244 -0
  91. package/core/server/host/published-page-assets.ts +44 -5
  92. package/core/server/mock/node/cli.mjs +0 -1
  93. package/core/server/mock/node/schema-generator.mjs +0 -1
  94. package/core/server/ooui.ts +34 -0
  95. package/core/server/page-assets.ts +8 -0
  96. package/core/server/page-declaration.ts +18 -0
  97. package/core/server/registry/asset-store.ts +327 -0
  98. package/core/server/registry/declaration-precompiler.ts +49 -6
  99. package/core/server/registry/index.ts +47 -1
  100. package/core/server/registry/page-registry-repository.ts +6 -2
  101. package/core/server/registry/page-registry-store.ts +133 -19
  102. package/core/server/registry/publish-asset-pin.ts +557 -0
  103. package/core/server/registry/services/pages/List.ts +15 -3
  104. package/core/server/registry/validation/compile-gate.ts +180 -8
  105. package/core/server/registry/validation/declaration-safety-scan.ts +113 -23
  106. package/core/shared/component/derive-sample.ts +290 -0
  107. package/core/shared/component/field-lines.ts +563 -0
  108. package/core/shared/container-breakpoints.ts +46 -0
  109. package/core/shared/object-semantics.ts +240 -0
  110. package/core/shared/ooui-declaration.ts +235 -0
  111. package/core/shared/types/component-contract.types.ts +30 -13
  112. package/core/shared/types/data-config.types.ts +95 -0
  113. package/core/shared/types/domain.types.ts +0 -24
  114. package/core/shared/types/engine.types.ts +0 -5
  115. package/core/shared/types/layout-constraint.types.ts +16 -0
  116. package/core/shared/types/ui-config.types.ts +163 -13
  117. package/dist/cli/soma.js +24 -11
  118. package/dist/core/index.cjs +2424 -1098
  119. package/dist/types/build/blocks/block-compile.d.ts +16 -1
  120. package/dist/types/build/blocks/migrate-flat.d.ts +17 -0
  121. package/dist/types/build/compiler/page-declaration-compiler.d.ts +46 -0
  122. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  123. package/dist/types/build/component/component-graph.d.ts +17 -53
  124. package/dist/types/build/component/package-loader.d.ts +8 -1
  125. package/dist/types/build/layout/constraint-solver.d.ts +46 -4
  126. package/dist/types/build/ooui/artifact/artifact.types.d.ts +180 -0
  127. package/dist/types/build/ooui/artifact/emit-artifact.d.ts +58 -0
  128. package/dist/types/build/ooui/artifact/emit-html.d.ts +53 -0
  129. package/dist/types/build/ooui/artifact/index.d.ts +5 -0
  130. package/dist/types/build/ooui/artifact/spatial-tree.d.ts +48 -0
  131. package/dist/types/build/ooui/artifact/utility-class.d.ts +46 -0
  132. package/dist/types/build/ooui/composition/candidate.types.d.ts +206 -0
  133. package/dist/types/build/ooui/composition/control-model.d.ts +37 -0
  134. package/dist/types/build/ooui/composition/registry.d.ts +66 -0
  135. package/dist/types/build/ooui/composition/select.d.ts +63 -0
  136. package/dist/types/build/ooui/diagnostics/codes.d.ts +28 -0
  137. package/dist/types/build/ooui/diagnostics/error.d.ts +135 -0
  138. package/dist/types/build/ooui/diagnostics/format.d.ts +51 -0
  139. package/dist/types/build/ooui/diagnostics/index.d.ts +25 -0
  140. package/dist/types/build/ooui/diagnostics/issue.types.d.ts +56 -0
  141. package/dist/types/build/ooui/engine-slot.d.ts +41 -0
  142. package/dist/types/build/ooui/index.d.ts +286 -0
  143. package/dist/types/build/ooui/intent/derive-intent.d.ts +32 -0
  144. package/dist/types/build/ooui/intent/field-rules.d.ts +99 -0
  145. package/dist/types/build/ooui/intent/intent.types.d.ts +318 -0
  146. package/dist/types/build/ooui/intent/verb-events.d.ts +70 -0
  147. package/dist/types/build/ooui/page/budget.d.ts +28 -0
  148. package/dist/types/build/ooui/page/compose-page.d.ts +41 -0
  149. package/dist/types/build/ooui/page/index.d.ts +4 -0
  150. package/dist/types/build/ooui/page/page.types.d.ts +264 -0
  151. package/dist/types/build/ooui/page/region-row.d.ts +16 -0
  152. package/dist/types/build/ooui/semantic/expand-page-surface.d.ts +33 -0
  153. package/dist/types/build/ooui/semantic/kernel.types.d.ts +125 -0
  154. package/dist/types/build/ooui/semantic/normalize.d.ts +45 -0
  155. package/dist/types/build/ooui/semantic/origin.types.d.ts +95 -0
  156. package/dist/types/build/ooui/semantic/role-alias.d.ts +35 -0
  157. package/dist/types/build/ooui/semantic/shorthand.d.ts +60 -0
  158. package/dist/types/build/ooui/semantic/surface.types.d.ts +16 -0
  159. package/dist/types/build/ooui/semantic/validate-request.d.ts +19 -0
  160. package/dist/types/build/ooui/spatial/emit-utility.d.ts +43 -0
  161. package/dist/types/build/ooui/spatial/ir.types.d.ts +280 -0
  162. package/dist/types/build/ooui/spatial/registry.d.ts +110 -0
  163. package/dist/types/build/ooui/spatial/solve.d.ts +42 -0
  164. package/dist/types/build/ooui/spatial/utility.types.d.ts +191 -0
  165. package/dist/types/build/ooui/trace/index.d.ts +3 -0
  166. package/dist/types/build/ooui/trace/render.d.ts +42 -0
  167. package/dist/types/build/ooui/trace/source.types.d.ts +25 -0
  168. package/dist/types/build/ooui/trace/trace.types.d.ts +143 -0
  169. package/dist/types/client/base/SomaHttpClient.d.ts +8 -0
  170. package/dist/types/client/component/ai-catalog.d.ts +9 -0
  171. package/dist/types/client/component/example-blocks.d.ts +0 -2
  172. package/dist/types/client/component/theme-component-tokens.d.ts +8 -5
  173. package/dist/types/client/component/verify-pack.d.ts +10 -0
  174. package/dist/types/client/data/data-object-resolver.d.ts +10 -2
  175. package/dist/types/client/diagnostics/telemetry-context.d.ts +0 -1
  176. package/dist/types/client/engine/soma-bootstrap.d.ts +123 -10
  177. package/dist/types/client/infra/http/IHttpClient.d.ts +0 -5
  178. package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
  179. package/dist/types/client/infra/router/router-path.d.ts +0 -2
  180. package/dist/types/client/parser/blocks/namespace.d.ts +20 -0
  181. package/dist/types/client/parser/blocks/page-parser.d.ts +9 -0
  182. package/dist/types/client/parser/blocks/use-parser.d.ts +38 -4
  183. package/dist/types/client/parser/data-config-parser.d.ts +13 -0
  184. package/dist/types/client/parser/ui-config-parser.d.ts +1 -11
  185. package/dist/types/client/theme/scene-tokens.d.ts +9 -3
  186. package/dist/types/client/theme/theme-axes.d.ts +64 -1
  187. package/dist/types/client/theme/theme-engine.d.ts +13 -8
  188. package/dist/types/client/theme/tone-solver.d.ts +9 -5
  189. package/dist/types/index.d.ts +6 -4
  190. package/dist/types/server/declaration-safety.d.ts +7 -0
  191. package/dist/types/server/host/page-response-request.d.ts +0 -1
  192. package/dist/types/server/host/published-page-artifact-cache.d.ts +64 -0
  193. package/dist/types/server/ooui.d.ts +18 -0
  194. package/dist/types/server/page-assets.d.ts +7 -0
  195. package/dist/types/server/page-declaration.d.ts +14 -0
  196. package/dist/types/server/registry/asset-store.d.ts +112 -0
  197. package/dist/types/server/registry/declaration-precompiler.d.ts +23 -3
  198. package/dist/types/server/registry/index.d.ts +3 -1
  199. package/dist/types/server/registry/page-registry-store.d.ts +57 -4
  200. package/dist/types/server/registry/publish-asset-pin.d.ts +166 -0
  201. package/dist/types/server/registry/services/pages/List.d.ts +9 -3
  202. package/dist/types/server/registry/validation/compile-gate.d.ts +63 -0
  203. package/dist/types/shared/component/derive-sample.d.ts +37 -0
  204. package/dist/types/shared/component/field-lines.d.ts +19 -0
  205. package/dist/types/shared/container-breakpoints.d.ts +42 -0
  206. package/dist/types/shared/object-semantics.d.ts +42 -0
  207. package/dist/types/shared/ooui-declaration.d.ts +135 -0
  208. package/dist/types/shared/types/component-contract.types.d.ts +29 -12
  209. package/dist/types/shared/types/data-config.types.d.ts +86 -0
  210. package/dist/types/shared/types/domain.types.d.ts +0 -21
  211. package/dist/types/shared/types/engine.types.d.ts +0 -4
  212. package/dist/types/shared/types/layout-constraint.types.d.ts +1 -0
  213. package/dist/types/shared/types/ui-config.types.d.ts +157 -12
  214. package/next.config.js +106 -13
  215. package/package.json +28 -2
  216. package/core/client/host-bridge/index.ts +0 -27
  217. package/core/client/parser/yaml-loader.ts +0 -36
  218. package/core/shared/manifest/manifest-contract.ts +0 -12
  219. package/dist/types/client/host-bridge/index.d.ts +0 -9
  220. package/dist/types/client/parser/yaml-loader.d.ts +0 -21
  221. package/dist/types/shared/manifest/manifest-contract.d.ts +0 -4
@@ -0,0 +1,520 @@
1
+ // 构件层 ③ · 候选词表的装配与对账(REQ-014 §3.3 + §16.3 + §16.5)。
2
+ //
3
+ // ── 为什么这一层的失败模式是静默的 ─────────────────────────────────
4
+ // 装配这一环有四种坏法,**没有一种会让任何东西变红**:
5
+ //
6
+ // ① **幽灵映射。** eventMap 的键写了契约里不存在的 emit。装配不报错、渲染不报错、
7
+ // 点击也不报错——组件照常 emit 它真正的名字,只是**没有人接**。
8
+ // 表现是"点了没反应",与"组件坏了""权限拦了"在浏览器里无法区分。
9
+ // 手写候选表的幽灵率实测 **4.2%**(§30.4 一次抓出三处:`DataTable` 是
10
+ // `select_change` 不是 `select`、`ProcessBranchGraph` 是 `action` 不是
11
+ // `node_action`、`filter_change` 整条漏映射)。
12
+ // **这道逐键对账是本文件存在的首要理由,它不可跳过、不可降级为告警。**
13
+ //
14
+ // ② **幽灵组件。** 候选表写了一个 catalog 里根本没有的组件名。
15
+ // 原型里的 `Text` 就是这么来的——全量演绎 1578 次字段级引用里 1472 次(93%)
16
+ // 指向它,而原型自己渲染 `<span>` 所以永远暴露不了(§36.3④)。
17
+ //
18
+ // ③ **空词表跑出"全部合法"。** 零候选的注册表跑出来的"0 个候选全部通过"
19
+ // 也是绿的,而那是最坏的一种绿。故本文件带**扫描面非空自检**:
20
+ // 词表为空、候选为空、契约表为空,三者任一即红。
21
+ //
22
+ // ④ **静默覆盖。** 两个词表都给 `record-list` 注册了 `DataTable`,后合进来的赢。
23
+ // 到底按谁的 eventMap 接线取决于数组顺序,而那个顺序写在宿主的一行 import 里。
24
+ // 裁决范式照抄 `client/component/component-pack.ts`:**默认拒收 + 点名两个来源**。
25
+ //
26
+ // ── 依赖方向 ───────────────────────────────────────────────────────
27
+ // 本模块只依赖 `shared/types/component-contract.types`(纯类型,零 import)与本层文件。
28
+ // **不 import soma-ui**——候选表由组件库注册进来,core 永远不反向依赖 ui
29
+ // (`scripts/verify-ooui-dep-graph.mjs` 守)。
30
+
31
+ import { formatOouiIssues, oouiIssue } from '../diagnostics';
32
+ import {
33
+ SOMA_TEXT_NODE,
34
+ SomaCompositionError,
35
+ type SomaCandidateRegistryLike,
36
+ type SomaCandidateVerdict,
37
+ type SomaComponentCandidate,
38
+ type SomaComponentContractView,
39
+ type SomaCompositionIssue,
40
+ type SomaCompositionVocabulary,
41
+ type SomaEventMapValue,
42
+ } from './candidate.types';
43
+ import { deriveControlModel } from './control-model';
44
+ import {
45
+ SOMA_AUTONOMOUS_INTERACTION_KINDS,
46
+ SOMA_CONTROLLED_INTERACTION_KINDS,
47
+ SOMA_DISPLAY_VARIANTS,
48
+ SOMA_INTERACTION_FORMS,
49
+ SOMA_INTERACTION_KINDS,
50
+ SOMA_SCALAR_FORMS,
51
+ isSomaCardinality,
52
+ isSomaInteractionForm,
53
+ type SomaInteractionForm,
54
+ type SomaInteractionKind,
55
+ } from '../intent/intent.types';
56
+
57
+ /** 词表标识:小写字母/数字/连字符。冲突报错与来源登记都用它 */
58
+ export const COMPOSITION_VOCABULARY_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
59
+
60
+ /** 契约名形状:PascalCase。与 `SomaComponentContract.name` 同一条规则 */
61
+ export const CANDIDATE_COMPONENT_PATTERN = /^[A-Z][A-Za-z0-9]*$/;
62
+
63
+ /** 一个形态的覆盖情况。`count === 0` 就是 §36.2③ 说的"形态空洞" */
64
+ export interface SomaFormCoverage {
65
+ readonly form: SomaInteractionForm;
66
+ readonly count: number;
67
+ /**
68
+ * 是不是页面级形态:**非标量形态**即页面级。
69
+ *
70
+ * 这条分组不是装饰:CLAUDE.md 明令"新页面只允许编排 solution/page 业务组件",
71
+ * 而这条纪律只对页面级形态成立——`text-entry` 选中的 `TextInput` 是 atom,
72
+ * 但它渲染在 `FormConsole` 内部,不是页面直接编排的东西(§34.4③ 把这两件事
73
+ * 混成了一条缺口)。分组之后闸门才判得清"这条违纪"与"这条本来就该是 atom"。
74
+ *
75
+ * 判据取自意图层的 `SOMA_SCALAR_FORMS`(取反),**不在本层重列一份形态名单**:
76
+ * 重列的那份会在形态扩表时漏掉新词,而漏掉的表现只是"某个新形态没被纪律管住"。
77
+ */
78
+ readonly pageLevel: boolean;
79
+ /** 候选组件名,按 preference 序 */
80
+ readonly components: readonly string[];
81
+ }
82
+
83
+ export interface SomaCandidateRegistry extends SomaCandidateRegistryLike {
84
+ /** 全部候选,按 (form 封闭集序, preference 升序) 稳定排列 */
85
+ readonly candidates: readonly SomaComponentCandidate[];
86
+ /** 有候选的形态,按封闭集序 */
87
+ readonly forms: readonly SomaInteractionForm[];
88
+ readonly vocabularies: readonly { readonly name: string; readonly version?: string; readonly count: number }[];
89
+ /** 某形态的候选,已按 preference 升序。**空数组是合法返回值**——由选择算法决定怎么红 */
90
+ candidatesFor(form: SomaInteractionForm): readonly SomaComponentCandidate[];
91
+ /** 十九种形态的覆盖全景(含零候选的那些),闸门与诊断报告按它出数 */
92
+ coverage(): readonly SomaFormCoverage[];
93
+ }
94
+
95
+ interface ComposeOptions {
96
+ /** 组件契约视图。**必填**:控制模型与幽灵 emit 都从它推,缺了这道对账就是空转 */
97
+ readonly contracts: readonly SomaComponentContractView[];
98
+ }
99
+
100
+ function normalizeKinds(value: SomaEventMapValue): readonly SomaInteractionKind[] {
101
+ return Array.isArray(value) ? [...value] : [value as SomaInteractionKind];
102
+ }
103
+
104
+ /**
105
+ * 构造一条诊断。**真源在横切层**(`../diagnostics/issue.types.ts` 的 `oouiIssue`)——
106
+ * 本层此前有一份六行的私有实现,语义层与意图层各有一份逐字相同的抄本。
107
+ */
108
+ const issue = oouiIssue;
109
+
110
+ /**
111
+ * 逐条对账候选词表,返回 `issues[]`(空数组即通过)。**不抛**——
112
+ * 照 `verifyTypeTable` 的先例,让调用方决定措辞与位置。
113
+ */
114
+ export function verifyComponentCandidates(
115
+ vocabularies: readonly SomaCompositionVocabulary[],
116
+ options: ComposeOptions,
117
+ ): SomaCompositionIssue[] {
118
+ const issues: SomaCompositionIssue[] = [];
119
+ const contractByName = new Map(options.contracts.map((view) => [view.name, view]));
120
+ const autonomousKinds = new Set<string>(SOMA_AUTONOMOUS_INTERACTION_KINDS);
121
+
122
+ // ── 扫描面非空自检:空表跑出来的"全部合法"是最坏的一种绿 ──────────
123
+ if (options.contracts.length === 0) {
124
+ issues.push(issue(
125
+ 'OOUI-CMP-NO-CONTRACTS',
126
+ 'options.contracts',
127
+ '一份组件契约都没传进来。',
128
+ 'eventMap 幽灵对账与控制模型判定全部以契约为基准;契约表为空时每条候选都"没有可对账的东西",'
129
+ + '于是校验一条不跑就返回通过——**空转的闸门比没有闸门更危险**,因为它会被当成已经检查过。',
130
+ "把真契约喂进来(soma-ui 侧从 catalog.json 取,core 侧用 contractView(contract) 转):\n"
131
+ + " composeComponentCandidates([pack], { contracts: entries.map(toView) });",
132
+ ));
133
+ }
134
+
135
+ const seen = new Map<string, string>();
136
+ const perFormPreference = new Map<string, string>();
137
+ let total = 0;
138
+
139
+ for (const vocabulary of vocabularies) {
140
+ if (!COMPOSITION_VOCABULARY_NAME_PATTERN.test(vocabulary.name)) {
141
+ issues.push(issue(
142
+ 'OOUI-CMP-VOCABULARY-NAME',
143
+ `${vocabulary.name}.name`,
144
+ `词表名 \`${vocabulary.name}\` 不合规。`,
145
+ '词表名是冲突消息里唯一能点名的东西;名字里带大写或空格时,两个来源在报错里长得一样,'
146
+ + '读的人无法判断该改哪一个包。',
147
+ "改成小写字母/数字/连字符,如 `name: 'soma-ui'`。",
148
+ ));
149
+ }
150
+ if (vocabulary.candidates.length === 0) {
151
+ issues.push(issue(
152
+ 'OOUI-CMP-VOCABULARY-EMPTY',
153
+ `${vocabulary.name}.candidates`,
154
+ `词表 \`${vocabulary.name}\` 一条候选都没有。`,
155
+ '空词表注册进来不会报错,只会让某些形态"看起来有人负责"而实际零候选,'
156
+ + '到选择那一步才炸,且报的是形态没有候选,指不回这份空词表。',
157
+ `删掉这次注册,或把候选补上:\n candidates: [{ form: 'display', component: 'PriceText', preference: 0 }]`,
158
+ ));
159
+ }
160
+
161
+ vocabulary.candidates.forEach((candidate, index) => {
162
+ total += 1;
163
+ const path = `${vocabulary.name}.candidates[${index}](${candidate.component}@${candidate.form})`;
164
+
165
+ if (!isSomaInteractionForm(candidate.form)) {
166
+ issues.push(issue(
167
+ 'OOUI-CMP-FORM-UNKNOWN',
168
+ `${path}.form`,
169
+ `形态 \`${String(candidate.form)}\` 不在封闭集里。`,
170
+ '形态封闭集是意图层与构件层之间唯一的接头。写一个集合外的值不会报错,'
171
+ + '只会让这条候选永远不被请求到——真正的表现在另一头:某个合法形态的候选池少了一条,'
172
+ + '选出来的是次优组件,而"次优"没有任何日志。',
173
+ `改成封闭集里的值:${SOMA_INTERACTION_FORMS.join(' / ')}\n`
174
+ + ' (`picker` 已按 §36.1 裁定 1 移出——请用 `single-choice` / `multi-choice`)',
175
+ ));
176
+ }
177
+
178
+ const isTextNode = candidate.component === SOMA_TEXT_NODE;
179
+ if (!isTextNode && !CANDIDATE_COMPONENT_PATTERN.test(candidate.component)) {
180
+ issues.push(issue(
181
+ 'OOUI-CMP-COMPONENT-NAME',
182
+ `${path}.component`,
183
+ `组件名 \`${candidate.component}\` 不是 PascalCase 契约名。`,
184
+ '候选表里的组件名必须能直接查契约;写成 kebab 或带包前缀时查不到,'
185
+ + '而"查不到"与"组件真的不存在"会报成同一条错,把人引到错误的方向。',
186
+ "写契约名,如 `component: 'DataTable'`;纯文本节点写 SOMA_TEXT_NODE('#text')。",
187
+ ));
188
+ }
189
+
190
+ const view = isTextNode ? undefined : contractByName.get(candidate.component);
191
+ if (!isTextNode && view === undefined && options.contracts.length > 0) {
192
+ issues.push(issue(
193
+ 'OOUI-CMP-GHOST-COMPONENT',
194
+ `${path}.component`,
195
+ `组件 \`${candidate.component}\` 不在组件契约目录里。`,
196
+ '幽灵组件被选中时不会抛异常:产物里那一格渲染成空,页面整体看着正常。'
197
+ + '原型里的 `Text` 就是这么活下来的——全量演绎 1578 次字段级引用有 1472 次(93%)指向它,'
198
+ + '而原型自己渲染 `<span>`,所以永远暴露不了(§36.3④)。',
199
+ "去 `soma-ui/catalog/catalog.json` 查证组件真实存在再写;\n"
200
+ + " 只读文本没有对应组件时写 SOMA_TEXT_NODE,不要现编一个组件名。",
201
+ ));
202
+ }
203
+
204
+ const eventMap = candidate.eventMap ?? {};
205
+ const emitNames = Object.keys(eventMap);
206
+ if (isTextNode && emitNames.length > 0) {
207
+ issues.push(issue(
208
+ 'OOUI-CMP-TEXT-NODE-EVENTS',
209
+ `${path}.eventMap`,
210
+ `纯文本节点声明了 ${emitNames.length} 条事件映射。`,
211
+ '文本节点不是组件,它没有 emit。这条映射永远不会触发,而"永远不触发"'
212
+ + '与"事件没接上"在运行期是同一副样子。',
213
+ '删掉 eventMap;需要交互的字段请换一个真组件候选。',
214
+ ));
215
+ }
216
+
217
+ for (const emit of emitNames) {
218
+ const kinds = normalizeKinds(eventMap[emit]);
219
+ if (view !== undefined && !view.emits.includes(emit)) {
220
+ issues.push(issue(
221
+ 'OOUI-CMP-GHOST-EMIT',
222
+ `${path}.eventMap.${emit}`,
223
+ `\`${candidate.component}\` 的契约里没有 \`${emit}\` 这个 emit(契约实有:${view.emits.join(' / ') || '无'})。`,
224
+ '幽灵映射是本层最贵的错:装配不报错、渲染不报错、点击也不报错——'
225
+ + '组件照常 emit 它真正的名字,只是没有人接。表现是"点了没反应",'
226
+ + '与组件坏了、权限拦了在浏览器里无法区分。手写候选表的幽灵率实测 4.2%。',
227
+ `照契约改键名(不要凭组件名猜):${view.emits.map((e) => `\`${e}\``).join(' / ') || '该组件零 emit,请删掉整段 eventMap'}`,
228
+ ));
229
+ }
230
+ if (kinds.length === 0) {
231
+ issues.push(issue(
232
+ 'OOUI-CMP-EVENT-EMPTY',
233
+ `${path}.eventMap.${emit}`,
234
+ `\`${emit}\` 映射到了一个空数组。`,
235
+ '空映射与"没写这条映射"效果相同,但读起来像"已经接过了"。'
236
+ + '下一个人不会再来接它,于是这条线永久缺失。',
237
+ `删掉这一条,或补上语义事件:\`${emit}: 'select'\``,
238
+ ));
239
+ }
240
+ for (const kind of kinds) {
241
+ if (!(SOMA_INTERACTION_KINDS as readonly string[]).includes(kind)) {
242
+ issues.push(issue(
243
+ 'OOUI-CMP-EVENT-UNKNOWN',
244
+ `${path}.eventMap.${emit}`,
245
+ `语义事件 \`${String(kind)}\` 不在封闭集里。`,
246
+ '语义事件名是体系的公事(§16.3):页面与 Domain 只认它。'
247
+ + '写一个集合外的名字,事件路由照样生成,只是接到了一个没人监听的名字上。',
248
+ `改成封闭集里的值。意图族:${SOMA_CONTROLLED_INTERACTION_KINDS.join(' / ')}\n`
249
+ + ` 完成族:${SOMA_AUTONOMOUS_INTERACTION_KINDS.join(' / ')}`,
250
+ ));
251
+ continue;
252
+ }
253
+ // ── 族对齐:完成族事件**只可能**来自自治型 ────────────────────
254
+ //
255
+ // 这道检查是**单向**的,不对称是有理由的:
256
+ // · 受控型 → 完成族:**红**。受控型按定义不自己调接口,它 emit 不出"做完了"。
257
+ // 接成完成族会让页面以为已经存过盘,于是**该调的接口一次都没调**,
258
+ // 而表单看上去提交成功了——不抛异常、不掉日志。
259
+ // · 自治型 → 意图族:**放行**。`select` 动词在两种模型下都必需 `pick`
260
+ // (意图层 VERB_EVENTS_BY_MODEL 的原话:"这次交互的产物就是被选中的东西,
261
+ // 自治型也必须把它交出去")。`pick` 是**产物**不是请求;两族的分界线是
262
+ // "事情做完没有",不是"谁发的"。在这里一刀切会把未来的自治型选择器
263
+ // 判成不合格——那正是 §35 修掉的那类判据缺陷的翻版。
264
+ if (view !== undefined && autonomousKinds.has(kind)) {
265
+ if (deriveControlModel(view).model === 'controlled') {
266
+ issues.push(issue(
267
+ 'OOUI-CMP-EVENT-FAMILY',
268
+ `${path}.eventMap.${emit}`,
269
+ `受控型 \`${candidate.component}\` 把 \`${emit}\` 映射成了完成族事件 \`${kind}\`。`,
270
+ '完成族是"事情已经做完了"的通知,只有自己调接口的自治型发得出来。'
271
+ + '受控型接成完成族,页面会以为已经存过盘,于是**该调的接口一次都没调**,'
272
+ + '而表单看上去提交成功了——没有异常、没有日志。',
273
+ `改成意图族(进行时):${SOMA_CONTROLLED_INTERACTION_KINDS.join(' / ')}\n`
274
+ + ' 若确认它真的会自己调接口,那说明控制模型判错了——'
275
+ + '去 control-model.ts 核对判据(端点 prop / values prop),不要在这里开例外。',
276
+ ));
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ for (const variant of candidate.when?.variant ?? []) {
283
+ if (!(SOMA_DISPLAY_VARIANTS as readonly string[]).includes(variant)) {
284
+ issues.push(issue(
285
+ 'OOUI-CMP-VARIANT-UNKNOWN',
286
+ `${path}.when.variant`,
287
+ `呈现变体 \`${String(variant)}\` 不在 display 变体封闭集里。`,
288
+ '§36.1 裁定 2(不补通用 Text,按 variant 分派到真实组件)要成立,前提是 variant 本身封闭。'
289
+ + '写一个集合外的值不会报错,只会让这条候选永远不被 display 请求匹配到,'
290
+ + '于是那一档静默落到别的候选——表现是"金额没有右对齐、状态没有语气色",没人会当成 bug 报上来。',
291
+ `改成封闭集里的值:${SOMA_DISPLAY_VARIANTS.join(' / ')}`,
292
+ ));
293
+ }
294
+ }
295
+ if ((candidate.when?.variant?.length ?? 0) > 0 && candidate.form !== 'display') {
296
+ issues.push(issue(
297
+ 'OOUI-CMP-VARIANT-MISPLACED',
298
+ `${path}.when.variant`,
299
+ `形态 \`${candidate.form}\` 上写了 variant 过滤条件。`,
300
+ '只有 `display` 有呈现变体(意图层强制其余形态的 variant 恒为 undefined)。'
301
+ + '在别的形态上写 variant,过滤器永远匹配不上——这条候选等于被自己注释掉了,而没有任何日志。',
302
+ '删掉 when.variant;要区分同形态的不同场景请用 cardinality / mutable / minInline。',
303
+ ));
304
+ }
305
+
306
+ const minInline = candidate.when?.minInline ?? 0;
307
+ if (!Number.isFinite(minInline) || minInline < 0) {
308
+ issues.push(issue(
309
+ 'OOUI-CMP-MIN-INLINE',
310
+ `${path}.when.minInline`,
311
+ `最小可用宽 \`${String(minInline)}\` 不是非负有限数(单位 rem)。`,
312
+ 'NaN 参与比较恒为 false,于是这条候选在**任何**宽度下都被判成空间不足,'
313
+ + '静默退出候选池——而报出来的是"没有合格候选",指不回这个数。',
314
+ '写成 rem 数值,如 `minInline: 30`;不挑宽度就别写这个字段。',
315
+ ));
316
+ }
317
+
318
+ for (const cardinality of candidate.when?.cardinality ?? []) {
319
+ if (!isSomaCardinality(cardinality)) {
320
+ issues.push(issue(
321
+ 'OOUI-CMP-CARDINALITY-UNKNOWN',
322
+ `${path}.when.cardinality`,
323
+ `基数 \`${String(cardinality)}\` 不在封闭集里。`,
324
+ '基数词汇漂移(`multiple` / `many` / `list` 三种写法)会让过滤器一个候选都不匹配,'
325
+ + '直接落到"候选全空",而报出来的原因指向候选表,不是写错的这一行。',
326
+ "改成 none / one / few / many / unbounded。",
327
+ ));
328
+ }
329
+ }
330
+
331
+ const dupKey = `${candidate.form}::${candidate.component}`;
332
+ const previous = seen.get(dupKey);
333
+ if (previous !== undefined) {
334
+ issues.push(issue(
335
+ 'OOUI-CMP-DUPLICATE',
336
+ path,
337
+ `\`${candidate.component}\` 在形态 \`${candidate.form}\` 上被 \`${previous}\` 与 \`${vocabulary.name}\` 各注册了一次。`,
338
+ '静默覆盖让"这个形态按谁的 eventMap 接线"取决于数组顺序,而那个顺序写在宿主的一行 import 里;'
339
+ + '改动它的人根本不知道自己在改事件路由,所有测试都绿。',
340
+ `二选一:把后注册的那条删掉;或让业务包换一个形态/组件。\n`
341
+ + ` (与 composeComponentPacks 同一条纪律:重名必须显式解决,不靠 spread 覆盖)`,
342
+ ));
343
+ } else {
344
+ seen.set(dupKey, vocabulary.name);
345
+ }
346
+
347
+ const prefKey = `${candidate.form}::${candidate.preference}`;
348
+ const prefOwner = perFormPreference.get(prefKey);
349
+ if (prefOwner !== undefined) {
350
+ issues.push(issue(
351
+ 'OOUI-CMP-PREFERENCE-CLASH',
352
+ `${path}.preference`,
353
+ `形态 \`${candidate.form}\` 上 \`${prefOwner}\` 与 \`${candidate.component}\` 的 preference 都是 ${candidate.preference}。`,
354
+ '偏好序相同时"取首个"退化成"取数组里排前面那个",而数组顺序不是设计决定。'
355
+ + '换一次 import 顺序就换一个组件,视觉与事件一起变,且没有任何断言拦得住。',
356
+ `给它们不同的 preference(小者优先),并在注释里写清凭什么排前面。`,
357
+ ));
358
+ } else {
359
+ perFormPreference.set(prefKey, candidate.component);
360
+ }
361
+ });
362
+ }
363
+
364
+ if (total === 0) {
365
+ issues.push(issue(
366
+ 'OOUI-CMP-REGISTRY-EMPTY',
367
+ 'vocabularies',
368
+ '装配后一条候选都没有。',
369
+ '零候选的注册表跑出来的"0 条候选全部合法"也是绿的,而那是最坏的一种绿:'
370
+ + '它证明的是校验没跑,不是候选没问题。',
371
+ "至少注册一份组件库词表:composeComponentCandidates([createSomaUiComponentCandidates()], { contracts })",
372
+ ));
373
+ }
374
+
375
+ return issues;
376
+ }
377
+
378
+ /**
379
+ * 装配候选词表。对账不过**当场抛**——构件层的错误必须在编译期发作,
380
+ * 不能留到"页面点了没反应"那一刻(§3.3 失败模式)。
381
+ */
382
+ export function composeComponentCandidates(
383
+ vocabularies: readonly SomaCompositionVocabulary[],
384
+ options: ComposeOptions,
385
+ ): SomaCandidateRegistry {
386
+ const issues = verifyComponentCandidates(vocabularies, options);
387
+ if (issues.length > 0) {
388
+ throw new SomaCompositionError({
389
+ layer: 'assembly',
390
+ code: 'OOUI-CMP-ASSEMBLY-FAILED',
391
+ // 这段拼装此前是仓库里第四份逐字相同的三段式格式化代码(另三份在语义层、
392
+ // 意图层与构件层的 `formatCompositionIssues`,后者已删)。收敛到 `formatOouiIssues` 之后
393
+ // 缩进由参数给出,输出与收敛前逐字一致。
394
+ situation: `候选词表对账失败,共 ${issues.length} 条:\n\n${formatOouiIssues(issues)}`,
395
+ problem: '构件层是唯一知道组件库的层。这里放行的每一条错误,'
396
+ + '都会在运行期表现成"看着正常但点不动",且现场没有指回候选表的线索。',
397
+ remedy: '逐条按上面的建议改;候选表的每一个 emit 键都要去 catalog.json 查证,不要凭组件名猜。',
398
+ issues,
399
+ });
400
+ }
401
+
402
+ const contractByName = new Map(options.contracts.map((view) => [view.name, view]));
403
+ const formOrder = new Map(SOMA_INTERACTION_FORMS.map((form, index) => [form, index]));
404
+ const fieldForms = new Set<string>(SOMA_SCALAR_FORMS);
405
+ const assembled: SomaComponentCandidate[] = [];
406
+
407
+ for (const vocabulary of vocabularies) {
408
+ for (const candidate of vocabulary.candidates) {
409
+ const isTextNode = candidate.component === SOMA_TEXT_NODE;
410
+ const view = contractByName.get(candidate.component);
411
+ const verdict = isTextNode || view === undefined
412
+ ? { model: 'controlled' as const, reason: '纯文本节点:无契约、无状态、无 emit' }
413
+ : deriveControlModel(view);
414
+ const eventMap: Record<string, readonly SomaInteractionKind[]> = {};
415
+ const kinds = new Set<SomaInteractionKind>();
416
+ for (const [emit, value] of Object.entries(candidate.eventMap ?? {})) {
417
+ const list = normalizeKinds(value);
418
+ eventMap[emit] = list;
419
+ for (const kind of list) kinds.add(kind);
420
+ }
421
+ const mapped = new Set(Object.keys(eventMap));
422
+ assembled.push({
423
+ form: candidate.form,
424
+ component: candidate.component,
425
+ vocabulary: vocabulary.name,
426
+ layer: isTextNode ? 'text-node' : (view?.layer ?? 'composite'),
427
+ controlModel: verdict.model,
428
+ controlModelReason: verdict.reason,
429
+ when: {
430
+ cardinality: candidate.when?.cardinality,
431
+ minInline: candidate.when?.minInline ?? 0,
432
+ mutable: candidate.when?.mutable,
433
+ variant: candidate.when?.variant,
434
+ },
435
+ eventMap,
436
+ semanticEvents: [...kinds].sort(),
437
+ unmappedEmits: (view?.emits ?? []).filter((emit) => !mapped.has(emit)).sort(),
438
+ preference: candidate.preference,
439
+ src: candidate.src,
440
+ });
441
+ }
442
+ }
443
+
444
+ // 末位按**码位序**(与默认 `.sort()` 同一口径),不用 `localeCompare`:
445
+ // 后者随运行时的 ICU 与区域设置变(大小写、`_` 与数字的先后都会翻),
446
+ // 同一份词表在两台机器上排出两种顺序,而本层承诺的是逐字节可复现(§19.3)。
447
+ assembled.sort((a, b) => (
448
+ (formOrder.get(a.form)! - formOrder.get(b.form)!)
449
+ || (a.preference - b.preference)
450
+ || (a.component < b.component ? -1 : a.component > b.component ? 1 : 0)
451
+ ));
452
+
453
+ const byForm = new Map<SomaInteractionForm, SomaComponentCandidate[]>();
454
+ for (const candidate of assembled) {
455
+ const group = byForm.get(candidate.form) ?? [];
456
+ group.push(candidate);
457
+ byForm.set(candidate.form, group);
458
+ }
459
+
460
+ return {
461
+ candidates: Object.freeze(assembled),
462
+ forms: SOMA_INTERACTION_FORMS.filter((form) => byForm.has(form)),
463
+ vocabularies: vocabularies.map((v) => ({ name: v.name, version: v.version, count: v.candidates.length })),
464
+ candidatesFor(form) {
465
+ return byForm.get(form) ?? [];
466
+ },
467
+ coverage() {
468
+ return SOMA_INTERACTION_FORMS.map((form) => {
469
+ const group = byForm.get(form) ?? [];
470
+ return {
471
+ form,
472
+ count: group.length,
473
+ pageLevel: !fieldForms.has(form),
474
+ components: group.map((candidate) => candidate.component),
475
+ };
476
+ });
477
+ },
478
+ };
479
+ }
480
+
481
+ export type { SomaCandidateVerdict };
482
+
483
+ /**
484
+ * 上游契约自检:语义事件封闭集必须**恰好**被"受控/自治"两族划分。
485
+ *
486
+ * `OOUI-CMP-EVENT-FAMILY` 那道检查完全建立在这个划分上。意图层若给封闭集加了一个词
487
+ * 却忘了归族,本层的族判定会静默地"两族都查不到 ⇒ 两边都不报",
488
+ * 于是一条接错族的映射被放行——而接错族的后果是提交两次或一次都不提交,都不抛异常。
489
+ * 所以划分本身必须先被断言一次,再拿它去判别人。
490
+ *
491
+ * 返回问题列表(空数组即通过),由测试与闸门调用。
492
+ */
493
+ export function verifyInteractionKindPartition(): string[] {
494
+ const problems: string[] = [];
495
+ const controlled = new Set<string>(SOMA_CONTROLLED_INTERACTION_KINDS);
496
+ const autonomous = new Set<string>(SOMA_AUTONOMOUS_INTERACTION_KINDS);
497
+ for (const kind of SOMA_INTERACTION_KINDS) {
498
+ const inControlled = controlled.has(kind);
499
+ const inAutonomous = autonomous.has(kind);
500
+ if (!inControlled && !inAutonomous) {
501
+ problems.push(
502
+ `语义事件 \`${kind}\` 两族都没归:构件层的族对齐检查会静默放行它的任何映射。`
503
+ + ' 建议:去 intent/intent.types.ts 把它加进 SOMA_CONTROLLED_INTERACTION_KINDS'
504
+ + '(进行时、页面持有状态)或 SOMA_AUTONOMOUS_INTERACTION_KINDS(过去式、组件持有状态)。',
505
+ );
506
+ }
507
+ if (inControlled && inAutonomous) {
508
+ problems.push(
509
+ `语义事件 \`${kind}\` 同时在两族里:族判定取决于代码先查哪一族,而那不是设计决定。`
510
+ + ' 建议:去 intent/intent.types.ts 删掉其中一处。',
511
+ );
512
+ }
513
+ }
514
+ for (const kind of [...controlled, ...autonomous]) {
515
+ if (!(SOMA_INTERACTION_KINDS as readonly string[]).includes(kind)) {
516
+ problems.push(`\`${kind}\` 在分族名单里却不在 SOMA_INTERACTION_KINDS 里,删掉它。`);
517
+ }
518
+ }
519
+ return problems;
520
+ }