// ------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version: 18.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // // ------------------------------------------------------------------------------ namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen { using System.Collections.Generic; using Microsoft.Agents.AI.Workflows.Declarative.Extensions; using Microsoft.Agents.ObjectModel; using Microsoft.Extensions.AI; using System; /// /// Class to produce the template output /// [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")] internal partial class InvokeAzureAgentTemplate : ActionTemplate { /// /// Create the template output /// public override string TransformText() { this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); this.Write("\n"); 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)); this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : AgentExec" + "utor(id: \""); this.Write(this.ToStringHelper.ToStringWithCulture(this.Id)); this.Write("\", session, agentProvider)\n{\n // \n protected override async V" + "alueTask ExecuteAsync(IWorkflowContext context, CancellationToken cance" + "llationToken)\n {"); EvaluateStringExpression(this.Model.Agent.Name, "agentName", isNullable: true); this.Write("\n\n if (string.IsNullOrWhiteSpace(agentName))\n {\n throw n" + "ew DeclarativeActionException($\"Agent name must be defined: {this.Id}\");\n " + " }\n "); EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); EvaluateBoolExpression(this.Model.Output?.AutoSend, "autoSend", defaultValue: true); EvaluateListExpression(this.Model.Input?.Messages, "inputMessages"); this.Write(@" AgentResponse agentResponse = await InvokeAgentAsync( context, agentName, conversationId, autoSend, inputMessages, cancellationToken).ConfigureAwait(false); if (autoSend) { await context.AddEventAsync(new AgentResponseEvent(this.Id, agentResponse)).ConfigureAwait(false); } "); AssignVariable(this.Messages, "agentResponse.Messages"); this.Write("\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 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 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);"); } } } }