@aestheticfunction/dspack-emit 0.3.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/LICENSE +201 -0
- package/README.md +285 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +186 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/targets/a2ui/surface.d.ts +43 -0
- package/dist/targets/a2ui/surface.js +219 -0
- package/dist/targets/csr.d.ts +15 -0
- package/dist/targets/csr.js +9 -0
- package/dist/targets/json-render/codegen.d.ts +35 -0
- package/dist/targets/json-render/codegen.js +91 -0
- package/dist/targets/json-render/emit.d.ts +66 -0
- package/dist/targets/json-render/emit.js +142 -0
- package/dist/targets/json-render/model.d.ts +57 -0
- package/dist/targets/json-render/model.js +99 -0
- package/dist/targets/json-render/profile.d.ts +23 -0
- package/dist/targets/json-render/profile.js +3 -0
- package/dist/transform/color.d.ts +11 -0
- package/dist/transform/color.js +53 -0
- package/dist/transform/emit.d.ts +15 -0
- package/dist/transform/emit.js +100 -0
- package/dist/transform/index.d.ts +26 -0
- package/dist/transform/index.js +26 -0
- package/dist/transform/inline-defs.d.ts +22 -0
- package/dist/transform/inline-defs.js +172 -0
- package/dist/transform/mapping.d.ts +24 -0
- package/dist/transform/mapping.js +322 -0
- package/dist/transform/profiles.d.ts +120 -0
- package/dist/transform/profiles.js +282 -0
- package/dist/transform/report.d.ts +13 -0
- package/dist/transform/report.js +76 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +6 -0
- package/dist/validate/ajv.d.ts +15 -0
- package/dist/validate/ajv.js +132 -0
- package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
- package/dist/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
- package/package.json +41 -0
- package/src/cli.ts +197 -0
- package/src/index.ts +39 -0
- package/src/ingestion.test.ts +156 -0
- package/src/json-render-emit.test.ts +196 -0
- package/src/surface-emit.test.ts +184 -0
- package/src/targets/a2ui/surface.ts +287 -0
- package/src/targets/csr.ts +23 -0
- package/src/targets/json-render/codegen.ts +135 -0
- package/src/targets/json-render/emit.ts +227 -0
- package/src/targets/json-render/model.ts +170 -0
- package/src/targets/json-render/profile.ts +26 -0
- package/src/transform/color.ts +52 -0
- package/src/transform/emit.ts +128 -0
- package/src/transform/index.ts +54 -0
- package/src/transform/inline-defs.ts +209 -0
- package/src/transform/mapping.ts +391 -0
- package/src/transform/profiles.ts +425 -0
- package/src/transform/report.ts +105 -0
- package/src/transform.test.ts +112 -0
- package/src/types.ts +145 -0
- package/src/validate/ajv.ts +151 -0
- package/src/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
- package/src/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PR-2 acceptance gates: the dspack surface → A2UI surface emitter.
|
|
3
|
+
*
|
|
4
|
+
* - The contract's worked example (ex.delete-account-confirmation) emits a
|
|
5
|
+
* surface whose instances pass all emitter gates (A1 schema-compile /
|
|
6
|
+
* A2 catalog-shape / A3 instance) against the freshly generated catalog,
|
|
7
|
+
* for both A2UI versions.
|
|
8
|
+
* - Compound flattening lands the documented projection (title/description/
|
|
9
|
+
* cancelLabel/confirmLabel/triggerLabel + synthesized action).
|
|
10
|
+
* - Unknown components fail with a typed error (never silently dropped).
|
|
11
|
+
* - A CSR violating the emitted component's required props FAILS gate A3 —
|
|
12
|
+
* the gate is proven non-vacuous.
|
|
13
|
+
* - Emission is deterministic.
|
|
14
|
+
*/
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { describe, expect, it } from "vitest";
|
|
17
|
+
import type { DspackDoc, DspackSurface, Json } from "./types.js";
|
|
18
|
+
import { transform } from "./transform/index.js";
|
|
19
|
+
import { emitSurface, EmitSurfaceError } from "./targets/a2ui/surface.js";
|
|
20
|
+
|
|
21
|
+
const doc = JSON.parse(readFileSync("input/shadcn-ui.dspack.json", "utf8")) as DspackDoc & {
|
|
22
|
+
examples: Array<{ id: string; surface: DspackSurface }>;
|
|
23
|
+
};
|
|
24
|
+
const workedExample = doc.examples.find((e) => e.id === "ex.delete-account-confirmation")!;
|
|
25
|
+
|
|
26
|
+
function componentsOf(messages: Json[]): Json[] {
|
|
27
|
+
const update = messages.find((m) => m.updateComponents) as Json;
|
|
28
|
+
return (update.updateComponents as Json).components as Json[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("emitSurface: worked example (ex.delete-account-confirmation)", () => {
|
|
32
|
+
const { messages, warnings } = emitSurface(workedExample.surface, doc);
|
|
33
|
+
const components = componentsOf(messages);
|
|
34
|
+
|
|
35
|
+
it("emits createSurface with the versioned catalogId and dspack-token theme", () => {
|
|
36
|
+
const create = (messages[0].createSurface ?? {}) as Json;
|
|
37
|
+
expect(create.catalogId).toBe("https://rdombrowski.dev/catalogs/shadcn-ui/v0_9_1/catalog.json");
|
|
38
|
+
expect((create.theme as Json).primaryColor).toBe("#0f172a");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("flattens the AlertDialog compound per the documented casualty mapping", () => {
|
|
42
|
+
const dialog = components.find((c) => c.component === "AlertDialog")!;
|
|
43
|
+
expect(dialog).toMatchObject({
|
|
44
|
+
triggerLabel: "Delete account",
|
|
45
|
+
title: "Delete your account?",
|
|
46
|
+
cancelLabel: "Cancel",
|
|
47
|
+
confirmLabel: "Delete account",
|
|
48
|
+
});
|
|
49
|
+
expect(String(dialog.description)).toMatch(/permanently delete/);
|
|
50
|
+
expect(dialog.action).toEqual({ event: { name: "delete_account", context: {} } });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("projects Card onto its single-child slot; the root emits under id 'root' (A2UI render entry)", () => {
|
|
54
|
+
const card = components.find((c) => c.component === "Card")!;
|
|
55
|
+
const dialog = components.find((c) => c.component === "AlertDialog")!;
|
|
56
|
+
expect(card.child).toBe(dialog.id);
|
|
57
|
+
expect(components[0]).toBe(card); // root first (pre-order)
|
|
58
|
+
expect(card.id).toBe("root");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("passes gates A1–A3 against both generated catalogs", () => {
|
|
62
|
+
for (const version of ["0.9.1", "1.0"] as const) {
|
|
63
|
+
const { validation } = transform(doc, version, { messages });
|
|
64
|
+
const gates = Object.fromEntries(validation.gates.map((g) => [g.name, g.pass]));
|
|
65
|
+
expect(gates, `gates for ${version}`).toEqual({
|
|
66
|
+
"schema-compile + no-external-ref": true,
|
|
67
|
+
"catalog-shape": true,
|
|
68
|
+
instance: true,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("records every synthesis as a warning — nothing silent", () => {
|
|
74
|
+
const codes = warnings.map((w) => w.code);
|
|
75
|
+
expect(codes).toContain("surface-synthesized-action");
|
|
76
|
+
expect(codes).toContain("surface-composition-flattened");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("subButtonText takes the label-bearing component's text, never incidental descendant text", () => {
|
|
80
|
+
const surfaceWithNoise: DspackSurface = structuredClone(workedExample.surface);
|
|
81
|
+
// Prepend explanatory text inside the trigger, before the button: a
|
|
82
|
+
// sub-component text node that subText does not claim.
|
|
83
|
+
const trigger = surfaceWithNoise.root.children![0].children![0];
|
|
84
|
+
trigger.children!.unshift({ component: "alert-dialog-header", text: "Danger zone" });
|
|
85
|
+
const dialog = componentsOf(emitSurface(surfaceWithNoise, doc).messages).find(
|
|
86
|
+
(c) => c.component === "AlertDialog",
|
|
87
|
+
)!;
|
|
88
|
+
expect(dialog.triggerLabel).toBe("Delete account"); // the button's label, not "Danger zone"
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("is deterministic", () => {
|
|
92
|
+
const again = emitSurface(workedExample.surface, doc);
|
|
93
|
+
expect(again.messages).toEqual(messages);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("emitSurface: general projection", () => {
|
|
98
|
+
const settingsLike: DspackSurface = {
|
|
99
|
+
dspackSurface: "0.1",
|
|
100
|
+
system: "shadcn/ui",
|
|
101
|
+
intent: "destructive-action",
|
|
102
|
+
root: {
|
|
103
|
+
component: "card",
|
|
104
|
+
children: [
|
|
105
|
+
{ component: "input", id: "email_field", props: { type: "email" }, text: "Email address" },
|
|
106
|
+
{ component: "button", id: "save_btn", props: { variant: "default" }, text: "Save changes" },
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
it("wraps multiple card children in a synthesized Column and buttons get Text children", () => {
|
|
112
|
+
const { messages, warnings } = emitSurface(settingsLike, doc);
|
|
113
|
+
const components = componentsOf(messages);
|
|
114
|
+
// Both structural syntheses are audited — nothing silent.
|
|
115
|
+
const codes = warnings.map((w) => w.code);
|
|
116
|
+
expect(codes).toContain("surface-synthesized-wrap");
|
|
117
|
+
expect(codes).toContain("surface-synthesized-text");
|
|
118
|
+
const card = components.find((c) => c.component === "Card")!;
|
|
119
|
+
const column = components.find((c) => c.component === "Column")!;
|
|
120
|
+
expect(card.child).toBe(column.id);
|
|
121
|
+
expect(column.children).toEqual(["email_field", "save_btn"]);
|
|
122
|
+
|
|
123
|
+
const field = components.find((c) => c.id === "email_field")!;
|
|
124
|
+
expect(field.component).toBe("TextField");
|
|
125
|
+
expect(field.variant).toBe("shortText"); // email -> shortText projection
|
|
126
|
+
expect(field.label).toBe("Email address");
|
|
127
|
+
|
|
128
|
+
const button = components.find((c) => c.id === "save_btn")!;
|
|
129
|
+
expect(button.variant).toBe("primary"); // default -> primary projection
|
|
130
|
+
const label = components.find((c) => c.id === button.child)!;
|
|
131
|
+
expect(label.component).toBe("Text");
|
|
132
|
+
expect(label.text).toBe("Save changes");
|
|
133
|
+
expect(button.action).toEqual({ event: { name: "save_btn", context: {} } });
|
|
134
|
+
|
|
135
|
+
const { validation } = transform(doc, "0.9.1", { messages });
|
|
136
|
+
expect(validation.pass).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("emitSurface: failure modes", () => {
|
|
141
|
+
it("throws a typed error on unknown components", () => {
|
|
142
|
+
const bad: DspackSurface = {
|
|
143
|
+
dspackSurface: "0.1",
|
|
144
|
+
system: "shadcn/ui",
|
|
145
|
+
intent: "destructive-action",
|
|
146
|
+
root: { component: "carousel" },
|
|
147
|
+
};
|
|
148
|
+
expect(() => emitSurface(bad, doc)).toThrowError(EmitSurfaceError);
|
|
149
|
+
expect(() => emitSurface(bad, doc)).toThrowError(/unknown component 'carousel'/);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("throws a typed error on a system/contract mismatch", () => {
|
|
153
|
+
const bad = { ...workedExample.surface, system: "someone-elses-system" };
|
|
154
|
+
expect(() => emitSurface(bad, doc)).toThrowError(/does not match contract name/);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("gate A3 is non-vacuous: an AlertDialog missing its title fails instance validation", () => {
|
|
158
|
+
const missingTitle: DspackSurface = {
|
|
159
|
+
dspackSurface: "0.1",
|
|
160
|
+
system: "shadcn/ui",
|
|
161
|
+
intent: "destructive-action",
|
|
162
|
+
root: {
|
|
163
|
+
component: "alert-dialog",
|
|
164
|
+
children: [
|
|
165
|
+
{
|
|
166
|
+
component: "alert-dialog-trigger",
|
|
167
|
+
children: [{ component: "button", props: { variant: "destructive" }, text: "Delete" }],
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
component: "alert-dialog-content",
|
|
171
|
+
children: [
|
|
172
|
+
{ component: "alert-dialog-footer", children: [{ component: "alert-dialog-cancel", text: "Cancel" }] },
|
|
173
|
+
],
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
const { messages } = emitSurface(missingTitle, doc);
|
|
179
|
+
const { validation } = transform(doc, "0.9.1", { messages });
|
|
180
|
+
const instance = validation.gates.find((g) => g.name === "instance")!;
|
|
181
|
+
expect(instance.pass).toBe(false);
|
|
182
|
+
expect((instance.errors ?? []).join("\n")).toMatch(/title/);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dspack surface → A2UI v0.9 surface emitter.
|
|
3
|
+
*
|
|
4
|
+
* Compiles a dspack surface document (the protocol-neutral component tree
|
|
5
|
+
* defined by dspack.surface.v0_1.schema.json) into the A2UI message array
|
|
6
|
+
* shape used by surface/*.surface.json: one `createSurface` (catalogId +
|
|
7
|
+
* dspack-token theme) followed by one `updateComponents` with a flat,
|
|
8
|
+
* id-referenced component list.
|
|
9
|
+
*
|
|
10
|
+
* Deterministic by construction: same surface + contract + profile => same
|
|
11
|
+
* messages. All projection knowledge is data in the profile's `surfacePlan`
|
|
12
|
+
* directives; this engine contains no component-name-specific code.
|
|
13
|
+
*
|
|
14
|
+
* Honest scope (mirrors MAPPING.md):
|
|
15
|
+
* - Compound composition flattens per the documented casualty mapping
|
|
16
|
+
* (`subText` / `subButtonText` consume the node's whole subtree).
|
|
17
|
+
* - A2UI requires declarative actions the surface format does not express;
|
|
18
|
+
* they are synthesized (deterministic event-name slug) and recorded as
|
|
19
|
+
* warnings, not silently invented.
|
|
20
|
+
* - The message envelope is A2UI v0.9 (`version: "v0.9"`), the version the
|
|
21
|
+
* maintained renderers speak. The emitted component instances themselves
|
|
22
|
+
* are version-independent and instance-validate (gate A3) against both
|
|
23
|
+
* generated catalogs.
|
|
24
|
+
*/
|
|
25
|
+
import type { DspackDoc, DspackSurface, Json, SurfaceNode, Warning } from "../../types.js";
|
|
26
|
+
import { shadcnProfile, type ComponentPlan, type Profile } from "../../transform/profiles.js";
|
|
27
|
+
import { toHex6 } from "../../transform/color.js";
|
|
28
|
+
import { collectChildren } from "../csr.js";
|
|
29
|
+
|
|
30
|
+
export class EmitSurfaceError extends Error {
|
|
31
|
+
constructor(
|
|
32
|
+
message: string,
|
|
33
|
+
readonly path: string,
|
|
34
|
+
) {
|
|
35
|
+
super(`${message} (at ${path})`);
|
|
36
|
+
this.name = "EmitSurfaceError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface EmitSurfaceResult {
|
|
41
|
+
/** The A2UI v0.9 message array (createSurface + updateComponents). */
|
|
42
|
+
messages: Json[];
|
|
43
|
+
/** Every synthesis/drop performed — nothing is silent. */
|
|
44
|
+
warnings: Warning[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface EmitSurfaceOptions {
|
|
48
|
+
profile?: Profile;
|
|
49
|
+
/** Defaults to a slug of the surface intent. */
|
|
50
|
+
surfaceId?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function emitSurface(
|
|
54
|
+
surface: DspackSurface,
|
|
55
|
+
doc: DspackDoc,
|
|
56
|
+
options: EmitSurfaceOptions = {},
|
|
57
|
+
): EmitSurfaceResult {
|
|
58
|
+
const profile = options.profile ?? shadcnProfile;
|
|
59
|
+
if (surface.dspackSurface !== "0.1") {
|
|
60
|
+
throw new EmitSurfaceError(
|
|
61
|
+
`unsupported dspackSurface version '${surface.dspackSurface}' (this emitter targets 0.1)`,
|
|
62
|
+
"$",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
if (surface.system !== doc.name) {
|
|
66
|
+
throw new EmitSurfaceError(
|
|
67
|
+
`surface.system '${surface.system}' does not match contract name '${doc.name}'`,
|
|
68
|
+
"$.system",
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const byDspackId = new Map<string, ComponentPlan>();
|
|
73
|
+
for (const plan of profile.components) {
|
|
74
|
+
if (plan.dspackId) byDspackId.set(plan.dspackId, plan);
|
|
75
|
+
}
|
|
76
|
+
const emitter = new SurfaceEmitter(profile, byDspackId);
|
|
77
|
+
const rootId = emitter.emitNode(surface.root, "$.root");
|
|
78
|
+
|
|
79
|
+
const surfaceId = options.surfaceId ?? slug(surface.intent);
|
|
80
|
+
const theme: Json = { agentDisplayName: `${doc.name} via dspack` };
|
|
81
|
+
const primaryHex = primaryColor(doc, profile);
|
|
82
|
+
if (primaryHex) theme.primaryColor = primaryHex;
|
|
83
|
+
|
|
84
|
+
const messages: Json[] = [
|
|
85
|
+
{
|
|
86
|
+
version: "v0.9",
|
|
87
|
+
createSurface: {
|
|
88
|
+
surfaceId,
|
|
89
|
+
catalogId: `${profile.catalogIdBase}/v0_9_1/catalog.json`,
|
|
90
|
+
theme,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
version: "v0.9",
|
|
95
|
+
updateComponents: { surfaceId, components: emitter.components },
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
void rootId; // root is components[0] by construction (pre-order emission)
|
|
99
|
+
return { messages, warnings: emitter.warnings };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function primaryColor(doc: DspackDoc, profile: Profile): string | null {
|
|
103
|
+
const { category, name } = profile.primaryColorToken;
|
|
104
|
+
const raw = doc.tokens?.[category]?.values?.[name]?.value;
|
|
105
|
+
return typeof raw === "string" ? toHex6(raw) : null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function slug(value: string): string {
|
|
109
|
+
const s = value
|
|
110
|
+
.toLowerCase()
|
|
111
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
112
|
+
.replace(/^_+|_+$/g, "");
|
|
113
|
+
return s || "surface";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
class SurfaceEmitter {
|
|
117
|
+
readonly components: Json[] = [];
|
|
118
|
+
readonly warnings: Warning[] = [];
|
|
119
|
+
private readonly usedIds = new Set<string>();
|
|
120
|
+
|
|
121
|
+
constructor(
|
|
122
|
+
private readonly profile: Profile,
|
|
123
|
+
private readonly byDspackId: Map<string, ComponentPlan>,
|
|
124
|
+
) {}
|
|
125
|
+
|
|
126
|
+
/** Emits the component for `node` (and its subtree) and returns its instance id. */
|
|
127
|
+
emitNode(node: SurfaceNode, path: string): string {
|
|
128
|
+
const plan = this.byDspackId.get(node.component);
|
|
129
|
+
if (!plan) {
|
|
130
|
+
throw new EmitSurfaceError(
|
|
131
|
+
`unknown component '${node.component}': not a mapped component of the '${this.profile.catalogTitle}' profile ` +
|
|
132
|
+
`(sub-components are consumed by their compound parent and cannot be emitted standalone)`,
|
|
133
|
+
path,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// A2UI renderers begin at the component with id "root" (see the
|
|
138
|
+
// hand-authored surfaces); the surface root always emits under that id.
|
|
139
|
+
const id = path === "$.root" ? this.allocateId("root", path) : this.allocateId(node.id ?? plan.a2ui.toLowerCase(), path);
|
|
140
|
+
// Reserve the slot so parent components precede children in the flat list.
|
|
141
|
+
const index = this.components.length;
|
|
142
|
+
this.components.push({});
|
|
143
|
+
const instance: Json = { id, component: plan.a2ui };
|
|
144
|
+
|
|
145
|
+
this.applyPropMap(node, plan, instance, path);
|
|
146
|
+
const sp = plan.surfacePlan ?? {};
|
|
147
|
+
const consumesSubtree = Boolean(sp.subText || sp.subButtonText);
|
|
148
|
+
|
|
149
|
+
if (sp.structuralPassthrough) {
|
|
150
|
+
for (const key of sp.structuralPassthrough) {
|
|
151
|
+
const value = node.props?.[key];
|
|
152
|
+
if (value !== undefined) instance[key] = value as Json[keyof Json];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (sp.subText || sp.subButtonText) this.applySubContent(node, sp.subText ?? {}, sp.subButtonText ?? {}, instance, path);
|
|
156
|
+
if (sp.textProp && node.text !== undefined) instance[sp.textProp] = node.text;
|
|
157
|
+
if (sp.textChildProp && node.text !== undefined) {
|
|
158
|
+
instance[sp.textChildProp] = this.emitTextPrimitive(node.text, `${id}_label`, path);
|
|
159
|
+
}
|
|
160
|
+
if (sp.actionProp) {
|
|
161
|
+
const eventName = slug(node.id ?? (instance.confirmLabel as string) ?? node.text ?? node.component);
|
|
162
|
+
instance[sp.actionProp] = { event: { name: eventName, context: {} } };
|
|
163
|
+
this.warnings.push({
|
|
164
|
+
code: "surface-synthesized-action",
|
|
165
|
+
message: `${path}: A2UI requires a declarative action on ${plan.a2ui}; synthesized event '${eventName}'.`,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!consumesSubtree) {
|
|
170
|
+
const childNodes = collectChildren(node);
|
|
171
|
+
if (childNodes.length > 0) {
|
|
172
|
+
const childIds = childNodes.map((child, i) => this.emitNode(child.node, `${path}${child.suffix}[${i}]`));
|
|
173
|
+
if (sp.childrenProp) {
|
|
174
|
+
instance[sp.childrenProp] = childIds;
|
|
175
|
+
} else if (sp.childProp) {
|
|
176
|
+
instance[sp.childProp] = childIds.length === 1 ? childIds[0] : this.wrapInColumn(childIds, id, path);
|
|
177
|
+
} else {
|
|
178
|
+
throw new EmitSurfaceError(
|
|
179
|
+
`component '${node.component}' has children but its surface plan declares no child slot`,
|
|
180
|
+
path,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
this.components[index] = instance;
|
|
187
|
+
return id;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** CSR props -> A2UI props via the profile's existing PropPlan projections. */
|
|
191
|
+
private applyPropMap(node: SurfaceNode, plan: ComponentPlan, instance: Json, path: string): void {
|
|
192
|
+
for (const [prop, raw] of Object.entries(node.props ?? {})) {
|
|
193
|
+
if (plan.surfacePlan?.structuralPassthrough?.includes(prop)) continue;
|
|
194
|
+
const pp = plan.propMap?.[prop];
|
|
195
|
+
if (!pp) {
|
|
196
|
+
this.warnings.push({
|
|
197
|
+
code: "surface-prop-dropped",
|
|
198
|
+
message: `${path}: prop '${prop}' on '${node.component}' has no A2UI projection; dropped.`,
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const value = pp.valueMap ? (pp.valueMap[String(raw)] ?? pp.default) : raw;
|
|
203
|
+
if (value === undefined) {
|
|
204
|
+
this.warnings.push({
|
|
205
|
+
code: "surface-prop-value-dropped",
|
|
206
|
+
message: `${path}: value '${String(raw)}' of prop '${prop}' has no projection and no default; dropped.`,
|
|
207
|
+
});
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
instance[pp.a2ui] = value as Json[keyof Json];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Compound flattening: pull text out of named sub-components anywhere in the
|
|
216
|
+
* subtree. The subtree is consumed — the documented composition casualty.
|
|
217
|
+
*/
|
|
218
|
+
private applySubContent(
|
|
219
|
+
node: SurfaceNode,
|
|
220
|
+
subText: Record<string, string>,
|
|
221
|
+
subButtonText: Record<string, string>,
|
|
222
|
+
instance: Json,
|
|
223
|
+
path: string,
|
|
224
|
+
): void {
|
|
225
|
+
const visit = (n: SurfaceNode, insideSub: string | null): void => {
|
|
226
|
+
const textProp = subText[n.component];
|
|
227
|
+
if (textProp !== undefined && n.text !== undefined && instance[textProp] === undefined) {
|
|
228
|
+
instance[textProp] = n.text;
|
|
229
|
+
}
|
|
230
|
+
const buttonProp = insideSub ? subButtonText[insideSub] : undefined;
|
|
231
|
+
if (buttonProp !== undefined && n.text !== undefined && instance[buttonProp] === undefined && n !== node) {
|
|
232
|
+
// Only a label-bearing component qualifies (one whose surface plan
|
|
233
|
+
// projects its text as a child label, e.g. the trigger's button) —
|
|
234
|
+
// never incidental text on other descendants.
|
|
235
|
+
const plan = this.byDspackId.get(n.component);
|
|
236
|
+
if (plan?.surfacePlan?.textChildProp) instance[buttonProp] = n.text;
|
|
237
|
+
}
|
|
238
|
+
const nextInside = subButtonText[n.component] !== undefined ? n.component : insideSub;
|
|
239
|
+
for (const child of collectChildren(n)) visit(child.node, nextInside);
|
|
240
|
+
};
|
|
241
|
+
visit(node, null);
|
|
242
|
+
this.warnings.push({
|
|
243
|
+
code: "surface-composition-flattened",
|
|
244
|
+
message: `${path}: compound '${node.component}' subtree flattened onto emitted props (documented casualty; nested props beyond text are not carried).`,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private emitTextPrimitive(text: string, preferredId: string, path: string): string {
|
|
249
|
+
const { textComponent, textProp } = this.profile.surfaceSynthesis;
|
|
250
|
+
const id = this.allocateId(preferredId, path);
|
|
251
|
+
this.components.push({ id, component: textComponent, [textProp]: text });
|
|
252
|
+
this.warnings.push({
|
|
253
|
+
code: "surface-synthesized-text",
|
|
254
|
+
message: `${path}: node text projected as a synthesized ${textComponent} child ('${id}') — the surface format has no text primitive.`,
|
|
255
|
+
});
|
|
256
|
+
return id;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private wrapInColumn(childIds: string[], parentId: string, path: string): string {
|
|
260
|
+
const { wrapComponent, wrapChildrenProp } = this.profile.surfaceSynthesis;
|
|
261
|
+
const id = this.allocateId(`${parentId}_col`, path);
|
|
262
|
+
this.components.push({
|
|
263
|
+
id,
|
|
264
|
+
component: wrapComponent,
|
|
265
|
+
[wrapChildrenProp]: childIds,
|
|
266
|
+
});
|
|
267
|
+
this.warnings.push({
|
|
268
|
+
code: "surface-synthesized-wrap",
|
|
269
|
+
message: `${path}: ${childIds.length} children wrapped in a synthesized ${wrapComponent} ('${id}') — the target slot takes a single child.`,
|
|
270
|
+
});
|
|
271
|
+
return id;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private allocateId(preferred: string, path: string): string {
|
|
275
|
+
let id = slug(preferred);
|
|
276
|
+
let n = 2;
|
|
277
|
+
while (this.usedIds.has(id)) id = `${slug(preferred)}_${n++}`;
|
|
278
|
+
if (id !== slug(preferred)) {
|
|
279
|
+
this.warnings.push({
|
|
280
|
+
code: "surface-id-deduplicated",
|
|
281
|
+
message: `${path}: node id '${preferred}' already used; emitted as '${id}'.`,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
this.usedIds.add(id);
|
|
285
|
+
return id;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared CSR (dspack surface) traversal semantics used by every emitter target.
|
|
3
|
+
*
|
|
4
|
+
* The child-ordering rule is part of the CSR's meaning, not of any one
|
|
5
|
+
* protocol projection: `children` first, then slot groups in sorted-key
|
|
6
|
+
* order. Both targets (a2ui, json-render) must walk a surface identically or
|
|
7
|
+
* the same governed CSR would emit differently-ordered artifacts per target.
|
|
8
|
+
*/
|
|
9
|
+
import type { SurfaceNode } from "../types.js";
|
|
10
|
+
|
|
11
|
+
export interface ChildRef {
|
|
12
|
+
node: SurfaceNode;
|
|
13
|
+
suffix: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Ordered children: `children` first, then slots in sorted-key order (deterministic). */
|
|
17
|
+
export function collectChildren(node: SurfaceNode): ChildRef[] {
|
|
18
|
+
const refs: ChildRef[] = (node.children ?? []).map((n) => ({ node: n, suffix: ".children" }));
|
|
19
|
+
for (const key of Object.keys(node.slots ?? {}).sort()) {
|
|
20
|
+
for (const n of node.slots![key]) refs.push({ node: n, suffix: `.slots.${key}` });
|
|
21
|
+
}
|
|
22
|
+
return refs;
|
|
23
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dspack contract → generated json-render modules (TypeScript source text).
|
|
3
|
+
*
|
|
4
|
+
* catalog.ts — defineCatalog(schema, …) with Zod prop schemas compiled
|
|
5
|
+
* from the contract's prop vocabulary (enums verbatim).
|
|
6
|
+
* registry.tsx — defineRegistry(catalog, …) with typed stub renderers, one
|
|
7
|
+
* per catalog component. Stubs exist so the registry's
|
|
8
|
+
* catalog-inferred prop typing is *compiled*, not assumed
|
|
9
|
+
* (gate J1); consumers replace them with real components.
|
|
10
|
+
*
|
|
11
|
+
* The generators emit source text only — this module has no runtime
|
|
12
|
+
* dependency on @json-render/* or zod. The generated modules are compiled
|
|
13
|
+
* and executed in gates/json-render, which pins those packages (the a2ui
|
|
14
|
+
* side of the repo is pinned to zod v3 via @a2ui/web_core, so the zod v4
|
|
15
|
+
* packages json-render requires cannot live in the root dependency tree).
|
|
16
|
+
*
|
|
17
|
+
* All props are `.nullable()`: CSR props are optional, and json-render's
|
|
18
|
+
* catalog idiom prefers nullable over optional for structured-output
|
|
19
|
+
* compatibility.
|
|
20
|
+
*/
|
|
21
|
+
import type { DspackDoc } from "../../types.js";
|
|
22
|
+
import { buildCatalogModel, type CatalogComponent, type CatalogModel, type CatalogProp } from "./model.js";
|
|
23
|
+
import { shadcnJsonRenderProfile, type JsonRenderProfile } from "./profile.js";
|
|
24
|
+
|
|
25
|
+
export interface JsonRenderModules {
|
|
26
|
+
/** Source text of the generated catalog module (catalog.ts). */
|
|
27
|
+
catalogTs: string;
|
|
28
|
+
/** Source text of the generated registry module (registry.tsx). */
|
|
29
|
+
registryTsx: string;
|
|
30
|
+
/** The model both modules were rendered from. */
|
|
31
|
+
model: CatalogModel;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function generateJsonRenderModules(
|
|
35
|
+
doc: DspackDoc,
|
|
36
|
+
profile: JsonRenderProfile = shadcnJsonRenderProfile,
|
|
37
|
+
): JsonRenderModules {
|
|
38
|
+
const model = buildCatalogModel(doc, profile);
|
|
39
|
+
return {
|
|
40
|
+
catalogTs: renderCatalogModule(model, doc),
|
|
41
|
+
registryTsx: renderRegistryModule(model, doc),
|
|
42
|
+
model,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function header(what: string, doc: DspackDoc): string {
|
|
47
|
+
return (
|
|
48
|
+
`/**\n` +
|
|
49
|
+
` * GENERATED ${what} — do not edit.\n` +
|
|
50
|
+
` * Generated by dspack-emit (--target json-render) from the dspack contract\n` +
|
|
51
|
+
` * ${JSON.stringify(doc.name)} (dspack ${JSON.stringify(doc.dspack)}).\n` +
|
|
52
|
+
` */\n`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function renderCatalogModule(model: CatalogModel, doc: DspackDoc): string {
|
|
57
|
+
const lines: string[] = [];
|
|
58
|
+
lines.push(header("json-render catalog", doc).trimEnd());
|
|
59
|
+
lines.push(`import { defineCatalog } from "@json-render/core";`);
|
|
60
|
+
lines.push(`import { schema } from "@json-render/react/schema";`);
|
|
61
|
+
lines.push(`import { z } from "zod";`);
|
|
62
|
+
lines.push(``);
|
|
63
|
+
lines.push(`export const catalog = defineCatalog(schema, {`);
|
|
64
|
+
lines.push(` components: {`);
|
|
65
|
+
for (const component of model.components) {
|
|
66
|
+
for (const excluded of component.excludedProps) {
|
|
67
|
+
lines.push(` // ${component.dspackId}.${excluded.name} excluded: ${excluded.reason}`);
|
|
68
|
+
}
|
|
69
|
+
lines.push(` ${component.name}: {`);
|
|
70
|
+
lines.push(` props: z.object({`);
|
|
71
|
+
for (const prop of component.props) {
|
|
72
|
+
lines.push(` ${propKey(prop.name)}: ${zodExpr(prop)},`);
|
|
73
|
+
}
|
|
74
|
+
lines.push(` }),`);
|
|
75
|
+
// Every component accepts children: the CSR node schema allows children
|
|
76
|
+
// on any node (composition correctness is gate S3's job, pre-emission).
|
|
77
|
+
lines.push(` slots: ["default"],`);
|
|
78
|
+
lines.push(` description: ${JSON.stringify(component.description)},`);
|
|
79
|
+
lines.push(` },`);
|
|
80
|
+
}
|
|
81
|
+
lines.push(` },`);
|
|
82
|
+
lines.push(` actions: {},`);
|
|
83
|
+
lines.push(`});`);
|
|
84
|
+
lines.push(``);
|
|
85
|
+
return lines.join("\n");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function renderRegistryModule(model: CatalogModel, doc: DspackDoc): string {
|
|
89
|
+
const lines: string[] = [];
|
|
90
|
+
lines.push(header("json-render registry (stub implementations)", doc).trimEnd());
|
|
91
|
+
lines.push(`import { defineRegistry } from "@json-render/react";`);
|
|
92
|
+
lines.push(`import { catalog } from "./catalog.js";`);
|
|
93
|
+
lines.push(``);
|
|
94
|
+
lines.push(`export const { registry } = defineRegistry(catalog, {`);
|
|
95
|
+
lines.push(` components: {`);
|
|
96
|
+
for (const component of model.components) {
|
|
97
|
+
lines.push(` ${component.name}: ({ props, children }) => (`);
|
|
98
|
+
lines.push(` <div data-dspack=${JSON.stringify(component.dspackId)}>`);
|
|
99
|
+
lines.push(` {props.text}`);
|
|
100
|
+
lines.push(` {children}`);
|
|
101
|
+
lines.push(` </div>`);
|
|
102
|
+
lines.push(` ),`);
|
|
103
|
+
}
|
|
104
|
+
lines.push(` },`);
|
|
105
|
+
lines.push(`});`);
|
|
106
|
+
lines.push(``);
|
|
107
|
+
return lines.join("\n");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function propKey(name: string): string {
|
|
111
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function zodExpr(prop: CatalogProp): string {
|
|
115
|
+
let base: string;
|
|
116
|
+
switch (prop.kind) {
|
|
117
|
+
case "enum":
|
|
118
|
+
base = `z.enum([${prop.values!.map((v) => JSON.stringify(v)).join(", ")}])`;
|
|
119
|
+
break;
|
|
120
|
+
case "string":
|
|
121
|
+
base = `z.string()`;
|
|
122
|
+
break;
|
|
123
|
+
case "boolean":
|
|
124
|
+
base = `z.boolean()`;
|
|
125
|
+
break;
|
|
126
|
+
case "number":
|
|
127
|
+
base = `z.number()`;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
const described = prop.description ? `${base}.describe(${JSON.stringify(prop.description)})` : base;
|
|
131
|
+
return `${described}.nullable()`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Re-exported for callers that only need naming/vocabulary (e.g. tests).
|
|
135
|
+
export type { CatalogComponent };
|