Use this file to discover all available pages before exploring further.
AG-Kit’s file system tools provide a comprehensive and reliable way for your agents to interact with files. With support for transactions, different backends, and a unified toolkit, it’s designed for building robust agents that perform file-based tasks.
FilesystemToolkit: This is the main entry point. It bundles a collection of file-related tools (read_file, write_file, etc.) that are ready to be used by an agent.
FileOperator (Backend): This is the engine that performs the actual file operations. You can choose from three backends:
LocalFileOperator: For high-performance access to the local filesystem.
InMemoryFileOperator: For fast, isolated testing in a virtual filesystem.
SandboxFileOperator: For secure execution in an isolated container, ideal for handling untrusted code.
import { FilesystemToolkit, LocalFileOperator } from '@ag-kit/tools';// 1. Choose a backend and create the toolkitconst operator = new LocalFileOperator();const fsToolkit = new FilesystemToolkit({ name: 'fs', context: { workingDirectory: process.cwd(), fsOperator: operator },});// 2. Get a tool and use itconst writeTool = fsToolkit.getTool('write_file');await writeTool.invoke({ file_path: 'greeting.txt', content: 'Hello});
For complex tasks like code generation, you often need to modify multiple files. If one step fails, you risk leaving the project in a broken state. Transactions solve this by ensuring that all file operations succeed or none of them do.If any operation inside withTransaction fails, all changes are automatically rolled back.
import { withTransaction } from '@ag-kit/tools/fs/operator/transaction';import { LocalFileOperator } from '@ag-kit/tools';const operator = new LocalFileOperator();try { await withTransaction(operator, async (tx) => { // These operations are now atomic await tx.writeFile('src/component.js', '// New component code'); await tx.writeFile('src/index.js', 'import "./component.js";'); // If this next one fails, the two files above will be reverted. await tx.writeFile('package.json', 'invalid json'); });} catch (error) { console.log('Transaction failed and was rolled back:', error.message);}
For more advanced transaction control, see the API Reference.
The grep tool searches for a specific text pattern (including regex) within files in a given directory. It’s perfect for finding where a function is called or locating specific comments.