| | | 1 | | using Elsa.AI.Abstractions.Models; |
| | | 2 | | using Elsa.AI.Host.Services; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | |
| | | 6 | | namespace Elsa.AI.Host.Tools.Workflows; |
| | | 7 | | |
| | 45 | 8 | | public class WorkflowUsageSearchTool(IServiceProvider serviceProvider, WorkflowGroundingMapper mapper, AIGroundingResult |
| | | 9 | | { |
| | 90 | 10 | | public override AIToolDefinition Definition { get; } = ReadOnlyDefinition( |
| | 45 | 11 | | "workflows.findUsages", |
| | 45 | 12 | | "Find workflow activity usages", |
| | 45 | 13 | | "Find authorized workflow definitions that reference an activity type in their serialized graph.", |
| | 45 | 14 | | GroundingToolSchemas.WithProperties( |
| | 45 | 15 | | ("activityType", GroundingToolSchemas.String("Activity type to find.")), |
| | 45 | 16 | | ("query", GroundingToolSchemas.String("Optional workflow search term.")))); |
| | | 17 | | |
| | | 18 | | public override async ValueTask<AIToolResult> ExecuteAsync(AIToolExecutionContext context, CancellationToken cancell |
| | | 19 | | { |
| | 0 | 20 | | var store = WorkflowDefinitionStore; |
| | 0 | 21 | | if (store == null) |
| | 0 | 22 | | return WorkflowStoreUnavailable(); |
| | | 23 | | |
| | 0 | 24 | | var activityType = GetString(context.Arguments, "activityType"); |
| | 0 | 25 | | if (string.IsNullOrWhiteSpace(activityType)) |
| | 0 | 26 | | return new AIToolResult { Status = AIToolInvocationStatus.Failed, Error = "activityType is required." }; |
| | | 27 | | |
| | 0 | 28 | | var page = await store.FindManyAsync(new WorkflowDefinitionFilter |
| | 0 | 29 | | { |
| | 0 | 30 | | SearchTerm = GetString(context.Arguments, "query"), |
| | 0 | 31 | | VersionOptions = VersionOptions.Latest |
| | 0 | 32 | | }, PageArgs.FromRange(0, 200), cancellationToken); |
| | 0 | 33 | | var definitions = page.Items |
| | 0 | 34 | | .Where(x => IsTenantAllowed(x, context.TenantId)) |
| | 0 | 35 | | .Where(x => mapper.GetGraph(x).ActivityTypes.Contains(activityType, StringComparer.OrdinalIgnoreCase)) |
| | 0 | 36 | | .ToList(); |
| | 0 | 37 | | var items = definitions.Select(x => AIGroundingJson.ToJsonObject(mapper.Map(x))); |
| | | 38 | | |
| | 0 | 39 | | return Formatter.CreateResult($"Found {definitions.Count} workflow definitions using {activityType}.", items, de |
| | 0 | 40 | | } |
| | | 41 | | } |