| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using NexusLabs.Needlr.AgentFramework; |
| | | 3 | | |
| | | 4 | | namespace 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> |
| | | 14 | | public 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) |
| | 9 | 25 | | : this(pattern, agentId: null, RegexOptions.IgnoreCase | RegexOptions.Singleline) |
| | | 26 | | { |
| | 7 | 27 | | } |
| | | 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) |
| | 3 | 39 | | : this(pattern, agentId, RegexOptions.IgnoreCase | RegexOptions.Singleline) |
| | | 40 | | { |
| | 3 | 41 | | } |
| | | 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> |
| | 13 | 52 | | public RegexTerminationCondition(string pattern, string? agentId, RegexOptions options) |
| | | 53 | | { |
| | 13 | 54 | | ArgumentException.ThrowIfNullOrWhiteSpace(pattern); |
| | 11 | 55 | | _regex = new Regex(pattern, options, TimeSpan.FromSeconds(5)); |
| | 11 | 56 | | _agentId = agentId; |
| | 11 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc/> |
| | | 60 | | public bool ShouldTerminate(TerminationContext context) |
| | | 61 | | { |
| | 11 | 62 | | ArgumentNullException.ThrowIfNull(context); |
| | | 63 | | |
| | 10 | 64 | | if (_agentId is not null |
| | 10 | 65 | | && !string.Equals(context.AgentId, _agentId, StringComparison.Ordinal) |
| | 10 | 66 | | && !context.AgentId.StartsWith(_agentId + "_", StringComparison.Ordinal)) |
| | | 67 | | { |
| | 1 | 68 | | return false; |
| | | 69 | | } |
| | | 70 | | |
| | 9 | 71 | | return _regex.IsMatch(context.ResponseText); |
| | | 72 | | } |
| | | 73 | | } |