| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | using Microsoft.Agents.AI.Workflows; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework.Workflows; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods on <see cref="IAgentFactory"/> for building MAF handoff workflow topologies. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class AgentFactoryWorkflowExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builds a handoff workflow where <paramref name="initialAgent"/> routes to one of the |
| | | 13 | | /// <paramref name="handoffTargets"/> based on LLM-driven tool-call decisions. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// This is an ergonomic wrapper over the raw MAF handoff builder, which requires passing |
| | | 17 | | /// <paramref name="initialAgent"/> twice — once to <c>CreateHandoffBuilderWith</c> and again |
| | | 18 | | /// as the <c>from</c> argument in <c>WithHandoffs</c>. |
| | | 19 | | /// </remarks> |
| | | 20 | | public static Workflow BuildHandoffWorkflow( |
| | | 21 | | this IAgentFactory agentFactory, |
| | | 22 | | AIAgent initialAgent, |
| | | 23 | | params AIAgent[] handoffTargets) |
| | | 24 | | { |
| | 4 | 25 | | ArgumentNullException.ThrowIfNull(agentFactory); |
| | 4 | 26 | | ArgumentNullException.ThrowIfNull(initialAgent); |
| | 4 | 27 | | ArgumentNullException.ThrowIfNull(handoffTargets); |
| | | 28 | | |
| | 4 | 29 | | return agentFactory.BuildHandoffWorkflow( |
| | 4 | 30 | | initialAgent, |
| | 11 | 31 | | handoffTargets.Select(t => (t, (string?)null)).ToArray()); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Builds a handoff workflow where <paramref name="initialAgent"/> routes to one of the |
| | | 36 | | /// <paramref name="handoffTargets"/> based on LLM-driven tool-call decisions. |
| | | 37 | | /// Each target is paired with an optional routing reason provided to the LLM when deciding |
| | | 38 | | /// which agent to hand off to. Targets with a null or empty reason use the no-reason handoff. |
| | | 39 | | /// </summary> |
| | | 40 | | public static Workflow BuildHandoffWorkflow( |
| | | 41 | | this IAgentFactory agentFactory, |
| | | 42 | | AIAgent initialAgent, |
| | | 43 | | params (AIAgent Target, string? Reason)[] handoffTargets) |
| | | 44 | | { |
| | 6 | 45 | | ArgumentNullException.ThrowIfNull(agentFactory); |
| | 6 | 46 | | ArgumentNullException.ThrowIfNull(initialAgent); |
| | 6 | 47 | | ArgumentNullException.ThrowIfNull(handoffTargets); |
| | | 48 | | |
| | 6 | 49 | | var builder = AgentWorkflowBuilder.CreateHandoffBuilderWith(initialAgent); |
| | | 50 | | |
| | 6 | 51 | | var withoutReason = handoffTargets |
| | 11 | 52 | | .Where(t => string.IsNullOrEmpty(t.Reason)) |
| | 7 | 53 | | .Select(t => t.Target) |
| | 6 | 54 | | .ToArray(); |
| | | 55 | | |
| | 6 | 56 | | if (withoutReason.Length > 0) |
| | 4 | 57 | | builder.WithHandoffs(initialAgent, withoutReason); |
| | | 58 | | |
| | 31 | 59 | | foreach (var (target, reason) in handoffTargets.Where(t => !string.IsNullOrEmpty(t.Reason))) |
| | 4 | 60 | | builder.WithHandoff(initialAgent, target, reason!); |
| | | 61 | | |
| | 6 | 62 | | return builder.Build(); |
| | | 63 | | } |
| | | 64 | | } |