snow-flow 10.0.1-dev.446 → 10.0.1-dev.448

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.446",
3
+ "version": "10.0.1-dev.448",
4
4
  "name": "snow-flow",
5
5
  "description": "Snow-Flow - ServiceNow Multi-Agent Development Framework powered by AI",
6
6
  "license": "Elastic-2.0",
@@ -38,6 +38,25 @@ function generateUUID(): string {
38
38
  });
39
39
  }
40
40
 
41
+ async function getNextOrder(client: any, flowId: string): Promise<number> {
42
+ let maxOrder = 0;
43
+ // Query all element types that have an order field on this flow
44
+ for (const table of ['sys_hub_action_instance', 'sys_hub_flow_logic', 'sys_hub_sub_flow_instance']) {
45
+ try {
46
+ const resp = await client.get('/api/now/table/' + table, {
47
+ params: {
48
+ sysparm_query: 'flow=' + flowId + '^ORDERBYDESCorder',
49
+ sysparm_fields: 'order',
50
+ sysparm_limit: 1
51
+ }
52
+ });
53
+ const order = parseInt(resp.data.result?.[0]?.order || '0', 10);
54
+ if (order > maxOrder) maxOrder = order;
55
+ } catch (_) {}
56
+ }
57
+ return maxOrder + 1;
58
+ }
59
+
41
60
  async function executeFlowPatchMutation(
42
61
  client: any,
43
62
  flowPatch: any,
@@ -183,6 +202,7 @@ async function addActionViaGraphQL(
183
202
  actionType: string,
184
203
  actionName: string,
185
204
  inputs?: Record<string, string>,
205
+ parentUiId?: string,
186
206
  order?: number
187
207
  ): Promise<{ success: boolean; actionId?: string; steps?: any; error?: string }> {
188
208
  const steps: any = {};
@@ -265,6 +285,7 @@ async function addActionViaGraphQL(
265
285
  }
266
286
 
267
287
  const uuid = generateUUID();
288
+ const resolvedOrder = order || await getNextOrder(client, flowId);
268
289
  const actionResponseFields = 'actions { inserts { sysId uiUniqueIdentifier __typename } updates deletes __typename }';
269
290
  try {
270
291
  const result = await executeFlowPatchMutation(client, {
@@ -275,11 +296,11 @@ async function addActionViaGraphQL(
275
296
  metadata: '{"predicates":[]}',
276
297
  flowSysId: flowId,
277
298
  generationSource: '',
278
- order: String(order || 1),
279
- parent: '',
299
+ order: String(resolvedOrder),
300
+ parent: parentUiId || '',
280
301
  uiUniqueIdentifier: uuid,
281
302
  type: 'action',
282
- parentUiId: '',
303
+ parentUiId: parentUiId || '',
283
304
  inputs: []
284
305
  }]
285
306
  }
@@ -365,13 +386,14 @@ async function addFlowLogicViaGraphQL(
365
386
  if (!defId) return { success: false, error: 'Flow logic definition not found for: ' + logicType, steps };
366
387
 
367
388
  const uuid = generateUUID();
389
+ const resolvedOrder = order || await getNextOrder(client, flowId);
368
390
  const logicResponseFields = 'flowLogics { inserts { sysId uiUniqueIdentifier __typename } updates deletes __typename }';
369
391
  try {
370
392
  const result = await executeFlowPatchMutation(client, {
371
393
  flowId: flowId,
372
394
  flowLogics: {
373
395
  insert: [{
374
- order: String(order || 1),
396
+ order: String(resolvedOrder),
375
397
  uiUniqueIdentifier: uuid,
376
398
  parent: '',
377
399
  metadata: '{"predicates":[]}',
@@ -472,6 +494,7 @@ async function addSubflowCallViaGraphQL(
472
494
  if (!subflowName) subflowName = subflowId;
473
495
 
474
496
  const uuid = generateUUID();
497
+ const resolvedOrder = order || await getNextOrder(client, flowId);
475
498
  const subflowResponseFields = 'subflows { inserts { sysId uiUniqueIdentifier __typename } updates deletes __typename }';
476
499
  try {
477
500
  const result = await executeFlowPatchMutation(client, {
@@ -482,7 +505,7 @@ async function addSubflowCallViaGraphQL(
482
505
  flowSysId: flowId,
483
506
  generationSource: '',
484
507
  name: subflowName,
485
- order: String(order || 1),
508
+ order: String(resolvedOrder),
486
509
  parent: parentUiId || '',
487
510
  subflowSysId: subflowSysId,
488
511
  uiUniqueIdentifier: uuid,
@@ -779,7 +802,7 @@ export const toolDefinition: MCPToolDefinition = {
779
802
  },
780
803
  logic_type: {
781
804
  type: 'string',
782
- description: 'Flow logic type for add_flow_logic. Looked up dynamically in sys_hub_flow_logic_definition. Common values: IF, FOR_EACH, DO_UNTIL, SWITCH'
805
+ description: 'Flow logic type for add_flow_logic. Looked up dynamically in sys_hub_flow_logic_definition. Common values: IF, FOR_EACH, DO_UNTIL, SWITCH. Note: IF does NOT require an Else block — if the condition is false the flow simply continues to the next step. Only add Else if explicitly requested.'
783
806
  },
784
807
  logic_inputs: {
785
808
  type: 'object',
@@ -797,6 +820,10 @@ export const toolDefinition: MCPToolDefinition = {
797
820
  type: 'string',
798
821
  description: 'Element sys_id or uiUniqueIdentifier for update_*/delete_* actions. For delete_* this can also be a comma-separated list of IDs.'
799
822
  },
823
+ order: {
824
+ type: 'number',
825
+ description: 'Position/order of the element in the flow (for add_* actions). Auto-detected if not provided (appends after last element).'
826
+ },
800
827
  type: {
801
828
  type: 'string',
802
829
  enum: ['flow', 'subflow', 'all'],
@@ -1716,7 +1743,7 @@ export async function execute(args: any, context: ServiceNowContext): Promise<To
1716
1743
  var addActName = args.action_name || args.name || addActType;
1717
1744
  var addActInputs = args.action_inputs || args.inputs || {};
1718
1745
 
1719
- var addActResult = await addActionViaGraphQL(client, addActFlowId, addActType, addActName, addActInputs);
1746
+ var addActResult = await addActionViaGraphQL(client, addActFlowId, addActType, addActName, addActInputs, args.parent_ui_id, args.order);
1720
1747
 
1721
1748
  var addActSummary = summary();
1722
1749
  if (addActResult.success) {