Skip to content

.NET: Add ChatClient decorator for calling AIContextProviders#4097

Open
westey-m wants to merge 2 commits intomicrosoft:mainfrom
westey-m:chatclient-aicontextprovider
Open

.NET: Add ChatClient decorator for calling AIContextProviders#4097
westey-m wants to merge 2 commits intomicrosoft:mainfrom
westey-m:chatclient-aicontextprovider

Conversation

@westey-m
Copy link
Contributor

Motivation and Context

#3957

Description

  • Add ChatClient decorator and ChatClientBuilder extension to allow calling AIContextProviders

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings February 19, 2026 19:47
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Feb 19, 2026
@github-actions github-actions bot changed the title Add ChatClient decorator for calling AIContextProviders .NET: Add ChatClient decorator for calling AIContextProviders Feb 19, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds decorator-based middleware to invoke AIContextProvider/MessageAIContextProvider pipelines, enabling context enrichment before chat/agent execution and provider notifications after completion, aligning with issue #3957’s request for an IChatClient-level decorator.

Changes:

  • Introduces AIContextProviderChatClient and a ChatClientBuilder.Use(...) extension to invoke AIContextProviders around IChatClient calls.
  • Introduces MessageAIContextProviderAgent to invoke MessageAIContextProviders around AIAgent runs (including streaming).
  • Adds unit tests and updates the Step14 middleware sample/docs to demonstrate the new pipeline.

Reviewed changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI/AIContextProviderDecorators/AIContextProviderChatClient.cs New delegating chat client that invokes AIContextProvider pipeline for non-streaming and streaming calls.
dotnet/src/Microsoft.Agents.AI/AIContextProviderDecorators/AIContextProviderChatClientBuilderExtensions.cs Adds ChatClientBuilder.Use(params AIContextProvider[]) extension to register the decorator.
dotnet/src/Microsoft.Agents.AI/AIContextProviderDecorators/MessageAIContextProviderAgent.cs New delegating agent that invokes MessageAIContextProvider pipeline around agent runs and streaming.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AIContextProviderDecorators/AIContextProviderChatClientTests.cs Unit tests covering enrichment, sequencing, success/failure notifications, and builder integration for chat client decorator.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AIContextProviderDecorators/MessageAIContextProviderAgentTests.cs Unit tests covering enrichment, sequencing, and success/failure notifications for agent decorator (incl. streaming).
dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware/Program.cs Sample updates to demonstrate using AIContextProvider in the IChatClient pipeline.
dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware/README.md Documentation update listing the new chat-client-level AIContextProvider middleware example.

Comment on lines +86 to +90
IAsyncEnumerator<ChatResponseUpdate> enumerator;
try
{
enumerator = base.GetStreamingResponseAsync(enrichedMessages, enrichedOptions, cancellationToken).GetAsyncEnumerator(cancellationToken);
}
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The streaming implementation creates an IAsyncEnumerator via GetAsyncEnumerator(...) but never disposes it. Consider using await using / try-finally to ensure DisposeAsync is called so underlying streaming resources are released promptly.

Copilot uses AI. Check for mistakes.
Comment on lines +114 to +123
// AIContextProvider at the chat client level. Unlike the agent-level MessageAIContextProvider,
// this operates within the IChatClient pipeline and can also enrich tools and instructions.
// It must be used within the context of a running AIAgent (uses AIAgent.CurrentRunContext).
Console.WriteLine("\n\n=== Example 6: AIContextProvider on chat client pipeline ===");

var chatClientProviderAgent = azureOpenAIClient.AsIChatClient()
.AsBuilder()
.Use(new DateTimeContextProvider())
.BuildAIAgent(
instructions: "You are an AI assistant that helps people find information.");
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example/comment says the chat-client-level AIContextProvider pipeline can enrich tools and instructions, but it wires up DateTimeContextProvider, which is a MessageAIContextProvider (defined later in this file) and only injects messages. Either adjust the wording to reflect message-only enrichment for this provider, or add a dedicated AIContextProvider implementation here that demonstrates tools/instructions enrichment so the sample matches the text.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments