Skip to content

.NET: Support a message only AIContextProvider as an AIAgent Decorator - #4009

Merged
westey (westey-m) merged 6 commits into
microsoft:mainfrom
westey-m:context-provider-agent-decorator
Feb 19, 2026
Merged

.NET: Support a message only AIContextProvider as an AIAgent Decorator#4009
westey (westey-m) merged 6 commits into
microsoft:mainfrom
westey-m:context-provider-agent-decorator

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

Motivation and Context

#3958

Description

  • Introduce a new MessageAIContextProvider that only supports message context enrichment
  • Allow the MessageAIContextProvider to be used to decorate any AIAgent instance

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.

CopilotAI review requested due to automatic review settings February 17, 2026 18:31
@markwallace-microsoftMark Wallace (markwallace-microsoft) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs .NET Usage: [Issues, PRs], Target: .Net labels Feb 17, 2026
@github-actionsgithub-actionsBot changed the title Support a message only AIContextProvider as an AIAgent Decorator.NET: Support a message only AIContextProvider as an AIAgent DecoratorFeb 17, 2026

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request introduces MessageAIContextProvider, a new base class that enables message-only context enrichment for any AIAgent instance, addressing issue #3958. The implementation allows providers to enrich agent messages without requiring support for tools and instructions, making context providers more flexible and widely applicable.

Changes:

  • Introduced MessageAIContextProvider abstract base class that inherits from AIContextProvider and focuses solely on message enrichment
  • Added MessageAIContextProviderAgent decorator that wraps any AIAgent and invokes a pipeline of MessageAIContextProvider instances
  • Converted TextSearchProvider, ChatHistoryMemoryProvider, and Mem0Provider to inherit from MessageAIContextProvider
  • Added AIAgentBuilder.Use(MessageAIContextProvider[]) extension method for easy integration
  • Enhanced ProviderSessionState constructor validation with proper null and whitespace checks
  • Exposed ProvideInputMessageFilter and StoreInputMessageFilter as protected properties in AIContextProvider to support inheritance
  • Updated samples to demonstrate the new pattern

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
dotnet/src/Microsoft.Agents.AI.Abstractions/MessageAIContextProvider.csNew base class for message-only context providers with InvokingAsync and ProvideMessagesAsync methods
dotnet/src/Microsoft.Agents.AI/MessageAIContextProviderAgent.csDecorator implementation that enriches messages via provider pipeline before delegating to inner agent
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.csExposed filter functions as protected properties to support derived class access
dotnet/src/Microsoft.Agents.AI.Abstractions/ProviderSessionState{TState}.csAdded null and whitespace validation to constructor parameters
dotnet/src/Microsoft.Agents.AI/TextSearchProvider.csRefactored to inherit from MessageAIContextProvider with proper OnDemand validation
dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProvider.csRefactored to inherit from MessageAIContextProvider with proper OnDemand validation
dotnet/src/Microsoft.Agents.AI.Mem0/Mem0Provider.csSimplified to inherit from MessageAIContextProvider returning messages directly
dotnet/src/Microsoft.Agents.AI/AIAgentBuilder.csAdded Use extension method accepting MessageAIContextProvider array
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/MessageAIContextProviderTests.csComprehensive tests for MessageAIContextProvider base class functionality
dotnet/tests/Microsoft.Agents.AI.UnitTests/MessageAIContextProviderAgentTests.csTests for decorator pattern including pipeline behavior and error handling
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ProviderSessionStateTests.csConstructor validation tests
dotnet/tests/Microsoft.Agents.AI.UnitTests/Memory/ChatHistoryMemoryProviderTests.csMessageAIContextProvider integration tests for ChatHistoryMemoryProvider
dotnet/tests/Microsoft.Agents.AI.UnitTests/Data/TextSearchProviderTests.csMessageAIContextProvider integration tests for TextSearchProvider
dotnet/tests/Microsoft.Agents.AI.Mem0.UnitTests/Mem0ProviderTests.csMessageAIContextProvider integration tests for Mem0Provider
dotnet/samples/GettingStarted/Agents/Agent_Step20_AdditionalAIContext/Program.csUpdated sample to use MessageAIContextProvider pattern
dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware/Program.csAdded example demonstrating MessageAIContextProvider middleware
dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware/README.mdUpdated documentation to mention MessageAIContextProvider middleware

Comment threaddotnet/src/Microsoft.Agents.AI.Abstractions/MessageAIContextProvider.cs Outdated
Comment threaddotnet/src/Microsoft.Agents.AI/MessageAIContextProviderAgent.cs Outdated
Comment threaddotnet/src/Microsoft.Agents.AI.Abstractions/MessageAIContextProvider.cs Outdated
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentationUsage: [Issues, PRs], Target: documentation in the code base and learn docs.NETUsage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@westey-m@lokitoth@SergeyMenshykh@markwallace-microsoft