llmz 0.1.4 → 0.2.0
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/README.md +185 -98
- package/dist/chat.d.ts +34 -0
- package/dist/{chunk-TIZWOIDF.js → chunk-33AB6SVX.js} +134 -76
- package/dist/{chunk-EFEF2AL7.js → chunk-DH35WO5G.js} +93 -556
- package/dist/chunk-E5NHK3FS.js +1637 -0
- package/dist/{chunk-L74UZONX.js → chunk-HM5H556L.js} +3 -1
- package/dist/chunk-I56MPOVG.cjs +62 -0
- package/dist/{chunk-C7T6OJB4.cjs → chunk-IKLFMVSW.cjs} +144 -86
- package/dist/chunk-KWQTYXAT.js +62 -0
- package/dist/{chunk-JXD2KJVB.cjs → chunk-WWZ2W4WR.cjs} +103 -566
- package/dist/chunk-YGKQEVQQ.cjs +1637 -0
- package/dist/{chunk-ZPHRTTQX.cjs → chunk-YQT5G77W.cjs} +3 -1
- package/dist/compiler/compiler.d.ts +7 -2
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/plugins/async-wrapper.d.ts +11 -0
- package/dist/component-2NYFY6CW.cjs +14 -0
- package/dist/{component-2Z3KLWLX.js → component-ASNZHQUR.js} +3 -7
- package/dist/component.d.ts +3 -10
- package/dist/context.d.ts +88 -2
- package/dist/{dual-modes-HEXPFVUE.js → dual-modes-RMF7V7ZQ.js} +2 -4
- package/dist/dual-modes-ZZY6QPYT.cjs +10 -0
- package/dist/{execute-22PSGL7P.cjs → execute-BXPFSEVX.cjs} +331 -87
- package/dist/{execute-ZENBZBWL.js → execute-HIRXXR6X.js} +321 -77
- package/dist/index.cjs +502 -27
- package/dist/index.d.ts +8 -5
- package/dist/index.js +491 -16
- package/dist/message-stream/index.d.ts +9 -0
- package/dist/message-stream/instructions.d.ts +22 -0
- package/dist/message-stream/normalizer.d.ts +21 -0
- package/dist/message-stream/parser.d.ts +81 -0
- package/dist/message-stream/registry.d.ts +14 -0
- package/dist/message-stream/replayable.d.ts +27 -0
- package/dist/message-stream/sanitize.d.ts +10 -0
- package/dist/message-stream/stream.d.ts +54 -0
- package/dist/message-stream/types.d.ts +96 -0
- package/dist/message-stream/validator.d.ts +23 -0
- package/dist/message-stream/validator.test.d.ts +1 -0
- package/dist/prompts/chat-mode/system.md.d.ts +1 -1
- package/dist/prompts/chat-mode/user.md.d.ts +1 -1
- package/dist/prompts/common.d.ts +5 -5
- package/dist/prompts/prompt.d.ts +43 -6
- package/dist/prompts/protocol.d.ts +18 -0
- package/dist/prompts/worker-mode/system.md.d.ts +1 -1
- package/dist/prompts/worker-mode/user.md.d.ts +1 -1
- package/dist/result.d.ts +31 -0
- package/dist/runtime/execute.test.d.ts +1 -0
- package/dist/runtime/generate.d.ts +24 -1
- package/dist/runtime/interpret-result.d.ts +10 -0
- package/dist/runtime/types.d.ts +29 -4
- package/dist/truncator-7PGEGCHV.cjs +12 -0
- package/dist/{truncator-3U2QUUJJ.js → truncator-OZOBNYON.js} +3 -1
- package/dist/truncator.d.ts +2 -0
- package/dist/types.d.ts +8 -1
- package/dist/vm/drivers/quickjs.d.ts +2 -0
- package/dist/vm/index.d.ts +7 -0
- package/dist/vm/instrument.d.ts +1 -1
- package/dist/{vm-PYCKS77H.js → vm-44H4UKI2.js} +5 -4
- package/dist/{vm-VHB67FVH.cjs → vm-A3OTQVIR.cjs} +4 -3
- package/package.json +6 -6
- package/dist/chunk-2O3UTPVH.js +0 -813
- package/dist/chunk-3T5IU7OU.js +0 -186
- package/dist/chunk-BBX2LDZE.cjs +0 -813
- package/dist/chunk-CQ3UJW76.cjs +0 -186
- package/dist/compiler/plugins/async-iterator.d.ts +0 -6
- package/dist/compiler/plugins/braces-tsx.d.ts +0 -3
- package/dist/compiler/plugins/html-to-markdown.d.ts +0 -21
- package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +0 -5
- package/dist/compiler/plugins/jsx-undefined-vars.d.ts +0 -14
- package/dist/component-NNHBHD72.cjs +0 -18
- package/dist/dual-modes-LLIWZCHC.cjs +0 -12
- package/dist/truncator-3ZN7W6NB.cjs +0 -10
- /package/dist/{compiler/plugins/html-to-markdown.test.d.ts → message-stream/instructions.test.d.ts} +0 -0
- /package/dist/{compiler/plugins/jsx-undefined-vars.test.d.ts → message-stream/normalizer.test.d.ts} +0 -0
- /package/dist/{component.test.d.ts → message-stream/parser.test.d.ts} +0 -0
- /package/dist/{vm/vm-jsx.test.d.ts → message-stream/stream.test.d.ts} +0 -0
|
@@ -8,9 +8,6 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
cleanStackTrace
|
|
10
10
|
} from "./chunk-YEAWWJSJ.js";
|
|
11
|
-
import {
|
|
12
|
-
createJsxComponent
|
|
13
|
-
} from "./chunk-XTEHT2TK.js";
|
|
14
11
|
import {
|
|
15
12
|
__commonJS,
|
|
16
13
|
__toESM,
|
|
@@ -1937,15 +1934,14 @@ var require_source_map = __commonJS({
|
|
|
1937
1934
|
var import_source_map_js = __toESM(require_source_map(), 1);
|
|
1938
1935
|
|
|
1939
1936
|
// src/compiler/compiler.ts
|
|
1940
|
-
import jsxPlugin from "@babel/plugin-transform-react-jsx";
|
|
1941
1937
|
import * as Babel from "@babel/standalone";
|
|
1942
1938
|
|
|
1943
|
-
// src/compiler/plugins/async-
|
|
1944
|
-
var Open = "async function
|
|
1939
|
+
// src/compiler/plugins/async-wrapper.ts
|
|
1940
|
+
var Open = "async function __fn__() {";
|
|
1945
1941
|
var Close = "}";
|
|
1946
1942
|
var USER_CODE_START_MARKER = "/* __LLMZ_USER_CODE_START__ */";
|
|
1947
1943
|
var USER_CODE_END_MARKER = "/* __LLMZ_USER_CODE_END__ */";
|
|
1948
|
-
var
|
|
1944
|
+
var AsyncWrapper = {
|
|
1949
1945
|
preProcessing: (code) => {
|
|
1950
1946
|
return `"use strict";
|
|
1951
1947
|
${Open}
|
|
@@ -1963,445 +1959,10 @@ ${Close}
|
|
|
1963
1959
|
}
|
|
1964
1960
|
};
|
|
1965
1961
|
|
|
1966
|
-
// src/compiler/plugins/braces-tsx.ts
|
|
1967
|
-
import { parse } from "@babel/parser";
|
|
1968
|
-
function fixTSXUntilNoErrors(rawCode, maxAttempts = 50) {
|
|
1969
|
-
let code = rawCode;
|
|
1970
|
-
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
1971
|
-
try {
|
|
1972
|
-
parse(code, {
|
|
1973
|
-
sourceType: "module",
|
|
1974
|
-
plugins: ["typescript", "jsx"]
|
|
1975
|
-
});
|
|
1976
|
-
return code;
|
|
1977
|
-
} catch (err) {
|
|
1978
|
-
if (err.code !== "BABEL_PARSER_SYNTAX_ERROR" || !err.loc) {
|
|
1979
|
-
throw err;
|
|
1980
|
-
}
|
|
1981
|
-
const fixed = fixOneBraceProblemAt(code, err.loc);
|
|
1982
|
-
if (!fixed) {
|
|
1983
|
-
throw err;
|
|
1984
|
-
}
|
|
1985
|
-
code = fixed;
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
throw new Error("Hit maxAttempts while trying to fix TSX code");
|
|
1989
|
-
}
|
|
1990
|
-
function fixOneBraceProblemAt(code, loc) {
|
|
1991
|
-
const lines = code.split("\n");
|
|
1992
|
-
if (loc.line < 1 || loc.line > lines.length) {
|
|
1993
|
-
return null;
|
|
1994
|
-
}
|
|
1995
|
-
const lineStr = lines[loc.line - 1];
|
|
1996
|
-
if (loc.column < 0 || lineStr && loc.column > lineStr.length) {
|
|
1997
|
-
return null;
|
|
1998
|
-
}
|
|
1999
|
-
const absIndex = lines.slice(0, loc.line - 1).reduce((sum, l) => sum + l.length + 1, 0) + loc.column;
|
|
2000
|
-
const braceIndex = findNearestBrace(code, absIndex);
|
|
2001
|
-
if (braceIndex == null) {
|
|
2002
|
-
return null;
|
|
2003
|
-
}
|
|
2004
|
-
const braceChar = code[braceIndex];
|
|
2005
|
-
if (braceChar !== "{" && braceChar !== "}") {
|
|
2006
|
-
return null;
|
|
2007
|
-
}
|
|
2008
|
-
const entity = braceChar === "{" ? "{" : "}";
|
|
2009
|
-
return code.slice(0, braceIndex) + entity + code.slice(braceIndex + 1);
|
|
2010
|
-
}
|
|
2011
|
-
function findNearestBrace(code, startIndex) {
|
|
2012
|
-
for (let i = startIndex; i >= 0; i--) {
|
|
2013
|
-
if (code[i] === "{" || code[i] === "}") {
|
|
2014
|
-
return i;
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
for (let i = startIndex; i < code.length; i++) {
|
|
2018
|
-
if (code[i] === "{" || code[i] === "}") {
|
|
2019
|
-
return i;
|
|
2020
|
-
}
|
|
2021
|
-
}
|
|
2022
|
-
return null;
|
|
2023
|
-
}
|
|
2024
|
-
var HTML_ENTITIES = {
|
|
2025
|
-
"{": "{",
|
|
2026
|
-
"}": "}",
|
|
2027
|
-
"<": "<",
|
|
2028
|
-
">": ">",
|
|
2029
|
-
"&": "&",
|
|
2030
|
-
'"': """,
|
|
2031
|
-
"'": "'",
|
|
2032
|
-
"`": "`"
|
|
2033
|
-
};
|
|
2034
|
-
function escapeBracesInCodeFences(tsx) {
|
|
2035
|
-
let i = 0;
|
|
2036
|
-
let output = "";
|
|
2037
|
-
let insideFencedBlock = false;
|
|
2038
|
-
let jsxDepth = 0;
|
|
2039
|
-
let braceDepth = 0;
|
|
2040
|
-
while (i < tsx.length) {
|
|
2041
|
-
const char = tsx[i];
|
|
2042
|
-
if (char === "<" && !insideFencedBlock) {
|
|
2043
|
-
if (tsx[i + 1] !== "/") {
|
|
2044
|
-
jsxDepth++;
|
|
2045
|
-
} else {
|
|
2046
|
-
jsxDepth--;
|
|
2047
|
-
}
|
|
2048
|
-
output += char;
|
|
2049
|
-
i++;
|
|
2050
|
-
continue;
|
|
2051
|
-
}
|
|
2052
|
-
if (jsxDepth > 0 && !insideFencedBlock) {
|
|
2053
|
-
if (char === "{") {
|
|
2054
|
-
braceDepth++;
|
|
2055
|
-
output += char;
|
|
2056
|
-
i++;
|
|
2057
|
-
continue;
|
|
2058
|
-
} else if (char === "}") {
|
|
2059
|
-
braceDepth--;
|
|
2060
|
-
output += char;
|
|
2061
|
-
i++;
|
|
2062
|
-
continue;
|
|
2063
|
-
}
|
|
2064
|
-
}
|
|
2065
|
-
if (tsx.startsWith("```", i)) {
|
|
2066
|
-
insideFencedBlock = !insideFencedBlock;
|
|
2067
|
-
output += "```";
|
|
2068
|
-
i += 3;
|
|
2069
|
-
continue;
|
|
2070
|
-
}
|
|
2071
|
-
if (jsxDepth > 0 && braceDepth === 0 && !insideFencedBlock && char === "`") {
|
|
2072
|
-
const closingIndex = tsx.indexOf("`", i + 1);
|
|
2073
|
-
if (closingIndex !== -1) {
|
|
2074
|
-
const inlineCode = tsx.slice(i + 1, closingIndex);
|
|
2075
|
-
let escaped = "`";
|
|
2076
|
-
for (const c of inlineCode) {
|
|
2077
|
-
escaped += HTML_ENTITIES[c] || c;
|
|
2078
|
-
}
|
|
2079
|
-
escaped += "`";
|
|
2080
|
-
output += escaped;
|
|
2081
|
-
i = closingIndex + 1;
|
|
2082
|
-
continue;
|
|
2083
|
-
}
|
|
2084
|
-
}
|
|
2085
|
-
if (insideFencedBlock && char in HTML_ENTITIES) {
|
|
2086
|
-
output += HTML_ENTITIES[char];
|
|
2087
|
-
i++;
|
|
2088
|
-
continue;
|
|
2089
|
-
}
|
|
2090
|
-
output += char;
|
|
2091
|
-
i++;
|
|
2092
|
-
}
|
|
2093
|
-
return output;
|
|
2094
|
-
}
|
|
2095
|
-
var JSXMarkdown = {
|
|
2096
|
-
preProcessing: (code) => {
|
|
2097
|
-
code = escapeBracesInCodeFences(code);
|
|
2098
|
-
code = fixTSXUntilNoErrors(code);
|
|
2099
|
-
return code;
|
|
2100
|
-
}
|
|
2101
|
-
};
|
|
2102
|
-
|
|
2103
|
-
// src/compiler/plugins/html-to-markdown.ts
|
|
2104
|
-
import { types as t } from "@babel/core";
|
|
2105
|
-
function htmlToMarkdownPlugin() {
|
|
2106
|
-
return {
|
|
2107
|
-
name: "html-to-markdown",
|
|
2108
|
-
visitor: {
|
|
2109
|
-
JSXElement(path) {
|
|
2110
|
-
const { openingElement } = path.node;
|
|
2111
|
-
const tagName = t.isJSXIdentifier(openingElement.name) ? openingElement.name.name : null;
|
|
2112
|
-
if (!tagName)
|
|
2113
|
-
return;
|
|
2114
|
-
const lowerTagName = tagName.toLowerCase();
|
|
2115
|
-
if (lowerTagName === "strong" || lowerTagName === "b") {
|
|
2116
|
-
convertToMarkdown(path, "**", "**");
|
|
2117
|
-
} else if (lowerTagName === "em" || lowerTagName === "i") {
|
|
2118
|
-
convertToMarkdown(path, "*", "*");
|
|
2119
|
-
} else if (lowerTagName === "u") {
|
|
2120
|
-
convertToMarkdown(path, "__", "__");
|
|
2121
|
-
} else if (lowerTagName === "a") {
|
|
2122
|
-
convertLinkToMarkdown(path);
|
|
2123
|
-
} else if (lowerTagName === "br") {
|
|
2124
|
-
convertBrToNewline(path);
|
|
2125
|
-
} else if (lowerTagName === "p") {
|
|
2126
|
-
convertParagraphToMarkdown(path);
|
|
2127
|
-
} else if (lowerTagName === "ul") {
|
|
2128
|
-
convertUnorderedListToMarkdown(path);
|
|
2129
|
-
} else if (lowerTagName === "ol") {
|
|
2130
|
-
convertOrderedListToMarkdown(path);
|
|
2131
|
-
}
|
|
2132
|
-
}
|
|
2133
|
-
}
|
|
2134
|
-
};
|
|
2135
|
-
}
|
|
2136
|
-
function hasOnlyTextChildren(children) {
|
|
2137
|
-
return children.every(
|
|
2138
|
-
(child) => t.isJSXText(child) || t.isJSXExpressionContainer(child) && !t.isJSXElement(child.expression) || t.isJSXElement(child) && canConvertToMarkdown(child)
|
|
2139
|
-
);
|
|
2140
|
-
}
|
|
2141
|
-
function canConvertToMarkdown(element) {
|
|
2142
|
-
const { openingElement } = element;
|
|
2143
|
-
const tagName = t.isJSXIdentifier(openingElement.name) ? openingElement.name.name.toLowerCase() : null;
|
|
2144
|
-
if (!tagName)
|
|
2145
|
-
return false;
|
|
2146
|
-
const allowedTags = ["strong", "b", "em", "i", "u", "br", "p", "a", "li", "ul", "ol"];
|
|
2147
|
-
if (!allowedTags.includes(tagName))
|
|
2148
|
-
return false;
|
|
2149
|
-
if (tagName === "a") {
|
|
2150
|
-
return openingElement.attributes.length === 1 && t.isJSXAttribute(openingElement.attributes[0]) && t.isJSXIdentifier(openingElement.attributes[0].name) && openingElement.attributes[0].name.name === "href";
|
|
2151
|
-
}
|
|
2152
|
-
if (tagName === "br") {
|
|
2153
|
-
return openingElement.attributes.length === 0;
|
|
2154
|
-
}
|
|
2155
|
-
return openingElement.attributes.length === 0;
|
|
2156
|
-
}
|
|
2157
|
-
function convertToMarkdown(path, prefix, suffix) {
|
|
2158
|
-
const element = path.node;
|
|
2159
|
-
if (!canConvertToMarkdown(element))
|
|
2160
|
-
return;
|
|
2161
|
-
const children = element.children;
|
|
2162
|
-
if (!hasOnlyTextChildren(children))
|
|
2163
|
-
return;
|
|
2164
|
-
const textContent = extractTextContent(children);
|
|
2165
|
-
if (!textContent)
|
|
2166
|
-
return;
|
|
2167
|
-
const markdownText = `${prefix}${textContent}${suffix}`;
|
|
2168
|
-
path.replaceWith(t.jsxText(markdownText));
|
|
2169
|
-
}
|
|
2170
|
-
function convertLinkToMarkdown(path) {
|
|
2171
|
-
const element = path.node;
|
|
2172
|
-
if (!canConvertToMarkdown(element))
|
|
2173
|
-
return;
|
|
2174
|
-
const children = element.children;
|
|
2175
|
-
if (!hasOnlyTextChildren(children))
|
|
2176
|
-
return;
|
|
2177
|
-
const hrefAttr = element.openingElement.attributes.find(
|
|
2178
|
-
(attr) => t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && attr.name.name === "href"
|
|
2179
|
-
);
|
|
2180
|
-
if (!hrefAttr || !t.isJSXAttribute(hrefAttr))
|
|
2181
|
-
return;
|
|
2182
|
-
let href = "";
|
|
2183
|
-
if (t.isStringLiteral(hrefAttr.value)) {
|
|
2184
|
-
href = hrefAttr.value.value;
|
|
2185
|
-
} else if (t.isJSXExpressionContainer(hrefAttr.value) && t.isStringLiteral(hrefAttr.value.expression)) {
|
|
2186
|
-
href = hrefAttr.value.expression.value;
|
|
2187
|
-
} else {
|
|
2188
|
-
return;
|
|
2189
|
-
}
|
|
2190
|
-
const textContent = extractTextContent(children);
|
|
2191
|
-
if (!textContent)
|
|
2192
|
-
return;
|
|
2193
|
-
const markdownText = `[${textContent}](${href})`;
|
|
2194
|
-
path.replaceWith(t.jsxText(markdownText));
|
|
2195
|
-
}
|
|
2196
|
-
function convertBrToNewline(path) {
|
|
2197
|
-
const element = path.node;
|
|
2198
|
-
if (!canConvertToMarkdown(element))
|
|
2199
|
-
return;
|
|
2200
|
-
path.replaceWith(t.jsxText("\n"));
|
|
2201
|
-
}
|
|
2202
|
-
function convertParagraphToMarkdown(path) {
|
|
2203
|
-
const element = path.node;
|
|
2204
|
-
if (!canConvertToMarkdown(element))
|
|
2205
|
-
return;
|
|
2206
|
-
const children = element.children;
|
|
2207
|
-
if (!hasOnlyTextChildren(children))
|
|
2208
|
-
return;
|
|
2209
|
-
const textContent = extractTextContent(children);
|
|
2210
|
-
if (!textContent)
|
|
2211
|
-
return;
|
|
2212
|
-
const markdownText = `${textContent}
|
|
2213
|
-
|
|
2214
|
-
`;
|
|
2215
|
-
path.replaceWith(t.jsxText(markdownText));
|
|
2216
|
-
}
|
|
2217
|
-
function convertUnorderedListToMarkdown(path) {
|
|
2218
|
-
const element = path.node;
|
|
2219
|
-
if (element.openingElement.attributes.length > 0)
|
|
2220
|
-
return;
|
|
2221
|
-
const children = element.children;
|
|
2222
|
-
const listItems = [];
|
|
2223
|
-
for (const child of children) {
|
|
2224
|
-
if (t.isJSXElement(child)) {
|
|
2225
|
-
const tagName = t.isJSXIdentifier(child.openingElement.name) ? child.openingElement.name.name : null;
|
|
2226
|
-
if ((tagName == null ? void 0 : tagName.toLowerCase()) === "li" && canConvertToMarkdown(child)) {
|
|
2227
|
-
const itemText = extractTextContent(child.children);
|
|
2228
|
-
if (itemText) {
|
|
2229
|
-
listItems.push(`- ${itemText}`);
|
|
2230
|
-
}
|
|
2231
|
-
} else {
|
|
2232
|
-
return;
|
|
2233
|
-
}
|
|
2234
|
-
} else if (t.isJSXText(child) && child.value.trim()) {
|
|
2235
|
-
return;
|
|
2236
|
-
}
|
|
2237
|
-
}
|
|
2238
|
-
if (listItems.length === 0)
|
|
2239
|
-
return;
|
|
2240
|
-
const markdownText = listItems.join("\n") + "\n";
|
|
2241
|
-
path.replaceWith(t.jsxText(markdownText));
|
|
2242
|
-
}
|
|
2243
|
-
function convertOrderedListToMarkdown(path) {
|
|
2244
|
-
const element = path.node;
|
|
2245
|
-
if (element.openingElement.attributes.length > 0)
|
|
2246
|
-
return;
|
|
2247
|
-
const children = element.children;
|
|
2248
|
-
const listItems = [];
|
|
2249
|
-
for (const child of children) {
|
|
2250
|
-
if (t.isJSXElement(child)) {
|
|
2251
|
-
const tagName = t.isJSXIdentifier(child.openingElement.name) ? child.openingElement.name.name : null;
|
|
2252
|
-
if ((tagName == null ? void 0 : tagName.toLowerCase()) === "li" && canConvertToMarkdown(child)) {
|
|
2253
|
-
const itemText = extractTextContent(child.children);
|
|
2254
|
-
if (itemText) {
|
|
2255
|
-
listItems.push(`${listItems.length + 1}. ${itemText}`);
|
|
2256
|
-
}
|
|
2257
|
-
} else {
|
|
2258
|
-
return;
|
|
2259
|
-
}
|
|
2260
|
-
} else if (t.isJSXText(child) && child.value.trim()) {
|
|
2261
|
-
return;
|
|
2262
|
-
}
|
|
2263
|
-
}
|
|
2264
|
-
if (listItems.length === 0)
|
|
2265
|
-
return;
|
|
2266
|
-
const markdownText = listItems.join("\n") + "\n";
|
|
2267
|
-
path.replaceWith(t.jsxText(markdownText));
|
|
2268
|
-
}
|
|
2269
|
-
function extractTextContent(children) {
|
|
2270
|
-
let text = "";
|
|
2271
|
-
for (const child of children) {
|
|
2272
|
-
if (t.isJSXText(child)) {
|
|
2273
|
-
text += child.value;
|
|
2274
|
-
} else if (t.isJSXExpressionContainer(child)) {
|
|
2275
|
-
return "";
|
|
2276
|
-
} else if (t.isJSXElement(child)) {
|
|
2277
|
-
const nestedText = extractTextContent(child.children);
|
|
2278
|
-
if (!nestedText)
|
|
2279
|
-
return "";
|
|
2280
|
-
const tagName = t.isJSXIdentifier(child.openingElement.name) ? child.openingElement.name.name.toLowerCase() : null;
|
|
2281
|
-
switch (tagName) {
|
|
2282
|
-
case "strong":
|
|
2283
|
-
case "b":
|
|
2284
|
-
text += `**${nestedText}**`;
|
|
2285
|
-
break;
|
|
2286
|
-
case "em":
|
|
2287
|
-
case "i":
|
|
2288
|
-
text += `*${nestedText}*`;
|
|
2289
|
-
break;
|
|
2290
|
-
case "u":
|
|
2291
|
-
text += `__${nestedText}__`;
|
|
2292
|
-
break;
|
|
2293
|
-
case "br":
|
|
2294
|
-
text += "\n";
|
|
2295
|
-
break;
|
|
2296
|
-
default:
|
|
2297
|
-
return "";
|
|
2298
|
-
}
|
|
2299
|
-
}
|
|
2300
|
-
}
|
|
2301
|
-
return text;
|
|
2302
|
-
}
|
|
2303
|
-
|
|
2304
|
-
// src/compiler/plugins/jsx-preserve-newlines.ts
|
|
2305
|
-
import {
|
|
2306
|
-
isJSXIdentifier,
|
|
2307
|
-
jsxIdentifier,
|
|
2308
|
-
jsxText
|
|
2309
|
-
} from "@babel/types";
|
|
2310
|
-
var preserveJSXNewLines = {
|
|
2311
|
-
visitor: {
|
|
2312
|
-
JSXText(path) {
|
|
2313
|
-
const rawText = path.node.value;
|
|
2314
|
-
const modifiedText = rawText.replace(/\n/g, "\n\u200B");
|
|
2315
|
-
if (rawText !== modifiedText) {
|
|
2316
|
-
path.replaceWith(jsxText(modifiedText));
|
|
2317
|
-
path.skip();
|
|
2318
|
-
}
|
|
2319
|
-
},
|
|
2320
|
-
JSXOpeningElement(path) {
|
|
2321
|
-
if (isJSXIdentifier(path.node.name)) {
|
|
2322
|
-
path.node.name = jsxIdentifier(path.node.name.name.toLowerCase());
|
|
2323
|
-
}
|
|
2324
|
-
},
|
|
2325
|
-
JSXClosingElement(path) {
|
|
2326
|
-
if (isJSXIdentifier(path.node.name)) {
|
|
2327
|
-
path.node.name = jsxIdentifier(path.node.name.name.toLowerCase());
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
|
-
}
|
|
2331
|
-
};
|
|
2332
|
-
var postProcessing = (code) => {
|
|
2333
|
-
return code.replaceAll("\\u200B", "\\n");
|
|
2334
|
-
};
|
|
2335
|
-
var JSXNewLines = {
|
|
2336
|
-
babelPlugin: preserveJSXNewLines,
|
|
2337
|
-
postProcessing
|
|
2338
|
-
};
|
|
2339
|
-
|
|
2340
|
-
// src/compiler/plugins/jsx-undefined-vars.ts
|
|
2341
|
-
import { types as t2 } from "@babel/core";
|
|
2342
|
-
function jsxUndefinedVarsPlugin() {
|
|
2343
|
-
return {
|
|
2344
|
-
name: "jsx-undefined-vars",
|
|
2345
|
-
visitor: {
|
|
2346
|
-
JSXExpressionContainer(path) {
|
|
2347
|
-
const expression = path.node.expression;
|
|
2348
|
-
if (t2.isJSXEmptyExpression(expression)) {
|
|
2349
|
-
return;
|
|
2350
|
-
}
|
|
2351
|
-
if (!t2.isIdentifier(expression)) {
|
|
2352
|
-
return;
|
|
2353
|
-
}
|
|
2354
|
-
const varName = expression.name;
|
|
2355
|
-
const knownGlobals = /* @__PURE__ */ new Set([
|
|
2356
|
-
"undefined",
|
|
2357
|
-
"null",
|
|
2358
|
-
"true",
|
|
2359
|
-
"false",
|
|
2360
|
-
"NaN",
|
|
2361
|
-
"Infinity",
|
|
2362
|
-
"console",
|
|
2363
|
-
"Math",
|
|
2364
|
-
"JSON",
|
|
2365
|
-
"Object",
|
|
2366
|
-
"Array",
|
|
2367
|
-
"String",
|
|
2368
|
-
"Number",
|
|
2369
|
-
"Boolean",
|
|
2370
|
-
"Date",
|
|
2371
|
-
"RegExp",
|
|
2372
|
-
"Error",
|
|
2373
|
-
"Promise",
|
|
2374
|
-
"Symbol"
|
|
2375
|
-
]);
|
|
2376
|
-
if (knownGlobals.has(varName)) {
|
|
2377
|
-
return;
|
|
2378
|
-
}
|
|
2379
|
-
const iife = t2.callExpression(
|
|
2380
|
-
t2.arrowFunctionExpression(
|
|
2381
|
-
[],
|
|
2382
|
-
t2.blockStatement([
|
|
2383
|
-
t2.tryStatement(
|
|
2384
|
-
t2.blockStatement([t2.returnStatement(t2.identifier(varName))]),
|
|
2385
|
-
t2.catchClause(
|
|
2386
|
-
null,
|
|
2387
|
-
// no error binding needed
|
|
2388
|
-
t2.blockStatement([t2.returnStatement(t2.stringLiteral(varName))])
|
|
2389
|
-
)
|
|
2390
|
-
)
|
|
2391
|
-
])
|
|
2392
|
-
),
|
|
2393
|
-
[]
|
|
2394
|
-
);
|
|
2395
|
-
path.node.expression = iife;
|
|
2396
|
-
}
|
|
2397
|
-
}
|
|
2398
|
-
};
|
|
2399
|
-
}
|
|
2400
|
-
|
|
2401
1962
|
// src/compiler/plugins/line-tracking.ts
|
|
2402
1963
|
import { callExpression, expressionStatement, identifier, numericLiteral } from "@babel/types";
|
|
2403
1964
|
var LineTrackingFnIdentifier = "__track__";
|
|
2404
|
-
var lineTrackingBabelPlugin = function({ types:
|
|
1965
|
+
var lineTrackingBabelPlugin = function({ types: t }) {
|
|
2405
1966
|
return {
|
|
2406
1967
|
visitor: {
|
|
2407
1968
|
Program(path) {
|
|
@@ -2422,8 +1983,8 @@ var lineTrackingBabelPlugin = function({ types: t3 }) {
|
|
|
2422
1983
|
console.error(e);
|
|
2423
1984
|
}
|
|
2424
1985
|
};
|
|
2425
|
-
if (
|
|
2426
|
-
if (!trackedLines.has(startLine) && !
|
|
1986
|
+
if (t.isFunctionDeclaration(node) || t.isClassMethod(node) || t.isReturnStatement(node) || t.isAwaitExpression(node) || t.isForOfStatement(node) || t.isCallExpression(node) || t.isUnaryExpression(node) || t.isVariableDeclaration(node) || t.isBlockStatement(path2.parent) || t.isAssignmentExpression(node)) {
|
|
1987
|
+
if (!trackedLines.has(startLine) && !t.isArrowFunctionExpression(path2.parent) && !t.isObjectProperty(path2.parent) && !t.isAwaitExpression(path2.parent) && !t.isUnaryExpression(path2.parent) && !t.isReturnStatement(path2.parent) && !t.isForOfStatement(path2.parent) && !t.isForStatement(path2.parent) && !t.isForInStatement(path2.parent) && !t.isVariableDeclaration(path2.parent) && !t.isBinaryExpression(path2.parent) && !t.isLogicalExpression(path2.parent)) {
|
|
2427
1988
|
track();
|
|
2428
1989
|
}
|
|
2429
1990
|
}
|
|
@@ -2438,7 +1999,7 @@ var lineTrackingBabelPlugin = function({ types: t3 }) {
|
|
|
2438
1999
|
// src/compiler/plugins/replace-comment.ts
|
|
2439
2000
|
var CommentFnIdentifier = "__comment__";
|
|
2440
2001
|
var replaceCommentBabelPlugin = function({
|
|
2441
|
-
types:
|
|
2002
|
+
types: t
|
|
2442
2003
|
}) {
|
|
2443
2004
|
return {
|
|
2444
2005
|
visitor: {
|
|
@@ -2457,13 +2018,13 @@ var replaceCommentBabelPlugin = function({
|
|
|
2457
2018
|
return;
|
|
2458
2019
|
}
|
|
2459
2020
|
processed.add(comment.loc);
|
|
2460
|
-
const commentCall =
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
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)
|
|
2464
2025
|
])
|
|
2465
2026
|
);
|
|
2466
|
-
const isInsideObjectProperty =
|
|
2027
|
+
const isInsideObjectProperty = t.isObjectProperty(node) || path2.findParent((path3) => t.isObjectProperty(path3.node));
|
|
2467
2028
|
if (!isInsideObjectProperty) {
|
|
2468
2029
|
insertMethod(commentCall);
|
|
2469
2030
|
}
|
|
@@ -2482,7 +2043,7 @@ var replaceCommentBabelPlugin = function({
|
|
|
2482
2043
|
|
|
2483
2044
|
// src/compiler/plugins/return-async.ts
|
|
2484
2045
|
var instrumentLastLinePlugin = function({
|
|
2485
|
-
types:
|
|
2046
|
+
types: t
|
|
2486
2047
|
}) {
|
|
2487
2048
|
return {
|
|
2488
2049
|
visitor: {
|
|
@@ -2496,14 +2057,14 @@ var instrumentLastLinePlugin = function({
|
|
|
2496
2057
|
return;
|
|
2497
2058
|
}
|
|
2498
2059
|
const lastStatement = statements[statements.length - 1];
|
|
2499
|
-
if (
|
|
2500
|
-
if (
|
|
2501
|
-
lastStatement.argument =
|
|
2060
|
+
if (t.isReturnStatement(lastStatement)) {
|
|
2061
|
+
if (t.isExpression(lastStatement.argument)) {
|
|
2062
|
+
lastStatement.argument = t.awaitExpression(lastStatement.argument);
|
|
2502
2063
|
}
|
|
2503
2064
|
return;
|
|
2504
2065
|
}
|
|
2505
|
-
if (
|
|
2506
|
-
const returnStatement3 =
|
|
2066
|
+
if (t.isExpressionStatement(lastStatement) && (t.isCallExpression(lastStatement.expression) || t.isAwaitExpression(lastStatement.expression))) {
|
|
2067
|
+
const returnStatement3 = t.returnStatement(t.awaitExpression(lastStatement.expression));
|
|
2507
2068
|
(_b = path.get("body").get("body")[statements.length - 1]) == null ? void 0 : _b.replaceWith(returnStatement3);
|
|
2508
2069
|
}
|
|
2509
2070
|
}
|
|
@@ -2721,7 +2282,7 @@ import {
|
|
|
2721
2282
|
returnStatement as returnStatement2
|
|
2722
2283
|
} from "@babel/types";
|
|
2723
2284
|
var VariableTrackingFnIdentifier = "__var__";
|
|
2724
|
-
var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function({ types:
|
|
2285
|
+
var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function({ types: t }) {
|
|
2725
2286
|
let trackingStatements = [];
|
|
2726
2287
|
const trackVariable = (variableName) => {
|
|
2727
2288
|
if (variableName.startsWith("__")) {
|
|
@@ -2741,19 +2302,19 @@ var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function
|
|
|
2741
2302
|
visitor: {
|
|
2742
2303
|
FunctionDeclaration(path) {
|
|
2743
2304
|
path.node.params.forEach((param) => {
|
|
2744
|
-
if (
|
|
2305
|
+
if (t.isIdentifier(param)) {
|
|
2745
2306
|
trackVariable(param.name);
|
|
2746
|
-
} else if (
|
|
2307
|
+
} else if (t.isAssignmentPattern(param) && t.isIdentifier(param.left)) {
|
|
2747
2308
|
trackVariable(param.left.name);
|
|
2748
|
-
} else if (
|
|
2309
|
+
} else if (t.isObjectPattern(param)) {
|
|
2749
2310
|
param.properties.forEach((prop) => {
|
|
2750
|
-
if (
|
|
2311
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2751
2312
|
trackVariable(prop.value.name);
|
|
2752
2313
|
}
|
|
2753
2314
|
});
|
|
2754
|
-
} else if (
|
|
2315
|
+
} else if (t.isArrayPattern(param)) {
|
|
2755
2316
|
param.elements.forEach((element) => {
|
|
2756
|
-
if (
|
|
2317
|
+
if (t.isIdentifier(element)) {
|
|
2757
2318
|
trackVariable(element.name);
|
|
2758
2319
|
}
|
|
2759
2320
|
});
|
|
@@ -2766,19 +2327,19 @@ var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function
|
|
|
2766
2327
|
},
|
|
2767
2328
|
ArrowFunctionExpression(path) {
|
|
2768
2329
|
path.node.params.forEach((param) => {
|
|
2769
|
-
if (
|
|
2330
|
+
if (t.isIdentifier(param)) {
|
|
2770
2331
|
trackVariable(param.name);
|
|
2771
|
-
} else if (
|
|
2332
|
+
} else if (t.isAssignmentPattern(param) && t.isIdentifier(param.left)) {
|
|
2772
2333
|
trackVariable(param.left.name);
|
|
2773
|
-
} else if (
|
|
2334
|
+
} else if (t.isObjectPattern(param)) {
|
|
2774
2335
|
param.properties.forEach((prop) => {
|
|
2775
|
-
if (
|
|
2336
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2776
2337
|
trackVariable(prop.value.name);
|
|
2777
2338
|
}
|
|
2778
2339
|
});
|
|
2779
|
-
} else if (
|
|
2340
|
+
} else if (t.isArrayPattern(param)) {
|
|
2780
2341
|
param.elements.forEach((element) => {
|
|
2781
|
-
if (
|
|
2342
|
+
if (t.isIdentifier(element)) {
|
|
2782
2343
|
trackVariable(element.name);
|
|
2783
2344
|
}
|
|
2784
2345
|
});
|
|
@@ -2799,11 +2360,11 @@ var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function
|
|
|
2799
2360
|
const parent = path.parentPath;
|
|
2800
2361
|
const handleObjectPattern = (pattern) => {
|
|
2801
2362
|
pattern.properties.forEach((prop) => {
|
|
2802
|
-
if (
|
|
2363
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
2803
2364
|
trackVariable(prop.value.name);
|
|
2804
|
-
} else if (
|
|
2365
|
+
} else if (t.isObjectProperty(prop) && t.isObjectPattern(prop.value)) {
|
|
2805
2366
|
handleObjectPattern(prop.value);
|
|
2806
|
-
} else if (
|
|
2367
|
+
} else if (t.isObjectProperty(prop) && t.isAssignmentPattern(prop.value) && t.isIdentifier(prop.value.left)) {
|
|
2807
2368
|
trackVariable(prop.value.left.name);
|
|
2808
2369
|
}
|
|
2809
2370
|
});
|
|
@@ -2812,19 +2373,19 @@ var variableTrackingPlugin = (variables = /* @__PURE__ */ new Set()) => function
|
|
|
2812
2373
|
if (parent.isForXStatement() || parent.isForStatement()) {
|
|
2813
2374
|
return;
|
|
2814
2375
|
}
|
|
2815
|
-
if (
|
|
2376
|
+
if (t.isIdentifier(declarator.id)) {
|
|
2816
2377
|
trackVariable(declarator.id.name);
|
|
2817
2378
|
}
|
|
2818
|
-
if (
|
|
2379
|
+
if (t.isObjectPattern(declarator.id)) {
|
|
2819
2380
|
handleObjectPattern(declarator.id);
|
|
2820
2381
|
}
|
|
2821
|
-
if (
|
|
2382
|
+
if (t.isArrayPattern(declarator.id)) {
|
|
2822
2383
|
declarator.id.elements.forEach((element) => {
|
|
2823
|
-
if (
|
|
2384
|
+
if (t.isIdentifier(element)) {
|
|
2824
2385
|
trackVariable(element.name);
|
|
2825
|
-
} else if (
|
|
2386
|
+
} else if (t.isRestElement(element) && t.isIdentifier(element.argument)) {
|
|
2826
2387
|
trackVariable(element.argument.name);
|
|
2827
|
-
} else if (
|
|
2388
|
+
} else if (t.isAssignmentPattern(element) && t.isIdentifier(element.left)) {
|
|
2828
2389
|
trackVariable(element.left.name);
|
|
2829
2390
|
}
|
|
2830
2391
|
});
|
|
@@ -2853,8 +2414,6 @@ var DEFAULT_TRANSFORM_OPTIONS = {
|
|
|
2853
2414
|
retainLines: true
|
|
2854
2415
|
};
|
|
2855
2416
|
var Identifiers = {
|
|
2856
|
-
JSXFnIdentifier: "__jsx__",
|
|
2857
|
-
AsyncIterYieldFnIdentifier: "__async_iter_yield__",
|
|
2858
2417
|
ConsoleObjIdentifier: "console",
|
|
2859
2418
|
LineTrackingFnIdentifier,
|
|
2860
2419
|
VariableTrackingFnIdentifier,
|
|
@@ -2862,32 +2421,43 @@ var Identifiers = {
|
|
|
2862
2421
|
ToolTrackerRetIdentifier,
|
|
2863
2422
|
CommentFnIdentifier
|
|
2864
2423
|
};
|
|
2424
|
+
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
|
+
try {
|
|
2436
|
+
Babel.transform(code, {
|
|
2437
|
+
parserOpts: {
|
|
2438
|
+
allowReturnOutsideFunction: true,
|
|
2439
|
+
allowAwaitOutsideFunction: true
|
|
2440
|
+
},
|
|
2441
|
+
presets: ["typescript"],
|
|
2442
|
+
plugins: [detectTopLevelReturnPlugin],
|
|
2443
|
+
filename: "<anonymous>.ts",
|
|
2444
|
+
code: false
|
|
2445
|
+
});
|
|
2446
|
+
} catch {
|
|
2447
|
+
return /\breturn\b/.test(code);
|
|
2448
|
+
}
|
|
2449
|
+
return found;
|
|
2450
|
+
}
|
|
2865
2451
|
function compile(code) {
|
|
2866
|
-
code =
|
|
2867
|
-
code = JSXMarkdown.preProcessing(code);
|
|
2452
|
+
code = AsyncWrapper.preProcessing(code);
|
|
2868
2453
|
let output = Babel.transform(code, {
|
|
2869
2454
|
parserOpts: {
|
|
2870
2455
|
allowReturnOutsideFunction: true,
|
|
2871
2456
|
allowAwaitOutsideFunction: true
|
|
2872
2457
|
},
|
|
2873
2458
|
presets: ["typescript"],
|
|
2874
|
-
plugins: [
|
|
2875
|
-
|
|
2876
|
-
htmlToMarkdownPlugin,
|
|
2877
|
-
// Convert simple HTML to markdown first
|
|
2878
|
-
jsxUndefinedVarsPlugin,
|
|
2879
|
-
// Must run BEFORE JSX transform
|
|
2880
|
-
[
|
|
2881
|
-
jsxPlugin,
|
|
2882
|
-
{
|
|
2883
|
-
throwIfNamespace: false,
|
|
2884
|
-
runtime: "classic",
|
|
2885
|
-
pragma: Identifiers.JSXFnIdentifier
|
|
2886
|
-
}
|
|
2887
|
-
],
|
|
2888
|
-
instrumentLastLinePlugin
|
|
2889
|
-
],
|
|
2890
|
-
filename: "<anonymous>.tsx",
|
|
2459
|
+
plugins: [instrumentLastLinePlugin],
|
|
2460
|
+
filename: "<anonymous>.ts",
|
|
2891
2461
|
sourceFileName: "<anonymous>",
|
|
2892
2462
|
sourceMaps: true,
|
|
2893
2463
|
minified: false,
|
|
@@ -2910,8 +2480,7 @@ function compile(code) {
|
|
|
2910
2480
|
retainLines: true
|
|
2911
2481
|
});
|
|
2912
2482
|
let outputCode = output.code;
|
|
2913
|
-
outputCode =
|
|
2914
|
-
outputCode = JSXNewLines.postProcessing(outputCode);
|
|
2483
|
+
outputCode = AsyncWrapper.postProcessing(outputCode);
|
|
2915
2484
|
return {
|
|
2916
2485
|
code: outputCode,
|
|
2917
2486
|
codeWithMarkers,
|
|
@@ -2946,7 +2515,7 @@ var handleErrorQuickJS = (err, code, _consumer, traces, variables, lines_execute
|
|
|
2946
2515
|
const stackTrace = err.stack || "";
|
|
2947
2516
|
const LINE_OFFSET = 1;
|
|
2948
2517
|
const regex = /<quickjs>:(\d+)/g;
|
|
2949
|
-
const QUICKJS_WRAPPER_OFFSET =
|
|
2518
|
+
const QUICKJS_WRAPPER_OFFSET = 9;
|
|
2950
2519
|
const matches = Array.from(stackTrace.matchAll(regex)).map((x) => {
|
|
2951
2520
|
const quickjsLine = Number(x[1]);
|
|
2952
2521
|
const transformedCodeLine = quickjsLine - QUICKJS_WRAPPER_OFFSET;
|
|
@@ -3091,7 +2660,6 @@ var NO_TRACKING = [
|
|
|
3091
2660
|
Identifiers.ToolCallTrackerFnIdentifier,
|
|
3092
2661
|
Identifiers.ToolTrackerRetIdentifier,
|
|
3093
2662
|
Identifiers.VariableTrackingFnIdentifier,
|
|
3094
|
-
Identifiers.JSXFnIdentifier,
|
|
3095
2663
|
Identifiers.ConsoleObjIdentifier
|
|
3096
2664
|
];
|
|
3097
2665
|
function instrumentContext(context, transformed, traces, variables, lines_executed, consumer, userCodeStartLine) {
|
|
@@ -3108,7 +2676,6 @@ function instrumentContext(context, transformed, traces, variables, lines_execut
|
|
|
3108
2676
|
const userCodeLine = Math.max(1, mappedLine - userCodeStartLine);
|
|
3109
2677
|
lines_executed.set(userCodeLine, (lines_executed.get(userCodeLine) ?? 0) + 1);
|
|
3110
2678
|
};
|
|
3111
|
-
context[Identifiers.JSXFnIdentifier] = (tool, props, ...children) => createJsxComponent({ type: tool, props, children });
|
|
3112
2679
|
context[Identifiers.VariableTrackingFnIdentifier] = (name, getter) => {
|
|
3113
2680
|
if (NO_TRACKING.includes(name)) {
|
|
3114
2681
|
return;
|
|
@@ -3141,21 +2708,6 @@ function instrumentContext(context, transformed, traces, variables, lines_execut
|
|
|
3141
2708
|
traces.push({ type: "log", message, args, started_at: Date.now() });
|
|
3142
2709
|
}
|
|
3143
2710
|
};
|
|
3144
|
-
context[Identifiers.AsyncIterYieldFnIdentifier] = async function(value) {
|
|
3145
|
-
const startedAt = Date.now();
|
|
3146
|
-
try {
|
|
3147
|
-
if (typeof value.type !== "string" || value.type.trim().length === 0) {
|
|
3148
|
-
throw new Error("A yield statement must yield a valid tool");
|
|
3149
|
-
}
|
|
3150
|
-
const toolName = Object.keys(context).find((x) => x.toUpperCase() === value.type.toUpperCase());
|
|
3151
|
-
if (!toolName) {
|
|
3152
|
-
throw new Error(`Yield tool "${value.type}", but tool is not found`);
|
|
3153
|
-
}
|
|
3154
|
-
await context[toolName](value);
|
|
3155
|
-
} finally {
|
|
3156
|
-
traces.push({ type: "yield", value, started_at: startedAt, ended_at: Date.now() });
|
|
3157
|
-
}
|
|
3158
|
-
};
|
|
3159
2711
|
return state;
|
|
3160
2712
|
}
|
|
3161
2713
|
function findUserCodeStartLine(transformed) {
|
|
@@ -3175,7 +2727,7 @@ var NodeDriver = class {
|
|
|
3175
2727
|
async execute(ctx) {
|
|
3176
2728
|
const { transformed, consumer, context, traces, code, lines_executed, variables } = ctx;
|
|
3177
2729
|
const state = instrumentContext(context, transformed, traces, variables, lines_executed, consumer, 0);
|
|
3178
|
-
const AsyncFunction = async function
|
|
2730
|
+
const AsyncFunction = async function() {
|
|
3179
2731
|
}.constructor;
|
|
3180
2732
|
return await (async () => {
|
|
3181
2733
|
const descriptors = Object.getOwnPropertyDescriptors(context);
|
|
@@ -3192,14 +2744,7 @@ var NodeDriver = class {
|
|
|
3192
2744
|
const assigner = `let __${Identifiers.LineTrackingFnIdentifier} = ${Identifiers.LineTrackingFnIdentifier}; ${Identifiers.LineTrackingFnIdentifier} = function(line) { ${reportAll}; __${Identifiers.LineTrackingFnIdentifier}(line);}`;
|
|
3193
2745
|
const wrapper = `"use strict"; try { ${assigner};${transformed.code} } finally { ${reportAll} };`;
|
|
3194
2746
|
const fn = AsyncFunction(...Object.keys(context), wrapper);
|
|
3195
|
-
|
|
3196
|
-
do {
|
|
3197
|
-
const { value, done } = await res.next();
|
|
3198
|
-
if (done) {
|
|
3199
|
-
return value;
|
|
3200
|
-
}
|
|
3201
|
-
await context[Identifiers.AsyncIterYieldFnIdentifier](value);
|
|
3202
|
-
} while (true);
|
|
2747
|
+
return await fn(...Object.values(context));
|
|
3203
2748
|
})().then((res) => {
|
|
3204
2749
|
res = Signals.maybeDeserializeError(res);
|
|
3205
2750
|
return {
|
|
@@ -3218,6 +2763,8 @@ import {
|
|
|
3218
2763
|
newQuickJSWASMModuleFromVariant,
|
|
3219
2764
|
shouldInterruptAfterDeadline
|
|
3220
2765
|
} from "quickjs-emscripten-core";
|
|
2766
|
+
var _quickJSModule;
|
|
2767
|
+
var loadQuickJSModule = () => _quickJSModule ??= newQuickJSWASMModuleFromVariant(BundledReleaseSyncVariant);
|
|
3221
2768
|
var QuickJSDriver = class {
|
|
3222
2769
|
async execute(ctx) {
|
|
3223
2770
|
const { transformed, consumer, context, traces, signal, timeout, code, lines_executed, variables } = ctx;
|
|
@@ -3231,7 +2778,7 @@ var QuickJSDriver = class {
|
|
|
3231
2778
|
consumer,
|
|
3232
2779
|
userCodeStartLine
|
|
3233
2780
|
);
|
|
3234
|
-
const QuickJS = await
|
|
2781
|
+
const QuickJS = await loadQuickJSModule();
|
|
3235
2782
|
const runtime = QuickJS.newRuntime();
|
|
3236
2783
|
runtime.setMemoryLimit(128 * 1024 * 1024);
|
|
3237
2784
|
const startTime = Date.now();
|
|
@@ -3694,35 +3241,15 @@ globalThis.__llmz_result = undefined;
|
|
|
3694
3241
|
globalThis.__llmz_result_set = false;
|
|
3695
3242
|
globalThis.__llmz_error = null;
|
|
3696
3243
|
globalThis.__llmz_error_stack = null;
|
|
3697
|
-
globalThis.__llmz_yields = [];
|
|
3698
3244
|
|
|
3699
3245
|
(async () => {
|
|
3700
3246
|
try {
|
|
3701
|
-
async function
|
|
3247
|
+
async function __fn__() {
|
|
3702
3248
|
${transformedCode}
|
|
3703
3249
|
}
|
|
3704
3250
|
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
const maxIterations = 10000;
|
|
3708
|
-
|
|
3709
|
-
while (iteration < maxIterations) {
|
|
3710
|
-
const { value, done } = await fn.next();
|
|
3711
|
-
|
|
3712
|
-
if (done) {
|
|
3713
|
-
globalThis.__llmz_result = value;
|
|
3714
|
-
globalThis.__llmz_result_set = true;
|
|
3715
|
-
break;
|
|
3716
|
-
}
|
|
3717
|
-
|
|
3718
|
-
globalThis.__llmz_yields.push(value);
|
|
3719
|
-
await ${Identifiers.AsyncIterYieldFnIdentifier}(value);
|
|
3720
|
-
iteration++;
|
|
3721
|
-
}
|
|
3722
|
-
|
|
3723
|
-
if (iteration >= maxIterations) {
|
|
3724
|
-
throw new Error('Maximum iterations exceeded');
|
|
3725
|
-
}
|
|
3251
|
+
globalThis.__llmz_result = await __fn__();
|
|
3252
|
+
globalThis.__llmz_result_set = true;
|
|
3726
3253
|
} catch (err) {
|
|
3727
3254
|
globalThis.__llmz_error = typeof err === 'string' ? err : String(err.message || err || '');
|
|
3728
3255
|
globalThis.__llmz_error_stack = '' + (err.stack || '');
|
|
@@ -3733,8 +3260,17 @@ ${transformedCode}
|
|
|
3733
3260
|
|
|
3734
3261
|
// src/vm/index.ts
|
|
3735
3262
|
var MAX_VM_EXECUTION_TIME = 6e4;
|
|
3736
|
-
|
|
3263
|
+
var useQuickJS = () => {
|
|
3737
3264
|
var _a;
|
|
3265
|
+
return typeof process === "undefined" || ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.USE_QUICKJS) !== "false";
|
|
3266
|
+
};
|
|
3267
|
+
var warmupVM = () => {
|
|
3268
|
+
if (useQuickJS()) {
|
|
3269
|
+
loadQuickJSModule().catch(() => {
|
|
3270
|
+
});
|
|
3271
|
+
}
|
|
3272
|
+
};
|
|
3273
|
+
async function runAsyncFunction(context, code, traces = [], signal = null, timeout = MAX_VM_EXECUTION_TIME) {
|
|
3738
3274
|
const transformed = (() => {
|
|
3739
3275
|
try {
|
|
3740
3276
|
return compile(code);
|
|
@@ -3764,8 +3300,7 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
3764
3300
|
delete context[name];
|
|
3765
3301
|
}
|
|
3766
3302
|
let driver;
|
|
3767
|
-
|
|
3768
|
-
if (useQuickJS) {
|
|
3303
|
+
if (useQuickJS()) {
|
|
3769
3304
|
try {
|
|
3770
3305
|
driver = new QuickJSDriver();
|
|
3771
3306
|
return await driver.execute({
|
|
@@ -3812,5 +3347,7 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
3812
3347
|
}
|
|
3813
3348
|
|
|
3814
3349
|
export {
|
|
3350
|
+
hasTopLevelReturn,
|
|
3351
|
+
warmupVM,
|
|
3815
3352
|
runAsyncFunction
|
|
3816
3353
|
};
|