| | | 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 IncidentsSearchTool(IServiceProvider serviceProvider, RuntimeGroundingMapper mapper, AIGroundingResultForma |
| | | 8 | | { |
| | 91 | 9 | | public override AIToolDefinition Definition { get; } = ReadOnlyDefinition( |
| | 45 | 10 | | "incidents.search", |
| | 45 | 11 | | "Search incidents", |
| | 45 | 12 | | "Search incidents across authorized workflow instances in an explicit workflow, status, or time scope.", |
| | 45 | 13 | | GroundingToolSchemas.WithProperties( |
| | 45 | 14 | | ("definitionId", GroundingToolSchemas.String("Workflow definition ID.")), |
| | 45 | 15 | | ("instanceId", GroundingToolSchemas.String("Workflow instance ID.")), |
| | 45 | 16 | | ("query", GroundingToolSchemas.String("Free-text incident or instance search term.")), |
| | 45 | 17 | | ("from", GroundingToolSchemas.String("Updated-at range start.")), |
| | 45 | 18 | | ("to", GroundingToolSchemas.String("Updated-at range end.")))); |
| | | 19 | | |
| | | 20 | | public override async ValueTask<AIToolResult> ExecuteAsync(AIToolExecutionContext context, CancellationToken cancell |
| | | 21 | | { |
| | 1 | 22 | | var store = WorkflowInstanceStore; |
| | 1 | 23 | | if (store == null) |
| | 0 | 24 | | return InstanceStoreUnavailable(); |
| | | 25 | | |
| | 1 | 26 | | var filter = CreateInstanceFilter(context.Arguments); |
| | 1 | 27 | | filter.HasIncidents = true; |
| | 1 | 28 | | var page = await store.FindManyAsync(filter, PageArgs.FromRange(0, 100), cancellationToken); |
| | 1 | 29 | | var incidents = page.Items |
| | 1 | 30 | | .Where(x => IsTenantAllowed(x, context.TenantId)) |
| | 2 | 31 | | .SelectMany(x => x.WorkflowState.Incidents.Select(incident => mapper.MapIncident(x.Id, incident))) |
| | 0 | 32 | | .OrderByDescending(x => x.Timestamp) |
| | 1 | 33 | | .ToList(); |
| | 1 | 34 | | var items = incidents.Select(AIGroundingJson.ToJsonObject); |
| | | 35 | | |
| | 1 | 36 | | return Formatter.CreateResult($"Found {incidents.Count} incidents.", items, incidents.Count, ["WorkflowInstanceS |
| | 1 | 37 | | } |
| | | 38 | | } |