soma-core 1.4.1 → 1.4.2
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/core/build/compiler/soma-precompile.js +12 -4
- package/core/build/component/package-loader.ts +32 -0
- package/core/build/pack/pack-init.ts +19 -14
- package/core/client/base/SomaHttpClient.ts +34 -1
- package/core/client/component/ai-catalog.ts +42 -0
- package/core/client/component/verify-pack.ts +120 -1
- package/core/client/container/SomaContainerAdapter.ts +6 -2
- package/core/client/infra/http/SomaHttpService.ts +2 -2
- package/core/index.ts +21 -0
- package/core/shared/component/derive-sample.ts +105 -0
- package/core/shared/component/field-lines.ts +473 -0
- package/core/shared/types/component-contract.types.ts +23 -7
- package/dist/core/index.cjs +925 -362
- package/dist/types/client/base/SomaHttpClient.d.ts +8 -0
- package/dist/types/client/component/ai-catalog.d.ts +9 -0
- package/dist/types/client/component/verify-pack.d.ts +10 -0
- package/dist/types/index.d.ts +4 -1
- package/dist/types/shared/component/derive-sample.d.ts +23 -0
- package/dist/types/shared/component/field-lines.d.ts +19 -0
- package/dist/types/shared/types/component-contract.types.d.ts +22 -6
- package/package.json +2 -2
|
@@ -34,7 +34,7 @@ __export(soma_precompile_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(soma_precompile_exports);
|
|
36
36
|
|
|
37
|
-
//
|
|
37
|
+
// node_modules/js-yaml/dist/js-yaml.mjs
|
|
38
38
|
function getDefaultExportFromCjs(x) {
|
|
39
39
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
40
40
|
}
|
|
@@ -1312,16 +1312,21 @@ function requireLoader() {
|
|
|
1312
1312
|
state.result += _result;
|
|
1313
1313
|
}
|
|
1314
1314
|
}
|
|
1315
|
+
function chargeMergeWork(state) {
|
|
1316
|
+
state.totalMergeKeys++;
|
|
1317
|
+
if (state.maxTotalMergeKeys !== -1 && state.totalMergeKeys > state.maxTotalMergeKeys) {
|
|
1318
|
+
throwError(state, "merge keys exceeded maxTotalMergeKeys (" + state.maxTotalMergeKeys + ")");
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1315
1321
|
function mergeMappings(state, destination, source, overridableKeys) {
|
|
1316
1322
|
if (!common2.isObject(source)) {
|
|
1317
1323
|
throwError(state, "cannot merge mappings; the provided source object is unacceptable");
|
|
1318
1324
|
}
|
|
1325
|
+
chargeMergeWork(state);
|
|
1319
1326
|
const sourceKeys = Object.keys(source);
|
|
1320
1327
|
for (let index = 0, quantity = sourceKeys.length; index < quantity; index += 1) {
|
|
1321
1328
|
const key = sourceKeys[index];
|
|
1322
|
-
|
|
1323
|
-
throwError(state, "merge keys exceeded maxTotalMergeKeys (" + state.maxTotalMergeKeys + ")");
|
|
1324
|
-
}
|
|
1329
|
+
chargeMergeWork(state);
|
|
1325
1330
|
if (!_hasOwnProperty.call(destination, key)) {
|
|
1326
1331
|
setProperty(destination, key, source[key]);
|
|
1327
1332
|
overridableKeys[key] = true;
|
|
@@ -1349,6 +1354,9 @@ function requireLoader() {
|
|
|
1349
1354
|
}
|
|
1350
1355
|
if (keyTag === "tag:yaml.org,2002:merge") {
|
|
1351
1356
|
if (Array.isArray(valueNode)) {
|
|
1357
|
+
if (valueNode.length > 100) {
|
|
1358
|
+
throwError(state, "abnormal merge sequence size");
|
|
1359
|
+
}
|
|
1352
1360
|
for (let index = 0, quantity = valueNode.length; index < quantity; index += 1) {
|
|
1353
1361
|
mergeMappings(state, _result, valueNode[index], overridableKeys);
|
|
1354
1362
|
}
|
|
@@ -19,6 +19,11 @@ import fs from 'node:fs';
|
|
|
19
19
|
import path from 'node:path';
|
|
20
20
|
import yaml from 'js-yaml';
|
|
21
21
|
import { parseUiConfig } from '../../client/parser/ui-config-parser';
|
|
22
|
+
import {
|
|
23
|
+
parseEmitLines,
|
|
24
|
+
parseFieldLines,
|
|
25
|
+
parseSlotLines,
|
|
26
|
+
} from '../../shared/component/field-lines';
|
|
22
27
|
import { parseEventConfig } from '../../client/parser/pipeline-parser';
|
|
23
28
|
import {
|
|
24
29
|
findNestedMutableContractDefault,
|
|
@@ -232,12 +237,39 @@ function toKebabCase(name: string): string {
|
|
|
232
237
|
.toLowerCase();
|
|
233
238
|
}
|
|
234
239
|
|
|
240
|
+
/** props / emits / slots 若写成文本块,在此解析成契约结构 */
|
|
241
|
+
function normalizeFieldBlocks(contract: Record<string, unknown>, packageDir: string): void {
|
|
242
|
+
const blocks = [
|
|
243
|
+
['props', parseFieldLines],
|
|
244
|
+
['emits', parseEmitLines],
|
|
245
|
+
['slots', parseSlotLines],
|
|
246
|
+
] as const;
|
|
247
|
+
|
|
248
|
+
for (const [key, parse] of blocks) {
|
|
249
|
+
const value = contract[key];
|
|
250
|
+
if (typeof value !== 'string') continue;
|
|
251
|
+
try {
|
|
252
|
+
contract[key] = (parse as (text: string) => unknown)(value);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
throw new SomaComponentPackageError(
|
|
255
|
+
`contract.yaml 的 ${key} 文本块非法:${(error as Error).message}`,
|
|
256
|
+
packageDir,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
235
262
|
function validateContract(raw: unknown, packageDir: string): SomaComponentContract {
|
|
236
263
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
237
264
|
throw new SomaComponentPackageError('contract.yaml 必须是对象', packageDir);
|
|
238
265
|
}
|
|
239
266
|
const contract = raw as Record<string, unknown>;
|
|
240
267
|
|
|
268
|
+
// 字段行文本块(REQ-012):props / emits / slots 写成文本时在这里归一成契约结构。
|
|
269
|
+
// 归一放在校验之前,后面所有既有规则(C1/C4/C4a)对两种写法一视同仁——
|
|
270
|
+
// 文本块是一种写法,不是第二套契约。
|
|
271
|
+
normalizeFieldBlocks(contract, packageDir);
|
|
272
|
+
|
|
241
273
|
for (const field of ['name', 'layer', 'version', 'description'] as const) {
|
|
242
274
|
if (typeof contract[field] !== 'string' || !contract[field]) {
|
|
243
275
|
throw new SomaComponentPackageError(`契约缺少必填字段 ${field}`, packageDir);
|
|
@@ -115,6 +115,7 @@ function renderIndex(packName: string, packVar: string, componentName: string):
|
|
|
115
115
|
function renderComponents(componentName: string, kebab: string): string {
|
|
116
116
|
return [
|
|
117
117
|
"import type { SomaComponentDefinition } from 'soma-core';",
|
|
118
|
+
"import { somaEmits, somaFields, somaSlots } from 'soma-core';",
|
|
118
119
|
'',
|
|
119
120
|
'/**',
|
|
120
121
|
' * 示例业务组件。solution 层的四维设计判据(深度 / 组合 / 完备 / 可操作)见',
|
|
@@ -128,20 +129,24 @@ function renderComponents(componentName: string, kebab: string): string {
|
|
|
128
129
|
" version: '0.1.0',",
|
|
129
130
|
" geometry: 'surface',",
|
|
130
131
|
" description: '示例卡片:把一类业务场景整套打包,一个声明落一个完整业务功能',",
|
|
131
|
-
'
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
'
|
|
137
|
-
'
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
'
|
|
141
|
-
'
|
|
142
|
-
'
|
|
143
|
-
|
|
144
|
-
'
|
|
132
|
+
' // 字段行写法(REQ-012):一行一个字段,缩进即嵌套,示例值就在行尾。',
|
|
133
|
+
' // 示例值不是装饰——组件橱窗、视觉矩阵与 AI 拿到的样张都由它派生,',
|
|
134
|
+
' // 缺了它这个组件在橱窗里只有一个空壳(verifyComponentPack 会点名)。',
|
|
135
|
+
' props: somaFields`',
|
|
136
|
+
" title: string, 卡片标题, 默认 \"\", 示例 本周待跟进",
|
|
137
|
+
' items: array<object>, 条目列表, 默认 []',
|
|
138
|
+
' id: string, 条目编号, 必填, 示例 T-1001',
|
|
139
|
+
' label: string, 条目文案, 必填, 示例 杭州云汀科技 · 周四回访',
|
|
140
|
+
' status: enum(todo|doing|done), 条目状态, 默认 todo, 示例 doing',
|
|
141
|
+
' loading: boolean, 加载态, 默认 false',
|
|
142
|
+
' `,',
|
|
143
|
+
' emits: somaEmits`',
|
|
144
|
+
' action: 用户触发了卡片上的动作(业务处置归页面)',
|
|
145
|
+
' key: string, 动作标识',
|
|
146
|
+
' `,',
|
|
147
|
+
' slots: somaSlots`',
|
|
148
|
+
' footer: 底部动作区,宿主接手时用它',
|
|
149
|
+
' `,',
|
|
145
150
|
' // 组件级令牌(M-1):组件自己声明的可调旋钮。',
|
|
146
151
|
' // default 必须引用语义令牌——组件令牌是语义令牌的收窄,不是新的真源。',
|
|
147
152
|
' tokens: {',
|
|
@@ -15,6 +15,36 @@ export interface SomaHttpClientConfig {
|
|
|
15
15
|
defaultHeaders?: Record<string, string> | (() => Record<string, string>);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/** 错误正文带进异常消息的长度上限:够看清服务端文案,又不至于把整页 HTML 错误页塞进日志 */
|
|
19
|
+
const ERROR_BODY_MAX_LENGTH = 500;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 从错误响应正文里提炼一句可读文案。
|
|
23
|
+
*
|
|
24
|
+
* 服务端精心写的结构化错误(`{ ok: false, error: "未知组件:X" }`)才是调用方要的信息,
|
|
25
|
+
* 只留 statusText 等于把它整条丢掉。正文也可能是 HTML 错误页或空串,
|
|
26
|
+
* 因此 JSON 解析必须容错——解析失败只是"没有结构化字段",不该把请求变成另一种崩法。
|
|
27
|
+
*/
|
|
28
|
+
export function describeHttpErrorBody(body: string, statusText: string): string {
|
|
29
|
+
const text = (body ?? '').trim();
|
|
30
|
+
if (text === '') return statusText;
|
|
31
|
+
|
|
32
|
+
let message = text;
|
|
33
|
+
try {
|
|
34
|
+
const parsed = JSON.parse(text);
|
|
35
|
+
if (parsed && typeof parsed === 'object') {
|
|
36
|
+
const field = (parsed as Record<string, unknown>).error ?? (parsed as Record<string, unknown>).message;
|
|
37
|
+
if (typeof field === 'string' && field.trim() !== '') message = field.trim();
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// 非 JSON(HTML 错误页、纯文本)就用正文原文
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return message.length > ERROR_BODY_MAX_LENGTH
|
|
44
|
+
? `${message.slice(0, ERROR_BODY_MAX_LENGTH)}…`
|
|
45
|
+
: message;
|
|
46
|
+
}
|
|
47
|
+
|
|
18
48
|
/**
|
|
19
49
|
* SomaHttpClient — API 请求封装
|
|
20
50
|
*
|
|
@@ -116,7 +146,10 @@ export class SomaHttpClient {
|
|
|
116
146
|
const res = await fetch(url, options);
|
|
117
147
|
|
|
118
148
|
if (!res.ok) {
|
|
119
|
-
|
|
149
|
+
const errorBody = typeof res.text === 'function'
|
|
150
|
+
? await res.text().catch(() => '')
|
|
151
|
+
: '';
|
|
152
|
+
throw new Error(`HTTP ${res.status}: ${describeHttpErrorBody(errorBody, res.statusText)}`);
|
|
120
153
|
}
|
|
121
154
|
|
|
122
155
|
return (await res.json()) as T;
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
// 纪律(D10):目录永远从契约生成,禁止手工维护第二份副本。
|
|
8
8
|
|
|
9
9
|
import { deriveBlockExample } from './example-blocks';
|
|
10
|
+
import { renderFieldLines } from '../../shared/component/field-lines';
|
|
11
|
+
import { deriveContractSample, hasRenderableSample } from '../../shared/component/derive-sample';
|
|
10
12
|
import { renderKeyboardMatrixLine } from './keyboard-matrix';
|
|
11
13
|
import {
|
|
12
14
|
componentTokenVar,
|
|
@@ -62,6 +64,24 @@ export interface SomaAiCatalogEntry {
|
|
|
62
64
|
example: string;
|
|
63
65
|
antiPatterns?: string[];
|
|
64
66
|
};
|
|
67
|
+
/**
|
|
68
|
+
* 字段行原文(REQ-012):对象 / 数组 prop 的逐字段形状与示例。
|
|
69
|
+
*
|
|
70
|
+
* 只在契约真写了嵌套形状或示例时出现——否则它与上面的 props 表逐字重复。
|
|
71
|
+
* 目录直接打印它,不另生成第二张字段表:作者写的和读者读到的是同一份东西。
|
|
72
|
+
*/
|
|
73
|
+
spec?: string;
|
|
74
|
+
/** 派生样张(REQ-012):展示页与视觉矩阵喂给组件的那份 props */
|
|
75
|
+
sample?: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 契约里是否写了值得单独打印的字段形状(嵌套或示例) */
|
|
79
|
+
function hasFieldSpec(contract: SomaComponentContract): boolean {
|
|
80
|
+
const seen = (field: SomaContractProp): boolean => {
|
|
81
|
+
if (field.fields !== undefined || field.items !== undefined || field.example !== undefined) return true;
|
|
82
|
+
return false;
|
|
83
|
+
};
|
|
84
|
+
return Object.values(contract.props ?? {}).some(seen);
|
|
65
85
|
}
|
|
66
86
|
|
|
67
87
|
/**
|
|
@@ -154,6 +174,11 @@ export function buildCatalogEntry(contract: SomaComponentContract): SomaAiCatalo
|
|
|
154
174
|
antiPatterns: contract.ai.anti_patterns,
|
|
155
175
|
}
|
|
156
176
|
: undefined,
|
|
177
|
+
// 两个可选段(REQ-012):缺席时连键都不写,目录 JSON 的形状对存量消费者逐键不变
|
|
178
|
+
...(hasFieldSpec(contract) ? { spec: renderFieldLines(contract.props ?? {}) } : {}),
|
|
179
|
+
...(hasRenderableSample(contract)
|
|
180
|
+
? { sample: deriveContractSample(contract).props }
|
|
181
|
+
: {}),
|
|
157
182
|
};
|
|
158
183
|
}
|
|
159
184
|
|
|
@@ -278,6 +303,23 @@ function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): st
|
|
|
278
303
|
}
|
|
279
304
|
}
|
|
280
305
|
|
|
306
|
+
// 字段形状与样张(REQ-012):对象 / 数组 prop 的内部长什么样、真实数据是什么。
|
|
307
|
+
// 上面的 props 表只说得清标量;`{ id, name, phone? }` 那种花括号提示机器读不出来。
|
|
308
|
+
if (entry.spec) {
|
|
309
|
+
lines.push('');
|
|
310
|
+
lines.push('字段形状(名: 类型, 描述, 必填 或 默认 x, 示例 y;缩进即嵌套):');
|
|
311
|
+
lines.push('```');
|
|
312
|
+
lines.push(entry.spec);
|
|
313
|
+
lines.push('```');
|
|
314
|
+
}
|
|
315
|
+
if (entry.sample) {
|
|
316
|
+
lines.push('');
|
|
317
|
+
lines.push('样张(由示例值派生,展示页与视觉矩阵喂的就是它):');
|
|
318
|
+
lines.push('```json');
|
|
319
|
+
lines.push(JSON.stringify(entry.sample, null, 2));
|
|
320
|
+
lines.push('```');
|
|
321
|
+
}
|
|
322
|
+
|
|
281
323
|
if (entry.emits.length) {
|
|
282
324
|
lines.push('');
|
|
283
325
|
lines.push('| emit | payload | 说明 |');
|
|
@@ -14,7 +14,11 @@
|
|
|
14
14
|
import type { SomaComponentDefinition } from './component-definition.types';
|
|
15
15
|
import type { SomaComponentPack } from './component-pack';
|
|
16
16
|
import { PACK_NAME_PATTERN } from './component-pack';
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
findNestedMutableContractDefault,
|
|
19
|
+
type SomaContractField,
|
|
20
|
+
} from '../../shared/types/component-contract.types';
|
|
21
|
+
import { deriveContractSample, hasRenderableSample } from '../../shared/component/derive-sample';
|
|
18
22
|
|
|
19
23
|
const LAYERS = new Set(['atom', 'composite', 'solution', 'template', 'page', 'token']);
|
|
20
24
|
|
|
@@ -32,6 +36,16 @@ export interface VerifyPackOptions {
|
|
|
32
36
|
* 换肤与 RTL 的正确性没有任何自动保障。
|
|
33
37
|
*/
|
|
34
38
|
skipStyleChecks?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 要求 solution 层组件带得出样张(REQ-012,默认开)。
|
|
41
|
+
*
|
|
42
|
+
* 没有示例值的业务组件在展示页上只有一个空壳,在视觉矩阵里量不出真实高度,
|
|
43
|
+
* 在 AI 那边只剩一句 desc——三处都不报错,所以要在这里拦。
|
|
44
|
+
* 存量组件用 samplePending 豁免,清单只减不增。
|
|
45
|
+
*/
|
|
46
|
+
requireSamples?: boolean;
|
|
47
|
+
/** 样张待补清单(组件名):只减不增 */
|
|
48
|
+
samplePending?: readonly string[];
|
|
35
49
|
}
|
|
36
50
|
|
|
37
51
|
/**
|
|
@@ -59,8 +73,12 @@ export function verifyComponentPack(
|
|
|
59
73
|
issues.push('[包] 一个组件都没有');
|
|
60
74
|
}
|
|
61
75
|
|
|
76
|
+
const pending = new Set(options.samplePending ?? []);
|
|
62
77
|
for (const [key, definition] of Object.entries(pack.components)) {
|
|
63
78
|
issues.push(...verifyDefinition(key, definition));
|
|
79
|
+
if (options.requireSamples !== false && !pending.has(key)) {
|
|
80
|
+
issues.push(...verifySample(key, definition));
|
|
81
|
+
}
|
|
64
82
|
}
|
|
65
83
|
issues.push(...verifyStyleRegistration(pack));
|
|
66
84
|
if (!options.skipStyleChecks) {
|
|
@@ -121,6 +139,7 @@ function verifyDefinition(key: string, definition: SomaComponentDefinition): str
|
|
|
121
139
|
`[契约] ${label} 的 prop "${propName}" default.${nestedMutablePath} 是嵌套可变默认值——运行时只隔离最外层(规则 C4a)`,
|
|
122
140
|
);
|
|
123
141
|
}
|
|
142
|
+
issues.push(...verifyFieldTree(label, propName, prop as unknown as SomaContractField));
|
|
124
143
|
}
|
|
125
144
|
|
|
126
145
|
const ai = contract.ai as Record<string, unknown> | undefined;
|
|
@@ -163,6 +182,106 @@ function verifyDefinition(key: string, definition: SomaComponentDefinition): str
|
|
|
163
182
|
return issues;
|
|
164
183
|
}
|
|
165
184
|
|
|
185
|
+
/**
|
|
186
|
+
* 字段树自检(REQ-012)。
|
|
187
|
+
*
|
|
188
|
+
* 查的都是「不报错但坏掉」:示例值类型对不上会静默渲染成错误形状;
|
|
189
|
+
* object 没写 fields 等于形状没描述,展示页与 AI 拿不到任何东西。
|
|
190
|
+
*/
|
|
191
|
+
function verifyFieldTree(label: string, path: string, field: SomaContractField): string[] {
|
|
192
|
+
const issues: string[] = [];
|
|
193
|
+
if (!field || typeof field !== 'object') return issues;
|
|
194
|
+
|
|
195
|
+
if (field.type === 'object' && field.fields === undefined && field.example === undefined) {
|
|
196
|
+
issues.push(
|
|
197
|
+
`[字段] ${label} 的 "${path}" 是 object 却既没有 fields 也没有 example——`
|
|
198
|
+
+ '对象的形状必须写出来,否则展示页与 AI 只能猜',
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (field.type === 'array' && field.items === undefined && field.example === undefined) {
|
|
202
|
+
issues.push(
|
|
203
|
+
`[字段] ${label} 的 "${path}" 是 array 却既没有 items 也没有 example——`
|
|
204
|
+
+ '元素形状必须写出来',
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (field.type === 'enum' && (!field.values || field.values.length === 0)) {
|
|
208
|
+
issues.push(`[字段] ${label} 的 "${path}" 是 enum 却没有 values`);
|
|
209
|
+
}
|
|
210
|
+
if (field.example !== undefined) {
|
|
211
|
+
const mismatch = exampleTypeMismatch(field.example, field);
|
|
212
|
+
if (mismatch) issues.push(`[字段] ${label} 的 "${path}" 示例值${mismatch}`);
|
|
213
|
+
}
|
|
214
|
+
if (field.required === true && field.default !== undefined) {
|
|
215
|
+
issues.push(`[字段] ${label} 的 "${path}" 同时是必填与有默认值——二选一`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
for (const [name, child] of Object.entries(field.fields ?? {})) {
|
|
219
|
+
issues.push(...verifyFieldTree(label, `${path}.${name}`, child));
|
|
220
|
+
}
|
|
221
|
+
if (field.items) {
|
|
222
|
+
issues.push(...verifyFieldTree(label, `${path}[]`, field.items));
|
|
223
|
+
}
|
|
224
|
+
return issues;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** 示例值与字段类型对不上时返回中文原因,对得上返回 null */
|
|
228
|
+
function exampleTypeMismatch(value: unknown, field: SomaContractField): string | null {
|
|
229
|
+
switch (field.type) {
|
|
230
|
+
case 'string':
|
|
231
|
+
return typeof value === 'string' ? null : `应该是字符串,实际是 ${typeName(value)}`;
|
|
232
|
+
case 'number':
|
|
233
|
+
return typeof value === 'number' ? null : `应该是数字,实际是 ${typeName(value)}`;
|
|
234
|
+
case 'boolean':
|
|
235
|
+
return typeof value === 'boolean' ? null : `应该是布尔,实际是 ${typeName(value)}`;
|
|
236
|
+
case 'array':
|
|
237
|
+
return Array.isArray(value) ? null : `应该是数组,实际是 ${typeName(value)}`;
|
|
238
|
+
case 'object':
|
|
239
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
240
|
+
? null
|
|
241
|
+
: `应该是对象,实际是 ${typeName(value)}`;
|
|
242
|
+
case 'enum': {
|
|
243
|
+
const values = field.values ?? [];
|
|
244
|
+
if (values.length === 0) return null;
|
|
245
|
+
return values.includes(value as string | number)
|
|
246
|
+
? null
|
|
247
|
+
: `不在取值集合里(${values.join(' | ')})`;
|
|
248
|
+
}
|
|
249
|
+
default:
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function typeName(value: unknown): string {
|
|
255
|
+
if (value === null) return 'null';
|
|
256
|
+
if (Array.isArray(value)) return '数组';
|
|
257
|
+
const names: Record<string, string> = {
|
|
258
|
+
string: '字符串',
|
|
259
|
+
number: '数字',
|
|
260
|
+
boolean: '布尔',
|
|
261
|
+
object: '对象',
|
|
262
|
+
};
|
|
263
|
+
return names[typeof value] ?? typeof value;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** solution 层必须派生得出一份有内容的样张 */
|
|
267
|
+
function verifySample(key: string, definition: SomaComponentDefinition): string[] {
|
|
268
|
+
const contract = definition?.contract;
|
|
269
|
+
if (!contract || contract.layer !== 'solution') return [];
|
|
270
|
+
|
|
271
|
+
const issues: string[] = [];
|
|
272
|
+
if (!hasRenderableSample(contract)) {
|
|
273
|
+
issues.push(
|
|
274
|
+
`[样张] 组件 ${key} 的 props 一个 example 都没有——`
|
|
275
|
+
+ '展示页只会渲染出一个空壳,视觉矩阵量不出真实高度,AI 也看不到数据形状(REQ-012)',
|
|
276
|
+
);
|
|
277
|
+
return issues;
|
|
278
|
+
}
|
|
279
|
+
for (const gap of deriveContractSample(contract).gaps) {
|
|
280
|
+
issues.push(`[样张] 组件 ${key} 的 "${gap.path}":${gap.reason}`);
|
|
281
|
+
}
|
|
282
|
+
return issues;
|
|
283
|
+
}
|
|
284
|
+
|
|
166
285
|
/** 样式登记的键必须是本包真的提供的组件/分片 */
|
|
167
286
|
function verifyStyleRegistration(pack: SomaComponentPack): string[] {
|
|
168
287
|
const issues: string[] = [];
|
|
@@ -170,8 +170,8 @@ export class SomaContainerAdapter implements IContainerAdapter {
|
|
|
170
170
|
return globalThis.performance?.now?.() ?? Date.now();
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
private resolveIncludes(
|
|
174
|
-
const elements =
|
|
173
|
+
private resolveIncludes(root: ParentNode, includes: Record<string, string>): void {
|
|
174
|
+
const elements = root.querySelectorAll<HTMLElement>('[data-soma-include]');
|
|
175
175
|
for (const element of elements) {
|
|
176
176
|
const includePath = element.getAttribute('data-soma-include');
|
|
177
177
|
if (!includePath || !includes[includePath]) {
|
|
@@ -182,5 +182,9 @@ export class SomaContainerAdapter implements IContainerAdapter {
|
|
|
182
182
|
element.removeAttribute('data-soma-include');
|
|
183
183
|
this.resolveIncludes(element, includes);
|
|
184
184
|
}
|
|
185
|
+
|
|
186
|
+
for (const template of root.querySelectorAll<HTMLTemplateElement>('template')) {
|
|
187
|
+
this.resolveIncludes(template.content, includes);
|
|
188
|
+
}
|
|
185
189
|
}
|
|
186
190
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SomaHttpClient, SomaHttpClientConfig } from '@/core/client/base/SomaHttpClient';
|
|
1
|
+
import { SomaHttpClient, SomaHttpClientConfig, describeHttpErrorBody } from '@/core/client/base/SomaHttpClient';
|
|
2
2
|
import { IHttpClient, HttpRequestConfig, UploadOptions } from './IHttpClient';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -195,7 +195,7 @@ export class SomaHttpService implements IHttpClient {
|
|
|
195
195
|
? await res.text().catch(() => '')
|
|
196
196
|
: '';
|
|
197
197
|
this.onHttpError?.(res.status, body || res.statusText);
|
|
198
|
-
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
198
|
+
throw new Error(`HTTP ${res.status}: ${describeHttpErrorBody(body, res.statusText)}`);
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
return (await res.json()) as T;
|
package/core/index.ts
CHANGED
|
@@ -286,6 +286,7 @@ export type {
|
|
|
286
286
|
SomaComponentContract,
|
|
287
287
|
SomaComponentLayer,
|
|
288
288
|
SomaGeometryFamily,
|
|
289
|
+
SomaContractField,
|
|
289
290
|
SomaContractProp,
|
|
290
291
|
SomaContractEmit,
|
|
291
292
|
SomaContractSlot,
|
|
@@ -294,6 +295,26 @@ export type {
|
|
|
294
295
|
SomaContractToken,
|
|
295
296
|
} from './shared/types/component-contract.types';
|
|
296
297
|
export { findNestedMutableContractDefault } from './shared/types/component-contract.types';
|
|
298
|
+
|
|
299
|
+
// ── 字段行文法与样张派生(REQ-012) ──
|
|
300
|
+
// 契约里的 props / emits / slots 可以写成文本块:一行一个字段,缩进即嵌套。
|
|
301
|
+
// 示例值挂在字段上,样张是它的函数——不另写一份,就不会有两个真源。
|
|
302
|
+
export {
|
|
303
|
+
parseFieldLines,
|
|
304
|
+
parseEmitLines,
|
|
305
|
+
parseSlotLines,
|
|
306
|
+
renderFieldLines,
|
|
307
|
+
somaFields,
|
|
308
|
+
somaEmits,
|
|
309
|
+
somaSlots,
|
|
310
|
+
SomaFieldLinesError,
|
|
311
|
+
} from './shared/component/field-lines';
|
|
312
|
+
export {
|
|
313
|
+
deriveContractSample,
|
|
314
|
+
deriveFieldSample,
|
|
315
|
+
hasRenderableSample,
|
|
316
|
+
} from './shared/component/derive-sample';
|
|
317
|
+
export type { DeriveSampleResult, SampleGap } from './shared/component/derive-sample';
|
|
297
318
|
// 组件级令牌(REQ-009 M-1)的命名规则。宿主与闸门都要按同一条规则拼名字——
|
|
298
319
|
// 各自拼一遍的话,主题的 `components` 段覆盖不上任何东西,而覆盖不上是静默的。
|
|
299
320
|
export {
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// 样张派生(REQ-012):契约字段的 example → 一份可直接渲染的 props。
|
|
2
|
+
//
|
|
3
|
+
// 为什么是派生而不是另写一份样张:另写一份就有两个真源,改了 prop 忘了改样张不会
|
|
4
|
+
// 报错——展示页与视觉矩阵会安静地拿旧形状渲染。示例值挂在字段上,样张是它的函数。
|
|
5
|
+
//
|
|
6
|
+
// 派生规则(对每个字段,递归):
|
|
7
|
+
// 1. example 有值 → 用它
|
|
8
|
+
// 2. type object → 逐 fields 递归
|
|
9
|
+
// 3. type array → 用 items 派生一个元素,组成单元素数组
|
|
10
|
+
// 4. 否则取 default
|
|
11
|
+
// 5. required 却以上都无 → 记一条缺口(由闸门报,不在这里抛)
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
SomaComponentContract,
|
|
15
|
+
SomaContractField,
|
|
16
|
+
} from '../types/component-contract.types';
|
|
17
|
+
|
|
18
|
+
export interface SampleGap {
|
|
19
|
+
/** 缺口路径,如 `order.items[].sku_id` */
|
|
20
|
+
path: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DeriveSampleResult {
|
|
25
|
+
/** 可直接喂给组件的 props */
|
|
26
|
+
props: Record<string, unknown>;
|
|
27
|
+
/** 必填却既无 example 也无 default 的字段 */
|
|
28
|
+
gaps: SampleGap[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function deriveField(field: SomaContractField, path: string, gaps: SampleGap[]): unknown {
|
|
32
|
+
if (field.example !== undefined) return clone(field.example);
|
|
33
|
+
|
|
34
|
+
if (field.type === 'object') {
|
|
35
|
+
const fields = field.fields;
|
|
36
|
+
if (fields && Object.keys(fields).length > 0) {
|
|
37
|
+
const out: Record<string, unknown> = {};
|
|
38
|
+
for (const [name, child] of Object.entries(fields)) {
|
|
39
|
+
const value = deriveField(child, `${path}.${name}`, gaps);
|
|
40
|
+
if (value !== undefined) out[name] = value;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (field.type === 'array') {
|
|
47
|
+
const items = field.items;
|
|
48
|
+
if (items) {
|
|
49
|
+
const element = deriveField(items, `${path}[]`, gaps);
|
|
50
|
+
if (element !== undefined) return [element];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (field.default !== undefined) return clone(field.default);
|
|
55
|
+
|
|
56
|
+
if (field.type === 'enum' && field.values && field.values.length > 0) {
|
|
57
|
+
// 枚举没写示例也没写默认时取第一个取值:比空串更接近真实形状,且不用作者多写一行
|
|
58
|
+
return field.values[0];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (field.required === true) {
|
|
62
|
+
gaps.push({
|
|
63
|
+
path,
|
|
64
|
+
reason: '必填字段既没有 示例 也没有 默认——样张渲染不出这一格',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function clone<T>(value: T): T {
|
|
71
|
+
if (value === null || typeof value !== 'object') return value;
|
|
72
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 从契约派生一份样张 props */
|
|
76
|
+
export function deriveContractSample(contract: SomaComponentContract): DeriveSampleResult {
|
|
77
|
+
const gaps: SampleGap[] = [];
|
|
78
|
+
const props: Record<string, unknown> = {};
|
|
79
|
+
for (const [name, prop] of Object.entries(contract.props ?? {})) {
|
|
80
|
+
const value = deriveField(prop, name, gaps);
|
|
81
|
+
if (value !== undefined) props[name] = value;
|
|
82
|
+
}
|
|
83
|
+
return { props, gaps };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 单个字段的样张值(组件内部或工具按需调用) */
|
|
87
|
+
export function deriveFieldSample(field: SomaContractField): unknown {
|
|
88
|
+
return deriveField(field, '', []);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 这份契约够不够渲染一张有内容的样张。
|
|
93
|
+
*
|
|
94
|
+
* 判据是「至少有一个 prop 写了 example,或有嵌套字段写了 example」——
|
|
95
|
+
* 全靠 default 派生出来的是空态,展示页放上去只有一个空壳。
|
|
96
|
+
*/
|
|
97
|
+
export function hasRenderableSample(contract: SomaComponentContract): boolean {
|
|
98
|
+
const seen = (field: SomaContractField): boolean => {
|
|
99
|
+
if (field.example !== undefined) return true;
|
|
100
|
+
if (field.fields && Object.values(field.fields).some(seen)) return true;
|
|
101
|
+
if (field.items && seen(field.items)) return true;
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
return Object.values(contract.props ?? {}).some(seen);
|
|
105
|
+
}
|