@mondaydotcomorg/atp-compiler 0.19.23 → 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) {
@@ -1250,54 +1360,72 @@ function transformToBatchWithReconstruction(path, node, methodName, callback, ba
1250
1360
  }
1251
1361
  const param = paramName.name;
1252
1362
  const array = node.callee.object;
1253
- const llmCall = findLLMCallExpression(callback.body);
1254
- if (!llmCall) {
1255
- return false;
1256
- }
1257
- const callInfo = batchDetector.extractCallInfo(llmCall);
1258
- if (!callInfo) {
1259
- return false;
1260
- }
1261
- const payloadNode = batchDetector.extractPayloadNode(llmCall);
1262
- if (!payloadNode) {
1363
+ const llmCalls = findAllLLMCallExpressions(callback.body, batchDetector);
1364
+ if (llmCalls.length === 0) {
1263
1365
  return false;
1264
1366
  }
1265
1367
  const methodId = generateUniqueId(`${methodName}_batch_reconstruct`);
1266
- const resultsVar = `__batch_results_${methodId.replace(/[^a-zA-Z0-9]/g, "_")}`;
1267
- const indexVar = "__idx";
1268
- const payloadMapper = t7.arrowFunctionExpression([
1269
- t7.identifier(param)
1270
- ], t7.objectExpression([
1271
- t7.objectProperty(t7.identifier("type"), t7.stringLiteral(callInfo.type)),
1272
- t7.objectProperty(t7.identifier("operation"), t7.stringLiteral(callInfo.operation)),
1273
- t7.objectProperty(t7.identifier("payload"), t7.cloneNode(payloadNode, true))
1274
- ]));
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
+ }
1275
1393
  const clonedBody = t7.cloneNode(callback.body, true);
1276
- const resultAccess = t7.memberExpression(t7.identifier(resultsVar), t7.identifier(indexVar), true);
1277
1394
  let traversableNode;
1278
1395
  if (t7.isBlockStatement(clonedBody)) {
1279
1396
  traversableNode = t7.functionDeclaration(t7.identifier("__temp"), [], clonedBody);
1280
1397
  } else {
1281
1398
  traversableNode = t7.expressionStatement(clonedBody);
1282
1399
  }
1283
- let replaced = false;
1400
+ let replacementCount = 0;
1284
1401
  traverse2(t7.file(t7.program([
1285
1402
  traversableNode
1286
1403
  ])), {
1287
1404
  AwaitExpression(awaitPath) {
1288
- if (replaced) return;
1289
1405
  const arg = awaitPath.node.argument;
1290
- if (t7.isCallExpression(arg)) {
1291
- const info = batchDetector.extractCallInfo(arg);
1292
- if (info && info.type === callInfo.type && info.operation === callInfo.operation) {
1293
- awaitPath.replaceWith(resultAccess);
1294
- replaced = true;
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;
1295
1416
  }
1296
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++;
1297
1425
  },
1298
1426
  noScope: true
1299
1427
  });
1300
- if (!replaced) {
1428
+ if (replacementCount === 0) {
1301
1429
  return false;
1302
1430
  }
1303
1431
  let reconstructBody;
@@ -1311,22 +1439,18 @@ function transformToBatchWithReconstruction(path, node, methodName, callback, ba
1311
1439
  t7.identifier(indexVar)
1312
1440
  ], reconstructBody);
1313
1441
  reconstructMapper.async = false;
1314
- const batchCall = t7.awaitExpression(t7.callExpression(t7.memberExpression(t7.identifier("__runtime"), t7.identifier("batchParallel")), [
1315
- t7.callExpression(t7.memberExpression(t7.cloneNode(array, true), t7.identifier("map")), [
1316
- payloadMapper
1317
- ]),
1318
- t7.stringLiteral(methodId)
1319
- ]));
1320
- const resultsDeclaration = t7.variableDeclaration("const", [
1321
- t7.variableDeclarator(t7.identifier(resultsVar), batchCall)
1322
- ]);
1323
1442
  const reconstructCall = t7.callExpression(t7.memberExpression(t7.cloneNode(array, true), t7.identifier("map")), [
1324
1443
  reconstructMapper
1325
1444
  ]);
1326
- const iife = t7.callExpression(t7.arrowFunctionExpression([], t7.blockStatement([
1327
- resultsDeclaration,
1328
- t7.returnStatement(reconstructCall)
1329
- ]), true), []);
1445
+ const iife = t7.callExpression(t7.arrowFunctionExpression(
1446
+ [],
1447
+ t7.blockStatement([
1448
+ ...batchDeclarations,
1449
+ t7.returnStatement(reconstructCall)
1450
+ ]),
1451
+ true
1452
+ // async
1453
+ ), []);
1330
1454
  const awaitIife = t7.awaitExpression(iife);
1331
1455
  path.replaceWith(awaitIife);
1332
1456
  onTransform();
@@ -1361,10 +1485,13 @@ var ArrayTransformer = class {
1361
1485
  const batchResult = this.batchOptimizer.canBatchArrayMethod(callback);
1362
1486
  if (!batchResult.canBatch && methodName === "map") {
1363
1487
  const reason = batchResult.reason || "";
1364
- const hasObjectOrArrayReturn = reason.includes("object expression") || reason.includes("array expression");
1365
- if (hasObjectOrArrayReturn) {
1488
+ const canTryBatchReconstruct = reason.includes("object expression") || reason.includes("array expression") || reason.includes("Multiple pausable calls");
1489
+ if (canTryBatchReconstruct) {
1366
1490
  const llmCall = findLLMCallExpression(callback.body);
1367
- if (llmCall) {
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) {
1368
1495
  const success = transformToBatchWithReconstruction(path, node, methodName, callback, this.batchDetector, () => this.transformCount++);
1369
1496
  if (success) {
1370
1497
  return true;