Class LocalFilesystem
- All Implemented Interfaces:
AbstractFilesystem
- Direct Known Subclasses:
LocalFilesystemWithShell
AbstractFilesystem implementation that reads and writes files on the local disk.
Path-resolution behaviour is controlled by the LocalFsMode passed at construction:
LocalFsMode.SANDBOXED— paths anchored torootDir,..and outside-root absolute paths blocked. Equivalent to legacyvirtualMode=true.LocalFsMode.ROOTED— absolute paths accepted only when they fall under one of the configuredPathPolicyroots; relative paths anchor torootDir.LocalFsMode.UNRESTRICTED— absolute paths pass through; relative paths anchor torootDir. Equivalent to legacyvirtualMode=false.
-
Constructor Summary
ConstructorsConstructorDescriptionLocalFilesystem(String rootDir) LocalFilesystem(String rootDir, boolean virtualMode, int maxFileSizeMb) Same asLocalFilesystem(Path, boolean, int)with a path string; seeLocalFilesystem(String)fornull/ blank rules.LocalFilesystem(String rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory) Same asLocalFilesystem(Path, boolean, int, NamespaceFactory)with a path string; seeLocalFilesystem(String)fornull/ blank rules.LocalFilesystem(Path rootDir) Creates a abstract filesystem rooted at the given directory.LocalFilesystem(Path rootDir, boolean virtualMode, int maxFileSizeMb) Creates a abstract filesystem with explicit configuration.LocalFilesystem(Path rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory) Legacy constructor: mapsvirtualModetoLocalFsMode.SANDBOXEDorLocalFsMode.UNRESTRICTEDand uses an emptyPathPolicy.LocalFilesystem(Path rootDir, LocalFsMode mode, PathPolicy pathPolicy, int maxFileSizeMb, NamespaceFactory namespaceFactory) Creates a filesystem with explicit mode and path policy. -
Method Summary
Modifier and TypeMethodDescriptionDelete a file or directory (recursive for directories).downloadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<String> paths) Download multiple files.edit(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, String oldString, String newString, boolean replaceAll) Perform exact string replacements in an existing file.booleanCheck whether a file or directory exists.getCwd()Returns the root directory for this filesystem.getMode()Returns the active path-resolution mode.protected NamespaceFactoryReturns the configured path policy.Find files matching a glob pattern.grep(io.agentscope.core.agent.RuntimeContext runtimeContext, String pattern, String path, String glob) Search for a literal text pattern in files.List all files in a directory with metadata.Move (rename) a file or directory fromfromPathtotoPath.read(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, int offset, int limit) Read file content with optional line-based pagination.protected PathresolvePath(io.agentscope.core.agent.RuntimeContext rc, String key) protected StringtoVirtualPath(Path path) uploadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<Map.Entry<String, byte[]>> files) Upload multiple files.Write content to a new file, error if file already exists.
-
Constructor Details
-
LocalFilesystem
Same asLocalFilesystem(Path)withPath.of(path)afterString.strip(). Passnullfor the same CWD semantics as anullPath. Blank strings are rejected.- Parameters:
rootDir- filesystem root as a path string, ornullfor process working directory
-
LocalFilesystem
Creates a abstract filesystem rooted at the given directory.- Parameters:
rootDir- root directory for all operations (nullmeans CWD)
-
LocalFilesystem
Creates a abstract filesystem with explicit configuration.- Parameters:
rootDir- root directory for all operations (nullmeans CWD)virtualMode- when true, all paths are anchored to rootDir and traversal is blockedmaxFileSizeMb- maximum file size in megabytes for search operations
-
LocalFilesystem
Same asLocalFilesystem(Path, boolean, int)with a path string; seeLocalFilesystem(String)fornull/ blank rules. -
LocalFilesystem
public LocalFilesystem(Path rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory) Legacy constructor: mapsvirtualModetoLocalFsMode.SANDBOXEDorLocalFsMode.UNRESTRICTEDand uses an emptyPathPolicy. Prefer the mode-aware constructor below when you needLocalFsMode.ROOTED.- Parameters:
rootDir- root directory for all operations (nullmeans CWD)virtualMode- when true, all paths are anchored to rootDir and traversal is blockedmaxFileSizeMb- maximum file size in megabytes for search operationsnamespaceFactory- optional namespace factory for path scoping (nullfor none)
-
LocalFilesystem
public LocalFilesystem(String rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory) Same asLocalFilesystem(Path, boolean, int, NamespaceFactory)with a path string; seeLocalFilesystem(String)fornull/ blank rules. -
LocalFilesystem
public LocalFilesystem(Path rootDir, LocalFsMode mode, PathPolicy pathPolicy, int maxFileSizeMb, NamespaceFactory namespaceFactory) Creates a filesystem with explicit mode and path policy.The
modecontrols how the agent's absolute paths are validated; seeLocalFsModefor the three options. InLocalFsMode.ROOTEDmode, an absolute path is accepted only when it falls under one of thepathPolicyroots or underrootDiritself; inLocalFsMode.SANDBOXEDevery path is anchored torootDir; inLocalFsMode.UNRESTRICTEDabsolute paths pass through unchanged.- Parameters:
rootDir- root directory for relative paths (nullmeans CWD)mode- path-resolution policy (nulldefaults toLocalFsMode.UNRESTRICTED)pathPolicy- allow-list forLocalFsMode.ROOTED; ignored otherwise (nulltreated as empty)maxFileSizeMb- maximum file size in megabytes for search operationsnamespaceFactory- optional namespace factory for path scoping (nullfor none)
-
-
Method Details
-
getCwd
Returns the root directory for this filesystem. -
getMode
Returns the active path-resolution mode. -
getPathPolicy
Returns the configured path policy. Empty whengetMode()is notLocalFsMode.ROOTED; even with an empty policy,LocalFsMode.ROOTEDstill implicitly accepts paths undergetCwd(). -
ls
Description copied from interface:AbstractFilesystemList all files in a directory with metadata.- Specified by:
lsin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepath- absolute path to the directory to list (must start with '/')- Returns:
- LsResult with directory entries or error
-
read
public ReadResult read(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, int offset, int limit) Description copied from interface:AbstractFilesystemRead file content with optional line-based pagination.- Specified by:
readin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonefilePath- absolute path to the file to read (must start with '/')offset- line number to start reading from (0-indexed). Default: 0limit- maximum number of lines to read. Default: 2000- Returns:
- ReadResult with file data on success or error on failure
-
write
public WriteResult write(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, String content) Description copied from interface:AbstractFilesystemWrite content to a new file, error if file already exists.- Specified by:
writein interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonefilePath- absolute path where the file should be createdcontent- string content to write to the file- Returns:
- WriteResult with path on success, or error if the file already exists or write fails
-
edit
public EditResult edit(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, String oldString, String newString, boolean replaceAll) Description copied from interface:AbstractFilesystemPerform exact string replacements in an existing file.- Specified by:
editin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonefilePath- absolute path to the file to editoldString- exact string to search for and replacenewString- string to replace oldString with (must be different from oldString)replaceAll- if true, replace all occurrences; if false, oldString must be unique- Returns:
- EditResult with path and occurrence count on success, or error on failure
-
grep
public GrepResult grep(io.agentscope.core.agent.RuntimeContext runtimeContext, String pattern, String path, String glob) Description copied from interface:AbstractFilesystemSearch for a literal text pattern in files.- Specified by:
grepin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepattern- literal string to search for (not regex)path- optional directory path to search in (null searches current working directory)glob- optional glob pattern to filter which files to search (e.g., "*.java")- Returns:
- GrepResult with matches or error
-
glob
public GlobResult glob(io.agentscope.core.agent.RuntimeContext runtimeContext, String pattern, String path) Description copied from interface:AbstractFilesystemFind files matching a glob pattern.- Specified by:
globin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepattern- glob pattern with wildcards to match file pathspath- base directory to search from (default: "/")- Returns:
- GlobResult with matching files or error
-
uploadFiles
public List<FileUploadResponse> uploadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<Map.Entry<String, byte[]>> files) Description copied from interface:AbstractFilesystemUpload multiple files.- Specified by:
uploadFilesin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonefiles- list of path-to-content mappings to upload- Returns:
- list of FileUploadResponse objects, one per input file (order matches input order)
-
downloadFiles
public List<FileDownloadResponse> downloadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<String> paths) Description copied from interface:AbstractFilesystemDownload multiple files.- Specified by:
downloadFilesin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepaths- list of file paths to download- Returns:
- list of FileDownloadResponse objects, one per input path (order matches input order)
-
delete
Description copied from interface:AbstractFilesystemDelete a file or directory (recursive for directories).Idempotent: deleting a path that does not exist is treated as success.
- Specified by:
deletein interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepath- absolute path to the file or directory to delete- Returns:
- WriteResult success when deleted (or already absent), failure on I/O error
-
move
public WriteResult move(io.agentscope.core.agent.RuntimeContext runtimeContext, String fromPath, String toPath) Description copied from interface:AbstractFilesystemMove (rename) a file or directory fromfromPathtotoPath.Implementations that span multiple stores (e.g.
CompositeFilesystem) may fall back to a read + write + delete sequence when source and destination live in different backend filesystems.- Specified by:
movein interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonefromPath- absolute source pathtoPath- absolute destination path- Returns:
- WriteResult success on completion, failure on I/O error or missing source
-
exists
Description copied from interface:AbstractFilesystemCheck whether a file or directory exists.Implementations may approximate this with a lightweight read probe where a dedicated
existsAPI is unavailable, but should avoid reading full file content.- Specified by:
existsin interfaceAbstractFilesystem- Parameters:
runtimeContext- per-call agent runtime;RuntimeContext.empty()when nonepath- absolute path to check- Returns:
trueif the path exists,falseotherwise
-
getNamespaceFactory
-
resolvePath
-
toVirtualPath
-