< Summary

Information
Class: Intentum.Core.Models.MultiStageIntentModel
Assembly: Intentum.Core
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Core/Models/MultiStageIntentModel.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 46
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%66100%
Infer(...)87.5%88100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Core/Models/MultiStageIntentModel.cs

#LineLine coverage
 1using Intentum.Core.Behavior;
 2using Intentum.Core.Contracts;
 3using Intentum.Core.Intents;
 4
 5namespace Intentum.Core.Models;
 6
 7/// <summary>
 8/// Multi-stage intent model: runs a sequence of models; first result with confidence above threshold wins.
 9/// E.g. Stage1 = rules, Stage2 = clustering-based, Stage3 = LLM for ambiguous cases only.
 10/// </summary>
 11public sealed class MultiStageIntentModel : IIntentModel
 12{
 13    private readonly IReadOnlyList<(IIntentModel Model, double ConfidenceThreshold)> _stages;
 14
 15    /// <summary>
 16    /// Creates a multi-stage intent model. Each stage is (model, threshold). First stage whose result has confidence >=
 17    /// </summary>
 18    /// <param name="stages">Ordered list of (model, confidence threshold). Typical: (RuleBasedIntentModel, 0.85), (Clus
 619    public MultiStageIntentModel(IEnumerable<(IIntentModel Model, double ConfidenceThreshold)>? stages)
 620    {
 621        var list = stages?.ToList() ?? throw new ArgumentNullException(nameof(stages));
 522        if (list.Count == 0)
 123            throw new ArgumentException("At least one stage required.", nameof(stages));
 1124        _stages = list.Select(s => (s.Model, Math.Clamp(s.ConfidenceThreshold, 0, 1))).ToList();
 425    }
 26
 27    /// <inheritdoc />
 28    public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null)
 429    {
 430        Intent? lastIntent = null;
 2231        foreach (var (model, threshold) in _stages)
 632        {
 633            var intent = model.Infer(behaviorSpace, precomputedVector);
 634            lastIntent = intent;
 635            if (intent.Confidence.Score >= threshold)
 236            {
 237                var reasoning = intent.Reasoning != null
 238                    ? intent.Reasoning
 239                    : $"Stage confidence {intent.Confidence.Score:F2} >= {threshold}";
 240                return intent with { Reasoning = reasoning };
 41            }
 442        }
 243        var fallback = lastIntent!;
 244        return fallback with { Reasoning = fallback.Reasoning ?? "Multi-stage: last stage (no threshold met)" };
 445    }
 46}