// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Checkpointing;
namespace Microsoft.Agents.AI.DevUI.Entities;
///
/// Extension methods for serializing workflows to DevUI-compatible format
///
internal static class WorkflowSerializationExtensions
{
// The frontend max iterations default value expected by the DevUI frontend
private const int MaxIterationsDefault = 100;
///
/// Converts a workflow to a dictionary representation compatible with DevUI frontend.
/// This matches the Python workflow.to_dict() format expected by the UI.
///
public static Dictionary ToDevUIDict(this Workflow workflow)
{
var result = new Dictionary
{
["id"] = workflow.Name ?? Guid.NewGuid().ToString(),
["start_executor_id"] = workflow.StartExecutorId,
["max_iterations"] = MaxIterationsDefault
};
// Add optional fields
if (!string.IsNullOrEmpty(workflow.Name))
{
result["name"] = workflow.Name;
}
if (!string.IsNullOrEmpty(workflow.Description))
{
result["description"] = workflow.Description;
}
// Convert executors to Python-compatible format
result["executors"] = ConvertExecutorsToDict(workflow);
// Convert edges to edge_groups format
result["edge_groups"] = ConvertEdgesToEdgeGroups(workflow);
return result;
}
///
/// Converts workflow executors to a dictionary format compatible with Python
///
private static Dictionary ConvertExecutorsToDict(Workflow workflow)
{
var executors = new Dictionary();
// Extract executor IDs from edges and start executor
// (Registrations is internal, so we infer executors from the graph structure)
var executorIds = new HashSet { workflow.StartExecutorId };
var reflectedEdges = workflow.ReflectEdges();
foreach (var (sourceId, edgeSet) in reflectedEdges)
{
executorIds.Add(sourceId);
foreach (var edge in edgeSet)
{
foreach (var sinkId in edge.Connection.SinkIds)
{
executorIds.Add(sinkId);
}
}
}
// Create executor entries (we can't access internal Registrations for type info)
foreach (var executorId in executorIds)
{
executors[executorId] = new Dictionary
{
["id"] = executorId,
["type"] = "Executor"
};
}
return executors;
}
///
/// Converts workflow edges to edge_groups format expected by the UI
///
private static List