| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.AgentFramework; |
| | | 4 | | |
| | | 5 | | using Polly; |
| | | 6 | | using Polly.Retry; |
| | | 7 | | using Polly.Timeout; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Middleware; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Extension methods that add Needlr middleware to the <see cref="AgentFrameworkSyringe"/> |
| | | 13 | | /// and to MAF's <see cref="AIAgentBuilder"/> directly. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class NeedlrAgentMiddlewareExtensions |
| | | 16 | | { |
| | | 17 | | // ─── AgentFrameworkSyringe extensions ──────────────────────────────────── |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Adds <see cref="ToolResultFunctionMiddleware"/> to every agent created by the factory. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <remarks> |
| | | 23 | | /// This ensures that all <c>[AgentFunction]</c> methods return a safe, structured JSON |
| | | 24 | | /// payload to the LLM — even when they throw an unhandled exception. |
| | | 25 | | /// </remarks> |
| | | 26 | | public static AgentFrameworkSyringe UsingToolResultMiddleware( |
| | | 27 | | this AgentFrameworkSyringe syringe) |
| | | 28 | | { |
| | 7 | 29 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 30 | | |
| | 6 | 31 | | return syringe with |
| | 6 | 32 | | { |
| | 6 | 33 | | Plugins = (syringe.Plugins ?? []).Append(new ToolResultFunctionMiddleware()).ToList() |
| | 6 | 34 | | }; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Wraps every agent created by the factory with a default resilience pipeline: |
| | | 39 | | /// 2 retries with exponential back-off and a 120-second timeout. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <remarks> |
| | | 42 | | /// <para> |
| | | 43 | | /// Per-agent settings can be overridden by applying <see cref="AgentResilienceAttribute"/> |
| | | 44 | | /// to the agent class. |
| | | 45 | | /// </para> |
| | | 46 | | /// <para> |
| | | 47 | | /// For a custom pipeline use the <see cref="UsingResilience(AgentFrameworkSyringe, Action{ResiliencePipelineBuilder |
| | | 48 | | /// overload. |
| | | 49 | | /// </para> |
| | | 50 | | /// </remarks> |
| | | 51 | | public static AgentFrameworkSyringe UsingResilience( |
| | | 52 | | this AgentFrameworkSyringe syringe) |
| | | 53 | | { |
| | 10 | 54 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 55 | | |
| | 9 | 56 | | return syringe.UsingResilience(builder => |
| | 9 | 57 | | { |
| | 9 | 58 | | builder |
| | 9 | 59 | | .AddRetry(new RetryStrategyOptions<AgentResponse> |
| | 9 | 60 | | { |
| | 9 | 61 | | MaxRetryAttempts = 2, |
| | 9 | 62 | | BackoffType = DelayBackoffType.Exponential, |
| | 9 | 63 | | UseJitter = true, |
| | 9 | 64 | | ShouldHandle = new PredicateBuilder<AgentResponse>() |
| | 9 | 65 | | .Handle<HttpRequestException>() |
| | 9 | 66 | | .Handle<TimeoutRejectedException>() |
| | 9 | 67 | | }) |
| | 9 | 68 | | .AddTimeout(TimeSpan.FromSeconds(120)); |
| | 18 | 69 | | }); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Wraps every agent created by the factory with a custom resilience pipeline. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 76 | | /// <param name="configure">Callback to configure the <see cref="ResiliencePipelineBuilder{TResult}"/>.</param> |
| | | 77 | | public static AgentFrameworkSyringe UsingResilience( |
| | | 78 | | this AgentFrameworkSyringe syringe, |
| | | 79 | | Action<ResiliencePipelineBuilder<AgentResponse>> configure) |
| | | 80 | | { |
| | 11 | 81 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 10 | 82 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 83 | | |
| | 9 | 84 | | var builder = new ResiliencePipelineBuilder<AgentResponse>(); |
| | 9 | 85 | | configure(builder); |
| | 9 | 86 | | var globalPipeline = builder.Build(); |
| | | 87 | | |
| | 9 | 88 | | return syringe with |
| | 9 | 89 | | { |
| | 9 | 90 | | Plugins = (syringe.Plugins ?? []).Append(new AgentResiliencePlugin(globalPipeline)).ToList(), |
| | 1 | 91 | | PerAgentResilienceFactory = attr => BuildPerAgentPlugin(attr) |
| | 9 | 92 | | }; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // ─── AIAgentBuilder extensions (any-agent, not just Needlr-managed) ────── |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Adds <see cref="ToolResultFunctionMiddleware"/> to an <see cref="AIAgentBuilder"/> |
| | | 99 | | /// directly, for agents not managed by Needlr. |
| | | 100 | | /// </summary> |
| | | 101 | | public static AIAgentBuilder UseToolResultMiddleware(this AIAgentBuilder builder) |
| | | 102 | | { |
| | 0 | 103 | | ArgumentNullException.ThrowIfNull(builder); |
| | | 104 | | |
| | 0 | 105 | | var plugin = new ToolResultFunctionMiddleware(); |
| | 0 | 106 | | plugin.Configure(new AIAgentBuilderPluginOptions { AgentBuilder = builder }); |
| | 0 | 107 | | return builder; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Adds a default resilience pipeline to an <see cref="AIAgentBuilder"/> |
| | | 112 | | /// directly, for agents not managed by Needlr. |
| | | 113 | | /// </summary> |
| | | 114 | | public static AIAgentBuilder UseResilience(this AIAgentBuilder builder) |
| | | 115 | | { |
| | 0 | 116 | | ArgumentNullException.ThrowIfNull(builder); |
| | | 117 | | |
| | 0 | 118 | | var pipeline = new ResiliencePipelineBuilder<AgentResponse>() |
| | 0 | 119 | | .AddRetry(new RetryStrategyOptions<AgentResponse> |
| | 0 | 120 | | { |
| | 0 | 121 | | MaxRetryAttempts = 2, |
| | 0 | 122 | | BackoffType = DelayBackoffType.Exponential, |
| | 0 | 123 | | UseJitter = true, |
| | 0 | 124 | | ShouldHandle = new PredicateBuilder<AgentResponse>() |
| | 0 | 125 | | .Handle<HttpRequestException>() |
| | 0 | 126 | | .Handle<TimeoutRejectedException>() |
| | 0 | 127 | | }) |
| | 0 | 128 | | .AddTimeout(TimeSpan.FromSeconds(120)) |
| | 0 | 129 | | .Build(); |
| | | 130 | | |
| | 0 | 131 | | var plugin = new AgentResiliencePlugin(pipeline); |
| | 0 | 132 | | plugin.Configure(new AIAgentBuilderPluginOptions { AgentBuilder = builder }); |
| | 0 | 133 | | return builder; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Adds a custom resilience pipeline to an <see cref="AIAgentBuilder"/> |
| | | 138 | | /// directly, for agents not managed by Needlr. |
| | | 139 | | /// </summary> |
| | | 140 | | public static AIAgentBuilder UseResilience( |
| | | 141 | | this AIAgentBuilder builder, |
| | | 142 | | ResiliencePipeline<AgentResponse> pipeline) |
| | | 143 | | { |
| | 0 | 144 | | ArgumentNullException.ThrowIfNull(builder); |
| | 0 | 145 | | ArgumentNullException.ThrowIfNull(pipeline); |
| | | 146 | | |
| | 0 | 147 | | var plugin = new AgentResiliencePlugin(pipeline); |
| | 0 | 148 | | plugin.Configure(new AIAgentBuilderPluginOptions { AgentBuilder = builder }); |
| | 0 | 149 | | return builder; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // ─── Internal helpers ──────────────────────────────────────────────────── |
| | | 153 | | |
| | | 154 | | private static AgentResiliencePlugin BuildPerAgentPlugin(AgentResilienceAttribute attr) |
| | | 155 | | { |
| | 1 | 156 | | var pipelineBuilder = new ResiliencePipelineBuilder<AgentResponse>(); |
| | | 157 | | |
| | 1 | 158 | | if (attr.MaxRetries > 0) |
| | | 159 | | { |
| | 1 | 160 | | pipelineBuilder.AddRetry(new RetryStrategyOptions<AgentResponse> |
| | 1 | 161 | | { |
| | 1 | 162 | | MaxRetryAttempts = attr.MaxRetries, |
| | 1 | 163 | | BackoffType = DelayBackoffType.Exponential, |
| | 1 | 164 | | UseJitter = true, |
| | 1 | 165 | | ShouldHandle = new PredicateBuilder<AgentResponse>() |
| | 1 | 166 | | .Handle<HttpRequestException>() |
| | 1 | 167 | | .Handle<TimeoutRejectedException>() |
| | 1 | 168 | | }); |
| | | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | if (attr.TimeoutSeconds > 0) |
| | | 172 | | { |
| | 1 | 173 | | pipelineBuilder.AddTimeout(TimeSpan.FromSeconds(attr.TimeoutSeconds)); |
| | | 174 | | } |
| | | 175 | | |
| | 1 | 176 | | return new AgentResiliencePlugin(pipelineBuilder.Build()); |
| | | 177 | | } |
| | | 178 | | } |