Skip to content

Commit

Permalink
fix: Temporary blocking of vm2 of js interpreter
Browse files Browse the repository at this point in the history
close #12
  • Loading branch information
JohannLai committed Oct 9, 2023
1 parent 71221e6 commit 4895ea4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 67 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"openai": "^3.3.0",
"vm2": "^3.9.19",
"zod": "^3.21.4",
"zod-to-json-schema": "^3.21.2"
},
Expand Down
16 changes: 3 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 29 additions & 29 deletions tools/javaScriptInterpreter.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { createJavaScriptInterpreter } from "./javaScriptInterpreter";
import { expect, it } from 'vitest';
// import { createJavaScriptInterpreter } from "./javaScriptInterpreter";
// import { expect, it } from 'vitest';

it('should interpret JavaScript code correctly', () => {
const [javaScriptInterpreter] = createJavaScriptInterpreter();
const result = javaScriptInterpreter({
code: `
1 + 1
`,
});
expect(result).toBe(2);
});
// it('should interpret JavaScript code correctly', () => {
// const [javaScriptInterpreter] = createJavaScriptInterpreter();
// const result = javaScriptInterpreter({
// code: `
// 1 + 1
// `,
// });
// expect(result).toBe(2);
// });

it('should timeout for long running scripts', () => {
const [javaScriptInterpreter] = createJavaScriptInterpreter();
const result = javaScriptInterpreter({
code: `
while (true) {}
`,
});
// it('should timeout for long running scripts', () => {
// const [javaScriptInterpreter] = createJavaScriptInterpreter();
// const result = javaScriptInterpreter({
// code: `
// while (true) {}
// `,
// });

expect(result).toBe("Failed to execute script: Script execution timed out after 5000ms")
});
// expect(result).toBe("Failed to execute script: Script execution timed out after 5000ms")
// });

it('should not have access to Node.js environment', () => {
const [javaScriptInterpreter] = createJavaScriptInterpreter();
const result = javaScriptInterpreter({
code: `
process.exit(1)
`,
});
// it('should not have access to Node.js environment', () => {
// const [javaScriptInterpreter] = createJavaScriptInterpreter();
// const result = javaScriptInterpreter({
// code: `
// process.exit(1)
// `,
// });

expect(result).toBe("Failed to execute script: process is not defined")
});
// expect(result).toBe("Failed to execute script: process is not defined")
// });
48 changes: 24 additions & 24 deletions tools/javaScriptInterpreter.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { z } from "zod";
import { VM } from "vm2";
import { Tool } from "./tool";
// import { z } from "zod";
// import { VM } from "vm2";
// import { Tool } from "./tool";

function createJavaScriptInterpreter() {
const paramsSchema = z.object({
code: z.string(),
});
const name = "javaScriptInterpreter";
const description = "Useful for running JavaScript code in sandbox. Input is a string of JavaScript code, output is the result of the code.";
// function createJavaScriptInterpreter() {
// const paramsSchema = z.object({
// code: z.string(),
// });
// const name = "javaScriptInterpreter";
// const description = "Useful for running JavaScript code in sandbox. Input is a string of JavaScript code, output is the result of the code.";

const execute = (params: z.infer<typeof paramsSchema>) => {
const { code } = params;
const vm = new VM({
timeout: 5000,
sandbox: {},
});
try {
return vm.run(code);
} catch (error) {
return `Failed to execute script: ${error.message}`;
}
};
// const execute = (params: z.infer<typeof paramsSchema>) => {
// const { code } = params;
// const vm = new VM({
// timeout: 5000,
// sandbox: {},
// });
// try {
// return vm.run(code);
// } catch (error) {
// return `Failed to execute script: ${error.message}`;
// }
// };

return new Tool<typeof paramsSchema, z.ZodType<any, any>>(paramsSchema, name, description, execute).tool;
}
// return new Tool<typeof paramsSchema, z.ZodType<any, any>>(paramsSchema, name, description, execute).tool;
// }

export { createJavaScriptInterpreter };
// export { createJavaScriptInterpreter };

0 comments on commit 4895ea4

Please sign in to comment.