| | | 1 | | using Elsa.AI.Abstractions.Models; |
| | | 2 | | using Elsa.AI.Host.Services; |
| | | 3 | | |
| | | 4 | | namespace Elsa.AI.Host.Tools.Runtime; |
| | | 5 | | |
| | 45 | 6 | | public class IncidentTool(IServiceProvider serviceProvider, RuntimeGroundingMapper mapper, AIGroundingResultFormatter fo |
| | | 7 | | { |
| | 90 | 8 | | public override AIToolDefinition Definition { get; } = ReadOnlyDefinition( |
| | 45 | 9 | | "incidents.get", |
| | 45 | 10 | | "Get incident", |
| | 45 | 11 | | "Get a redacted incident by workflow instance and activity or node ID.", |
| | 45 | 12 | | GroundingToolSchemas.WithProperties( |
| | 45 | 13 | | ("instanceId", GroundingToolSchemas.String("Workflow instance ID.")), |
| | 45 | 14 | | ("activityId", GroundingToolSchemas.String("Activity ID.")), |
| | 45 | 15 | | ("activityNodeId", GroundingToolSchemas.String("Activity node ID.")))); |
| | | 16 | | |
| | | 17 | | public override async ValueTask<AIToolResult> ExecuteAsync(AIToolExecutionContext context, CancellationToken cancell |
| | | 18 | | { |
| | 0 | 19 | | if (WorkflowInstanceStore == null) |
| | 0 | 20 | | return InstanceStoreUnavailable(); |
| | | 21 | | |
| | 0 | 22 | | var instance = await FindAuthorizedInstanceAsync(context.Arguments, context.TenantId, cancellationToken); |
| | 0 | 23 | | if (instance == null) |
| | 0 | 24 | | return new AIToolResult { Status = AIToolInvocationStatus.Failed, Error = "Workflow instance was not found." |
| | | 25 | | |
| | 0 | 26 | | var activityId = GetString(context.Arguments, "activityId"); |
| | 0 | 27 | | var activityNodeId = GetString(context.Arguments, "activityNodeId"); |
| | 0 | 28 | | var incidents = instance.WorkflowState.Incidents |
| | 0 | 29 | | .Where(x => Matches(x.ActivityId, activityId) || Matches(x.ActivityNodeId, activityNodeId)) |
| | 0 | 30 | | .Select(x => mapper.MapIncident(instance.Id, x)) |
| | 0 | 31 | | .ToList(); |
| | | 32 | | |
| | 0 | 33 | | return Formatter.CreateResult($"Resolved {incidents.Count} incidents for workflow instance {instance.Id}.", inci |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | private static bool Matches(string? value, string? expected) => |
| | 0 | 37 | | !string.IsNullOrWhiteSpace(value) && !string.IsNullOrWhiteSpace(expected) && string.Equals(value, expected, Stri |
| | | 38 | | } |