| | | 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 | | /// Non-<see cref="IToolResult"/> return values pass through unchanged. |
| | | 37 | | /// </para> |
| | | 38 | | /// </remarks> |
| | | 39 | | public sealed class ToolResultFunctionMiddleware : IAIAgentBuilderPlugin |
| | | 40 | | { |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public void Configure(AIAgentBuilderPluginOptions options) |
| | | 43 | | { |
| | 7 | 44 | | ArgumentNullException.ThrowIfNull(options); |
| | | 45 | | |
| | 6 | 46 | | FunctionInvocationDelegatingAgentBuilderExtensions.Use( |
| | 6 | 47 | | options.AgentBuilder, |
| | 6 | 48 | | async (agent, context, next, cancellationToken) => |
| | 6 | 49 | | { |
| | 6 | 50 | | object? raw; |
| | 6 | 51 | | |
| | 6 | 52 | | try |
| | 6 | 53 | | { |
| | 0 | 54 | | raw = await next(context, cancellationToken).ConfigureAwait(false); |
| | 0 | 55 | | } |
| | 6 | 56 | | catch (Exception ex) |
| | 6 | 57 | | { |
| | 0 | 58 | | raw = ToolResult.UnhandledFailure(ex); |
| | 0 | 59 | | } |
| | 6 | 60 | | |
| | 0 | 61 | | if (raw is IToolResult result) |
| | 6 | 62 | | { |
| | 0 | 63 | | return result.IsSuccess |
| | 0 | 64 | | ? result.BoxedValue |
| | 0 | 65 | | : new { error = result.BoxedError }; |
| | 6 | 66 | | } |
| | 6 | 67 | | |
| | 0 | 68 | | return raw; |
| | 6 | 69 | | }); |
| | 6 | 70 | | } |
| | | 71 | | } |