Skip to content

Commit 8f0a35f

Browse files
kshyjuCopilot
andauthored
.NET: Add durable workflow support (#4436)
* .NET: [Feature Branch] Add basic durable workflow support (#3648) * Add basic durable workflow support. * PR feedback fixes * Add conditional edge sample. * PR feedback fixes. * Minor cleanup. * Minor cleanup * Minor formatting improvements. * Improve comments/documentation on the execution flow. * .NET: [Feature Branch] Add Azure Functions hosting support for durable workflows (#3935) * Adding azure functions workflow support. * - PR feedback fixes. - Add example to demonstrate complex Object as payload. * rename instanceId to runId. * Use custom ITaskOrchestrator to run orchestrator function. * .NET: [Feature Branch] Adding support for events & shared state in durable workflows (#4020) * Adding support for events & shared state in durable workflows. * PR feedback fixes * PR feedback fixes. * Add YieldOutputAsync calls to 05_WorkflowEvents sample executors The integration test asserts that WorkflowOutputEvent is found in the stream, but the sample executors only used AddEventAsync for custom events and never called YieldOutputAsync. Since WorkflowOutputEvent is only emitted via explicit YieldOutputAsync calls, the assertion would fail. Added YieldOutputAsync to each executor to match the test expectation and demonstrate the API in the sample. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix deserialization to use shared serializer options. * PR feedback updates. * Sample cleanup * PR feedback fixes * Addressing PR review feedback for DurableStreamingWorkflowRun - Use -1 instead of 0 for taskId in TaskFailedException when task ID is not relevant. - Add [NotNullWhen(true)] to TryParseWorkflowResult out parameter following .NET TryXXX conventions. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: [Feature Branch] Add nested sub-workflow support for durable workflows (#4190) * .NET: [Feature Branch] Add nested sub-workflow support for durable workflows * fix readme path * Switch Orchestration output from string to DurableWorkflowResult. * PR feedback fixes * Minor cleanup based on PR feedback. * .NET: [Feature Branch] Add Human In the Loop support for durable workflows (#4358) * Add Azure Functions HITL workflow sample Add 06_WorkflowHITL Azure Functions sample demonstrating Human-in-the-Loop workflow support with HTTP endpoints for status checking and approval responses. The sample includes: - ExpenseReimbursement workflow with RequestPort for manager approval - Custom HTTP endpoint to check workflow status and pending approvals - Custom HTTP endpoint to send approval responses via RaiseEventAsync - demo.http file with step-by-step interaction examples * PR feedback fixes * Minor comment cleanup * Minor comment clReverted the `!context.IsReplaying` guards on `PendingEvents.Add`/`RemoveAll` and `SetCustomStatus` in `ExecuteRequestPortAsync`. The guards broke fan-out scenarios where parallel RequestPorts need to be discoverable after replay. `SetCustomStatus` is idempotent metadata that doesn't affect replay determinism.eanup * fix for PR feedback * PR feedback updates * Improvements to samples * Improvements to README * Update samples to use parallel request ports. * Unit tests * Introduce local variables to improve readability of Workflows.Workflows access patter * Use GitHub-style callouts and add PowerShell command variants in HITL sample README * Add changelog entries for durable workflow support (#4436) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump Microsoft.DurableTask.Worker to 1.19.1 to fix version downgrade Microsoft.Azure.Functions.Worker.Extensions.DurableTask 1.13.1 requires Microsoft.DurableTask.Worker >= 1.19.1 via its transitive dependency on Microsoft.DurableTask.Worker.Grpc 1.19.1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix broken markdown links in durable workflow sample READMEs - Create Workflow/README.md with environment setup docs - Fix ../README.md -> ../../README.md in ConsoleApps 01, 02, 03, 08 - Fix SubWorkflows relative path (3 levels -> 4 levels up) - Fix dead Durable Task Scheduler URL Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix build errors from main merge: Throw conflict, ExecuteAsync rename, GetNewSessionAsync rename - Remove InjectSharedThrow from DurableTask csproj (uses Workflows' internal Throw via InternalsVisibleTo) - Update ExecuteAsync -> ExecuteCoreAsync with WorkflowTelemetryContext.Disabled - Update GetNewSessionAsync -> CreateSessionAsync Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move durable workflow samples to 04-hosting/DurableWorkflows Aligns with main branch sample reorganization where durable samples live under 04-hosting/ (alongside DurableAgents/). - Move samples/Durable/Workflow/ -> samples/04-hosting/DurableWorkflows/ - Add Directory.Build.props matching DurableAgents pattern - Update slnx project paths - Update integration test sample paths - Update README cd paths and cross-references Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix build errors: remove duplicate base class members, update renamed APIs - Remove duplicate OutputLog, WriteInputAsync, CreateTestTimeoutCts, etc. from ConsoleAppSamplesValidation (already in SamplesValidationBase) - Update AddFanInEdge -> AddFanInBarrierEdge in workflow samples - Update GetNewSessionAsync -> CreateSessionAsync in workflow samples - Update SourceId -> ExecutorId (obsolete) in workflow samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix dotnet format issues: add UTF-8 BOM and remove unused using - Add UTF-8 BOM to 20 .cs files across DurableTask, AzureFunctions, unit tests, and workflow samples - Remove unnecessary using directive in 07_SubWorkflows/Executors.cs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix typo PaymentProcesser -> PaymentProcessor and garbled arrows in README Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix GetExecutorName to handle agent names with underscores Split on last underscore instead of first, and validate that the suffix is a 32-char hex string (sanitized GUID) before stripping it. This prevents truncation of agent names like 'my_agent' when the executor ID is 'my_agent_<guid>'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Align DurableTask.Client.AzureManaged to 1.19.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump DurableTask and Azure Functions extension package versions - DurableTask.* packages: 1.19.1 -> 1.22.0 - Functions.Worker.Extensions.DurableTask: 1.13.1 -> 1.16.0 - Functions.Worker.Extensions.DurableTask.AzureManaged: 1.0.1 -> 1.5.0 (telemetry bug fix) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Bump DurableTask SDK packages to 1.22.0 - DurableTask.Client: 1.19.1 -> 1.22.0 - DurableTask.Client.AzureManaged: 1.19.1 -> 1.22.0 - DurableTask.Worker: 1.19.1 -> 1.22.0 - DurableTask.Worker.AzureManaged: 1.19.1 -> 1.22.0 - Azure Functions extensions kept at original versions (1.13.1/1.0.1) due to host-side DurableTask.Core 3.7.0 incompatibility with newer extensions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update Microsoft.Azure.Functions.Worker.Extensions.DurableTask to "1.16.0" * Add the local.settings.json files to the sample which were previously ignored. This aligns with our other samples. * Increase timeout for tests as CI has them failing transiently. * increaset timeout value for azure functions integration tests. * Add YieldsOutput(string) to workflow shared state sample executors ValidateOrder and EnrichOrder call YieldOutputAsync with string messages, but only their TOutput (OrderDetails) was in the allowed yield types. This caused TargetInvocationException in the WorkflowSharedState sample validation integration test. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 052ba7b commit 8f0a35f

File tree

123 files changed

+12606
-598
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+12606
-598
lines changed

dotnet/Directory.Packages.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@
120120
<PackageVersion Include="Microsoft.Agents.ObjectModel.PowerFx" Version="2026.2.4.1" />
121121
<PackageVersion Include="Microsoft.PowerFx.Interpreter" Version="1.8.1" />
122122
<!-- Durable Task -->
123-
<PackageVersion Include="Microsoft.DurableTask.Client" Version="1.18.0" />
124-
<PackageVersion Include="Microsoft.DurableTask.Client.AzureManaged" Version="1.18.0" />
125-
<PackageVersion Include="Microsoft.DurableTask.Worker" Version="1.18.0" />
126-
<PackageVersion Include="Microsoft.DurableTask.Worker.AzureManaged" Version="1.18.0" />
123+
<PackageVersion Include="Microsoft.DurableTask.Client" Version="1.22.0" />
124+
<PackageVersion Include="Microsoft.DurableTask.Client.AzureManaged" Version="1.22.0" />
125+
<PackageVersion Include="Microsoft.DurableTask.Worker" Version="1.22.0" />
126+
<PackageVersion Include="Microsoft.DurableTask.Worker.AzureManaged" Version="1.22.0" />
127127
<!-- Azure Functions -->
128128
<PackageVersion Include="Microsoft.Azure.Functions.Worker" Version="2.50.0" />
129129
<PackageVersion Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.50.0" />
130-
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.11.0" />
130+
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.16.0" />
131131
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" Version="1.0.1" />
132132
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
133133
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.1.0" />

dotnet/agent-framework-dotnet.slnx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@
6161
<Folder Name="/Samples/02-agents/DeclarativeAgents/">
6262
<Project Path="samples/02-agents/DeclarativeAgents/ChatClient/DeclarativeChatClientAgents.csproj" />
6363
</Folder>
64+
<Folder Name="/Samples/04-hosting/DurableWorkflows/" />
65+
<Folder Name="/Samples/04-hosting/DurableWorkflows/ConsoleApps/">
66+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/01_SequentialWorkflow/01_SequentialWorkflow.csproj" />
67+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/02_ConcurrentWorkflow/02_ConcurrentWorkflow.csproj" />
68+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/03_ConditionalEdges/03_ConditionalEdges.csproj" />
69+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/04_WorkflowAndAgents/04_WorkflowAndAgents.csproj" />
70+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/05_WorkflowEvents/05_WorkflowEvents.csproj" />
71+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/06_WorkflowSharedState/06_WorkflowSharedState.csproj" />
72+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/07_SubWorkflows/07_SubWorkflows.csproj" />
73+
<Project Path="samples/04-hosting/DurableWorkflows/ConsoleApps/08_WorkflowHITL/08_WorkflowHITL.csproj" />
74+
</Folder>
75+
<Folder Name="/Samples/04-hosting/DurableWorkflows/AzureFunctions/">
76+
<Project Path="samples/04-hosting/DurableWorkflows/AzureFunctions/01_SequentialWorkflow/01_SequentialWorkflow.csproj" />
77+
<Project Path="samples/04-hosting/DurableWorkflows/AzureFunctions/02_ConcurrentWorkflow/02_ConcurrentWorkflow.csproj" />
78+
<Project Path="samples/04-hosting/DurableWorkflows/AzureFunctions/03_WorkflowHITL/03_WorkflowHITL.csproj" />
79+
</Folder>
80+
<Folder Name="/Samples/GettingStarted/">
81+
<File Path="samples/GettingStarted/README.md" />
82+
</Folder>
6483
<Folder Name="/Samples/02-agents/AGUI/">
6584
<File Path="samples/02-agents/AGUI/README.md" />
6685
</Folder>
@@ -520,4 +539,4 @@
520539
<Project Path="tests/Microsoft.Agents.AI.Workflows.Generators.UnitTests/Microsoft.Agents.AI.Workflows.Generators.UnitTests.csproj" />
521540
<Project Path="tests/Microsoft.Agents.AI.Workflows.UnitTests/Microsoft.Agents.AI.Workflows.UnitTests.csproj" />
522541
</Folder>
523-
</Solution>
542+
</Solution>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net10.0</TargetFrameworks>
4+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<!-- The Functions build tools don't like namespaces that start with a number -->
9+
<AssemblyName>SingleAgent</AssemblyName>
10+
<RootNamespace>SingleAgent</RootNamespace>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15+
</ItemGroup>
16+
17+
<!-- Azure Functions packages -->
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.Azure.Functions.Worker" />
20+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" />
21+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" />
22+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" />
23+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="Azure.AI.OpenAI" />
28+
<PackageReference Include="Azure.Identity" />
29+
</ItemGroup>
30+
31+
<!-- Local projects that should be switched to package references when using the sample outside of this MAF repo -->
32+
<!--
33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.Agents.AI.Hosting.AzureFunctions" />
35+
<PackageReference Include="Microsoft.Agents.AI.OpenAI" />
36+
</ItemGroup>
37+
-->
38+
<ItemGroup>
39+
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Hosting.AzureFunctions\Microsoft.Agents.AI.Hosting.AzureFunctions.csproj" />
40+
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
41+
</ItemGroup>
42+
</Project>
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Agents.AI.Workflows;
4+
5+
namespace SequentialWorkflow;
6+
7+
/// <summary>
8+
/// Looks up an order by its ID and return an Order object.
9+
/// </summary>
10+
internal sealed class OrderLookup() : Executor<string, Order>("OrderLookup")
11+
{
12+
public override async ValueTask<Order> HandleAsync(
13+
string message,
14+
IWorkflowContext context,
15+
CancellationToken cancellationToken = default)
16+
{
17+
Console.WriteLine();
18+
Console.ForegroundColor = ConsoleColor.Magenta;
19+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
20+
Console.WriteLine($"│ [Activity] OrderLookup: Starting lookup for order '{message}'");
21+
Console.ResetColor();
22+
23+
// Simulate database lookup with delay
24+
await Task.Delay(TimeSpan.FromMicroseconds(100), cancellationToken);
25+
26+
Order order = new(
27+
Id: message,
28+
OrderDate: DateTime.UtcNow.AddDays(-1),
29+
IsCancelled: false,
30+
Customer: new Customer(Name: "Jerry", Email: "jerry@example.com"));
31+
32+
Console.ForegroundColor = ConsoleColor.Magenta;
33+
Console.WriteLine($"│ [Activity] OrderLookup: Found order '{message}' for customer '{order.Customer.Name}'");
34+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
35+
Console.ResetColor();
36+
37+
return order;
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Cancels an order.
43+
/// </summary>
44+
internal sealed class OrderCancel() : Executor<Order, Order>("OrderCancel")
45+
{
46+
public override async ValueTask<Order> HandleAsync(
47+
Order message,
48+
IWorkflowContext context,
49+
CancellationToken cancellationToken = default)
50+
{
51+
Console.WriteLine();
52+
Console.ForegroundColor = ConsoleColor.Yellow;
53+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
54+
Console.WriteLine($"│ [Activity] OrderCancel: Starting cancellation for order '{message.Id}'");
55+
Console.ResetColor();
56+
57+
// Simulate a slow cancellation process (e.g., calling external payment system)
58+
for (int i = 1; i <= 3; i++)
59+
{
60+
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
61+
Console.ForegroundColor = ConsoleColor.DarkYellow;
62+
Console.WriteLine("│ [Activity] OrderCancel: Processing...");
63+
Console.ResetColor();
64+
}
65+
66+
Order cancelledOrder = message with { IsCancelled = true };
67+
68+
Console.ForegroundColor = ConsoleColor.Yellow;
69+
Console.WriteLine($"│ [Activity] OrderCancel: ✓ Order '{cancelledOrder.Id}' has been cancelled");
70+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
71+
Console.ResetColor();
72+
73+
return cancelledOrder;
74+
}
75+
}
76+
77+
/// <summary>
78+
/// Sends a cancellation confirmation email to the customer.
79+
/// </summary>
80+
internal sealed class SendEmail() : Executor<Order, string>("SendEmail")
81+
{
82+
public override ValueTask<string> HandleAsync(
83+
Order message,
84+
IWorkflowContext context,
85+
CancellationToken cancellationToken = default)
86+
{
87+
Console.WriteLine();
88+
Console.ForegroundColor = ConsoleColor.Cyan;
89+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
90+
Console.WriteLine($"│ [Activity] SendEmail: Sending email to '{message.Customer.Email}'...");
91+
Console.ResetColor();
92+
93+
string result = $"Cancellation email sent for order {message.Id} to {message.Customer.Email}.";
94+
95+
Console.ForegroundColor = ConsoleColor.Cyan;
96+
Console.WriteLine("│ [Activity] SendEmail: ✓ Email sent successfully!");
97+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
98+
Console.ResetColor();
99+
100+
return ValueTask.FromResult(result);
101+
}
102+
}
103+
104+
internal sealed record Order(string Id, DateTime OrderDate, bool IsCancelled, Customer Customer);
105+
106+
internal sealed record Customer(string Name, string Email);
107+
108+
/// <summary>
109+
/// Represents a batch cancellation request with multiple order IDs and a reason.
110+
/// This demonstrates using a complex typed object as workflow input.
111+
/// </summary>
112+
#pragma warning disable CA1812 // Instantiated via JSON deserialization at runtime
113+
internal sealed record BatchCancelRequest(string[] OrderIds, string Reason, bool NotifyCustomers);
114+
#pragma warning restore CA1812
115+
116+
/// <summary>
117+
/// Represents the result of processing a batch cancellation.
118+
/// </summary>
119+
internal sealed record BatchCancelResult(int TotalOrders, int CancelledCount, string Reason);
120+
121+
/// <summary>
122+
/// Generates a status report for an order.
123+
/// </summary>
124+
internal sealed class StatusReport() : Executor<Order, string>("StatusReport")
125+
{
126+
public override ValueTask<string> HandleAsync(
127+
Order message,
128+
IWorkflowContext context,
129+
CancellationToken cancellationToken = default)
130+
{
131+
Console.WriteLine();
132+
Console.ForegroundColor = ConsoleColor.Green;
133+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
134+
Console.WriteLine($"│ [Activity] StatusReport: Generating report for order '{message.Id}'");
135+
Console.ResetColor();
136+
137+
string status = message.IsCancelled ? "Cancelled" : "Active";
138+
string result = $"Order {message.Id} for {message.Customer.Name}: Status={status}, Date={message.OrderDate:yyyy-MM-dd}";
139+
140+
Console.ForegroundColor = ConsoleColor.Green;
141+
Console.WriteLine($"│ [Activity] StatusReport: ✓ {result}");
142+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
143+
Console.ResetColor();
144+
145+
return ValueTask.FromResult(result);
146+
}
147+
}
148+
149+
/// <summary>
150+
/// Processes a batch cancellation request. Accepts a complex <see cref="BatchCancelRequest"/> object
151+
/// as input, demonstrating how workflows can receive structured JSON input.
152+
/// </summary>
153+
internal sealed class BatchCancelProcessor() : Executor<BatchCancelRequest, BatchCancelResult>("BatchCancelProcessor")
154+
{
155+
public override async ValueTask<BatchCancelResult> HandleAsync(
156+
BatchCancelRequest message,
157+
IWorkflowContext context,
158+
CancellationToken cancellationToken = default)
159+
{
160+
Console.WriteLine();
161+
Console.ForegroundColor = ConsoleColor.Yellow;
162+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
163+
Console.WriteLine($"│ [Activity] BatchCancelProcessor: Processing {message.OrderIds.Length} orders");
164+
Console.WriteLine($"│ [Activity] BatchCancelProcessor: Reason: {message.Reason}");
165+
Console.WriteLine($"│ [Activity] BatchCancelProcessor: Notify customers: {message.NotifyCustomers}");
166+
Console.ResetColor();
167+
168+
// Simulate processing each order
169+
int cancelledCount = 0;
170+
foreach (string orderId in message.OrderIds)
171+
{
172+
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
173+
cancelledCount++;
174+
Console.ForegroundColor = ConsoleColor.DarkYellow;
175+
Console.WriteLine($"│ [Activity] BatchCancelProcessor: ✓ Cancelled order '{orderId}'");
176+
Console.ResetColor();
177+
}
178+
179+
BatchCancelResult result = new(message.OrderIds.Length, cancelledCount, message.Reason);
180+
181+
Console.ForegroundColor = ConsoleColor.Yellow;
182+
Console.WriteLine($"│ [Activity] BatchCancelProcessor: ✓ Batch complete: {cancelledCount}/{message.OrderIds.Length} cancelled");
183+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
184+
Console.ResetColor();
185+
186+
return result;
187+
}
188+
}
189+
190+
/// <summary>
191+
/// Generates a summary of the batch cancellation.
192+
/// </summary>
193+
internal sealed class BatchCancelSummary() : Executor<BatchCancelResult, string>("BatchCancelSummary")
194+
{
195+
public override ValueTask<string> HandleAsync(
196+
BatchCancelResult message,
197+
IWorkflowContext context,
198+
CancellationToken cancellationToken = default)
199+
{
200+
Console.WriteLine();
201+
Console.ForegroundColor = ConsoleColor.Cyan;
202+
Console.WriteLine("┌─────────────────────────────────────────────────────────────────┐");
203+
Console.WriteLine("│ [Activity] BatchCancelSummary: Generating summary");
204+
Console.ResetColor();
205+
206+
string result = $"Batch cancellation complete: {message.CancelledCount}/{message.TotalOrders} orders cancelled. Reason: {message.Reason}";
207+
208+
Console.ForegroundColor = ConsoleColor.Cyan;
209+
Console.WriteLine($"│ [Activity] BatchCancelSummary: ✓ {result}");
210+
Console.WriteLine("└─────────────────────────────────────────────────────────────────┘");
211+
Console.ResetColor();
212+
213+
return ValueTask.FromResult(result);
214+
}
215+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample demonstrates three workflows that share executors.
4+
// The CancelOrder workflow cancels an order and notifies the customer.
5+
// The OrderStatus workflow looks up an order and generates a status report.
6+
// The BatchCancelOrders workflow accepts a complex JSON input to cancel multiple orders.
7+
// Both CancelOrder and OrderStatus reuse the same OrderLookup executor, demonstrating executor sharing.
8+
9+
using Microsoft.Agents.AI.Hosting.AzureFunctions;
10+
using Microsoft.Agents.AI.Workflows;
11+
using Microsoft.Azure.Functions.Worker.Builder;
12+
using Microsoft.Extensions.Hosting;
13+
using SequentialWorkflow;
14+
15+
// Define executors for all workflows
16+
OrderLookup orderLookup = new();
17+
OrderCancel orderCancel = new();
18+
SendEmail sendEmail = new();
19+
StatusReport statusReport = new();
20+
BatchCancelProcessor batchCancelProcessor = new();
21+
BatchCancelSummary batchCancelSummary = new();
22+
23+
// Build the CancelOrder workflow: OrderLookup -> OrderCancel -> SendEmail
24+
Workflow cancelOrder = new WorkflowBuilder(orderLookup)
25+
.WithName("CancelOrder")
26+
.WithDescription("Cancel an order and notify the customer")
27+
.AddEdge(orderLookup, orderCancel)
28+
.AddEdge(orderCancel, sendEmail)
29+
.Build();
30+
31+
// Build the OrderStatus workflow: OrderLookup -> StatusReport
32+
// This workflow shares the OrderLookup executor with the CancelOrder workflow.
33+
Workflow orderStatus = new WorkflowBuilder(orderLookup)
34+
.WithName("OrderStatus")
35+
.WithDescription("Look up an order and generate a status report")
36+
.AddEdge(orderLookup, statusReport)
37+
.Build();
38+
39+
// Build the BatchCancelOrders workflow: BatchCancelProcessor -> BatchCancelSummary
40+
// This workflow demonstrates using a complex JSON object as the workflow input.
41+
Workflow batchCancelOrders = new WorkflowBuilder(batchCancelProcessor)
42+
.WithName("BatchCancelOrders")
43+
.WithDescription("Cancel multiple orders in a batch using a complex JSON input")
44+
.AddEdge(batchCancelProcessor, batchCancelSummary)
45+
.Build();
46+
47+
using IHost app = FunctionsApplication
48+
.CreateBuilder(args)
49+
.ConfigureFunctionsWebApplication()
50+
.ConfigureDurableWorkflows(workflows => workflows.AddWorkflows(cancelOrder, orderStatus, batchCancelOrders))
51+
.Build();
52+
app.Run();

0 commit comments

Comments
 (0)