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
112 lines
4.4 KiB
C#
112 lines
4.4 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 MyWorkflowRootExecutor<TInput>(
|
|
DeclarativeWorkflowOptions options,
|
|
Func<TInput, ChatMessage> inputTransform) :
|
|
RootExecutor<TInput>("my_workflow_Root", options, inputTransform)
|
|
where TInput : notnull
|
|
{
|
|
protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken)
|
|
{
|
|
// Set environment variables
|
|
await this.InitializeEnvironmentAsync(
|
|
context,
|
|
"MY_STUDENT").ConfigureAwait(false);
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invokes an agent to process messages and return a response within a conversation context.
|
|
/// </summary>
|
|
internal sealed class InvokeAgentExecutor(FormulaSession session, ResponseAgentProvider agentProvider) : AgentExecutor(id: "invoke_agent", session, agentProvider)
|
|
{
|
|
// <inheritdoc />
|
|
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
|
{
|
|
string? agentName = await context.ReadStateAsync<string>(key: "MY_STUDENT", scopeName: "Env").ConfigureAwait(false);
|
|
|
|
if (string.IsNullOrWhiteSpace(agentName))
|
|
{
|
|
throw new DeclarativeActionException($"Agent name must be defined: {this.Id}");
|
|
}
|
|
|
|
string? conversationId = await context.ReadStateAsync<string>(key: "ConversationId", scopeName: "System").ConfigureAwait(false);
|
|
bool autoSend = true;
|
|
IList<ChatMessage>? inputMessages = await context.EvaluateListAsync<ChatMessage>("[UserMessage(System.LastMessageText)]").ConfigureAwait(false);
|
|
|
|
AgentResponse agentResponse =
|
|
await InvokeAgentAsync(
|
|
context,
|
|
agentName,
|
|
conversationId,
|
|
autoSend,
|
|
inputMessages,
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
if (autoSend)
|
|
{
|
|
await context.AddEventAsync(new AgentResponseEvent(this.Id, agentResponse)).ConfigureAwait(false);
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|
|
|
|
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);
|
|
MyWorkflowRootExecutor<TInput> myWorkflowRoot = new(options, inputTransform);
|
|
DelegateExecutor myWorkflow = new(id: "my_workflow", myWorkflowRoot.Session);
|
|
InvokeAgentExecutor invokeAgent = new(myWorkflowRoot.Session, options.AgentProvider);
|
|
|
|
// Define the workflow builder
|
|
WorkflowBuilder builder = new(myWorkflowRoot);
|
|
|
|
// Connect executors
|
|
builder.AddEdge(myWorkflowRoot, myWorkflow);
|
|
builder.AddEdge(myWorkflow, invokeAgent);
|
|
|
|
// Build the workflow
|
|
return builder.Build(validateOrphans: false);
|
|
}
|
|
} |