snow-flow 10.0.1-dev.403 → 10.0.1-dev.404

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.403",
3
+ "version": "10.0.1-dev.404",
4
4
  "name": "snow-flow",
5
5
  "description": "Snow-Flow - ServiceNow Multi-Agent Development Framework powered by AI",
6
6
  "license": "Elastic-2.0",
@@ -165,24 +165,45 @@ async function createFlowViaScheduledJob(
165
165
  "",
166
166
  // ── Trigger, action, variable creation (runs for any tier) ──
167
167
  " if (flowSysId) {",
168
- // Trigger
168
+ // Trigger — search broadly for trigger definition (internal_name varies per instance)
169
169
  " if (!isSubflow && trigType !== 'manual') {",
170
170
  " try {",
171
- " var triggerMap = { 'record_created': 'sn_fd.trigger.record_created', 'record_updated': 'sn_fd.trigger.record_updated', 'scheduled': 'sn_fd.trigger.scheduled' };",
172
- " var trigIntName = triggerMap[trigType] || '';",
173
- " if (trigIntName) {",
174
- " var trigDef = new GlideRecord('sys_hub_action_type_definition');",
175
- " trigDef.addQuery('internal_name', trigIntName); trigDef.query();",
176
- " if (trigDef.next()) {",
177
- " var trigInst = new GlideRecord('sys_hub_trigger_instance');",
178
- " trigInst.initialize();",
179
- " trigInst.setValue('flow', flowSysId); trigInst.setValue('action_type', trigDef.getUniqueValue());",
180
- " trigInst.setValue('name', trigType); trigInst.setValue('order', 0); trigInst.setValue('active', true);",
181
- " if (trigTable) trigInst.setValue('table', trigTable);",
182
- " if (trigCondition) trigInst.setValue('condition', trigCondition);",
183
- " var trigId = trigInst.insert();",
184
- " r.steps.trigger = { success: !!trigId, sys_id: trigId + '' };",
185
- " } else { r.steps.trigger = { success: false, error: 'Trigger def not found: ' + trigIntName }; }",
171
+ " var trigSearchTerms = {",
172
+ " 'record_created': ['record_created', 'record_insert', 'created'],",
173
+ " 'record_updated': ['record_updated', 'record_update', 'updated'],",
174
+ " 'scheduled': ['scheduled', 'timer', 'schedule']",
175
+ " };",
176
+ " var terms = trigSearchTerms[trigType] || [trigType];",
177
+ " var trigDef = null;",
178
+ " var trigDefId = null;",
179
+ " var trigSearchLog = [];",
180
+ " for (var ts = 0; ts < terms.length; ts++) {",
181
+ " var tg = new GlideRecord('sys_hub_action_type_definition');",
182
+ " tg.addQuery('internal_name', 'CONTAINS', terms[ts]);",
183
+ " tg.addQuery('type', 'trigger');",
184
+ " tg.setLimit(1); tg.query();",
185
+ " if (tg.next()) { trigDef = tg; trigDefId = tg.getUniqueValue(); trigSearchLog.push(terms[ts] + ':found'); break; }",
186
+ " trigSearchLog.push(terms[ts] + ':not_found');",
187
+ " }",
188
+ " if (!trigDefId) {",
189
+ " var tg2 = new GlideRecord('sys_hub_action_type_definition');",
190
+ " tg2.addQuery('name', 'CONTAINS', trigType.replace('_', ' '));",
191
+ " tg2.addQuery('type', 'trigger');",
192
+ " tg2.setLimit(1); tg2.query();",
193
+ " if (tg2.next()) { trigDef = tg2; trigDefId = tg2.getUniqueValue(); trigSearchLog.push('name_search:found'); }",
194
+ " else { trigSearchLog.push('name_search:not_found'); }",
195
+ " }",
196
+ " if (trigDefId) {",
197
+ " var trigInst = new GlideRecord('sys_hub_trigger_instance');",
198
+ " trigInst.initialize();",
199
+ " trigInst.setValue('flow', flowSysId); trigInst.setValue('action_type', trigDefId);",
200
+ " trigInst.setValue('name', trigType); trigInst.setValue('order', 0); trigInst.setValue('active', true);",
201
+ " if (trigTable) trigInst.setValue('table', trigTable);",
202
+ " if (trigCondition) trigInst.setValue('condition', trigCondition);",
203
+ " var trigId = trigInst.insert();",
204
+ " r.steps.trigger = { success: !!trigId, sys_id: trigId + '', def_name: trigDef.getValue('name'), search: trigSearchLog };",
205
+ " } else {",
206
+ " r.steps.trigger = { success: false, error: 'No trigger def found', search: trigSearchLog };",
186
207
  " }",
187
208
  " } catch(te) { r.steps.trigger = { success: false, error: te.getMessage ? te.getMessage() : te + '' }; }",
188
209
  " }",
@@ -224,39 +245,28 @@ async function createFlowViaScheduledJob(
224
245
  " }",
225
246
  " r.steps.variables = { success: true, created: varsCreated };",
226
247
  "",
227
- // ── Engine registration (server-side sn_fd APIs) ──
228
- " r.steps.engine = { apis_found: [] };",
248
+ // ── Engine probe (discovery only — do NOT call publish/compile) ──
249
+ // Previous versions called FlowAPI.publish() which caused StackOverflowError
250
+ // and corrupted the flow. Now we only probe which APIs exist for diagnostics.
251
+ // The flow is left as a clean draft; user publishes via Flow Designer UI.
252
+ " r.steps.engine = { apis_found: [], mode: 'probe_only' };",
229
253
  " try {",
230
254
  " if (typeof sn_fd !== 'undefined') {",
231
255
  " r.steps.engine.sn_fd = 'available';",
232
- " if (sn_fd.FlowDesigner) {",
233
- " r.steps.engine.apis_found.push('FlowDesigner');",
234
- " if (typeof sn_fd.FlowDesigner.publishFlow === 'function') {",
235
- " try {",
236
- " sn_fd.FlowDesigner.publishFlow(flowSysId);",
237
- " r.steps.engine.publish = 'success';",
238
- " } catch(pe) { r.steps.engine.publish = pe.getMessage ? pe.getMessage() : pe + ''; }",
239
- " }",
240
- " }",
241
- " if (sn_fd.FlowCompiler) {",
242
- " r.steps.engine.apis_found.push('FlowCompiler');",
243
- " if (typeof sn_fd.FlowCompiler.compile === 'function') {",
244
- " try {",
245
- " sn_fd.FlowCompiler.compile(flowSysId);",
246
- " r.steps.engine.compile = 'success';",
247
- " } catch(ce) { r.steps.engine.compile = ce.getMessage ? ce.getMessage() : ce + ''; }",
248
- " }",
249
- " }",
250
- " var otherApis = ['FlowAPI', 'FlowPublisher'];",
251
- " for (var oa = 0; oa < otherApis.length; oa++) {",
252
- " if (sn_fd[otherApis[oa]]) {",
253
- " r.steps.engine.apis_found.push(otherApis[oa]);",
254
- " var apiObj = sn_fd[otherApis[oa]];",
255
- " if (typeof apiObj.publish === 'function') {",
256
- " try { apiObj.publish(flowSysId); r.steps.engine[otherApis[oa] + '_publish'] = 'success'; }",
257
- " catch(e) { r.steps.engine[otherApis[oa] + '_publish'] = e + ''; }",
256
+ " var probeNames = ['FlowDesigner', 'FlowCompiler', 'FlowAPI', 'FlowPublisher'];",
257
+ " for (var pn = 0; pn < probeNames.length; pn++) {",
258
+ " try {",
259
+ " if (sn_fd[probeNames[pn]]) {",
260
+ " r.steps.engine.apis_found.push(probeNames[pn]);",
261
+ " var methods = [];",
262
+ " var pObj = sn_fd[probeNames[pn]];",
263
+ " var checkMethods = ['createFlow','publishFlow','activateFlow','compileFlow','compile','publish','getFlow'];",
264
+ " for (var cm = 0; cm < checkMethods.length; cm++) {",
265
+ " if (typeof pObj[checkMethods[cm]] === 'function') methods.push(checkMethods[cm]);",
266
+ " }",
267
+ " if (methods.length > 0) r.steps.engine[probeNames[pn] + '_methods'] = methods;",
258
268
  " }",
259
- " }",
269
+ " } catch(e) {}",
260
270
  " }",
261
271
  " } else {",
262
272
  " r.steps.engine.sn_fd = 'unavailable';",
@@ -1466,39 +1476,53 @@ export async function execute(args: any, context: ServiceNowContext): Promise<To
1466
1476
  }
1467
1477
 
1468
1478
  // Create trigger instance (non-manual flows only)
1479
+ // Search broadly — internal_name varies across ServiceNow instances
1469
1480
  if (!isSubflow && triggerType !== 'manual') {
1470
1481
  try {
1471
- var triggerTypeLookup: Record<string, string> = {
1472
- 'record_created': 'sn_fd.trigger.record_created',
1473
- 'record_updated': 'sn_fd.trigger.record_updated',
1474
- 'scheduled': 'sn_fd.trigger.scheduled'
1482
+ var trigSearchTerms: Record<string, string[]> = {
1483
+ 'record_created': ['record_created', 'record_insert', 'created'],
1484
+ 'record_updated': ['record_updated', 'record_update', 'updated'],
1485
+ 'scheduled': ['scheduled', 'timer', 'schedule']
1475
1486
  };
1476
- var triggerInternalName = triggerTypeLookup[triggerType] || '';
1487
+ var searchTerms = trigSearchTerms[triggerType] || [triggerType];
1488
+ var triggerDefId: string | null = null;
1477
1489
 
1478
- if (triggerInternalName) {
1490
+ for (var tsi = 0; tsi < searchTerms.length && !triggerDefId; tsi++) {
1479
1491
  var triggerDefResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
1480
1492
  params: {
1481
- sysparm_query: 'internal_name=' + triggerInternalName,
1493
+ sysparm_query: 'internal_nameLIKE' + searchTerms[tsi] + '^type=trigger',
1482
1494
  sysparm_fields: 'sys_id',
1483
1495
  sysparm_limit: 1
1484
1496
  }
1485
1497
  });
1498
+ triggerDefId = triggerDefResp.data.result?.[0]?.sys_id || null;
1499
+ }
1486
1500
 
1487
- var triggerDefId = triggerDefResp.data.result?.[0]?.sys_id;
1488
- if (triggerDefId) {
1489
- var triggerData: any = {
1490
- flow: flowSysId,
1491
- action_type: triggerDefId,
1492
- name: triggerType,
1493
- order: 0,
1494
- active: true
1495
- };
1496
- if (flowTable) triggerData.table = flowTable;
1497
- if (triggerCondition) triggerData.condition = triggerCondition;
1498
-
1499
- await client.post('/api/now/table/sys_hub_trigger_instance', triggerData);
1500
- triggerCreated = true;
1501
- }
1501
+ // Fallback: search by display name
1502
+ if (!triggerDefId) {
1503
+ var trigNameResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
1504
+ params: {
1505
+ sysparm_query: 'nameLIKE' + triggerType.replace(/_/g, ' ') + '^type=trigger',
1506
+ sysparm_fields: 'sys_id',
1507
+ sysparm_limit: 1
1508
+ }
1509
+ });
1510
+ triggerDefId = trigNameResp.data.result?.[0]?.sys_id || null;
1511
+ }
1512
+
1513
+ if (triggerDefId) {
1514
+ var triggerData: any = {
1515
+ flow: flowSysId,
1516
+ action_type: triggerDefId,
1517
+ name: triggerType,
1518
+ order: 0,
1519
+ active: true
1520
+ };
1521
+ if (flowTable) triggerData.table = flowTable;
1522
+ if (triggerCondition) triggerData.condition = triggerCondition;
1523
+
1524
+ await client.post('/api/now/table/sys_hub_trigger_instance', triggerData);
1525
+ triggerCreated = true;
1502
1526
  }
1503
1527
  } catch (triggerError) {
1504
1528
  // Best-effort