@aestheticfunction/dspack-emit 0.3.2 → 0.4.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.
@@ -0,0 +1,229 @@
1
+ /**
2
+ * scaffoldProfile: a mechanical 1:1 draft Profile from a dspack contract.
3
+ *
4
+ * Seeded from the json-render CatalogModel (the judgment-free 1:1 IR): every
5
+ * top-level contract component becomes a ComponentPlan with verbatim prop
6
+ * projections; compound sub-components get subFlatten directives derived from
7
+ * their *declared* `acceptsChildren` (text → asText, otherwise transparent).
8
+ *
9
+ * The scaffold invents no judgment: no valueMaps, no casualties, no lossy
10
+ * projections. Everything a human must review is returned as `notes` — the
11
+ * scaffold's analog of dspack-export's `awaitingAuthorship`. The result is a
12
+ * valid v1 profile document (loadProfile accepts it) whose transform passes
13
+ * gates A1/A2 with full coverage; whether each mapping is *right* is the
14
+ * author's call.
15
+ */
16
+ import type { DspackDoc, DspackProp } from "../types.js";
17
+ import { buildCatalogModel, type CatalogComponent } from "../targets/json-render/model.js";
18
+
19
+ export interface ScaffoldOptions {
20
+ /** Required: the project's catalog identity root (https URI). */
21
+ catalogIdBase: string;
22
+ catalogTitle?: string;
23
+ catalogDescription?: string;
24
+ instructions?: string;
25
+ /** Which token supplies theme.primaryColor; auto-detects color.primary when omitted. */
26
+ primaryColorToken?: { category: string; name: string };
27
+ }
28
+
29
+ export interface ScaffoldNote {
30
+ /** What the note is about: a component id, "<id>.<prop>", or a profile field. */
31
+ target: string;
32
+ note: string;
33
+ }
34
+
35
+ export interface ScaffoldResult {
36
+ /** A v1 profile *document* (includes profileVersion; loadProfile accepts it). */
37
+ profile: Record<string, unknown>;
38
+ /** Everything that needs human judgment before the profile is trustworthy. */
39
+ notes: ScaffoldNote[];
40
+ }
41
+
42
+ const DYN_STRING = { $ref: "#/$defs/DynamicString" };
43
+ const CHILD_LIST = { $ref: "#/$defs/ChildList" };
44
+ const COMP_ID = { $ref: "#/$defs/ComponentId" };
45
+
46
+ const TEXT_PLAN = {
47
+ a2ui: "Text",
48
+ commons: ["ComponentCommon"],
49
+ description: "Displays text content. Synthesized A2UI content primitive (not in the contract).",
50
+ structural: {
51
+ text: {
52
+ schema: DYN_STRING,
53
+ description: "The text content to display.",
54
+ synthNote: "A2UI content primitive required to render labels/titles in a surface.",
55
+ },
56
+ },
57
+ propMap: {
58
+ variant: {
59
+ a2ui: "variant",
60
+ kind: "enum",
61
+ targetEnum: ["h1", "h2", "h3", "h4", "h5", "caption", "body"],
62
+ default: "body",
63
+ description: "A hint for the base text style.",
64
+ },
65
+ },
66
+ required: ["text"],
67
+ };
68
+
69
+ const COLUMN_PLAN = {
70
+ a2ui: "Column",
71
+ commons: ["ComponentCommon"],
72
+ description:
73
+ "Arranges children vertically. Synthesized A2UI structural primitive (the contract has no layout component).",
74
+ structural: {
75
+ children: {
76
+ schema: CHILD_LIST,
77
+ description: "Child component IDs (or a template).",
78
+ synthNote: "A2UI structural primitive required to compose multiple children.",
79
+ },
80
+ },
81
+ required: ["children"],
82
+ };
83
+
84
+ interface SubDecl {
85
+ id: string;
86
+ acceptsChildren?: string;
87
+ }
88
+
89
+ function subsOf(doc: DspackDoc, id: string): SubDecl[] {
90
+ const composition = (doc.components?.[id]?.composition ?? {}) as { subComponents?: unknown[] };
91
+ const out: SubDecl[] = [];
92
+ for (const raw of composition.subComponents ?? []) {
93
+ if (typeof raw === "string") out.push({ id: raw });
94
+ else if (raw && typeof raw === "object" && typeof (raw as SubDecl).id === "string") out.push(raw as SubDecl);
95
+ }
96
+ return out;
97
+ }
98
+
99
+ export function scaffoldProfile(doc: DspackDoc, options: ScaffoldOptions): ScaffoldResult {
100
+ const notes: ScaffoldNote[] = [];
101
+ const model = buildCatalogModel(doc, {});
102
+ const byId = new Map<string, CatalogComponent>(model.components.map((c) => [c.dspackId, c]));
103
+ const topLevelIds = Object.keys(doc.components ?? {});
104
+ const name = (id: string) => byId.get(id)?.name ?? id;
105
+
106
+ const usedNames = new Set(topLevelIds.map((id) => name(id)));
107
+
108
+ const components = topLevelIds.map((id) => {
109
+ const entry = byId.get(id)!;
110
+ const contractProps = (doc.components?.[id]?.props ?? {}) as Record<string, DspackProp>;
111
+
112
+ const propMap: Record<string, unknown> = {};
113
+ for (const prop of entry.props) {
114
+ if (prop.synthesized) continue; // the model's universal text prop belongs to json-render, not here
115
+ const plan: Record<string, unknown> = { a2ui: prop.name, kind: prop.kind };
116
+ if (prop.kind === "enum") plan.targetEnum = prop.values;
117
+ const dflt = contractProps[prop.name]?.default;
118
+ if (typeof dflt === "string") plan.default = dflt;
119
+ if (prop.description) plan.description = prop.description;
120
+ propMap[prop.name] = plan;
121
+ }
122
+ for (const excluded of entry.excludedProps) {
123
+ notes.push({
124
+ target: `${id}.${excluded.name}`,
125
+ note: `prop not scaffolded (${excluded.reason}); map it via a structural slot, or declare the component a casualty if it cannot be represented.`,
126
+ });
127
+ }
128
+
129
+ const required = Object.entries(contractProps)
130
+ .filter(([, p]) => p.required === true)
131
+ .map(([propName]) => propName)
132
+ .filter((propName) => propName in propMap);
133
+
134
+ const plan: Record<string, unknown> = {
135
+ a2ui: entry.name,
136
+ dspackId: id,
137
+ commons: ["ComponentCommon"],
138
+ structural: {},
139
+ required,
140
+ };
141
+ if (Object.keys(propMap).length > 0) plan.propMap = propMap;
142
+ else
143
+ notes.push({
144
+ target: id,
145
+ note: "no declarative props were scaffolded; the contract likely needs prop enrichment before this mapping is useful.",
146
+ });
147
+
148
+ const subs = subsOf(doc, id);
149
+ if (subs.length > 0) {
150
+ const transparent = subs.filter((s) => s.acceptsChildren !== "text").map((s) => s.id);
151
+ const asText = Object.fromEntries(subs.filter((s) => s.acceptsChildren === "text").map((s) => [s.id, "body"]));
152
+ plan.structural = {
153
+ child: {
154
+ schema: COMP_ID,
155
+ description: "The ID of the single child component. Wrap multiple elements in a Column and pass its ID.",
156
+ synthNote:
157
+ "Scaffolded: the compound's sub-components flatten (subFlatten) and collapse to a single, possibly Column-wrapped, child slot.",
158
+ },
159
+ };
160
+ (plan.required as string[]).push("child");
161
+ plan.surfacePlan = { childProp: "child", subFlatten: { transparent, asText } };
162
+ plan.subCoverage = Object.fromEntries(
163
+ subs.map((s) => [
164
+ s.id,
165
+ s.acceptsChildren === "text"
166
+ ? "text -> synthesized Text (variant body) [scaffolded from acceptsChildren: text — review]"
167
+ : "transparent grouping: children splice inline, in order [scaffolded — review]",
168
+ ]),
169
+ );
170
+ notes.push({
171
+ target: id,
172
+ note: `compound scaffolded from declared acceptsChildren (${subs.length} sub-components: ${transparent.length} transparent, ${Object.keys(asText).length} asText); review each subCoverage line and pick text variants.`,
173
+ });
174
+ }
175
+
176
+ return plan;
177
+ });
178
+
179
+ const synthesized: Record<string, unknown>[] = [];
180
+ for (const primitive of [TEXT_PLAN, COLUMN_PLAN]) {
181
+ if (usedNames.has(primitive.a2ui)) {
182
+ notes.push({
183
+ target: primitive.a2ui,
184
+ note: `contract already has a component named '${primitive.a2ui}'; the synthesized primitive was skipped — verify surfaceSynthesis points at a component with the expected '${primitive.a2ui === "Text" ? "text" : "children"}' prop.`,
185
+ });
186
+ } else {
187
+ synthesized.push(structuredClone(primitive) as Record<string, unknown>);
188
+ }
189
+ }
190
+
191
+ let primaryColorToken = options.primaryColorToken;
192
+ if (!primaryColorToken) {
193
+ const hasColorPrimary = Boolean((doc.tokens as Record<string, { values?: Record<string, unknown> }> | undefined)?.color?.values?.primary);
194
+ primaryColorToken = { category: "color", name: "primary" };
195
+ notes.push({
196
+ target: "primaryColorToken",
197
+ note: hasColorPrimary
198
+ ? "auto-detected color.primary; verify it is the intended theme primary."
199
+ : "defaulted to color.primary, which this contract does not define — theme.primaryColor will emit null with a warning until you point this at a real token.",
200
+ });
201
+ }
202
+
203
+ const profile: Record<string, unknown> = {
204
+ profileVersion: "1",
205
+ catalogTitle: options.catalogTitle ?? `${doc.name} — A2UI catalog (compiled from dspack)`,
206
+ catalogDescription:
207
+ options.catalogDescription ??
208
+ `A2UI catalog compiled from the ${doc.name} dspack contract by scaffoldProfile. Mechanical 1:1 draft: review the scaffold notes before trusting any mapping.`,
209
+ catalogIdBase: options.catalogIdBase,
210
+ instructions: options.instructions ?? "For layout, use the Column component to organize other components.",
211
+ primaryColorToken,
212
+ components,
213
+ synthesized,
214
+ casualtyComponents: [],
215
+ surfaceSynthesis: {
216
+ textComponent: "Text",
217
+ textProp: "text",
218
+ wrapComponent: "Column",
219
+ wrapChildrenProp: "children",
220
+ },
221
+ };
222
+
223
+ notes.push({
224
+ target: "casualtyComponents",
225
+ note: "scaffolds never declare casualties; components that cannot be represented must be moved here by the author, with a reason.",
226
+ });
227
+
228
+ return { profile, notes };
229
+ }