@contentful/experience-design-system-types 2.42.1-dev-build-f69db6bf.0 → 2.43.0-alpha.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.
- package/README.md +15 -0
- package/dist/package.json +1 -1
- package/dist/src/agent-process-lifecycle.d.ts +26 -0
- package/dist/src/agent-process-lifecycle.js +171 -0
- package/dist/src/agent-prompt-artifact.d.ts +8 -0
- package/dist/src/agent-prompt-artifact.js +37 -0
- package/dist/src/agent-prompt-transport.d.ts +1 -0
- package/dist/src/agent-prompt-transport.js +4 -0
- package/dist/src/cdf/build.d.ts +9 -2
- package/dist/src/cdf/build.js +134 -17
- package/dist/src/cdf/finite-values.d.ts +8 -0
- package/dist/src/cdf/finite-values.js +21 -0
- package/dist/src/cdf/index.d.ts +2 -1
- package/dist/src/cdf/index.js +1 -0
- package/dist/src/cdf/property-validation.d.ts +16 -0
- package/dist/src/cdf/property-validation.js +211 -0
- package/dist/src/cdf/representation-validation.d.ts +6 -0
- package/dist/src/cdf/representation-validation.js +182 -0
- package/dist/src/cdf/schema.d.ts +778 -20
- package/dist/src/cdf/schema.js +362 -5
- package/dist/src/cdf/types.d.ts +57 -59
- package/dist/src/cdf/types.js +72 -15
- package/dist/src/cdf/validate.js +31 -1
- package/dist/src/cdf/vocabularies.d.ts +1 -1
- package/dist/src/cdf/vocabularies.js +9 -1
- package/dist/src/config/index.js +6 -3
- package/dist/src/exploration/constants.d.ts +10 -0
- package/dist/src/exploration/constants.js +18 -0
- package/dist/src/exploration/index.d.ts +3 -0
- package/dist/src/exploration/index.js +3 -0
- package/dist/src/exploration/schemas.d.ts +201 -0
- package/dist/src/exploration/schemas.js +126 -0
- package/dist/src/exploration/types.d.ts +393 -0
- package/dist/src/exploration/types.js +1 -0
- package/dist/src/handoff/index.d.ts +1 -1
- package/dist/src/index.d.ts +15 -1
- package/dist/src/index.js +12 -4
- package/dist/src/property-contract/analysis-representation-candidates.d.ts +19 -0
- package/dist/src/property-contract/analysis-representation-candidates.js +45 -0
- package/dist/src/property-contract/classification.d.ts +38 -0
- package/dist/src/property-contract/classification.js +84 -0
- package/dist/src/property-contract/compact-model-components.d.ts +16 -0
- package/dist/src/property-contract/compact-model-components.js +113 -0
- package/dist/src/property-contract/content-digest.d.ts +2 -0
- package/dist/src/property-contract/content-digest.js +69 -0
- package/dist/src/property-contract/decision-protocol.d.ts +45 -0
- package/dist/src/property-contract/decision-protocol.js +251 -0
- package/dist/src/property-contract/definition-import-rules.d.ts +10 -0
- package/dist/src/property-contract/definition-import-rules.js +105 -0
- package/dist/src/property-contract/definition-import.d.ts +13 -0
- package/dist/src/property-contract/definition-import.js +52 -0
- package/dist/src/property-contract/discriminated-union-source.d.ts +17 -0
- package/dist/src/property-contract/discriminated-union-source.js +180 -0
- package/dist/src/property-contract/import-prerequisites.d.ts +70 -0
- package/dist/src/property-contract/import-prerequisites.js +208 -0
- package/dist/src/property-contract/index.d.ts +18 -1
- package/dist/src/property-contract/index.js +18 -6
- package/dist/src/property-contract/logical-source-graph.d.ts +36 -0
- package/dist/src/property-contract/logical-source-graph.js +240 -0
- package/dist/src/property-contract/mapper-readiness.d.ts +6 -0
- package/dist/src/property-contract/mapper-readiness.js +34 -0
- package/dist/src/property-contract/model-exploration.d.ts +42 -0
- package/dist/src/property-contract/model-exploration.js +94 -0
- package/dist/src/property-contract/native-source-region.d.ts +5 -0
- package/dist/src/property-contract/native-source-region.js +125 -0
- package/dist/src/property-contract/null-handling.d.ts +10 -0
- package/dist/src/property-contract/null-handling.js +45 -0
- package/dist/src/property-contract/physical-property-import.d.ts +6 -0
- package/dist/src/property-contract/physical-property-import.js +55 -0
- package/dist/src/property-contract/property-decision.d.ts +14 -0
- package/dist/src/property-contract/property-decision.js +155 -0
- package/dist/src/property-contract/property-model.d.ts +197 -0
- package/dist/src/property-contract/property-model.js +833 -0
- package/dist/src/property-contract/representation-plan.d.ts +133 -0
- package/dist/src/property-contract/representation-plan.js +756 -0
- package/dist/src/property-contract/representation-proposal.d.ts +65 -0
- package/dist/src/property-contract/representation-proposal.js +204 -0
- package/dist/src/property-contract/resource-link-schema.d.ts +55 -0
- package/dist/src/property-contract/resource-link-schema.js +28 -0
- package/dist/src/property-contract/source-representation.d.ts +9 -0
- package/dist/src/property-contract/source-representation.js +79 -0
- package/dist/src/property-contract/target-definition.d.ts +17 -16
- package/dist/src/property-contract/target-definition.js +4 -26
- package/dist/src/schema-export.d.ts +30 -0
- package/dist/src/schema-export.js +285 -0
- package/dist/src/shared-source.d.ts +105 -0
- package/dist/src/shared-source.js +1 -0
- package/dist/src/source-input-association.d.ts +23 -0
- package/dist/src/source-input-association.js +52 -0
- package/dist/src/sources-api/preview/index.d.ts +37 -3
- package/dist/src/sources-api/preview/index.js +39 -1
- package/dist/src/sources-api/preview/schema.d.ts +2 -0
- package/dist/src/sources-api/preview/schema.js +74 -0
- package/package.json +1 -1
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
import { validateRepresentationProposal } from './representation-proposal.js';
|
|
2
|
+
import { cdfToTarget } from '../cdf/property-validation.js';
|
|
3
|
+
import { validateCDFProperty } from '../cdf/property-validation.js';
|
|
4
|
+
import { cloneJson, freezeJson, jsonEqual, jsonValueIssue, own, } from './json-value.js';
|
|
5
|
+
import { contentDigest } from './content-digest.js';
|
|
6
|
+
import { propertyMapperBlockers } from './mapper-readiness.js';
|
|
7
|
+
import { prepareLogicalSourceGraph, } from './logical-source-graph.js';
|
|
8
|
+
import { nativeSourceRegion, sourceRecordFields } from './native-source-region.js';
|
|
9
|
+
import { propertyDefinitionImportIssues } from './physical-property-import.js';
|
|
10
|
+
import { compactModelComponents } from './compact-model-components.js';
|
|
11
|
+
import { applyModelComponentExploration } from './model-exploration.js';
|
|
12
|
+
import { LOCAL_NATIVE_PROPERTY_PROFILE, projectProperty, validateSourceGraph, validateSourceValue, } from './property-model.js';
|
|
13
|
+
/** Attach a validated model plan without claiming an ordinary source composition slot. */
|
|
14
|
+
export function attachComponentModelPlan(entry, plan) {
|
|
15
|
+
if (plan.kind !== 'component-model' || !plan.attachment)
|
|
16
|
+
throw new Error('A component model requires a root attachment');
|
|
17
|
+
const issues = validateRepresentationPlan(plan, plan.sourceModel, plan.contextSnapshot);
|
|
18
|
+
if (issues.length)
|
|
19
|
+
throw new Error(issues.map((issue) => `${issue.path}: ${issue.message}`).join('; '));
|
|
20
|
+
const result = cloneJson(entry);
|
|
21
|
+
const property = plan.identity.key;
|
|
22
|
+
const previous = result.$representationPlans?.[property];
|
|
23
|
+
if (result.$slots?.[plan.attachment.slot] &&
|
|
24
|
+
(!previous?.attachment || !jsonEqual(result.$slots[plan.attachment.slot], previous.attachment.definition)))
|
|
25
|
+
throw new Error('Source slot and generated model attachment collide');
|
|
26
|
+
const reserved = new Set(Object.entries(result.$representationPlans ?? {})
|
|
27
|
+
.filter(([name]) => name !== property)
|
|
28
|
+
.flatMap(([, other]) => other.models.map((generated) => generated.key)));
|
|
29
|
+
if (plan.models.some((generated) => reserved.has(generated.key)))
|
|
30
|
+
throw new Error('Generated model component identity collision');
|
|
31
|
+
if (previous?.attachment && result.$slots)
|
|
32
|
+
delete result.$slots[previous.attachment.slot];
|
|
33
|
+
delete result.$properties[property];
|
|
34
|
+
result.$representationPlans ??= Object.create(null);
|
|
35
|
+
result.$representationPlans[property] = cloneJson(plan);
|
|
36
|
+
result.$slots ??= Object.create(null);
|
|
37
|
+
result.$slots[plan.attachment.slot] = cloneJson(plan.attachment.definition);
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
function sourceDigest(model) {
|
|
41
|
+
return contentDigest({ identity: model.identity, source: model.source });
|
|
42
|
+
}
|
|
43
|
+
function localContext(definition) {
|
|
44
|
+
const result = Object.create(null);
|
|
45
|
+
const visit = (field, path) => {
|
|
46
|
+
result[path + ':mapper'] = 'enabled';
|
|
47
|
+
result[path + ':default'] = 'enabled';
|
|
48
|
+
if (field.$items)
|
|
49
|
+
visit(field.$items, path + '/items');
|
|
50
|
+
if (field.valueType)
|
|
51
|
+
visit(field.valueType, path + '/valueType');
|
|
52
|
+
for (const [index, member] of (field.members ?? []).entries())
|
|
53
|
+
visit(member, path + '/members/' + index);
|
|
54
|
+
if (field.$fields)
|
|
55
|
+
for (const [key, child] of Object.entries(field.$fields))
|
|
56
|
+
visit(child, path + '/fields/' + key.replace(/~/g, '~0').replace(/\//g, '~1'));
|
|
57
|
+
};
|
|
58
|
+
visit(definition, '');
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
export function deriveEditableRepresentationPlans(model, options = {}) {
|
|
62
|
+
if (model.identity.nodeId !== model.source.graph.rootId ||
|
|
63
|
+
Object.values(model.identity).some((value) => typeof value !== 'string' || !value))
|
|
64
|
+
throw new Error('Representation source owner identity is incomplete or mismatched');
|
|
65
|
+
const invalid = validateSourceGraph(model.source.graph);
|
|
66
|
+
if (invalid.length)
|
|
67
|
+
throw new Error(invalid.map((issue) => issue.message).join('; '));
|
|
68
|
+
if (options.proposal) {
|
|
69
|
+
const validation = validateRepresentationProposal(options.proposal, model, () => ({
|
|
70
|
+
status: 'unknown',
|
|
71
|
+
requirements: [{ path: '/', capability: 'source-reconstruction', availability: 'unknown' }],
|
|
72
|
+
}));
|
|
73
|
+
const desired = model.representation ?? model.source.shape;
|
|
74
|
+
if (!validation.valid ||
|
|
75
|
+
options.proposal.name !== model.identity.key ||
|
|
76
|
+
!desired ||
|
|
77
|
+
!jsonEqual(options.proposal.desiredDefinition, cdfToTarget(desired)))
|
|
78
|
+
throw new Error('Proposal does not match the current source and desired definition context');
|
|
79
|
+
}
|
|
80
|
+
const sourceGraph = model.source.graph;
|
|
81
|
+
const graph = prepareLogicalSourceGraph(model);
|
|
82
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
83
|
+
const root = nodes.get(graph.rootId);
|
|
84
|
+
const category = model.category ?? 'content';
|
|
85
|
+
const intent = cloneJson(options.defaultIntent ?? model.defaultIntent);
|
|
86
|
+
const defaultIssues = intent.action === 'set' ? validateSourceValue(sourceGraph, intent.value) : [];
|
|
87
|
+
const base = {
|
|
88
|
+
schemaVersion: 1,
|
|
89
|
+
identity: cloneJson(model.identity),
|
|
90
|
+
sourceModel: cloneJson(model),
|
|
91
|
+
...(options.context === undefined ? {} : { contextSnapshot: cloneJson(options.context) }),
|
|
92
|
+
logicalGraph: cloneJson(graph),
|
|
93
|
+
sourceDigest: sourceDigest(model),
|
|
94
|
+
contextDigest: contentDigest(options.context ?? LOCAL_NATIVE_PROPERTY_PROFILE),
|
|
95
|
+
category,
|
|
96
|
+
defaultIntent: intent,
|
|
97
|
+
...(options.proposal ? { proposal: cloneJson(options.proposal) } : {}),
|
|
98
|
+
...(model.source.default ? { sourceDefault: cloneJson(model.source.default) } : {}),
|
|
99
|
+
};
|
|
100
|
+
const finish = (input) => {
|
|
101
|
+
const plan = cloneJson(input);
|
|
102
|
+
plan.id = contentDigest(input);
|
|
103
|
+
for (const generated of plan.models) {
|
|
104
|
+
generated.entry.$model = {
|
|
105
|
+
planId: plan.id,
|
|
106
|
+
componentId: model.identity.componentId,
|
|
107
|
+
propertyId: model.identity.propertyId,
|
|
108
|
+
nodeId: generated.nodeId,
|
|
109
|
+
sourceNodeId: nodes.get(generated.nodeId).sourceNodeId,
|
|
110
|
+
};
|
|
111
|
+
for (const slot of Object.values(generated.entry.$slots ?? {}))
|
|
112
|
+
if (slot.$modelBinding)
|
|
113
|
+
slot.$modelBinding.planId = plan.id;
|
|
114
|
+
}
|
|
115
|
+
if (plan.attachment?.definition.$modelBinding)
|
|
116
|
+
plan.attachment.definition.$modelBinding.planId = plan.id;
|
|
117
|
+
return freezeJson(cloneJson(plan));
|
|
118
|
+
};
|
|
119
|
+
const plans = [];
|
|
120
|
+
const mappingFacts = (node) => ({
|
|
121
|
+
nodeId: node.id,
|
|
122
|
+
sourceNodeId: node.sourceNodeId,
|
|
123
|
+
path: cloneJson(node.path),
|
|
124
|
+
required: node.required,
|
|
125
|
+
readonly: node.readonly,
|
|
126
|
+
occurrenceReadonly: node.occurrenceReadonly,
|
|
127
|
+
intrinsicReadonly: node.intrinsicReadonly,
|
|
128
|
+
nullable: node.nullable ?? null,
|
|
129
|
+
undefinable: node.undefinable ?? null,
|
|
130
|
+
...(node.defaultIntent ? { defaultIntent: cloneJson(node.defaultIntent) } : {}),
|
|
131
|
+
...(node.branchConditions ? { branchConditions: cloneJson(node.branchConditions) } : {}),
|
|
132
|
+
});
|
|
133
|
+
const emptyValueRequirements = graph.nodes
|
|
134
|
+
.filter((node) => node.kind === 'record' && !node.fields?.length && !node.indexSignatures?.length)
|
|
135
|
+
.map((node) => ({ path: '/source/' + node.id, capability: 'empty-source-value-materialization' }));
|
|
136
|
+
const valueStateRequirements = graph.nodes
|
|
137
|
+
.filter((node) => node.nullable ||
|
|
138
|
+
node.undefinable ||
|
|
139
|
+
['null', 'undefined'].includes(node.kind) ||
|
|
140
|
+
node.nullability?.null.state === 'unknown' ||
|
|
141
|
+
node.nullability?.undefined.state === 'unknown')
|
|
142
|
+
.map((node) => ({ path: '/source/' + node.id, capability: 'presence-null-state' }));
|
|
143
|
+
const derived = nativeSourceRegion(root.id, nodes);
|
|
144
|
+
const original = model.representation;
|
|
145
|
+
const ordinary = original && ['string', 'number', 'boolean', 'record', 'array'].includes(original.$type);
|
|
146
|
+
const rawNative = !original || ordinary ? (derived ? { ...(original ?? {}), ...derived } : original) : original;
|
|
147
|
+
const native = rawNative
|
|
148
|
+
? { ...cloneJson(rawNative), $category: category }
|
|
149
|
+
: undefined;
|
|
150
|
+
if (native) {
|
|
151
|
+
const candidate = { ...model, representation: native, defaultIntent: intent };
|
|
152
|
+
const projection = projectProperty(candidate, LOCAL_NATIVE_PROPERTY_PROFILE, {
|
|
153
|
+
capabilities: localContext(native),
|
|
154
|
+
});
|
|
155
|
+
if (projection.outcome !== 'unsupported' && projection.target) {
|
|
156
|
+
const applyDefaults = (field, target) => {
|
|
157
|
+
if (own(target, 'defaultValue'))
|
|
158
|
+
field.$default = cloneJson(target.defaultValue);
|
|
159
|
+
else
|
|
160
|
+
delete field.$default;
|
|
161
|
+
if (field.$items && target.items)
|
|
162
|
+
applyDefaults(field.$items, target.items);
|
|
163
|
+
if (field.valueType && target.valueType)
|
|
164
|
+
applyDefaults(field.valueType, target.valueType);
|
|
165
|
+
if (field.members && target.members)
|
|
166
|
+
field.members.forEach((member, index) => applyDefaults(member, target.members[index]));
|
|
167
|
+
if (field.$fields && target.fields)
|
|
168
|
+
for (const [key, child] of Object.entries(field.$fields))
|
|
169
|
+
applyDefaults(child, target.fields[key]);
|
|
170
|
+
};
|
|
171
|
+
if (projection.target)
|
|
172
|
+
applyDefaults(native, projection.target);
|
|
173
|
+
const importIssues = propertyDefinitionImportIssues(native);
|
|
174
|
+
plans.push(finish({
|
|
175
|
+
...base,
|
|
176
|
+
kind: 'native',
|
|
177
|
+
nativeDefinition: cloneJson(native),
|
|
178
|
+
models: [],
|
|
179
|
+
mappings: graph.nodes.map((node) => ({
|
|
180
|
+
...mappingFacts(node),
|
|
181
|
+
disposition: 'native-field',
|
|
182
|
+
field: model.identity.key,
|
|
183
|
+
withinValue: {
|
|
184
|
+
ownerNodeId: root.id,
|
|
185
|
+
path: cloneJson(node.path
|
|
186
|
+
.slice(root.path.length)
|
|
187
|
+
.filter((segment) => !['union', 'intersection'].includes(segment.kind))),
|
|
188
|
+
},
|
|
189
|
+
...(node.kind === 'reference' ? { targetNodeId: node.alternatives?.[0] } : {}),
|
|
190
|
+
})),
|
|
191
|
+
issues: [...defaultIssues, ...importIssues],
|
|
192
|
+
sdkValid: !defaultIssues.length && !importIssues.length && graph.coverage === 'complete-within-declared-bound',
|
|
193
|
+
requirements: [
|
|
194
|
+
...valueStateRequirements,
|
|
195
|
+
...emptyValueRequirements,
|
|
196
|
+
...(projection.outcome === 'conditional'
|
|
197
|
+
? projection.requirements
|
|
198
|
+
.filter((requirement) => requirement.availability !== 'enabled')
|
|
199
|
+
.map(({ path, capability }) => ({ path, capability }))
|
|
200
|
+
: []),
|
|
201
|
+
...propertyMapperBlockers(native).map((path) => ({ path, capability: 'deployed-mapper' })),
|
|
202
|
+
...(['RichText', 'JSON', 'ResourceLink'].includes(native.$type) && model.source.default
|
|
203
|
+
? [{ path: '/', capability: 'source-default-materialization' }]
|
|
204
|
+
: []),
|
|
205
|
+
...(model.representationProposal?.compatibility?.unresolvedRequirements ?? []).map((capability) => ({
|
|
206
|
+
path: '/',
|
|
207
|
+
capability,
|
|
208
|
+
})),
|
|
209
|
+
],
|
|
210
|
+
}));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (category !== 'content' && graph.nodes.some((node) => node.unionBranches))
|
|
214
|
+
return plans;
|
|
215
|
+
const mappings = new Map();
|
|
216
|
+
const models = new Map();
|
|
217
|
+
const branchSelections = [];
|
|
218
|
+
const issues = [...defaultIssues];
|
|
219
|
+
const requirements = [
|
|
220
|
+
{ path: '/', capability: 'source-reconstruction' },
|
|
221
|
+
{ path: '/', capability: 'component-model-mapper' },
|
|
222
|
+
];
|
|
223
|
+
if (intent.action === 'absent' && model.source.default?.state === 'unsupported')
|
|
224
|
+
requirements.push({ path: '/', capability: 'source-default-evaluation' });
|
|
225
|
+
const bound = (node, disposition, extra = {}) => {
|
|
226
|
+
if (!mappings.has(node.id))
|
|
227
|
+
mappings.set(node.id, {
|
|
228
|
+
...mappingFacts(node),
|
|
229
|
+
disposition,
|
|
230
|
+
...extra,
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
requirements.push(...valueStateRequirements, ...emptyValueRequirements);
|
|
234
|
+
if (graph.nodes.some((node) => node.defaultIntent))
|
|
235
|
+
requirements.push({ path: '/', capability: 'source-structural-default-materialization' });
|
|
236
|
+
const binding = (nodeId) => ({
|
|
237
|
+
planId: '',
|
|
238
|
+
componentId: model.identity.componentId,
|
|
239
|
+
propertyId: model.identity.propertyId,
|
|
240
|
+
nodeId,
|
|
241
|
+
sourceNodeId: nodes.get(nodeId).sourceNodeId,
|
|
242
|
+
});
|
|
243
|
+
const generatedKey = (node) => 'Model_' + contentDigest({ owner: model.identity, nodeId: node.id }).slice(0, 32);
|
|
244
|
+
const hold = (node, reason) => {
|
|
245
|
+
mappings.delete(node.id);
|
|
246
|
+
bound(node, 'held', { reason });
|
|
247
|
+
issues.push({ path: '/source/' + node.id, message: reason });
|
|
248
|
+
};
|
|
249
|
+
const activeModels = new Set();
|
|
250
|
+
const resolvingReferences = new Set();
|
|
251
|
+
const nativeMapping = (id, componentKey, field, ownerId = id) => {
|
|
252
|
+
const pending = [id];
|
|
253
|
+
const visited = new Set();
|
|
254
|
+
const owner = nodes.get(ownerId);
|
|
255
|
+
while (pending.length) {
|
|
256
|
+
const current = nodes.get(pending.pop());
|
|
257
|
+
if (visited.has(current.id))
|
|
258
|
+
continue;
|
|
259
|
+
visited.add(current.id);
|
|
260
|
+
bound(current, 'native-field', {
|
|
261
|
+
componentKey,
|
|
262
|
+
field,
|
|
263
|
+
withinValue: {
|
|
264
|
+
ownerNodeId: ownerId,
|
|
265
|
+
path: cloneJson(current.path
|
|
266
|
+
.slice(owner.path.length)
|
|
267
|
+
.filter((segment) => !['union', 'intersection'].includes(segment.kind))),
|
|
268
|
+
},
|
|
269
|
+
...(current.kind === 'reference' ? { targetNodeId: current.alternatives?.[0] } : {}),
|
|
270
|
+
});
|
|
271
|
+
if (current.kind === 'reference')
|
|
272
|
+
continue;
|
|
273
|
+
pending.push(...(current.fields?.map((item) => item.nodeId) ?? []), ...(current.items ?? []), ...(current.alternatives ?? []));
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const nativeProperty = (id) => {
|
|
277
|
+
const field = nativeSourceRegion(id, nodes);
|
|
278
|
+
const definition = field
|
|
279
|
+
? { ...field, $category: category === 'unattached' ? 'content' : category }
|
|
280
|
+
: undefined;
|
|
281
|
+
return definition && !propertyDefinitionImportIssues(definition).length ? definition : undefined;
|
|
282
|
+
};
|
|
283
|
+
const modelFor = (id) => {
|
|
284
|
+
const node = nodes.get(id);
|
|
285
|
+
if (node.reason === 'branch-occurrence-expansion-bound') {
|
|
286
|
+
hold(node, 'Source retained; branch occurrence expansion reached its declared resource bound');
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (node.kind === 'reference') {
|
|
290
|
+
const target = node.alternatives?.[0];
|
|
291
|
+
if (!target || !nodes.has(target)) {
|
|
292
|
+
hold(node, 'Source reference has no current logical target');
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (resolvingReferences.has(id) && !models.has(generatedKey(nodes.get(target)))) {
|
|
296
|
+
hold(node, 'Reference cycle has no structural model target');
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
bound(node, 'reference', { targetNodeId: target });
|
|
300
|
+
if (activeModels.has(target))
|
|
301
|
+
requirements.push({ path: '/source/' + id, capability: 'cyclic-model-dependencies' });
|
|
302
|
+
const existing = models.get(generatedKey(nodes.get(target)));
|
|
303
|
+
if (existing)
|
|
304
|
+
return existing.key;
|
|
305
|
+
resolvingReferences.add(id);
|
|
306
|
+
try {
|
|
307
|
+
return modelFor(target);
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
resolvingReferences.delete(id);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const key = generatedKey(node);
|
|
314
|
+
if (models.has(key)) {
|
|
315
|
+
if (activeModels.has(id))
|
|
316
|
+
requirements.push({ path: '/source/' + id, capability: 'cyclic-model-dependencies' });
|
|
317
|
+
return key;
|
|
318
|
+
}
|
|
319
|
+
const entry = {
|
|
320
|
+
$type: 'component',
|
|
321
|
+
$properties: Object.create(null),
|
|
322
|
+
$slots: Object.create(null),
|
|
323
|
+
};
|
|
324
|
+
models.set(key, { key, nodeId: id, entry });
|
|
325
|
+
activeModels.add(id);
|
|
326
|
+
bound(node, 'model', { componentKey: key });
|
|
327
|
+
const addSlot = (name, target, cardinality) => {
|
|
328
|
+
const targetKey = modelFor(target.id);
|
|
329
|
+
if (!targetKey)
|
|
330
|
+
return;
|
|
331
|
+
entry.$slots[name] = {
|
|
332
|
+
$required: cardinality.min > 0,
|
|
333
|
+
$allowedComponents: [targetKey],
|
|
334
|
+
$cardinality: cardinality,
|
|
335
|
+
$modelBinding: binding(target.id),
|
|
336
|
+
};
|
|
337
|
+
requirements.push({
|
|
338
|
+
path: '/' + key + '/$slots/' + name,
|
|
339
|
+
capability: cardinality.ordered ? 'ordered-slot-cardinality' : 'singleton-slot-cardinality',
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
const fields = sourceRecordFields(node, nodes);
|
|
343
|
+
if (fields) {
|
|
344
|
+
for (const field of fields) {
|
|
345
|
+
const child = nodes.get(field.nodeId);
|
|
346
|
+
const definition = nativeProperty(child.id);
|
|
347
|
+
if (definition) {
|
|
348
|
+
definition.$required = field.required;
|
|
349
|
+
const branch = node.branchConditions?.at(-1);
|
|
350
|
+
if (branch?.sourceMemberNodeId === node.sourceNodeId && branch.discriminator === field.key) {
|
|
351
|
+
definition.$type = 'Literal';
|
|
352
|
+
definition.value = branch.tag;
|
|
353
|
+
definition.valueType = { $type: typeof branch.tag };
|
|
354
|
+
definition.$required = true;
|
|
355
|
+
delete definition.$values;
|
|
356
|
+
}
|
|
357
|
+
if (field.description)
|
|
358
|
+
definition.$description = field.description;
|
|
359
|
+
entry.$properties[field.key] = definition;
|
|
360
|
+
nativeMapping(child.id, key, field.key);
|
|
361
|
+
}
|
|
362
|
+
else
|
|
363
|
+
addSlot(field.key, child, { min: field.required ? 1 : 0, max: 1, ordered: false });
|
|
364
|
+
}
|
|
365
|
+
for (const [index, signature] of (node.indexSignatures ?? []).entries()) {
|
|
366
|
+
const child = nodes.get(signature.valueNodeId);
|
|
367
|
+
const targetKey = modelFor(child.id);
|
|
368
|
+
if (!targetKey)
|
|
369
|
+
continue;
|
|
370
|
+
const itemKey = 'Model_' + contentDigest({ owner: model.identity, nodeId: node.id, index }).slice(0, 32);
|
|
371
|
+
models.set(itemKey, {
|
|
372
|
+
key: itemKey,
|
|
373
|
+
nodeId: id,
|
|
374
|
+
entry: {
|
|
375
|
+
$type: 'component',
|
|
376
|
+
$properties: {
|
|
377
|
+
key: {
|
|
378
|
+
$type: signature.keyType === 'number' ? 'number' : 'string',
|
|
379
|
+
$category: 'content',
|
|
380
|
+
$required: true,
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
$slots: {
|
|
384
|
+
value: {
|
|
385
|
+
$required: true,
|
|
386
|
+
$allowedComponents: [targetKey],
|
|
387
|
+
$cardinality: { min: 1, max: 1, ordered: false },
|
|
388
|
+
$modelBinding: binding(child.id),
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
entry.$slots['index_' + index] = {
|
|
394
|
+
$required: false,
|
|
395
|
+
$allowedComponents: [itemKey],
|
|
396
|
+
$cardinality: { min: 0, max: null, ordered: true },
|
|
397
|
+
$modelBinding: binding(node.id),
|
|
398
|
+
};
|
|
399
|
+
requirements.push({ path: '/' + key, capability: 'index-key-preservation:' + signature.keyType });
|
|
400
|
+
}
|
|
401
|
+
if (node.kind === 'intersection') {
|
|
402
|
+
const pending = [...(node.alternatives ?? [])];
|
|
403
|
+
const visited = new Set();
|
|
404
|
+
while (pending.length) {
|
|
405
|
+
const member = nodes.get(pending.pop());
|
|
406
|
+
if (visited.has(member.id))
|
|
407
|
+
continue;
|
|
408
|
+
visited.add(member.id);
|
|
409
|
+
bound(member, 'reference', {
|
|
410
|
+
targetNodeId: node.id,
|
|
411
|
+
reason: 'Disjoint compiler record members retained in the merged region',
|
|
412
|
+
});
|
|
413
|
+
for (const field of member.fields ?? []) {
|
|
414
|
+
const target = fields.find((candidate) => candidate.key === field.key);
|
|
415
|
+
if (target) {
|
|
416
|
+
bound(nodes.get(field.nodeId), 'reference', { targetNodeId: target.nodeId });
|
|
417
|
+
const descendants = [{ source: field.nodeId, target: target.nodeId }];
|
|
418
|
+
const reached = new Set();
|
|
419
|
+
while (descendants.length) {
|
|
420
|
+
const pair = descendants.pop();
|
|
421
|
+
const child = nodes.get(pair.source);
|
|
422
|
+
const destination = nodes.get(pair.target);
|
|
423
|
+
if (reached.has(child.id))
|
|
424
|
+
continue;
|
|
425
|
+
reached.add(child.id);
|
|
426
|
+
if (child.sourceNodeId !== destination.sourceNodeId &&
|
|
427
|
+
(!child.compilerIdentity || child.compilerIdentity.typeId !== destination.compilerIdentity?.typeId)) {
|
|
428
|
+
hold(child, 'Intersection field has no exact compiler association with the merged field');
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
bound(child, 'reference', { targetNodeId: destination.id });
|
|
432
|
+
if (child.kind === 'reference')
|
|
433
|
+
continue;
|
|
434
|
+
for (const edge of child.fields ?? []) {
|
|
435
|
+
const other = destination.fields?.find((candidate) => candidate.key === edge.key);
|
|
436
|
+
if (other)
|
|
437
|
+
descendants.push({ source: edge.nodeId, target: other.nodeId });
|
|
438
|
+
}
|
|
439
|
+
for (const edge of ['items', 'alternatives'])
|
|
440
|
+
(child[edge] ?? []).forEach((source, index) => {
|
|
441
|
+
const target = destination[edge]?.[index];
|
|
442
|
+
if (target)
|
|
443
|
+
descendants.push({ source, target });
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
pending.push(...(member.alternatives ?? []));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
else if (node.kind === 'array' && node.items?.length === 1) {
|
|
453
|
+
const definition = nativeProperty(id);
|
|
454
|
+
if (definition) {
|
|
455
|
+
entry.$properties.value = definition;
|
|
456
|
+
mappings.delete(id);
|
|
457
|
+
nativeMapping(id, key, 'value');
|
|
458
|
+
}
|
|
459
|
+
else
|
|
460
|
+
addSlot('items', nodes.get(node.items[0]), { min: 0, max: null, ordered: true });
|
|
461
|
+
}
|
|
462
|
+
else if (node.kind === 'tuple') {
|
|
463
|
+
(node.items ?? []).forEach((item, index) => {
|
|
464
|
+
const element = node.tupleElements?.[index];
|
|
465
|
+
addSlot('position_' + index, nodes.get(item), {
|
|
466
|
+
min: element?.optional || element?.rest ? 0 : 1,
|
|
467
|
+
max: element?.rest ? null : 1,
|
|
468
|
+
ordered: true,
|
|
469
|
+
tuplePosition: index,
|
|
470
|
+
rest: element?.rest ?? false,
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
else if (node.kind === 'union') {
|
|
475
|
+
const members = (node.alternatives ?? []).map((alternative) => nodes.get(alternative));
|
|
476
|
+
const values = members.filter((member) => !['null', 'undefined', 'never'].includes(member.kind));
|
|
477
|
+
const definition = nativeProperty(id);
|
|
478
|
+
if (node.unionBranches) {
|
|
479
|
+
const selected = [];
|
|
480
|
+
for (const branch of node.unionBranches.branches) {
|
|
481
|
+
const modelKey = modelFor(branch.nodeId);
|
|
482
|
+
if (modelKey)
|
|
483
|
+
selected.push({ ...cloneJson(branch), modelKey });
|
|
484
|
+
}
|
|
485
|
+
if (selected.length !== node.unionBranches.branches.length) {
|
|
486
|
+
hold(node, 'Union branch has no complete editable model');
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
entry.$slots.variant = {
|
|
490
|
+
$required: true,
|
|
491
|
+
$allowedComponents: selected.map((branch) => branch.modelKey),
|
|
492
|
+
$cardinality: { min: 1, max: 1, ordered: false },
|
|
493
|
+
$modelBinding: binding(node.id),
|
|
494
|
+
};
|
|
495
|
+
mappings.set(id, { ...mappingFacts(node), disposition: 'slot', componentKey: key, slot: 'variant' });
|
|
496
|
+
for (const branch of selected) {
|
|
497
|
+
const mapping = mappings.get(branch.nodeId);
|
|
498
|
+
if (mapping)
|
|
499
|
+
mapping.variant = { wrapperKey: key, slot: 'variant', componentKey: branch.modelKey };
|
|
500
|
+
}
|
|
501
|
+
branchSelections.push({
|
|
502
|
+
unionNodeId: id,
|
|
503
|
+
wrapperKey: key,
|
|
504
|
+
slot: 'variant',
|
|
505
|
+
discriminator: node.unionBranches.discriminator,
|
|
506
|
+
branches: selected,
|
|
507
|
+
});
|
|
508
|
+
requirements.push({ path: '/' + key + '/$slots/variant', capability: 'exclusive-union-branch-selection' });
|
|
509
|
+
requirements.push({ path: '/' + key + '/$slots/variant', capability: 'singleton-slot-cardinality' });
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else if (definition) {
|
|
513
|
+
entry.$properties.value = definition;
|
|
514
|
+
mappings.delete(id);
|
|
515
|
+
nativeMapping(id, key, 'value');
|
|
516
|
+
}
|
|
517
|
+
else if (values.length === 1) {
|
|
518
|
+
for (const absent of members.filter((member) => member !== values[0]))
|
|
519
|
+
bound(absent, 'slot', { componentKey: key, slot: 'presence' });
|
|
520
|
+
addSlot('value', values[0], { min: 1, max: 1, ordered: false });
|
|
521
|
+
requirements.push({ path: '/source/' + id, capability: 'presence-null-state' });
|
|
522
|
+
}
|
|
523
|
+
else
|
|
524
|
+
hold(node, 'Compiler union retained; no proven editable variant representation');
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
const definition = nativeProperty(id);
|
|
528
|
+
if (definition) {
|
|
529
|
+
entry.$properties.value = definition;
|
|
530
|
+
mappings.delete(id);
|
|
531
|
+
nativeMapping(id, key, 'value');
|
|
532
|
+
}
|
|
533
|
+
else
|
|
534
|
+
hold(node, 'Source kind has no proven editable field representation');
|
|
535
|
+
}
|
|
536
|
+
activeModels.delete(id);
|
|
537
|
+
return key;
|
|
538
|
+
};
|
|
539
|
+
const rootKey = modelFor(root.id);
|
|
540
|
+
for (const node of graph.nodes)
|
|
541
|
+
if (!mappings.has(node.id))
|
|
542
|
+
hold(node, 'Logical source node has no physical mapping');
|
|
543
|
+
if (graph.coverage !== 'complete-within-declared-bound')
|
|
544
|
+
issues.push({ path: '/source', message: 'Source coverage remains bounded or incomplete' });
|
|
545
|
+
const attachment = {
|
|
546
|
+
slot: model.identity.key,
|
|
547
|
+
definition: {
|
|
548
|
+
$required: root.required,
|
|
549
|
+
...(model.description !== undefined ? { $description: model.description } : {}),
|
|
550
|
+
...(native?.$sourceInput ? { $sourceInput: cloneJson(native.$sourceInput) } : {}),
|
|
551
|
+
$allowedComponents: rootKey ? [rootKey] : [],
|
|
552
|
+
$cardinality: { min: root.required ? 1 : 0, max: 1, ordered: false },
|
|
553
|
+
$modelBinding: binding(root.id),
|
|
554
|
+
},
|
|
555
|
+
};
|
|
556
|
+
if (!issues.length) {
|
|
557
|
+
compactModelComponents({ nodes, models, mappings, attachment: attachment.definition });
|
|
558
|
+
const cardinalityCapabilities = new Set(['ordered-slot-cardinality', 'singleton-slot-cardinality']);
|
|
559
|
+
for (let index = requirements.length - 1; index >= 0; index--)
|
|
560
|
+
if (cardinalityCapabilities.has(requirements[index].capability))
|
|
561
|
+
requirements.splice(index, 1);
|
|
562
|
+
for (const generated of models.values())
|
|
563
|
+
for (const [name, slot] of Object.entries(generated.entry.$slots ?? {}))
|
|
564
|
+
requirements.push({
|
|
565
|
+
path: '/' + generated.key + '/$slots/' + name,
|
|
566
|
+
capability: slot.$cardinality?.ordered ? 'ordered-slot-cardinality' : 'singleton-slot-cardinality',
|
|
567
|
+
});
|
|
568
|
+
requirements.push({
|
|
569
|
+
path: '/$slots/' + attachment.slot,
|
|
570
|
+
capability: attachment.definition.$cardinality?.ordered
|
|
571
|
+
? 'ordered-slot-cardinality'
|
|
572
|
+
: 'singleton-slot-cardinality',
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
const modelExplorations = options.modelExplorations ? cloneJson([...options.modelExplorations]) : undefined;
|
|
576
|
+
if (modelExplorations) {
|
|
577
|
+
const seen = new Set();
|
|
578
|
+
for (const report of modelExplorations) {
|
|
579
|
+
const generated = models.get(report.modelKey);
|
|
580
|
+
if (!generated || seen.has(report.modelKey))
|
|
581
|
+
throw new Error('Unknown or duplicate model exploration owner');
|
|
582
|
+
seen.add(report.modelKey);
|
|
583
|
+
applyModelComponentExploration(report, generated, nodes, mappings, model.identity.sourceRevision);
|
|
584
|
+
if (report.properties.some((field) => field.authorability === 'fixed'))
|
|
585
|
+
requirements.push({ path: '/' + generated.key, capability: 'intrinsic-field-reconstruction' });
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
for (const generated of models.values()) {
|
|
589
|
+
const edit = options.modelEdits && own(options.modelEdits, generated.key) ? options.modelEdits[generated.key] : undefined;
|
|
590
|
+
if (!edit)
|
|
591
|
+
continue;
|
|
592
|
+
if (jsonValueIssue(edit) ||
|
|
593
|
+
edit.$type !== 'component' ||
|
|
594
|
+
!jsonEqual(Object.keys(edit.$properties).sort(), Object.keys(generated.entry.$properties).sort()) ||
|
|
595
|
+
!jsonEqual(Object.fromEntries(Object.entries(edit.$slots ?? {}).map(([name, slot]) => {
|
|
596
|
+
const { $description: _description, ...structure } = slot;
|
|
597
|
+
return [name, structure];
|
|
598
|
+
})), Object.fromEntries(Object.entries(generated.entry.$slots ?? {}).map(([name, slot]) => {
|
|
599
|
+
const { $description: _description, ...structure } = slot;
|
|
600
|
+
return [name, structure];
|
|
601
|
+
}))))
|
|
602
|
+
throw new Error('Edited model changes source-owned structure');
|
|
603
|
+
for (const [field, edited] of Object.entries(edit.$properties)) {
|
|
604
|
+
const original = generated.entry.$properties[field];
|
|
605
|
+
const structural = cloneJson(edited);
|
|
606
|
+
delete structural.$description;
|
|
607
|
+
delete structural.$default;
|
|
608
|
+
const originalStructural = cloneJson(original);
|
|
609
|
+
delete originalStructural.$description;
|
|
610
|
+
delete originalStructural.$default;
|
|
611
|
+
if (!jsonEqual(structural, originalStructural) || !validateCDFProperty(edited).valid)
|
|
612
|
+
throw new Error('Edited model field violates source type or requiredness');
|
|
613
|
+
const mapped = [...mappings.values()].find((mapping) => mapping.componentKey === generated.key && mapping.field === field);
|
|
614
|
+
const node = mapped ? nodes.get(mapped.nodeId) : nodes.get(generated.nodeId);
|
|
615
|
+
if (own(edited, '$default') &&
|
|
616
|
+
validateSourceValue(sourceGraph, edited.$default, '/defaultValue', node.sourceNodeId).length)
|
|
617
|
+
throw new Error('Edited model default violates source structure');
|
|
618
|
+
if (own(edited, '$default') &&
|
|
619
|
+
node.domain.openness === 'closed' &&
|
|
620
|
+
node.domain.declaredValues.length &&
|
|
621
|
+
!node.domain.declaredValues.some((value) => jsonEqual(value, edited.$default)))
|
|
622
|
+
throw new Error('Edited model default violates source domain');
|
|
623
|
+
generated.entry.$properties[field] = cloneJson(edited);
|
|
624
|
+
}
|
|
625
|
+
if (edit.$description !== undefined)
|
|
626
|
+
generated.entry.$description = edit.$description;
|
|
627
|
+
for (const [name, slot] of Object.entries(edit.$slots ?? {}))
|
|
628
|
+
if (slot.$description !== undefined)
|
|
629
|
+
generated.entry.$slots[name].$description = slot.$description;
|
|
630
|
+
}
|
|
631
|
+
if (options.modelEdits && Object.keys(options.modelEdits).some((key) => !models.has(key)))
|
|
632
|
+
throw new Error('Edited model has no exact source owner');
|
|
633
|
+
const renamedKeys = new Map();
|
|
634
|
+
const reservedKeys = new Set();
|
|
635
|
+
for (const generated of models.values()) {
|
|
636
|
+
const name = options.modelRenames && own(options.modelRenames, generated.key)
|
|
637
|
+
? options.modelRenames[generated.key]
|
|
638
|
+
: (modelExplorations?.find((report) => report.modelKey === generated.key)?.name ?? generated.key);
|
|
639
|
+
if (typeof name !== 'string' ||
|
|
640
|
+
!name.trim() ||
|
|
641
|
+
name !== name.trim() ||
|
|
642
|
+
name.split('.').some((segment) => !segment || segment.startsWith('$')) ||
|
|
643
|
+
reservedKeys.has(name))
|
|
644
|
+
throw new Error('Edited model identity is invalid or duplicated');
|
|
645
|
+
renamedKeys.set(generated.key, name);
|
|
646
|
+
reservedKeys.add(name);
|
|
647
|
+
}
|
|
648
|
+
if (options.modelRenames && Object.keys(options.modelRenames).some((key) => !models.has(key)))
|
|
649
|
+
throw new Error('Edited model identity has no exact source owner');
|
|
650
|
+
const renamedModels = [...models.values()].map((generated) => {
|
|
651
|
+
const entry = cloneJson(generated.entry);
|
|
652
|
+
for (const slot of Object.values(entry.$slots ?? {}))
|
|
653
|
+
if (slot.$allowedComponents)
|
|
654
|
+
slot.$allowedComponents = slot.$allowedComponents.map((key) => renamedKeys.get(key));
|
|
655
|
+
return { ...generated, originalKey: generated.key, key: renamedKeys.get(generated.key), entry };
|
|
656
|
+
});
|
|
657
|
+
attachment.definition.$allowedComponents = attachment.definition.$allowedComponents?.map((key) => renamedKeys.get(key));
|
|
658
|
+
for (const mapping of mappings.values()) {
|
|
659
|
+
if (mapping.componentKey)
|
|
660
|
+
mapping.componentKey = renamedKeys.get(mapping.componentKey);
|
|
661
|
+
if (mapping.variant) {
|
|
662
|
+
mapping.variant.wrapperKey = renamedKeys.get(mapping.variant.wrapperKey);
|
|
663
|
+
mapping.variant.componentKey = renamedKeys.get(mapping.variant.componentKey);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const rebasedRequirements = requirements.map((requirement) => {
|
|
667
|
+
for (const [original, renamed] of renamedKeys)
|
|
668
|
+
if (requirement.path === '/' + original || requirement.path.startsWith('/' + original + '/'))
|
|
669
|
+
return { ...requirement, path: '/' + renamed + requirement.path.slice(original.length + 1) };
|
|
670
|
+
return requirement;
|
|
671
|
+
});
|
|
672
|
+
const uniqueRequirements = [
|
|
673
|
+
...new Map(rebasedRequirements.map((requirement) => [JSON.stringify(requirement), requirement])).values(),
|
|
674
|
+
];
|
|
675
|
+
const renamedSelections = branchSelections.map((selection) => ({
|
|
676
|
+
...selection,
|
|
677
|
+
wrapperKey: renamedKeys.get(selection.wrapperKey),
|
|
678
|
+
branches: selection.branches.map((branch) => ({ ...branch, modelKey: renamedKeys.get(branch.modelKey) })),
|
|
679
|
+
}));
|
|
680
|
+
plans.push(finish({
|
|
681
|
+
...base,
|
|
682
|
+
kind: 'component-model',
|
|
683
|
+
models: renamedModels,
|
|
684
|
+
attachment,
|
|
685
|
+
mappings: graph.nodes.map((node) => mappings.get(node.id)),
|
|
686
|
+
issues,
|
|
687
|
+
sdkValid: !issues.length,
|
|
688
|
+
requirements: uniqueRequirements,
|
|
689
|
+
...(renamedSelections.length ? { branchSelections: renamedSelections } : {}),
|
|
690
|
+
...(modelExplorations ? { modelExplorations } : {}),
|
|
691
|
+
}));
|
|
692
|
+
return plans;
|
|
693
|
+
}
|
|
694
|
+
export function validateRepresentationPlan(input, model, context) {
|
|
695
|
+
const unsafe = jsonValueIssue(input);
|
|
696
|
+
if (unsafe || !input || typeof input !== 'object' || Array.isArray(input))
|
|
697
|
+
return [unsafe ?? { path: '/', message: 'Expected representation plan' }];
|
|
698
|
+
const plan = input;
|
|
699
|
+
try {
|
|
700
|
+
if (!Array.isArray(plan.models) ||
|
|
701
|
+
!Array.isArray(plan.mappings) ||
|
|
702
|
+
!plan.defaultIntent ||
|
|
703
|
+
!['native', 'component-model'].includes(plan.kind))
|
|
704
|
+
throw new Error('Malformed representation plan');
|
|
705
|
+
const edits = Object.create(null);
|
|
706
|
+
const renames = Object.create(null);
|
|
707
|
+
const originalKeys = new Map(plan.models.map((generated) => [generated.key, generated.originalKey]));
|
|
708
|
+
for (const generated of plan.models) {
|
|
709
|
+
if (!generated ||
|
|
710
|
+
typeof generated.key !== 'string' ||
|
|
711
|
+
typeof generated.originalKey !== 'string' ||
|
|
712
|
+
own(edits, generated.originalKey) ||
|
|
713
|
+
originalKeys.size !== plan.models.length)
|
|
714
|
+
throw new Error('Duplicate or invalid generated model identity');
|
|
715
|
+
renames[generated.originalKey] = generated.key;
|
|
716
|
+
edits[generated.originalKey] = cloneJson(generated.entry);
|
|
717
|
+
delete edits[generated.originalKey].$model;
|
|
718
|
+
for (const slot of Object.values(edits[generated.originalKey].$slots ?? {})) {
|
|
719
|
+
if (slot.$modelBinding)
|
|
720
|
+
slot.$modelBinding.planId = '';
|
|
721
|
+
if (slot.$allowedComponents)
|
|
722
|
+
slot.$allowedComponents = slot.$allowedComponents.map((key) => originalKeys.get(key) ?? key);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
const expected = deriveEditableRepresentationPlans(model, {
|
|
726
|
+
context,
|
|
727
|
+
modelEdits: edits,
|
|
728
|
+
modelRenames: renames,
|
|
729
|
+
...(plan.modelExplorations ? { modelExplorations: plan.modelExplorations } : {}),
|
|
730
|
+
defaultIntent: plan.defaultIntent,
|
|
731
|
+
...(plan.proposal ? { proposal: plan.proposal } : {}),
|
|
732
|
+
}).find((candidate) => candidate.kind === plan.kind);
|
|
733
|
+
if (!expected || !jsonEqual(expected, plan))
|
|
734
|
+
throw new Error('Stale, forged or incompatible source representation plan');
|
|
735
|
+
if (!plan.sdkValid)
|
|
736
|
+
return plan.issues.length
|
|
737
|
+
? cloneJson(plan.issues)
|
|
738
|
+
: [{ path: '/', message: 'Plan has held logical source nodes' }];
|
|
739
|
+
return [];
|
|
740
|
+
}
|
|
741
|
+
catch (error) {
|
|
742
|
+
return [{ path: '/', message: error instanceof Error ? error.message : 'Invalid representation plan' }];
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
export function evaluateRepresentationReadiness(plan, capabilities = {}) {
|
|
746
|
+
const requirements = plan.requirements.map((requirement) => ({
|
|
747
|
+
...requirement,
|
|
748
|
+
availability: own(capabilities, requirement.path + ':' + requirement.capability)
|
|
749
|
+
? capabilities[requirement.path + ':' + requirement.capability]
|
|
750
|
+
: 'unknown',
|
|
751
|
+
}));
|
|
752
|
+
return {
|
|
753
|
+
ready: plan.sdkValid && requirements.every((requirement) => requirement.availability === 'enabled'),
|
|
754
|
+
requirements: freezeJson(cloneJson(requirements)),
|
|
755
|
+
};
|
|
756
|
+
}
|