< 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: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 75
Line coverage: 100%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
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(...)80%1010100%

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)
 1625        : this(keyword, agentId: null, StringComparison.OrdinalIgnoreCase)
 26    {
 1427    }
 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)
 1239        : this(keyword, agentId, StringComparison.OrdinalIgnoreCase)
 40    {
 1241    }
 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>
 3052    public KeywordTerminationCondition(string keyword, string? agentId, StringComparison comparison)
 53    {
 3054        ArgumentException.ThrowIfNullOrWhiteSpace(keyword);
 2855        _keyword = keyword;
 2856        _agentId = agentId;
 2857        _comparison = comparison;
 2858    }
 59
 60    /// <inheritdoc/>
 61    public bool ShouldTerminate(TerminationContext context)
 62    {
 4663        ArgumentNullException.ThrowIfNull(context);
 64
 4565        if (_agentId is not null
 4566            && !string.Equals(context.AgentId, _agentId, StringComparison.Ordinal)
 4567            && !context.AgentId.StartsWith(_agentId + "_", StringComparison.Ordinal))
 68        {
 2169            return false;
 70        }
 71
 2472        var text = context.LastMessage?.Text ?? string.Empty;
 2473        return text.Contains(_keyword, _comparison);
 74    }
 75}