| | | 1 | | using NexusLabs.Needlr.AgentFramework; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 13 | | public 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) |
| | 14 | 25 | | : this(keyword, agentId: null, StringComparison.OrdinalIgnoreCase) |
| | | 26 | | { |
| | 12 | 27 | | } |
| | | 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) |
| | 6 | 39 | | : this(keyword, agentId, StringComparison.OrdinalIgnoreCase) |
| | | 40 | | { |
| | 6 | 41 | | } |
| | | 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> |
| | 22 | 52 | | public KeywordTerminationCondition(string keyword, string? agentId, StringComparison comparison) |
| | | 53 | | { |
| | 22 | 54 | | ArgumentException.ThrowIfNullOrWhiteSpace(keyword); |
| | 20 | 55 | | _keyword = keyword; |
| | 20 | 56 | | _agentId = agentId; |
| | 20 | 57 | | _comparison = comparison; |
| | 20 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public bool ShouldTerminate(TerminationContext context) |
| | | 62 | | { |
| | 22 | 63 | | ArgumentNullException.ThrowIfNull(context); |
| | | 64 | | |
| | 21 | 65 | | if (_agentId is not null |
| | 21 | 66 | | && !string.Equals(context.AgentId, _agentId, StringComparison.Ordinal) |
| | 21 | 67 | | && !context.AgentId.StartsWith(_agentId + "_", StringComparison.Ordinal)) |
| | | 68 | | { |
| | 3 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | |
| | 18 | 72 | | return context.ResponseText.Contains(_keyword, _comparison); |
| | | 73 | | } |
| | | 74 | | } |