@tanstack/ai-code-mode
Version:
Secure TypeScript Code Mode for TanStack AI agents to execute sandboxed tool orchestration programs.
50 lines (49 loc) • 1.66 kB
JavaScript
import { transform } from "sucrase";
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 = transform(wrappedCode, {
// Only strip/lower TypeScript-specific syntax...
transforms: ["typescript"],
// ...and leave modern ECMAScript syntax untouched for the sandbox engines.
disableESTransforms: true
});
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