soma-core 1.4.2 → 1.5.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/bin/component.cjs +240 -0
- package/bin/eject.cjs +174 -0
- package/bin/migrate.cjs +362 -0
- package/core/build/blocks/block-compile.ts +143 -17
- package/core/build/blocks/migrate-flat.ts +118 -25
- package/core/build/compiler/page-declaration-compiler.ts +149 -5
- package/core/build/compiler/route-builder.ts +0 -4
- package/core/build/compiler/soma-precompile.js +431 -26
- package/core/build/component/component-graph.ts +28 -105
- package/core/build/component/package-loader.ts +18 -4
- package/core/build/eject/component-eject.ts +8 -2
- package/core/build/layout/constraint-solver.ts +131 -16
- package/core/build/layout/css-emitter.ts +29 -10
- package/core/build/ooui/artifact/artifact.types.ts +275 -0
- package/core/build/ooui/artifact/emit-artifact.ts +269 -0
- package/core/build/ooui/artifact/emit-html.ts +204 -0
- package/core/build/ooui/artifact/index.ts +53 -0
- package/core/build/ooui/artifact/spatial-tree.ts +268 -0
- package/core/build/ooui/artifact/utility-class.ts +83 -0
- package/core/build/ooui/composition/candidate.types.ts +323 -0
- package/core/build/ooui/composition/control-model.ts +114 -0
- package/core/build/ooui/composition/registry.ts +520 -0
- package/core/build/ooui/composition/select.ts +232 -0
- package/core/build/ooui/diagnostics/codes.ts +230 -0
- package/core/build/ooui/diagnostics/error.ts +235 -0
- package/core/build/ooui/diagnostics/format.ts +106 -0
- package/core/build/ooui/diagnostics/index.ts +62 -0
- package/core/build/ooui/diagnostics/issue.types.ts +112 -0
- package/core/build/ooui/engine-slot.ts +184 -0
- package/core/build/ooui/index.ts +1427 -0
- package/core/build/ooui/intent/derive-intent.ts +450 -0
- package/core/build/ooui/intent/field-rules.ts +330 -0
- package/core/build/ooui/intent/intent.types.ts +476 -0
- package/core/build/ooui/intent/verb-events.ts +285 -0
- package/core/build/ooui/page/budget.ts +503 -0
- package/core/build/ooui/page/compose-page.ts +736 -0
- package/core/build/ooui/page/index.ts +66 -0
- package/core/build/ooui/page/page.types.ts +402 -0
- package/core/build/ooui/page/region-row.ts +241 -0
- package/core/build/ooui/semantic/expand-page-surface.ts +180 -0
- package/core/build/ooui/semantic/kernel.types.ts +187 -0
- package/core/build/ooui/semantic/normalize.ts +784 -0
- package/core/build/ooui/semantic/origin.types.ts +211 -0
- package/core/build/ooui/semantic/role-alias.ts +154 -0
- package/core/build/ooui/semantic/shorthand.ts +233 -0
- package/core/build/ooui/semantic/surface.types.ts +78 -0
- package/core/build/ooui/semantic/validate-request.ts +267 -0
- package/core/build/ooui/spatial/emit-utility.ts +480 -0
- package/core/build/ooui/spatial/ir.types.ts +443 -0
- package/core/build/ooui/spatial/registry.ts +565 -0
- package/core/build/ooui/spatial/solve.ts +769 -0
- package/core/build/ooui/spatial/utility.types.ts +242 -0
- package/core/build/ooui/trace/index.ts +40 -0
- package/core/build/ooui/trace/render.ts +190 -0
- package/core/build/ooui/trace/source.types.ts +69 -0
- package/core/build/ooui/trace/trace.types.ts +326 -0
- package/core/client/component/component-expander.ts +18 -3
- package/core/client/component/component-manager.ts +4 -0
- package/core/client/component/example-blocks.ts +1 -7
- package/core/client/component/theme-component-tokens.ts +12 -15
- package/core/client/component/verify-pack.ts +43 -0
- package/core/client/data/data-object-resolver.ts +27 -8
- package/core/client/diagnostics/telemetry-context.ts +0 -4
- package/core/client/engine/asset-resolver.ts +0 -1
- package/core/client/engine/event-engine.ts +7 -3
- package/core/client/engine/soma-bootstrap.ts +292 -34
- package/core/client/engine/style-binder.ts +0 -1
- package/core/client/infra/http/IHttpClient.ts +0 -6
- package/core/client/infra/http/SomaHttpService.ts +4 -1
- package/core/client/infra/notify/DomNotifyService.ts +1 -1
- package/core/client/infra/router/router-path.ts +0 -6
- package/core/client/parser/annotation-extractor.ts +1 -1
- package/core/client/parser/blocks/namespace.ts +30 -5
- package/core/client/parser/blocks/page-parser.ts +44 -1
- package/core/client/parser/blocks/use-parser.ts +266 -46
- package/core/client/parser/data-config-parser.ts +71 -3
- package/core/client/parser/layout-parser.ts +4 -3
- package/core/client/parser/reaction-parser.ts +0 -1
- package/core/client/parser/ui-config-parser.ts +212 -74
- package/core/client/theme/scene-tokens.ts +9 -3
- package/core/client/theme/theme-axes.ts +74 -16
- package/core/client/theme/theme-engine.ts +341 -70
- package/core/client/theme/tone-solver.ts +9 -8
- package/core/index.ts +24 -5
- package/core/server/api/load-host-service.ts +8 -2
- package/core/server/declaration-safety.ts +11 -0
- package/core/server/host/page-response-request.ts +0 -2
- package/core/server/host/published-page-artifact-cache.ts +244 -0
- package/core/server/host/published-page-assets.ts +44 -5
- package/core/server/mock/node/cli.mjs +0 -1
- package/core/server/mock/node/schema-generator.mjs +0 -1
- package/core/server/ooui.ts +34 -0
- package/core/server/page-assets.ts +8 -0
- package/core/server/page-declaration.ts +18 -0
- package/core/server/registry/asset-store.ts +327 -0
- package/core/server/registry/declaration-precompiler.ts +49 -6
- package/core/server/registry/index.ts +47 -1
- package/core/server/registry/page-registry-repository.ts +6 -2
- package/core/server/registry/page-registry-store.ts +133 -19
- package/core/server/registry/publish-asset-pin.ts +557 -0
- package/core/server/registry/services/pages/List.ts +15 -3
- package/core/server/registry/validation/compile-gate.ts +180 -8
- package/core/server/registry/validation/declaration-safety-scan.ts +113 -23
- package/core/shared/component/derive-sample.ts +201 -16
- package/core/shared/component/field-lines.ts +98 -8
- package/core/shared/container-breakpoints.ts +46 -0
- package/core/shared/object-semantics.ts +240 -0
- package/core/shared/ooui-declaration.ts +235 -0
- package/core/shared/types/component-contract.types.ts +7 -6
- package/core/shared/types/data-config.types.ts +95 -0
- package/core/shared/types/domain.types.ts +0 -24
- package/core/shared/types/engine.types.ts +0 -5
- package/core/shared/types/layout-constraint.types.ts +16 -0
- package/core/shared/types/ui-config.types.ts +163 -13
- package/dist/cli/soma.js +24 -11
- package/dist/core/index.cjs +1829 -1028
- package/dist/types/build/blocks/block-compile.d.ts +16 -1
- package/dist/types/build/blocks/migrate-flat.d.ts +17 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +46 -0
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/component/component-graph.d.ts +17 -53
- package/dist/types/build/component/package-loader.d.ts +8 -1
- package/dist/types/build/layout/constraint-solver.d.ts +46 -4
- package/dist/types/build/ooui/artifact/artifact.types.d.ts +180 -0
- package/dist/types/build/ooui/artifact/emit-artifact.d.ts +58 -0
- package/dist/types/build/ooui/artifact/emit-html.d.ts +53 -0
- package/dist/types/build/ooui/artifact/index.d.ts +5 -0
- package/dist/types/build/ooui/artifact/spatial-tree.d.ts +48 -0
- package/dist/types/build/ooui/artifact/utility-class.d.ts +46 -0
- package/dist/types/build/ooui/composition/candidate.types.d.ts +206 -0
- package/dist/types/build/ooui/composition/control-model.d.ts +37 -0
- package/dist/types/build/ooui/composition/registry.d.ts +66 -0
- package/dist/types/build/ooui/composition/select.d.ts +63 -0
- package/dist/types/build/ooui/diagnostics/codes.d.ts +28 -0
- package/dist/types/build/ooui/diagnostics/error.d.ts +135 -0
- package/dist/types/build/ooui/diagnostics/format.d.ts +51 -0
- package/dist/types/build/ooui/diagnostics/index.d.ts +25 -0
- package/dist/types/build/ooui/diagnostics/issue.types.d.ts +56 -0
- package/dist/types/build/ooui/engine-slot.d.ts +41 -0
- package/dist/types/build/ooui/index.d.ts +286 -0
- package/dist/types/build/ooui/intent/derive-intent.d.ts +32 -0
- package/dist/types/build/ooui/intent/field-rules.d.ts +99 -0
- package/dist/types/build/ooui/intent/intent.types.d.ts +318 -0
- package/dist/types/build/ooui/intent/verb-events.d.ts +70 -0
- package/dist/types/build/ooui/page/budget.d.ts +28 -0
- package/dist/types/build/ooui/page/compose-page.d.ts +41 -0
- package/dist/types/build/ooui/page/index.d.ts +4 -0
- package/dist/types/build/ooui/page/page.types.d.ts +264 -0
- package/dist/types/build/ooui/page/region-row.d.ts +16 -0
- package/dist/types/build/ooui/semantic/expand-page-surface.d.ts +33 -0
- package/dist/types/build/ooui/semantic/kernel.types.d.ts +125 -0
- package/dist/types/build/ooui/semantic/normalize.d.ts +45 -0
- package/dist/types/build/ooui/semantic/origin.types.d.ts +95 -0
- package/dist/types/build/ooui/semantic/role-alias.d.ts +35 -0
- package/dist/types/build/ooui/semantic/shorthand.d.ts +60 -0
- package/dist/types/build/ooui/semantic/surface.types.d.ts +16 -0
- package/dist/types/build/ooui/semantic/validate-request.d.ts +19 -0
- package/dist/types/build/ooui/spatial/emit-utility.d.ts +43 -0
- package/dist/types/build/ooui/spatial/ir.types.d.ts +280 -0
- package/dist/types/build/ooui/spatial/registry.d.ts +110 -0
- package/dist/types/build/ooui/spatial/solve.d.ts +42 -0
- package/dist/types/build/ooui/spatial/utility.types.d.ts +191 -0
- package/dist/types/build/ooui/trace/index.d.ts +3 -0
- package/dist/types/build/ooui/trace/render.d.ts +42 -0
- package/dist/types/build/ooui/trace/source.types.d.ts +25 -0
- package/dist/types/build/ooui/trace/trace.types.d.ts +143 -0
- package/dist/types/client/component/component-expander.d.ts +1 -0
- package/dist/types/client/component/example-blocks.d.ts +0 -2
- package/dist/types/client/component/theme-component-tokens.d.ts +8 -5
- package/dist/types/client/data/data-object-resolver.d.ts +10 -2
- package/dist/types/client/diagnostics/telemetry-context.d.ts +0 -1
- package/dist/types/client/engine/soma-bootstrap.d.ts +123 -10
- package/dist/types/client/infra/http/IHttpClient.d.ts +0 -5
- package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
- package/dist/types/client/infra/router/router-path.d.ts +0 -2
- package/dist/types/client/parser/blocks/namespace.d.ts +20 -0
- package/dist/types/client/parser/blocks/page-parser.d.ts +9 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +38 -4
- package/dist/types/client/parser/data-config-parser.d.ts +13 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +1 -11
- package/dist/types/client/theme/scene-tokens.d.ts +9 -3
- package/dist/types/client/theme/theme-axes.d.ts +64 -1
- package/dist/types/client/theme/theme-engine.d.ts +13 -8
- package/dist/types/client/theme/tone-solver.d.ts +9 -5
- package/dist/types/index.d.ts +4 -6
- package/dist/types/server/declaration-safety.d.ts +7 -0
- package/dist/types/server/host/page-response-request.d.ts +0 -1
- package/dist/types/server/host/published-page-artifact-cache.d.ts +64 -0
- package/dist/types/server/ooui.d.ts +18 -0
- package/dist/types/server/page-assets.d.ts +7 -0
- package/dist/types/server/page-declaration.d.ts +14 -0
- package/dist/types/server/registry/asset-store.d.ts +112 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +23 -3
- package/dist/types/server/registry/index.d.ts +3 -1
- package/dist/types/server/registry/page-registry-store.d.ts +57 -4
- package/dist/types/server/registry/publish-asset-pin.d.ts +166 -0
- package/dist/types/server/registry/services/pages/List.d.ts +9 -3
- package/dist/types/server/registry/validation/compile-gate.d.ts +63 -0
- package/dist/types/shared/component/derive-sample.d.ts +14 -0
- package/dist/types/shared/container-breakpoints.d.ts +42 -0
- package/dist/types/shared/object-semantics.d.ts +42 -0
- package/dist/types/shared/ooui-declaration.d.ts +135 -0
- package/dist/types/shared/types/component-contract.types.d.ts +7 -6
- package/dist/types/shared/types/data-config.types.d.ts +86 -0
- package/dist/types/shared/types/domain.types.d.ts +0 -21
- package/dist/types/shared/types/engine.types.d.ts +0 -4
- package/dist/types/shared/types/layout-constraint.types.d.ts +1 -0
- package/dist/types/shared/types/ui-config.types.d.ts +157 -12
- package/next.config.js +106 -13
- package/package.json +29 -2
- package/core/client/host-bridge/index.ts +0 -27
- package/core/client/parser/yaml-loader.ts +0 -36
- package/core/shared/manifest/manifest-contract.ts +0 -12
- package/dist/types/client/host-bridge/index.d.ts +0 -9
- package/dist/types/client/parser/yaml-loader.d.ts +0 -21
- package/dist/types/shared/manifest/manifest-contract.d.ts +0 -4
|
@@ -17,8 +17,38 @@
|
|
|
17
17
|
// 看到不像声明问题的内部错误,那就该去查产物是不是陈旧了。
|
|
18
18
|
//
|
|
19
19
|
// HTML 与统一声明必须一起编译,因为锚点归属是语言语义的一部分。
|
|
20
|
+
//
|
|
21
|
+
// ── 为什么"把结构化错误压成一句字符串"是个静默失败 ─────────────────────
|
|
22
|
+
// 这里 catch 到的错误不是给人看的终点,而是 **AI 修订链自修复的输入**。
|
|
23
|
+
// 架构 §20 的错误体系带 `code`(稳定契约,进 external-docs)、`src`(指回改哪一行)、
|
|
24
|
+
// `trace`(到失败点为止的推导链)、`remedy`(能直接抄的修法);仓库里
|
|
25
|
+
// `SomaOouiError`(core/build/ooui/diagnostics/error.ts)与它的子类
|
|
26
|
+
// `SomaSpatialError`(core/build/ooui/spatial/utility.types.ts)已经按这套形状在抛,
|
|
27
|
+
// `SomaBlockParseError` 也带着 `blockId` / `line`。
|
|
28
|
+
// 一旦这里只做 `${error.message}`,除 message 外的字段全部蒸发,而**没有任何东西会红**:
|
|
29
|
+
// ① 类型检查过——模板字符串接受任何值,丢掉的字段不留痕迹;
|
|
30
|
+
// ② 闸门照常拒绝非法声明,fail-closed 语义一点没坏;
|
|
31
|
+
// ③ 测试照常绿——断言的本来就是那句字符串。
|
|
32
|
+
// 坏掉的只有修复回路:模型收到"编译不过:…",看不见 remedy 就只能猜,
|
|
33
|
+
// 猜不中就在同一处反复撞(实测的收敛失败正是这个形状)。
|
|
34
|
+
// 发作时刻(某次生成怎么改都收敛不了)与写下时刻(这一行模板字符串)相隔很远,
|
|
35
|
+
// 现场没有任何线索指回这里——所以必须在这一层就把结构留住。
|
|
36
|
+
//
|
|
37
|
+
// 本模块的兑现方式:**字符串照旧发,结构照旧带**。
|
|
38
|
+
// - `dryRunCompile()` 的返回值逐字不变(现有消费者零改动);
|
|
39
|
+
// - `dryRunCompileWithDiagnostics()` 额外给出 `diagnostics`(纯数据,可 JSON/YAML 序列化)
|
|
40
|
+
// 与 `compiled`(编译产物,供同一条发布链复用,见下)。
|
|
41
|
+
//
|
|
42
|
+
// ── 为什么要把编译产物一起带出来 ───────────────────────────────────
|
|
43
|
+
// 发布路径上 `compilePageDeclaration` 曾在同一次发布里被调不止一次:本闸门一次,
|
|
44
|
+
// `page-registry-store` 取 `dataConfig` 做 API 白名单交叉校验一次,
|
|
45
|
+
// `createPageVersionRecord` 落预编译产物(`precompileDeclaration`)又一次。
|
|
46
|
+
// 同一份输入、同一个纯函数、同一次发布里相邻几步——后几次只是在重付编译代价。
|
|
47
|
+
// 闸门把产物一并返回,`normalizeDeclaration` 把它交给白名单与落库两处复用;
|
|
48
|
+
// 复用的前提是**产物在手才放行**:拿不到产物时调用方必须拒绝(fail-closed),
|
|
49
|
+
// 不能"没编出来就跳过白名单"。
|
|
20
50
|
|
|
21
|
-
import { compilePageDeclaration } from '../../../build/compiler/page-declaration-compiler';
|
|
51
|
+
import { compilePageDeclaration, type CompiledPageDeclaration } from '../../../build/compiler/page-declaration-compiler';
|
|
22
52
|
|
|
23
53
|
export interface CompilableDeclaration {
|
|
24
54
|
html: string;
|
|
@@ -28,27 +58,169 @@ export interface CompilableDeclaration {
|
|
|
28
58
|
dataYaml?: string;
|
|
29
59
|
}
|
|
30
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 声明位置。与架构 §3.1 的 `SomaSource`、空间层的 `SomaSpatialSource` 同构,
|
|
63
|
+
* 另收 `SomaBlockParseError` 的块标识与行号——两种定位方式都要能落地。
|
|
64
|
+
*/
|
|
65
|
+
export interface SomaCompileDiagnosticSource {
|
|
66
|
+
/** 声明所在文件(仓库相对路径),或 `<vocabulary:名字>` 这样的合成来源 */
|
|
67
|
+
file?: string;
|
|
68
|
+
/** 文件内的定位路径,如 `page.state`、utility 的 root */
|
|
69
|
+
path?: string;
|
|
70
|
+
/** SOMA DSL 块标识 */
|
|
71
|
+
blockId?: string;
|
|
72
|
+
/** 块内行号(1 起) */
|
|
73
|
+
line?: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 一条编译诊断。**纯数据**:无函数、无 Map/Set、无循环引用,可直接 JSON/YAML 序列化——
|
|
78
|
+
* 它要跨进程(HTTP 响应)与跨包(soma-ai 自修复提示词)传,带不动活对象。
|
|
79
|
+
*
|
|
80
|
+
* 字段全部可选是刻意的:演绎链抛的 `SomaOouiError`(及其子类)带 `code` / `layer` /
|
|
81
|
+
* `remedy`,`src` 与 `trace` 视抛出点而定;解析器与块编译抛的多是普通 `Error` /
|
|
82
|
+
* `SomaBlockParseError`,只有 `message`(后者另有 `blockId` / `line`)。
|
|
83
|
+
* 同一个结构几种来源都装得下,消费者不必按来源分形状。
|
|
84
|
+
*/
|
|
85
|
+
export interface SomaCompileDiagnostic {
|
|
86
|
+
/** 与 `issues[]` 同序、逐字相同的那句中文(消费者可用它对账) */
|
|
87
|
+
message: string;
|
|
88
|
+
/** 解析器原话,不含 `[编译] …` 前缀 */
|
|
89
|
+
detail: string;
|
|
90
|
+
/** 错误类名,如 `SomaBlockParseError`;拿不到就是 `Error` */
|
|
91
|
+
name: string;
|
|
92
|
+
/** 稳定错误码(架构 §20.2 的公开契约),如 `OOUI-INT-NO-RULE` */
|
|
93
|
+
code?: string;
|
|
94
|
+
/** 出错的层,如 `spatial` / `intent` */
|
|
95
|
+
layer?: string;
|
|
96
|
+
/** 可操作的修法——AI 自修复真正要读的那一段 */
|
|
97
|
+
remedy?: string;
|
|
98
|
+
/** 指回声明位置 */
|
|
99
|
+
src?: SomaCompileDiagnosticSource;
|
|
100
|
+
/** 到失败点为止的推导链;只在原样可序列化时带出 */
|
|
101
|
+
trace?: readonly unknown[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 干跑编译的完整结果:判定 + 结构化诊断 + 可复用的编译产物 */
|
|
105
|
+
export interface CompileGateResult {
|
|
106
|
+
/** 中文问题清单(空数组 = 通过);格式与 `dryRunCompile()` 逐字一致 */
|
|
107
|
+
issues: string[];
|
|
108
|
+
/** 与 `issues` 同序的结构化诊断;通过时为空数组 */
|
|
109
|
+
diagnostics: SomaCompileDiagnostic[];
|
|
110
|
+
/**
|
|
111
|
+
* 编译产物。**只有 `issues` 为空时才可能非 null**;
|
|
112
|
+
* 调用方复用它之前必须判空(拿不到就拒,不能跳过后续闸门)。
|
|
113
|
+
*/
|
|
114
|
+
compiled: CompiledPageDeclaration | null;
|
|
115
|
+
}
|
|
116
|
+
|
|
31
117
|
function message(error: unknown): string {
|
|
32
118
|
return error instanceof Error ? error.message : String(error);
|
|
33
119
|
}
|
|
34
120
|
|
|
121
|
+
/** 只认真正的字符串,空串按"没有"处理——半个字段比没有字段更难查 */
|
|
122
|
+
function readString(source: Record<string, unknown>, key: string): string | undefined {
|
|
123
|
+
const value = source[key];
|
|
124
|
+
return typeof value === 'string' && value.trim() !== '' ? value : undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readNumber(source: Record<string, unknown>, key: string): number | undefined {
|
|
128
|
+
const value = source[key];
|
|
129
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
35
132
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
133
|
+
* 抽取声明位置:`src: { file, path }`(§20 形状)与 `blockId` / `line`
|
|
134
|
+
* (`SomaBlockParseError` 形状)都收,一条都没有就不产出 `src` 字段——
|
|
135
|
+
* 空壳 `{}` 会让消费者以为"有位置信息但都是空的"。
|
|
38
136
|
*/
|
|
39
|
-
|
|
40
|
-
const
|
|
137
|
+
function readSource(source: Record<string, unknown>): SomaCompileDiagnosticSource | undefined {
|
|
138
|
+
const nested = source.src;
|
|
139
|
+
const nestedRecord = nested && typeof nested === 'object' && !Array.isArray(nested)
|
|
140
|
+
? nested as Record<string, unknown>
|
|
141
|
+
: {};
|
|
142
|
+
const src: SomaCompileDiagnosticSource = {
|
|
143
|
+
...(readString(nestedRecord, 'file') ? { file: readString(nestedRecord, 'file')! } : {}),
|
|
144
|
+
...(readString(nestedRecord, 'path') ? { path: readString(nestedRecord, 'path')! } : {}),
|
|
145
|
+
...(readString(source, 'blockId') ? { blockId: readString(source, 'blockId')! } : {}),
|
|
146
|
+
...(readNumber(source, 'line') !== undefined ? { line: readNumber(source, 'line')! } : {}),
|
|
147
|
+
};
|
|
148
|
+
return Object.keys(src).length > 0 ? src : undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* trace 必须原样可序列化才带出。
|
|
153
|
+
*
|
|
154
|
+
* 为什么要 JSON 往返一次:trace 是别的层构造的对象,可能挂着函数、Map 或循环引用。
|
|
155
|
+
* 直接塞进诊断,等到 HTTP 响应 `JSON.stringify` 时才炸——那一刻栈里已经没有编译现场了,
|
|
156
|
+
* 且**一条声明校验失败会退化成 500**。宁可这里丢掉 trace,也不能让诊断本身成为新失败面。
|
|
157
|
+
*/
|
|
158
|
+
function readTrace(source: Record<string, unknown>): readonly unknown[] | undefined {
|
|
159
|
+
const trace = source.trace;
|
|
160
|
+
if (!Array.isArray(trace) || trace.length === 0) return undefined;
|
|
161
|
+
try {
|
|
162
|
+
return JSON.parse(JSON.stringify(trace)) as unknown[];
|
|
163
|
+
} catch {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 把抛出来的错误翻译成结构化诊断。
|
|
170
|
+
*
|
|
171
|
+
* 刻意用鸭子类型而不是 `instanceof`:
|
|
172
|
+
* ① 错误来自不止一个类族——`SomaOouiError`(core/build/ooui)、`SomaBlockParseError`
|
|
173
|
+
* (core/shared)、解析器的普通 `Error`——按类判就得把每个类族都 import 进注册中心,
|
|
174
|
+
* 而这里要的只是它们共有的那几个字段;
|
|
175
|
+
* ② 同一个类经 dist / 源码两条路加载时 `instanceof` 会失手(跨包最常见的假阴性;
|
|
176
|
+
* soma-precompile.js 打包的解析器就是另一份类定义),而字段读取不受影响。
|
|
177
|
+
*/
|
|
178
|
+
function toDiagnostic(error: unknown, issue: string): SomaCompileDiagnostic {
|
|
179
|
+
const detail = message(error);
|
|
180
|
+
const source = error && typeof error === 'object' ? error as Record<string, unknown> : {};
|
|
181
|
+
const name = readString(source, 'name') ?? 'Error';
|
|
182
|
+
const src = readSource(source);
|
|
183
|
+
const trace = readTrace(source);
|
|
184
|
+
return {
|
|
185
|
+
message: issue,
|
|
186
|
+
detail,
|
|
187
|
+
name,
|
|
188
|
+
...(readString(source, 'code') ? { code: readString(source, 'code')! } : {}),
|
|
189
|
+
...(readString(source, 'layer') ? { layer: readString(source, 'layer')! } : {}),
|
|
190
|
+
...(readString(source, 'remedy') ? { remedy: readString(source, 'remedy')! } : {}),
|
|
191
|
+
...(src ? { src } : {}),
|
|
192
|
+
...(trace ? { trace } : {}),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
41
195
|
|
|
196
|
+
/**
|
|
197
|
+
* 干跑编译的完整形态:判定 + 结构化诊断 + 编译产物。
|
|
198
|
+
*
|
|
199
|
+
* `issues` 与 `dryRunCompile()` 逐字一致——两者是同一条判定,不是两套规则。
|
|
200
|
+
*/
|
|
201
|
+
export function dryRunCompileWithDiagnostics(declaration: CompilableDeclaration): CompileGateResult {
|
|
42
202
|
try {
|
|
43
|
-
compilePageDeclaration({
|
|
203
|
+
const compiled = compilePageDeclaration({
|
|
44
204
|
html: declaration.html ?? '',
|
|
45
205
|
uiYaml: declaration.uiYaml ?? '',
|
|
46
206
|
...(declaration.eventYaml ? { eventYaml: declaration.eventYaml } : {}),
|
|
47
207
|
...(declaration.styleYaml ? { styleYaml: declaration.styleYaml } : {}),
|
|
48
208
|
...(declaration.dataYaml ? { dataYaml: declaration.dataYaml } : {}),
|
|
49
209
|
});
|
|
210
|
+
return { issues: [], diagnostics: [], compiled };
|
|
50
211
|
} catch (error) {
|
|
51
|
-
|
|
212
|
+
const issue = `[编译] SOMA DSL 声明编译不过:${message(error)}`;
|
|
213
|
+
return { issues: [issue], diagnostics: [toDiagnostic(error, issue)], compiled: null };
|
|
52
214
|
}
|
|
53
|
-
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 干跑编译,返回中文问题清单(空数组 = 通过)。
|
|
219
|
+
* 与安全扫描同款约定:命中即拒,清单点名到具体哪一件、解析器原话是什么。
|
|
220
|
+
*
|
|
221
|
+
* 需要 `code` / `src` / `trace` / `remedy` 或想复用编译产物的调用方,
|
|
222
|
+
* 改用 `dryRunCompileWithDiagnostics()`——本函数是它的字符串投影,判定完全相同。
|
|
223
|
+
*/
|
|
224
|
+
export function dryRunCompile(declaration: CompilableDeclaration): string[] {
|
|
225
|
+
return dryRunCompileWithDiagnostics(declaration).issues;
|
|
54
226
|
}
|
|
@@ -10,6 +10,26 @@
|
|
|
10
10
|
// **绑定写入**这条路;mount 的静态骨架不经过它们。故在写入声明仓库这个唯一入口做静态
|
|
11
11
|
// 扫描:坏声明根本不允许落库,而不是指望渲染端每次都拦得住。
|
|
12
12
|
//
|
|
13
|
+
// ── 为什么四个 YAML 面也要跑 html 那四条判据 ────────────────────────
|
|
14
|
+
// 曾经的读法是「DOM 只住在 `declaration.html` 这一面,YAML 面只可能在**值位**上写伪协议
|
|
15
|
+
// (`@href` 绑定的默认值)」,于是 YAML 面只跑了伪协议一条。REQ-014 的 OOUI 演绎产物
|
|
16
|
+
// 把这个前提推翻了:**演绎出来的那份 DOM 就住在 `_ui.yaml` 里**
|
|
17
|
+
// (`ooui_artifact.regions[].html`),运行时由 `soma-bootstrap.ts` 的 `installOouiHtml`
|
|
18
|
+
// 整串插进锚点。产物本该由构建期编译生成,但资产是可手写、可手改的——
|
|
19
|
+
// 于是一份改过的 `_ui.yaml` 能把 `<img src=x onerror=…>` 送进 DOM,
|
|
20
|
+
// **而发布闸门是绿的**:门口那道扫描根本没看这一面。
|
|
21
|
+
//
|
|
22
|
+
// 这个失败模式之所以静默:注入点那一侧已经用允许集兜住了(`installOouiHtml` 注入前
|
|
23
|
+
// 再核一道结构),所以坏产物今天的表现是「那一块不出现 + 控制台一条 error」——
|
|
24
|
+
// 页面其余部分照常,发布是成功的,没有任何东西指回「这份声明当初就不该落库」。
|
|
25
|
+
// 纵深防御的两层里,门口那层缺了就只剩最后一层,而最后一层是按标签/属性允许集写的:
|
|
26
|
+
// 允许集哪天为了新形态放宽一格,缺口立刻直通 DOM。
|
|
27
|
+
//
|
|
28
|
+
// 判据施加到 YAML 面时是**按文本**扫的,不解析 YAML(本文件零依赖,不引 js-yaml):
|
|
29
|
+
// 文本扫描会连注释、块标量、被引号裹住的片段一起看,宁可误伤也不放过——
|
|
30
|
+
// 而 YAML 面上「合法却长得像 html 注入」的写法本来就不该存在(实测语料见
|
|
31
|
+
// tests/unit/registry/declaration-safety-scan.test.ts 的真产物正样本)。
|
|
32
|
+
//
|
|
13
33
|
// 扫描同时是 CSP 的事实依据:正因为落库的声明里不可能有内联脚本与外链标签,
|
|
14
34
|
// Host 才敢把 `script-src` 收到 `'self'`(见 doc/soma 细节与生产化强化.md §4.2)。
|
|
15
35
|
//
|
|
@@ -37,6 +57,9 @@ const DANGEROUS_SCHEME = String.raw`(?:javascript|vbscript)\s*:|data\s*:\s*text/
|
|
|
37
57
|
/** 内联事件处理器属性:要求 `on` 前是空白或标签起始,避免误伤 data-on-xxx 这类命名 */
|
|
38
58
|
const INLINE_HANDLER_RE = /(?:^|\s)on[a-z]+\s*=/i;
|
|
39
59
|
|
|
60
|
+
/** `srcdoc=`:等价于内联一整份文档 */
|
|
61
|
+
const SRCDOC_RE = /\bsrcdoc\s*=/i;
|
|
62
|
+
|
|
40
63
|
/** URL 属性上的伪协议(属性值可带引号,也可裸写) */
|
|
41
64
|
const URL_SCHEME_RE = new RegExp(
|
|
42
65
|
String.raw`(?:${URL_ATTRS.map((attr) => attr.replace(':', '\\:')).join('|')})\s*=\s*["']?\s*(?:${DANGEROUS_SCHEME})`,
|
|
@@ -50,38 +73,101 @@ function findForbiddenTags(html: string): string[] {
|
|
|
50
73
|
return FORBIDDEN_TAGS.filter((tag) => new RegExp(String.raw`<\s*/?\s*${tag}\b`, 'i').test(html));
|
|
51
74
|
}
|
|
52
75
|
|
|
76
|
+
/** html 形态的四条判据之一的命中项 */
|
|
77
|
+
type HtmlOffenceKind = 'forbidden-tag' | 'inline-handler' | 'srcdoc' | 'url-scheme';
|
|
78
|
+
|
|
79
|
+
interface HtmlOffence {
|
|
80
|
+
kind: HtmlOffenceKind;
|
|
81
|
+
/** 命中了什么(禁用标签清单 / 命中的属性片段)。进消息,省得读者自己再找一遍 */
|
|
82
|
+
detail: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
53
85
|
/**
|
|
54
|
-
*
|
|
86
|
+
* 一个面上的 html 形态命中项。**html 面与四个 YAML 面共用这一份判据**——
|
|
87
|
+
* 各写一份的话,两份在写下的那天一致,漂开发生在半年后只改了其中一份那天,
|
|
88
|
+
* 而漂开的表现是「同一段注入在某一面被拦、在另一面放行」,两处都看着对。
|
|
55
89
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
90
|
+
* 判据顺序固定(禁用标签 → 内联处理器 → srcdoc → 伪协议):问题清单要幂等,
|
|
91
|
+
* 同一份声明每次扫描必须给出同一串消息(清单会回喂模型自修复,顺序一抖
|
|
92
|
+
* 就是一次「声明没变而修法变了」)。
|
|
58
93
|
*/
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
const html = declaration.html ?? '';
|
|
94
|
+
function findHtmlOffences(source: string): HtmlOffence[] {
|
|
95
|
+
const offences: HtmlOffence[] = [];
|
|
62
96
|
|
|
63
|
-
const forbidden = findForbiddenTags(
|
|
97
|
+
const forbidden = findForbiddenTags(source);
|
|
64
98
|
if (forbidden.length > 0) {
|
|
65
|
-
|
|
66
|
-
`[安全] html 含禁用标签 ${forbidden.map((tag) => `<${tag}>`).join('、')}`
|
|
67
|
-
+ '——声明态页面不许自带脚本与外链资源;样式状态与事件必须写进 canonical _ui.yaml,受信任能力封装为 Host 组件',
|
|
68
|
-
);
|
|
99
|
+
offences.push({ kind: 'forbidden-tag', detail: forbidden.map((tag) => `<${tag}>`).join('、') });
|
|
69
100
|
}
|
|
70
101
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
'[安全] html 含内联事件属性(on*=…)'
|
|
74
|
-
+ '——innerHTML 挂载时这类属性会立刻执行,事件一律写入 canonical _ui.yaml 的四动词 Pipeline',
|
|
75
|
-
);
|
|
76
|
-
}
|
|
102
|
+
const handler = INLINE_HANDLER_RE.exec(source);
|
|
103
|
+
if (handler) offences.push({ kind: 'inline-handler', detail: handler[0].trim() });
|
|
77
104
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
105
|
+
const srcdoc = SRCDOC_RE.exec(source);
|
|
106
|
+
if (srcdoc) offences.push({ kind: 'srcdoc', detail: srcdoc[0].trim() });
|
|
107
|
+
|
|
108
|
+
const scheme = URL_SCHEME_RE.exec(source);
|
|
109
|
+
if (scheme) offences.push({ kind: 'url-scheme', detail: scheme[0].trim() });
|
|
110
|
+
|
|
111
|
+
return offences;
|
|
112
|
+
}
|
|
81
113
|
|
|
82
|
-
|
|
83
|
-
|
|
114
|
+
/** html 面的措辞(历史文案,逐字保留:生成链闸门的自修复提示词按它匹配) */
|
|
115
|
+
function htmlIssue(offence: HtmlOffence): string {
|
|
116
|
+
switch (offence.kind) {
|
|
117
|
+
case 'forbidden-tag':
|
|
118
|
+
return `[安全] html 含禁用标签 ${offence.detail}`
|
|
119
|
+
+ '——声明态页面不许自带脚本与外链资源;样式状态与事件必须写进 canonical _ui.yaml,受信任能力封装为 Host 组件';
|
|
120
|
+
case 'inline-handler':
|
|
121
|
+
return '[安全] html 含内联事件属性(on*=…)'
|
|
122
|
+
+ '——innerHTML 挂载时这类属性会立刻执行,事件一律写入 canonical _ui.yaml 的四动词 Pipeline';
|
|
123
|
+
case 'srcdoc':
|
|
124
|
+
return '[安全] html 含 srcdoc 属性——等价于内联一整份文档,不允许出现在页面声明里';
|
|
125
|
+
case 'url-scheme':
|
|
126
|
+
return '[安全] html 的链接/资源属性含伪协议(javascript: / vbscript: / data:text/html)';
|
|
84
127
|
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* YAML 面的措辞:**点名是哪一面、命中了什么、修法能直接抄**。
|
|
132
|
+
*
|
|
133
|
+
* 不复用 html 面那四句:修法不同。html 面的修法是「改 html」;YAML 面上这类命中
|
|
134
|
+
* 十有八九出在 `ooui_artifact` 上,而那是构建期产物——正确的修法是重新编译一次,
|
|
135
|
+
* 不是手工把这一串删掉(手删只会让下一次编译再把它写回来,
|
|
136
|
+
* 或者更坏:手删之后产物与声明不再对应,页面上那一块变成上一版的 DOM)。
|
|
137
|
+
*/
|
|
138
|
+
function yamlIssue(label: string, offence: HtmlOffence): string {
|
|
139
|
+
const head = (() => {
|
|
140
|
+
switch (offence.kind) {
|
|
141
|
+
case 'forbidden-tag':
|
|
142
|
+
return `含禁用标签 ${offence.detail}`;
|
|
143
|
+
case 'inline-handler':
|
|
144
|
+
return `含内联事件属性(命中 \`${offence.detail}\`)——innerHTML 挂载时这类属性会立刻执行`;
|
|
145
|
+
case 'srcdoc':
|
|
146
|
+
return `含 srcdoc 属性(命中 \`${offence.detail}\`)——等价于内联一整份文档`;
|
|
147
|
+
case 'url-scheme':
|
|
148
|
+
return `的链接/资源属性含伪协议(命中 \`${offence.detail}\`)`;
|
|
149
|
+
}
|
|
150
|
+
})();
|
|
151
|
+
return `[安全] ${label} ${head}`
|
|
152
|
+
+ '——YAML 面同样是注入面:OOUI 演绎产物的 DOM 就住在 `ooui_artifact.regions[].html` 里,'
|
|
153
|
+
+ '运行时 `installOouiHtml` 会把它整串插进锚点。\n'
|
|
154
|
+
+ ' 修法:不要手写或手改 `ooui_artifact`(它是构建期产物),把这份声明重新编译一次即可——\n'
|
|
155
|
+
+ ' compilePageDeclaration({ html, uiYaml }) # 产物随 runtimeAssets.uiYaml 出来\n'
|
|
156
|
+
+ ' 确实要在页面上放脚本或外链资源:封装成受信任的 Host 组件;'
|
|
157
|
+
+ '事件写进 canonical 页面块的四动词 Pipeline(event: { on: { click: UI_REFRESH | domain.method } })。';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 扫描声明的注入面,返回中文问题清单(空数组 = 通过)。
|
|
162
|
+
*
|
|
163
|
+
* 清单同时喂两处:发布管线用它拒绝落库,生成链闸门用它回喂模型自修复——
|
|
164
|
+
* 一把尺子量两处,规则不会各写一份而漂移。
|
|
165
|
+
*/
|
|
166
|
+
export function scanDeclarationSafety(declaration: ScannableDeclaration): string[] {
|
|
167
|
+
const issues: string[] = [];
|
|
168
|
+
const html = declaration.html ?? '';
|
|
169
|
+
|
|
170
|
+
for (const offence of findHtmlOffences(html)) issues.push(htmlIssue(offence));
|
|
85
171
|
|
|
86
172
|
const yamlFaces: Array<[string, string | undefined]> = [
|
|
87
173
|
['ui.yaml', declaration.uiYaml],
|
|
@@ -90,12 +176,16 @@ export function scanDeclarationSafety(declaration: ScannableDeclaration): string
|
|
|
90
176
|
['data.yaml', declaration.dataYaml],
|
|
91
177
|
];
|
|
92
178
|
for (const [label, source] of yamlFaces) {
|
|
93
|
-
if (source
|
|
179
|
+
if (!source) continue;
|
|
180
|
+
// 值位伪协议:YAML 里 `href: "javascript:…"` 这种写法**不带 `=`**,
|
|
181
|
+
// 所以 html 面那条属性判据看不见它——两条判据管的是同一件事的两种书写。
|
|
182
|
+
if (YAML_SCHEME_RE.test(source)) {
|
|
94
183
|
issues.push(
|
|
95
184
|
`[安全] ${label} 的字面量含伪协议(javascript: / vbscript: / data:text/html)`
|
|
96
185
|
+ '——@href/@src 绑定的默认值同样会被写进 DOM',
|
|
97
186
|
);
|
|
98
187
|
}
|
|
188
|
+
for (const offence of findHtmlOffences(source)) issues.push(yamlIssue(label, offence));
|
|
99
189
|
}
|
|
100
190
|
|
|
101
191
|
return issues;
|
|
@@ -28,7 +28,19 @@ export interface DeriveSampleResult {
|
|
|
28
28
|
gaps: SampleGap[];
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
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 {
|
|
32
44
|
if (field.example !== undefined) return clone(field.example);
|
|
33
45
|
|
|
34
46
|
if (field.type === 'object') {
|
|
@@ -36,7 +48,7 @@ function deriveField(field: SomaContractField, path: string, gaps: SampleGap[]):
|
|
|
36
48
|
if (fields && Object.keys(fields).length > 0) {
|
|
37
49
|
const out: Record<string, unknown> = {};
|
|
38
50
|
for (const [name, child] of Object.entries(fields)) {
|
|
39
|
-
const value =
|
|
51
|
+
const value = walkField(child, `${path}.${name}`, strategy);
|
|
40
52
|
if (value !== undefined) out[name] = value;
|
|
41
53
|
}
|
|
42
54
|
return out;
|
|
@@ -46,25 +58,47 @@ function deriveField(field: SomaContractField, path: string, gaps: SampleGap[]):
|
|
|
46
58
|
if (field.type === 'array') {
|
|
47
59
|
const items = field.items;
|
|
48
60
|
if (items) {
|
|
49
|
-
const element =
|
|
61
|
+
const element = walkField(items, `${path}[]`, strategy);
|
|
50
62
|
if (element !== undefined) return [element];
|
|
51
63
|
}
|
|
52
64
|
}
|
|
53
65
|
|
|
54
|
-
if (field.default !== undefined) return clone(field.default);
|
|
66
|
+
if (field.default !== undefined && strategy.useDefault(field, path)) return clone(field.default);
|
|
55
67
|
|
|
56
68
|
if (field.type === 'enum' && field.values && field.values.length > 0) {
|
|
57
69
|
// 枚举没写示例也没写默认时取第一个取值:比空串更接近真实形状,且不用作者多写一行
|
|
58
70
|
return field.values[0];
|
|
59
71
|
}
|
|
60
72
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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;
|
|
66
84
|
}
|
|
67
|
-
return
|
|
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
|
+
};
|
|
68
102
|
}
|
|
69
103
|
|
|
70
104
|
function clone<T>(value: T): T {
|
|
@@ -75,17 +109,168 @@ function clone<T>(value: T): T {
|
|
|
75
109
|
/** 从契约派生一份样张 props */
|
|
76
110
|
export function deriveContractSample(contract: SomaComponentContract): DeriveSampleResult {
|
|
77
111
|
const gaps: SampleGap[] = [];
|
|
78
|
-
const props
|
|
79
|
-
for (const [name, prop] of Object.entries(contract.props ?? {})) {
|
|
80
|
-
const value = deriveField(prop, name, gaps);
|
|
81
|
-
if (value !== undefined) props[name] = value;
|
|
82
|
-
}
|
|
112
|
+
const props = walkProps(contract, sampleStrategy(gaps));
|
|
83
113
|
return { props, gaps };
|
|
84
114
|
}
|
|
85
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
|
+
|
|
86
271
|
/** 单个字段的样张值(组件内部或工具按需调用) */
|
|
87
272
|
export function deriveFieldSample(field: SomaContractField): unknown {
|
|
88
|
-
return
|
|
273
|
+
return walkField(field, '', sampleStrategy([]));
|
|
89
274
|
}
|
|
90
275
|
|
|
91
276
|
/**
|