| | | 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 | | |
| | 41 | 9 | | public class WorkflowDefinitionContextProvider(IServiceProvider serviceProvider, WorkflowGroundingMapper mapper) : IAICo |
| | | 10 | | { |
| | 44 | 11 | | public string Kind => AIContextAttachmentKinds.WorkflowDefinition; |
| | | 12 | | |
| | | 13 | | public async ValueTask<AIResolvedContext> ResolveAsync(AIContextResolutionRequest request, CancellationToken cancell |
| | | 14 | | { |
| | 1 | 15 | | var attachment = request.Attachment; |
| | 1 | 16 | | var store = serviceProvider.GetService(typeof(IWorkflowDefinitionStore)) as IWorkflowDefinitionStore; |
| | 1 | 17 | | if (store == null) |
| | 1 | 18 | | return Unavailable(attachment); |
| | | 19 | | |
| | 0 | 20 | | var definition = await store.FindAsync(new WorkflowDefinitionFilter { Id = attachment.ReferenceId }, cancellatio |
| | 0 | 21 | | ?? await store.FindAsync(new WorkflowDefinitionFilter |
| | 0 | 22 | | { |
| | 0 | 23 | | DefinitionId = attachment.ReferenceId, |
| | 0 | 24 | | VersionOptions = Elsa.Common.Models.VersionOptions.Latest |
| | 0 | 25 | | }, cancellationToken); |
| | 0 | 26 | | if (definition == null || !string.Equals(NormalizeTenant(definition.TenantId), NormalizeTenant(request.TenantId) |
| | 0 | 27 | | return NotFound(attachment); |
| | | 28 | | |
| | 0 | 29 | | return new AIResolvedContext |
| | 0 | 30 | | { |
| | 0 | 31 | | Kind = Kind, |
| | 0 | 32 | | ReferenceId = attachment.ReferenceId, |
| | 0 | 33 | | Summary = $"Workflow definition {definition.DefinitionId} v{definition.Version}: {definition.Name}", |
| | 0 | 34 | | Data = AIGroundingJson.ToJsonObject(mapper.Map(definition)), |
| | 0 | 35 | | Metadata = new JsonObject |
| | 0 | 36 | | { |
| | 0 | 37 | | ["activityId"] = attachment.ActivityId |
| | 0 | 38 | | } |
| | 0 | 39 | | }; |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | private AIResolvedContext Unavailable(AIContextAttachment attachment) => |
| | 1 | 43 | | new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow definition store is not available |
| | | 44 | | |
| | | 45 | | private AIResolvedContext NotFound(AIContextAttachment attachment) => |
| | 0 | 46 | | new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow definition was not found or is no |
| | | 47 | | |
| | | 48 | | private static string NormalizeTenant(string? tenantId) => |
| | 0 | 49 | | string.IsNullOrWhiteSpace(tenantId) ? "" : tenantId; |
| | | 50 | | } |