| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Applies a session's <see cref="LangfuseScoreFailureMode"/> to a failed score upload and tracks |
| | | 5 | | /// how many uploads have failed. Shared by every scenario created from one session. |
| | | 6 | | /// </summary> |
| | | 7 | | internal sealed class LangfuseScoreFailureSink |
| | | 8 | | { |
| | | 9 | | private readonly LangfuseScoreFailureMode _mode; |
| | | 10 | | private readonly Action<LangfuseScoreError>? _callback; |
| | | 11 | | private int _failedCount; |
| | | 12 | | |
| | 16 | 13 | | public LangfuseScoreFailureSink( |
| | 16 | 14 | | LangfuseScoreFailureMode mode, |
| | 16 | 15 | | Action<LangfuseScoreError>? callback) |
| | | 16 | | { |
| | 16 | 17 | | _mode = mode; |
| | 16 | 18 | | _callback = callback; |
| | 16 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>Gets the cumulative number of score uploads that have failed.</summary> |
| | 3 | 22 | | public int FailedCount => Volatile.Read(ref _failedCount); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Records a failed score upload. In <see cref="LangfuseScoreFailureMode.Strict"/> mode the |
| | | 26 | | /// originating exception is rethrown; otherwise the failure counter is incremented, the error |
| | | 27 | | /// callback (if any) is invoked, and control returns to the caller. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="scoreName">The name of the score that failed.</param> |
| | | 30 | | /// <param name="traceId">The destination trace id, if known.</param> |
| | | 31 | | /// <param name="exception">The failure.</param> |
| | | 32 | | public void Record(string scoreName, string? traceId, LangfuseException exception) |
| | | 33 | | { |
| | 3 | 34 | | ArgumentNullException.ThrowIfNull(exception); |
| | | 35 | | |
| | 3 | 36 | | if (_mode == LangfuseScoreFailureMode.Strict) |
| | | 37 | | { |
| | 1 | 38 | | throw exception; |
| | | 39 | | } |
| | | 40 | | |
| | 2 | 41 | | Interlocked.Increment(ref _failedCount); |
| | 2 | 42 | | _callback?.Invoke(new LangfuseScoreError(scoreName, traceId, exception)); |
| | 2 | 43 | | } |
| | | 44 | | } |