import { Tool } from '@agenite/tool';
import * as fs from 'fs/promises';
import * as path from 'path';
export function createFileSystemTool() {
return new Tool({
name: 'file-system',
description: 'Perform file system operations',
inputSchema: {
type: 'object',
properties: {
operation: {
type: 'string',
enum: ['read', 'write', 'list', 'exists', 'mkdir'],
},
path: { type: 'string' },
content: { type: 'string' },
},
required: ['operation', 'path'],
},
execute: async ({ input }) => {
const startTime = Date.now();
const fullPath = path.resolve(input.path);
try {
switch (input.operation) {
case 'read': {
const content = await fs.readFile(fullPath, 'utf-8');
return {
isError: false,
data: content,
duration: Date.now() - startTime,
};
}
case 'write': {
await fs.mkdir(path.dirname(fullPath), { recursive: true });
await fs.writeFile(fullPath, input.content || '');
return {
isError: false,
data: `File written to ${input.path}`,
duration: Date.now() - startTime,
};
}
// ... other operations
}
} catch (error) {
return {
isError: true,
data: error.message,
duration: Date.now() - startTime,
};
}
},
});
}