| | | 1 | | using Microsoft.Extensions.AI; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Result of a single agent stage within a multi-agent pipeline or group chat workflow, |
| | | 7 | | /// combining the response text with the captured diagnostics for that stage. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// Access stage results via <see cref="IPipelineRunResult.Stages"/> after calling |
| | | 12 | | /// <c>RunWithDiagnosticsAsync</c>. Each stage corresponds to one agent turn in the |
| | | 13 | | /// workflow. |
| | | 14 | | /// </para> |
| | | 15 | | /// </remarks> |
| | | 16 | | /// <example> |
| | | 17 | | /// <code> |
| | | 18 | | /// var result = await workflow.RunWithDiagnosticsAsync(prompt, accessor); |
| | | 19 | | /// foreach (var stage in result.Stages) |
| | | 20 | | /// { |
| | | 21 | | /// var tokens = stage.Diagnostics?.AggregateTokenUsage; |
| | | 22 | | /// Console.WriteLine($"[{stage.AgentName}] {tokens?.TotalTokens ?? 0} tokens, " + |
| | | 23 | | /// $"{stage.Diagnostics?.TotalDuration.TotalSeconds:F1}s"); |
| | | 24 | | /// var text = stage.FinalResponse?.Text ?? string.Empty; |
| | | 25 | | /// Console.WriteLine($" Response: {text[..Math.Min(100, text.Length)]}..."); |
| | | 26 | | /// } |
| | | 27 | | /// </code> |
| | | 28 | | /// </example> |
| | | 29 | | public interface IAgentStageResult |
| | | 30 | | { |
| | | 31 | | /// <summary>Gets the agent's executor ID (agent name, possibly with a MAF-assigned GUID suffix).</summary> |
| | | 32 | | string AgentName { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the final <see cref="ChatResponse"/> the agent produced during this stage |
| | | 36 | | /// (preserving full message content, role, usage, and metadata), or |
| | | 37 | | /// <see langword="null"/> if the agent responded only via tool calls with no |
| | | 38 | | /// terminating text response. Call <c>.Text</c> for a flat text view when evaluating. |
| | | 39 | | /// </summary> |
| | | 40 | | ChatResponse? FinalResponse { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the diagnostics captured during this agent's execution, including per-call |
| | | 44 | | /// token usage, tool call details, and timing. <see langword="null"/> if diagnostics |
| | | 45 | | /// were not enabled via <c>UsingDiagnostics()</c>. |
| | | 46 | | /// </summary> |
| | | 47 | | IAgentRunDiagnostics? Diagnostics { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the outcome of this stage's execution. Defaults to |
| | | 51 | | /// <see cref="StageOutcome.Succeeded"/> for backward compatibility with |
| | | 52 | | /// implementations that do not track outcomes. |
| | | 53 | | /// </summary> |
| | 0 | 54 | | StageOutcome Outcome => StageOutcome.Succeeded; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the name of the pipeline phase this stage belongs to, or |
| | | 58 | | /// <see langword="null"/> when running a flat (non-phased) pipeline. |
| | | 59 | | /// </summary> |
| | 0 | 60 | | string? PhaseName => null; |
| | | 61 | | } |