@mondaydotcomorg/atp-compiler
Version:
Production-ready compiler for transforming async iteration patterns into resumable operations with checkpoint-based state management
1,525 lines (1,511 loc) • 93.5 kB
JavaScript
'use strict';
var parser = require('@babel/parser');
var _traverse = require('@babel/traverse');
var _generate = require('@babel/generator');
var t7 = require('@babel/types');
var atpRuntime = require('@mondaydotcomorg/atp-runtime');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var _traverse__default = /*#__PURE__*/_interopDefault(_traverse);
var _generate__default = /*#__PURE__*/_interopDefault(_generate);
var t7__namespace = /*#__PURE__*/_interopNamespace(t7);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/types.ts
var PAUSABLE_CALL_PATTERNS = [
{
namespace: "atp.llm",
method: "call"
},
{
namespace: "atp.llm",
method: "extract"
},
{
namespace: "atp.llm",
method: "classify"
},
{
namespace: "atp.llm",
method: "stream"
},
{
namespace: "atp.llm",
method: "generate"
},
{
namespace: "atp.approval",
method: "request"
},
{
namespace: "atp.approval",
method: "confirm"
},
{
namespace: "atp.approval",
method: "verify"
},
{
namespace: "atp.embedding",
method: "embed"
},
{
namespace: "atp.embedding",
method: "search"
},
{
namespace: "atp.embedding",
method: "create"
},
{
namespace: "atp.embedding",
method: "generate"
},
{
namespace: "atp.embedding",
method: "encode"
}
];
var DEFAULT_COMPILER_CONFIG = {
enableBatchParallel: true,
maxLoopNesting: 10,
checkpointInterval: 1,
debugMode: false,
batchSizeThreshold: 10
};
function isPausableCall(node) {
if (!t7__namespace.isAwaitExpression(node)) {
return false;
}
const argument = node.argument;
if (!t7__namespace.isCallExpression(argument)) {
return false;
}
return isPausableCallExpression(argument);
}
__name(isPausableCall, "isPausableCall");
function isPausableCallExpression(node) {
const callee = node.callee;
if (!t7__namespace.isMemberExpression(callee)) {
return false;
}
const fullPath = getMemberExpressionPath(callee);
return PAUSABLE_CALL_PATTERNS.some((pattern) => fullPath === `${pattern.namespace}.${pattern.method}`);
}
__name(isPausableCallExpression, "isPausableCallExpression");
function getMemberExpressionPath(node) {
const parts = [];
let current = node;
while (t7__namespace.isMemberExpression(current)) {
if (t7__namespace.isIdentifier(current.property)) {
parts.unshift(current.property.name);
}
current = current.object;
}
if (t7__namespace.isIdentifier(current)) {
parts.unshift(current.name);
}
return parts.join(".");
}
__name(getMemberExpressionPath, "getMemberExpressionPath");
function containsAwait(node) {
let hasAwait = false;
const checkNode = /* @__PURE__ */ __name((n) => {
if (t7__namespace.isAwaitExpression(n)) {
hasAwait = true;
return;
}
if (hasAwait) return;
Object.keys(n).forEach((key) => {
const value = n[key];
if (Array.isArray(value)) {
value.forEach((item) => {
if (item && typeof item === "object" && item.type) {
checkNode(item);
}
});
} else if (value && typeof value === "object" && value.type) {
checkNode(value);
}
});
}, "checkNode");
checkNode(node);
return hasAwait;
}
__name(containsAwait, "containsAwait");
function containsPausableCall(node) {
let hasPausable = false;
const checkNode = /* @__PURE__ */ __name((n) => {
if (t7__namespace.isAwaitExpression(n) && isPausableCall(n)) {
hasPausable = true;
return;
}
if (hasPausable) return;
Object.keys(n).forEach((key) => {
const value = n[key];
if (Array.isArray(value)) {
value.forEach((item) => {
if (item && typeof item === "object" && item.type) {
checkNode(item);
}
});
} else if (value && typeof value === "object" && value.type) {
checkNode(value);
}
});
}, "checkNode");
checkNode(node);
return hasPausable;
}
__name(containsPausableCall, "containsPausableCall");
function isAsyncFunction(node) {
return (t7__namespace.isFunctionDeclaration(node) || t7__namespace.isFunctionExpression(node) || t7__namespace.isArrowFunctionExpression(node)) && node.async === true;
}
__name(isAsyncFunction, "isAsyncFunction");
function getNodeLocation(node) {
if (node.loc) {
return {
line: node.loc.start.line,
column: node.loc.start.column
};
}
return void 0;
}
__name(getNodeLocation, "getNodeLocation");
function createRuntimeCall(fnName, args) {
return t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier(fnName)), args));
}
__name(createRuntimeCall, "createRuntimeCall");
function wrapInAsyncFunction(body) {
return t7__namespace.functionExpression(null, [], t7__namespace.blockStatement(body), false, true);
}
__name(wrapInAsyncFunction, "wrapInAsyncFunction");
function isArrayMethod(node, methodName) {
if (!t7__namespace.isCallExpression(node)) {
return false;
}
const callee = node.callee;
if (!t7__namespace.isMemberExpression(callee)) {
return false;
}
return t7__namespace.isIdentifier(callee.property) && callee.property.name === methodName;
}
__name(isArrayMethod, "isArrayMethod");
function extractForOfParamName(left) {
if (t7__namespace.isVariableDeclaration(left)) {
const id = left.declarations[0]?.id;
return t7__namespace.isIdentifier(id) ? id.name : "item";
} else if (t7__namespace.isIdentifier(left)) {
return left.name;
} else {
return "item";
}
}
__name(extractForOfParamName, "extractForOfParamName");
// src/transformer/detector.ts
var traverse = typeof _traverse__default.default.default === "function" ? _traverse__default.default.default : _traverse__default.default;
var AsyncIterationDetector = class {
static {
__name(this, "AsyncIterationDetector");
}
detect(code) {
const patterns = [];
let batchableParallel = false;
try {
const ast = parser.parse(code, {
sourceType: "module",
plugins: [
"typescript"
],
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true
});
traverse(ast, {
ForOfStatement: /* @__PURE__ */ __name((path) => {
if (containsAwait(path.node.body)) {
patterns.push("for-of-await");
}
}, "ForOfStatement"),
WhileStatement: /* @__PURE__ */ __name((path) => {
if (containsAwait(path.node.body)) {
patterns.push("while-await");
}
}, "WhileStatement"),
CallExpression: /* @__PURE__ */ __name((path) => {
const node = path.node;
if (isArrayMethod(node, "map")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("map-async");
}
}
if (isArrayMethod(node, "forEach")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("forEach-async");
}
}
if (isArrayMethod(node, "filter")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("filter-async");
}
}
if (isArrayMethod(node, "reduce")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("reduce-async");
}
}
if (isArrayMethod(node, "find")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("find-async");
}
}
if (isArrayMethod(node, "some")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("some-async");
}
}
if (isArrayMethod(node, "every")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("every-async");
}
}
if (isArrayMethod(node, "flatMap")) {
const callback = node.arguments[0];
if (callback && t7__namespace.isFunction(callback) && callback.async) {
patterns.push("flatMap-async");
}
}
if (this.isPromiseAll(node)) {
patterns.push("promise-all");
if (this.canBatchPromiseAll(node)) {
batchableParallel = true;
}
}
if (this.isPromiseAllSettled(node)) {
patterns.push("promise-allSettled");
}
}, "CallExpression")
});
return {
needsTransform: patterns.length > 0,
patterns: [
...new Set(patterns)
],
batchableParallel
};
} catch (error) {
return {
needsTransform: false,
patterns: [],
batchableParallel: false
};
}
}
isPromiseAll(node) {
const callee = node.callee;
return t7__namespace.isMemberExpression(callee) && t7__namespace.isIdentifier(callee.object, {
name: "Promise"
}) && t7__namespace.isIdentifier(callee.property, {
name: "all"
});
}
isPromiseAllSettled(node) {
const callee = node.callee;
return t7__namespace.isMemberExpression(callee) && t7__namespace.isIdentifier(callee.object, {
name: "Promise"
}) && t7__namespace.isIdentifier(callee.property, {
name: "allSettled"
});
}
canBatchPromiseAll(node) {
const arrayArg = node.arguments[0];
if (!t7__namespace.isArrayExpression(arrayArg)) {
return false;
}
if (arrayArg.elements.length === 0) {
return false;
}
return arrayArg.elements.every((el) => {
if (!el || t7__namespace.isSpreadElement(el)) {
return false;
}
return this.isDirectPausableCall(el);
});
}
isDirectPausableCall(node) {
if (t7__namespace.isAwaitExpression(node)) {
node = node.argument;
}
if (!t7__namespace.isCallExpression(node)) {
return false;
}
return isPausableCallExpression(node);
}
};
// src/runtime/context.ts
var contextStack = [];
function setRuntimeContext(context) {
contextStack.push(context);
}
__name(setRuntimeContext, "setRuntimeContext");
function getRuntimeContext() {
const context = contextStack[contextStack.length - 1];
if (!context) {
throw new Error("No runtime context available. Compiler runtime not properly initialized.");
}
return context;
}
__name(getRuntimeContext, "getRuntimeContext");
function clearRuntimeContext() {
contextStack.pop();
}
__name(clearRuntimeContext, "clearRuntimeContext");
function hasRuntimeContext() {
return contextStack.length > 0;
}
__name(hasRuntimeContext, "hasRuntimeContext");
var idCounter = 0;
function generateUniqueId(prefix) {
return `${prefix}_${Date.now()}_${idCounter++}`;
}
__name(generateUniqueId, "generateUniqueId");
function resetIdCounter() {
idCounter = 0;
}
__name(resetIdCounter, "resetIdCounter");
var BatchOptimizer = class {
static {
__name(this, "BatchOptimizer");
}
arrayMethodsWithEarlyExit = [
"find",
"some",
"every"
];
canBatchArrayMethod(callback) {
if (!callback.async) {
return {
canBatch: false,
reason: "Not async"
};
}
const body = callback.body;
if (!t7__namespace.isBlockStatement(body)) {
if (t7__namespace.isAwaitExpression(body)) {
if (this.isDirectPausableCall(body.argument)) {
return {
canBatch: true,
llmCallPattern: "single",
hasConditionals: false
};
}
}
return {
canBatch: false,
reason: "Non-block body without direct call"
};
}
const statements = body.body;
if (statements.length === 0) {
return {
canBatch: false,
reason: "Empty body"
};
}
let hasConditionals = false;
let hasLoops = false;
let hasTryCatch = false;
for (const stmt of statements) {
if (t7__namespace.isIfStatement(stmt) || t7__namespace.isSwitchStatement(stmt)) {
hasConditionals = true;
}
if (t7__namespace.isTryStatement(stmt)) {
hasTryCatch = true;
}
if (t7__namespace.isForStatement(stmt) || t7__namespace.isForOfStatement(stmt) || t7__namespace.isForInStatement(stmt) || t7__namespace.isWhileStatement(stmt) || t7__namespace.isDoWhileStatement(stmt)) {
hasLoops = true;
}
if (t7__namespace.isBreakStatement(stmt) || t7__namespace.isContinueStatement(stmt)) {
return {
canBatch: false,
reason: "Contains break/continue"
};
}
if (t7__namespace.isReturnStatement(stmt) && stmt !== statements[statements.length - 1]) {
return {
canBatch: false,
reason: "Early return"
};
}
}
if (hasLoops) {
return {
canBatch: false,
reason: "Contains loops",
hasLoops: true
};
}
if (hasTryCatch) {
return {
canBatch: false,
reason: "Contains try-catch"
};
}
const pausableCalls = this.countPausableCalls(body);
if (pausableCalls === 0) {
return {
canBatch: false,
reason: "No pausable calls"
};
}
if (pausableCalls > 1) {
return {
canBatch: false,
reason: "Multiple pausable calls",
llmCallPattern: "multiple"
};
}
if (hasConditionals) {
return {
canBatch: true,
llmCallPattern: "conditional",
hasConditionals: true,
reason: "Simple conditional - can batch but consider array size"
};
}
return {
canBatch: true,
llmCallPattern: "single",
hasConditionals: false
};
}
/**
* Smart decision: Should we batch based on array size and method type?
*/
makeSmartBatchDecision(methodName, batchResult, arrayNode, threshold = 10) {
if (!batchResult.canBatch) {
return {
shouldBatch: false,
reason: "Complex callback - use sequential",
strategy: "never-batch"
};
}
if (!batchResult.hasConditionals) {
return {
shouldBatch: true,
reason: "Simple callback - batching is faster",
strategy: "always-batch"
};
}
const hasEarlyExitBenefit = this.arrayMethodsWithEarlyExit.includes(methodName);
if (!hasEarlyExitBenefit) {
const arraySize2 = this.estimateArraySize(arrayNode);
if (arraySize2 !== null && arraySize2 < threshold) {
return {
shouldBatch: true,
reason: `Small array (${arraySize2} < ${threshold}) - batch despite conditionals`,
strategy: "size-dependent"
};
}
return {
shouldBatch: false,
reason: "Conditionals + large/unknown array - sequential for safety",
strategy: "size-dependent"
};
}
const arraySize = this.estimateArraySize(arrayNode);
if (arraySize !== null && arraySize < threshold) {
return {
shouldBatch: true,
reason: `Small array (${arraySize} < ${threshold}) - batch for speed`,
strategy: "size-dependent"
};
}
if (arraySize !== null && arraySize >= threshold) {
return {
shouldBatch: false,
reason: `Large array (${arraySize} >= ${threshold}) + conditionals - sequential for early-exit savings`,
strategy: "size-dependent"
};
}
if (t7__namespace.isArrayExpression(arrayNode)) {
return {
shouldBatch: true,
reason: "Array literal (likely small) - batch",
strategy: "size-dependent"
};
}
return {
shouldBatch: false,
reason: "Unknown array size + conditionals - sequential for safety",
strategy: "size-dependent"
};
}
estimateArraySize(arrayNode) {
if (t7__namespace.isArrayExpression(arrayNode)) {
return arrayNode.elements.length;
}
return null;
}
canBatchForOfLoop(loopNode) {
const body = loopNode.body;
if (!t7__namespace.isBlockStatement(body)) {
return {
canBatch: false,
reason: "Loop body not a block"
};
}
const statements = body.body;
if (statements.length === 0) {
return {
canBatch: false,
reason: "Empty loop body"
};
}
const hasBreakOrContinue = this.containsBreakOrContinue(body);
if (hasBreakOrContinue) {
return {
canBatch: false,
reason: "Contains break/continue"
};
}
let hasConditionals = false;
for (const stmt of statements) {
if (t7__namespace.isIfStatement(stmt) || t7__namespace.isSwitchStatement(stmt)) {
hasConditionals = true;
}
if (t7__namespace.isForStatement(stmt) || t7__namespace.isForOfStatement(stmt) || t7__namespace.isForInStatement(stmt) || t7__namespace.isWhileStatement(stmt) || t7__namespace.isDoWhileStatement(stmt)) {
return {
canBatch: false,
reason: "Contains nested loops",
hasLoops: true
};
}
}
const pausableCalls = this.countPausableCalls(body);
if (pausableCalls === 0) {
return {
canBatch: false,
reason: "No pausable calls"
};
}
if (pausableCalls > 1) {
return {
canBatch: false,
reason: "Multiple pausable calls",
llmCallPattern: "multiple"
};
}
if (hasConditionals) {
return {
canBatch: true,
llmCallPattern: "conditional",
hasConditionals: true,
reason: "Simple conditional - can batch but consider array size"
};
}
return {
canBatch: true,
llmCallPattern: "single",
hasConditionals: false
};
}
containsBreakOrContinue(node) {
let found = false;
const visit = /* @__PURE__ */ __name((n) => {
if (found) return;
if (t7__namespace.isBreakStatement(n) || t7__namespace.isContinueStatement(n)) {
found = true;
return;
}
if (t7__namespace.isForStatement(n) || t7__namespace.isForOfStatement(n) || t7__namespace.isForInStatement(n) || t7__namespace.isWhileStatement(n) || t7__namespace.isDoWhileStatement(n)) {
return;
}
Object.keys(n).forEach((key) => {
const value = n[key];
if (Array.isArray(value)) {
value.forEach((item) => {
if (item && typeof item === "object" && item.type) {
visit(item);
}
});
} else if (value && typeof value === "object" && value.type) {
visit(value);
}
});
}, "visit");
visit(node);
return found;
}
isDirectPausableCall(node) {
if (!t7__namespace.isCallExpression(node)) {
return false;
}
return isPausableCallExpression(node);
}
countPausableCalls(body) {
let count = 0;
const visit = /* @__PURE__ */ __name((node) => {
if (t7__namespace.isAwaitExpression(node) && this.isDirectPausableCall(node.argument)) {
count++;
return;
}
Object.keys(node).forEach((key) => {
const value = node[key];
if (Array.isArray(value)) {
value.forEach((item) => {
if (item && typeof item === "object" && item.type) {
visit(item);
}
});
} else if (value && typeof value === "object" && value.type) {
visit(value);
}
});
}, "visit");
visit(body);
return count;
}
};
var BatchParallelDetector = class {
static {
__name(this, "BatchParallelDetector");
}
canBatch(promiseAllNode) {
const arrayArg = promiseAllNode.arguments[0];
if (!t7__namespace.isArrayExpression(arrayArg)) {
return false;
}
if (arrayArg.elements.length === 0) {
return false;
}
return arrayArg.elements.every((el) => {
if (!el || t7__namespace.isSpreadElement(el)) {
return false;
}
return this.isDirectPausableCall(el);
});
}
isDirectPausableCall(node) {
if (t7__namespace.isAwaitExpression(node)) {
node = node.argument;
}
if (!t7__namespace.isCallExpression(node)) {
return false;
}
return isPausableCallExpression(node);
}
extractBatchCalls(arrayNode) {
const calls = [];
for (const el of arrayNode.elements) {
if (!el || t7__namespace.isSpreadElement(el)) {
continue;
}
let callNode = el;
if (t7__namespace.isAwaitExpression(callNode)) {
callNode = callNode.argument;
}
if (!t7__namespace.isCallExpression(callNode)) {
continue;
}
const callInfo = this.extractCallInfo(callNode);
if (callInfo) {
calls.push(callInfo);
}
}
return calls;
}
extractCallInfo(callNode) {
if (!t7__namespace.isMemberExpression(callNode.callee)) {
return null;
}
const path = getMemberExpressionPath(callNode.callee);
const parts = path.split(".");
if (parts.length < 3) {
return null;
}
const [namespace, service, method] = parts;
if (namespace !== "atp" || !method) {
return null;
}
const type = service;
const payload = this.extractPayload(callNode.arguments);
return {
type,
operation: method,
payload
};
}
/**
* Extract payload AST node directly
*/
extractPayloadNode(callNode) {
if (callNode.arguments.length === 0) {
return t7__namespace.objectExpression([]);
}
const firstArg = callNode.arguments[0];
if (!firstArg || t7__namespace.isSpreadElement(firstArg) || !t7__namespace.isExpression(firstArg)) {
return null;
}
return firstArg;
}
extractPayload(args) {
if (args.length === 0) {
return {};
}
const firstArg = args[0];
if (t7__namespace.isSpreadElement(firstArg)) {
return {};
}
if (t7__namespace.isObjectExpression(firstArg)) {
return this.objectExpressionToRecord(firstArg);
}
if (t7__namespace.isStringLiteral(firstArg)) {
return {
message: firstArg.value
};
}
return {};
}
objectExpressionToRecord(obj) {
const record = {};
for (const prop of obj.properties) {
if (t7__namespace.isObjectProperty(prop) && !prop.computed) {
const key = t7__namespace.isIdentifier(prop.key) ? prop.key.name : String(prop.key);
const value = this.extractValue(prop.value);
record[key] = value;
}
}
return record;
}
extractValue(node) {
if (t7__namespace.isStringLiteral(node)) {
return node.value;
}
if (t7__namespace.isNumericLiteral(node)) {
return node.value;
}
if (t7__namespace.isBooleanLiteral(node)) {
return node.value;
}
if (t7__namespace.isNullLiteral(node)) {
return null;
}
if (t7__namespace.isArrayExpression(node)) {
return node.elements.map((el) => el && !t7__namespace.isSpreadElement(el) ? this.extractValue(el) : null);
}
if (t7__namespace.isObjectExpression(node)) {
return this.objectExpressionToRecord(node);
}
return void 0;
}
};
function findLLMCallExpression(body) {
let found = null;
const visit = /* @__PURE__ */ __name((node) => {
if (found) return;
if (t7__namespace.isAwaitExpression(node) && t7__namespace.isCallExpression(node.argument)) {
const call = node.argument;
if (t7__namespace.isMemberExpression(call.callee)) {
found = call;
return;
}
}
Object.keys(node).forEach((key) => {
const value = node[key];
if (Array.isArray(value)) {
value.forEach((item) => {
if (item && typeof item === "object" && item.type) {
visit(item);
}
});
} else if (value && typeof value === "object" && value.type) {
visit(value);
}
});
}, "visit");
visit(body);
return found;
}
__name(findLLMCallExpression, "findLLMCallExpression");
function getArrayMethodName(node) {
const arrayMethods = [
"map",
"forEach",
"filter",
"reduce",
"find",
"some",
"every",
"flatMap"
];
for (const method of arrayMethods) {
if (isArrayMethod(node, method)) {
return method;
}
}
return null;
}
__name(getArrayMethodName, "getArrayMethodName");
function getRuntimeMethodName(arrayMethod) {
const mapping = {
map: "resumableMap",
forEach: "resumableForEach",
filter: "resumableFilter",
reduce: "resumableReduce",
find: "resumableFind",
some: "resumableSome",
every: "resumableEvery",
flatMap: "resumableFlatMap"
};
return mapping[arrayMethod] || null;
}
__name(getRuntimeMethodName, "getRuntimeMethodName");
function canUseBatchParallel(methodName) {
return [
"map",
"forEach",
"filter",
"find",
"some",
"every"
].includes(methodName);
}
__name(canUseBatchParallel, "canUseBatchParallel");
// src/transformer/loop-transformer.ts
var LoopTransformer = class {
static {
__name(this, "LoopTransformer");
}
transformCount = 0;
batchOptimizer;
batchDetector;
batchSizeThreshold;
constructor(batchSizeThreshold = 10) {
this.batchOptimizer = new BatchOptimizer();
this.batchDetector = new BatchParallelDetector();
this.batchSizeThreshold = batchSizeThreshold;
}
transformForOfLoop(path) {
const node = path.node;
if (!containsAwait(node.body)) {
return false;
}
const batchResult = this.batchOptimizer.canBatchForOfLoop(node);
if (batchResult.canBatch) {
const decision = this.batchOptimizer.makeSmartBatchDecision("for...of", batchResult, node.right, this.batchSizeThreshold);
if (decision.shouldBatch) {
return this.transformForOfToBatch(path, node);
}
}
return this.transformForOfToSequential(path, node);
}
/**
* Transform simple for...of to batch parallel
*/
transformForOfToBatch(path, node) {
const loopId = generateUniqueId("for_of_batch");
const right = node.right;
const paramName = extractForOfParamName(node.left);
const llmCall = findLLMCallExpression(node.body);
if (!llmCall) {
return this.transformForOfToSequential(path, node);
}
const callInfo = this.batchDetector.extractCallInfo(llmCall);
if (!callInfo) {
return this.transformForOfToSequential(path, node);
}
const payloadNode = this.batchDetector.extractPayloadNode(llmCall);
if (!payloadNode) {
return this.transformForOfToSequential(path, node);
}
const batchCallsArray = t7__namespace.callExpression(t7__namespace.memberExpression(right, t7__namespace.identifier("map")), [
t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(paramName)
], t7__namespace.objectExpression([
t7__namespace.objectProperty(t7__namespace.identifier("type"), t7__namespace.stringLiteral(callInfo.type)),
t7__namespace.objectProperty(t7__namespace.identifier("operation"), t7__namespace.stringLiteral(callInfo.operation)),
t7__namespace.objectProperty(t7__namespace.identifier("payload"), payloadNode)
]))
]);
const batchCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier("batchParallel")), [
batchCallsArray,
t7__namespace.stringLiteral(loopId)
]));
path.replaceWith(t7__namespace.expressionStatement(batchCall));
this.transformCount++;
return true;
}
/**
* Transform for...of to sequential with checkpoints (fallback)
*/
transformForOfToSequential(path, node) {
const loopId = generateUniqueId("for_of");
const right = node.right;
const paramName = extractForOfParamName(node.left);
const bodyStatements = t7__namespace.isBlockStatement(node.body) ? node.body.body : [
node.body
];
const callbackFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(paramName),
t7__namespace.identifier("__index")
], t7__namespace.blockStatement(bodyStatements), true);
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier("resumableForOf")), [
right,
callbackFn,
t7__namespace.stringLiteral(loopId)
]));
path.replaceWith(t7__namespace.expressionStatement(runtimeCall));
this.transformCount++;
return true;
}
transformWhileLoop(path) {
const node = path.node;
if (!containsAwait(node.body)) {
return false;
}
const loopId = generateUniqueId("while");
const conditionFn = t7__namespace.arrowFunctionExpression([], node.test, false);
const bodyStatements = t7__namespace.isBlockStatement(node.body) ? node.body.body : [
node.body
];
const bodyFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier("__iteration")
], t7__namespace.blockStatement(bodyStatements), true);
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier("resumableWhile")), [
conditionFn,
bodyFn,
t7__namespace.stringLiteral(loopId)
]));
path.replaceWith(t7__namespace.expressionStatement(runtimeCall));
this.transformCount++;
return true;
}
transformForLoop(path) {
const node = path.node;
if (!containsAwait(node.body)) {
return false;
}
if (!node.init || !node.test || !node.update) {
return false;
}
const loopId = generateUniqueId("for");
let initValue = t7__namespace.numericLiteral(0);
let loopVar = "__i";
if (t7__namespace.isVariableDeclaration(node.init)) {
const decl = node.init.declarations[0];
if (decl && t7__namespace.isIdentifier(decl.id) && decl.init) {
loopVar = decl.id.name;
initValue = decl.init;
}
}
const conditionFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(loopVar)
], node.test, false);
const bodyStatements = t7__namespace.isBlockStatement(node.body) ? node.body.body : [
node.body
];
const bodyFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(loopVar)
], t7__namespace.blockStatement(bodyStatements), true);
let incrementFn;
if (t7__namespace.isUpdateExpression(node.update)) {
if (node.update.operator === "++") {
incrementFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(loopVar)
], t7__namespace.binaryExpression("+", t7__namespace.identifier(loopVar), t7__namespace.numericLiteral(1)), false);
} else if (node.update.operator === "--") {
incrementFn = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(loopVar)
], t7__namespace.binaryExpression("-", t7__namespace.identifier(loopVar), t7__namespace.numericLiteral(1)), false);
} else {
return false;
}
} else {
return false;
}
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier("resumableForLoop")), [
initValue,
conditionFn,
incrementFn,
bodyFn,
t7__namespace.stringLiteral(loopId)
]));
path.replaceWith(t7__namespace.expressionStatement(runtimeCall));
this.transformCount++;
return true;
}
getTransformCount() {
return this.transformCount;
}
resetTransformCount() {
this.transformCount = 0;
}
};
function wrapFilterResult(batchCall, array, methodId) {
const resultsVar = `__filter_results_${methodId}`;
const indexVar = `__i_${methodId}`;
const arrayClone = t7__namespace.cloneNode(array, true);
return t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.arrowFunctionExpression([], t7__namespace.blockStatement([
t7__namespace.variableDeclaration("const", [
t7__namespace.variableDeclarator(t7__namespace.identifier(resultsVar), batchCall.argument)
]),
t7__namespace.returnStatement(t7__namespace.callExpression(t7__namespace.memberExpression(arrayClone, t7__namespace.identifier("filter")), [
t7__namespace.arrowFunctionExpression([
t7__namespace.identifier("_"),
t7__namespace.identifier(indexVar)
], t7__namespace.callExpression(t7__namespace.identifier("Boolean"), [
t7__namespace.memberExpression(t7__namespace.identifier(resultsVar), t7__namespace.identifier(indexVar), true)
]))
]))
]), true), []));
}
__name(wrapFilterResult, "wrapFilterResult");
function wrapFindResult(batchCall, array, methodId) {
const resultsVar = `__find_results_${methodId}`;
const arrayClone = t7__namespace.cloneNode(array, true);
return t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.arrowFunctionExpression([], t7__namespace.blockStatement([
t7__namespace.variableDeclaration("const", [
t7__namespace.variableDeclarator(t7__namespace.identifier(resultsVar), batchCall.argument)
]),
t7__namespace.returnStatement(t7__namespace.callExpression(t7__namespace.memberExpression(arrayClone, t7__namespace.identifier("find")), [
t7__namespace.arrowFunctionExpression([
t7__namespace.identifier("_"),
t7__namespace.identifier("__i")
], t7__namespace.callExpression(t7__namespace.identifier("Boolean"), [
t7__namespace.memberExpression(t7__namespace.identifier(resultsVar), t7__namespace.identifier("__i"), true)
]))
]))
]), true), []));
}
__name(wrapFindResult, "wrapFindResult");
function wrapSomeResult(batchCall, methodId) {
const resultsVar = `__some_results_${methodId}`;
return t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.arrowFunctionExpression([], t7__namespace.blockStatement([
t7__namespace.variableDeclaration("const", [
t7__namespace.variableDeclarator(t7__namespace.identifier(resultsVar), batchCall.argument)
]),
t7__namespace.returnStatement(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier(resultsVar), t7__namespace.identifier("some")), [
t7__namespace.arrowFunctionExpression([
t7__namespace.identifier("r")
], t7__namespace.callExpression(t7__namespace.identifier("Boolean"), [
t7__namespace.identifier("r")
]))
]))
]), true), []));
}
__name(wrapSomeResult, "wrapSomeResult");
function wrapEveryResult(batchCall, methodId) {
const resultsVar = `__every_results_${methodId}`;
return t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.arrowFunctionExpression([], t7__namespace.blockStatement([
t7__namespace.variableDeclaration("const", [
t7__namespace.variableDeclarator(t7__namespace.identifier(resultsVar), batchCall.argument)
]),
t7__namespace.returnStatement(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier(resultsVar), t7__namespace.identifier("every")), [
t7__namespace.arrowFunctionExpression([
t7__namespace.identifier("r")
], t7__namespace.callExpression(t7__namespace.identifier("Boolean"), [
t7__namespace.identifier("r")
]))
]))
]), true), []));
}
__name(wrapEveryResult, "wrapEveryResult");
function wrapBatchResultIfNeeded(batchCall, methodName, array, methodId) {
switch (methodName) {
case "filter":
return wrapFilterResult(batchCall, array, methodId);
case "find":
return wrapFindResult(batchCall, array, methodId);
case "some":
return wrapSomeResult(batchCall, methodId);
case "every":
return wrapEveryResult(batchCall, methodId);
case "forEach":
return batchCall;
default:
return batchCall;
}
}
__name(wrapBatchResultIfNeeded, "wrapBatchResultIfNeeded");
// src/transformer/array-transformer-batch.ts
function extractBatchCallInfo(callback, batchDetector) {
const paramName = callback.params[0];
if (!t7__namespace.isIdentifier(paramName)) {
return null;
}
const param = paramName.name;
const llmCall = findLLMCallExpression(callback.body);
if (!llmCall) {
return null;
}
const callInfo = batchDetector.extractCallInfo(llmCall);
if (!callInfo) {
return null;
}
const payloadNode = batchDetector.extractPayloadNode(llmCall);
if (!payloadNode) {
return null;
}
const mapperFunction = t7__namespace.arrowFunctionExpression([
t7__namespace.identifier(param)
], t7__namespace.objectExpression([
t7__namespace.objectProperty(t7__namespace.identifier("type"), t7__namespace.stringLiteral(callInfo.type)),
t7__namespace.objectProperty(t7__namespace.identifier("operation"), t7__namespace.stringLiteral(callInfo.operation)),
t7__namespace.objectProperty(t7__namespace.identifier("payload"), payloadNode)
]));
return {
mapperFunction
};
}
__name(extractBatchCallInfo, "extractBatchCallInfo");
function transformToBatchParallel(path, node, methodName, callback, batchDetector, onTransform, fallbackTransform) {
const methodId = generateUniqueId(`${methodName}_batch`);
const array = node.callee.object;
const callInfo = extractBatchCallInfo(callback, batchDetector);
if (!callInfo) {
return fallbackTransform();
}
const batchCallsArray = t7__namespace.callExpression(t7__namespace.memberExpression(array, t7__namespace.identifier("map")), [
callInfo.mapperFunction
]);
const batchCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier("batchParallel")), [
batchCallsArray,
t7__namespace.stringLiteral(methodId)
]));
const finalCall = wrapBatchResultIfNeeded(batchCall, methodName, array, methodId);
path.replaceWith(finalCall);
onTransform();
return true;
}
__name(transformToBatchParallel, "transformToBatchParallel");
function transformToSequential(path, node, methodName, callback, onTransform) {
const runtimeMethod = getRuntimeMethodName(methodName);
if (!runtimeMethod) {
return false;
}
const methodId = generateUniqueId(methodName);
const array = node.callee.object;
const args = [
array,
callback,
t7__namespace.stringLiteral(methodId)
];
if (methodName === "reduce" && node.arguments[1]) {
args.push(node.arguments[1]);
}
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier(runtimeMethod)), args));
path.replaceWith(runtimeCall);
onTransform();
return true;
}
__name(transformToSequential, "transformToSequential");
// src/transformer/array-transformer.ts
var ArrayTransformer = class {
static {
__name(this, "ArrayTransformer");
}
transformCount = 0;
batchOptimizer;
batchDetector;
batchSizeThreshold;
constructor(batchSizeThreshold = 10) {
this.batchOptimizer = new BatchOptimizer();
this.batchDetector = new BatchParallelDetector();
this.batchSizeThreshold = batchSizeThreshold;
}
transformArrayMethod(path) {
const node = path.node;
const methodName = getArrayMethodName(node);
if (!methodName) {
return false;
}
const callback = node.arguments[0];
if (!callback || !t7__namespace.isFunction(callback) || !callback.async) {
return false;
}
const batchResult = this.batchOptimizer.canBatchArrayMethod(callback);
if (batchResult.canBatch && canUseBatchParallel(methodName)) {
const array = node.callee.object;
const decision = this.batchOptimizer.makeSmartBatchDecision(methodName, batchResult, array, this.batchSizeThreshold);
if (decision.shouldBatch) {
return transformToBatchParallel(path, node, methodName, callback, this.batchDetector, () => this.transformCount++, () => this.doTransformToSequential(path, node, methodName, callback));
}
}
return this.doTransformToSequential(path, node, methodName, callback);
}
doTransformToSequential(path, node, methodName, callback) {
return transformToSequential(path, node, methodName, callback, () => this.transformCount++);
}
getTransformCount() {
return this.transformCount;
}
resetTransformCount() {
this.transformCount = 0;
}
};
// src/runtime/runtime-functions.ts
var RuntimeFunction = {
// Promise operations
RESUMABLE_PROMISE_ALL: "resumablePromiseAll",
RESUMABLE_PROMISE_ALL_SETTLED: "resumablePromiseAllSettled",
BATCH_PARALLEL: "batchParallel",
// Loop operations
RESUMABLE_FOR_OF: "resumableForOf",
RESUMABLE_FOR_LOOP: "resumableForLoop",
RESUMABLE_WHILE: "resumableWhile",
// Array method operations
RESUMABLE_MAP: "resumableMap",
RESUMABLE_FOR_EACH: "resumableForEach",
RESUMABLE_FILTER: "resumableFilter",
RESUMABLE_REDUCE: "resumableReduce",
RESUMABLE_FIND: "resumableFind",
RESUMABLE_SOME: "resumableSome",
RESUMABLE_EVERY: "resumableEvery",
RESUMABLE_FLAT_MAP: "resumableFlatMap"
};
var IN_ISOLATE_RUNTIME_FUNCTIONS = [
RuntimeFunction.RESUMABLE_PROMISE_ALL,
RuntimeFunction.RESUMABLE_PROMISE_ALL_SETTLED,
RuntimeFunction.RESUMABLE_FOR_OF,
RuntimeFunction.RESUMABLE_FOR_LOOP,
RuntimeFunction.RESUMABLE_WHILE,
RuntimeFunction.RESUMABLE_MAP,
RuntimeFunction.RESUMABLE_FOR_EACH,
RuntimeFunction.RESUMABLE_FILTER,
RuntimeFunction.RESUMABLE_REDUCE,
RuntimeFunction.RESUMABLE_FIND,
RuntimeFunction.RESUMABLE_SOME,
RuntimeFunction.RESUMABLE_EVERY,
RuntimeFunction.RESUMABLE_FLAT_MAP
];
function isInIsolateRuntimeFunction(name) {
return IN_ISOLATE_RUNTIME_FUNCTIONS.includes(name);
}
__name(isInIsolateRuntimeFunction, "isInIsolateRuntimeFunction");
// src/transformer/promise-transformer.ts
var PromiseTransformer = class {
static {
__name(this, "PromiseTransformer");
}
transformCount = 0;
batchDetector;
enableBatchParallel;
constructor(enableBatchParallel = true) {
this.batchDetector = new BatchParallelDetector();
this.enableBatchParallel = enableBatchParallel;
}
transformPromiseAll(path) {
const node = path.node;
if (!this.isPromiseAll(node)) {
return false;
}
const arrayArg = node.arguments[0];
if (this.enableBatchParallel && this.batchDetector.canBatch(node)) {
return this.transformToBatchParallel(path, node);
}
if (t7__namespace.isArrayExpression(arrayArg)) {
return this.transformToSequential(path, node);
}
return false;
}
transformPromiseAllSettled(path) {
const node = path.node;
if (!this.isPromiseAllSettled(node)) {
return false;
}
const arrayArg = node.arguments[0];
if (t7__namespace.isArrayExpression(arrayArg)) {
const parallelId = generateUniqueId("allSettled");
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier(RuntimeFunction.RESUMABLE_PROMISE_ALL_SETTLED)), [
arrayArg,
t7__namespace.stringLiteral(parallelId)
]));
path.replaceWith(runtimeCall);
this.transformCount++;
return true;
}
return false;
}
transformToBatchParallel(path, node) {
const arrayArg = node.arguments[0];
if (!t7__namespace.isArrayExpression(arrayArg)) {
return false;
}
const batchId = generateUniqueId("batch");
const batchCallsArray = t7__namespace.arrayExpression(arrayArg.elements.map((el) => {
if (!el || t7__namespace.isSpreadElement(el)) {
return t7__namespace.nullLiteral();
}
let callNode = el;
if (t7__namespace.isAwaitExpression(callNode)) {
callNode = callNode.argument;
}
if (!t7__namespace.isCallExpression(callNode) || !t7__namespace.isMemberExpression(callNode.callee)) {
return t7__namespace.nullLiteral();
}
const callInfo = this.batchDetector.extractCallInfo(callNode);
if (!callInfo) {
return t7__namespace.nullLiteral();
}
const payloadArg = callNode.arguments[0];
return t7__namespace.objectExpression([
t7__namespace.objectProperty(t7__namespace.identifier("type"), t7__namespace.stringLiteral(callInfo.type)),
t7__namespace.objectProperty(t7__namespace.identifier("operation"), t7__namespace.stringLiteral(callInfo.operation)),
t7__namespace.objectProperty(t7__namespace.identifier("payload"), payloadArg && t7__namespace.isExpression(payloadArg) ? payloadArg : t7__namespace.objectExpression([]))
]);
}));
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier(RuntimeFunction.BATCH_PARALLEL)), [
batchCallsArray,
t7__namespace.stringLiteral(batchId)
]));
path.replaceWith(runtimeCall);
this.transformCount++;
return true;
}
transformToSequential(path, node) {
const arrayArg = node.arguments[0];
if (!t7__namespace.isArrayExpression(arrayArg)) {
return false;
}
const parallelId = generateUniqueId("parallel");
const runtimeCall = t7__namespace.awaitExpression(t7__namespace.callExpression(t7__namespace.memberExpression(t7__namespace.identifier("__runtime"), t7__namespace.identifier(RuntimeFunction.RESUMABLE_PROMISE_ALL)), [
arrayArg,
t7__namespace.stringLiteral(parallelId)
]));
path.replaceWith(runtimeCall);
this.transformCount++;
return true;
}
payloadToExpression(payload) {
const properties = [];
for (const [key, value] of Object.entries(payload)) {
properties.push(t7__namespace.objectProperty(t7__namespace.identifier(key), this.valueToExpression(value)));
}
return t7__namespace.objectExpression(properties);
}
valueToExpression(value) {
if (typeof value === "string") {
return t7__namespace.stringLiteral(value);
}
if (typeof value === "number") {
return t7__namespace.numericLiteral(value);
}
if (typeof value === "boolean") {
return t7__namespace.booleanLiteral(value);
}
if (value === null) {
return t7__namespace.nullLiteral();
}
if (Array.isArray(value)) {
return t7__namespace.arrayExpression(value.map((v) => this.valueToExpression(v)));
}
if (typeof value === "object") {
return this.payloadToExpression(value);
}
return t7__namespace.identifier("undefined");
}
isPromiseAll(node) {
const callee = node.callee;
return t7__namespace.isMemberExpression(callee) && t7__namespace.isIdentifier(callee.object, {
name: "Promise"
}) && t7__namespace.isIdentifier(callee.property, {
name: "all"
});
}
isPromiseAllSettled(node) {
const callee = node.callee;
return t7__namespace.isMemberExpression(callee) && t7__namespace.isIdentifier(callee.object, {
name: "Promise"
}) && t7__namespace.isIdentifier(callee.property, {
name: "allSettled"
});
}
getTransformCount() {
return this.transformCount;
}
resetTransformCount() {
this.transformCount = 0;
}
};
// src/runtime/errors.ts
exports.CheckpointOperation = void 0;
(function(CheckpointOperation2) {
CheckpointOperation2["SAVE"] = "save";
CheckpointOperation2["LOAD"] = "load";
CheckpointOperation2["CLEAR"] = "clear";
})(exports.CheckpointOperation || (exports.CheckpointOperation = {}));
var BatchPauseExecutionError = class extends Error {
static {
__name(this, "BatchPauseExecutionError");
}
calls;
batchId;
sequenceNumber;
constructor(calls, batchId, sequenceNumber) {
super(`Batch pause for parallel execution (${calls.length} callbacks)`);
this.name = "BatchPauseExecutionError";
this.calls = calls;
this.batchId = batchId;
this.sequenceNumber = sequenceNumber;
}
};
var CheckpointError = class extends Error {
static {
__name(this, "CheckpointError");
}
checkpointId;
operation;
constructor(message, checkpointId, operation) {
super(`Checkpoint ${operation} failed for ${checkpointId}: ${message}`);
this.name = "CheckpointError";
this.checkpointId = checkpointId;
this.operation = operation;
}
};
var TransformationError = class extends Error {
static {
__name(this, "TransformationError");
}
code;
pattern;
location;
constructor(message, code, pattern, location) {
const loc = location ? ` at line ${location.line}:${location.column}` : "";
super(`Transformation failed for ${pattern}${loc}: ${message}`);
this.name = "TransformationError";
this.code = code;
this.pattern = pattern;
this.location = location;
}
};
var InfiniteLoopDetectionError = class extends Error {
static {
__name(this, "InfiniteLoopDetectionError");
}
loopId;
iterationCount;
constructor(loopId, iterationCount) {
super(`Infinite loop detected: ${loopId} exceed