// ------------------------------------------------------------------------------ // // This code was generated by a tool. // // ------------------------------------------------------------------------------ #nullable enable #pragma warning disable IDE0005 // Extra using directive is ok. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Workflows; using Microsoft.Agents.AI.Workflows.Declarative; using Microsoft.Agents.AI.Workflows.Declarative.Kit; using Microsoft.Extensions.AI; namespace Test.WorkflowProviders; /// /// This class provides a factory method to create a instance. /// /// /// The workflow defined here was generated from a declarative workflow definition. /// Declarative workflows utilize Power FX for defining conditions and expressions. /// To learn more about Power FX, see: /// https://learn.microsoft.com/power-platform/power-fx/formula-reference-copilot-studio /// public static class WorkflowProvider { /// /// The root executor for a declarative workflow. /// internal sealed class MyWorkflowRootExecutor( DeclarativeWorkflowOptions options, Func inputTransform) : RootExecutor("my_workflow_Root", options, inputTransform) where TInput : notnull { protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken) { // Initialize variables await context.QueueStateUpdateAsync("TestValue", UnassignedValue.Instance, "Local").ConfigureAwait(false); } } /// /// Assigns an evaluated expression, other variable, or literal value to the "Local.TestValue" variable. /// internal sealed class SetInputExecutor(FormulaSession session) : ActionExecutor(id: "set_input", session) { // protected override async ValueTask ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken) { object? evaluatedValue = await context.ReadStateAsync(key: "LastMessageText", scopeName: "System").ConfigureAwait(false); await context.QueueStateUpdateAsync(key: "TestValue", value: evaluatedValue, scopeName: "Local").ConfigureAwait(false); return default; } } /// /// Formats a message template and sends an activity event. /// internal sealed class ActivityInputExecutor(FormulaSession session) : ActionExecutor(id: "activity_input", session) { // protected override async ValueTask ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken) { string activityText = await context.FormatTemplateAsync( """ Input: "{Local.TestValue}" """ ); AgentResponse response = new([new ChatMessage(ChatRole.Assistant, activityText)]); await context.AddEventAsync(new AgentResponseEvent(this.Id, response)).ConfigureAwait(false); return default; } } public static Workflow CreateWorkflow( DeclarativeWorkflowOptions options, Func? inputTransform = null) where TInput : notnull { // Create root executor to initialize the workflow. inputTransform ??= (message) => DeclarativeWorkflowBuilder.DefaultTransform(message); MyWorkflowRootExecutor myWorkflowRoot = new(options, inputTransform); DelegateExecutor myWorkflow = new(id: "my_workflow", myWorkflowRoot.Session); SetInputExecutor setInput = new(myWorkflowRoot.Session); ActivityInputExecutor activityInput = new(myWorkflowRoot.Session); // Define the workflow builder WorkflowBuilder builder = new(myWorkflowRoot); // Connect executors builder.AddEdge(myWorkflowRoot, myWorkflow); builder.AddEdge(myWorkflow, setInput); builder.AddEdge(setInput, activityInput); // Build the workflow return builder.Build(validateOrphans: false); } }