@uipath/maestro-builder-sdk 6.31.6 → 6.31.7

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,90 @@
1
+ /**
2
+ * merge-embedded-agent — keep an EMBEDDED agent embedded when a brownfield edit
3
+ * touches it (flow-builder-sdk#1220).
4
+ *
5
+ * An agent node stores its configuration (prompts, model, run settings) in one
6
+ * of two places:
7
+ *
8
+ * - embedded: on the node itself (`inputs.systemPrompt`, `inputs.model`, …).
9
+ * Older Studio Web saves, and any save with the self-contained flag on.
10
+ * - shell: the node keeps only its structural keys (`source`,
11
+ * `agentInputVariables`, `agentOutputVariables`, …) and the configuration
12
+ * lives in `<source>/agent.json`.
13
+ *
14
+ * The runtime and Studio Web pick the copy by `hasEmbeddedAgentContent`
15
+ * (`@uipath/flow-schema`): any prompt string on the node makes the node win and
16
+ * the sidecar is never read. Since #927 compile emits only shells. So when an
17
+ * edit changed an embedded agent's node (an input, an output, the label), the
18
+ * old merge took the edited SHELL node whole, and the agent's storage form
19
+ * flipped: the runtime then read the project's `<source>/agent.json`, which an
20
+ * embedded node had been shadowing and which is often stale. On corpus fixture
21
+ * `01-agent-input-binding` that silently changed the model from
22
+ * `gpt-4o-2024-11-20` to `gpt-5.4` and emptied the system prompt.
23
+ *
24
+ * The rule here is the same delta rule merge applies everywhere: the original
25
+ * node, plus what the author changed (`edited` minus `baseline`). The baseline
26
+ * and the edited node are both shells, so the configuration keys never appear in
27
+ * that delta and stay exactly as the original has them. Only the structural
28
+ * keys the author changed are taken from the edit. Without a baseline (a two-way
29
+ * merge), the edited node is taken, and every key the original has that the
30
+ * shell does not (the configuration) is put back.
31
+ *
32
+ * A prompt or model edit in the source does NOT reach an embedded agent: it
33
+ * lands in the edited `<source>/agent.json`, which the embedded node shadows.
34
+ * Merge reports that ({@link embeddedAgentFindings}), and refuses under
35
+ * `strict` any merged node that would no longer carry the original's embedded
36
+ * configuration, or whose kept prompt names an input the node no longer
37
+ * declares.
38
+ */
39
+ import type { MergeFinding } from './merge-roundtrip.js';
40
+ interface AgentNode {
41
+ id: string;
42
+ type: string;
43
+ inputs?: Record<string, unknown>;
44
+ [k: string]: unknown;
45
+ }
46
+ /**
47
+ * Whether `node` is an agent whose configuration is on the node, by
48
+ * `@uipath/flow-schema`'s `hasEmbeddedAgentContent`: a prompt string, or, on
49
+ * the voice node only, a `voice` settings object.
50
+ */
51
+ export declare function isEmbeddedAgent(node: {
52
+ type?: string;
53
+ inputs?: Record<string, unknown>;
54
+ } | undefined): boolean;
55
+ /**
56
+ * The merged node for an embedded original whose edited node is a shell: the
57
+ * original, plus the author's change to the node.
58
+ *
59
+ * @param orig - The original node (embedded).
60
+ * @param edited - The edited compile's node (a shell).
61
+ * @param base - The baseline compile's node, when the merge has a baseline.
62
+ * @returns The original node with the edit's changed structural inputs and changed top-level keys applied.
63
+ */
64
+ export declare function mergeEmbeddedAgentNode<N extends AgentNode>(orig: N, edited: N, base: N | undefined): N;
65
+ /**
66
+ * What a merged embedded agent needs said, per original embedded agent (root
67
+ * and subflows, matched by scope and node id):
68
+ *
69
+ * - `agent-storage` (refuses): the merged node no longer carries the original's
70
+ * embedded configuration, so the runtime would read `<source>/agent.json`
71
+ * instead — a model or prompt change the author never wrote.
72
+ * - `agent-prompt-input` (refuses): a kept prompt names `{{input.X}}` but the
73
+ * merged node declares no input `X` (the edit removed or renamed it), so the
74
+ * prompt would render the placeholder empty.
75
+ * - `agent-embedded-edit` (warns): the author edited the node; merge applied the
76
+ * node change and kept the original's prompts, model and settings, so a
77
+ * prompt or model edit in the source did not reach this agent.
78
+ *
79
+ * @param hasBaseline - Whether the merge had a baseline. Without one, merge
80
+ * cannot tell the author's change from the round trip's, so the
81
+ * `agent-embedded-edit` warning says the node may have been edited.
82
+ * @param scopes - Per scope (`''` = root, otherwise the subflow key): the original, merged and edited-node-ids.
83
+ */
84
+ export declare function embeddedAgentFindings(hasBaseline: boolean, scopes: Array<{
85
+ scope: string;
86
+ original: AgentNode[];
87
+ merged: AgentNode[];
88
+ edited: ReadonlySet<string>;
89
+ }>): MergeFinding[];
90
+ export {};
@@ -0,0 +1,260 @@
1
+ /**
2
+ * merge-embedded-agent — keep an EMBEDDED agent embedded when a brownfield edit
3
+ * touches it (flow-builder-sdk#1220).
4
+ *
5
+ * An agent node stores its configuration (prompts, model, run settings) in one
6
+ * of two places:
7
+ *
8
+ * - embedded: on the node itself (`inputs.systemPrompt`, `inputs.model`, …).
9
+ * Older Studio Web saves, and any save with the self-contained flag on.
10
+ * - shell: the node keeps only its structural keys (`source`,
11
+ * `agentInputVariables`, `agentOutputVariables`, …) and the configuration
12
+ * lives in `<source>/agent.json`.
13
+ *
14
+ * The runtime and Studio Web pick the copy by `hasEmbeddedAgentContent`
15
+ * (`@uipath/flow-schema`): any prompt string on the node makes the node win and
16
+ * the sidecar is never read. Since #927 compile emits only shells. So when an
17
+ * edit changed an embedded agent's node (an input, an output, the label), the
18
+ * old merge took the edited SHELL node whole, and the agent's storage form
19
+ * flipped: the runtime then read the project's `<source>/agent.json`, which an
20
+ * embedded node had been shadowing and which is often stale. On corpus fixture
21
+ * `01-agent-input-binding` that silently changed the model from
22
+ * `gpt-4o-2024-11-20` to `gpt-5.4` and emptied the system prompt.
23
+ *
24
+ * The rule here is the same delta rule merge applies everywhere: the original
25
+ * node, plus what the author changed (`edited` minus `baseline`). The baseline
26
+ * and the edited node are both shells, so the configuration keys never appear in
27
+ * that delta and stay exactly as the original has them. Only the structural
28
+ * keys the author changed are taken from the edit. Without a baseline (a two-way
29
+ * merge), the edited node is taken, and every key the original has that the
30
+ * shell does not (the configuration) is put back.
31
+ *
32
+ * A prompt or model edit in the source does NOT reach an embedded agent: it
33
+ * lands in the edited `<source>/agent.json`, which the embedded node shadows.
34
+ * Merge reports that ({@link embeddedAgentFindings}), and refuses under
35
+ * `strict` any merged node that would no longer carry the original's embedded
36
+ * configuration, or whose kept prompt names an input the node no longer
37
+ * declares.
38
+ */
39
+ import { jsonClone } from './core/json-clone.js';
40
+ /**
41
+ * The agent node types whose configuration can live on the node or in
42
+ * `<source>/agent.json`, and whether a `voice` settings object on the node
43
+ * counts as embedded configuration (flow-schema counts it on the voice node only).
44
+ */
45
+ const SIDECAR_AGENT_TYPES = new Map([
46
+ ['uipath.agent.autonomous', { voiceSettings: false }],
47
+ ['uipath.agent.conversational', { voiceSettings: false }],
48
+ ['uipath.agent.voice', { voiceSettings: true }],
49
+ ]);
50
+ /**
51
+ * The agent-node inputs that are flow-level and survive Studio Web's shell
52
+ * strip (`STRUCTURAL_AGENT_INPUT_KEYS`, `@uipath/flow-schema`). Every other
53
+ * input of an embedded node is agent configuration.
54
+ */
55
+ const STRUCTURAL_AGENT_INPUT_KEYS = new Set([
56
+ 'source',
57
+ 'agentInputVariables',
58
+ 'agentOutputVariables',
59
+ 'conversationalAgentSettings',
60
+ 'callContext',
61
+ ]);
62
+ /** The descriptor arrays, merged entry by entry (keyed by `id`) rather than whole. */
63
+ const VARIABLE_ARRAY_KEYS = new Set(['agentInputVariables', 'agentOutputVariables']);
64
+ /** Node keys the delta never takes from the edit: identity, and the position merge re-attaches separately. */
65
+ const NODE_KEYS_NOT_IN_DELTA = new Set(['id', 'ui', 'inputs']);
66
+ /**
67
+ * Whether `node` is an agent whose configuration is on the node, by
68
+ * `@uipath/flow-schema`'s `hasEmbeddedAgentContent`: a prompt string, or, on
69
+ * the voice node only, a `voice` settings object.
70
+ */
71
+ export function isEmbeddedAgent(node) {
72
+ const kind = node?.type === undefined ? undefined : SIDECAR_AGENT_TYPES.get(node.type);
73
+ if (!node || kind === undefined)
74
+ return false;
75
+ const i = node.inputs ?? {};
76
+ if (typeof i.systemPrompt === 'string' || typeof i.userPrompt === 'string')
77
+ return true;
78
+ return kind.voiceSettings && typeof i.voice === 'object' && i.voice !== null;
79
+ }
80
+ /** The original's agent-configuration inputs: everything but the structural keys. */
81
+ function configInputs(node) {
82
+ return Object.fromEntries(Object.entries(node.inputs ?? {}).filter(([k]) => !STRUCTURAL_AGENT_INPUT_KEYS.has(k)));
83
+ }
84
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
85
+ function entriesById(v) {
86
+ return new Map((Array.isArray(v) ? v : []).map((e) => [e?.id, e]));
87
+ }
88
+ /**
89
+ * A descriptor array (`agentInputVariables` / `agentOutputVariables`) in the
90
+ * edited order. An entry the author did not change (equal to its baseline
91
+ * entry) is the original's, so it keeps what only the original carries (an
92
+ * embedded entry's `description`, its binding spelling). A changed or added
93
+ * entry is the edited one, over the original entry's extra fields, except a
94
+ * `description` when the edit rebound the entry: the original's describes the
95
+ * old binding (for instance "Bound from $vars.prep1.output.ticket").
96
+ */
97
+ function mergeVariableArray(orig, edited, base, hasBaseline) {
98
+ if (!Array.isArray(edited))
99
+ return jsonClone(edited);
100
+ const o = entriesById(orig);
101
+ const b = entriesById(base);
102
+ return edited.map((e) => {
103
+ const oe = o.get(e?.id);
104
+ if (oe === undefined)
105
+ return jsonClone(e);
106
+ const be = b.get(e?.id);
107
+ if (hasBaseline && be !== undefined && same(be, e))
108
+ return jsonClone(oe);
109
+ // Two-way: an entry that matches the original's apart from the binding's
110
+ // spelling and the fields compile does not write is the original's.
111
+ if (!hasBaseline && Object.keys(e ?? {}).every((k) => (k === 'binding'
112
+ ? bindingText(oe.binding) === bindingText(e.binding)
113
+ : same(oe[k], e[k]))))
114
+ return jsonClone(oe);
115
+ const merged = { ...jsonClone(oe), ...jsonClone(e) };
116
+ // The baseline and the edit spell bindings the same way (both compiled); the
117
+ // original may not (`=$vars.x` vs `=js:$vars.x`), so compare it by text.
118
+ const rebound = hasBaseline && be !== undefined
119
+ ? !same(be.binding, e.binding)
120
+ : bindingText(oe.binding) !== bindingText(e.binding);
121
+ if (rebound && !('description' in e))
122
+ delete merged.description;
123
+ return merged;
124
+ });
125
+ }
126
+ /** A binding's expression text, without the `=` / `=js:` prefix, so two spellings of one binding compare equal. */
127
+ function bindingText(binding) {
128
+ return typeof binding === 'string' ? binding.replace(/^=(js:)?/, '') : binding;
129
+ }
130
+ /**
131
+ * The merged node for an embedded original whose edited node is a shell: the
132
+ * original, plus the author's change to the node.
133
+ *
134
+ * @param orig - The original node (embedded).
135
+ * @param edited - The edited compile's node (a shell).
136
+ * @param base - The baseline compile's node, when the merge has a baseline.
137
+ * @returns The original node with the edit's changed structural inputs and changed top-level keys applied.
138
+ */
139
+ export function mergeEmbeddedAgentNode(orig, edited, base) {
140
+ if (base === undefined) {
141
+ // Two-way: the edited node, with the original's configuration put back.
142
+ const out = jsonClone(edited);
143
+ const inputs = { ...(out.inputs ?? {}) };
144
+ for (const k of VARIABLE_ARRAY_KEYS) {
145
+ if (k in inputs)
146
+ inputs[k] = mergeVariableArray(orig.inputs?.[k], inputs[k], undefined, false);
147
+ }
148
+ out.inputs = { ...inputs, ...jsonClone(configInputs(orig)) };
149
+ return out;
150
+ }
151
+ const out = jsonClone(orig);
152
+ // Top-level keys (outputs, display, typeVersion, parentId, …): the edit's
153
+ // value where the author changed it.
154
+ for (const k of new Set([...Object.keys(base), ...Object.keys(edited)])) {
155
+ if (NODE_KEYS_NOT_IN_DELTA.has(k) || same(base[k], edited[k]))
156
+ continue;
157
+ if (k in edited)
158
+ out[k] = jsonClone(edited[k]);
159
+ else
160
+ delete out[k];
161
+ }
162
+ // Inputs: the same, key by key. The configuration keys are on neither shell,
163
+ // so they never differ here and stay the original's.
164
+ const bi = base.inputs ?? {};
165
+ const ei = edited.inputs ?? {};
166
+ const inputs = { ...(out.inputs ?? {}) };
167
+ for (const k of new Set([...Object.keys(bi), ...Object.keys(ei)])) {
168
+ if (same(bi[k], ei[k]))
169
+ continue;
170
+ if (!(k in ei))
171
+ delete inputs[k];
172
+ else if (VARIABLE_ARRAY_KEYS.has(k))
173
+ inputs[k] = mergeVariableArray(orig.inputs?.[k], ei[k], bi[k], true);
174
+ else
175
+ inputs[k] = jsonClone(ei[k]);
176
+ }
177
+ out.inputs = inputs;
178
+ return out;
179
+ }
180
+ /** `{{input.<name>}}` placeholders in a prompt string. */
181
+ function promptInputs(prompt) {
182
+ if (typeof prompt !== 'string')
183
+ return [];
184
+ return [...prompt.matchAll(/\{\{\s*input\.([A-Za-z_$][\w$]*)\s*\}\}/g)].map((m) => m[1]);
185
+ }
186
+ /**
187
+ * What a merged embedded agent needs said, per original embedded agent (root
188
+ * and subflows, matched by scope and node id):
189
+ *
190
+ * - `agent-storage` (refuses): the merged node no longer carries the original's
191
+ * embedded configuration, so the runtime would read `<source>/agent.json`
192
+ * instead — a model or prompt change the author never wrote.
193
+ * - `agent-prompt-input` (refuses): a kept prompt names `{{input.X}}` but the
194
+ * merged node declares no input `X` (the edit removed or renamed it), so the
195
+ * prompt would render the placeholder empty.
196
+ * - `agent-embedded-edit` (warns): the author edited the node; merge applied the
197
+ * node change and kept the original's prompts, model and settings, so a
198
+ * prompt or model edit in the source did not reach this agent.
199
+ *
200
+ * @param hasBaseline - Whether the merge had a baseline. Without one, merge
201
+ * cannot tell the author's change from the round trip's, so the
202
+ * `agent-embedded-edit` warning says the node may have been edited.
203
+ * @param scopes - Per scope (`''` = root, otherwise the subflow key): the original, merged and edited-node-ids.
204
+ */
205
+ export function embeddedAgentFindings(hasBaseline, scopes) {
206
+ const out = [];
207
+ for (const { scope, original, merged, edited } of scopes) {
208
+ const where = scope === '' ? '' : ` in subflow "${scope}"`;
209
+ const mergedById = new Map(merged.map((n) => [n.id, n]));
210
+ for (const o of original) {
211
+ if (!isEmbeddedAgent(o))
212
+ continue;
213
+ const m = mergedById.get(o.id);
214
+ if (m === undefined || m.type !== o.type)
215
+ continue; // deleted or replaced by the author
216
+ const lost = Object.entries(configInputs(o))
217
+ .filter(([k, v]) => !same(m.inputs?.[k], v))
218
+ .map(([k]) => k);
219
+ if (!isEmbeddedAgent(m) || lost.length > 0) {
220
+ out.push({
221
+ kind: 'agent-storage', refuses: true,
222
+ message: `agent "${o.id}"${where} carries its configuration on the node (embedded), but the merged node `
223
+ + `${isEmbeddedAgent(m) ? 'changes' : 'drops'} ${lost.map((k) => `inputs.${k}`).join(', ') || 'its prompts'}, `
224
+ + 'so the agent would run a configuration the author did not write (flow-builder-sdk#1220).',
225
+ });
226
+ continue;
227
+ }
228
+ const declared = new Set((Array.isArray(m.inputs?.agentInputVariables) ? m.inputs.agentInputVariables : [])
229
+ .map((v) => v?.id)
230
+ .filter((id) => typeof id === 'string'));
231
+ const missing = [...new Set([...promptInputs(m.inputs?.systemPrompt), ...promptInputs(m.inputs?.userPrompt)])]
232
+ .filter((name) => !declared.has(name));
233
+ const origDeclared = new Set((Array.isArray(o.inputs?.agentInputVariables) ? o.inputs.agentInputVariables : []).map((v) => v?.id));
234
+ // A placeholder the original already left undeclared is not the merge's doing.
235
+ const newlyMissing = missing.filter((name) => origDeclared.has(name));
236
+ if (newlyMissing.length > 0) {
237
+ out.push({
238
+ kind: 'agent-prompt-input', refuses: true,
239
+ message: `agent "${o.id}"${where} keeps its original embedded prompt, which uses `
240
+ + `${newlyMissing.map((n) => `{{input.${n}}}`).join(', ')}, but the edit removed or renamed that input. `
241
+ + 'An embedded agent\'s prompts are not taken from the source; change the prompt on the canvas too.',
242
+ });
243
+ }
244
+ if (edited.has(o.id)) {
245
+ out.push({
246
+ kind: 'agent-embedded-edit', refuses: false,
247
+ message: `agent "${o.id}"${where} carries its prompts, model and settings on the node (embedded). Merge applied `
248
+ + (hasBaseline
249
+ ? 'your change to the node'
250
+ : 'the edited node\'s inputs and outputs (with no baseline it cannot tell your change from the round trip\'s, '
251
+ + 'so this node may not have been edited)')
252
+ + ' and kept those as the original has them. A prompt, model or settings edit in the '
253
+ + 'source is not carried for an embedded agent (it would land in <source>/agent.json, which the node '
254
+ + 'shadows); make that change on the canvas.',
255
+ });
256
+ }
257
+ }
258
+ }
259
+ return out;
260
+ }
@@ -33,12 +33,13 @@ interface RtFlow {
33
33
  /** One difference, or one inconsistency in the merged file. */
34
34
  export interface MergeFinding {
35
35
  /** Which comparison found it. */
36
- kind: 'node-removed' | 'node-added' | 'node-type' | 'implicit-end' | 'edge-handle-renamed' | 'edge-rewired' | 'edge-removed' | 'edge-added' | 'handle-ids' | 'binding-removed' | 'binding-added' | 'undeclared-handle' | 'duplicate-binding' | 'agent-sidecar';
36
+ kind: 'node-removed' | 'node-added' | 'node-type' | 'implicit-end' | 'edge-handle-renamed' | 'edge-rewired' | 'edge-removed' | 'edge-added' | 'handle-ids' | 'binding-removed' | 'binding-added' | 'undeclared-handle' | 'duplicate-binding' | 'agent-sidecar' | 'agent-storage' | 'agent-prompt-input' | 'agent-embedded-edit';
37
37
  /** The finding, phrased for the author, naming the node, edge or row. */
38
38
  message: string;
39
39
  /**
40
40
  * Whether `strict` refuses the merge over it. False for `implicit-end` and
41
- * `binding-removed`, which leave the merged file doing what the original does.
41
+ * `binding-removed`, which leave the merged file doing what the original does,
42
+ * and for `agent-embedded-edit`, which reports what merge did not carry.
42
43
  */
43
44
  refuses: boolean;
44
45
  }
@@ -65,6 +65,7 @@
65
65
  * Pure: the caller reads the files ({@link SidecarSet}) and writes the result.
66
66
  */
67
67
  import { jsonClone } from './core/json-clone.js';
68
+ import { isEmbeddedAgent } from './merge-embedded-agent.js';
68
69
  /** The `{ path, json }` list `serializeWithSidecars` returns, as a {@link SidecarSet}. */
69
70
  export function sidecarSetFromList(list) {
70
71
  return (source) => {
@@ -258,11 +259,13 @@ function carryResources(source, files, flows) {
258
259
  }
259
260
  return { writes, removals, unmatched };
260
261
  }
261
- /** Whether a node carries its prompts itself, so its `agent.json` is not what runs (see decompile's `sidecarAgentInputs`). */
262
- function carriesOwnPrompts(node) {
263
- const i = node?.inputs ?? {};
264
- return typeof i.systemPrompt === 'string' || typeof i.userPrompt === 'string';
265
- }
262
+ /**
263
+ * The `agent.json` keys an embedded node carries itself, in its descriptor
264
+ * arrays (`agentInputVariables` / `agentOutputVariables`). An input or output
265
+ * edit changes them in the compiled `agent.json` and on the node; merge applies
266
+ * the node change (#1220), so their delta is not an edit the node shadows.
267
+ */
268
+ const SCHEMA_ON_THE_NODE = /^(inputSchema|outputSchema)(\.|$)/;
266
269
  const finding = (message) => ({ kind: 'agent-sidecar', message, refuses: true });
267
270
  /**
268
271
  * The sidecar files the merged flow needs written beside it, and what merge could
@@ -310,12 +313,20 @@ export function mergeAgentSidecars(merged, sources, flows) {
310
313
  const changed = [];
311
314
  const next = mergeJson3(orig[agentPath], base[agentPath], edited[agentPath], changed);
312
315
  if (changed.length > 0) {
313
- // `node` is the merged node: the original's when merge kept it, the
314
- // edited shell when the author changed the node too.
315
- if (carriesOwnPrompts(node)) {
316
- findings.push(finding(`agent "${node.id}": its ${agentPath} was edited (${changed.join(', ')}), but the original node carries its `
317
- + 'own prompts, which are read before agent.json, so the edit would not reach the agent. The change is NOT '
318
- + 'carried. Make the change on the canvas, or save the agent with its prompts in agent.json only.'));
316
+ // `node` is the merged node: the original's when merge kept it; when the
317
+ // author changed the node too, the edited shell, or, for an embedded
318
+ // original, the original plus that change (#1220), still embedded.
319
+ // An embedded node (prompts or voice settings on the node) is read before
320
+ // agent.json, so an agent.json edit does not reach it. Its input and
321
+ // output schemas are the exception: the node's descriptor arrays carry
322
+ // them, and merge applied that node change already.
323
+ if (isEmbeddedAgent(node)) {
324
+ const shadowed = changed.filter((p) => !SCHEMA_ON_THE_NODE.test(p));
325
+ if (shadowed.length > 0) {
326
+ findings.push(finding(`agent "${node.id}": its ${agentPath} was edited (${shadowed.join(', ')}), but the original node carries its `
327
+ + 'own configuration (embedded), which is read before agent.json, so the edit would not reach the agent. The '
328
+ + 'change is NOT carried. Make the change on the canvas, or save the agent with its prompts in agent.json only.'));
329
+ }
319
330
  }
320
331
  else if (stable(next) !== stable(orig[agentPath])) {
321
332
  writes.push({ path: agentPath, json: next, carried: changed });
package/dist/merge.d.ts CHANGED
@@ -75,6 +75,12 @@
75
75
  * `warn`, and `strict` refuses the merge over it; the merged file is also
76
76
  * checked for an edge that leaves a handle its kept source node does not
77
77
  * declare. See merge-roundtrip.ts.
78
+ *
79
+ * ## An embedded agent stays embedded
80
+ *
81
+ * An agent node whose original carries its prompts and model on the node is
82
+ * never replaced by the edited compile's shell node: the merged node is the
83
+ * original plus the author's change to the node. See merge-embedded-agent.ts.
78
84
  */
79
85
  import { type MergeSidecarSources, type SidecarWrite } from './merge-sidecars.js';
80
86
  export { MergeRefusedError, type MergeFinding } from './merge-roundtrip.js';
@@ -142,7 +148,12 @@ export interface MergeOptions {
142
148
  * - an edited node whose `typeVersion` differs from the original node with
143
149
  * the same id, because the edit took the SDK's version;
144
150
  * - a merged edge whose `sourcePort` is not a handle its source node declares,
145
- * and two merged binding rows for the same resource property.
151
+ * and two merged binding rows for the same resource property;
152
+ * - an edited agent whose original carries its prompts and model on the node
153
+ * (embedded): merge keeps them and applies only the node change, so a prompt
154
+ * or model edit in the source is not carried. Under `strict` it refuses a
155
+ * merged node that would drop or change that embedded configuration, or
156
+ * whose kept prompt uses an input the edit removed (flow-builder-sdk#1220).
146
157
  */
147
158
  warn?: (message: string) => void;
148
159
  /**
package/dist/merge.js CHANGED
@@ -75,6 +75,12 @@
75
75
  * `warn`, and `strict` refuses the merge over it; the merged file is also
76
76
  * checked for an edge that leaves a handle its kept source node does not
77
77
  * declare. See merge-roundtrip.ts.
78
+ *
79
+ * ## An embedded agent stays embedded
80
+ *
81
+ * An agent node whose original carries its prompts and model on the node is
82
+ * never replaced by the edited compile's shell node: the merged node is the
83
+ * original plus the author's change to the node. See merge-embedded-agent.ts.
78
84
  */
79
85
  import { bindingIdentity } from './core/binding-identity.js';
80
86
  import { jsonClone } from './core/json-clone.js';
@@ -82,6 +88,7 @@ import { DEFINITIONLESS_NODE_TYPES } from './core/definitionless-node-types.js';
82
88
  import { MergeRefusedError, mergedFileFindings, roundTripFindings } from './merge-roundtrip.js';
83
89
  import { placeAddedNodes } from './node-placement.js';
84
90
  import { mergeAgentSidecars } from './merge-sidecars.js';
91
+ import { embeddedAgentFindings, isEmbeddedAgent, mergeEmbeddedAgentNode } from './merge-embedded-agent.js';
85
92
  export { MergeRefusedError } from './merge-roundtrip.js';
86
93
  function byId(nodes) {
87
94
  return new Map((nodes ?? []).map((n) => [n.id, n]));
@@ -218,6 +225,8 @@ function mergeFlowFindings(original, edited, options) {
218
225
  // references are the original's now, not the recompile's
219
226
  // (see withoutRowsMintedForKeptNodes).
220
227
  const keptFromOriginal = new Set();
228
+ /** Root node ids taken from `edited` that the original also has (the author edited them). */
229
+ const editedIds = new Set();
221
230
  for (const e of edited.nodes ?? []) {
222
231
  const orig = origNodes.get(e.id);
223
232
  if (isKept(e, origNodes, baseNodes, options.baseline) && orig) {
@@ -228,9 +237,17 @@ function mergeFlowFindings(original, edited, options) {
228
237
  }
229
238
  else {
230
239
  // Added or edited → take the edited node, re-attaching original layout.
231
- finalNodes.push(withOriginalSource(withOriginalLayout(jsonClone(e), orig, layoutNodesOf(original)), orig, baseNodes.get(e.id)));
232
- if (orig && orig.type === e.type && orig.typeVersion !== e.typeVersion) {
233
- options.warn?.(`node ${JSON.stringify(e.id)} moved ${e.type}@${String(orig.typeVersion)} -> ${String(e.typeVersion)} `
240
+ // An EMBEDDED agent stays embedded: the original plus the author's change
241
+ // to the node, never the edited shell whole (#1220, merge-embedded-agent.ts).
242
+ const node = keepsEmbeddedAgent(orig, e) ? mergeEmbeddedAgentNode(orig, e, baseNodes.get(e.id)) : jsonClone(e);
243
+ const placed = withOriginalSource(withOriginalLayout(node, orig, layoutNodesOf(original)), orig, baseNodes.get(e.id));
244
+ finalNodes.push(placed);
245
+ // Without a baseline every node lands here; count one as edited only
246
+ // when the merged node differs from the original.
247
+ if (orig && (options.baseline || !sameApartFromUi(placed, orig)))
248
+ editedIds.add(e.id);
249
+ if (orig && orig.type === node.type && orig.typeVersion !== node.typeVersion) {
250
+ options.warn?.(`node ${JSON.stringify(e.id)} moved ${e.type}@${String(orig.typeVersion)} -> ${String(node.typeVersion)} `
234
251
  + `(the edited source does not pin ${String(orig.typeVersion)}; keep the { version } pin, `
235
252
  + `with .definitions([...]) when the SDK does not bundle that version).`);
236
253
  }
@@ -242,6 +259,11 @@ function mergeFlowFindings(original, edited, options) {
242
259
  // ── Variables & subflows: the edited flow is authoritative (the user's graph). ──
243
260
  result.variables = jsonClone(edited.variables);
244
261
  result.subflows = jsonClone(edited.subflows ?? {});
262
+ // A subflow is taken from `edited` whole, so an embedded agent inside one
263
+ // would flip to the edited shell even with no edit. Keep it embedded too.
264
+ // This runs before the source fix-up below: it compares each node with the
265
+ // baseline to tell which ones the author edited.
266
+ const subflowEdited = keepEmbeddedAgentsInSubflows(original, result.subflows, options.baseline);
245
267
  if (options.baseline)
246
268
  keepOriginalSourcesInSubflows(original, result.subflows, options.baseline);
247
269
  const subflowScopes = Object.values(result.subflows);
@@ -296,9 +318,74 @@ function mergeFlowFindings(original, edited, options) {
296
318
  const findings = [
297
319
  ...(options.baseline ? roundTripFindings(original, options.baseline, result) : []),
298
320
  ...mergedFileFindings(result, original),
321
+ ...embeddedAgentFindings(!!options.baseline, [
322
+ { scope: '', original: original.nodes ?? [], merged: finalNodes, edited: editedIds },
323
+ ...Object.entries(original.subflows ?? {}).map(([key, sf]) => ({
324
+ scope: key,
325
+ original: sf?.nodes ?? [],
326
+ merged: result.subflows?.[key]?.nodes ?? [],
327
+ edited: subflowEdited.get(key) ?? new Set(),
328
+ })),
329
+ ]),
299
330
  ];
300
331
  return { result, findings };
301
332
  }
333
+ /**
334
+ * Whether the merged node for `orig` must be built from the original rather than
335
+ * taken from `edited`: the original is an embedded agent and the edited node is
336
+ * not (compile emits only shells), so taking it would move the agent's
337
+ * configuration from the node to `<source>/agent.json`.
338
+ */
339
+ function keepsEmbeddedAgent(orig, e) {
340
+ return orig !== undefined && orig.type === e.type && isEmbeddedAgent(orig) && !isEmbeddedAgent(e);
341
+ }
342
+ /**
343
+ * Whether two nodes are equal apart from their position (`ui`), which merge
344
+ * re-attaches separately, and apart from object key order (a two-way embedded
345
+ * merge puts the original's configuration keys after the edited shell's).
346
+ */
347
+ function sameApartFromUi(a, b) {
348
+ if (!a || !b)
349
+ return false;
350
+ const { ui: _a, ...ra } = a;
351
+ const { ui: _b, ...rb } = b;
352
+ return sortedJson(ra) === sortedJson(rb);
353
+ }
354
+ /** JSON text with object keys sorted, so two objects that differ only in key order compare equal. */
355
+ function sortedJson(v) {
356
+ return JSON.stringify(v, (_k, x) => (x && typeof x === 'object' && !Array.isArray(x)
357
+ ? Object.fromEntries(Object.entries(x).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
358
+ : x));
359
+ }
360
+ /**
361
+ * In each subflow taken from `edited`, replace an edited shell agent whose
362
+ * original (same subflow, same id) is embedded with
363
+ * {@link mergeEmbeddedAgentNode}'s result. Returns, per subflow, the ids of the
364
+ * original's nodes the author edited (differ from the baseline, or, without a
365
+ * baseline, merged differently from the original).
366
+ */
367
+ function keepEmbeddedAgentsInSubflows(original, subflows, baseline) {
368
+ const edited = new Map();
369
+ for (const [key, sf] of Object.entries(subflows)) {
370
+ const nodes = sf?.nodes;
371
+ if (!Array.isArray(nodes))
372
+ continue;
373
+ const origNodes = byId(original.subflows?.[key]?.nodes);
374
+ const baseNodes = byId(baseline?.subflows?.[key]?.nodes);
375
+ const ids = new Set();
376
+ for (let i = 0; i < nodes.length; i++) {
377
+ const e = nodes[i];
378
+ const orig = origNodes.get(e.id);
379
+ if (keepsEmbeddedAgent(orig, e))
380
+ nodes[i] = mergeEmbeddedAgentNode(orig, e, baseline ? baseNodes.get(e.id) : undefined);
381
+ const changed = baseline ? !nodeUnchanged(e, baseNodes.get(e.id)) : !sameApartFromUi(nodes[i], orig);
382
+ if (orig && changed)
383
+ ids.add(e.id);
384
+ }
385
+ edited.set(key, ids);
386
+ }
387
+ return edited;
388
+ }
302
389
  /**
303
390
  * The root edges with their edited ids, except that an edge with no id, or with
304
391
  * an id already used by a subflow edge or an earlier root edge, gets a fresh
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uipath/maestro-builder-sdk",
3
- "version": "6.31.6",
3
+ "version": "6.31.7",
4
4
  "description": "Build UiPath Flow, Case, and BPMN artifacts by writing TypeScript.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://docs.uipath.com/maestro",
@@ -93,5 +93,5 @@
93
93
  "@types/node": "^22.7.0",
94
94
  "esbuild": "^0.28.1"
95
95
  },
96
- "gitref": "77c2e7b1f09268cc94ee0f4b8f9d4ae80ba625ee"
96
+ "gitref": "99f1861467ac06fba1c816f365bf027e1e2c8e05"
97
97
  }