.NET: Porting Mem0Provider to Agent Framework (#1601)

* Porting Mem0Provider to AF from SK

* Switch integration tests to manual

* Address issues

* Move Mem0Provider to separate project.

* Move integration tests to new project

* Address PR comments.
This commit is contained in:
westey
2025-10-28 12:28:37 +00:00
committed by GitHub
parent 8408209c70
commit 742203fb12
12 changed files with 1061 additions and 1 deletions
@@ -0,0 +1,141 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Shared.IntegrationTests;
namespace Microsoft.Agents.AI.Mem0.IntegrationTests;
/// <summary>
/// Integration tests for <see cref="Mem0Provider"/> against a configured Mem0 service.
/// </summary>
public sealed class Mem0ProviderTests : IDisposable
{
private const string SkipReason = "Requires a Mem0 service configured"; // Set to null to enable.
private readonly HttpClient _httpClient;
public Mem0ProviderTests()
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets<Mem0ProviderTests>(optional: true)
.Build();
var mem0Settings = configuration.GetSection("Mem0").Get<Mem0Configuration>();
this._httpClient = new HttpClient();
if (mem0Settings is not null && !string.IsNullOrWhiteSpace(mem0Settings.ServiceUri) && !string.IsNullOrWhiteSpace(mem0Settings.ApiKey))
{
this._httpClient.BaseAddress = new Uri(mem0Settings.ServiceUri);
this._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", mem0Settings.ApiKey);
}
}
[Fact(Skip = SkipReason)]
public async Task CanAddAndRetrieveUserMemoriesAsync()
{
// Arrange
var question = new ChatMessage(ChatRole.User, "What is my name?");
var input = new ChatMessage(ChatRole.User, "Hello, my name is Caoimhe.");
var options = new Mem0ProviderOptions { ThreadId = "it-thread-1", UserId = "it-user-1" };
var sut = new Mem0Provider(this._httpClient, options);
await sut.ClearStoredMemoriesAsync();
var ctxBefore = await sut.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
Assert.DoesNotContain("Caoimhe", ctxBefore.Messages?[0].Text ?? string.Empty);
// Act
await sut.InvokedAsync(new AIContextProvider.InvokedContext(new[] { input }));
var ctxAfterAdding = await GetContextWithRetryAsync(sut, question);
await sut.ClearStoredMemoriesAsync();
var ctxAfterClearing = await sut.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
// Assert
Assert.Contains("Caoimhe", ctxAfterAdding.Messages?[0].Text ?? string.Empty);
Assert.DoesNotContain("Caoimhe", ctxAfterClearing.Messages?[0].Text ?? string.Empty);
}
[Fact(Skip = SkipReason)]
public async Task CanAddAndRetrieveAgentMemoriesAsync()
{
// Arrange
var question = new ChatMessage(ChatRole.User, "What is your name?");
var assistantIntro = new ChatMessage(ChatRole.Assistant, "Hello, I'm a friendly assistant and my name is Caoimhe.");
var options = new Mem0ProviderOptions { AgentId = "it-agent-1" };
var sut = new Mem0Provider(this._httpClient, options);
await sut.ClearStoredMemoriesAsync();
var ctxBefore = await sut.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
Assert.DoesNotContain("Caoimhe", ctxBefore.Messages?[0].Text ?? string.Empty);
// Act
await sut.InvokedAsync(new AIContextProvider.InvokedContext(new[] { assistantIntro }));
var ctxAfterAdding = await GetContextWithRetryAsync(sut, question);
await sut.ClearStoredMemoriesAsync();
var ctxAfterClearing = await sut.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
// Assert
Assert.Contains("Caoimhe", ctxAfterAdding.Messages?[0].Text ?? string.Empty);
Assert.DoesNotContain("Caoimhe", ctxAfterClearing.Messages?[0].Text ?? string.Empty);
}
[Fact(Skip = SkipReason)]
public async Task DoesNotLeakMemoriesAcrossAgentScopesAsync()
{
// Arrange
var question = new ChatMessage(ChatRole.User, "What is your name?");
var assistantIntro = new ChatMessage(ChatRole.Assistant, "I'm an AI tutor and my name is Caoimhe.");
var sut1 = new Mem0Provider(this._httpClient, new Mem0ProviderOptions { AgentId = "it-agent-a" });
var sut2 = new Mem0Provider(this._httpClient, new Mem0ProviderOptions { AgentId = "it-agent-b" });
await sut1.ClearStoredMemoriesAsync();
await sut2.ClearStoredMemoriesAsync();
var ctxBefore1 = await sut1.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
var ctxBefore2 = await sut2.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }));
Assert.DoesNotContain("Caoimhe", ctxBefore1.Messages?[0].Text ?? string.Empty);
Assert.DoesNotContain("Caoimhe", ctxBefore2.Messages?[0].Text ?? string.Empty);
// Act
await sut1.InvokedAsync(new AIContextProvider.InvokedContext(new[] { assistantIntro }));
var ctxAfterAdding1 = await GetContextWithRetryAsync(sut1, question);
var ctxAfterAdding2 = await GetContextWithRetryAsync(sut2, question);
// Assert
Assert.Contains("Caoimhe", ctxAfterAdding1.Messages?[0].Text ?? string.Empty);
Assert.DoesNotContain("Caoimhe", ctxAfterAdding2.Messages?[0].Text ?? string.Empty);
// Cleanup
await sut1.ClearStoredMemoriesAsync();
await sut2.ClearStoredMemoriesAsync();
}
private static async Task<AIContext> GetContextWithRetryAsync(Mem0Provider provider, ChatMessage question, int attempts = 5, int delayMs = 1000)
{
AIContext? ctx = null;
for (int i = 0; i < attempts; i++)
{
ctx = await provider.InvokingAsync(new AIContextProvider.InvokingContext(new[] { question }), CancellationToken.None);
var text = ctx.Messages?[0].Text;
if (!string.IsNullOrEmpty(text) && text.IndexOf("Caoimhe", StringComparison.OrdinalIgnoreCase) >= 0)
{
break;
}
await Task.Delay(delayMs);
}
return ctx!;
}
public void Dispose()
{
this._httpClient.Dispose();
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(ProjectsTargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)' == 'Debug'">$(ProjectsDebugTargetFrameworks)</TargetFrameworks>
<InjectSharedIntegrationTestCode>True</InjectSharedIntegrationTestCode>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.Mem0\Microsoft.Agents.AI.Mem0.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
</ItemGroup>
</Project>
@@ -0,0 +1,306 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging;
namespace Microsoft.Agents.AI.Mem0.UnitTests;
/// <summary>
/// Tests for <see cref="Mem0Provider"/>.
/// </summary>
public sealed class Mem0ProviderTests : IDisposable
{
private readonly RecordingHandler _handler = new();
private readonly HttpClient _httpClient;
private readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(b => b.AddProvider(new NullLoggerProvider()));
private bool _disposed;
public Mem0ProviderTests()
{
this._httpClient = new HttpClient(this._handler)
{
BaseAddress = new Uri("https://localhost/")
};
}
[Fact]
public void Constructor_Throws_WhenBaseAddressMissing()
{
// Arrange
using HttpClient client = new();
// Act & Assert
var ex = Assert.Throws<ArgumentException>(() => new Mem0Provider(client));
Assert.StartsWith("The HttpClient BaseAddress must be set for Mem0 operations.", ex.Message);
}
[Fact]
public void Constructor_Defaults_Scopes()
{
// Arrange & Act
var sut = new Mem0Provider(this._httpClient);
// Assert
Assert.Null(sut.ApplicationId);
Assert.Null(sut.AgentId);
Assert.Null(sut.ThreadId);
Assert.Null(sut.UserId);
}
[Fact]
public void DeserializingConstructor_Defaults_Scopes()
{
// Arrange & Act
var jsonElement = JsonSerializer.SerializeToElement(new object(), Mem0JsonUtilities.DefaultOptions);
var sut = new Mem0Provider(this._httpClient, jsonElement);
// Assert
Assert.Null(sut.ApplicationId);
Assert.Null(sut.AgentId);
Assert.Null(sut.ThreadId);
Assert.Null(sut.UserId);
}
[Fact]
public async Task InvokingAsync_PerformsSearch_AndReturnsContextMessageAsync()
{
// Arrange
this._handler.EnqueueJsonResponse("[ { \"id\": \"1\", \"memory\": \"Name is Caoimhe\", \"hash\": \"h\", \"metadata\": null, \"score\": 0.9, \"created_at\": \"2023-01-01T00:00:00Z\", \"updated_at\": null, \"user_id\": \"u\", \"app_id\": null, \"agent_id\": \"agent\", \"session_id\": \"thread\" } ]");
var options = new Mem0ProviderOptions
{
ApplicationId = "app",
AgentId = "agent",
ThreadId = "thread",
UserId = "user"
};
var sut = new Mem0Provider(this._httpClient, options, this._loggerFactory);
var invokingContext = new AIContextProvider.InvokingContext(new[] { new ChatMessage(ChatRole.User, "What is my name?") });
// Act
var aiContext = await sut.InvokingAsync(invokingContext);
// Assert
var searchRequest = Assert.Single(this._handler.Requests, r => r.RequestMessage.Method == HttpMethod.Post && r.RequestMessage.RequestUri!.AbsoluteUri.EndsWith("/v1/memories/search/", StringComparison.Ordinal));
using JsonDocument doc = JsonDocument.Parse(searchRequest.RequestBody);
Assert.Equal("app", doc.RootElement.GetProperty("app_id").GetString());
Assert.Equal("agent", doc.RootElement.GetProperty("agent_id").GetString());
Assert.Equal("thread", doc.RootElement.GetProperty("run_id").GetString());
Assert.Equal("user", doc.RootElement.GetProperty("user_id").GetString());
Assert.Equal("What is my name?", doc.RootElement.GetProperty("query").GetString());
Assert.NotNull(aiContext.Messages);
var contextMessage = Assert.Single(aiContext.Messages);
Assert.Equal(ChatRole.User, contextMessage.Role);
Assert.Contains("Name is Caoimhe", contextMessage.Text);
}
[Fact]
public async Task InvokedAsync_PersistsAllowedMessagesAsync()
{
// Arrange
this._handler.EnqueueEmptyOk(); // For first CreateMemory
this._handler.EnqueueEmptyOk(); // For second CreateMemory
this._handler.EnqueueEmptyOk(); // For third CreateMemory
var options = new Mem0ProviderOptions { ApplicationId = "a", AgentId = "b", ThreadId = "c", UserId = "d" };
var sut = new Mem0Provider(this._httpClient, options);
var requestMessages = new List<ChatMessage>
{
new(ChatRole.User, "User text"),
new(ChatRole.System, "System text"),
new(ChatRole.Tool, "Tool text should be ignored")
};
var responseMessages = new List<ChatMessage>
{
new(ChatRole.Assistant, "Assistant text")
};
// Act
await sut.InvokedAsync(new AIContextProvider.InvokedContext(requestMessages)); // persists request messages
await sut.InvokedAsync(new AIContextProvider.InvokedContext(responseMessages)); // persists assistant
// Assert
var memoryPosts = this._handler.Requests.Where(r => r.RequestMessage.RequestUri!.AbsolutePath == "/v1/memories/" && r.RequestMessage.Method == HttpMethod.Post).ToList();
Assert.Equal(3, memoryPosts.Count); // user, system, assistant
foreach (var req in memoryPosts)
{
Assert.Contains("\"messages\":[{", req.RequestBody);
}
Assert.DoesNotContain(memoryPosts, r => ContainsOrdinal(r.RequestBody, "Tool text"));
}
[Fact]
public async Task ClearStoredMemoriesAsync_SendsDeleteWithQueryAsync()
{
// Arrange
var options = new Mem0ProviderOptions { ApplicationId = "app", AgentId = "agent", ThreadId = "thread", UserId = "user" };
var sut = new Mem0Provider(this._httpClient, options);
this._handler.EnqueueEmptyOk(); // for DELETE
// Act
await sut.ClearStoredMemoriesAsync();
// Assert
var delete = Assert.Single(this._handler.Requests, r => r.RequestMessage.Method == HttpMethod.Delete);
Assert.Equal("https://localhost/v1/memories/?app_id=app&agent_id=agent&run_id=thread&user_id=user", delete.RequestMessage.RequestUri!.AbsoluteUri);
}
[Fact]
public void Properties_Roundtrip()
{
// Arrange
var options = new Mem0ProviderOptions { ApplicationId = "app", AgentId = "agent", ThreadId = "thread", UserId = "user" };
var sut = new Mem0Provider(this._httpClient, options);
// Assert
Assert.Equal("app", sut.ApplicationId);
Assert.Equal("agent", sut.AgentId);
Assert.Equal("thread", sut.ThreadId);
Assert.Equal("user", sut.UserId);
// Act
sut.ApplicationId = "app2";
sut.AgentId = "agent2";
sut.ThreadId = "thread2";
sut.UserId = "user2";
// Assert
Assert.Equal("app2", sut.ApplicationId);
Assert.Equal("agent2", sut.AgentId);
Assert.Equal("thread2", sut.ThreadId);
Assert.Equal("user2", sut.UserId);
}
[Fact]
public void Serialize_Deserialize_Roundtrips()
{
// Arrange
var options = new Mem0ProviderOptions { ApplicationId = "app", AgentId = "agent", ThreadId = "thread", UserId = "user" };
var sut = new Mem0Provider(this._httpClient, options);
// Act
var stateElement = sut.Serialize();
var sut2 = new Mem0Provider(this._httpClient, stateElement);
// Assert
Assert.Equal("app", sut.ApplicationId);
Assert.Equal("agent", sut.AgentId);
Assert.Equal("thread", sut.ThreadId);
Assert.Equal("user", sut.UserId);
Assert.Equal("app", sut2.ApplicationId);
Assert.Equal("agent", sut2.AgentId);
Assert.Equal("thread", sut2.ThreadId);
Assert.Equal("user", sut2.UserId);
}
[Fact]
public void Serialize_RoundTripsCustomContextPrompt()
{
// Arrange
var options = new Mem0ProviderOptions { ApplicationId = "app", AgentId = "agent", ThreadId = "thread", UserId = "user", ContextPrompt = "Custom:" };
var sut = new Mem0Provider(this._httpClient, options);
// Act
var stateElement = sut.Serialize();
using JsonDocument doc = JsonDocument.Parse(stateElement.GetRawText());
Assert.Equal("Custom:", doc.RootElement.GetProperty("contextPrompt").GetString());
var sut2 = new Mem0Provider(this._httpClient, stateElement);
var stateElement2 = sut2.Serialize();
// Assert
using JsonDocument doc2 = JsonDocument.Parse(stateElement2.GetRawText());
Assert.Equal("Custom:", doc2.RootElement.GetProperty("contextPrompt").GetString());
}
[Fact]
public void Serialize_DoesNotIncludeDefaultContextPrompt()
{
// Arrange
var options = new Mem0ProviderOptions { ApplicationId = "app" };
var sut = new Mem0Provider(this._httpClient, options);
// Act
var stateElement = sut.Serialize();
// Assert
using JsonDocument doc = JsonDocument.Parse(stateElement.GetRawText());
Assert.False(doc.RootElement.TryGetProperty("contextPrompt", out _));
}
[Fact]
public async Task InvokingAsync_Throws_WhenNoScopesAsync()
{
// Arrange
var sut = new Mem0Provider(this._httpClient, new Mem0ProviderOptions());
var ctx = new AIContextProvider.InvokingContext(new[] { new ChatMessage(ChatRole.User, "Test") });
// Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => sut.InvokingAsync(ctx).AsTask());
}
private static bool ContainsOrdinal(string source, string value) => source.IndexOf(value, StringComparison.Ordinal) >= 0;
public void Dispose()
{
if (!this._disposed)
{
this._httpClient.Dispose();
this._handler.Dispose();
this._loggerFactory.Dispose();
this._disposed = true;
}
}
private sealed class RecordingHandler : HttpMessageHandler
{
private readonly Queue<HttpResponseMessage> _responses = new();
public List<(HttpRequestMessage RequestMessage, string RequestBody)> Requests { get; } = new();
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
#if NET
var requestBody = await (request.Content?.ReadAsStringAsync(cancellationToken) ?? Task.FromResult(string.Empty));
#else
var requestBody = await (request.Content?.ReadAsStringAsync() ?? Task.FromResult(string.Empty));
#endif
this.Requests.Add((request, requestBody));
if (this._responses.Count > 0)
{
return this._responses.Dequeue();
}
return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
}
public void EnqueueJsonResponse(string json)
{
this._responses.Enqueue(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
});
}
public void EnqueueEmptyOk() => this._responses.Enqueue(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
}
private sealed class NullLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new NullLogger();
public void Dispose() { }
private sealed class NullLogger : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => false;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { }
}
}
}
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(ProjectsTargetFrameworks)</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.Mem0\Microsoft.Agents.AI.Mem0.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
</ItemGroup>
</Project>