@nordsym/apiclaw 2.2.0 → 2.2.1

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.
Files changed (176) hide show
  1. package/README.md +15 -2
  2. package/dist/bin-http.js +0 -0
  3. package/dist/bin.bundled.js +79288 -0
  4. package/dist/gateway-client.d.ts.map +1 -1
  5. package/dist/gateway-client.js +24 -2
  6. package/dist/gateway-client.js.map +1 -1
  7. package/dist/index.bundled.js +61263 -0
  8. package/dist/index.js +2 -2
  9. package/dist/index.js.map +1 -1
  10. package/package.json +7 -2
  11. package/.claude/settings.local.json +0 -13
  12. package/.env.prod +0 -1
  13. package/apiclaw-README.md +0 -494
  14. package/convex/_generated/api.d.ts +0 -145
  15. package/convex/_generated/api.js +0 -23
  16. package/convex/_generated/dataModel.d.ts +0 -60
  17. package/convex/_generated/server.d.ts +0 -143
  18. package/convex/_generated/server.js +0 -93
  19. package/convex/_listWorkspaces.ts +0 -13
  20. package/convex/adminActivate.ts +0 -53
  21. package/convex/adminStats.ts +0 -306
  22. package/convex/agents.ts +0 -939
  23. package/convex/analytics.ts +0 -187
  24. package/convex/apiKeys.ts +0 -220
  25. package/convex/backfillAnalytics.ts +0 -272
  26. package/convex/backfillSearchLogs.ts +0 -35
  27. package/convex/billing.ts +0 -834
  28. package/convex/capabilities.ts +0 -157
  29. package/convex/chains.ts +0 -1318
  30. package/convex/credits.ts +0 -211
  31. package/convex/crons.ts +0 -65
  32. package/convex/debugFilestackLogs.ts +0 -16
  33. package/convex/debugGetToken.ts +0 -18
  34. package/convex/directCall.ts +0 -713
  35. package/convex/earnProgress.ts +0 -753
  36. package/convex/email.ts +0 -329
  37. package/convex/feedback.ts +0 -265
  38. package/convex/funnel.ts +0 -431
  39. package/convex/guards.ts +0 -174
  40. package/convex/http.ts +0 -3756
  41. package/convex/inbound.ts +0 -32
  42. package/convex/logs.ts +0 -701
  43. package/convex/migrateFilestack.ts +0 -81
  44. package/convex/migratePartnersProd.ts +0 -174
  45. package/convex/migratePratham.ts +0 -126
  46. package/convex/migrateProviderWorkspaces.ts +0 -175
  47. package/convex/mou.ts +0 -91
  48. package/convex/nurture.ts +0 -355
  49. package/convex/providerKeys.ts +0 -289
  50. package/convex/providers.ts +0 -1135
  51. package/convex/purchases.ts +0 -183
  52. package/convex/ratelimit.ts +0 -104
  53. package/convex/schema.ts +0 -926
  54. package/convex/searchLogs.ts +0 -265
  55. package/convex/seedAPILayerAPIs.ts +0 -191
  56. package/convex/seedDirectCallConfigs.ts +0 -336
  57. package/convex/seedPratham.ts +0 -149
  58. package/convex/spendAlerts.ts +0 -442
  59. package/convex/stripeActions.ts +0 -607
  60. package/convex/teams.ts +0 -243
  61. package/convex/telemetry.ts +0 -81
  62. package/convex/tsconfig.json +0 -25
  63. package/convex/updateAPIStatus.ts +0 -44
  64. package/convex/usage.ts +0 -260
  65. package/convex/usageReports.ts +0 -357
  66. package/convex/waitlist.ts +0 -55
  67. package/convex/webhooks.ts +0 -494
  68. package/convex/workspaceSettings.ts +0 -143
  69. package/convex/workspaces.ts +0 -1331
  70. package/convex.json +0 -3
  71. package/direct-test.mjs +0 -51
  72. package/email-templates/filestack-provider-outreach.html +0 -162
  73. package/email-templates/partnership-template.html +0 -116
  74. package/email-templates/pratham-draft-preview.txt +0 -57
  75. package/email-templates/pratham-partnership-draft.html +0 -141
  76. package/reports/APIClaw-Session-Report-2026-04-05.pdf +0 -0
  77. package/reports/pipeline/PIPELINE-REPORT.json +0 -153
  78. package/reports/pipeline/acquire_apisguru.json +0 -17
  79. package/reports/pipeline/capabilities.json +0 -38
  80. package/reports/pipeline/discover_azure_recursive.json +0 -1551
  81. package/reports/pipeline/discover_github.json +0 -25
  82. package/reports/pipeline/discover_github_repos.json +0 -49
  83. package/reports/pipeline/discover_swaggerhub.json +0 -24
  84. package/reports/pipeline/discover_well_known.json +0 -23
  85. package/reports/pipeline/fetch_specs.json +0 -19
  86. package/reports/pipeline/generate_providers.json +0 -14
  87. package/reports/pipeline/match_registry.json +0 -11
  88. package/reports/pipeline/parse_specs.json +0 -17
  89. package/reports/pipeline/promote_candidates.json +0 -34
  90. package/reports/pipeline/validate.json +0 -30
  91. package/reports/pipeline/validate_smoke_details.json +0 -3835
  92. package/reports/session-report-2026-04-05.html +0 -433
  93. package/seed-apis-direct.mjs +0 -106
  94. package/src/access-control.ts +0 -174
  95. package/src/adapters/base.ts +0 -364
  96. package/src/adapters/claude-desktop.ts +0 -41
  97. package/src/adapters/cline.ts +0 -88
  98. package/src/adapters/continue.ts +0 -91
  99. package/src/adapters/cursor.ts +0 -43
  100. package/src/adapters/custom.ts +0 -188
  101. package/src/adapters/detect.ts +0 -202
  102. package/src/adapters/index.ts +0 -47
  103. package/src/adapters/windsurf.ts +0 -44
  104. package/src/bin-http.ts +0 -45
  105. package/src/bin.ts +0 -34
  106. package/src/capability-router.ts +0 -331
  107. package/src/chainExecutor.ts +0 -730
  108. package/src/chainResolver.test.ts +0 -246
  109. package/src/chainResolver.ts +0 -658
  110. package/src/cli/commands/demo.ts +0 -109
  111. package/src/cli/commands/doctor.ts +0 -435
  112. package/src/cli/commands/index.ts +0 -9
  113. package/src/cli/commands/login.ts +0 -203
  114. package/src/cli/commands/mcp-install.ts +0 -373
  115. package/src/cli/commands/restore.ts +0 -333
  116. package/src/cli/commands/setup.ts +0 -297
  117. package/src/cli/commands/uninstall.ts +0 -240
  118. package/src/cli/index.ts +0 -148
  119. package/src/cli.ts +0 -370
  120. package/src/confirmation.ts +0 -296
  121. package/src/credentials.ts +0 -455
  122. package/src/credits.ts +0 -329
  123. package/src/crypto.ts +0 -75
  124. package/src/discovery.ts +0 -568
  125. package/src/enterprise/env.ts +0 -156
  126. package/src/enterprise/index.ts +0 -7
  127. package/src/enterprise/script-generator.ts +0 -481
  128. package/src/execute-dynamic.ts +0 -617
  129. package/src/execute.ts +0 -2386
  130. package/src/funnel-client.ts +0 -168
  131. package/src/funnel.test.ts +0 -187
  132. package/src/gateway-client.ts +0 -192
  133. package/src/hivr-whitelist.ts +0 -110
  134. package/src/http-api.ts +0 -286
  135. package/src/http-server-minimal.ts +0 -154
  136. package/src/index.ts +0 -2702
  137. package/src/intelligent-gateway.ts +0 -339
  138. package/src/mcp-analytics.ts +0 -156
  139. package/src/metered.ts +0 -149
  140. package/src/open-apis-generated.ts +0 -157
  141. package/src/open-apis.ts +0 -558
  142. package/src/postinstall.ts +0 -40
  143. package/src/product-whitelist.ts +0 -246
  144. package/src/proxy.ts +0 -36
  145. package/src/registration-guard.ts +0 -117
  146. package/src/session.ts +0 -129
  147. package/src/stripe.ts +0 -497
  148. package/src/telemetry.ts +0 -71
  149. package/src/test.ts +0 -135
  150. package/src/types/convex-api.d.ts +0 -20
  151. package/src/types/convex-api.ts +0 -21
  152. package/src/types.ts +0 -109
  153. package/src/ui/colors.ts +0 -219
  154. package/src/ui/errors.ts +0 -394
  155. package/src/ui/index.ts +0 -17
  156. package/src/ui/prompts.ts +0 -390
  157. package/src/ui/spinner.ts +0 -325
  158. package/src/utils/backup.ts +0 -224
  159. package/src/utils/config.ts +0 -318
  160. package/src/utils/os.ts +0 -124
  161. package/src/utils/paths.ts +0 -203
  162. package/src/webhook.ts +0 -107
  163. package/test-10-working.cjs +0 -97
  164. package/test-14-final.cjs +0 -96
  165. package/test-actual-handlers.ts +0 -92
  166. package/test-apilayer-all-14.ts +0 -249
  167. package/test-apilayer-fixed.ts +0 -248
  168. package/test-direct-endpoints.ts +0 -174
  169. package/test-exact-endpoints.ts +0 -144
  170. package/test-final.ts +0 -83
  171. package/test-full-routing.ts +0 -100
  172. package/test-handlers-correct.ts +0 -217
  173. package/test-numverify-key.ts +0 -41
  174. package/test-via-handlers.ts +0 -92
  175. package/test-worldnews.mjs +0 -26
  176. package/tsconfig.json +0 -20
@@ -1,658 +0,0 @@
1
- /**
2
- * APIClaw Chain Resolver
3
- *
4
- * Handles reference resolution between chain steps.
5
- * Supports: $prev, $stepId, $parallel, $chain, $inputs, $forEach
6
- */
7
-
8
- // ============================================================================
9
- // TYPES
10
- // ============================================================================
11
-
12
- export interface ChainContext {
13
- results: Record<string, any>; // stepId → result data
14
- inputs?: Record<string, any>; // Template inputs ($inputs.x)
15
- currentIndex: number; // Current step index
16
- startedAt: string; // ISO timestamp
17
- prevStepId?: string; // ID of previous step (for $prev)
18
- parallelResults?: any[]; // Results from parallel execution ($parallel)
19
- forEachItem?: any; // Current item in forEach loop ($item)
20
- forEachIndex?: number; // Current index in forEach loop ($index)
21
- forEachResults?: any[]; // Accumulated forEach results ($forEach.results)
22
- env?: Record<string, string>; // Environment variables ($env.x)
23
- }
24
-
25
- export interface Reference {
26
- raw: string; // Original reference string (e.g., "$step1.url")
27
- type: 'prev' | 'step' | 'parallel' | 'chain' | 'inputs' | 'forEach' | 'item' | 'index' | 'env';
28
- stepId?: string; // For step references
29
- path: string[]; // Property path (e.g., ["output", "url"])
30
- arrayIndex?: number; // For array access (e.g., $parallel[0])
31
- }
32
-
33
- export interface ValidationResult {
34
- valid: boolean;
35
- errors: ValidationError[];
36
- warnings: ValidationWarning[];
37
- }
38
-
39
- export interface ValidationError {
40
- stepId: string;
41
- reference: string;
42
- message: string;
43
- }
44
-
45
- export interface ValidationWarning {
46
- stepId: string;
47
- reference: string;
48
- message: string;
49
- }
50
-
51
- // Chain definition types
52
- export interface ChainStep {
53
- id: string;
54
- provider: string;
55
- action: string;
56
- params?: Record<string, any>;
57
- onError?: ErrorConfig;
58
- }
59
-
60
- export interface ParallelStep {
61
- parallel: ChainStep[];
62
- }
63
-
64
- export interface ConditionalStep {
65
- if: string;
66
- then: ChainStep | ChainStep[];
67
- else?: ChainStep | ChainStep[];
68
- }
69
-
70
- export interface ForEachStep {
71
- forEach: string;
72
- as?: string;
73
- do: ChainStep;
74
- }
75
-
76
- export type ChainStepUnion = ChainStep | ParallelStep | ConditionalStep | ForEachStep;
77
-
78
- export interface ErrorConfig {
79
- retry?: { attempts: number; backoff?: number[] };
80
- fallback?: ChainStep;
81
- abort?: boolean;
82
- }
83
-
84
- // ============================================================================
85
- // REFERENCE PATTERNS
86
- // ============================================================================
87
-
88
- // Matches all $references in a string
89
- const REFERENCE_PATTERN = /\$(?:prev|chain|inputs|parallel|forEach|item|index|env|\w+)(?:\[\d+\])?(?:\.[a-zA-Z_][\w]*(?:\[\d+\])?)*|\$\w+(?:\.[a-zA-Z_][\w]*(?:\[\d+\])?)+/g;
90
-
91
- // Matches a single complete reference for parsing
92
- const SINGLE_REF_PATTERN = /^\$(\w+)(\[\d+\])?(.*)$/;
93
-
94
- // ============================================================================
95
- // REFERENCE EXTRACTION
96
- // ============================================================================
97
-
98
- /**
99
- * Extract all $references from a string
100
- */
101
- export function extractReferences(text: string): Reference[] {
102
- const matches = text.match(REFERENCE_PATTERN);
103
- if (!matches) return [];
104
-
105
- return matches.map(parseReference).filter((r): r is Reference => r !== null);
106
- }
107
-
108
- /**
109
- * Parse a single reference string into a Reference object
110
- */
111
- export function parseReference(raw: string): Reference | null {
112
- const match = raw.match(SINGLE_REF_PATTERN);
113
- if (!match) return null;
114
-
115
- const [, rootName, arrayPart, pathPart] = match;
116
-
117
- // Determine reference type
118
- let type: Reference['type'];
119
- let stepId: string | undefined;
120
- let arrayIndex: number | undefined;
121
-
122
- // Parse array index from root (e.g., $parallel[0])
123
- if (arrayPart) {
124
- arrayIndex = parseInt(arrayPart.slice(1, -1), 10);
125
- }
126
-
127
- switch (rootName) {
128
- case 'prev':
129
- type = 'prev';
130
- break;
131
- case 'chain':
132
- type = 'chain';
133
- break;
134
- case 'inputs':
135
- type = 'inputs';
136
- break;
137
- case 'parallel':
138
- type = 'parallel';
139
- break;
140
- case 'forEach':
141
- type = 'forEach';
142
- break;
143
- case 'item':
144
- type = 'item';
145
- break;
146
- case 'index':
147
- type = 'index';
148
- break;
149
- case 'env':
150
- type = 'env';
151
- break;
152
- default:
153
- // It's a step ID reference
154
- type = 'step';
155
- stepId = rootName;
156
- }
157
-
158
- // Parse the property path
159
- const path = parsePath(pathPart || '');
160
-
161
- return { raw, type, stepId, path, arrayIndex };
162
- }
163
-
164
- /**
165
- * Parse a property path like ".output.images[0].url" into ["output", "images", 0, "url"]
166
- */
167
- function parsePath(pathStr: string): string[] {
168
- if (!pathStr) return [];
169
-
170
- const parts: string[] = [];
171
- // Remove leading dot
172
- const cleaned = pathStr.startsWith('.') ? pathStr.slice(1) : pathStr;
173
-
174
- // Split by dots and brackets
175
- const segments = cleaned.split(/\.|\[|\]/g).filter(Boolean);
176
-
177
- for (const segment of segments) {
178
- // Check if it's a numeric index
179
- if (/^\d+$/.test(segment)) {
180
- parts.push(segment); // Keep as string, will convert during resolution
181
- } else {
182
- parts.push(segment);
183
- }
184
- }
185
-
186
- return parts;
187
- }
188
-
189
- // ============================================================================
190
- // REFERENCE RESOLUTION
191
- // ============================================================================
192
-
193
- /**
194
- * Resolve a reference to its actual value from context
195
- */
196
- export function resolveReference(ref: Reference, context: ChainContext): any {
197
- let value: any;
198
-
199
- switch (ref.type) {
200
- case 'prev':
201
- // Get the previous step's result
202
- if (!context.prevStepId) {
203
- throw new ReferenceError(`$prev used but no previous step exists`);
204
- }
205
- value = context.results[context.prevStepId];
206
- break;
207
-
208
- case 'step':
209
- // Get a specific step's result
210
- if (!ref.stepId || !(ref.stepId in context.results)) {
211
- throw new ReferenceError(`Step '${ref.stepId}' not found in results`);
212
- }
213
- value = context.results[ref.stepId];
214
- break;
215
-
216
- case 'parallel':
217
- // Get parallel execution results
218
- if (!context.parallelResults) {
219
- throw new ReferenceError(`$parallel used but not in parallel context`);
220
- }
221
- if (ref.arrayIndex !== undefined) {
222
- value = context.parallelResults[ref.arrayIndex];
223
- } else {
224
- value = context.parallelResults;
225
- }
226
- break;
227
-
228
- case 'chain':
229
- // Built-in chain variables
230
- value = {
231
- startedAt: context.startedAt,
232
- index: context.currentIndex,
233
- };
234
- break;
235
-
236
- case 'inputs':
237
- // Template inputs
238
- if (!context.inputs) {
239
- throw new ReferenceError(`$inputs used but no inputs provided`);
240
- }
241
- value = context.inputs;
242
- break;
243
-
244
- case 'forEach':
245
- // forEach accumulated results
246
- value = {
247
- results: context.forEachResults || [],
248
- index: context.forEachIndex,
249
- };
250
- break;
251
-
252
- case 'item':
253
- // Current forEach item
254
- if (context.forEachItem === undefined) {
255
- throw new ReferenceError(`$item used but not in forEach loop`);
256
- }
257
- value = context.forEachItem;
258
- break;
259
-
260
- case 'index':
261
- // Current forEach index
262
- if (context.forEachIndex === undefined) {
263
- throw new ReferenceError(`$index used but not in forEach loop`);
264
- }
265
- value = context.forEachIndex;
266
- break;
267
-
268
- case 'env':
269
- // Environment variables
270
- value = context.env || process.env;
271
- break;
272
-
273
- default:
274
- throw new ReferenceError(`Unknown reference type: ${ref.type}`);
275
- }
276
-
277
- // Traverse the path
278
- return traversePath(value, ref.path, ref.raw);
279
- }
280
-
281
- /**
282
- * Traverse a path through an object/array
283
- */
284
- function traversePath(obj: any, path: string[], originalRef: string): any {
285
- let current = obj;
286
-
287
- for (let i = 0; i < path.length; i++) {
288
- if (current === null || current === undefined) {
289
- const traversed = path.slice(0, i).join('.');
290
- throw new ReferenceError(
291
- `Cannot read '${path[i]}' of ${current} at ${originalRef} (traversed: ${traversed})`
292
- );
293
- }
294
-
295
- const key = path[i];
296
-
297
- // Handle array index (stored as string)
298
- if (/^\d+$/.test(key)) {
299
- current = current[parseInt(key, 10)];
300
- } else {
301
- current = current[key];
302
- }
303
- }
304
-
305
- return current;
306
- }
307
-
308
- // ============================================================================
309
- // RESOLVE ALL REFERENCES IN PARAMS
310
- // ============================================================================
311
-
312
- /**
313
- * Resolve all $references in a params object
314
- * Recursively processes strings, arrays, and nested objects
315
- */
316
- export function resolveReferences(params: Record<string, any>, context: ChainContext): Record<string, any> {
317
- return resolveValue(params, context) as Record<string, any>;
318
- }
319
-
320
- /**
321
- * Resolve references in any value type
322
- */
323
- function resolveValue(value: any, context: ChainContext): any {
324
- if (value === null || value === undefined) {
325
- return value;
326
- }
327
-
328
- if (typeof value === 'string') {
329
- return resolveStringValue(value, context);
330
- }
331
-
332
- if (Array.isArray(value)) {
333
- return value.map(item => resolveValue(item, context));
334
- }
335
-
336
- if (typeof value === 'object') {
337
- const resolved: Record<string, any> = {};
338
- for (const [key, val] of Object.entries(value)) {
339
- resolved[key] = resolveValue(val, context);
340
- }
341
- return resolved;
342
- }
343
-
344
- // Primitives (number, boolean) pass through
345
- return value;
346
- }
347
-
348
- /**
349
- * Resolve references in a string value
350
- * Handles both full replacement and interpolation
351
- */
352
- function resolveStringValue(str: string, context: ChainContext): any {
353
- const refs = extractReferences(str);
354
-
355
- if (refs.length === 0) {
356
- return str;
357
- }
358
-
359
- // If the entire string is a single reference, return the resolved value directly
360
- // This preserves types (objects, arrays, numbers)
361
- if (refs.length === 1 && refs[0].raw === str) {
362
- return resolveReference(refs[0], context);
363
- }
364
-
365
- // Multiple references or mixed content: interpolate into string
366
- let result = str;
367
- for (const ref of refs) {
368
- const resolved = resolveReference(ref, context);
369
- const stringValue = typeof resolved === 'object' ? JSON.stringify(resolved) : String(resolved);
370
- result = result.replace(ref.raw, stringValue);
371
- }
372
-
373
- return result;
374
- }
375
-
376
- // ============================================================================
377
- // VALIDATION
378
- // ============================================================================
379
-
380
- /**
381
- * Validate all references in a chain before execution
382
- * Checks that referenced steps exist and come before the referencing step
383
- */
384
- export function validateReferences(steps: ChainStepUnion[]): ValidationResult {
385
- const errors: ValidationError[] = [];
386
- const warnings: ValidationWarning[] = [];
387
-
388
- // Build set of step IDs that will exist
389
- const definedSteps = new Set<string>();
390
- const stepOrder: string[] = [];
391
-
392
- // First pass: collect all step IDs
393
- for (const step of steps) {
394
- if ('parallel' in step) {
395
- for (const ps of step.parallel) {
396
- definedSteps.add(ps.id);
397
- stepOrder.push(ps.id);
398
- }
399
- } else if ('forEach' in step) {
400
- // forEach generates dynamic IDs
401
- definedSteps.add(step.do.id.replace(/\$index/g, '*'));
402
- } else if ('if' in step) {
403
- const collectConditional = (s: ChainStep | ChainStep[]) => {
404
- const arr = Array.isArray(s) ? s : [s];
405
- for (const cs of arr) {
406
- definedSteps.add(cs.id);
407
- stepOrder.push(cs.id);
408
- }
409
- };
410
- collectConditional(step.then);
411
- if (step.else) collectConditional(step.else);
412
- } else {
413
- definedSteps.add(step.id);
414
- stepOrder.push(step.id);
415
- }
416
- }
417
-
418
- // Second pass: validate references
419
- let currentIndex = 0;
420
-
421
- const validateStep = (step: ChainStep, availableSteps: Set<string>, stepIdx: number) => {
422
- if (!step.params) return;
423
-
424
- const paramStr = JSON.stringify(step.params);
425
- const refs = extractReferences(paramStr);
426
-
427
- for (const ref of refs) {
428
- // Validate step references
429
- if (ref.type === 'step' && ref.stepId) {
430
- if (!definedSteps.has(ref.stepId)) {
431
- errors.push({
432
- stepId: step.id,
433
- reference: ref.raw,
434
- message: `References undefined step '${ref.stepId}'`,
435
- });
436
- } else if (!availableSteps.has(ref.stepId)) {
437
- errors.push({
438
- stepId: step.id,
439
- reference: ref.raw,
440
- message: `References step '${ref.stepId}' which hasn't executed yet`,
441
- });
442
- }
443
- }
444
-
445
- // Validate $prev usage
446
- if (ref.type === 'prev' && stepIdx === 0) {
447
- errors.push({
448
- stepId: step.id,
449
- reference: ref.raw,
450
- message: `$prev used in first step (no previous step exists)`,
451
- });
452
- }
453
-
454
- // Warn about $parallel outside parallel context
455
- if (ref.type === 'parallel') {
456
- warnings.push({
457
- stepId: step.id,
458
- reference: ref.raw,
459
- message: `$parallel used - ensure this follows a parallel block`,
460
- });
461
- }
462
-
463
- // Warn about $item/$index outside forEach
464
- if (ref.type === 'item' || ref.type === 'index') {
465
- warnings.push({
466
- stepId: step.id,
467
- reference: ref.raw,
468
- message: `${ref.raw} used - ensure this is inside a forEach block`,
469
- });
470
- }
471
- }
472
- };
473
-
474
- const availableSteps = new Set<string>();
475
-
476
- for (const step of steps) {
477
- if ('parallel' in step) {
478
- // Parallel steps can't reference each other
479
- for (const ps of step.parallel) {
480
- validateStep(ps, availableSteps, currentIndex);
481
- }
482
- // After parallel, all are available
483
- for (const ps of step.parallel) {
484
- availableSteps.add(ps.id);
485
- }
486
- } else if ('forEach' in step) {
487
- validateStep(step.do, availableSteps, currentIndex);
488
- } else if ('if' in step) {
489
- const validateBranch = (s: ChainStep | ChainStep[]) => {
490
- const arr = Array.isArray(s) ? s : [s];
491
- for (const cs of arr) {
492
- validateStep(cs, availableSteps, currentIndex);
493
- availableSteps.add(cs.id);
494
- }
495
- };
496
- validateBranch(step.then);
497
- if (step.else) validateBranch(step.else);
498
- } else {
499
- validateStep(step, availableSteps, currentIndex);
500
- availableSteps.add(step.id);
501
- }
502
-
503
- currentIndex++;
504
- }
505
-
506
- return {
507
- valid: errors.length === 0,
508
- errors,
509
- warnings,
510
- };
511
- }
512
-
513
- // ============================================================================
514
- // CONDITIONAL EVALUATION
515
- // ============================================================================
516
-
517
- /**
518
- * Evaluate a conditional expression string
519
- * Supports: ===, !==, >, <, >=, <=, &&, ||, !
520
- */
521
- export function evaluateCondition(condition: string, context: ChainContext): boolean {
522
- // First resolve any references in the condition
523
- const resolvedCondition = resolveStringValue(condition, context);
524
-
525
- // Simple expression evaluation
526
- // For security, we use a restricted evaluator instead of eval()
527
- try {
528
- return safeEvaluate(resolvedCondition, context);
529
- } catch (e) {
530
- throw new Error(`Failed to evaluate condition '${condition}': ${e}`);
531
- }
532
- }
533
-
534
- /**
535
- * Safe expression evaluator for conditions
536
- * Only allows comparison and logical operators
537
- */
538
- function safeEvaluate(expr: string, context: ChainContext): boolean {
539
- // Trim whitespace
540
- expr = expr.trim();
541
-
542
- // Handle logical operators (lowest precedence)
543
- // Split on || first (lowest precedence)
544
- if (expr.includes('||')) {
545
- const parts = splitOnOperator(expr, '||');
546
- if (parts.length > 1) {
547
- return parts.some(part => safeEvaluate(part, context));
548
- }
549
- }
550
-
551
- // Then && (higher precedence than ||)
552
- if (expr.includes('&&')) {
553
- const parts = splitOnOperator(expr, '&&');
554
- if (parts.length > 1) {
555
- return parts.every(part => safeEvaluate(part, context));
556
- }
557
- }
558
-
559
- // Handle negation
560
- if (expr.startsWith('!')) {
561
- return !safeEvaluate(expr.slice(1), context);
562
- }
563
-
564
- // Handle parentheses
565
- if (expr.startsWith('(') && expr.endsWith(')')) {
566
- return safeEvaluate(expr.slice(1, -1), context);
567
- }
568
-
569
- // Handle comparison operators
570
- const comparisons = ['===', '!==', '>=', '<=', '>', '<', '==', '!='];
571
- for (const op of comparisons) {
572
- const idx = expr.indexOf(op);
573
- if (idx !== -1) {
574
- const left = parseValue(expr.slice(0, idx).trim());
575
- const right = parseValue(expr.slice(idx + op.length).trim());
576
-
577
- switch (op) {
578
- case '===': return left === right;
579
- case '!==': return left !== right;
580
- case '==': return left == right;
581
- case '!=': return left != right;
582
- case '>': return left > right;
583
- case '<': return left < right;
584
- case '>=': return left >= right;
585
- case '<=': return left <= right;
586
- }
587
- }
588
- }
589
-
590
- // If no operators, evaluate as truthy/falsy
591
- return !!parseValue(expr);
592
- }
593
-
594
- /**
595
- * Split expression on operator, respecting parentheses
596
- */
597
- function splitOnOperator(expr: string, op: string): string[] {
598
- const parts: string[] = [];
599
- let depth = 0;
600
- let current = '';
601
-
602
- for (let i = 0; i < expr.length; i++) {
603
- const char = expr[i];
604
-
605
- if (char === '(') depth++;
606
- else if (char === ')') depth--;
607
-
608
- if (depth === 0 && expr.slice(i, i + op.length) === op) {
609
- parts.push(current);
610
- current = '';
611
- i += op.length - 1;
612
- } else {
613
- current += char;
614
- }
615
- }
616
-
617
- if (current) parts.push(current);
618
- return parts;
619
- }
620
-
621
- /**
622
- * Parse a value string into its actual type
623
- */
624
- function parseValue(str: string): any {
625
- str = str.trim();
626
-
627
- // String literals
628
- if ((str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'"))) {
629
- return str.slice(1, -1);
630
- }
631
-
632
- // Numbers
633
- if (/^-?\d+\.?\d*$/.test(str)) {
634
- return parseFloat(str);
635
- }
636
-
637
- // Booleans
638
- if (str === 'true') return true;
639
- if (str === 'false') return false;
640
-
641
- // Null/undefined
642
- if (str === 'null') return null;
643
- if (str === 'undefined') return undefined;
644
-
645
- // Already resolved value (from reference resolution)
646
- return str;
647
- }
648
-
649
- // ============================================================================
650
- // CUSTOM ERROR CLASS
651
- // ============================================================================
652
-
653
- export class ReferenceError extends Error {
654
- constructor(message: string) {
655
- super(message);
656
- this.name = 'ReferenceError';
657
- }
658
- }