| | | 1 | | using Elsa.Workflows; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.AI.Host.Services; |
| | | 5 | | |
| | 44 | 6 | | public class ActivityGroundingSearchService(ActivityGroundingMapper mapper) |
| | | 7 | | { |
| | | 8 | | public IReadOnlyCollection<ActivityDescriptor> Search( |
| | | 9 | | IActivityRegistry registry, |
| | | 10 | | string? query, |
| | | 11 | | string? category, |
| | | 12 | | string? type, |
| | | 13 | | int? version, |
| | | 14 | | string? input, |
| | | 15 | | string? output, |
| | | 16 | | bool? trigger) |
| | | 17 | | { |
| | 1 | 18 | | var descriptors = registry.ListAll().AsEnumerable(); |
| | | 19 | | |
| | 1 | 20 | | if (!string.IsNullOrWhiteSpace(query)) |
| | 2 | 21 | | descriptors = descriptors.Where(x => MatchesQuery(x, query)); |
| | | 22 | | |
| | 1 | 23 | | if (!string.IsNullOrWhiteSpace(category)) |
| | 0 | 24 | | descriptors = descriptors.Where(x => string.Equals(x.Category, category, StringComparison.OrdinalIgnoreCase) |
| | | 25 | | |
| | 1 | 26 | | if (!string.IsNullOrWhiteSpace(type)) |
| | 0 | 27 | | descriptors = descriptors.Where(x => string.Equals(x.TypeName, type, StringComparison.OrdinalIgnoreCase) || |
| | 0 | 28 | | string.Equals(x.Name, type, StringComparison.OrdinalIgnoreCase)); |
| | | 29 | | |
| | 1 | 30 | | if (version != null) |
| | 0 | 31 | | descriptors = descriptors.Where(x => x.Version == version); |
| | | 32 | | |
| | 1 | 33 | | if (!string.IsNullOrWhiteSpace(input)) |
| | 0 | 34 | | descriptors = descriptors.Where(x => x.Inputs.Any(inputDescriptor => MatchesText(inputDescriptor.Name, input |
| | | 35 | | |
| | 1 | 36 | | if (!string.IsNullOrWhiteSpace(output)) |
| | 0 | 37 | | descriptors = descriptors.Where(x => x.Outputs.Any(outputDescriptor => MatchesText(outputDescriptor.Name, ou |
| | | 38 | | |
| | 1 | 39 | | if (trigger != null) |
| | 2 | 40 | | descriptors = descriptors.Where(x => x.IsStart == trigger); |
| | | 41 | | |
| | 1 | 42 | | return descriptors |
| | 0 | 43 | | .OrderBy(x => x.Category) |
| | 0 | 44 | | .ThenBy(x => x.DisplayName ?? x.Name) |
| | 0 | 45 | | .ThenByDescending(x => x.Version) |
| | 1 | 46 | | .ToList(); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public JsonObject Map(ActivityDescriptor descriptor) => |
| | 1 | 50 | | AIGroundingJson.ToJsonObject(mapper.Map(descriptor)); |
| | | 51 | | |
| | | 52 | | private static bool MatchesQuery(ActivityDescriptor descriptor, string query) => |
| | 1 | 53 | | MatchesText(descriptor.Name, query) || |
| | 1 | 54 | | MatchesText(descriptor.DisplayName, query) || |
| | 1 | 55 | | MatchesText(descriptor.Description, query) || |
| | 1 | 56 | | MatchesText(descriptor.Category, query) || |
| | 1 | 57 | | MatchesText(descriptor.Namespace, query) || |
| | 1 | 58 | | MatchesText(descriptor.TypeName, query) || |
| | 0 | 59 | | descriptor.Inputs.Any(x => MatchesText(x.Name, query) || MatchesText(x.DisplayName, query) || MatchesText(x.Desc |
| | 1 | 60 | | descriptor.Outputs.Any(x => MatchesText(x.Name, query) || MatchesText(x.DisplayName, query) || MatchesText(x.Des |
| | | 61 | | |
| | | 62 | | private static bool MatchesText(string? value, string query) => |
| | 1 | 63 | | !string.IsNullOrWhiteSpace(value) && value.Contains(query, StringComparison.OrdinalIgnoreCase); |
| | | 64 | | } |