mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
904a5b843e
* Python: .NET Samples - Restructure and Improve Samples (Feature Branch) (#4091) * Moved by agent (#4094) * Fix readme links * .NET Samples - Create `04-hosting` learning path step (#4098) * Agent move * Agent reorderd * Remove A2A section from README Removed A2A section from the Getting Started README. * Agent fixed links * Fix broken sample links in durable-agents README (#4101) * Initial plan * Fix broken internal links in documentation Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Revert template link changes; keep only durable-agents README fix Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * .NET Samples - Create `03-workflows` learning path step (#4102) * Fix solution project path * Python: Fix broken markdown links to repo resources (outside /docs) (#4105) * Initial plan * Fix broken markdown links to repo resources Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Update README to rename .NET Workflows Samples section --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * .NET Samples - Create `02-agents` learning path step (#4107) * .NET: Fix broken relative link in GroupChatToolApproval README (#4108) * Initial plan * Fix broken link in GroupChatToolApproval README Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Update labeler configuration for workflow samples * .NET - Reorder Agents samples to start from Step01 instead of Step04 (#4110) * Fix solution * Resolve new sample paths * Move new AgentSkills and AgentWithMemory_Step04 samples * Fix link * Fix readme path * fix: update stale dotnet/samples/Durable path reference in AGENTS.md Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Moved new sample * Update solution * Resolve merge (new sample) * Sync to new sample - FoundryAgents_Step21_BingCustomSearch * Updated README * .NET Samples - Configuration Naming Update (#4149) * .NET: Restore AzureFunctions index parity with ConsoleApps under DurableAgents samples (#4221) * Clean-up `05_host_your_agent` * Config setting consistency * Refine samples * AGENTS.md * Move new samples * Re-order samples * Move new project and fixup solution * Fixup model config * Fix up new UT project --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
140 lines
4.7 KiB
C#
140 lines
4.7 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Microsoft.Agents.AI.Workflows;
|
|
|
|
namespace WorkflowLoopSample;
|
|
|
|
/// <summary>
|
|
/// This sample demonstrates a simple number guessing game using a workflow with looping behavior.
|
|
///
|
|
/// The workflow consists of two executors that are connected in a feedback loop:
|
|
/// 1. GuessNumberExecutor: Makes a guess based on the current known bounds.
|
|
/// 2. JudgeExecutor: Evaluates the guess and provides feedback.
|
|
/// The workflow continues until the correct number is guessed.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Pre-requisites:
|
|
/// - Foundational samples should be completed first.
|
|
/// </remarks>
|
|
public static class Program
|
|
{
|
|
private static async Task Main()
|
|
{
|
|
// Create the executors
|
|
GuessNumberExecutor guessNumberExecutor = new("GuessNumber", 1, 100);
|
|
JudgeExecutor judgeExecutor = new("Judge", 42);
|
|
|
|
// Build the workflow by connecting executors in a loop
|
|
var workflow = new WorkflowBuilder(guessNumberExecutor)
|
|
.AddEdge(guessNumberExecutor, judgeExecutor)
|
|
.AddEdge(judgeExecutor, guessNumberExecutor)
|
|
.WithOutputFrom(judgeExecutor)
|
|
.Build();
|
|
|
|
// Execute the workflow
|
|
await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, NumberSignal.Init);
|
|
await foreach (WorkflowEvent evt in run.WatchStreamAsync())
|
|
{
|
|
if (evt is WorkflowOutputEvent outputEvent)
|
|
{
|
|
Console.WriteLine($"Result: {outputEvent}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signals used for communication between GuessNumberExecutor and JudgeExecutor.
|
|
/// </summary>
|
|
internal enum NumberSignal
|
|
{
|
|
Init,
|
|
Above,
|
|
Below,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executor that makes a guess based on the current bounds.
|
|
/// </summary>
|
|
internal sealed class GuessNumberExecutor : Executor<NumberSignal>
|
|
{
|
|
/// <summary>
|
|
/// The lower bound of the guessing range.
|
|
/// </summary>
|
|
public int LowerBound { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The upper bound of the guessing range.
|
|
/// </summary>
|
|
public int UpperBound { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GuessNumberExecutor"/> class.
|
|
/// </summary>
|
|
/// <param name="id">A unique identifier for the executor.</param>
|
|
/// <param name="lowerBound">The initial lower bound of the guessing range.</param>
|
|
/// <param name="upperBound">The initial upper bound of the guessing range.</param>
|
|
public GuessNumberExecutor(string id, int lowerBound, int upperBound) : base(id)
|
|
{
|
|
this.LowerBound = lowerBound;
|
|
this.UpperBound = upperBound;
|
|
}
|
|
|
|
private int NextGuess => (this.LowerBound + this.UpperBound) / 2;
|
|
|
|
public override async ValueTask HandleAsync(NumberSignal message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
switch (message)
|
|
{
|
|
case NumberSignal.Init:
|
|
await context.SendMessageAsync(this.NextGuess, cancellationToken: cancellationToken);
|
|
break;
|
|
case NumberSignal.Above:
|
|
this.UpperBound = this.NextGuess - 1;
|
|
await context.SendMessageAsync(this.NextGuess, cancellationToken: cancellationToken);
|
|
break;
|
|
case NumberSignal.Below:
|
|
this.LowerBound = this.NextGuess + 1;
|
|
await context.SendMessageAsync(this.NextGuess, cancellationToken: cancellationToken);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executor that judges the guess and provides feedback.
|
|
/// </summary>
|
|
internal sealed class JudgeExecutor : Executor<int>
|
|
{
|
|
private readonly int _targetNumber;
|
|
private int _tries;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="JudgeExecutor"/> class.
|
|
/// </summary>
|
|
/// <param name="id">A unique identifier for the executor.</param>
|
|
/// <param name="targetNumber">The number to be guessed.</param>
|
|
public JudgeExecutor(string id, int targetNumber) : base(id)
|
|
{
|
|
this._targetNumber = targetNumber;
|
|
}
|
|
|
|
public override async ValueTask HandleAsync(int message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
this._tries++;
|
|
if (message == this._targetNumber)
|
|
{
|
|
await context.YieldOutputAsync($"{this._targetNumber} found in {this._tries} tries!", cancellationToken)
|
|
;
|
|
}
|
|
else if (message < this._targetNumber)
|
|
{
|
|
await context.SendMessageAsync(NumberSignal.Below, cancellationToken: cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
await context.SendMessageAsync(NumberSignal.Above, cancellationToken: cancellationToken);
|
|
}
|
|
}
|
|
}
|