UNPKG

@tanstack/ai-code-mode

Version:

Code Mode for TanStack AI - LLM-driven code execution in secure sandboxes

53 lines (52 loc) 1.76 kB
import { transform } from "esbuild"; const WRAPPER_START = "___TANSTACK_WRAPPER_START___"; const WRAPPER_END = "___TANSTACK_WRAPPER_END___"; async function stripTypeScript(code) { const wrappedCode = `async function ${WRAPPER_START}() { ${code} }; ${WRAPPER_END}`; const result = await transform(wrappedCode, { loader: "ts", // Don't minify - keep the code readable for debugging minify: false, // Don't use keepNames as it adds __name() helper calls that aren't available in the sandbox keepNames: false, // Target modern JavaScript (ES2022 has top-level await) target: "es2022" }); const transformed = result.code; const functionStart = transformed.indexOf(`async function ${WRAPPER_START}()`); if (functionStart === -1) { throw new Error( "[stripTypeScript] Could not find wrapper function start in transformed output" ); } const openBrace = transformed.indexOf("{", functionStart); if (openBrace === -1) { throw new Error( "[stripTypeScript] Could not find opening brace in transformed output" ); } const endMarkerIndex = transformed.indexOf(WRAPPER_END); if (endMarkerIndex === -1) { throw new Error( "[stripTypeScript] Could not find end marker in transformed output" ); } const codeBeforeEndMarker = transformed.substring( openBrace + 1, endMarkerIndex ); const closingBraceIndex = codeBeforeEndMarker.lastIndexOf("}"); if (closingBraceIndex === -1) { throw new Error( "[stripTypeScript] Could not find closing brace in transformed output" ); } const functionBody = codeBeforeEndMarker.substring(0, closingBraceIndex).trim(); return functionBody; } export { stripTypeScript }; //# sourceMappingURL=strip-typescript.js.map