// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Agents.Workflows.Checkpointing;
using Microsoft.Agents.Workflows.Specialized;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.Workflows;
///
/// A class that represents a workflow that can be executed.
///
public class Workflow
{
///
/// A dictionary of executor providers, keyed by executor ID.
///
internal Dictionary Registrations { get; init; } = [];
internal Dictionary> Edges { get; init; } = [];
///
/// Gets the collection of edges grouped by their source node identifier.
///
public Dictionary> ReflectEdges()
{
return this.Edges.Keys.ToDictionary(
keySelector: key => key,
elementSelector: key => new HashSet(this.Edges[key].Select(RepresentationExtensions.ToEdgeInfo))
);
}
internal Dictionary Ports { get; init; } = [];
///
/// Gets the collection of external request ports, keyed by their ID.
///
///
/// Each port has a corresponding entry in the dictionary.
///
public Dictionary ReflectPorts()
{
return this.Ports.Keys.ToDictionary(
keySelector: key => key,
elementSelector: key => this.Ports[key].ToPortInfo()
);
}
///
/// Gets the identifier of the starting executor of the workflow.
///
public string StartExecutorId { get; }
///
/// Gets the type of input expected by the starting executor of the workflow.
///
public Type InputType { get; }
///
/// Initializes a new instance of the class with the specified starting executor identifier
/// and input type.
///
/// The unique identifier of the starting executor for the workflow. Cannot be null.
/// The representing the input data for the workflow. Cannot be null.
internal Workflow(string startExecutorId, Type type)
{
this.StartExecutorId = Throw.IfNull(startExecutorId);
this.InputType = Throw.IfNull(type);
}
}
///
/// Represents a workflow that operates on data of type .
///
/// The type of input to the workflow.
public class Workflow : Workflow
{
///
/// Initializes a new instance of the class with the specified starting executor identifier
///
/// The unique identifier of the starting executor for the workflow. Cannot be null.
public Workflow(string startExecutorId) : base(startExecutorId, typeof(T))
{
}
internal Workflow Promote(IOutputSink outputSource)
{
Throw.IfNull(outputSource);
return new Workflow(this.StartExecutorId, outputSource)
{
Registrations = this.Registrations,
Edges = this.Edges,
Ports = this.Ports
};
}
}
///
/// Represents a workflow that operates on data of type , resulting in
/// .
///
/// The type of input to the workflow.
/// The type of the output from the workflow.
public class Workflow : Workflow
{
private readonly IOutputSink _output;
internal Workflow(string startExecutorId, IOutputSink outputSource)
: base(startExecutorId)
{
this._output = Throw.IfNull(outputSource);
}
///
/// Gets the unique identifier of the output collector.
///
public string OutputCollectorId => this._output.Id;
///
/// The running (partial) output of the workflow, if any.
///
public TResult? RunningOutput => this._output.Result;
}