Skip to content

Commit 3a44f18

Browse files
committed
Add nested workflow AG-UI sample
Characterize duplicate local executor IDs from parallel subworkflows and the resulting active-step collision tracked by issue #7763. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 681114d3-3e0d-47f8-8b87-4e5873ecdd6d
1 parent 84e8314 commit 3a44f18

10 files changed

Lines changed: 242 additions & 0 deletions

File tree

‎dotnet/agent-framework-dotnet.slnx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
<Project Path="samples/02-agents/AGUI/Step09_WorkflowTools/Client/Client.csproj" />
140140
<Project Path="samples/02-agents/AGUI/Step09_WorkflowTools/Server/Server.csproj" />
141141
</Folder>
142+
<Folder Name="/Samples/02-agents/AGUI/Step10_WorkflowNested/">
143+
<Project Path="samples/02-agents/AGUI/Step10_WorkflowNested/Client/Client.csproj" />
144+
<Project Path="samples/02-agents/AGUI/Step10_WorkflowNested/Server/Server.csproj" />
145+
</Folder>
142146
<Folder Name="/Samples/02-agents/DevUI/">
143147
<File Path="samples/02-agents/DevUI/README.md" />
144148
<Project Path="samples/02-agents/DevUI/DevUI_Step01_BasicUsage/DevUI_Step01_BasicUsage.csproj" />

‎dotnet/samples/02-agents/AGUI/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ dotnet run --project Server --urls http://localhost:8888
229229
dotnet run --project Client
230230
```
231231

232+
### Step10_WorkflowNested
233+
234+
Two parallel subworkflows whose local executor IDs are both `Analyze`. The sample characterizes the
235+
currently ambiguous AG-UI step names tracked by issue #7763.
236+
237+
```bash
238+
cd Step10_WorkflowNested
239+
dotnet run --project Server --urls http://localhost:8888
240+
dotnet run --project Client
241+
```
242+
232243
The server runs on port 8888 by default.
233244

234245
#### Client (`Step05_StateManagement/Client`)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj" />
12+
<PackageReference Include="AGUI.Client" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using AGUI.Abstractions;
4+
using AGUI.Client;
5+
using Microsoft.Agents.AI;
6+
using Microsoft.Extensions.AI;
7+
8+
string serverUrl = Environment.GetEnvironmentVariable("AGUI_SERVER_URL") ?? "http://localhost:8888";
9+
using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(60) };
10+
AGUIChatClient chatClient = new(new(httpClient, serverUrl));
11+
AIAgent agent = chatClient.AsAIAgent(name: "workflow-client");
12+
AgentSession session = await agent.CreateSessionAsync();
13+
14+
try
15+
{
16+
await foreach (AgentResponseUpdate update in agent.RunStreamingAsync(
17+
new ChatMessage(ChatRole.User, "Review this proposal."),
18+
session))
19+
{
20+
switch (update.AsChatResponseUpdate().RawRepresentation)
21+
{
22+
case StepStartedEvent started:
23+
Console.WriteLine($"\n[Step started: {started.StepName}]");
24+
break;
25+
case StepFinishedEvent finished:
26+
Console.WriteLine($"\n[Step finished: {finished.StepName}]");
27+
break;
28+
}
29+
30+
foreach (TextContent text in update.Contents.OfType<TextContent>())
31+
{
32+
Console.Write(text.Text);
33+
}
34+
}
35+
}
36+
catch (InvalidOperationException exception)
37+
{
38+
Console.WriteLine($"\n[Known nested-step identity limitation (#7763): {exception.Message}]");
39+
}
40+
41+
Console.WriteLine();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Parallel Nested Workflows over AG-UI
2+
3+
This sample runs `SecurityPipeline` and `StylePipeline` as parallel subworkflows. Each subworkflow contains
4+
a locally scoped executor named `Analyze`.
5+
6+
The client currently receives duplicate `Analyze_Analyze` step names because nested executor lifecycle events
7+
do not carry their parent workflow scope. AG-UI rejects the second overlapping `STEP_STARTED` as already active.
8+
The client reports this known limitation, which is tracked by issue #7763.
9+
10+
## Run
11+
12+
```powershell
13+
dotnet run --project Server --urls http://localhost:8888
14+
dotnet run --project Client
15+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Collections.Generic;
4+
using System.Runtime.CompilerServices;
5+
using System.Text.Json;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.Agents.AI;
9+
using Microsoft.Agents.AI.Workflows;
10+
using Microsoft.Extensions.AI;
11+
12+
namespace AGUI.WorkflowNested;
13+
14+
/// <summary>
15+
/// Creates parallel nested workflows with duplicate local executor IDs.
16+
/// </summary>
17+
public static class NestedWorkflow
18+
{
19+
/// <summary>
20+
/// Creates the parent workflow.
21+
/// </summary>
22+
/// <returns>A parent workflow containing security and style subworkflows.</returns>
23+
public static Workflow Create()
24+
{
25+
ChatForwardingExecutor start = new("Start");
26+
ExecutorBinding security = CreateAnalysisWorkflow("Security").BindAsExecutor("SecurityPipeline");
27+
ExecutorBinding style = CreateAnalysisWorkflow("Style").BindAsExecutor("StylePipeline");
28+
29+
return new WorkflowBuilder(start)
30+
.AddFanOutEdge(start, [security, style])
31+
.WithOutputFrom(security, style)
32+
.Build();
33+
}
34+
35+
private static Workflow CreateAnalysisWorkflow(string analysisType)
36+
=> new SequentialWorkflowBuilder(new AnalysisAgent(analysisType)).Build();
37+
}
38+
39+
internal sealed class AnalysisAgent(string analysisType) : AIAgent
40+
{
41+
protected override string? IdCore => "Analyze";
42+
43+
public override string? Name => "Analyze";
44+
45+
protected override Task<AgentResponse> RunCoreAsync(
46+
IEnumerable<ChatMessage> messages,
47+
AgentSession? session = null,
48+
AgentRunOptions? options = null,
49+
CancellationToken cancellationToken = default)
50+
=> Task.FromResult(new AgentResponse(
51+
new ChatMessage(ChatRole.Assistant, $"{analysisType} analysis complete.")));
52+
53+
protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
54+
IEnumerable<ChatMessage> messages,
55+
AgentSession? session = null,
56+
AgentRunOptions? options = null,
57+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
58+
{
59+
await Task.Yield();
60+
yield return new AgentResponseUpdate(ChatRole.Assistant, $"{analysisType} analysis complete.")
61+
{
62+
MessageId = $"{analysisType}-message",
63+
ResponseId = $"{analysisType}-response",
64+
};
65+
}
66+
67+
protected override ValueTask<AgentSession> CreateSessionCoreAsync(CancellationToken cancellationToken = default)
68+
=> new(new AnalysisSession());
69+
70+
protected override ValueTask<JsonElement> SerializeSessionCoreAsync(
71+
AgentSession session,
72+
JsonSerializerOptions? jsonSerializerOptions = null,
73+
CancellationToken cancellationToken = default)
74+
=> new(JsonSerializer.SerializeToElement(new Dictionary<string, string>()));
75+
76+
protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(
77+
JsonElement serializedState,
78+
JsonSerializerOptions? jsonSerializerOptions = null,
79+
CancellationToken cancellationToken = default)
80+
=> new(new AnalysisSession());
81+
82+
private sealed class AnalysisSession : AgentSession;
83+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using AGUI.WorkflowNested;
4+
using Microsoft.Agents.AI;
5+
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
6+
using Microsoft.Agents.AI.Workflows;
7+
8+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
9+
builder.Services.AddHttpClient().AddLogging();
10+
builder.Services.AddAGUIServer();
11+
12+
AIAgent workflowAgent = NestedWorkflow.Create().AsAIAgent(
13+
name: "NestedWorkflow",
14+
includeWorkflowOutputsInResponse: true);
15+
16+
WebApplication app = builder.Build();
17+
app.MapAGUIServer("/", workflowAgent);
18+
await app.RunAsync();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Hosting.AGUI.AspNetCore\Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.csproj" />
12+
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

‎dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
Link="Workflows\Samples\FailingWorkflow.cs" />
3636
<Compile Include="..\..\samples\02-agents\AGUI\Step09_WorkflowTools\Server\ToolWorkflow.cs"
3737
Link="Workflows\Samples\ToolWorkflow.cs" />
38+
<Compile Include="..\..\samples\02-agents\AGUI\Step10_WorkflowNested\Server\NestedWorkflow.cs"
39+
Link="Workflows\Samples\NestedWorkflow.cs" />
3840
</ItemGroup>
3941

4042
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using AGUI.Abstractions;
8+
using AGUI.Client;
9+
using AGUI.WorkflowNested;
10+
using FluentAssertions;
11+
using Microsoft.Agents.AI.Workflows;
12+
using Microsoft.Extensions.AI;
13+
14+
namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests.Workflows;
15+
16+
public sealed class NestedWorkflowTests
17+
{
18+
[Fact]
19+
public async Task ClientRejectsDuplicateActiveLocalExecutorIdsFromParallelSubworkflowsAsync()
20+
{
21+
// Arrange
22+
Workflow workflow = NestedWorkflow.Create();
23+
await using WorkflowTestHost host = await WorkflowTestHost.StartAsync(
24+
workflow.AsAIAgent(name: "NestedWorkflow", includeWorkflowOutputsInResponse: true));
25+
AGUIChatClient chatClient = new(new(host.Client, ""));
26+
AIAgent clientAgent = chatClient.AsAIAgent(name: "client");
27+
AgentSession session = await clientAgent.CreateSessionAsync();
28+
29+
// Act
30+
Func<Task> act = async () => _ = await clientAgent
31+
.RunStreamingAsync(new ChatMessage(ChatRole.User, "start"), session)
32+
.ToListAsync();
33+
34+
// Assert
35+
await act.Should().ThrowAsync<InvalidOperationException>()
36+
.WithMessage("*Step \"Analyze_Analyze\" is already active*");
37+
}
38+
}

0 commit comments

Comments
 (0)