// Copyright (c) Microsoft. All rights reserved. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; namespace Microsoft.Agents.AI.Workflows; /// /// Provides configuration options for . /// public class ChatProtocolExecutorOptions { /// /// Gets or sets the chat role to use when converting string messages to instances. /// If set, the executor will accept string messages and convert them to chat messages with this role. /// public ChatRole? StringMessageChatRole { get; set; } } /// /// Provides a base class for executors that implement the Agent Workflow Chat Protocol. /// This executor maintains a list of chat messages and processes them when a turn is taken. /// public abstract class ChatProtocolExecutor : StatefulExecutor> { private static readonly Func> s_initFunction = () => []; private readonly ChatRole? _stringMessageChatRole; private static readonly StatefulExecutorOptions s_baseExecutorOptions = new() { AutoSendMessageHandlerResultObject = false, AutoYieldOutputHandlerResultObject = false }; /// /// Initializes a new instance of the class. /// /// The unique identifier for this executor instance. Cannot be null or empty. /// Optional configuration settings for the executor. If null, default options are used. /// Declare that this executor may be used simultaneously by multiple runs safely. protected ChatProtocolExecutor(string id, ChatProtocolExecutorOptions? options = null, bool declareCrossRunShareable = false) : base(id, () => [], s_baseExecutorOptions, declareCrossRunShareable) { this._stringMessageChatRole = options?.StringMessageChatRole; } /// protected override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder) { if (this._stringMessageChatRole.HasValue) { routeBuilder = routeBuilder.AddHandler( (message, context) => this.AddMessageAsync(new(this._stringMessageChatRole.Value, message), context)); } return routeBuilder.AddHandler(this.AddMessageAsync) .AddHandler>(this.AddMessagesAsync) .AddHandler(this.AddMessagesAsync) .AddHandler>(this.AddMessagesAsync) .AddHandler(this.TakeTurnAsync); } /// /// Adds a single chat message to the accumulated messages for the current turn. /// /// The chat message to add. /// The workflow context in which the executor executes. /// The to monitor for cancellation requests. /// A representing the asynchronous operation. protected ValueTask AddMessageAsync(ChatMessage message, IWorkflowContext context, CancellationToken cancellationToken = default) { return this.InvokeWithStateAsync(ForwardMessageAsync, context, cancellationToken: cancellationToken); ValueTask?> ForwardMessageAsync(List? maybePendingMessages, IWorkflowContext context, CancellationToken cancelationToken) { maybePendingMessages ??= s_initFunction(); maybePendingMessages.Add(message); return new(maybePendingMessages); } } /// /// Adds multiple chat messages to the accumulated messages for the current turn. /// /// The collection of chat messages to add. /// The workflow context in which the executor executes. /// The to monitor for cancellation requests. /// A representing the asynchronous operation. protected ValueTask AddMessagesAsync(IEnumerable messages, IWorkflowContext context, CancellationToken cancellationToken = default) { return this.InvokeWithStateAsync(ForwardMessageAsync, context, cancellationToken: cancellationToken); ValueTask?> ForwardMessageAsync(List? maybePendingMessages, IWorkflowContext context, CancellationToken cancelationToken) { maybePendingMessages ??= s_initFunction(); maybePendingMessages.AddRange(messages); return new(maybePendingMessages); } } /// /// Handles a turn token by processing all accumulated chat messages and then resetting the message state. /// /// The turn token that triggers message processing. /// The workflow context in which the executor executes. /// The to monitor for cancellation requests. /// A representing the asynchronous operation. public ValueTask TakeTurnAsync(TurnToken token, IWorkflowContext context, CancellationToken cancellationToken = default) { return this.InvokeWithStateAsync(InvokeTakeTurnAsync, context, cancellationToken: cancellationToken); async ValueTask?> InvokeTakeTurnAsync(List? maybePendingMessages, IWorkflowContext context, CancellationToken cancellationToken) { await this.TakeTurnAsync(maybePendingMessages ?? s_initFunction(), context, token.EmitEvents, cancellationToken) .ConfigureAwait(false); await context.SendMessageAsync(token, cancellationToken: cancellationToken).ConfigureAwait(false); // Rerun the initialStateFactory to reset the state to empty list. (We could return the empty list directly, // but this is more consistent if the initial state factory becomes more complex.) return s_initFunction(); } } /// /// When overridden in a derived class, processes the accumulated chat messages for a single turn. /// /// The list of chat messages accumulated since the last turn. /// The workflow context in which the executor executes. /// Indicates whether events should be emitted during processing. If null, the default behavior is used. /// The to monitor for cancellation requests. /// A representing the asynchronous operation. protected abstract ValueTask TakeTurnAsync(List messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellationToken = default); }