feat: 新增 6 个工具并重组目录结构
新增工具: - grep_content: 在文件内容中搜索文本/正则表达式 - get_file_info: 获取文件元信息(大小、权限、时间等) - move_file: 移动或重命名文件/目录 - copy_file: 复制文件或目录(支持递归) - delete_file: 删除文件或目录 - create_directory: 创建目录 目录重组: - src/tools/shell/ - Shell 相关工具(bash) - src/tools/filesystem/ - 文件系统工具(12个) - 每个子目录有独立的 index.ts 导出 权限系统扩展: - 新增操作类型: grep, info, move, copy, mkdir - 读取类操作默认允许,写入类操作需要确认
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import type { Tool, ToolResult } from '../../types/index.js';
|
||||
import { loadDescription } from '../load_description.js';
|
||||
import { getPermissionManager } from '../../permission/index.js';
|
||||
|
||||
export const deleteFileTool: Tool = {
|
||||
name: 'delete_file',
|
||||
description: loadDescription('delete_file'),
|
||||
parameters: {
|
||||
path: {
|
||||
type: 'string',
|
||||
description: '要删除的文件或目录的路径',
|
||||
required: true,
|
||||
},
|
||||
recursive: {
|
||||
type: 'boolean',
|
||||
description: '是否递归删除目录(默认 false)',
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
execute: async (params: Record<string, unknown>): Promise<ToolResult> => {
|
||||
const filePath = params.path as string;
|
||||
const recursive = (params.recursive as boolean) || false;
|
||||
const cwd = process.cwd();
|
||||
|
||||
const absolutePath = path.isAbsolute(filePath)
|
||||
? filePath
|
||||
: path.join(cwd, filePath);
|
||||
|
||||
// 权限检查
|
||||
const permissionManager = getPermissionManager();
|
||||
const permResult = await permissionManager.checkFilePermission({
|
||||
operation: 'delete',
|
||||
path: absolutePath,
|
||||
workdir: cwd,
|
||||
});
|
||||
|
||||
if (!permResult.allowed) {
|
||||
if (permResult.needsConfirmation) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: `需要用户确认: 删除 ${absolutePath}\n原因: ${permResult.reason || '需要权限确认'}`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: `权限被拒绝: ${permResult.reason || '不允许删除此文件'}`,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const stats = await fs.stat(absolutePath);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
if (!recursive) {
|
||||
// 检查目录是否为空
|
||||
const entries = await fs.readdir(absolutePath);
|
||||
if (entries.length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: `目录不为空。如需删除非空目录,请设置 recursive: true`,
|
||||
};
|
||||
}
|
||||
await fs.rmdir(absolutePath);
|
||||
} else {
|
||||
await fs.rm(absolutePath, { recursive: true });
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
output: `已删除目录: ${absolutePath}`,
|
||||
};
|
||||
} else {
|
||||
await fs.unlink(absolutePath);
|
||||
return {
|
||||
success: true,
|
||||
output: `已删除文件: ${absolutePath}`,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user