Spaces:
Running

FlowAPI / FlowAPI.Application /Services /AutoLayoutEngine.cs
danylokhodus's picture
Layout: Swap axes to flow top-to-bottom for sequential and horizontal for sub-blocks
3105a78
Raw History Blame
5.35 kB
using System;
using System.Collections.Generic;
using System.Linq;
using FlowAPI.Application.DTOs.AI;
using FlowAPI.Application.DTOs.Graph;
namespace FlowAPI.Application.Services
{
public static class AutoLayoutEngine
{
private const double StepX = 300.0;
private const double StepY = 180.0;
public static FullGraphResponseDto LayoutGraph(QuestSchemaDto schema, Guid userId)
{
var graphId = Guid.NewGuid();
var response = new FullGraphResponseDto
{
Id = graphId,
UserId = userId,
Title = schema.Title,
Description = schema.Description,
CreatedAt = DateTime.UtcNow,
IsShared = false,
Permission = "Owner",
Nodes = new List<NodeDto>(),
Edges = new List<EdgeDto>()
};
if (schema.Nodes == null || !schema.Nodes.Any())
return response;
// 1. Map string string-based IDs from LLM into Guids
var idMap = new Dictionary<string, Guid>();
foreach (var node in schema.Nodes)
{
idMap[node.Id] = Guid.NewGuid();
}
// 2. Calculate logical layers (Sugiyama-like ranking)
var layerMap = new Dictionary<string, int>();
foreach (var node in schema.Nodes)
{
layerMap[node.Id] = 0; // Default layer
}
// Transitively propagate layer ranks based on dependencies (max N passes)
int n = schema.Nodes.Count;
for (int pass = 0; pass < n; pass++)
{
bool changed = false;
foreach (var node in schema.Nodes)
{
if (!string.IsNullOrEmpty(node.DependsOn) && idMap.ContainsKey(node.DependsOn))
{
int parentLayer = layerMap[node.DependsOn];
int currentLayer = layerMap[node.Id];
if (currentLayer < parentLayer + 1)
{
layerMap[node.Id] = parentLayer + 1;
changed = true;
}
}
}
if (!changed) break; // Optimization if stabilized early
}
// 3. Group nodes by layers and calculate coordinates
var layers = new Dictionary<int, List<QuestNodeDto>>();
foreach (var node in schema.Nodes)
{
int layer = layerMap[node.Id];
if (!layers.ContainsKey(layer))
{
layers[layer] = new List<QuestNodeDto>();
}
layers[layer].Add(node);
}
// Set coordinates symmetrically per layer
foreach (var layerKvp in layers)
{
int layer = layerKvp.Key;
var layerNodes = layerKvp.Value;
int count = layerNodes.Count;
for (int i = 0; i < count; i++)
{
var node = layerNodes[i];
var nodeGuid = idMap[node.Id];
// Symmetric distribution horizontally around X = 0, and sequential progression top-to-bottom
double posX = (i - (count - 1) / 2.0) * StepX;
double posY = layer * StepY;
// Determine visual properties
string? nodeColor = null;
if (!string.IsNullOrEmpty(node.Condition))
{
if (node.Condition.Equals("Fail", StringComparison.OrdinalIgnoreCase))
{
nodeColor = "#ba1a1a"; // Red for Fail paths
}
else if (node.Condition.Equals("Success", StringComparison.OrdinalIgnoreCase))
{
nodeColor = "#45a84a"; // Green for Success paths
}
}
response.Nodes.Add(new NodeDto
{
Id = nodeGuid,
Label = node.Label,
PosX = posX,
PosY = posY,
Type = "sketch",
State = 0, // Pending
Decision = node.Condition,
Description = node.Description,
Color = nodeColor,
IsPinned = false,
Width = 160,
Height = 80
});
// 4. Create Edges
if (!string.IsNullOrEmpty(node.DependsOn) && idMap.ContainsKey(node.DependsOn))
{
response.Edges.Add(new EdgeDto
{
Id = Guid.NewGuid(),
FromNodeId = idMap[node.DependsOn],
ToNodeId = nodeGuid,
Condition = node.Condition ?? "True"
});
}
}
}
return response;
}
}
}