@bpmnkit/core 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/bpmn/bpmn-builder.d.ts +71 -0
  2. package/dist/bpmn/bpmn-builder.js +240 -106
  3. package/dist/bpmn/bpmn-serializer.js +70 -0
  4. package/dist/bpmn/builder-coverage.d.ts +59 -0
  5. package/dist/bpmn/builder-coverage.js +62 -0
  6. package/dist/bpmn/compact.d.ts +6 -0
  7. package/dist/bpmn/compact.js +37 -2
  8. package/dist/bpmn/diagram-diff.d.ts +71 -0
  9. package/dist/bpmn/diagram-diff.js +124 -0
  10. package/dist/bpmn/element-catalog.d.ts +31 -0
  11. package/dist/bpmn/element-catalog.js +50 -0
  12. package/dist/bpmn/element-shape.d.ts +49 -0
  13. package/dist/bpmn/element-shape.js +182 -0
  14. package/dist/bpmn/lint.d.ts +97 -0
  15. package/dist/bpmn/lint.js +135 -0
  16. package/dist/bpmn/preserving-writer.d.ts +64 -0
  17. package/dist/bpmn/preserving-writer.js +70 -0
  18. package/dist/bpmn/stable-ids.d.ts +30 -0
  19. package/dist/bpmn/stable-ids.js +134 -0
  20. package/dist/dmn/dmn-serializer.js +7 -2
  21. package/dist/dmn/preserving-writer.d.ts +31 -0
  22. package/dist/dmn/preserving-writer.js +36 -0
  23. package/dist/form/preserving-writer.d.ts +47 -0
  24. package/dist/form/preserving-writer.js +50 -0
  25. package/dist/index.d.ts +15 -1
  26. package/dist/index.js +9 -1
  27. package/dist/json/index.d.ts +5 -0
  28. package/dist/json/index.js +3 -0
  29. package/dist/json/json-patch.d.ts +65 -0
  30. package/dist/json/json-patch.js +416 -0
  31. package/dist/json/json-spans.d.ts +48 -0
  32. package/dist/json/json-spans.js +153 -0
  33. package/dist/layout/types.js +4 -0
  34. package/dist/xml/index.d.ts +5 -1
  35. package/dist/xml/index.js +2 -0
  36. package/dist/xml/xml-parser.d.ts +39 -0
  37. package/dist/xml/xml-parser.js +43 -2
  38. package/dist/xml/xml-patch.d.ts +109 -0
  39. package/dist/xml/xml-patch.js +459 -0
  40. package/dist/xml/xml-spans.d.ts +45 -0
  41. package/dist/xml/xml-spans.js +74 -0
  42. package/package.json +2 -1
@@ -0,0 +1,62 @@
1
+ /** Narrows a {@link BuilderSupport} to the entries that name a method. */
2
+ export function hasBuilderMethod(support) {
3
+ return "method" in support;
4
+ }
5
+ /**
6
+ * Data elements are not sequence-flow participants, and the fluent builder has
7
+ * no data-association API. Every builder method appends to the chain and wires
8
+ * a sequence flow from the previous node, so a `.dataObject()` would either
9
+ * emit invalid BPMN or leave an orphan shape. Parsing and serialising keep
10
+ * these types; authoring them is a separate API, not a chain method.
11
+ */
12
+ const NOT_A_CHAIN_PARTICIPANT = "Connected by data associations, not sequence flows; the chain has nowhere to put it";
13
+ /**
14
+ * Every `BpmnElementType`, mapped to the builder method that creates it.
15
+ *
16
+ * Adding a type to the union without adding it here is a compile error.
17
+ */
18
+ export const BUILDER_COVERAGE = {
19
+ // ---- Events ----
20
+ startEvent: { method: "startEvent" },
21
+ endEvent: { method: "endEvent" },
22
+ intermediateThrowEvent: { method: "intermediateThrowEvent" },
23
+ intermediateCatchEvent: { method: "intermediateCatchEvent" },
24
+ boundaryEvent: { method: "boundaryEvent" },
25
+ // ---- Activities ----
26
+ task: { method: "task" },
27
+ serviceTask: { method: "serviceTask" },
28
+ scriptTask: { method: "scriptTask" },
29
+ userTask: { method: "userTask" },
30
+ sendTask: { method: "sendTask" },
31
+ receiveTask: { method: "receiveTask" },
32
+ businessRuleTask: { method: "businessRuleTask" },
33
+ manualTask: { method: "manualTask" },
34
+ callActivity: { method: "callActivity" },
35
+ // ---- Gateways ----
36
+ exclusiveGateway: { method: "exclusiveGateway" },
37
+ parallelGateway: { method: "parallelGateway" },
38
+ inclusiveGateway: { method: "inclusiveGateway" },
39
+ eventBasedGateway: { method: "eventBasedGateway" },
40
+ complexGateway: { method: "complexGateway" },
41
+ // ---- Containers ----
42
+ subProcess: { method: "subProcess" },
43
+ adHocSubProcess: { method: "adHocSubProcess" },
44
+ eventSubProcess: { method: "eventSubProcess", emits: "subProcess" },
45
+ transaction: { method: "transaction" },
46
+ // ---- Data ----
47
+ dataObject: { exempt: NOT_A_CHAIN_PARTICIPANT },
48
+ dataObjectReference: { exempt: NOT_A_CHAIN_PARTICIPANT },
49
+ dataStoreReference: { exempt: NOT_A_CHAIN_PARTICIPANT },
50
+ };
51
+ /**
52
+ * The builders the table applies to — every class that builds a flow chain.
53
+ *
54
+ * `DiagramBuilder` is excluded on purpose: it assembles collaborations,
55
+ * participants and message flows, and never adds flow elements itself.
56
+ */
57
+ export const FLOW_BUILDER_NAMES = [
58
+ "ProcessBuilder",
59
+ "BranchBuilder",
60
+ "SubProcessContentBuilder",
61
+ ];
62
+ //# sourceMappingURL=builder-coverage.js.map
@@ -7,6 +7,12 @@ export interface CompactElement {
7
7
  name?: string;
8
8
  /** Zeebe job type (serviceTask: zeebe:taskDefinition.type) */
9
9
  jobType?: string;
10
+ /** dataObjectReference: id of the dataObject it points at. */
11
+ dataObjectRef?: string;
12
+ /** dataStoreReference: id of the data store it points at. */
13
+ dataStoreRef?: string;
14
+ /** dataObject / dataObjectReference: marks a collection. */
15
+ isCollection?: boolean;
10
16
  /**
11
17
  * Zeebe task headers (key→value).
12
18
  * For the Camunda HTTP connector use jobType "io.camunda:http-json:1" and set:
@@ -57,6 +57,15 @@ function compactifyElement(el) {
57
57
  if (el.cancelActivity === false)
58
58
  result.interrupting = false;
59
59
  }
60
+ if (el.type === "dataObjectReference" && el.dataObjectRef !== undefined) {
61
+ result.dataObjectRef = el.dataObjectRef;
62
+ }
63
+ if (el.type === "dataStoreReference" && el.dataStoreRef !== undefined) {
64
+ result.dataStoreRef = el.dataStoreRef;
65
+ }
66
+ if ((el.type === "dataObject" || el.type === "dataObjectReference") && el.isCollection) {
67
+ result.isCollection = true;
68
+ }
60
69
  // Recurse into sub-process container children
61
70
  if ("flowElements" in el && Array.isArray(el.flowElements) && el.flowElements.length > 0) {
62
71
  const seqFlows = "sequenceFlows" in el && Array.isArray(el.sequenceFlows) ? el.sequenceFlows : [];
@@ -291,8 +300,34 @@ export function buildFlowElement(el, incoming, outgoing) {
291
300
  return { ...base, type: "eventBasedGateway" };
292
301
  case "complexGateway":
293
302
  return { ...base, type: "complexGateway" };
294
- default:
295
- return { ...base, type: "task" };
303
+ // Data elements are not sequence-flow participants; they carry a
304
+ // reference instead, and dropping that reference is what made them
305
+ // round-trip as bare tasks before.
306
+ case "dataObject":
307
+ return {
308
+ ...base,
309
+ type: "dataObject",
310
+ ...(el.isCollection !== undefined ? { isCollection: el.isCollection } : {}),
311
+ };
312
+ case "dataObjectReference":
313
+ return {
314
+ ...base,
315
+ type: "dataObjectReference",
316
+ ...(el.dataObjectRef !== undefined ? { dataObjectRef: el.dataObjectRef } : {}),
317
+ ...(el.isCollection !== undefined ? { isCollection: el.isCollection } : {}),
318
+ };
319
+ case "dataStoreReference":
320
+ return {
321
+ ...base,
322
+ type: "dataStoreReference",
323
+ ...(el.dataStoreRef !== undefined ? { dataStoreRef: el.dataStoreRef } : {}),
324
+ };
325
+ default: {
326
+ // Exhaustive: a new BpmnElementType fails here rather than silently
327
+ // expanding to a task, which is how the data types were lost.
328
+ const unhandled = el.type;
329
+ throw new Error(`Unhandled BPMN element type in expand(): ${String(unhandled)}`);
330
+ }
296
331
  }
297
332
  }
298
333
  function buildDiagram(processId, process) {
@@ -0,0 +1,71 @@
1
+ import type { BpmnDefinitions } from "./bpmn-model.js";
2
+ /**
3
+ * How an element differs between two versions of a diagram.
4
+ *
5
+ * - `added` — present only in the later model.
6
+ * - `removed` — present only in the earlier model.
7
+ * - `changed` — the model changed (a property, an expression, an extension).
8
+ * - `moved` — the model is identical but the picture is not: position, size,
9
+ * waypoints, label placement, or a diagram flag such as collapsed/expanded.
10
+ *
11
+ * An element that is both changed and moved is reported as `changed` — a
12
+ * semantic change is what a reviewer needs to see first.
13
+ */
14
+ export type BpmnDiffCategory = "added" | "removed" | "changed" | "moved";
15
+ /**
16
+ * How many differences fall on one diagram plane.
17
+ *
18
+ * A plane is a drawing surface: the process itself, and one more for each
19
+ * collapsed sub-process. A viewer shows one at a time, so a change inside a
20
+ * collapsed sub-process is invisible until the reader drills into it — these
21
+ * counts are what tells them there is something down there to look at.
22
+ */
23
+ export interface BpmnDiffPlaneSummary {
24
+ /** The plane's `bpmnElement` — the process or sub-process it draws. */
25
+ readonly id: string;
26
+ readonly added: number;
27
+ readonly removed: number;
28
+ readonly changed: number;
29
+ readonly moved: number;
30
+ readonly total: number;
31
+ }
32
+ /**
33
+ * What changed between two diagrams, as element ids per category.
34
+ *
35
+ * Restricted to elements that carry diagram interchange on at least one side —
36
+ * those are exactly the elements a canvas can show. A change with nothing to
37
+ * draw (a `targetNamespace`, an exporter attribute) is not a visual diff and is
38
+ * left out, so the counts match what the reviewer actually sees.
39
+ */
40
+ export interface BpmnDiffResult {
41
+ readonly added: readonly string[];
42
+ readonly removed: readonly string[];
43
+ readonly changed: readonly string[];
44
+ readonly moved: readonly string[];
45
+ /** `added.length + removed.length + changed.length + moved.length`. */
46
+ readonly total: number;
47
+ /** Per-plane breakdown, planes with no differences omitted, ordered by id. */
48
+ readonly planes: readonly BpmnDiffPlaneSummary[];
49
+ }
50
+ /**
51
+ * Compares two BPMN diagrams and reports what a reviewer would see change.
52
+ *
53
+ * The semantic half is {@link diffSemantics}, which excludes diagram
54
+ * interchange by design — so a pure layout change reads as "no change" there.
55
+ * This adds the layout half back as its own category, which is the difference
56
+ * between a model diff and a *diagram* diff.
57
+ *
58
+ * @param before - The earlier model.
59
+ * @param after - The later model.
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const result = diffDiagram(Bpmn.parse(oldXml), Bpmn.parse(newXml));
64
+ * console.log(`${result.changed.length} changed, ${result.moved.length} moved`);
65
+ * for (const plane of result.planes) {
66
+ * console.log(` ${plane.id}: ${plane.total}`);
67
+ * }
68
+ * ```
69
+ */
70
+ export declare function diffDiagram(before: BpmnDefinitions, after: BpmnDefinitions): BpmnDiffResult;
71
+ //# sourceMappingURL=diagram-diff.d.ts.map
@@ -0,0 +1,124 @@
1
+ import { diffSemantics } from "./semantic-hash.js";
2
+ /**
3
+ * Rounds to two decimals so re-serialising a file cannot register as a move.
4
+ * BPMN coordinates are usually integers, but a round trip through a modeler can
5
+ * leave `100` as `100.00000000000001`.
6
+ */
7
+ function round(value) {
8
+ return Math.round(value * 100) / 100;
9
+ }
10
+ function shapeKey(shape) {
11
+ const { x, y, width, height } = shape.bounds;
12
+ const label = shape.label?.bounds;
13
+ return [
14
+ round(x),
15
+ round(y),
16
+ round(width),
17
+ round(height),
18
+ shape.isExpanded ?? "",
19
+ shape.isMarkerVisible ?? "",
20
+ shape.isHorizontal ?? "",
21
+ label === undefined ? "" : `${round(label.x)},${round(label.y)}`,
22
+ ].join("|");
23
+ }
24
+ function edgeKey(edge) {
25
+ const label = edge.label?.bounds;
26
+ return [
27
+ edge.waypoints.map((p) => `${round(p.x)},${round(p.y)}`).join(" "),
28
+ label === undefined ? "" : `${round(label.x)},${round(label.y)}`,
29
+ ].join("|");
30
+ }
31
+ /** Indexes every element that carries diagram interchange, in any plane. */
32
+ function indexLayout(definitions) {
33
+ const index = new Map();
34
+ for (const diagram of definitions.diagrams) {
35
+ const plane = diagram.plane.bpmnElement;
36
+ for (const shape of diagram.plane.shapes) {
37
+ index.set(shape.bpmnElement, { plane, key: shapeKey(shape) });
38
+ }
39
+ for (const edge of diagram.plane.edges) {
40
+ index.set(edge.bpmnElement, { plane, key: edgeKey(edge) });
41
+ }
42
+ }
43
+ return index;
44
+ }
45
+ function summarisePlanes(assignments) {
46
+ const counts = new Map();
47
+ for (const [plane, category] of assignments) {
48
+ const entry = counts.get(plane) ?? { added: 0, removed: 0, changed: 0, moved: 0 };
49
+ entry[category] += 1;
50
+ counts.set(plane, entry);
51
+ }
52
+ return [...counts.entries()]
53
+ .map(([id, c]) => ({
54
+ id,
55
+ ...c,
56
+ total: c.added + c.removed + c.changed + c.moved,
57
+ }))
58
+ .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
59
+ }
60
+ /**
61
+ * Compares two BPMN diagrams and reports what a reviewer would see change.
62
+ *
63
+ * The semantic half is {@link diffSemantics}, which excludes diagram
64
+ * interchange by design — so a pure layout change reads as "no change" there.
65
+ * This adds the layout half back as its own category, which is the difference
66
+ * between a model diff and a *diagram* diff.
67
+ *
68
+ * @param before - The earlier model.
69
+ * @param after - The later model.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const result = diffDiagram(Bpmn.parse(oldXml), Bpmn.parse(newXml));
74
+ * console.log(`${result.changed.length} changed, ${result.moved.length} moved`);
75
+ * for (const plane of result.planes) {
76
+ * console.log(` ${plane.id}: ${plane.total}`);
77
+ * }
78
+ * ```
79
+ */
80
+ export function diffDiagram(before, after) {
81
+ const beforeLayout = indexLayout(before);
82
+ const afterLayout = indexLayout(after);
83
+ /** Ids a canvas could draw on one side or the other. */
84
+ const drawable = new Set([...beforeLayout.keys(), ...afterLayout.keys()]);
85
+ const semantic = diffSemantics(before, after);
86
+ const added = semantic.added.filter((id) => drawable.has(id));
87
+ const removed = semantic.removed.filter((id) => drawable.has(id));
88
+ const changed = semantic.changed.map((entry) => entry.id).filter((id) => drawable.has(id));
89
+ const changedIds = new Set(changed);
90
+ const moved = [];
91
+ for (const [id, placement] of afterLayout) {
92
+ if (changedIds.has(id))
93
+ continue;
94
+ const previous = beforeLayout.get(id);
95
+ if (previous !== undefined && previous.key !== placement.key)
96
+ moved.push(id);
97
+ }
98
+ moved.sort();
99
+ // An element is attributed to the plane that still draws it; only a removal
100
+ // has no later plane to belong to.
101
+ const planeOf = (id) => (afterLayout.get(id) ?? beforeLayout.get(id))?.plane;
102
+ const assignments = [];
103
+ for (const [category, ids] of [
104
+ ["added", added],
105
+ ["removed", removed],
106
+ ["changed", changed],
107
+ ["moved", moved],
108
+ ]) {
109
+ for (const id of ids) {
110
+ const plane = planeOf(id);
111
+ if (plane !== undefined)
112
+ assignments.push([plane, category]);
113
+ }
114
+ }
115
+ return {
116
+ added,
117
+ removed,
118
+ changed,
119
+ moved,
120
+ total: added.length + removed.length + changed.length + moved.length,
121
+ planes: summarisePlanes(assignments),
122
+ };
123
+ }
124
+ //# sourceMappingURL=diagram-diff.js.map
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Every BPMN element type, grouped.
3
+ *
4
+ * Tool schemas, CLI help and prompts all want to say "here are the element
5
+ * types you can use", and each one used to say it as a hand-written string.
6
+ * Those strings drift: the MCP tool schema advertised 18 types while the
7
+ * compact path accepted 23, so five constructs — `receiveTask`, `task`,
8
+ * `complexGateway`, `transaction`, `eventSubProcess` — worked but no caller
9
+ * could discover them.
10
+ *
11
+ * This is the one list. Like `BUILDER_COVERAGE`, it is a total `Record` keyed
12
+ * by `BpmnElementType`, so **adding a member to that union fails `tsc` here
13
+ * until the new type is placed in a group** — and every generated description
14
+ * picks it up automatically.
15
+ */
16
+ import type { BpmnElementType } from "./bpmn-model.js";
17
+ /** The coarse families a modeller picks from. */
18
+ export type ElementTypeGroup = "event" | "task" | "gateway" | "container" | "data";
19
+ /** Group labels, in the order a description should list them. */
20
+ export declare const ELEMENT_GROUP_ORDER: readonly ElementTypeGroup[];
21
+ /**
22
+ * Every `BpmnElementType`, mapped to its group.
23
+ *
24
+ * Adding a type to the union without placing it here is a compile error.
25
+ */
26
+ export declare const ELEMENT_TYPE_GROUPS: Record<BpmnElementType, ElementTypeGroup>;
27
+ /** Every element type in the given group, in declaration order. */
28
+ export declare function elementTypesInGroup(group: ElementTypeGroup): BpmnElementType[];
29
+ /** Every element type the SDK models, in declaration order. */
30
+ export declare function allElementTypes(): BpmnElementType[];
31
+ //# sourceMappingURL=element-catalog.d.ts.map
@@ -0,0 +1,50 @@
1
+ /** Group labels, in the order a description should list them. */
2
+ export const ELEMENT_GROUP_ORDER = [
3
+ "event",
4
+ "task",
5
+ "gateway",
6
+ "container",
7
+ "data",
8
+ ];
9
+ /**
10
+ * Every `BpmnElementType`, mapped to its group.
11
+ *
12
+ * Adding a type to the union without placing it here is a compile error.
13
+ */
14
+ export const ELEMENT_TYPE_GROUPS = {
15
+ startEvent: "event",
16
+ endEvent: "event",
17
+ intermediateThrowEvent: "event",
18
+ intermediateCatchEvent: "event",
19
+ boundaryEvent: "event",
20
+ task: "task",
21
+ serviceTask: "task",
22
+ scriptTask: "task",
23
+ userTask: "task",
24
+ sendTask: "task",
25
+ receiveTask: "task",
26
+ businessRuleTask: "task",
27
+ manualTask: "task",
28
+ callActivity: "task",
29
+ exclusiveGateway: "gateway",
30
+ parallelGateway: "gateway",
31
+ inclusiveGateway: "gateway",
32
+ eventBasedGateway: "gateway",
33
+ complexGateway: "gateway",
34
+ subProcess: "container",
35
+ adHocSubProcess: "container",
36
+ eventSubProcess: "container",
37
+ transaction: "container",
38
+ dataObject: "data",
39
+ dataObjectReference: "data",
40
+ dataStoreReference: "data",
41
+ };
42
+ /** Every element type in the given group, in declaration order. */
43
+ export function elementTypesInGroup(group) {
44
+ return Object.keys(ELEMENT_TYPE_GROUPS).filter((type) => ELEMENT_TYPE_GROUPS[type] === group);
45
+ }
46
+ /** Every element type the SDK models, in declaration order. */
47
+ export function allElementTypes() {
48
+ return Object.keys(ELEMENT_TYPE_GROUPS);
49
+ }
50
+ //# sourceMappingURL=element-catalog.js.map
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Building a flow element of a given type, and changing the type of one that
3
+ * already exists.
4
+ *
5
+ * `createFlowElement` is the single place that knows which fields each
6
+ * `BpmnElementType` carries. Its switch is exhaustive — the `default` branch
7
+ * assigns to `never`, so **adding a member to `BpmnElementType` fails `tsc`
8
+ * here** rather than falling through to a plausible-looking wrong shape.
9
+ *
10
+ * `retypeElement` is what lets a caller change a task's type without hand-
11
+ * editing XML: the reason the SDK's own users reached for the file. It keeps
12
+ * identity and wiring, and carries over anything both the old and new type can
13
+ * hold.
14
+ */
15
+ import type { XmlElement } from "../types/xml-element.js";
16
+ import type { BpmnElementType, BpmnFlowElement } from "./bpmn-model.js";
17
+ export interface CreateFlowElementOptions {
18
+ name?: string;
19
+ documentation?: string;
20
+ extensionElements?: XmlElement[];
21
+ }
22
+ /** True when the type holds nested flow elements. */
23
+ export declare function isContainerType(type: BpmnElementType): boolean;
24
+ /** True when the type can carry a multi-instance marker. */
25
+ export declare function isLoopCapableType(type: BpmnElementType): boolean;
26
+ /** True when the type carries event definitions. */
27
+ export declare function isEventType(type: BpmnElementType): boolean;
28
+ /**
29
+ * Build an empty flow element of the given type.
30
+ *
31
+ * The switch is exhaustive over `BpmnElementType`; the `default` branch exists
32
+ * only to make that a compile error rather than a silent wrong shape.
33
+ */
34
+ export declare function createFlowElement(id: string, type: BpmnElementType, options?: CreateFlowElementOptions): BpmnFlowElement;
35
+ /**
36
+ * Return a copy of `el` with a different `type`, keeping everything the new
37
+ * type can still hold.
38
+ *
39
+ * Kept: id, name, documentation, incoming/outgoing flows, compensation flag,
40
+ * extension elements and unmodelled attributes — so retyping never breaks the
41
+ * graph around the element. Carried over when *both* types support them:
42
+ * nested content (container → container), the multi-instance marker, and event
43
+ * definitions. Anything the new type cannot hold is dropped, which is the
44
+ * point: a `serviceTask` retyped to `manualTask` should not keep a job worker.
45
+ *
46
+ * Returns `el` unchanged when the type already matches.
47
+ */
48
+ export declare function retypeElement(el: BpmnFlowElement, type: BpmnElementType): BpmnFlowElement;
49
+ //# sourceMappingURL=element-shape.d.ts.map
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Building a flow element of a given type, and changing the type of one that
3
+ * already exists.
4
+ *
5
+ * `createFlowElement` is the single place that knows which fields each
6
+ * `BpmnElementType` carries. Its switch is exhaustive — the `default` branch
7
+ * assigns to `never`, so **adding a member to `BpmnElementType` fails `tsc`
8
+ * here** rather than falling through to a plausible-looking wrong shape.
9
+ *
10
+ * `retypeElement` is what lets a caller change a task's type without hand-
11
+ * editing XML: the reason the SDK's own users reached for the file. It keeps
12
+ * identity and wiring, and carries over anything both the old and new type can
13
+ * hold.
14
+ */
15
+ import { bpmnElementName, isZeebePlacementAllowed } from "./zeebe-extensions.js";
16
+ /** Container types — the ones that hold their own flow elements and flows. */
17
+ const CONTAINER_TYPES = new Set([
18
+ "subProcess",
19
+ "adHocSubProcess",
20
+ "eventSubProcess",
21
+ "transaction",
22
+ ]);
23
+ /** Types that can carry `loopCharacteristics` (a multi-instance marker). */
24
+ const LOOP_CAPABLE_TYPES = new Set([
25
+ "task",
26
+ "serviceTask",
27
+ "scriptTask",
28
+ "userTask",
29
+ "sendTask",
30
+ "receiveTask",
31
+ "businessRuleTask",
32
+ "manualTask",
33
+ "callActivity",
34
+ "subProcess",
35
+ "adHocSubProcess",
36
+ "transaction",
37
+ ]);
38
+ /** Event types — the ones that carry `eventDefinitions`. */
39
+ const EVENT_TYPES = new Set([
40
+ "startEvent",
41
+ "endEvent",
42
+ "intermediateThrowEvent",
43
+ "intermediateCatchEvent",
44
+ "boundaryEvent",
45
+ ]);
46
+ /** True when the type holds nested flow elements. */
47
+ export function isContainerType(type) {
48
+ return CONTAINER_TYPES.has(type);
49
+ }
50
+ /** True when the type can carry a multi-instance marker. */
51
+ export function isLoopCapableType(type) {
52
+ return LOOP_CAPABLE_TYPES.has(type);
53
+ }
54
+ /** True when the type carries event definitions. */
55
+ export function isEventType(type) {
56
+ return EVENT_TYPES.has(type);
57
+ }
58
+ function emptyContainer() {
59
+ return {
60
+ flowElements: [],
61
+ sequenceFlows: [],
62
+ textAnnotations: [],
63
+ associations: [],
64
+ groups: [],
65
+ };
66
+ }
67
+ /**
68
+ * Build an empty flow element of the given type.
69
+ *
70
+ * The switch is exhaustive over `BpmnElementType`; the `default` branch exists
71
+ * only to make that a compile error rather than a silent wrong shape.
72
+ */
73
+ export function createFlowElement(id, type, options) {
74
+ const base = {
75
+ id,
76
+ name: options?.name,
77
+ incoming: [],
78
+ outgoing: [],
79
+ documentation: options?.documentation,
80
+ extensionElements: options?.extensionElements ?? [],
81
+ unknownAttributes: {},
82
+ };
83
+ switch (type) {
84
+ case "startEvent":
85
+ case "endEvent":
86
+ case "intermediateThrowEvent":
87
+ case "intermediateCatchEvent":
88
+ return { ...base, type, eventDefinitions: [] };
89
+ case "boundaryEvent":
90
+ return { ...base, type, attachedToRef: "", eventDefinitions: [] };
91
+ case "task":
92
+ case "serviceTask":
93
+ case "scriptTask":
94
+ case "userTask":
95
+ case "sendTask":
96
+ case "receiveTask":
97
+ case "businessRuleTask":
98
+ case "manualTask":
99
+ case "callActivity":
100
+ return { ...base, type };
101
+ case "exclusiveGateway":
102
+ case "parallelGateway":
103
+ case "inclusiveGateway":
104
+ case "eventBasedGateway":
105
+ case "complexGateway":
106
+ return { ...base, type };
107
+ case "subProcess":
108
+ case "adHocSubProcess":
109
+ case "eventSubProcess":
110
+ case "transaction":
111
+ return { ...base, type, ...emptyContainer() };
112
+ case "dataObject":
113
+ case "dataObjectReference":
114
+ case "dataStoreReference":
115
+ return { ...base, type };
116
+ default: {
117
+ const unhandled = type;
118
+ throw new Error(`Unhandled BPMN element type: ${String(unhandled)}`);
119
+ }
120
+ }
121
+ }
122
+ /**
123
+ * Return a copy of `el` with a different `type`, keeping everything the new
124
+ * type can still hold.
125
+ *
126
+ * Kept: id, name, documentation, incoming/outgoing flows, compensation flag,
127
+ * extension elements and unmodelled attributes — so retyping never breaks the
128
+ * graph around the element. Carried over when *both* types support them:
129
+ * nested content (container → container), the multi-instance marker, and event
130
+ * definitions. Anything the new type cannot hold is dropped, which is the
131
+ * point: a `serviceTask` retyped to `manualTask` should not keep a job worker.
132
+ *
133
+ * Returns `el` unchanged when the type already matches.
134
+ */
135
+ export function retypeElement(el, type) {
136
+ if (el.type === type)
137
+ return el;
138
+ // Keep only the extensions the Zeebe schema allows on the new element. This
139
+ // is what stops a serviceTask retyped to manualTask carrying a job worker
140
+ // the engine would then refuse to deploy. Extensions the descriptor says
141
+ // nothing about — anyone else's namespace — are kept.
142
+ const ownerName = bpmnElementName({ type });
143
+ const extensionElements = el.extensionElements.filter((ext) => isZeebePlacementAllowed(ownerName, ext.name));
144
+ const next = createFlowElement(el.id, type, {
145
+ name: el.name,
146
+ documentation: el.documentation,
147
+ extensionElements,
148
+ });
149
+ next.incoming = [...el.incoming];
150
+ next.outgoing = [...el.outgoing];
151
+ next.unknownAttributes = { ...el.unknownAttributes };
152
+ if (el.isForCompensation !== undefined)
153
+ next.isForCompensation = el.isForCompensation;
154
+ if (isContainerType(el.type) && isContainerType(type) && "flowElements" in el) {
155
+ const target = next;
156
+ target.flowElements = el.flowElements;
157
+ target.sequenceFlows = el.sequenceFlows;
158
+ target.textAnnotations = el.textAnnotations;
159
+ target.associations = el.associations;
160
+ target.groups = el.groups;
161
+ }
162
+ if (isLoopCapableType(el.type) && isLoopCapableType(type) && "loopCharacteristics" in el) {
163
+ const loop = el.loopCharacteristics;
164
+ if (loop) {
165
+ ;
166
+ next.loopCharacteristics = loop;
167
+ }
168
+ }
169
+ if (isEventType(el.type) && isEventType(type) && "eventDefinitions" in el) {
170
+ ;
171
+ next.eventDefinitions = el.eventDefinitions;
172
+ }
173
+ // A boundary event is meaningless without its host, so keep the attachment
174
+ // when retyping between boundary events; when the source was not one, the
175
+ // caller must set it.
176
+ if (el.type === "boundaryEvent" && type === "boundaryEvent") {
177
+ ;
178
+ next.attachedToRef = el.attachedToRef;
179
+ }
180
+ return next;
181
+ }
182
+ //# sourceMappingURL=element-shape.js.map