Spaces:
Sleeping
Sleeping
Commit ·
0d9e67d
1
Parent(s): e4f56e2
feat: implement QuestSchema DTO, AutoLayoutEngine and AgentCanvasController for LLM Agent Skill integration
Browse files
FlowAPI.Application/DTOs/AI/QuestSchemaDto.cs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Collections.Generic;
|
| 2 |
+
|
| 3 |
+
namespace FlowAPI.Application.DTOs.AI
|
| 4 |
+
{
|
| 5 |
+
public class QuestNodeDto
|
| 6 |
+
{
|
| 7 |
+
public string Id { get; set; } = string.Empty;
|
| 8 |
+
public string Label { get; set; } = string.Empty;
|
| 9 |
+
public string? Description { get; set; }
|
| 10 |
+
public string? DependsOn { get; set; }
|
| 11 |
+
public string? Condition { get; set; }
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
public class QuestSchemaDto
|
| 15 |
+
{
|
| 16 |
+
public string Title { get; set; } = string.Empty;
|
| 17 |
+
public string? Description { get; set; }
|
| 18 |
+
public List<QuestNodeDto> Nodes { get; set; } = new();
|
| 19 |
+
}
|
| 20 |
+
}
|
FlowAPI.Application/Services/AutoLayoutEngine.cs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.Linq;
|
| 4 |
+
using FlowAPI.Application.DTOs.AI;
|
| 5 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 6 |
+
|
| 7 |
+
namespace FlowAPI.Application.Services
|
| 8 |
+
{
|
| 9 |
+
public static class AutoLayoutEngine
|
| 10 |
+
{
|
| 11 |
+
private const double StepX = 300.0;
|
| 12 |
+
private const double StepY = 180.0;
|
| 13 |
+
|
| 14 |
+
public static FullGraphResponseDto LayoutGraph(QuestSchemaDto schema, Guid userId)
|
| 15 |
+
{
|
| 16 |
+
var graphId = Guid.NewGuid();
|
| 17 |
+
var response = new FullGraphResponseDto
|
| 18 |
+
{
|
| 19 |
+
Id = graphId,
|
| 20 |
+
UserId = userId,
|
| 21 |
+
Title = schema.Title,
|
| 22 |
+
Description = schema.Description,
|
| 23 |
+
CreatedAt = DateTime.UtcNow,
|
| 24 |
+
IsShared = false,
|
| 25 |
+
Permission = "Owner",
|
| 26 |
+
Nodes = new List<NodeDto>(),
|
| 27 |
+
Edges = new List<EdgeDto>()
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
if (schema.Nodes == null || !schema.Nodes.Any())
|
| 31 |
+
return response;
|
| 32 |
+
|
| 33 |
+
// 1. Map string string-based IDs from LLM into Guids
|
| 34 |
+
var idMap = new Dictionary<string, Guid>();
|
| 35 |
+
foreach (var node in schema.Nodes)
|
| 36 |
+
{
|
| 37 |
+
idMap[node.Id] = Guid.NewGuid();
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// 2. Calculate logical layers (Sugiyama-like ranking)
|
| 41 |
+
var layerMap = new Dictionary<string, int>();
|
| 42 |
+
foreach (var node in schema.Nodes)
|
| 43 |
+
{
|
| 44 |
+
layerMap[node.Id] = 0; // Default layer
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Transitively propagate layer ranks based on dependencies (max N passes)
|
| 48 |
+
int n = schema.Nodes.Count;
|
| 49 |
+
for (int pass = 0; pass < n; pass++)
|
| 50 |
+
{
|
| 51 |
+
bool changed = false;
|
| 52 |
+
foreach (var node in schema.Nodes)
|
| 53 |
+
{
|
| 54 |
+
if (!string.IsNullOrEmpty(node.DependsOn) && idMap.ContainsKey(node.DependsOn))
|
| 55 |
+
{
|
| 56 |
+
int parentLayer = layerMap[node.DependsOn];
|
| 57 |
+
int currentLayer = layerMap[node.Id];
|
| 58 |
+
if (currentLayer < parentLayer + 1)
|
| 59 |
+
{
|
| 60 |
+
layerMap[node.Id] = parentLayer + 1;
|
| 61 |
+
changed = true;
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
if (!changed) break; // Optimization if stabilized early
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// 3. Group nodes by layers and calculate coordinates
|
| 69 |
+
var layers = new Dictionary<int, List<QuestNodeDto>>();
|
| 70 |
+
foreach (var node in schema.Nodes)
|
| 71 |
+
{
|
| 72 |
+
int layer = layerMap[node.Id];
|
| 73 |
+
if (!layers.ContainsKey(layer))
|
| 74 |
+
{
|
| 75 |
+
layers[layer] = new List<QuestNodeDto>();
|
| 76 |
+
}
|
| 77 |
+
layers[layer].Add(node);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// Set coordinates symmetrically per layer
|
| 81 |
+
foreach (var layerKvp in layers)
|
| 82 |
+
{
|
| 83 |
+
int layer = layerKvp.Key;
|
| 84 |
+
var layerNodes = layerKvp.Value;
|
| 85 |
+
int count = layerNodes.Count;
|
| 86 |
+
|
| 87 |
+
for (int i = 0; i < count; i++)
|
| 88 |
+
{
|
| 89 |
+
var node = layerNodes[i];
|
| 90 |
+
var nodeGuid = idMap[node.Id];
|
| 91 |
+
|
| 92 |
+
// Symmetric distribution vertically around Y = 0
|
| 93 |
+
double posX = layer * StepX;
|
| 94 |
+
double posY = (i - (count - 1) / 2.0) * StepY;
|
| 95 |
+
|
| 96 |
+
// Determine visual properties
|
| 97 |
+
string? nodeColor = null;
|
| 98 |
+
if (!string.IsNullOrEmpty(node.Condition))
|
| 99 |
+
{
|
| 100 |
+
if (node.Condition.Equals("Fail", StringComparison.OrdinalIgnoreCase))
|
| 101 |
+
{
|
| 102 |
+
nodeColor = "#ba1a1a"; // Red for Fail paths
|
| 103 |
+
}
|
| 104 |
+
else if (node.Condition.Equals("Success", StringComparison.OrdinalIgnoreCase))
|
| 105 |
+
{
|
| 106 |
+
nodeColor = "#45a84a"; // Green for Success paths
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
response.Nodes.Add(new NodeDto
|
| 111 |
+
{
|
| 112 |
+
Id = nodeGuid,
|
| 113 |
+
Label = node.Label,
|
| 114 |
+
PosX = posX,
|
| 115 |
+
PosY = posY,
|
| 116 |
+
Type = "sketch",
|
| 117 |
+
State = 0, // Pending
|
| 118 |
+
Decision = node.Condition,
|
| 119 |
+
Description = node.Description,
|
| 120 |
+
Color = nodeColor,
|
| 121 |
+
IsPinned = false,
|
| 122 |
+
Width = 160,
|
| 123 |
+
Height = 80
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
// 4. Create Edges
|
| 127 |
+
if (!string.IsNullOrEmpty(node.DependsOn) && idMap.ContainsKey(node.DependsOn))
|
| 128 |
+
{
|
| 129 |
+
response.Edges.Add(new EdgeDto
|
| 130 |
+
{
|
| 131 |
+
Id = Guid.NewGuid(),
|
| 132 |
+
FromNodeId = idMap[node.DependsOn],
|
| 133 |
+
ToNodeId = nodeGuid,
|
| 134 |
+
Condition = node.Condition ?? "True"
|
| 135 |
+
});
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
return response;
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
}
|
FlowAPI/Controllers/AgentCanvasController.cs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using FlowAPI.Application.DTOs.AI;
|
| 2 |
+
using FlowAPI.Application.DTOs.Graph;
|
| 3 |
+
using FlowAPI.Application.Interfaces;
|
| 4 |
+
using FlowAPI.Application.Services;
|
| 5 |
+
using Microsoft.AspNetCore.Authorization;
|
| 6 |
+
using Microsoft.AspNetCore.Mvc;
|
| 7 |
+
using System;
|
| 8 |
+
using System.Security.Claims;
|
| 9 |
+
using System.Threading.Tasks;
|
| 10 |
+
|
| 11 |
+
namespace FlowAPI.Controllers
|
| 12 |
+
{
|
| 13 |
+
[ApiController]
|
| 14 |
+
[Authorize]
|
| 15 |
+
[Route("api/agents/canvas")]
|
| 16 |
+
public class AgentCanvasController : ControllerBase
|
| 17 |
+
{
|
| 18 |
+
private readonly IGraphService _graphService;
|
| 19 |
+
|
| 20 |
+
public AgentCanvasController(IGraphService graphService)
|
| 21 |
+
{
|
| 22 |
+
_graphService = graphService;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
private Guid CurrentUserId
|
| 26 |
+
{
|
| 27 |
+
get
|
| 28 |
+
{
|
| 29 |
+
var userIdStr = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
| 30 |
+
return Guid.TryParse(userIdStr, out var userId) ? userId : Guid.Empty;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
[HttpPost("create")]
|
| 35 |
+
public async Task<IActionResult> CreateFromAgent([FromBody] QuestSchemaDto schema)
|
| 36 |
+
{
|
| 37 |
+
if (CurrentUserId == Guid.Empty)
|
| 38 |
+
return Unauthorized(new { message = "User authentication required." });
|
| 39 |
+
|
| 40 |
+
if (schema == null || schema.Nodes == null || schema.Nodes.Count == 0)
|
| 41 |
+
{
|
| 42 |
+
return BadRequest(new { message = "QuestboardAI plan must contain at least one task node." });
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
try
|
| 46 |
+
{
|
| 47 |
+
// 1. Create the Graph record in the database
|
| 48 |
+
var createDto = new CreateGraphDto
|
| 49 |
+
{
|
| 50 |
+
Title = string.IsNullOrWhiteSpace(schema.Title) ? "QuestboardAI Plan" : schema.Title.Trim(),
|
| 51 |
+
Description = schema.Description
|
| 52 |
+
};
|
| 53 |
+
var graphResponse = await _graphService.CreateAsync(createDto, CurrentUserId);
|
| 54 |
+
|
| 55 |
+
// 2. Perform Auto-Layout to assign visual coordinates PosX and PosY
|
| 56 |
+
var fullGraph = AutoLayoutEngine.LayoutGraph(schema, CurrentUserId);
|
| 57 |
+
fullGraph.Id = graphResponse.Id; // Sync graph ID
|
| 58 |
+
|
| 59 |
+
// 3. Save all nodes and dependency edges in the database
|
| 60 |
+
var saveResult = await _graphService.SaveFullGraphAsync(graphResponse.Id, fullGraph, CurrentUserId);
|
| 61 |
+
if (!saveResult)
|
| 62 |
+
{
|
| 63 |
+
return BadRequest(new { message = "Failed to store generated task nodes in database." });
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// 4. Return successful response with the Canvas redirection URL
|
| 67 |
+
return Ok(new
|
| 68 |
+
{
|
| 69 |
+
success = true,
|
| 70 |
+
graphId = graphResponse.Id,
|
| 71 |
+
canvasUrl = $"/canvas/{graphResponse.Id}"
|
| 72 |
+
});
|
| 73 |
+
}
|
| 74 |
+
catch (Exception ex)
|
| 75 |
+
{
|
| 76 |
+
return BadRequest(new { message = $"Internal error during plan generation: {ex.Message}" });
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
}
|