| | | 1 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 2 | | using NexusLabs.Needlr.AgentFramework.Progress; |
| | | 3 | | using NexusLabs.Needlr.AgentFramework.Workspace; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides typed access to the pipeline's shared state during stage execution. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="Workspace">The shared workspace for reading/writing files across stages.</param> |
| | | 11 | | /// <param name="DiagnosticsAccessor">Accessor for capturing per-stage diagnostics.</param> |
| | | 12 | | /// <param name="ProgressReporter">Optional progress reporter for emitting pipeline events.</param> |
| | | 13 | | /// <param name="StageIndex">Zero-based index of the current stage in the pipeline.</param> |
| | | 14 | | /// <param name="TotalStages">Total number of stages in the pipeline.</param> |
| | | 15 | | /// <param name="StageName">Human-readable name of the current stage.</param> |
| | | 16 | | /// <param name="CallerCancellationToken"> |
| | | 17 | | /// The original caller's cancellation token. Decorators that create linked tokens |
| | | 18 | | /// (e.g. <see cref="TimeoutExecutor"/>) should check this to distinguish user |
| | | 19 | | /// cancellation from internal timeouts. |
| | | 20 | | /// </param> |
| | | 21 | | /// <param name="PipelineState"> |
| | | 22 | | /// Optional shared state object passed to all stages in the pipeline. Use |
| | | 23 | | /// <see cref="GetRequiredState{T}"/> to retrieve it with type safety. |
| | | 24 | | /// </param> |
| | | 25 | | /// <param name="PhaseName"> |
| | | 26 | | /// The name of the pipeline phase this stage belongs to, or <see langword="null"/> |
| | | 27 | | /// when running a flat (non-phased) pipeline. |
| | | 28 | | /// </param> |
| | | 29 | | /// <param name="PhaseIndex"> |
| | | 30 | | /// Zero-based index of the current phase, or <see langword="null"/> for flat pipelines. |
| | | 31 | | /// </param> |
| | | 32 | | /// <param name="StageIndexInPhase"> |
| | | 33 | | /// Zero-based index of the current stage within its phase, or <see langword="null"/> |
| | | 34 | | /// for flat pipelines. |
| | | 35 | | /// </param> |
| | | 36 | | /// <param name="TotalStagesInPhase"> |
| | | 37 | | /// Total number of stages in the current phase, or <see langword="null"/> for flat pipelines. |
| | | 38 | | /// </param> |
| | | 39 | | /// <example> |
| | | 40 | | /// <code> |
| | | 41 | | /// public async Task<StageExecutionResult> ExecuteAsync( |
| | | 42 | | /// StageExecutionContext context, |
| | | 43 | | /// CancellationToken cancellationToken) |
| | | 44 | | /// { |
| | | 45 | | /// Console.WriteLine($"Running stage {context.StageIndex + 1}/{context.TotalStages}: {context.StageName}"); |
| | | 46 | | /// if (context.PhaseName is not null) |
| | | 47 | | /// Console.WriteLine($" Phase: {context.PhaseName} (stage {context.StageIndexInPhase + 1}/{context.TotalStages |
| | | 48 | | /// return StageExecutionResult.Success(context.StageName, diagnostics: null, responseText: null); |
| | | 49 | | /// } |
| | | 50 | | /// </code> |
| | | 51 | | /// </example> |
| | 191 | 52 | | public sealed record StageExecutionContext( |
| | 58 | 53 | | IWorkspace Workspace, |
| | 103 | 54 | | IAgentDiagnosticsAccessor DiagnosticsAccessor, |
| | 0 | 55 | | IProgressReporter? ProgressReporter, |
| | 4 | 56 | | int StageIndex, |
| | 1 | 57 | | int TotalStages, |
| | 192 | 58 | | string StageName, |
| | 5 | 59 | | CancellationToken CallerCancellationToken = default, |
| | 5 | 60 | | object? PipelineState = null, |
| | 1 | 61 | | string? PhaseName = null, |
| | 1 | 62 | | int? PhaseIndex = null, |
| | 1 | 63 | | int? StageIndexInPhase = null, |
| | 192 | 64 | | int? TotalStagesInPhase = null) |
| | | 65 | | { |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the typed pipeline state, or throws if no state was provided or the type doesn't match. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <typeparam name="T">The expected pipeline state type.</typeparam> |
| | | 70 | | /// <returns>The pipeline state cast to <typeparamref name="T"/>.</returns> |
| | | 71 | | /// <exception cref="InvalidOperationException"> |
| | | 72 | | /// Thrown when <see cref="PipelineState"/> is <see langword="null"/> or not of type <typeparamref name="T"/>. |
| | | 73 | | /// </exception> |
| | | 74 | | public T GetRequiredState<T>() where T : class => |
| | 5 | 75 | | PipelineState as T ?? throw new InvalidOperationException( |
| | 5 | 76 | | $"Pipeline state is not available or is not of type {typeof(T).Name}."); |
| | | 77 | | } |