mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: feat: Implement message filtering to exclude non-portable content typ… (#5410)
* feat: Implement message filtering to exclude non-portable content types before forwarding Co-authored-by: Copilot <copilot@github.com> * Added unit tests to cover forwarded message filtering within AI Agent executors Co-authored-by: Copilot <copilot@github.com> * Update dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/AIAgentHostExecutor.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/AIAgentHostExecutor.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/AIAgentHostExecutor.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: Disable forwarding of incoming messages in AIAgentHostExecutor tests Co-authored-by: Copilot <copilot@github.com> --------- Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Jacob Alber <jaalber@microsoft.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
e7dc3b91f1
commit
162985f2a3
@@ -181,8 +181,16 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
|
||||
|
||||
AgentResponse response = await this.InvokeAgentAsync(filteredMessages, context, emitEvents, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await context.SendMessageAsync(response.Messages is List<ChatMessage> list ? list : response.Messages.ToList(), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
// Filter out server-side artifacts (reasoning tokens, web search calls, etc.)
|
||||
// that are internal to this agent. Forwarding them to other agents in the workflow
|
||||
// causes invalid request errors when the receiving agent uses the Responses API,
|
||||
// because these item types are not valid as input items.
|
||||
List<ChatMessage> forwardableMessages = FilterForwardableMessages(response.Messages).ToList();
|
||||
if (forwardableMessages.Count > 0)
|
||||
{
|
||||
await context.SendMessageAsync(forwardableMessages, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// If we have no outstanding requests, we can yield a turn token back to the workflow.
|
||||
if (!this.HasOutstandingRequests)
|
||||
@@ -241,4 +249,60 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Content types that represent meaningful conversational content portable across agents.
|
||||
/// Messages containing only content types not in this set (e.g. reasoning tokens, web search
|
||||
/// calls) are filtered out before forwarding, as they are output-only items that cause
|
||||
/// schema validation errors when sent as input to the Responses API.
|
||||
/// </summary>
|
||||
private static readonly HashSet<Type> s_forwardableContentTypes =
|
||||
[
|
||||
typeof(TextContent),
|
||||
typeof(DataContent),
|
||||
typeof(UriContent),
|
||||
typeof(FunctionCallContent),
|
||||
typeof(FunctionResultContent),
|
||||
typeof(ToolApprovalRequestContent),
|
||||
typeof(ToolApprovalResponseContent),
|
||||
typeof(HostedFileContent),
|
||||
typeof(ErrorContent),
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Filters response messages to only include those with portable conversational content,
|
||||
/// and strips <see cref="ChatMessage.RawRepresentation"/> so that provider-specific output
|
||||
/// items (e.g. <c>mcp_list_tools</c>, <c>reasoning</c>, <c>fabric_dataagent_preview_call</c>)
|
||||
/// are not round-tripped by the M.E.AI library when the messages are sent to another agent.
|
||||
/// </summary>
|
||||
private static List<ChatMessage> FilterForwardableMessages(IList<ChatMessage> messages)
|
||||
{
|
||||
List<ChatMessage> result = [];
|
||||
|
||||
foreach (ChatMessage message in messages)
|
||||
{
|
||||
// Extract only the content items that are portable across agents.
|
||||
List<AIContent> forwardableContents = message.Contents
|
||||
.Where(c => s_forwardableContentTypes.Any(t => t.IsAssignableFrom(c.GetType())))
|
||||
.ToList();
|
||||
|
||||
if (forwardableContents.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build a clean message without the provider-specific RawRepresentation,
|
||||
// which would otherwise cause the M.E.AI library to round-trip the original
|
||||
// output-only items (e.g. mcp_list_tools) as input to the next agent.
|
||||
result.Add(new ChatMessage(message.Role, forwardableContents)
|
||||
{
|
||||
AuthorName = message.AuthorName,
|
||||
MessageId = message.MessageId,
|
||||
CreatedAt = message.CreatedAt,
|
||||
AdditionalProperties = message.AdditionalProperties is null ? null : new(message.AdditionalProperties),
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user