< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.Workflows.RegexTerminationCondition
Assembly: NexusLabs.Needlr.AgentFramework.Workflows
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Workflows/RegexTerminationCondition.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
ShouldTerminate(...)100%66100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Workflows/RegexTerminationCondition.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2using NexusLabs.Needlr.AgentFramework;
 3
 4namespace NexusLabs.Needlr.AgentFramework.Workflows;
 5
 6/// <summary>
 7/// Terminates a workflow when an agent's response matches a specified regular expression pattern.
 8/// </summary>
 9/// <remarks>
 10/// Matching is performed against the full response text of each completed agent turn.
 11/// To restrict the condition to a specific agent, provide the agent's name or executor ID via
 12/// the <c>agentId</c> constructor parameter.
 13/// </remarks>
 14public sealed class RegexTerminationCondition : IWorkflowTerminationCondition
 15{
 16    private readonly Regex _regex;
 17    private readonly string? _agentId;
 18
 19    /// <summary>
 20    /// Initializes a new instance that fires when <em>any</em> agent's response matches
 21    /// <paramref name="pattern"/> (case-insensitive, single-line).
 22    /// </summary>
 23    /// <param name="pattern">The regular expression pattern to match against response text.</param>
 24    public RegexTerminationCondition(string pattern)
 925        : this(pattern, agentId: null, RegexOptions.IgnoreCase | RegexOptions.Singleline)
 26    {
 727    }
 28
 29    /// <summary>
 30    /// Initializes a new instance that fires when a specific agent's response matches
 31    /// <paramref name="pattern"/>.
 32    /// </summary>
 33    /// <param name="pattern">The regular expression pattern to match against response text.</param>
 34    /// <param name="agentId">
 35    /// The agent name or executor ID to restrict the match to, or <see langword="null"/> to
 36    /// match any agent.
 37    /// </param>
 38    public RegexTerminationCondition(string pattern, string? agentId)
 339        : this(pattern, agentId, RegexOptions.IgnoreCase | RegexOptions.Singleline)
 40    {
 341    }
 42
 43    /// <summary>
 44    /// Initializes a new instance with full control over agent filtering and regex options.
 45    /// </summary>
 46    /// <param name="pattern">The regular expression pattern to match against response text.</param>
 47    /// <param name="agentId">
 48    /// The agent name or executor ID to restrict the match to, or <see langword="null"/> to
 49    /// match any agent.
 50    /// </param>
 51    /// <param name="options">Regex options applied when compiling and evaluating the pattern.</param>
 1352    public RegexTerminationCondition(string pattern, string? agentId, RegexOptions options)
 53    {
 1354        ArgumentException.ThrowIfNullOrWhiteSpace(pattern);
 1155        _regex = new Regex(pattern, options, TimeSpan.FromSeconds(5));
 1156        _agentId = agentId;
 1157    }
 58
 59    /// <inheritdoc/>
 60    public bool ShouldTerminate(TerminationContext context)
 61    {
 1162        ArgumentNullException.ThrowIfNull(context);
 63
 1064        if (_agentId is not null
 1065            && !string.Equals(context.AgentId, _agentId, StringComparison.Ordinal)
 1066            && !context.AgentId.StartsWith(_agentId + "_", StringComparison.Ordinal))
 67        {
 168            return false;
 69        }
 70
 971        return _regex.IsMatch(context.ResponseText);
 72    }
 73}