.NET Workflows - Update structure of samples (#645)

* Updated

* Typos

* Update readme fwiw
This commit is contained in:
Chris
2025-09-08 23:16:29 +00:00
committed by GitHub
parent 726eb66982
commit 240edb00cd
29 changed files with 451 additions and 19 deletions
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.Workflows\Microsoft.Agents.Workflows.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading.Tasks;
using Microsoft.Agents.Workflows;
using Microsoft.Agents.Workflows.Reflection;
namespace WorkflowStreamingSample;
/// <summary>
/// This sample introduces streaming output in workflows.
///
/// While 01_Executors_And_Edges waits for the entire workflow to complete before showing results,
/// this example streams events back to you in real-time as each executor finishes processing.
/// This is useful for monitoring long-running workflows or providing live feedback to users.
///
/// The workflow logic is identical: uppercase text, then reverse it. The difference is in
/// how we observe the execution - we see intermediate results as they happen.
/// </summary>
public static class Program
{
private static async Task Main()
{
// Create the executors
UppercaseExecutor uppercase = new();
ReverseTextExecutor reverse = new();
// Build the workflow by connecting executors sequentially
WorkflowBuilder builder = new(uppercase);
builder.AddEdge(uppercase, reverse);
var workflow = builder.Build<string>();
// Execute the workflow in streaming mode
StreamingRun run = await InProcessExecution.StreamAsync(workflow, "Hello, World!");
await foreach (WorkflowEvent evt in run.WatchStreamAsync().ConfigureAwait(false))
{
if (evt is ExecutorCompleteEvent executorComplete)
{
Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}");
}
}
}
}
/// <summary>
/// First executor: converts input text to uppercase.
/// </summary>
internal sealed class UppercaseExecutor() : ReflectingExecutor<UppercaseExecutor>("UppercaseExecutor"), IMessageHandler<string, string>
{
/// <summary>
/// Processes the input message by converting it to uppercase.
/// </summary>
/// <param name="message">The input text to convert</param>
/// <param name="context">Workflow context for accessing workflow services and adding events</param>
/// <returns>The input text converted to uppercase</returns>
public async ValueTask<string> HandleAsync(string message, IWorkflowContext context)
{
string result = message.ToUpperInvariant();
// The return value will be sent as a message along an edge to subsequent executors
return result;
}
}
/// <summary>
/// Second executor: reverses the input text and completes the workflow.
/// </summary>
internal sealed class ReverseTextExecutor() : ReflectingExecutor<ReverseTextExecutor>("ReverseTextExecutor"), IMessageHandler<string, string>
{
/// <summary>
/// Processes the input message by reversing the text.
/// </summary>
/// <param name="message">The input text to reverse</param>
/// <param name="context">Workflow context for accessing workflow services and adding events</param>
/// <returns>The input text reversed</returns>
public async ValueTask<string> HandleAsync(string message, IWorkflowContext context)
{
char[] charArray = message.ToCharArray();
System.Array.Reverse(charArray);
string result = new(charArray);
// Signal that the workflow is complete
await context.AddEventAsync(new WorkflowCompletedEvent(result)).ConfigureAwait(false);
return result;
}
}