soma-core 1.3.1 → 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 +39 -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 +43 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +389 -5
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/infra/http/SomaHttpService.ts +5 -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/scheduler/SomaScheduler.ts +29 -1
- 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 +5388 -888
- 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 +26 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +122 -0
- package/dist/types/client/infra/http/SomaHttpService.d.ts +2 -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/scheduler/SomaScheduler.d.ts +8 -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 +12 -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,262 @@
|
|
|
1
|
+
// AI 组件目录生成器:契约 → AI 可查表的组件目录(契约编译器"一源五出"的产出面之一)。
|
|
2
|
+
//
|
|
3
|
+
// 同一份契约同时产出:
|
|
4
|
+
// - JSON:结构化目录,供程序侧(soma-ai 的工具调用、校验器)消费;
|
|
5
|
+
// - Markdown:喂给 AI 上下文的查表文档——AI 生成页面前先读它选组件、抄用法。
|
|
6
|
+
//
|
|
7
|
+
// 纪律(D10):目录永远从契约生成,禁止手工维护第二份副本。
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
SomaComponentContract,
|
|
11
|
+
SomaGeometryFamily,
|
|
12
|
+
SomaContractProp,
|
|
13
|
+
} from '../../shared/types/component-contract.types';
|
|
14
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
15
|
+
|
|
16
|
+
export interface SomaAiCatalogEntry {
|
|
17
|
+
name: string;
|
|
18
|
+
layer: string;
|
|
19
|
+
version: string;
|
|
20
|
+
description: string;
|
|
21
|
+
/** 组件主几何族;由契约字段提供,旧组件由编译期迁移推导补齐。 */
|
|
22
|
+
geometry: SomaGeometryFamily;
|
|
23
|
+
props: Array<{
|
|
24
|
+
name: string;
|
|
25
|
+
type: string;
|
|
26
|
+
required: boolean;
|
|
27
|
+
default?: unknown;
|
|
28
|
+
values?: (string | number)[];
|
|
29
|
+
mode: 'oneway' | 'model';
|
|
30
|
+
desc: string;
|
|
31
|
+
}>;
|
|
32
|
+
emits: Array<{
|
|
33
|
+
name: string;
|
|
34
|
+
payload: Array<{ name: string; type: string; desc: string }>;
|
|
35
|
+
desc: string;
|
|
36
|
+
}>;
|
|
37
|
+
slots: Array<{ name: string; required: boolean; desc: string }>;
|
|
38
|
+
ai?: {
|
|
39
|
+
whenToUse: string;
|
|
40
|
+
whenNotToUse?: string;
|
|
41
|
+
example: string;
|
|
42
|
+
antiPatterns?: string[];
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 旧组件契约的迁移归族。
|
|
48
|
+
*
|
|
49
|
+
* 这是目录编译器中的一次性兼容层,不是生成链的第二份布局真源:一旦组件契约
|
|
50
|
+
* 写入 geometry,显式值永远优先。名称表只覆盖几何语义明显的公共骨架/覆盖层/消息
|
|
51
|
+
* 组件,剩余 composite/solution 默认归 surface,保证生成链不会把未知组件裸放。
|
|
52
|
+
*/
|
|
53
|
+
export function inferGeometryFamily(contract: Pick<SomaComponentContract, 'name' | 'layer'>): SomaGeometryFamily {
|
|
54
|
+
const name = contract.name;
|
|
55
|
+
if (/^(Alert|Toast|Collapse|Accordion|ResultPanel|ValidationMessage|NotificationCenter)$/.test(name)) {
|
|
56
|
+
return 'message';
|
|
57
|
+
}
|
|
58
|
+
if (/(Modal|Drawer|Popover|Tooltip|Dropdown|TourGuide|CommandPalette)$/.test(name)) {
|
|
59
|
+
return 'overlay';
|
|
60
|
+
}
|
|
61
|
+
if (/^(PageFrame|AppShell|LandingConsole|ResponsiveGrid|StackLayout|AdaptiveSurface|Splitter|Dock|Navbar|NavMenu|MenuBar|MegaMenu)$/.test(name)) {
|
|
62
|
+
return 'layout';
|
|
63
|
+
}
|
|
64
|
+
if (/(Table|Chart|Graph|Calendar|Timeline|KpiBoard|Stats|Progress|Gauge|HeatMap|Scatter|Gantt|Kanban|Data|List|Description)$/.test(name)) {
|
|
65
|
+
return 'data';
|
|
66
|
+
}
|
|
67
|
+
if (contract.layer === 'atom' || /^(Button|TextInput|NumberInput|Select|Checkbox|Radio|Switch|Slider|Upload|DatePicker|TimePicker|Cascader|Combobox|TagInput|QtyStepper)$/.test(name)) {
|
|
68
|
+
return 'control';
|
|
69
|
+
}
|
|
70
|
+
if (/^(Avatar|Badge|Icon|Divider|Link|Kbd|Status|Indicator|PriceText|Loading|Skeleton|Mask)$/.test(name)) {
|
|
71
|
+
return 'display';
|
|
72
|
+
}
|
|
73
|
+
return 'surface';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface SomaAiCatalog {
|
|
77
|
+
generatedFrom: string[];
|
|
78
|
+
entries: SomaAiCatalogEntry[];
|
|
79
|
+
markdown: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function buildCatalogEntry(contract: SomaComponentContract): SomaAiCatalogEntry {
|
|
83
|
+
return {
|
|
84
|
+
name: contract.name,
|
|
85
|
+
layer: contract.layer,
|
|
86
|
+
version: contract.version,
|
|
87
|
+
description: contract.description,
|
|
88
|
+
geometry: contract.geometry ?? inferGeometryFamily(contract),
|
|
89
|
+
props: Object.entries(contract.props ?? {}).map(([name, prop]) => ({
|
|
90
|
+
name,
|
|
91
|
+
type: propTypeLabel(prop),
|
|
92
|
+
required: prop.required === true,
|
|
93
|
+
default: prop.default,
|
|
94
|
+
values: prop.values,
|
|
95
|
+
mode: prop.mode ?? 'oneway',
|
|
96
|
+
desc: prop.desc ?? '',
|
|
97
|
+
})),
|
|
98
|
+
emits: Object.entries(contract.emits ?? {}).map(([name, emit]) => ({
|
|
99
|
+
name,
|
|
100
|
+
payload: Object.entries(emit.payload ?? {}).map(([field, shape]) => ({
|
|
101
|
+
name: field,
|
|
102
|
+
type: shape.type,
|
|
103
|
+
desc: shape.desc ?? '',
|
|
104
|
+
})),
|
|
105
|
+
desc: emit.desc ?? '',
|
|
106
|
+
})),
|
|
107
|
+
slots: Object.entries(contract.slots ?? {}).map(([name, slot]) => ({
|
|
108
|
+
name,
|
|
109
|
+
required: slot.required === true,
|
|
110
|
+
desc: slot.desc ?? '',
|
|
111
|
+
})),
|
|
112
|
+
ai: contract.ai
|
|
113
|
+
? {
|
|
114
|
+
whenToUse: contract.ai.when_to_use,
|
|
115
|
+
whenNotToUse: contract.ai.when_not_to_use,
|
|
116
|
+
example: contract.ai.example,
|
|
117
|
+
antiPatterns: contract.ai.anti_patterns,
|
|
118
|
+
}
|
|
119
|
+
: undefined,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface GenerateAiCatalogOptions {
|
|
124
|
+
/** 严格模式(默认 true):缺 ai 段的契约视为目录质量线不达标,直接抛错 */
|
|
125
|
+
strict?: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* 设计规范正文(Markdown 片段,可选)。
|
|
128
|
+
*
|
|
129
|
+
* 传入后会被渲染成目录开头的「版式与视觉规范」章节,排在组件清单之前——
|
|
130
|
+
* 让"怎么排版才好看"和"有哪些组件"成为同一份通用资产,任何 AI 只要读目录
|
|
131
|
+
* 就同时拿到选型知识与美观知识,无需调用方再打提示词补丁。
|
|
132
|
+
*
|
|
133
|
+
* 约定:正文内部的小标题请用 `###` 起步(`##` 由本函数产出,避免抢占章节层级)。
|
|
134
|
+
* 不传则目录产物与历史完全一致。
|
|
135
|
+
*/
|
|
136
|
+
conventions?: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 生成完整 AI 目录。
|
|
141
|
+
* strict 模式(默认)下缺少 ai 段的组件视为违反目录质量线(契约规则 C1 的目录侧),抛错。
|
|
142
|
+
*/
|
|
143
|
+
export function generateAiCatalog(
|
|
144
|
+
defs: SomaComponentDefinition[] | Record<string, SomaComponentDefinition>,
|
|
145
|
+
options?: GenerateAiCatalogOptions,
|
|
146
|
+
): SomaAiCatalog {
|
|
147
|
+
const list = Array.isArray(defs) ? defs : Object.values(defs);
|
|
148
|
+
const strict = options?.strict !== false;
|
|
149
|
+
|
|
150
|
+
const entries = list.map((def) => {
|
|
151
|
+
if (strict && !def.contract.ai) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`[generateAiCatalog] 组件 "${def.contract.name}" 缺少契约 ai 段(when_to_use/example),目录质量线不达标`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return buildCatalogEntry(def.contract);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
entries.sort((a, b) => a.layer.localeCompare(b.layer) || a.name.localeCompare(b.name));
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
generatedFrom: entries.map((entry) => `${entry.name}@${entry.version}`),
|
|
163
|
+
entries,
|
|
164
|
+
markdown: renderMarkdown(entries, options?.conventions),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function propTypeLabel(prop: SomaContractProp): string {
|
|
169
|
+
if (prop.type === 'enum' && prop.values?.length) {
|
|
170
|
+
return prop.values.join(' | ');
|
|
171
|
+
}
|
|
172
|
+
return prop.type;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 设计规范章节标题:固定文案,供调用方与测试稳定断言 */
|
|
176
|
+
const CONVENTIONS_HEADING = '## 版式与视觉规范(生成页面时必须遵守)';
|
|
177
|
+
|
|
178
|
+
function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): string {
|
|
179
|
+
const lines: string[] = [
|
|
180
|
+
'# SOMA 组件目录(AI 查表用,契约自动生成 — 禁止手改)',
|
|
181
|
+
'',
|
|
182
|
+
'生成页面前先在此选组件;`props` 只能传表中列出的名字,`emit` 订阅写法见各组件示例。',
|
|
183
|
+
'',
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
// 设计规范前置:排在组件清单之前——先立版式底线,再谈选哪个组件
|
|
187
|
+
const guide = conventions?.trim();
|
|
188
|
+
if (guide) {
|
|
189
|
+
lines.push(
|
|
190
|
+
CONVENTIONS_HEADING,
|
|
191
|
+
'',
|
|
192
|
+
guide,
|
|
193
|
+
'',
|
|
194
|
+
'---',
|
|
195
|
+
'',
|
|
196
|
+
`以下是全部可用组件(共 ${entries.length} 个),每个组件一节:`,
|
|
197
|
+
'',
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (const entry of entries) {
|
|
202
|
+
lines.push(`## ${entry.name} \`${entry.layer}\` v${entry.version}`);
|
|
203
|
+
lines.push('');
|
|
204
|
+
lines.push(entry.description);
|
|
205
|
+
lines.push(`几何族:\`${entry.geometry}\``);
|
|
206
|
+
if (entry.ai) {
|
|
207
|
+
lines.push('');
|
|
208
|
+
lines.push(`**何时使用**:${entry.ai.whenToUse}`);
|
|
209
|
+
if (entry.ai.whenNotToUse) {
|
|
210
|
+
lines.push(`**何时不用**:${entry.ai.whenNotToUse}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (entry.props.length) {
|
|
215
|
+
lines.push('');
|
|
216
|
+
lines.push('| prop | 类型 | 必填 | 默认 | 说明 |');
|
|
217
|
+
lines.push('|---|---|---|---|---|');
|
|
218
|
+
for (const prop of entry.props) {
|
|
219
|
+
const dft = prop.required ? '—' : JSON.stringify(prop.default ?? null);
|
|
220
|
+
const mode = prop.mode === 'model' ? '(双向)' : '';
|
|
221
|
+
lines.push(`| ${prop.name} | ${prop.type} | ${prop.required ? '是' : '否'} | ${dft} | ${prop.desc}${mode} |`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (entry.emits.length) {
|
|
226
|
+
lines.push('');
|
|
227
|
+
lines.push('| emit | payload | 说明 |');
|
|
228
|
+
lines.push('|---|---|---|');
|
|
229
|
+
for (const emit of entry.emits) {
|
|
230
|
+
const payload = emit.payload.map((f) => `${f.name}: ${f.type}`).join(', ') || '—';
|
|
231
|
+
lines.push(`| ${emit.name} | { ${payload} } | ${emit.desc} |`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (entry.slots.length) {
|
|
236
|
+
lines.push('');
|
|
237
|
+
lines.push(
|
|
238
|
+
`插槽:${entry.slots.map((s) => `\`${s.name}\`${s.required ? '(必填)' : ''} ${s.desc}`).join(';')}`,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (entry.ai?.example) {
|
|
243
|
+
lines.push('');
|
|
244
|
+
lines.push('用法:');
|
|
245
|
+
lines.push('```');
|
|
246
|
+
lines.push(entry.ai.example.trimEnd());
|
|
247
|
+
lines.push('```');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (entry.ai?.antiPatterns?.length) {
|
|
251
|
+
lines.push('');
|
|
252
|
+
lines.push('反模式:');
|
|
253
|
+
for (const anti of entry.ai.antiPatterns) {
|
|
254
|
+
lines.push(`- ${anti}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
lines.push('');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return lines.join('\n');
|
|
262
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// 组件运行时定义 — 组件包在客户端的可执行形态。
|
|
2
|
+
//
|
|
3
|
+
// P1 契约编译器落地前,组件包以本结构直接提供(TS 常量/模块导出);
|
|
4
|
+
// P1 后由编译器从 contract.yaml + template.html + ui.yaml 编译产出同构对象。
|
|
5
|
+
|
|
6
|
+
import type { UiConfig } from '../../shared/types/ui-config.types';
|
|
7
|
+
import type { ParsedEvent } from '../../shared/types/event.types';
|
|
8
|
+
import type { SomaComponentContract } from '../../shared/types/component-contract.types';
|
|
9
|
+
|
|
10
|
+
/** 控制器工厂上下文:props 即实例作用域根(可读取最新值),emit 向页面上抛 */
|
|
11
|
+
export interface SomaComponentControllerContext {
|
|
12
|
+
props: Record<string, unknown>;
|
|
13
|
+
emit: (name: string, payload?: unknown) => void;
|
|
14
|
+
host: HTMLElement;
|
|
15
|
+
/**
|
|
16
|
+
* 注入服务(仅自治组件,契约规范第 10 章):
|
|
17
|
+
* 按定义的 services 白名单过滤后的页面运行时服务子集(http/auth/notify/router…),
|
|
18
|
+
* 白名单外的服务不可见——组件包可信分发的安全边界。
|
|
19
|
+
*/
|
|
20
|
+
services?: Record<string, unknown>;
|
|
21
|
+
/** 请求实例重渲(控制器异步状态变更后调用,如 onMount 拉数据完成) */
|
|
22
|
+
refresh: () => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 组件控制器:组件内部行为逻辑(边界判断、派生 getter 等)。
|
|
27
|
+
* 实例作用域根上以保留键 `ctrl` 暴露:
|
|
28
|
+
* - 内部 event.yaml 以 `ctrl.method` 调用方法;
|
|
29
|
+
* - 内部 ui.yaml 以 `ctrl.getter` 绑定派生值(契约状态机的 P0 承载形态)。
|
|
30
|
+
*
|
|
31
|
+
* 生命周期约定(自治组件):控制器可暴露 `onPropsChange(): void`——props 下行更新后
|
|
32
|
+
* 触发(浮层的焦点陷阱、滚动锁定这类副作用靠它),以及 `onMount(): void | Promise<void>`
|
|
33
|
+
* 与 `onUnmount(): void`——实例引擎就绪后调用 onMount(异步完成后自动重渲),
|
|
34
|
+
* 实例销毁时调用 onUnmount(清理订阅/定时器)。
|
|
35
|
+
*/
|
|
36
|
+
export type SomaComponentControllerFactory = (
|
|
37
|
+
ctx: SomaComponentControllerContext,
|
|
38
|
+
) => Record<string, unknown>;
|
|
39
|
+
|
|
40
|
+
export interface SomaComponentDefinition {
|
|
41
|
+
contract: SomaComponentContract;
|
|
42
|
+
/**
|
|
43
|
+
* 组件内部结构模板。内部锚点写 data-soma-id(与页面 DSL 一致),
|
|
44
|
+
* 展开器统一改写为 data-soma-scoped-id 实现作用域隔离。
|
|
45
|
+
* 插槽出口:<div data-soma-slot-outlet="插槽名"></div>
|
|
46
|
+
*/
|
|
47
|
+
templateHtml: string;
|
|
48
|
+
/**
|
|
49
|
+
* 内部绑定声明。作用域根为 props(约定 bind: { obj: '__component', var: 'p' }),
|
|
50
|
+
* 路径 `p.xxx` 读 prop,`ctrl.xxx` 读控制器派生值。
|
|
51
|
+
*/
|
|
52
|
+
uiConfig?: UiConfig;
|
|
53
|
+
/** 内部事件声明(针对内部锚点;funcPath 以 ctrl. 开头调用控制器) */
|
|
54
|
+
eventConfig?: ParsedEvent[];
|
|
55
|
+
controllerFactory?: SomaComponentControllerFactory;
|
|
56
|
+
/**
|
|
57
|
+
* 自治组件的服务注入白名单(契约规范第 10 章):
|
|
58
|
+
* 声明用到的页面运行时服务名(http/auth/notify/router/storage/i18n/permission…),
|
|
59
|
+
* 未声明的服务不注入。不声明本字段 = 非自治组件,ctx.services 为 undefined。
|
|
60
|
+
*/
|
|
61
|
+
services?: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 保留键:实例作用域根上暴露控制器的字段名,契约禁止同名 prop */
|
|
65
|
+
export const SOMA_COMPONENT_CTRL_KEY = 'ctrl';
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// 组件展开器:把组件模板注入宿主元素,完成锚点作用域化与插槽投影。
|
|
2
|
+
//
|
|
3
|
+
// 展开发生在页面引擎构建之前(静态实例)或行克隆渲染时(行内实例),
|
|
4
|
+
// 展开后:
|
|
5
|
+
// - 组件内部锚点 data-soma-id → data-soma-scoped-id + data-soma-scope=<本次 scopeId>
|
|
6
|
+
// (页面引擎不可见;scope 标记让父/子实例的锚点在同一子树里也能分清归属);
|
|
7
|
+
// - 页面插槽内容(<template data-soma-slot>)移入组件出口,保留页面锚点归页面作用域;
|
|
8
|
+
// - 宿主元素自身的 data-soma-id / class 不变(页面继续用它做 when/样式/事件寻址)。
|
|
9
|
+
|
|
10
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
11
|
+
import { SOMA_SCOPED_ID_ATTR, SOMA_SCOPE_ATTR } from './scoped-adapter';
|
|
12
|
+
|
|
13
|
+
export const SOMA_COMPONENT_ATTR = 'data-soma-component';
|
|
14
|
+
/**
|
|
15
|
+
* 展开完成标记;**值为该次展开分配的 scopeId**(此前是空串)。
|
|
16
|
+
* 只用 hasAttribute / `[data-soma-component-ready]` 判定的调用方不受影响。
|
|
17
|
+
* 之所以把 scopeId 挂在这里而不是新加一个宿主属性:宿主本身还是**父**作用域的锚点,
|
|
18
|
+
* 已经带着父的 data-soma-scope,两个"作用域"含义挂在同一个属性上必然要出事。
|
|
19
|
+
*/
|
|
20
|
+
export const SOMA_COMPONENT_READY_ATTR = 'data-soma-component-ready';
|
|
21
|
+
export const SOMA_SLOT_ATTR = 'data-soma-slot';
|
|
22
|
+
export const SOMA_SLOT_OUTLET_ATTR = 'data-soma-slot-outlet';
|
|
23
|
+
/** 组件模板内部的子组件局部 id;页面组件管理器忽略,由父实例运行时接管。 */
|
|
24
|
+
export const SOMA_CHILD_COMPONENT_ID_ATTR = 'data-soma-child-id';
|
|
25
|
+
|
|
26
|
+
export class SomaComponentExpandError extends Error {
|
|
27
|
+
constructor(message: string) {
|
|
28
|
+
super(`[SomaComponentExpander] ${message}`);
|
|
29
|
+
this.name = 'SomaComponentExpandError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 作用域序号:进程内单调递增,保证同一页面上任意两次展开的 scopeId 不同。 */
|
|
34
|
+
let scopeSeq = 0;
|
|
35
|
+
|
|
36
|
+
export class SomaComponentExpander {
|
|
37
|
+
/** 宿主是否已展开(行内实例复用节点时避免重复展开) */
|
|
38
|
+
isExpanded(host: HTMLElement): boolean {
|
|
39
|
+
return host.hasAttribute(SOMA_COMPONENT_READY_ATTR);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 已展开宿主的 scopeId(未展开返回 null) */
|
|
43
|
+
scopeIdOf(host: HTMLElement): string | null {
|
|
44
|
+
return host.getAttribute(SOMA_COMPONENT_READY_ATTR);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @returns 本次展开分配的 scopeId(宿主已展开时返回既有的那个) */
|
|
48
|
+
expand(host: HTMLElement, def: SomaComponentDefinition): string {
|
|
49
|
+
const existing = this.scopeIdOf(host);
|
|
50
|
+
if (existing !== null) {
|
|
51
|
+
return existing;
|
|
52
|
+
}
|
|
53
|
+
const scopeId = `c${++scopeSeq}`;
|
|
54
|
+
|
|
55
|
+
// 1. 摘出页面提供的插槽内容(在模板注入前,DocumentFragment 引用在注入后仍有效)
|
|
56
|
+
const slotContent = new Map<string, DocumentFragment>();
|
|
57
|
+
for (const child of Array.from(host.children)) {
|
|
58
|
+
if (child.tagName === 'TEMPLATE' && child.hasAttribute(SOMA_SLOT_ATTR)) {
|
|
59
|
+
const slotName = child.getAttribute(SOMA_SLOT_ATTR)!;
|
|
60
|
+
slotContent.set(slotName, (child as HTMLTemplateElement).content);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 2. 注入组件模板
|
|
65
|
+
host.innerHTML = def.templateHtml;
|
|
66
|
+
|
|
67
|
+
// 3. 内部锚点作用域化(含 <template> 内容,插槽内容尚未注入故不受影响)
|
|
68
|
+
// 此刻宿主子树里的锚点,恰好就是"本组件模板写下的"全集——包括写在
|
|
69
|
+
// <template data-soma-slot> 里、待会儿要被搬进**子**组件出口的那些。
|
|
70
|
+
// 它们全打上本次展开的 scopeId:搬家不改归属,父引擎日后照样认得。
|
|
71
|
+
this.scopeAnchors(host, scopeId);
|
|
72
|
+
|
|
73
|
+
// 4. 插槽投影:出口替换为页面内容(页面锚点保持 data-soma-id,归页面作用域)
|
|
74
|
+
const outlets = Array.from(
|
|
75
|
+
host.querySelectorAll<HTMLElement>(`[${SOMA_SLOT_OUTLET_ATTR}]`),
|
|
76
|
+
);
|
|
77
|
+
for (const outlet of outlets) {
|
|
78
|
+
const slotName = outlet.getAttribute(SOMA_SLOT_OUTLET_ATTR)!;
|
|
79
|
+
const content = slotContent.get(slotName);
|
|
80
|
+
if (!content) {
|
|
81
|
+
if (def.contract.slots?.[slotName]?.required) {
|
|
82
|
+
throw new SomaComponentExpandError(
|
|
83
|
+
`组件 "${def.contract.name}" 的必填插槽 "${slotName}" 缺少内容(<template data-soma-slot="${slotName}">)`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
outlet.remove();
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
outlet.replaceWith(content);
|
|
90
|
+
slotContent.delete(slotName);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const missed of slotContent.keys()) {
|
|
94
|
+
throw new SomaComponentExpandError(
|
|
95
|
+
`组件 "${def.contract.name}" 不存在插槽 "${missed}" 的出口(data-soma-slot-outlet)`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
host.setAttribute(SOMA_COMPONENT_READY_ATTR, scopeId);
|
|
100
|
+
return scopeId;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private scopeAnchors(root: ParentNode, scopeId: string): void {
|
|
104
|
+
root.querySelectorAll<HTMLElement>('[data-soma-id]').forEach((el) => {
|
|
105
|
+
const somaId = el.getAttribute('data-soma-id')!;
|
|
106
|
+
el.setAttribute(SOMA_SCOPED_ID_ATTR, somaId);
|
|
107
|
+
el.setAttribute(SOMA_SCOPE_ATTR, scopeId);
|
|
108
|
+
// 模板内部可以直接声明真实子组件。保留一份局部 id,供父实例的渲染钩子
|
|
109
|
+
// 下发 props / 接回 emit;同时移除页面 id,避免污染页面命名空间。
|
|
110
|
+
if (el.hasAttribute(SOMA_COMPONENT_ATTR)) {
|
|
111
|
+
el.setAttribute(SOMA_CHILD_COMPONENT_ID_ATTR, somaId);
|
|
112
|
+
}
|
|
113
|
+
el.removeAttribute('data-soma-id');
|
|
114
|
+
});
|
|
115
|
+
root.querySelectorAll<HTMLTemplateElement>('template').forEach((tpl) => {
|
|
116
|
+
this.scopeAnchors(tpl.content, scopeId);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|