< Summary

Information
Class: Elsa.AI.Host.Context.WorkflowInstanceContextProvider
Assembly: Elsa.AI.Host
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Host/Context/WorkflowInstanceContextProvider.cs
Line coverage
7%
Covered lines: 2
Uncovered lines: 25
Coverable lines: 27
Total lines: 49
Line coverage: 7.4%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Kind()100%11100%
ResolveAsync()0%4260%
Unavailable(...)100%210%
NotFound(...)100%210%
NormalizeTenant(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Host/Context/WorkflowInstanceContextProvider.cs

#LineLine coverage
 1using Elsa.AI.Abstractions.Contracts;
 2using Elsa.AI.Abstractions.Models;
 3using Elsa.AI.Host.Services;
 4using Elsa.Workflows.Management;
 5using Elsa.Workflows.Management.Filters;
 6
 7namespace Elsa.AI.Host.Context;
 8
 399public class WorkflowInstanceContextProvider(IServiceProvider serviceProvider, RuntimeGroundingMapper mapper) : IAIConte
 10{
 4111    public string Kind => AIContextAttachmentKinds.WorkflowInstance;
 12
 13    public async ValueTask<AIResolvedContext> ResolveAsync(AIContextResolutionRequest request, CancellationToken cancell
 14    {
 015        var attachment = request.Attachment;
 016        var store = serviceProvider.GetService(typeof(IWorkflowInstanceStore)) as IWorkflowInstanceStore;
 017        if (store == null)
 018            return Unavailable(attachment);
 19
 020        var instance = await store.FindAsync(new WorkflowInstanceFilter { Id = attachment.ReferenceId }, cancellationTok
 021        if (instance == null || !string.Equals(NormalizeTenant(instance.TenantId), NormalizeTenant(request.TenantId), St
 022            return NotFound(attachment);
 23
 024        var data = AIGroundingJson.ToJsonObject(mapper.Map(instance));
 025        data["state"] = mapper.MapState(instance);
 026        data["incidents"] = AIGroundingJson.ToJsonArray(instance.WorkflowState.Incidents.Select(x => mapper.MapIncident(
 27
 028        return new AIResolvedContext
 029        {
 030            Kind = Kind,
 031            ReferenceId = attachment.ReferenceId,
 032            Summary = $"Workflow instance {instance.Id} is {instance.Status}/{instance.SubStatus} with {instance.Inciden
 033            Data = data,
 034            Metadata = new JsonObject
 035            {
 036                ["activityId"] = attachment.ActivityId
 037            }
 038        };
 039    }
 40
 41    private AIResolvedContext Unavailable(AIContextAttachment attachment) =>
 042        new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow instance store is not available."
 43
 44    private AIResolvedContext NotFound(AIContextAttachment attachment) =>
 045        new() { Kind = Kind, ReferenceId = attachment.ReferenceId, Summary = "Workflow instance was not found or is not 
 46
 47    private static string NormalizeTenant(string? tenantId) =>
 048        string.IsNullOrWhiteSpace(tenantId) ? "" : tenantId;
 49}