/**
* 文件系统操作工具
*/
import fs from 'fs/promises';
import path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
export class FileSystemTools {
getToolDefinitions() {
return [
{
name: 'read_file',
description: '读取文件内容',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string', description: '文件路径' },
},
required: ['path'],
},
},
{
name: 'write_file',
description: '写入文件内容',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string', description: '文件路径' },
content: { type: 'string', description: '文件内容' },
},
required: ['path', 'content'],
},
},
{
name: 'list_directory',
description: '列出目录内容',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string', description: '目录路径' },
},
required: ['path'],
},
},
{
name: 'create_directory',
description: '创建目录',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string', description: '目录路径' },
},
required: ['path'],
},
},
{
name: 'delete_file',
description: '删除文件或目录',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string', description: '文件或目录路径' },
},
required: ['path'],
},
},
{
name: 'copy_file',
description: '复制文件',
inputSchema: {
type: 'object',
properties: {
source: { type: 'string', description: '源文件路径' },
destination: { type: 'string', description: '目标文件路径' },
},
required: ['source', 'destination'],
},
},
{
name: 'move_file',
description: '移动或重命名文件',
inputSchema: {
type: 'object',
properties: {
source: { type: 'string', description: '源文件路径' },
destination: { type: 'string', description: '目标文件路径' },
},
required: ['source', 'destination'],
},
},
{
name: 'search_files',
description: '搜索文件(支持通配符)',
inputSchema: {
type: 'object',
properties: {
directory: { type: 'string', description: '搜索目录' },
pattern: { type: 'string', description: '搜索模式(如 *.txt)' },
},
required: ['directory', 'pattern'],
},
},
];
}
canHandle(toolName) {
const tools = ['read_file', 'write_file', 'list_directory', 'create_directory',
'delete_file', 'copy_file', 'move_file', 'search_files'];
return tools.includes(toolName);
}
async executeTool(name, args) {
switch (name) {
case 'read_file':
return await this.readFile(args.path);
case 'write_file':
return await this.writeFile(args.path, args.content);
case 'list_directory':
return await this.listDirectory(args.path);
case 'create_directory':
return await this.createDirectory(args.path);
case 'delete_file':
return await this.deleteFile(args.path);
case 'copy_file':
return await this.copyFile(args.source, args.destination);
case 'move_file':
return await this.moveFile(args.source, args.destination);
case 'search_files':
return await this.searchFiles(args.directory, args.pattern);
default:
throw new Error(`未知工具: ${name}`);
}
}
async readFile(filePath) {
try {
const content = await fs.readFile(filePath, 'utf-8');
return { success: true, content, path: filePath };
} catch (error) {
return { success: false, error: error.message };
}
}
async writeFile(filePath, content) {
try {
await fs.writeFile(filePath, content, 'utf-8');
return { success: true, path: filePath, message: '文件写入成功' };
} catch (error) {
return { success: false, error: error.message };
}
}
async listDirectory(dirPath) {
try {
const files = await fs.readdir(dirPath, { withFileTypes: true });
const items = files.map(file => ({
name: file.name,
type: file.isDirectory() ? 'directory' : 'file',
path: path.join(dirPath, file.name),
}));
return { success: true, items, path: dirPath };
} catch (error) {
return { success: false, error: error.message };
}
}
async createDirectory(dirPath) {
try {
await fs.mkdir(dirPath, { recursive: true });
return { success: true, path: dirPath, message: '目录创建成功' };
} catch (error) {
return { success: false, error: error.message };
}
}
async deleteFile(filePath) {
try {
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
await fs.rm(filePath, { recursive: true, force: true });
} else {
await fs.unlink(filePath);
}
return { success: true, path: filePath, message: '删除成功' };
} catch (error) {
return { success: false, error: error.message };
}
}
async copyFile(source, destination) {
try {
await fs.copyFile(source, destination);
return { success: true, source, destination, message: '复制成功' };
} catch (error) {
return { success: false, error: error.message };
}
}
async moveFile(source, destination) {
try {
await fs.rename(source, destination);
return { success: true, source, destination, message: '移动成功' };
} catch (error) {
return { success: false, error: error.message };
}
}
async searchFiles(directory, pattern) {
try {
const { stdout } = await execAsync(
`dir /s /b "${path.join(directory, pattern)}"`,
{ shell: 'cmd.exe' }
);
const files = stdout.trim().split('\n').filter(f => f);
return { success: true, files, count: files.length };
} catch (error) {
return { success: false, error: error.message, files: [] };
}
}
}