soma-core 1.4.0 → 1.4.1

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 (121) hide show
  1. package/README.md +1 -1
  2. package/core/build/blocks/block-compile.ts +31 -2
  3. package/core/build/blocks/block-container.ts +2 -11
  4. package/core/build/compiler/page-declaration-compiler.ts +30 -2
  5. package/core/build/compiler/soma-precompile.js +2041 -19
  6. package/core/build/component/component-graph.ts +208 -0
  7. package/core/build/eject/component-eject.ts +473 -0
  8. package/core/build/eject/component-upgrade.ts +359 -0
  9. package/core/build/layout/constraint-solver.ts +235 -0
  10. package/core/build/layout/css-emitter.ts +116 -0
  11. package/core/build/layout/page-layout.ts +85 -0
  12. package/core/build/pack/pack-init.ts +265 -0
  13. package/core/client/base/SomaPageRootBase.ts +61 -0
  14. package/core/client/component/ai-catalog.ts +79 -4
  15. package/core/client/component/component-defaults.ts +100 -0
  16. package/core/client/component/component-definition.types.ts +39 -0
  17. package/core/client/component/component-manager.ts +79 -4
  18. package/core/client/component/component-pack.ts +126 -2
  19. package/core/client/component/keyboard-matrix.ts +144 -0
  20. package/core/client/component/pack-styles.ts +59 -1
  21. package/core/client/component/style-features.ts +131 -0
  22. package/core/client/component/theme-component-tokens.ts +83 -0
  23. package/core/client/data/data-object-resolver.ts +407 -4
  24. package/core/client/data/reaction-engine.ts +176 -0
  25. package/core/client/data/window-engine.ts +477 -0
  26. package/core/client/engine/event-engine.ts +98 -0
  27. package/core/client/engine/soma-bootstrap.ts +265 -5
  28. package/core/client/engine/step-runner.ts +134 -0
  29. package/core/client/parser/blocks/page-parser.ts +15 -1
  30. package/core/client/parser/blocks/use-parser.ts +5 -12
  31. package/core/client/parser/data-config-parser.ts +316 -8
  32. package/core/client/parser/duration.ts +16 -0
  33. package/core/client/parser/layout-parser.ts +157 -0
  34. package/core/client/parser/page-state-parser.ts +166 -0
  35. package/core/client/parser/pipeline-parser.ts +76 -1
  36. package/core/client/parser/reaction-parser.ts +459 -0
  37. package/core/client/parser/reaction-sugar.ts +47 -0
  38. package/core/client/parser/step-parser.ts +359 -0
  39. package/core/client/parser/ui-config-parser.ts +59 -0
  40. package/core/client/parser/window-parser.ts +294 -0
  41. package/core/client/theme/theme-engine.ts +102 -2
  42. package/core/index.ts +163 -0
  43. package/core/shared/block-errors.ts +11 -0
  44. package/core/shared/calc/coerce.ts +29 -0
  45. package/core/shared/calc/errors.ts +43 -0
  46. package/core/shared/calc/evaluator.ts +101 -0
  47. package/core/shared/calc/index.ts +12 -0
  48. package/core/shared/calc/lexer.ts +194 -0
  49. package/core/shared/calc/parser.ts +204 -0
  50. package/core/shared/calc/typer.ts +184 -0
  51. package/core/shared/path-algebra.ts +169 -0
  52. package/core/shared/predicates/index.ts +527 -0
  53. package/core/shared/types/component-contract.types.ts +128 -2
  54. package/core/shared/types/data-config.types.ts +120 -1
  55. package/core/shared/types/domain.types.ts +13 -0
  56. package/core/shared/types/event.types.ts +22 -2
  57. package/core/shared/types/layout-constraint.types.ts +73 -0
  58. package/core/shared/types/page-state.types.ts +60 -0
  59. package/core/shared/types/reaction.types.ts +144 -0
  60. package/core/shared/types/step.types.ts +91 -0
  61. package/core/shared/types/ui-config.types.ts +18 -0
  62. package/core/shared/types/window.types.ts +177 -0
  63. package/dist/cli/soma.js +13 -1
  64. package/dist/core/index.cjs +4684 -349
  65. package/dist/types/build/blocks/block-container.d.ts +1 -6
  66. package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
  67. package/dist/types/build/component/component-graph.d.ts +85 -0
  68. package/dist/types/build/eject/component-eject.d.ts +121 -0
  69. package/dist/types/build/eject/component-upgrade.d.ts +43 -0
  70. package/dist/types/build/layout/constraint-solver.d.ts +36 -0
  71. package/dist/types/build/layout/css-emitter.d.ts +18 -0
  72. package/dist/types/build/layout/page-layout.d.ts +24 -0
  73. package/dist/types/build/pack/pack-init.d.ts +8 -0
  74. package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
  75. package/dist/types/client/component/ai-catalog.d.ts +24 -1
  76. package/dist/types/client/component/component-defaults.d.ts +15 -0
  77. package/dist/types/client/component/component-definition.types.d.ts +38 -0
  78. package/dist/types/client/component/component-manager.d.ts +36 -0
  79. package/dist/types/client/component/component-pack.d.ts +32 -2
  80. package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
  81. package/dist/types/client/component/pack-styles.d.ts +21 -0
  82. package/dist/types/client/component/style-features.d.ts +26 -0
  83. package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
  84. package/dist/types/client/data/data-object-resolver.d.ts +106 -1
  85. package/dist/types/client/data/reaction-engine.d.ts +21 -0
  86. package/dist/types/client/data/window-engine.d.ts +27 -0
  87. package/dist/types/client/engine/event-engine.d.ts +15 -0
  88. package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
  89. package/dist/types/client/engine/step-runner.d.ts +24 -0
  90. package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
  91. package/dist/types/client/parser/duration.d.ts +1 -0
  92. package/dist/types/client/parser/layout-parser.d.ts +9 -0
  93. package/dist/types/client/parser/page-state-parser.d.ts +32 -0
  94. package/dist/types/client/parser/reaction-parser.d.ts +16 -0
  95. package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
  96. package/dist/types/client/parser/step-parser.d.ts +23 -0
  97. package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
  98. package/dist/types/client/parser/window-parser.d.ts +25 -0
  99. package/dist/types/client/theme/theme-engine.d.ts +26 -0
  100. package/dist/types/index.d.ts +32 -2
  101. package/dist/types/shared/block-errors.d.ts +5 -0
  102. package/dist/types/shared/calc/coerce.d.ts +4 -0
  103. package/dist/types/shared/calc/errors.d.ts +14 -0
  104. package/dist/types/shared/calc/evaluator.d.ts +2 -0
  105. package/dist/types/shared/calc/index.d.ts +6 -0
  106. package/dist/types/shared/calc/lexer.d.ts +20 -0
  107. package/dist/types/shared/calc/parser.d.ts +49 -0
  108. package/dist/types/shared/calc/typer.d.ts +16 -0
  109. package/dist/types/shared/path-algebra.d.ts +56 -0
  110. package/dist/types/shared/predicates/index.d.ts +79 -0
  111. package/dist/types/shared/types/component-contract.types.d.ts +112 -0
  112. package/dist/types/shared/types/data-config.types.d.ts +115 -1
  113. package/dist/types/shared/types/domain.types.d.ts +12 -0
  114. package/dist/types/shared/types/event.types.d.ts +8 -2
  115. package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
  116. package/dist/types/shared/types/page-state.types.d.ts +49 -0
  117. package/dist/types/shared/types/reaction.types.d.ts +115 -0
  118. package/dist/types/shared/types/step.types.d.ts +76 -0
  119. package/dist/types/shared/types/ui-config.types.d.ts +18 -0
  120. package/dist/types/shared/types/window.types.d.ts +171 -0
  121. package/package.json +2 -1
@@ -1,4 +1,8 @@
1
1
  // core/shared/types/data-config.types.ts — 数据对象层(*_data.yaml)类型定义
2
+
3
+ import type { SomaNamedPredicates } from '../predicates';
4
+ import type { SomaReactionGraph } from './reaction.types';
5
+ import type { SomaWindowConfig, SomaWindowHandle } from './window.types';
2
6
  // 对应《soma 数据对象层》第 2/3 节:数据对象声明 + 字段字典。
3
7
  // 数据对象是业务抽象出的命名数据(如 order),来源是一个或多个后端接口;
4
8
  // 组件与页面只认对象与字段,不认接口协议。
@@ -27,7 +31,18 @@ export type SomaDataFieldType =
27
31
  // 以下三型原本只在块声明侧存在,收编进唯一真源:
28
32
  // percent 与 progress 是同一份数据的两种读法(前者出文字、后者出进度条)
29
33
  | 'percent'
30
- | 'progress';
34
+ | 'progress'
35
+ // 以下四型随 REQ-009 F-15 进表:它们此前只能写成 text,于是「多值标签、附件、
36
+ // 树形归属、级联路径」四类字段在字典里全部塌成同一个单行输入框。
37
+ // 有了自己的类型,双态映射才能把它们分别接到 TagInput / Upload / TreeSelect / Cascader。
38
+ /** 多值标签(string[])→ TagInput */
39
+ | 'tags'
40
+ /** 附件(文件列表或文件地址)→ Upload */
41
+ | 'file'
42
+ /** 树形单选(options 为节点集)→ TreeSelect */
43
+ | 'tree'
44
+ /** 级联路径(string[],从根到叶)→ Cascader */
45
+ | 'cascade';
31
46
 
32
47
  /**
33
48
  * 类型别名:块声明历史上用 `number` 表示小数,与本表的 `float` 同义。
@@ -42,6 +57,7 @@ export const SOMA_DATA_FIELD_TYPE_ALIASES: Readonly<Record<string, SomaDataField
42
57
  export const SOMA_DATA_FIELD_TYPES: readonly string[] = [
43
58
  'text', 'int', 'float', 'money', 'bool', 'enum', 'date', 'datetime',
44
59
  'image', 'url', 'richtext', 'percent', 'progress',
60
+ 'tags', 'file', 'tree', 'cascade',
45
61
  ...Object.keys(SOMA_DATA_FIELD_TYPE_ALIASES),
46
62
  ];
47
63
 
@@ -89,6 +105,15 @@ export interface SomaDataFieldConfig {
89
105
  rules?: SomaDataFieldRule[];
90
106
  /** 枚举候选项 */
91
107
  options?: SomaDataFieldOption[];
108
+ /**
109
+ * 派生列(REQ-010 S2 / F-10):`"[revenue] - [cost]"`。
110
+ *
111
+ * 这是 AG Grid `valueGetter` 的封闭降级:那边是一个任意函数,这边是受限文法
112
+ * (方括号引用同对象字段、四则、比较、六个函数),解析期能推出结果类型并与声明的
113
+ * `type` 对账。声了 `calc` 的字段**权限恒为 r**——它是算出来的,写它没有意义,
114
+ * 而"写了没反应"是最难查的一类。
115
+ */
116
+ calc?: string;
92
117
  }
93
118
 
94
119
  /**
@@ -105,6 +130,46 @@ export interface SomaDataSourceConfig {
105
130
  into?: string;
106
131
  }
107
132
 
133
+ /** 写通道的一个端点:接口 + 方法(《数据对象层》§8.2) */
134
+ export interface SomaDataWriteEndpoint {
135
+ /** 后端接口路径(后端接口是唯一数据边界) */
136
+ api: string;
137
+ /** HTTP 方法;白名单 POST / PUT / PATCH */
138
+ method: 'POST' | 'PUT' | 'PATCH';
139
+ }
140
+
141
+ /**
142
+ * 写通道声明(REQ-009 M-5,《数据对象层》§8)。
143
+ *
144
+ * 读通道早已统一(from / transform / fields 走 SomaDataObjectResolver),
145
+ * 而写路径此前仍是组件直连:FormConsole 在 props 里自带 fields + submit_api + load_api,
146
+ * 于是字段与校验在 `_data.yaml` 和组件 props 各存一份,字典事实源分裂;
147
+ * 提交编排(收值 / 校验 / 调接口 / 错误呈现)每个自治组件各写一遍。
148
+ *
149
+ * 目标:**一份 `_data.yaml` 覆盖读 + 写,组件只说"我消费哪个对象"**。
150
+ */
151
+ export interface SomaDataWriteConfig {
152
+ /** 新建端点(create / update 至少给一个) */
153
+ create?: SomaDataWriteEndpoint;
154
+ /** 更新端点 */
155
+ update?: SomaDataWriteEndpoint;
156
+ /** 乐观锁字段名(必须在字典里;它是唯一允许随请求体上行的 r 字段) */
157
+ version?: string;
158
+ /** 表单键 → 请求体键的平面改名(键必须在字典且非 r 字段) */
159
+ map?: Record<string, string>;
160
+ /**
161
+ * 请求体字段白名单(REQ-010 F-38):只发这几个字段。
162
+ *
163
+ * 与 `permission` 的分工:`permission: r` 说的是「这个字段不可编辑」,
164
+ * 是**字段自身的性质**;`only` 说的是「这次写只送这几个」,是**这条通道的口径**。
165
+ * 用 permission 去表达后者会连带把控件降级成只读文本——那是另一件事。
166
+ * 块声明 `save.fields` 归一到这里。
167
+ */
168
+ only?: string[];
169
+ /** 提交成功后重取:'self' 只重取本对象 / 'all' 全部 / 指定对象名数组 */
170
+ reload?: 'self' | 'all' | string[];
171
+ }
172
+
108
173
  /** 数据对象声明(*_data.yaml 中 data.<对象名> 的解析产物) */
109
174
  export interface SomaDataObjectConfig {
110
175
  /** 对象名(页面内唯一,即 data 下的键名) */
@@ -120,10 +185,64 @@ export interface SomaDataObjectConfig {
120
185
  fields?: Record<string, SomaDataFieldConfig>;
121
186
  /** mock 样本:SOMA_MOCK_ENABLED / 接口失败兜底 / 无接口预览 */
122
187
  mock?: unknown;
188
+ /** 写通道声明(§8);不给 = 该对象只读 */
189
+ write?: SomaDataWriteConfig;
190
+ /**
191
+ * 具名谓词表(REQ-010 S1b):`{ 名字: 谓词 }`。
192
+ * 一条「是否企业客户」声明一次,反应与字段显隐处处 `{ ref: 名字 }` 复用——
193
+ * 复制粘贴出来的那几份,将来必然漏改一份。
194
+ */
195
+ predicates?: SomaNamedPredicates;
196
+ /**
197
+ * 反应图(REQ-010 S1b):字段之间的联动。
198
+ * 解析期已建成 DAG、检过环、算好回溯表——这是 Formily / amis 靠 eval + 运行时
199
+ * 依赖收集换不来的东西。
200
+ */
201
+ reactions?: SomaReactionGraph;
202
+ /**
203
+ * 数据窗口(REQ-010 S2):一屏一屏地取。
204
+ * 声明期已把过滤 AST、排序/分组/聚合的字段引用对着字典校验过,并算好字典哈希。
205
+ */
206
+ window?: SomaWindowConfig;
123
207
  /** 解析期提取:params 引用到的其他数据对象名(DAG 拓扑排序输入,不含自引用) */
124
208
  deps: string[];
125
209
  }
126
210
 
211
+ /** 写通道提交结果:成功带数据,失败带整体文案与字段级错误(§8.3) */
212
+ export type SomaDataSubmitResult =
213
+ | { ok: true; data?: unknown }
214
+ | { ok: false; message: string; fieldErrors?: Record<string, string> };
215
+
216
+ /**
217
+ * 注入给自治组件的数据端口(services 白名单里的 `data`,§8.4)。
218
+ * 复杂度沉引擎:组件只做「收值 → 调 submit → 渲染返回的 fieldErrors」。
219
+ */
220
+ export interface SomaDataPort {
221
+ /** 读态(回填来源) */
222
+ getObject(name: string): unknown;
223
+ /** 字段字典(控件 / 校验来源) */
224
+ getDict(name: string): Record<string, SomaDataFieldConfig>;
225
+ /**
226
+ * 反应图与具名谓词表(REQ-010 S1b):`_data.yaml` 的 `reactions:` / `predicates:` 段。
227
+ * 组件拿它跑 `runReactions`——联动声明在数据对象上,与字段字典同源同一份,
228
+ * 不必在页面 DSL 里再抄一遍。没声明的对象返回 null(= 无联动)。
229
+ */
230
+ getReactions(name: string): { graph: SomaReactionGraph; named: SomaNamedPredicates } | null;
231
+ /**
232
+ * 数据窗口(REQ-010 S2):声了 `window:` 段的对象才有,没有返回 null。
233
+ * 组件拿到它就不再自持 `page / sort_key / filters`——那些裁决收在引擎里只有一份。
234
+ */
235
+ window(name: string): SomaWindowHandle | null;
236
+ /** 写通道:校验 → 构体 → 调接口 → 错误逆映射 → reload */
237
+ submit(
238
+ name: string,
239
+ model: Record<string, unknown>,
240
+ mode?: 'create' | 'update',
241
+ ): Promise<SomaDataSubmitResult>;
242
+ /** 手动重取(等价 Domain 的 _reloadData 细粒度版) */
243
+ reload(names?: string[]): Promise<void>;
244
+ }
245
+
127
246
  /** *_data.yaml 整体解析产物(对象名 → 声明) */
128
247
  export interface SomaDataConfig {
129
248
  objects: Record<string, SomaDataObjectConfig>;
@@ -5,6 +5,8 @@
5
5
  * 页面聚合根接口
6
6
  * 展示对象必须实现此接口,提供页面生命周期钩子
7
7
  */
8
+ import type { SomaPageState, SomaPageStateConfig } from './page-state.types';
9
+
8
10
  export interface SomaPageRoot {
9
11
  /** Content strings from locale.yaml (injected by bootstrap) */
10
12
  _locale?: Record<string, unknown>;
@@ -18,6 +20,17 @@ export interface SomaPageRoot {
18
20
  _reloadData?: () => Promise<void>;
19
21
  /** 重新执行数据对象取数(无声明时为空操作) */
20
22
  reloadData?(): Promise<void>;
23
+ /**
24
+ * 页面态(REQ-010 F-37):排序 / 过滤 / 选择 / 列显隐 / 密度 / 展开。
25
+ * 没声明 `state:` 段时恒为 null——不是空对象:空对象会让调用方以为
26
+ * "有状态,只是都是缺省",于是把它序列化进分享链接,而链接打开后什么都恢复不了。
27
+ */
28
+ _pageState?: SomaPageState | null;
29
+ /** `state:` 段的声明(bootstrap 注入) */
30
+ _pageStateConfig?: SomaPageStateConfig | null;
31
+ getState?(): SomaPageState | null;
32
+ setState?(next: SomaPageState): void;
33
+ onStateUpdated?(listener: (state: SomaPageState) => void): () => void;
21
34
  /** 页面初始化(引擎在渲染完成后调用) */
22
35
  onMount?(): void | Promise<void>;
23
36
  /** 页面销毁前(引擎在卸载时调用) */
@@ -5,23 +5,43 @@
5
5
  * 事件类型枚举
6
6
  * 定义 Pipeline DSL 支持的四种事件处理类型
7
7
  */
8
+ import type { SomaStepGraph } from './step.types';
9
+
8
10
  export type EventType =
9
11
  | 'NAV_STATIC' // 静态页面跳转
10
12
  | 'NAV_ACTION' // 逻辑驱动跳转(先执行函数,成功后跳转)
11
13
  | 'UI_REFRESH' // 数据刷新(执行函数后触发 UI 重新渲染)
12
- | 'UI_INTERACT'; // UI 交互操作(显隐控制)
14
+ | 'UI_INTERACT' // UI 交互操作(显隐、聚焦、滚动、选中)
15
+ // 具名动作(REQ-010 F-20):`ACTION | <名字>`,实现在 bootstrap 的 options.actions 里。
16
+ // 它是 amis `custom(script)` 的替代——同样能接任意 Host 逻辑,但**有名字、要注册、
17
+ // 未注册即抛**,声明层因此仍然零脚本。
18
+ | 'ACTION'
19
+ // 编排事件(REQ-010 F-13):值不是一条管道字符串,而是 `{ steps: [...] }`。
20
+ // 它不能写成管道串——一串步骤没法压进一行竖线分隔的文本,硬压只会得到
21
+ // 一门新的微型语法。所以它只从对象形态来,`parsePipelineBody` 不认它。
22
+ | 'STEPS';
13
23
 
14
24
  /**
15
25
  * UI 交互动作枚举
16
26
  * 用于 UI_INTERACT 类型的动作指令
17
27
  */
18
- export type InteractAction = 'open' | 'close' | 'toggle';
28
+ export type InteractAction =
29
+ | 'open' | 'close' | 'toggle'
30
+ // REQ-010 §4.4 扩容:编排里最常见的三件"看得见的副作用"。
31
+ // 没有它们,「提交完把焦点放回列表」这类事只能回到 Host 代码里写 DOM 操作。
32
+ | 'focus' | 'scroll_to' | 'select';
19
33
 
20
34
  /**
21
35
  * 解析后的事件配置
22
36
  * Pipeline DSL 解析器将 YAML 中的事件声明解析为此结构
23
37
  */
24
38
  export interface ParsedEvent {
39
+ /**
40
+ * 动作编排图(`type === 'STEPS'` 时有)。
41
+ * 解析在 `step-parser`,执行在 `step-runner`;不带它的事件走原来的单管道路径。
42
+ */
43
+ steps?: SomaStepGraph;
44
+
25
45
  /** 目标元素的 data-soma-id 值 */
26
46
  somaId: string;
27
47
 
@@ -0,0 +1,73 @@
1
+ // 约束布局类型(REQ-010 S3)——「这一行里谁让谁」的声明式表达。
2
+ //
3
+ // ── 为什么需要它 ──────────────────────────────────────────────────────
4
+ // 今天写一个「侧栏固定 20rem、正文占满、窄了侧栏先缩到 16rem、再窄就换行」的布局,
5
+ // 要手写四段容器查询并在每一档里各写一遍 flex 值。四段之间的一致性靠人维护,
6
+ // 而它们坏起来是"某一档下侧栏比 min 还窄"——只有把窗口拖到那一档才看得见。
7
+ //
8
+ // Figma 的 Auto Layout 用约束求解表达同一件事(hug / fill / 优先级),
9
+ // 但它在**运行时**求解,因为画布可交互。页面不需要:容器宽度是八档封闭集,
10
+ // 每档一个解就是全部解。所以 SOMA 在**构建期**求解,运行期只剩 CSS。
11
+ // 这是本段的结构性差异——运行期零求解 = 零 JS 成本 + 零布局抖动 + 产物可审计。
12
+
13
+ /** 尺寸意图三档(Auto Layout 的对位物) */
14
+ export type SomaSizing = 'fixed' | 'hug' | 'fill';
15
+
16
+ /**
17
+ * 优先级四档(Auto Layout 惯例):
18
+ * 250 = Content Hugging("我愿意被拉大")、500 = 中庸、
19
+ * 750 = Compression Resistance("我不愿意被压小")、1000 = required(不可违反)。
20
+ *
21
+ * 是四档不是任意整数:优先级只在**相对次序**上有意义,开放成整数只会让人写出
22
+ * 748 与 752 这种自己也说不清差别的值,而它们的差别在求解结果上确实存在。
23
+ */
24
+ export const SOMA_LAYOUT_PRIORITIES = [250, 500, 750, 1000] as const;
25
+ export type SomaPriority = typeof SOMA_LAYOUT_PRIORITIES[number];
26
+
27
+ /** 换行策略:不换 / 窄了就竖排 / 横向滚动成一条卷轴 */
28
+ export type SomaWrap = 'none' | 'below' | 'reel';
29
+
30
+ export const SOMA_LAYOUT_ALIGNS = [
31
+ 'start', 'center', 'end', 'baseline', 'space_between', 'space_evenly', 'space_around',
32
+ ] as const;
33
+ export type SomaAlign = typeof SOMA_LAYOUT_ALIGNS[number];
34
+
35
+ /** 一个子项的约束 */
36
+ export interface SomaLayoutNodeConstraint {
37
+ sizing: SomaSizing;
38
+ /** `sizing: fixed` 的目标宽;`rem` / `%` / `cqi` 三种单位 */
39
+ width?: string;
40
+ min?: string;
41
+ max?: string;
42
+ priority?: SomaPriority;
43
+ }
44
+
45
+ /** 一组(一行)的约束 */
46
+ export interface SomaLayoutConstraint {
47
+ /** 子项名 → 约束;声明序即视觉序 */
48
+ nodes: Record<string, SomaLayoutNodeConstraint>;
49
+ /** 子项之间的间隔(rem) */
50
+ gap?: string;
51
+ wrap?: SomaWrap;
52
+ align?: SomaAlign;
53
+ /** 具名容器上下文:编译成 `container-name` */
54
+ container_name?: string;
55
+ }
56
+
57
+ /** 单档的解 */
58
+ export interface SomaLayoutSolution {
59
+ /** 档名(`cqXs` … `cq4xl`) */
60
+ breakpoint: string;
61
+ /** 该档的容器宽度(rem) */
62
+ containerRem: number;
63
+ /** 子项名 → 解出的宽度(rem) */
64
+ widths: Record<string, number>;
65
+ /** 该档是否必须换行(解不出「两者都 ≥ min」时为真) */
66
+ wrapped: boolean;
67
+ }
68
+
69
+ /** 求解产物 */
70
+ export interface SomaLayoutPlan {
71
+ constraint: SomaLayoutConstraint;
72
+ solutions: SomaLayoutSolution[];
73
+ }
@@ -0,0 +1,60 @@
1
+ // 页面态切片(REQ-010 F-37)——「这一屏此刻长什么样」的可序列化表达。
2
+ //
3
+ // ── 要解决的问题 ──────────────────────────────────────────────────────
4
+ // 排序、过滤、选了哪些行、哪几列可见、列宽列序、密度、展开了哪些分组——
5
+ // 这些状态今天散在各个组件的私有变量里。后果是三件事都做不了:
6
+ // ① 分享链接(把当前视图发给同事);
7
+ // ② 用户偏好(下次打开还是这个样子);
8
+ // ③ 审计回放(他当时到底看到的是哪一屏)。
9
+ //
10
+ // AG Grid 的 `GridState` 解决了它,但那是一个网格的状态。SOMA 的落点更靠上:
11
+ // 页面态是**具名切片的并集**,每个切片对应一个已声明的东西(一个数据窗口、
12
+ // 一个表格锚点),所以它能被静态校验——切片名指向一个不存在的窗口时当场报错,
13
+ // 而不是在恢复状态时静默丢掉一半。
14
+
15
+ /** 一个数据窗口的可序列化状态 */
16
+ export interface SomaWindowStateSlice {
17
+ sort?: Array<{ field: string; order: 'asc' | 'desc' }>;
18
+ filter?: unknown;
19
+ selection?: { base: 'none' | 'all'; toggled: string[] };
20
+ /** 分组展开路径 */
21
+ group_keys?: string[];
22
+ }
23
+
24
+ /** 一个表格锚点的列状态 */
25
+ export interface SomaColumnStateSlice {
26
+ visible?: string[];
27
+ order?: string[];
28
+ pinned?: Record<string, 'start' | 'end'>;
29
+ width?: Record<string, number>;
30
+ }
31
+
32
+ /**
33
+ * 页面态。
34
+ *
35
+ * `version` 是必需的:恢复一份旧结构的状态会让界面进入一个谁都没设计过的样子,
36
+ * 而那种坏法是静默的(列还在、但顺序是乱的)。版本对不上就整份丢弃并说明原因。
37
+ */
38
+ export interface SomaPageState {
39
+ version: number;
40
+ window?: Record<string, SomaWindowStateSlice>;
41
+ columns?: Record<string, SomaColumnStateSlice>;
42
+ density?: 'sm' | 'md' | 'lg';
43
+ /** 展开的行/分组 id */
44
+ expanded?: string[];
45
+ }
46
+
47
+ /** `_ui.yaml` 顶层 `state:` 段的声明形态 */
48
+ export interface SomaPageStateConfig {
49
+ version: number;
50
+ /** 允许出现在页面态里的窗口名(必须是已声明的数据对象) */
51
+ window?: string[];
52
+ /** 允许出现在页面态里的表格锚点 */
53
+ columns?: string[];
54
+ density?: boolean;
55
+ expanded?: boolean;
56
+ /** 初始状态:只读一次,之后由用户交互接管 */
57
+ initial_state?: SomaPageState;
58
+ /** 是否把状态同步进 URL(`?s=<紧凑序列化>`) */
59
+ url?: boolean;
60
+ }
@@ -0,0 +1,144 @@
1
+ // 反应图类型(REQ-010 S1b)——「字段之间怎么互相影响」的声明式表达。
2
+ //
3
+ // 要解决的问题:表单里第三类逻辑——不是「值合不合法」(rules),不是「该不该出现」
4
+ // (谓词图已覆盖),而是**「A 变了,B 要跟着变成什么」**:选了公司就必须填税号、
5
+ // 金额减折扣自动算出实付、校验失败就锁住提交。
6
+ //
7
+ // ── 为什么必须是「图」而不是一串回调 ──────────────────────────────────
8
+ // 上游三家(Formily 的 x-reactions、amis 的 onEvent、antd 的 dependencies)都能表达联动,
9
+ // 但它们的依赖是**运行时收集**的:Formily 的编译器是 `new Function('$root','with($root){…}')`
10
+ // 加 MobX 式的 Tracker,amis 是 `${}` 沿运行时数据链向上找。后果是编译期什么都不知道——
11
+ // 建不了图、检不了环、说不清一个字段的值最终来自哪个接口,CSP 也去不掉 unsafe-eval。
12
+ //
13
+ // SOMA 的字段是字典里的封闭枚举、算子是闭集、calc 是受限文法,所以**依赖可以在声明期
14
+ // 静态提取**:`when` 的字段(collectPredicateFields)∪ `depends` ∪ `calc` 的引用
15
+ // (collectCalcRefs)就是这条反应的全部入边。有了入边就能建 DAG、能拓扑排序、能检环、
16
+ // 能回答「改这个字段会牵动谁」和「这个字段的值是怎么来的」。
17
+ // 这是本需求里对方结构上做不到的三件事之一。
18
+
19
+ import type { SomaFieldPredicate } from '../predicates';
20
+
21
+ /**
22
+ * 触发时机——闭集十一档(对齐 Formily 的 effect hooks,但作为 DSL 枚举而非自由字符串)。
23
+ * 时机进类型系统之后,「什么时候触发」不再是拼写正确才生效的字符串。
24
+ */
25
+ export const SOMA_REACTION_PHASES = [
26
+ 'field_init', 'field_mount', 'field_unmount',
27
+ 'field_value_change', 'field_input_change',
28
+ 'field_validate_start', 'field_validate_end',
29
+ 'field_validate_failed', 'field_validate_success',
30
+ 'form_submit_start', 'form_submit_end',
31
+ ] as const;
32
+
33
+ export type SomaReactionPhase = typeof SOMA_REACTION_PHASES[number];
34
+
35
+ /**
36
+ * 显示三态(Formily `display` 的对位物)。
37
+ *
38
+ * `hidden` 与 `none` 的差别在**提交载荷**上:前者字段还在 values 里(只是不给看),
39
+ * 后者连数据一起摘除。用一个布尔 `visible` 表达不了这件事,而后端收到一份
40
+ * 「隐藏了但仍然提交」的脏数据时,前端这边一点迹象都没有。
41
+ */
42
+ export const SOMA_FIELD_DISPLAYS = ['visible', 'hidden', 'none'] as const;
43
+ export type SomaFieldDisplay = typeof SOMA_FIELD_DISPLAYS[number];
44
+
45
+ /** 可编辑四态(Formily `pattern` 的对位物),与显示三态**正交** */
46
+ export const SOMA_FIELD_PATTERNS = ['editable', 'disabled', 'read_only', 'read_pretty'] as const;
47
+ export type SomaFieldPattern = typeof SOMA_FIELD_PATTERNS[number];
48
+
49
+ /** 值 patch:清空 / 复制另一个字段 / 走 calc 算 —— 三种,没有第四种(没有 `run`) */
50
+ export type SomaFieldValuePatch = 'clear' | { from: string } | { calc: string };
51
+
52
+ /**
53
+ * 封闭 patch 集。
54
+ *
55
+ * 这里是 Formily `fulfill.state` 的对位物,**刻意删掉了 `run`**——那是它的逃生舱,
56
+ * 也是它必须带 eval 的原因。八个键各自一句话说得清,且每个都能在解析期与字段类型配对。
57
+ */
58
+ export interface SomaFieldStatePatch {
59
+ /** 语法糖:true → visible,false → hidden(要摘数据请显式写 display: none) */
60
+ visible?: boolean;
61
+ display?: SomaFieldDisplay;
62
+ pattern?: SomaFieldPattern;
63
+ required?: boolean;
64
+ value?: SomaFieldValuePatch;
65
+ /** 枚举源切换:'<对象>.<字段>' */
66
+ options?: { from: string };
67
+ /** 覆盖该字段的提示文案 */
68
+ message?: string;
69
+ }
70
+
71
+ /** 表单级 patch(不挂在任何字段上) */
72
+ export interface SomaFormStatePatch {
73
+ submit?: { disabled?: boolean };
74
+ }
75
+
76
+ export interface SomaReactionConfig {
77
+ /** 条件;与 `depends` 至少给一个(否则这条反应永远不知道何时该跑) */
78
+ when?: SomaFieldPredicate;
79
+ /** 显式依赖:`when` 之外还要盯着的字段(`calc` 的引用会自动并入,不必重复写) */
80
+ depends?: string[];
81
+ /** 缺省 `field_value_change` */
82
+ on?: SomaReactionPhase;
83
+ /** 仅 `field_validate_*` 时有意义:盯着哪个字段的校验结果 */
84
+ target?: string;
85
+ fulfill?: Record<string, SomaFieldStatePatch | SomaFormStatePatch>;
86
+ otherwise?: Record<string, SomaFieldStatePatch | SomaFormStatePatch>;
87
+ }
88
+
89
+ /** 表单级 patch 在 fulfill/otherwise 里用的保留键 */
90
+ export const SOMA_REACTION_FORM_KEY = 'submit';
91
+
92
+ /** 编译后的单条反应 */
93
+ export interface SomaCompiledReaction {
94
+ /** 拓扑序下标 */
95
+ index: number;
96
+ /** 声明序(错误信息用:作者看的是自己写的顺序,不是拓扑序) */
97
+ declaredAt: number;
98
+ /** 入边:`when` 字段 ∪ `depends` ∪ `calc` 引用(去重) */
99
+ reads: readonly string[];
100
+ /** 出边:`fulfill` / `otherwise` 的字段键(不含 submit) */
101
+ writes: readonly string[];
102
+ phase: SomaReactionPhase;
103
+ config: SomaReactionConfig;
104
+ /** 预解析的 calc AST(避免每次求值重新解析):`<字段>` → AST */
105
+ calcAsts: Readonly<Record<string, unknown>>;
106
+ }
107
+
108
+ /**
109
+ * 字段回溯:这个字段的状态是谁写的、依据什么、数据从哪来。
110
+ *
111
+ * amis 的 `${x}` 只能沿运行时链向上找,给不出这张表;这是「可静态回溯」的落点。
112
+ */
113
+ export interface SomaFieldProvenance {
114
+ /** 写它的反应(声明序下标) */
115
+ writtenBy: readonly number[];
116
+ /** 传递闭包内的全部上游字段 */
117
+ readsFrom: readonly string[];
118
+ /** 该字段来自读通道的哪个接口(`from[].api`);派生字段为 undefined */
119
+ sourceApi?: string;
120
+ }
121
+
122
+ /** 编译产物 */
123
+ export interface SomaReactionGraph {
124
+ /** 已按拓扑序排列 */
125
+ reactions: readonly SomaCompiledReaction[];
126
+ /** 字段 → 读它的反应在 `reactions` 里的下标(受影响子图的入口) */
127
+ byField: Readonly<Record<string, readonly number[]>>;
128
+ /** 字段 → 回溯信息 */
129
+ provenance: Readonly<Record<string, SomaFieldProvenance>>;
130
+ }
131
+
132
+ /** 一次求值的产物 */
133
+ export interface SomaReactionResult {
134
+ /** 字段 → 合并后的 patch(后写的赢,与声明序一致) */
135
+ patches: Readonly<Record<string, SomaFieldStatePatch>>;
136
+ /** 表单级 patch */
137
+ form: SomaFormStatePatch;
138
+ /** 应用 value patch 之后的值(新对象,不改入参) */
139
+ values: Record<string, unknown>;
140
+ /** 被 `display: none` 摘除的字段(写通道据此剔除请求体) */
141
+ detached: readonly string[];
142
+ /** 传播步数——图无环时必然有界,作为 RG-5 的断言面 */
143
+ steps: number;
144
+ }
@@ -0,0 +1,91 @@
1
+ // 动作编排类型(REQ-010 S4)——「一个交互要按顺序做好几件事」的声明式表达。
2
+ //
3
+ // ── 它替掉的是什么 ────────────────────────────────────────────────────
4
+ // 今天一个事件只能挂一条管道。要「先导出、成功了跳转、失败了弹错、顺带刷统计」,
5
+ // 只能在 Domain 里写一个 handler 把四件事串起来——于是编排逻辑回到了代码里,
6
+ // 声明层看不见它,AI 也生成不了它。
7
+ //
8
+ // amis 的 `onEvent` 能表达编排,代价是它带 `custom(script)` 逃生舱;
9
+ // XState 能表达状态机,但它的 actions 是函数。两边的共同点:**编排图不是数据**,
10
+ // 所以画不出来、diff 不了、也没法在编译期回答「这条链会不会不终止」。
11
+ //
12
+ // SOMA 的每一步都是既有的四动词管道 + 闭集控制流,所以编排图是**纯数据**:
13
+ // 可画、可 diff、可静态判定可终止性。逃生舱换成**具名动作注册表**——
14
+ // 实现仍在 Host 代码里,但它有名字、要注册、可审计,声明层零脚本。
15
+
16
+ import type { SomaFieldPredicate } from '../predicates';
17
+ import type { ParsedEvent } from './event.types';
18
+
19
+ /** 出错之后怎么走:停 / 继续 / 跳到第 n 步(只许向后,见 AO-1) */
20
+ export type SomaStepOnError = 'stop' | 'continue' | `goto:${number}`;
21
+
22
+ /** 单步:一条管道 + 可选的条件、输出槽与错误去向 */
23
+ export interface SomaDoStep {
24
+ when?: SomaFieldPredicate;
25
+ do: string;
26
+ output?: string;
27
+ on_error?: SomaStepOnError;
28
+ }
29
+
30
+ /** 分支:取第一个成立的 `when`;`otherwise` 可选且只能有一个、且必须在末尾 */
31
+ export interface SomaSwitchStep {
32
+ when?: SomaFieldPredicate;
33
+ switch: Array<{ when?: SomaFieldPredicate; otherwise?: true; do: string; output?: string }>;
34
+ }
35
+
36
+ /** 并行:分支之间**不许互引输出槽**(互引就意味着有顺序,那它就不是并行) */
37
+ export interface SomaParallelStep {
38
+ parallel: SomaStep[];
39
+ }
40
+
41
+ /** 遍历:`over` 必须是有界的(数组型输出槽或事件载荷里的数组) */
42
+ export interface SomaForEachStep {
43
+ for_each: { over: string; as: string; steps: SomaStep[] };
44
+ }
45
+
46
+ export type SomaStep = SomaDoStep | SomaSwitchStep | SomaParallelStep | SomaForEachStep;
47
+
48
+ /** 输出槽:本次事件内的具名中间结果 */
49
+ export interface SomaStepSlot {
50
+ /** 声明它的步骤序号(引用只许向后,见 AO-2) */
51
+ declaredAt: number;
52
+ /** 产出它的动作类型,用于「这个槽是不是数组」这类判断 */
53
+ producedBy: string;
54
+ /** 是否已知为数组(`for_each.over` 只认这种) */
55
+ bounded: boolean;
56
+ }
57
+
58
+ /** 编译后的一步:管道已解析,条件已校验 */
59
+ export interface SomaCompiledStep {
60
+ index: number;
61
+ kind: 'do' | 'switch' | 'parallel' | 'for_each';
62
+ when?: SomaFieldPredicate;
63
+ /** kind='do' */
64
+ pipeline?: Omit<ParsedEvent, 'somaId' | 'domEvent'>;
65
+ output?: string;
66
+ onError: SomaStepOnError;
67
+ /** kind='switch' */
68
+ branches?: Array<{
69
+ when?: SomaFieldPredicate;
70
+ otherwise: boolean;
71
+ pipeline: Omit<ParsedEvent, 'somaId' | 'domEvent'>;
72
+ output?: string;
73
+ }>;
74
+ /** kind='parallel' / 'for_each' */
75
+ children?: SomaCompiledStep[];
76
+ /** kind='for_each' */
77
+ over?: string;
78
+ as?: string;
79
+ }
80
+
81
+ /**
82
+ * 编译产物:纯数据,可画、可 diff、可进目录快照(AO-4)。
83
+ *
84
+ * `edges` 是顺序边与 `goto` 边的并集——它存在的唯一理由是**能被画出来**:
85
+ * 一张编排图如果只能靠读 YAML 想象,那它和写在代码里的 handler 没有区别。
86
+ */
87
+ export interface SomaStepGraph {
88
+ steps: SomaCompiledStep[];
89
+ slots: Record<string, SomaStepSlot>;
90
+ edges: Array<[number, number]>;
91
+ }
@@ -3,6 +3,8 @@
3
3
 
4
4
  import type { OnChangeRule } from './watcher.types';
5
5
  import type { SomaAdaptiveConfig } from './adaptive.types';
6
+ import type { SomaPageStateConfig } from './page-state.types';
7
+ import type { SomaLayoutConstraint } from './layout-constraint.types';
6
8
 
7
9
  /**
8
10
  * 作用域声明
@@ -102,6 +104,22 @@ export interface UiConfig {
102
104
  * 所以此键必须保持可选,且缺席时不得在下游产生任何默认值。
103
105
  */
104
106
  adaptive?: SomaAdaptiveConfig;
107
+ /**
108
+ * 页面态切片声明(REQ-010 F-37,保留键,可选)。
109
+ * 与 `adaptive` 同一条纪律:未声明时编译产物与迁移前字节级一致。
110
+ */
111
+ state?: SomaPageStateConfig;
112
+ /**
113
+ * 约束布局声明(REQ-010 S3 / REQ-011 接线,保留键,可选):锚点 id → 一行的约束。
114
+ *
115
+ * 这是**声明**;它的产物是 `layoutCss`。求解只在构建期(`compilePageDeclaration`)发生,
116
+ * 运行时收到的是已经解好的 CSS(`layout_css`),浏览器一行 JS 都不用算。
117
+ * 运行时若收到的是 `layout` 而不是 `layout_css`,说明页面没走构建期编译——
118
+ * 引擎只告警、不求解(CL-4:求解器不进 client)。
119
+ */
120
+ layout?: Record<string, SomaLayoutConstraint>;
121
+ /** 构建期求解产物:只含容器查询 + flex 的 CSS(CL-2),运行时按页注入一次 */
122
+ layoutCss?: string;
105
123
  }
106
124
 
107
125
  /**