Skip to content

Commit 023cb4f

Browse files
authored
Add Name and Description support for GroupChat workflow builder (#4334)
1 parent 0d6b9d6 commit 023cb4f

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

dotnet/samples/03-workflows/_StartHere/03_AgentWorkflowPatterns/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ await RunWorkflowAsync(
7272
await RunWorkflowAsync(
7373
AgentWorkflowBuilder.CreateGroupChatBuilderWith(agents => new RoundRobinGroupChatManager(agents) { MaximumIterationCount = 5 })
7474
.AddParticipants(from lang in (string[])["French", "Spanish", "English"] select GetTranslationAgent(lang, client))
75+
.WithName("Translation Round Robin Workflow")
76+
.WithDescription("A workflow where three translation agents take turns responding in a round-robin fashion.")
7577
.Build(),
7678
[new(ChatRole.User, "Hello, world!")]);
7779
break;

dotnet/src/Microsoft.Agents.AI.Workflows/GroupChatWorkflowBuilder.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public sealed class GroupChatWorkflowBuilder
1616
{
1717
private readonly Func<IReadOnlyList<AIAgent>, GroupChatManager> _managerFactory;
1818
private readonly HashSet<AIAgent> _participants = new(AIAgentIDEqualityComparer.Instance);
19+
private string _name = string.Empty;
20+
private string _description = string.Empty;
1921

2022
internal GroupChatWorkflowBuilder(Func<IReadOnlyList<AIAgent>, GroupChatManager> managerFactory) =>
2123
this._managerFactory = managerFactory;
@@ -42,6 +44,28 @@ public GroupChatWorkflowBuilder AddParticipants(params IEnumerable<AIAgent> agen
4244
return this;
4345
}
4446

47+
/// <summary>
48+
/// Sets the human-readable name for the workflow.
49+
/// </summary>
50+
/// <param name="name">The name of the workflow.</param>
51+
/// <returns>This instance of the <see cref="GroupChatWorkflowBuilder"/>.</returns>
52+
public GroupChatWorkflowBuilder WithName(string name)
53+
{
54+
this._name = name;
55+
return this;
56+
}
57+
58+
/// <summary>
59+
/// Sets the description for the workflow.
60+
/// </summary>
61+
/// <param name="description">The description of what the workflow does.</param>
62+
/// <returns>This instance of the <see cref="GroupChatWorkflowBuilder"/>.</returns>
63+
public GroupChatWorkflowBuilder WithDescription(string description)
64+
{
65+
this._description = description;
66+
return this;
67+
}
68+
4569
/// <summary>
4670
/// Builds a <see cref="Workflow"/> composed of agents that operate via group chat, with the next
4771
/// agent to process messages selected by the group chat manager.
@@ -65,6 +89,16 @@ public Workflow Build()
6589
ExecutorBinding host = groupChatHostFactory.BindExecutor(nameof(GroupChatHost));
6690
WorkflowBuilder builder = new(host);
6791

92+
if (!string.IsNullOrEmpty(this._name))
93+
{
94+
builder = builder.WithName(this._name);
95+
}
96+
97+
if (!string.IsNullOrEmpty(this._description))
98+
{
99+
builder = builder.WithDescription(this._description);
100+
}
101+
68102
foreach (var participant in agentMap.Values)
69103
{
70104
builder

dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AgentWorkflowBuilderTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,50 @@ public void GroupChatManager_MaximumIterationCount_Invalid_Throws()
8888
Assert.Equal(int.MaxValue, manager.MaximumIterationCount);
8989
}
9090

91+
[Fact]
92+
public void BuildGroupChat_WithNameAndDescription_SetsWorkflowNameAndDescription()
93+
{
94+
const string WorkflowName = "Test Group Chat";
95+
const string WorkflowDescription = "A test group chat workflow";
96+
97+
var workflow = AgentWorkflowBuilder
98+
.CreateGroupChatBuilderWith(agents => new RoundRobinGroupChatManager(agents) { MaximumIterationCount = 2 })
99+
.AddParticipants(new DoubleEchoAgent("agent1"), new DoubleEchoAgent("agent2"))
100+
.WithName(WorkflowName)
101+
.WithDescription(WorkflowDescription)
102+
.Build();
103+
104+
Assert.Equal(WorkflowName, workflow.Name);
105+
Assert.Equal(WorkflowDescription, workflow.Description);
106+
}
107+
108+
[Fact]
109+
public void BuildGroupChat_WithNameOnly_SetsWorkflowName()
110+
{
111+
const string WorkflowName = "Named Group Chat";
112+
113+
var workflow = AgentWorkflowBuilder
114+
.CreateGroupChatBuilderWith(agents => new RoundRobinGroupChatManager(agents) { MaximumIterationCount = 2 })
115+
.AddParticipants(new DoubleEchoAgent("agent1"))
116+
.WithName(WorkflowName)
117+
.Build();
118+
119+
Assert.Equal(WorkflowName, workflow.Name);
120+
Assert.Null(workflow.Description);
121+
}
122+
123+
[Fact]
124+
public void BuildGroupChat_WithoutNameOrDescription_DefaultsToNull()
125+
{
126+
var workflow = AgentWorkflowBuilder
127+
.CreateGroupChatBuilderWith(agents => new RoundRobinGroupChatManager(agents) { MaximumIterationCount = 2 })
128+
.AddParticipants(new DoubleEchoAgent("agent1"))
129+
.Build();
130+
131+
Assert.Null(workflow.Name);
132+
Assert.Null(workflow.Description);
133+
}
134+
91135
[Theory]
92136
[InlineData(1)]
93137
[InlineData(2)]

0 commit comments

Comments
 (0)