| | | 1 | | using Elsa.AI.Abstractions.Models; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.AI.Host.Services; |
| | | 5 | | |
| | | 6 | | public class ActivityGroundingMapper |
| | | 7 | | { |
| | | 8 | | public ActivityGroundingSummary Map(ActivityDescriptor descriptor) => |
| | 2 | 9 | | new() |
| | 2 | 10 | | { |
| | 2 | 11 | | Type = descriptor.TypeName, |
| | 2 | 12 | | Version = descriptor.Version, |
| | 2 | 13 | | Namespace = descriptor.Namespace, |
| | 2 | 14 | | Name = descriptor.Name, |
| | 2 | 15 | | DisplayName = descriptor.DisplayName ?? descriptor.Name, |
| | 2 | 16 | | Description = descriptor.Description, |
| | 2 | 17 | | Category = descriptor.Category, |
| | 2 | 18 | | IsBrowsable = descriptor.IsBrowsable, |
| | 2 | 19 | | IsTrigger = descriptor.IsStart, |
| | 2 | 20 | | IsContainer = descriptor.IsContainer, |
| | 2 | 21 | | IsTerminal = descriptor.IsTerminal, |
| | 0 | 22 | | Inputs = descriptor.Inputs.Select(MapInput).OrderBy(x => x.Name).ToList(), |
| | 0 | 23 | | Outputs = descriptor.Outputs.Select(MapOutput).OrderBy(x => x.Name).ToList(), |
| | 2 | 24 | | Ports = descriptor.Ports.Select(x => x.Name).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringCompar |
| | 2 | 25 | | Constraints = GetConstraints(descriptor).ToList() |
| | 2 | 26 | | }; |
| | | 27 | | |
| | | 28 | | private static ActivityPortSummary MapInput(InputDescriptor input) => |
| | 1 | 29 | | new() |
| | 1 | 30 | | { |
| | 1 | 31 | | Name = input.Name, |
| | 1 | 32 | | DisplayName = input.DisplayName ?? input.Name, |
| | 1 | 33 | | Description = input.Description, |
| | 1 | 34 | | Type = GetFriendlyTypeName(input.Type), |
| | 1 | 35 | | Category = input.Category, |
| | 1 | 36 | | IsBrowsable = input.IsBrowsable ?? true, |
| | 1 | 37 | | IsSensitive = input.IsSensitive, |
| | 1 | 38 | | IsRequired = input.DefaultValue == null && !IsNullable(input.Type), |
| | 1 | 39 | | UIHint = input.UIHint, |
| | 1 | 40 | | DefaultSyntax = input.DefaultSyntax |
| | 1 | 41 | | }; |
| | | 42 | | |
| | | 43 | | private static ActivityPortSummary MapOutput(OutputDescriptor output) => |
| | 1 | 44 | | new() |
| | 1 | 45 | | { |
| | 1 | 46 | | Name = output.Name, |
| | 1 | 47 | | DisplayName = output.DisplayName ?? output.Name, |
| | 1 | 48 | | Description = output.Description, |
| | 1 | 49 | | Type = GetFriendlyTypeName(output.Type), |
| | 1 | 50 | | IsBrowsable = output.IsBrowsable ?? true |
| | 1 | 51 | | }; |
| | | 52 | | |
| | | 53 | | private static IEnumerable<string> GetConstraints(ActivityDescriptor descriptor) |
| | | 54 | | { |
| | 2 | 55 | | if (!descriptor.IsBrowsable) |
| | 0 | 56 | | yield return "Not selectable from activity pickers."; |
| | | 57 | | |
| | 2 | 58 | | if (descriptor.RunAsynchronously) |
| | 0 | 59 | | yield return "Runs asynchronously by default."; |
| | | 60 | | |
| | 2 | 61 | | if (descriptor.IsStart) |
| | 2 | 62 | | yield return "Can start a workflow."; |
| | | 63 | | |
| | 2 | 64 | | if (descriptor.IsTerminal) |
| | 0 | 65 | | yield return "Can terminate a workflow path."; |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | private static bool IsNullable(Type type) => |
| | 1 | 69 | | !type.IsValueType || Nullable.GetUnderlyingType(type) != null; |
| | | 70 | | |
| | | 71 | | private static string GetFriendlyTypeName(Type? type) |
| | | 72 | | { |
| | 2 | 73 | | if (type == null) |
| | 0 | 74 | | return "unknown"; |
| | | 75 | | |
| | 2 | 76 | | var nullableType = Nullable.GetUnderlyingType(type); |
| | 2 | 77 | | if (nullableType != null) |
| | 0 | 78 | | return $"{GetFriendlyTypeName(nullableType)}?"; |
| | | 79 | | |
| | 2 | 80 | | if (!type.IsGenericType) |
| | 2 | 81 | | return type.Name; |
| | | 82 | | |
| | 0 | 83 | | var name = type.Name[..type.Name.IndexOf('`', StringComparison.Ordinal)]; |
| | 0 | 84 | | return $"{name}<{string.Join(", ", type.GetGenericArguments().Select(GetFriendlyTypeName))}>"; |
| | | 85 | | } |
| | | 86 | | } |