Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.6" />
<PackageVersion Include="Microsoft.Extensions.FileSystemGlobbing" Version="10.0.6" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public static string Format(FunctionCallContent call)
"ContinueTask" => FormatContinueTask(call),
"ClearCompletedTask" => FormatSingleId(call, "taskId"),

// File memory tools
"FileMemory_SaveFile" => FormatSaveFile(call),
"FileMemory_ReadFile" => FormatStringArg(call, "fileName"),
"FileMemory_DeleteFile" => FormatStringArg(call, "fileName"),
"FileMemory_ListFiles" => null,
"FileMemory_SearchFiles" => FormatSearchFiles(call),

// External tools
"web_search" => FormatStringArg(call, "query"),
"DownloadUri" => FormatStringArg(call, "uri"),
Expand Down Expand Up @@ -152,6 +159,36 @@ public static string Format(FunctionCallContent call)
: $"(task #{taskId.Value})";
}

private static string? FormatSaveFile(FunctionCallContent call)
{
string? fileName = GetString(call, "fileName");
string? description = GetString(call, "description");

if (fileName is null)
{
return null;
}

return string.IsNullOrEmpty(description)
? $"({fileName})"
: $"({fileName}, with description)";
}

private static string? FormatSearchFiles(FunctionCallContent call)
{
string? pattern = GetString(call, "regexPattern");
string? filePattern = GetString(call, "filePattern");

if (pattern is null)
{
return null;
}

return string.IsNullOrEmpty(filePattern)
? $"(/{pattern}/)"
: $"(/{pattern}/ in {filePattern})";
}

private static string? FormatStringArg(FunctionCallContent call, string paramName)
{
string? value = GetString(call, paramName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ This rule applies even when the answer seems obvious or the task seems small.
{
Name = "ResearchAgent",
Description = "A research assistant that plans and executes research tasks.",
AIContextProviders = [new TodoProvider(), new AgentModeProvider()],
AIContextProviders = [new TodoProvider(), new AgentModeProvider(), new FileMemoryProvider(new InMemoryAgentFileStore())],
ChatHistoryProvider = new InMemoryChatHistoryProvider(new InMemoryChatHistoryProviderOptions
{
ChatReducer = compactionStrategy.AsChatReducer(),
Expand Down
9 changes: 9 additions & 0 deletions dotnet/src/Microsoft.Agents.AI/AgentJsonUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ private static JsonSerializerOptions CreateDefaultOptions()
// AgentModeProvider types
[JsonSerializable(typeof(AgentModeState))]

// FileMemoryProvider types
[JsonSerializable(typeof(FileMemoryState))]
[JsonSerializable(typeof(FileSearchResult))]
[JsonSerializable(typeof(List<FileSearchResult>), TypeInfoPropertyName = "FileSearchResultList")]
[JsonSerializable(typeof(FileSearchMatch))]
[JsonSerializable(typeof(List<FileSearchMatch>), TypeInfoPropertyName = "FileSearchMatchList")]
[JsonSerializable(typeof(FileListEntry))]
[JsonSerializable(typeof(List<FileListEntry>), TypeInfoPropertyName = "FileListEntryList")]

[ExcludeFromCodeCoverage]
internal sealed partial class JsonContext : JsonSerializerContext;
}
128 changes: 128 additions & 0 deletions dotnet/src/Microsoft.Agents.AI/Harness/FileMemory/AgentFileStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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;

/// <summary>
/// Provides an abstract base class for file storage operations.
/// </summary>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// Paths use forward slashes as separators and must not escape the root (e.g., via <c>..</c> segments).
/// It is up to each implementation to ensure that this is enforced.
/// </para>
/// </remarks>
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
public abstract class AgentFileStore
{
/// <summary>
/// Writes content to a file, creating or overwriting it.
/// </summary>
/// <param name="path">The relative path of the file to write.</param>
/// <param name="content">The content to write to the file.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task WriteFileAsync(string path, string content, CancellationToken cancellationToken = default);

/// <summary>
/// Reads the content of a file.
/// </summary>
/// <param name="path">The relative path of the file to read.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>The file content, or <see langword="null"/> if the file does not exist.</returns>
public abstract Task<string?> ReadFileAsync(string path, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes a file.
/// </summary>
/// <param name="path">The relative path of the file to delete.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns><see langword="true"/> if the file was deleted; <see langword="false"/> if it did not exist.</returns>
public abstract Task<bool> DeleteFileAsync(string path, CancellationToken cancellationToken = default);

/// <summary>
/// Lists files in a directory.
/// </summary>
/// <param name="directory">The relative path of the directory to list. Use an empty string for the root.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A list of file names in the specified directory (direct children only).</returns>
public abstract Task<IReadOnlyList<string>> ListFilesAsync(string directory, CancellationToken cancellationToken = default);

/// <summary>
/// Checks whether a file exists.
/// </summary>
/// <param name="path">The relative path of the file to check.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns><see langword="true"/> if the file exists; otherwise, <see langword="false"/>.</returns>
public abstract Task<bool> FileExistsAsync(string path, CancellationToken cancellationToken = default);

/// <summary>
/// Searches for files whose content matches a regular expression pattern.
/// </summary>
/// <param name="directory">The relative path of the directory to search. Use an empty string for the root.</param>
/// <param name="regexPattern">
/// A regular expression pattern to match against file contents. The pattern is matched case-insensitively.
/// For example, <c>"error|warning"</c> matches lines containing "error" or "warning".
/// </param>
/// <param name="filePattern">
/// An optional glob pattern to filter which files are searched (e.g., <c>"*.md"</c>, <c>"research*"</c>).
/// When <see langword="null"/>, all files in the directory are searched.
/// Uses standard glob syntax from <see cref="Matcher"/>.
/// </param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A list of search results with matching file names, snippets, and matching lines.</returns>
public abstract Task<IReadOnlyList<FileSearchResult>> SearchFilesAsync(string directory, string regexPattern, string? filePattern = null, CancellationToken cancellationToken = default);
Comment thread
westey-m marked this conversation as resolved.

/// <summary>
/// Ensures a directory exists, creating it if necessary.
/// </summary>
/// <param name="path">The relative path of the directory to create.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a <see cref="Matcher"/> for the specified glob pattern. Use the returned instance
/// to test multiple file names without allocating a new matcher for each one.
/// </summary>
/// <param name="filePattern">
/// The glob pattern to match against (e.g., <c>"*.md"</c>, <c>"research*"</c>).
/// </param>
/// <returns>A <see cref="Matcher"/> configured with the specified pattern.</returns>
protected static Matcher CreateGlobMatcher(string filePattern)
{
var matcher = new Matcher(System.StringComparison.OrdinalIgnoreCase);
matcher.AddInclude(filePattern);
return matcher;
}

/// <summary>
/// Determines whether a file name matches a pre-built glob <see cref="Matcher"/>.
/// </summary>
/// <param name="fileName">The file name to test (not a full path — just the name).</param>
/// <param name="matcher">
/// A pre-built <see cref="Matcher"/> to test against.
/// When <see langword="null"/>, this method returns <see langword="true"/> for any file name.
/// </param>
/// <returns><see langword="true"/> if the file name matches the pattern or if the matcher is <see langword="null"/>; otherwise, <see langword="false"/>.</returns>
protected static bool MatchesGlob(string fileName, Matcher? matcher)
{
if (matcher is null)
{
return true;
}

PatternMatchingResult result = matcher.Match(fileName);
return result.HasMatches;
Comment thread
westey-m marked this conversation as resolved.
}
}
27 changes: 27 additions & 0 deletions dotnet/src/Microsoft.Agents.AI/Harness/FileMemory/FileListEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Agents.AI;

/// <summary>
/// Represents a file entry returned by the <see cref="FileMemoryProvider"/> list files tool,
/// containing the file name and an optional description.
/// </summary>
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
public sealed class FileListEntry
{
/// <summary>
/// Gets or sets the name of the file.
/// </summary>
[JsonPropertyName("fileName")]
public string FileName { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the description of the file, or <see langword="null"/> if no description is available.
/// </summary>
[JsonPropertyName("description")]
public string? Description { get; set; }
}
Loading
Loading