Skip to content

Sandbox

Execute JavaScript, Python, or shell commands in isolated environments.

3 modules

ModuleDescription
執行 JavaScript通過 Node.js 執行 JavaScript 程式碼,並設置超時
執行 Python在子程序中執行 Python 程式碼,並設置超時
執行 Shell執行 Shell 命令,並設置超時和環境控制

Modules

執行 JavaScript

sandbox.execute_js

通過 Node.js 執行 JavaScript 程式碼,並設置超時

Parameters:

NameTypeRequiredDefaultDescription
codestringYes-要通過 Node.js 執行的 JavaScript 程式碼
timeoutnumberNo10執行超時時間(秒)

Output:

FieldTypeDescription
stdoutstring腳本的標準輸出
stderrstring腳本的標準錯誤輸出
exit_codenumber程序退出代碼(0 = 成功)
execution_time_msnumber執行時間(毫秒)

Example: Simple console.log

yaml
code: console.log("Hello, World!");
timeout: 10

Example: JSON processing

yaml
code: const data = { name: "test", value: 42 };
console.log(JSON.stringify(data, null, 2));

執行 Python

sandbox.execute_python

在子程序中執行 Python 程式碼,並設置超時

Parameters:

NameTypeRequiredDefaultDescription
codestringYes-要執行的 Python 程式碼
timeoutnumberNo10執行超時時間(秒)
allowed_modulesarrayNo-可匯入模組的白名單(留空表示允許所有)

Output:

FieldTypeDescription
stdoutstring腳本的標準輸出
stderrstring腳本的標準錯誤輸出
exit_codenumber程序退出代碼(0 = 成功)
execution_time_msnumber執行時間(毫秒)

Example: Simple print

yaml
code: print("Hello, World!")
timeout: 10

Example: Math calculation

yaml
code: import math
print(math.pi)
allowed_modules: ["math"]

執行 Shell

sandbox.execute_shell

執行 Shell 命令,並設置超時和環境控制

Parameters:

NameTypeRequiredDefaultDescription
commandstringYes-要執行的 Shell 命令
timeoutnumberNo10執行超時時間(秒)
working_dirstringNo-命令的工作目錄
envobjectNo-要設置的額外環境變數(與當前環境合併)

Output:

FieldTypeDescription
stdoutstring命令的標準輸出
stderrstring命令的標準錯誤輸出
exit_codenumber程序退出代碼(0 = 成功)
execution_time_msnumber執行時間(毫秒)

Example: Simple echo

yaml
command: echo "Hello, World!"
timeout: 10

Example: List files with custom working directory

yaml
command: ls -la
working_dir: /tmp

Released under the Apache 2.0 License.