Skip to content

.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filtering - #3540

Merged
westey (westey-m) merged 9 commits into
microsoft:mainfrom
westey-m:agent-request-message-source
Feb 9, 2026
Merged

.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filtering#3540
westey (westey-m) merged 9 commits into
microsoft:mainfrom
westey-m:agent-request-message-source

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

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

  • Add AdditionalProperties value to ChatMessage to annotate where it came from for certain cases
  • Treat all chat history, ai context provider and user input as request messages, and filter using the new property.

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.

@markwallace-microsoftMark Wallace (markwallace-microsoft) added .NET Usage: [Issues, PRs], Target: .Net workflows Usage: [Issues, PRs], Target: Workflows labels Jan 30, 2026
@github-actionsgithub-actionsBot changed the title Add ability to mark the source of Agent request messages and use that for filtering.NET: Add ability to mark the source of Agent request messages and use that for filteringJan 30, 2026
@westey-m
westey (westey-m) marked this pull request as ready for review February 3, 2026 14:45
CopilotAI review requested due to automatic review settings February 3, 2026 14:45

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 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 AgentRequestMessageSource and ChatMessageExtensions.GetAgentRequestMessageSource to tag and retrieve the logical source of ChatMessage instances via AdditionalProperties.
  • Refactor AIContextProvider and ChatHistoryProvider to use public wrapper methods (InvokingAsync/InvokedAsync) delegating to new protected InvokingCoreAsync / InvokedCoreAsync, and update all implementations (Mem0, Cosmos, in-memory, workflow, samples) and tests accordingly.
  • Update ChatClientAgent and 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
FileDescription
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSource.csAdds a new sealed type and key for annotating ChatMessage source via AdditionalProperties.
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.csAdds an extension method to read the AgentRequestMessageSource from a ChatMessage, defaulting to External.
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.csIntroduces InvokingCoreAsync/InvokedCoreAsync and a wrapper that tags AI-context messages with AIContextProvider source.
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.csIntroduces InvokingCoreAsync/InvokedCoreAsync and a wrapper that tags returned history messages with ChatHistory source, and simplifies InvokedContext.
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderExtensions.csUpdates filters to work on the unified request-message list using AgentRequestMessageSource instead of separate AI-context collections.
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderMessageFilter.csAdjusts to new InvokingCoreAsync/InvokedCoreAsync pattern while preserving filtering behavior.
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.csUpdates core methods and switches to aggregating only request+response messages, relying on message-source metadata.
dotnet/src/Microsoft.Agents.AI/TextSearchProvider.csUses message-source metadata to include only external-request text in search input and recent-message tracking.
dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProvider.csFilters to external request messages when querying and storing vector-memory items.
dotnet/src/Microsoft.Agents.AI.Mem0/Mem0Provider.csFilters to external request messages for memory query/persist operations.
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.csMigrates to new core methods and stops treating AI-context messages as a separate collection.
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.csUpdates 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.csAdapts 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/*.csAdds 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.

Comment threaddotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.cs Outdated
Comment threaddotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSource.cs Outdated
@westey-mwestey (westey-m) changed the title .NET: Add ability to mark the source of Agent request messages and use that for filtering.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filteringFeb 4, 2026
@westey-mwestey (westey-m) mentioned this pull request Feb 4, 2026
61 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NETUsage: [Issues, PRs], Target: .NetworkflowsUsage: [Issues, PRs], Target: Workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@westey-m@rogerbarreto@SergeyMenshykh@markwallace-microsoft