Skip to content

Commit

Permalink
feat: improve execute js/py code and collect results (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 23, 2024
1 parent d499954 commit fbeaa9c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
6 changes: 2 additions & 4 deletions scripts/run-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ async function loadEnv(filePath) {

if (!process.env[envName]) {
let envValue = valueParts.join("=").trim();
if (envValue.startsWith('"') && envValue.endsWith('"')) {
envValue = envValue.slice(1, -1);
} else if (envValue.startsWith("'") && envValue.endsWith("'")) {
if ((envValue.startsWith('"') && envValue.endsWith('"')) || (envValue.startsWith("'") && envValue.endsWith("'"))) {
envValue = envValue.slice(1, -1);
}
envVars.set(envName, envValue);
Expand Down Expand Up @@ -129,7 +127,7 @@ function returnToLLM(value) {
}
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
writer.write(value);
writer.write(value.toString());
} else if (type === "object") {
const proto = Object.prototype.toString.call(value);
if (proto === "[object Object]" || proto === "[object Array]") {
Expand Down
6 changes: 2 additions & 4 deletions scripts/run-agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def load_env(file_path):

if env_name not in os.environ:
env_value = "=".join(value_parts).strip()
if env_value.startswith('"') and env_value.endswith('"'):
env_value = env_value[1:-1]
elif env_value.startswith("'") and env_value.endswith("'"):
if (env_value.startswith('"') and env_value.endswith('"')) or (env_value.startswith("'") and env_value.endswith("'")):
env_value = env_value[1:-1]
env_vars[env_name] = env_value

Expand Down Expand Up @@ -150,7 +148,7 @@ def return_to_llm(value):

value_type = type(value).__name__
if value_type in ("str", "int", "float", "bool"):
writer.write(value)
writer.write(str(value))
elif value_type == "dict" or value_type == "list":
value_str = json.dumps(value, indent=2)
assert value == json.loads(value_str)
Expand Down
6 changes: 2 additions & 4 deletions scripts/run-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ async function loadEnv(filePath) {

if (!process.env[envName]) {
let envValue = valueParts.join("=").trim();
if (envValue.startsWith('"') && envValue.endsWith('"')) {
envValue = envValue.slice(1, -1);
} else if (envValue.startsWith("'") && envValue.endsWith("'")) {
if ((envValue.startsWith('"') && envValue.endsWith('"')) || (envValue.startsWith("'") && envValue.endsWith("'"))) {
envValue = envValue.slice(1, -1);
}
envVars.set(envName, envValue);
Expand Down Expand Up @@ -116,7 +114,7 @@ function returnToLLM(value) {
}
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
writer.write(value);
writer.write(value.toString());
} else if (type === "object") {
const proto = Object.prototype.toString.call(value);
if (proto === "[object Object]" || proto === "[object Array]") {
Expand Down
6 changes: 2 additions & 4 deletions scripts/run-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def load_env(file_path):

if env_name not in os.environ:
env_value = "=".join(value_parts).strip()
if env_value.startswith('"') and env_value.endswith('"'):
env_value = env_value[1:-1]
elif env_value.startswith("'") and env_value.endswith("'"):
if (env_value.startswith('"') and env_value.endswith('"')) or (env_value.startswith("'") and env_value.endswith("'")):
env_value = env_value[1:-1]
env_vars[env_name] = env_value

Expand Down Expand Up @@ -110,7 +108,7 @@ def return_to_llm(value):

value_type = type(value).__name__
if value_type in ("str", "int", "float", "bool"):
writer.write(value)
writer.write(str(value))
elif value_type == "dict" or value_type == "list":
value_str = json.dumps(value, indent=2)
assert value == json.loads(value_str)
Expand Down
6 changes: 5 additions & 1 deletion tools/execute_js_code.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const vm = require('vm');

/**
* Execute the javascript code in node.js.
* @typedef {Object} Args
* @property {string} code - Javascript code to execute, such as `console.log("hello world")`
* @param {Args} args
*/
exports.run = function run({ code }) {
return eval(code);
const context = vm.createContext({});
const script = new vm.Script(code);
return script.runInContext(context);
}
2 changes: 1 addition & 1 deletion tools/execute_py_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ def run(code: str):
Args:
code: Python code to execute, such as `print("hello world")`
"""
return exec(code)
return eval(code)

0 comments on commit fbeaa9c

Please sign in to comment.