> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # WorkspaceFilesystem **新增於:** `@mastra/core@1.1.0` `WorkspaceFilesystem` 介面定義 Workspace 與檔案儲存空間互動的方式。 ## 方法 ### `readFile(path, options?)` 讀取檔案內容。 ```typescript const content = await filesystem.readFile('/docs/guide.md') const buffer = await filesystem.readFile('/image.png', { encoding: 'binary' }) ``` **參數:** **path** (`string`): 相對於 basePath 的檔案路徑。 **options** (`Options`): readFile 選項。 **options.encoding** (`'utf-8' | 'binary'`): 文字或二進位編碼 **傳回值:** `Promise` ### `writeFile(path, content, options?)` 寫入檔案內容。 ```typescript await filesystem.writeFile('/docs/new.md', '# New Document') await filesystem.writeFile('/nested/path/file.md', content, { recursive: true }) ``` **參數:** **path** (`string`): 相對於 basePath 的檔案路徑。 **content** (`string | Buffer`): 檔案內容 **options** (`Options`): 設定選項。 **options.recursive** (`boolean`): 若上層目錄不存在則建立 **options.overwrite** (`boolean`): 覆寫現有檔案 **options.expectedMtime** (`Date`): 提供此值時,如果檔案目前的修改時間不相符,寫入會因 StaleFileError 而失敗。可用於樂觀並行控制,以偵測讀取與寫入之間的外部修改。 ### `deleteFile(path, options?)` 刪除檔案。 ```typescript await filesystem.deleteFile('/docs/old.md') await filesystem.deleteFile('/docs/maybe.md', { force: true }) // Don't throw if missing ``` **參數:** **path** (`string`): 檔案路徑 **options** (`Options`): 設定選項。 **options.force** (`boolean`): 檔案不存在時不擲回錯誤 ### `appendFile(path, content)` 將內容附加至檔案。如果檔案尚不存在,則建立檔案。系統會自動建立上層目錄。 ```typescript await filesystem.appendFile('/logs/app.log', 'New log entry\n') ``` **參數:** **path** (`string`): 檔案路徑 **content** (`string | Buffer`): 要附加的內容 ### `copyFile(src, dest, options?)` 將檔案複製到新位置。 ```typescript await filesystem.copyFile('/docs/template.md', '/docs/new-doc.md') ``` **參數:** **src** (`string`): 來源檔案路徑 **dest** (`string`): 目的檔案路徑 **options** (`Options`): 設定選項。 **options.overwrite** (`boolean`): 若目的地已存在則覆寫 ### `moveFile(src, dest, options?)` 移動檔案或重新命名。 ```typescript await filesystem.moveFile('/docs/draft.md', '/docs/final.md') ``` **參數:** **src** (`string`): 來源檔案路徑 **dest** (`string`): 目的檔案路徑 **options** (`Options`): 設定選項。 **options.overwrite** (`boolean`): 若目的地已存在則覆寫 ### `readdir(path, options?)` 列出目錄內容。 ```typescript const entries = await filesystem.readdir('/docs') // [{ name: 'guide.md', type: 'file' }, { name: 'api', type: 'directory' }] ``` **傳回值:** `Promise` ```typescript interface FileEntry { name: string type: 'file' | 'directory' size?: number isSymlink?: boolean symlinkTarget?: string } ``` ### `mkdir(path, options?)` 建立目錄。 ```typescript await filesystem.mkdir('/docs/api') await filesystem.mkdir('/deeply/nested/path', { recursive: true }) ``` **參數:** **path** (`string`): 目錄路徑 **options** (`Options`): 設定選項。 **options.recursive** (`boolean`): 建立上層目錄 ### `rmdir(path, options?)` 移除目錄。 ```typescript await filesystem.rmdir('/docs/old') await filesystem.rmdir('/docs/nested', { recursive: true }) ``` **參數:** **path** (`string`): 目錄路徑 **options** (`Options`): 設定選項。 **options.recursive** (`boolean`): 以遞迴方式移除內容 **options.force** (`boolean`): 目錄不存在時不擲回錯誤 ### `exists(path)` 檢查路徑是否存在。 ```typescript const exists = await filesystem.exists('/docs/guide.md') ``` **傳回值:** `Promise` ### `stat(path)` 取得檔案或目錄的中繼資料。 ```typescript const stat = await filesystem.stat('/docs/guide.md') // { name: 'guide.md', path: '/docs/guide.md', type: 'file', size: 1234, createdAt: Date, modifiedAt: Date } ``` **傳回值:** `Promise` ```typescript 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 } ``` ## 選用方法 ### `init()` 初始化檔案系統。由 `workspace.init()` 呼叫。 ```typescript await filesystem.init?.() ``` ### `destroy()` 清除資源。由 `workspace.destroy()` 呼叫。 ```typescript await filesystem.destroy?.() ``` ### `getInfo()` 取得檔案系統中繼資料。 ```typescript const info = await filesystem.getInfo?.() // { id, name, provider, basePath, readOnly, status, storage? } ``` **傳回值:** `Promise` ```typescript interface FilesystemInfo { id: string name: string provider: string basePath?: string readOnly?: boolean status?: string storage?: { totalBytes?: number usedBytes?: number availableBytes?: number } } ``` ### `getInstructions(opts?)` 傳回此檔案系統運作方式的說明。將 Workspace 指派給 Agent 時,此說明會注入 Agent 的系統訊息。 ```typescript const instructions = filesystem.getInstructions?.() // 'Local filesystem at "/workspace". Files at workspace path "/foo" are stored at "/workspace/foo" on disk.' ``` **參數:** **opts.requestContext** (`RequestContext`): 如果建構函式中提供了 instructions 函式,便會轉傳給該函式。 **傳回值:** `string` ## 相關內容 - [Workspace 類別](https://mastra.zisheng.pro/zh-TW/reference/workspace/workspace-class) - [Sandbox 介面](https://mastra.zisheng.pro/zh-TW/reference/workspace/sandbox)