< Summary

Information
Class: Intentum.AI.IntentModelStreamingExtensions
Assembly: Intentum.AI
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AI/IntentModelStreamingExtensions.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
InferMany(...)100%11100%
InferManyIterator()100%22100%
InferManyAsync(...)100%11100%
InferManyAsyncIterator()100%22100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AI/IntentModelStreamingExtensions.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Intentum.Core.Behavior;
 3using Intentum.Core.Contracts;
 4using Intentum.Core.Intents;
 5using JetBrains.Annotations;
 6
 7namespace Intentum.AI;
 8
 9/// <summary>
 10/// Streaming and batch extensions for <see cref="IIntentModel"/>.
 11/// </summary>
 12[UsedImplicitly]
 13public static class IntentModelStreamingExtensions
 14{
 15    /// <summary>
 16    /// Infers intent for each behavior space and returns results as a sequence (lazy enumeration).
 17    /// </summary>
 18    /// <param name="model">The intent model.</param>
 19    /// <param name="spaces">Behavior spaces to infer.</param>
 20    /// <returns>Sequence of inferred intents in the same order as <paramref name="spaces"/>.</returns>
 21    [UsedImplicitly]
 22    public static IEnumerable<Intent> InferMany(
 23        this IIntentModel model,
 24        IEnumerable<BehaviorSpace> spaces)
 525    {
 526        ArgumentNullException.ThrowIfNull(model);
 427        ArgumentNullException.ThrowIfNull(spaces);
 328        return InferManyIterator(model, spaces);
 329    }
 30
 31    private static IEnumerable<Intent> InferManyIterator(IIntentModel model, IEnumerable<BehaviorSpace> spaces)
 332    {
 1733        foreach (var space in spaces)
 434            yield return model.Infer(space);
 335    }
 36
 37    /// <summary>
 38    /// Infers intent for each behavior space as they are enumerated, yielding results as they are ready (async stream).
 39    /// </summary>
 40    /// <param name="model">The intent model.</param>
 41    /// <param name="spaces">Async sequence of behavior spaces.</param>
 42    /// <param name="cancellationToken">Cancellation token.</param>
 43    /// <returns>Async sequence of inferred intents in the same order as <paramref name="spaces"/>.</returns>
 44    [UsedImplicitly]
 45    public static IAsyncEnumerable<Intent> InferManyAsync(
 46        this IIntentModel model,
 47        IAsyncEnumerable<BehaviorSpace> spaces,
 48        CancellationToken cancellationToken = default)
 549    {
 550        ArgumentNullException.ThrowIfNull(model);
 451        ArgumentNullException.ThrowIfNull(spaces);
 352        return InferManyAsyncIterator(model, spaces, cancellationToken);
 353    }
 54
 55    private static async IAsyncEnumerable<Intent> InferManyAsyncIterator(
 56        IIntentModel model,
 57        IAsyncEnumerable<BehaviorSpace> spaces,
 58        [EnumeratorCancellation] CancellationToken cancellationToken)
 359    {
 2260        await foreach (var space in spaces.WithCancellation(cancellationToken))
 761        {
 762            cancellationToken.ThrowIfCancellationRequested();
 763            yield return model.Infer(space);
 664        }
 365    }
 66}