Spaces:
Running

danylokhodus's picture
fix: optimized Dockerfile, backend AI service, and gitignore
ecea7e2
Raw History Blame
10.1 kB
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using FlowAPI.Application.DTOs.AI;
using FlowAPI.Application.DTOs.Graph;
using FlowAPI.Application.Interfaces;
using Microsoft.Extensions.Configuration;
namespace FlowAPI.Application.Services
{
public class AIService : IAIService
{
private readonly IUserRepository _userRepo;
private readonly IGraphRepository _graphRepo;
private readonly IConfiguration _config;
private readonly IGraphService _graphService;
private readonly IGeminiBridgeManager _bridgeManager;
public AIService(IUserRepository userRepo, IGraphRepository graphRepo, IConfiguration config, IGraphService graphService, IGeminiBridgeManager bridgeManager)
{
_userRepo = userRepo;
_graphRepo = graphRepo;
_config = config;
_graphService = graphService;
_bridgeManager = bridgeManager;
}
public async Task<AIPathResponseDto> GenerateAlternativePathAsync(GenerateAlternativeRequestDto request)
{
throw new Exception("Функція ШІ тимчасово недоступна. Ми працюємо над її покращенням!");
}
public async Task<FullGraphResponseDto> GenerateGoalGraphAsync(GenerateGoalGraphRequestDto request)
{
throw new Exception("Функція ШІ тимчасово недоступна. Ми працюємо над її покращенням!");
}
public async Task<AICanvasAssistResponseDto> CanvasAssistAsync(AICanvasAssistRequestDto request)
{
throw new Exception("Функція ШІ тимчасово недоступна. Ми працюємо над її покращенням!");
}
private string CleanJson(string text)
{
text = text.Trim();
if (text.StartsWith("```"))
{
var lines = text.Split('\n');
var cleanLines = lines.Where(line => !line.Trim().StartsWith("```")).ToArray();
text = string.Join("\n", cleanLines).Trim();
}
return text;
}
private void ApplyBeautifulLayout(List<NodeDto> nodes, List<EdgeDto> edges)
{
if (nodes == null || nodes.Count == 0) return;
var inDegree = new Dictionary<Guid, int>();
var adj = new Dictionary<Guid, List<Guid>>();
foreach (var node in nodes)
{
inDegree[node.Id] = 0;
adj[node.Id] = new List<Guid>();
}
foreach (var edge in edges)
{
if (inDegree.ContainsKey(edge.ToNodeId) && inDegree.ContainsKey(edge.FromNodeId))
{
inDegree[edge.ToNodeId]++;
adj[edge.FromNodeId].Add(edge.ToNodeId);
}
}
var layers = new Dictionary<Guid, int>();
foreach (var node in nodes) { layers[node.Id] = 0; }
var queue = new Queue<Guid>();
foreach (var node in nodes)
{
if (inDegree[node.Id] == 0) { queue.Enqueue(node.Id); }
}
if (queue.Count == 0 && nodes.Count > 0)
{
queue.Enqueue(nodes[0].Id);
}
var processed = new HashSet<Guid>();
while (queue.Count > 0)
{
var curr = queue.Dequeue();
processed.Add(curr);
int currentLayer = layers[curr];
foreach (var neighbor in adj[curr])
{
layers[neighbor] = Math.Max(layers[neighbor], currentLayer + 1);
if (!processed.Contains(neighbor) && !queue.Contains(neighbor))
{
queue.Enqueue(neighbor);
}
}
}
var nodesByLayer = new Dictionary<int, List<NodeDto>>();
foreach (var node in nodes)
{
int layer = layers.ContainsKey(node.Id) ? layers[node.Id] : 0;
if (!nodesByLayer.ContainsKey(layer)) { nodesByLayer[layer] = new List<NodeDto>(); }
nodesByLayer[layer].Add(node);
}
double marginY = 100;
double spacingY = 220;
double spacingX = 350;
double centerX = 450;
var sortedLayers = nodesByLayer.Keys.OrderBy(l => l).ToList();
foreach (var layer in sortedLayers)
{
var layerNodes = nodesByLayer[layer];
int count = layerNodes.Count;
for (int i = 0; i < count; i++)
{
var node = layerNodes[i];
node.PosY = marginY + layer * spacingY;
node.PosX = centerX + (i - (count - 1) / 2.0) * spacingX;
}
}
}
private void ApplyBeautifulLayout(List<FlowAPI.Domain.Entities.TaskNode> nodes, List<FlowAPI.Domain.Entities.Edge> edges)
{
if (nodes == null || nodes.Count == 0) return;
var inDegree = new Dictionary<Guid, int>();
var adj = new Dictionary<Guid, List<Guid>>();
foreach (var node in nodes)
{
inDegree[node.Id] = 0;
adj[node.Id] = new List<Guid>();
}
foreach (var edge in edges)
{
if (inDegree.ContainsKey(edge.ToNodeId) && inDegree.ContainsKey(edge.FromNodeId))
{
inDegree[edge.ToNodeId]++;
adj[edge.FromNodeId].Add(edge.ToNodeId);
}
}
var layers = new Dictionary<Guid, int>();
foreach (var node in nodes) { layers[node.Id] = 0; }
var queue = new Queue<Guid>();
foreach (var node in nodes)
{
if (inDegree[node.Id] == 0) { queue.Enqueue(node.Id); }
}
if (queue.Count == 0 && nodes.Count > 0)
{
queue.Enqueue(nodes[0].Id);
}
var processed = new HashSet<Guid>();
while (queue.Count > 0)
{
var curr = queue.Dequeue();
processed.Add(curr);
int currentLayer = layers[curr];
foreach (var neighbor in adj[curr])
{
layers[neighbor] = Math.Max(layers[neighbor], currentLayer + 1);
if (!processed.Contains(neighbor) && !queue.Contains(neighbor))
{
queue.Enqueue(neighbor);
}
}
}
var nodesByLayer = new Dictionary<int, List<FlowAPI.Domain.Entities.TaskNode>>();
foreach (var node in nodes)
{
int layer = layers.ContainsKey(node.Id) ? layers[node.Id] : 0;
if (!nodesByLayer.ContainsKey(layer)) { nodesByLayer[layer] = new List<FlowAPI.Domain.Entities.TaskNode>(); }
nodesByLayer[layer].Add(node);
}
double marginY = 100;
double spacingY = 220;
double spacingX = 350;
double centerX = 450;
var sortedLayers = nodesByLayer.Keys.OrderBy(l => l).ToList();
foreach (var layer in sortedLayers)
{
var layerNodes = nodesByLayer[layer];
int count = layerNodes.Count;
for (int i = 0; i < count; i++)
{
var node = layerNodes[i];
node.PosY = marginY + layer * spacingY;
node.PosX = centerX + (i - (count - 1) / 2.0) * spacingX;
}
}
}
private class AIGoalGraphResponse
{
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<AINodeDto> Nodes { get; set; } = new();
public List<AIEdgeDto> Edges { get; set; } = new();
}
private class AIOptionDto
{
public string Id { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
}
private class AINodeDto
{
public string Id { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
public double PosX { get; set; }
public double PosY { get; set; }
public string? Type { get; set; }
public List<AIOptionDto>? Options { get; set; }
}
private class AIEdgeDto
{
public string From { get; set; } = string.Empty;
public string To { get; set; } = string.Empty;
public string? Condition { get; set; }
}
private class AICanvasAssistResponseInternal
{
public List<AINewNodeInternalDto> NewNodes { get; set; } = new();
public List<AINewEdgeInternalDto> NewEdges { get; set; } = new();
}
private class AINewNodeInternalDto
{
public string Id { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
public double PosX { get; set; }
public double PosY { get; set; }
public string? Type { get; set; }
public List<AIOptionDto>? Options { get; set; }
}
private class AINewEdgeInternalDto
{
public string From { get; set; } = string.Empty;
public string To { get; set; } = string.Empty;
public string? Condition { get; set; }
}
}
}