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

* Add ability to mark the source of Agent request messages and use that for filtering

* Add support for source, in addition to source type, and add unit tests for automatic stamping

* Address PR comments.

* Add merge fixes

* Address PR comments
This commit is contained in:
westey
2026-02-09 16:53:01 +00:00
committed by GitHub
parent 390f93344c
commit ad0dac3c86
34 changed files with 1718 additions and 384 deletions
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI;
/// <summary>
/// Contains extension methods for <see cref="ChatMessage"/>
/// </summary>
public static class ChatMessageExtensions
{
/// <summary>
/// Gets the source of the provided <see cref="ChatMessage"/> in the context of messages passed into an agent run.
/// </summary>
/// <param name="message">The <see cref="ChatMessage"/> for which we need the source.</param>
/// <returns>An <see cref="AgentRequestMessageSourceType"/> value indicating the source of the <see cref="ChatMessage"/>. Defaults to <see
/// cref="AgentRequestMessageSourceType.External"/> if no explicit source is defined.</returns>
public static AgentRequestMessageSourceType GetAgentRequestMessageSource(this ChatMessage message)
{
if (message.AdditionalProperties?.TryGetValue(AgentRequestMessageSourceType.AdditionalPropertiesKey, out var source) is true && source is AgentRequestMessageSourceType typedSource)
{
return typedSource;
}
return AgentRequestMessageSourceType.External;
}
}