diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.cs index 52cb6eb2e2..34e1cb0020 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -10,16 +10,13 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// /// Class to produce the template output /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class AddConversationMessageTemplate : ActionTemplate { /// @@ -35,17 +32,8 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen this.Write("\n"); this.Write("\n"); this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n\n/// \n/// Adds a new message to the specified agent conversation\n/// \ninternal sealed class "); + this.Write("\n/// \n/// Adds a new message to the specified agent conversation\n/// \ninternal sealed class "); this.Write(this.ToStringHelper.ToStringWithCulture(this.Name)); this.Write("Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExe" + "cutor(id: \""); @@ -134,446 +122,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) { string resultTypeName = $"Dictionary()}?>?"; @@ -803,116 +351,6 @@ this.Write(").ConfigureAwait(false);"); } -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);"); - - - } -} - - void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) { if (templateLine is not null) diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.tt index 439f62f8db..d1e7db928c 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/AddConversationMessageTemplate.tt @@ -1,8 +1,12 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> - +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateRecordExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/FormatMessageTemplate.tt" once="true" #> /// /// Adds a new message to the specified agent conversation /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.cs index ea534767bc..0d87f1c739 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.cs @@ -9,11 +9,8 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; - using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; using System.Collections.Generic; + using Microsoft.Agents.ObjectModel; using System; /// @@ -27,17 +24,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -87,85 +73,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateEnumExpression( EnumExpression expression, string targetVariable, @@ -310,601 +217,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.tt index fbac67edf2..b86d189ddb 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ClearAllVariablesTemplate.tt @@ -1,7 +1,10 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateEnumExpressionTemplate.tt" once="true" #> /// /// Reset all the state for the targeted variable scope. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.cs index 8f330d9afd..eabaff77d8 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.cs @@ -9,11 +9,8 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,17 +24,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -182,746 +168,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.tt index d91cadc4d2..37ec8863f1 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ConditionGroupTemplate.tt @@ -1,7 +1,10 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.ObjectModel" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateBoolExpressionTemplate.tt" once="true" #> /// /// Conditional branching similar to an if / elseif / elseif / else chain. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.cs index 9826408215..c29b7ed714 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -9,17 +9,14 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// /// Class to produce the template output /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class CopyConversationMessagesTemplate : ActionTemplate { /// @@ -27,16 +24,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -103,554 +90,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) { string typeName = isNullable ? "string?" : "string"; @@ -881,46 +320,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.tt index 8014af3f8b..4a27eb40cb 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CopyConversationMessagesTemplate.tt @@ -1,7 +1,11 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ import namespace="Microsoft.Extensions.AI" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateValueExpressionTemplate.tt" once="true" #> /// /// Copies one or more messages into the specified agent conversation. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.cs index f9f0c52939..1a27946e42 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -9,17 +9,13 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// /// Class to produce the template output /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class CreateConversationTemplate : ActionTemplate { /// @@ -27,19 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -91,825 +74,5 @@ this.Write("\n "); } } - -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.tt index 859f25a56a..902bcc2ff4 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/CreateConversationTemplate.tt @@ -1,7 +1,8 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> /// /// Creates a new conversation and stores the identifier value to the "<#= this.Model.ConversationId #>" variable. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.cs index 56a1048946..a05ddba863 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.cs @@ -9,11 +9,6 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; - using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,21 +22,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n/// \n/// Modify items in a list\n/// \ninternal sealed class "); @@ -53,853 +33,5 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen " {\n return default;\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 EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.tt index a39630ac5b..e783089c51 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/EditTableV2Template.tt @@ -1,7 +1,6 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> /// /// Modify items in a list /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.cs index 2815bb4bdc..ba15f9d0c7 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,18 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -143,675 +127,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateValueExpression(ValueExpression expression, string targetVariable) => EvaluateValueExpression(expression, targetVariable); @@ -921,46 +236,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.tt index 5e77d26b05..77f13c5184 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ForeachTemplate.tt @@ -1,7 +1,9 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateValueExpressionTemplate.tt" once="true" #> /// /// Loops over a list assignign the loop variable to "<#= this.Model.Value #>" variable. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.cs index 2f4bbf63d5..3cb2642f79 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -9,17 +9,16 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { + using System.Collections.Generic; using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// /// Class to produce the template output /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class InvokeAzureAgentTemplate : ActionTemplate { /// @@ -37,13 +36,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen this.Write("\n"); this.Write("\n"); this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n/// \n/// Invokes an agent to process messages and return a response wit" + "hin a conversation context.\n/// \ninternal sealed class "); this.Write(this.ToStringHelper.ToStringWithCulture(this.Name)); @@ -191,259 +183,6 @@ this.Write(").ConfigureAwait(false);"); } -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateListExpression(ValueExpression expression, string targetVariable) { string typeName = GetTypeAlias(); @@ -552,114 +291,6 @@ this.Write(").ConfigureAwait(false);"); } -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) { string typeName = isNullable ? "string?" : "string"; @@ -780,156 +411,5 @@ this.Write(").ConfigureAwait(false);"); } } - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.tt index b4ca34174d..e38068260a 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/InvokeAzureAgentTemplate.tt @@ -1,7 +1,14 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ import namespace="Microsoft.Extensions.AI" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateBoolExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateListExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #> /// /// Invokes an agent to process messages and return a response within a conversation context. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.cs index a2415bea8f..4572f39973 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,19 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -112,825 +95,5 @@ this.Write("\n "); } } - -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.tt index 5a7c073293..246e1d331b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ParseValueTemplate.tt @@ -1,7 +1,8 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> /// /// Parses a string or untyped value to the provided data type. When the input is a string, it will be treated as JSON. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.cs index cb3cfa89ad..61afa0309a 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.cs @@ -9,11 +9,6 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; - using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,21 +22,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n/// \n/// Request input.\n/// \ninternal sealed class "); @@ -53,853 +33,5 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen " {\n return default;\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 EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.tt index 5b98d30c9a..7d3a15af6b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/QuestionTemplate.tt @@ -1,7 +1,6 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> /// /// Request input. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.cs index 597e567d5c..bd56580a7b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,19 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -87,825 +70,5 @@ this.Write("\n "); } } - -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.tt index 80eb9b4323..0565a2328b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/ResetVariableTemplate.tt @@ -1,7 +1,8 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> /// /// Resets the value of the "<#= this.Model.Variable #>" variable, potentially causing re-evaluation /// of the default value, question or action that provides the value to this variable. diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.cs index a2a679e688..8a3a860914 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,17 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -93,446 +78,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) { string resultTypeName = $"Dictionary()}?>?"; @@ -761,156 +306,5 @@ this.Write(").ConfigureAwait(false);"); } } - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.tt index e2e3754bda..42ec61ad27 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessageTemplate.tt @@ -1,7 +1,10 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateRecordExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #> /// /// Retrieves a list of messages from an agent conversation. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.cs index 08fb0a75ec..1a740ed76c 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version: 17.0.0.0 +// Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -9,17 +9,15 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; - using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; using System.Collections.Generic; + using Microsoft.Agents.AI.Workflows.Declarative.Extensions; + using Microsoft.Agents.ObjectModel; using System; /// /// Class to produce the template output /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class RetrieveConversationMessagesTemplate : ActionTemplate { /// @@ -37,13 +35,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen this.Write("\n"); this.Write("\n"); this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n/// \n/// Retrieves a specific message from an agent conversation.\n/// <" + "/summary>\ninternal sealed class "); this.Write(this.ToStringHelper.ToStringWithCulture(this.Name)); @@ -108,85 +99,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateEnumExpression( EnumExpression expression, string targetVariable, @@ -440,114 +352,6 @@ this.Write(").ConfigureAwait(false);"); } -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) { string resultTypeName = $"Dictionary()}?>?"; @@ -776,156 +580,5 @@ this.Write(").ConfigureAwait(false);"); } } - -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);"); - - - } -} - - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.tt index 96aec79282..1b035d674b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/RetrieveConversationMessagesTemplate.tt @@ -1,7 +1,14 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateEnumExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateIntExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateRecordExpressionTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateStringExpressionTemplate.tt" once="true" #> /// /// Retrieves a specific message from an agent conversation. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.cs index 509127bdf4..cce0854bff 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.cs @@ -10,10 +10,7 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,18 +24,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -71,822 +56,15 @@ if (this.Model.Activity is MessageActivityTemplate messageActivity) } - this.Write("\n );\n AgentResponse response = new([new ChatMessage(ChatRole" + - ".Assistant, activityText)]);\n await context.AddEventAsync(new AgentRes" + - "ponseEvent(this.Id, response)).ConfigureAwait(false);"); + this.Write("\n );\n AgentResponse response = new([new ChatMessage(ChatRole.As" + + "sistant, activityText)]);\n await context.AddEventAsync(new AgentResponseE" + + "vent(this.Id, response)).ConfigureAwait(false);"); } this.Write("\n\n return default;\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 EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) { if (templateLine is not null) diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.tt index f11d2181b4..283ac57e1b 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SendActivityTemplate.tt @@ -1,7 +1,9 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/FormatMessageTemplate.tt" once="true" #> /// /// Formats a message template and sends an activity event. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.cs index 65e0c71e99..52e86a1f65 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,18 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -98,675 +82,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateValueExpression(ValueExpression expression, string targetVariable) => EvaluateValueExpression(expression, targetVariable); @@ -876,46 +191,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.tt index 3746488c27..44b697eb2f 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetMultipleVariablesTemplate.tt @@ -1,7 +1,9 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateValueExpressionTemplate.tt" once="true" #> /// /// Assigns an evaluated expression, other variable, or literal value to one or more variables. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.cs index a8ac685649..81a9ee28bf 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.cs @@ -10,10 +10,7 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,17 +24,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -87,785 +73,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -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);"); - - - } -} - - void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) { if (templateLine is not null) diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.tt index fc5996e20a..1d16ee92ea 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetTextVariableTemplate.tt @@ -1,7 +1,10 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/FormatMessageTemplate.tt" once="true" #> /// /// Assigns an evaluated message template to the "<#= this.Model.Variable #>" variable. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.cs b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.cs index 5d2e08b3e4..972fc1e5c9 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.cs @@ -9,11 +9,7 @@ // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { - using Microsoft.Agents.AI.Workflows.Declarative.Extensions; - using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel; using Microsoft.Agents.ObjectModel; - using Microsoft.Extensions.AI; - using System.Collections.Generic; using System; /// @@ -27,18 +23,6 @@ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen /// public override string TransformText() { - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); - this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); @@ -89,675 +73,6 @@ this.Write("\n "); } -void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false) -{ - if (expression is null) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatBoolValue(expression.LiteralValue))); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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 bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync>("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n bool "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateEnumExpression( - EnumExpression expression, - string targetVariable, - IDictionary resultMap, - string defaultValue = null, - bool qualifyResult = false, - bool isNullable = false) - where TWrapper : EnumWrapper -{ - string resultType = $"{GetTypeAlias()}{(isNullable ? "?" : "")}"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(defaultValue))); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - resultMap.TryGetValue(expression.LiteralValue, out string resultValue); - if (qualifyResult) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias())); - -this.Write("."); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultValue)); - -this.Write(";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatValue(resultValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -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(resultType)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultType)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "int?" : "int"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "0")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write(";"); - - - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateListExpression(ValueExpression expression, string targetVariable) -{ - string typeName = GetTypeAlias(); - if (expression is null) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadListAsync<"); - -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 IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write("> = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n IList<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateListAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateRecordExpression(ObjectExpression expression, string targetVariable) -{ - string resultTypeName = $"Dictionary()}?>?"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = null;"); - - - } - else if (expression.IsLiteral) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" =\n "); - -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(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -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(resultTypeName)); - -this.Write("? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateExpressionAsync<"); - -this.Write(this.ToStringHelper.ToStringWithCulture(resultTypeName)); - -this.Write(">("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - -void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false) -{ - string typeName = isNullable ? "string?" : "string"; - if (expression is null) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(isNullable ? "null" : "string.Empty")); - -this.Write(";"); - - - } - else if (expression.IsLiteral) - { - if (expression.LiteralValue.Contains("\n")) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = \n \"\"\"\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(expression.LiteralValue)); - -this.Write("\n \"\"\";"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = "); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.LiteralValue))); - -this.Write(";"); - - - } - } - else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.ReadStateAsync(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(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.VariableReference.ToString()))); - -this.Write(").ConfigureAwait(false);"); - - - } - else - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(typeName)); - -this.Write(" "); - -this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable)); - -this.Write(" = await context.EvaluateValueAsync("); - -this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText))); - -this.Write(").ConfigureAwait(false);"); - - - } -} - - void EvaluateValueExpression(ValueExpression expression, string targetVariable) => EvaluateValueExpression(expression, targetVariable); @@ -867,46 +182,5 @@ this.Write(").ConfigureAwait(false);"); } } - -void EvaluateMessageTemplate(TemplateLine templateLine, string variableName) -{ - if (templateLine is not null) - { -this.Write("\n string "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" =\n await context.FormatTemplateAsync(\n \"\"\""); - - - FormatMessageTemplate(templateLine); -this.Write("\n \"\"\");"); - - - } - else - { -this.Write("\n string? "); - -this.Write(this.ToStringHelper.ToStringWithCulture(variableName)); - -this.Write(" = null;"); - - - } -} - -void FormatMessageTemplate(TemplateLine line) -{ - foreach (string text in line.ToTemplateString().ByLine()) - { -this.Write("\n "); - -this.Write(this.ToStringHelper.ToStringWithCulture(text)); - - - } -} - } } diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.tt index 42e12c1f99..2adbb1922c 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.tt +++ b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/SetVariableTemplate.tt @@ -1,7 +1,9 @@ <#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> -<#@ include file="Snippets/Index.tt" once="true" #> +<#@ import namespace="Microsoft.Agents.ObjectModel" #> +<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #> +<#@ include file="Snippets/EvaluateValueExpressionTemplate.tt" once="true" #> /// /// Assigns an evaluated expression, other variable, or literal value to the "<#= this.Model.Variable #>" variable. /// diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/Snippets/Index.tt b/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/Snippets/Index.tt deleted file mode 100644 index 7b4e68deb3..0000000000 --- a/dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/CodeGen/Snippets/Index.tt +++ /dev/null @@ -1,14 +0,0 @@ -<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #> -<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.ObjectModel" #> -<#@ import namespace="Microsoft.Agents.ObjectModel" #> -<#@ import namespace="Microsoft.Extensions.AI" #> -<#@ import namespace="System.Collections.Generic" #> -<#@ include file="AssignVariableTemplate.tt" once="true" #> -<#@ include file="EvaluateBoolExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateEnumExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateIntExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateListExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateRecordExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateStringExpressionTemplate.tt" once="true" #> -<#@ include file="EvaluateValueExpressionTemplate.tt" once="true" #> -<#@ include file="FormatMessageTemplate.tt" once="true" #>