@mondaydotcomorg/atp-compiler 0.19.22 → 0.19.26

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/dist/index.js CHANGED
@@ -837,15 +837,102 @@ var BatchParallelDetector = class {
837
837
  return void 0;
838
838
  }
839
839
  };
840
- function findLLMCallExpression(body) {
841
- let found = null;
840
+ function collectReferencedIdentifiers(node) {
841
+ const identifiers = /* @__PURE__ */ new Set();
842
+ const visit = /* @__PURE__ */ __name((n) => {
843
+ if (t7.isIdentifier(n)) {
844
+ identifiers.add(n.name);
845
+ }
846
+ Object.keys(n).forEach((key) => {
847
+ if (key === "type" || key === "loc" || key === "start" || key === "end") return;
848
+ const value = n[key];
849
+ if (Array.isArray(value)) {
850
+ value.forEach((item) => {
851
+ if (item && typeof item === "object" && item.type) {
852
+ visit(item);
853
+ }
854
+ });
855
+ } else if (value && typeof value === "object" && value.type) {
856
+ visit(value);
857
+ }
858
+ });
859
+ }, "visit");
860
+ visit(node);
861
+ return identifiers;
862
+ }
863
+ __name(collectReferencedIdentifiers, "collectReferencedIdentifiers");
864
+ function collectLocalVariables(body) {
865
+ const locals = /* @__PURE__ */ new Set();
866
+ const visit = /* @__PURE__ */ __name((node) => {
867
+ if (t7.isVariableDeclaration(node)) {
868
+ for (const decl of node.declarations) {
869
+ if (t7.isIdentifier(decl.id)) {
870
+ locals.add(decl.id.name);
871
+ } else if (t7.isObjectPattern(decl.id)) {
872
+ for (const prop of decl.id.properties) {
873
+ if (t7.isObjectProperty(prop) && t7.isIdentifier(prop.value)) {
874
+ locals.add(prop.value.name);
875
+ } else if (t7.isRestElement(prop) && t7.isIdentifier(prop.argument)) {
876
+ locals.add(prop.argument.name);
877
+ }
878
+ }
879
+ } else if (t7.isArrayPattern(decl.id)) {
880
+ for (const elem of decl.id.elements) {
881
+ if (t7.isIdentifier(elem)) {
882
+ locals.add(elem.name);
883
+ }
884
+ }
885
+ }
886
+ }
887
+ }
888
+ if (t7.isFunction(node)) {
889
+ return;
890
+ }
891
+ Object.keys(node).forEach((key) => {
892
+ if (key === "type" || key === "loc" || key === "start" || key === "end") return;
893
+ const value = node[key];
894
+ if (Array.isArray(value)) {
895
+ value.forEach((item) => {
896
+ if (item && typeof item === "object" && item.type) {
897
+ visit(item);
898
+ }
899
+ });
900
+ } else if (value && typeof value === "object" && value.type) {
901
+ visit(value);
902
+ }
903
+ });
904
+ }, "visit");
905
+ visit(body);
906
+ return locals;
907
+ }
908
+ __name(collectLocalVariables, "collectLocalVariables");
909
+ function hasLLMCallDependencies(body, batchDetector, itemParamName) {
910
+ const localVariables = collectLocalVariables(body);
911
+ if (localVariables.size === 0) {
912
+ return false;
913
+ }
914
+ const allCalls = findAllAwaitedMemberCalls(body);
915
+ for (const call of allCalls) {
916
+ const payloadNode = batchDetector.extractPayloadNode(call);
917
+ if (payloadNode) {
918
+ const referencedIds = collectReferencedIdentifiers(payloadNode);
919
+ for (const id of referencedIds) {
920
+ if (localVariables.has(id) && id !== itemParamName) {
921
+ return true;
922
+ }
923
+ }
924
+ }
925
+ }
926
+ return false;
927
+ }
928
+ __name(hasLLMCallDependencies, "hasLLMCallDependencies");
929
+ function findAllAwaitedMemberCalls(body) {
930
+ const calls = [];
842
931
  const visit = /* @__PURE__ */ __name((node) => {
843
- if (found) return;
844
932
  if (t7.isAwaitExpression(node) && t7.isCallExpression(node.argument)) {
845
933
  const call = node.argument;
846
934
  if (t7.isMemberExpression(call.callee)) {
847
- found = call;
848
- return;
935
+ calls.push(call);
849
936
  }
850
937
  }
851
938
  Object.keys(node).forEach((key) => {
@@ -862,7 +949,30 @@ function findLLMCallExpression(body) {
862
949
  });
863
950
  }, "visit");
864
951
  visit(body);
865
- return found;
952
+ return calls;
953
+ }
954
+ __name(findAllAwaitedMemberCalls, "findAllAwaitedMemberCalls");
955
+ function findAllLLMCallExpressions(body, batchDetector) {
956
+ const allCalls = findAllAwaitedMemberCalls(body);
957
+ const llmCalls = [];
958
+ for (const call of allCalls) {
959
+ const callInfo = batchDetector.extractCallInfo(call);
960
+ const payloadNode = batchDetector.extractPayloadNode(call);
961
+ if (callInfo && payloadNode) {
962
+ llmCalls.push({
963
+ callNode: call,
964
+ callInfo,
965
+ payloadNode,
966
+ key: `${callInfo.type}:${callInfo.operation}`
967
+ });
968
+ }
969
+ }
970
+ return llmCalls;
971
+ }
972
+ __name(findAllLLMCallExpressions, "findAllLLMCallExpressions");
973
+ function findLLMCallExpression(body) {
974
+ const calls = findAllAwaitedMemberCalls(body);
975
+ return calls[0] ?? null;
866
976
  }
867
977
  __name(findLLMCallExpression, "findLLMCallExpression");
868
978
  function getArrayMethodName(node) {
@@ -1239,6 +1349,114 @@ function transformToSequential(path, node, methodName, callback, onTransform) {
1239
1349
  return true;
1240
1350
  }
1241
1351
  __name(transformToSequential, "transformToSequential");
1352
+ var traverse2 = typeof _traverse.default === "function" ? _traverse.default : _traverse;
1353
+ function transformToBatchWithReconstruction(path, node, methodName, callback, batchDetector, onTransform) {
1354
+ if (methodName !== "map") {
1355
+ return false;
1356
+ }
1357
+ const paramName = callback.params[0];
1358
+ if (!t7.isIdentifier(paramName)) {
1359
+ return false;
1360
+ }
1361
+ const param = paramName.name;
1362
+ const array = node.callee.object;
1363
+ const llmCalls = findAllLLMCallExpressions(callback.body, batchDetector);
1364
+ if (llmCalls.length === 0) {
1365
+ return false;
1366
+ }
1367
+ const methodId = generateUniqueId(`${methodName}_batch_reconstruct`);
1368
+ const originalIndexParam = callback.params[1];
1369
+ const indexVar = originalIndexParam && t7.isIdentifier(originalIndexParam) ? originalIndexParam.name : "__idx";
1370
+ const batchDeclarations = [];
1371
+ const resultVarByCallIndex = /* @__PURE__ */ new Map();
1372
+ for (let i = 0; i < llmCalls.length; i++) {
1373
+ const call = llmCalls[i];
1374
+ const resultsVar = `__batch_results_${i}_${methodId.replace(/[^a-zA-Z0-9]/g, "_")}`;
1375
+ resultVarByCallIndex.set(i, resultsVar);
1376
+ const payloadMapper = t7.arrowFunctionExpression([
1377
+ t7.identifier(param)
1378
+ ], t7.objectExpression([
1379
+ t7.objectProperty(t7.identifier("type"), t7.stringLiteral(call.callInfo.type)),
1380
+ t7.objectProperty(t7.identifier("operation"), t7.stringLiteral(call.callInfo.operation)),
1381
+ t7.objectProperty(t7.identifier("payload"), t7.cloneNode(call.payloadNode, true))
1382
+ ]));
1383
+ const batchCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("batchParallel")), [
1384
+ t7.callExpression(t7.memberExpression(t7.cloneNode(array, true), t7.identifier("map")), [
1385
+ payloadMapper
1386
+ ]),
1387
+ t7.stringLiteral(`${methodId}_${i}`)
1388
+ ]));
1389
+ batchDeclarations.push(t7.variableDeclaration("const", [
1390
+ t7.variableDeclarator(t7.identifier(resultsVar), batchCall)
1391
+ ]));
1392
+ }
1393
+ const clonedBody = t7.cloneNode(callback.body, true);
1394
+ let traversableNode;
1395
+ if (t7.isBlockStatement(clonedBody)) {
1396
+ traversableNode = t7.functionDeclaration(t7.identifier("__temp"), [], clonedBody);
1397
+ } else {
1398
+ traversableNode = t7.expressionStatement(clonedBody);
1399
+ }
1400
+ let replacementCount = 0;
1401
+ traverse2(t7.file(t7.program([
1402
+ traversableNode
1403
+ ])), {
1404
+ AwaitExpression(awaitPath) {
1405
+ const arg = awaitPath.node.argument;
1406
+ if (!t7.isCallExpression(arg)) return;
1407
+ const info = batchDetector.extractCallInfo(arg);
1408
+ if (!info) return;
1409
+ const key = `${info.type}:${info.operation}`;
1410
+ let matchedIndex = -1;
1411
+ for (let i = 0; i < llmCalls.length; i++) {
1412
+ const original = llmCalls[i];
1413
+ if (original.key === key && resultVarByCallIndex.has(i)) {
1414
+ matchedIndex = i;
1415
+ break;
1416
+ }
1417
+ }
1418
+ if (matchedIndex === -1) return;
1419
+ const resultsVar = resultVarByCallIndex.get(matchedIndex);
1420
+ if (!resultsVar) return;
1421
+ resultVarByCallIndex.delete(matchedIndex);
1422
+ const resultAccess = t7.memberExpression(t7.identifier(resultsVar), t7.identifier(indexVar), true);
1423
+ awaitPath.replaceWith(resultAccess);
1424
+ replacementCount++;
1425
+ },
1426
+ noScope: true
1427
+ });
1428
+ if (replacementCount === 0) {
1429
+ return false;
1430
+ }
1431
+ let reconstructBody;
1432
+ if (t7.isBlockStatement(clonedBody)) {
1433
+ reconstructBody = clonedBody;
1434
+ } else {
1435
+ reconstructBody = clonedBody;
1436
+ }
1437
+ const reconstructMapper = t7.arrowFunctionExpression([
1438
+ t7.identifier(param),
1439
+ t7.identifier(indexVar)
1440
+ ], reconstructBody);
1441
+ reconstructMapper.async = false;
1442
+ const reconstructCall = t7.callExpression(t7.memberExpression(t7.cloneNode(array, true), t7.identifier("map")), [
1443
+ reconstructMapper
1444
+ ]);
1445
+ const iife = t7.callExpression(t7.arrowFunctionExpression(
1446
+ [],
1447
+ t7.blockStatement([
1448
+ ...batchDeclarations,
1449
+ t7.returnStatement(reconstructCall)
1450
+ ]),
1451
+ true
1452
+ // async
1453
+ ), []);
1454
+ const awaitIife = t7.awaitExpression(iife);
1455
+ path.replaceWith(awaitIife);
1456
+ onTransform();
1457
+ return true;
1458
+ }
1459
+ __name(transformToBatchWithReconstruction, "transformToBatchWithReconstruction");
1242
1460
 
1243
1461
  // src/transformer/array-transformer.ts
1244
1462
  var ArrayTransformer = class {
@@ -1265,6 +1483,22 @@ var ArrayTransformer = class {
1265
1483
  return false;
1266
1484
  }
1267
1485
  const batchResult = this.batchOptimizer.canBatchArrayMethod(callback);
1486
+ if (!batchResult.canBatch && methodName === "map") {
1487
+ const reason = batchResult.reason || "";
1488
+ const canTryBatchReconstruct = reason.includes("object expression") || reason.includes("array expression") || reason.includes("Multiple pausable calls");
1489
+ if (canTryBatchReconstruct) {
1490
+ const llmCall = findLLMCallExpression(callback.body);
1491
+ const itemParam = callback.params[0];
1492
+ const itemParamName = t7.isIdentifier(itemParam) ? itemParam.name : void 0;
1493
+ const hasDependencies = hasLLMCallDependencies(callback.body, this.batchDetector, itemParamName);
1494
+ if (llmCall && !hasDependencies) {
1495
+ const success = transformToBatchWithReconstruction(path, node, methodName, callback, this.batchDetector, () => this.transformCount++);
1496
+ if (success) {
1497
+ return true;
1498
+ }
1499
+ }
1500
+ }
1501
+ }
1268
1502
  if (batchResult.canBatch && canUseBatchParallel(methodName)) {
1269
1503
  const array = node.callee.object;
1270
1504
  const decision = this.batchOptimizer.makeSmartBatchDecision(methodName, batchResult, array, this.batchSizeThreshold);
@@ -1568,7 +1802,7 @@ function isTransformationError(error) {
1568
1802
  __name(isTransformationError, "isTransformationError");
1569
1803
 
1570
1804
  // src/transformer/index.ts
1571
- var traverse2 = _traverse.default || _traverse;
1805
+ var traverse3 = _traverse.default || _traverse;
1572
1806
  var generate = _generate.default || _generate;
1573
1807
  var ATPCompiler = class {
1574
1808
  static {
@@ -1620,7 +1854,7 @@ var ATPCompiler = class {
1620
1854
  this.loopTransformer.resetTransformCount();
1621
1855
  this.arrayTransformer.resetTransformCount();
1622
1856
  this.promiseTransformer.resetTransformCount();
1623
- traverse2(ast, {
1857
+ traverse3(ast, {
1624
1858
  ForOfStatement: /* @__PURE__ */ __name((path) => {
1625
1859
  this.loopTransformer.transformForOfLoop(path);
1626
1860
  }, "ForOfStatement"),
@@ -2303,7 +2537,7 @@ function createTransformPlugin(config) {
2303
2537
  };
2304
2538
  }
2305
2539
  __name(createTransformPlugin, "createTransformPlugin");
2306
- var traverse3 = _traverse.default || _traverse;
2540
+ var traverse4 = _traverse.default || _traverse;
2307
2541
  var generate2 = _generate.default || _generate;
2308
2542
  var PluggableCompiler = class {
2309
2543
  static {
@@ -2424,7 +2658,7 @@ var PluggableCompiler = class {
2424
2658
  }
2425
2659
  }
2426
2660
  }
2427
- traverse3(ast, visitors);
2661
+ traverse4(ast, visitors);
2428
2662
  let optimizedAst = ast;
2429
2663
  const optimizers = this.registry.getOptimizers();
2430
2664
  for (const optimizer of optimizers) {
@@ -2816,7 +3050,7 @@ var AsyncTimeoutPlugin = class {
2816
3050
  return null;
2817
3051
  }
2818
3052
  };
2819
- var traverse4 = _traverse.default || _traverse;
3053
+ var traverse5 = _traverse.default || _traverse;
2820
3054
  var SecurityValidatorPlugin = class {
2821
3055
  static {
2822
3056
  __name(this, "SecurityValidatorPlugin");
@@ -2853,7 +3087,7 @@ var SecurityValidatorPlugin = class {
2853
3087
  }
2854
3088
  let maxNestingFound = 0;
2855
3089
  let complexityScore = 0;
2856
- traverse4(ast, {
3090
+ traverse5(ast, {
2857
3091
  enter(path) {
2858
3092
  const depth = path.scope.path.node ? getDepth(path) : 0;
2859
3093
  maxNestingFound = Math.max(maxNestingFound, depth);
@@ -2900,6 +3134,6 @@ function getDepth(path) {
2900
3134
  }
2901
3135
  __name(getDepth, "getDepth");
2902
3136
 
2903
- export { ATPCompiler, ArrayTransformer, AsyncIterationDetector, AsyncTimeoutPlugin, BatchOptimizer, BatchParallelDetector, BatchPauseExecutionError, CheckpointError, CheckpointManager, CheckpointOperation, DEFAULT_COMPILER_CONFIG, DefaultArrayTransformerPlugin, DefaultDetectionPlugin, DefaultLoopTransformerPlugin, DefaultPromiseTransformerPlugin, IN_ISOLATE_RUNTIME_FUNCTIONS, InfiniteLoopDetectionError, LoopTransformer, PAUSABLE_CALL_PATTERNS, PluggableCompiler, PluginRegistry, PromiseTransformer, RuntimeFunction, SecurityValidationError, SecurityValidatorPlugin, TransformationError, batchParallel, cleanupRuntime, clearCheckpointManager, clearRuntimeContext, containsAwait, containsPausableCall, createDefaultCompiler, createRuntimeCall, createTransformPlugin, extractForOfParamName, generateUniqueId, getCheckpointManager, getMemberExpressionPath, getNodeLocation, getRuntimeContext, hasCheckpointManager, hasRuntimeContext, initializeRuntime, isArrayMethod, isAsyncFunction, isBatchPauseError, isCheckpointError, isCompiler, isInIsolateRuntimeFunction, isPausableCall, isPausableCallExpression, isTransformationError, resetIdCounter, resumableEvery, resumableFilter, resumableFind, resumableFlatMap, resumableForEach, resumableForLoop, resumableForOf, resumableMap, resumablePromiseAll, resumablePromiseAllSettled, resumableReduce, resumableSome, resumableWhile, setCheckpointManager, setRuntimeContext, wrapInAsyncFunction };
3137
+ export { ATPCompiler, ArrayTransformer, AsyncIterationDetector, AsyncTimeoutPlugin, BatchOptimizer, BatchParallelDetector, BatchPauseExecutionError, CheckpointError, CheckpointManager, CheckpointOperation, DEFAULT_COMPILER_CONFIG, DefaultArrayTransformerPlugin, DefaultDetectionPlugin, DefaultLoopTransformerPlugin, DefaultPromiseTransformerPlugin, IN_ISOLATE_RUNTIME_FUNCTIONS, InfiniteLoopDetectionError, LoopTransformer, PAUSABLE_CALL_PATTERNS, PluggableCompiler, PluginRegistry, PromiseTransformer, RuntimeFunction, SecurityValidationError, SecurityValidatorPlugin, TransformationError, batchParallel, cleanupRuntime, clearCheckpointManager, clearRuntimeContext, containsAwait, containsPausableCall, createDefaultCompiler, createRuntimeCall, createTransformPlugin, extractForOfParamName, generateUniqueId, getCheckpointManager, getMemberExpressionPath, getNodeLocation, getRuntimeContext, hasCheckpointManager, hasRuntimeContext, initializeRuntime, isArrayMethod, isAsyncFunction, isBatchPauseError, isCheckpointError, isCompiler, isInIsolateRuntimeFunction, isPausableCall, isPausableCallExpression, isTransformationError, resetIdCounter, resumableEvery, resumableFilter, resumableFind, resumableFlatMap, resumableForEach, resumableForLoop, resumableForOf, resumableMap, resumablePromiseAll, resumablePromiseAllSettled, resumableReduce, resumableSome, resumableWhile, setCheckpointManager, setRuntimeContext, transformToBatchWithReconstruction, wrapInAsyncFunction };
2904
3138
  //# sourceMappingURL=index.js.map
2905
3139
  //# sourceMappingURL=index.js.map