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,290 @@
1
+ // 样张派生(REQ-012):契约字段的 example → 一份可直接渲染的 props。
2
+ //
3
+ // 为什么是派生而不是另写一份样张:另写一份就有两个真源,改了 prop 忘了改样张不会
4
+ // 报错——展示页与视觉矩阵会安静地拿旧形状渲染。示例值挂在字段上,样张是它的函数。
5
+ //
6
+ // 派生规则(对每个字段,递归):
7
+ // 1. example 有值 → 用它
8
+ // 2. type object → 逐 fields 递归
9
+ // 3. type array → 用 items 派生一个元素,组成单元素数组
10
+ // 4. 否则取 default
11
+ // 5. required 却以上都无 → 记一条缺口(由闸门报,不在这里抛)
12
+
13
+ import type {
14
+ SomaComponentContract,
15
+ SomaContractField,
16
+ } from '../types/component-contract.types';
17
+
18
+ export interface SampleGap {
19
+ /** 缺口路径,如 `order.items[].sku_id` */
20
+ path: string;
21
+ reason: string;
22
+ }
23
+
24
+ export interface DeriveSampleResult {
25
+ /** 可直接喂给组件的 props */
26
+ props: Record<string, unknown>;
27
+ /** 必填却既无 example 也无 default 的字段 */
28
+ gaps: SampleGap[];
29
+ }
30
+
31
+ /**
32
+ * 契约没给内容时怎么收尾。样张与橱窗预览**只在这两处**不同,其余规则(示例优先、
33
+ * 对象逐字段、数组派生一个元素、默认值、枚举取第一个)是同一条递归——
34
+ * 曾经两份各写一遍,改了一份忘了另一份不会报错,只会让目录样张与橱窗预览悄悄分叉。
35
+ */
36
+ interface SampleStrategy {
37
+ /** 这个字段的 default 采不采用(样张一律采用;预览对展示型空数组放弃它) */
38
+ useDefault(field: SomaContractField, path: string): boolean;
39
+ /** 示例 / 子字段 / 元素 / 默认 / 枚举都给不出值时的收尾 */
40
+ fallback(field: SomaContractField, path: string): unknown;
41
+ }
42
+
43
+ function walkField(field: SomaContractField, path: string, strategy: SampleStrategy): unknown {
44
+ if (field.example !== undefined) return clone(field.example);
45
+
46
+ if (field.type === 'object') {
47
+ const fields = field.fields;
48
+ if (fields && Object.keys(fields).length > 0) {
49
+ const out: Record<string, unknown> = {};
50
+ for (const [name, child] of Object.entries(fields)) {
51
+ const value = walkField(child, `${path}.${name}`, strategy);
52
+ if (value !== undefined) out[name] = value;
53
+ }
54
+ return out;
55
+ }
56
+ }
57
+
58
+ if (field.type === 'array') {
59
+ const items = field.items;
60
+ if (items) {
61
+ const element = walkField(items, `${path}[]`, strategy);
62
+ if (element !== undefined) return [element];
63
+ }
64
+ }
65
+
66
+ if (field.default !== undefined && strategy.useDefault(field, path)) return clone(field.default);
67
+
68
+ if (field.type === 'enum' && field.values && field.values.length > 0) {
69
+ // 枚举没写示例也没写默认时取第一个取值:比空串更接近真实形状,且不用作者多写一行
70
+ return field.values[0];
71
+ }
72
+
73
+ return strategy.fallback(field, path);
74
+ }
75
+
76
+ function walkProps(
77
+ contract: SomaComponentContract,
78
+ strategy: SampleStrategy,
79
+ ): Record<string, unknown> {
80
+ const props: Record<string, unknown> = {};
81
+ for (const [name, prop] of Object.entries(contract.props ?? {})) {
82
+ const value = walkField(prop, name, strategy);
83
+ if (value !== undefined) props[name] = value;
84
+ }
85
+ return props;
86
+ }
87
+
88
+ /** 样张:默认值照用;必填却什么都没给 → 记一条缺口(由闸门报,不在这里抛) */
89
+ function sampleStrategy(gaps: SampleGap[]): SampleStrategy {
90
+ return {
91
+ useDefault: () => true,
92
+ fallback(field, path) {
93
+ if (field.required === true) {
94
+ gaps.push({
95
+ path,
96
+ reason: '必填字段既没有 示例 也没有 默认——样张渲染不出这一格',
97
+ });
98
+ }
99
+ return undefined;
100
+ },
101
+ };
102
+ }
103
+
104
+ function clone<T>(value: T): T {
105
+ if (value === null || typeof value !== 'object') return value;
106
+ return JSON.parse(JSON.stringify(value)) as T;
107
+ }
108
+
109
+ /** 从契约派生一份样张 props */
110
+ export function deriveContractSample(contract: SomaComponentContract): DeriveSampleResult {
111
+ const gaps: SampleGap[] = [];
112
+ const props = walkProps(contract, sampleStrategy(gaps));
113
+ return { props, gaps };
114
+ }
115
+
116
+ /**
117
+ * 组件目录的交互式预览不能因作者还没补 example 而退化成空态。
118
+ *
119
+ * 这份值仍以契约为唯一输入:example/default/enum 永远优先;只有契约没有给出
120
+ * 内容时,才按字段类型和名称补一个确定的「预览基线」。它只用于开发者橱窗,
121
+ * 不回写目录样张、更不进入业务页面或生成链。
122
+ */
123
+ export interface DerivePreviewSampleResult {
124
+ props: Record<string, unknown>;
125
+ /** 使用了预览基线的字段路径,供调用方如实标注来源。 */
126
+ synthesized: string[];
127
+ }
128
+
129
+ function previewFieldName(path: string): string {
130
+ const segments = path.replace(/\[\]/gu, '').split('.');
131
+ return (segments[segments.length - 1] ?? '').toLocaleLowerCase();
132
+ }
133
+
134
+ function previewString(path: string): string {
135
+ const name = previewFieldName(path);
136
+ if (/(?:^|_)(?:id|key|code|sku)(?:$|_)/u.test(name)) return 'sample-item';
137
+ if (/(?:date|day)$/u.test(name)) return '2026-01-15';
138
+ if (/(?:time|hour)$/u.test(name)) return '09:30';
139
+ if (/(?:email|mail)$/u.test(name)) return 'demo@example.test';
140
+ if (/(?:phone|tel|mobile)$/u.test(name)) return '138 0000 0000';
141
+ if (/(?:url|href|link)$/u.test(name)) return '#';
142
+ if (/(?:src|image|avatar|cover|icon)$/u.test(name)) return '';
143
+ if (/(?:color|colour)$/u.test(name)) return 'var(--soma-c-primary)';
144
+ if (/(?:placeholder)$/u.test(name)) return '请输入内容';
145
+ if (/(?:label|action|button|cta)$/u.test(name)) return '示例操作';
146
+ if (/(?:title|name|heading)$/u.test(name)) return '示例标题';
147
+ if (/(?:description|summary|message|hint|help|text|content|subtitle)$/u.test(name)) {
148
+ return '这是一段来自组件契约的预览内容';
149
+ }
150
+ if (/(?:status|state)$/u.test(name)) return 'active';
151
+ return '示例值';
152
+ }
153
+
154
+ function previewNumber(path: string): number {
155
+ const name = previewFieldName(path);
156
+ if (/(?:percent|ratio|rate|progress)$/u.test(name)) return 68;
157
+ if (/(?:quantity|qty|count|index|page|step)$/u.test(name)) return 2;
158
+ if (/(?:price|amount|total|cost|value)$/u.test(name)) return 1280;
159
+ return 42;
160
+ }
161
+
162
+ function previewBoolean(path: string): boolean {
163
+ const name = previewFieldName(path);
164
+ if (/(?:open|visible|expanded|selected|checked|active)$/u.test(name)) return true;
165
+ return false;
166
+ }
167
+
168
+ function previewRecord(): Record<string, unknown> {
169
+ return {
170
+ id: 'sample-item',
171
+ key: 'sample-item',
172
+ name: '示例名称',
173
+ label: '示例标签',
174
+ title: '示例标题',
175
+ subtitle: '用于组件预览的辅助说明',
176
+ description: '这是一段来自组件契约的预览内容',
177
+ value: 'sample-value',
178
+ text: '示例内容',
179
+ status: 'active',
180
+ amount: 1280,
181
+ count: 12,
182
+ quantity: 2,
183
+ };
184
+ }
185
+
186
+ function previewArrayFallback(path: string): unknown[] {
187
+ const name = previewFieldName(path);
188
+ if (/(?:ids?|keys?|values?|dates?|tags?)$/u.test(name)) return [previewString(path)];
189
+ return [previewRecord()];
190
+ }
191
+
192
+ /**
193
+ * An empty default has two very different meanings in component contracts:
194
+ * presentational collections need one row in a developer specimen, while
195
+ * behaviour/configuration collections (`reactions`, `conditions`, validators,
196
+ * and the like) mean "do not enable this feature". A generic record is valid
197
+ * enough to render a list, but invalid as a reaction or rule declaration.
198
+ *
199
+ * Keep this intentionally an allowlist. A newly named array remains faithful
200
+ * to its product default until its contract supplies `items`, makes it
201
+ * required, or explicitly joins a recognised visual collection family.
202
+ */
203
+ const PRESENTATIONAL_EMPTY_ARRAY_FIELDS = new Set([
204
+ 'accounts',
205
+ 'actions',
206
+ 'balances',
207
+ 'blockers',
208
+ 'breadcrumb',
209
+ 'candidates',
210
+ 'checks',
211
+ 'columns',
212
+ 'data',
213
+ 'detail',
214
+ 'downstream',
215
+ 'entries',
216
+ 'events',
217
+ 'evidence',
218
+ 'facts',
219
+ 'files',
220
+ 'items',
221
+ 'lab_panels',
222
+ 'lines',
223
+ 'messages',
224
+ 'presets',
225
+ 'products',
226
+ 'records',
227
+ 'rows',
228
+ 'stats',
229
+ 'steps',
230
+ 'tabs',
231
+ 'tags',
232
+ 'upstream',
233
+ ]);
234
+
235
+ function shouldSynthesizeEmptyArrayPreview(field: SomaContractField, path: string): boolean {
236
+ return field.required === true
237
+ || field.items !== undefined
238
+ || PRESENTATIONAL_EMPTY_ARRAY_FIELDS.has(previewFieldName(path));
239
+ }
240
+
241
+ /** 预览:展示型空数组放弃默认值走基线;契约什么都没给就按类型与名字补一个确定值 */
242
+ function previewStrategy(synthesized: string[]): SampleStrategy {
243
+ return {
244
+ // A product-facing empty array is often the correct default. Only recognised
245
+ // visual collections may reach the deterministic preview fallback: arbitrary
246
+ // configuration arrays must stay empty so a generic record cannot enable an
247
+ // invalid reaction/rule/condition in the component preview.
248
+ useDefault: (field, path) => !(field.type === 'array'
249
+ && Array.isArray(field.default)
250
+ && field.default.length === 0
251
+ && shouldSynthesizeEmptyArrayPreview(field, path)),
252
+ fallback(field, path) {
253
+ synthesized.push(path);
254
+ if (field.type === 'string') return previewString(path);
255
+ if (field.type === 'number') return previewNumber(path);
256
+ if (field.type === 'boolean') return previewBoolean(path);
257
+ if (field.type === 'array') return previewArrayFallback(path);
258
+ if (field.type === 'object') return previewRecord();
259
+ return undefined;
260
+ },
261
+ };
262
+ }
263
+
264
+ /** 从完整契约派生开发者橱窗的可运行预览基线。 */
265
+ export function deriveContractPreviewSample(contract: SomaComponentContract): DerivePreviewSampleResult {
266
+ const synthesized: string[] = [];
267
+ const props = walkProps(contract, previewStrategy(synthesized));
268
+ return { props, synthesized };
269
+ }
270
+
271
+ /** 单个字段的样张值(组件内部或工具按需调用) */
272
+ export function deriveFieldSample(field: SomaContractField): unknown {
273
+ return walkField(field, '', sampleStrategy([]));
274
+ }
275
+
276
+ /**
277
+ * 这份契约够不够渲染一张有内容的样张。
278
+ *
279
+ * 判据是「至少有一个 prop 写了 example,或有嵌套字段写了 example」——
280
+ * 全靠 default 派生出来的是空态,展示页放上去只有一个空壳。
281
+ */
282
+ export function hasRenderableSample(contract: SomaComponentContract): boolean {
283
+ const seen = (field: SomaContractField): boolean => {
284
+ if (field.example !== undefined) return true;
285
+ if (field.fields && Object.values(field.fields).some(seen)) return true;
286
+ if (field.items && seen(field.items)) return true;
287
+ return false;
288
+ };
289
+ return Object.values(contract.props ?? {}).some(seen);
290
+ }