// Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.FileSystemGlobbing; using Microsoft.Shared.DiagnosticIds; namespace Microsoft.Agents.AI; /// /// Provides an abstract base class for file storage operations. /// /// /// /// All paths are relative to an implementation-defined root. Implementations may map these /// paths to a local file system, in-memory store, remote blob storage, or other mechanisms. /// /// /// Paths use forward slashes as separators and must not escape the root (e.g., via .. segments). /// It is up to each implementation to ensure that this is enforced. /// /// [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] public abstract class AgentFileStore { /// /// Writes content to a file, creating or overwriting it. /// /// The relative path of the file to write. /// The content to write to the file. /// A token to cancel the operation. /// A task representing the asynchronous operation. public abstract Task WriteFileAsync(string path, string content, CancellationToken cancellationToken = default); /// /// Reads the content of a file. /// /// The relative path of the file to read. /// A token to cancel the operation. /// The file content, or if the file does not exist. public abstract Task ReadFileAsync(string path, CancellationToken cancellationToken = default); /// /// Deletes a file. /// /// The relative path of the file to delete. /// A token to cancel the operation. /// if the file was deleted; if it did not exist. public abstract Task DeleteFileAsync(string path, CancellationToken cancellationToken = default); /// /// Lists files in a directory. /// /// The relative path of the directory to list. Use an empty string for the root. /// A token to cancel the operation. /// A list of file names in the specified directory (direct children only). public abstract Task> ListFilesAsync(string directory, CancellationToken cancellationToken = default); /// /// Lists the direct child subdirectories of a directory. /// /// The relative path of the directory to list. Use an empty string for the root. /// A token to cancel the operation. /// A list of subdirectory names in the specified directory (direct children only). public abstract Task> ListDirectoriesAsync(string directory, CancellationToken cancellationToken = default); /// /// Checks whether a file exists. /// /// The relative path of the file to check. /// A token to cancel the operation. /// if the file exists; otherwise, . public abstract Task FileExistsAsync(string path, CancellationToken cancellationToken = default); /// /// Searches for files whose content matches a regular expression pattern. /// /// The relative path of the directory to search. Use an empty string for the root. /// /// A regular expression pattern to match against file contents. The pattern is matched case-insensitively. /// For example, "error|warning" matches lines containing "error" or "warning". /// /// /// An optional glob pattern to filter which files are searched (e.g., "*.md", "research*"). /// When , all files are searched. /// Uses standard glob syntax from , matched against each file's path relative to /// . Use ** to match across subdirectories (e.g., "**/*.md"). /// /// /// When , all descendant files of are searched. /// When (default), only the direct children of are searched. /// /// A token to cancel the operation. /// /// A list of search results. Each result's is the matching file's /// path relative to . /// public abstract Task> SearchFilesAsync(string directory, string regexPattern, string? filePattern = null, bool recursive = false, CancellationToken cancellationToken = default); /// /// Ensures a directory exists, creating it if necessary. /// /// The relative path of the directory to create. /// A token to cancel the operation. /// A task representing the asynchronous operation. public abstract Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default); }