| | | 1 | | using System.Threading.Channels; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Progress; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Non-blocking <see cref="IProgressReporter"/> that pushes events to a |
| | | 7 | | /// <see cref="Channel{T}"/> and drains them to sinks on a background task. |
| | | 8 | | /// Use this when sinks do I/O (database, network) and you don't want to |
| | | 9 | | /// block the agent pipeline. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// <see cref="Report"/> writes to the channel and returns immediately. |
| | | 14 | | /// A background consumer drains events to all sinks. Events are delivered |
| | | 15 | | /// in order but asynchronously. |
| | | 16 | | /// </para> |
| | | 17 | | /// <para> |
| | | 18 | | /// <see cref="CreateChild"/> returns a lightweight wrapper that shares |
| | | 19 | | /// the parent's channel — no additional background tasks are created. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// Call <see cref="DisposeAsync"/> to drain remaining events and stop |
| | | 23 | | /// the background consumer. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | [DoNotAutoRegister] |
| | | 27 | | public sealed class ChannelProgressReporter : IProgressReporter, IAsyncDisposable |
| | | 28 | | { |
| | | 29 | | private readonly Channel<IProgressEvent> _channel; |
| | | 30 | | private readonly IProgressSequence _sequence; |
| | | 31 | | private readonly IProgressReporterErrorHandler _errorHandler; |
| | | 32 | | private readonly Task _consumer; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a channel-based reporter with the given sinks. |
| | | 36 | | /// Starts a background consumer immediately. |
| | | 37 | | /// </summary> |
| | 6 | 38 | | public ChannelProgressReporter( |
| | 6 | 39 | | string workflowId, |
| | 6 | 40 | | IReadOnlyList<IProgressSink> sinks, |
| | 6 | 41 | | IProgressSequence sequence, |
| | 6 | 42 | | IProgressReporterErrorHandler? errorHandler = null, |
| | 6 | 43 | | string? agentId = null, |
| | 6 | 44 | | string? parentAgentId = null, |
| | 6 | 45 | | int depth = 0, |
| | 6 | 46 | | int capacity = 1000) |
| | | 47 | | { |
| | 6 | 48 | | WorkflowId = workflowId; |
| | 6 | 49 | | _sequence = sequence; |
| | 6 | 50 | | _errorHandler = errorHandler ?? new NullProgressReporterErrorHandler(); |
| | 6 | 51 | | AgentId = agentId; |
| | 6 | 52 | | Depth = depth; |
| | | 53 | | |
| | 6 | 54 | | _channel = Channel.CreateBounded<IProgressEvent>(new BoundedChannelOptions(capacity) |
| | 6 | 55 | | { |
| | 6 | 56 | | FullMode = BoundedChannelFullMode.Wait, |
| | 6 | 57 | | SingleReader = true, |
| | 6 | 58 | | SingleWriter = false, |
| | 6 | 59 | | }); |
| | | 60 | | |
| | 12 | 61 | | _consumer = Task.Run(() => ConsumeAsync(sinks)); |
| | 6 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | 1 | 65 | | public string WorkflowId { get; } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | 0 | 68 | | public string? AgentId { get; } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | 1 | 71 | | public int Depth { get; } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | 2 | 74 | | public long NextSequence() => _sequence.Next(); |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public void Report(IProgressEvent progressEvent) |
| | | 78 | | { |
| | 17 | 79 | | _channel.Writer.TryWrite(progressEvent); |
| | 17 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public IProgressReporter CreateChild(string agentId) => |
| | 1 | 84 | | new ChannelChildReporter(this, agentId); |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Completes the channel and waits for the background consumer to drain |
| | | 88 | | /// all remaining events to sinks. |
| | | 89 | | /// </summary> |
| | | 90 | | public async ValueTask DisposeAsync() |
| | | 91 | | { |
| | 6 | 92 | | _channel.Writer.TryComplete(); |
| | 6 | 93 | | await _consumer; |
| | 6 | 94 | | } |
| | | 95 | | |
| | | 96 | | private async Task ConsumeAsync(IReadOnlyList<IProgressSink> sinks) |
| | | 97 | | { |
| | | 98 | | try |
| | | 99 | | { |
| | 46 | 100 | | await foreach (var evt in _channel.Reader.ReadAllAsync()) |
| | | 101 | | { |
| | 74 | 102 | | for (int i = 0; i < sinks.Count; i++) |
| | | 103 | | { |
| | | 104 | | try |
| | | 105 | | { |
| | 20 | 106 | | await sinks[i].OnEventAsync(evt, CancellationToken.None); |
| | 19 | 107 | | } |
| | 1 | 108 | | catch (Exception ex) |
| | | 109 | | { |
| | 1 | 110 | | _errorHandler.OnSinkException(sinks[i], evt, ex); |
| | 1 | 111 | | } |
| | | 112 | | } |
| | 17 | 113 | | } |
| | 6 | 114 | | } |
| | 0 | 115 | | catch (OperationCanceledException) |
| | | 116 | | { |
| | | 117 | | // Shutdown |
| | 0 | 118 | | } |
| | 6 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Lightweight child reporter that shares the parent's channel. |
| | | 123 | | /// No background task — writes go directly to the parent's channel. |
| | | 124 | | /// </summary> |
| | | 125 | | private sealed class ChannelChildReporter : IProgressReporter |
| | | 126 | | { |
| | | 127 | | private readonly ChannelProgressReporter _root; |
| | | 128 | | |
| | 1 | 129 | | internal ChannelChildReporter(ChannelProgressReporter root, string agentId) |
| | | 130 | | { |
| | 1 | 131 | | _root = root; |
| | 1 | 132 | | WorkflowId = root.WorkflowId; |
| | 1 | 133 | | AgentId = agentId; |
| | 1 | 134 | | Depth = root.Depth + 1; |
| | 1 | 135 | | } |
| | | 136 | | |
| | 1 | 137 | | public string WorkflowId { get; } |
| | 1 | 138 | | public string? AgentId { get; } |
| | 1 | 139 | | public int Depth { get; } |
| | | 140 | | |
| | 0 | 141 | | public long NextSequence() => _root.NextSequence(); |
| | | 142 | | |
| | | 143 | | public void Report(IProgressEvent progressEvent) => |
| | 1 | 144 | | _root.Report(progressEvent); |
| | | 145 | | |
| | | 146 | | public IProgressReporter CreateChild(string agentId) => |
| | 0 | 147 | | new ChannelChildReporter(_root, agentId); |
| | | 148 | | } |
| | | 149 | | } |