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.
- package/README.md +1 -1
- package/core/build/blocks/block-compile.ts +31 -2
- package/core/build/blocks/block-container.ts +2 -11
- package/core/build/compiler/page-declaration-compiler.ts +30 -2
- package/core/build/compiler/soma-precompile.js +2041 -19
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +265 -0
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +79 -4
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +265 -5
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/parser/blocks/page-parser.ts +15 -1
- package/core/client/parser/blocks/use-parser.ts +5 -12
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/theme/theme-engine.ts +102 -2
- package/core/index.ts +163 -0
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/types/component-contract.types.ts +128 -2
- package/core/shared/types/data-config.types.ts +120 -1
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +13 -1
- package/dist/core/index.cjs +4684 -349
- package/dist/types/build/blocks/block-container.d.ts +1 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +24 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/theme/theme-engine.d.ts +26 -0
- package/dist/types/index.d.ts +32 -2
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/types/component-contract.types.d.ts +112 -0
- package/dist/types/shared/types/data-config.types.d.ts +115 -1
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +2 -1
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// 反应图解析(REQ-010 S1b)——把声明编译成一张**在解析期就成立**的依赖图。
|
|
2
|
+
//
|
|
3
|
+
// 六步,每一步失败即抛(`SomaDataConfigError`,与 transform / write 同一纪律):
|
|
4
|
+
// 1. 谓词图校验(复用 predicates,传字段类型表 → 算子×类型配对)
|
|
5
|
+
// 2. 依赖提取:`when` 字段 ∪ `depends` ∪ `calc` 引用
|
|
6
|
+
// 3. 目标提取:`fulfill`/`otherwise` 的键,patch 与字段类型/权限配对
|
|
7
|
+
// 4. 建图 + Kahn 拓扑 + 检环(残留即环,打印完整环路)
|
|
8
|
+
// 5. 时机校验(十一档闭集)
|
|
9
|
+
// 6. 产出拓扑序 + 反向索引 + 回溯表
|
|
10
|
+
//
|
|
11
|
+
// 为什么这些必须在解析期做:它们全都是**静默失败**的错法。
|
|
12
|
+
// 目标字段拼错一个字母,那条反应永远不生效,页面上什么都不会发生;
|
|
13
|
+
// 反应成环,两个字段会在一次输入里来回改,用户看到的是「输入框自己跳」;
|
|
14
|
+
// patch 与类型不配(对 enum 字段写 calc),求值结果是 NaN 塞进一个下拉框。
|
|
15
|
+
// 这三类在运行期都不报错,只是行为不对——所以它们必须在页面加载之前就红。
|
|
16
|
+
|
|
17
|
+
import type {
|
|
18
|
+
SomaCompiledReaction,
|
|
19
|
+
SomaFieldProvenance,
|
|
20
|
+
SomaFieldStatePatch,
|
|
21
|
+
SomaFormStatePatch,
|
|
22
|
+
SomaReactionConfig,
|
|
23
|
+
SomaReactionGraph,
|
|
24
|
+
SomaReactionPhase,
|
|
25
|
+
} from '../../shared/types/reaction.types';
|
|
26
|
+
import {
|
|
27
|
+
SOMA_FIELD_DISPLAYS,
|
|
28
|
+
SOMA_FIELD_PATTERNS,
|
|
29
|
+
SOMA_REACTION_FORM_KEY,
|
|
30
|
+
SOMA_REACTION_PHASES,
|
|
31
|
+
} from '../../shared/types/reaction.types';
|
|
32
|
+
import type { SomaDataFieldConfig } from '../../shared/types/data-config.types';
|
|
33
|
+
import type { SomaFieldPredicate, SomaNamedPredicates } from '../../shared/predicates';
|
|
34
|
+
import { collectPredicateFields, validatePredicate } from '../../shared/predicates';
|
|
35
|
+
import {
|
|
36
|
+
assertCalcResultType,
|
|
37
|
+
collectCalcRefs,
|
|
38
|
+
inferCalcType,
|
|
39
|
+
parseCalc,
|
|
40
|
+
SomaCalcSyntaxError,
|
|
41
|
+
SomaCalcTypeError,
|
|
42
|
+
type CalcAst,
|
|
43
|
+
} from '../../shared/calc';
|
|
44
|
+
|
|
45
|
+
/** 与 data-config-parser 同一个错误类;由调用方注入以免循环依赖 */
|
|
46
|
+
export interface ReactionParseDeps {
|
|
47
|
+
fail(message: string, path: string): never;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ReactionParseInput {
|
|
51
|
+
raw: unknown;
|
|
52
|
+
fields: Record<string, SomaDataFieldConfig>;
|
|
53
|
+
named: SomaNamedPredicates;
|
|
54
|
+
/** 字段 → 来源接口(读通道 `from[].api`),用于回溯表 */
|
|
55
|
+
sourceApiOf?: Readonly<Record<string, string | undefined>>;
|
|
56
|
+
path: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PHASES = new Set<string>(SOMA_REACTION_PHASES);
|
|
60
|
+
const DISPLAYS = new Set<string>(SOMA_FIELD_DISPLAYS);
|
|
61
|
+
const PATTERNS = new Set<string>(SOMA_FIELD_PATTERNS);
|
|
62
|
+
const PATCH_KEYS = new Set(['visible', 'display', 'pattern', 'required', 'value', 'options', 'message']);
|
|
63
|
+
const CONFIG_KEYS = new Set(['when', 'depends', 'on', 'target', 'fulfill', 'otherwise']);
|
|
64
|
+
|
|
65
|
+
/** 只有这些字段类型能被 `value: { calc }` 写 —— 与 calc 的结果类型对账表同源 */
|
|
66
|
+
const CALC_WRITABLE = new Set(['int', 'float', 'money', 'percent', 'progress', 'text', 'url', 'enum', 'date', 'datetime']);
|
|
67
|
+
|
|
68
|
+
export function parseReactions(input: ReactionParseInput, deps: ReactionParseDeps): SomaReactionGraph {
|
|
69
|
+
const { raw, fields, named, path } = input;
|
|
70
|
+
const { fail } = deps;
|
|
71
|
+
|
|
72
|
+
if (!Array.isArray(raw)) fail('reactions 必须是数组', path);
|
|
73
|
+
const list = raw as unknown[];
|
|
74
|
+
|
|
75
|
+
const fieldKeys = Object.keys(fields);
|
|
76
|
+
const fieldTypes: Record<string, string | undefined> = Object.fromEntries(
|
|
77
|
+
fieldKeys.map((key) => [key, fields[key].type]),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// ── 步 1~3、5:逐条校验并提取入边/出边 ──────────────────────────────
|
|
81
|
+
const parsed: Array<{
|
|
82
|
+
config: SomaReactionConfig;
|
|
83
|
+
reads: string[];
|
|
84
|
+
writes: string[];
|
|
85
|
+
phase: SomaReactionPhase;
|
|
86
|
+
calcAsts: Record<string, CalcAst>;
|
|
87
|
+
declaredAt: number;
|
|
88
|
+
}> = [];
|
|
89
|
+
|
|
90
|
+
list.forEach((entry, index) => {
|
|
91
|
+
const at = `${path}[${index}]`;
|
|
92
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
93
|
+
fail('每条反应必须是对象', at);
|
|
94
|
+
}
|
|
95
|
+
const box = entry as Record<string, unknown>;
|
|
96
|
+
for (const key of Object.keys(box)) {
|
|
97
|
+
if (!CONFIG_KEYS.has(key)) {
|
|
98
|
+
fail(`未知的反应字段 "${key}"(支持: ${[...CONFIG_KEYS].join(' / ')})`, `${at}.${key}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 步 5:时机
|
|
103
|
+
const phaseRaw = box.on === undefined ? 'field_value_change' : String(box.on);
|
|
104
|
+
if (!PHASES.has(phaseRaw)) {
|
|
105
|
+
fail(`未知的触发时机 "${phaseRaw}"(支持: ${SOMA_REACTION_PHASES.join(' / ')})`, `${at}.on`);
|
|
106
|
+
}
|
|
107
|
+
const phase = phaseRaw as SomaReactionPhase;
|
|
108
|
+
|
|
109
|
+
// 步 1:谓词
|
|
110
|
+
const reads = new Set<string>();
|
|
111
|
+
if (box.when !== undefined) {
|
|
112
|
+
const issues = validatePredicate(box.when, fieldKeys, `${at}.when`, { fieldTypes, named });
|
|
113
|
+
if (issues.length > 0) fail(issues.join(';'), `${at}.when`);
|
|
114
|
+
for (const field of collectPredicateFields(box.when as SomaFieldPredicate, named)) reads.add(field);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 步 2:显式依赖
|
|
118
|
+
if (box.depends !== undefined) {
|
|
119
|
+
if (!Array.isArray(box.depends)) fail('depends 必须是字段名数组', `${at}.depends`);
|
|
120
|
+
for (const item of box.depends as unknown[]) {
|
|
121
|
+
const field = String(item ?? '');
|
|
122
|
+
if (!fieldKeys.includes(field)) {
|
|
123
|
+
fail(`depends 引用了不存在的字段 "${field}"`, `${at}.depends`);
|
|
124
|
+
}
|
|
125
|
+
reads.add(field);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (box.when === undefined && box.depends === undefined) {
|
|
130
|
+
fail(
|
|
131
|
+
'when 与 depends 至少要给一个——两个都没有的反应不知道何时该跑,'
|
|
132
|
+
+ '它既不会生效也不会报错,是最难发现的一类空声明',
|
|
133
|
+
at,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// `target` 只在校验类时机有意义
|
|
138
|
+
if (box.target !== undefined) {
|
|
139
|
+
const target = String(box.target);
|
|
140
|
+
if (!fieldKeys.includes(target)) fail(`target 引用了不存在的字段 "${target}"`, `${at}.target`);
|
|
141
|
+
if (!phase.startsWith('field_validate')) {
|
|
142
|
+
fail('target 只在 on: field_validate_* 时有意义(其余时机没有"哪个字段"这个概念)', `${at}.target`);
|
|
143
|
+
}
|
|
144
|
+
reads.add(target);
|
|
145
|
+
} else if (phase.startsWith('field_validate')) {
|
|
146
|
+
fail(`on: ${phase} 必须给 target(盯着哪个字段的校验结果)`, at);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 步 3:目标与 patch
|
|
150
|
+
const writes = new Set<string>();
|
|
151
|
+
const calcAsts: Record<string, CalcAst> = {};
|
|
152
|
+
for (const branch of ['fulfill', 'otherwise'] as const) {
|
|
153
|
+
const value = box[branch];
|
|
154
|
+
if (value === undefined) continue;
|
|
155
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
156
|
+
fail(`${branch} 必须是 { 字段名: patch } 对象`, `${at}.${branch}`);
|
|
157
|
+
}
|
|
158
|
+
for (const [target, patchRaw] of Object.entries(value as Record<string, unknown>)) {
|
|
159
|
+
const patchPath = `${at}.${branch}.${target}`;
|
|
160
|
+
if (target === SOMA_REACTION_FORM_KEY) {
|
|
161
|
+
checkFormPatch(patchRaw, patchPath, fail);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (!fieldKeys.includes(target)) {
|
|
165
|
+
fail(`${branch} 的目标 "${target}" 不是字典里的字段`, patchPath);
|
|
166
|
+
}
|
|
167
|
+
const ast = checkFieldPatch(patchRaw, target, fields[target], fieldKeys, fieldTypes, patchPath, fail);
|
|
168
|
+
if (ast) {
|
|
169
|
+
calcAsts[`${branch}:${target}`] = ast.node;
|
|
170
|
+
for (const ref of ast.refs) reads.add(ref);
|
|
171
|
+
}
|
|
172
|
+
writes.add(target);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (box.fulfill === undefined && box.otherwise === undefined) {
|
|
177
|
+
fail('fulfill 与 otherwise 至少要给一个(一条什么都不做的反应没有意义)', at);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
parsed.push({
|
|
181
|
+
config: box as unknown as SomaReactionConfig,
|
|
182
|
+
reads: [...reads],
|
|
183
|
+
writes: [...writes],
|
|
184
|
+
phase,
|
|
185
|
+
calcAsts,
|
|
186
|
+
declaredAt: index,
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ── 步 4:建图 + Kahn 拓扑 + 检环 ─────────────────────────────────
|
|
191
|
+
const order = topoSort(parsed, path, fail);
|
|
192
|
+
|
|
193
|
+
const reactions: SomaCompiledReaction[] = order.map((declaredAt, position) => {
|
|
194
|
+
const item = parsed[declaredAt];
|
|
195
|
+
return {
|
|
196
|
+
index: position,
|
|
197
|
+
declaredAt,
|
|
198
|
+
reads: item.reads,
|
|
199
|
+
writes: item.writes,
|
|
200
|
+
phase: item.phase,
|
|
201
|
+
config: item.config,
|
|
202
|
+
calcAsts: item.calcAsts,
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// ── 步 6:反向索引 + 回溯表 ────────────────────────────────────────
|
|
207
|
+
const byField: Record<string, number[]> = {};
|
|
208
|
+
reactions.forEach((reaction) => {
|
|
209
|
+
for (const field of reaction.reads) {
|
|
210
|
+
(byField[field] ??= []).push(reaction.index);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
reactions,
|
|
216
|
+
byField,
|
|
217
|
+
provenance: buildProvenance(reactions, fieldKeys, input.sourceApiOf ?? {}),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function checkFormPatch(raw: unknown, path: string, fail: ReactionParseDeps['fail']): void {
|
|
222
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
223
|
+
fail('submit 的 patch 必须是对象 { disabled: 布尔 }', path);
|
|
224
|
+
}
|
|
225
|
+
const box = raw as Record<string, unknown>;
|
|
226
|
+
for (const key of Object.keys(box)) {
|
|
227
|
+
if (key !== 'disabled') fail(`submit 只支持 disabled,收到 "${key}"`, `${path}.${key}`);
|
|
228
|
+
}
|
|
229
|
+
if (box.disabled !== undefined && typeof box.disabled !== 'boolean') {
|
|
230
|
+
fail('submit.disabled 必须是布尔', `${path}.disabled`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 返回 calc 的 AST 与其引用集(若该 patch 用了 calc) */
|
|
235
|
+
function checkFieldPatch(
|
|
236
|
+
raw: unknown,
|
|
237
|
+
target: string,
|
|
238
|
+
field: SomaDataFieldConfig,
|
|
239
|
+
fieldKeys: string[],
|
|
240
|
+
fieldTypes: Record<string, string | undefined>,
|
|
241
|
+
path: string,
|
|
242
|
+
fail: ReactionParseDeps['fail'],
|
|
243
|
+
): { node: CalcAst; refs: string[] } | null {
|
|
244
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
245
|
+
fail('patch 必须是对象', path);
|
|
246
|
+
}
|
|
247
|
+
const patch = raw as SomaFieldStatePatch & Record<string, unknown>;
|
|
248
|
+
for (const key of Object.keys(patch)) {
|
|
249
|
+
if (!PATCH_KEYS.has(key)) {
|
|
250
|
+
fail(`未知的 patch 键 "${key}"(支持: ${[...PATCH_KEYS].join(' / ')})`, `${path}.${key}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (patch.visible !== undefined && typeof patch.visible !== 'boolean') {
|
|
255
|
+
fail('visible 必须是布尔(要连数据一起摘除请写 display: none)', `${path}.visible`);
|
|
256
|
+
}
|
|
257
|
+
if (patch.visible !== undefined && patch.display !== undefined) {
|
|
258
|
+
fail('visible 与 display 只能给一个——visible 本来就是 display 的语法糖', path);
|
|
259
|
+
}
|
|
260
|
+
if (patch.display !== undefined && !DISPLAYS.has(String(patch.display))) {
|
|
261
|
+
fail(`display 只能是 ${SOMA_FIELD_DISPLAYS.join(' / ')}`, `${path}.display`);
|
|
262
|
+
}
|
|
263
|
+
if (patch.pattern !== undefined && !PATTERNS.has(String(patch.pattern))) {
|
|
264
|
+
fail(`pattern 只能是 ${SOMA_FIELD_PATTERNS.join(' / ')}`, `${path}.pattern`);
|
|
265
|
+
}
|
|
266
|
+
if (patch.required !== undefined && typeof patch.required !== 'boolean') {
|
|
267
|
+
fail('required 必须是布尔', `${path}.required`);
|
|
268
|
+
}
|
|
269
|
+
if (patch.message !== undefined && typeof patch.message !== 'string') {
|
|
270
|
+
fail('message 必须是字符串', `${path}.message`);
|
|
271
|
+
}
|
|
272
|
+
if (patch.options !== undefined) {
|
|
273
|
+
const from = (patch.options as { from?: unknown }).from;
|
|
274
|
+
if (typeof from !== 'string' || !/^[A-Za-z_]\w*\.[A-Za-z_]\w*$/.test(from)) {
|
|
275
|
+
fail("options.from 必须是 '<对象>.<字段>' 形态", `${path}.options`);
|
|
276
|
+
}
|
|
277
|
+
if (field.type !== 'enum') {
|
|
278
|
+
fail(`options 只能写在 enum 字段上,"${target}" 是 ${field.type ?? 'text'}`, `${path}.options`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// 只读字段不许被写值:permission 是数据层的裁决,反应图不能绕过它
|
|
283
|
+
if (patch.value !== undefined && field.permission === 'r') {
|
|
284
|
+
fail(
|
|
285
|
+
`"${target}" 是只读字段(permission: r),反应不能给它写值`
|
|
286
|
+
+ '——只读是数据层的裁决,联动绕过它就等于没有裁决',
|
|
287
|
+
`${path}.value`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (patch.value === undefined) return null;
|
|
292
|
+
if (patch.value === 'clear') return null;
|
|
293
|
+
|
|
294
|
+
if (typeof patch.value === 'object' && patch.value !== null && 'from' in patch.value) {
|
|
295
|
+
const from = String((patch.value as { from: unknown }).from);
|
|
296
|
+
if (!fieldKeys.includes(from)) fail(`value.from 引用了不存在的字段 "${from}"`, `${path}.value`);
|
|
297
|
+
return { node: null as unknown as CalcAst, refs: [from] };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (typeof patch.value === 'object' && patch.value !== null && 'calc' in patch.value) {
|
|
301
|
+
const source = String((patch.value as { calc: unknown }).calc);
|
|
302
|
+
const declared = field.type ?? 'text';
|
|
303
|
+
if (!CALC_WRITABLE.has(declared)) {
|
|
304
|
+
fail(`calc 不能写进 ${declared} 类型的字段 "${target}"`, `${path}.value`);
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const node = parseCalc(source);
|
|
308
|
+
const ctx = { fieldTypes, source };
|
|
309
|
+
const resultType = inferCalcType(node, ctx);
|
|
310
|
+
assertCalcResultType(resultType, declared, ctx, node);
|
|
311
|
+
return { node, refs: collectCalcRefs(node) };
|
|
312
|
+
} catch (error) {
|
|
313
|
+
if (error instanceof SomaCalcSyntaxError || error instanceof SomaCalcTypeError) {
|
|
314
|
+
fail(error.message, `${path}.value`);
|
|
315
|
+
}
|
|
316
|
+
throw error;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return fail("value 只能是 'clear' / { from: 字段 } / { calc: 表达式 }", `${path}.value`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Kahn 分层拓扑:节点是反应,边是「A 写的字段被 B 读」。
|
|
325
|
+
*
|
|
326
|
+
* 残留即环。错误信息必须给出**完整环路**而不是「存在环」——
|
|
327
|
+
* 一个五条反应的环里,只说「有环」等于让人自己重建这张图。
|
|
328
|
+
*/
|
|
329
|
+
function topoSort(
|
|
330
|
+
parsed: Array<{ reads: string[]; writes: string[]; declaredAt: number }>,
|
|
331
|
+
path: string,
|
|
332
|
+
fail: ReactionParseDeps['fail'],
|
|
333
|
+
): number[] {
|
|
334
|
+
const n = parsed.length;
|
|
335
|
+
const writerOf = new Map<string, number[]>();
|
|
336
|
+
parsed.forEach((item, index) => {
|
|
337
|
+
for (const field of item.writes) {
|
|
338
|
+
const list = writerOf.get(field) ?? [];
|
|
339
|
+
list.push(index);
|
|
340
|
+
writerOf.set(field, list);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const edges: Array<Set<number>> = Array.from({ length: n }, () => new Set<number>());
|
|
345
|
+
const indegree = new Array<number>(n).fill(0);
|
|
346
|
+
parsed.forEach((item, index) => {
|
|
347
|
+
for (const field of item.reads) {
|
|
348
|
+
for (const writer of writerOf.get(field) ?? []) {
|
|
349
|
+
if (writer === index) continue; // 自读自写不算边(同一条反应内是一次求值)
|
|
350
|
+
if (edges[writer].has(index)) continue;
|
|
351
|
+
edges[writer].add(index);
|
|
352
|
+
indegree[index] += 1;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const queue: number[] = [];
|
|
358
|
+
for (let i = 0; i < n; i += 1) if (indegree[i] === 0) queue.push(i);
|
|
359
|
+
// 声明序入队:同层反应按作者写的顺序跑,patch 合并时「后写的赢」才有确定含义
|
|
360
|
+
queue.sort((a, b) => a - b);
|
|
361
|
+
|
|
362
|
+
const order: number[] = [];
|
|
363
|
+
while (queue.length > 0) {
|
|
364
|
+
const current = queue.shift() as number;
|
|
365
|
+
order.push(current);
|
|
366
|
+
const next: number[] = [];
|
|
367
|
+
for (const to of edges[current]) {
|
|
368
|
+
indegree[to] -= 1;
|
|
369
|
+
if (indegree[to] === 0) next.push(to);
|
|
370
|
+
}
|
|
371
|
+
next.sort((a, b) => a - b);
|
|
372
|
+
queue.push(...next);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (order.length !== n) {
|
|
376
|
+
const remaining = new Set<number>();
|
|
377
|
+
for (let i = 0; i < n; i += 1) if (!order.includes(i)) remaining.add(i);
|
|
378
|
+
const cycle = traceCycle(remaining, edges);
|
|
379
|
+
fail(
|
|
380
|
+
`反应之间成环:${cycle.map((i) => `#${i + 1}`).join(' → ')} → #${cycle[0] + 1}`
|
|
381
|
+
+ '(编号是声明序)。成环的反应会在一次输入里来回改同一批字段,'
|
|
382
|
+
+ '表现是输入框自己跳;请把其中一条改成单向,或把中间结果做成一个真实字段',
|
|
383
|
+
path,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return order;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** 从残留节点里走出一条真实环路(DFS 找回边) */
|
|
390
|
+
function traceCycle(remaining: Set<number>, edges: Array<Set<number>>): number[] {
|
|
391
|
+
const start = [...remaining][0];
|
|
392
|
+
const stack: number[] = [];
|
|
393
|
+
const onStack = new Set<number>();
|
|
394
|
+
const seen = new Set<number>();
|
|
395
|
+
let found: number[] | null = null;
|
|
396
|
+
|
|
397
|
+
const walk = (node: number): void => {
|
|
398
|
+
if (found) return;
|
|
399
|
+
stack.push(node);
|
|
400
|
+
onStack.add(node);
|
|
401
|
+
seen.add(node);
|
|
402
|
+
for (const to of edges[node]) {
|
|
403
|
+
if (!remaining.has(to)) continue;
|
|
404
|
+
if (onStack.has(to)) {
|
|
405
|
+
found = stack.slice(stack.indexOf(to));
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (!seen.has(to)) walk(to);
|
|
409
|
+
if (found) return;
|
|
410
|
+
}
|
|
411
|
+
stack.pop();
|
|
412
|
+
onStack.delete(node);
|
|
413
|
+
};
|
|
414
|
+
walk(start);
|
|
415
|
+
return found ?? [start];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** 回溯表:写它的反应、传递闭包内的上游字段、来源接口 */
|
|
419
|
+
function buildProvenance(
|
|
420
|
+
reactions: readonly SomaCompiledReaction[],
|
|
421
|
+
fieldKeys: string[],
|
|
422
|
+
sourceApiOf: Readonly<Record<string, string | undefined>>,
|
|
423
|
+
): Record<string, SomaFieldProvenance> {
|
|
424
|
+
const writtenBy: Record<string, number[]> = {};
|
|
425
|
+
const directReads: Record<string, Set<string>> = {};
|
|
426
|
+
for (const field of fieldKeys) {
|
|
427
|
+
writtenBy[field] = [];
|
|
428
|
+
directReads[field] = new Set<string>();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
for (const reaction of reactions) {
|
|
432
|
+
for (const target of reaction.writes) {
|
|
433
|
+
writtenBy[target] ??= [];
|
|
434
|
+
writtenBy[target].push(reaction.declaredAt);
|
|
435
|
+
directReads[target] ??= new Set<string>();
|
|
436
|
+
for (const source of reaction.reads) directReads[target].add(source);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// 传递闭包:图无环,所以逐字段做一次 DFS 必然终止
|
|
441
|
+
const closure = (field: string, seen: Set<string>): Set<string> => {
|
|
442
|
+
for (const upstream of directReads[field] ?? []) {
|
|
443
|
+
if (seen.has(upstream)) continue;
|
|
444
|
+
seen.add(upstream);
|
|
445
|
+
closure(upstream, seen);
|
|
446
|
+
}
|
|
447
|
+
return seen;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const provenance: Record<string, SomaFieldProvenance> = {};
|
|
451
|
+
for (const field of Object.keys(directReads)) {
|
|
452
|
+
provenance[field] = {
|
|
453
|
+
writtenBy: writtenBy[field] ?? [],
|
|
454
|
+
readsFrom: [...closure(field, new Set<string>())].sort(),
|
|
455
|
+
...(sourceApiOf[field] ? { sourceApi: sourceApiOf[field] } : {}),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return provenance;
|
|
459
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// 字段级语法糖 → 反应(REQ-010 F-12)。
|
|
2
|
+
//
|
|
3
|
+
// `visible_when` / `disabled_when` 写在字段上(REQ-009 落地的形态)读起来最顺:
|
|
4
|
+
// 「税号:选了公司才填」就该写在税号那一行,而不是挪到远处的 reactions 段里。
|
|
5
|
+
// 但**求值器只能有一套**——如果字段糖走 `evaluatePredicate`、reactions 段走反应引擎,
|
|
6
|
+
// 两条路会在时机、传播、`display` 三态上慢慢漂开,而漂开的那天没有任何测试会红。
|
|
7
|
+
//
|
|
8
|
+
// 所以糖在解析期就地展开成反应,与 reactions 段合成同一张图、跑同一遍引擎。
|
|
9
|
+
// 展开是一对一的,回溯表因此仍能指回作者写的那一行。
|
|
10
|
+
|
|
11
|
+
import type { SomaReactionConfig } from '../../shared/types/reaction.types';
|
|
12
|
+
|
|
13
|
+
/** 展开所需的最小字段形状(不绑定 UI 侧的字段声明类型) */
|
|
14
|
+
export interface SomaSugarField {
|
|
15
|
+
key: string;
|
|
16
|
+
visible_when?: unknown;
|
|
17
|
+
disabled_when?: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `visible_when` 不成立时用 `display: none` 而不是 `hidden`——这是**行为等价**的要求:
|
|
22
|
+
* REQ-009 的 FormPanel 对隐藏字段既不渲染、也不校验、也不进提交载荷(`projected` 删键),
|
|
23
|
+
* 而 `hidden` 只是不给看、数据仍在。选错这一档,等价回归会红在提交载荷上。
|
|
24
|
+
*/
|
|
25
|
+
export function compileFieldSugar(fields: readonly SomaSugarField[]): SomaReactionConfig[] {
|
|
26
|
+
const out: SomaReactionConfig[] = [];
|
|
27
|
+
for (const field of fields) {
|
|
28
|
+
const { key } = field;
|
|
29
|
+
if (field.visible_when !== undefined && field.visible_when !== null) {
|
|
30
|
+
out.push({
|
|
31
|
+
when: field.visible_when as SomaReactionConfig['when'],
|
|
32
|
+
fulfill: { [key]: { display: 'visible' } },
|
|
33
|
+
otherwise: { [key]: { display: 'none' } },
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (field.disabled_when !== undefined && field.disabled_when !== null) {
|
|
37
|
+
// `otherwise` 给 `editable` 而不是「不写」:不写的话上一帧留下的 disabled 不会被撤销。
|
|
38
|
+
// 全局 disabled / permission='r' 由消费方另行取或,这里只表达这条规则自己的意见。
|
|
39
|
+
out.push({
|
|
40
|
+
when: field.disabled_when as SomaReactionConfig['when'],
|
|
41
|
+
fulfill: { [key]: { pattern: 'disabled' } },
|
|
42
|
+
otherwise: { [key]: { pattern: 'editable' } },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|