-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflowCore.js
More file actions
1351 lines (1207 loc) · 61.3 KB
/
Copy pathflowCore.js
File metadata and controls
1351 lines (1207 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* flowCore.js
* Core logic and utility functions for API Flow Configuration Builder.
* Contains NO DOM manipulation or UI-specific code.
*/
import { logger } from './logger.js';
import { TRANSFORM_OP_DEFS, isTransformOpName, normalizeRefPath } from './transformOps.js';
/**
* Generate a unique ID for steps
* @return {string} Unique identifier
*/
export function generateUniqueId() {
// Simple timestamp + random string for uniqueness in a session
return 'step_' + Date.now() + '_' + Math.random().toString(36).substring(2, 11);
}
/**
* Convert flow model to JSON representation suitable for backend storage.
* Handles variable placeholders correctly.
* @param {Object} flowModel - Internal flow model using {{variable}} syntax.
* @return {Object} JSON representation with placeholders processed.
*/
export function flowModelToJson(flowModel) {
const json = {
schemaVersion: flowModel.schemaVersion || '1.0', // additive: absence is read as "1.0"; never gates the wire format. See docs/schema-versioning.md.
id: flowModel.id, // Include ID if present (for updates)
name: flowModel.name || '',
description: flowModel.description || '',
headers: flowModel.headers ? { ...flowModel.headers } : {},
steps: [],
staticVars: flowModel.staticVars ? { ...flowModel.staticVars } : {},
visualLayout: flowModel.visualLayout ? { ...flowModel.visualLayout } : {} // <-- NEW: Include visual layout
};
function makeExtractPathsExplicit(extract) {
if (!extract) return extract;
const explicit = {};
for (const [varName, path] of Object.entries(extract)) {
if (typeof path === 'string' &&
path !== '.status' &&
!path.startsWith('body.') &&
!path.startsWith('headers.') &&
path !== 'body' &&
path !== 'headers') {
explicit[varName] = `body.${path}`;
} else {
explicit[varName] = path;
}
}
return explicit;
}
function processSteps(steps) {
if (!steps || !Array.isArray(steps)) return [];
return steps.map(step => {
const jsonStep = {
id: step.id,
name: step.name || '',
type: step.type
};
if (step.type === 'request') {
jsonStep.method = step.method || 'GET';
jsonStep.url = step.url || '';
if (step.headers && Object.keys(step.headers).length > 0) {
jsonStep.headers = { ...step.headers };
}
// --- MODIFICATION START ---
// Include onFailure, defaulting to 'stop' if missing
jsonStep.onFailure = step.onFailure || 'stop';
// --- MODIFICATION END ---
// Process body: Use preProcessBody to handle placeholders
// Ensure 'body' field is included only if step.body has content or is a non-empty object.
let bodyToStore = null;
if (typeof step.body === 'string' && step.body.trim()) {
try {
// Preprocess the body text (which might be a stringified object or just a string)
bodyToStore = preProcessBody(step.body.trim());
} catch (e) {
logger.warn(`Failed to preprocess request body for step ${step.id}. Storing as potentially invalid string. Error: ${e.message}`);
bodyToStore = step.body; // Fallback to original string - might be invalid JSON
}
}
// Add body to jsonStep only if it has content after processing
if (bodyToStore !== null && bodyToStore !== '') {
// Attempt to parse if it looks like JSON, otherwise store as string
try {
// The preProcessBody should have returned a valid JSON string (with markers)
jsonStep.body = JSON.parse(bodyToStore);
} catch (e) {
// If JSON.parse fails even after preprocessing, store the (potentially invalid) result as a string.
// This might happen if preprocessing failed or the original was fundamentally broken.
logger.warn(`Storing potentially invalid JSON string for step ${step.id} body after parse failure: ${e.message}`);
jsonStep.body = bodyToStore;
}
}
if (step.extract && Object.keys(step.extract).length > 0) {
jsonStep.extract = makeExtractPathsExplicit(step.extract);
}
// === WAVE3 assertions ===
// Additive optional per-step assertions. Emitted verbatim (frozen
// conditionData operator vocabulary + { target, operator, value, critical? }).
// Only written when non-empty so a step without assertions never gains an
// empty key — keeps golden pre-sprint flows byte-stable and CLI-safe.
if (Array.isArray(step.assertions) && step.assertions.length > 0) {
jsonStep.assertions = step.assertions.map(a => ({ ...a }));
}
// === END WAVE3 assertions ===
// Additive per-request retries ({ count, delayMs }; engine: flowRunner).
// Only written when count > 0 so retry-free flows stay byte-identical.
if (step.retries && typeof step.retries === 'object'
&& Number.isFinite(step.retries.count) && step.retries.count > 0) {
jsonStep.retries = {
count: Math.floor(step.retries.count),
delayMs: Math.max(0, Number.isFinite(step.retries.delayMs) ? step.retries.delayMs : 0),
};
}
} else if (step.type === 'condition') {
jsonStep.condition = step.condition || '';
// Store structured condition data if available (preferred)
if (step.conditionData) {
jsonStep.conditionData = { ...step.conditionData }; // Ensure copy
}
jsonStep.then = processSteps(step.thenSteps);
jsonStep.else = processSteps(step.elseSteps);
} else if (step.type === 'loop') {
jsonStep.source = step.source || '';
jsonStep.loopVariable = step.loopVariable || 'item';
jsonStep.steps = processSteps(step.loopSteps);
} else if (step.type === 'transform') {
jsonStep.ops = Array.isArray(step.ops) ? step.ops : [];
}
return jsonStep;
});
}
json.steps = processSteps(flowModel.steps);
return json;
}
/**
* Recursively traverses data (object, array, string) loaded from JSON
* and converts ##VAR:type:name## marker strings back into {{name}} placeholders for UI display.
* NO BASE64 DECODING IS PERFORMED.
* @param {*} data - The data structure (potentially containing markers) loaded from the file.
* @returns {*} A new data structure with markers replaced by {{name}} placeholders.
*/
export function decodeMarkersRecursive(data) {
if (typeof data === 'string') {
// Regex to match the entire string as ##VAR:(string|unquoted):ACTUAL_NAME##
const markerRegex = /^##VAR:(string|unquoted):([^#]+)##$/;
const match = data.match(markerRegex);
if (match) {
// *** CRITICAL: Use the captured name directly ***
const name = match[2]; // <-- This is the ACTUAL name, not Base64
// Return the UI placeholder format {{ACTUAL_NAME}}
return `{{${name}}}`;
}
// If the string doesn't exactly match the marker format, return it unchanged.
return data;
} else if (Array.isArray(data)) {
// Recursively process array elements
return data.map(item => decodeMarkersRecursive(item));
} else if (typeof data === 'object' && data !== null) {
// Recursively process object values
const newObj = {};
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
newObj[key] = decodeMarkersRecursive(data[key]);
}
}
return newObj;
}
// Return numbers, booleans, null, undefined as is
return data;
}
/**
* Render a stored body (with ##VAR:type:name## markers) as editor text.
* CRITICAL round-trip contract: an UNQUOTED marker must come back as a bare
* {{var}} (no surrounding quotes) in the JSON text, otherwise the next save
* re-classifies it as a *string* marker and silently changes the JSON type
* the request sends ({"id":{{n}}} becoming {"id":"{{n}}"}). We decode unquoted
* markers through a sentinel, stringify, then strip the sentinel's quotes.
* @param {*} rawBodyWithMarkers
* @return {string} body text for the editor
*/
export function stringifyBodyForDisplay(rawBodyWithMarkers) {
const UNQ_RE = /^##VAR:unquoted:([^#]+)##$/;
const decode = (data) => {
if (typeof data === 'string') {
const unq = data.match(UNQ_RE);
if (unq) return `##UNQ##{{${unq[1]}}}##/UNQ##`;
return decodeMarkersRecursive(data);
} else if (Array.isArray(data)) {
return data.map(decode);
} else if (typeof data === 'object' && data !== null) {
const out = {};
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) out[key] = decode(data[key]);
}
return out;
}
return data;
};
const decoded = decode(rawBodyWithMarkers);
if (typeof decoded === 'object' && decoded !== null) {
try {
return JSON.stringify(decoded, null, 2)
.replace(/"##UNQ##(\{\{[^}]+\}\})##\/UNQ##"/g, '$1');
} catch (e) {
return String(decoded);
}
}
return String(decoded).replace(/##UNQ##(\{\{[^}]+\}\})##\/UNQ##/g, '$1');
}
/**
* Convert JSON representation (from backend) to internal flow model.
* Restores {{variable}} syntax for UI display.
* @param {Object} json - JSON flow definition from backend.
* @return {Object} Internal flow model.
*/
export function jsonToFlowModel(json) {
if (!json) {
logger.error("jsonToFlowModel received null or undefined input.");
return createTemplateFlow(); // Return a default empty flow
}
const flowModel = {
schemaVersion: json.schemaVersion, // preserve for lossless round-trip; absence (undefined) is stamped "1.0" on save
id: json.id, // Keep the ID
name: json.name || 'New Flow',
description: json.description || '',
headers: json.headers || {},
steps: [],
staticVars: json.staticVars || {},
visualLayout: json.visualLayout || {} // <-- NEW: Load visual layout, default to empty object
};
function upgradeExtractPaths(extract) {
if (!extract) return extract;
const upgraded = {};
for (const [varName, path] of Object.entries(extract)) {
if (typeof path === 'string' &&
path !== '.status' &&
!path.startsWith('body.') &&
!path.startsWith('headers.') &&
path !== 'body' &&
path !== 'headers') {
upgraded[varName] = `body.${path}`;
} else {
upgraded[varName] = path;
}
}
return upgraded;
}
function processJsonSteps(jsonSteps) {
if (!jsonSteps || !Array.isArray(jsonSteps)) return [];
return jsonSteps.map(jsonStep => {
const step = {
id: jsonStep.id || generateUniqueId(),
name: jsonStep.name || `Unnamed ${jsonStep.type}`,
type: jsonStep.type
};
if (jsonStep.type === 'request') {
step.method = jsonStep.method || 'GET';
step.url = jsonStep.url || '';
step.headers = jsonStep.headers || {};
step.onFailure = (jsonStep.onFailure === 'continue' || jsonStep.onFailure === 'stop') ? jsonStep.onFailure : 'stop';
step.rawBodyWithMarkers = null;
step.body = '';
if (jsonStep.body !== undefined && jsonStep.body !== null) {
try {
step.rawBodyWithMarkers = JSON.parse(JSON.stringify(jsonStep.body));
} catch (e) {
logger.warn(`Could not deep copy body for step ${step.id}, storing reference.`, e);
step.rawBodyWithMarkers = jsonStep.body;
}
step.body = stringifyBodyForDisplay(step.rawBodyWithMarkers);
}
// Upgrade extract paths for backward compatibility
step.extract = upgradeExtractPaths(jsonStep.extract || {});
// === WAVE3 assertions ===
// Load additive per-step assertions into the model verbatim (deep-copied)
// when present. Absent/malformed ⇒ no `assertions` key on the model, so a
// pre-sprint flow round-trips byte-identically.
if (Array.isArray(jsonStep.assertions) && jsonStep.assertions.length > 0) {
step.assertions = jsonStep.assertions
.filter(a => a && typeof a === 'object')
.map(a => ({ ...a }));
}
// === END WAVE3 assertions ===
// Additive per-request retries: carried into the model when valid so the
// editor can show it and the engine (flowRunner) can honor it. Absent ⇒
// no key on the model (pre-existing flows round-trip byte-identically).
if (jsonStep.retries && typeof jsonStep.retries === 'object'
&& Number.isFinite(jsonStep.retries.count) && jsonStep.retries.count > 0) {
step.retries = {
count: Math.floor(jsonStep.retries.count),
delayMs: Math.max(0, Number.isFinite(jsonStep.retries.delayMs) ? jsonStep.retries.delayMs : 0),
};
}
} else if (jsonStep.type === 'condition') {
step.condition = jsonStep.condition || '';
if (jsonStep.conditionData) {
step.conditionData = jsonStep.conditionData;
} else if (step.condition) {
step.conditionData = parseConditionString(step.condition);
} else {
step.conditionData = { variable: '', operator: '', value: '' };
}
step.thenSteps = processJsonSteps(jsonStep.then);
step.elseSteps = processJsonSteps(jsonStep.else);
} else if (jsonStep.type === 'loop') {
step.source = jsonStep.source || '';
step.loopVariable = jsonStep.loopVariable || 'item';
step.loopSteps = processJsonSteps(jsonStep.steps);
} else if (jsonStep.type === 'transform') {
step.ops = Array.isArray(jsonStep.ops) ? jsonStep.ops : [];
}
return step;
});
}
flowModel.steps = processJsonSteps(json.steps);
return flowModel;
}
/**
* Extract variables from a string using {{variable}} syntax
* @param {string} text - Text to extract variables from
* @return {Array} List of unique variable names found
*/
export function extractVariableReferences(text) {
if (!text || typeof text !== 'string') return [];
const regex = /\{\{([^}]+)\}\}/g;
const variables = new Set();
let match;
while ((match = regex.exec(text)) !== null) {
variables.add(match[1].trim());
}
return Array.from(variables);
}
/**
* Find all variables defined within a flow model up to a certain point, or including runtime context.
* @param {Object} flowModel - The flow model to analyze.
* @param {Object} [runtimeContext] - Optional runtime context to include.
* @return {Object} Map where keys are variable names and values are objects describing their origin.
*/
export function findDefinedVariables(flowModel, runtimeContext = null) {
const variables = {};
// 1. Add Static/Flow Variables
if (flowModel?.staticVars) {
Object.keys(flowModel.staticVars).forEach(key => {
if (key) {
variables[key] = {
origin: 'Flow Variable',
path: null,
stepId: null,
type: 'static',
// value: flowModel.staticVars[key] // Include value? Maybe for display.
};
}
});
}
// 2. Add variables from runtime context if provided
if (runtimeContext) {
Object.keys(runtimeContext).forEach(key => {
if (key && !variables[key]) { // Add if not already defined as static
// Determine origin based on how context was populated (difficult without execution history)
variables[key] = {
origin: 'Runtime', // Generic origin for runtime values
path: null,
stepId: null, // Can't easily determine which step defined it here
type: 'runtime',
// value: runtimeContext[key] // Include runtime value?
};
}
// Optionally update value if already present
// else if (variables[key]) {
// variables[key].value = runtimeContext[key];
// }
});
}
// 3. Recursively process steps to find extracted and loop variables definitions
function processSteps(steps, pathPrefix = '', currentLoopVars = new Set()) {
if (!steps || !Array.isArray(steps)) return;
steps.forEach((step, index) => {
const stepName = step.name || `Step ${index + 1}`;
const currentPath = pathPrefix ? `${pathPrefix} > ${stepName}` : stepName;
// Add variables defined IN THIS STEP if not already present from runtime context
if (step.type === 'request' && step.extract) {
Object.keys(step.extract).forEach(varName => {
if (varName && !variables[varName]) {
variables[varName] = {
origin: currentPath,
path: step.extract[varName],
stepId: step.id,
type: 'extraction'
};
}
});
} else if (step.type === 'loop') {
const loopVar = step.loopVariable || 'item';
if (loopVar && !variables[loopVar]) {
variables[loopVar] = {
origin: currentPath,
isIterationVariable: true,
stepId: step.id,
type: 'loop'
};
currentLoopVars.add(loopVar);
}
processSteps(step.loopSteps, `${currentPath} > Loop Body`, new Set(currentLoopVars));
if (loopVar) currentLoopVars.delete(loopVar); // Goes out of scope
} else if (step.type === 'transform') {
const ops = Array.isArray(step.ops) ? step.ops : [];
ops.forEach(op => {
if (op && typeof op.set === 'string' && op.set.trim() && !variables[op.set]) {
variables[op.set] = {
origin: currentPath,
path: op.op || 'transform',
stepId: step.id,
type: 'transform'
};
}
});
} else if (step.type === 'condition') {
processSteps(step.thenSteps, `${currentPath} > Then`, new Set(currentLoopVars));
processSteps(step.elseSteps, `${currentPath} > Else`, new Set(currentLoopVars));
}
});
}
// Only scan steps if runtime context wasn't provided (runtime context implies full potential scope)
if (!runtimeContext && flowModel?.steps) {
processSteps(flowModel.steps);
}
return variables;
}
/**
* Evaluate a dotted / bracket path on a data object.
*
* Rules
* • `.status` → data.status (special case)
* • `body.…` / `headers.…` → explicit roots
* • `body` / `headers` → return those whole objects
* • anything else (`id`, `user.name`, `arr[3].id`, …)
* – if the object has a `body` key → look inside `data.body`
* – otherwise → look in the object itself
*
* Supports array‑index syntax (`items[3].value`).
*/
/**
* Evaluate a dotted / bracket path on a data object.
*
* Rules
* ─────────────────────────────────────────────────────────────
* • `.status` → data.status (special case)
* • `body` → data.body ▸ if it exists, otherwise data
* • `body.…` → inside data.body ▸ if it exists, else inside data
* • `headers` → data.headers
* • `headers.…` → inside data.headers
* • anything else (`id`, `user.name`, `arr[2].id`, …)
* – if the object has a `body` key → look in data.body
* – otherwise → look in the object itself
*
* Array indices like `items[3].value` are supported.
*/
export function evaluatePath(data, path) {
/* Sanity checks */
if (data == null || typeof path !== 'string' || !path.trim()) return undefined;
/* 1. ─ Special literal ------------------------------------ */
if (path === '.status') {
return Object.prototype.hasOwnProperty.call(data, 'status') ? data.status : undefined;
}
/* 2. ─ Explicit BODY handling ------------------------------ */
if (path === 'body') {
return ('body' in data) ? data.body : data; // whole body, or the object itself
}
if (path.startsWith('body.')) {
const root = ('body' in data) ? data.body : data; // fall back when body is absent
return walk(root, path.slice(5)); // drop 'body.'
}
/* 3. ─ Explicit HEADERS handling --------------------------- */
if (path === 'headers') {
return data.headers; // may be undefined
}
if (path.startsWith('headers.')) {
return walk(data.headers, path.slice(8)); // drop 'headers.'
}
/* 4. ─ Implicit (no prefix) ------------------------------- */
const implicitRoot = ('body' in data) ? data.body : data;
return walk(implicitRoot, path);
/* --------------------------------------------------------- */
function walk(obj, subPath) {
if (obj == null) return undefined;
if (!subPath) return obj; // caller asked for the whole root
/* tokenise: items[3].value → ['items','3','value'] */
const tokens = subPath
.replace(/\[(\d+)\]/g, '.$1') // [index] → .index
.split('.')
.filter(Boolean);
let cur = obj;
for (const key of tokens) {
if (cur == null) return undefined;
cur = cur[key];
}
return cur;
}
}
/**
* Validates JSON text, intelligently handling unquoted {{variable}} placeholders.
* Allows `key: {{var}}` but flags other JSON errors.
* @param {string} bodyText - The request body text.
* @return {{valid: boolean, message?: string}} Validation result.
*/
export function validateRequestBodyJson(bodyText) {
if (!bodyText || typeof bodyText !== 'string' || bodyText.trim() === '') {
return { valid: true }; // Empty body is valid
}
try {
// Temporarily replace unquoted {{var}} with a valid JSON value (e.g., null)
// for validation purposes ONLY. This preserves quoted "{{var}}" which are valid strings.
const tempJsonString = bodyText.replace(
/(?<=[:\[,\s])\s*{{([^}]+)}}\s*(?=[,}\]\s]|$)/g,
'null' // Replace with null for validation
);
JSON.parse(tempJsonString);
return { valid: true };
} catch (error) {
let message = error.message;
// Try to improve common error messages
if (message.includes('Unexpected token')) {
const badTokenMatch = message.match(/Unexpected token ({|}) in JSON/);
if (badTokenMatch) {
message = `Likely syntax error near '{{' or '}}'.\n\nFor variables, use:\n - \"key\": \"{{var}}\" for strings\n - \"key\": {{var}} for numbers/booleans\n\nCheck for missing or extra braces, or misplaced commas.\nExample: { \"id\": {{userId}} }`;
} else {
const positionMatch = message.match(/at position (\d+)/);
const position = positionMatch ? parseInt(positionMatch[1], 10) : -1;
let context = '';
if (position !== -1) {
const snippetStart = Math.max(0, position - 15);
const snippetEnd = Math.min(bodyText.length, position + 15);
context = `\nError near: ...${bodyText.substring(snippetStart, position)}[HERE]${bodyText.substring(position, snippetEnd)}...`;
}
message = `Invalid JSON syntax.${context}\n\nCheck for missing commas, quotes, or brackets.\nTip: Each key-value pair should be separated by a comma, and all keys must be in double quotes.\nExample: { \"name\": \"value\", \"id\": 123 }`;
}
} else if (message.includes('Unexpected end of JSON input')) {
message = `Incomplete JSON.\n\nCheck for unclosed brackets or braces.\nTip: Every opening { or [ must have a matching closing } or ].`;
} else {
message = `JSON validation failed: ${message}\n\nTip: Ensure your JSON is properly formatted. All keys must be in double quotes, and values must be valid JSON types.`;
}
return { valid: false, message: message };
}
}
// Helper function to escape characters for use in RegExp (if not already available)
export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* Pre-processes a JSON string containing {{variable}} placeholders.
* Replaces placeholders with unique markers ##VAR:type:name## to allow standard JSON parsing.
* Distinguishes between quoted ("{{var}}") and unquoted ({{var}}) placeholders.
* DOES NOT USE BASE64. Variable names are stored directly in the marker.
* @param {string} bodyText - The raw JSON string with placeholders.
* @return {string} A valid JSON string with placeholders replaced by markers, or the original string on error/fallback.
*/
export function preProcessBody(bodyText) {
if (!bodyText || typeof bodyText !== 'string' || bodyText.trim() === '') return bodyText; // Return original if empty or not a string
const placeholders = {};
let counter = 0;
const placeholderPrefix = "__TEMP_VAR_PLACEHOLDER_"; // Unique temporary string
try {
// Pass 1: Replace QUOTED "{{var}}" with temporary placeholder
// Matches "{{var}}" only when enclosed in double quotes.
let tempText = bodyText.replace(/"(\{\{([^}]+)\}\})"/g, (match, fullVar, varName) => {
const placeholder = `${placeholderPrefix}${counter++}`;
// Store the RAW name, no encoding, mark as string type
placeholders[placeholder] = { type: 'string', name: varName.trim() };
// Replace with the placeholder, keeping the quotes
return `"${placeholder}"`;
});
// Pass 2: Replace UNQUOTED {{var}} with temporary placeholder
// Matches {{var}} when preceded by a colon, bracket, curly brace, comma, or start of string,
// potentially surrounded by whitespace, and followed by whitespace, comma, closing bracket/brace, or end of string.
// This prevents matching {{var}} inside strings like "abc{{var}}def".
tempText = tempText.replace(/(^|[:\[{,\s])\s*(\{\{([^}]+)\}\})\s*(?=$|[,}\]])/g, (match, prefix, fullVar, varName) => {
const placeholder = `${placeholderPrefix}${counter++}`;
// Store the RAW name, no encoding, mark as unquoted type
placeholders[placeholder] = { type: 'unquoted', name: varName.trim() };
// Replace with the placeholder, adding quotes (to make it a valid JSON string temporarily)
// The prefix ensures we keep the colon/comma etc.
return `${prefix}"${placeholder}"`;
});
// Pass 3: Replace temporary placeholders with final ##VAR:type:name## markers
let jsonStringWithMarkers = tempText;
for (const placeholder in placeholders) {
if (Object.prototype.hasOwnProperty.call(placeholders, placeholder)) {
const info = placeholders[placeholder];
// *** CRITICAL: Construct marker with RAW name ***
// Example: ##VAR:string:myVar## or ##VAR:unquoted:count##
const marker = `##VAR:${info.type}:${info.name}##`; // <-- NO BASE64 ENCODING HERE
// Escape the placeholder for use in regex (it contains underscores)
const placeholderPattern = new RegExp(`"${escapeRegExp(placeholder)}"`, 'g');
// Replace the quoted temporary placeholder with the final marker string, ensuring it remains quoted
jsonStringWithMarkers = jsonStringWithMarkers.replace(placeholderPattern, `"${marker}"`);
}
}
// Final check: Ensure the resulting string is valid JSON
try {
JSON.parse(jsonStringWithMarkers);
// If parsing succeeds, the string with markers is valid JSON
return jsonStringWithMarkers;
} catch (parseError) {
// If parsing fails *after* marker insertion, something went fundamentally wrong.
// This might happen with complex nested structures or malformed original input.
// Fallback: Perform a simpler, less robust replacement directly on the original text.
// This fallback might incorrectly quote numbers/booleans, but aims to preserve data.
logger.warn(`preProcessBody: Resulting string with markers is not valid JSON (${parseError.message}). Falling back to simple replacement.`);
// Simple fallback: Treat all {{var}} as string markers.
return bodyText.replace(/\{\{([^}]+)\}\}/g, (match, varName) => `"##VAR:string:${varName.trim()}##"`);
}
} catch (error) {
// Catch any unexpected errors during the replacement process.
logger.error("Error during preProcessBody execution:", error);
// Fallback: As above, perform a simple replacement.
logger.warn("Falling back to simple replacement due to error.");
return bodyText.replace(/\{\{([^}]+)\}\}/g, (match, varName) => `"##VAR:string:${varName.trim()}##"`);
}
}
/**
* Formats a JavaScript object/array (potentially with {{var}} placeholders)
* into a pretty-printed JSON string.
* @param {*} data - The object/array/string to format.
* @return {string} A formatted JSON string. Returns input as string on error.
*/
export function postProcessFormattedJson(data) {
if (data === null || data === undefined) return '';
if (typeof data === 'string') {
// If it's already a string, assume it's formatted or doesn't need formatting.
// Could attempt to parse and re-stringify, but risky.
return data;
}
try {
// Stringify the object/array with pretty printing
return JSON.stringify(data, null, 2);
} catch (e) {
logger.error("Error stringifying data in postProcessFormattedJson:", e);
return String(data); // Fallback to basic string conversion
}
}
/**
* Formats a JSON string containing {{variable}} placeholders for display.
* Uses pre/post processing to handle placeholders during standard JSON formatting.
* Returns the original text if formatting fails.
* @param {string} bodyText - Raw JSON string with {{variable}} placeholders.
* @return {string} Pretty-printed JSON string with {{variable}} syntax, or original text on error.
*/
export function formatJson(bodyText) {
if (!bodyText || typeof bodyText !== 'string' || bodyText.trim() === '') {
return ''; // Return empty string for empty input
}
try {
// 1. Validate syntax roughly, allowing for unquoted {{vars}} for now
const validation = validateRequestBodyJson(bodyText);
if (!validation.valid) {
// Use the validation error directly if available
throw new Error(validation.message || 'Invalid JSON syntax before processing.');
}
// 2. Pre-process to replace {{vars}} with ##VAR:...## markers
const processedJsonString = preProcessBody(bodyText);
// 3. Parse the marker-filled string into a JS object
let parsedObject;
try {
parsedObject = JSON.parse(processedJsonString);
} catch(e) {
// If parsing fails even after preprocessing, the structure is likely fundamentally wrong
logger.error("JSON parsing failed even after preProcessing:", e.message, "Processed string:", processedJsonString);
throw new Error(`JSON parsing failed after attempting to handle placeholders. Check overall structure. Original error: ${e.message}`);
}
// 4. Decode markers ##VAR:...## back to {{var}} within the JS object structure
const decodedObject = decodeMarkersRecursive(parsedObject);
// 5. Stringify the decoded object with pretty printing
const finalFormattedJson = postProcessFormattedJson(decodedObject);
return finalFormattedJson;
} catch (error) {
logger.warn("JSON formatting failed:", error.message);
// Toast via the uiUtils bridge (no import: flowCore must not depend on UI modules).
document.dispatchEvent(new CustomEvent('flowrunner:toast', {
detail: { message: `Formatting error: ${error.message}. Check the JSON syntax.`, type: 'error' }
}));
return bodyText; // Return original on error to avoid data loss
}
}
/**
* Returns TRUE when an extraction path string is syntactically legal.
*
* Accepted forms ──────────────────────────────────────────────────────────
* · .status – the special status literal
* · status – same as above
* · body – whole body
* · body.id – inside body …
* · body.items[0].value – array access
* · headers – whole headers object
* · headers.Content‑Type – header look‑ups
* · id, user.profile.name, … – implicit root (data.body if present)
*
* Basically: letters, digits, _ , $ , . , [index] , 'string' , "string" and – in
* header names – the dash (‑). **NO white‑space, commas, parens, etc.**
*/
export function isValidExtractPath(p) {
if (!p || typeof p !== 'string') return false;
// one big (but still readable) regexp
const rx = new RegExp(
'^(' +
'(?:\\.status|status)' + // .status / status
'|body(?:\\.[a-zA-Z0-9_$\\.\\[\\]\'"\\-]+)?' + // body or body.…
'|headers?(?:\\.[a-zA-Z0-9_\\-]+)?' + // headers / headers.X
'|[a-zA-Z_$][a-zA-Z0-9_$]*(?:[\\.\\[][a-zA-Z0-9_$\'"\\]]*\\]?)*' + // id / arr[3].x
')$'
);
return rx.test(p);
}
/**
* Validates the entire flow model for common issues.
* Checks for required fields, undefined variable references, syntax errors, etc.
* @param {Object} flowModel - The flow model to validate.
* @return {{valid: boolean, errors: string[]}} Validation result.
*/
export function validateFlow(flowModel) {
const result = { valid: true, errors: [] };
if (!flowModel) {
return { valid: false, errors: ['Flow model is missing. Please create or load a flow before proceeding.'] };
}
/* ──────────────────────────────────────────────────────────────────
* 1. Flow‑level checks
* ────────────────────────────────────────────────────────────────── */
if (!flowModel.name?.trim()) {
result.valid = false;
result.errors.push('Flow name is required. Please enter a descriptive name for your flow.');
}
const initialVarNames = new Set(Object.keys(flowModel.staticVars || {}));
/* helper – variable reference scanner */
function checkVariableUsage(text, context, stepName, availableVars) {
if (!text || typeof text !== 'string') return;
const regex = /\{\{([^}]+)\}\}/g;
let match;
while ((match = regex.exec(text)) !== null) {
const varName = match[1].trim();
const baseName = varName.split('.')[0]; // accept slide.title, slide.foo.bar …
if (availableVars.has(baseName)) continue; // treat as defined if the base exists
if (!availableVars.has(varName)) {
result.valid = false;
const msg = `${stepName}: ${context} references undefined variable \"{{${varName}}}\".\n\nHint: Make sure this variable is defined earlier in the flow or as a static variable.`;
if (!result.errors.includes(msg)) result.errors.push(msg);
}
}
}
/* ──────────────────────────────────────────────────────────────────
* 2. Step recursion
* ────────────────────────────────────────────────────────────────── */
function validateStepsRecursive(steps, pathPrefix = '', availableVars = new Set()) {
if (!Array.isArray(steps)) return;
steps.forEach((step, idx) => {
const stepName = step.name || `Step ${idx + 1}`;
const here = pathPrefix ? `${pathPrefix} > ${stepName}` : stepName;
const varsDefinedHere = new Set();
/* — generic requirements — */
if (!step.name?.trim()) {
result.valid = false;
result.errors.push(`${here}: Step name is required. Please provide a descriptive name for this step.`);
}
if (!step.type) {
result.valid = false;
result.errors.push(`${here}: Step type is missing. This usually means the step is incomplete or corrupted. Please select a valid step type.`);
return;
}
/* — type‑specific validation — */
switch (step.type) {
/* ■■■ REQUEST ───────────────────────────────────────────── */
case 'request': {
/* URL */
if (!step.url) {
result.valid = false;
result.errors.push(`${here}: Request URL is required. Enter a valid URL (e.g., https://api.example.com/data).`);
} else {
checkVariableUsage(step.url, 'URL', here, availableVars);
}
/* Headers */
if (step.headers) {
Object.entries(step.headers).forEach(([k, v]) =>
checkVariableUsage(v, `Header \"${k}\"`, here, availableVars)
);
}
/* Body */
if (step.body && typeof step.body === 'string' && step.body.trim()) {
const bodyCheck = validateRequestBodyJson(step.body);
if (!bodyCheck.valid) {
result.valid = false;
result.errors.push(`${here}: Request body is not valid JSON.\n${bodyCheck.message || 'Check for missing commas, brackets, or quotes.'}`);
} else {
checkVariableUsage(step.body, 'Body', here, availableVars);
}
}
/* Extraction table */
if (step.extract) {
Object.entries(step.extract).forEach(([varName, jsonPath]) => {
/* variable name */
if (!varName?.trim() || !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(varName)) {
result.valid = false;
result.errors.push(`${here}: Extraction variable name \"${varName}\" is invalid.\n\nUse only letters, numbers, and underscores, and do not start with a number. Example: myVar1`);
} else {
varsDefinedHere.add(varName);
}
/* extraction path */
if (!jsonPath?.trim()) {
result.valid = false;
result.errors.push(`${here}: Extraction path for \"${varName}\" is required. Enter a valid path, e.g., body.data.token or .status.`);
} else if (!isValidExtractPath(jsonPath.trim())) {
result.valid = false;
result.errors.push(`${here}: Extraction path \"${jsonPath}\" for \"${varName}\" contains invalid characters.\n\nUse dot notation (body.field), array indices (body.items[0].id), or .status. No spaces or special characters allowed.`);
}
});
}
break;
}
/* ■■■ CONDITION ────────────────────────────────────────── */
case 'condition': {
const { variable, operator, value } = step.conditionData || {};
if (!variable || !operator) {
result.valid = false;
result.errors.push(`${here}: Condition step is missing a variable or operator. Please select both.`);
} else {
checkVariableUsage(`{{${variable}}}`, 'Condition variable', here, availableVars);
if (doesOperatorNeedValue(operator) && typeof value === 'string') {
checkVariableUsage(value, 'Condition value', here, availableVars);
}
}
/* recurse into THEN / ELSE */
validateStepsRecursive(step.thenSteps, `${here} > Then`, new Set(availableVars));
validateStepsRecursive(step.elseSteps, `${here} > Else`, new Set(availableVars));
break;
}
/* ■■■ LOOP ─────────────────────────────────────────────── */
case 'loop': {
const loopVar = step.loopVariable || 'item';
const currentPath = here;
const currentAvailableVars = new Set(availableVars);
/* source */
if (!step.source) {
result.valid = false;
result.errors.push(`${currentPath}: Loop source variable is required. Enter a variable or path to an array (e.g., items or body.data.items).`);
} else {
let sourceVar = step.source.trim();
if (sourceVar.startsWith('{{') && sourceVar.endsWith('}}')) {
sourceVar = sourceVar.slice(2, -2).trim();
}
if (!sourceVar) {
result.valid = false;
result.errors.push(`${currentPath}: Loop source variable is required. Enter a variable or path to an array.`);
} else if (!currentAvailableVars.has(sourceVar.split(/[.[]/)[0])) {
// Dotted/indexed paths (items.data, resp.items[0]) are valid loop
// sources — the runtime evaluates the path; only the base variable
// must be defined.
result.valid = false;
result.errors.push(`${currentPath}: Loop source references undefined variable \"{{${sourceVar}}}\".\n\nHint: Make sure this variable is defined earlier in the flow or as a static variable.`);
}
}
/* loop variable */
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(loopVar)) {
result.valid = false;
result.errors.push(`${currentPath}: Loop variable name \"${loopVar}\" is invalid.\n\nUse only letters, numbers, and underscores, and do not start with a number. Example: item1`);
}
const nestedScope = new Set(currentAvailableVars);
nestedScope.add(loopVar);
validateStepsRecursive(step.loopSteps, `${currentPath} > Loop Body`, nestedScope);
break;
}
/* ■■■ TRANSFORM ────────────────────────────────────────── */
case 'transform': {
if (!Array.isArray(step.ops)) {
result.valid = false;
result.errors.push(`${here}: Transform step requires an ops array.`);
break;
}
const opAvailableVars = new Set(availableVars);
step.ops.forEach((op, opIndex) => {
const opLabel = `${here}: Op ${opIndex + 1}`;
if (!op || typeof op !== 'object') {
result.valid = false;
result.errors.push(`${opLabel} is not a valid operation object.`);
return;
}
if (!isTransformOpName(op.op)) {
result.valid = false;
result.errors.push(`${opLabel}: Unsupported operation "${op.op}".`);
}
const def = TRANSFORM_OP_DEFS[op.op] || { args: [] };
if (!Array.isArray(op.args)) {
result.valid = false;
result.errors.push(`${opLabel}: args must be an array.`);
} else if (op.args.length < def.args.length) {
result.valid = false;
result.errors.push(`${opLabel}: Missing required arguments for "${op.op}".`);
}
if (op.options && typeof op.options !== 'object') {
result.valid = false;
result.errors.push(`${opLabel}: options must be an object.`);