| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.AgentFramework; |
| | | 4 | | |
| | | 5 | | using Polly; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Middleware; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// MAF agent-level middleware that wraps each agent run call in a |
| | | 11 | | /// <see cref="ResiliencePipeline{TResult}"/> from Microsoft.Extensions.Resilience / Polly. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// This is the right middleware level for agent resilience because it wraps the entire |
| | | 15 | | /// <c>RunAsync()</c> call and catches LLM failures, tool failures, and orchestration errors |
| | | 16 | | /// together. Streaming <c>RunStreamingAsync()</c> passes through without retry. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class AgentResiliencePlugin : IAIAgentBuilderPlugin |
| | | 19 | | { |
| | | 20 | | private readonly ResiliencePipeline<AgentResponse> _pipeline; |
| | | 21 | | |
| | | 22 | | /// <param name="pipeline"> |
| | | 23 | | /// The resilience pipeline to wrap around each <c>RunAsync</c> call. |
| | | 24 | | /// </param> |
| | 14 | 25 | | public AgentResiliencePlugin(ResiliencePipeline<AgentResponse> pipeline) |
| | | 26 | | { |
| | 14 | 27 | | ArgumentNullException.ThrowIfNull(pipeline); |
| | 13 | 28 | | _pipeline = pipeline; |
| | 13 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public void Configure(AIAgentBuilderPluginOptions options) |
| | | 33 | | { |
| | 11 | 34 | | ArgumentNullException.ThrowIfNull(options); |
| | | 35 | | |
| | 10 | 36 | | options.AgentBuilder.Use( |
| | 10 | 37 | | // Non-streaming: wrap in resilience pipeline. |
| | 10 | 38 | | async (messages, session, runOptions, innerAgent, cancellationToken) => |
| | 4 | 39 | | await _pipeline.ExecuteAsync( |
| | 6 | 40 | | async ct => await innerAgent.RunAsync(messages, session, runOptions, ct).ConfigureAwait(false), |
| | 4 | 41 | | cancellationToken) |
| | 4 | 42 | | .ConfigureAwait(false), |
| | 10 | 43 | | |
| | 10 | 44 | | // Streaming: pass through without retry — retrying a partially-consumed stream |
| | 10 | 45 | | // is not meaningful. |
| | 10 | 46 | | (messages, session, runOptions, innerAgent, cancellationToken) => |
| | 10 | 47 | | innerAgent.RunStreamingAsync(messages, session, runOptions, cancellationToken)); |
| | 10 | 48 | | } |
| | | 49 | | } |