Uh oh!
There was an error while loading. Please reload this page.
.NET: [BREAKING] Refactor providers to move common functionality to base - #3900
Conversation
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 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ee2cb5f to
e252d58CompareCo-Authored-By: Copilot <175728472+Copilot@users.noreply.github.com>
e252d58 to
46bb61aCompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Motivation and Context
All providers have common filtering, state bag access and source stamping, so moving common functionality to base classes.
Description
Contribution Checklist