mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
77e90e6013
* Renamed with comments * Fix rename arcs * Integration tests
119 lines
4.8 KiB
C#
119 lines
4.8 KiB
C#
// ------------------------------------------------------------------------------
|
|
// <auto-generated>
|
|
// This code was generated by a tool.
|
|
// </auto-generated>
|
|
// ------------------------------------------------------------------------------
|
|
|
|
#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;
|
|
|
|
/// <summary>
|
|
/// This class provides a factory method to create a <see cref="Workflow" /> instance.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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
|
|
/// </remarks>
|
|
public static class WorkflowProvider
|
|
{
|
|
/// <summary>
|
|
/// The root executor for a declarative workflow.
|
|
/// </summary>
|
|
internal sealed class WorkflowTestRootExecutor<TInput>(
|
|
DeclarativeWorkflowOptions options,
|
|
Func<TInput, ChatMessage> inputTransform) :
|
|
RootExecutor<TInput>("workflow_test_Root", options, inputTransform)
|
|
where TInput : notnull
|
|
{
|
|
protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken)
|
|
{
|
|
// Initialize variables
|
|
await context.QueueStateUpdateAsync("MyMessage1", UnassignedValue.Instance, "Local").ConfigureAwait(false);
|
|
await context.QueueStateUpdateAsync("TestInput", UnassignedValue.Instance, "Local").ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new message to the specified agent conversation
|
|
/// </summary>
|
|
internal sealed class AddMessageExecutor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExecutor(id: "add_message", session)
|
|
{
|
|
// <inheritdoc />
|
|
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
|
{
|
|
string? conversationId = await context.ReadStateAsync<string>(key: "ConversationId", scopeName: "System").ConfigureAwait(false);
|
|
if (string.IsNullOrWhiteSpace(conversationId))
|
|
{
|
|
throw new DeclarativeActionException($"Conversation identifier must be defined: {this.Id}");
|
|
}
|
|
ChatMessage newMessage = new(ChatRole.User, await this.GetContentAsync(context).ConfigureAwait(false)) { AdditionalProperties = this.GetMetadata() };
|
|
newMessage = await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);
|
|
await context.QueueStateUpdateAsync(key: "MyMessage1", value: newMessage, scopeName: "Local").ConfigureAwait(false);
|
|
|
|
return default;
|
|
}
|
|
|
|
private async ValueTask<IList<AIContent>> GetContentAsync(IWorkflowContext context)
|
|
{
|
|
List<AIContent> content = [];
|
|
|
|
string contentValue1 =
|
|
await context.FormatTemplateAsync(
|
|
"""
|
|
{Local.TestInput}
|
|
""");
|
|
content.Add(new TextContent(contentValue1));
|
|
return content;
|
|
}
|
|
|
|
private AdditionalPropertiesDictionary? GetMetadata()
|
|
{
|
|
Dictionary<string, object?>? metadata = null;
|
|
|
|
if (metadata is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new AdditionalPropertiesDictionary(metadata);
|
|
}
|
|
}
|
|
|
|
public static Workflow CreateWorkflow<TInput>(
|
|
DeclarativeWorkflowOptions options,
|
|
Func<TInput, ChatMessage>? inputTransform = null)
|
|
where TInput : notnull
|
|
{
|
|
// Create root executor to initialize the workflow.
|
|
inputTransform ??= (message) => DeclarativeWorkflowBuilder.DefaultTransform(message);
|
|
WorkflowTestRootExecutor<TInput> workflowTestRoot = new(options, inputTransform);
|
|
DelegateExecutor workflowTest = new(id: "workflow_test", workflowTestRoot.Session);
|
|
AddMessageExecutor addMessage = new(workflowTestRoot.Session, options.AgentProvider);
|
|
|
|
// Define the workflow builder
|
|
WorkflowBuilder builder = new(workflowTestRoot);
|
|
|
|
// Connect executors
|
|
builder.AddEdge(workflowTestRoot, workflowTest);
|
|
builder.AddEdge(workflowTest, addMessage);
|
|
|
|
// Build the workflow
|
|
return builder.Build(validateOrphans: false);
|
|
}
|
|
} |