mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
628bb1af48
* Update Foundry Responses as ChatClientAgent * Migrate obsolete AzureAI integration tests to versioned agent pattern Replace obsolete CreateAIAgentAsync/GetAIAgentAsync calls with Agents.CreateAgentVersionAsync() + AsAIAgent(AgentVersion) in all AzureAI integration tests. - Rename AIProjectClient* test files to FoundryVersionedAgent* - Register AIFunction tools in PromptAgentDefinition.Tools for server-side visibility via AsOpenAIResponseTool() - Skip structured output tests (AzureAIProjectChatClient clears ResponseFormat for versioned agents) - Remove all [Obsolete] attributes and #pragma warning disable CS0618 * Merge FoundryMemory package into AzureAI under Memory/ folder Move all FoundryMemory source, unit tests, and integration tests into the Microsoft.Agents.AI.AzureAI package. Change namespace from Microsoft.Agents.AI.FoundryMemory to Microsoft.Agents.AI.AzureAI. - Add [Experimental] to FoundryMemoryProviderOptions and Scope - Rename internal AIProjectClientExtensions to MemoryStoreExtensions - Update AzureAI .csproj with Compliance.Abstractions, Redaction - Remove FoundryMemory from solution and release filter - Update sample to reference AzureAI instead of FoundryMemory - Delete old Microsoft.Agents.AI.FoundryMemory project and tests * Add EnsureMemoryStoreCreatedAsync and memory existence checks to integration tests - Ensure memory store is created before testing memory operations - Add AZURE_AI_EMBEDDING_DEPLOYMENT_NAME config setting - Assert memories exist in store via SearchMemoriesAsync before cleanup - Verify scope isolation with direct memory store queries * Fix and rename AzureAI unit tests for RAPI vs Versioned clarity - Rename AsAIAgentAsync_* to AsAIAgent_* (drop Async from method group) - Add _Rapi_ prefix to non-versioned (Responses API) tests - Add _Versioned_ prefix to versioned agent tests where needed - Fix RAPI tests: assert GetService<AIProjectClient>() is null - Fix Versioned tests: assert IsType<FoundryAgent> and GetService<AIProjectClient>() returns the client instance - Fix UserAgent header tests: proper HTTP handler routing - Fix ChatClient_UsesDefaultConversationIdAsync test setup - All 153 unit tests pass with 0 failures * Rename Microsoft.Agents.AI.AzureAI to Microsoft.Agents.AI.Foundry Rename the project, namespace, folder, and all references from Microsoft.Agents.AI.AzureAI to Microsoft.Agents.AI.Foundry. Also rename Workflows.Declarative.AzureAI to .Foundry. - Rename src, unit test, integration test, and workflow folders - Update namespaces in all source and test .cs files - Update ProjectReferences in ~47 sample and test .csproj files - Update solution files (.slnx, .slnf) - Update sample using statements - Update READMEs, SKILL.md, ADRs in docs/ - Disable package validation baseline for renamed packages - Fix UTF-8 BOM encoding on all affected .cs files - AzureAI.Persistent left completely unchanged * Fix format: remove ImplicitUsings, add explicit usings, fix BOM encoding - Remove ImplicitUsings=enable from Foundry csproj to resolve IDE0005 on shared ReplacingRedactor.cs - Add explicit System usings to all source files that relied on them - Sort usings alphabetically per editorconfig rules - Fix UTF-8 BOM on 12 sample Program.cs files - Rename Azure AI Foundry Agents to Microsoft Foundry Agents in docs
197 lines
6.7 KiB
C#
197 lines
6.7 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.ClientModel.Primitives;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Azure.AI.Projects;
|
|
using Azure.Core;
|
|
|
|
namespace Microsoft.Agents.AI.Foundry.UnitTests.Memory;
|
|
|
|
/// <summary>
|
|
/// Creates a testable AIProjectClient with a mock HTTP handler.
|
|
/// </summary>
|
|
internal sealed class TestableAIProjectClient : IDisposable
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public TestableAIProjectClient(
|
|
string? searchMemoriesResponse = null,
|
|
string? updateMemoriesResponse = null,
|
|
HttpStatusCode? searchStatusCode = null,
|
|
HttpStatusCode? updateStatusCode = null,
|
|
HttpStatusCode? deleteStatusCode = null,
|
|
HttpStatusCode? createStoreStatusCode = null,
|
|
HttpStatusCode? getStoreStatusCode = null)
|
|
{
|
|
this.Handler = new MockHttpMessageHandler(
|
|
searchMemoriesResponse,
|
|
updateMemoriesResponse,
|
|
searchStatusCode,
|
|
updateStatusCode,
|
|
deleteStatusCode,
|
|
createStoreStatusCode,
|
|
getStoreStatusCode);
|
|
|
|
this._httpClient = new HttpClient(this.Handler);
|
|
|
|
AIProjectClientOptions options = new()
|
|
{
|
|
Transport = new HttpClientPipelineTransport(this._httpClient)
|
|
};
|
|
|
|
// Using a valid format endpoint
|
|
this.Client = new AIProjectClient(
|
|
new Uri("https://test.services.ai.azure.com/api/projects/test-project"),
|
|
new MockTokenCredential(),
|
|
options);
|
|
}
|
|
|
|
public AIProjectClient Client { get; }
|
|
|
|
public MockHttpMessageHandler Handler { get; }
|
|
|
|
public void Dispose()
|
|
{
|
|
this._httpClient.Dispose();
|
|
this.Handler.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mock HTTP message handler for testing.
|
|
/// </summary>
|
|
internal sealed class MockHttpMessageHandler : HttpMessageHandler
|
|
{
|
|
private readonly string? _searchMemoriesResponse;
|
|
private readonly string? _updateMemoriesResponse;
|
|
private readonly HttpStatusCode _searchStatusCode;
|
|
private readonly HttpStatusCode _updateStatusCode;
|
|
private readonly HttpStatusCode _deleteStatusCode;
|
|
private readonly HttpStatusCode _createStoreStatusCode;
|
|
private readonly HttpStatusCode _getStoreStatusCode;
|
|
|
|
public MockHttpMessageHandler(
|
|
string? searchMemoriesResponse = null,
|
|
string? updateMemoriesResponse = null,
|
|
HttpStatusCode? searchStatusCode = null,
|
|
HttpStatusCode? updateStatusCode = null,
|
|
HttpStatusCode? deleteStatusCode = null,
|
|
HttpStatusCode? createStoreStatusCode = null,
|
|
HttpStatusCode? getStoreStatusCode = null)
|
|
{
|
|
this._searchMemoriesResponse = searchMemoriesResponse ?? """{"memories":[]}""";
|
|
this._updateMemoriesResponse = updateMemoriesResponse ?? """{"update_id":"test-update-id","status":"queued"}""";
|
|
this._searchStatusCode = searchStatusCode ?? HttpStatusCode.OK;
|
|
this._updateStatusCode = updateStatusCode ?? HttpStatusCode.OK;
|
|
this._deleteStatusCode = deleteStatusCode ?? HttpStatusCode.NoContent;
|
|
this._createStoreStatusCode = createStoreStatusCode ?? HttpStatusCode.Created;
|
|
this._getStoreStatusCode = getStoreStatusCode ?? HttpStatusCode.NotFound;
|
|
}
|
|
|
|
public string? LastRequestUri { get; private set; }
|
|
public string? LastRequestBody { get; private set; }
|
|
public HttpMethod? LastRequestMethod { get; private set; }
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
this.LastRequestUri = request.RequestUri?.ToString();
|
|
this.LastRequestMethod = request.Method;
|
|
|
|
if (request.Content != null)
|
|
{
|
|
#if NET472
|
|
this.LastRequestBody = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
#else
|
|
this.LastRequestBody = await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
|
#endif
|
|
}
|
|
|
|
string path = request.RequestUri?.AbsolutePath ?? "";
|
|
|
|
// Route based on path and method
|
|
if (path.Contains("/memory-stores/") && path.Contains("/search") && request.Method == HttpMethod.Post)
|
|
{
|
|
return CreateResponse(this._searchStatusCode, this._searchMemoriesResponse);
|
|
}
|
|
|
|
if (path.Contains("/memory-stores/") && path.Contains("/memories") && request.Method == HttpMethod.Post)
|
|
{
|
|
return CreateResponse(this._updateStatusCode, this._updateMemoriesResponse);
|
|
}
|
|
|
|
if (path.Contains("/memory-stores/") && path.Contains("/scopes") && request.Method == HttpMethod.Delete)
|
|
{
|
|
return CreateResponse(this._deleteStatusCode, "");
|
|
}
|
|
|
|
if (path.Contains("/memory-stores") && request.Method == HttpMethod.Post)
|
|
{
|
|
return CreateResponse(this._createStoreStatusCode, """{"name":"test-store","status":"active"}""");
|
|
}
|
|
|
|
if (path.Contains("/memory-stores/") && request.Method == HttpMethod.Get)
|
|
{
|
|
return CreateResponse(this._getStoreStatusCode, """{"name":"test-store","status":"active"}""");
|
|
}
|
|
|
|
// Default response
|
|
return CreateResponse(HttpStatusCode.NotFound, "{}");
|
|
}
|
|
|
|
private static HttpResponseMessage CreateResponse(HttpStatusCode statusCode, string? content)
|
|
{
|
|
return new HttpResponseMessage(statusCode)
|
|
{
|
|
Content = new StringContent(content ?? "{}", Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mock token credential for testing.
|
|
/// </summary>
|
|
internal sealed class MockTokenCredential : TokenCredential
|
|
{
|
|
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
|
|
{
|
|
return new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1));
|
|
}
|
|
|
|
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
|
|
{
|
|
return new ValueTask<AccessToken>(new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1)));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Source-generated JSON serializer context for unit test types.
|
|
/// </summary>
|
|
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
|
|
[JsonSerializable(typeof(TestState))]
|
|
[JsonSerializable(typeof(TestScope))]
|
|
internal sealed partial class TestJsonContext : JsonSerializerContext
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test state class for deserialization tests.
|
|
/// </summary>
|
|
internal sealed class TestState
|
|
{
|
|
public TestScope? Scope { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test scope class for deserialization tests.
|
|
/// </summary>
|
|
internal sealed class TestScope
|
|
{
|
|
public string? Scope { get; set; }
|
|
}
|