Spaces:
Running

File size: 5,352 Bytes
0d9e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3105a78
 
 
0d9e67d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
        }
    }
}