| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | |
| | | 4 | | using Microsoft.Extensions.AI; |
| | | 5 | | |
| | | 6 | | using NexusLabs.Needlr.AgentFramework.Progress; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A <see cref="FunctionInvokingChatClient"/> that records per-tool-call diagnostics, |
| | | 12 | | /// OTel metrics, and Activity spans for every function invocation. This is the MEAI-native |
| | | 13 | | /// equivalent of the MAF <c>DiagnosticsFunctionCallingMiddleware</c> in Workflows. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// Use this when the chat pipeline includes <c>FunctionInvokingChatClient</c> (auto |
| | | 18 | | /// tool calling) rather than the <c>IterativeAgentLoop</c>. The loop does its own |
| | | 19 | | /// tool-call recording; using both would produce duplicates. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// Records are written to the AsyncLocal <see cref="AgentRunDiagnosticsBuilder"/> and |
| | | 23 | | /// OTel metrics via <see cref="IAgentMetrics"/>. Progress events are emitted to |
| | | 24 | | /// <see cref="IProgressReporterAccessor"/> when available. |
| | | 25 | | /// </para> |
| | | 26 | | /// </remarks> |
| | | 27 | | [DoNotAutoRegister] |
| | | 28 | | public sealed class DiagnosticsFunctionInvokingChatClient : FunctionInvokingChatClient |
| | | 29 | | { |
| | | 30 | | private readonly IAgentMetrics? _metrics; |
| | | 31 | | private readonly IProgressReporterAccessor? _progressAccessor; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates a new diagnostics-enabled <see cref="FunctionInvokingChatClient"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="innerClient">The inner chat client to delegate to.</param> |
| | | 37 | | /// <param name="metrics">Optional OTel metrics recorder.</param> |
| | | 38 | | /// <param name="progressAccessor">Optional progress reporter for real-time events.</param> |
| | | 39 | | public DiagnosticsFunctionInvokingChatClient( |
| | | 40 | | IChatClient innerClient, |
| | | 41 | | IAgentMetrics? metrics = null, |
| | | 42 | | IProgressReporterAccessor? progressAccessor = null) |
| | 6 | 43 | | : base(innerClient) |
| | | 44 | | { |
| | 6 | 45 | | _metrics = metrics; |
| | 6 | 46 | | _progressAccessor = progressAccessor; |
| | 6 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | protected override async ValueTask<object?> InvokeFunctionAsync( |
| | | 51 | | FunctionInvocationContext context, |
| | | 52 | | CancellationToken cancellationToken) |
| | | 53 | | { |
| | 6 | 54 | | var diagnosticsBuilder = AgentRunDiagnosticsBuilder.GetCurrent(); |
| | 6 | 55 | | var sequence = diagnosticsBuilder?.NextToolCallSequence() ?? -1; |
| | 6 | 56 | | var startedAt = DateTimeOffset.UtcNow; |
| | 6 | 57 | | var stopwatch = Stopwatch.StartNew(); |
| | | 58 | | |
| | 6 | 59 | | var toolName = context.Function?.Name ?? "unknown"; |
| | | 60 | | |
| | 6 | 61 | | using var activity = _metrics?.ActivitySource.StartActivity( |
| | 6 | 62 | | $"agent.tool {toolName}", ActivityKind.Internal); |
| | 6 | 63 | | activity?.SetTag("agent.tool.name", toolName); |
| | 6 | 64 | | activity?.SetTag("agent.tool.sequence", sequence); |
| | 6 | 65 | | activity?.SetTag("gen_ai.agent.name", diagnosticsBuilder?.AgentName); |
| | | 66 | | |
| | 6 | 67 | | var reporter = _progressAccessor?.Current; |
| | 6 | 68 | | reporter?.Report(new ToolCallStartedEvent( |
| | 6 | 69 | | Timestamp: startedAt, |
| | 6 | 70 | | WorkflowId: reporter.WorkflowId, |
| | 6 | 71 | | AgentId: reporter.AgentId, |
| | 6 | 72 | | ParentAgentId: diagnosticsBuilder?.ParentAgentName, |
| | 6 | 73 | | Depth: reporter.Depth, |
| | 6 | 74 | | SequenceNumber: reporter.NextSequence(), |
| | 6 | 75 | | ToolName: toolName)); |
| | | 76 | | |
| | 6 | 77 | | var customMetrics = new ConcurrentDictionary<string, object?>(StringComparer.OrdinalIgnoreCase); |
| | 6 | 78 | | ToolMetricsAccessor.CurrentToolMetrics.Value = customMetrics; |
| | | 79 | | |
| | | 80 | | try |
| | | 81 | | { |
| | 6 | 82 | | var result = await base.InvokeFunctionAsync(context, cancellationToken) |
| | 6 | 83 | | .ConfigureAwait(false); |
| | | 84 | | |
| | 5 | 85 | | stopwatch.Stop(); |
| | | 86 | | |
| | 5 | 87 | | activity?.SetTag("status", "success"); |
| | | 88 | | |
| | 5 | 89 | | if (activity is not null && customMetrics.Count > 0) |
| | | 90 | | { |
| | 0 | 91 | | foreach (var (key, value) in customMetrics) |
| | | 92 | | { |
| | 0 | 93 | | activity.SetTag($"tool.custom.{key}", value); |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | 5 | 97 | | _metrics?.RecordToolCall( |
| | 5 | 98 | | toolName, stopwatch.Elapsed, succeeded: true, |
| | 5 | 99 | | agentName: diagnosticsBuilder?.AgentName); |
| | | 100 | | |
| | 5 | 101 | | var arguments = SnapshotArguments(context.Arguments); |
| | | 102 | | |
| | 5 | 103 | | var toolDiag = new ToolCallDiagnostics( |
| | 5 | 104 | | Sequence: sequence, |
| | 5 | 105 | | ToolName: toolName, |
| | 5 | 106 | | Duration: stopwatch.Elapsed, |
| | 5 | 107 | | Succeeded: true, |
| | 5 | 108 | | ErrorMessage: null, |
| | 5 | 109 | | StartedAt: startedAt, |
| | 5 | 110 | | CompletedAt: DateTimeOffset.UtcNow, |
| | 5 | 111 | | CustomMetrics: customMetrics.Count > 0 ? customMetrics : null) |
| | 5 | 112 | | { |
| | 5 | 113 | | AgentName = diagnosticsBuilder?.AgentName, |
| | 5 | 114 | | Arguments = arguments, |
| | 5 | 115 | | Result = result, |
| | 5 | 116 | | ArgumentsCharCount = DiagnosticsCharCounter.JsonLength(arguments), |
| | 5 | 117 | | ResultCharCount = DiagnosticsCharCounter.JsonLength(result), |
| | 5 | 118 | | }; |
| | | 119 | | |
| | 5 | 120 | | diagnosticsBuilder?.AddToolCall(toolDiag); |
| | | 121 | | |
| | 5 | 122 | | reporter?.Report(new ToolCallCompletedEvent( |
| | 5 | 123 | | Timestamp: DateTimeOffset.UtcNow, |
| | 5 | 124 | | WorkflowId: reporter.WorkflowId, |
| | 5 | 125 | | AgentId: reporter.AgentId, |
| | 5 | 126 | | ParentAgentId: diagnosticsBuilder?.ParentAgentName, |
| | 5 | 127 | | Depth: reporter.Depth, |
| | 5 | 128 | | SequenceNumber: reporter.NextSequence(), |
| | 5 | 129 | | ToolName: toolName, |
| | 5 | 130 | | Duration: stopwatch.Elapsed, |
| | 5 | 131 | | CustomMetrics: customMetrics.Count > 0 ? customMetrics : null)); |
| | | 132 | | |
| | 5 | 133 | | return result; |
| | | 134 | | } |
| | 1 | 135 | | catch (Exception ex) |
| | | 136 | | { |
| | 1 | 137 | | stopwatch.Stop(); |
| | | 138 | | |
| | 1 | 139 | | activity?.SetStatus(ActivityStatusCode.Error, ex.Message); |
| | 1 | 140 | | activity?.SetTag("status", "failed"); |
| | | 141 | | |
| | 1 | 142 | | _metrics?.RecordToolCall( |
| | 1 | 143 | | toolName, stopwatch.Elapsed, succeeded: false, |
| | 1 | 144 | | agentName: diagnosticsBuilder?.AgentName); |
| | | 145 | | |
| | 1 | 146 | | var arguments = SnapshotArguments(context.Arguments); |
| | | 147 | | |
| | 1 | 148 | | var failedToolDiag = new ToolCallDiagnostics( |
| | 1 | 149 | | Sequence: sequence, |
| | 1 | 150 | | ToolName: toolName, |
| | 1 | 151 | | Duration: stopwatch.Elapsed, |
| | 1 | 152 | | Succeeded: false, |
| | 1 | 153 | | ErrorMessage: ex.Message, |
| | 1 | 154 | | StartedAt: startedAt, |
| | 1 | 155 | | CompletedAt: DateTimeOffset.UtcNow, |
| | 1 | 156 | | CustomMetrics: customMetrics.Count > 0 ? customMetrics : null) |
| | 1 | 157 | | { |
| | 1 | 158 | | AgentName = diagnosticsBuilder?.AgentName, |
| | 1 | 159 | | Arguments = arguments, |
| | 1 | 160 | | ArgumentsCharCount = DiagnosticsCharCounter.JsonLength(arguments), |
| | 1 | 161 | | }; |
| | | 162 | | |
| | 1 | 163 | | diagnosticsBuilder?.AddToolCall(failedToolDiag); |
| | | 164 | | |
| | 1 | 165 | | reporter?.Report(new ToolCallFailedEvent( |
| | 1 | 166 | | Timestamp: DateTimeOffset.UtcNow, |
| | 1 | 167 | | WorkflowId: reporter.WorkflowId, |
| | 1 | 168 | | AgentId: reporter.AgentId, |
| | 1 | 169 | | ParentAgentId: diagnosticsBuilder?.ParentAgentName, |
| | 1 | 170 | | Depth: reporter.Depth, |
| | 1 | 171 | | SequenceNumber: reporter.NextSequence(), |
| | 1 | 172 | | ToolName: toolName, |
| | 1 | 173 | | ErrorMessage: ex.Message, |
| | 1 | 174 | | Duration: stopwatch.Elapsed)); |
| | | 175 | | |
| | 1 | 176 | | throw; |
| | | 177 | | } |
| | | 178 | | finally |
| | | 179 | | { |
| | 6 | 180 | | ToolMetricsAccessor.CurrentToolMetrics.Value = null; |
| | | 181 | | } |
| | 5 | 182 | | } |
| | | 183 | | |
| | | 184 | | private static IReadOnlyDictionary<string, object?>? SnapshotArguments( |
| | | 185 | | IReadOnlyDictionary<string, object?>? arguments) |
| | | 186 | | { |
| | 6 | 187 | | if (arguments is null || arguments.Count == 0) |
| | | 188 | | { |
| | 0 | 189 | | return null; |
| | | 190 | | } |
| | | 191 | | |
| | 6 | 192 | | return new Dictionary<string, object?>(arguments); |
| | | 193 | | } |
| | | 194 | | } |