MesaFilesystem
通过标准 Mastra WorkspaceFilesystem 接口,将 Workspace 文件存储在 Mesa repo 中。
当 Agent 需要版本化文件存储时,请使用 MesaFilesystem。对于本地目录,请使用 LocalFilesystem。对于对象存储,请使用 S3Filesystem、GCSFilesystem 或 AzureBlobFilesystem。
MesaFilesystem 在 Mastra 进程中运行。
Mesa 的 POSIX 挂载(用于在 Sandbox 中使用 Mesa 文件系统)尚未包含在 @mastra/mesa 包中。相关支持即将推出。
安装安装的直接链接
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/mesa
pnpm add @mastra/mesa
yarn add @mastra/mesa
bun add @mastra/mesa
用法示例用法示例的直接链接
挂载一个 Mesa repo,并将文件系统传给 Workspace:
import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { MesaFilesystem } from '@mastra/mesa'
const workspace = new Workspace({
filesystem: new MesaFilesystem({
apiKey: process.env.MESA_API_KEY,
org: 'acme',
repos: [{ name: 'docs', bookmark: 'main' }],
}),
})
const agent = new Agent({
id: 'file-agent',
name: 'file-agent',
model: 'anthropic/claude-opus-4-7',
workspace,
})
省略 apiKey 时,会回退到 MESA_API_KEY。
构造函数参数构造函数参数的直接链接
apiKey?:
MESA_API_KEY。org?:
repos:
cache?:
ttl?:
readOnly?:
属性属性的直接链接
id:
name:
'MesaFilesystem')。provider:
'mesa')。readOnly:
client:
filesystem:
change:
bookmark:
方法方法的直接链接
MesaFilesystem 实现了 WorkspaceFilesystem 接口。
文件操作文件操作的直接链接
readFile(path, options?)readfilepath-options的直接链接
从 Mesa 读取文件。
const content = await filesystem.readFile('/acme/docs/README.md', {
encoding: 'utf-8',
})
返回:Promise<string | Buffer>
writeFile(path, content, options?)writefilepath-content-options的直接链接
将文件写入 Mesa。
await filesystem.writeFile('/acme/docs/report.md', '# Report')
返回:Promise<void>
appendFile(path, content)appendfilepath-content的直接链接
向文件追加内容。
await filesystem.appendFile('/acme/docs/log.txt', 'new line\n')
返回:Promise<void>
deleteFile(path, options?)deletefilepath-options的直接链接
删除文件。
await filesystem.deleteFile('/acme/docs/old-report.md')
返回:Promise<void>
copyFile(src, dest, options?)copyfilesrc-dest-options的直接链接
复制文件。
await filesystem.copyFile('/acme/docs/report.md', '/acme/docs/archive/report.md')
返回:Promise<void>
moveFile(src, dest, options?)movefilesrc-dest-options的直接链接
移动文件或重命名。
await filesystem.moveFile('/acme/docs/draft.md', '/acme/docs/final.md')
返回:Promise<void>
目录操作目录操作的直接链接
mkdir(path, options?)mkdirpath-options的直接链接
创建目录。
await filesystem.mkdir('/acme/docs/reports', { recursive: true })
返回:Promise<void>
rmdir(path, options?)rmdirpath-options的直接链接
删除目录。
await filesystem.rmdir('/acme/docs/reports', { recursive: true })
返回:Promise<void>
readdir(path, options?)readdirpath-options的直接链接
列出目录条目。
const entries = await filesystem.readdir('/acme/docs', {
recursive: true,
extension: '.md',
})
返回:Promise<FileEntry[]>
路径操作路径操作的直接链接
exists(path)existspath的直接链接
检查路径是否存在。
const exists = await filesystem.exists('/acme/docs/README.md')
返回:Promise<boolean>
stat(path)statpath的直接链接
返回文件或目录元数据。
const stat = await filesystem.stat('/acme/docs/README.md')
返回:Promise<FileStat>
realpath(path)realpathpath的直接链接
返回 Mesa 中的规范路径。
const realPath = await filesystem.realpath('/acme/docs/README.md')
返回:Promise<string>
Mesa 操作Mesa 操作的直接链接
bash(options?)bashoptions的直接链接
为此文件系统创建由 Mesa 支持的 Bash runtime。
const bash = await filesystem.bash({
cwd: '/acme/docs',
})
返回:Promise<Bash>
路径语义路径语义的直接链接
方法要求使用绝对路径。对于 MesaFilesystem,路径以 Mesa 挂载点为根,并且必须包含 org slug 和 repo 名称:
await filesystem.readFile('/acme/docs/README.md')
不要省略开头的斜杠:
await filesystem.readFile('acme/docs/README.md') // Incorrect
await filesystem.readFile('/acme/docs/README.md') // Correct
Org 来自构造函数中的 org;如果省略 org,则来自 Mesa SDK 推断的默认 org。它始终是路径的第一个部分。
挂载多个 repo 时,每个 repo 都可在 org 路径段下访问:
const filesystem = new MesaFilesystem({
org: 'acme',
repos: [
{ name: 'docs', bookmark: 'main' },
{ name: 'website', bookmark: 'main' },
],
})
await filesystem.readFile('/acme/docs/README.md')
await filesystem.readFile('/acme/website/package.json')
Mesa 版本控制 APIMesa 版本控制 API的直接链接
访问底层 Mesa 文件系统,执行 Mesa 特有的 change 和 bookmark 操作:
await filesystem.writeFile('/acme/docs/draft.md', 'Draft')
const current = await filesystem.change.current({
repo: 'docs',
})
await filesystem.bookmark.move({
repo: 'docs',
name: 'main',
changeId: current.changeId,
})
有关版本控制语义的更多详情,请参阅 Mesa 文档。
只读模式只读模式的直接链接
设置 readOnly: true 可阻止通过 Mastra 执行写入操作:
const filesystem = new MesaFilesystem({
repos: [{ name: 'docs', bookmark: 'main' }],
readOnly: true,
})
读取操作仍可正常执行。写入操作会抛出 WorkspaceReadOnlyError。
并发并发的直接链接
overwrite: false 和 expectedMtime 会在写入前执行预检查。除非 Mesa 为应用挂载添加原生条件写入功能,否则这些检查不具备原子性。