@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
package/src/types.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal types for the dspack input and the A2UI catalog output.
|
|
3
|
+
* Only the fields the transformer reads/writes are modeled; dspack documents
|
|
4
|
+
* may carry more (we ignore unknown properties per dspack conformance rules).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// dspack (v0.2) — subset we consume
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
export interface DspackDoc {
|
|
12
|
+
dspack: string;
|
|
13
|
+
name: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
version?: string;
|
|
16
|
+
metadata?: Record<string, unknown>;
|
|
17
|
+
tokens?: Record<string, DspackTokenCategory>;
|
|
18
|
+
components?: Record<string, DspackComponent>;
|
|
19
|
+
patterns?: unknown[];
|
|
20
|
+
antiPatterns?: unknown[];
|
|
21
|
+
frameworkBindings?: Record<string, unknown>;
|
|
22
|
+
themes?: Record<string, unknown>;
|
|
23
|
+
layout?: Record<string, unknown>;
|
|
24
|
+
[k: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DspackTokenCategory {
|
|
28
|
+
description?: string;
|
|
29
|
+
tier?: string;
|
|
30
|
+
values: Record<string, DspackToken>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface DspackToken {
|
|
34
|
+
value: string;
|
|
35
|
+
description?: string;
|
|
36
|
+
type?: string;
|
|
37
|
+
[k: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DspackComponent {
|
|
41
|
+
name: string;
|
|
42
|
+
description: string;
|
|
43
|
+
status?: unknown;
|
|
44
|
+
whenToUse?: string;
|
|
45
|
+
whenNotToUse?: string;
|
|
46
|
+
props?: Record<string, DspackProp>;
|
|
47
|
+
tokens?: string[];
|
|
48
|
+
tags?: string[];
|
|
49
|
+
accessibility?: unknown;
|
|
50
|
+
composition?: unknown;
|
|
51
|
+
constraints?: unknown[];
|
|
52
|
+
relatedComponents?: string[];
|
|
53
|
+
[k: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface DspackProp {
|
|
57
|
+
type: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
values?: Array<string | number | boolean | { value: unknown; description?: string; deprecated?: boolean }>;
|
|
60
|
+
default?: unknown;
|
|
61
|
+
required?: boolean;
|
|
62
|
+
propRole?: string;
|
|
63
|
+
[k: string]: unknown;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// dspack surface (v0.1) — the protocol-neutral component tree this repo's
|
|
68
|
+
// surface emitter compiles to A2UI messages. Schema:
|
|
69
|
+
// dspack repo, schema/dspack.surface.v0_1.schema.json.
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
export interface DspackSurface {
|
|
73
|
+
dspackSurface: string;
|
|
74
|
+
system: string;
|
|
75
|
+
intent: string;
|
|
76
|
+
root: SurfaceNode;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface SurfaceNode {
|
|
80
|
+
component: string;
|
|
81
|
+
id?: string;
|
|
82
|
+
props?: Record<string, unknown>;
|
|
83
|
+
text?: string;
|
|
84
|
+
children?: SurfaceNode[];
|
|
85
|
+
slots?: Record<string, SurfaceNode[]>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// A2UI catalog — JSON-Schema-shaped output (kept loose: it *is* a JSON Schema)
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
export type Json = Record<string, unknown>;
|
|
93
|
+
|
|
94
|
+
export interface A2uiCatalog extends Json {
|
|
95
|
+
$schema: string;
|
|
96
|
+
$id: string;
|
|
97
|
+
title: string;
|
|
98
|
+
description: string;
|
|
99
|
+
catalogId: string;
|
|
100
|
+
instructions?: string;
|
|
101
|
+
components: Record<string, Json>;
|
|
102
|
+
functions?: Record<string, Json>;
|
|
103
|
+
$defs: Record<string, Json>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type A2uiVersion = "0.9.1" | "1.0";
|
|
107
|
+
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// Fidelity reporting
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
export type FidelityClass =
|
|
113
|
+
| "maps-cleanly"
|
|
114
|
+
| "synthesis-defaults"
|
|
115
|
+
| "lossy"
|
|
116
|
+
| "cannot-represent";
|
|
117
|
+
|
|
118
|
+
export interface FidelityEntry {
|
|
119
|
+
source: string; // dspack construct, e.g. "components.button.props.variant"
|
|
120
|
+
target: string; // A2UI target, e.g. "components.Button.properties.variant"
|
|
121
|
+
class: FidelityClass;
|
|
122
|
+
note: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface Warning {
|
|
126
|
+
code: string;
|
|
127
|
+
message: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
// Coverage: every input dspack component must be explicitly accounted for.
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
export type Disposition =
|
|
135
|
+
| "mapped" // emitted as an A2UI component derived from this dspack component
|
|
136
|
+
| "adapted" // emitted but lossy (folds onto another component)
|
|
137
|
+
| "omitted" // intentionally left out (declared in the profile)
|
|
138
|
+
| "unsupported" // cannot be represented (declared casualty)
|
|
139
|
+
| "unclassified"; // NOT accounted for anywhere — a silent drop, now surfaced
|
|
140
|
+
|
|
141
|
+
export interface CoverageEntry {
|
|
142
|
+
id: string; // dspack component id
|
|
143
|
+
disposition: Disposition;
|
|
144
|
+
detail?: string;
|
|
145
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A2UI catalog schema validation — three executable gates, mirroring the ajv
|
|
3
|
+
* approach A2UI uses in specification/scripts/validate.py
|
|
4
|
+
* (draft 2020-12, strict:false, ajv-formats):
|
|
5
|
+
*
|
|
6
|
+
* 1. schema-compile + no-external-ref: ajv compiles the catalog AS a JSON Schema.
|
|
7
|
+
* Compilation fails on any unresolved `$ref`, proving the catalog is a valid,
|
|
8
|
+
* fully self-contained schema with zero external references.
|
|
9
|
+
* 2. catalog-shape: the catalog validates against the version-specific
|
|
10
|
+
* a2ui-catalog.meta.<ver>.json (the literal "catalog schema" check; this is what
|
|
11
|
+
* makes v0.9.1 vs v1.0 conformance distinct — theme vs surfaceProperties).
|
|
12
|
+
* 3. instance: every component instance in the hand-authored surface validates
|
|
13
|
+
* against the catalog's own #/$defs/anyComponent.
|
|
14
|
+
*/
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
18
|
+
import addFormats from "ajv-formats";
|
|
19
|
+
import type { A2uiCatalog, A2uiVersion, Json } from "../types.js";
|
|
20
|
+
|
|
21
|
+
export interface GateResult {
|
|
22
|
+
name: string;
|
|
23
|
+
pass: boolean;
|
|
24
|
+
detail: string;
|
|
25
|
+
errors?: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ValidationReport {
|
|
29
|
+
version: A2uiVersion;
|
|
30
|
+
pass: boolean;
|
|
31
|
+
gates: GateResult[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const metaPath = (ver: A2uiVersion): string =>
|
|
35
|
+
fileURLToPath(new URL(`./meta/a2ui-catalog.meta.${ver === "0.9.1" ? "0_9_1" : "1_0"}.json`, import.meta.url));
|
|
36
|
+
|
|
37
|
+
function newAjv(): Ajv2020 {
|
|
38
|
+
const ajv = new Ajv2020({ strict: false, allErrors: true, validateFormats: true });
|
|
39
|
+
addFormats(ajv);
|
|
40
|
+
return ajv;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Collect every external `$ref` (a $ref whose value does not start with '#'). */
|
|
44
|
+
function externalRefs(node: unknown, acc: string[] = []): string[] {
|
|
45
|
+
if (Array.isArray(node)) {
|
|
46
|
+
for (const n of node) externalRefs(n, acc);
|
|
47
|
+
} else if (node && typeof node === "object") {
|
|
48
|
+
for (const [k, v] of Object.entries(node)) {
|
|
49
|
+
if (k === "$ref" && typeof v === "string" && !v.startsWith("#")) acc.push(v);
|
|
50
|
+
else externalRefs(v, acc);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return acc;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Pull every component instance (object with string `component` + `id`) from a surface. */
|
|
57
|
+
export function extractInstances(surface: unknown, acc: Json[] = []): Json[] {
|
|
58
|
+
if (Array.isArray(surface)) {
|
|
59
|
+
for (const n of surface) extractInstances(n, acc);
|
|
60
|
+
} else if (surface && typeof surface === "object") {
|
|
61
|
+
const o = surface as Json;
|
|
62
|
+
if (typeof o.component === "string" && typeof o.id === "string") acc.push(o);
|
|
63
|
+
for (const v of Object.values(o)) extractInstances(v, acc);
|
|
64
|
+
}
|
|
65
|
+
return acc;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function validateCatalog(
|
|
69
|
+
catalog: A2uiCatalog,
|
|
70
|
+
version: A2uiVersion,
|
|
71
|
+
surface?: unknown,
|
|
72
|
+
): ValidationReport {
|
|
73
|
+
const gates: GateResult[] = [];
|
|
74
|
+
|
|
75
|
+
// Gate 1 — schema compile + no external $ref.
|
|
76
|
+
const exts = externalRefs(catalog);
|
|
77
|
+
let compiled = false;
|
|
78
|
+
let compileErr = "";
|
|
79
|
+
try {
|
|
80
|
+
newAjv().compile(catalog as unknown as Json);
|
|
81
|
+
compiled = true;
|
|
82
|
+
} catch (e) {
|
|
83
|
+
compileErr = e instanceof Error ? e.message : String(e);
|
|
84
|
+
}
|
|
85
|
+
gates.push({
|
|
86
|
+
name: "schema-compile + no-external-ref",
|
|
87
|
+
pass: compiled && exts.length === 0,
|
|
88
|
+
detail:
|
|
89
|
+
compiled && exts.length === 0
|
|
90
|
+
? "Catalog compiles as a draft-2020-12 JSON Schema with only internal $refs."
|
|
91
|
+
: !compiled
|
|
92
|
+
? `ajv failed to compile the catalog as a schema: ${compileErr}`
|
|
93
|
+
: `Catalog contains external $refs: ${[...new Set(exts)].join(", ")}`,
|
|
94
|
+
errors: exts.length ? [...new Set(exts)] : undefined,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Gate 2 — catalog shape.
|
|
98
|
+
const meta = JSON.parse(readFileSync(metaPath(version), "utf8")) as Json;
|
|
99
|
+
const validateShape = newAjv().compile(meta);
|
|
100
|
+
const shapeOk = validateShape(catalog) as boolean;
|
|
101
|
+
gates.push({
|
|
102
|
+
name: "catalog-shape",
|
|
103
|
+
pass: shapeOk,
|
|
104
|
+
detail: shapeOk
|
|
105
|
+
? `Catalog satisfies the A2UI v${version} catalog-shape meta-schema.`
|
|
106
|
+
: `Catalog violates the v${version} catalog-shape meta-schema.`,
|
|
107
|
+
errors: shapeOk ? undefined : (validateShape.errors ?? []).map(fmtErr),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Gate 3 — instances (only if compilable and a surface was supplied).
|
|
111
|
+
if (surface !== undefined) {
|
|
112
|
+
if (!compiled) {
|
|
113
|
+
gates.push({
|
|
114
|
+
name: "instance",
|
|
115
|
+
pass: false,
|
|
116
|
+
detail: "Skipped: catalog did not compile, so instances cannot be checked.",
|
|
117
|
+
});
|
|
118
|
+
} else {
|
|
119
|
+
const ajv = newAjv();
|
|
120
|
+
ajv.addSchema(catalog as unknown as Json, catalog.$id);
|
|
121
|
+
const validateAny = ajv.getSchema(`${catalog.$id}#/$defs/anyComponent`);
|
|
122
|
+
const instances = extractInstances(surface);
|
|
123
|
+
const failures: string[] = [];
|
|
124
|
+
if (!validateAny) {
|
|
125
|
+
failures.push("Could not resolve #/$defs/anyComponent from the catalog.");
|
|
126
|
+
} else {
|
|
127
|
+
for (const inst of instances) {
|
|
128
|
+
if (!validateAny(inst)) {
|
|
129
|
+
const where = `${inst.component}#${inst.id}`;
|
|
130
|
+
for (const e of validateAny.errors ?? []) failures.push(`${where}: ${fmtErr(e)}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
gates.push({
|
|
135
|
+
name: "instance",
|
|
136
|
+
pass: failures.length === 0,
|
|
137
|
+
detail:
|
|
138
|
+
failures.length === 0
|
|
139
|
+
? `All ${instances.length} surface component instance(s) validate against #/$defs/anyComponent.`
|
|
140
|
+
: `${failures.length} instance validation error(s).`,
|
|
141
|
+
errors: failures.length ? failures : undefined,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { version, pass: gates.every((g) => g.pass), gates };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function fmtErr(e: { instancePath?: string; message?: string }): string {
|
|
150
|
+
return `${e.instancePath || "(root)"} ${e.message ?? ""}`.trim();
|
|
151
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://rdombrowski.dev/meta/a2ui-catalog.v0_9_1.json",
|
|
4
|
+
"title": "A2UI catalog shape (v0.9.1)",
|
|
5
|
+
"description": "Structural invariants an A2UI v0.9.1 catalog must satisfy, derived from the checked-in basic catalog fixture. The version-distinguishing requirement is $defs.theme (with primaryColor).",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["$schema", "$id", "catalogId", "title", "description", "components", "$defs"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"catalogId": { "type": "string", "format": "uri" },
|
|
10
|
+
"$id": { "type": "string", "format": "uri" },
|
|
11
|
+
"components": {
|
|
12
|
+
"type": "object",
|
|
13
|
+
"minProperties": 1,
|
|
14
|
+
"additionalProperties": {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"required": ["allOf", "unevaluatedProperties"],
|
|
17
|
+
"properties": {
|
|
18
|
+
"allOf": { "type": "array", "minItems": 1 },
|
|
19
|
+
"unevaluatedProperties": { "const": false }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"$defs": {
|
|
24
|
+
"type": "object",
|
|
25
|
+
"required": ["anyComponent", "theme"],
|
|
26
|
+
"not": { "required": ["surfaceProperties"] },
|
|
27
|
+
"properties": {
|
|
28
|
+
"anyComponent": { "type": "object", "required": ["oneOf"] },
|
|
29
|
+
"theme": {
|
|
30
|
+
"type": "object",
|
|
31
|
+
"required": ["properties"],
|
|
32
|
+
"properties": {
|
|
33
|
+
"properties": { "type": "object", "required": ["primaryColor"] }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://rdombrowski.dev/meta/a2ui-catalog.v1_0.json",
|
|
4
|
+
"title": "A2UI catalog shape (v1.0)",
|
|
5
|
+
"description": "Structural invariants an A2UI v1.0 catalog must satisfy, derived from the checked-in basic catalog fixture. The version-distinguishing requirement is $defs.surfaceProperties (and the absence of $defs.theme).",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["$schema", "$id", "catalogId", "title", "description", "components", "$defs"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"catalogId": { "type": "string", "format": "uri" },
|
|
10
|
+
"$id": { "type": "string", "format": "uri" },
|
|
11
|
+
"instructions": { "type": "string" },
|
|
12
|
+
"components": {
|
|
13
|
+
"type": "object",
|
|
14
|
+
"minProperties": 1,
|
|
15
|
+
"additionalProperties": {
|
|
16
|
+
"type": "object",
|
|
17
|
+
"required": ["allOf", "unevaluatedProperties"],
|
|
18
|
+
"properties": {
|
|
19
|
+
"allOf": { "type": "array", "minItems": 1 },
|
|
20
|
+
"unevaluatedProperties": { "const": false }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"$defs": {
|
|
25
|
+
"type": "object",
|
|
26
|
+
"required": ["anyComponent", "surfaceProperties"],
|
|
27
|
+
"not": { "required": ["theme"] },
|
|
28
|
+
"properties": {
|
|
29
|
+
"anyComponent": { "type": "object", "required": ["oneOf"] },
|
|
30
|
+
"surfaceProperties": { "type": "object", "required": ["properties"] }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|