| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides access to the diagnostics captured during the most recent agent run in the current |
| | | 5 | | /// async flow. Uses the mutable-holder <see cref="AsyncLocal{T}"/> pattern so that writes from |
| | | 6 | | /// child async flows (inside middleware) are visible to the parent scope. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// <para> |
| | | 10 | | /// Callers wrap agent execution in <see cref="BeginCapture"/>: |
| | | 11 | | /// </para> |
| | | 12 | | /// <code> |
| | | 13 | | /// using (diagnosticsAccessor.BeginCapture()) |
| | | 14 | | /// { |
| | | 15 | | /// await agent.RunAsync(prompt); |
| | | 16 | | /// var diagnostics = diagnosticsAccessor.LastRunDiagnostics; |
| | | 17 | | /// // diagnostics contains token usage, tool calls, timing, etc. |
| | | 18 | | /// } |
| | | 19 | | /// </code> |
| | | 20 | | /// </remarks> |
| | | 21 | | public interface IAgentDiagnosticsAccessor |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the diagnostics from the last completed agent run in the current scope, |
| | | 25 | | /// or <see langword="null"/> if no run has completed yet. |
| | | 26 | | /// </summary> |
| | | 27 | | IAgentRunDiagnostics? LastRunDiagnostics { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Opens a capture scope. Diagnostics written by middleware during agent execution |
| | | 31 | | /// are visible via <see cref="LastRunDiagnostics"/> after the run completes. |
| | | 32 | | /// Disposing the returned handle restores the previous scope. |
| | | 33 | | /// </summary> |
| | | 34 | | IDisposable BeginCapture(); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the <see cref="IChatCompletionCollector"/> wired by the diagnostics system, |
| | | 38 | | /// or <see langword="null"/> if diagnostics were not configured. Used as a fallback |
| | | 39 | | /// when <see cref="LastRunDiagnostics"/> is unavailable (e.g., when AsyncLocal state |
| | | 40 | | /// does not propagate across workflow execution boundaries). |
| | | 41 | | /// </summary> |
| | 0 | 42 | | IChatCompletionCollector? CompletionCollector => null; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the <see cref="IToolCallCollector"/> wired by the diagnostics system, |
| | | 46 | | /// or <see langword="null"/> if diagnostics were not configured. Used as a fallback |
| | | 47 | | /// when <see cref="LastRunDiagnostics"/> is unavailable (e.g., when AsyncLocal state |
| | | 48 | | /// does not propagate across workflow execution boundaries). |
| | | 49 | | /// </summary> |
| | 0 | 50 | | IToolCallCollector? ToolCallCollector => null; |
| | | 51 | | } |