// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Agents.AI.Workflows;
///
/// Provides extension methods for working with instances.
///
public static class IWorkflowContextExtensions
{
///
/// Invokes an asynchronous operation that reads, updates, and persists workflow state associated with the specified
/// key.
///
/// The type of the state object to read, update, and persist.
/// The workflow context used to access and update state.
/// A delegate that receives the current state, workflow context, and cancellation token, and returns the updated
/// state asynchronously.
/// The key identifying the state to read and update. Cannot be null or empty.
/// An optional scope name that further qualifies the state key. If null, the default scope is used.
/// A cancellation token that can be used to cancel the asynchronous operation.
/// A ValueTask that represents the asynchronous operation.
public static async ValueTask InvokeWithStateAsync(this IWorkflowContext context,
Func> invocation,
string key,
string? scopeName = null,
CancellationToken cancellationToken = default)
{
TState? state = await context.ReadStateAsync(key, scopeName, cancellationToken).ConfigureAwait(false);
state = await invocation(state, context, cancellationToken).ConfigureAwait(false);
await context.QueueStateUpdateAsync(key, state, scopeName, cancellationToken).ConfigureAwait(false);
}
///
/// Invokes an asynchronous operation that reads, updates, and persists workflow state associated with the specified
/// key.
///
/// The type of the state object to read, update, and persist.
/// The workflow context used to access and update state.
/// A delegate that receives the current state, workflow context, and cancellation token, and returns the updated
/// state asynchronously.
/// The key identifying the state to read and update. Cannot be null or empty.
/// A factory to initialize state to if it is not set at the provided key.
/// An optional scope name that further qualifies the state key. If null, the default scope is used.
/// A cancellation token that can be used to cancel the asynchronous operation.
/// A ValueTask that represents the asynchronous operation.
public static async ValueTask InvokeWithStateAsync(this IWorkflowContext context,
Func> invocation,
string key,
Func initialStateFactory,
string? scopeName = null,
CancellationToken cancellationToken = default)
{
TState? state = await context.ReadOrInitStateAsync(key, initialStateFactory, scopeName, cancellationToken).ConfigureAwait(false);
state = await invocation(state, context, cancellationToken).ConfigureAwait(false);
await context.QueueStateUpdateAsync(key, state ?? initialStateFactory(), scopeName, cancellationToken).ConfigureAwait(false);
}
///
/// Queues a message to be sent to connected executors. The message will be sent during the next SuperStep.
///
/// The workflow context used to access and update state.
/// The message to be sent.
/// The to monitor for cancellation requests.
/// A representing the asynchronous operation.
public static ValueTask SendMessageAsync(this IWorkflowContext context, object message, CancellationToken cancellationToken = default) =>
context.SendMessageAsync(message, null, cancellationToken);
}