.NET: [BREAKING] Refactor providers to move common functionality to base#3900
Open
westey-m wants to merge 2 commits intomicrosoft:mainfrom
Open
.NET: [BREAKING] Refactor providers to move common functionality to base#3900westey-m wants to merge 2 commits intomicrosoft:mainfrom
westey-m wants to merge 2 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors provider classes to consolidate common functionality into base classes, introducing a template method pattern and generic state management. The refactoring moves filtering, state management, and source stamping logic from individual provider implementations into ChatHistoryProvider, AIContextProvider, and their new generic variants ChatHistoryProvider<TState> and AIContextProvider<TState>.
Changes:
- Introduced generic base classes
ChatHistoryProvider<TState>andAIContextProvider<TState>that consolidate state management (GetOrInitializeState, SaveState) with StateKey and JsonSerializerOptions handling - Refactored base provider classes to use template method pattern: sealed InvokingCoreAsync/InvokedCoreAsync methods now call overridable ProvideChatHistoryAsync/StoreChatHistoryAsync (for ChatHistoryProvider) and ProvideAIContextAsync/StoreAIContextAsync (for AIContextProvider)
- Moved message filtering logic to base class constructors with default implementations (ChatHistory excludes ChatHistory-sourced messages from storage; AIContext includes only External messages)
- Updated all concrete provider implementations and tests to work with the new base class structure
- Renamed
RetrievalOutputMessageFiltertoProvideOutputMessageFilterfor consistency
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider{TState}.cs | New generic base class providing typed state management for ChatHistoryProvider implementations |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider{TState}.cs | New generic base class providing typed state management for AIContextProvider implementations |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs | Refactored to template method pattern with ProvideChatHistoryAsync/StoreChatHistoryAsync, added constructor for filters |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs | Refactored to template method pattern with ProvideAIContextAsync/StoreAIContextAsync, added constructor for filters |
| dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs | Updated to extend ChatHistoryProvider and pass filters to base constructor |
| dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProviderOptions.cs | Renamed RetrievalOutputMessageFilter to ProvideOutputMessageFilter |
| dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs | Updated to extend ChatHistoryProvider, removed properties in favor of constructor parameters (breaking change) |
| dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowChatHistoryProvider.cs | Updated to extend ChatHistoryProvider, removed duplicate state management code |
| dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProvider.cs | Updated to extend AIContextProvider, removed duplicate state management and filter code |
| dotnet/src/Microsoft.Agents.AI/TextSearchProvider.cs | Updated to extend AIContextProvider, removed duplicate state and filter code, made State class public |
| dotnet/src/Microsoft.Agents.AI.Mem0/Mem0Provider.cs | Updated to extend AIContextProvider, moved state validation to wrapper function |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTests.cs | Added comprehensive tests for new base class functionality |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTStateTests.cs | New test file for ChatHistoryProvider functionality |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextProviderTests.cs | Added comprehensive tests for new base class functionality |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextProviderTStateTests.cs | New test file for AIContextProvider functionality |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/*.cs | Updated Mock instantiations to pass null parameters to new base class constructors |
| dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs | Updated to use constructor parameters instead of property setters, updated comment |
| dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyChatHistoryStorage/Program.cs | Updated VectorChatHistoryProvider to extend ChatHistoryProvider |
| dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs | Updated UserInfoMemory to extend AIContextProvider |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextProviderTStateTests.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextProviderTStateTests.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTStateTests.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTStateTests.cs
Outdated
Show resolved
Hide resolved
ee2cb5f to
e252d58
Compare
Co-Authored-By: Copilot <175728472+Copilot@users.noreply.github.com>
e252d58 to
46bb61a
Compare
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider{TState}.cs
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider{TState}.cs
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
All providers have common filtering, state bag access and source stamping, so moving common functionality to base classes.
Description
Contribution Checklist