Skip to content

Commit 734b88a

Browse files
committed
feat: 增加一个沙盒运行代码的工具函数
1 parent 621061b commit 734b88a

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/apiTools.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,30 +1317,34 @@ registerTool(diagnosticTop5Errors);
13171317
// 12. 沙盒执行 Lua/Python 代码
13181318
export const sandboxRun: Tool = {
13191319
name: 'sandbox_run',
1320-
description: `在沙盒环境中执行 Lua 或 Python 代码,并返回标准输出、标准错误。禁止访问文件系统。`,
1320+
description: `在沙盒环境中执行 Lua、Python、Node.js、TypeScriptWSL Bash 代码,并返回标准输出、标准错误。禁止访问文件系统。`,
13211321
parameters: {
13221322
type: 'object',
13231323
properties: {
1324-
language: { type: 'string', description: '执行语言(luapython)' },
1324+
language: { type: 'string', description: '执行语言(luapython、nodejs、typescript、bash)' },
13251325
code: { type: 'string', description: '要执行的代码内容。' },
13261326
input: { type: 'string', description: '可选的标准输入内容(stdin)。' }
13271327
},
13281328
required: ['language', 'code'],
13291329
},
1330-
function: async (args: { language: 'lua' | 'python'; code: string; input?: string }) => {
1330+
function: async (args: { language: 'lua' | 'python' | 'nodejs' | 'typescript' | 'bash'; code: string; input?: string }) =>
1331+
{
13311332
const strLanguage: string = args.language;
13321333
const strCode: string = args.code;
13331334
const strInput: string = args.input ?? '';
13341335

13351336
try
13361337
{
13371338
// 简单敏感词检测
1338-
const arrForbidden: string[] = ['os.', 'io.', 'open(', 'require(', 'import os', 'import shutil']
1339+
const arrForbidden: string[] = [
1340+
'os.', 'io.', 'open(', 'require(', 'import os', 'import shutil',
1341+
'fs.', 'child_process', 'process.', 'import fs', 'import child_process', 'import process'
1342+
];
13391343
for (const strBad of arrForbidden)
13401344
{
13411345
if (strCode.includes(strBad))
13421346
{
1343-
return `安全警告:代码中包含禁止的调用 (${strBad})`
1347+
return `安全警告:代码中包含禁止的调用 (${strBad})`;
13441348
}
13451349
}
13461350

@@ -1357,9 +1361,24 @@ export const sandboxRun: Tool = {
13571361
strCommand = 'python';
13581362
arrArgs = ['-c', strCode];
13591363
}
1364+
else if (strLanguage === 'nodejs')
1365+
{
1366+
strCommand = 'node';
1367+
arrArgs = ['-e', strCode];
1368+
}
1369+
else if (strLanguage === 'typescript')
1370+
{
1371+
strCommand = 'ts-node';
1372+
arrArgs = ['-e', strCode];
1373+
}
1374+
else if (strLanguage === 'bash')
1375+
{
1376+
strCommand = 'wsl';
1377+
arrArgs = ['bash', '-c', strCode];
1378+
}
13601379
else
13611380
{
1362-
return `不支持的语言类型: ${strLanguage}`
1381+
return `不支持的语言类型: ${strLanguage}`;
13631382
}
13641383

13651384
return await new Promise<string>((resolve, reject) =>
@@ -1392,7 +1411,7 @@ export const sandboxRun: Tool = {
13921411
// 写入标准输入
13931412
if (strInput.length > 0)
13941413
{
1395-
objChild.stdin.write(strInput)
1414+
objChild.stdin.write(strInput);
13961415
}
13971416
objChild.stdin.end();
13981417

@@ -1407,9 +1426,9 @@ export const sandboxRun: Tool = {
14071426
return `沙盒执行失败: ${error.message}`;
14081427
}
14091428
},
1410-
}
1429+
};
14111430

1412-
registerTool(sandboxRun)
1431+
registerTool(sandboxRun);
14131432

14141433

14151434

0 commit comments

Comments
 (0)