完善了agent
This commit is contained in:
82
agent/tools/system/envs.js
Normal file
82
agent/tools/system/envs.js
Normal file
@@ -0,0 +1,82 @@
|
||||
// LangChain 环境变量工具
|
||||
|
||||
import { tool } from '@langchain/core/tools';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* 获取环境变量工具
|
||||
* @param {Object} input - 输入参数
|
||||
* @param {string} [input.name] - 环境变量名,不指定则返回所有
|
||||
* @returns {string} - 返回环境变量值或所有环境变量的JSON字符串
|
||||
*/
|
||||
export const getEnvTool = tool(
|
||||
async (input) => {
|
||||
const { name } = input || {};
|
||||
|
||||
if (name) {
|
||||
const value = process.env[name];
|
||||
if (value === undefined) {
|
||||
return JSON.stringify({ error: `Environment variable '${name}' not found` });
|
||||
}
|
||||
return JSON.stringify({ name, 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))) {
|
||||
env[key] = '***REDACTED***';
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify({ env });
|
||||
},
|
||||
{
|
||||
name: 'env_get',
|
||||
description: '获取环境变量的值。如果不指定变量名,则返回所有环境变量。',
|
||||
schema: z.object({
|
||||
name: z.string().optional().describe('环境变量名,不指定则返回所有')
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* 设置环境变量工具
|
||||
* @param {Object} input - 输入参数
|
||||
* @param {string} input.name - 环境变量名
|
||||
* @param {string} input.value - 环境变量值
|
||||
* @returns {string} - 返回设置结果的JSON字符串
|
||||
*/
|
||||
export const setEnvTool = tool(
|
||||
async (input) => {
|
||||
const { name, value } = input || {};
|
||||
|
||||
if (!name) {
|
||||
return JSON.stringify({ error: 'Environment variable name is required' });
|
||||
}
|
||||
|
||||
if (value === undefined) {
|
||||
return JSON.stringify({ error: 'Environment variable value is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
process.env[name] = value;
|
||||
return JSON.stringify({ success: true, name, value });
|
||||
} catch (error) {
|
||||
return JSON.stringify({ error: `Failed to set environment variable: ${error.message}` });
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'env_set',
|
||||
description: '设置环境变量的值。',
|
||||
schema: z.object({
|
||||
name: z.string().describe('环境变量名'),
|
||||
value: z.string().describe('环境变量值')
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
export default getEnvTool;
|
||||
Reference in New Issue
Block a user