From cbb05e210fdc6aac6ecdaa0e7a3bbb2d37612dbe Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Tue, 5 Aug 2025 10:08:45 +0100 Subject: [PATCH] .NET: Fix error for ChatClientAgent DI ambiguity (#299) * Fix error for ChatClientAgent DI ambiguity * Using AIAgent abstraction for DI samples * Update dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix warnings --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- dotnet/samples/GettingStarted/AgentSample.cs | 10 +++++----- .../Step04_ChatClientAgent_DependencyInjection.cs | 13 ++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dotnet/samples/GettingStarted/AgentSample.cs b/dotnet/samples/GettingStarted/AgentSample.cs index 5d1e11f2ff..57e2305c72 100644 --- a/dotnet/samples/GettingStarted/AgentSample.cs +++ b/dotnet/samples/GettingStarted/AgentSample.cs @@ -65,7 +65,7 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output) /// /// Ideally for faster execution and potential cost savings, server-side agents should be reused. /// - protected Task AgentCleanUpAsync(ChatClientProviders provider, ChatClientAgent agent, AgentThread? thread = null, CancellationToken cancellationToken = default) + protected Task AgentCleanUpAsync(ChatClientProviders provider, AIAgent agent, AgentThread? thread = null, CancellationToken cancellationToken = default) { return provider switch { @@ -157,9 +157,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output) #region Private AgentCleanUp - private async Task AzureAIAgentsPersistentAgentCleanUpAsync(ChatClientAgent agent, AgentThread? thread, CancellationToken cancellationToken) + private async Task AzureAIAgentsPersistentAgentCleanUpAsync(AIAgent agent, AgentThread? thread, CancellationToken cancellationToken) { - var persistentAgentsClient = agent.ChatClient.GetService() ?? + var persistentAgentsClient = (agent as ChatClientAgent)?.ChatClient.GetService() ?? throw new InvalidOperationException("The provided chat client is not a Persistent Agents Chat Client"); await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id, cancellationToken); @@ -171,9 +171,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output) } } - private async Task OpenAIAssistantCleanUpAgentAsync(ChatClientAgent agent, AgentThread? thread, CancellationToken cancellationToken) + private async Task OpenAIAssistantCleanUpAgentAsync(AIAgent agent, AgentThread? thread, CancellationToken cancellationToken) { - var assistantClient = agent.ChatClient + var assistantClient = (agent as ChatClientAgent)?.ChatClient .GetService() ?? throw new InvalidOperationException("The provided chat client is not an OpenAI Assistant Chat Client"); diff --git a/dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs b/dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs index 8d473ce23f..a793adf3c4 100644 --- a/dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs +++ b/dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.AI.Agents; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace Steps; @@ -23,6 +24,8 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper name: "Parrot", instructions: "Repeat the user message in the voice of a pirate and then end with a parrot sound."); + services.AddLogging(); + // Create the server-side agent Id when applicable (depending on the provider). agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions); @@ -30,18 +33,22 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper services.AddChatClient((sp) => base.GetChatClient(provider, sp.GetRequiredService())); - services.AddSingleton(); + services.AddSingleton((sp) + => new ChatClientAgent( + chatClient: sp.GetRequiredService(), + options: sp.GetRequiredService(), + loggerFactory: sp.GetRequiredService())); // Build the service provider. using var serviceProvider = services.BuildServiceProvider(); // Get the agent from the service provider. - var agent = serviceProvider.GetRequiredService(); + var agent = serviceProvider.GetRequiredService(); // Create the chat history thread to capture the agent interaction. var thread = agent.GetNewThread(); - Console.WriteLine($"Using chat client for provider: {agent.ChatClient.GetService()!.ProviderName}, for agent: {agent.Name}"); + Console.WriteLine($"Using chat client for provider: {provider}"); // Respond to user input, invoking functions where appropriate. await RunAgentAsync("Tell me a joke about a pirate.");