@nanobpm/urban 0.17.0 → 0.17.1

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,69 @@
1
+ import type { DerivedArtifact, Deriver } from "../artifact.ts";
2
+ import type { ColumnMeta, TableMeta } from "../../runtime/core/modules/gateway.ts";
3
+ import { type ToolkitManifest } from "./migrations.ts";
4
+ export type { ColumnMeta, TableMeta };
5
+ /** One datasource's tables, paired with its manifest alias. */
6
+ export interface SourceSchema {
7
+ source: string;
8
+ tables: TableMeta[];
9
+ }
10
+ /**
11
+ * Map a SQLite declared column type to a TypeScript field type. SQLite is dynamically typed but
12
+ * assigns each column a *type affinity* from its declared name, so we apply the affinity rules
13
+ * (https://sqlite.org/datatype3.html §3.1) with two maker-friendly overrides: `BOOLEAN` →
14
+ * `boolean` and date/time types → `string` (ISO text), matching how Urban surfaces them.
15
+ */
16
+ export declare function sqliteAffinityToTs(declaredType: string): string;
17
+ /** PascalCase a table name into an interface name, sanitised to an identifier. */
18
+ export declare function interfaceName(table: string): string;
19
+ /**
20
+ * Emit the full `domain-rows.d.ts` from a datasource's tables: one `interface` per table plus a
21
+ * `DomainTables` lookup keyed by the *raw* table name, so callers write `DomainTables["customers"]`.
22
+ */
23
+ export declare function emitDomainDts(tables: TableMeta[]): string;
24
+ /**
25
+ * Emit `domain-rows.d.ts` for an App that declares *multiple* datasources: one `interface` per
26
+ * (source, table), a `DomainSources` map keyed by alias then wire table name, and a `DomainTables`
27
+ * alias to the default source. When there is one source (or none) this is byte-identical to
28
+ * `emitDomainDts` so the common case stays stable.
29
+ */
30
+ export declare function emitDomainDtsForSources(sources: SourceSchema[], defaultSource?: string): string;
31
+ /** One field of a declared domain type in the manifest `types` registry. */
32
+ export interface DomainFieldDef {
33
+ /** A primitive keyword or the id of another registry type (nominal ref). */
34
+ type: string;
35
+ optional?: boolean;
36
+ list?: boolean;
37
+ }
38
+ /** A declared (transient) domain record type from the manifest `types` block. */
39
+ export interface DomainTypeDef {
40
+ name?: string;
41
+ match?: string;
42
+ table?: string;
43
+ fields: Record<string, DomainFieldDef>;
44
+ }
45
+ /** The manifest `types` registry: type id → declared record type. */
46
+ export type DomainTypeRegistry = Record<string, DomainTypeDef>;
47
+ /**
48
+ * Emit the `DomainTypes` block for the manifest `types` registry: an inline map keyed by type id.
49
+ * Returns `""` when the registry is empty, so the table spine stays byte-identical when no types
50
+ * are declared. `optional` widens the key with `?`.
51
+ */
52
+ export declare function emitDomainTypeRegistry(types: DomainTypeRegistry): string;
53
+ /**
54
+ * Compose the full `domain-rows.d.ts`: the datasource table spine (every source, ADR 0029 §6)
55
+ * followed by the manifest `types` registry (§4.2).
56
+ */
57
+ export declare function emitDomainModel(sources: SourceSchema[], defaultSource: string | undefined, types: DomainTypeRegistry): string;
58
+ /** The datasource table spine derived from the manifest types, grouped by source (same grouping
59
+ * and order as the SQL migration). */
60
+ export declare function sourcesFromManifest(manifest: ToolkitManifest): SourceSchema[];
61
+ /** The `DomainTypes` registry derived from the manifest types (declared fields only — the
62
+ * persisted `id` belongs to the table row, not the logical type). */
63
+ export declare function registryFromManifest(manifest: ToolkitManifest): DomainTypeRegistry;
64
+ /** Emit the `domain-rows.d.ts` content for an app manifest. */
65
+ export declare function emitDomain(manifest: ToolkitManifest): string;
66
+ /** Derive `nano-generated/domain-rows.d.ts` from the manifest types. Empty when the app declares
67
+ * no types (no tables, no registry, and nothing references `DomainTypes`). */
68
+ export declare function deriveDomain(manifest: ToolkitManifest): DerivedArtifact[];
69
+ export declare const domainDeriver: Deriver<ToolkitManifest>;
@@ -0,0 +1,243 @@
1
+ // Deriver: manifest `types` → `domain-rows.d.ts`, the domain row-type contract (ADR 0029 §4.1/§6,
2
+ // ADR 0053). Two blocks compose into one generated `.d.ts`:
3
+ // • the **table spine** — one `interface` per persisted table plus a `DomainTables` lookup
4
+ // keyed by wire name, so a worker types `DomainTables["greetings"]`; and
5
+ // • the **`DomainTypes` registry** — the declared (transient) manifest types keyed by id, so
6
+ // envelope/worker/message refs resolve to `DomainTypes["<id>"]` (see `typeRefFor`).
7
+ //
8
+ // Byte-faithful port of the console's `emitDomainModel`/`emitDomainDts…`/`emitDomainTypeRegistry`
9
+ // (server `domain_types.ts`), so the toolkit is a drop-in for the IDE's codegen. The emitters are
10
+ // pure; the table spine is derived from the manifest (code-first, ADR 0054) via the *same*
11
+ // canonical `tableSchemaForType` the SQL migration uses — one schema truth, no drift surface.
12
+ import { GENERATED_DIR } from "../artifact.js";
13
+ import { DOMAIN_DTS } from "./worker-io.js";
14
+ import { groupTypesBySource, tableSchemaForType, } from "./migrations.js";
15
+ // --- table spine emitters (port of domain_types.ts) ------------------------------------------
16
+ /**
17
+ * Map a SQLite declared column type to a TypeScript field type. SQLite is dynamically typed but
18
+ * assigns each column a *type affinity* from its declared name, so we apply the affinity rules
19
+ * (https://sqlite.org/datatype3.html §3.1) with two maker-friendly overrides: `BOOLEAN` →
20
+ * `boolean` and date/time types → `string` (ISO text), matching how Urban surfaces them.
21
+ */
22
+ export function sqliteAffinityToTs(declaredType) {
23
+ const t = declaredType.toUpperCase();
24
+ if (t.length === 0)
25
+ return "unknown"; // NONE affinity — opaque
26
+ if (t.includes("BOOL"))
27
+ return "boolean";
28
+ if (t.includes("DATE") || t.includes("TIME"))
29
+ return "string";
30
+ if (t.includes("INT"))
31
+ return "number";
32
+ if (t.includes("CHAR") || t.includes("CLOB") || t.includes("TEXT"))
33
+ return "string";
34
+ if (t.includes("BLOB"))
35
+ return "Uint8Array";
36
+ if (t.includes("REAL") || t.includes("FLOA") || t.includes("DOUB"))
37
+ return "number";
38
+ return "number"; // NUMERIC affinity
39
+ }
40
+ /** A TS identifier is safe to use bare as a property name / type name. */
41
+ function isIdent(name) {
42
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
43
+ }
44
+ /** PascalCase a table name into an interface name, sanitised to an identifier. */
45
+ export function interfaceName(table) {
46
+ const pascal = table
47
+ .replace(/[^A-Za-z0-9]+/g, " ")
48
+ .trim()
49
+ .split(/\s+/)
50
+ .map((w) => (w ? w[0].toUpperCase() + w.slice(1) : ""))
51
+ .join("");
52
+ const safe = pascal.replace(/[^A-Za-z0-9_$]/g, "");
53
+ return /^[A-Za-z_$]/.test(safe) ? safe : `T_${safe}`;
54
+ }
55
+ /** The TS field type for a column: nullable columns widen with `| null`. */
56
+ function fieldType(col) {
57
+ const base = sqliteAffinityToTs(col.type);
58
+ // A NOT NULL column (or the INTEGER PRIMARY KEY rowid alias) is never null.
59
+ return col.notNull || col.primaryKey ? base : `${base} | null`;
60
+ }
61
+ /** One `export interface` block for a table under an explicit interface name. */
62
+ function tableInterfaceNamed(name, t) {
63
+ const fields = t.columns
64
+ .map((c) => {
65
+ const key = isIdent(c.name) ? c.name : JSON.stringify(c.name);
66
+ const pk = c.primaryKey ? " (primary key)" : "";
67
+ return ` /** ${c.type || "?"}${pk} */\n ${key}: ${fieldType(c)};`;
68
+ })
69
+ .join("\n");
70
+ return `export interface ${name} {\n${fields}\n}`;
71
+ }
72
+ /** One `export interface` block for a table (name derived from the table). */
73
+ function tableInterface(t) {
74
+ return tableInterfaceNamed(interfaceName(t.name), t);
75
+ }
76
+ /**
77
+ * Emit the full `domain-rows.d.ts` from a datasource's tables: one `interface` per table plus a
78
+ * `DomainTables` lookup keyed by the *raw* table name, so callers write `DomainTables["customers"]`.
79
+ */
80
+ export function emitDomainDts(tables) {
81
+ const header = "// AUTO-GENERATED by nanobpmn from the datasource schema (ADR 0029 §4.1).\n" +
82
+ "// Do not edit — regenerated from the live tables. Erased at `deno compile`.\n" +
83
+ "// eslint-disable\n";
84
+ if (tables.length === 0) {
85
+ return `${header}\nexport interface DomainTables {}\n`;
86
+ }
87
+ const interfaces = tables.map(tableInterface).join("\n\n");
88
+ const map = tables
89
+ .map((t) => ` ${JSON.stringify(t.name)}: ${interfaceName(t.name)};`)
90
+ .join("\n");
91
+ return `${header}\n${interfaces}\n\n/** Every table keyed by its wire name — index it for a row type. */\nexport interface DomainTables {\n${map}\n}\n`;
92
+ }
93
+ /**
94
+ * The interface name for a table in a multi-source union: the source alias is PascalCased and
95
+ * prefixed to the table's interface name so tables that share a name across datasources get
96
+ * distinct, collision-free interfaces (`AppCustomers`, `AnalyticsCustomers`).
97
+ */
98
+ function prefixedInterfaceName(source, table) {
99
+ return `${interfaceName(source)}${interfaceName(table)}`;
100
+ }
101
+ /**
102
+ * Emit `domain-rows.d.ts` for an App that declares *multiple* datasources: one `interface` per
103
+ * (source, table), a `DomainSources` map keyed by alias then wire table name, and a `DomainTables`
104
+ * alias to the default source. When there is one source (or none) this is byte-identical to
105
+ * `emitDomainDts` so the common case stays stable.
106
+ */
107
+ export function emitDomainDtsForSources(sources, defaultSource) {
108
+ if (sources.length <= 1) {
109
+ return emitDomainDts(sources[0]?.tables ?? []);
110
+ }
111
+ const header = "// AUTO-GENERATED by nanobpmn from the datasource schemas (ADR 0029 §4.1/§6).\n" +
112
+ "// Do not edit — regenerated from the live tables. Erased at `deno compile`.\n" +
113
+ "// eslint-disable\n";
114
+ const interfaces = sources
115
+ .flatMap((s) => s.tables.map((t) => tableInterfaceNamed(prefixedInterfaceName(s.source, t.name), t)))
116
+ .join("\n\n");
117
+ const sourceMap = sources
118
+ .map((s) => {
119
+ if (s.tables.length === 0)
120
+ return ` ${JSON.stringify(s.source)}: {};`;
121
+ const tableMap = s.tables
122
+ .map((t) => ` ${JSON.stringify(t.name)}: ${prefixedInterfaceName(s.source, t.name)};`)
123
+ .join("\n");
124
+ return ` ${JSON.stringify(s.source)}: {\n${tableMap}\n };`;
125
+ })
126
+ .join("\n");
127
+ const def = defaultSource && sources.some((s) => s.source === defaultSource)
128
+ ? defaultSource
129
+ : sources[0].source;
130
+ return `${header}\n${interfaces}\n\n` +
131
+ `/** Every datasource keyed by alias, then table by wire name. */\n` +
132
+ `export interface DomainSources {\n${sourceMap}\n}\n\n` +
133
+ `/** The default datasource's tables — index by wire name. */\n` +
134
+ `export type DomainTables = DomainSources[${JSON.stringify(def)}];\n`;
135
+ }
136
+ /** Manifest primitive field keywords → TS types (ADR 0029 §4.2 / schema). */
137
+ const PRIMITIVE_TS = {
138
+ string: "string",
139
+ number: "number",
140
+ integer: "number",
141
+ boolean: "boolean",
142
+ date: "string",
143
+ datetime: "string",
144
+ json: "unknown",
145
+ };
146
+ /**
147
+ * The TS type for a registry field. A primitive keyword maps directly (and takes precedence over
148
+ * an identically named type, per the schema); anything else is a nominal reference to another
149
+ * registry type, emitted as an indexed access into `DomainTypes`. An unresolved reference degrades
150
+ * to `unknown`. `list` wraps the base type in `[]`.
151
+ */
152
+ function fieldTsType(field, ids) {
153
+ const base = PRIMITIVE_TS[field.type] ??
154
+ (ids.has(field.type) ? `DomainTypes[${JSON.stringify(field.type)}]` : "unknown");
155
+ return field.list ? `${base}[]` : base;
156
+ }
157
+ /**
158
+ * Emit the `DomainTypes` block for the manifest `types` registry: an inline map keyed by type id.
159
+ * Returns `""` when the registry is empty, so the table spine stays byte-identical when no types
160
+ * are declared. `optional` widens the key with `?`.
161
+ */
162
+ export function emitDomainTypeRegistry(types) {
163
+ const ids = new Set(Object.keys(types));
164
+ if (ids.size === 0)
165
+ return "";
166
+ const entries = Object.entries(types)
167
+ .map(([id, def]) => {
168
+ const fields = Object.entries(def.fields ?? {})
169
+ .map(([fname, field]) => {
170
+ const key = isIdent(fname) ? fname : JSON.stringify(fname);
171
+ const opt = field.optional ? "?" : "";
172
+ return ` ${key}${opt}: ${fieldTsType(field, ids)};`;
173
+ })
174
+ .join("\n");
175
+ const body = fields.length > 0 ? `{\n${fields}\n }` : "{}";
176
+ return ` ${JSON.stringify(id)}: ${body};`;
177
+ })
178
+ .join("\n");
179
+ return `/** Declared (transient) domain types from the manifest \`types\` registry (ADR 0029 §4.2), keyed by id. */\nexport interface DomainTypes {\n${entries}\n}\n`;
180
+ }
181
+ /**
182
+ * Compose the full `domain-rows.d.ts`: the datasource table spine (every source, ADR 0029 §6)
183
+ * followed by the manifest `types` registry (§4.2).
184
+ */
185
+ export function emitDomainModel(sources, defaultSource, types) {
186
+ const spine = emitDomainDtsForSources(sources, defaultSource);
187
+ const registry = emitDomainTypeRegistry(types);
188
+ return registry ? `${spine}\n${registry}` : spine;
189
+ }
190
+ // --- manifest bridge (code-first, ADR 0054) --------------------------------------------------
191
+ /** Project one manifest type's canonical schema into a `TableMeta` for the row-type spine. Reuses
192
+ * the *same* `tableSchemaForType` the SQL migration uses, so the row types can't drift from the DB. */
193
+ function tableMetaFromType(typeName, def) {
194
+ const { table, columns } = tableSchemaForType(typeName, def);
195
+ return {
196
+ name: table,
197
+ columns: columns.map((c) => ({
198
+ name: c.name,
199
+ type: c.sqlType,
200
+ notNull: c.notNull,
201
+ primaryKey: c.primaryKey,
202
+ })),
203
+ indexes: [],
204
+ foreignKeys: [],
205
+ };
206
+ }
207
+ /** The datasource table spine derived from the manifest types, grouped by source (same grouping
208
+ * and order as the SQL migration). */
209
+ export function sourcesFromManifest(manifest) {
210
+ return [...groupTypesBySource(manifest)].map(([source, list]) => ({
211
+ source,
212
+ tables: list.map(([name, def]) => tableMetaFromType(name, def)),
213
+ }));
214
+ }
215
+ /** The `DomainTypes` registry derived from the manifest types (declared fields only — the
216
+ * persisted `id` belongs to the table row, not the logical type). */
217
+ export function registryFromManifest(manifest) {
218
+ const out = {};
219
+ for (const [id, def] of Object.entries(manifest.types ?? {})) {
220
+ const fields = {};
221
+ for (const [fname, f] of Object.entries(def.fields ?? {})) {
222
+ fields[fname] = { type: f.type ?? "string", optional: f.optional, list: f.list };
223
+ }
224
+ out[id] = { table: def.table, fields };
225
+ }
226
+ return out;
227
+ }
228
+ /** Emit the `domain-rows.d.ts` content for an app manifest. */
229
+ export function emitDomain(manifest) {
230
+ return emitDomainModel(sourcesFromManifest(manifest), manifest.data?.default ?? "app", registryFromManifest(manifest));
231
+ }
232
+ /** Derive `nano-generated/domain-rows.d.ts` from the manifest types. Empty when the app declares
233
+ * no types (no tables, no registry, and nothing references `DomainTypes`). */
234
+ export function deriveDomain(manifest) {
235
+ if (!manifest.types || Object.keys(manifest.types).length === 0)
236
+ return [];
237
+ return [{ path: `${GENERATED_DIR}/${DOMAIN_DTS}`, content: emitDomain(manifest) }];
238
+ }
239
+ export const domainDeriver = {
240
+ id: "types->domain",
241
+ describe: "Derive domain row types (DomainTables spine + DomainTypes registry) from manifest types.",
242
+ derive: deriveDomain,
243
+ };
@@ -0,0 +1,36 @@
1
+ import type { DerivedArtifact, Deriver } from "../artifact.ts";
2
+ import { type ModelSource } from "./worker-io.ts";
3
+ /** Filename the console uses for the derived message-payload map; kept identical (drop-in). */
4
+ export declare const MESSAGE_BINDINGS_DTS = "message-io.d.ts";
5
+ /** One message-IO entry scanned from a model: a `bpmn:message` name plus the envelope
6
+ * type refs it carries. `inputType` is the received payload (what a `publishMessage`
7
+ * caller sends); `outputType` is reserved. Mirrors the Rust `MessageIo`. */
8
+ export interface MessageIo {
9
+ messageName: string;
10
+ inputType?: string;
11
+ outputType?: string;
12
+ }
13
+ /** The binding shape the emitter reads. Identical to a scanned `MessageIo`, so it is an
14
+ * alias — one canonical shape, no second declaration to drift (the console keeps the two
15
+ * apart only because its scan output is Rust and its emitter input is TS). */
16
+ export type MessageBindingDecl = MessageIo;
17
+ /** Scan one BPMN document for `bpmn:message` envelopes. Handles both a self-closing
18
+ * `<bpmn:message name=.. />` (name only — completes the `MessageName` union) and an open
19
+ * message whose body carries the `io.nanobpm.dataEnvelope.in/out` `zeebe:property`. */
20
+ export declare function scanModelMessages(xml: string): MessageIo[];
21
+ /** Emit `message-io.d.ts` from message bindings + the set of declared domain type ids.
22
+ * Each named message joins the `MessageName` union; a message whose envelope names a
23
+ * declared type also maps to it in `MessagePayloads` (else falls back to `MessageVars`). */
24
+ export declare function emitMessageBindings(messages: MessageBindingDecl[], declaredTypeIds: Iterable<string>): string;
25
+ /**
26
+ * Derive `nano-generated/message-io.d.ts` from BPMN models. Messages are aggregated the way
27
+ * the console's Rust scan does (a `BTreeMap` keyed by name): deduped by name, sorted by name,
28
+ * with the envelope type refs merged first-wins across models — so the derived output matches
29
+ * the host's `derivedMessages` for the same models. `declaredTypeIds` is the manifest `types`
30
+ * key set; a message only gets a typed payload when its envelope names a declared type.
31
+ */
32
+ export declare function deriveMessageBindings(models: ModelSource[], declaredTypeIds?: Iterable<string>): DerivedArtifact[];
33
+ export declare const messagesDeriver: Deriver<{
34
+ models: ModelSource[];
35
+ declaredTypeIds: Iterable<string>;
36
+ }>;
@@ -0,0 +1,134 @@
1
+ // Deriver: BPMN models → `message-io.d.ts`, the message-payload type map (ADR 0040 slice 2).
2
+ // Scans each process for `bpmn:message` envelopes and maps each message name to its declared
3
+ // payload type. It emits the SAME `message-io.d.ts` the console generates (a faithful port of
4
+ // `emitMessageBindings` in the server's domain_types.ts), so the toolkit is a drop-in for the
5
+ // IDE's codegen (ADR 0053). The static typed-`publishMessage` wrapper (`messages.ts`) never
6
+ // changes with the model, so — like the worker SDK — it is a package/runtime concern, not a
7
+ // derived artifact; only the model-derived `message-io.d.ts` is emitted here.
8
+ import { GENERATED_DIR } from "../artifact.js";
9
+ import { byModelPath, DOMAIN_DTS, typeRefFor } from "./worker-io.js";
10
+ /** Filename the console uses for the derived message-payload map; kept identical (drop-in). */
11
+ export const MESSAGE_BINDINGS_DTS = "message-io.d.ts";
12
+ const ENVELOPE_IN = "io.nanobpm.dataEnvelope.in";
13
+ const ENVELOPE_OUT = "io.nanobpm.dataEnvelope.out";
14
+ function attr(tag, name) {
15
+ const m = tag.match(new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`));
16
+ return m ? m[1] : undefined;
17
+ }
18
+ /** An envelope ref is trimmed; an empty/whitespace value contributes nothing (mirrors the
19
+ * Rust `envelope_ref`). A message name, by contrast, is a correlation identity matched
20
+ * verbatim by the engine, so it is NOT trimmed — only an absent/empty name is dropped. */
21
+ function envelopeRef(value) {
22
+ const v = value?.trim();
23
+ return v ? v : undefined;
24
+ }
25
+ /** Scan one BPMN document for `bpmn:message` envelopes. Handles both a self-closing
26
+ * `<bpmn:message name=.. />` (name only — completes the `MessageName` union) and an open
27
+ * message whose body carries the `io.nanobpm.dataEnvelope.in/out` `zeebe:property`. */
28
+ export function scanModelMessages(xml) {
29
+ const out = [];
30
+ const blockRe = /<bpmn:message\b([^>]*?)(?:\/>|>([\s\S]*?)<\/bpmn:message>)/g;
31
+ let m;
32
+ while ((m = blockRe.exec(xml)) !== null) {
33
+ const name = attr(`<x ${m[1]}>`, "name");
34
+ if (name == null || name.length === 0)
35
+ continue;
36
+ const io = { messageName: name };
37
+ const body = m[2] ?? "";
38
+ const propRe = /<zeebe:property\b[^>]*>/g;
39
+ let p;
40
+ while ((p = propRe.exec(body)) !== null) {
41
+ const pname = attr(p[0], "name");
42
+ if (pname === ENVELOPE_IN)
43
+ io.inputType = envelopeRef(attr(p[0], "value"));
44
+ else if (pname === ENVELOPE_OUT)
45
+ io.outputType = envelopeRef(attr(p[0], "value"));
46
+ }
47
+ out.push(io);
48
+ }
49
+ return out;
50
+ }
51
+ // --- Faithful port of the console's emitMessageBindings (domain_types.ts) so the emitted
52
+ // message-io.d.ts is byte-compatible with the IDE's own codegen. ---
53
+ /** Emit `message-io.d.ts` from message bindings + the set of declared domain type ids.
54
+ * Each named message joins the `MessageName` union; a message whose envelope names a
55
+ * declared type also maps to it in `MessagePayloads` (else falls back to `MessageVars`). */
56
+ export function emitMessageBindings(messages, declaredTypeIds) {
57
+ const declared = new Set(declaredTypeIds);
58
+ const propKey = (m) => JSON.stringify(m);
59
+ const names = [
60
+ ...new Set(messages
61
+ .map((m) => m?.messageName)
62
+ .filter((n) => typeof n === "string" && n.length > 0)),
63
+ ];
64
+ const nameUnion = names.length > 0
65
+ ? names.map((n) => JSON.stringify(n)).join(" | ")
66
+ : "string";
67
+ const payloads = [];
68
+ for (const m of messages) {
69
+ if (typeof m?.messageName !== "string" || m.messageName.length === 0)
70
+ continue;
71
+ const ref = typeRefFor(m.inputType, declared);
72
+ if (ref)
73
+ payloads.push(` ${propKey(m.messageName)}: ${ref};`);
74
+ }
75
+ const header = "// AUTO-GENERATED by nanobpmn from the process model (ADR 0040 slice 2).\n" +
76
+ "// The bridge from a `bpmn:message`'s data envelope to the message type system:\n" +
77
+ "// each named message maps to the TS type of its payload, so the typed\n" +
78
+ "// `publishMessage` types a call by its message name. Do not edit — regenerated\n" +
79
+ "// from the model. Erased to plain JS at compile.\n" +
80
+ "// eslint-disable\n";
81
+ const needsRegistry = payloads.length > 0;
82
+ const importTypes = needsRegistry
83
+ ? `import type { DomainTypes } from "./${DOMAIN_DTS}";\n`
84
+ : "";
85
+ const payloadsIface = payloads.length > 0
86
+ ? `export interface MessagePayloads {\n${payloads.join("\n")}\n}\n`
87
+ : `export interface MessagePayloads {}\n`;
88
+ return `${header}\n` +
89
+ importTypes +
90
+ `\n/** Untyped fallback payload for a message that declares no envelope type. */\n` +
91
+ `export type MessageVars = Record<string, unknown>;\n\n` +
92
+ `/** Every declared message \`name\` (ADR 0040 slice 2): the model-derived set the\n` +
93
+ ` * typed \`publishMessage\` accepts, so \`name\` autocompletes and rejects unknown messages. */\n` +
94
+ `export type MessageName = ${nameUnion};\n\n` +
95
+ `/** Payload (\`variables\`) per declared message, keyed by \`name\`. */\n` +
96
+ payloadsIface;
97
+ }
98
+ /**
99
+ * Derive `nano-generated/message-io.d.ts` from BPMN models. Messages are aggregated the way
100
+ * the console's Rust scan does (a `BTreeMap` keyed by name): deduped by name, sorted by name,
101
+ * with the envelope type refs merged first-wins across models — so the derived output matches
102
+ * the host's `derivedMessages` for the same models. `declaredTypeIds` is the manifest `types`
103
+ * key set; a message only gets a typed payload when its envelope names a declared type.
104
+ */
105
+ export function deriveMessageBindings(models, declaredTypeIds = []) {
106
+ const byName = new Map();
107
+ for (const model of [...models].sort(byModelPath)) {
108
+ for (const msg of scanModelMessages(model.xml)) {
109
+ const existing = byName.get(msg.messageName);
110
+ if (!existing) {
111
+ byName.set(msg.messageName, {
112
+ messageName: msg.messageName,
113
+ inputType: msg.inputType,
114
+ outputType: msg.outputType,
115
+ });
116
+ }
117
+ else {
118
+ // First-wins merge of the envelope refs (mirrors the Rust `fill`).
119
+ if (existing.inputType == null)
120
+ existing.inputType = msg.inputType;
121
+ if (existing.outputType == null)
122
+ existing.outputType = msg.outputType;
123
+ }
124
+ }
125
+ }
126
+ const messages = [...byName.values()].sort((a, b) => a.messageName < b.messageName ? -1 : a.messageName > b.messageName ? 1 : 0);
127
+ const content = emitMessageBindings(messages, declaredTypeIds);
128
+ return [{ path: `${GENERATED_DIR}/${MESSAGE_BINDINGS_DTS}`, content }];
129
+ }
130
+ export const messagesDeriver = {
131
+ id: "model->message-io",
132
+ describe: "Derive message-io.d.ts (bpmn:message name + envelope payload) from BPMN models.",
133
+ derive: ({ models, declaredTypeIds }) => deriveMessageBindings(models, declaredTypeIds),
134
+ };
@@ -0,0 +1,34 @@
1
+ import type { DerivedArtifact, Deriver } from "../artifact.ts";
2
+ import { type ModelSource } from "./worker-io.ts";
3
+ /** Basename the console uses for the generated accessor; kept identical (drop-in). */
4
+ export declare const META_TS = "meta.ts";
5
+ /** One model-level metadata entry (`nano:meta`), tagged with its defining process for
6
+ * the `model:<processId>` provenance (ADR 0040 §5). `process` is carried for the fuse;
7
+ * the emitted `meta.ts` folds on key/value only. */
8
+ export interface MetaDecl {
9
+ process?: string;
10
+ key: string;
11
+ value: string;
12
+ }
13
+ /** Scan one BPMN document for model-level `nano:meta` key/value entries, in document
14
+ * order, tagged with the enclosing process. A meta with no `key` attribute is ignored;
15
+ * a missing `value` folds to the empty string (matches envelope_scan.rs). */
16
+ export declare function scanModelMeta(xml: string): MetaDecl[];
17
+ /** Fold the scanned model-level metadata into a single key→value record. When two models
18
+ * declare the same key the last (scan order) wins. Empty keys are ignored. The accumulator
19
+ * is a **null-prototype** dict so user-authored keys (including `__proto__`) round-trip
20
+ * deterministically as own data properties. */
21
+ export declare function foldMeta(metas: MetaDecl[]): Record<string, string>;
22
+ /** Emit `meta.ts`: the typed model-metadata accessor (`@nanobpm/meta`, ADR 0040 §5).
23
+ * `AppMeta` keys the declared metadata as string-literal properties; `appMeta` is the
24
+ * concrete null-prototype record; the overloaded `meta()` returns the typed value for a
25
+ * known key and `string | undefined` for a dynamic one. Strip-safe (ADR 0036). */
26
+ export declare function emitMeta(metas: MetaDecl[]): string;
27
+ /**
28
+ * Derive `nano-generated/meta.ts` from BPMN models. Deterministic: models are scanned in
29
+ * path order, then their `nano:meta` entries folded (last write wins) into the accessor.
30
+ */
31
+ export declare function deriveMeta(models: ModelSource[]): DerivedArtifact[];
32
+ export declare const metaDeriver: Deriver<{
33
+ models: ModelSource[];
34
+ }>;
@@ -0,0 +1,106 @@
1
+ // Deriver: BPMN models → `meta.ts`, the typed model-metadata accessor (`@nanobpm/meta`,
2
+ // ADR 0040 §5). Scans each process for `nano:meta` key/value siblings of the `nano:shapes`
3
+ // container and folds them into a single app-wide record. It emits the SAME `meta.ts` the
4
+ // console generates (a faithful port of `foldMeta`/`emitMeta` in the server's domain_types.ts),
5
+ // so the toolkit is a drop-in for the IDE's codegen (ADR 0053).
6
+ import { GENERATED_DIR } from "../artifact.js";
7
+ import { byModelPath } from "./worker-io.js";
8
+ /** Basename the console uses for the generated accessor; kept identical (drop-in). */
9
+ export const META_TS = "meta.ts";
10
+ function attr(tag, name) {
11
+ const m = tag.match(new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`));
12
+ return m ? m[1] : undefined;
13
+ }
14
+ function processId(xml) {
15
+ const m = xml.match(/<[\w.-]*:?process\b[^>]*>/);
16
+ return m ? attr(m[0], "id") : undefined;
17
+ }
18
+ // A `nano:shape` carrier (`<...:shape>…</...:shape>`, and self-closing) — meta is only
19
+ // recognised *outside* a shape carrier (its op children, e.g. `extend`, are a different
20
+ // vocabulary), so strip carriers before matching `nano:meta` (mirrors envelope_scan.rs).
21
+ const SHAPE_BLOCK_RE = /<([\w.-]*:)?shape\b[^>]*(?:\/>|>[\s\S]*?<\/\1?shape\s*>)/g;
22
+ // A `nano:meta` element (any namespace prefix; matches the Rust local-name scan).
23
+ const META_RE = /<([\w.-]*:)?meta\b([^>]*?)\/?>/g;
24
+ /** Scan one BPMN document for model-level `nano:meta` key/value entries, in document
25
+ * order, tagged with the enclosing process. A meta with no `key` attribute is ignored;
26
+ * a missing `value` folds to the empty string (matches envelope_scan.rs). */
27
+ export function scanModelMeta(xml) {
28
+ const proc = processId(xml);
29
+ const withoutShapes = xml.replace(SHAPE_BLOCK_RE, "");
30
+ const out = [];
31
+ let m;
32
+ while ((m = META_RE.exec(withoutShapes)) !== null) {
33
+ const key = attr(`<x ${m[2]}>`, "key");
34
+ if (key == null)
35
+ continue;
36
+ out.push({ process: proc, key, value: attr(`<x ${m[2]}>`, "value") ?? "" });
37
+ }
38
+ return out;
39
+ }
40
+ // --- Faithful port of the console's foldMeta/emitMeta (domain_types.ts) so the emitted
41
+ // meta.ts is byte-compatible with the IDE's own codegen. ---
42
+ function isIdent(name) {
43
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
44
+ }
45
+ /** Fold the scanned model-level metadata into a single key→value record. When two models
46
+ * declare the same key the last (scan order) wins. Empty keys are ignored. The accumulator
47
+ * is a **null-prototype** dict so user-authored keys (including `__proto__`) round-trip
48
+ * deterministically as own data properties. */
49
+ export function foldMeta(metas) {
50
+ const out = Object.create(null);
51
+ for (const m of metas) {
52
+ const key = (m?.key ?? "").trim();
53
+ if (!key)
54
+ continue;
55
+ out[key] = m.value ?? "";
56
+ }
57
+ return out;
58
+ }
59
+ /** Emit `meta.ts`: the typed model-metadata accessor (`@nanobpm/meta`, ADR 0040 §5).
60
+ * `AppMeta` keys the declared metadata as string-literal properties; `appMeta` is the
61
+ * concrete null-prototype record; the overloaded `meta()` returns the typed value for a
62
+ * known key and `string | undefined` for a dynamic one. Strip-safe (ADR 0036). */
63
+ export function emitMeta(metas) {
64
+ const folded = foldMeta(metas);
65
+ const keys = Object.keys(folded);
66
+ const propKey = (k) => (isIdent(k) ? k : JSON.stringify(k));
67
+ const header = "// AUTO-GENERATED by nanobpmn from the process model (ADR 0040 §5): the typed\n" +
68
+ "// model-metadata accessor. Each `nano:meta` key/value the models declare becomes\n" +
69
+ "// a checked `AppMeta` property, so `meta(key)` is typed and app-wide queryable\n" +
70
+ "// without a second store. Do not edit — regenerated from the model.\n" +
71
+ "// eslint-disable\n\n";
72
+ const iface = keys.length > 0
73
+ ? `export interface AppMeta {\n${keys.map((k) => ` ${propKey(k)}: string;`).join("\n")}\n}\n`
74
+ : `export interface AppMeta {}\n`;
75
+ const constDecl = keys.length > 0
76
+ ? `export const appMeta: AppMeta = (() => {\n` +
77
+ ` const m: Record<string, string> = Object.create(null);\n` +
78
+ keys.map((k) => ` m[${JSON.stringify(k)}] = ${JSON.stringify(folded[k])};`).join("\n") +
79
+ `\n return m as AppMeta;\n})();\n`
80
+ : `export const appMeta: AppMeta = Object.create(null) as AppMeta;\n`;
81
+ const accessor = `\n/** Read a model-level metadata value. A declared key is typed; a dynamic key\n` +
82
+ ` * returns \`string | undefined\`. */\n` +
83
+ `export function meta<K extends keyof AppMeta>(key: K): AppMeta[K];\n` +
84
+ `export function meta(key: string): string | undefined;\n` +
85
+ `export function meta(key: string): string | undefined {\n` +
86
+ ` return (appMeta as Record<string, string>)[key];\n` +
87
+ `}\n`;
88
+ return `${header}/** Every model-level metadata key the App's models declare (ADR 0040 §5). */\n` +
89
+ iface + "\n" + constDecl + accessor;
90
+ }
91
+ /**
92
+ * Derive `nano-generated/meta.ts` from BPMN models. Deterministic: models are scanned in
93
+ * path order, then their `nano:meta` entries folded (last write wins) into the accessor.
94
+ */
95
+ export function deriveMeta(models) {
96
+ const metas = [];
97
+ for (const model of [...models].sort(byModelPath)) {
98
+ metas.push(...scanModelMeta(model.xml));
99
+ }
100
+ return [{ path: `${GENERATED_DIR}/${META_TS}`, content: emitMeta(metas) }];
101
+ }
102
+ export const metaDeriver = {
103
+ id: "model->meta",
104
+ describe: "Derive meta.ts (the typed @nanobpm/meta accessor) from BPMN nano:meta entries.",
105
+ derive: ({ models }) => deriveMeta(models),
106
+ };