// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
using Microsoft.Agents.ObjectModel;
using Microsoft.Agents.ObjectModel.Abstractions;
namespace Microsoft.Agents.AI.Workflows.Declarative.Kit;
///
/// Extension methods for that assist with
/// Power Fx expression evaluation.
///
public static class IWorkflowContextExtensions
{
///
/// Formats a template lines using the workflow's declarative state
/// and evaluating any embedded expressions (e.g., Power Fx) contained within each line.
///
/// The workflow execution context used to restore persisted state prior to formatting.
/// The template line to format.
/// A token that propagates notification when operation should be canceled.
///
/// A single string containing the formatted results of all lines separated by newline characters.
/// A trailing newline will be present if at least one line was processed.
///
///
/// Example:
/// var text = await context.FormatAsync("Hello @{User.Name}", "Count: @{Metrics.Count}");
///
public static ValueTask FormatTemplateAsync(this IWorkflowContext context, string line, CancellationToken cancellationToken = default) =>
context.FormatTemplateAsync([line], cancellationToken);
///
/// Formats a template lines using the workflow's declarative state
/// and evaluating any embedded expressions (e.g., Power Fx) contained within each line.
///
/// The workflow execution context used to restore persisted state prior to formatting.
/// The template lines to format.
/// A token that propagates notification when operation should be canceled.
///
/// A single string containing the formatted results of all lines separated by newline characters.
/// A trailing newline will be present if at least one line was processed.
///
///
/// Example:
/// var text = await context.FormatAsync("Hello @{User.Name}", "Count: @{Metrics.Count}");
///
public static async ValueTask FormatTemplateAsync(this IWorkflowContext context, IEnumerable lines, CancellationToken cancellationToken = default)
{
WorkflowFormulaState state = await context.GetStateAsync(cancellationToken).ConfigureAwait(false);
StringBuilder builder = new();
foreach (string line in lines)
{
builder.AppendLine(state.Engine.Format(TemplateLine.Parse(line)));
}
return builder.ToString();
}
///
/// Evaluate an expression using the workflow's declarative state.
///
/// The workflow execution context used to restore persisted state prior to formatting.
/// The expression to evaluate.
/// A token that propagates notification when operation should be canceled.
/// The evaluated expression value
public static ValueTask