| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Implements an evaluate→revise→retry loop. A critic agent evaluates the |
| | | 9 | | /// current state, a programmatic check determines pass/fail, and if it fails |
| | | 10 | | /// a reviser agent applies feedback. The loop repeats up to the configured |
| | | 11 | | /// maximum number of additional attempts. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <example> |
| | | 14 | | /// <code> |
| | | 15 | | /// var executor = new CritiqueAndReviseExecutor( |
| | | 16 | | /// criticAgent, |
| | | 17 | | /// reviserAgent, |
| | | 18 | | /// ctx => $"Review the draft in the workspace for quality.", |
| | | 19 | | /// (ctx, feedback) => $"Revise the draft. Feedback: {feedback}", |
| | | 20 | | /// (diag, feedback) => feedback?.Contains("PASS") == true, |
| | | 21 | | /// maxRetries: 2); |
| | | 22 | | /// |
| | | 23 | | /// var result = await executor.ExecuteAsync(context, cancellationToken); |
| | | 24 | | /// </code> |
| | | 25 | | /// </example> |
| | | 26 | | [DoNotAutoRegister] |
| | | 27 | | public sealed class CritiqueAndReviseExecutor : IStageExecutor |
| | | 28 | | { |
| | | 29 | | private readonly AIAgent _critic; |
| | | 30 | | private readonly AIAgent _reviser; |
| | | 31 | | private readonly Func<StageExecutionContext, string> _criticPromptFactory; |
| | | 32 | | private readonly Func<StageExecutionContext, string, string> _reviserPromptFactory; |
| | | 33 | | private readonly Func<IAgentRunDiagnostics?, string?, bool> _passCheck; |
| | | 34 | | private readonly int _maxRetries; |
| | | 35 | | private readonly Func<StageExecutionContext, string?, bool>? _postPassCheck; |
| | | 36 | | private readonly Func<StageExecutionContext, string, Task>? _onRevisionCompleted; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new <see cref="CritiqueAndReviseExecutor"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="critic">The AI agent that evaluates the current state.</param> |
| | | 42 | | /// <param name="reviser">The AI agent that applies revision feedback.</param> |
| | | 43 | | /// <param name="criticPromptFactory">Builds the prompt sent to the critic agent from the stage context.</param> |
| | | 44 | | /// <param name="reviserPromptFactory">Builds the prompt sent to the reviser agent from the stage context and critic |
| | | 45 | | /// <param name="passCheck">Determines whether the critic's response constitutes a pass.</param> |
| | | 46 | | /// <param name="maxRetries">Maximum number of revision attempts after the initial critique. Defaults to 3.</param> |
| | | 47 | | /// <param name="postPassCheck">Optional secondary check applied after <paramref name="passCheck"/> returns <see lan |
| | | 48 | | /// Returns <see langword="false"/> to override the pass and continue revision.</param> |
| | | 49 | | /// <param name="onRevisionCompleted">Optional callback invoked after the reviser runs, receiving the reviser's outp |
| | 3 | 50 | | public CritiqueAndReviseExecutor( |
| | 3 | 51 | | AIAgent critic, |
| | 3 | 52 | | AIAgent reviser, |
| | 3 | 53 | | Func<StageExecutionContext, string> criticPromptFactory, |
| | 3 | 54 | | Func<StageExecutionContext, string, string> reviserPromptFactory, |
| | 3 | 55 | | Func<IAgentRunDiagnostics?, string?, bool> passCheck, |
| | 3 | 56 | | int maxRetries = 3, |
| | 3 | 57 | | Func<StageExecutionContext, string?, bool>? postPassCheck = null, |
| | 3 | 58 | | Func<StageExecutionContext, string, Task>? onRevisionCompleted = null) |
| | | 59 | | { |
| | 3 | 60 | | _critic = critic; |
| | 3 | 61 | | _reviser = reviser; |
| | 3 | 62 | | _criticPromptFactory = criticPromptFactory; |
| | 3 | 63 | | _reviserPromptFactory = reviserPromptFactory; |
| | 3 | 64 | | _passCheck = passCheck; |
| | 3 | 65 | | _maxRetries = maxRetries; |
| | 3 | 66 | | _postPassCheck = postPassCheck; |
| | 3 | 67 | | _onRevisionCompleted = onRevisionCompleted; |
| | 3 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public async Task<StageExecutionResult> ExecuteAsync( |
| | | 72 | | StageExecutionContext context, |
| | | 73 | | CancellationToken cancellationToken) |
| | | 74 | | { |
| | 0 | 75 | | var allDiagnostics = new List<IAgentRunDiagnostics>(); |
| | 0 | 76 | | string? feedback = null; |
| | 0 | 77 | | bool passed = false; |
| | | 78 | | |
| | 0 | 79 | | for (int i = 0; i <= _maxRetries; i++) |
| | | 80 | | { |
| | 0 | 81 | | var criticPrompt = _criticPromptFactory(context); |
| | 0 | 82 | | using (context.DiagnosticsAccessor.BeginCapture()) |
| | | 83 | | { |
| | 0 | 84 | | var response = await _critic.RunAsync(criticPrompt, cancellationToken: cancellationToken); |
| | 0 | 85 | | feedback = response.GetText(); |
| | 0 | 86 | | var diag = context.DiagnosticsAccessor.LastRunDiagnostics; |
| | 0 | 87 | | if (diag is not null) |
| | | 88 | | { |
| | 0 | 89 | | allDiagnostics.Add(diag); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | passed = _passCheck(diag, feedback); |
| | 0 | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | if (passed && _postPassCheck is not null) |
| | | 96 | | { |
| | 0 | 97 | | passed = _postPassCheck(context, feedback); |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | if (passed) |
| | | 101 | | { |
| | | 102 | | break; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // Run reviser only if not the last attempt |
| | 0 | 106 | | if (i < _maxRetries) |
| | | 107 | | { |
| | 0 | 108 | | var reviserPrompt = _reviserPromptFactory(context, feedback ?? ""); |
| | 0 | 109 | | using (context.DiagnosticsAccessor.BeginCapture()) |
| | | 110 | | { |
| | 0 | 111 | | var reviserResponse = await _reviser.RunAsync(reviserPrompt, cancellationToken: cancellationToken); |
| | 0 | 112 | | var diag = context.DiagnosticsAccessor.LastRunDiagnostics; |
| | 0 | 113 | | if (diag is not null) |
| | | 114 | | { |
| | 0 | 115 | | allDiagnostics.Add(diag); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (_onRevisionCompleted is not null) |
| | | 119 | | { |
| | 0 | 120 | | var reviserText = reviserResponse.GetText(); |
| | 0 | 121 | | await _onRevisionCompleted(context, reviserText ?? ""); |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | var lastDiag = allDiagnostics.Count > 0 ? allDiagnostics[^1] : null; |
| | | 128 | | |
| | 0 | 129 | | return passed |
| | 0 | 130 | | ? StageExecutionResult.Success(context.StageName, lastDiag, feedback) |
| | 0 | 131 | | : StageExecutionResult.Failed( |
| | 0 | 132 | | context.StageName, |
| | 0 | 133 | | new InvalidOperationException( |
| | 0 | 134 | | $"Critique-and-revise did not pass after {_maxRetries + 1} attempts. Last feedback: {feedback}"), |
| | 0 | 135 | | lastDiag); |
| | 0 | 136 | | } |
| | | 137 | | } |