@uipath/maestro-builder-sdk 6.111.0 → 6.113.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.
@@ -0,0 +1,271 @@
1
+ /**
2
+ * auto-layout — the canvas geometry compile writes: every node's size, and, in a flow
3
+ * or subflow with no `.layout()` node entries, every node's position.
4
+ *
5
+ * Sizes follow the node's canvas shape, the way the designer draws it and
6
+ * `uip maestro flow format` writes it: a `container` (a loop, a do-while) is
7
+ * 560 × 320, a `rectangle` (an inline agent) 288 × 96, anything else 96 × 96. The shape
8
+ * comes from the node's own `display.shape`, else from its definition's (the manifest
9
+ * every family writes into `definitions[]`), so no node kind is named here.
10
+ * A `.layout()` entry with a `size` (`at(x, y, dim(w, h))`) keeps it.
11
+ *
12
+ * Positions, when the author gave none, are the ones `format` computes: this is a copy
13
+ * of uipath/cli `packages/flow-tool/src/services/tidy-service.ts` `tidyNodesAndEdges`
14
+ * (main@62942700c), over the flow-core layout in `core/elk-layout.ts`. It lays each
15
+ * top-level container's body out inside it, grows the container to fit, then lays the
16
+ * scope out left to right with ELK and puts its leftmost node at (256, 144), where the
17
+ * canvas puts a new flow's trigger. Sticky notes keep their own position and size.
18
+ *
19
+ * One difference from `format`, on purpose: `format` keeps the leftmost node where it
20
+ * was, and before this compile put a subflow's first node at its placeholder column
21
+ * (`x = 256 + 288 × index`), far right of the canvas origin. Here every scope starts at
22
+ * (256, 144). `format` on a compiled file therefore moves nothing: compile's output is
23
+ * already formatted (`tests/auto-layout.test.ts`).
24
+ *
25
+ * Each node so placed gets `ui.placeholder` set to that position (`PLACEHOLDER_FLAG` in
26
+ * `node-placement.ts`): the position is compile's, not the author's, for as long as the
27
+ * node is still there. Merge places such a node when an edit added it, and decompile
28
+ * writes no `.layout()` entry for it. Studio Web keeps the key when someone drags the
29
+ * node, but the position changes, so the node then reads as placed by a person.
30
+ *
31
+ * What `format` does that this does not need: it hides the artifact children of a node
32
+ * whose `ui.isCollapsed` is set, and compile never sets it.
33
+ *
34
+ * Example, the shipped `ClubDirectory` (no `.layout()`): `start` (256,144),
35
+ * `groups` (448,144), `headline` (640,128), the loop `eachClub` (832,48) at
36
+ * 560 × 320 with `poster` at (240,112) inside it, `end` (1488,64). Before, they were
37
+ * one row, x = 256, 544, 832, … and all 96 × 96.
38
+ */
39
+ import { STICKY_NOTE_TYPE } from './core/definitionless-node-types.js';
40
+ import { markCompilePlaced } from './node-placement.js';
41
+ import { CompileRefusal } from './core/compile-refusal.js';
42
+ import { DEFAULT_NODE_SIZE, defaultContainerFit, layoutRoot, snapToGrid, tidyContainer, } from './core/elk-layout.js';
43
+ /** Where the canvas puts a new flow's trigger (flow-core `DEFAULT_TRIGGER_POSITION`). */
44
+ const ORIGIN = { x: 256, y: 144 };
45
+ /** The width of a `rectangle` node (apollo-react `getExpandedSize`, via tidy-service). */
46
+ const RECTANGLE_WIDTH = 288;
47
+ /** The canvas size of a node of `shape` (tidy-service `expandedSizeForShape`). */
48
+ function sizeForShape(shape) {
49
+ if (shape === 'container') {
50
+ const fit = defaultContainerFit();
51
+ return { width: fit.minWidth, height: fit.minHeight };
52
+ }
53
+ return { width: shape === 'rectangle' ? RECTANGLE_WIDTH : DEFAULT_NODE_SIZE, height: DEFAULT_NODE_SIZE };
54
+ }
55
+ /**
56
+ * The node's canvas shape: its own `display.shape`, else its definition's, matched by
57
+ * type and version, then by type (tidy-service `buildShapeResolver`).
58
+ */
59
+ function shapeResolver(definitions) {
60
+ const byKey = new Map();
61
+ const byType = new Map();
62
+ for (const raw of definitions) {
63
+ const def = raw;
64
+ if (!def || typeof def !== 'object' || typeof def.nodeType !== 'string')
65
+ continue;
66
+ const shape = def.display?.shape;
67
+ if (def.version !== undefined)
68
+ byKey.set(`${def.nodeType}:${def.version}`, shape);
69
+ if (!byType.has(def.nodeType))
70
+ byType.set(def.nodeType, shape);
71
+ }
72
+ return (node) => node.display?.shape || (byKey.get(`${node.type}:${node.typeVersion}`) ?? byType.get(node.type));
73
+ }
74
+ /** A handle group's `position` as a port side (tidy-service `positionToSide`). */
75
+ function positionToSide(position) {
76
+ switch (position) {
77
+ case 'left': return 'WEST';
78
+ case 'right': return 'EAST';
79
+ case 'top': return 'NORTH';
80
+ case 'bottom':
81
+ case 'bottom-left':
82
+ case 'bottom-right': return 'SOUTH';
83
+ default: return 'EAST';
84
+ }
85
+ }
86
+ /** A handle's ids on this node: one per item of its `repeat` collection (uipath/cli `expandHandleIds`). */
87
+ function expandHandleIds(handle, node) {
88
+ if (!handle.repeat)
89
+ return [handle.id];
90
+ let collection = node;
91
+ for (const segment of handle.repeat.split('.')) {
92
+ collection = collection && typeof collection === 'object' ? collection[segment] : undefined;
93
+ }
94
+ if (!Array.isArray(collection))
95
+ return [handle.id];
96
+ return collection
97
+ .filter((item) => item != null && typeof item === 'object')
98
+ .map((item) => handle.id.replace(/\{item\.(\w+)\}/g, (_m, key) => String(item[key] ?? '')));
99
+ }
100
+ const DEFAULT_PORTS = [
101
+ { id: 'input', side: 'WEST', index: 0 },
102
+ { id: 'output', side: 'EAST', index: 1 },
103
+ ];
104
+ /** Each node's handles as ports, from its definition's `handleConfiguration` (tidy-service `buildPortResolver`). */
105
+ function portResolver(definitions, nodeOf) {
106
+ const defs = new Map();
107
+ for (const raw of definitions) {
108
+ const def = raw;
109
+ if (def && typeof def === 'object' && 'nodeType' in def && 'version' in def)
110
+ defs.set(`${def.nodeType}:${def.version}`, def);
111
+ }
112
+ return (layoutNode) => {
113
+ const node = nodeOf(layoutNode.id);
114
+ const def = node && defs.get(`${node.type}:${node.typeVersion}`);
115
+ if (!node || !def?.handleConfiguration?.length)
116
+ return [...DEFAULT_PORTS];
117
+ const ports = [];
118
+ let index = 0;
119
+ for (const group of def.handleConfiguration) {
120
+ const side = positionToSide(group.position);
121
+ for (const handle of group.handles ?? []) {
122
+ for (const id of expandHandleIds(handle, node))
123
+ ports.push({ id, side, index: index++ });
124
+ }
125
+ }
126
+ return ports.length > 0 ? ports : [...DEFAULT_PORTS];
127
+ };
128
+ }
129
+ /**
130
+ * Write compile's geometry onto every scope of `flow`: canonical sizes everywhere, and
131
+ * the `format` layout in each scope where the author placed no node.
132
+ *
133
+ * @param flow - The compiled flow (root `nodes`/`edges`, `subflows`, `definitions`); changed in place.
134
+ * @param author - What `.layout()` set.
135
+ */
136
+ export function applyCompiledGeometry(flow, author) {
137
+ const definitions = flow.definitions ?? [];
138
+ const shapeOf = shapeResolver(definitions);
139
+ for (const [where, scope] of [['the flow', flow], ...Object.entries(flow.subflows ?? {}).map(([k, sf]) => [`subflow "${k}"`, sf])]) {
140
+ const authored = scope.nodes.some((n) => n.type !== STICKY_NOTE_TYPE && author.placed.has(n.id));
141
+ if (authored) {
142
+ for (const n of scope.nodes) {
143
+ if (n.type === STICKY_NOTE_TYPE || author.sized.has(n.id))
144
+ continue;
145
+ n.ui = { ...n.ui, size: sizeForShape(shapeOf(n)) };
146
+ }
147
+ }
148
+ else {
149
+ try {
150
+ layOutScope(scope, definitions, shapeOf);
151
+ }
152
+ catch (e) {
153
+ if (e instanceof CompileRefusal)
154
+ throw e;
155
+ // Before #1992 compile ran no layout, so a layout failure must not leave the
156
+ // author stuck: name the way out, which is to place the nodes yourself.
157
+ throw new CompileRefusal('LAYOUT_AUTO_FAILED', `compile could not lay out ${where}: ${e instanceof Error ? e.message : String(e)}. `
158
+ + 'Give it a .layout() with at least one entry (for example `start: at(256, 144)`): compile then '
159
+ + 'keeps its one-row placeholder for every other node instead of laying the scope out. '
160
+ + 'Please report the flow, since `uip maestro flow format` runs the same layout.');
161
+ }
162
+ }
163
+ }
164
+ }
165
+ /** tidy-service `tidyNodesAndEdges` for one scope, anchored at {@link ORIGIN}. */
166
+ function layOutScope(scope, definitions, shapeOf) {
167
+ const { nodes, edges } = scope;
168
+ if (nodes.length === 0)
169
+ return;
170
+ const isSticky = (n) => n.type === STICKY_NOTE_TYPE;
171
+ const byId = new Map(nodes.map((n) => [n.id, n]));
172
+ const parentOf = (n) => typeof n?.parentId === 'string' && n.parentId.length > 0 ? n.parentId : undefined;
173
+ const allNodes = nodes.map((n) => {
174
+ const size = isSticky(n) ? (n.ui?.size ?? { width: DEFAULT_NODE_SIZE, height: DEFAULT_NODE_SIZE }) : sizeForShape(shapeOf(n));
175
+ return { id: n.id, position: { ...(n.ui?.position ?? { x: 0, y: 0 }) }, width: size.width, height: size.height };
176
+ });
177
+ const allEdges = edges.map((e) => ({ id: e.id, source: e.sourceNodeId, target: e.targetNodeId, sourceHandle: e.sourcePort, targetHandle: e.targetPort }));
178
+ const isContainerId = (id) => {
179
+ const n = byId.get(id);
180
+ return n !== undefined && shapeOf(n) === 'container';
181
+ };
182
+ // A parent chain that cycles would leave its members in neither pass.
183
+ const chainTerminates = (id) => {
184
+ const seen = new Set();
185
+ let cur = id;
186
+ while (cur !== undefined) {
187
+ if (seen.has(cur))
188
+ return false;
189
+ seen.add(cur);
190
+ const p = parentOf(byId.get(cur));
191
+ if (p === undefined || !byId.has(p))
192
+ return true;
193
+ cur = p;
194
+ }
195
+ return true;
196
+ };
197
+ // Container bodies are laid out inside their container, not in the scope's pass.
198
+ const childrenByParent = new Map();
199
+ for (const ln of allNodes) {
200
+ const n = byId.get(ln.id);
201
+ if (isSticky(n))
202
+ continue;
203
+ const pid = parentOf(n);
204
+ if (pid && pid !== n.id && byId.has(pid) && isContainerId(pid) && chainTerminates(n.id)) {
205
+ const group = childrenByParent.get(pid);
206
+ if (group)
207
+ group.push(ln);
208
+ else
209
+ childrenByParent.set(pid, [ln]);
210
+ }
211
+ }
212
+ const containerChildIds = new Set([...childrenByParent.values()].flat().map((n) => n.id));
213
+ const interiorEdgesByParent = new Map();
214
+ for (const e of allEdges) {
215
+ const sp = parentOf(byId.get(e.source));
216
+ const tp = parentOf(byId.get(e.target));
217
+ if (sp && sp === tp) {
218
+ const list = interiorEdgesByParent.get(sp) ?? interiorEdgesByParent.set(sp, []).get(sp);
219
+ list.push({ id: e.id, sources: [e.source], targets: [e.target] });
220
+ }
221
+ }
222
+ const tidyChildren = new Map();
223
+ for (const [pid, kids] of childrenByParent) {
224
+ tidyChildren.set(pid, kids.map((k) => ({
225
+ id: k.id,
226
+ width: k.width ?? DEFAULT_NODE_SIZE,
227
+ height: k.height ?? DEFAULT_NODE_SIZE,
228
+ isContainer: childrenByParent.has(k.id),
229
+ })));
230
+ }
231
+ const nested = new Map();
232
+ for (const containerId of [...childrenByParent.keys()].filter((id) => !containerChildIds.has(id))) {
233
+ for (const [id, upd] of tidyContainer(containerId, tidyChildren, interiorEdgesByParent))
234
+ nested.set(id, upd);
235
+ }
236
+ const layoutNodes = allNodes.filter((n) => !containerChildIds.has(n.id));
237
+ const layoutEdges = allEdges.filter((e) => !containerChildIds.has(e.source) && !containerChildIds.has(e.target));
238
+ for (const n of layoutNodes) {
239
+ const grown = nested.get(n.id);
240
+ if (grown?.width !== undefined && grown.height !== undefined) {
241
+ n.width = grown.width;
242
+ n.height = grown.height;
243
+ }
244
+ }
245
+ const positions = new Map();
246
+ if (layoutNodes.length > 0) {
247
+ // The leftmost real node is the anchor; `format` keeps it where it was.
248
+ const pool = layoutNodes.filter((n) => !isSticky(byId.get(n.id)));
249
+ const anchorPool = pool.length > 0 ? pool : layoutNodes;
250
+ const anchor = anchorPool.reduce((left, n) => (n.position.x < left.position.x ? n : left), anchorPool[0]);
251
+ const laid = layoutRoot(layoutNodes, layoutEdges, portResolver(definitions, (id) => byId.get(id)));
252
+ const placedAnchor = laid.find((n) => n.id === anchor.id);
253
+ const dx = ORIGIN.x - placedAnchor.position.x;
254
+ const dy = ORIGIN.y - placedAnchor.position.y;
255
+ for (const n of laid)
256
+ positions.set(n.id, { x: snapToGrid(n.position.x + dx), y: snapToGrid(n.position.y + dy) });
257
+ }
258
+ for (const n of nodes) {
259
+ if (isSticky(n))
260
+ continue;
261
+ const ui = { ...n.ui };
262
+ const pos = nested.get(n.id)?.position ?? positions.get(n.id);
263
+ if (pos)
264
+ ui.position = { x: pos.x, y: pos.y };
265
+ const upd = nested.get(n.id);
266
+ ui.size = upd?.width !== undefined && upd.height !== undefined ? { width: upd.width, height: upd.height } : sizeForShape(shapeOf(n));
267
+ // Compile chose this position, not the author: merge places the node if an edit
268
+ // added it, and decompile writes no `.layout()` entry for it while it is there.
269
+ n.ui = markCompilePlaced(ui);
270
+ }
271
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The layout `uip maestro flow format` computes, as plain data in and out: a copy of
3
+ * the parts of flow-workbench `@uipath/flow-core` `layout/` (0.172.x) that `format`
4
+ * runs, made synchronous (`core/elk-module.ts`).
5
+ *
6
+ * The SDK may not import `@uipath/flow-core` (lint FB016), so this is a copy, like the
7
+ * collision code in `node-placement.ts`. What is copied, from `packages/flow-core/src/layout/`:
8
+ *
9
+ * - `types.ts`: `GRID_SPACING`, `snapToGrid`, `resolveWidth`/`resolveHeight`,
10
+ * `DEFAULT_LAYOUT_OPTIONS` (layered, RIGHT, 96 node and layer spacing, crossing
11
+ * minimisation on);
12
+ * - `container-tidy-defaults.ts`: `snapUpToGrid`, the 32/64 interior spacing and
13
+ * `defaultContainerFit` (560 × 320, padding 96/48/144/144);
14
+ * - `elk-graph.ts`: `computeElkPortIndices`, `buildElkPorts` and the edge clearances;
15
+ * - `artifact-grouping.ts`: `analyzeArtifactConnections`, `buildArtifactParentWrappers`;
16
+ * - `elk-layout-engine.ts`: `ElkLayoutEngine.layout` (the root pass, with artifact
17
+ * wrappers frozen first), less the waypoints and metrics `format` does not read, and
18
+ * with one fix: an edge from a wrapped parent's undeclared handle (see `buildEdges`);
19
+ * - `container-tidy.ts`: `tidyContainerHierarchy` with what `format` passes: no
20
+ * `hooks`, no `overhangById`, no `portsById`.
21
+ *
22
+ * Example: three 96 × 96 nodes `a → b → c` come out on one row 192 px apart
23
+ * (`x` 16, 208, 400 before the caller anchors them), because the layer gap is 96.
24
+ *
25
+ * `tests/auto-layout.test.ts` runs these functions and the pinned flow-core's own
26
+ * (`typescript/package.json` devDependency) on the same graphs and requires the same
27
+ * answer. The CLI takes flow-core `^0.172.0` and moves on its own, so the daily
28
+ * `vendor-drift` job also runs that comparison against `@uipath/flow-core@latest`
29
+ * (`FLOW_CORE_LAYOUT`), and opens an issue when the published layout moves.
30
+ */
31
+ import { type ElkEdge } from './elk-module.js';
32
+ /** The canvas grid increment, in px. */
33
+ export declare const GRID_SPACING = 16;
34
+ /** Snap to the nearest grid increment. */
35
+ export declare function snapToGrid(value: number, gridSize?: number): number;
36
+ /** Round up to the next grid increment. */
37
+ export declare function snapUpToGrid(value: number, gridSize?: number): number;
38
+ /** A node handed to the root pass. `position` is ignored by ELK and returned replaced. */
39
+ export interface LayoutNode {
40
+ id: string;
41
+ position: {
42
+ x: number;
43
+ y: number;
44
+ };
45
+ width?: number;
46
+ height?: number;
47
+ }
48
+ /** An edge between two layout nodes, by handle. */
49
+ export interface LayoutEdge {
50
+ id: string;
51
+ source: string;
52
+ target: string;
53
+ sourceHandle?: string | null;
54
+ targetHandle?: string | null;
55
+ }
56
+ /** A port side, in ELK's terms. */
57
+ export type ElkPortSide = 'NORTH' | 'SOUTH' | 'EAST' | 'WEST';
58
+ /** A node's handle as a port: its side and its visual order on that side. */
59
+ export interface PortInfo {
60
+ id: string;
61
+ side: ElkPortSide;
62
+ index: number;
63
+ handleType?: 'input' | 'output' | 'artifact';
64
+ }
65
+ /** flow-core `DEFAULT_INTERIOR_NODE_SPACING` / `DEFAULT_INTERIOR_LAYER_SPACING`. */
66
+ export declare const INTERIOR_NODE_SPACING = 32;
67
+ export declare const INTERIOR_LAYER_SPACING = 64;
68
+ /** The default node box (flow-core `resolveWidth`/`resolveHeight`). */
69
+ export declare const DEFAULT_NODE_SIZE = 96;
70
+ /** Container chrome: the padding and minimum size ELK gets (flow-core `TidyFitGeometry`). */
71
+ export interface TidyFitGeometry {
72
+ minWidth: number;
73
+ minHeight: number;
74
+ paddingTop: number;
75
+ paddingBottom: number;
76
+ paddingLeft: number;
77
+ paddingRight: number;
78
+ elkPadding: string;
79
+ }
80
+ /** flow-core `defaultContainerFit()`. */
81
+ export declare function defaultContainerFit(): TidyFitGeometry;
82
+ /**
83
+ * flow-core `ElkLayoutEngine.layout` with `DEFAULT_LAYOUT_OPTIONS`: each node's new
84
+ * top-left, snapped to the grid, in ELK's frame (the caller anchors it).
85
+ *
86
+ * @param nodes - The nodes to lay out, in model order (ELK keeps it where it can).
87
+ * @param edges - The edges between them.
88
+ * @param resolveNodePorts - Each node's handles as ports.
89
+ * @returns The nodes with their new `position`.
90
+ */
91
+ export declare function layoutRoot(nodes: LayoutNode[], edges: LayoutEdge[], resolveNodePorts: (node: LayoutNode) => PortInfo[]): LayoutNode[];
92
+ /** One interior child, with its real size and whether it is a container itself. */
93
+ export interface TidyChild {
94
+ id: string;
95
+ width: number;
96
+ height: number;
97
+ isContainer: boolean;
98
+ }
99
+ /** A node's new position (relative to its container) and/or size. */
100
+ export interface TidyNodeUpdate {
101
+ position?: {
102
+ x: number;
103
+ y: number;
104
+ };
105
+ width?: number;
106
+ height?: number;
107
+ }
108
+ /**
109
+ * flow-core `tidyContainerHierarchy` as `format` calls it: lay out a container's
110
+ * interior (nested containers recurse), size every container to fit, and centre each body.
111
+ *
112
+ * @param containerId - The top-level container.
113
+ * @param childrenByParent - Each container's direct children.
114
+ * @param edgesByParent - Each container's interior edges (node ids, no ports).
115
+ * @returns Each node's new relative position and size, by id.
116
+ */
117
+ export declare function tidyContainer(containerId: string, childrenByParent: ReadonlyMap<string, TidyChild[]>, edgesByParent: ReadonlyMap<string, ElkEdge[]>): Map<string, TidyNodeUpdate>;