< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.Workflows.KeywordTerminationCondition
Assembly: NexusLabs.Needlr.AgentFramework.Workflows
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Workflows/KeywordTerminationCondition.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 74
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/KeywordTerminationCondition.cs

#LineLine coverage
 1using NexusLabs.Needlr.AgentFramework;
 2
 3namespace NexusLabs.Needlr.AgentFramework.Workflows;
 4
 5/// <summary>
 6/// Terminates a workflow when an agent's response contains a specified keyword.
 7/// </summary>
 8/// <remarks>
 9/// The check is a simple substring match (case-insensitive by default). To restrict the
 10/// condition to a specific agent, provide the agent's name or executor ID via the
 11/// <c>agentId</c> constructor parameter.
 12/// </remarks>
 13public sealed class KeywordTerminationCondition : IWorkflowTerminationCondition
 14{
 15    private readonly string _keyword;
 16    private readonly string? _agentId;
 17    private readonly StringComparison _comparison;
 18
 19    /// <summary>
 20    /// Initializes a new instance that fires when <em>any</em> agent's response contains
 21    /// <paramref name="keyword"/> (case-insensitive).
 22    /// </summary>
 23    /// <param name="keyword">The keyword to look for in the response text.</param>
 24    public KeywordTerminationCondition(string keyword)
 1425        : this(keyword, agentId: null, StringComparison.OrdinalIgnoreCase)
 26    {
 1227    }
 28
 29    /// <summary>
 30    /// Initializes a new instance that fires when a specific agent's response contains
 31    /// <paramref name="keyword"/>.
 32    /// </summary>
 33    /// <param name="keyword">The keyword to look for in the 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 KeywordTerminationCondition(string keyword, string? agentId)
 639        : this(keyword, agentId, StringComparison.OrdinalIgnoreCase)
 40    {
 641    }
 42
 43    /// <summary>
 44    /// Initializes a new instance with full control over agent filtering and comparison.
 45    /// </summary>
 46    /// <param name="keyword">The keyword to look for in the 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="comparison">The string comparison used when searching for the keyword.</param>
 2252    public KeywordTerminationCondition(string keyword, string? agentId, StringComparison comparison)
 53    {
 2254        ArgumentException.ThrowIfNullOrWhiteSpace(keyword);
 2055        _keyword = keyword;
 2056        _agentId = agentId;
 2057        _comparison = comparison;
 2058    }
 59
 60    /// <inheritdoc/>
 61    public bool ShouldTerminate(TerminationContext context)
 62    {
 2263        ArgumentNullException.ThrowIfNull(context);
 64
 2165        if (_agentId is not null
 2166            && !string.Equals(context.AgentId, _agentId, StringComparison.Ordinal)
 2167            && !context.AgentId.StartsWith(_agentId + "_", StringComparison.Ordinal))
 68        {
 369            return false;
 70        }
 71
 1872        return context.ResponseText.Contains(_keyword, _comparison);
 73    }
 74}