| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | using Microsoft.Agents.AI; |
| | | 5 | | using Microsoft.Extensions.AI; |
| | | 6 | | |
| | | 7 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Diagnostics; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Outermost middleware layer: wraps <c>agent.RunAsync()</c> and |
| | | 13 | | /// <c>agent.RunStreamingAsync()</c> to capture per-run diagnostics including |
| | | 14 | | /// total duration, message counts, and success/failure state. Emits |
| | | 15 | | /// <see cref="IAgentMetrics"/> counters on start and completion. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Both the non-streaming and streaming paths produce equivalent |
| | | 19 | | /// <see cref="IAgentRunDiagnostics"/> via <see cref="IAgentDiagnosticsWriter.Set"/>. |
| | | 20 | | /// </remarks> |
| | | 21 | | internal sealed class DiagnosticsAgentRunMiddleware |
| | | 22 | | { |
| | | 23 | | private readonly string _agentName; |
| | | 24 | | private readonly IAgentDiagnosticsWriter _writer; |
| | | 25 | | private readonly IAgentMetrics _metrics; |
| | | 26 | | |
| | 46 | 27 | | internal DiagnosticsAgentRunMiddleware( |
| | 46 | 28 | | string agentName, |
| | 46 | 29 | | IAgentDiagnosticsWriter writer, |
| | 46 | 30 | | IAgentMetrics metrics) |
| | | 31 | | { |
| | 46 | 32 | | _agentName = agentName; |
| | 46 | 33 | | _writer = writer; |
| | 46 | 34 | | _metrics = metrics; |
| | 46 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal async IAsyncEnumerable<AgentResponseUpdate> HandleStreamingAsync( |
| | | 38 | | IEnumerable<ChatMessage> messages, |
| | | 39 | | AgentSession? session, |
| | | 40 | | AgentRunOptions? options, |
| | | 41 | | AIAgent innerAgent, |
| | | 42 | | [EnumeratorCancellation] CancellationToken cancellationToken) |
| | | 43 | | { |
| | 25 | 44 | | var resolvedName = !string.IsNullOrEmpty(innerAgent.Name) ? innerAgent.Name : _agentName; |
| | | 45 | | |
| | 25 | 46 | | _metrics.RecordRunStarted(resolvedName); |
| | 25 | 47 | | using var activity = _metrics.ActivitySource.StartActivity($"agent.run {resolvedName}", ActivityKind.Internal); |
| | 25 | 48 | | activity?.SetTag("gen_ai.agent.name", resolvedName); |
| | 25 | 49 | | activity?.SetTag("gen_ai.agent.streaming", true); |
| | | 50 | | |
| | 25 | 51 | | using var builder = AgentRunDiagnosticsBuilder.StartNew(resolvedName); |
| | | 52 | | |
| | 25 | 53 | | var messageList = messages as IReadOnlyList<ChatMessage> ?? messages.ToList(); |
| | 25 | 54 | | builder.RecordInputMessageCount(messageList.Count); |
| | 25 | 55 | | builder.RecordInputMessages(messageList); |
| | | 56 | | |
| | 25 | 57 | | var messageIds = new HashSet<string>(StringComparer.Ordinal); |
| | 25 | 58 | | var accumulated = new List<AgentResponseUpdate>(); |
| | 25 | 59 | | Exception? failure = null; |
| | | 60 | | |
| | 25 | 61 | | var enumerator = innerAgent |
| | 25 | 62 | | .RunStreamingAsync(messageList, session, options, cancellationToken) |
| | 25 | 63 | | .GetAsyncEnumerator(cancellationToken); |
| | | 64 | | try |
| | | 65 | | { |
| | | 66 | | while (true) |
| | | 67 | | { |
| | | 68 | | AgentResponseUpdate update; |
| | | 69 | | try |
| | | 70 | | { |
| | 58 | 71 | | if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) |
| | | 72 | | { |
| | 22 | 73 | | break; |
| | | 74 | | } |
| | 33 | 75 | | update = enumerator.Current; |
| | 33 | 76 | | } |
| | 3 | 77 | | catch (Exception ex) |
| | | 78 | | { |
| | 3 | 79 | | failure = ex; |
| | 3 | 80 | | break; |
| | | 81 | | } |
| | | 82 | | |
| | 33 | 83 | | if (!string.IsNullOrEmpty(update.MessageId)) |
| | | 84 | | { |
| | 12 | 85 | | messageIds.Add(update.MessageId); |
| | | 86 | | } |
| | 33 | 87 | | accumulated.Add(update); |
| | | 88 | | |
| | 33 | 89 | | yield return update; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | finally |
| | | 93 | | { |
| | 25 | 94 | | await enumerator.DisposeAsync().ConfigureAwait(false); |
| | | 95 | | } |
| | | 96 | | |
| | 25 | 97 | | builder.RecordOutputMessageCount(messageIds.Count); |
| | 25 | 98 | | builder.RecordOutputResponse(SynthesizeResponse(accumulated)); |
| | | 99 | | |
| | 25 | 100 | | if (failure is not null) |
| | | 101 | | { |
| | 3 | 102 | | builder.RecordFailure(failure.Message); |
| | 3 | 103 | | activity?.SetStatus(ActivityStatusCode.Error, failure.Message); |
| | | 104 | | } |
| | | 105 | | |
| | 25 | 106 | | var diagnostics = builder.Build(); |
| | 25 | 107 | | _writer.Set(diagnostics); |
| | 25 | 108 | | _metrics.RecordRunCompleted(diagnostics); |
| | | 109 | | |
| | 25 | 110 | | activity?.SetTag("status", diagnostics.Succeeded ? "success" : "failed"); |
| | 25 | 111 | | activity?.SetTag("gen_ai.usage.input_tokens", diagnostics.AggregateTokenUsage.InputTokens); |
| | 25 | 112 | | activity?.SetTag("gen_ai.usage.output_tokens", diagnostics.AggregateTokenUsage.OutputTokens); |
| | 25 | 113 | | activity?.SetTag("gen_ai.usage.total_tokens", diagnostics.AggregateTokenUsage.TotalTokens); |
| | | 114 | | |
| | 25 | 115 | | if (failure is not null) |
| | | 116 | | { |
| | 3 | 117 | | throw failure; |
| | | 118 | | } |
| | 22 | 119 | | } |
| | | 120 | | |
| | | 121 | | internal async Task<AgentResponse> HandleAsync( |
| | | 122 | | IEnumerable<ChatMessage> messages, |
| | | 123 | | AgentSession? session, |
| | | 124 | | AgentRunOptions? options, |
| | | 125 | | AIAgent innerAgent, |
| | | 126 | | CancellationToken cancellationToken) |
| | | 127 | | { |
| | | 128 | | // Resolve the agent name at runtime from the inner agent. The plugin creates |
| | | 129 | | // this middleware before the agent is fully built, so the name passed at |
| | | 130 | | // construction time is a fallback. |
| | 21 | 131 | | var resolvedName = !string.IsNullOrEmpty(innerAgent.Name) ? innerAgent.Name : _agentName; |
| | | 132 | | |
| | 21 | 133 | | _metrics.RecordRunStarted(resolvedName); |
| | 21 | 134 | | using var activity = _metrics.ActivitySource.StartActivity($"agent.run {resolvedName}", ActivityKind.Internal); |
| | 21 | 135 | | activity?.SetTag("gen_ai.agent.name", resolvedName); |
| | | 136 | | |
| | 21 | 137 | | using var builder = AgentRunDiagnosticsBuilder.StartNew(resolvedName); |
| | | 138 | | |
| | | 139 | | try |
| | | 140 | | { |
| | 21 | 141 | | var messageList = messages as IReadOnlyList<ChatMessage> ?? messages.ToList(); |
| | 21 | 142 | | builder.RecordInputMessageCount(messageList.Count); |
| | 21 | 143 | | builder.RecordInputMessages(messageList); |
| | | 144 | | |
| | 21 | 145 | | var response = await innerAgent.RunAsync(messageList, session, options, cancellationToken) |
| | 21 | 146 | | .ConfigureAwait(false); |
| | | 147 | | |
| | 21 | 148 | | builder.RecordOutputMessageCount(response.Messages?.Count ?? 0); |
| | 21 | 149 | | builder.RecordOutputResponse(response); |
| | | 150 | | |
| | 21 | 151 | | return response; |
| | | 152 | | } |
| | 0 | 153 | | catch (Exception ex) |
| | | 154 | | { |
| | 0 | 155 | | builder.RecordFailure(ex.Message); |
| | 0 | 156 | | activity?.SetStatus(ActivityStatusCode.Error, ex.Message); |
| | 0 | 157 | | throw; |
| | | 158 | | } |
| | | 159 | | finally |
| | | 160 | | { |
| | 21 | 161 | | var diagnostics = builder.Build(); |
| | 21 | 162 | | _writer.Set(diagnostics); |
| | 21 | 163 | | _metrics.RecordRunCompleted(diagnostics); |
| | | 164 | | |
| | 21 | 165 | | activity?.SetTag("status", diagnostics.Succeeded ? "success" : "failed"); |
| | 21 | 166 | | activity?.SetTag("gen_ai.usage.input_tokens", diagnostics.AggregateTokenUsage.InputTokens); |
| | 21 | 167 | | activity?.SetTag("gen_ai.usage.output_tokens", diagnostics.AggregateTokenUsage.OutputTokens); |
| | 21 | 168 | | activity?.SetTag("gen_ai.usage.total_tokens", diagnostics.AggregateTokenUsage.TotalTokens); |
| | | 169 | | } |
| | 21 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Synthesizes an <see cref="AgentResponse"/> from the raw stream of |
| | | 174 | | /// <see cref="AgentResponseUpdate"/> items observed during a streaming run. |
| | | 175 | | /// Groups contents by <c>MessageId</c> so each logical message becomes one |
| | | 176 | | /// <see cref="ChatMessage"/>. Updates with no <c>MessageId</c> are grouped |
| | | 177 | | /// positionally so partial streams (mid-failure) still capture what was |
| | | 178 | | /// observed. Returns <see langword="null"/> when no updates were received. |
| | | 179 | | /// </summary> |
| | | 180 | | private static AgentResponse? SynthesizeResponse(List<AgentResponseUpdate> updates) |
| | | 181 | | { |
| | 25 | 182 | | if (updates.Count == 0) |
| | | 183 | | { |
| | 1 | 184 | | return null; |
| | | 185 | | } |
| | | 186 | | |
| | 24 | 187 | | var order = new List<string>(); |
| | 24 | 188 | | var groups = new Dictionary<string, (ChatRole Role, List<AIContent> Contents, string? AuthorName)>(StringCompare |
| | | 189 | | |
| | 114 | 190 | | for (var i = 0; i < updates.Count; i++) |
| | | 191 | | { |
| | 33 | 192 | | var u = updates[i]; |
| | 33 | 193 | | var key = !string.IsNullOrEmpty(u.MessageId) ? u.MessageId : $"__ordinal_{i}"; |
| | 33 | 194 | | if (!groups.TryGetValue(key, out var entry)) |
| | | 195 | | { |
| | 29 | 196 | | entry = (u.Role ?? ChatRole.Assistant, new List<AIContent>(), u.AuthorName); |
| | 29 | 197 | | groups[key] = entry; |
| | 29 | 198 | | order.Add(key); |
| | | 199 | | } |
| | 33 | 200 | | if (u.Contents is { Count: > 0 }) |
| | | 201 | | { |
| | 33 | 202 | | entry.Contents.AddRange(u.Contents); |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | 24 | 206 | | var messages = new List<ChatMessage>(order.Count); |
| | 106 | 207 | | foreach (var k in order) |
| | | 208 | | { |
| | 29 | 209 | | var (role, contents, authorName) = groups[k]; |
| | 29 | 210 | | var msg = new ChatMessage(role, contents); |
| | 29 | 211 | | if (!string.IsNullOrEmpty(authorName)) |
| | | 212 | | { |
| | 29 | 213 | | msg.AuthorName = authorName; |
| | | 214 | | } |
| | 29 | 215 | | messages.Add(msg); |
| | | 216 | | } |
| | | 217 | | |
| | 24 | 218 | | return new AgentResponse(messages); |
| | | 219 | | } |
| | | 220 | | } |