From c8a46d6219e4fcb8fd43cf53dd5c88a03ff4efb2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 13 May 2026 19:52:22 +0000 Subject: [PATCH] Apply TFM-specialized count check to ForwardMessage as well Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/9238ea32-a3e8-4b83-9683-484ad400071f Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com> --- .../WorkflowBuilderExtensions.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs index a771f89878..41cceb25a3 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs @@ -61,26 +61,23 @@ public static class WorkflowBuilderExtensions Throw.IfNull(targets); Func predicate = WorkflowBuilder.CreateConditionFunc(IsAllowedTypeAndMatchingCondition)!; - List targetList = new(); - foreach (ExecutorBinding target in targets) - { - Throw.IfNull(target, nameof(targets)); - targetList.Add(target); +#if NET6_0_OR_GREATER + if (targets.TryGetNonEnumeratedCount(out int count) && count == 1) +#else + if (targets is ICollection { Count: 1 }) +#endif + { + return builder.AddEdge(source, Throw.IfNull(targets.First(), nameof(targets)), predicate); } - Throw.IfNullOrEmpty(targetList, nameof(targets)); - - if (targetList.Count == 1) - { - return builder.AddEdge(source, targetList[0], predicate); - } - - return builder.AddSwitch(source, (switch_) => switch_.AddCase(predicate, targetList)); + return builder.AddSwitch(source, (switch_) => switch_.AddCase(predicate, targets.Select(ValidateTarget))); // The reason we can check for "not null" here is that CreateConditionFunc will do the correct unwrapping // logic for PortableValues. bool IsAllowedTypeAndMatchingCondition(TMessage? message) => message != null && (condition == null || condition(message)); + + ExecutorBinding ValidateTarget(ExecutorBinding target) => Throw.IfNull(target, nameof(targets)); } ///