|
| 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 | +} |
0 commit comments