Skip to main content

文件系统

🌐 Filesystem

文件系统提供程序使代理能够读取、写入和管理文件。当你在工作区上配置文件系统时,代理将获得用于文件操作的工具。

🌐 Filesystem providers give agents the ability to read, write, and manage files. When you configure a filesystem on a workspace, agents receive tools for file operations.

文件系统提供程序处理工作区的所有文件操作:

🌐 A filesystem provider handles all file operations for a workspace:

  • 读取 - 读取文件内容
  • 写入 - 创建和更新文件
  • 列表 - 浏览目录
  • 删除 - 移除文件和目录
  • 统计 - 获取文件元数据

支持的提供商
Direct link to 支持的提供商

🌐 Supported providers

可用提供商:

🌐 Available providers:

tip

LocalFilesystem 是入门最简单的方式,因为它不需要任何外部服务。

基本用法
Direct link to 基本用法

🌐 Basic usage

创建一个带有文件系统的工作区,并将其分配给代理。然后代理可以在执行任务时读取、写入和管理文件:

🌐 Create a workspace with a filesystem and assign it to an agent. The agent can then read, write, and manage files as part of its tasks:

src/mastra/agents/file-agent.ts
import { Agent } from '@mastra/core/agent';
import { Workspace, LocalFilesystem } from '@mastra/core/workspace';

const workspace = new Workspace({
filesystem: new LocalFilesystem({
basePath: './workspace',
}),
});

const agent = new Agent({
id: 'file-agent',
model: 'openai/gpt-4o',
instructions: 'You are a helpful file management assistant.',
workspace,
});

// The agent now has filesystem tools available
const response = await agent.generate('List all files in the workspace');

只读模式
Direct link to 只读模式

🌐 Read-only mode

要防止代理修改文件,请启用只读模式:

🌐 To prevent agents from modifying files, enable read-only mode:

const workspace = new Workspace({
filesystem: new LocalFilesystem({
basePath: './workspace',
readOnly: true,
}),
});

在只读模式下,写入工具(write_fileedit_filedeletemkdir)不会被添加到代理的工具集。代理仍然可以读取和列出文件。

🌐 When read-only, write tools (write_file, edit_file, delete, mkdir) are not added to the agent's toolset. The agent can still read and list files.

代理工具
Direct link to 代理工具

🌐 Agent tools

当你在工作区配置文件系统时,代理将获得用于读取、写入、列出和删除文件的工具。详情请参见 工作区类参考

🌐 When you configure a filesystem on a workspace, agents receive tools for reading, writing, listing, and deleting files. See workspace class reference for details.

🌐 Related