| | | 1 | | using Microsoft.Extensions.AI; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Default implementation of <see cref="IPipelineRunResult"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | [DoNotAutoRegister] |
| | | 9 | | internal sealed class PipelineRunResult : IPipelineRunResult |
| | | 10 | | { |
| | | 11 | | private readonly Lazy<IReadOnlyDictionary<string, ChatResponse?>> _lazyResponses; |
| | | 12 | | private readonly Lazy<TokenUsage?> _lazyAggregateTokenUsage; |
| | | 13 | | |
| | 84 | 14 | | internal PipelineRunResult( |
| | 84 | 15 | | IReadOnlyList<IAgentStageResult> stages, |
| | 84 | 16 | | TimeSpan totalDuration, |
| | 84 | 17 | | bool succeeded, |
| | 84 | 18 | | string? errorMessage, |
| | 84 | 19 | | Exception? exception = null, |
| | 84 | 20 | | int? plannedStageCount = null) |
| | | 21 | | { |
| | 84 | 22 | | Stages = stages; |
| | 84 | 23 | | PlannedStageCount = plannedStageCount ?? stages.Count; |
| | 84 | 24 | | TotalDuration = totalDuration; |
| | 84 | 25 | | Succeeded = succeeded; |
| | 84 | 26 | | ErrorMessage = errorMessage; |
| | 84 | 27 | | Exception = exception; |
| | | 28 | | |
| | | 29 | | // GroupBy handles duplicate agent names (last stage wins). |
| | 84 | 30 | | _lazyResponses = new Lazy<IReadOnlyDictionary<string, ChatResponse?>>(() => |
| | 87 | 31 | | stages |
| | 4 | 32 | | .GroupBy(s => s.AgentName) |
| | 93 | 33 | | .ToDictionary(g => g.Key, g => g.Last().FinalResponse)); |
| | | 34 | | |
| | 84 | 35 | | _lazyAggregateTokenUsage = new Lazy<TokenUsage?>(() => |
| | 84 | 36 | | { |
| | 4 | 37 | | var diagnostics = stages |
| | 5 | 38 | | .Select(s => s.Diagnostics) |
| | 5 | 39 | | .Where(d => d is not null) |
| | 4 | 40 | | .ToList(); |
| | 84 | 41 | | |
| | 6 | 42 | | if (diagnostics.Count == 0) return null; |
| | 84 | 43 | | |
| | 84 | 44 | | // AggregateTokenUsage is always non-null on IAgentRunDiagnostics produced by |
| | 84 | 45 | | // AgentRunDiagnosticsBuilder.Build() — the ! suppression is safe. |
| | 2 | 46 | | return new TokenUsage( |
| | 3 | 47 | | InputTokens: diagnostics.Sum(d => d!.AggregateTokenUsage.InputTokens), |
| | 3 | 48 | | OutputTokens: diagnostics.Sum(d => d!.AggregateTokenUsage.OutputTokens), |
| | 3 | 49 | | TotalTokens: diagnostics.Sum(d => d!.AggregateTokenUsage.TotalTokens), |
| | 3 | 50 | | CachedInputTokens: diagnostics.Sum(d => d!.AggregateTokenUsage.CachedInputTokens), |
| | 5 | 51 | | ReasoningTokens: diagnostics.Sum(d => d!.AggregateTokenUsage.ReasoningTokens)); |
| | 84 | 52 | | }); |
| | 84 | 53 | | } |
| | | 54 | | |
| | 56 | 55 | | public IReadOnlyList<IAgentStageResult> Stages { get; } |
| | | 56 | | |
| | 5 | 57 | | public int PlannedStageCount { get; } |
| | | 58 | | |
| | 6 | 59 | | public IReadOnlyDictionary<string, ChatResponse?> FinalResponses => _lazyResponses.Value; |
| | | 60 | | |
| | 2 | 61 | | public TimeSpan TotalDuration { get; } |
| | | 62 | | |
| | 8 | 63 | | public TokenUsage? AggregateTokenUsage => _lazyAggregateTokenUsage.Value; |
| | | 64 | | |
| | 31 | 65 | | public bool Succeeded { get; } |
| | | 66 | | |
| | 11 | 67 | | public string? ErrorMessage { get; } |
| | | 68 | | |
| | 8 | 69 | | public Exception? Exception { get; } |
| | | 70 | | } |