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,78 @@
1
+ // 语义层 ① · 表层契约:人与 AI 直接书写的那一份(架构 §3.1.1)。
2
+ //
3
+ // ── 为什么这一层的失败模式是静默的 ─────────────────────────────────
4
+ // 表层与核心层**字段名几乎一样、结构几乎一样**,只在几处值形态上不同
5
+ // (`no: text!` vs `no: { type:'text', rules:[…] }`)。这种"几乎一样"正是最坏的形状:
6
+ //
7
+ // ① **表层被当成核心层直接喂进管线**。`{ type: 'text!' }` 里的 `'text!'`
8
+ // 不在字段类型封闭集里,可是**没有任何一处会红**:双态映射拿不到匹配就按
9
+ // text 渲染(见 `data-config.types.ts` 头注释里 `type: percent` 那次事故),
10
+ // 必填规则整个不存在,表单点提交直接过。页面看着完全正常,
11
+ // 而"这个字段本来必填"这件事从声明里静默蒸发了。
12
+ //
13
+ // ② **简写在多处各展开一次**。表层没有一处把"哪些是简写"钉死的话,
14
+ // 解析器展一次、闸门展一次、soma-ai 的提示词里再展一次,
15
+ // 三处对 `enum(a|b|c)` 的空格与转义处理必然不同。
16
+ // 分叉的表现是"同一份 YAML 在预览里有三个候选、在发布产物里只有两个",
17
+ // 而两边各自都跑得出来。**所以简写只准有一个展开点**(§2.3 判据 3)。
18
+ //
19
+ // ③ **表层键写错静默变成"没这一项"**。`stauts: state` 与 `status: state`
20
+ // 在映射里都是合法键。不封闭键集的话,前者的下场是角色没设上——
21
+ // 演绎出的页面少一个状态位,而少一位与"这个对象本来就没状态"同形。
22
+ // 本文件因此把顶层键分成四类(字段字典 / 角色 / 关系简写 / 核心层直写),
23
+ // 落不进任何一类的键**当场红**,并按别名表提示他多半想写哪个角色。
24
+ //
25
+ // ── 三条简写(架构 §3.1.1 表格),只此三条 ───────────────────────────
26
+ //
27
+ // order:
28
+ // fields:
29
+ // no: text! # ! = required
30
+ // amount: money
31
+ // status: enum(pending|paid|shipped) # 枚举候选内联
32
+ // title: no # 角色直接写在对象下
33
+ // items: -> order_item[] # 关系:-> 目标[] 即 many
34
+ //
35
+ // 三条都必须**确定性展开**:展不开就编译期红,不猜(§3.1.3 失败模式原文)。
36
+ // 扩第四条简写是一次体系决策——它连带要改展开器、闸门、AI 侧 schema 与外部文档,
37
+ // 而"少改一处"的表现是某一条书写路径认它、另一条不认。
38
+ //
39
+ // ── 逃生门 ─────────────────────────────────────────────────────────
40
+ // 表层可以直接写核心层的完整形态,跳过简写(§3.1.3 原文)。
41
+ // 这不是妥协:三条简写覆盖不了 `permission` / `calc` / `modifies` / `name`,
42
+ // 而**表层至今没有"可写"的简写**——所以 `verb: edit` 的页面必须走逃生门
43
+ // 给至少一个字段写 `{ type: text, permission: w }`,否则演绎出的是只读页
44
+ // (由 `OOUI-SEM-EDIT-ALL-READONLY` 当场拦下,见 `validate-request.ts`)。
45
+ //
46
+ // ── 表层对象的四类顶层键 ───────────────────────────────────────────
47
+ // 一个表层对象的书写形态(不含对象名——名字是它所在映射里的键)。
48
+ // 判据按**键名 + 值形态**联合裁决(见 `normalize.ts` 的分派):
49
+ //
50
+ // | 类 | 形状 | 例 |
51
+ // |------------|---------------------------------------------|--------------------------|
52
+ // | 字段字典 | `fields:` 映射,值是简写串或核心层完整形态 | `fields: { no: text! }` |
53
+ // | 角色直写 | 键 ∈ 角色封闭集,值是字段名 | `title: no` |
54
+ // | 关系简写 | 值是 `-> 目标[]` 形态的字符串 | `items: -> order_item[]` |
55
+ // | 核心层直写 | 键 ∈ 下面的 `SOMA_SURFACE_PASSTHROUGH_KEYS` | `write: { update: {…} }` |
56
+ //
57
+ // 这里刻意**没有**描述表层对象的 TypeScript 类型:表层的真实来源是 YAML,
58
+ // 类型层面挡不住任何东西,**校验必须在运行期做**(`normalizeSurfaceObject` 收的是 `unknown`)。
59
+ // 一个没人消费的类型只会给人"类型已经保证了"的错觉,而那正是 ③ 的温床。
60
+
61
+ /**
62
+ * 表层允许**原样直写**的核心层键(逃生门的闭集)。
63
+ *
64
+ * `deps` 不在表里:它是**解析期提取的产物**(params 引用到的其他对象名),
65
+ * 不是可书写的东西。让人写 deps 等于让他手工维护一份拓扑序,
66
+ * 写错了表现为取数顺序不对、某个对象拿到空参数——而那时页面只是"数据没出来"。
67
+ *
68
+ * `predicates` / `reactions` / `window` 同样不在表里,但理由不同:它们的核心层形态
69
+ * **都是编译产物**,不是可书写形态——`reactions` 是 DAG + 拓扑序 + 回溯表,
70
+ * `window` 带一个按字段名+类型+权限算出来的 `dict_hash`(后端据它拒收"按一份它不认识的
71
+ * 字典发来的意图")。手写这两样等于让人手工维护一份拓扑序和一个哈希。
72
+ * 而"谁把 `reactions:` 声明编译成图"架构至今没有指定(§34.5 的真空)。
73
+ * **在那件事被裁定之前,本层拒收它们**——收下一个填不满的键,
74
+ * 表现是"联动声明写了但一点反应都没有",而这正是本体系明令禁止的静默降级。
75
+ */
76
+ export const SOMA_SURFACE_PASSTHROUGH_KEYS: readonly string[] = [
77
+ 'fields', 'roles', 'relations', 'write', 'from', 'transform', 'mock',
78
+ ];
@@ -0,0 +1,267 @@
1
+ // 语义层 ① · 语义请求的声明期闸门:`{object, verb, focus?, through?}` 自洽性。
2
+ //
3
+ // ── 为什么这一层的失败模式是静默的 ─────────────────────────────────
4
+ // 本模块拦的四类错,**没有一类会让管线抛异常**——它们全都能演绎出一个页面,
5
+ // 只是那个页面答非所问,而"答非所问"这件事只有懂业务的人打开才看得出来:
6
+ //
7
+ // ① `verb` 拼错(`'update'` 而不是 `'edit'`)。下游意图层找不到形态规则,
8
+ // 报的是"这个形态没有候选组件"——指向构件层,不指向写错的那一行。
9
+ // 作者会去查候选表,而问题在请求里。
10
+ // ② `compare` / `navigate` 少了 `through`。演绎照常完成,产出的是
11
+ // "多个实验并排"而不是"一个实验内部的多个变体"(§27.2 盲测当场卡住的地方)。
12
+ // 两者都是合法界面,肉眼分不出哪个是错的。
13
+ // ③ `edit` / `advance` 落在没有 `write` 段的对象上。§16.4 立了硬闸门
14
+ // "写事件必须在对象的 write 段有端点",但闸门此前**无处取数**(§34.1)。
15
+ // 不拦的后果:演绎出一张能填的表单,点提交没有任何接口可调——
16
+ // 而"点了没反应"是本仓库反复点名的最难查的一类。
17
+ // ④ `edit` 落在**全部字段都是只读**的对象上。`permission` 缺省是 `r`
18
+ // (与仓库真源一致),于是"最小声明 + verb: edit"会静默演绎出一张
19
+ // **只读详情页**(§34.2)。它长得像成功。
20
+ //
21
+ // ── 一条裁定:`verb: edit` **不翻转**字段默认可写性 ─────────────────
22
+ // §34.2 记录了文档自相矛盾并要求裁定"edit 是否让 permission 默认变 w"。
23
+ // 本模块取**不翻转**,理由是"同一个词在两处有两个答案"正是本仓库
24
+ // 反复付学费的那类缺陷:字典里的 `permission` 是**字段自身的性质**,
25
+ // 不该随这一次的动词改变含义——否则同一份 `_data.yaml`
26
+ // 在 inspect 页上是只读、在 edit 页上凭空可写,而两处都没写过这件事。
27
+ // 代价是"最小声明 + edit"不再自动可用;对策不是猜,而是 ④ 那条**当场报错**,
28
+ // 消息里直接给出该写哪一行。这与铁律三(不猜测意图)一致。
29
+
30
+ import { oouiIssue } from '../diagnostics';
31
+ import {
32
+ validateFieldModifies,
33
+ validateObjectRelations,
34
+ validateObjectRoles,
35
+ } from '../../../shared/object-semantics';
36
+ import {
37
+ isSomaVerb,
38
+ SOMA_VERBS,
39
+ SOMA_VERBS_REQUIRING_THROUGH,
40
+ SOMA_WRITE_VERBS,
41
+ type SomaSemanticIssue,
42
+ type SomaSemanticRequest,
43
+ type SomaVerb,
44
+ } from './kernel.types';
45
+ import type { SomaTrace } from '../trace/trace.types';
46
+ import { traceLink } from '../trace/trace.types';
47
+
48
+ /**
49
+ * 构造一条诊断。**真源在横切层**(`../diagnostics/issue.types.ts` 的 `oouiIssue`)——
50
+ * 本层此前有一份六行的私有实现,意图层与构件层各有一份逐字相同的抄本。
51
+ * 参数次序与原实现一致,收敛不必动任何调用点的实参。
52
+ */
53
+ const issue = oouiIssue;
54
+
55
+ function relationNames(request: SomaSemanticRequest): string[] {
56
+ return (request.object.relations ?? []).map((relation) => relation.name);
57
+ }
58
+
59
+ /**
60
+ * 校验一条语义请求。返回空数组即合法。
61
+ *
62
+ * **纯函数**:不读文件、不看时钟、不改入参。同一份输入必然给出逐字相同的输出,
63
+ * 这是 §19.3 幂等要求在本层的落点,也是它能被闸门直接消费的前提。
64
+ */
65
+ export function validateSemanticRequest(request: SomaSemanticRequest): SomaSemanticIssue[] {
66
+ const issues: SomaSemanticIssue[] = [];
67
+ const object = request.object;
68
+ const fields = object.fields ?? {};
69
+ const fieldKeys = Object.keys(fields);
70
+
71
+ // ── ① 动词封闭集 ───────────────────────────────────────────────────
72
+ if (!isSomaVerb(request.verb)) {
73
+ issues.push(issue(
74
+ 'OOUI-SEM-VERB-UNKNOWN',
75
+ 'request.verb',
76
+ `动词 "${String(request.verb)}" 不在封闭集里`,
77
+ '动词是封闭集:开放成自由字符串会让推导退化成字符串匹配,'
78
+ + '非法值要到"这个形态没有候选组件"那一步才发作,而那句话指向的是构件层,不是这一行。',
79
+ `八动词二选一:${SOMA_VERBS.join(' / ')}。`
80
+ + '确实需要第九个动词的话,那是一次体系决策(§27.4:一个新动词连带要备齐'
81
+ + '候选组件 + 空间规则 + 事件映射 + 呈现规则),不在声明里临时扩。',
82
+ ));
83
+ // 动词不合法时,后面按动词分派的判据全部无意义,直接返回
84
+ return issues;
85
+ }
86
+ const verb: SomaVerb = request.verb;
87
+
88
+ // ── ② 对象自身的语义声明(roles / relations / modifies) ─────────────
89
+ // 解析器已经查过一遍,这里再查一遍是因为**请求可以被程序构造**
90
+ // (演绎链、测试夹具、soma-ai 生成),那条路径根本不经过解析器。
91
+ for (const text of validateObjectRoles(object.roles, fieldKeys)) {
92
+ issues.push(issue(
93
+ 'OOUI-SEM-ROLES-INVALID',
94
+ 'object.roles',
95
+ text,
96
+ '角色指错字段不会报错,只会让那一位静默留空——标题栏空白、状态位不出现、'
97
+ + '对比没有基准。页面照常渲染,接口照常 200。',
98
+ '把角色指向 fields 里真实存在的字段名;角色名本身是封闭集,不要临时扩。',
99
+ ));
100
+ }
101
+ for (const text of validateObjectRelations(object.relations, {
102
+ fieldKeys,
103
+ selfName: object.name,
104
+ })) {
105
+ issues.push(issue(
106
+ 'OOUI-SEM-RELATIONS-INVALID',
107
+ 'object.relations',
108
+ text,
109
+ '关系是 compare / navigate 两个动词(占封闭集 25%)的作用路径。'
110
+ + '声明不合法时它们取不到目标对象的字典,演绎出的是一片空白而不是一条报错。',
111
+ '按 `{ name, to, cardinality: one|many, via? }` 补齐;'
112
+ + 'via 必须是本对象字典里的外键字段。',
113
+ ));
114
+ }
115
+ for (const text of validateFieldModifies(fields)) {
116
+ issues.push(issue(
117
+ 'OOUI-SEM-MODIFIES-INVALID',
118
+ 'object.fields',
119
+ text,
120
+ '修饰关系不成立时,修饰字段不会被并入被修饰者,而是自己占一格——'
121
+ + '页面上多出一排空的 `status_tone: —`(§15.3 原型跑出来的第一个缺口)。',
122
+ '让 modifies 指向同一本字典里的另一个字段,且那个字段自己不带 modifies(修饰不可级联)。',
123
+ ));
124
+ }
125
+
126
+ // ── ③ focus 子集 ───────────────────────────────────────────────────
127
+ if (request.focus !== undefined) {
128
+ if (request.focus.length === 0) {
129
+ issues.push(issue(
130
+ 'OOUI-SEM-FOCUS-EMPTY',
131
+ 'request.focus',
132
+ 'focus 是空数组',
133
+ '"省略 focus" 表示全部字段,"focus: []" 表示一个字段都不要——'
134
+ + '后者演绎出的是一张空界面,而它看起来像加载失败。两者不该长得这么像。',
135
+ '要全部字段就删掉 focus 这一行;确实只要几个字段就把它们列出来。',
136
+ ));
137
+ }
138
+ for (const name of request.focus) {
139
+ if (!(name in fields)) {
140
+ issues.push(issue(
141
+ 'OOUI-SEM-FOCUS-UNKNOWN',
142
+ 'request.focus',
143
+ `focus 里的 "${name}" 不在对象字典里`
144
+ + `(本对象字典:${fieldKeys.length > 0 ? fieldKeys.join(' / ') : '(空,未声明 fields)'})`,
145
+ 'focus 指错的字段会被静默丢弃:产出的界面少一格,而少的那一格'
146
+ + '既不在错误里也不在 trace 里——作者只会觉得"怎么没显示"。',
147
+ fieldKeys.length > 0
148
+ ? `改成字典里已有的字段名,或先在 fields 里补上 "${name}"。`
149
+ : '先给对象补 `fields:` 字段字典,focus 才有对账的依据。',
150
+ ));
151
+ }
152
+ }
153
+ }
154
+
155
+ // ── ④ through:动词的作用路径(§27.2) ──────────────────────────────
156
+ const names = relationNames(request);
157
+ const needsThrough = (SOMA_VERBS_REQUIRING_THROUGH as readonly string[]).includes(verb);
158
+ if (request.through === undefined) {
159
+ if (needsThrough) {
160
+ issues.push(issue(
161
+ 'OOUI-SEM-THROUGH-REQUIRED',
162
+ 'request.through',
163
+ `动词 "${verb}" 没有给 through(动词的作用路径)`,
164
+ `${verb} 的作用对象往往**不是主对象**:要比的是"一个实验内部的多个变体",`
165
+ + '要导航的必然是关系的另一端。缺了 through,演绎照样出得来一个页面,'
166
+ + '只是那个页面答非所问——而两者都是合法界面,肉眼分不出哪个是错的。',
167
+ names.length > 0
168
+ ? `补一条 \`through: '${names[0]}'\`(本对象已声明的关系:${names.join(' / ')});`
169
+ + '确实要作用于对象本身的话,说明这个动词用错了。'
170
+ : '本对象还没有 relations,先补一条:'
171
+ + '`relations: [{ name: variants, to: variant, cardinality: many }]`。',
172
+ ));
173
+ }
174
+ } else if (!names.includes(request.through)) {
175
+ issues.push(issue(
176
+ 'OOUI-SEM-THROUGH-UNKNOWN',
177
+ 'request.through',
178
+ `through "${request.through}" 不是本对象的关系名`
179
+ + `(已声明的关系:${names.length > 0 ? names.join(' / ') : '(无)'})`,
180
+ 'through 是按关系名解析的。指错时下游拿不到目标对象,'
181
+ + '关联区会演绎成空——不是报错,是空白。',
182
+ names.length > 0
183
+ ? `改成 ${names.join(' / ')} 之一。`
184
+ : '先在对象上声明 relations,再让 through 指向其中一条的 name。',
185
+ ));
186
+ }
187
+
188
+ // ── ⑤ 写通道:§16.4 的硬闸门,这里是它的取数点 ─────────────────────
189
+ if ((SOMA_WRITE_VERBS as readonly string[]).includes(verb)) {
190
+ const write = object.write;
191
+ if (write === undefined) {
192
+ issues.push(issue(
193
+ 'OOUI-SEM-NO-WRITE',
194
+ 'object.write',
195
+ `动词 "${verb}" 会产生写意图,但对象 "${object.name}" 没有 write 段(= 只读对象)`,
196
+ '§16.4:演绎不得凭空产生写通道——写事件必须在对象的 write 段里有登记的端点。'
197
+ + '这与取数白名单同构:AI 或推导都不能凭空绑上一个没登记的接口。'
198
+ + '不拦的后果是演绎出一张能填的表单,点提交没有任何接口可调,且不报错。',
199
+ '在数据对象上补写通道(create / update 至少一个):\n'
200
+ + ` data:\n ${object.name}:\n write:\n`
201
+ + ' update: { api: /api/xxx/update, method: PATCH }\n'
202
+ + ' 确实只读的话,把动词换成 inspect / browse。',
203
+ ));
204
+ } else if (write.create === undefined && write.update === undefined) {
205
+ // 平铺解析器已经拦过这一条;程序构造的对象绕不过它,所以这里补一道
206
+ issues.push(issue(
207
+ 'OOUI-SEM-WRITE-NO-ENDPOINT',
208
+ 'object.write',
209
+ `对象 "${object.name}" 有 write 段,但 create 与 update 都没给`,
210
+ '两个端点都没有的写通道不写任何东西。它的存在只会让 §16.4 的闸门误判为"有写通道"。',
211
+ '补上 `write.update: { api: …, method: PATCH }` 或 `write.create: { … }`。',
212
+ ));
213
+ }
214
+
215
+ if (verb === 'edit') {
216
+ const writable = fieldKeys.filter((key) => fields[key].permission === 'w');
217
+ if (fieldKeys.length > 0 && writable.length === 0) {
218
+ issues.push(issue(
219
+ 'OOUI-SEM-EDIT-ALL-READONLY',
220
+ 'object.fields',
221
+ `动词 edit,但对象 "${object.name}" 的 ${fieldKeys.length} 个字段没有一个是 permission: w`,
222
+ 'permission 缺省是 r(字段自身的性质,不随动词翻转——见本文件头注释的裁定)。'
223
+ + '于是"最小声明 + verb: edit"会静默演绎出一张**只读详情页**:'
224
+ + '它有标题、有字段、有布局,唯独一个都改不了,而这看起来像成功(§34.2)。',
225
+ '给真正可写的字段显式标出权限:\n'
226
+ + ` fields:\n ${fieldKeys[0]}: { type: text, permission: w }\n`
227
+ + ' 全部只读就把动词换成 inspect。',
228
+ ));
229
+ }
230
+ }
231
+ }
232
+
233
+ return issues;
234
+ }
235
+
236
+ // ── 本层的 trace(架构 §5 链条的第 ① 跳) ────────────────────────────
237
+ //
238
+ // 为什么这条必须存在:整条链的**根**就是它。§5 的硬要求是"能从一个 CSS 规则
239
+ // 一路回溯到**某句对象声明**",而"某句对象声明"在链条上没有落点的话,
240
+ // 回溯会止于意图层的一条规则名——那时报告说得出"命中了哪条规则",
241
+ // 却说不出"这条规则是看着你写的哪一行命中的"。少的那一跳恰好是作者唯一能改的那一跳。
242
+
243
+ /**
244
+ * 把一次语义请求翻成 trace(链条的根)。
245
+ *
246
+ * `from` 取 `request.src`;合成请求(工具、测试)没有真实文件时退到
247
+ * `<generated:request>` 形态而**不留空**——留空的那一刻,"这条是谁给的"就永久丢失了。
248
+ *
249
+ * `origin: 'declared'` 是本条的定性:请求是作者写下的,不是推出来的。
250
+ * 报告因此不会在这一行印"命中规则「…」"(见 `renderTrace` 的 `traceBody`)。
251
+ */
252
+ export function semanticRequestTrace(request: SomaSemanticRequest): SomaTrace {
253
+ const fields = Object.keys(request.object.fields ?? {});
254
+ const focus = request.focus === undefined || request.focus.length === 0
255
+ ? ''
256
+ : `,focus 限定 ${request.focus.join(' / ')}`;
257
+ const through = request.through === undefined ? '' : `,through 关系 ${request.through}`;
258
+ return traceLink({
259
+ layer: 'semantic',
260
+ conclusion: `对象 ${request.object.name} 声明了 ${fields.length} 个字段`
261
+ + `(${fields.join('、') || '一个都没有'}),本次动词 ${request.verb}${focus}${through}`,
262
+ rule: 'declared',
263
+ from: request.src ?? { file: '<generated:request>', path: request.object.name },
264
+ path: request.object.name,
265
+ origin: 'declared',
266
+ });
267
+ }