@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,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dspack surface → json-render spec emitter.
|
|
3
|
+
*
|
|
4
|
+
* Compiles a dspack surface document (CSR) into json-render's flat spec
|
|
5
|
+
* shape: `{ root, elements }` where each element is
|
|
6
|
+
* `{ type, props, children, visible }` and children reference element keys.
|
|
7
|
+
*
|
|
8
|
+
* Because the json-render catalog is generated from the contract 1:1
|
|
9
|
+
* (model.ts), this emitter performs no compound flattening and no prop value
|
|
10
|
+
* projection — CSR nesting becomes element nesting, prop names and enum
|
|
11
|
+
* values carry verbatim. The projections that DO happen are each recorded:
|
|
12
|
+
* - `text` leaves become the universal `text` prop;
|
|
13
|
+
* - slot *names* are not representable (json-render children are one
|
|
14
|
+
* ordered array), so slotted children flatten into child order — warned;
|
|
15
|
+
* - props outside the declarative catalog (function-typed handlers,
|
|
16
|
+
* unknown names) are dropped — warned.
|
|
17
|
+
*
|
|
18
|
+
* Gate naming (this target's equivalents of the a2ui A-gates):
|
|
19
|
+
* J1 — generated catalog + registry modules compile (tsc --noEmit).
|
|
20
|
+
* J2 — spec structural integrity (json-render's validateSpec).
|
|
21
|
+
* J3 — instance acceptance: catalog.validate() parses the spec with the
|
|
22
|
+
* generated Zod schemas.
|
|
23
|
+
* J1–J3 run with the real framework in gates/json-render (pinned zod v4,
|
|
24
|
+
* isolated from the a2ui side's zod v3 pin). `validateSpecAgainstModel`
|
|
25
|
+
* below is the offline mirror of J2+J3's vocabulary semantics, used by unit
|
|
26
|
+
* tests and the CLI without the framework dependency.
|
|
27
|
+
*/
|
|
28
|
+
import type { DspackDoc, DspackSurface, Warning } from "../../types.js";
|
|
29
|
+
import { type CatalogModel } from "./model.js";
|
|
30
|
+
import { type JsonRenderProfile } from "./profile.js";
|
|
31
|
+
export declare class EmitJsonRenderError extends Error {
|
|
32
|
+
readonly path: string;
|
|
33
|
+
constructor(message: string, path: string);
|
|
34
|
+
}
|
|
35
|
+
export interface JsonRenderElement {
|
|
36
|
+
type: string;
|
|
37
|
+
props: Record<string, unknown>;
|
|
38
|
+
children: string[];
|
|
39
|
+
/** Required by json-render's element schema; the CSR has no visibility conditions. */
|
|
40
|
+
visible: true;
|
|
41
|
+
}
|
|
42
|
+
export interface JsonRenderSpec {
|
|
43
|
+
root: string;
|
|
44
|
+
elements: Record<string, JsonRenderElement>;
|
|
45
|
+
}
|
|
46
|
+
export interface EmitJsonRenderResult {
|
|
47
|
+
spec: JsonRenderSpec;
|
|
48
|
+
/** Every drop/projection performed — nothing is silent. */
|
|
49
|
+
warnings: Warning[];
|
|
50
|
+
}
|
|
51
|
+
export interface EmitJsonRenderOptions {
|
|
52
|
+
profile?: JsonRenderProfile;
|
|
53
|
+
}
|
|
54
|
+
export declare function emitJsonRenderSpec(surface: DspackSurface, doc: DspackDoc, options?: EmitJsonRenderOptions): EmitJsonRenderResult;
|
|
55
|
+
export interface SpecFinding {
|
|
56
|
+
path: string;
|
|
57
|
+
message: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validates an emitted spec against the catalog model: root resolves, child
|
|
61
|
+
* references resolve, every element type is cataloged, every prop is known
|
|
62
|
+
* and enum values are in vocabulary. This is the framework-free mirror used
|
|
63
|
+
* by unit tests and the CLI; the authoritative check is gates J2/J3 with the
|
|
64
|
+
* generated Zod catalog (gates/json-render).
|
|
65
|
+
*/
|
|
66
|
+
export declare function validateSpecAgainstModel(spec: JsonRenderSpec, model: CatalogModel): SpecFinding[];
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { collectChildren } from "../csr.js";
|
|
2
|
+
import { buildCatalogModel } from "./model.js";
|
|
3
|
+
import { shadcnJsonRenderProfile } from "./profile.js";
|
|
4
|
+
export class EmitJsonRenderError extends Error {
|
|
5
|
+
path;
|
|
6
|
+
constructor(message, path) {
|
|
7
|
+
super(`${message} (at ${path})`);
|
|
8
|
+
this.path = path;
|
|
9
|
+
this.name = "EmitJsonRenderError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function emitJsonRenderSpec(surface, doc, options = {}) {
|
|
13
|
+
const profile = options.profile ?? shadcnJsonRenderProfile;
|
|
14
|
+
if (surface.dspackSurface !== "0.1") {
|
|
15
|
+
throw new EmitJsonRenderError(`unsupported dspackSurface version '${surface.dspackSurface}' (this emitter targets 0.1)`, "$");
|
|
16
|
+
}
|
|
17
|
+
if (surface.system !== doc.name) {
|
|
18
|
+
throw new EmitJsonRenderError(`surface.system '${surface.system}' does not match contract name '${doc.name}'`, "$.system");
|
|
19
|
+
}
|
|
20
|
+
const model = buildCatalogModel(doc, profile);
|
|
21
|
+
const emitter = new SpecEmitter(model);
|
|
22
|
+
const rootKey = emitter.emitNode(surface.root, "$.root", true);
|
|
23
|
+
return { spec: { root: rootKey, elements: emitter.elements }, warnings: emitter.warnings };
|
|
24
|
+
}
|
|
25
|
+
class SpecEmitter {
|
|
26
|
+
elements = {};
|
|
27
|
+
warnings = [];
|
|
28
|
+
usedKeys = new Set();
|
|
29
|
+
byDspackId = new Map();
|
|
30
|
+
constructor(model) {
|
|
31
|
+
for (const component of model.components)
|
|
32
|
+
this.byDspackId.set(component.dspackId, component);
|
|
33
|
+
}
|
|
34
|
+
emitNode(node, path, isRoot = false) {
|
|
35
|
+
const component = this.byDspackId.get(node.component);
|
|
36
|
+
if (!component) {
|
|
37
|
+
throw new EmitJsonRenderError(`unknown component '${node.component}': not in the contract vocabulary this catalog was generated from`, path);
|
|
38
|
+
}
|
|
39
|
+
// Same convention as the a2ui target: the surface root always emits
|
|
40
|
+
// under the key "root" (deterministic entry point for consumers).
|
|
41
|
+
const key = this.allocateKey(isRoot ? "root" : (node.id ?? node.component), path);
|
|
42
|
+
const props = {};
|
|
43
|
+
const knownProps = new Map(component.props.map((p) => [p.name, p]));
|
|
44
|
+
for (const [name, value] of Object.entries(node.props ?? {})) {
|
|
45
|
+
const known = knownProps.get(name);
|
|
46
|
+
if (!known) {
|
|
47
|
+
this.warnings.push({
|
|
48
|
+
code: "jr-prop-dropped",
|
|
49
|
+
message: `${path}: prop '${name}' on '${node.component}' is not in the declarative catalog (handler or unknown prop); dropped.`,
|
|
50
|
+
});
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (known.synthesized) {
|
|
54
|
+
// Synthesized catalog props (the universal `text`) are populated only
|
|
55
|
+
// by this emitter's own projection — the CSR `text` leaf is their
|
|
56
|
+
// single source; accepting them via `props` would bypass that rule.
|
|
57
|
+
this.warnings.push({
|
|
58
|
+
code: "jr-prop-dropped",
|
|
59
|
+
message: `${path}: prop '${name}' on '${node.component}' is reserved for the CSR '${name}' leaf projection (set node.${name}, not props.${name}); dropped.`,
|
|
60
|
+
});
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
props[name] = value;
|
|
64
|
+
}
|
|
65
|
+
if (node.text !== undefined)
|
|
66
|
+
props.text = node.text;
|
|
67
|
+
if (node.slots && Object.keys(node.slots).length > 0) {
|
|
68
|
+
this.warnings.push({
|
|
69
|
+
code: "jr-slot-names-flattened",
|
|
70
|
+
message: `${path}: json-render children form one ordered array; slot names (${Object.keys(node.slots).sort().join(", ")}) are not carried — slotted children appended in sorted-slot order.`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const element = { type: component.name, props, children: [], visible: true };
|
|
74
|
+
this.elements[key] = element; // parent registered before children (insertion order = pre-order)
|
|
75
|
+
element.children = collectChildren(node).map((child, i) => this.emitNode(child.node, `${path}${child.suffix}[${i}]`));
|
|
76
|
+
return key;
|
|
77
|
+
}
|
|
78
|
+
allocateKey(preferred, path) {
|
|
79
|
+
const base = slugKey(preferred);
|
|
80
|
+
let key = base;
|
|
81
|
+
let n = 2;
|
|
82
|
+
while (this.usedKeys.has(key))
|
|
83
|
+
key = `${base}_${n++}`;
|
|
84
|
+
if (key !== base) {
|
|
85
|
+
this.warnings.push({
|
|
86
|
+
code: "jr-key-deduplicated",
|
|
87
|
+
message: `${path}: element key '${base}' already used; emitted as '${key}'.`,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
this.usedKeys.add(key);
|
|
91
|
+
return key;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function slugKey(value) {
|
|
95
|
+
const s = value
|
|
96
|
+
.toLowerCase()
|
|
97
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
98
|
+
.replace(/^_+|_+$/g, "");
|
|
99
|
+
return s || "el";
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Validates an emitted spec against the catalog model: root resolves, child
|
|
103
|
+
* references resolve, every element type is cataloged, every prop is known
|
|
104
|
+
* and enum values are in vocabulary. This is the framework-free mirror used
|
|
105
|
+
* by unit tests and the CLI; the authoritative check is gates J2/J3 with the
|
|
106
|
+
* generated Zod catalog (gates/json-render).
|
|
107
|
+
*/
|
|
108
|
+
export function validateSpecAgainstModel(spec, model) {
|
|
109
|
+
const findings = [];
|
|
110
|
+
const byName = new Map(model.components.map((c) => [c.name, c]));
|
|
111
|
+
if (!spec.elements[spec.root]) {
|
|
112
|
+
findings.push({ path: "$.root", message: `root '${spec.root}' not found in elements` });
|
|
113
|
+
}
|
|
114
|
+
for (const [key, element] of Object.entries(spec.elements)) {
|
|
115
|
+
const componentPath = `$.elements.${key}`;
|
|
116
|
+
const component = byName.get(element.type);
|
|
117
|
+
if (!component) {
|
|
118
|
+
findings.push({ path: `${componentPath}.type`, message: `unknown component type '${element.type}'` });
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const knownProps = new Map(component.props.map((p) => [p.name, p]));
|
|
122
|
+
for (const [name, value] of Object.entries(element.props)) {
|
|
123
|
+
const prop = knownProps.get(name);
|
|
124
|
+
if (!prop) {
|
|
125
|
+
findings.push({ path: `${componentPath}.props.${name}`, message: `unknown prop on '${element.type}'` });
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (prop.kind === "enum" && value !== null && !prop.values.includes(String(value))) {
|
|
129
|
+
findings.push({
|
|
130
|
+
path: `${componentPath}.props.${name}`,
|
|
131
|
+
message: `value '${String(value)}' not in enum vocabulary [${prop.values.join(", ")}]`,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const child of element.children) {
|
|
136
|
+
if (!spec.elements[child]) {
|
|
137
|
+
findings.push({ path: `${componentPath}.children`, message: `child '${child}' not found in elements` });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return findings;
|
|
142
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog model: the deterministic middle representation between a dspack
|
|
3
|
+
* contract and the generated json-render modules.
|
|
4
|
+
*
|
|
5
|
+
* One entry per contract component AND per sub-component — json-render specs
|
|
6
|
+
* nest by reference, so dspack compound composition survives whole (no
|
|
7
|
+
* flattening step, no casualty mapping). The model is also the offline
|
|
8
|
+
* validation vocabulary for emitted specs (see emit.ts); the generated Zod
|
|
9
|
+
* catalog is its authoritative framework-side twin (see codegen.ts and
|
|
10
|
+
* gates/json-render).
|
|
11
|
+
*
|
|
12
|
+
* Declarative boundary: function-typed props (event handlers) cannot appear
|
|
13
|
+
* in a declarative catalog; they are excluded and recorded on the model —
|
|
14
|
+
* never silently dropped.
|
|
15
|
+
*/
|
|
16
|
+
import type { DspackDoc } from "../../types.js";
|
|
17
|
+
import type { JsonRenderProfile } from "./profile.js";
|
|
18
|
+
export interface CatalogModel {
|
|
19
|
+
/** Contract name (surface.system must match, same rule as the a2ui target). */
|
|
20
|
+
system: string;
|
|
21
|
+
/** Ordered: contract component order; each component's sub-components follow it. */
|
|
22
|
+
components: CatalogComponent[];
|
|
23
|
+
}
|
|
24
|
+
export interface CatalogComponent {
|
|
25
|
+
/** PascalCase catalog/registry key (derived from the dspack id). */
|
|
26
|
+
name: string;
|
|
27
|
+
/** The dspack id a CSR node uses in its `component` field. */
|
|
28
|
+
dspackId: string;
|
|
29
|
+
description: string;
|
|
30
|
+
/** Declarative props only, contract order; a universal `text` prop is appended. */
|
|
31
|
+
props: CatalogProp[];
|
|
32
|
+
/** Contract props excluded from the declarative catalog, with the reason. */
|
|
33
|
+
excludedProps: ExcludedProp[];
|
|
34
|
+
}
|
|
35
|
+
export interface CatalogProp {
|
|
36
|
+
name: string;
|
|
37
|
+
kind: "enum" | "string" | "boolean" | "number";
|
|
38
|
+
/** Enum vocabulary in contract order (enum kind only). */
|
|
39
|
+
values?: string[];
|
|
40
|
+
description?: string;
|
|
41
|
+
/**
|
|
42
|
+
* True for props this target adds beyond the contract (the universal
|
|
43
|
+
* `text` prop). Synthesized props are populated only by the emitter's own
|
|
44
|
+
* projection (the CSR `text` leaf), never via a node's `props` — the
|
|
45
|
+
* emitter warns and drops attempts to set them there.
|
|
46
|
+
*/
|
|
47
|
+
synthesized?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface ExcludedProp {
|
|
50
|
+
name: string;
|
|
51
|
+
reason: string;
|
|
52
|
+
}
|
|
53
|
+
export declare class CatalogModelError extends Error {
|
|
54
|
+
constructor(message: string);
|
|
55
|
+
}
|
|
56
|
+
export declare function pascalName(dspackId: string, profile: JsonRenderProfile): string;
|
|
57
|
+
export declare function buildCatalogModel(doc: DspackDoc, profile: JsonRenderProfile): CatalogModel;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export class CatalogModelError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "CatalogModelError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The universal `text` prop mirrors the CSR node schema, where `text` is a
|
|
9
|
+
* leaf field allowed on any node — the projection is uniform because the
|
|
10
|
+
* source is uniform.
|
|
11
|
+
*/
|
|
12
|
+
const TEXT_PROP = {
|
|
13
|
+
name: "text",
|
|
14
|
+
kind: "string",
|
|
15
|
+
description: "Text content (projected from the dspack surface node's `text` field).",
|
|
16
|
+
synthesized: true,
|
|
17
|
+
};
|
|
18
|
+
export function pascalName(dspackId, profile) {
|
|
19
|
+
const override = profile.nameOverrides?.[dspackId];
|
|
20
|
+
if (override)
|
|
21
|
+
return override;
|
|
22
|
+
return dspackId
|
|
23
|
+
.split(/[^a-zA-Z0-9]+/)
|
|
24
|
+
.filter(Boolean)
|
|
25
|
+
.map((seg) => seg[0].toUpperCase() + seg.slice(1))
|
|
26
|
+
.join("");
|
|
27
|
+
}
|
|
28
|
+
export function buildCatalogModel(doc, profile) {
|
|
29
|
+
const components = [];
|
|
30
|
+
const byDspackId = new Set();
|
|
31
|
+
const byName = new Set();
|
|
32
|
+
const add = (dspackId, description, props) => {
|
|
33
|
+
if (byDspackId.has(dspackId)) {
|
|
34
|
+
// dspack v0.3 makes sub-component ids unique document-wide (normative
|
|
35
|
+
// for governance contracts); a collision here is a malformed contract.
|
|
36
|
+
throw new CatalogModelError(`duplicate component/sub-component id '${dspackId}' in contract`);
|
|
37
|
+
}
|
|
38
|
+
const name = pascalName(dspackId, profile);
|
|
39
|
+
if (byName.has(name)) {
|
|
40
|
+
throw new CatalogModelError(`catalog name '${name}' (from '${dspackId}') collides with another component; add a profile nameOverride`);
|
|
41
|
+
}
|
|
42
|
+
byDspackId.add(dspackId);
|
|
43
|
+
byName.add(name);
|
|
44
|
+
const { props: catalogProps, excluded } = projectProps(props ?? {});
|
|
45
|
+
if (!catalogProps.some((p) => p.name === TEXT_PROP.name))
|
|
46
|
+
catalogProps.push(TEXT_PROP);
|
|
47
|
+
components.push({ name, dspackId, description, props: catalogProps, excludedProps: excluded });
|
|
48
|
+
};
|
|
49
|
+
for (const [id, component] of Object.entries(doc.components ?? {})) {
|
|
50
|
+
if (profile.intentionallyOmitted?.includes(id))
|
|
51
|
+
continue;
|
|
52
|
+
add(id, component.description, component.props);
|
|
53
|
+
for (const sub of subComponentsOf(component)) {
|
|
54
|
+
add(sub.id, sub.description ?? `${component.name} sub-component.`, sub.props);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { system: doc.name, components };
|
|
58
|
+
}
|
|
59
|
+
function projectProps(props) {
|
|
60
|
+
const out = [];
|
|
61
|
+
const excluded = [];
|
|
62
|
+
for (const [name, def] of Object.entries(props)) {
|
|
63
|
+
switch (def.type) {
|
|
64
|
+
case "enum": {
|
|
65
|
+
const values = (def.values ?? []).map((v) => (typeof v === "object" && v !== null ? v.value : v));
|
|
66
|
+
if (values.length === 0 || !values.every((v) => typeof v === "string")) {
|
|
67
|
+
excluded.push({ name, reason: `enum prop without an all-string value vocabulary` });
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
out.push({ name, kind: "enum", values: values, description: def.description });
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "string":
|
|
74
|
+
case "boolean":
|
|
75
|
+
case "number":
|
|
76
|
+
out.push({ name, kind: def.type, description: def.description });
|
|
77
|
+
break;
|
|
78
|
+
case "function":
|
|
79
|
+
excluded.push({ name, reason: "function-typed (event handler) — not representable in a declarative catalog" });
|
|
80
|
+
break;
|
|
81
|
+
default:
|
|
82
|
+
excluded.push({ name, reason: `unsupported prop type '${def.type}'` });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { props: out, excluded };
|
|
86
|
+
}
|
|
87
|
+
function subComponentsOf(component) {
|
|
88
|
+
const composition = component.composition;
|
|
89
|
+
const entries = [];
|
|
90
|
+
for (const raw of composition?.subComponents ?? []) {
|
|
91
|
+
if (typeof raw === "string") {
|
|
92
|
+
entries.push({ id: raw });
|
|
93
|
+
}
|
|
94
|
+
else if (raw && typeof raw === "object" && typeof raw.id === "string") {
|
|
95
|
+
entries.push(raw);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return entries;
|
|
99
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* json-render target profile — pure data, per the same profile mechanism as
|
|
3
|
+
* the a2ui target (the engine reads only profile + dspack document).
|
|
4
|
+
*
|
|
5
|
+
* Deliberately near-empty, and that emptiness is a finding: json-render's
|
|
6
|
+
* catalog is *generated from the contract*, so the target carries the
|
|
7
|
+
* contract's own vocabulary — every component and sub-component becomes a
|
|
8
|
+
* catalog component 1:1, props keep their dspack names and enum values, and
|
|
9
|
+
* compound composition survives as real nesting. There is no per-component
|
|
10
|
+
* projection table because nothing is projected; contrast the a2ui profile's
|
|
11
|
+
* surfacePlan flattening directives and documented casualties.
|
|
12
|
+
*/
|
|
13
|
+
export interface JsonRenderProfile {
|
|
14
|
+
/**
|
|
15
|
+
* Overrides for the mechanical kebab-case → PascalCase derivation of
|
|
16
|
+
* catalog component names from dspack ids (e.g. if a contract id produced
|
|
17
|
+
* a name colliding with another).
|
|
18
|
+
*/
|
|
19
|
+
nameOverrides?: Record<string, string>;
|
|
20
|
+
/** dspack component ids deliberately not cataloged. Must stay documented, never silent. */
|
|
21
|
+
intentionallyOmitted?: string[];
|
|
22
|
+
}
|
|
23
|
+
export declare const shadcnJsonRenderProfile: JsonRenderProfile;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token color synthesis: convert a dspack color value into the `#rrggbb` form
|
|
3
|
+
* that the A2UI `theme.primaryColor` schema requires (pattern ^#[0-9a-fA-F]{6}$).
|
|
4
|
+
*
|
|
5
|
+
* dspack stores resolved values as strings in whatever notation the source used.
|
|
6
|
+
* The shadcn source uses `hsl(H, S%, L%)`. Anything already in `#rrggbb` passes
|
|
7
|
+
* through; anything we cannot parse returns null so the caller can record a
|
|
8
|
+
* lossy/cannot-represent warning instead of emitting an invalid color.
|
|
9
|
+
*/
|
|
10
|
+
export declare function toHex6(value: string): string | null;
|
|
11
|
+
export declare function hslToHex(h: number, s: number, l: number): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token color synthesis: convert a dspack color value into the `#rrggbb` form
|
|
3
|
+
* that the A2UI `theme.primaryColor` schema requires (pattern ^#[0-9a-fA-F]{6}$).
|
|
4
|
+
*
|
|
5
|
+
* dspack stores resolved values as strings in whatever notation the source used.
|
|
6
|
+
* The shadcn source uses `hsl(H, S%, L%)`. Anything already in `#rrggbb` passes
|
|
7
|
+
* through; anything we cannot parse returns null so the caller can record a
|
|
8
|
+
* lossy/cannot-represent warning instead of emitting an invalid color.
|
|
9
|
+
*/
|
|
10
|
+
export function toHex6(value) {
|
|
11
|
+
const v = value.trim();
|
|
12
|
+
// Already a 6-digit hex.
|
|
13
|
+
const hex = /^#([0-9a-fA-F]{6})$/.exec(v);
|
|
14
|
+
if (hex)
|
|
15
|
+
return `#${hex[1].toLowerCase()}`;
|
|
16
|
+
// 3-digit hex -> expand.
|
|
17
|
+
const hex3 = /^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/.exec(v);
|
|
18
|
+
if (hex3)
|
|
19
|
+
return `#${hex3[1]}${hex3[1]}${hex3[2]}${hex3[2]}${hex3[3]}${hex3[3]}`.toLowerCase();
|
|
20
|
+
// hsl(H, S%, L%) / hsl(H S% L%), with an optional alpha channel, anchored to the
|
|
21
|
+
// end of the string so trailing garbage (e.g. "hsl(0,0%,0%)x") is rejected.
|
|
22
|
+
const hsl = /^hsla?\(\s*([\d.]+)\s*[, ]\s*([\d.]+)%\s*[, ]\s*([\d.]+)%\s*(?:[,/]\s*[\d.]+%?\s*)?\)\s*$/.exec(v);
|
|
23
|
+
if (hsl)
|
|
24
|
+
return hslToHex(parseFloat(hsl[1]), parseFloat(hsl[2]), parseFloat(hsl[3]));
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
export function hslToHex(h, s, l) {
|
|
28
|
+
s /= 100;
|
|
29
|
+
l /= 100;
|
|
30
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
31
|
+
const hp = (((h % 360) + 360) % 360) / 60;
|
|
32
|
+
const x = c * (1 - Math.abs((hp % 2) - 1));
|
|
33
|
+
let r = 0;
|
|
34
|
+
let g = 0;
|
|
35
|
+
let b = 0;
|
|
36
|
+
if (hp < 1)
|
|
37
|
+
[r, g, b] = [c, x, 0];
|
|
38
|
+
else if (hp < 2)
|
|
39
|
+
[r, g, b] = [x, c, 0];
|
|
40
|
+
else if (hp < 3)
|
|
41
|
+
[r, g, b] = [0, c, x];
|
|
42
|
+
else if (hp < 4)
|
|
43
|
+
[r, g, b] = [0, x, c];
|
|
44
|
+
else if (hp < 5)
|
|
45
|
+
[r, g, b] = [x, 0, c];
|
|
46
|
+
else
|
|
47
|
+
[r, g, b] = [c, 0, x];
|
|
48
|
+
const m = l - c / 2;
|
|
49
|
+
const ch = (n) => Math.round((n + m) * 255)
|
|
50
|
+
.toString(16)
|
|
51
|
+
.padStart(2, "0");
|
|
52
|
+
return `#${ch(r)}${ch(g)}${ch(b)}`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Versioned catalog emitter. The two supported A2UI versions differ only in:
|
|
3
|
+
* - v0.9.1: a `$defs.theme` object (carries `primaryColor`, #rrggbb).
|
|
4
|
+
* - v1.0: a `$defs.surfaceProperties` object + a top-level `instructions`.
|
|
5
|
+
* Everything else (components, inlined `$defs`, `anyComponent`) is identical.
|
|
6
|
+
*
|
|
7
|
+
* The theme / surfaceProperties shapes are copied from the checked-in fixtures
|
|
8
|
+
* (fixtures/a2ui/basic-catalog.{v0_9_1,v1_0}.json) and confirmed in pre-flight.
|
|
9
|
+
* We additionally carry the resolved design tokens as a documented extension
|
|
10
|
+
* (`additionalProperties: true` on both shapes permits it).
|
|
11
|
+
*/
|
|
12
|
+
import type { A2uiCatalog, A2uiVersion } from "../types.js";
|
|
13
|
+
import type { MappingResult } from "./mapping.js";
|
|
14
|
+
import type { Profile } from "./profiles.js";
|
|
15
|
+
export declare function emitCatalog(result: MappingResult, version: A2uiVersion, profile: Profile): A2uiCatalog;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { INLINED_DEFS } from "./inline-defs.js";
|
|
2
|
+
const VER_SEGMENT = {
|
|
3
|
+
"0.9.1": "v0_9_1",
|
|
4
|
+
"1.0": "v1_0",
|
|
5
|
+
};
|
|
6
|
+
export function emitCatalog(result, version, profile) {
|
|
7
|
+
const seg = VER_SEGMENT[version];
|
|
8
|
+
const id = `${profile.catalogIdBase}/${seg}/catalog.json`;
|
|
9
|
+
const anyComponent = {
|
|
10
|
+
oneOf: result.componentOrder.map((n) => ({ $ref: `#/components/${n}` })),
|
|
11
|
+
discriminator: { propertyName: "component" },
|
|
12
|
+
};
|
|
13
|
+
const $defs = {
|
|
14
|
+
...structuredClone(INLINED_DEFS),
|
|
15
|
+
anyComponent,
|
|
16
|
+
};
|
|
17
|
+
if (version === "0.9.1") {
|
|
18
|
+
$defs.theme = themeDef(result);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
$defs.surfaceProperties = surfacePropertiesDef(result);
|
|
22
|
+
}
|
|
23
|
+
const catalog = {
|
|
24
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
25
|
+
$id: id,
|
|
26
|
+
title: profile.catalogTitle,
|
|
27
|
+
description: profile.catalogDescription,
|
|
28
|
+
catalogId: id,
|
|
29
|
+
components: result.components,
|
|
30
|
+
$defs,
|
|
31
|
+
};
|
|
32
|
+
if (version === "1.0") {
|
|
33
|
+
// Insert `instructions` right after description, mirroring the v1.0 fixture order.
|
|
34
|
+
return {
|
|
35
|
+
$schema: catalog.$schema,
|
|
36
|
+
$id: catalog.$id,
|
|
37
|
+
title: catalog.title,
|
|
38
|
+
description: catalog.description,
|
|
39
|
+
instructions: profile.instructions,
|
|
40
|
+
catalogId: catalog.catalogId,
|
|
41
|
+
components: catalog.components,
|
|
42
|
+
$defs: catalog.$defs,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return catalog;
|
|
46
|
+
}
|
|
47
|
+
/** v0.9.1 — shape from fixtures/a2ui/basic-catalog.v0_9_1.json#/$defs/theme. */
|
|
48
|
+
function themeDef(result) {
|
|
49
|
+
const primaryColor = {
|
|
50
|
+
type: "string",
|
|
51
|
+
description: "The primary brand color used for highlights (e.g., primary buttons, active borders). " +
|
|
52
|
+
"Format: Hexadecimal code (e.g., '#00BFFF').",
|
|
53
|
+
pattern: "^#[0-9a-fA-F]{6}$",
|
|
54
|
+
};
|
|
55
|
+
if (result.primaryColorHex) {
|
|
56
|
+
primaryColor.default = result.primaryColorHex;
|
|
57
|
+
primaryColor["x-dspack-source"] = result.primaryColorSource;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
type: "object",
|
|
61
|
+
properties: {
|
|
62
|
+
primaryColor,
|
|
63
|
+
iconUrl: {
|
|
64
|
+
type: "string",
|
|
65
|
+
format: "uri",
|
|
66
|
+
description: "A URL for an image that identifies the agent or tool associated with the surface.",
|
|
67
|
+
},
|
|
68
|
+
agentDisplayName: {
|
|
69
|
+
type: "string",
|
|
70
|
+
description: "Text displayed next to the surface to identify the agent or tool that created it.",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
additionalProperties: true,
|
|
74
|
+
...result.tokenExtension,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** v1.0 — shape from fixtures/a2ui/basic-catalog.v1_0.json#/$defs/surfaceProperties. */
|
|
78
|
+
function surfacePropertiesDef(result) {
|
|
79
|
+
const def = {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties: {
|
|
82
|
+
iconUrl: {
|
|
83
|
+
type: "string",
|
|
84
|
+
format: "uri",
|
|
85
|
+
description: "A URL for an image that identifies the agent or tool associated with the surface.",
|
|
86
|
+
},
|
|
87
|
+
agentDisplayName: {
|
|
88
|
+
type: "string",
|
|
89
|
+
description: "Text displayed next to the surface to identify the agent or tool that created it.",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
additionalProperties: true,
|
|
93
|
+
...result.tokenExtension,
|
|
94
|
+
};
|
|
95
|
+
// v1.0 basic surfaceProperties has no primaryColor field; carry it as an extension.
|
|
96
|
+
if (result.primaryColorHex) {
|
|
97
|
+
def["x-dspack-primaryColor"] = { value: result.primaryColorHex, source: result.primaryColorSource };
|
|
98
|
+
}
|
|
99
|
+
return def;
|
|
100
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrates the transform: load -> map -> emit -> validate -> report.
|
|
3
|
+
*/
|
|
4
|
+
import type { A2uiCatalog, A2uiVersion, DspackDoc } from "../types.js";
|
|
5
|
+
import { type Profile } from "./profiles.js";
|
|
6
|
+
import { type MappingResult } from "./mapping.js";
|
|
7
|
+
import { type ReportBundle } from "./report.js";
|
|
8
|
+
import { type ValidationReport } from "../validate/ajv.js";
|
|
9
|
+
export interface TransformOutput {
|
|
10
|
+
catalog: A2uiCatalog;
|
|
11
|
+
mapping: MappingResult;
|
|
12
|
+
validation: ValidationReport;
|
|
13
|
+
report: ReportBundle;
|
|
14
|
+
}
|
|
15
|
+
export declare function transform(doc: DspackDoc, version: A2uiVersion, surface: unknown, profile?: Profile): TransformOutput;
|
|
16
|
+
/**
|
|
17
|
+
* Convenience wrapper for in-process callers (tests, the ingestion acceptance gate):
|
|
18
|
+
* accepts a raw dspack object and options, defaults the surface, and returns the same
|
|
19
|
+
* `TransformOutput`. This is the thin overload the Phase 2 plan's precondition asks for;
|
|
20
|
+
* the underlying pipeline is unchanged.
|
|
21
|
+
*/
|
|
22
|
+
export declare function transformFromJson(dspackJson: DspackDoc, options?: {
|
|
23
|
+
a2uiVersion?: A2uiVersion;
|
|
24
|
+
surface?: unknown;
|
|
25
|
+
profile?: Profile;
|
|
26
|
+
}): TransformOutput;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { shadcnProfile } from "./profiles.js";
|
|
2
|
+
import { mapDspack } from "./mapping.js";
|
|
3
|
+
import { emitCatalog } from "./emit.js";
|
|
4
|
+
import { buildReport } from "./report.js";
|
|
5
|
+
import { validateCatalog } from "../validate/ajv.js";
|
|
6
|
+
export function transform(doc, version, surface, profile = shadcnProfile) {
|
|
7
|
+
// Conformance: reject unknown dspack MAJOR version (per dspack §2).
|
|
8
|
+
const major = String(doc.dspack ?? "").split(".")[0];
|
|
9
|
+
if (major !== "0") {
|
|
10
|
+
throw new Error(`Unsupported dspack major version '${doc.dspack}'. This PoC targets dspack 0.x.`);
|
|
11
|
+
}
|
|
12
|
+
const mapping = mapDspack(doc, profile);
|
|
13
|
+
const catalog = emitCatalog(mapping, version, profile);
|
|
14
|
+
const validation = validateCatalog(catalog, version, surface);
|
|
15
|
+
const report = buildReport(version, mapping, validation);
|
|
16
|
+
return { catalog, mapping, validation, report };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convenience wrapper for in-process callers (tests, the ingestion acceptance gate):
|
|
20
|
+
* accepts a raw dspack object and options, defaults the surface, and returns the same
|
|
21
|
+
* `TransformOutput`. This is the thin overload the Phase 2 plan's precondition asks for;
|
|
22
|
+
* the underlying pipeline is unchanged.
|
|
23
|
+
*/
|
|
24
|
+
export function transformFromJson(dspackJson, options = {}) {
|
|
25
|
+
return transform(dspackJson, options.a2uiVersion ?? "0.9.1", options.surface ?? { messages: [] }, options.profile ?? shadcnProfile);
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self-contained `$defs` for the generated catalog.
|
|
3
|
+
*
|
|
4
|
+
* The official A2UI basic catalogs reference shared types via EXTERNAL `$ref`s
|
|
5
|
+
* into `common_types.json` (e.g.
|
|
6
|
+
* `https://a2ui.org/specification/v0_9/common_types.json#/$defs/DynamicString`).
|
|
7
|
+
* Our success criterion forbids external `$ref`s, so we mirror the exact shapes
|
|
8
|
+
* of the defs we use as INTERNAL `#/$defs/...` entries. Internal refs resolve
|
|
9
|
+
* within the document, so the emitted catalog validates standalone.
|
|
10
|
+
*
|
|
11
|
+
* Two deliberate simplifications versus the upstream defs, noted in MAPPING.md:
|
|
12
|
+
* - `FunctionCall` drops the upstream `oneOf: [{ $ref: "catalog.json#/$defs/anyFunction" }]`
|
|
13
|
+
* constraint (that ref is external and couples to a function catalog we do not emit).
|
|
14
|
+
* - We only mirror the defs actually reachable from the components we emit.
|
|
15
|
+
*
|
|
16
|
+
* Shapes copied verbatim from the checked-in fixtures
|
|
17
|
+
* (fixtures/a2ui/common_types.v0_9_1.json).
|
|
18
|
+
*/
|
|
19
|
+
import type { Json } from "../types.js";
|
|
20
|
+
export declare const INLINED_DEFS: Record<string, Json>;
|
|
21
|
+
/** Names of the inlined helper defs (everything that is not a component or anyComponent). */
|
|
22
|
+
export declare const INLINED_DEF_NAMES: string[];
|