.NET Workflows - Code Generation for Declarative Workflow (#655)

* Notes

* Readme typo

* Update readme

* Checkpoint

* Namespace fix

* Fix ID and namespace

* Checkpoint

* Verified

* Comments

* Isolate "Kit"

* Address note: static

* Checkpoint

* Checkpoint "Executor<>"

* Prefix and internal executors

* Test passing

* Cleanup

* Rename "session" concept

* Revert workflow debug

* Fix template base / pragma

* Tune system scope

* Update dotnet/src/Microsoft.Agents.Workflows.Declarative/CodeGen/ResetVariableTemplate.tt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix empty template

* Add validation for codegen ut

* Fix test

* Codegen baselines

* Constant

* Prep

* Mark TODO

* Fix

* Namespace

* One more

* Update baselines

* Checkpoint

* Checkpoint

* Checkpoint

* fme

* Checkpoint

* Another step

* Fixed up

* Roslyn

* Fix

* More cleaning

* Async

* Fix

* Enum checkpoint

* Refine enum

* Checkpoint

* Sync templates

* Checkpoint

* Streamline

* Pre-merge analyzer updates

* Foreach

* Placeholders

* Checkpoint

* Clean-up

* Sample path resolution

* Checkpoint

* Checkpoint - Workflow Code Building

* Validation

* Test cleanup

* Update test basline

* Update test baseline

* Fix DefaultTemplate usage

* Validation checkpoint

* Fix break/continue edges

* Verify generated code builds

* Fix merge

* Fix build validation

* Update template handling of literal string values.

* Test for metadata case

* Update baselines

* Fix merge

* Checkpoint

* Checkpoint: Conditions

* Invoke Agent Checkpoint

* Namespace

* Address code-analysis issues

* Cross platform test support

* Invoke agent checkpoint

* Clean sample

* Checkpoint: Agent Invoke Input Messages

* Checkpoint - Passing

* Checkpoint

* Regenerate all template + port conversation fix

* Checkpoint: Tests good

* Fix test for unbuntu

* Fix build command

* Checkpoint - E2E

* Test fix

* Update integration tests

* Fix merge

* Update

* Checkpoint !!!

* Baby steps

* Checkpoint

* Checkpoint E2E !!!

* So close...

* Integrate test validation

* Fix merge

* Rebase tests

* Namespace

* Namespace

* Test cleanup

* Sample comment cleanup

* Checkpoint: List conversion

* Include these

* CheckPoint: ParseValue

* Namespace

* Fix sampel

* More namspace

* Comments

* Test updates

* Test fix

* Better build

* Shared code

* Sort solution

* Fix build

* Prune solution

* One more

* Conversion matrix

* Final table conversion

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Chris
2025-09-30 21:56:14 +00:00
committed by GitHub
co-authored by Copilot
parent 40f5b6d8fe
commit 77404d165c
181 changed files with 56682 additions and 230 deletions
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal abstract class ActionTemplate : CodeTemplate, IModeledAction
{
public string Id { get; private set; } = string.Empty;
public string Name { get; private set; } = string.Empty;
public string ParentId { get; private set; } = string.Empty;
public bool UseAgentProvider { get; init; }
protected TAction Initialize<TAction>(TAction model) where TAction : DialogAction
{
this.Id = model.GetId();
this.ParentId = model.GetParentId() ?? WorkflowActionVisitor.Steps.Root();
this.Name = this.Id.FormatType();
return model;
}
}
@@ -0,0 +1,57 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Adds a new message to the specified agent conversation
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); #>
ArgumentNullException.ThrowIfNull(conversationId, nameof(conversationId));
ChatMessage newMessage = new(ChatRole.<#= FormatEnum(this.Model.Role, RoleMap) #>, [.. this.GetContentAsync(context).ToEnumerable()]) { AdditionalProperties = this.GetMetadata() };
await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);<#
AssignVariable(this.Message, "newMessage");
#>
return default;
}
private async IAsyncEnumerable<AIContent> GetContentAsync(IWorkflowContext context)
{<#
int index = 0;
foreach (AddConversationMessageContent content in this.Model.Content)
{
++index;
EvaluateMessageTemplate(content.Value, $"contentValue{index}");
AgentMessageContentType contentType = content.Type.Value;
if (contentType == AgentMessageContentType.ImageUrl)
{#>
yield return new UriContent(contentValue, "image/*");<#
}
else if (contentType == AgentMessageContentType.ImageFile)
{#>
yield return new HostedFileContent(contentValue);<#
}
else
{#>
yield return new TextContent(contentValue<#= index #>);<#
}
}#>
}
private AdditionalPropertiesDictionary? GetMetadata()
{<#
EvaluateRecordExpression<object>(this.Model.Metadata, "metadata"); #>
if (metadata is null)
{
return null;
}
return new AdditionalPropertiesDictionary(metadata);
}
}
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Frozen;
using System.Collections.Generic;
using Microsoft.Bot.ObjectModel;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class AddConversationMessageTemplate
{
public AddConversationMessageTemplate(AddConversationMessage model)
{
this.Model = this.Initialize(model);
this.Message = this.Model.Message?.Path;
this.UseAgentProvider = true;
}
public AddConversationMessage Model { get; }
public PropertyPath? Message { get; }
public const string DefaultRole = nameof(ChatRole.User);
public static readonly FrozenDictionary<AgentMessageRoleWrapper, string> RoleMap =
new Dictionary<AgentMessageRoleWrapper, string>()
{
[AgentMessageRoleWrapper.Get(AgentMessageRole.User)] = nameof(ChatRole.User),
[AgentMessageRoleWrapper.Get(AgentMessageRole.Agent)] = nameof(ChatRole.Assistant),
}.ToFrozenDictionary();
}
@@ -0,0 +1,18 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Reset all the state for the targeted variable scope.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateEnumExpression<VariablesToClearWrapper, string>(this.Model.Variables, "targetScopeName", ScopeMap, isNullable: true); #>
await context.QueueClearScopeAsync(targetScopeName).ConfigureAwait(false);
return default;
}
}
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Frozen;
using System.Collections.Generic;
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ClearAllVariablesTemplate
{
public ClearAllVariablesTemplate(ClearAllVariables model)
{
this.Model = this.Initialize(model);
}
public ClearAllVariables Model { get; }
public static readonly FrozenDictionary<VariablesToClearWrapper, string?> ScopeMap =
new Dictionary<VariablesToClearWrapper, string?>()
{
[VariablesToClearWrapper.Get(VariablesToClear.AllGlobalVariables)] = VariableScopeNames.Global,
[VariablesToClearWrapper.Get(VariablesToClear.ConversationScopedVariables)] = WorkflowFormulaState.DefaultScopeName,
}.ToFrozenDictionary();
}
@@ -0,0 +1,345 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal abstract class CodeTemplate
{
private StringBuilder? _generationEnvironmentField;
private CompilerErrorCollection? _errorsField;
private List<int>? _indentLengthsField;
private bool _endsWithNewline;
private string CurrentIndentField { get; set; } = string.Empty;
/// <summary>
/// Create the template output
/// </summary>
public abstract string TransformText();
#region Object Model helpers
public static string VariableName(PropertyPath path) => Throw.IfNull(path.VariableName);
public static string VariableScope(PropertyPath path) => Throw.IfNull(path.NamespaceAlias);
public static string FormatBoolValue(bool? value, bool defaultValue = false) =>
value ?? defaultValue ? "true" : "false";
public static string FormatStringValue(string? value)
{
if (value is null)
{
return "null";
}
if (value.Contains('\n') || value.Contains('\r'))
{
return @$"""""""{Environment.NewLine}{value}{Environment.NewLine}""""""";
}
if (value.Contains('"') || value.Contains('\\'))
{
return @$"""""""{value}""""""";
}
return @$"""{value}""";
}
public static string FormatValue<TValue>(string? value)
{
if (typeof(TValue) == typeof(string))
{
return FormatStringValue(value);
}
if (value is null)
{
return "null";
}
if (typeof(TValue).IsEnum)
{
return $"{typeof(TValue).Name}.{value}";
}
return $"{value}";
}
public static string FormatDataValue(DataValue value) =>
value switch
{
BlankDataValue => "null",
BooleanDataValue booleanValue => FormatBoolValue(booleanValue.Value),
FloatDataValue decimalValue => $"{decimalValue.Value}",
NumberDataValue numberValue => $"{numberValue.Value}",
DateDataValue dateValue => $"new DateTime({dateValue.Value.Ticks}, DateTimeKind.{dateValue.Value.Kind})",
DateTimeDataValue datetimeValue => $"new DateTimeOffset({datetimeValue.Value.Ticks}, TimeSpan.FromTicks({datetimeValue.Value.Offset}))",
TimeDataValue timeValue => $"TimeSpan.FromTicks({timeValue.Value.Ticks})",
StringDataValue stringValue => FormatStringValue(stringValue.Value),
OptionDataValue optionValue => @$"""{optionValue.Value}""",
// Indenting is important here to make the generated code readable. Don't change it without testing the output.
RecordDataValue recordValue =>
$"""
[
{string.Join(",\n ", recordValue.Properties.Select(p => $"[\"{p.Key}\"] = {FormatDataValue(p.Value)}"))}
]
""",
_ => throw new DeclarativeModelException($"Unable to format '{value.GetType().Name}'"),
};
public static TTarget FormatEnum<TSource, TTarget>(TSource value, IDictionary<TSource, TTarget> map, TTarget? defaultValue = default)
{
if (map.TryGetValue(value, out TTarget? target))
{
return target;
}
if (defaultValue is null)
{
throw new DeclarativeModelException($"No default value suppied for '{typeof(TTarget).Name}'");
}
return defaultValue;
}
public static string GetTypeAlias<TValue>() => GetTypeAlias(typeof(TValue));
public static string GetTypeAlias(Type type)
{
return type switch
{
Type t when t == typeof(bool) => "bool",
Type t when t == typeof(byte) => "byte",
Type t when t == typeof(sbyte) => "sbyte",
Type t when t == typeof(char) => "char",
Type t when t == typeof(decimal) => "decimal",
Type t when t == typeof(double) => "double",
Type t when t == typeof(float) => "float",
Type t when t == typeof(int) => "int",
Type t when t == typeof(uint) => "uint",
Type t when t == typeof(long) => "long",
Type t when t == typeof(ulong) => "ulong",
Type t when t == typeof(nint) => "nint",
Type t when t == typeof(nuint) => "nuint",
Type t when t == typeof(short) => "short",
Type t when t == typeof(ushort) => "ushort",
Type t when t == typeof(string) => "string",
Type t when t == typeof(object) => "object",
_ => type.Name
};
}
#endregion
#region Properties
/// <summary>
/// The string builder that generation-time code is using to assemble generated output
/// </summary>
public StringBuilder GenerationEnvironment
{
get
{
return this._generationEnvironmentField ??= new StringBuilder();
}
set
{
this._generationEnvironmentField = value;
}
}
/// <summary>
/// The error collection for the generation process
/// </summary>
public CompilerErrorCollection Errors => this._errorsField ??= [];
/// <summary>
/// A list of the lengths of each indent that was added with PushIndent
/// </summary>
private List<int> indentLengths => this._indentLengthsField ??= [];
/// <summary>
/// Gets the current indent we use when adding lines to the output
/// </summary>
public string CurrentIndent
{
get
{
return this.CurrentIndentField;
}
}
/// <summary>
/// Current transformation session
/// </summary>
public virtual IDictionary<string, object>? Session { get; set; }
#endregion
#region Transform-time helpers
/// <summary>
/// Write text directly into the generated output
/// </summary>
public void Write(string textToAppend)
{
if (string.IsNullOrEmpty(textToAppend))
{
return;
}
// If we're starting off, or if the previous text ended with a newline,
// we have to append the current indent first.
if ((this.GenerationEnvironment.Length == 0)
|| this._endsWithNewline)
{
this.GenerationEnvironment.Append(this.CurrentIndentField);
this._endsWithNewline = false;
}
// Check if the current text ends with a newline
if (textToAppend.EndsWith(Environment.NewLine, StringComparison.CurrentCulture))
{
this._endsWithNewline = true;
}
// This is an optimization. If the current indent is "", then we don't have to do any
// of the more complex stuff further down.
if (this.CurrentIndentField.Length == 0)
{
this.GenerationEnvironment.Append(textToAppend);
return;
}
// Everywhere there is a newline in the text, add an indent after it
textToAppend = textToAppend.Replace(Environment.NewLine, Environment.NewLine + this.CurrentIndentField);
// If the text ends with a newline, then we should strip off the indent added at the very end
// because the appropriate indent will be added when the next time Write() is called
if (this._endsWithNewline)
{
this.GenerationEnvironment.Append(textToAppend, 0, textToAppend.Length - this.CurrentIndentField.Length);
}
else
{
this.GenerationEnvironment.Append(textToAppend);
}
}
/// <summary>
/// Write text directly into the generated output
/// </summary>
public void WriteLine(string textToAppend)
{
this.Write(textToAppend);
this.GenerationEnvironment.AppendLine();
this._endsWithNewline = true;
}
/// <summary>
/// Write formatted text directly into the generated output
/// </summary>
public void Write(string format, params object[] args)
{
this.Write(string.Format(CultureInfo.CurrentCulture, format, args));
}
/// <summary>
/// Write formatted text directly into the generated output
/// </summary>
public void WriteLine(string format, params object[] args)
{
this.WriteLine(string.Format(CultureInfo.CurrentCulture, format, args));
}
/// <summary>
/// Raise an error
/// </summary>
public void Error(string message)
{
CompilerError error = new()
{
ErrorText = message
};
this.Errors.Add(error);
}
/// <summary>
/// Raise a warning
/// </summary>
public void Warning(string message)
{
CompilerError error = new()
{
ErrorText = message,
IsWarning = true
};
error.ErrorText = message;
error.IsWarning = true;
this.Errors.Add(error);
}
/// <summary>
/// Increase the indent
/// </summary>
public void PushIndent(string indent)
{
if (indent is null)
{
throw new ArgumentNullException(nameof(indent));
}
this.CurrentIndentField += indent;
this.indentLengths.Add(indent.Length);
}
/// <summary>
/// Remove the last indent that was added with PushIndent
/// </summary>
public string PopIndent()
{
string returnValue = string.Empty;
if (this.indentLengths.Count > 0)
{
int indentLength = this.indentLengths[this.indentLengths.Count - 1];
this.indentLengths.RemoveAt(this.indentLengths.Count - 1);
if (indentLength > 0)
{
returnValue = this.CurrentIndentField.Substring(this.CurrentIndentField.Length - indentLength);
this.CurrentIndentField = this.CurrentIndentField.Remove(this.CurrentIndentField.Length - indentLength);
}
}
return returnValue;
}
/// <summary>
/// Remove any indentation
/// </summary>
public void ClearIndent()
{
this.indentLengths.Clear();
this.CurrentIndentField = string.Empty;
}
#endregion
#region ToString Helpers
/// <summary>
/// Utility class to produce culture-oriented representation of an object as a string.
/// </summary>
public sealed class ToStringInstanceHelper
{
/// <summary>
/// This is called from the compile/run appdomain to convert objects within an expression block to a string
/// </summary>
#pragma warning disable CA1822 // Required to be non-static for use in generated code
public string ToStringWithCulture(object objectToConvert) => $"{objectToConvert}";
#pragma warning restore CA1822
}
/// <summary>
/// Helper to produce culture-oriented representation of an object as a string
/// </summary>
public ToStringInstanceHelper ToStringHelper { get; } = new();
#endregion
}
@@ -0,0 +1,31 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Conditional branching similar to an if / elseif / elseif / else chain.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
for (int index = 0; index < this.Model.Conditions.Length; ++index)
{
ConditionItem conditionItem = this.Model.Conditions[index];
if (conditionItem.Condition is null)
{
continue; // Skip if no condition is defined
}
EvaluateBoolExpression(conditionItem.Condition, $"condition{index}");#>
if (condition<#= index #>)
{
return "<#= ConditionGroupExecutor.Steps.Item(this.Model, conditionItem)#>";
}
<#
}
#>
return "<#= ConditionGroupExecutor.Steps.Else(this.Model)#>";
}
}
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ConditionGroupTemplate
{
public ConditionGroupTemplate(ConditionGroup model)
{
this.Model = this.Initialize(model);
}
public ConditionGroup Model { get; }
}
@@ -0,0 +1,26 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Copies one or more messages into the specified agent conversation.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); #>
ArgumentNullException.ThrowIfNull(conversationId, nameof(conversationId));<#
EvaluateValueExpression<ChatMessage[]>(this.Model.Messages, "messages");
#>
if (messages is not null)
{
foreach (ChatMessage message in messages)
{
await agentProvider.CreateMessageAsync(conversationId, message, cancellationToken).ConfigureAwait(false);
}
}
return default;
}
}
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class CopyConversationMessagesTemplate
{
public CopyConversationMessagesTemplate(CopyConversationMessages model)
{
this.Model = this.Initialize(model);
this.UseAgentProvider = true;
}
public CopyConversationMessages Model { get; }
}
@@ -0,0 +1,17 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Creates a new conversation and stores the identifier value to the "<#= this.Model.ConversationId #>" variable.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
string conversationId = await agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);<#
AssignVariable(this.ConversationId, "conversationId");
#>
return default;
}
}
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class CreateConversationTemplate
{
public CreateConversationTemplate(CreateConversation model)
{
this.Model = this.Initialize(model);
this.ConversationId = Throw.IfNull(this.Model.ConversationId);
this.UseAgentProvider = true;
}
public CreateConversation Model { get; }
public PropertyPath ConversationId { get; }
}
@@ -0,0 +1,66 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\DefaultTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class DefaultTemplate : ActionTemplate, IModeledAction
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
this.Write("\nDelegateExecutor ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\DefaultTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
#line default
#line hidden
this.Write(" = new(id: \"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\DefaultTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
#line default
#line hidden
this.Write("\", ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\DefaultTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
#line default
#line hidden
this.Write(".Session");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\DefaultTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Action is not null ? $", {this.Action}" : ""));
#line default
#line hidden
this.Write(");\n");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,4 @@
<#@ template language="C#" inherits="ActionTemplate, IModeledAction" visibility="internal" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Interpreter" #>
<#@ assembly name="System.Core" #>
DelegateExecutor <#= this.InstanceVariable #> = new(id: "<#= this.Id #>", <#= this.RootVariable #>.Session<#= this.Action is not null ? $", {this.Action}" : "" #>);
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class DefaultTemplate
{
public DefaultTemplate(DialogAction model, string rootId, string? action = null)
{
this.Initialize(model);
this.Action = action;
this.InstanceVariable = this.Id.FormatName();
this.RootVariable = rootId.FormatName();
}
public string? Action { get; }
public string InstanceVariable { get; }
public string RootVariable { get; }
}
@@ -0,0 +1,97 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class EdgeTemplate : CodeTemplate
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
if (this.Condition is not null)
{
#line default
#line hidden
this.Write("\n builder.AddEdge(");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.SourceId));
#line default
#line hidden
this.Write(", ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.TargetId));
#line default
#line hidden
this.Write(", (object? result) => ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Condition));
#line default
#line hidden
this.Write(");");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
}
else
{
#line default
#line hidden
this.Write("\n builder.AddEdge(");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.SourceId));
#line default
#line hidden
this.Write(", ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.TargetId));
#line default
#line hidden
this.Write(");");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EdgeTemplate.tt"
}
#line default
#line hidden
this.Write("\n");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,10 @@
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" #>
<#@ assembly name="System.Core" #>
<# if (this.Condition is not null)
{#>
builder.AddEdge(<#= this.SourceId #>, <#= this.TargetId #>, (object? result) => <#= this.Condition #>);<#
}
else
{#>
builder.AddEdge(<#= this.SourceId #>, <#= this.TargetId #>);<#
} #>
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class EdgeTemplate
{
public EdgeTemplate(string sourceId, string targetId, string? condition = null)
{
this.SourceId = sourceId.FormatName();
this.TargetId = targetId.FormatName();
this.Condition = condition;
}
public string SourceId { get; }
public string TargetId { get; }
public string? Condition { get; }
}
@@ -0,0 +1,15 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Modify items in a list
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
return default;
}
}
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class EditTableV2Template
{
public EditTableV2Template(EditTableV2 model)
{
this.Model = this.Initialize(model);
}
public EditTableV2 Model { get; }
}
@@ -0,0 +1,66 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EmptyTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class EmptyTemplate : CodeTemplate, IModeledAction
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
this.Write("\nDelegateExecutor ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EmptyTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
#line default
#line hidden
this.Write(" = new(id: \"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EmptyTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
#line default
#line hidden
this.Write("\", ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EmptyTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
#line default
#line hidden
this.Write(".Session");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\EmptyTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Action is not null ? $", {this.Action}" : ""));
#line default
#line hidden
this.Write(");\n");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,4 @@
<#@ template language="C#" inherits="CodeTemplate, IModeledAction" visibility="internal" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Interpreter" #>
<#@ assembly name="System.Core" #>
DelegateExecutor <#= this.InstanceVariable #> = new(id: "<#= this.Id #>", <#= this.RootVariable #>.Session<#= this.Action is not null ? $", {this.Action}" : "" #>);
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class EmptyTemplate
{
public EmptyTemplate(string actionId, string rootId, string? action = null)
{
this.Id = actionId;
this.Name = this.Id.FormatType();
this.InstanceVariable = this.Id.FormatName();
this.RootVariable = rootId.FormatName();
this.Action = action;
}
public string Id { get; }
public string Name { get; }
public string InstanceVariable { get; }
public string RootVariable { get; }
public string? Action { get; }
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,70 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Loops over a list assignign the loop variable to "<#= this.Model.Value #>" variable.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
private int _index;
private object[] _values = [];
public bool HasValue { get; private set; }
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
this._index = 0;<#
EvaluateValueExpression(this.Model.Items, "evaluatedValue");#>
if (evaluatedValue == null)
{
this._values = [];
this.HasValue = false;
}
else
if (evaluatedValue is IEnumerable evaluatedList)
{
this._values = [.. evaluatedList];
}
else
{
this._values = [evaluatedValue];
}
await this.ResetAsync(context, null, cancellationToken).ConfigureAwait(false);
return default;
}
public async ValueTask TakeNextAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken)
{
if (this.HasValue = this._index < this._values.Length)
{
object value = this._values[this._index];
<#
AssignVariable(this.Value, "value", tightFormat: true);
if (this.Index is not null)
{
AssignVariable(this.Index, "this._index", tightFormat: true);
}
#>
this._index++;
}
}
public async ValueTask ResetAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken)
{<#
AssignVariable(this.Value, "UnassignedValue.Instance", tightFormat: true);
if (this.Index is not null)
{
AssignVariable(this.Index, "UnassignedValue.Instance", tightFormat: true);
}
#>
}
}
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ForeachTemplate
{
public ForeachTemplate(Foreach model)
{
this.Model = this.Initialize(model);
this.Index = this.Model.Index?.Path;
this.Value = Throw.IfNull(this.Model.Value);
}
public Foreach Model { get; }
public PropertyPath? Index { get; }
public PropertyPath Value { get; }
}
@@ -0,0 +1,64 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\InstanceTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class InstanceTemplate : CodeTemplate
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\InstanceTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.ExecutorType));
#line default
#line hidden
this.Write("Executor ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\InstanceTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
#line default
#line hidden
this.Write(" = new(");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\InstanceTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
#line default
#line hidden
this.Write(".Session");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\InstanceTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.HasProvider ? ", options.AgentProvider" : ""));
#line default
#line hidden
this.Write(");");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,3 @@
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" #>
<#@ assembly name="System.Core" #>
<#= this.ExecutorType #>Executor <#= this.InstanceVariable #> = new(<#= this.RootVariable #>.Session<#= this.HasProvider ? ", options.AgentProvider" : "" #>);
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class InstanceTemplate
{
public InstanceTemplate(string executorId, string rootId, bool hasProvider = false)
{
this.InstanceVariable = executorId.FormatName();
this.ExecutorType = executorId.FormatType();
this.RootVariable = rootId.FormatName();
this.HasProvider = hasProvider;
}
public string InstanceVariable { get; }
public string ExecutorType { get; }
public string RootVariable { get; }
public bool HasProvider { get; }
}
@@ -0,0 +1,43 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Invokes an agent to process messages and return a response within a conversation context.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : AgentExecutor(id: "<#= this.Id #>", session, agentProvider)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.Agent.Name, "agentName", isNullable: true);#>
if (string.IsNullOrWhiteSpace(agentName))
{
throw new InvalidOperationException($"Agent name must be defined: {this.Id}");
}
<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true);
EvaluateBoolExpression(this.Model.Output?.AutoSend, "autoSend", defaultValue: true);
EvaluateMessageTemplate(this.Model.Input?.AdditionalInstructions, "additionalInstructions");
EvaluateListExpression<ChatMessage>(this.Model.Input?.Messages, "inputMessages");#>
AgentRunResponse agentResponse =
await InvokeAgentAsync(
context,
agentName,
conversationId,
autoSend,
additionalInstructions,
inputMessages,
cancellationToken).ConfigureAwait(false);
if (autoSend)
{
await context.AddEventAsync(new AgentRunResponseEvent(this.Id, agentResponse)).ConfigureAwait(false);
}
<#
AssignVariable(this.Messages, "agentResponse.Messages"); #>
return default;
}
}
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class InvokeAzureAgentTemplate
{
public InvokeAzureAgentTemplate(InvokeAzureAgent model)
{
this.Model = this.Initialize(model);
this.Messages = this.Model.Output?.Messages?.Path;
this.UseAgentProvider = true;
}
public InvokeAzureAgent Model { get; }
public PropertyPath? Messages { get; }
}
@@ -0,0 +1,29 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Parses a string or untyped value to the provided data type. When the input is a string, it will be treated as JSON.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
VariableType targetType = <#= this.GetVariableType() #>;<#
if (this.Model.Value.IsVariableReference && this.Model.Value.VariableReference.SegmentCount == 2)
{#>
object? parsedValue = await context.ConvertValueAsync(targetType, key: "<#= this.Model.Value.VariableReference.VariableName #>", scopeName: "<#= this.Model.Value.VariableReference.NamespaceAlias #>", cancellationToken).ConfigureAwait(false);<#
}
else if (this.Model.Value.IsVariableReference)
{#>
object? parsedValue = await context.ConvertValueAsync(targetType, <#= FormatStringValue(this.Model.Value.VariableReference.ToString()) #>, cancellationToken).ConfigureAwait(false);<#
}
else
{#>
object? parsedValue = await context.ConvertValueAsync(targetType, <#= FormatStringValue(this.Model.Value.ExpressionText) #>, cancellationToken).ConfigureAwait(false);<#
}
AssignVariable(this.Variable, "parsedValue"); #>
return default;
}
}
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Linq;
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ParseValueTemplate
{
public ParseValueTemplate(ParseValue model)
{
this.Model = this.Initialize(model);
this.Variable = Throw.IfNull(this.Model.Variable);
}
public ParseValue Model { get; }
public PropertyPath Variable { get; }
private string GetVariableType()
{
return GetVariableType(this.Model.ValueType);
static string GetVariableType(DataType? dataType) =>
dataType switch
{
null => "null",
StringDataType => "typeof(string)",
BooleanDataType => "typeof(bool)",
FloatDataType => "typeof(double)",
NumberDataType => "typeof(decimal)",
DateTimeDataType => "typeof(DateTime)",
DateDataType => "typeof(DateTime)",
TimeDataType => "typeof(TimeSpan)",
RecordDataType recordType => $"\nVariableType.Record(\n{string.Join(",\n ", recordType.Properties.Select(property => @$"( ""{property.Key}"", {GetVariableType(property.Value.Type)} )"))})",
TableDataType tableType => $"\nVariableType.Record(\n{string.Join(",\n ", tableType.Properties.Select(property => @$"( ""{property.Key}"", {GetVariableType(property.Value.Type)} )"))})",
_ => throw new DeclarativeModelException($"Unsupported data type: {dataType}"),
};
}
}
@@ -0,0 +1,203 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class ProviderTemplate : CodeTemplate
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
this.Write(@"
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// </auto-generated>
// ------------------------------------------------------------------------------
#nullable enable
#pragma warning disable IDE0005 // Extra using directive is ok.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Declarative;
using Microsoft.Agents.AI.Workflows.Declarative.Kit;
using Microsoft.Extensions.AI;
");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
if (this.Namespace is not null)
{
#line default
#line hidden
this.Write("\nnamespace ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Namespace));
#line default
#line hidden
this.Write(";\n");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
}
#line default
#line hidden
this.Write(@"
/// <summary>
/// This class provides a factory method to create a <see cref=""Workflow"" /> instance.
/// </summary>
/// <remarks>
/// The workflow defined here was generated from a declarative workflow definition.
/// Declarative workflows utilize Power FX for defining conditions and expressions.
/// To learn more about Power FX, see:
/// https://learn.microsoft.com/power-platform/power-fx/formula-reference-copilot-studio
/// </remarks>
public static class ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Prefix ?? string.Empty));
#line default
#line hidden
this.Write("WorkflowProvider\n{");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
foreach (string executor in ByLine(this.Executors, formatGroup: true))
{
#line default
#line hidden
this.Write("\n ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(executor));
#line default
#line hidden
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
}
#line default
#line hidden
this.Write(@"
public static Workflow CreateWorkflow<TInput>(
DeclarativeWorkflowOptions options,
Func<TInput, ChatMessage>? inputTransform = null)
where TInput : notnull
{
// Create root executor to initialize the workflow.
inputTransform ??= (message) => DeclarativeWorkflowBuilder.DefaultTransform(message);
");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootExecutorType));
#line default
#line hidden
this.Write("Executor<TInput> ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootInstance));
#line default
#line hidden
this.Write(" = new(options, inputTransform);");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
// Create executor instances
foreach (string instance in ByLine(this.Instances))
{
#line default
#line hidden
this.Write("\n ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(instance));
#line default
#line hidden
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
}
#line default
#line hidden
this.Write("\n\n // Define the workflow builder\n WorkflowBuilder builder = new(");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootInstance));
#line default
#line hidden
this.Write(");\n\n // Connect executors");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
foreach (string edge in ByLine(this.Edges))
{
#line default
#line hidden
this.Write("\n ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(edge));
#line default
#line hidden
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\ProviderTemplate.tt"
}
#line default
#line hidden
this.Write("\n\n // Build the workflow\n return builder.Build();\n }\n}\n");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,75 @@
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #>
<#@ assembly name="System.Core" #>
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// </auto-generated>
// ------------------------------------------------------------------------------
#nullable enable
#pragma warning disable IDE0005 // Extra using directive is ok.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Declarative;
using Microsoft.Agents.AI.Workflows.Declarative.Kit;
using Microsoft.Extensions.AI;
<#
if (this.Namespace is not null)
{#>
namespace <#= this.Namespace #>;
<#
}
#>
/// <summary>
/// This class provides a factory method to create a <see cref="Workflow" /> instance.
/// </summary>
/// <remarks>
/// The workflow defined here was generated from a declarative workflow definition.
/// Declarative workflows utilize Power FX for defining conditions and expressions.
/// To learn more about Power FX, see:
/// https://learn.microsoft.com/power-platform/power-fx/formula-reference-copilot-studio
/// </remarks>
public static class <#= this.Prefix ?? string.Empty #>WorkflowProvider
{<#
foreach (string executor in ByLine(this.Executors, formatGroup: true))
{ #>
<#= executor #><#
}
#>
public static Workflow CreateWorkflow<TInput>(
DeclarativeWorkflowOptions options,
Func<TInput, ChatMessage>? inputTransform = null)
where TInput : notnull
{
// Create root executor to initialize the workflow.
inputTransform ??= (message) => DeclarativeWorkflowBuilder.DefaultTransform(message);
<#= this.RootExecutorType #>Executor<TInput> <#= this.RootInstance #> = new(options, inputTransform);<#
// Create executor instances
foreach (string instance in ByLine(this.Instances))
{ #>
<#= instance #><#
}#>
// Define the workflow builder
WorkflowBuilder builder = new(<#= this.RootInstance #>);
// Connect executors<#
foreach (string edge in ByLine(this.Edges))
{ #>
<#= edge #><#
}
#>
// Build the workflow
return builder.Build();
}
}
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ProviderTemplate
{
public ProviderTemplate(
string workflowId,
IEnumerable<string> executors,
IEnumerable<string> instances,
IEnumerable<string> edges)
{
this.Executors = executors;
this.Instances = instances;
this.Edges = edges;
this.RootInstance = workflowId.FormatName();
this.RootExecutorType = workflowId.FormatType();
}
public string? Namespace { get; init; }
public string? Prefix { get; init; }
public string RootInstance { get; }
public string RootExecutorType { get; }
public IEnumerable<string> Executors { get; }
public IEnumerable<string> Instances { get; }
public IEnumerable<string> Edges { get; }
public static IEnumerable<string> ByLine(IEnumerable<string> templates, bool formatGroup = false)
{
foreach (string template in templates)
{
foreach (string line in template.ByLine())
{
yield return line;
}
if (formatGroup)
{
yield return string.Empty;
}
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,15 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Request input.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
return default;
}
}
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class QuestionTemplate
{
public QuestionTemplate(Question model)
{
this.Model = this.Initialize(model);
}
public Question Model { get; }
}
@@ -0,0 +1,16 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// 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.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
AssignVariable(this.Variable, "UnassignedValue.Instance"); #>
return default;
}
}
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class ResetVariableTemplate
{
public ResetVariableTemplate(ResetVariable model)
{
this.Model = this.Initialize(model);
this.Variable = Throw.IfNull(this.Model.Variable);
}
public ResetVariable Model { get; }
public PropertyPath Variable { get; }
}
@@ -0,0 +1,20 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Retrieves a list of messages from an agent conversation.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId");
EvaluateStringExpression(this.Model.MessageId, "messageId"); #>
ChatMessage message = await agentProvider.GetMessageAsync(conversationId, messageId, cancellationToken).ConfigureAwait(false);<#
AssignVariable(this.Model.Message, "message");
#>
return default;
}
}
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class RetrieveConversationMessageTemplate
{
public RetrieveConversationMessageTemplate(RetrieveConversationMessage model)
{
this.Model = this.Initialize(model);
this.UseAgentProvider = true;
}
public RetrieveConversationMessage Model { get; }
}
@@ -0,0 +1,30 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>f
/// <summary>
/// Retrieves a specific message from an agent conversation.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session, WorkflowAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateStringExpression(this.Model.ConversationId, "conversationId");
EvaluateIntExpression(this.Model.Limit, "limit");
EvaluateStringExpression(this.Model.MessageAfter, "after", isNullable: true);
EvaluateStringExpression(this.Model.MessageBefore, "before", isNullable: true);
EvaluateEnumExpression<AgentMessageSortOrderWrapper, bool>(this.Model.SortOrder, "newestFirst", SortMap, defaultValue: DefaultSort); #>
ChatMessage messages =
await agentProvider.GetMessageAsync(
converationId,
limit,
after,
before,
newestFirst,
cancellationToken).ConfigureAwait(false);<#
AssignVariable(this.Model.Messages, "messages");
#>
return default;
}
}
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Frozen;
using System.Collections.Generic;
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class RetrieveConversationMessagesTemplate
{
public RetrieveConversationMessagesTemplate(RetrieveConversationMessages model)
{
this.Model = this.Initialize(model);
this.UseAgentProvider = true;
}
public RetrieveConversationMessages Model { get; }
public const string DefaultSort = "false";
public static readonly FrozenDictionary<AgentMessageSortOrderWrapper, string> SortMap =
new Dictionary<AgentMessageSortOrderWrapper, string>()
{
[AgentMessageSortOrderWrapper.Get(AgentMessageSortOrder.NewestFirst)] = "true",
[AgentMessageSortOrderWrapper.Get(AgentMessageSortOrder.OldestFirst)] = "false",
}.ToFrozenDictionary();
}
@@ -0,0 +1,145 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
{
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
using Microsoft.Bot.ObjectModel;
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
internal partial class RootTemplate : CodeTemplate, IModeledAction
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public override string TransformText()
{
this.Write("\n");
this.Write("\n");
this.Write("\n");
this.Write("\n");
this.Write("\n/// <summary>\n/// The root executor for a declarative workflow.\n/// </summary>\ni" +
"nternal sealed class ");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.TypeName));
#line default
#line hidden
this.Write("Executor<TInput>(\n DeclarativeWorkflowOptions options,\n Func<TInput, ChatMe" +
"ssage> inputTransform) :\n RootExecutor<TInput>(\"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
#line default
#line hidden
this.Write("\", options, inputTransform)\n where TInput : notnull\n{\n protected override a" +
"sync ValueTask ExecuteAsync(TInput message, IWorkflowContext context, Cancellati" +
"onToken cancellationToken)\n {");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
if (this.TypeInfo.EnvironmentVariables.Count > 0)
{
#line default
#line hidden
this.Write("\n // Set environment variables\n await this.InitializeEnvironmentAsy" +
"nc(\n context,");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
int index = this.TypeInfo.EnvironmentVariables.Count - 1;
foreach (string variableName in this.TypeInfo.EnvironmentVariables)
{
#line default
#line hidden
this.Write("\n \"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(variableName));
#line default
#line hidden
this.Write("\"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(index > 0 ? "," : ""));
#line default
#line hidden
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
--index;
}
#line default
#line hidden
this.Write(").ConfigureAwait(false);\n");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
}
if (this.TypeInfo.UserVariables.Count > 0)
{
#line default
#line hidden
this.Write("\n // Initialize variables");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
foreach (VariableInformationDiagnostic variableInfo in this.TypeInfo.UserVariables)
{
#line default
#line hidden
this.Write("\n await context.QueueStateUpdateAsync(\"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(variableInfo.Path.VariableName));
#line default
#line hidden
this.Write("\", UnassignedValue.Instance, \"");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(variableInfo.Path.NamespaceAlias));
#line default
#line hidden
this.Write("\").ConfigureAwait(false);");
#line 1 "C:\Users\crickman\source\repos\af5\dotnet\src\Microsoft.Agents.AI.Workflows.Declarative\CodeGen\RootTemplate.tt"
}
}
#line default
#line hidden
this.Write("\n }\n}\n");
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
}
@@ -0,0 +1,40 @@
<#@ template language="C#" inherits="CodeTemplate, IModeledAction" visibility="internal" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Interpreter" #>
<#@ import namespace="Microsoft.Bot.ObjectModel" #>
<#@ assembly name="System.Core" #>
/// <summary>
/// The root executor for a declarative workflow.
/// </summary>
internal sealed class <#= this.TypeName #>Executor<TInput>(
DeclarativeWorkflowOptions options,
Func<TInput, ChatMessage> inputTransform) :
RootExecutor<TInput>("<#= this.Id #>", options, inputTransform)
where TInput : notnull
{
protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken)
{<#
if (this.TypeInfo.EnvironmentVariables.Count > 0)
{ #>
// Set environment variables
await this.InitializeEnvironmentAsync(
context,<#
int index = this.TypeInfo.EnvironmentVariables.Count - 1;
foreach (string variableName in this.TypeInfo.EnvironmentVariables)
{#>
"<#= variableName #>"<#= index > 0 ? "," : "" #><#
--index;
}#>).ConfigureAwait(false);
<#}
if (this.TypeInfo.UserVariables.Count > 0)
{
#>
// Initialize variables<#
foreach (VariableInformationDiagnostic variableInfo in this.TypeInfo.UserVariables)
{#>
await context.QueueStateUpdateAsync("<#= variableInfo.Path.VariableName #>", UnassignedValue.Instance, "<#= variableInfo.Path.NamespaceAlias #>").ConfigureAwait(false);<#
}
}#>
}
}
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class RootTemplate
{
internal RootTemplate(
string workflowId,
WorkflowTypeInfo typeInfo)
{
this.Id = workflowId;
this.TypeInfo = typeInfo;
this.TypeName = workflowId.FormatType();
}
public string Id { get; }
public WorkflowTypeInfo TypeInfo { get; }
public string TypeName { get; }
}
@@ -0,0 +1,34 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Formats a message template and sends an activity event.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{ <#
if (this.Model.Activity is MessageActivityTemplate messageActivity)
{ #>
string activityText =
await context.FormatTemplateAsync( <#
foreach (TemplateLine line in messageActivity.Text)
{ #>
"""<#
foreach (string text in line.ToTemplateString().ByLine())
{ #>
<#= text #><#
} #>
"""<#
}
#>
);
AgentRunResponse response = new([new ChatMessage(ChatRole.Assistant, activityText)]);
await context.AddEventAsync(new AgentRunResponseEvent(this.Id, response)).ConfigureAwait(false);<#
} #>
return default;
}
}
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class SendActivityTemplate
{
public SendActivityTemplate(SendActivity model)
{
this.Model = this.Initialize(model);
}
public SendActivity Model { get; }
}
@@ -0,0 +1,27 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Assigns an evaluated expression, other variable, or literal value to one or more variables.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<# int index = 0;
foreach (var assignment in this.Model.Assignments)
{
// Separate assigments with a blank line for readability
if (index > 0)
{#>
<#
}
++index;
EvaluateValueExpression(assignment.Value, $"evaluatedValue{index}");
AssignVariable(assignment.Variable, $"evaluatedValue{index}");
}
#>
return default;
}
}
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class SetMultipleVariablesTemplate
{
public SetMultipleVariablesTemplate(SetMultipleVariables model)
{
this.Model = this.Initialize(model);
}
public SetMultipleVariables Model { get; }
}
@@ -0,0 +1,16 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Assigns an evaluated message template to the "<#= this.Model.Variable #>" variable.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateMessageTemplate(this.Model.Value, "textValue");
AssignVariable(this.Variable, "textValue"); #>
return default;
}
}
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class SetTextVariableTemplate
{
public SetTextVariableTemplate(SetTextVariable model)
{
this.Model = this.Initialize(model);
this.Variable = Throw.IfNull(this.Model.Variable);
}
public SetTextVariable Model { get; }
public PropertyPath Variable { get; }
}
@@ -0,0 +1,17 @@
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ include file="Snippets/Index.tt" once="true" #>
/// <summary>
/// Assigns an evaluated expression, other variable, or literal value to the "<#= this.Model.Variable #>" variable.
/// </summary>
internal sealed class <#= this.Name #>Executor(FormulaSession session) : ActionExecutor(id: "<#= this.Id #>", session)
{
// <inheritdoc />
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
{<#
EvaluateValueExpression(this.Model.Value, "evaluatedValue");
AssignVariable(this.Variable, "evaluatedValue"); #>
return default;
}
}
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
internal partial class SetVariableTemplate
{
internal SetVariableTemplate(SetVariable model)
{
this.Model = this.Initialize(model);
this.Variable = Throw.IfNull(this.Model.Variable);
}
public SetVariable Model { get; }
public PropertyPath Variable { get; }
}
@@ -0,0 +1,12 @@
<#+
void AssignVariable(PropertyPath targetVariable, string valueVariable, bool tightFormat = false)
{
if (targetVariable is not null)
{#>
await context.QueueStateUpdateAsync(key: "<#= VariableName(targetVariable) #>", value: <#= valueVariable #>, scopeName: "<#= VariableScope(targetVariable) #>").ConfigureAwait(false);<#+
if (!tightFormat)
{#>
<#+}
}
}
#>
@@ -0,0 +1,25 @@
<#+
void EvaluateBoolExpression(BoolExpression expression, string targetVariable, bool defaultValue = false)
{
if (expression is null)
{#>
bool <#= targetVariable #> = <#= FormatBoolValue(defaultValue) #>;<#+
}
else if (expression.IsLiteral)
{#>
bool <#= targetVariable #> = <#= FormatBoolValue(expression.LiteralValue) #>;<#+
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
bool <#= targetVariable #> = await context.ReadStateAsync<bool>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
bool <#= targetVariable #> = await context.EvaluateValueAsync<bool>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
bool <#= targetVariable #> = await context.EvaluateValueAsync<bool>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,41 @@
<#+
void EvaluateEnumExpression<TWrapper, TValue>(
EnumExpression<TWrapper> expression,
string targetVariable,
IDictionary<TWrapper, string> resultMap,
string defaultValue = null,
bool qualifyResult = false,
bool isNullable = false)
where TWrapper : EnumWrapper
{
string resultType = $"{GetTypeAlias<TValue>()}{(isNullable ? "?" : "")}";
if (expression is null)
{#>
<#= resultType #> <#= targetVariable #> = <#= FormatValue<TValue>(defaultValue) #>;<#+
}
else if (expression.IsLiteral)
{
resultMap.TryGetValue(expression.LiteralValue, out string resultValue);
if (qualifyResult)
{#>
<#= resultType #> <#= targetVariable #> = <#= GetTypeAlias<TValue>() #>.<#= resultValue #>;<#+
}
else
{#>
<#= resultType #> <#= targetVariable #> = <#= FormatValue<TValue>(resultValue) #>;<#+
}
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
<#= resultType #> <#= targetVariable #> = await context.ReadStateAsync<<#= resultType #>>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
<#= resultType #>? <#= targetVariable #> = await context.EvaluateValueAsync<<#= resultType #>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
<#= resultType #> <#= targetVariable #> = await context.EvaluateValueAsync<<#= resultType #>>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,26 @@
<#+
void EvaluateIntExpression(IntExpression expression, string targetVariable, bool isNullable = false)
{
string typeName = isNullable ? "int?" : "int";
if (expression is null)
{#>
<#= typeName #> <#= targetVariable #> = <#= isNullable ? "null" : "0" #>;<#+
}
else if (expression.IsLiteral)
{#>
<#= typeName #> <#= targetVariable #> = <#= expression.LiteralValue #>;<#+
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
<#= typeName #> <#= targetVariable #> = await context.ReadStateAsync<int>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
<#= typeName #>? <#= targetVariable #> = await context.EvaluateValueAsync<<#= typeName #>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
<#= typeName #> <#= targetVariable #> = await context.EvaluateValueAsync<<#= typeName #>>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,26 @@
<#+
void EvaluateListExpression<TElement>(ValueExpression expression, string targetVariable)
{
string typeName = GetTypeAlias<TElement>();
if (expression is null)
{#>
IList<<#= typeName #>>? <#= targetVariable #> = null;<#+
}
else if (expression.IsLiteral)
{#>
IList<<#= typeName #>>? <#= targetVariable #> = <#= FormatDataValue(expression.LiteralValue) #>;<#+
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
IList<<#= typeName #>>? <#= targetVariable #> = await context.ReadListAsync<<#= GetTypeAlias<TElement>() #>>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
IList<<#= typeName #>>? <#= targetVariable #>> = await context.EvaluateListAsync<<#= typeName #>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
IList<<#= typeName #>>? <#= targetVariable #> = await context.EvaluateListAsync<<#= typeName #>>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,27 @@
<#+
void EvaluateRecordExpression<TValue>(ObjectExpression<RecordDataValue> expression, string targetVariable)
{
string resultTypeName = $"Dictionary<string, {GetTypeAlias<TValue>()}?>?";
if (expression is null)
{#>
<#= resultTypeName #> <#= targetVariable #> = null;<#+
}
else if (expression.IsLiteral)
{#>
<#= resultTypeName #> <#= targetVariable #> =
<#= FormatDataValue(expression.LiteralValue) #>;<#+
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
<#= resultTypeName #> <#= targetVariable #> = await context.ReadStateAsync<<#= resultTypeName #>>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
<#= resultTypeName #>? <#= targetVariable #> = await context.EvaluateExpressionAsync<<#= resultTypeName #>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
<#= resultTypeName #> <#= targetVariable #> = await context.EvaluateExpressionAsync<<#= resultTypeName #>>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,36 @@
<#+
void EvaluateStringExpression(StringExpression expression, string targetVariable, bool isNullable = false)
{
string typeName = isNullable ? "string?" : "string";
if (expression is null)
{#>
<#= typeName #> <#= targetVariable #> = <#= isNullable ? "null" : "string.Empty" #>;<#+
}
else if (expression.IsLiteral)
{
if (expression.LiteralValue.Contains("\n"))
{#>
<#= typeName #> <#= targetVariable #> =
"""
<#= expression.LiteralValue #>
""";<#+
}
else
{#>
<#= typeName #> <#= targetVariable #> = <#= FormatStringValue(expression.LiteralValue) #>;<#+
}
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
<#= typeName #> <#= targetVariable #> = await context.ReadStateAsync<string>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
<#= typeName #> <#= targetVariable #> = await context.EvaluateValueAsync<string>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
<#= typeName #> <#= targetVariable #> = await context.EvaluateValueAsync<string>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,28 @@
<#+
void EvaluateValueExpression(ValueExpression expression, string targetVariable) =>
EvaluateValueExpression<object>(expression, targetVariable);
void EvaluateValueExpression<TValue>(ValueExpression expression, string targetVariable)
{
if (expression is null)
{#>
<#= GetTypeAlias<TValue>() #>? <#= targetVariable #> = null;<#+
}
else if (expression.IsLiteral)
{#>
<#= GetTypeAlias<TValue>() #>? <#= targetVariable #> = <#= FormatDataValue(expression.LiteralValue) #>;<#+
}
else if (expression.IsVariableReference && expression.VariableReference.SegmentCount == 2)
{#>
<#= GetTypeAlias<TValue>() #>? <#= targetVariable #> = await context.ReadStateAsync<<#= GetTypeAlias<TValue>() #>>(key: "<#= expression.VariableReference.VariableName #>", scopeName: "<#= expression.VariableReference.NamespaceAlias #>").ConfigureAwait(false);<#+
}
else if (expression.IsVariableReference)
{#>
<#= GetTypeAlias<TValue>() #>? <#= targetVariable #> = await context.EvaluateValueAsync<<#= GetTypeAlias<TValue>() #>>(<#= FormatStringValue(expression.VariableReference.ToString()) #>).ConfigureAwait(false);<#+
}
else
{#>
<#= GetTypeAlias<TValue>() #>? <#= targetVariable #> = await context.EvaluateValueAsync<<#= GetTypeAlias<TValue>() #>>(<#= FormatStringValue(expression.ExpressionText) #>).ConfigureAwait(false);<#+
}
}
#>
@@ -0,0 +1,25 @@
<#+
void EvaluateMessageTemplate(TemplateLine templateLine, string variableName)
{
if (templateLine is not null)
{#>
string <#= variableName #> =
await context.FormatTemplateAsync(
"""<#+
FormatMessageTemplate(templateLine); #>
""");<#+
}
else
{#>
string? <#= variableName #> = null;<#+
}
}
void FormatMessageTemplate(TemplateLine line)
{
foreach (string text in line.ToTemplateString().ByLine())
{ #>
<#= text #><#+
}
}
#>
@@ -0,0 +1,14 @@
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #>
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.ObjectModel" #>
<#@ import namespace="Microsoft.Bot.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" #>