| | | 1 | | using Elsa.AI.Abstractions.Models; |
| | | 2 | | using Elsa.Workflows; |
| | | 3 | | |
| | | 4 | | namespace Elsa.AI.Host.Services; |
| | | 5 | | |
| | 47 | 6 | | public class WorkflowDraftValidationService(IServiceProvider serviceProvider) |
| | | 7 | | { |
| | | 8 | | public IReadOnlyCollection<AIValidationDiagnostic> Validate(JsonObject draft, string? baselineVersionId = null, stri |
| | | 9 | | { |
| | 4 | 10 | | var diagnostics = new List<AIValidationDiagnostic>(); |
| | 4 | 11 | | if (draft.Count == 0) |
| | 1 | 12 | | diagnostics.Add(Error("draft.empty", "Draft payload is empty.", "$")); |
| | | 13 | | |
| | 4 | 14 | | if (!string.IsNullOrWhiteSpace(expectedBaselineVersionId) && |
| | 4 | 15 | | !string.Equals(baselineVersionId, expectedBaselineVersionId, StringComparison.Ordinal)) |
| | 1 | 16 | | diagnostics.Add(Error("baseline.stale", "The proposal baseline version is stale.", "$.baselineVersionId")); |
| | | 17 | | |
| | 4 | 18 | | var registry = serviceProvider.GetService(typeof(IActivityRegistry)) as IActivityRegistry; |
| | 4 | 19 | | if (registry == null) |
| | | 20 | | { |
| | 3 | 21 | | diagnostics.Add(Warning("activityRegistry.unavailable", "Activity Registry is not available, so activity val |
| | 3 | 22 | | return diagnostics; |
| | | 23 | | } |
| | | 24 | | |
| | 6 | 25 | | foreach (var activityType in FindActivityTypes(draft).Distinct(StringComparer.OrdinalIgnoreCase)) |
| | | 26 | | { |
| | 2 | 27 | | if (registry.Find(activityType) == null) |
| | 1 | 28 | | diagnostics.Add(Error("activity.unavailable", $"Activity '{activityType}' is not installed.", "$")); |
| | | 29 | | } |
| | | 30 | | |
| | 1 | 31 | | return diagnostics; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | private static IEnumerable<string> FindActivityTypes(JsonNode? node) |
| | | 35 | | { |
| | 8 | 36 | | if (node is JsonObject jsonObject) |
| | | 37 | | { |
| | 3 | 38 | | if (TryReadString(jsonObject, "type", out var type) || TryReadString(jsonObject, "typeName", out type) || Tr |
| | 2 | 39 | | yield return type; |
| | | 40 | | |
| | 21 | 41 | | foreach (var child in jsonObject.Select(x => x.Value)) |
| | | 42 | | { |
| | 14 | 43 | | foreach (var activityType in FindActivityTypes(child)) |
| | 2 | 44 | | yield return activityType; |
| | | 45 | | } |
| | | 46 | | } |
| | 5 | 47 | | else if (node is JsonArray jsonArray) |
| | | 48 | | { |
| | 6 | 49 | | foreach (var child in jsonArray) |
| | | 50 | | { |
| | 8 | 51 | | foreach (var activityType in FindActivityTypes(child)) |
| | 2 | 52 | | yield return activityType; |
| | | 53 | | } |
| | | 54 | | } |
| | 8 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static bool TryReadString(JsonObject jsonObject, string name, out string value) |
| | | 58 | | { |
| | 5 | 59 | | value = ""; |
| | 5 | 60 | | if (!jsonObject.TryGetPropertyValue(name, out var node) || node is not JsonValue jsonValue || !jsonValue.TryGetV |
| | 3 | 61 | | return false; |
| | | 62 | | |
| | 2 | 63 | | value = result; |
| | 2 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private static AIValidationDiagnostic Error(string code, string message, string path) => |
| | 3 | 68 | | new() { Code = code, Message = message, Path = path, Severity = AIValidationSeverity.Error }; |
| | | 69 | | |
| | | 70 | | private static AIValidationDiagnostic Warning(string code, string message, string path) => |
| | 3 | 71 | | new() { Code = code, Message = message, Path = path, Severity = AIValidationSeverity.Warning }; |
| | | 72 | | } |