llmz 0.2.1 → 0.2.3
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/CLAUDE.md +0 -1
- package/dist/{chunk-WWZ2W4WR.cjs → chunk-3UZKLXAD.cjs} +410 -514
- package/dist/{chunk-HM5H556L.js → chunk-72ZWBNZV.js} +1 -1
- package/dist/{chunk-D6PU5YAS.cjs → chunk-DDSJTR6Q.cjs} +8 -7
- package/dist/{chunk-665O7CAN.js → chunk-GDJTKI7J.js} +5 -1
- package/dist/{chunk-YX27XM67.js → chunk-GLXADNTR.js} +13 -1
- package/dist/{chunk-FPLSJF4S.cjs → chunk-GOWPACSK.cjs} +15 -3
- package/dist/{chunk-XVVYNOTX.js → chunk-HB2G32AK.js} +3 -2
- package/dist/{chunk-YQT5G77W.cjs → chunk-HEAQAEFZ.cjs} +2 -2
- package/dist/{chunk-DD4AZSP5.cjs → chunk-HMMM32RG.cjs} +19 -15
- package/dist/{chunk-MY26EAOP.cjs → chunk-KG5E3UZZ.cjs} +20 -20
- package/dist/{chunk-YGKQEVQQ.cjs → chunk-OVRDJMZJ.cjs} +29 -37
- package/dist/{chunk-E5NHK3FS.js → chunk-OXEKAOVI.js} +6 -14
- package/dist/{chunk-ETYFOCWC.cjs → chunk-PZE6PIPG.cjs} +12 -11
- package/dist/chunk-RGUG3UMF.cjs +26 -0
- package/dist/{chunk-GC5IQDGV.js → chunk-TDALRNGL.js} +4 -3
- package/dist/chunk-UGHFNCPD.js +26 -0
- package/dist/{chunk-ZWUWCRHG.js → chunk-XMFQVARN.js} +5 -5
- package/dist/{chunk-DH35WO5G.js → chunk-YRS7KRKP.js} +401 -505
- package/dist/compiler/ast.d.ts +26 -0
- package/dist/compiler/compiler.d.ts +13 -11
- package/dist/compiler/plugins/line-tracking.d.ts +6 -4
- package/dist/compiler/plugins/replace-comment.d.ts +8 -4
- package/dist/compiler/plugins/return-async.d.ts +18 -4
- package/dist/compiler/plugins/track-tool-calls.d.ts +21 -4
- package/dist/compiler/plugins/variable-extraction.d.ts +9 -4
- package/dist/component.default.d.ts +13 -91
- package/dist/{dual-modes-RMF7V7ZQ.js → dual-modes-BYABHBDI.js} +3 -3
- package/dist/dual-modes-HO6RRIZ5.cjs +10 -0
- package/dist/emscripten-module.wasm +0 -0
- package/dist/{execute-LNUBLVMZ.js → execute-DSGJ2UNL.js} +9 -8
- package/dist/{execute-2AGZ5QUN.cjs → execute-MRZZ4AYY.cjs} +41 -40
- package/dist/exit-MX56VN2C.cjs +8 -0
- package/dist/{exit-VJATJTQI.js → exit-UQ3EUX5D.js} +2 -2
- package/dist/exit.d.ts +1 -1
- package/dist/index.cjs +210 -253
- package/dist/index.d.ts +2 -0
- package/dist/index.js +184 -227
- package/dist/index.workerd.js +17328 -0
- package/dist/prompts/chat-mode/system.md.d.ts +1 -1
- package/dist/prompts/worker-mode/system.md.d.ts +1 -1
- package/dist/prompts/worker-mode/user.md.d.ts +1 -1
- package/dist/quickjs-variant.d.ts +22 -0
- package/dist/quickjs-variant.workerd.d.ts +22 -0
- package/dist/{tool-HTAYKIA2.js → tool-KD7FL45V.js} +3 -3
- package/dist/{tool-V55GD2FS.cjs → tool-VE2YHIY2.cjs} +4 -4
- package/dist/{truncator-OZOBNYON.js → truncator-73RCL52Y.js} +2 -2
- package/dist/truncator-FZNLYSDL.cjs +12 -0
- package/dist/{typings-SCKZCAPN.js → typings-E2RROCLT.js} +2 -2
- package/dist/{typings-S6I6IPTG.cjs → typings-LQOBZXSO.cjs} +3 -3
- package/dist/typings-formatter.d.ts +19 -0
- package/dist/{utils-ZEZ66X6Q.js → utils-IATCUNKF.js} +5 -1
- package/dist/utils-ITB4NEL7.cjs +43 -0
- package/dist/utils.d.ts +18 -0
- package/dist/vm-6D3IIIU7.cjs +12 -0
- package/dist/{vm-44H4UKI2.js → vm-QO5FSGPN.js} +2 -1
- package/package.json +8 -14
- package/dist/dual-modes-ZZY6QPYT.cjs +0 -10
- package/dist/exit-LUJY7JIG.cjs +0 -8
- package/dist/truncator-7PGEGCHV.cjs +0 -12
- package/dist/utils-GLZVMRWO.cjs +0 -39
- package/dist/vm-A3OTQVIR.cjs +0 -11
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getQuickJSVariant
|
|
3
|
+
} from "./chunk-UGHFNCPD.js";
|
|
1
4
|
import {
|
|
2
5
|
CodeExecutionError,
|
|
3
6
|
InvalidCodeError,
|
|
@@ -1934,7 +1937,75 @@ var require_source_map = __commonJS({
|
|
|
1934
1937
|
var import_source_map_js = __toESM(require_source_map(), 1);
|
|
1935
1938
|
|
|
1936
1939
|
// src/compiler/compiler.ts
|
|
1937
|
-
import
|
|
1940
|
+
import MagicString from "magic-string";
|
|
1941
|
+
|
|
1942
|
+
// src/compiler/ast.ts
|
|
1943
|
+
import { parse } from "acorn";
|
|
1944
|
+
var TS_SMELL = /(^|[^:]):\s*(string|number|boolean|any|unknown|void)\b|\bas\s+(const\b|[A-Z])|\binterface\s+[A-Z]|^\s*type\s+[A-Z]|[a-zA-Z)\]]!\.|<(string|number|boolean)[,>]/m;
|
|
1945
|
+
function parseScript(code, opts) {
|
|
1946
|
+
try {
|
|
1947
|
+
return parse(code, {
|
|
1948
|
+
ecmaVersion: "latest",
|
|
1949
|
+
sourceType: "script",
|
|
1950
|
+
locations: true,
|
|
1951
|
+
allowReturnOutsideFunction: true,
|
|
1952
|
+
allowAwaitOutsideFunction: true,
|
|
1953
|
+
...opts,
|
|
1954
|
+
onComment: opts == null ? void 0 : opts.comments
|
|
1955
|
+
});
|
|
1956
|
+
} catch (err) {
|
|
1957
|
+
if (err instanceof SyntaxError && TS_SMELL.test(code)) {
|
|
1958
|
+
err.message += '. The code must be plain JavaScript: do not use TypeScript syntax (type annotations, "as" casts, generics, interfaces or type aliases).';
|
|
1959
|
+
}
|
|
1960
|
+
throw err;
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
function walk(root, enter) {
|
|
1964
|
+
const ancestors = [];
|
|
1965
|
+
const visit = (node) => {
|
|
1966
|
+
enter(node, ancestors[ancestors.length - 1] ?? null, ancestors);
|
|
1967
|
+
ancestors.push(node);
|
|
1968
|
+
for (const key of Object.keys(node)) {
|
|
1969
|
+
if (key === "loc") {
|
|
1970
|
+
continue;
|
|
1971
|
+
}
|
|
1972
|
+
const value = node[key];
|
|
1973
|
+
if (Array.isArray(value)) {
|
|
1974
|
+
for (const child of value) {
|
|
1975
|
+
if (isNode(child)) {
|
|
1976
|
+
visit(child);
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
} else if (isNode(value)) {
|
|
1980
|
+
visit(value);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
ancestors.pop();
|
|
1984
|
+
};
|
|
1985
|
+
visit(root);
|
|
1986
|
+
}
|
|
1987
|
+
var isNode = (value) => !!value && typeof value === "object" && typeof value.type === "string";
|
|
1988
|
+
var isStatementContainer = (node) => !!node && (node.type === "Program" || node.type === "BlockStatement" || node.type === "StaticBlock");
|
|
1989
|
+
function enclosingStatement(node, ancestors) {
|
|
1990
|
+
const chain = [...ancestors, node];
|
|
1991
|
+
for (let i = chain.length - 1; i > 0; i--) {
|
|
1992
|
+
if (isStatementContainer(chain[i - 1])) {
|
|
1993
|
+
return chain[i];
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
return null;
|
|
1997
|
+
}
|
|
1998
|
+
function innermostContaining(ast, start, end) {
|
|
1999
|
+
let best = null;
|
|
2000
|
+
walk(ast, (node) => {
|
|
2001
|
+
if (node.start <= start && node.end >= end) {
|
|
2002
|
+
if (!best || node.start >= best.start && node.end <= best.end) {
|
|
2003
|
+
best = node;
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
});
|
|
2007
|
+
return best;
|
|
2008
|
+
}
|
|
1938
2009
|
|
|
1939
2010
|
// src/compiler/plugins/async-wrapper.ts
|
|
1940
2011
|
var Open = "async function __fn__() {";
|
|
@@ -1952,467 +2023,303 @@ ${Close}
|
|
|
1952
2023
|
`.trim();
|
|
1953
2024
|
},
|
|
1954
2025
|
postProcessing: (code) => {
|
|
1955
|
-
code = code.slice(code.indexOf(Open) + Open.length)
|
|
2026
|
+
code = code.slice(code.indexOf(Open) + Open.length);
|
|
1956
2027
|
code = code.slice(0, code.lastIndexOf(Close));
|
|
1957
2028
|
code = code.replace(USER_CODE_START_MARKER, "").replace(USER_CODE_END_MARKER, "");
|
|
1958
|
-
return code;
|
|
2029
|
+
return code.trimEnd();
|
|
1959
2030
|
}
|
|
1960
2031
|
};
|
|
1961
2032
|
|
|
1962
2033
|
// src/compiler/plugins/line-tracking.ts
|
|
1963
|
-
import { callExpression, expressionStatement, identifier, numericLiteral } from "@babel/types";
|
|
1964
2034
|
var LineTrackingFnIdentifier = "__track__";
|
|
1965
|
-
var
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
}
|
|
2035
|
+
var TRACKED_TYPES = /* @__PURE__ */ new Set([
|
|
2036
|
+
"FunctionDeclaration",
|
|
2037
|
+
"MethodDefinition",
|
|
2038
|
+
"ReturnStatement",
|
|
2039
|
+
"AwaitExpression",
|
|
2040
|
+
"ForOfStatement",
|
|
2041
|
+
"CallExpression",
|
|
2042
|
+
"UnaryExpression",
|
|
2043
|
+
"VariableDeclaration",
|
|
2044
|
+
"AssignmentExpression"
|
|
2045
|
+
]);
|
|
2046
|
+
var SKIPPED_PARENT_TYPES = /* @__PURE__ */ new Set([
|
|
2047
|
+
"ArrowFunctionExpression",
|
|
2048
|
+
"Property",
|
|
2049
|
+
"AwaitExpression",
|
|
2050
|
+
"UnaryExpression",
|
|
2051
|
+
"ReturnStatement",
|
|
2052
|
+
"ForOfStatement",
|
|
2053
|
+
"ForStatement",
|
|
2054
|
+
"ForInStatement",
|
|
2055
|
+
"VariableDeclaration",
|
|
2056
|
+
"BinaryExpression",
|
|
2057
|
+
"LogicalExpression"
|
|
2058
|
+
]);
|
|
2059
|
+
function applyLineTracking(ctx) {
|
|
2060
|
+
const trackedLines = /* @__PURE__ */ new Set();
|
|
2061
|
+
walk(ctx.ast, (node, parent, ancestors) => {
|
|
2062
|
+
if (!parent || !node.loc) {
|
|
2063
|
+
return;
|
|
1995
2064
|
}
|
|
1996
|
-
|
|
1997
|
-
|
|
2065
|
+
if (!TRACKED_TYPES.has(node.type) && parent.type !== "BlockStatement") {
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
2068
|
+
const line = node.loc.start.line;
|
|
2069
|
+
if (trackedLines.has(line) || SKIPPED_PARENT_TYPES.has(parent.type)) {
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
const statement = enclosingStatement(node, ancestors);
|
|
2073
|
+
if (!statement) {
|
|
2074
|
+
return;
|
|
2075
|
+
}
|
|
2076
|
+
trackedLines.add(line);
|
|
2077
|
+
ctx.ms.appendLeft(statement.start, `${LineTrackingFnIdentifier}(${line});`);
|
|
2078
|
+
});
|
|
2079
|
+
}
|
|
1998
2080
|
|
|
1999
2081
|
// src/compiler/plugins/replace-comment.ts
|
|
2000
2082
|
var CommentFnIdentifier = "__comment__";
|
|
2001
|
-
var
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
return {
|
|
2005
|
-
visitor: {
|
|
2006
|
-
Program(path) {
|
|
2007
|
-
const processed = /* @__PURE__ */ new Set();
|
|
2008
|
-
path.traverse({
|
|
2009
|
-
enter(path2) {
|
|
2010
|
-
const node = path2.node;
|
|
2011
|
-
function processComments(comments, insertMethod) {
|
|
2012
|
-
if (!comments) {
|
|
2013
|
-
return;
|
|
2014
|
-
}
|
|
2015
|
-
comments.sort((a, b) => a.start > b.start ? -1 : 1).forEach((comment) => {
|
|
2016
|
-
var _a;
|
|
2017
|
-
if (processed.has(comment.loc)) {
|
|
2018
|
-
return;
|
|
2019
|
-
}
|
|
2020
|
-
processed.add(comment.loc);
|
|
2021
|
-
const commentCall = t.expressionStatement(
|
|
2022
|
-
t.callExpression(t.identifier(CommentFnIdentifier), [
|
|
2023
|
-
t.stringLiteral(comment.value.trim()),
|
|
2024
|
-
t.numericLiteral(((_a = comment.loc) == null ? void 0 : _a.start.line) ?? 0)
|
|
2025
|
-
])
|
|
2026
|
-
);
|
|
2027
|
-
const isInsideObjectProperty = t.isObjectProperty(node) || path2.findParent((path3) => t.isObjectProperty(path3.node));
|
|
2028
|
-
if (!isInsideObjectProperty) {
|
|
2029
|
-
insertMethod(commentCall);
|
|
2030
|
-
}
|
|
2031
|
-
});
|
|
2032
|
-
comments.length = 0;
|
|
2033
|
-
}
|
|
2034
|
-
processComments(node.trailingComments ?? [], (commentCall) => path2.insertAfter(commentCall));
|
|
2035
|
-
processComments(node.leadingComments ?? [], (commentCall) => path2.insertBefore(commentCall));
|
|
2036
|
-
processComments(node.innerComments ?? [], (commentCall) => path2.insertAfter(commentCall));
|
|
2037
|
-
}
|
|
2038
|
-
});
|
|
2039
|
-
}
|
|
2040
|
-
}
|
|
2041
|
-
};
|
|
2083
|
+
var MARKER_TAGS = [USER_CODE_START_MARKER, USER_CODE_END_MARKER].map((m) => m.replace(/\/\*|\*\/|\s/g, ""));
|
|
2084
|
+
var blank = (ctx, start, end) => {
|
|
2085
|
+
ctx.ms.update(start, end, ctx.code.slice(start, end).replace(/[^\n]/g, " "));
|
|
2042
2086
|
};
|
|
2087
|
+
function applyCommentReplacement(ctx, skipRanges) {
|
|
2088
|
+
for (const comment of ctx.comments) {
|
|
2089
|
+
const text = comment.value.trim();
|
|
2090
|
+
if (MARKER_TAGS.some((tag) => text.includes(tag))) {
|
|
2091
|
+
blank(ctx, comment.start, comment.end);
|
|
2092
|
+
continue;
|
|
2093
|
+
}
|
|
2094
|
+
if (skipRanges.some(([start, end]) => comment.start >= start && comment.end <= end)) {
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
const container = innermostContaining(ctx.ast, comment.start, comment.end);
|
|
2098
|
+
if (isStatementContainer(container)) {
|
|
2099
|
+
const newlines = ctx.code.slice(comment.start, comment.end).replace(/[^\n]/g, "");
|
|
2100
|
+
ctx.ms.update(
|
|
2101
|
+
comment.start,
|
|
2102
|
+
comment.end,
|
|
2103
|
+
`;${CommentFnIdentifier}(${JSON.stringify(text)}, ${comment.loc.start.line});${newlines}`
|
|
2104
|
+
);
|
|
2105
|
+
} else {
|
|
2106
|
+
blank(ctx, comment.start, comment.end);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2043
2110
|
|
|
2044
2111
|
// src/compiler/plugins/return-async.ts
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
if (t.isExpression(lastStatement.argument)) {
|
|
2062
|
-
lastStatement.argument = t.awaitExpression(lastStatement.argument);
|
|
2063
|
-
}
|
|
2064
|
-
return;
|
|
2065
|
-
}
|
|
2066
|
-
if (t.isExpressionStatement(lastStatement) && (t.isCallExpression(lastStatement.expression) || t.isAwaitExpression(lastStatement.expression))) {
|
|
2067
|
-
const returnStatement3 = t.returnStatement(t.awaitExpression(lastStatement.expression));
|
|
2068
|
-
(_b = path.get("body").get("body")[statements.length - 1]) == null ? void 0 : _b.replaceWith(returnStatement3);
|
|
2069
|
-
}
|
|
2070
|
-
}
|
|
2112
|
+
function planLastLineInstrumentation(ast) {
|
|
2113
|
+
const fn = ast.body.find((node) => {
|
|
2114
|
+
var _a;
|
|
2115
|
+
return node.type === "FunctionDeclaration" && ((_a = node.id) == null ? void 0 : _a.name) === "__fn__";
|
|
2116
|
+
});
|
|
2117
|
+
if (!fn) {
|
|
2118
|
+
return null;
|
|
2119
|
+
}
|
|
2120
|
+
const statements = fn.body.body;
|
|
2121
|
+
const last = statements[statements.length - 1];
|
|
2122
|
+
if (!last) {
|
|
2123
|
+
return null;
|
|
2124
|
+
}
|
|
2125
|
+
if (last.type === "ReturnStatement") {
|
|
2126
|
+
if (!last.argument) {
|
|
2127
|
+
return null;
|
|
2071
2128
|
}
|
|
2072
|
-
|
|
2073
|
-
}
|
|
2129
|
+
return { prefixPos: last.argument.start, prefix: "await (", suffixPos: last.argument.end, suffix: ")" };
|
|
2130
|
+
}
|
|
2131
|
+
if (last.type === "ExpressionStatement" && (last.expression.type === "CallExpression" || last.expression.type === "AwaitExpression")) {
|
|
2132
|
+
return { prefixPos: last.expression.start, prefix: "return await (", suffixPos: last.expression.end, suffix: ")" };
|
|
2133
|
+
}
|
|
2134
|
+
return null;
|
|
2135
|
+
}
|
|
2074
2136
|
|
|
2075
2137
|
// src/compiler/plugins/track-tool-calls.ts
|
|
2076
|
-
import {
|
|
2077
|
-
expressionStatement as expressionStatement2,
|
|
2078
|
-
identifier as identifier2,
|
|
2079
|
-
stringLiteral,
|
|
2080
|
-
callExpression as callExpression2,
|
|
2081
|
-
arrowFunctionExpression,
|
|
2082
|
-
blockStatement,
|
|
2083
|
-
returnStatement,
|
|
2084
|
-
tryStatement,
|
|
2085
|
-
numericLiteral as numericLiteral2,
|
|
2086
|
-
catchClause,
|
|
2087
|
-
throwStatement,
|
|
2088
|
-
awaitExpression,
|
|
2089
|
-
variableDeclaration,
|
|
2090
|
-
variableDeclarator,
|
|
2091
|
-
newExpression,
|
|
2092
|
-
memberExpression,
|
|
2093
|
-
assignmentExpression,
|
|
2094
|
-
binaryExpression
|
|
2095
|
-
} from "@babel/types";
|
|
2096
2138
|
var ToolCallTrackerFnIdentifier = "__toolc__";
|
|
2097
2139
|
var ToolTrackerRetIdentifier = "__ret__";
|
|
2098
|
-
|
|
2099
|
-
let source = path.getSource();
|
|
2100
|
-
if (path.isIdentifier()) {
|
|
2101
|
-
return {
|
|
2102
|
-
type: "single",
|
|
2103
|
-
left: source,
|
|
2104
|
-
evalFn: `let ${source} = arguments[0]; return { ${source} };`
|
|
2105
|
-
};
|
|
2106
|
-
}
|
|
2107
|
-
if (path.isArrayPattern()) {
|
|
2108
|
-
source = path.get("elements").map((el) => el.getSource()).join(", ");
|
|
2109
|
-
return {
|
|
2110
|
-
type: "array",
|
|
2111
|
-
left: source,
|
|
2112
|
-
evalFn: `let [${source}] = arguments[0] ?? []; return { ${source} };`
|
|
2113
|
-
};
|
|
2114
|
-
}
|
|
2115
|
-
if (path.isObjectPattern()) {
|
|
2116
|
-
return {
|
|
2117
|
-
type: "object",
|
|
2118
|
-
left: source,
|
|
2119
|
-
evalFn: `let ${source} = arguments[0] ?? {}; return ${source};`
|
|
2120
|
-
};
|
|
2121
|
-
}
|
|
2122
|
-
return {
|
|
2123
|
-
type: "unsupported",
|
|
2124
|
-
evalFn: "",
|
|
2125
|
-
left: ""
|
|
2126
|
-
};
|
|
2127
|
-
};
|
|
2128
|
-
var toolCallTrackingPlugin = (calls = /* @__PURE__ */ new Map()) => function({}) {
|
|
2140
|
+
function applyToolCallTracking(ctx, calls) {
|
|
2129
2141
|
let callId = 0;
|
|
2130
|
-
const
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2142
|
+
const wrappedRanges = [];
|
|
2143
|
+
const src = ctx.code;
|
|
2144
|
+
const sliceOf = (node) => src.slice(node.start, node.end);
|
|
2145
|
+
const extractAssignment = (lval) => {
|
|
2146
|
+
const source = sliceOf(lval);
|
|
2147
|
+
if (lval.type === "Identifier") {
|
|
2148
|
+
return { type: "single", left: source, evalFn: `let ${source} = arguments[0]; return { ${source} };` };
|
|
2149
|
+
}
|
|
2150
|
+
if (lval.type === "ArrayPattern") {
|
|
2151
|
+
const elements = lval.elements.map((el) => el ? sliceOf(el) : "").join(", ");
|
|
2152
|
+
return {
|
|
2153
|
+
type: "array",
|
|
2154
|
+
left: elements,
|
|
2155
|
+
evalFn: `let [${elements}] = arguments[0] ?? []; return { ${elements} };`
|
|
2156
|
+
};
|
|
2157
|
+
}
|
|
2158
|
+
if (lval.type === "ObjectPattern") {
|
|
2159
|
+
return { type: "object", left: source, evalFn: `let ${source} = arguments[0] ?? {}; return ${source};` };
|
|
2160
|
+
}
|
|
2161
|
+
return { type: "unsupported", left: "", evalFn: "" };
|
|
2162
|
+
};
|
|
2163
|
+
walk(ctx.ast, (node, parent, ancestors) => {
|
|
2164
|
+
var _a;
|
|
2165
|
+
if (node.type !== "CallExpression") {
|
|
2166
|
+
return;
|
|
2167
|
+
}
|
|
2168
|
+
if (wrappedRanges.some(([start2, end2]) => node.start >= start2 && node.end <= end2)) {
|
|
2169
|
+
return;
|
|
2170
|
+
}
|
|
2171
|
+
if ((parent == null ? void 0 : parent.type) === "YieldExpression") {
|
|
2172
|
+
return;
|
|
2173
|
+
}
|
|
2174
|
+
const declaration = [...ancestors].reverse().find((n) => n.type === "VariableDeclaration");
|
|
2175
|
+
const assignment = [...ancestors].reverse().find((n) => n.type === "AssignmentExpression");
|
|
2176
|
+
let lval = null;
|
|
2177
|
+
if (declaration) {
|
|
2178
|
+
lval = ((_a = declaration.declarations[0]) == null ? void 0 : _a.id) ?? null;
|
|
2179
|
+
}
|
|
2180
|
+
if (assignment) {
|
|
2181
|
+
lval = assignment.left;
|
|
2182
|
+
}
|
|
2183
|
+
const isAsync = (parent == null ? void 0 : parent.type) === "AwaitExpression";
|
|
2184
|
+
const start = (id) => `${ToolCallTrackerFnIdentifier}(${id}, "start");`;
|
|
2185
|
+
const end = (id, value) => `${ToolCallTrackerFnIdentifier}(${id}, "end", ${value});`;
|
|
2186
|
+
const prefix = (id) => `(${isAsync ? "async " : ""}() => {try {${start(id)}const ${ToolTrackerRetIdentifier} = ${isAsync ? "await " : ""}`;
|
|
2187
|
+
const successSuffix = (id) => `;${end(id, ToolTrackerRetIdentifier)}return ${ToolTrackerRetIdentifier};}`;
|
|
2188
|
+
if (!lval) {
|
|
2189
|
+
if (!sliceOf(node).trim().length) {
|
|
2190
|
+
return;
|
|
2191
|
+
}
|
|
2192
|
+
const catchClause2 = ` catch (err) {${end(callId, "err")}const __newError = new Error(err.message);__newError.stack = err.stack + ("\\n" + __newError.stack);throw __newError;}})()`;
|
|
2193
|
+
ctx.ms.appendLeft(node.start, prefix(callId));
|
|
2194
|
+
ctx.ms.appendRight(node.end, successSuffix(callId) + catchClause2);
|
|
2195
|
+
wrappedRanges.push([node.start, node.end]);
|
|
2196
|
+
callId++;
|
|
2197
|
+
return;
|
|
2198
|
+
}
|
|
2199
|
+
const assign = extractAssignment(lval);
|
|
2200
|
+
if (assign.type === "unsupported") {
|
|
2201
|
+
return;
|
|
2202
|
+
}
|
|
2203
|
+
const tryId = callId;
|
|
2204
|
+
callId++;
|
|
2205
|
+
const callee = node.callee;
|
|
2206
|
+
let object = "global";
|
|
2207
|
+
let tool = callee.type === "Identifier" ? callee.name : sliceOf(callee);
|
|
2208
|
+
if (callee.type === "MemberExpression") {
|
|
2209
|
+
object = sliceOf(callee.object);
|
|
2210
|
+
const property = callee.property;
|
|
2211
|
+
tool = property.type === "Identifier" ? property.name : property.type === "Literal" ? String(property.value) : sliceOf(property);
|
|
2212
|
+
}
|
|
2213
|
+
const catchClause = ` catch (err) {${end(callId, "err")}throw new Error(err.message);}})()`;
|
|
2214
|
+
ctx.ms.appendLeft(node.start, prefix(tryId));
|
|
2215
|
+
ctx.ms.appendRight(node.end, successSuffix(tryId) + catchClause);
|
|
2216
|
+
wrappedRanges.push([node.start, node.end]);
|
|
2217
|
+
calls.set(callId, { object, tool, assignment: assign });
|
|
2218
|
+
});
|
|
2219
|
+
return wrappedRanges;
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
// src/compiler/plugins/variable-extraction.ts
|
|
2223
|
+
var VariableTrackingFnIdentifier = "__var__";
|
|
2224
|
+
function applyVariableTracking(ctx, variables) {
|
|
2225
|
+
const trackerFor = (name) => {
|
|
2226
|
+
if (name.startsWith("__")) {
|
|
2227
|
+
return null;
|
|
2228
|
+
}
|
|
2229
|
+
variables.add(name);
|
|
2230
|
+
return `${VariableTrackingFnIdentifier}(${JSON.stringify(name)}, () => eval(${JSON.stringify(name)}));`;
|
|
2231
|
+
};
|
|
2232
|
+
const track = (names) => names.map(trackerFor).filter((t) => !!t).join("");
|
|
2233
|
+
const declaredNames = (id) => {
|
|
2234
|
+
if (id.type === "Identifier") {
|
|
2235
|
+
return [id.name];
|
|
2236
|
+
}
|
|
2237
|
+
if (id.type === "ObjectPattern") {
|
|
2238
|
+
const names = [];
|
|
2239
|
+
for (const prop of id.properties) {
|
|
2240
|
+
if (prop.type !== "Property") {
|
|
2241
|
+
continue;
|
|
2141
2242
|
}
|
|
2142
|
-
if (
|
|
2143
|
-
|
|
2243
|
+
if (prop.value.type === "Identifier") {
|
|
2244
|
+
names.push(prop.value.name);
|
|
2245
|
+
} else if (prop.value.type === "ObjectPattern") {
|
|
2246
|
+
names.push(...declaredNames(prop.value));
|
|
2247
|
+
} else if (prop.value.type === "AssignmentPattern" && prop.value.left.type === "Identifier") {
|
|
2248
|
+
names.push(prop.value.left.name);
|
|
2144
2249
|
}
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
)
|
|
2152
|
-
|
|
2153
|
-
lval = (_a = declaration.get("declarations")[0]) == null ? void 0 : _a.get("id");
|
|
2250
|
+
}
|
|
2251
|
+
return names;
|
|
2252
|
+
}
|
|
2253
|
+
if (id.type === "ArrayPattern") {
|
|
2254
|
+
const names = [];
|
|
2255
|
+
for (const element of id.elements) {
|
|
2256
|
+
if (!element) {
|
|
2257
|
+
continue;
|
|
2154
2258
|
}
|
|
2155
|
-
if (
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2259
|
+
if (element.type === "Identifier") {
|
|
2260
|
+
names.push(element.name);
|
|
2261
|
+
} else if (element.type === "RestElement" && element.argument.type === "Identifier") {
|
|
2262
|
+
names.push(element.argument.name);
|
|
2263
|
+
} else if (element.type === "AssignmentPattern" && element.left.type === "Identifier") {
|
|
2264
|
+
names.push(element.left.name);
|
|
2160
2265
|
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
)
|
|
2178
|
-
]),
|
|
2179
|
-
endStatement(identifier2(ToolTrackerRetIdentifier)),
|
|
2180
|
-
returnStatement(identifier2(ToolTrackerRetIdentifier))
|
|
2181
|
-
]);
|
|
2182
|
-
if (!lval) {
|
|
2183
|
-
if (path.getSource().trim().length) {
|
|
2184
|
-
const newError = identifier2("__newError");
|
|
2185
|
-
const newCall2 = callExpression2(
|
|
2186
|
-
arrowFunctionExpression(
|
|
2187
|
-
[],
|
|
2188
|
-
// params
|
|
2189
|
-
blockStatement([
|
|
2190
|
-
tryStatement(
|
|
2191
|
-
tryBodyStatement,
|
|
2192
|
-
catchClause(
|
|
2193
|
-
identifier2("err"),
|
|
2194
|
-
blockStatement([
|
|
2195
|
-
endStatement(identifier2("err")),
|
|
2196
|
-
variableDeclaration("const", [
|
|
2197
|
-
variableDeclarator(
|
|
2198
|
-
newError,
|
|
2199
|
-
newExpression(identifier2("Error"), [
|
|
2200
|
-
memberExpression(identifier2("err"), identifier2("message"))
|
|
2201
|
-
])
|
|
2202
|
-
)
|
|
2203
|
-
]),
|
|
2204
|
-
expressionStatement2(
|
|
2205
|
-
assignmentExpression(
|
|
2206
|
-
"=",
|
|
2207
|
-
memberExpression(newError, identifier2("stack")),
|
|
2208
|
-
binaryExpression(
|
|
2209
|
-
"+",
|
|
2210
|
-
memberExpression(identifier2("err"), identifier2("stack")),
|
|
2211
|
-
binaryExpression(
|
|
2212
|
-
"+",
|
|
2213
|
-
stringLiteral("\n"),
|
|
2214
|
-
memberExpression(newError, identifier2("stack"))
|
|
2215
|
-
)
|
|
2216
|
-
)
|
|
2217
|
-
)
|
|
2218
|
-
),
|
|
2219
|
-
throwStatement(newError)
|
|
2220
|
-
])
|
|
2221
|
-
)
|
|
2222
|
-
)
|
|
2223
|
-
]),
|
|
2224
|
-
isAsync
|
|
2225
|
-
),
|
|
2226
|
-
[]
|
|
2227
|
-
// args
|
|
2228
|
-
);
|
|
2229
|
-
callId++;
|
|
2230
|
-
skip.add(newCall2);
|
|
2231
|
-
path.replaceWith(newCall2);
|
|
2266
|
+
}
|
|
2267
|
+
return names;
|
|
2268
|
+
}
|
|
2269
|
+
return [];
|
|
2270
|
+
};
|
|
2271
|
+
const paramNames = (params) => {
|
|
2272
|
+
const names = [];
|
|
2273
|
+
for (const param of params) {
|
|
2274
|
+
if (param.type === "Identifier") {
|
|
2275
|
+
names.push(param.name);
|
|
2276
|
+
} else if (param.type === "AssignmentPattern" && param.left.type === "Identifier") {
|
|
2277
|
+
names.push(param.left.name);
|
|
2278
|
+
} else if (param.type === "ObjectPattern") {
|
|
2279
|
+
for (const prop of param.properties) {
|
|
2280
|
+
if (prop.type === "Property" && prop.value.type === "Identifier") {
|
|
2281
|
+
names.push(prop.value.name);
|
|
2232
2282
|
}
|
|
2233
|
-
return;
|
|
2234
2283
|
}
|
|
2235
|
-
|
|
2236
|
-
const
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
if (assign.type === "unsupported") {
|
|
2241
|
-
return;
|
|
2284
|
+
} else if (param.type === "ArrayPattern") {
|
|
2285
|
+
for (const element of param.elements) {
|
|
2286
|
+
if ((element == null ? void 0 : element.type) === "Identifier") {
|
|
2287
|
+
names.push(element.name);
|
|
2288
|
+
}
|
|
2242
2289
|
}
|
|
2243
|
-
const newCall = callExpression2(
|
|
2244
|
-
arrowFunctionExpression(
|
|
2245
|
-
[],
|
|
2246
|
-
// params
|
|
2247
|
-
blockStatement([
|
|
2248
|
-
tryStatement(
|
|
2249
|
-
tryBodyStatement,
|
|
2250
|
-
catchClause(
|
|
2251
|
-
identifier2("err"),
|
|
2252
|
-
blockStatement([
|
|
2253
|
-
endStatement(identifier2("err")),
|
|
2254
|
-
throwStatement(
|
|
2255
|
-
newExpression(identifier2("Error"), [memberExpression(identifier2("err"), identifier2("message"))])
|
|
2256
|
-
)
|
|
2257
|
-
])
|
|
2258
|
-
)
|
|
2259
|
-
)
|
|
2260
|
-
]),
|
|
2261
|
-
isAsync
|
|
2262
|
-
),
|
|
2263
|
-
[]
|
|
2264
|
-
// args
|
|
2265
|
-
);
|
|
2266
|
-
calls.set(callId, { object, tool, assignment: assign });
|
|
2267
|
-
skip.add(newCall);
|
|
2268
|
-
path.replaceWith(newCall);
|
|
2269
2290
|
}
|
|
2270
2291
|
}
|
|
2292
|
+
return names;
|
|
2271
2293
|
};
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
returnStatement as returnStatement2
|
|
2283
|
-
} from "@babel/types";
|
|
2284
|
-
var VariableTrackingFnIdentifier = "__var__";
|
|
2285
|
-
var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function({ types: t }) {
|
|
2286
|
-
let trackingStatements = [];
|
|
2287
|
-
const trackVariable = (variableName) => {
|
|
2288
|
-
if (variableName.startsWith("__")) {
|
|
2294
|
+
walk(ctx.ast, (node, parent) => {
|
|
2295
|
+
if (node.type === "VariableDeclaration") {
|
|
2296
|
+
if (parent && (parent.type === "ForStatement" || parent.type === "ForInStatement" || parent.type === "ForOfStatement")) {
|
|
2297
|
+
return;
|
|
2298
|
+
}
|
|
2299
|
+
const trackers = track(node.declarations.flatMap((d) => declaredNames(d.id)));
|
|
2300
|
+
if (trackers) {
|
|
2301
|
+
const semi = ctx.code[node.end - 1] === ";" ? "" : ";";
|
|
2302
|
+
ctx.ms.appendRight(node.end, `${semi}${trackers}`);
|
|
2303
|
+
}
|
|
2289
2304
|
return;
|
|
2290
2305
|
}
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
visitor: {
|
|
2303
|
-
FunctionDeclaration(path) {
|
|
2304
|
-
path.node.params.forEach((param) => {
|
|
2305
|
-
if (t.isIdentifier(param)) {
|
|
2306
|
-
trackVariable(param.name);
|
|
2307
|
-
} else if (t.isAssignmentPattern(param) && t.isIdentifier(param.left)) {
|
|
2308
|
-
trackVariable(param.left.name);
|
|
2309
|
-
} else if (t.isObjectPattern(param)) {
|
|
2310
|
-
param.properties.forEach((prop) => {
|
|
2311
|
-
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2312
|
-
trackVariable(prop.value.name);
|
|
2313
|
-
}
|
|
2314
|
-
});
|
|
2315
|
-
} else if (t.isArrayPattern(param)) {
|
|
2316
|
-
param.elements.forEach((element) => {
|
|
2317
|
-
if (t.isIdentifier(element)) {
|
|
2318
|
-
trackVariable(element.name);
|
|
2319
|
-
}
|
|
2320
|
-
});
|
|
2321
|
-
}
|
|
2322
|
-
});
|
|
2323
|
-
if (trackingStatements.length) {
|
|
2324
|
-
path.get("body").unshiftContainer("body", trackingStatements);
|
|
2325
|
-
trackingStatements = [];
|
|
2326
|
-
}
|
|
2327
|
-
},
|
|
2328
|
-
ArrowFunctionExpression(path) {
|
|
2329
|
-
path.node.params.forEach((param) => {
|
|
2330
|
-
if (t.isIdentifier(param)) {
|
|
2331
|
-
trackVariable(param.name);
|
|
2332
|
-
} else if (t.isAssignmentPattern(param) && t.isIdentifier(param.left)) {
|
|
2333
|
-
trackVariable(param.left.name);
|
|
2334
|
-
} else if (t.isObjectPattern(param)) {
|
|
2335
|
-
param.properties.forEach((prop) => {
|
|
2336
|
-
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2337
|
-
trackVariable(prop.value.name);
|
|
2338
|
-
}
|
|
2339
|
-
});
|
|
2340
|
-
} else if (t.isArrayPattern(param)) {
|
|
2341
|
-
param.elements.forEach((element) => {
|
|
2342
|
-
if (t.isIdentifier(element)) {
|
|
2343
|
-
trackVariable(element.name);
|
|
2344
|
-
}
|
|
2345
|
-
});
|
|
2346
|
-
}
|
|
2347
|
-
});
|
|
2348
|
-
if (trackingStatements.length) {
|
|
2349
|
-
const body = path.get("body");
|
|
2350
|
-
if (body.isBlockStatement()) {
|
|
2351
|
-
body.unshiftContainer("body", trackingStatements);
|
|
2352
|
-
} else if (body.isExpression() && body.getSource().trim().length) {
|
|
2353
|
-
const newBody = blockStatement2([...trackingStatements, returnStatement2(body.node)]);
|
|
2354
|
-
path.get("body").replaceWith(newBody);
|
|
2355
|
-
}
|
|
2356
|
-
trackingStatements = [];
|
|
2357
|
-
}
|
|
2358
|
-
},
|
|
2359
|
-
VariableDeclaration(path) {
|
|
2360
|
-
const parent = path.parentPath;
|
|
2361
|
-
const handleObjectPattern = (pattern) => {
|
|
2362
|
-
pattern.properties.forEach((prop) => {
|
|
2363
|
-
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2364
|
-
trackVariable(prop.value.name);
|
|
2365
|
-
} else if (t.isObjectProperty(prop) && t.isObjectPattern(prop.value)) {
|
|
2366
|
-
handleObjectPattern(prop.value);
|
|
2367
|
-
} else if (t.isObjectProperty(prop) && t.isAssignmentPattern(prop.value) && t.isIdentifier(prop.value.left)) {
|
|
2368
|
-
trackVariable(prop.value.left.name);
|
|
2369
|
-
}
|
|
2370
|
-
});
|
|
2371
|
-
};
|
|
2372
|
-
path.node.declarations.forEach((declarator) => {
|
|
2373
|
-
if (parent.isForXStatement() || parent.isForStatement()) {
|
|
2374
|
-
return;
|
|
2375
|
-
}
|
|
2376
|
-
if (t.isIdentifier(declarator.id)) {
|
|
2377
|
-
trackVariable(declarator.id.name);
|
|
2378
|
-
}
|
|
2379
|
-
if (t.isObjectPattern(declarator.id)) {
|
|
2380
|
-
handleObjectPattern(declarator.id);
|
|
2381
|
-
}
|
|
2382
|
-
if (t.isArrayPattern(declarator.id)) {
|
|
2383
|
-
declarator.id.elements.forEach((element) => {
|
|
2384
|
-
if (t.isIdentifier(element)) {
|
|
2385
|
-
trackVariable(element.name);
|
|
2386
|
-
} else if (t.isRestElement(element) && t.isIdentifier(element.argument)) {
|
|
2387
|
-
trackVariable(element.argument.name);
|
|
2388
|
-
} else if (t.isAssignmentPattern(element) && t.isIdentifier(element.left)) {
|
|
2389
|
-
trackVariable(element.left.name);
|
|
2390
|
-
}
|
|
2391
|
-
});
|
|
2392
|
-
}
|
|
2393
|
-
});
|
|
2394
|
-
if (trackingStatements.length) {
|
|
2395
|
-
path.insertAfter(trackingStatements);
|
|
2396
|
-
trackingStatements = [];
|
|
2397
|
-
}
|
|
2306
|
+
if (node.type === "FunctionDeclaration" || node.type === "ArrowFunctionExpression") {
|
|
2307
|
+
const trackers = track(paramNames(node.params));
|
|
2308
|
+
if (!trackers) {
|
|
2309
|
+
return;
|
|
2310
|
+
}
|
|
2311
|
+
const body = node.body;
|
|
2312
|
+
if (body.type === "BlockStatement") {
|
|
2313
|
+
ctx.ms.appendRight(body.start + 1, trackers);
|
|
2314
|
+
} else {
|
|
2315
|
+
ctx.ms.appendLeft(body.start, `{${trackers}return (`);
|
|
2316
|
+
ctx.ms.appendRight(body.end, ");}");
|
|
2398
2317
|
}
|
|
2399
2318
|
}
|
|
2400
|
-
};
|
|
2401
|
-
}
|
|
2319
|
+
});
|
|
2320
|
+
}
|
|
2402
2321
|
|
|
2403
2322
|
// src/compiler/compiler.ts
|
|
2404
|
-
var DEFAULT_TRANSFORM_OPTIONS = {
|
|
2405
|
-
parserOpts: {
|
|
2406
|
-
allowReturnOutsideFunction: true,
|
|
2407
|
-
allowAwaitOutsideFunction: true,
|
|
2408
|
-
strictMode: true,
|
|
2409
|
-
startLine: 1
|
|
2410
|
-
},
|
|
2411
|
-
sourceFileName: "<anonymous>",
|
|
2412
|
-
sourceMaps: true,
|
|
2413
|
-
minified: false,
|
|
2414
|
-
retainLines: true
|
|
2415
|
-
};
|
|
2416
2323
|
var Identifiers = {
|
|
2417
2324
|
ConsoleObjIdentifier: "console",
|
|
2418
2325
|
LineTrackingFnIdentifier,
|
|
@@ -2421,93 +2328,57 @@ var Identifiers = {
|
|
|
2421
2328
|
ToolTrackerRetIdentifier,
|
|
2422
2329
|
CommentFnIdentifier
|
|
2423
2330
|
};
|
|
2331
|
+
var FUNCTION_TYPES = /* @__PURE__ */ new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
|
|
2424
2332
|
function hasTopLevelReturn(code) {
|
|
2425
|
-
let found = false;
|
|
2426
|
-
const detectTopLevelReturnPlugin = () => ({
|
|
2427
|
-
visitor: {
|
|
2428
|
-
ReturnStatement(path) {
|
|
2429
|
-
if (!path.getFunctionParent()) {
|
|
2430
|
-
found = true;
|
|
2431
|
-
}
|
|
2432
|
-
}
|
|
2433
|
-
}
|
|
2434
|
-
});
|
|
2435
2333
|
try {
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
plugins: [detectTopLevelReturnPlugin],
|
|
2443
|
-
filename: "<anonymous>.ts",
|
|
2444
|
-
code: false
|
|
2334
|
+
const ast = parseScript(code);
|
|
2335
|
+
let found = false;
|
|
2336
|
+
walk(ast, (node, _parent, ancestors) => {
|
|
2337
|
+
if (node.type === "ReturnStatement" && !ancestors.some((a) => FUNCTION_TYPES.has(a.type))) {
|
|
2338
|
+
found = true;
|
|
2339
|
+
}
|
|
2445
2340
|
});
|
|
2341
|
+
return found;
|
|
2446
2342
|
} catch {
|
|
2447
2343
|
return /\breturn\b/.test(code);
|
|
2448
2344
|
}
|
|
2449
|
-
return found;
|
|
2450
2345
|
}
|
|
2451
2346
|
function compile(code) {
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
allowReturnOutsideFunction: true,
|
|
2456
|
-
allowAwaitOutsideFunction: true
|
|
2457
|
-
},
|
|
2458
|
-
presets: ["typescript"],
|
|
2459
|
-
plugins: [instrumentLastLinePlugin],
|
|
2460
|
-
filename: "<anonymous>.ts",
|
|
2461
|
-
sourceFileName: "<anonymous>",
|
|
2462
|
-
sourceMaps: true,
|
|
2463
|
-
minified: false,
|
|
2464
|
-
retainLines: true
|
|
2465
|
-
});
|
|
2347
|
+
const wrapped = AsyncWrapper.preProcessing(code);
|
|
2348
|
+
const comments = [];
|
|
2349
|
+
const ast = parseScript(wrapped, { comments });
|
|
2466
2350
|
const variables = /* @__PURE__ */ new Set();
|
|
2467
2351
|
const toolCalls = /* @__PURE__ */ new Map();
|
|
2468
|
-
const
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2352
|
+
const lastLine = planLastLineInstrumentation(ast);
|
|
2353
|
+
const msMarkers = new MagicString(wrapped);
|
|
2354
|
+
if (lastLine) {
|
|
2355
|
+
msMarkers.appendLeft(lastLine.prefixPos, lastLine.prefix);
|
|
2356
|
+
msMarkers.appendRight(lastLine.suffixPos, lastLine.suffix);
|
|
2357
|
+
}
|
|
2358
|
+
const codeWithMarkers = msMarkers.toString();
|
|
2359
|
+
const ms = new MagicString(wrapped);
|
|
2360
|
+
const ctx = { code: wrapped, ms, ast, comments };
|
|
2361
|
+
applyLineTracking(ctx);
|
|
2362
|
+
if (lastLine) {
|
|
2363
|
+
ms.appendLeft(lastLine.prefixPos, lastLine.prefix);
|
|
2364
|
+
}
|
|
2365
|
+
const wrappedRanges = applyToolCallTracking(ctx, toolCalls);
|
|
2366
|
+
applyCommentReplacement(ctx, wrappedRanges);
|
|
2367
|
+
applyVariableTracking(ctx, variables);
|
|
2368
|
+
if (lastLine) {
|
|
2369
|
+
ms.appendRight(lastLine.suffixPos, lastLine.suffix);
|
|
2370
|
+
}
|
|
2371
|
+
const outputCode = AsyncWrapper.postProcessing(ms.toString());
|
|
2372
|
+
const map = ms.generateMap({ hires: true, source: "<anonymous>" });
|
|
2484
2373
|
return {
|
|
2485
2374
|
code: outputCode,
|
|
2486
2375
|
codeWithMarkers,
|
|
2487
|
-
|
|
2488
|
-
map: output.map,
|
|
2376
|
+
map,
|
|
2489
2377
|
variables,
|
|
2490
2378
|
toolCalls
|
|
2491
2379
|
};
|
|
2492
2380
|
}
|
|
2493
2381
|
|
|
2494
|
-
// src/quickjs-variant.ts
|
|
2495
|
-
var getVariant = async () => {
|
|
2496
|
-
const module = await import("@jitl/quickjs-singlefile-browser-release-sync");
|
|
2497
|
-
return module.default;
|
|
2498
|
-
};
|
|
2499
|
-
var BundledReleaseSyncVariant = {
|
|
2500
|
-
type: "sync",
|
|
2501
|
-
importFFI: async () => {
|
|
2502
|
-
const variant = await getVariant();
|
|
2503
|
-
return variant.importFFI();
|
|
2504
|
-
},
|
|
2505
|
-
importModuleLoader: async () => {
|
|
2506
|
-
const variant = await getVariant();
|
|
2507
|
-
return variant.importModuleLoader();
|
|
2508
|
-
}
|
|
2509
|
-
};
|
|
2510
|
-
|
|
2511
2382
|
// src/vm/errors.ts
|
|
2512
2383
|
var handleErrorQuickJS = (err, code, _consumer, traces, variables, lines_executed, userCodeStartLine, currentToolCall) => {
|
|
2513
2384
|
err = Signals.maybeDeserializeError(err);
|
|
@@ -2539,12 +2410,18 @@ var handleErrorNode = (err, code, consumer, traces, variables, _lines_executed,
|
|
|
2539
2410
|
const stackTrace = err.stack || "";
|
|
2540
2411
|
const LINE_OFFSET = 1;
|
|
2541
2412
|
const regex = /<anonymous>:(\d+):(\d+)/g;
|
|
2413
|
+
const ASYNC_FUNCTION_HEADER_LINES = 2;
|
|
2414
|
+
const SLICED_WRAPPER_LINES = 2;
|
|
2415
|
+
const KEPT_BLANK_LINES = 1;
|
|
2416
|
+
const RUNTIME_TO_MAP_OFFSET = SLICED_WRAPPER_LINES - ASYNC_FUNCTION_HEADER_LINES - KEPT_BLANK_LINES;
|
|
2417
|
+
const WRAPPED_HEADER_LINES = 3;
|
|
2542
2418
|
const matches = [...stackTrace.matchAll(regex)].map((x) => {
|
|
2419
|
+
const mapLine = Number(x[1]) + RUNTIME_TO_MAP_OFFSET;
|
|
2543
2420
|
const originalLine = consumer.originalPositionFor({
|
|
2544
|
-
line:
|
|
2421
|
+
line: mapLine,
|
|
2545
2422
|
column: Number(x[2])
|
|
2546
2423
|
});
|
|
2547
|
-
const line = originalLine.line ??
|
|
2424
|
+
const line = (originalLine.line ?? mapLine) - WRAPPED_HEADER_LINES;
|
|
2548
2425
|
const actualLine = lines[line - LINE_OFFSET] ?? "";
|
|
2549
2426
|
const whiteSpacesCount = actualLine.length - actualLine.trimStart().length;
|
|
2550
2427
|
const minColumn = Math.max(whiteSpacesCount, originalLine.column);
|
|
@@ -2764,7 +2641,15 @@ import {
|
|
|
2764
2641
|
shouldInterruptAfterDeadline
|
|
2765
2642
|
} from "quickjs-emscripten-core";
|
|
2766
2643
|
var _quickJSModule;
|
|
2767
|
-
var
|
|
2644
|
+
var _loadedVariant;
|
|
2645
|
+
var loadQuickJSModule = () => {
|
|
2646
|
+
const variant = getQuickJSVariant();
|
|
2647
|
+
if (!_quickJSModule || _loadedVariant !== variant) {
|
|
2648
|
+
_loadedVariant = variant;
|
|
2649
|
+
_quickJSModule = newQuickJSWASMModuleFromVariant(variant);
|
|
2650
|
+
}
|
|
2651
|
+
return _quickJSModule;
|
|
2652
|
+
};
|
|
2768
2653
|
var QuickJSDriver = class {
|
|
2769
2654
|
async execute(ctx) {
|
|
2770
2655
|
const { transformed, consumer, context, traces, signal, timeout, code, lines_executed, variables } = ctx;
|
|
@@ -3264,6 +3149,7 @@ var useQuickJS = () => {
|
|
|
3264
3149
|
var _a;
|
|
3265
3150
|
return typeof process === "undefined" || ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.USE_QUICKJS) !== "false";
|
|
3266
3151
|
};
|
|
3152
|
+
var isWorkerd = () => typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.userAgent) === "Cloudflare-Workers";
|
|
3267
3153
|
var warmupVM = () => {
|
|
3268
3154
|
if (useQuickJS()) {
|
|
3269
3155
|
loadQuickJSModule().catch(() => {
|
|
@@ -3290,10 +3176,9 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
3290
3176
|
version: transformed.map.version.toString(),
|
|
3291
3177
|
mappings: transformed.map.mappings,
|
|
3292
3178
|
names: transformed.map.names,
|
|
3293
|
-
sources:
|
|
3179
|
+
sources: transformed.map.sources,
|
|
3294
3180
|
sourcesContent: [transformed.code],
|
|
3295
|
-
file: transformed.map.file
|
|
3296
|
-
sourceRoot: transformed.map.sourceRoot
|
|
3181
|
+
file: transformed.map.file
|
|
3297
3182
|
});
|
|
3298
3183
|
context ??= {};
|
|
3299
3184
|
for (const name of Array.from(transformed.variables)) {
|
|
@@ -3316,16 +3201,27 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
3316
3201
|
currentToolCall: void 0
|
|
3317
3202
|
});
|
|
3318
3203
|
} catch (quickjsError) {
|
|
3204
|
+
const variant = getQuickJSVariant();
|
|
3319
3205
|
const debugInfo = {
|
|
3320
3206
|
error: (quickjsError == null ? void 0 : quickjsError.message) || String(quickjsError),
|
|
3321
3207
|
errorStack: quickjsError == null ? void 0 : quickjsError.stack,
|
|
3322
|
-
wasmSource:
|
|
3323
|
-
wasmLoadedSuccessfully:
|
|
3324
|
-
wasmSize:
|
|
3325
|
-
wasmLoadError:
|
|
3208
|
+
wasmSource: variant._wasmSource,
|
|
3209
|
+
wasmLoadedSuccessfully: variant._wasmLoadedSuccessfully,
|
|
3210
|
+
wasmSize: variant._wasmSize,
|
|
3211
|
+
wasmLoadError: variant._wasmLoadError,
|
|
3326
3212
|
nodeVersion: typeof process !== "undefined" && process.version ? process.version : "undefined",
|
|
3327
3213
|
platform: typeof process !== "undefined" && process.platform ? process.platform : "undefined"
|
|
3328
3214
|
};
|
|
3215
|
+
if (isWorkerd()) {
|
|
3216
|
+
console.error("QuickJS failed to load and the node driver is unavailable on Cloudflare Workers.");
|
|
3217
|
+
console.error("Debug info:", JSON.stringify(debugInfo, null, 2));
|
|
3218
|
+
if (quickjsError instanceof Error) {
|
|
3219
|
+
;
|
|
3220
|
+
quickjsError.debugInfo = debugInfo;
|
|
3221
|
+
throw quickjsError;
|
|
3222
|
+
}
|
|
3223
|
+
throw new Error(`QuickJS failed to load on Cloudflare Workers: ${debugInfo.error}`);
|
|
3224
|
+
}
|
|
3329
3225
|
console.warn("QuickJS failed to load, falling back to node driver.");
|
|
3330
3226
|
console.warn("Error:", (quickjsError == null ? void 0 : quickjsError.message) || quickjsError);
|
|
3331
3227
|
console.warn("Debug info:", JSON.stringify(debugInfo, null, 2));
|