// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Workflows;
///
/// Provides extension methods for treating workflows as
///
public static class WorkflowHostingExtensions
{
///
/// Convert a workflow with the appropriate primary input type to an .
///
/// The workflow to be hosted by the resulting
/// A unique id for the hosting .
/// A name for the hosting .
/// A description for the hosting .
/// A to enable persistence of run state.
/// Specify the execution environment to use when running the workflows. See
/// , and
/// for the in-process environments.
///
public static AIAgent AsAgent(
this Workflow> workflow,
string? id = null,
string? name = null,
string? description = null,
CheckpointManager? checkpointManager = null,
IWorkflowExecutionEnvironment? executionEnvironment = null)
{
return new WorkflowHostAgent(workflow, id, name, description, checkpointManager, executionEnvironment);
}
///
/// Convert a workflow with the appropriate primary input type to an .
///
/// The workflow to be hosted by the resulting
/// A unique id for the hosting .
/// A name for the hosting .
/// /// A description for the hosting .
/// A to enable persistence of run state.
/// Specify the execution environment to use when running the workflows. See
/// , and
/// for the in-process environments.
///
public static async ValueTask AsAgentAsync(
this Workflow workflow,
string? id = null,
string? name = null,
string? description = null,
CheckpointManager? checkpointManager = null,
IWorkflowExecutionEnvironment? executionEnvironment = null)
{
Workflow>? maybeTyped = await workflow.TryPromoteAsync>()
.ConfigureAwait(false);
if (maybeTyped is null)
{
throw new InvalidOperationException("Cannot host a workflow that does not accept List as an input");
}
return maybeTyped.AsAgent(id, name, description, checkpointManager, executionEnvironment);
}
internal static FunctionCallContent ToFunctionCall(this ExternalRequest request)
{
Dictionary parameters = new()
{
{ "data", request.Data}
};
return new FunctionCallContent(request.RequestId, request.PortInfo.PortId, parameters);
}
}