// ------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { using Microsoft.Agents.ObjectModel; using System; /// /// Class to produce the template output /// [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class ForeachTemplate : ActionTemplate { /// /// Create the template output /// public override string TransformText() { this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n/// \n/// Loops over a list assignign the loop variable to \""); this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.Value)); this.Write("\" variable.\n/// \ninternal sealed class "); this.Write(this.ToStringHelper.ToStringWithCulture(this.Name)); this.Write("Executor(FormulaSession session) : ActionExecutor(id: \""); this.Write(this.ToStringHelper.ToStringWithCulture(this.Id)); this.Write(@""", session) { private int _index; private object[] _values = []; public bool HasValue { get; private set; } // protected override async ValueTask ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken) { this._index = 0;"); EvaluateValueExpression(this.Model.Items, "evaluatedValue"); this.Write(@" if (evaluatedValue == null) { this._values = []; this.HasValue = false; } else if (evaluatedValue is IEnumerable evaluatedList) { this._values = [.. evaluatedList]; } else { this._values = [evaluatedValue]; } await this.ResetAsync(context, cancellationToken).ConfigureAwait(false); return default; } public async ValueTask TakeNextAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken) { if (this.HasValue = this._index < this._values.Length) { object value = this._values[this._index]; "); AssignVariable(this.Value, "value", tightFormat: true); if (this.Index is not null) { AssignVariable(this.Index, "this._index", tightFormat: true); } this.Write(@" this._index++; } } public async ValueTask CompleteAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken) { await this.ResetAsync(context, cancellationToken).ConfigureAwait(false); } private async ValueTask ResetAsync(IWorkflowContext context, CancellationToken cancellationToken) {"); AssignVariable(this.Value, "UnassignedValue.Instance", tightFormat: true); if (this.Index is not null) { AssignVariable(this.Index, "UnassignedValue.Instance", tightFormat: true); } this.Write("\n }\n}"); return this.GenerationEnvironment.ToString(); } void AssignVariable(PropertyPath targetVariable, string valueVariable, bool tightFormat = false) { if (targetVariable is not null) { this.Write("\n await context.QueueStateUpdateAsync(key: \""); this.Write(this.ToStringHelper.ToStringWithCulture(VariableName(targetVariable))); this.Write("\", value: "); this.Write(this.ToStringHelper.ToStringWithCulture(valueVariable)); this.Write(", scopeName: \""); this.Write(this.ToStringHelper.ToStringWithCulture(VariableScope(targetVariable))); this.Write("\").ConfigureAwait(false);"); if (!tightFormat) { this.Write("\n "); } } } void EvaluateValueExpression(ValueExpression expression, string targetVariable) => EvaluateValueExpression(expression, targetVariable); void EvaluateValueExpression(ValueExpression expression, string targetVariable) { if (expression is null) { this.Write("\n "); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write("? "); this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); this.Write(" = null;"); } else if (expression.IsLiteral) { this.Write("\n "); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write("? "); this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); this.Write(" = "); this.Write(this.ToStringHelper.ToStringWithCulture(FormatDataValue(expression.LiteralValue))); this.Write(";"); } else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) { this.Write("\n "); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write("? "); this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); this.Write(" = await context.ReadStateAsync<"); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write(">(key: \""); this.Write(this.ToStringHelper.ToStringWithCulture(expression.VariableReference.VariableName)); this.Write("\", scopeName: \""); this.Write(this.ToStringHelper.ToStringWithCulture(expression.VariableReference.NamespaceAlias)); this.Write("\").ConfigureAwait(false);"); } else if (expression.IsVariableReference) { this.Write("\n "); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write("? "); this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); this.Write(" = await context.EvaluateValueAsync<"); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write(">("); this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); this.Write(").ConfigureAwait(false);"); } else { this.Write("\n "); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write("? "); this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); this.Write(" = await context.EvaluateValueAsync<"); this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); this.Write(">("); this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); this.Write(").ConfigureAwait(false);"); } } } }