| | | 1 | | using Microsoft.Agents.AI; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Executes a pipeline stage by running an <see cref="AIAgent"/> with a |
| | | 7 | | /// dynamically constructed prompt. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <example> |
| | | 10 | | /// <code> |
| | | 11 | | /// var executor = new AgentStageExecutor( |
| | | 12 | | /// writerAgent, |
| | | 13 | | /// ctx => $"Write a draft about topic in workspace."); |
| | | 14 | | /// |
| | | 15 | | /// var result = await executor.ExecuteAsync(context, cancellationToken); |
| | | 16 | | /// Console.WriteLine(result.ResponseText); |
| | | 17 | | /// </code> |
| | | 18 | | /// </example> |
| | | 19 | | [DoNotAutoRegister] |
| | | 20 | | public sealed class AgentStageExecutor : IStageExecutor |
| | | 21 | | { |
| | | 22 | | private readonly AIAgent _agent; |
| | | 23 | | private readonly Func<StageExecutionContext, string> _promptFactory; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new <see cref="AgentStageExecutor"/>. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="agent">The AI agent to execute.</param> |
| | | 29 | | /// <param name="promptFactory"> |
| | | 30 | | /// Factory that produces the prompt string from the current stage context. |
| | | 31 | | /// </param> |
| | 30 | 32 | | public AgentStageExecutor( |
| | 30 | 33 | | AIAgent agent, |
| | 30 | 34 | | Func<StageExecutionContext, string> promptFactory) |
| | | 35 | | { |
| | 30 | 36 | | _agent = agent; |
| | 30 | 37 | | _promptFactory = promptFactory; |
| | 30 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public async Task<StageExecutionResult> ExecuteAsync( |
| | | 42 | | StageExecutionContext context, |
| | | 43 | | CancellationToken cancellationToken) |
| | | 44 | | { |
| | 0 | 45 | | var prompt = _promptFactory(context); |
| | 0 | 46 | | using (context.DiagnosticsAccessor.BeginCapture()) |
| | | 47 | | { |
| | 0 | 48 | | var response = await _agent.RunAsync(prompt, cancellationToken: cancellationToken); |
| | 0 | 49 | | var diagnostics = context.DiagnosticsAccessor.LastRunDiagnostics; |
| | 0 | 50 | | return StageExecutionResult.Success(context.StageName, diagnostics, response.GetText()); |
| | | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | } |