| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | using Microsoft.Extensions.AI; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AgentFramework; |
| | | 5 | | using NexusLabs.Needlr.AgentFramework.Tools; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Middleware; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// MAF function-invocation middleware that intercepts <c>[AgentFunction]</c> return values and |
| | | 11 | | /// exceptions, ensuring the LLM always receives a structured JSON response instead of a raw |
| | | 12 | | /// stack trace. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// When an <c>[AgentFunction]</c> method returns a <see cref="IToolResult"/>: |
| | | 17 | | /// <list type="bullet"> |
| | | 18 | | /// <item> |
| | | 19 | | /// <description>Success — the LLM receives the <see cref="IToolResult.BoxedValue"/> directly.</description> |
| | | 20 | | /// </item> |
| | | 21 | | /// <item> |
| | | 22 | | /// <description> |
| | | 23 | | /// Failure — the LLM receives <c>{ "error": { … } }</c> (the <see cref="IToolResult.BoxedError"/> |
| | | 24 | | /// wrapped), and the original <see cref="Exception"/> is preserved on <see cref="IToolResult.Exception"/> |
| | | 25 | | /// for diagnostics. |
| | | 26 | | /// </description> |
| | | 27 | | /// </item> |
| | | 28 | | /// </list> |
| | | 29 | | /// </para> |
| | | 30 | | /// <para> |
| | | 31 | | /// When an <c>[AgentFunction]</c> throws an <em>unhandled</em> exception, the middleware catches it, |
| | | 32 | | /// wraps it in an <see cref="ToolResult.UnhandledFailure"/> result, and returns a safe generic error |
| | | 33 | | /// message to the LLM. <see cref="IToolResult.IsTransient"/> is <see langword="null"/> in this case. |
| | | 34 | | /// </para> |
| | | 35 | | /// <para> |
| | | 36 | | /// <see cref="OperationCanceledException"/> is intentionally <em>not</em> caught — it propagates so |
| | | 37 | | /// cooperative cancellation (parent timeouts, user cancels, structured-concurrency aborts) continues |
| | | 38 | | /// to function correctly. Tools that legitimately catch and translate cancellation should do so |
| | | 39 | | /// inside the tool body, not rely on this middleware. |
| | | 40 | | /// </para> |
| | | 41 | | /// <para> |
| | | 42 | | /// Non-<see cref="IToolResult"/> return values pass through unchanged. |
| | | 43 | | /// </para> |
| | | 44 | | /// </remarks> |
| | | 45 | | public sealed class ToolResultFunctionMiddleware : IAIAgentBuilderPlugin |
| | | 46 | | { |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public void Configure(AIAgentBuilderPluginOptions options) |
| | | 49 | | { |
| | 7 | 50 | | ArgumentNullException.ThrowIfNull(options); |
| | | 51 | | |
| | 6 | 52 | | FunctionInvocationDelegatingAgentBuilderExtensions.Use( |
| | 6 | 53 | | options.AgentBuilder, |
| | 6 | 54 | | async (agent, context, next, cancellationToken) => |
| | 6 | 55 | | await HandleInvocationAsync( |
| | 0 | 56 | | invokeNext: ct => next(context, ct), |
| | 6 | 57 | | cancellationToken: cancellationToken).ConfigureAwait(false)); |
| | 6 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Core middleware logic: invoke <paramref name="invokeNext"/>, translate exceptions into |
| | | 62 | | /// <see cref="IToolResult"/> failures, and unwrap <see cref="IToolResult"/> returns into |
| | | 63 | | /// LLM-facing <see cref="IToolResult.BoxedValue"/> or <c>{ error: BoxedError }</c>. |
| | | 64 | | /// Cooperative <see cref="OperationCanceledException"/> propagates unchanged so cancellation |
| | | 65 | | /// signals are not swallowed. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// Internal-but-exposed-via-<c>InternalsVisibleTo</c> for direct unit testing — exercising the |
| | | 69 | | /// translation logic without standing up a full agent pipeline. |
| | | 70 | | /// </remarks> |
| | | 71 | | internal static async ValueTask<object?> HandleInvocationAsync( |
| | | 72 | | Func<CancellationToken, ValueTask<object?>> invokeNext, |
| | | 73 | | CancellationToken cancellationToken) |
| | | 74 | | { |
| | | 75 | | object? raw; |
| | | 76 | | |
| | | 77 | | try |
| | | 78 | | { |
| | 6 | 79 | | raw = await invokeNext(cancellationToken).ConfigureAwait(false); |
| | 3 | 80 | | } |
| | 2 | 81 | | catch (OperationCanceledException) |
| | | 82 | | { |
| | 2 | 83 | | throw; |
| | | 84 | | } |
| | | 85 | | catch (Exception ex) |
| | | 86 | | { |
| | 1 | 87 | | raw = ToolResult.UnhandledFailure(ex); |
| | 1 | 88 | | } |
| | | 89 | | |
| | 4 | 90 | | if (raw is IToolResult result) |
| | | 91 | | { |
| | 3 | 92 | | return result.IsSuccess |
| | 3 | 93 | | ? result.BoxedValue |
| | 3 | 94 | | : new { error = result.BoxedError }; |
| | | 95 | | } |
| | | 96 | | |
| | 1 | 97 | | return raw; |
| | 4 | 98 | | } |
| | | 99 | | } |