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>
This commit is contained in:
copilot-swe-agent[bot]
2026-05-13 19:52:22 +00:00
committed by GitHub
Unverified
parent 27018d615d
commit c8a46d6219
@@ -61,26 +61,23 @@ public static class WorkflowBuilderExtensions
Throw.IfNull(targets);
Func<object?, bool> predicate = WorkflowBuilder.CreateConditionFunc<TMessage>(IsAllowedTypeAndMatchingCondition)!;
List<ExecutorBinding> 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<ExecutorBinding> { 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<T> 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));
}
/// <summary>