// ------------------------------------------------------------------------------ // // 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 WorkflowTestRootExecutor( DeclarativeWorkflowOptions options, Func inputTransform) : RootExecutor("workflow_test_Root", options, inputTransform) where TInput : notnull { protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken) { // Initialize variables await context.QueueStateUpdateAsync("AllMessages", UnassignedValue.Instance, "Local").ConfigureAwait(false); } } /// /// Retrieves a specific message from an agent conversation. /// internal sealed class GetMessagesAllExecutor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExecutor(id: "get_messages_all", session) { // protected override async ValueTask ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken) { string conversationId = await context.ReadStateAsync(key: "ConversationId", scopeName: "System").ConfigureAwait(false); int limit = 20; string? after = null; string? before = null; bool newestFirst = false; IAsyncEnumerable messagesResult = agentProvider.GetMessagesAsync( conversationId, limit, after, before, newestFirst, cancellationToken); List messages = []; await foreach (ChatMessage message in messagesResult.ConfigureAwait(false)) { messages.Add(message); } await context.QueueStateUpdateAsync(key: "AllMessages", value: messages, scopeName: "Local").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); WorkflowTestRootExecutor workflowTestRoot = new(options, inputTransform); DelegateExecutor workflowTest = new(id: "workflow_test", workflowTestRoot.Session); GetMessagesAllExecutor getMessagesAll = new(workflowTestRoot.Session, options.AgentProvider); // Define the workflow builder WorkflowBuilder builder = new(workflowTestRoot); // Connect executors builder.AddEdge(workflowTestRoot, workflowTest); builder.AddEdge(workflowTest, getMessagesAll); // Build the workflow return builder.Build(validateOrphans: false); } }