tmp
This commit is contained in:
@@ -13,21 +13,22 @@ export const getEnvTool = tool(
|
||||
async (input) => {
|
||||
const { name } = input || {};
|
||||
|
||||
const sensitiveKeys = ['API_KEY', 'SECRET', 'PASSWORD', 'TOKEN', 'CREDENTIAL'];
|
||||
const isSensitive = (key) => sensitiveKeys.some(s => key.toUpperCase().includes(s));
|
||||
|
||||
if (name) {
|
||||
const value = process.env[name];
|
||||
if (value === undefined) {
|
||||
return JSON.stringify({ error: `Environment variable '${name}' not found` });
|
||||
}
|
||||
return JSON.stringify({ name, value });
|
||||
// 按名查询同样脱敏,防止 LLM 绕过读取密钥
|
||||
return JSON.stringify({ name, value: isSensitive(name) ? '***REDACTED***' : value });
|
||||
}
|
||||
|
||||
// 返回所有环境变量(过滤敏感信息)
|
||||
const env = { ...process.env };
|
||||
const sensitiveKeys = ['API_KEY', 'SECRET', 'PASSWORD', 'TOKEN', 'CREDENTIAL'];
|
||||
|
||||
for (const key of Object.keys(env)) {
|
||||
const upperKey = key.toUpperCase();
|
||||
if (sensitiveKeys.some(s => upperKey.includes(s))) {
|
||||
if (isSensitive(key)) {
|
||||
env[key] = '***REDACTED***';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import * as z from 'zod';
|
||||
import { tool } from 'langchain';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
export const winCmdTool = tool(
|
||||
async ({ command, cwd, timeout = 30000 }) => {
|
||||
const workingDir = cwd || process.cwd();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
let killed = false;
|
||||
|
||||
const proc = spawn('cmd.exe', ['/c', command], {
|
||||
cwd: workingDir,
|
||||
shell: false,
|
||||
windowsHide: true
|
||||
});
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
killed = true;
|
||||
proc.kill('SIGKILL');
|
||||
}, timeout);
|
||||
|
||||
proc.stdout.on('data', (data) => {
|
||||
stdout += data.toString();
|
||||
});
|
||||
|
||||
proc.stderr.on('data', (data) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
|
||||
proc.on('close', (code) => {
|
||||
clearTimeout(timer);
|
||||
if (killed) {
|
||||
resolve(JSON.stringify({
|
||||
success: false,
|
||||
error: `Command timed out after ${timeout}ms`,
|
||||
command,
|
||||
cwd: workingDir
|
||||
}));
|
||||
return;
|
||||
}
|
||||
resolve(JSON.stringify({
|
||||
success: code === 0,
|
||||
exitCode: code,
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim(),
|
||||
command,
|
||||
cwd: workingDir
|
||||
}));
|
||||
});
|
||||
|
||||
proc.on('error', (error) => {
|
||||
clearTimeout(timer);
|
||||
resolve(JSON.stringify({
|
||||
success: false,
|
||||
error: error.message,
|
||||
command,
|
||||
cwd: workingDir
|
||||
}));
|
||||
});
|
||||
});
|
||||
},
|
||||
{
|
||||
name: 'win_cmd',
|
||||
description: '在 Windows 系统上执行 cmd.exe 命令行命令。适用于运行 Windows 批处理命令、文件系统操作命令(如 dir、copy、del、mkdir 等)。不支持 PowerShell 命令。',
|
||||
schema: z.object({
|
||||
command: z.string().describe('要执行的 cmd.exe 命令或命令组合,使用 && 连接多个命令,如 "dir /b" 或 "echo hello && dir"'),
|
||||
cwd: z.string().optional().describe('执行命令的工作目录,默认为当前工作目录'),
|
||||
timeout: z.number().optional().describe('命令执行超时时间(毫秒),默认30000')
|
||||
})
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user