| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// <see cref="AsyncLocal{T}"/>-backed implementation of <see cref="IAgentDiagnosticsAccessor"/> |
| | | 5 | | /// using the mutable-holder pattern. A mutable <see cref="Holder"/> reference is stored in the |
| | | 6 | | /// <see cref="AsyncLocal{T}"/> slot — because the reference flows downward, mutations made by |
| | | 7 | | /// child async flows (middleware) are visible to the parent scope. |
| | | 8 | | /// </summary> |
| | | 9 | | [DoNotAutoRegister] |
| | | 10 | | internal sealed class AgentDiagnosticsAccessor : IAgentDiagnosticsAccessor, IAgentDiagnosticsWriter |
| | | 11 | | { |
| | 1 | 12 | | private static readonly AsyncLocal<Holder?> Current = new(); |
| | | 13 | | private readonly IChatCompletionCollector? _completionCollector; |
| | | 14 | | private readonly IToolCallCollector? _toolCallCollector; |
| | | 15 | | |
| | 97 | 16 | | internal AgentDiagnosticsAccessor( |
| | 97 | 17 | | ChatCompletionCollectorHolder? completionCollector = null, |
| | 97 | 18 | | ToolCallCollectorHolder? toolCallCollector = null) |
| | | 19 | | { |
| | 97 | 20 | | _completionCollector = completionCollector; |
| | 97 | 21 | | _toolCallCollector = toolCallCollector; |
| | 97 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 42 | 25 | | public IAgentRunDiagnostics? LastRunDiagnostics => Current.Value?.Value; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 16 | 28 | | public IChatCompletionCollector? CompletionCollector => _completionCollector; |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | 10 | 31 | | public IToolCallCollector? ToolCallCollector => _toolCallCollector; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public IDisposable BeginCapture() |
| | | 35 | | { |
| | 33 | 36 | | var previous = Current.Value; |
| | 33 | 37 | | Current.Value = new Holder(); |
| | 33 | 38 | | return new Scope(previous); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 46 | 42 | | void IAgentDiagnosticsWriter.Set(IAgentRunDiagnostics diagnostics) => SetCore(diagnostics); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Stores completed diagnostics into the current holder. Called by the diagnostics |
| | | 46 | | /// middleware after an agent run completes. |
| | | 47 | | /// </summary> |
| | 6 | 48 | | internal void Set(IAgentRunDiagnostics diagnostics) => SetCore(diagnostics); |
| | | 49 | | |
| | | 50 | | private static void SetCore(IAgentRunDiagnostics diagnostics) |
| | | 51 | | { |
| | 52 | 52 | | if (Current.Value is { } holder) |
| | | 53 | | { |
| | 35 | 54 | | holder.Value = diagnostics; |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 17 | 58 | | Current.Value = new Holder { Value = diagnostics }; |
| | | 59 | | } |
| | 17 | 60 | | } |
| | | 61 | | |
| | | 62 | | private sealed class Holder |
| | | 63 | | { |
| | | 64 | | public IAgentRunDiagnostics? Value; |
| | | 65 | | } |
| | | 66 | | |
| | 33 | 67 | | private sealed class Scope(Holder? previous) : IDisposable |
| | | 68 | | { |
| | | 69 | | private bool _disposed; |
| | | 70 | | |
| | | 71 | | public void Dispose() |
| | | 72 | | { |
| | 33 | 73 | | if (_disposed) return; |
| | 33 | 74 | | _disposed = true; |
| | 33 | 75 | | Current.Value = previous; |
| | 33 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |