.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filtering#3540
Open
westey-m wants to merge 6 commits intomicrosoft:mainfrom
Open
.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filtering#3540westey-m wants to merge 6 commits intomicrosoft:mainfrom
westey-m wants to merge 6 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces explicit tracking of the source of agent request messages (external input, AI context provider, chat history), and updates the agent pipeline and providers to use this metadata for filtering and behavior.
Changes:
- Add
AgentRequestMessageSourceandChatMessageExtensions.GetAgentRequestMessageSourceto tag and retrieve the logical source ofChatMessageinstances viaAdditionalProperties. - Refactor
AIContextProviderandChatHistoryProviderto use public wrapper methods (InvokingAsync/InvokedAsync) delegating to new protectedInvokingCoreAsync/InvokedCoreAsync, and update all implementations (Mem0, Cosmos, in-memory, workflow, samples) and tests accordingly. - Update
ChatClientAgentand various providers (ChatHistoryMemoryProvider,TextSearchProvider, Mem0, ChatHistory filters) so they treat all messages uniformly as request messages, using the new source metadata to include or exclude messages from memory, search, and history operations.
Reviewed changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSource.cs |
Adds a new sealed type and key for annotating ChatMessage source via AdditionalProperties. |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.cs |
Adds an extension method to read the AgentRequestMessageSource from a ChatMessage, defaulting to External. |
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs |
Introduces InvokingCoreAsync/InvokedCoreAsync and a wrapper that tags AI-context messages with AIContextProvider source. |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs |
Introduces InvokingCoreAsync/InvokedCoreAsync and a wrapper that tags returned history messages with ChatHistory source, and simplifies InvokedContext. |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderExtensions.cs |
Updates filters to work on the unified request-message list using AgentRequestMessageSource instead of separate AI-context collections. |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderMessageFilter.cs |
Adjusts to new InvokingCoreAsync/InvokedCoreAsync pattern while preserving filtering behavior. |
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs |
Updates core methods and switches to aggregating only request+response messages, relying on message-source metadata. |
dotnet/src/Microsoft.Agents.AI/TextSearchProvider.cs |
Uses message-source metadata to include only external-request text in search input and recent-message tracking. |
dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProvider.cs |
Filters to external request messages when querying and storing vector-memory items. |
dotnet/src/Microsoft.Agents.AI.Mem0/Mem0Provider.cs |
Filters to external request messages for memory query/persist operations. |
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs |
Migrates to new core methods and stops treating AI-context messages as a separate collection. |
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs |
Updates the run pipeline to use unified input-message lists, adjusts notifications to context/history providers, and removes explicit AI-context/history message collections in InvokedContext. |
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowChatHistoryProvider.cs |
Adapts workflow chat history to new core methods and unified message model. |
dotnet/samples/... (multiple) |
Updates custom AIContextProvider and ChatHistoryProvider implementations in samples to override the new *CoreAsync methods and to work with the unified request-message model. |
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/*.cs |
Adds tests for AgentRequestMessageSource and ChatMessageExtensions, and updates existing tests to align with the new InvokedContext shape and message-source semantics. |
dotnet/tests/Microsoft.Agents.AI.UnitTests/* and *.Mem0*, *.CosmosNoSql* |
Adjusts tests to the new provider APIs and confirms that history, AI context, and memory providers behave correctly with the new source-tagging and filtering logic. |
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSource.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSource.cs
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRequestMessageSourceTypeTests.cs
Show resolved
Hide resolved
…s for automatic stamping
rogerbarreto
reviewed
Feb 4, 2026
Comment on lines
+81
to
+88
| if (message.AdditionalProperties != null | ||
| && message.AdditionalProperties.TryGetValue(AgentRequestMessageSourceType.AdditionalPropertiesKey, out var messageSourceType) | ||
| && messageSourceType is AgentRequestMessageSourceType typedMessageSourceType | ||
| && typedMessageSourceType == AgentRequestMessageSourceType.AIContextProvider | ||
| && message.AdditionalProperties.TryGetValue(AgentRequestMessageSource.AdditionalPropertiesKey, out var messageSource) | ||
| && messageSource is string typedMessageSource | ||
| && typedMessageSource == this._sourceName) | ||
| { |
Member
There was a problem hiding this comment.
This conditional deserves a Method or Comment for clarity 😊
SergeyMenshykh
approved these changes
Feb 4, 2026
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
This is addressing feedback that messages should be annotated with their source, rather than split by type when passed to the AIContextProvider and ChatHistoryProvider.
#2542
Description
Contribution Checklist