| | | 1 | | using Elsa.AI.Abstractions.Contracts; |
| | | 2 | | using Elsa.AI.Abstractions.Models; |
| | | 3 | | using Elsa.AI.Host.Services; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Management.Filters; |
| | | 6 | | |
| | | 7 | | namespace Elsa.AI.Host.Context; |
| | | 8 | | |
| | 39 | 9 | | public class WorkflowInstanceContextProvider(IServiceProvider serviceProvider, RuntimeGroundingMapper mapper) : IAIConte |
| | | 10 | | { |
| | 41 | 11 | | public string Kind => AIContextAttachmentKinds.WorkflowInstance; |
| | | 12 | | |
| | | 13 | | public async ValueTask<AIResolvedContext> ResolveAsync(AIContextResolutionRequest request, CancellationToken cancell |
| | | 14 | | { |
| | 0 | 15 | | var attachment = request.Attachment; |
| | 0 | 16 | | var store = serviceProvider.GetService(typeof(IWorkflowInstanceStore)) as IWorkflowInstanceStore; |
| | 0 | 17 | | if (store == null) |
| | 0 | 18 | | return Unavailable(attachment); |
| | | 19 | | |
| | 0 | 20 | | var instance = await store.FindAsync(new WorkflowInstanceFilter { Id = attachment.ReferenceId }, cancellationTok |
| | 0 | 21 | | if (instance == null || !string.Equals(NormalizeTenant(instance.TenantId), NormalizeTenant(request.TenantId), St |
| | 0 | 22 | | return NotFound(attachment); |
| | | 23 | | |
| | 0 | 24 | | var data = AIGroundingJson.ToJsonObject(mapper.Map(instance)); |
| | 0 | 25 | | data["state"] = mapper.MapState(instance); |
| | 0 | 26 | | data["incidents"] = AIGroundingJson.ToJsonArray(instance.WorkflowState.Incidents.Select(x => mapper.MapIncident( |
| | | 27 | | |
| | 0 | 28 | | return new AIResolvedContext |
| | 0 | 29 | | { |
| | 0 | 30 | | Kind = Kind, |
| | 0 | 31 | | ReferenceId = attachment.ReferenceId, |
| | 0 | 32 | | Summary = $"Workflow instance {instance.Id} is {instance.Status}/{instance.SubStatus} with {instance.Inciden |
| | 0 | 33 | | Data = data, |
| | 0 | 34 | | Metadata = new JsonObject |
| | 0 | 35 | | { |
| | 0 | 36 | | ["activityId"] = attachment.ActivityId |
| | 0 | 37 | | } |
| | 0 | 38 | | }; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | private AIResolvedContext Unavailable(AIContextAttachment attachment) => |
| | 0 | 42 | | new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow instance store is not available." |
| | | 43 | | |
| | | 44 | | private AIResolvedContext NotFound(AIContextAttachment attachment) => |
| | 0 | 45 | | new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow instance was not found or is not |
| | | 46 | | |
| | | 47 | | private static string NormalizeTenant(string? tenantId) => |
| | 0 | 48 | | string.IsNullOrWhiteSpace(tenantId) ? "" : tenantId; |
| | | 49 | | } |