跳至主要內容

AgentFSFilesystem

透過 AgentFS SDK 將檔案儲存在 Turso/SQLite 資料庫中。檔案會跨工作階段持久保存於本機 SQLite 資料庫,讓 Agent 無須外部雲端服務也能使用持久儲存空間。介面詳情請參閱 WorkspaceFilesystem 介面

安裝
「安裝」的直接連結

npm install @mastra/agentfs

使用方式
「使用方式」的直接連結

AgentFSFilesystem 加入 Workspace,並指派給 Agent。建立 AgentFSFilesystem 類別的執行個體時,必須至少提供 agentIdpathagent 其中之一。

import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { AgentFSFilesystem } from '@mastra/agentfs'

const workspace = new Workspace({
filesystem: new AgentFSFilesystem({
agentId: 'my-agent',
}),
})

const agent = new Agent({
id: 'file-agent',
name: 'file-agent',
model: 'openai/gpt-5.6-sol',
workspace,
})

使用明確的資料庫路徑
「使用明確的資料庫路徑」的直接連結

預設情況下,資料庫會儲存在 .agentfs 目錄中,並以 agentId 作為檔名。你也可以指定自訂資料庫路徑:

import { AgentFSFilesystem } from '@mastra/agentfs'

const filesystem = new AgentFSFilesystem({
path: '/data/my-agent.db',
})

使用預先開啟的 AgentFS 執行個體
「使用預先開啟的 AgentFS 執行個體」的直接連結

如果需要自行管理 AgentFS 的生命週期:

import { AgentFS } from 'agentfs-sdk'
import { AgentFSFilesystem } from '@mastra/agentfs'

const agent = await AgentFS.open({ id: 'my-agent' })

const filesystem = new AgentFSFilesystem({
agent, // caller manages open/close
})

建構函式參數
「建構函式參數」的直接連結

你必須至少提供 agentIdpathagent 其中之一。

agentId?:

string
Agent ID;會在 .agentfs/<agentId>.db 建立資料庫

path?:

string
明確的資料庫檔案路徑(可取代 agentId)

agent?:

AgentFS
預先開啟的 AgentFS 執行個體。提供此值時,由呼叫端管理生命週期(開啟/關閉)。

id?:

string
= 自動產生
此檔案系統執行個體的唯一識別碼

displayName?:

string
= 'AgentFS'
在 UI 中顯示的易讀名稱

icon?:

FilesystemIcon
= 'database'
UI 使用的圖示識別碼

description?:

string
在 UI 中顯示的檔案系統簡短說明

readOnly?:

boolean
= false
設為 true 時,會封鎖所有寫入操作

屬性
「屬性」的直接連結

id:

string
檔案系統執行個體識別碼

name:

string
Provider 名稱('AgentFSFilesystem')

provider:

string
Provider 識別碼('agentfs')

readOnly:

boolean | undefined
檔案系統是否處於唯讀模式