soma-core 1.5.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/app/SomaPageClient.tsx +28 -9
  2. package/app/soma/manifest/route.ts +3 -0
  3. package/bin/migrate.cjs +71 -1
  4. package/bin/soma-lsp.cjs +8 -1
  5. package/core/build/blocks/block-compile.ts +23 -5
  6. package/core/build/compiler/full-manifest-builder.ts +15 -0
  7. package/core/build/compiler/page-assets.ts +17 -1
  8. package/core/build/compiler/page-declaration-compiler.ts +67 -11
  9. package/core/build/compiler/precompiled-manifest-loader.ts +7 -0
  10. package/core/build/compiler/soma-manifest-webpack-plugin.js +7 -0
  11. package/core/build/compiler/soma-precompile.js +212 -21
  12. package/core/build/component/package-loader.ts +35 -4
  13. package/core/build/component-tree/artifact-codec.ts +192 -0
  14. package/core/build/component-tree/binding-bridge.ts +272 -0
  15. package/core/build/component-tree/collection-binding-lowerer.ts +65 -0
  16. package/core/build/component-tree/definition-preparation.ts +320 -0
  17. package/core/build/component-tree/migration-files.ts +214 -0
  18. package/core/build/component-tree/migration.ts +170 -0
  19. package/core/build/component-tree/parser.ts +362 -0
  20. package/core/build/component-tree/registry-snapshot.ts +317 -0
  21. package/core/build/component-tree/source-preflight.ts +132 -0
  22. package/core/build/component-tree/structure-topology.ts +63 -0
  23. package/core/build/component-tree/validator.ts +318 -0
  24. package/core/build/eject/component-eject.ts +1 -0
  25. package/core/build/lsp/component-tree-service.ts +79 -0
  26. package/core/build/lsp/component-tree.cjs +42 -0
  27. package/core/build/lsp/cross-file-diagnostics.cjs +15 -3
  28. package/core/build/lsp/extra-completion.cjs +7 -0
  29. package/core/client/adaptive/observer-pool.ts +122 -8
  30. package/core/client/compiler/manifest-compiler.ts +48 -5
  31. package/core/client/component/ai-catalog.ts +41 -3
  32. package/core/client/component/catalog-examples.ts +60 -0
  33. package/core/client/component/component-defaults.ts +1 -1
  34. package/core/client/component/component-definition.types.ts +9 -0
  35. package/core/client/component/component-expander.ts +22 -6
  36. package/core/client/component/component-manager.ts +881 -45
  37. package/core/client/component/component-pack.ts +1 -1
  38. package/core/client/component/component-registry.ts +11 -2
  39. package/core/client/component/scoped-adapter.ts +29 -2
  40. package/core/client/component/structure-adapter.ts +207 -0
  41. package/core/client/component/verify-pack.ts +21 -1
  42. package/core/client/container/SomaContainerAdapter.ts +103 -2
  43. package/core/client/data/data-object-resolver.ts +45 -1
  44. package/core/client/engine/asset-resolver.ts +1 -1
  45. package/core/client/engine/dom-renderer.ts +712 -26
  46. package/core/client/engine/event-engine.ts +328 -21
  47. package/core/client/engine/property-watcher.ts +164 -18
  48. package/core/client/engine/reactive-proxy.ts +194 -23
  49. package/core/client/engine/render-engine.ts +120 -11
  50. package/core/client/engine/row-range.ts +62 -0
  51. package/core/client/engine/row-scope.ts +77 -0
  52. package/core/client/engine/runtime-builder.ts +2 -0
  53. package/core/client/engine/scope-resolver.ts +3 -3
  54. package/core/client/engine/soma-bootstrap.ts +176 -17
  55. package/core/client/parser/adaptive-parser.ts +1 -1
  56. package/core/client/parser/annotation-extractor.ts +1 -1
  57. package/core/client/parser/blocks/page-parser.ts +53 -3
  58. package/core/client/parser/blocks/use-parser.ts +21 -6
  59. package/core/client/parser/data-config-parser.ts +1 -1
  60. package/core/client/parser/pipeline-parser.ts +252 -15
  61. package/core/client/parser/style-config-parser.ts +1 -1
  62. package/core/client/parser/timer-parser.ts +1 -1
  63. package/core/client/parser/trigger-parser.ts +1 -1
  64. package/core/client/parser/ui-config-parser.ts +39 -7
  65. package/core/client/pipes/pipe-registry.ts +45 -4
  66. package/core/client/registry/declaration-renderer.ts +32 -24
  67. package/core/client/registry/remote-page.ts +1 -1
  68. package/core/client/runtime/SomaSnapshotStore.ts +1 -1
  69. package/core/client/scheduler/SomaScheduler.ts +1 -1
  70. package/core/client/security/attribute-presence.ts +3 -0
  71. package/core/client/theme/harmony.ts +135 -13
  72. package/core/client/theme/scene-tokens.ts +17 -2
  73. package/core/client/theme/theme-axes.ts +43 -6
  74. package/core/client/theme/theme-engine.ts +921 -126
  75. package/core/client/validation/contract-validator.ts +2 -2
  76. package/core/index.ts +3 -0
  77. package/core/server/host/page-markup-config.ts +42 -0
  78. package/core/server/host/published-page-artifact-cache.ts +36 -4
  79. package/core/server/host/published-page-assets.ts +13 -7
  80. package/core/server/host/resolve-host-tree-context.ts +43 -0
  81. package/core/server/host/server-page-assets.ts +23 -4
  82. package/core/server/host/soma-page-compile.ts +10 -0
  83. package/core/server/mock/node/cli.mjs +3 -3
  84. package/core/server/page-declaration.ts +7 -0
  85. package/core/server/registry/declaration-precompiler.ts +35 -3
  86. package/core/server/registry/page-registry-repository.ts +31 -3
  87. package/core/server/registry/page-registry-store.ts +64 -7
  88. package/core/server/registry/publish-asset-pin.ts +6 -1
  89. package/core/server/registry/services/pages/Get.ts +11 -6
  90. package/core/server/registry/validation/compile-gate.ts +10 -1
  91. package/core/shared/component-tree-artifact.ts +237 -0
  92. package/core/shared/component-tree-errors.ts +52 -0
  93. package/core/shared/path-algebra.ts +1 -1
  94. package/core/shared/types/component-contract.types.ts +51 -0
  95. package/core/shared/types/component-tree-artifact.types.ts +121 -0
  96. package/core/shared/types/component-tree.types.ts +170 -0
  97. package/core/shared/types/engine.types.ts +5 -0
  98. package/core/shared/types/event.types.ts +8 -4
  99. package/core/shared/types/manifest.types.ts +4 -0
  100. package/core/shared/types/ui-config.types.ts +8 -0
  101. package/dist/cli/soma.js +1 -1
  102. package/dist/core/index.cjs +14508 -11358
  103. package/dist/lsp/component-tree.cjs +20997 -0
  104. package/dist/types/build/blocks/block-compile.d.ts +10 -1
  105. package/dist/types/build/compiler/full-manifest-builder.d.ts +9 -0
  106. package/dist/types/build/compiler/page-assets.d.ts +6 -1
  107. package/dist/types/build/compiler/page-declaration-compiler.d.ts +11 -1
  108. package/dist/types/build/component/package-loader.d.ts +4 -1
  109. package/dist/types/build/component-tree/artifact-codec.d.ts +50 -0
  110. package/dist/types/build/component-tree/binding-bridge.d.ts +19 -0
  111. package/dist/types/build/component-tree/collection-binding-lowerer.d.ts +18 -0
  112. package/dist/types/build/component-tree/definition-preparation.d.ts +11 -0
  113. package/dist/types/build/component-tree/migration-files.d.ts +35 -0
  114. package/dist/types/build/component-tree/migration.d.ts +28 -0
  115. package/dist/types/build/component-tree/parser.d.ts +13 -0
  116. package/dist/types/build/component-tree/registry-snapshot.d.ts +15 -0
  117. package/dist/types/build/component-tree/source-preflight.d.ts +22 -0
  118. package/dist/types/build/component-tree/structure-topology.d.ts +7 -0
  119. package/dist/types/build/component-tree/validator.d.ts +9 -0
  120. package/dist/types/build/lsp/component-tree-service.d.ts +34 -0
  121. package/dist/types/client/adaptive/observer-pool.d.ts +9 -2
  122. package/dist/types/client/compiler/manifest-compiler.d.ts +11 -2
  123. package/dist/types/client/component/ai-catalog.d.ts +18 -1
  124. package/dist/types/client/component/catalog-examples.d.ts +18 -0
  125. package/dist/types/client/component/component-definition.types.d.ts +9 -0
  126. package/dist/types/client/component/component-expander.d.ts +2 -1
  127. package/dist/types/client/component/component-manager.d.ts +97 -0
  128. package/dist/types/client/component/scoped-adapter.d.ts +1 -0
  129. package/dist/types/client/component/structure-adapter.d.ts +17 -0
  130. package/dist/types/client/container/SomaContainerAdapter.d.ts +8 -0
  131. package/dist/types/client/data/data-object-resolver.d.ts +19 -0
  132. package/dist/types/client/engine/dom-renderer.d.ts +125 -2
  133. package/dist/types/client/engine/event-engine.d.ts +62 -3
  134. package/dist/types/client/engine/property-watcher.d.ts +41 -1
  135. package/dist/types/client/engine/reactive-proxy.d.ts +42 -2
  136. package/dist/types/client/engine/render-engine.d.ts +55 -3
  137. package/dist/types/client/engine/row-range.d.ts +13 -0
  138. package/dist/types/client/engine/row-scope.d.ts +20 -0
  139. package/dist/types/client/engine/runtime-builder.d.ts +1 -0
  140. package/dist/types/client/engine/soma-bootstrap.d.ts +4 -1
  141. package/dist/types/client/parser/blocks/use-parser.d.ts +18 -0
  142. package/dist/types/client/parser/pipeline-parser.d.ts +12 -4
  143. package/dist/types/client/parser/ui-config-parser.d.ts +2 -0
  144. package/dist/types/client/pipes/pipe-registry.d.ts +39 -1
  145. package/dist/types/client/registry/declaration-renderer.d.ts +5 -1
  146. package/dist/types/client/registry/remote-page.d.ts +1 -1
  147. package/dist/types/client/theme/harmony.d.ts +44 -2
  148. package/dist/types/client/theme/scene-tokens.d.ts +11 -1
  149. package/dist/types/client/theme/theme-axes.d.ts +36 -6
  150. package/dist/types/index.d.ts +3 -1
  151. package/dist/types/server/host/page-markup-config.d.ts +6 -0
  152. package/dist/types/server/host/published-page-artifact-cache.d.ts +9 -1
  153. package/dist/types/server/host/published-page-assets.d.ts +5 -3
  154. package/dist/types/server/host/resolve-host-tree-context.d.ts +8 -0
  155. package/dist/types/server/host/server-page-assets.d.ts +6 -0
  156. package/dist/types/server/host/soma-page-compile.d.ts +10 -1
  157. package/dist/types/server/page-declaration.d.ts +6 -0
  158. package/dist/types/server/registry/declaration-precompiler.d.ts +8 -3
  159. package/dist/types/server/registry/page-registry-repository.d.ts +5 -0
  160. package/dist/types/server/registry/page-registry-store.d.ts +11 -1
  161. package/dist/types/server/registry/publish-asset-pin.d.ts +4 -0
  162. package/dist/types/server/registry/validation/compile-gate.d.ts +4 -1
  163. package/dist/types/shared/component-tree-artifact.d.ts +5 -0
  164. package/dist/types/shared/component-tree-errors.d.ts +22 -0
  165. package/dist/types/shared/types/component-contract.types.d.ts +42 -0
  166. package/dist/types/shared/types/component-tree-artifact.types.d.ts +104 -0
  167. package/dist/types/shared/types/component-tree.types.d.ts +149 -0
  168. package/dist/types/shared/types/engine.types.d.ts +5 -0
  169. package/dist/types/shared/types/event.types.d.ts +8 -4
  170. package/dist/types/shared/types/manifest.types.d.ts +4 -0
  171. package/dist/types/shared/types/ui-config.types.d.ts +8 -0
  172. package/package.json +5 -4
  173. package/dist/preview-export.cjs +0 -3401
@@ -30,7 +30,7 @@ export class SomaContractError extends Error {
30
30
 
31
31
  constructor(report: SomaContractReport) {
32
32
  const summary = report.errors[0] ?? 'SOMA contract validation failed';
33
- super(`[SOMA Contract] ${summary}`);
33
+ super(`[soma-core][引擎] ${summary}`);
34
34
  this.name = 'SomaContractError';
35
35
  this.report = report;
36
36
  }
@@ -103,7 +103,7 @@ export class SomaContractValidator {
103
103
  }
104
104
  });
105
105
  // Also scan inside <template> elements whose content lives in a DocumentFragment
106
- root.querySelectorAll<HTMLTemplateElement>('template[data-soma-role]').forEach((tmpl) => {
106
+ root.querySelectorAll<HTMLTemplateElement>('template').forEach((tmpl) => {
107
107
  if (tmpl.content) {
108
108
  scanNode(tmpl.content);
109
109
  }
package/core/index.ts CHANGED
@@ -20,6 +20,8 @@ export type {
20
20
  export { SomaComponentRegistry } from './client/component/component-registry';
21
21
  export { SomaComponentManager } from './client/component/component-manager';
22
22
  export { generateAiCatalog, buildCatalogEntry } from './client/component/ai-catalog';
23
+ export { selectSomaCatalogExample, formatSomaCatalogExample } from './client/component/catalog-examples';
24
+ export type { CatalogExampleCapabilities, CatalogExampleSource } from './client/component/catalog-examples';
23
25
  export {
24
26
  deriveKeyboardMatrix,
25
27
  renderKeyboardMatrixLine,
@@ -301,6 +303,7 @@ export type {
301
303
  SomaContractKeyboard,
302
304
  SomaContractOverlay,
303
305
  SomaContractToken,
306
+ SomaContractExample,
304
307
  } from './shared/types/component-contract.types';
305
308
  export { findNestedMutableContractDefault } from './shared/types/component-contract.types';
306
309
 
@@ -0,0 +1,42 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import yaml from 'js-yaml';
4
+ import { resolvePageMarkup } from '../../build/component-tree/source-preflight';
5
+ import type { SomaPageMarkup } from '../../shared/types/component-tree.types';
6
+
7
+ /** Trusted Host/source-package source inventory; page declarations cannot change this file. */
8
+ export function loadPageMarkupConfig(configRoot: string, base: string): {
9
+ markup?: SomaPageMarkup;
10
+ grammarVersion?: number;
11
+ } {
12
+ const file = join(configRoot, 'page-markup.config.yaml');
13
+ if (!existsSync(file)) return {};
14
+ const raw: unknown = yaml.load(readFileSync(file, 'utf8'));
15
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
16
+ throw new Error('[soma-core][页面格式] page-markup.config.yaml 必须包含 pages 对象');
17
+ }
18
+ const pages = (raw as Record<string, unknown>).pages;
19
+ if (!pages || typeof pages !== 'object' || Array.isArray(pages)) {
20
+ throw new Error('[soma-core][页面格式] page-markup.config.yaml 的 pages 必须是对象');
21
+ }
22
+ if (!Object.hasOwn(pages, base)) return {};
23
+ const metadata = (pages as Record<string, unknown>)[base];
24
+ if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
25
+ throw new Error('[soma-core][页面格式] 页面格式项必须是 markup/grammarVersion 对象');
26
+ }
27
+ const entry = metadata as Record<string, unknown>;
28
+ if (Object.keys(entry).some((key) => key !== 'markup' && key !== 'grammarVersion')) {
29
+ throw new Error('[soma-core][页面格式] 页面格式项仅允许 markup/grammarVersion');
30
+ }
31
+ if (entry.markup !== undefined && entry.markup !== 'legacy-html' && entry.markup !== 'component-tree') {
32
+ throw new Error('[soma-core][页面格式] markup 不受支持');
33
+ }
34
+ if (entry.grammarVersion !== undefined && typeof entry.grammarVersion !== 'number') {
35
+ throw new Error('[soma-core][页面格式] grammarVersion 必须为数字');
36
+ }
37
+ const markup = resolvePageMarkup({ markup: entry.markup, grammarVersion: entry.grammarVersion });
38
+ return {
39
+ markup,
40
+ ...(markup === 'component-tree' ? { grammarVersion: 1 } : {}),
41
+ };
42
+ }
@@ -37,12 +37,18 @@ import { compilePageDeclaration } from '../../build/compiler/page-declaration-co
37
37
  import { resolveOouiEngine } from '../../build/ooui/engine-slot';
38
38
  import { DECLARATION_COMPILER_VERSION } from '../registry/declaration-precompiler';
39
39
  import { currentAssetSupplyKey } from '../registry/publish-asset-pin';
40
+ import type { SomaPageMarkup, TreeCompileContext } from '../../shared/types/component-tree.types';
41
+ import type { TreeArtifact } from '../../shared/types/component-tree-artifact.types';
42
+ import { computeTreeCompileKey, verifyTreeRuntimeAssets } from '../../build/component-tree/artifact-codec';
43
+ import { SomaComponentTreeError } from '../../shared/component-tree-errors';
40
44
 
41
45
  /**
42
46
  * 参与编译、因而必须参与哈希的声明字段闭集。
43
47
  * `notes` 等元信息不进编译器,也不进键——把它算进去只会让相同产物反复重算。
44
48
  */
45
49
  export interface PublishedPageDeclaration {
50
+ markup?: SomaPageMarkup;
51
+ grammarVersion?: number;
46
52
  html: string;
47
53
  uiYaml: string;
48
54
  eventYaml?: string;
@@ -52,6 +58,9 @@ export interface PublishedPageDeclaration {
52
58
 
53
59
  /** 浏览器最终收到的运行时资产。缓存条目一律冻结:共享对象被就地改写是另一种静默污染。 */
54
60
  export interface PublishedPageArtifact {
61
+ readonly sourceMarkup: SomaPageMarkup;
62
+ readonly html: string;
63
+ readonly artifact?: TreeArtifact;
55
64
  readonly uiYaml: string;
56
65
  readonly eventYaml: string;
57
66
  readonly dataYaml?: string;
@@ -100,6 +109,8 @@ export function declarationFingerprint(declaration: PublishedPageDeclaration): s
100
109
  const hash = createHash('sha256');
101
110
  const fields: ReadonlyArray<readonly [string, string]> = [
102
111
  ['html', declaration.html],
112
+ ['markup', declaration.markup ?? 'legacy-html'],
113
+ ['grammarVersion', declaration.grammarVersion === undefined ? '' : String(declaration.grammarVersion)],
103
114
  ['uiYaml', declaration.uiYaml],
104
115
  ['eventYaml', declaration.eventYaml ?? ''],
105
116
  ['styleYaml', declaration.styleYaml ?? ''],
@@ -168,15 +179,22 @@ export function publishedPageArtifactKey(
168
179
  */
169
180
  export function compilePublishedPageArtifact(
170
181
  declaration: PublishedPageDeclaration,
182
+ treeContext?: TreeCompileContext,
171
183
  ): PublishedPageArtifact {
172
184
  const compiled = compilePageDeclaration({
173
185
  html: declaration.html,
174
186
  uiYaml: declaration.uiYaml,
187
+ markup: declaration.markup,
188
+ grammarVersion: declaration.grammarVersion,
189
+ treeContext,
175
190
  ...(declaration.eventYaml ? { eventYaml: declaration.eventYaml } : {}),
176
191
  ...(declaration.styleYaml ? { styleYaml: declaration.styleYaml } : {}),
177
192
  ...(declaration.dataYaml ? { dataYaml: declaration.dataYaml } : {}),
178
193
  });
179
194
  return Object.freeze({
195
+ sourceMarkup: compiled.runtimeAssets.sourceMarkup,
196
+ html: compiled.runtimeAssets.html,
197
+ ...(compiled.runtimeAssets.artifact ? { artifact: compiled.runtimeAssets.artifact } : {}),
180
198
  uiYaml: compiled.runtimeAssets.uiYaml,
181
199
  eventYaml: compiled.runtimeAssets.eventYaml,
182
200
  ...(compiled.runtimeAssets.dataYaml ? { dataYaml: compiled.runtimeAssets.dataYaml } : {}),
@@ -191,17 +209,25 @@ export function compilePublishedPageArtifact(
191
209
  */
192
210
  export function resolvePublishedPageArtifact(
193
211
  declaration: PublishedPageDeclaration,
194
- options: { compilerVersion?: string } = {},
212
+ options: { compilerVersion?: string; treeContext?: TreeCompileContext } = {},
195
213
  ): PublishedPageArtifactResult {
196
214
  const compilerVersion = options.compilerVersion ?? DECLARATION_COMPILER_VERSION;
197
- const identity = compileIdentity(compilerVersion);
215
+ if (declaration.markup === 'component-tree' && !options.treeContext) {
216
+ throw new SomaComponentTreeError({
217
+ code: 'CT-REGISTRY-CONTEXT', phase: 'artifact', sourceId: '<registry>', nodePath: 'treeContext',
218
+ fixHint: '读取树页面必须提供当前 Host 的受信装配上下文,不能复用无上下文缓存。',
219
+ });
220
+ }
221
+ const source = declaration.markup === 'component-tree' && options.treeContext
222
+ ? { ...declaration, treeContext: options.treeContext } : undefined;
223
+ const identity = source ? computeTreeCompileKey(source) : compileIdentity(compilerVersion);
198
224
  const key = publishedPageArtifactKey(declaration, compilerVersion, identity);
199
225
 
200
226
  const entry = cache.get(key);
201
227
  if (entry) {
202
228
  // 身份闸(与 isCompiledUsable 同源的判据):键里已含身份,这里是第二道保险——
203
229
  // 万一将来键的构成被改动,条目也不会带着别的编译身份被端出去。
204
- if (entry.identity === identity) {
230
+ if (entry.identity === identity && (!source || verifyTreeRuntimeAssets(entry.artifact, source))) {
205
231
  cache.delete(key);
206
232
  cache.set(key, entry);
207
233
  hits += 1;
@@ -211,7 +237,13 @@ export function resolvePublishedPageArtifact(
211
237
  }
212
238
 
213
239
  misses += 1;
214
- const artifact = compilePublishedPageArtifact(declaration);
240
+ const artifact = compilePublishedPageArtifact(declaration, options.treeContext);
241
+ if (source && !verifyTreeRuntimeAssets(artifact, source)) {
242
+ throw new SomaComponentTreeError({
243
+ code: 'CT-ARTIFACT-INCOMPLETE', phase: 'artifact', sourceId: '<registry>', nodePath: 'artifact',
244
+ fixHint: '发布页运行时资产不完整或摘要不一致,不能保存或下发缓存。',
245
+ });
246
+ }
215
247
  if (cache.size >= CACHE_CAPACITY) {
216
248
  const oldest = cache.keys().next();
217
249
  if (!oldest.done) {
@@ -18,6 +18,7 @@ import { getPageRegistryRepository } from '../registry/page-registry-repository'
18
18
  import type { PageVersionRecord } from '../registry/page-registry-store';
19
19
  import { currentAssetSupplyKey } from '../registry/publish-asset-pin';
20
20
  import { resolvePublishedPageArtifact } from './published-page-artifact-cache';
21
+ import type { TreeCompileContext } from '../../shared/types/component-tree.types';
21
22
 
22
23
  /** Registry `myworkspace` 的正式地址是 `/myworkspace`;兼容读取旧 `/pages/myworkspace`。 */
23
24
  export const registryPageNameFromPath = publishedPageSlugFromPath;
@@ -27,15 +28,20 @@ export async function loadPublishedRegistryPageAssets(pagePath: string) {
27
28
  const name = registryPageNameFromPath(pagePath);
28
29
  if (!name) return null;
29
30
 
30
- const record = await getPageRegistryRepository().get(name);
31
- const { artifact, cached } = resolvePublishedPageArtifact(record.declaration);
32
- if (!cached) warnOnCompilerDrift(record);
31
+ const repository = getPageRegistryRepository();
32
+ const record = await repository.get(name);
33
+ const treeContext = record.declaration.markup === 'component-tree'
34
+ ? repository.getTreeCompileContext?.(record.name, record.app) : undefined;
35
+ const { artifact, cached } = resolvePublishedPageArtifact(record.declaration, { treeContext });
36
+ if (!cached) warnOnCompilerDrift(record, treeContext);
33
37
 
34
38
  return {
35
39
  base: record.name,
36
40
  sourceType: 'registry' as const,
37
41
  sourcePackage: null,
38
- html: record.declaration.html,
42
+ sourceMarkup: artifact.sourceMarkup,
43
+ html: artifact.html,
44
+ ...(artifact.artifact ? { artifact: artifact.artifact } : {}),
39
45
  ui: artifact.uiYaml,
40
46
  event: artifact.eventYaml,
41
47
  styleYaml: '',
@@ -50,8 +56,8 @@ export async function loadPublishedRegistryPageAssets(pagePath: string) {
50
56
  * 只在真正重新编译时(缓存未命中)检查一次,命中缓存不再重复刷屏;
51
57
  * 条目被 LRU 淘汰后最多再说一遍,噪声由缓存容量天然封顶。
52
58
  */
53
- function warnOnCompilerDrift(record: PageVersionRecord): void {
54
- if (isCompiledUsable(record.compiled)) return;
59
+ function warnOnCompilerDrift(record: PageVersionRecord, treeContext?: TreeCompileContext): void {
60
+ if (isCompiledUsable(record.compiled, undefined, undefined, { declaration: record.declaration, treeContext })) return;
55
61
  const stored = record.compiled?.compilerVersion ?? '无落库产物';
56
62
  // 版本号一样也可能判不可用:供货指纹相左(组件库在发布之后换过版本)。
57
63
  // 那时只报两个一模一样的版本号,读的人会以为这条告警是误报。
@@ -64,6 +70,6 @@ function warnOnCompilerDrift(record: PageVersionRecord): void {
64
70
  `[soma-core][缓存] 发布页 ${record.name} v${record.version} 的落库产物版本为 ${stored},`
65
71
  + `当前进程编译器版本为 ${DECLARATION_COMPILER_VERSION};${supplyDrift}`
66
72
  + '本次按当前编译器重新编译。回滚只回滚声明、不回滚产物,'
67
- + '若需产物级回滚请在发布期落库运行时资产,并让本路径直接读取 record.compiled。',
73
+ + '历史声明按当前组件供货与准入策略重新编译,不承诺历史二进制或像素级恢复。',
68
74
  );
69
75
  }
@@ -0,0 +1,43 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join, resolve } from 'node:path';
3
+ import createJiti from 'jiti';
4
+ import { SomaComponentTreeError } from '../../shared/component-tree-errors';
5
+ import type { TreeCompileContext } from '../../shared/types/component-tree.types';
6
+ import { getSomaHostSourceRoot } from './source-packages';
7
+
8
+ export interface SomaTreeContextRequest {
9
+ pageBase: string;
10
+ domain?: string;
11
+ app?: string;
12
+ }
13
+
14
+ /** Synchronous, server-only Host assembly hook. No page/request-provided module paths. */
15
+ export function resolveHostTreeCompileContext(
16
+ hostDir: string | undefined,
17
+ request: SomaTreeContextRequest,
18
+ ): TreeCompileContext | undefined {
19
+ if (!hostDir) return undefined;
20
+ const hostRoot = getSomaHostSourceRoot(hostDir);
21
+ const file = join(hostRoot, 'bootstrap', 'soma-tree-context.ts');
22
+ if (!existsSync(file)) return undefined;
23
+ try {
24
+ const loader = createJiti(import.meta.url, {
25
+ interopDefault: false,
26
+ requireCache: false,
27
+ alias: { '@soma-host': hostRoot, '@core': resolve(process.cwd(), 'core') },
28
+ });
29
+ const loaded = loader(file) as {
30
+ createSomaTreeCompileContext?: (input: SomaTreeContextRequest) => TreeCompileContext;
31
+ };
32
+ if (typeof loaded?.createSomaTreeCompileContext !== 'function') throw new Error('missing hook');
33
+ const context = loaded.createSomaTreeCompileContext({ ...request });
34
+ if (!context || typeof context !== 'object'
35
+ || !context.registrySnapshot || !context.compositionPolicy) throw new Error('invalid context');
36
+ return context;
37
+ } catch {
38
+ throw new SomaComponentTreeError({
39
+ code: 'CT-REGISTRY-CONTEXT', phase: 'validate', sourceId: '<host>', nodePath: 'treeContext',
40
+ fixHint: 'Host 的 bootstrap/soma-tree-context.ts 必须同步返回实际装配的组件快照和受控页面策略。',
41
+ });
42
+ }
43
+ }
@@ -12,10 +12,16 @@ import {
12
12
  type SomaAssetSource,
13
13
  } from './source-packages';
14
14
  import { compileSomaPageAssets } from './soma-page-compile';
15
+ import { loadPageMarkupConfig } from './page-markup-config';
16
+ import { resolveHostTreeCompileContext } from './resolve-host-tree-context';
17
+ import type { SomaPageMarkup, TreeCompileContext } from '../../shared/types/component-tree.types';
18
+ import type { TreeArtifact } from '../../shared/types/component-tree-artifact.types';
15
19
 
16
20
  export type SomaPageAssetLoadProfile = 'render' | 'headless';
17
21
 
18
22
  export interface SomaServerPageAssets {
23
+ sourceMarkup: SomaPageMarkup;
24
+ artifact?: TreeArtifact;
19
25
  base: string;
20
26
  sourceType: SomaAssetSource['type'];
21
27
  sourcePackage: string | null;
@@ -107,6 +113,8 @@ export function loadSomaServerPageAssets(options: {
107
113
  urlPath: string;
108
114
  profile?: SomaPageAssetLoadProfile;
109
115
  watch?: boolean;
116
+ /** Trusted server context, never read from the page declaration or request body. */
117
+ treeContext?: TreeCompileContext;
110
118
  }): SomaServerPageAssets {
111
119
  const { hostDir, urlPath } = options;
112
120
  if (!hostDir) {
@@ -179,15 +187,23 @@ export function loadSomaServerPageAssets(options: {
179
187
  const includes = collectIncludes(sources);
180
188
 
181
189
  // 唯一页面声明在服务端编译;浏览器只接收运行时资产,不解析源 YAML。
182
- const compiled = compileSomaPageAssets(ui, html, uiDir, base, includes);
190
+ const metadata = loadPageMarkupConfig(source.configRoot, base);
191
+ const compiled = compileSomaPageAssets(ui, html, uiDir, base, includes, {
192
+ ...metadata,
193
+ treeContext: options.treeContext ?? (metadata.markup === 'component-tree'
194
+ ? resolveHostTreeCompileContext(hostDir, { pageBase: base }) : undefined),
195
+ });
183
196
 
184
197
  if (profile === 'headless') {
185
198
  return {
199
+ sourceMarkup: compiled.sourceMarkup,
200
+ ...(compiled.artifact ? { artifact: compiled.artifact } : {}),
201
+ ...(compiled.sourceMarkup === 'component-tree' ? { html: compiled.html } : {}),
186
202
  base,
187
203
  sourceType: source.type,
188
204
  sourcePackage: source.packageName ?? null,
189
205
  ui: compiled.ui,
190
- event: '',
206
+ event: compiled.sourceMarkup === 'component-tree' ? compiled.event : '',
191
207
  styleYaml: '',
192
208
  ...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
193
209
  localeYaml,
@@ -196,17 +212,20 @@ export function loadSomaServerPageAssets(options: {
196
212
  }
197
213
 
198
214
  return {
215
+ sourceMarkup: compiled.sourceMarkup,
216
+ ...(compiled.artifact ? { artifact: compiled.artifact } : {}),
199
217
  base,
200
218
  sourceType: source.type,
201
219
  sourcePackage: source.packageName ?? null,
202
- html,
220
+ html: compiled.html,
203
221
  ui: compiled.ui,
204
222
  event: compiled.event,
205
223
  styleYaml: '',
206
224
  ...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
207
225
  localeYaml,
208
226
  appConfigYaml,
209
- includes,
227
+ // Includes have already been expanded; never mutate compiled HTML again.
228
+ ...(compiled.sourceMarkup === 'legacy-html' ? { includes } : {}),
210
229
  };
211
230
  } catch (error) {
212
231
  if (error instanceof SomaPageAssetError) throw error;
@@ -2,8 +2,13 @@
2
2
 
3
3
  import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
4
4
  import { loadExternalBlocks } from '../../build/blocks/block-loader';
5
+ import type { SomaPageMarkup, TreeCompileContext } from '../../shared/types/component-tree.types';
6
+ import type { TreeArtifact } from '../../shared/types/component-tree-artifact.types';
5
7
 
6
8
  export interface SomaPageCompileResult {
9
+ sourceMarkup: SomaPageMarkup;
10
+ html: string;
11
+ artifact?: TreeArtifact;
7
12
  ui: string;
8
13
  event: string;
9
14
  dataYaml?: string;
@@ -16,16 +21,21 @@ export function compileSomaPageAssets(
16
21
  uiDir: string,
17
22
  base: string,
18
23
  includes?: Readonly<Record<string, string>>,
24
+ options: { markup?: SomaPageMarkup; grammarVersion?: number; treeContext?: TreeCompileContext } = {},
19
25
  ): SomaPageCompileResult {
20
26
  const externalBlocks = loadExternalBlocks(uiDir, base);
21
27
  const compiled = compilePageDeclaration({
22
28
  html,
23
29
  uiYaml: uiRaw,
30
+ ...options,
24
31
  ...(externalBlocks.size > 0 ? { externalBlocks } : {}),
25
32
  ...(includes ? { includes } : {}),
26
33
  });
27
34
 
28
35
  return {
36
+ sourceMarkup: compiled.runtimeAssets.sourceMarkup,
37
+ html: compiled.runtimeAssets.html,
38
+ ...(compiled.runtimeAssets.artifact ? { artifact: compiled.runtimeAssets.artifact } : {}),
29
39
  ui: compiled.runtimeAssets.uiYaml,
30
40
  event: compiled.runtimeAssets.eventYaml,
31
41
  ...(compiled.runtimeAssets.dataYaml ? { dataYaml: compiled.runtimeAssets.dataYaml } : {}),
@@ -10,6 +10,7 @@ import {
10
10
  import { filterMockEnvironment } from './environment-policy.mjs';
11
11
 
12
12
  const MOCK_COMMANDS = new Set(['dev', 'build', 'start']);
13
+ const REQUIRED_NODE_VERSION = '24.21.0';
13
14
  const DEFAULT_PRELOAD_URL = pathToFileURL(
14
15
  path.join(path.dirname(fileURLToPath(import.meta.url)), 'preload.mjs'),
15
16
  ).href;
@@ -81,11 +82,10 @@ function sanitizeMockEnvironment(environment, preloadUrl, options = {}) {
81
82
  }
82
83
 
83
84
  function assertSupportedNode(version = process.versions.node) {
84
- const [major, minor] = String(version).split('.').map(Number);
85
- if (major < 20 || (major === 20 && minor < 9)) {
85
+ if (String(version) !== REQUIRED_NODE_VERSION) {
86
86
  throw new SomaMockCliError(
87
87
  'NODE_VERSION_UNSUPPORTED',
88
- `Server Mock preloading requires Node >=20.9; current version is ${version}`,
88
+ `Server Mock preloading requires Node ${REQUIRED_NODE_VERSION}; current version is ${version}`,
89
89
  );
90
90
  }
91
91
  }
@@ -16,3 +16,10 @@ export {
16
16
  type CompiledPageDeclaration,
17
17
  type SomaPageDeclarationInput,
18
18
  } from '../build/compiler/page-declaration-compiler';
19
+
20
+ /** Source diagnostics and trusted build-time component preparation. */
21
+ export { preflightComponentTree, type ComponentTreePreflightInput } from '../build/component-tree/source-preflight';
22
+ export { buildComponentTreeRegistrySnapshot } from '../build/component-tree/registry-snapshot';
23
+ export { prepareComponentTreeDefinitions } from '../build/component-tree/definition-preparation';
24
+ export { normalizeRuntimeConfig, computeTreeCompileKey, verifyTreeRuntimeAssets } from '../build/component-tree/artifact-codec';
25
+ export type { TreeCompileContext, TreeValidationResult } from '../shared/types/component-tree.types';
@@ -28,6 +28,10 @@ import {
28
28
  } from '../../build/compiler/page-declaration-compiler';
29
29
  import { currentAssetSupplyKey } from './publish-asset-pin';
30
30
  import type { PageRegistryDeclaration } from './page-registry-store';
31
+ import type { TreeCompileContext } from '../../shared/types/component-tree.types';
32
+ import type { ComponentTreeRuntimeEnvelope } from '../../shared/types/component-tree-artifact.types';
33
+ import { assertRuntimeArtifact } from '../../shared/component-tree-artifact';
34
+ import { verifyTreeRuntimeAssets } from '../../build/component-tree/artifact-codec';
31
35
 
32
36
  /**
33
37
  * 编译器版本。与 soma-core 的包版本对齐——语义变更必然伴随版本号变化,
@@ -54,7 +58,7 @@ function readCoreVersion(): string {
54
58
  }
55
59
 
56
60
  /** 落库的完整运行时产物。 */
57
- export interface CompiledDeclaration {
61
+ export interface CompiledDeclaration extends ComponentTreeRuntimeEnvelope {
58
62
  compilerVersion: string;
59
63
  /**
60
64
  * 供货指纹(词表版本那一维,可选)。缺席 = 编译那一刻宿主没报供货版本,
@@ -81,19 +85,32 @@ export interface CompiledDeclaration {
81
85
  export function precompileDeclaration(
82
86
  declaration: PageRegistryDeclaration,
83
87
  compiledPage?: CompiledPageDeclaration,
88
+ treeContext?: TreeCompileContext,
84
89
  ): CompiledDeclaration | null {
85
90
  try {
86
91
  const page = compiledPage ?? compilePageDeclaration({
87
92
  html: declaration.html,
88
93
  uiYaml: declaration.uiYaml,
94
+ markup: declaration.markup,
95
+ grammarVersion: declaration.grammarVersion,
96
+ treeContext,
89
97
  ...(declaration.eventYaml ? { eventYaml: declaration.eventYaml } : {}),
90
98
  ...(declaration.styleYaml ? { styleYaml: declaration.styleYaml } : {}),
91
99
  ...(declaration.dataYaml ? { dataYaml: declaration.dataYaml } : {}),
92
100
  });
93
101
  const supplyKey = currentAssetSupplyKey();
94
- return {
102
+ assertRuntimeArtifact(page.runtimeAssets);
103
+ if (declaration.markup === 'component-tree' && page.runtimeAssets.sourceMarkup !== 'component-tree') {
104
+ throw new Error('组件树声明与编译产物格式不匹配');
105
+ }
106
+ const result: CompiledDeclaration = {
95
107
  compilerVersion: DECLARATION_COMPILER_VERSION,
96
- ...(supplyKey ? { assetSupplyKey: supplyKey } : {}),
108
+ sourceMarkup: page.runtimeAssets.sourceMarkup,
109
+ html: page.runtimeAssets.html,
110
+ ...(page.runtimeAssets.artifact ? {
111
+ artifact: page.runtimeAssets.artifact,
112
+ assetSupplyKey: page.runtimeAssets.artifact.assetSupplyKey,
113
+ } : supplyKey ? { assetSupplyKey: supplyKey } : {}),
97
114
  uiConfig: page.uiConfig,
98
115
  eventConfig: page.eventConfig,
99
116
  triggerConfig: page.triggers,
@@ -101,6 +118,12 @@ export function precompileDeclaration(
101
118
  ...(page.styleConfig ? { styleConfig: page.styleConfig } : {}),
102
119
  ...(page.dataConfig ? { dataConfig: page.dataConfig } : {}),
103
120
  };
121
+ if (declaration.markup === 'component-tree' && (!treeContext || !verifyTreeRuntimeAssets(result, {
122
+ ...declaration, treeContext,
123
+ }))) {
124
+ throw new Error('组件树预编译产物与源声明、受信上下文或运行时配置摘要不匹配');
125
+ }
126
+ return result;
104
127
  } catch (error) {
105
128
  console.warn(`[soma-core][注册中心] SOMA DSL 预编译失败:${message(error)}`);
106
129
  return null;
@@ -124,10 +147,19 @@ export function isCompiledUsable(
124
147
  compiled: CompiledDeclaration | null | undefined,
125
148
  runtimeVersion = DECLARATION_COMPILER_VERSION,
126
149
  runtimeSupplyKey = currentAssetSupplyKey(),
150
+ options: { declaration?: PageRegistryDeclaration; treeContext?: TreeCompileContext } = {},
127
151
  ): boolean {
128
152
  if (!compiled || typeof compiled !== 'object') return false;
129
153
  if (compiled.compilerVersion !== runtimeVersion) return false;
130
154
  if (compiled.uiConfig === undefined || compiled.eventConfig === undefined) return false;
155
+ if (compiled.sourceMarkup === 'component-tree' || options.declaration?.markup === 'component-tree') {
156
+ if (compiled.sourceMarkup !== 'component-tree' || !options.treeContext) return false;
157
+ try { assertRuntimeArtifact(compiled); } catch { return false; }
158
+ return options.declaration !== undefined && verifyTreeRuntimeAssets(compiled, {
159
+ ...options.declaration, treeContext: options.treeContext,
160
+ });
161
+ }
162
+ try { assertRuntimeArtifact(compiled); } catch { return false; }
131
163
  const stored = compiled.assetSupplyKey;
132
164
  if (typeof stored === 'string' && stored !== ''
133
165
  && typeof runtimeSupplyKey === 'string' && runtimeSupplyKey !== ''
@@ -4,6 +4,7 @@ import {
4
4
  cloneRecord,
5
5
  createPageVersionRecord,
6
6
  normalizeDeclaration,
7
+ isTreeDeclaration,
7
8
  normalizePublishOptions,
8
9
  normalizeStoredRecord,
9
10
  PageRegistryError,
@@ -23,6 +24,8 @@ import {
23
24
  } from './page-registry-store';
24
25
  import type { ConditionalObjectStore, WriteCondition } from './conditional-object-store';
25
26
  import { withRegistryLock } from './registry-file-lock';
27
+ import type { TreeCompileContext } from '../../shared/types/component-tree.types';
28
+ import { resolveHostTreeCompileContext } from '../host/resolve-host-tree-context';
26
29
 
27
30
  const MAX_CAS_ATTEMPTS = 5;
28
31
  const AGGREGATE_SCHEMA_VERSION = 1;
@@ -47,6 +50,7 @@ export interface RollbackResult {
47
50
 
48
51
  /** API 的唯一仓储门面;全部方法异步,后端可为共享文件系统或对象存储 CAS。 */
49
52
  export interface PageRegistryRepository {
53
+ getTreeCompileContext?(name: string, app: string): TreeCompileContext | undefined;
50
54
  publish(
51
55
  name: string,
52
56
  declaration: unknown,
@@ -65,6 +69,10 @@ export interface PageRegistryRepository {
65
69
  export class FilePageRegistryRepository implements PageRegistryRepository {
66
70
  constructor(private readonly injectedPages?: PageRegistryStore) {}
67
71
 
72
+ getTreeCompileContext(name: string, app: string): TreeCompileContext | undefined {
73
+ return this.pages().getTreeCompileContext(name, app);
74
+ }
75
+
68
76
  async publish(
69
77
  name: string,
70
78
  declaration: unknown,
@@ -118,9 +126,16 @@ export class ConditionalPageRegistryRepository implements PageRegistryRepository
118
126
  prefix?: string;
119
127
  now?: () => number;
120
128
  maxAttempts?: number;
129
+ treeContextResolver?: (name: string, app: string) => TreeCompileContext | undefined;
121
130
  } = {},
122
131
  ) {}
123
132
 
133
+ getTreeCompileContext(name: string, app: string): TreeCompileContext | undefined {
134
+ return this.options.treeContextResolver
135
+ ? this.options.treeContextResolver(name, app)
136
+ : resolveHostTreeCompileContext(process.env.SOMA_HOST_DIR, { pageBase: name, app });
137
+ }
138
+
124
139
  async publish(
125
140
  name: string,
126
141
  declaration: unknown,
@@ -129,7 +144,9 @@ export class ConditionalPageRegistryRepository implements PageRegistryRepository
129
144
  const safeName = assertPageName(name);
130
145
  const normalizedOptions = normalizePublishOptions(options);
131
146
  // 闸门那一次的编译结果一路带到落库:CAS 冲突重试也只是换版本号,不重编同一份声明
132
- const { declaration: normalized, compiledPage } = normalizeDeclaration(declaration);
147
+ const treeContext = isTreeDeclaration(declaration)
148
+ ? this.getTreeCompileContext(safeName, normalizedOptions.app ?? 'default') : undefined;
149
+ const { declaration: normalized, compiledPage } = normalizeDeclaration(declaration, treeContext);
133
150
  return this.mutate(safeName, (aggregate) => {
134
151
  const record = createPageVersionRecord(safeName, aggregate.versions, {
135
152
  declaration: normalized,
@@ -139,6 +156,7 @@ export class ConditionalPageRegistryRepository implements PageRegistryRepository
139
156
  meta: normalizedOptions.meta,
140
157
  operator: normalizedOptions.operator,
141
158
  compiledPage,
159
+ treeContext,
142
160
  }, this.now());
143
161
  aggregate.versions.push(record);
144
162
  return { value: { record: cloneRecord(record) }, aggregate };
@@ -196,9 +214,15 @@ export class ConditionalPageRegistryRepository implements PageRegistryRepository
196
214
  ): Promise<RollbackResult> {
197
215
  const safeName = assertPageName(name);
198
216
  assertVersionNumber(version);
217
+ let treeContext: TreeCompileContext | undefined;
218
+ let verified: ReturnType<typeof normalizeDeclaration> | undefined;
199
219
  return this.mutate(safeName, (aggregate) => {
200
220
  const target = findVersion(safeName, aggregate.versions, version, '无法回滚');
201
- const record = copyAsPublished(safeName, aggregate.versions, target, options.operator, this.now());
221
+ if (target.declaration.markup === 'component-tree' && !verified) {
222
+ treeContext = this.getTreeCompileContext(safeName, target.app);
223
+ verified = normalizeDeclaration(target.declaration, treeContext);
224
+ }
225
+ const record = copyAsPublished(safeName, aggregate.versions, target, options.operator, this.now(), verified, treeContext);
202
226
  aggregate.versions.push(record);
203
227
  return { value: { record: cloneRecord(record) }, aggregate };
204
228
  });
@@ -337,9 +361,13 @@ function copyAsPublished(
337
361
  target: PageVersionRecord,
338
362
  operator: string | undefined,
339
363
  now: number,
364
+ verified?: ReturnType<typeof normalizeDeclaration>,
365
+ treeContext?: TreeCompileContext,
340
366
  ): PageVersionRecord {
341
367
  return createPageVersionRecord(name, versions, {
342
- declaration: target.declaration,
368
+ declaration: verified?.declaration ?? target.declaration,
369
+ compiledPage: verified?.compiledPage,
370
+ treeContext,
343
371
  status: 'published',
344
372
  app: target.app,
345
373
  tags: target.tags,