WorkspaceFilesystem
Added in: @mastra/core@1.1.0
The WorkspaceFilesystem interface defines how workspaces interact with file storage.
MethodsDirect link to Methods
readFile(path, options?)Direct link to readfilepath-options
Read file contents.
const content = await filesystem.readFile('/docs/guide.md')
const buffer = await filesystem.readFile('/image.png', { encoding: 'binary' })
Parameters:
path:
options?:
encoding?:
Returns: Promise<string | Buffer>
writeFile(path, content, options?)Direct link to writefilepath-content-options
Write file contents.
await filesystem.writeFile('/docs/new.md', '# New Document')
await filesystem.writeFile('/nested/path/file.md', content, { recursive: true })
Parameters:
path:
content:
options?:
recursive?:
overwrite?:
deleteFile(path, options?)Direct link to deletefilepath-options
Delete a file.
await filesystem.deleteFile('/docs/old.md')
await filesystem.deleteFile('/docs/maybe.md', { force: true }) // Don't throw if missing
Parameters:
path:
options?:
force?:
appendFile(path, content)Direct link to appendfilepath-content
Append content to a file, creating it if it doesn't already exist. Parent directories are created automatically.
await filesystem.appendFile('/logs/app.log', 'New log entry\n')
Parameters:
path:
content:
copyFile(src, dest, options?)Direct link to copyfilesrc-dest-options
Copy a file to a new location.
await filesystem.copyFile('/docs/template.md', '/docs/new-doc.md')
Parameters:
src:
dest:
options?:
overwrite?:
moveFile(src, dest, options?)Direct link to movefilesrc-dest-options
Move or rename a file.
await filesystem.moveFile('/docs/draft.md', '/docs/final.md')
Parameters:
src:
dest:
options?:
overwrite?:
readdir(path, options?)Direct link to readdirpath-options
List directory contents.
const entries = await filesystem.readdir('/docs')
// [{ name: 'guide.md', type: 'file' }, { name: 'api', type: 'directory' }]
Returns: Promise<FileEntry[]>
interface FileEntry {
name: string
type: 'file' | 'directory'
size?: number
isSymlink?: boolean
symlinkTarget?: string
}
mkdir(path, options?)Direct link to mkdirpath-options
Create a directory.
await filesystem.mkdir('/docs/api')
await filesystem.mkdir('/deeply/nested/path', { recursive: true })
Parameters:
path:
options?:
recursive?:
rmdir(path, options?)Direct link to rmdirpath-options
Remove a directory.
await filesystem.rmdir('/docs/old')
await filesystem.rmdir('/docs/nested', { recursive: true })
Parameters:
path:
options?:
recursive?:
force?:
exists(path)Direct link to existspath
Check if a path exists.
const exists = await filesystem.exists('/docs/guide.md')
Returns: Promise<boolean>
stat(path)Direct link to statpath
Get file or directory metadata.
const stat = await filesystem.stat('/docs/guide.md')
// { name: 'guide.md', path: '/docs/guide.md', type: 'file', size: 1234, createdAt: Date, modifiedAt: Date }
Returns: Promise<FileStat>
interface FileStat {
name: string // File or directory name (basename only)
path: string // Path relative to the filesystem basePath
type: 'file' | 'directory'
size: number
createdAt: Date
modifiedAt: Date
mimeType?: string
}
Optional methodsDirect link to Optional methods
init()Direct link to init
Initialize the filesystem. Called by workspace.init().
await filesystem.init?.()
destroy()Direct link to destroy
Clean up resources. Called by workspace.destroy().
await filesystem.destroy?.()
getInfo()Direct link to getinfo
Get filesystem metadata.
const info = await filesystem.getInfo?.()
// { id, name, provider, basePath, readOnly, status, storage? }
Returns: Promise<FilesystemInfo>
interface FilesystemInfo {
id: string
name: string
provider: string
basePath?: string
readOnly?: boolean
status?: string
storage?: {
totalBytes?: number
usedBytes?: number
availableBytes?: number
}
}
getInstructions(opts?)Direct link to getinstructionsopts
Returns a description of how this filesystem works. Injected into the agent's system message when the workspace is assigned to an agent.
const instructions = filesystem.getInstructions?.()
// 'Local filesystem at "/workspace". Files at workspace path "/foo" are stored at "/workspace/foo" on disk.'
Parameters:
opts.requestContext?:
Returns: string