Skip to content

Commit

Permalink
update nodejs interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneeHertz committed Sep 10, 2023
1 parent 558978e commit 9e4ded9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let { config: { useProxy, proxyObject, AI_NAME, writeFolder, allowPowerfulInterp
const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}`

const { sliceStringbyTokenLength } = require('./tiktoken.js')
const { javaScriptInterpreterPowerful } = require('./vm.js')
const { nodejsInterpreter } = require('./vm.js')

let STORE_PATH = path.join(process.cwd(), 'data')
if (!fs.existsSync(STORE_PATH)) {
Expand Down Expand Up @@ -136,11 +136,11 @@ if (allowPowerfulInterpreter) {
let findExistInterpreter = functionInfo.findIndex(f => f.name === 'javaScriptInterpreter')
if (findExistInterpreter !== -1) {
functionInfo.splice(findExistInterpreter, 1, {
"name": "javaScriptInterpreterPowerful",
"name": "nodejsInterpreter",
"description": `Useful for running JavaScript code in node.js VM.
Input is a string of JavaScript code, output is the result of the code.
You can require node modules except fs, and use lodash, axios directly.
The context of the VM will be preserved, you can store global variables for future use.`,
The context of the VM will be preserved, you can only store variables in the "global" object for future use.`,
"parameters": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -179,7 +179,7 @@ const functionAction = {
})
return `${AI_NAME}运行了\n\`\`\`javascript\n${code}\n\`\`\``
},
javaScriptInterpreterPowerful ({ code }) {
nodejsInterpreter ({ code }) {
code = beautify(code, {
indent_size: 2,
space_after_anon_function: true,
Expand Down Expand Up @@ -267,7 +267,7 @@ module.exports = {
writeFileToDisk,
readFileFromDisk,
javaScriptInterpreter,
javaScriptInterpreterPowerful,
nodejsInterpreter,
openLocalFileOrWebpage
}
}
11 changes: 7 additions & 4 deletions modules/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ const vm = require('node:vm')
const axios = require('axios')
const _ = require('lodash')


const env = {
axios,
_,
lodash: _,
require
require,
Buffer,
global: {}
}

const context = new vm.createContext(env)

const javaScriptInterpreterPowerful = ({ code }) => {
const nodejsInterpreter = ({ code }) => {
try {
const script = new vm.Script(code)
const script = new vm.Script(`{${code}}`)
let result = script.runInContext(context)
return JSON.stringify(result)
} catch (error) {
Expand All @@ -22,5 +25,5 @@ const javaScriptInterpreterPowerful = ({ code }) => {
}

module.exports = {
javaScriptInterpreterPowerful
nodejsInterpreter
}

0 comments on commit 9e4ded9

Please sign in to comment.