| | | 1 | | using Microsoft.Extensions.AI; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AgentFramework; |
| | | 5 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 6 | | using NexusLabs.Needlr.AgentFramework.Progress; |
| | | 7 | | using NexusLabs.Needlr.AgentFramework.Workflows.Budget; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Diagnostics; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Extension methods for wiring agent diagnostics into the <see cref="AgentFrameworkSyringe"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class DiagnosticsExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Enables agent-run diagnostics for every agent created by the factory. |
| | | 18 | | /// Wires the agent-run, chat-completion, and function-calling middleware layers, |
| | | 19 | | /// and emits <see cref="IAgentMetrics"/> counters/histograms for OpenTelemetry. |
| | | 20 | | /// Automatically includes token tracking via <c>UsingTokenTracking()</c>. |
| | | 21 | | /// </summary> |
| | | 22 | | public static AgentFrameworkSyringe UsingDiagnostics( |
| | | 23 | | this AgentFrameworkSyringe syringe) |
| | | 24 | | { |
| | 36 | 25 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 26 | | |
| | 36 | 27 | | syringe = syringe.UsingTokenTracking(); |
| | | 28 | | |
| | 36 | 29 | | var result = syringe.Configure(opts => |
| | 36 | 30 | | { |
| | 35 | 31 | | var metrics = opts.ServiceProvider.GetRequiredService<IAgentMetrics>(); |
| | 35 | 32 | | var progressAccessor = opts.ServiceProvider.GetRequiredService<IProgressReporterAccessor>(); |
| | 35 | 33 | | var metricsOptions = opts.ServiceProvider.GetService<AgentFrameworkMetricsOptions>(); |
| | 35 | 34 | | var chatMiddleware = new DiagnosticsChatClientMiddleware( |
| | 35 | 35 | | metrics, progressAccessor, |
| | 35 | 36 | | metricsOptions?.ChatCompletionActivityMode ?? ChatCompletionActivityMode.Always); |
| | 36 | 37 | | |
| | 36 | 38 | | // Register the real collector via the DI-managed holder — NOT a static field. |
| | 35 | 39 | | var holder = opts.ServiceProvider.GetRequiredService<ChatCompletionCollectorHolder>(); |
| | 35 | 40 | | holder.SetCollector(chatMiddleware); |
| | 36 | 41 | | |
| | 36 | 42 | | // Register the tool call collector via the DI-managed holder. |
| | 35 | 43 | | var toolCallCollector = new ToolCallCollector(); |
| | 35 | 44 | | var toolCallHolder = opts.ServiceProvider.GetRequiredService<ToolCallCollectorHolder>(); |
| | 35 | 45 | | toolCallHolder.SetCollector(toolCallCollector); |
| | 36 | 46 | | |
| | 35 | 47 | | var existingFactory = opts.ChatClientFactory; |
| | 35 | 48 | | opts.ChatClientFactory = sp => |
| | 35 | 49 | | { |
| | 44 | 50 | | var innerClient = existingFactory?.Invoke(sp) |
| | 44 | 51 | | ?? sp.GetRequiredService<IChatClient>(); |
| | 35 | 52 | | |
| | 44 | 53 | | return new DiagnosticsRecordingChatClient(innerClient, chatMiddleware); |
| | 35 | 54 | | }; |
| | 71 | 55 | | }); |
| | | 56 | | |
| | 36 | 57 | | return result with |
| | 36 | 58 | | { |
| | 36 | 59 | | Plugins = (result.Plugins ?? []) |
| | 36 | 60 | | .Append(new AgentDiagnosticsPlugin(syringe.ServiceProvider)) |
| | 36 | 61 | | .ToList() |
| | 36 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | } |