// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.AI;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows;
///
/// Provides utility methods for constructing common patterns of workflows composed of agents.
///
public static partial class AgentWorkflowBuilder
{
///
/// Builds a composed of a pipeline of agents where the output of one agent is the input to the next.
///
/// The sequence of agents to compose into a sequential workflow.
/// The built workflow composed of the supplied , in the order in which they were yielded from the source.
public static Workflow BuildSequential(params IEnumerable agents)
=> BuildSequentialCore(workflowName: null, agents);
///
/// Builds a composed of a pipeline of agents where the output of one agent is the input to the next.
///
/// The name of workflow.
/// The sequence of agents to compose into a sequential workflow.
/// The built workflow composed of the supplied , in the order in which they were yielded from the source.
public static Workflow BuildSequential(string workflowName, params IEnumerable agents)
=> BuildSequentialCore(workflowName, agents);
private static Workflow BuildSequentialCore(string? workflowName, params IEnumerable agents)
{
Throw.IfNullOrEmpty(agents);
SequentialWorkflowBuilder builder = new(agents);
if (workflowName is not null)
{
builder.WithName(workflowName);
}
return builder.Build();
}
///
/// Builds a composed of agents that operate concurrently on the same input,
/// aggregating their outputs into a single collection.
///
/// The set of agents to compose into a concurrent workflow.
///
/// The aggregation function that accepts a list of the output messages from each and produces
/// a single result list. If , the default behavior is to return a list containing the last message
/// from each agent that produced at least one message.
///
/// The built workflow composed of the supplied concurrent .
public static Workflow BuildConcurrent(
IEnumerable agents,
Func>, List>? aggregator = null)
=> BuildConcurrentCore(workflowName: null, agents, aggregator);
///
/// Builds a composed of agents that operate concurrently on the same input,
/// aggregating their outputs into a single collection.
///
/// The name of the workflow.
/// The set of agents to compose into a concurrent workflow.
///
/// The aggregation function that accepts a list of the output messages from each and produces
/// a single result list. If , the default behavior is to return a list containing the last message
/// from each agent that produced at least one message.
///
/// The built workflow composed of the supplied concurrent .
public static Workflow BuildConcurrent(
string workflowName,
IEnumerable agents,
Func>, List>? aggregator = null)
=> BuildConcurrentCore(workflowName, agents, aggregator);
private static Workflow BuildConcurrentCore(
string? workflowName,
IEnumerable agents,
Func>, List>? aggregator = null)
{
Throw.IfNull(agents);
ConcurrentWorkflowBuilder builder = new(agents);
if (workflowName is not null)
{
builder.WithName(workflowName);
}
if (aggregator is not null)
{
builder.WithAggregator(aggregator);
}
return builder.Build();
}
/// Creates a new using as the starting agent in the workflow.
/// The agent that will receive inputs provided to the workflow.
/// The builder for creating a workflow based on handoffs.
///
/// Handoffs between agents are achieved by the current agent invoking an provided to an agent
/// via 's ..
/// The must be capable of understanding those provided. If the agent
/// ignores the tools or is otherwise unable to advertize them to the underlying provider, handoffs will not occur.
///
public static HandoffWorkflowBuilder CreateHandoffBuilderWith(AIAgent initialAgent)
{
Throw.IfNull(initialAgent);
return new(initialAgent);
}
/// Creates a new with .
///
/// Function that will create the for the workflow instance. The manager will be
/// provided with the set of agents that will participate in the group chat.
///
/// The builder for creating a workflow based on handoffs.
///
/// Handoffs between agents are achieved by the current agent invoking an provided to an agent
/// via 's ..
/// The must be capable of understanding those provided. If the agent
/// ignores the tools or is otherwise unable to advertize them to the underlying provider, handoffs will not occur.
///
public static GroupChatWorkflowBuilder CreateGroupChatBuilderWith(Func, GroupChatManager> managerFactory)
{
Throw.IfNull(managerFactory);
return new GroupChatWorkflowBuilder(managerFactory);
}
/// Creates a new with the given pipeline of .
/// The sequence of agents to compose into a sequential workflow.
/// The builder for creating a sequential workflow.
public static SequentialWorkflowBuilder CreateSequentialBuilderWith(params IEnumerable agents)
{
Throw.IfNull(agents);
return new SequentialWorkflowBuilder(agents);
}
/// Creates a new with the given participating .
/// The set of agents to compose into a concurrent workflow.
/// The builder for creating a concurrent workflow.
public static ConcurrentWorkflowBuilder CreateConcurrentBuilderWith(params IEnumerable agents)
{
Throw.IfNull(agents);
return new ConcurrentWorkflowBuilder(agents);
}
/// Creates a new with the given .
/// The LLM-powered manager agent that coordinates the team.
/// The builder for creating a Magentic workflow.
public static MagenticWorkflowBuilder CreateMagenticBuilderWith(AIAgent managerAgent)
{
Throw.IfNull(managerAgent);
return new MagenticWorkflowBuilder(managerAgent);
}
}