snow-flow 10.0.1-dev.452 → 10.0.1-dev.453

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
- "version": "10.0.1-dev.452",
3
+ "version": "10.0.1-dev.453",
4
4
  "name": "snow-flow",
5
5
  "description": "Snow-Flow - ServiceNow Multi-Agent Development Framework powered by AI",
6
6
  "license": "Elastic-2.0",
@@ -254,62 +254,78 @@ async function calculateInsertOrder(
254
254
  ): Promise<{ insertOrder: number; reorders: { flowLogicUpdates: any[]; actionUpdates: any[]; subflowUpdates: any[] } }> {
255
255
  var noReorders = { flowLogicUpdates: [], actionUpdates: [], subflowUpdates: [] };
256
256
 
257
- // Explicit order: bump elements at that position
258
- if (explicitOrder) {
259
- var reorders = await findElementsToReorder(client, flowId, explicitOrder);
260
- return { insertOrder: explicitOrder, reorders };
261
- }
262
-
263
- // No parent: append at end, no bumping needed
264
- if (!parentUiId) {
265
- var nextOrder = await getNextOrder(client, flowId);
266
- return { insertOrder: nextOrder, reorders: noReorders };
267
- }
268
-
269
- // Parent specified: find parent's order, then find max child order
270
- var parentSysId = '';
271
- var parentOrder = 0;
272
- try {
273
- var pResp = await client.get('/api/now/table/sys_hub_flow_logic', {
274
- params: {
275
- sysparm_query: 'flow=' + flowId + '^ui_unique_identifier=' + parentUiId,
276
- sysparm_fields: 'sys_id,order',
277
- sysparm_limit: 1
278
- }
279
- });
280
- var found = pResp.data.result?.[0];
281
- if (found) {
282
- parentSysId = found.sys_id;
283
- parentOrder = parseInt(found.order || '0', 10);
284
- }
285
- } catch (_) {}
286
-
287
- if (!parentSysId) {
288
- // Fallback: append at end
289
- var fallbackOrder = await getNextOrder(client, flowId);
290
- return { insertOrder: fallbackOrder, reorders: noReorders };
291
- }
292
-
293
- // Find max order of existing children of this parent
294
- var maxChildOrder = parentOrder;
295
- for (var table of ['sys_hub_action_instance', 'sys_hub_flow_logic', 'sys_hub_sub_flow_instance']) {
257
+ // ── Parent specified: ALWAYS compute global order from parent context ──
258
+ // Flow Designer uses GLOBAL ordering for ALL elements (not per-parent).
259
+ // When a parent is specified, any explicit "order" is ignored because callers
260
+ // typically pass a local/relative order (1, 2, 3) which would conflict with
261
+ // elements already at those global positions.
262
+ if (parentUiId) {
263
+ var parentSysId = '';
264
+ var parentOrder = 0;
296
265
  try {
297
- var cResp = await client.get('/api/now/table/' + table, {
266
+ var pResp = await client.get('/api/now/table/sys_hub_flow_logic', {
298
267
  params: {
299
- sysparm_query: 'flow=' + flowId + '^parent=' + parentSysId + '^ORDERBYDESCorder',
300
- sysparm_fields: 'order',
268
+ sysparm_query: 'flow=' + flowId + '^ui_unique_identifier=' + parentUiId,
269
+ sysparm_fields: 'sys_id,order',
301
270
  sysparm_limit: 1
302
271
  }
303
272
  });
304
- var childOrder = parseInt(cResp.data.result?.[0]?.order || '0', 10);
305
- if (childOrder > maxChildOrder) maxChildOrder = childOrder;
273
+ var found = pResp.data.result?.[0];
274
+ if (found) {
275
+ parentSysId = str(found.sys_id);
276
+ parentOrder = parseInt(str(found.order) || '0', 10);
277
+ }
306
278
  } catch (_) {}
279
+
280
+ if (!parentSysId) {
281
+ // Fallback: append at end
282
+ var fallbackOrder = await getNextOrder(client, flowId);
283
+ return { insertOrder: fallbackOrder, reorders: noReorders };
284
+ }
285
+
286
+ // Find max order of existing children of this parent (query by both sys_id and UUID)
287
+ var maxChildOrder = parentOrder;
288
+ for (var table of ['sys_hub_action_instance', 'sys_hub_flow_logic', 'sys_hub_sub_flow_instance']) {
289
+ try {
290
+ var cResp = await client.get('/api/now/table/' + table, {
291
+ params: {
292
+ sysparm_query: 'flow=' + flowId + '^parent=' + parentSysId + '^ORDERBYDESCorder',
293
+ sysparm_fields: 'order',
294
+ sysparm_limit: 1
295
+ }
296
+ });
297
+ var childOrder = parseInt(str(cResp.data.result?.[0]?.order) || '0', 10);
298
+ if (childOrder > maxChildOrder) maxChildOrder = childOrder;
299
+ } catch (_) {}
300
+ // Also try querying by parent UUID (GraphQL may store UUID in parent field)
301
+ try {
302
+ var cResp2 = await client.get('/api/now/table/' + table, {
303
+ params: {
304
+ sysparm_query: 'flow=' + flowId + '^parent=' + parentUiId + '^ORDERBYDESCorder',
305
+ sysparm_fields: 'order',
306
+ sysparm_limit: 1
307
+ }
308
+ });
309
+ var childOrder2 = parseInt(str(cResp2.data.result?.[0]?.order) || '0', 10);
310
+ if (childOrder2 > maxChildOrder) maxChildOrder = childOrder2;
311
+ } catch (_) {}
312
+ }
313
+
314
+ // Insert after last child; bump everything at that position
315
+ var insertOrder = maxChildOrder + 1;
316
+ var reorderInfo = await findElementsToReorder(client, flowId, insertOrder);
317
+ return { insertOrder: insertOrder, reorders: reorderInfo };
307
318
  }
308
319
 
309
- // Insert after last child; bump everything at that position
310
- var insertOrder = maxChildOrder + 1;
311
- var reorderInfo = await findElementsToReorder(client, flowId, insertOrder);
312
- return { insertOrder: insertOrder, reorders: reorderInfo };
320
+ // ── Top-level with explicit order: bump elements at that position ──
321
+ if (explicitOrder) {
322
+ var reorders = await findElementsToReorder(client, flowId, explicitOrder);
323
+ return { insertOrder: explicitOrder, reorders };
324
+ }
325
+
326
+ // ── Top-level without order: append at end ──
327
+ var nextOrder = await getNextOrder(client, flowId);
328
+ return { insertOrder: nextOrder, reorders: noReorders };
313
329
  }
314
330
 
315
331
  async function addTriggerViaGraphQL(
@@ -629,7 +645,7 @@ async function addFlowLogicViaGraphQL(
629
645
  insert: [{
630
646
  order: String(resolvedOrder),
631
647
  uiUniqueIdentifier: uuid,
632
- parent: '',
648
+ parent: parentUiId || '',
633
649
  metadata: '{"predicates":[]}',
634
650
  flowSysId: flowId,
635
651
  generationSource: '',
@@ -641,10 +657,18 @@ async function addFlowLogicViaGraphQL(
641
657
  }
642
658
  };
643
659
 
644
- // Merge reorder updates into flowLogics.update
645
- if (orderCalc.reorders.flowLogicUpdates.length > 0) {
660
+ // Merge reorder updates + parent update into flowLogics.update
661
+ var flowLogicUpdatesForPatch: any[] = orderCalc.reorders.flowLogicUpdates.slice();
662
+ if (parentUiId) {
663
+ // Signal the parent flow logic was modified (same as action insert does)
664
+ var parentAlreadyInList = flowLogicUpdatesForPatch.some(function (u: any) { return u.uiUniqueIdentifier === parentUiId; });
665
+ if (!parentAlreadyInList) {
666
+ flowLogicUpdatesForPatch.push({ uiUniqueIdentifier: parentUiId, type: 'flowlogic' });
667
+ }
668
+ }
669
+ if (flowLogicUpdatesForPatch.length > 0) {
646
670
  if (!flowPatch.flowLogics.update) flowPatch.flowLogics.update = [];
647
- flowPatch.flowLogics.update = flowPatch.flowLogics.update.concat(orderCalc.reorders.flowLogicUpdates);
671
+ flowPatch.flowLogics.update = flowPatch.flowLogics.update.concat(flowLogicUpdatesForPatch);
648
672
  }
649
673
  if (orderCalc.reorders.actionUpdates.length > 0) {
650
674
  flowPatch.actions = { update: orderCalc.reorders.actionUpdates };