soma-core 1.3.2 → 1.3.3
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 +42 -0
- package/app/SomaPageClient.tsx +26 -2
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/core/build/compiler/full-manifest-builder.ts +6 -1
- package/core/build/compiler/rollup-plugin-soma.ts +6 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
- package/core/build/compiler/soma-precompile.js +3507 -2387
- package/core/build/compiler/soma-precompile.ts +31 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
- package/core/build/compiler/soma-webpack-loader.js +7 -1
- package/core/build/compiler/vite-plugin-soma.ts +11 -1
- package/core/build/component/package-loader.ts +135 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +139 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +262 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +271 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +235 -16
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +3 -3
- package/core/client/engine/render-engine.ts +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +359 -4
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +159 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/theme-engine.ts +936 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +190 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +10 -0
- package/core/server/index.ts +30 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +129 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +903 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +86 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +71 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +77 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +110 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +5341 -894
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
- package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
- package/dist/types/build/component/package-loader.d.ts +9 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +90 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +49 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +116 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/theme-engine.d.ts +49 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +36 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +5 -0
- package/dist/types/server/index.d.ts +4 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +68 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/global.d.ts +4 -0
- package/next.config.js +76 -1
- package/package.json +32 -11
- package/proxy.ts +62 -0
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/middleware.ts +0 -33
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// 前端错误采集(可观测性最小通道)。
|
|
2
|
+
//
|
|
3
|
+
// 解决的问题(doc/soma 细节与生产化强化.md §5):声明态页面可以随时发布新版本,
|
|
4
|
+
// 但线上报错时回答不了"哪个版本引入的"——错误与页面身份之间没有通道。
|
|
5
|
+
// 本模块把两件事绑在一起:①捕获运行时错误(error / unhandledrejection /
|
|
6
|
+
// 资源加载失败);②给每条错误盖上"页面身份戳"(页面名@版本 + 声明来源)。
|
|
7
|
+
//
|
|
8
|
+
// 设计取舍:
|
|
9
|
+
// - 不引入任何 APM 依赖:采集器只负责规范化与分发,上报通道由 Host 注入
|
|
10
|
+
// (可以是自己的接口、console、或第三方 SDK);
|
|
11
|
+
// - 默认不自动启用:Host 显式 install 才生效(库不擅自劫持全局钩子);
|
|
12
|
+
// - 采样与去重内置:同一错误签名在窗口期内只报一次,避免错误风暴打爆接口。
|
|
13
|
+
|
|
14
|
+
/** 页面身份:错误归因的最小事实 */
|
|
15
|
+
export interface SomaPageIdentity {
|
|
16
|
+
/** 页面名(声明仓库里的 name;文件态页面用路由路径) */
|
|
17
|
+
name: string;
|
|
18
|
+
/** 声明版本号(声明态页面才有) */
|
|
19
|
+
version?: number;
|
|
20
|
+
/** 声明来源:file=文件态四件套 / registry=声明仓库 / preview=即时预览 */
|
|
21
|
+
source?: 'file' | 'registry' | 'preview';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 规范化后的错误事件 */
|
|
25
|
+
export interface SomaErrorEvent {
|
|
26
|
+
kind: 'runtime' | 'promise' | 'resource';
|
|
27
|
+
message: string;
|
|
28
|
+
stack?: string;
|
|
29
|
+
/** 资源类错误的地址 */
|
|
30
|
+
source?: string;
|
|
31
|
+
line?: number;
|
|
32
|
+
column?: number;
|
|
33
|
+
page: SomaPageIdentity;
|
|
34
|
+
/** 事件时间(毫秒时间戳,由采集器打点) */
|
|
35
|
+
at: number;
|
|
36
|
+
/** 页面地址(去掉 hash 与 query 的路径部分,避免带出敏感参数) */
|
|
37
|
+
path?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SomaErrorReporterOptions {
|
|
41
|
+
page: SomaPageIdentity;
|
|
42
|
+
/** 上报通道:Host 注入(接口/console/第三方 SDK 皆可) */
|
|
43
|
+
report: (event: SomaErrorEvent) => void;
|
|
44
|
+
/** 同签名错误的去重窗口(毫秒,默认 60s) */
|
|
45
|
+
dedupeWindowMs?: number;
|
|
46
|
+
/** 单页最多上报条数(默认 50,防错误风暴) */
|
|
47
|
+
maxEvents?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SomaErrorReporterHandle {
|
|
51
|
+
/** 更新页面身份(发布新版本后重渲时调用) */
|
|
52
|
+
setPage(page: SomaPageIdentity): void;
|
|
53
|
+
/** 手动上报一条(业务侧捕获的异常也能进同一通道) */
|
|
54
|
+
capture(error: unknown, kind?: SomaErrorEvent['kind']): void;
|
|
55
|
+
/** 卸载全局监听 */
|
|
56
|
+
dispose(): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function safePath(): string | undefined {
|
|
60
|
+
if (typeof location === 'undefined') {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return location.pathname;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function toMessage(value: unknown): { message: string; stack?: string } {
|
|
67
|
+
if (value instanceof Error) {
|
|
68
|
+
return { message: value.message || value.name, stack: value.stack };
|
|
69
|
+
}
|
|
70
|
+
if (typeof value === 'string') {
|
|
71
|
+
return { message: value };
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
return { message: JSON.stringify(value) };
|
|
75
|
+
} catch {
|
|
76
|
+
return { message: String(value) };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 安装前端错误采集器。返回句柄用于更新页面身份与卸载。
|
|
82
|
+
*
|
|
83
|
+
* 用法(Host 侧):
|
|
84
|
+
* const reporter = installSomaErrorReporter({
|
|
85
|
+
* page: { name: 'order-console', version: 3, source: 'registry' },
|
|
86
|
+
* report: (e) => navigator.sendBeacon('/api/observability/error', JSON.stringify(e)),
|
|
87
|
+
* });
|
|
88
|
+
*/
|
|
89
|
+
export function installSomaErrorReporter(
|
|
90
|
+
options: SomaErrorReporterOptions,
|
|
91
|
+
): SomaErrorReporterHandle {
|
|
92
|
+
let page = options.page;
|
|
93
|
+
const dedupeWindowMs = options.dedupeWindowMs ?? 60_000;
|
|
94
|
+
const maxEvents = options.maxEvents ?? 50;
|
|
95
|
+
const seen = new Map<string, number>();
|
|
96
|
+
let sent = 0;
|
|
97
|
+
|
|
98
|
+
const emit = (event: SomaErrorEvent): void => {
|
|
99
|
+
if (sent >= maxEvents) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const signature = `${event.kind}|${event.message}|${event.source ?? ''}|${event.line ?? ''}`;
|
|
103
|
+
const now = event.at;
|
|
104
|
+
const last = seen.get(signature);
|
|
105
|
+
if (last !== undefined && now - last < dedupeWindowMs) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
seen.set(signature, now);
|
|
109
|
+
sent += 1;
|
|
110
|
+
try {
|
|
111
|
+
options.report(event);
|
|
112
|
+
} catch {
|
|
113
|
+
// 上报通道自身异常绝不能影响页面
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const stamp = (partial: Omit<SomaErrorEvent, 'page' | 'at' | 'path'>): SomaErrorEvent => ({
|
|
118
|
+
...partial,
|
|
119
|
+
page,
|
|
120
|
+
at: Date.now(),
|
|
121
|
+
path: safePath(),
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const onError = (event: ErrorEvent): void => {
|
|
125
|
+
// 资源加载失败:target 是元素而非 window,且没有 error 对象
|
|
126
|
+
const target = event.target as (HTMLElement & { src?: string; href?: string }) | null;
|
|
127
|
+
if (target && target !== (globalThis as unknown as EventTarget) && (target.src || target.href)) {
|
|
128
|
+
emit(stamp({
|
|
129
|
+
kind: 'resource',
|
|
130
|
+
message: `资源加载失败: ${target.tagName.toLowerCase()}`,
|
|
131
|
+
source: target.src ?? target.href,
|
|
132
|
+
}));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const { message, stack } = toMessage(event.error ?? event.message);
|
|
136
|
+
emit(stamp({
|
|
137
|
+
kind: 'runtime',
|
|
138
|
+
message,
|
|
139
|
+
stack,
|
|
140
|
+
source: event.filename,
|
|
141
|
+
line: event.lineno,
|
|
142
|
+
column: event.colno,
|
|
143
|
+
}));
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const onRejection = (event: PromiseRejectionEvent): void => {
|
|
147
|
+
const { message, stack } = toMessage(event.reason);
|
|
148
|
+
emit(stamp({ kind: 'promise', message, stack }));
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
if (typeof window !== 'undefined') {
|
|
152
|
+
// 资源错误不冒泡,必须用捕获阶段
|
|
153
|
+
window.addEventListener('error', onError, true);
|
|
154
|
+
window.addEventListener('unhandledrejection', onRejection);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
setPage(next: SomaPageIdentity): void {
|
|
159
|
+
page = next;
|
|
160
|
+
},
|
|
161
|
+
capture(error: unknown, kind: SomaErrorEvent['kind'] = 'runtime'): void {
|
|
162
|
+
const { message, stack } = toMessage(error);
|
|
163
|
+
emit(stamp({ kind, message, stack }));
|
|
164
|
+
},
|
|
165
|
+
dispose(): void {
|
|
166
|
+
if (typeof window !== 'undefined') {
|
|
167
|
+
window.removeEventListener('error', onError, true);
|
|
168
|
+
window.removeEventListener('unhandledrejection', onRejection);
|
|
169
|
+
}
|
|
170
|
+
seen.clear();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// core/client/parser/adaptive-parser.ts — `_ui.yaml` 顶层 `adaptive` 键的解析与解析期校验
|
|
2
|
+
//
|
|
3
|
+
// F-07 声明语法 / F-08 解析期校验 / F-09 词汇注册契约。
|
|
4
|
+
//
|
|
5
|
+
// 失败策略:快速失败(抛错)而非降级忽略。
|
|
6
|
+
// 理由:阶梯是版式承诺,静默忽略会让页面以"看着正常但不适配"的状态上线——
|
|
7
|
+
// 那比报错更难发现,也更难归因。
|
|
8
|
+
//
|
|
9
|
+
// 边界:本模块不校验级序在语义上是否合理(那是评审项),只校验声明值是否在注册词汇表内。
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
SOMA_ADAPTIVE_MAX_LADDER,
|
|
13
|
+
SOMA_ADAPTIVE_PROFILES,
|
|
14
|
+
type SomaAdaptiveConfig,
|
|
15
|
+
type SomaAdaptiveLevel,
|
|
16
|
+
type SomaAdaptiveLevelDefinition,
|
|
17
|
+
type SomaAdaptiveProfile,
|
|
18
|
+
type SomaAdaptiveVocabulary,
|
|
19
|
+
} from '../../shared/types/adaptive.types';
|
|
20
|
+
|
|
21
|
+
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
/** 阶梯声明错误(ERR-02):携带违规位置,文案必须给出合法取值或接线点 */
|
|
24
|
+
export class SomaAdaptiveConfigError extends Error {
|
|
25
|
+
public readonly path: string;
|
|
26
|
+
|
|
27
|
+
constructor(message: string, path: string = '') {
|
|
28
|
+
const prefix = path ? ` (路径: ${path})` : '';
|
|
29
|
+
super(`[SOMA AdaptiveParser] ${message}${prefix}`);
|
|
30
|
+
this.name = 'SomaAdaptiveConfigError';
|
|
31
|
+
this.path = path;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 词汇注册接线点,报错时原样指路——让人一眼知道该去哪儿补 */
|
|
36
|
+
const VOCABULARY_WIRING_HINT =
|
|
37
|
+
'在宿主 bootstrap/soma-bootstrap.ts 中与组件注册同点注入:'
|
|
38
|
+
+ 'adaptiveVocabulary: createSomaUiAdaptiveVocabulary()';
|
|
39
|
+
|
|
40
|
+
// ─── 词汇注册(F-09) ─────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 合并多份词汇来源为一张词汇表。
|
|
44
|
+
*
|
|
45
|
+
* 同名级语义被注册两次即显式冲突报错,不做"后者覆盖前者"——
|
|
46
|
+
* 静默覆盖会让"实际生效的级语义"取决于注册顺序,那是第二份事实。
|
|
47
|
+
*/
|
|
48
|
+
export function createAdaptiveVocabulary(
|
|
49
|
+
...sources: SomaAdaptiveVocabulary[]
|
|
50
|
+
): SomaAdaptiveVocabulary {
|
|
51
|
+
const merged: Record<string, Record<string, SomaAdaptiveLevelDefinition>> = {};
|
|
52
|
+
|
|
53
|
+
for (const source of sources) {
|
|
54
|
+
for (const [slot, levels] of Object.entries(source ?? {})) {
|
|
55
|
+
const bucket = merged[slot] ?? (merged[slot] = {});
|
|
56
|
+
for (const [value, definition] of Object.entries(levels ?? {})) {
|
|
57
|
+
if (value in bucket) {
|
|
58
|
+
throw new SomaAdaptiveConfigError(
|
|
59
|
+
`阶梯词汇冲突:级语义 "${slot}: ${value}" 被注册了两次。`
|
|
60
|
+
+ `同名级只允许有一份定义,请在注册侧去重后再注入`,
|
|
61
|
+
`adaptiveVocabulary.${slot}.${value}`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
bucket[value] = definition;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return merged;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ─── 内部校验 ─────────────────────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
function assertPlainObject(raw: unknown, what: string, path: string): Record<string, unknown> {
|
|
75
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
76
|
+
throw new SomaAdaptiveConfigError(
|
|
77
|
+
`${what} 必须是对象,当前值: ${JSON.stringify(raw)}`,
|
|
78
|
+
path,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return raw as Record<string, unknown>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function parseProfile(raw: unknown, path: string): SomaAdaptiveProfile {
|
|
85
|
+
if (typeof raw !== 'string' || !raw.trim()) {
|
|
86
|
+
throw new SomaAdaptiveConfigError(
|
|
87
|
+
`profile 必须是非空字符串,合法取值: ${SOMA_ADAPTIVE_PROFILES.join(' | ')}`,
|
|
88
|
+
path,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (!(SOMA_ADAPTIVE_PROFILES as readonly string[]).includes(raw)) {
|
|
92
|
+
throw new SomaAdaptiveConfigError(
|
|
93
|
+
`未知的 profile "${raw}",合法取值: ${SOMA_ADAPTIVE_PROFILES.join(' | ')}`,
|
|
94
|
+
path,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return raw as SomaAdaptiveProfile;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 单级:YAML 上是单键对象 `{ 槽名: 级值 }`;多键即歧义(哪个是这一级?),直接非法 */
|
|
101
|
+
function parseLevel(raw: unknown, path: string): SomaAdaptiveLevel {
|
|
102
|
+
const obj = assertPlainObject(raw, '阶梯的每一级', path);
|
|
103
|
+
const keys = Object.keys(obj);
|
|
104
|
+
if (keys.length !== 1) {
|
|
105
|
+
throw new SomaAdaptiveConfigError(
|
|
106
|
+
`阶梯的每一级必须是单键对象 { 槽名: 级值 },当前有 ${keys.length} 个键: ${keys.join(', ')}`,
|
|
107
|
+
path,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const slot = keys[0];
|
|
112
|
+
const value = obj[slot];
|
|
113
|
+
if (!slot.trim()) {
|
|
114
|
+
throw new SomaAdaptiveConfigError(`槽名不能为空`, path);
|
|
115
|
+
}
|
|
116
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
117
|
+
throw new SomaAdaptiveConfigError(
|
|
118
|
+
`级值必须是非空字符串,当前值: ${JSON.stringify(value)}`
|
|
119
|
+
+ `(含 → 等符号的值须按 YAML 引号纪律加双引号,如 cols: "4→2→1")`,
|
|
120
|
+
`${path}.${slot}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { slot, value };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseLadder(
|
|
128
|
+
raw: unknown,
|
|
129
|
+
vocabulary: SomaAdaptiveVocabulary | undefined,
|
|
130
|
+
path: string,
|
|
131
|
+
vocabularyRequired: boolean,
|
|
132
|
+
): SomaAdaptiveLevel[] {
|
|
133
|
+
if (!Array.isArray(raw)) {
|
|
134
|
+
throw new SomaAdaptiveConfigError(
|
|
135
|
+
`ladder 必须是数组(级序),当前类型: ${typeof raw}`,
|
|
136
|
+
path,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
if (raw.length > SOMA_ADAPTIVE_MAX_LADDER) {
|
|
140
|
+
throw new SomaAdaptiveConfigError(
|
|
141
|
+
`阶梯级数 ${raw.length} 超过上限 ${SOMA_ADAPTIVE_MAX_LADDER}:`
|
|
142
|
+
+ `超过 ${SOMA_ADAPTIVE_MAX_LADDER} 级说明页面信息过载,应拆页而不是加级`,
|
|
143
|
+
path,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const levels = raw.map((item, index) => parseLevel(item, `${path}[${index}]`));
|
|
148
|
+
|
|
149
|
+
// 词汇校验:未注册词汇表而页面声明了 ladder,视为词汇未知(F-09)
|
|
150
|
+
if (levels.length > 0 && !vocabulary) {
|
|
151
|
+
if (vocabularyRequired) {
|
|
152
|
+
throw new SomaAdaptiveConfigError(
|
|
153
|
+
`页面声明了 adaptive.ladder,但宿主未注册阶梯词汇表。${VOCABULARY_WIRING_HINT}`,
|
|
154
|
+
path,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return levels; // 构建期预编译:词汇归属推迟到 compileAdaptive
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
levels.forEach((level, index) => {
|
|
161
|
+
const bucket = vocabulary?.[level.slot];
|
|
162
|
+
if (!bucket) {
|
|
163
|
+
const known = Object.keys(vocabulary ?? {});
|
|
164
|
+
throw new SomaAdaptiveConfigError(
|
|
165
|
+
`未注册的阶梯槽 "${level.slot}",已注册槽: ${known.length ? known.join(' | ') : '(空)'}。`
|
|
166
|
+
+ VOCABULARY_WIRING_HINT,
|
|
167
|
+
`${path}[${index}]`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (!(level.value in bucket)) {
|
|
171
|
+
throw new SomaAdaptiveConfigError(
|
|
172
|
+
`槽 "${level.slot}" 不认识级值 "${level.value}",`
|
|
173
|
+
+ `合法取值: ${Object.keys(bucket).join(' | ')}`,
|
|
174
|
+
`${path}[${index}].${level.slot}`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return levels;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function parsePin(raw: unknown, path: string): string[] {
|
|
183
|
+
if (!Array.isArray(raw)) {
|
|
184
|
+
throw new SomaAdaptiveConfigError(
|
|
185
|
+
`pin 必须是数组(豁免槽锚点名),当前类型: ${typeof raw}`,
|
|
186
|
+
path,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const pinned: string[] = [];
|
|
191
|
+
raw.forEach((item, index) => {
|
|
192
|
+
if (typeof item !== 'string' || !item.trim()) {
|
|
193
|
+
throw new SomaAdaptiveConfigError(
|
|
194
|
+
`pin 条目必须是非空锚点名,当前值: ${JSON.stringify(item)}`,
|
|
195
|
+
`${path}[${index}]`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (pinned.includes(item)) {
|
|
199
|
+
throw new SomaAdaptiveConfigError(`pin 条目重复: "${item}"`, `${path}[${index}]`);
|
|
200
|
+
}
|
|
201
|
+
pinned.push(item);
|
|
202
|
+
});
|
|
203
|
+
return pinned;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ─── 公共 API ─────────────────────────────────────────────────────────────────
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 解析并校验 `_ui.yaml` 顶层 `adaptive` 键。
|
|
210
|
+
*
|
|
211
|
+
* 解析期四项快速失败校验(F-08):
|
|
212
|
+
* 1. 级数 ≤ 5;
|
|
213
|
+
* 2. 每一级的槽与级值 ∈ 已注册词汇表(词汇未注册 = 词汇未知);
|
|
214
|
+
* 3. pin 条目合法且不重复;
|
|
215
|
+
* 4. pin 与 ladder 冲突(同槽既被钉住又被列入降级序)即非法。
|
|
216
|
+
*
|
|
217
|
+
* `pin` 与 ladder 槽的**锚点存在性**不在此校验——解析期看不到 DOM,
|
|
218
|
+
* 那一项在编译期做(见 adaptive-compile.ts),归属仍是 F-08。
|
|
219
|
+
*/
|
|
220
|
+
export function parseAdaptiveConfig(
|
|
221
|
+
raw: unknown,
|
|
222
|
+
vocabulary?: SomaAdaptiveVocabulary,
|
|
223
|
+
path: string = 'adaptive',
|
|
224
|
+
options?: { vocabularyRequired?: boolean },
|
|
225
|
+
): SomaAdaptiveConfig {
|
|
226
|
+
const vocabularyRequired = options?.vocabularyRequired !== false;
|
|
227
|
+
const obj = assertPlainObject(raw, 'adaptive', path);
|
|
228
|
+
|
|
229
|
+
// profile 缺席而 ladder 存在:声明非法(ERR-02)。
|
|
230
|
+
// 先于 profile 自身的报错判定,因为这一条的修复方向不同(补 profile 或删 ladder)。
|
|
231
|
+
if (!('profile' in obj) && 'ladder' in obj) {
|
|
232
|
+
throw new SomaAdaptiveConfigError(
|
|
233
|
+
`声明了 ladder 却缺少 profile:阶梯必须挂在一个版式画像上,`
|
|
234
|
+
+ `请补 profile(${SOMA_ADAPTIVE_PROFILES.join(' | ')})或删除 ladder`,
|
|
235
|
+
path,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const config: SomaAdaptiveConfig = {
|
|
240
|
+
profile: parseProfile(obj.profile, `${path}.profile`),
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
if ('ladder' in obj && obj.ladder !== undefined) {
|
|
244
|
+
config.ladder = parseLadder(obj.ladder, vocabulary, `${path}.ladder`, vocabularyRequired);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if ('pin' in obj && obj.pin !== undefined) {
|
|
248
|
+
config.pin = parsePin(obj.pin, `${path}.pin`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (config.pin && config.ladder) {
|
|
252
|
+
for (const slot of config.pin) {
|
|
253
|
+
if (config.ladder.some((level) => level.slot === slot)) {
|
|
254
|
+
throw new SomaAdaptiveConfigError(
|
|
255
|
+
`槽 "${slot}" 同时出现在 pin 与 ladder 中:钉住与降级互斥,`
|
|
256
|
+
+ `请二选一(要豁免就从 ladder 移除,要降级就从 pin 移除)`,
|
|
257
|
+
`${path}.pin`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return config;
|
|
264
|
+
}
|