mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
afd2739e38
* .NET: Surface x-ms-served-model header as ChatResponse.ModelId for Foundry agents Mirrors Python PR #5910. Adds an internal SCM PipelinePolicy that reads the x-ms-served-model HTTP response header on Azure OpenAI Responses calls and writes it into an AsyncLocal box. A DelegatingChatClient sits between OpenTelemetry and the MEAI OpenAIResponsesChatClient and overwrites ChatResponse.ModelId with the served snapshot so OTel spans report the actual model rather than the deployment alias. Wired through all AsAIAgent paths in Microsoft.Agents.AI.Foundry. * .NET: Fix line endings and BOM on ResponsesAgentServedModelTests * .NET: Address Copilot review on Foundry served-model PR - Restore previous ServedModelScope in finally to avoid AsyncLocal leak into caller execution context. - Make served-model integration test assertion robust to deployment names that already match the snapshot pattern. - Broaden UnitTests csproj comment to cover all conditional removals (net8.0+ requirement). * .NET: Split ServedModelTests into per-SUT files with regions Split the combined ServedModelTests.cs into one test class per SUT: - ServedModelScopeTests.cs (AsyncLocal carrier) - ServedModelPolicyTests.cs (SCM pipeline policy) - ServedModelChatClientTests.cs (delegating client, with regions for Non-streaming / Streaming / End-to-end) Shared helpers and fake clients moved into ServedModelTestHelpers.cs. Csproj net8.0+ exclusion list updated accordingly. * .NET: Consolidate served-model logic into FoundryChatClient Move x-ms-served-model header capture from the standalone ServedModelChatClient decorator directly into FoundryChatClient, eliminating a separate wrapper that had to be applied at every Foundry entry point via WireServedModel(). - Register ServedModelPolicy in FoundryChatClient constructors (alongside the existing AgentFrameworkUserAgentPolicy registration) - Add StrongBox push/read logic to FoundryChatClient.GetResponseAsync and GetStreamingResponseAsync - Delete ServedModelChatClient.cs and its unit tests - Remove WireServedModel() from FoundryAgent and AIProjectClientExtensions - Update ServedModelPolicy/Scope XML docs to reference FoundryChatClient - Simplify ServedModelTestHelpers to use FoundryChatClient directly Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
#pragma warning disable OPENAI001, MEAI001, MAAI001, SCME0001
|
|
|
|
namespace Microsoft.Agents.AI.Foundry.UnitTests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for <see cref="ServedModelPolicy"/>: the SCM pipeline policy that reads the
|
|
/// <c>x-ms-served-model</c> response header and writes it into the active
|
|
/// <see cref="ServedModelScope"/> box.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Tests drive the policy through a real OpenAI ResponsesClient SCM pipeline against a mock
|
|
/// HTTP handler so the policy executes in its production configuration.
|
|
/// </remarks>
|
|
public sealed class ServedModelPolicyTests
|
|
{
|
|
[Fact]
|
|
public void Instance_IsSingleton()
|
|
{
|
|
Assert.Same(ServedModelPolicy.Instance, ServedModelPolicy.Instance);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProcessAsync_HeaderPresent_SetsModelIdOnResponseAsync()
|
|
{
|
|
// Arrange
|
|
using var handler = new ServedModelTestHelpers.ServedModelHandler(ServedModelTestHelpers.MinimalResponseJson(), servedModel: "gpt-5-nano-2025-08-07");
|
|
IChatClient chatClient = ServedModelTestHelpers.CreateChatClientWithPolicy(handler);
|
|
|
|
// Act
|
|
var response = await chatClient.GetResponseAsync("hi");
|
|
|
|
// Assert
|
|
Assert.Equal("gpt-5-nano-2025-08-07", response.ModelId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProcessAsync_HeaderAbsent_PreservesModelIdFromBodyAsync()
|
|
{
|
|
// Arrange
|
|
using var handler = new ServedModelTestHelpers.ServedModelHandler(ServedModelTestHelpers.MinimalResponseJson(), servedModel: null);
|
|
IChatClient chatClient = ServedModelTestHelpers.CreateChatClientWithPolicy(handler);
|
|
|
|
// Act
|
|
var response = await chatClient.GetResponseAsync("hi");
|
|
|
|
// Assert: ModelId is the deployment alias from the JSON body ("fake").
|
|
Assert.Equal("fake", response.ModelId);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public async Task ProcessAsync_EmptyOrWhitespaceHeader_PreservesModelIdFromBodyAsync(string headerValue)
|
|
{
|
|
// Arrange
|
|
using var handler = new ServedModelTestHelpers.ServedModelHandler(ServedModelTestHelpers.MinimalResponseJson(), servedModel: headerValue);
|
|
IChatClient chatClient = ServedModelTestHelpers.CreateChatClientWithPolicy(handler);
|
|
|
|
// Act
|
|
var response = await chatClient.GetResponseAsync("hi");
|
|
|
|
// Assert: empty/whitespace header is rejected by the policy, ModelId stays as "fake".
|
|
Assert.Equal("fake", response.ModelId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProcessAsync_HeaderWithSurroundingWhitespace_TrimsValueAsync()
|
|
{
|
|
// Arrange
|
|
using var handler = new ServedModelTestHelpers.ServedModelHandler(ServedModelTestHelpers.MinimalResponseJson(), servedModel: " gpt-5-nano-2025-08-07 ");
|
|
IChatClient chatClient = ServedModelTestHelpers.CreateChatClientWithPolicy(handler);
|
|
|
|
// Act
|
|
var response = await chatClient.GetResponseAsync("hi");
|
|
|
|
// Assert
|
|
Assert.Equal("gpt-5-nano-2025-08-07", response.ModelId);
|
|
}
|
|
}
|