| | | 1 | | using Elsa.AI.Abstractions.Models; |
| | | 2 | | using Elsa.AI.Host.Services; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.AI.Host.Tools.Runtime; |
| | | 6 | | |
| | 45 | 7 | | public class InstancesSearchTool(IServiceProvider serviceProvider, RuntimeGroundingMapper mapper, AIGroundingResultForma |
| | | 8 | | { |
| | 90 | 9 | | public override AIToolDefinition Definition { get; } = ReadOnlyDefinition( |
| | 45 | 10 | | "instances.search", |
| | 45 | 11 | | "Search workflow instances", |
| | 45 | 12 | | "Search authorized workflow instances by workflow, status, incident flag, correlation ID, and time range.", |
| | 45 | 13 | | GroundingToolSchemas.WithProperties( |
| | 45 | 14 | | ("query", GroundingToolSchemas.String("Free-text search term.")), |
| | 45 | 15 | | ("definitionId", GroundingToolSchemas.String("Workflow definition ID.")), |
| | 45 | 16 | | ("status", GroundingToolSchemas.String("Workflow status.")), |
| | 45 | 17 | | ("subStatus", GroundingToolSchemas.String("Workflow sub-status.")), |
| | 45 | 18 | | ("correlationId", GroundingToolSchemas.String("Correlation ID.")), |
| | 45 | 19 | | ("hasIncidents", GroundingToolSchemas.Boolean("Whether incidents are present.")), |
| | 45 | 20 | | ("from", GroundingToolSchemas.String("Updated-at range start.")), |
| | 45 | 21 | | ("to", GroundingToolSchemas.String("Updated-at range end.")))); |
| | | 22 | | |
| | | 23 | | public override async ValueTask<AIToolResult> ExecuteAsync(AIToolExecutionContext context, CancellationToken cancell |
| | | 24 | | { |
| | 0 | 25 | | var store = WorkflowInstanceStore; |
| | 0 | 26 | | if (store == null) |
| | 0 | 27 | | return InstanceStoreUnavailable(); |
| | | 28 | | |
| | 0 | 29 | | var page = await store.FindManyAsync(CreateInstanceFilter(context.Arguments), PageArgs.FromRange(0, 100), cancel |
| | 0 | 30 | | var instances = page.Items.Where(x => IsTenantAllowed(x, context.TenantId)).ToList(); |
| | 0 | 31 | | var items = instances.Select(x => AIGroundingJson.ToJsonObject(mapper.Map(x))); |
| | | 32 | | |
| | 0 | 33 | | return Formatter.CreateResult($"Found {instances.Count} authorized workflow instances.", items, instances.Count, |
| | 0 | 34 | | } |
| | | 35 | | } |