mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aee043d17e | ||
|
|
5c52926123 | ||
|
|
02e5600ede | ||
|
|
9361cda413 | ||
|
|
ef86fb51d5 | ||
|
|
d5c07f2623 | ||
|
|
ae989b92e7 | ||
|
|
3242d8a4c4 | ||
|
|
e1e6e3d35e | ||
|
|
08697f8037 | ||
|
|
b0f5fa541c | ||
|
|
e3290a2d22 | ||
|
|
200488cb08 | ||
|
|
bd4fc64b4d | ||
|
|
b2e77067e9 | ||
|
|
08541ee5a9 | ||
|
|
dc4bafbc1e | ||
|
|
de6d0267f2 | ||
|
|
0099a6e2fa |
@@ -1,15 +1,19 @@
|
||||
{
|
||||
"name": "Python 3",
|
||||
"image": "mcr.microsoft.com/devcontainers/python:3.13-bullseye",
|
||||
"image": "mcr.microsoft.com/devcontainers/python:3.14-bookworm",
|
||||
"features": {
|
||||
"ghcr.io/va-h/devcontainers-features/uv:1": {},
|
||||
"ghcr.io/devcontainers/features/azure-cli:1.2.8": {}
|
||||
"ghcr.io/devcontainers/features/docker-in-docker:3": {},
|
||||
"ghcr.io/devcontainers/features/azure-cli:1.2.9": {},
|
||||
"ghcr.io/devcontainers/features/copilot-cli:1": {}
|
||||
},
|
||||
"postCreateCommand": "bash ./devsetup.sh",
|
||||
"workspaceFolder": "/workspaces/agent-framework/python/",
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"GitHub.copilot",
|
||||
"GitHub.vscode-github-actions",
|
||||
"ms-python.python",
|
||||
"ms-windows-ai-studio.windows-ai-studio",
|
||||
"littlefoxteam.vscode-python-test-adapter"
|
||||
|
||||
@@ -8,7 +8,7 @@ ignorePatterns:
|
||||
- pattern: "./blob"
|
||||
- pattern: "./issues"
|
||||
- pattern: "./discussions"
|
||||
- pattern: "./pulls"
|
||||
- pattern: "./pull"
|
||||
- pattern: "https:\/\/platform.openai.com"
|
||||
- pattern: "http:\/\/localhost"
|
||||
- pattern: "http:\/\/127.0.0.1"
|
||||
|
||||
@@ -241,6 +241,7 @@
|
||||
<Project Path="samples/03-workflows/Declarative/ExecuteCode/ExecuteCode.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/ExecuteWorkflow/ExecuteWorkflow.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/FunctionTools/FunctionTools.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/GenerateCode/GenerateCode.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/HostedWorkflow/HostedWorkflow.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/InputArguments/InputArguments.csproj" />
|
||||
<Project Path="samples/03-workflows/Declarative/InvokeFunctionTool/InvokeFunctionTool.csproj" />
|
||||
@@ -358,6 +359,9 @@
|
||||
<Folder Name="/Samples/04-hosting/FoundryHostedAgents/responses/Hosted-Workflow-Handoff/">
|
||||
<Project Path="samples/04-hosting/FoundryHostedAgents/responses/Hosted-Workflow-Handoff/HostedWorkflowHandoff.csproj" />
|
||||
</Folder>
|
||||
<Folder Name="/Samples/04-hosting/FoundryHostedAgents/responses/Hosted-AgentSkills/">
|
||||
<Project Path="samples/04-hosting/FoundryHostedAgents/responses/Hosted-AgentSkills/HostedAgentSkills.csproj" />
|
||||
</Folder>
|
||||
<Folder Name="/Samples/04-hosting/DurableAgents/" />
|
||||
<Folder Name="/Samples/04-hosting/DurableAgents/AzureFunctions/">
|
||||
<File Path="samples/04-hosting/DurableAgents/AzureFunctions/.editorconfig" />
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
"src\\Microsoft.Agents.AI.Mem0\\Microsoft.Agents.AI.Mem0.csproj",
|
||||
"src\\Microsoft.Agents.AI.OpenAI\\Microsoft.Agents.AI.OpenAI.csproj",
|
||||
"src\\Microsoft.Agents.AI.Purview\\Microsoft.Agents.AI.Purview.csproj",
|
||||
"src\\Microsoft.Agents.AI.Tools.Shell\\Microsoft.Agents.AI.Tools.Shell.csproj",
|
||||
"src\\Microsoft.Agents.AI.Workflows.Declarative.Foundry\\Microsoft.Agents.AI.Workflows.Declarative.Foundry.csproj",
|
||||
"src\\Microsoft.Agents.AI.Workflows.Declarative\\Microsoft.Agents.AI.Workflows.Declarative.csproj",
|
||||
"src\\Microsoft.Agents.AI.Workflows.Generators\\Microsoft.Agents.AI.Workflows.Generators.csproj",
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<!-- Central version prefix - applies to all nuget packages. -->
|
||||
<VersionPrefix>1.6.2</VersionPrefix>
|
||||
<VersionPrefix>1.7.0</VersionPrefix>
|
||||
<RCNumber>1</RCNumber>
|
||||
<DateSuffix>260521</DateSuffix>
|
||||
<DateSuffix>260526</DateSuffix>
|
||||
<PackageVersion Condition="'$(IsReleaseCandidate)' == 'true'">$(VersionPrefix)-rc$(RCNumber)</PackageVersion>
|
||||
<PackageVersion Condition="'$(IsReleaseCandidate)' != 'true' AND '$(VersionSuffix)' != ''">$(VersionPrefix)-$(VersionSuffix).$(DateSuffix).1</PackageVersion>
|
||||
<PackageVersion Condition="'$(IsReleaseCandidate)' != 'true' AND '$(VersionSuffix)' == ''">$(VersionPrefix)-preview.$(DateSuffix).1</PackageVersion>
|
||||
<PackageVersion Condition="'$(IsReleased)' == 'true'">$(VersionPrefix)</PackageVersion>
|
||||
<GitTag>1.6.2</GitTag>
|
||||
<GitTag>1.7.0</GitTag>
|
||||
|
||||
<Configurations>Debug;Release;Publish</Configurations>
|
||||
<IsPackable>true</IsPackable>
|
||||
|
||||
@@ -24,6 +24,7 @@ string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYM
|
||||
var skillsProvider = new AgentSkillsProvider(
|
||||
Path.Combine(AppContext.BaseDirectory, "skills"),
|
||||
SubprocessScriptRunner.RunAsync);
|
||||
|
||||
// --- Agent Setup ---
|
||||
AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
|
||||
.GetResponsesClient()
|
||||
|
||||
@@ -51,7 +51,7 @@ Console.WriteLine($"Agent: {response.Text}");
|
||||
/// Properties annotated with <see cref="AgentSkillResourceAttribute"/> are automatically
|
||||
/// discovered as skill resources, and methods annotated with <see cref="AgentSkillScriptAttribute"/>
|
||||
/// are automatically discovered as skill scripts. Alternatively,
|
||||
/// <see cref="AgentSkill.Resources"/> and <see cref="AgentSkill.Scripts"/> can be overridden.
|
||||
/// <see cref="AgentClassSkill{TSelf}.Resources"/> and <see cref="AgentClassSkill{TSelf}.Scripts"/> can be overridden.
|
||||
/// </remarks>
|
||||
internal sealed class UnitConverterSkill : AgentClassSkill<UnitConverterSkill>
|
||||
{
|
||||
|
||||
@@ -40,8 +40,8 @@ public class ListSelection : ConsoleReactiveComponent<ListSelectionProps, Consol
|
||||
foreach (string line in props.Title.Split('\n'))
|
||||
{
|
||||
Console.Write(AnsiEscapes.MoveCursor(props.Y + row, props.X));
|
||||
Console.Write(AnsiEscapes.EraseEntireLine);
|
||||
Console.Write(line);
|
||||
Console.Write(AnsiEscapes.EraseToEndOfLine);
|
||||
row++;
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,6 @@ public class ListSelection : ConsoleReactiveComponent<ListSelectionProps, Consol
|
||||
for (int i = 0; i < totalItems; i++)
|
||||
{
|
||||
Console.Write(AnsiEscapes.MoveCursor(props.Y + row, props.X));
|
||||
Console.Write(AnsiEscapes.EraseEntireLine);
|
||||
|
||||
bool isSelected = i == props.SelectedIndex;
|
||||
bool isCustomTextOption = props.CustomTextPlaceholder != null && i == props.Items.Count;
|
||||
@@ -72,6 +71,7 @@ public class ListSelection : ConsoleReactiveComponent<ListSelectionProps, Consol
|
||||
}
|
||||
|
||||
Console.Write(props.Items[i]);
|
||||
Console.Write(AnsiEscapes.EraseToEndOfLine);
|
||||
|
||||
if (isSelected)
|
||||
{
|
||||
@@ -101,6 +101,7 @@ public class ListSelection : ConsoleReactiveComponent<ListSelectionProps, Consol
|
||||
}
|
||||
|
||||
Console.Write(props.CustomText);
|
||||
Console.Write(AnsiEscapes.EraseToEndOfLine);
|
||||
|
||||
if (isSelected)
|
||||
{
|
||||
@@ -121,6 +122,7 @@ public class ListSelection : ConsoleReactiveComponent<ListSelectionProps, Consol
|
||||
|
||||
Console.Write(" ");
|
||||
Console.Write(props.CustomTextPlaceholder);
|
||||
Console.Write(AnsiEscapes.EraseToEndOfLine);
|
||||
Console.Write(AnsiEscapes.ResetAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,19 @@ public record TextScrollPanelProps : ConsoleReactiveProps
|
||||
/// <summary>
|
||||
/// State for <see cref="TextScrollPanel"/>.
|
||||
/// </summary>
|
||||
/// <param name="RenderedCount">The number of items already rendered.</param>
|
||||
public record TextScrollPanelState(int RenderedCount = 0) : ConsoleReactiveState;
|
||||
public record TextScrollPanelState : ConsoleReactiveState;
|
||||
|
||||
/// <summary>
|
||||
/// A component that renders pre-rendered string items within a scroll area.
|
||||
/// All items are considered finalized — only new items since the last render are output.
|
||||
/// Use <see cref="Reset"/> to force a full re-render.
|
||||
/// The last rendered item is considered dynamic and will be re-rendered on each call.
|
||||
/// All prior items are considered finalized and are not re-rendered.
|
||||
/// Use <see cref="Invalidate"/> to force a full re-render.
|
||||
/// </summary>
|
||||
public class TextScrollPanel : ConsoleReactiveComponent<TextScrollPanelProps, TextScrollPanelState>
|
||||
{
|
||||
private int _renderedCount;
|
||||
private int _lastItemOffsetFromBottom;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TextScrollPanel"/> class.
|
||||
/// </summary>
|
||||
@@ -35,12 +38,12 @@ public class TextScrollPanel : ConsoleReactiveComponent<TextScrollPanelProps, Te
|
||||
this.State = new TextScrollPanelState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the panel so all items will be re-rendered on the next Render call.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
/// <inheritdoc />
|
||||
public override void Invalidate()
|
||||
{
|
||||
this.State = new TextScrollPanelState();
|
||||
this._renderedCount = 0;
|
||||
this._lastItemOffsetFromBottom = 0;
|
||||
base.Invalidate();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -51,16 +54,59 @@ public class TextScrollPanel : ConsoleReactiveComponent<TextScrollPanelProps, Te
|
||||
return;
|
||||
}
|
||||
|
||||
// Move cursor to the bottom of the scroll area
|
||||
Console.Write(AnsiEscapes.MoveCursor(props.Y + props.Height - 1, props.X));
|
||||
int bottomRow = props.Y + props.Height - 1;
|
||||
|
||||
// Output only new items since last rendered
|
||||
for (int i = state.RenderedCount; i < props.Items.Count; i++)
|
||||
// Determine the first item to render. If we previously rendered items,
|
||||
// re-render the last one (it may have changed/grown) from its stored position.
|
||||
int startIndex = this._renderedCount > 0 ? this._renderedCount - 1 : 0;
|
||||
|
||||
if (this._renderedCount > 0 && this._lastItemOffsetFromBottom > 0)
|
||||
{
|
||||
// Reposition cursor to where the last rendered item began
|
||||
Console.Write(AnsiEscapes.MoveCursor(bottomRow - this._lastItemOffsetFromBottom, props.X));
|
||||
}
|
||||
else
|
||||
{
|
||||
// First render — position at the bottom of the scroll area
|
||||
Console.Write(AnsiEscapes.MoveCursor(bottomRow, props.X));
|
||||
}
|
||||
|
||||
// Render from startIndex onwards
|
||||
for (int i = startIndex; i < props.Items.Count; i++)
|
||||
{
|
||||
Console.Write(props.Items[i]);
|
||||
}
|
||||
|
||||
// Update state to track what we've rendered
|
||||
this.State = new TextScrollPanelState(props.Items.Count);
|
||||
// Calculate the offset from bottom for the start of the new last item
|
||||
int lastItemLines = CountLines(props.Items[^1]);
|
||||
this._lastItemOffsetFromBottom = lastItemLines > 0 ? lastItemLines - 1 : 0;
|
||||
|
||||
// Update rendered count
|
||||
this._renderedCount = props.Items.Count;
|
||||
}
|
||||
|
||||
private static int CountLines(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
if (text[i] == '\n')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// If text ends with a newline, don't count the trailing empty line
|
||||
if (text[text.Length - 1] == '\n')
|
||||
{
|
||||
count--;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
namespace Harness.ConsoleReactiveFramework;
|
||||
|
||||
/// <summary>
|
||||
/// Caches the result of a mapping function and only recomputes when the input changes.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">The type of the input value.</typeparam>
|
||||
/// <typeparam name="TOutput">The type of the mapped output value.</typeparam>
|
||||
public class ConsoleReactiveMemo<TInput, TOutput>
|
||||
{
|
||||
private TInput? _previousInput;
|
||||
private TOutput? _cachedOutput;
|
||||
private bool _hasValue;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached output if <paramref name="input"/> equals the previously stored input;
|
||||
/// otherwise invokes <paramref name="mapper"/> to compute and cache a new output.
|
||||
/// </summary>
|
||||
/// <param name="input">The current input value.</param>
|
||||
/// <param name="mapper">A function that maps the input to an output value.</param>
|
||||
/// <returns>The cached or newly computed output.</returns>
|
||||
public TOutput Map(TInput input, Func<TInput, TOutput> mapper)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(mapper);
|
||||
|
||||
if (!this._hasValue || !EqualityComparer<TInput>.Default.Equals(input, this._previousInput))
|
||||
{
|
||||
this._previousInput = input;
|
||||
this._cachedOutput = mapper(input);
|
||||
this._hasValue = true;
|
||||
}
|
||||
|
||||
return this._cachedOutput!;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ public class HarnessAppComponent : ConsoleReactiveComponent<ConsoleReactiveProps
|
||||
private readonly ListSelection _listSelection = new();
|
||||
private readonly TextInput _textInput = new();
|
||||
private readonly TextScrollPanel _textScrollPanel = new();
|
||||
private readonly TextPanel _textPanel = new();
|
||||
private readonly TextPanel _queuedPanel = new();
|
||||
private readonly AgentStatus _agentStatus = new();
|
||||
private readonly AgentModeAndHelp _modeAndHelp = new();
|
||||
@@ -341,16 +340,6 @@ public class HarnessAppComponent : ConsoleReactiveComponent<ConsoleReactiveProps
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine the text panel height for the last scroll item
|
||||
IReadOnlyList<string> lastItems = state.ScrollAreaContentItems.Count > 0
|
||||
? [state.ScrollAreaContentItems[^1]]
|
||||
: [];
|
||||
int textPanelHeight = TextPanel.CalculateHeight(lastItems);
|
||||
if (textPanelHeight > 0)
|
||||
{
|
||||
textPanelHeight++; // Extra line for spacing between text panel and rule
|
||||
}
|
||||
|
||||
// Calculate queued items panel height
|
||||
int queuedPanelHeight = TextPanel.CalculateHeight(state.QueuedItems);
|
||||
|
||||
@@ -444,7 +433,7 @@ public class HarnessAppComponent : ConsoleReactiveComponent<ConsoleReactiveProps
|
||||
int modeAndHelpHeight = showStatusAndHelp ? AgentModeAndHelp.CalculateHeight(modeAndHelpProps) : 0;
|
||||
|
||||
int ruleHeight = TopBottomRule.CalculateHeight(ruleProps);
|
||||
int nonScrollHeight = ruleHeight + textPanelHeight + agentStatusHeight + queuedPanelHeight + modeAndHelpHeight + 1; // +1 for bottom padding
|
||||
int nonScrollHeight = ruleHeight + agentStatusHeight + queuedPanelHeight + modeAndHelpHeight + 1; // +1 for bottom padding
|
||||
int scrollBottom = Math.Max(1, state.ConsoleHeight - nonScrollHeight);
|
||||
|
||||
// If scroll region changed or a clear is needed, reset everything
|
||||
@@ -455,52 +444,36 @@ public class HarnessAppComponent : ConsoleReactiveComponent<ConsoleReactiveProps
|
||||
System.Console.Write(AnsiEscapes.ResetScrollRegion);
|
||||
System.Console.Write(AnsiEscapes.EraseEntireScreen);
|
||||
System.Console.Write(AnsiEscapes.EraseScrollbackBuffer);
|
||||
this._textScrollPanel.Reset();
|
||||
this._resizedSinceLastRender = false;
|
||||
|
||||
// Invalidate all children so they re-render even if props haven't changed
|
||||
this._rule.Invalidate();
|
||||
this._textScrollPanel.Invalidate();
|
||||
this._textPanel.Invalidate();
|
||||
this._queuedPanel.Invalidate();
|
||||
this._agentStatus.Invalidate();
|
||||
this._modeAndHelp.Invalidate();
|
||||
this._textInput.Invalidate();
|
||||
this._listSelection.Invalidate();
|
||||
|
||||
this._resizedSinceLastRender = false;
|
||||
}
|
||||
|
||||
this._scrollRegionBottom = scrollBottom;
|
||||
|
||||
System.Console.Write(AnsiEscapes.SetScrollRegion(scrollBottom));
|
||||
|
||||
// Render text scroll panel in the scroll area (all items except the last)
|
||||
IReadOnlyList<string> scrollItems = state.ScrollAreaContentItems.Count > 1
|
||||
? state.ScrollAreaContentItems.Take(state.ScrollAreaContentItems.Count - 1).ToList()
|
||||
: [];
|
||||
|
||||
// Render text scroll panel in the scroll area
|
||||
this._textScrollPanel.Props = new TextScrollPanelProps
|
||||
{
|
||||
X = 1,
|
||||
Y = 1,
|
||||
Width = state.ConsoleWidth,
|
||||
Height = scrollBottom,
|
||||
Items = scrollItems,
|
||||
Items = state.ScrollAreaContentItems,
|
||||
};
|
||||
this._textScrollPanel.Render();
|
||||
|
||||
// Render the text panel for the last (dynamic) item just below the scroll region
|
||||
this._textPanel.Props = new TextPanelProps
|
||||
{
|
||||
X = 1,
|
||||
Y = scrollBottom + 1,
|
||||
Width = state.ConsoleWidth,
|
||||
Height = textPanelHeight,
|
||||
Items = lastItems,
|
||||
};
|
||||
this._textPanel.Render();
|
||||
|
||||
// Render queued input items between text panel and agent status
|
||||
int queuedPanelY = scrollBottom + textPanelHeight + 1;
|
||||
// Render queued input items between scroll area and agent status
|
||||
int queuedPanelY = scrollBottom + 1;
|
||||
this._queuedPanel.Props = new TextPanelProps
|
||||
{
|
||||
X = 1,
|
||||
|
||||
+3
-3
@@ -5,17 +5,17 @@ using Microsoft.Extensions.AI;
|
||||
namespace Harness.Shared.Console.ToolFormatters;
|
||||
|
||||
/// <summary>
|
||||
/// Formats <c>AgentMode_*</c> tool calls, showing the target mode for Set operations.
|
||||
/// Formats <c>mode_*</c> tool calls, showing the target mode for Set operations.
|
||||
/// </summary>
|
||||
public sealed class ModeToolFormatter : ToolCallFormatter
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool CanFormat(FunctionCallContent call) => call.Name.StartsWith("AgentMode_", StringComparison.Ordinal);
|
||||
public override bool CanFormat(FunctionCallContent call) => call.Name.StartsWith("mode_", StringComparison.Ordinal);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string? FormatDetail(FunctionCallContent call) => call.Name switch
|
||||
{
|
||||
"AgentMode_Set" => FormatStringArg(call, "mode"),
|
||||
"mode_set" => FormatStringArg(call, "mode"),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
<NoWarn>$(NoWarn);CA1812</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<InjectSharedThrow>true</InjectSharedThrow>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Workflows.Declarative\Microsoft.Agents.AI.Workflows.Declarative.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,105 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Agents.AI.Workflows.Declarative;
|
||||
|
||||
namespace Demo.DeclarativeEject;
|
||||
|
||||
/// <summary>
|
||||
/// HOW TO: Convert a workflow from a declartive (yaml based) definition to code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <b>Usage</b>
|
||||
/// Provide the path to the workflow definition file as the first argument.
|
||||
/// All other arguments are intepreted as a queue of inputs.
|
||||
/// When no input is queued, interactive input is requested from the console.
|
||||
/// </remarks>
|
||||
internal sealed class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Program program = new(args);
|
||||
program.Execute();
|
||||
}
|
||||
|
||||
private void Execute()
|
||||
{
|
||||
// Read and parse the declarative workflow.
|
||||
Notify($"WORKFLOW: Parsing {Path.GetFullPath(this.WorkflowFile)}");
|
||||
|
||||
Stopwatch timer = Stopwatch.StartNew();
|
||||
|
||||
// Use DeclarativeWorkflowBuilder to generate code based on a YAML file.
|
||||
string code =
|
||||
DeclarativeWorkflowBuilder.Eject(
|
||||
this.WorkflowFile,
|
||||
DeclarativeWorkflowLanguage.CSharp,
|
||||
workflowNamespace: "Demo.DeclarativeCode",
|
||||
workflowPrefix: "Sample");
|
||||
|
||||
Notify($"\nWORKFLOW: Defined {timer.Elapsed}\n");
|
||||
|
||||
Console.WriteLine(code);
|
||||
}
|
||||
|
||||
private const string DefaultWorkflow = "Marketing.yaml";
|
||||
|
||||
private string WorkflowFile { get; }
|
||||
|
||||
private Program(string[] args)
|
||||
{
|
||||
this.WorkflowFile = ParseWorkflowFile(args);
|
||||
}
|
||||
|
||||
private static string ParseWorkflowFile(string[] args)
|
||||
{
|
||||
string workflowFile = args.FirstOrDefault() ?? DefaultWorkflow;
|
||||
|
||||
if (!File.Exists(workflowFile) && !Path.IsPathFullyQualified(workflowFile))
|
||||
{
|
||||
string? repoFolder = GetRepoFolder();
|
||||
if (repoFolder is not null)
|
||||
{
|
||||
workflowFile = Path.Combine(repoFolder, "declarative-agents", "workflow-samples", workflowFile);
|
||||
workflowFile = Path.ChangeExtension(workflowFile, ".yaml");
|
||||
}
|
||||
}
|
||||
|
||||
if (!File.Exists(workflowFile))
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to locate workflow: {Path.GetFullPath(workflowFile)}.");
|
||||
}
|
||||
|
||||
return workflowFile;
|
||||
|
||||
static string? GetRepoFolder()
|
||||
{
|
||||
DirectoryInfo? current = new(Directory.GetCurrentDirectory());
|
||||
|
||||
while (current is not null)
|
||||
{
|
||||
if (Directory.Exists(Path.Combine(current.FullName, ".git")))
|
||||
{
|
||||
return current.FullName;
|
||||
}
|
||||
|
||||
current = current.Parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Notify(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
try
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Marketing": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"Marketing.yaml\""
|
||||
},
|
||||
"MathChat": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"MathChat.yaml\""
|
||||
},
|
||||
"Question": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"Question.yaml\""
|
||||
},
|
||||
"Research": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"DeepResearch.yaml\""
|
||||
},
|
||||
"ResponseObject": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"ResponseObject.yaml\""
|
||||
},
|
||||
"UserInput": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "\"UserInput.yaml\""
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
AZURE_AI_PROJECT_ENDPOINT=<your-azure-ai-project-endpoint>
|
||||
ASPNETCORE_URLS=http://+:8088
|
||||
ASPNETCORE_ENVIRONMENT=Development
|
||||
AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4o
|
||||
AGENT_NAME=hosted-agent-skills
|
||||
SKILL_NAMES=support-style,escalation-policy
|
||||
# Set to true to provision sample skills to Foundry on startup (first-run convenience).
|
||||
# In production, skills are provisioned externally — leave this unset or false.
|
||||
PROVISION_SAMPLE_SKILLS=true
|
||||
AZURE_BEARER_TOKEN=DefaultAzureCredential
|
||||
# When running outside the Foundry platform the platform-injected isolation keys are absent.
|
||||
# These two variables provide fallback values for local Docker debugging only.
|
||||
HOSTED_USER_ISOLATION_KEY=local-dev-user
|
||||
HOSTED_CHAT_ISOLATION_KEY=local-dev-chat
|
||||
@@ -0,0 +1,26 @@
|
||||
# Dockerfile for end-users consuming the Agent Framework via NuGet packages.
|
||||
#
|
||||
# This Dockerfile performs a full `dotnet restore` and `dotnet publish` inside the container,
|
||||
# which only succeeds when the project references its dependencies via PackageReference (see the
|
||||
# commented-out section in HostedAgentSkills.csproj). Contributors building from the
|
||||
# agent-framework repository source must use Dockerfile.contributor instead because
|
||||
# ProjectReference dependencies live outside this folder and cannot be restored from inside
|
||||
# this build context.
|
||||
#
|
||||
# Use the official .NET 10.0 ASP.NET runtime as a parent image
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
RUN dotnet restore
|
||||
RUN dotnet publish -c Release -o /app/publish
|
||||
|
||||
# Final stage
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=build /app/publish .
|
||||
EXPOSE 8088
|
||||
ENV ASPNETCORE_URLS=http://+:8088
|
||||
ENTRYPOINT ["dotnet", "HostedAgentSkills.dll"]
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
# Dockerfile for contributors building from the agent-framework repository source.
|
||||
#
|
||||
# This project uses ProjectReference to the local Microsoft.Agents.AI source,
|
||||
# which means a standard multi-stage Docker build cannot resolve dependencies outside
|
||||
# this folder. Instead, pre-publish the app targeting the container runtime and copy
|
||||
# the output into the container:
|
||||
#
|
||||
# dotnet publish -c Debug -f net10.0 -r linux-musl-x64 --self-contained false -o out
|
||||
# docker build -f Dockerfile.contributor -t hosted-agent-skills .
|
||||
# docker run --rm -p 8088:8088 \
|
||||
# -e AGENT_NAME=hosted-agent-skills \
|
||||
# -e HOSTED_USER_ISOLATION_KEY=alice \
|
||||
# -e HOSTED_CHAT_ISOLATION_KEY=alice-chat-1 \
|
||||
# --env-file .env hosted-agent-skills
|
||||
#
|
||||
# For end-users consuming the NuGet package (not ProjectReference), use the standard
|
||||
# Dockerfile which performs a full dotnet restore + publish inside the container.
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS final
|
||||
WORKDIR /app
|
||||
COPY out/ .
|
||||
EXPOSE 8088
|
||||
ENV ASPNETCORE_URLS=http://+:8088
|
||||
ENTRYPOINT ["dotnet", "HostedAgentSkills.dll"]
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
|
||||
<RootNamespace>HostedAgentSkills</RootNamespace>
|
||||
<AssemblyName>HostedAgentSkills</AssemblyName>
|
||||
<NoWarn>$(NoWarn);MEAI001;OPENAI001;AAIP001</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.Projects" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="DotNetEnv" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- For contributors: uses ProjectReference to build against local source -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Foundry\Microsoft.Agents.AI.Foundry.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Foundry.Hosting\Microsoft.Agents.AI.Foundry.Hosting.csproj" />
|
||||
<ProjectReference Include="..\Hosted_Shared_Contributor_Setup\Hosted_Shared_Contributor_Setup.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReferences above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI" Version="1.6.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
<!-- Include the skills/ directory in the publish output so the sample can provision them -->
|
||||
<ItemGroup>
|
||||
<None Include="skills\**\*" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// Hosted-AgentSkills
|
||||
//
|
||||
// Demonstrates how to host an agent that loads its behavioral guidelines from Foundry Skills at
|
||||
// startup. Skills are authored as SKILL.md files, uploaded to Foundry via the Skills REST API,
|
||||
// and downloaded by the agent on boot so guideline updates ship without code changes.
|
||||
//
|
||||
// The agent uses AgentSkillsProvider from the Agent Framework which implements the progressive
|
||||
// disclosure pattern from the Agent Skills specification (https://agentskills.io/):
|
||||
// 1. Advertise — skill names and descriptions are injected into the system prompt.
|
||||
// 2. Load — the model calls load_skill to retrieve the full SKILL.md body on demand.
|
||||
//
|
||||
// IMPORTANT: In production, skill provisioning (uploading SKILL.md files to Foundry) is an
|
||||
// external concern — it is NOT the hosted agent's responsibility. The provisioning helper below
|
||||
// is included for sample convenience only, so the sample is self-contained and runnable without
|
||||
// a separate setup step. A real deployment pipeline would provision skills separately (e.g., via
|
||||
// a CI/CD step, a CLI script, or a management portal).
|
||||
|
||||
#pragma warning disable AAIP001 // ProjectAgentSkills is experimental
|
||||
|
||||
using System.ClientModel;
|
||||
using System.IO.Compression;
|
||||
using Azure.AI.Projects;
|
||||
using Azure.AI.Projects.Agents;
|
||||
using Azure.Core;
|
||||
using Azure.Identity;
|
||||
using DotNetEnv;
|
||||
using Hosted_Shared_Contributor_Setup;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Agents.AI.Foundry.Hosting;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
// Load .env file if present (for local development)
|
||||
Env.TraversePath().Load();
|
||||
|
||||
string endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT")
|
||||
?? throw new InvalidOperationException("AZURE_AI_PROJECT_ENDPOINT is not set.");
|
||||
string deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME") ?? "gpt-4o";
|
||||
string skillNames = Environment.GetEnvironmentVariable("SKILL_NAMES")
|
||||
?? throw new InvalidOperationException("SKILL_NAMES is not set. Provide a comma-separated list of skill names (e.g., support-style,escalation-policy).");
|
||||
|
||||
string[] requestedSkills = skillNames.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
if (requestedSkills.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException("SKILL_NAMES must list at least one skill name.");
|
||||
}
|
||||
|
||||
// Validate skill names to prevent path traversal.
|
||||
foreach (string name in requestedSkills)
|
||||
{
|
||||
if (name.Contains('.') || name.Contains('/') || name.Contains('\\') || Path.IsPathRooted(name))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Invalid skill name '{name}': skill names must not contain path separators or dots.");
|
||||
}
|
||||
}
|
||||
|
||||
// Use a chained credential: try a temporary dev token first (for local Docker debugging),
|
||||
// then fall back to DefaultAzureCredential (for local dev via dotnet run / managed identity in production).
|
||||
TokenCredential credential = new ChainedTokenCredential(
|
||||
new DevTemporaryTokenCredential(),
|
||||
new DefaultAzureCredential());
|
||||
|
||||
AIProjectClient projectClient = new(new Uri(endpoint), credential);
|
||||
ProjectAgentSkills skillsClient = projectClient.AgentAdministrationClient.GetAgentSkills();
|
||||
|
||||
// ── Provision skills (sample convenience only — NOT a production pattern) ─────
|
||||
// In production, skills are provisioned externally (e.g., via CI/CD or a management script).
|
||||
// This helper ensures the sample's SKILL.md files exist in Foundry so the sample is runnable
|
||||
// out of the box without a separate setup step. Set PROVISION_SAMPLE_SKILLS=true to enable.
|
||||
string sourceSkillsDir = Path.Combine(AppContext.BaseDirectory, "skills");
|
||||
bool provisionEnabled = string.Equals(
|
||||
Environment.GetEnvironmentVariable("PROVISION_SAMPLE_SKILLS"), "true", StringComparison.OrdinalIgnoreCase);
|
||||
if (provisionEnabled && Directory.Exists(sourceSkillsDir))
|
||||
{
|
||||
await EnsureSkillsProvisionedAsync(skillsClient, sourceSkillsDir, requestedSkills);
|
||||
}
|
||||
|
||||
// ── Download skills from Foundry ─────────────────────────────────────────────
|
||||
// Pull the latest copy of each skill from Foundry into a runtime-only folder.
|
||||
// This directory is recreated on every startup so the agent always picks up
|
||||
// the latest version of each skill.
|
||||
string downloadedSkillsDir = Path.Combine(AppContext.BaseDirectory, "downloaded_skills");
|
||||
await DownloadSkillsAsync(skillsClient, requestedSkills, downloadedSkillsDir);
|
||||
|
||||
// ── Wire skills into the agent ───────────────────────────────────────────────
|
||||
// AgentSkillsProvider implements progressive disclosure: skill names and descriptions
|
||||
// are advertised in the system prompt (~100 tokens per skill), and the full SKILL.md
|
||||
// body is loaded on demand when the model calls the load_skill tool.
|
||||
AgentSkillsProvider skillsProvider = new(downloadedSkillsDir);
|
||||
|
||||
ChatClientAgent agent = projectClient.AsAIAgent(new ChatClientAgentOptions
|
||||
{
|
||||
Name = Environment.GetEnvironmentVariable("AGENT_NAME") ?? "hosted-agent-skills",
|
||||
ChatOptions = new ChatOptions
|
||||
{
|
||||
ModelId = deploymentName,
|
||||
Instructions = "You are a customer-support assistant for Contoso Outdoors.",
|
||||
},
|
||||
AIContextProviders = [skillsProvider]
|
||||
});
|
||||
|
||||
// Host the agent as a Foundry Hosted Agent using the Responses API.
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddFoundryResponses(agent);
|
||||
builder.Services.AddDevTemporaryLocalContributorSetup(); // Local Docker debugging only - must not be used in production.
|
||||
|
||||
var app = builder.Build();
|
||||
app.MapFoundryResponses();
|
||||
|
||||
// Contributor-only: in Development, also map the per-agent OpenAI route shape that live Foundry uses
|
||||
// so a local REPL client can target this server via AIProjectClient.AsAIAgent(Uri agentEndpoint).
|
||||
// Do not use this in production. Hosted Foundry agents only support the agent-endpoint path.
|
||||
app.MapDevTemporaryLocalAgentEndpoint();
|
||||
|
||||
app.Run();
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// Downloads each named skill from Foundry and extracts the ZIP archive into a
|
||||
// separate subdirectory under the target directory.
|
||||
static async Task DownloadSkillsAsync(ProjectAgentSkills skillsClient, string[] skillNames, string targetDir)
|
||||
{
|
||||
if (Directory.Exists(targetDir))
|
||||
{
|
||||
Directory.Delete(targetDir, recursive: true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(targetDir);
|
||||
|
||||
foreach (string name in skillNames)
|
||||
{
|
||||
Console.WriteLine($"Downloading skill '{name}' from Foundry...");
|
||||
BinaryData zipData = await skillsClient.DownloadSkillAsync(name);
|
||||
|
||||
string skillDir = Path.Combine(targetDir, name);
|
||||
Directory.CreateDirectory(skillDir);
|
||||
|
||||
using var zipStream = zipData.ToStream();
|
||||
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
|
||||
SafeExtractZip(archive, skillDir);
|
||||
|
||||
if (!File.Exists(Path.Combine(skillDir, "SKILL.md")))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Downloaded archive for '{name}' did not contain a SKILL.md at the root.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extracts a ZIP archive into a destination directory, rejecting entries that would
|
||||
// escape the target path (zip-slip guard).
|
||||
static void SafeExtractZip(ZipArchive archive, string destinationDir)
|
||||
{
|
||||
string destRoot = Path.GetFullPath(destinationDir);
|
||||
string destRootWithSep = Path.EndsInDirectorySeparator(destRoot)
|
||||
? destRoot
|
||||
: destRoot + Path.DirectorySeparatorChar;
|
||||
|
||||
// Use ordinal comparison on Unix (case-sensitive FS) and ordinal-ignore-case on Windows.
|
||||
var comparison = OperatingSystem.IsWindows()
|
||||
? StringComparison.OrdinalIgnoreCase
|
||||
: StringComparison.Ordinal;
|
||||
|
||||
foreach (ZipArchiveEntry entry in archive.Entries)
|
||||
{
|
||||
string entryPath = Path.GetFullPath(Path.Combine(destRoot, entry.FullName));
|
||||
if (!entryPath.StartsWith(destRootWithSep, comparison)
|
||||
&& !string.Equals(entryPath, destRoot, comparison))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Refusing to extract unsafe path '{entry.FullName}' outside of '{destRoot}'.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(entry.Name))
|
||||
{
|
||||
// Directory entry — ensure it exists.
|
||||
Directory.CreateDirectory(entryPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(entryPath)!);
|
||||
entry.ExtractToFile(entryPath, overwrite: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensures each requested skill is provisioned in Foundry. For each skill name, checks whether
|
||||
// the skill exists and uploads it from the local source directory if it does not.
|
||||
//
|
||||
// This is a sample convenience helper — in production, skill provisioning is an external concern.
|
||||
static async Task EnsureSkillsProvisionedAsync(ProjectAgentSkills skillsClient, string sourceDir, string[] skillNames)
|
||||
{
|
||||
foreach (string name in skillNames)
|
||||
{
|
||||
string skillPath = Path.Combine(sourceDir, name);
|
||||
if (!Directory.Exists(skillPath) || !File.Exists(Path.Combine(skillPath, "SKILL.md")))
|
||||
{
|
||||
continue; // No local source for this skill — skip provisioning.
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await skillsClient.GetSkillAsync(name);
|
||||
Console.WriteLine($"Skill '{name}' already exists in Foundry.");
|
||||
}
|
||||
catch (ClientResultException ex) when (ex.Status == 404)
|
||||
{
|
||||
Console.WriteLine($"Provisioning skill '{name}' from {skillPath}...");
|
||||
AgentsSkill imported = await skillsClient.CreateSkillFromPackageAsync(skillPath);
|
||||
Console.WriteLine($" Imported skill '{imported.Name}' (id={imported.SkillId}, has_blob={imported.HasBlob}).");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
# What this sample demonstrates
|
||||
|
||||
An [Agent Framework](https://github.com/microsoft/agent-framework) agent that loads its behavioral guidelines from [**Foundry Skills**](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/tools/skills) at startup, hosted using the **Responses protocol**. Skills are authored once as `SKILL.md` files, uploaded to your Foundry project through the Skills REST API, and downloaded by the agent on boot so updates ship without code changes.
|
||||
|
||||
## How It Works
|
||||
|
||||
### Authoring skills
|
||||
|
||||
Each skill is a Markdown file with a YAML front matter block. This sample ships two source skills under [`skills/`](skills/):
|
||||
|
||||
| Skill | Purpose |
|
||||
|---|---|
|
||||
| [`support-style`](skills/support-style/SKILL.md) | Voice, formatting, and signature rules for Contoso Outdoors support replies. |
|
||||
| [`escalation-policy`](skills/escalation-policy/SKILL.md) | When and how to escalate a customer ticket. |
|
||||
|
||||
Each `SKILL.md` includes a unique `*-CANARY-*` token that the model is asked to echo, so you can prove the skill was loaded from Foundry (not hallucinated) by checking the response.
|
||||
|
||||
> The `name` and `description` values in the YAML front matter must be **unquoted** — quoting them causes the Skills REST API to return HTTP 500 on import.
|
||||
|
||||
### Uploading skills
|
||||
|
||||
The sample includes a convenience provisioning step that checks whether each skill exists in Foundry and uploads it if not, gated behind the `PROVISION_SAMPLE_SKILLS=true` env var. **In production, skill provisioning is an external concern** — it is NOT the hosted agent's responsibility. A real deployment pipeline would provision skills separately (e.g., via a CI/CD step, a CLI script, or a management portal).
|
||||
|
||||
The provisioning uses `ProjectAgentSkills.CreateSkillFromPackageAsync(directoryPath)` from the `Azure.AI.Projects.Agents` SDK. The method packages the `SKILL.md` file as a ZIP and uploads it to Foundry.
|
||||
|
||||
### Downloading skills at agent startup
|
||||
|
||||
[`Program.cs`](Program.cs) reads the comma-separated `SKILL_NAMES` env var and for each skill name downloads the ZIP archive from Foundry via `ProjectAgentSkills.DownloadSkillAsync(name)`, then unpacks it into a **separate runtime directory** at `downloaded_skills/<name>/` (kept distinct from the static `skills/` source folder).
|
||||
|
||||
An [`AgentSkillsProvider`](../../../../../src/Microsoft.Agents.AI/Skills/AgentSkillsProvider.cs) is then built over `downloaded_skills/` and attached to the agent as a context provider. The provider follows the [Agent Skills](https://agentskills.io/) progressive-disclosure pattern:
|
||||
|
||||
1. **Advertise** — skill names and descriptions are injected into the system prompt at session start (~100 tokens per skill).
|
||||
2. **Load** — the model calls the `load_skill` tool when it decides a skill is relevant to the user's turn, and the full `SKILL.md` body is returned.
|
||||
|
||||
This means the model only pays the token cost for a skill's full body when it actually needs it, and updating a skill in Foundry + restarting the agent is enough to pick up the change — no code redeploy required.
|
||||
|
||||
> **Note:** This sample supports instruction-only and resource-based skills. If your downloaded skills contain scripts, add a script runner when constructing the `AgentSkillsProvider`.
|
||||
|
||||
### Agent Hosting
|
||||
|
||||
The agent is hosted using the [Agent Framework](https://github.com/microsoft/agent-framework) with the Responses API hosting layer (`AddFoundryResponses` / `MapFoundryResponses`).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- An Azure AI Foundry project with a deployed model (e.g., `gpt-4o`)
|
||||
- Azure CLI logged in (`az login`)
|
||||
|
||||
### Required RBAC
|
||||
|
||||
Your identity (or the Managed Identity running the container in production) needs **Azure AI User** on the Foundry project scope. This single role covers both authoring skills and downloading them.
|
||||
|
||||
## Running the Agent Host
|
||||
|
||||
Set the required environment variables and run the sample with `dotnet run`:
|
||||
|
||||
```bash
|
||||
export AZURE_AI_PROJECT_ENDPOINT="https://<account>.services.ai.azure.com/api/projects/<project>"
|
||||
export AZURE_AI_MODEL_DEPLOYMENT_NAME="gpt-4o"
|
||||
export SKILL_NAMES="support-style,escalation-policy"
|
||||
export PROVISION_SAMPLE_SKILLS="true" # First run only — provisions skills to Foundry
|
||||
```
|
||||
|
||||
Or in PowerShell:
|
||||
|
||||
```powershell
|
||||
$env:SKILL_NAMES="support-style,escalation-policy"
|
||||
$env:PROVISION_SAMPLE_SKILLS="true" # First run only — provisions skills to Foundry
|
||||
```
|
||||
|
||||
You can also place these in a `.env` file next to `Program.cs` — see [`.env.example`](.env.example).
|
||||
|
||||
On startup you should see:
|
||||
|
||||
```text
|
||||
Skill 'support-style' already exists in Foundry.
|
||||
Skill 'escalation-policy' already exists in Foundry.
|
||||
Downloading skill 'support-style' from Foundry...
|
||||
Downloading skill 'escalation-policy' from Foundry...
|
||||
```
|
||||
|
||||
The downloaded `SKILL.md` files land under `downloaded_skills/<name>/SKILL.md` next to the published output. This directory is recreated from scratch on every run, so deleting it manually is never necessary.
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
> Send a POST request to the server with a JSON body containing an `"input"` field to interact with the agent. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "Hi, I am Alex. I just want to confirm I can return my tent within 30 days."}'
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "I want a $750 refund on Order #A-1042 right now or I am calling my lawyer."}'
|
||||
```
|
||||
|
||||
| Prompt mentions | Skill that should drive the response |
|
||||
|---|---|
|
||||
| Routine return / shipping / care question | Model loads `support-style` (canary `STYLE-CANARY-3318`) — no escalation. |
|
||||
| Injury, legal threat, press, or refund > $500 | Model loads `escalation-policy` (canary `ESC-CANARY-7742`) **and** `support-style`. |
|
||||
|
||||
Because skills are loaded on demand, the canary token in a response also proves the model actually invoked `load_skill` for the matching skill (not just saw its name in the advertised list).
|
||||
|
||||
## Deploying the Agent to Foundry
|
||||
|
||||
When deploying to Foundry, make sure `SKILL_NAMES` is set in your `azd` environment so it gets injected into the hosted container per [`agent.manifest.yaml`](agent.manifest.yaml):
|
||||
|
||||
```bash
|
||||
azd env set SKILL_NAMES "support-style,escalation-policy"
|
||||
```
|
||||
|
||||
The deployed agent's Managed Identity needs **Azure AI User** on the Foundry project to download skills at startup.
|
||||
|
||||
> The `skills/` source folder is **not** deployed to Foundry — only the downloaded skills are used at runtime. The provisioning step must have been run against the same Foundry project before the agent can download the skills.
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
# yaml-language-server: $schema=https://raw.githubusercontent.com/microsoft/AgentSchema/refs/heads/main/schemas/v1.0/AgentManifest.yaml
|
||||
name: hosted-agent-skills
|
||||
displayName: "Hosted Agent Skills"
|
||||
|
||||
description: >
|
||||
An Agent Framework agent that downloads its behavioral guidelines from the Foundry
|
||||
Skills REST API at startup, demonstrating how to decouple behavioral guidelines
|
||||
(tone, escalation policy, etc.) from agent code using AgentSkillsProvider.
|
||||
|
||||
metadata:
|
||||
tags:
|
||||
- AI Agent Hosting
|
||||
- Azure AI AgentServer
|
||||
- Responses Protocol
|
||||
- Agent Framework
|
||||
- Agent Skills
|
||||
- Foundry Skills
|
||||
|
||||
template:
|
||||
name: hosted-agent-skills
|
||||
kind: hosted
|
||||
protocols:
|
||||
- protocol: responses
|
||||
version: 1.0.0
|
||||
resources:
|
||||
cpu: "0.25"
|
||||
memory: 0.5Gi
|
||||
environment_variables:
|
||||
- name: AZURE_AI_MODEL_DEPLOYMENT_NAME
|
||||
value: "{{AZURE_AI_MODEL_DEPLOYMENT_NAME}}"
|
||||
- name: SKILL_NAMES
|
||||
value: "{{SKILL_NAMES}}"
|
||||
parameters:
|
||||
properties:
|
||||
- name: SKILL_NAMES
|
||||
secret: false
|
||||
description: Comma-separated list of Foundry skill names to download at startup (e.g., support-style,escalation-policy)
|
||||
resources:
|
||||
- kind: model
|
||||
id: gpt-4.1-mini
|
||||
name: AZURE_AI_MODEL_DEPLOYMENT_NAME
|
||||
@@ -0,0 +1,14 @@
|
||||
# yaml-language-server: $schema=https://raw.githubusercontent.com/microsoft/AgentSchema/refs/heads/main/schemas/v1.0/ContainerAgent.yaml
|
||||
kind: hosted
|
||||
name: hosted-agent-skills
|
||||
protocols:
|
||||
- protocol: responses
|
||||
version: 1.0.0
|
||||
resources:
|
||||
cpu: "0.25"
|
||||
memory: 0.5Gi
|
||||
environment_variables:
|
||||
- name: AZURE_AI_MODEL_DEPLOYMENT_NAME
|
||||
value: ${AZURE_AI_MODEL_DEPLOYMENT_NAME}
|
||||
- name: SKILL_NAMES
|
||||
value: ${SKILL_NAMES}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
#requires -Version 7
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Local smoke test for the Hosted-AgentSkills sample.
|
||||
.DESCRIPTION
|
||||
Publishes the sample, builds the contributor Docker image, runs the container, drives
|
||||
two conversations via curl invocations, and asserts that the agent loaded the correct
|
||||
Foundry Skill for each prompt (verified via canary tokens in the response).
|
||||
Exits non-zero on failure.
|
||||
|
||||
Prerequisites:
|
||||
- Docker
|
||||
- az login (token is fetched from the host)
|
||||
- .env populated with AZURE_AI_PROJECT_ENDPOINT and model deployment
|
||||
- Skills provisioned to Foundry (set PROVISION_SAMPLE_SKILLS=true on first run)
|
||||
.NOTES
|
||||
This script is for local Docker debugging only. The Foundry platform supplies the
|
||||
isolation keys for every inbound request in production and the dev fallback used here
|
||||
must not be enabled in production deployments.
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[int]$Port = 8088,
|
||||
[string]$ImageName = 'hosted-agent-skills-smoke',
|
||||
[string]$ContainerName = 'hosted-agent-skills-smoke'
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location -Path $PSScriptRoot/..
|
||||
|
||||
if (-not (Test-Path .env)) {
|
||||
throw '.env not found. Copy .env.example to .env and fill in AZURE_AI_PROJECT_ENDPOINT.'
|
||||
}
|
||||
|
||||
Write-Host '==> Publishing sample for linux-musl-x64 ...'
|
||||
dotnet publish -c Debug -f net10.0 -r linux-musl-x64 --self-contained false -o out --tl:off | Out-Host
|
||||
if ($LASTEXITCODE -ne 0) { throw 'dotnet publish failed.' }
|
||||
|
||||
Write-Host '==> Building docker image ...'
|
||||
docker build -f Dockerfile.contributor -t $ImageName . | Out-Host
|
||||
if ($LASTEXITCODE -ne 0) { throw 'docker build failed.' }
|
||||
|
||||
Write-Host '==> Fetching bearer token ...'
|
||||
$bearer = az account get-access-token --resource https://ai.azure.com --query accessToken -o tsv
|
||||
if (-not $bearer) { throw 'Failed to obtain bearer token. Run az login.' }
|
||||
|
||||
function Start-Container {
|
||||
docker rm -f $ContainerName 2>$null | Out-Null
|
||||
docker run -d --name $ContainerName -p ${Port}:8088 `
|
||||
-e AGENT_NAME=hosted-agent-skills `
|
||||
-e AZURE_BEARER_TOKEN=$bearer `
|
||||
-e HOSTED_USER_ISOLATION_KEY=smoke-user `
|
||||
-e HOSTED_CHAT_ISOLATION_KEY=smoke-chat-1 `
|
||||
--env-file .env `
|
||||
$ImageName | Out-Host
|
||||
if ($LASTEXITCODE -ne 0) { throw "docker run failed." }
|
||||
# Wait for the server to start and download skills from Foundry.
|
||||
Write-Host ' Waiting for startup (skill download + server ready) ...'
|
||||
Start-Sleep -Seconds 15
|
||||
}
|
||||
|
||||
function Invoke-Agent([string]$Prompt, [string]$PreviousResponseId = $null) {
|
||||
$body = @{ input = $Prompt; model = 'hosted-agent-skills' }
|
||||
if ($PreviousResponseId) { $body['previous_response_id'] = $PreviousResponseId }
|
||||
$json = $body | ConvertTo-Json -Compress
|
||||
$resp = Invoke-RestMethod -Method Post -Uri "http://localhost:$Port/responses" -ContentType 'application/json' -Body $json
|
||||
return $resp
|
||||
}
|
||||
|
||||
function Get-ResponseText($response) {
|
||||
return ($response.output | ForEach-Object { $_.content | ForEach-Object { $_.text } }) -join ' '
|
||||
}
|
||||
|
||||
function Assert-Contains([string]$Haystack, [string]$Needle, [string]$Label) {
|
||||
if ($Haystack -notmatch [regex]::Escape($Needle)) {
|
||||
throw "FAILED [$Label]: expected response to contain '$Needle' but got: $Haystack"
|
||||
}
|
||||
Write-Host "PASS [$Label]: response contains '$Needle'."
|
||||
}
|
||||
|
||||
try {
|
||||
Start-Container
|
||||
|
||||
Write-Host '==> Test 1: Routine support question -> support-style skill ...'
|
||||
$r1 = Invoke-Agent -Prompt 'Hi, I am Alex. I just want to confirm I can return my tent within 30 days.'
|
||||
$text1 = Get-ResponseText $r1
|
||||
Assert-Contains $text1 'STYLE-CANARY-3318' 'routine question: support-style canary'
|
||||
|
||||
Write-Host '==> Test 2: Escalation trigger -> escalation-policy skill ...'
|
||||
$r2 = Invoke-Agent -Prompt 'I want a $750 refund on Order #A-1042 right now or I am calling my lawyer.'
|
||||
$text2 = Get-ResponseText $r2
|
||||
Assert-Contains $text2 'ESC-CANARY-7742' 'escalation trigger: escalation-policy canary'
|
||||
|
||||
Write-Host ''
|
||||
Write-Host '==> All smoke assertions passed.'
|
||||
}
|
||||
finally {
|
||||
docker rm -f $ContainerName 2>$null | Out-Null
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
---
|
||||
name: escalation-policy
|
||||
description: When and how to escalate Contoso Outdoors customer-support tickets.
|
||||
---
|
||||
|
||||
# Contoso Outdoors Escalation Policy
|
||||
|
||||
You must follow this escalation policy on every conversation.
|
||||
|
||||
## Escalate immediately when the customer
|
||||
|
||||
- Reports an injury, allergic reaction, or other safety incident.
|
||||
- Mentions legal action, regulators, or the press.
|
||||
- Has waited more than 14 days for a refund that was already approved.
|
||||
- Requests a refund larger than $500.
|
||||
|
||||
## How to escalate
|
||||
|
||||
1. Acknowledge the issue in one sentence.
|
||||
2. Tell the customer you are escalating to a senior specialist.
|
||||
3. Provide the escalation reference `ESC-CANARY-7742` and the SLA: a senior
|
||||
specialist will reply within 1 business day.
|
||||
4. Do not promise a specific outcome (refund, replacement, compensation) on
|
||||
escalated tickets — only the senior specialist can commit to one.
|
||||
|
||||
## Do not escalate
|
||||
|
||||
- Routine returns within the standard 30-day window.
|
||||
- Shipping status questions.
|
||||
- Product care and usage questions.
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
---
|
||||
name: support-style
|
||||
description: Contoso Outdoors customer-support tone and formatting guidelines.
|
||||
---
|
||||
|
||||
# Contoso Outdoors Support Style
|
||||
|
||||
You are speaking on behalf of Contoso Outdoors customer support.
|
||||
|
||||
## Voice
|
||||
|
||||
- Warm, concise, and confident — never apologetic in a hand-wringing way.
|
||||
- Use the customer's name when it is known.
|
||||
- Sign every response with `— Contoso Outdoors Support`.
|
||||
|
||||
## Formatting
|
||||
|
||||
- Keep replies to 1–3 short paragraphs unless the customer asks for detail.
|
||||
- Use bullet lists only when enumerating concrete steps or options.
|
||||
- Always reference order numbers as `Order #<id>` (e.g. `Order #A-1042`).
|
||||
|
||||
## Canary
|
||||
|
||||
To prove this skill was loaded, include the token `STYLE-CANARY-3318` on a
|
||||
separate line at the bottom of every response, prefixed with `# `.
|
||||
+3
-3
@@ -27,9 +27,9 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReferences above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.6.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -23,8 +23,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Azure.AI.Projects" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
</ItemGroup>
|
||||
|
||||
+2
-2
@@ -33,8 +33,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -23,8 +23,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Azure.AI.Projects" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
</ItemGroup>
|
||||
|
||||
+2
-2
@@ -25,8 +25,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -26,8 +26,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -25,8 +25,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReferences above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -25,8 +25,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+3
-3
@@ -26,9 +26,9 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReferences above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.6.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+2
-2
@@ -25,8 +25,8 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+5
-5
@@ -32,11 +32,11 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReference above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Hosting" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Workflows" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.6.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Workflows" Version="1.6.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
+4
-4
@@ -27,10 +27,10 @@
|
||||
|
||||
<!-- For end-users: uncomment the PackageReference below and remove the ProjectReferences above
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Workflows" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Foundry.Hosting" Version="1.6.1-preview.260514.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.6.1" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.Workflows" Version="1.6.1" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
|
||||
|
||||
@@ -20,8 +20,28 @@ internal static class AGUIChatMessageExtensions
|
||||
this IEnumerable<AGUIMessage> aguiMessages,
|
||||
JsonSerializerOptions jsonSerializerOptions)
|
||||
{
|
||||
// Coalesce consecutive AGUIAssistantMessages that carry tool_calls into a single
|
||||
// ChatMessage. The AG-UI client (e.g. @ag-ui/client) creates a separate assistant
|
||||
// message per tool call when ToolCallStartEvent.parentMessageId is empty, but
|
||||
// OpenAI's chat-completion API requires every assistant message with tool_calls
|
||||
// to be IMMEDIATELY followed by tool responses for each of its tool_call_ids.
|
||||
// Sending two consecutive single-tool-call assistant messages before any tool
|
||||
// result triggers HTTP 400 "tool_call_ids did not have response messages".
|
||||
List<AIContent>? pendingContents = null;
|
||||
string? pendingId = null;
|
||||
|
||||
foreach (var message in aguiMessages)
|
||||
{
|
||||
bool isAssistantWithToolCalls =
|
||||
message is AGUIAssistantMessage am && am.ToolCalls is { Length: > 0 };
|
||||
|
||||
if (pendingContents is not null && !isAssistantWithToolCalls)
|
||||
{
|
||||
yield return new ChatMessage(ChatRole.Assistant, pendingContents) { MessageId = pendingId };
|
||||
pendingContents = null;
|
||||
pendingId = null;
|
||||
}
|
||||
|
||||
var role = MapChatRole(message.Role);
|
||||
|
||||
switch (message)
|
||||
@@ -84,14 +104,14 @@ internal static class AGUIChatMessageExtensions
|
||||
|
||||
case AGUIAssistantMessage assistantMessage when assistantMessage.ToolCalls is { Length: > 0 }:
|
||||
{
|
||||
var contents = new List<AIContent>();
|
||||
pendingContents ??= new List<AIContent>();
|
||||
pendingId ??= message.Id;
|
||||
|
||||
if (!string.IsNullOrEmpty(assistantMessage.Content))
|
||||
{
|
||||
contents.Add(new TextContent(assistantMessage.Content));
|
||||
pendingContents.Add(new TextContent(assistantMessage.Content));
|
||||
}
|
||||
|
||||
// Add tool calls
|
||||
foreach (var toolCall in assistantMessage.ToolCalls)
|
||||
{
|
||||
Dictionary<string, object?>? arguments = null;
|
||||
@@ -102,16 +122,12 @@ internal static class AGUIChatMessageExtensions
|
||||
jsonSerializerOptions.GetTypeInfo(typeof(Dictionary<string, object?>)));
|
||||
}
|
||||
|
||||
contents.Add(new FunctionCallContent(
|
||||
pendingContents.Add(new FunctionCallContent(
|
||||
toolCall.Id,
|
||||
toolCall.Function.Name,
|
||||
arguments));
|
||||
}
|
||||
|
||||
yield return new ChatMessage(role, contents)
|
||||
{
|
||||
MessageId = message.Id
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -134,6 +150,12 @@ internal static class AGUIChatMessageExtensions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flush remaining pending assistant-tool-call entry at end of stream.
|
||||
if (pendingContents is not null)
|
||||
{
|
||||
yield return new ChatMessage(ChatRole.Assistant, pendingContents) { MessageId = pendingId };
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<AGUIMessage> AsAGUIMessages(
|
||||
|
||||
@@ -448,24 +448,36 @@ internal static class ChatResponseUpdateAGUIExtensions
|
||||
};
|
||||
|
||||
string? currentMessageId = null;
|
||||
string? streamingMessageId = null;
|
||||
string? textStreamingFallback = null;
|
||||
bool textInFallback = false;
|
||||
string? currentReasoningBaseId = null;
|
||||
string? currentReasoningId = null;
|
||||
string? currentReasoningMessageId = null;
|
||||
await foreach (var chatResponse in updates.WithCancellation(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
// Generate a fallback MessageId when the provider doesn't supply one.
|
||||
// This ensures all AGUI events have a valid messageId regardless of agent type.
|
||||
if (string.IsNullOrWhiteSpace(chatResponse.MessageId))
|
||||
// The text-event surface (TextMessageStart/Content/End) requires a non-empty
|
||||
// MessageId to be valid AGUI. Generate a fallback scoped to a contiguous run of
|
||||
// null/empty-MessageId chunks (one logical text message). Leave the raw
|
||||
// chatResponse.MessageId untouched so the tool-call surface below uses the raw
|
||||
// provider value — collapsing parallel tool calls under a synthetic shared parent
|
||||
// would make the FE render them as one assistant-message bubble instead of
|
||||
// distinct rows.
|
||||
string? textMessageId = chatResponse.MessageId;
|
||||
if (string.IsNullOrWhiteSpace(textMessageId))
|
||||
{
|
||||
chatResponse.MessageId = ContainsToolResult(chatResponse)
|
||||
? Guid.NewGuid().ToString("N")
|
||||
: (streamingMessageId ??= Guid.NewGuid().ToString("N"));
|
||||
textStreamingFallback ??= Guid.NewGuid().ToString("N");
|
||||
textMessageId = textStreamingFallback;
|
||||
textInFallback = true;
|
||||
}
|
||||
else if (textInFallback)
|
||||
{
|
||||
textStreamingFallback = null;
|
||||
textInFallback = false;
|
||||
}
|
||||
|
||||
if (chatResponse is { Contents.Count: > 0 } &&
|
||||
chatResponse.Contents[0] is TextContent &&
|
||||
!string.Equals(currentMessageId, chatResponse.MessageId, StringComparison.Ordinal))
|
||||
!string.Equals(currentMessageId, textMessageId, StringComparison.Ordinal))
|
||||
{
|
||||
// Close any open reasoning block before opening a text message, so AG-UI
|
||||
// events are properly bracketed. MEAI providers share one MessageId across
|
||||
@@ -498,11 +510,11 @@ internal static class ChatResponseUpdateAGUIExtensions
|
||||
// Start the new message
|
||||
yield return new TextMessageStartEvent
|
||||
{
|
||||
MessageId = chatResponse.MessageId!,
|
||||
MessageId = textMessageId!,
|
||||
Role = chatResponse.Role!.Value.Value
|
||||
};
|
||||
|
||||
currentMessageId = chatResponse.MessageId;
|
||||
currentMessageId = textMessageId;
|
||||
}
|
||||
|
||||
// Emit text content if present
|
||||
@@ -577,9 +589,15 @@ internal static class ChatResponseUpdateAGUIExtensions
|
||||
currentReasoningMessageId = null;
|
||||
}
|
||||
|
||||
// Each tool result is a distinct tool-role message on the AGUI wire.
|
||||
// MEAI's FunctionInvokingChatClient shares one synthetic MessageId
|
||||
// across all FunctionResultContent items, but the FE keys messages
|
||||
// by id, so emitting them with the same id collapses them in React
|
||||
// reconciliation. Derive a unique, deterministic per-result id from
|
||||
// the (LLM-assigned) call id.
|
||||
yield return new ToolCallResultEvent
|
||||
{
|
||||
MessageId = chatResponse.MessageId,
|
||||
MessageId = $"result-{functionResultContent.CallId}",
|
||||
ToolCallId = functionResultContent.CallId,
|
||||
Content = SerializeResultContent(functionResultContent, jsonSerializerOptions) ?? "",
|
||||
Role = AGUIRoles.Tool
|
||||
@@ -674,7 +692,7 @@ internal static class ChatResponseUpdateAGUIExtensions
|
||||
// Text content event
|
||||
yield return new TextMessageContentEvent
|
||||
{
|
||||
MessageId = chatResponse.MessageId!,
|
||||
MessageId = textMessageId!,
|
||||
#if !NET
|
||||
Delta = Encoding.UTF8.GetString(dataContent.Data.ToArray())
|
||||
#else
|
||||
@@ -726,17 +744,4 @@ internal static class ChatResponseUpdateAGUIExtensions
|
||||
_ => JsonSerializer.Serialize(functionResultContent.Result, options.GetTypeInfo(functionResultContent.Result.GetType())),
|
||||
};
|
||||
}
|
||||
|
||||
private static bool ContainsToolResult(ChatResponseUpdate chatResponse)
|
||||
{
|
||||
foreach (AIContent content in chatResponse.Contents)
|
||||
{
|
||||
if (content is FunctionResultContent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
## v1.0.0-preview.260219.1
|
||||
|
||||
- [BREAKING] Changed ChatHistory and AIContext Providers to have pipeline semantics ([#3806](https://github.com/microsoft/agent-framework/pull/3806))
|
||||
- Marked all `RunAsync<T>` overloads as `new`, added missing ones, and added support for primitives and arrays ([#3803](https://github.com/microsoft/agent-framework/pull/3803))
|
||||
- Marked all `RunAsync<T>` overloads as `new`, added missing ones, and added support for primitives and arrays #3803
|
||||
- Improve session cast error message quality and consistency ([#3973](https://github.com/microsoft/agent-framework/pull/3973))
|
||||
|
||||
## v1.0.0-preview.260212.1
|
||||
|
||||
@@ -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.Agents.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;
|
||||
}
|
||||
}
|
||||
+395
@@ -0,0 +1,395 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class AddConversationMessageTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Adds a new message to the specified agent conversation\n/// </s" +
|
||||
"ummary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExe" +
|
||||
"cutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true);
|
||||
this.Write("\n if (string.IsNullOrWhiteSpace(conversationId))\n {\n thr" +
|
||||
"ow new DeclarativeActionException($\"Conversation identifier must be defined: {th" +
|
||||
"is.Id}\");\n }\n ChatMessage newMessage = new(ChatRole.");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatEnum(this.Model.Role, RoleMap)));
|
||||
this.Write(", await this.GetContentAsync(context).ConfigureAwait(false)) { AdditionalProperti" +
|
||||
"es = this.GetMetadata() };\n newMessage = await agentProvider.CreateMessag" +
|
||||
"eAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
AssignVariable(this.Message, "newMessage");
|
||||
|
||||
this.Write("\n return default;\n }\n\n private async ValueTask<IList<AIContent>> Get" +
|
||||
"ContentAsync(IWorkflowContext context)\n {\n List<AIContent> content = [" +
|
||||
"];\n ");
|
||||
|
||||
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)
|
||||
{
|
||||
this.Write("\n content.Add(UriContent(contentValue");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index));
|
||||
this.Write(", \"image/*\"));");
|
||||
|
||||
}
|
||||
else if (contentType == AgentMessageContentType.ImageFile)
|
||||
{
|
||||
this.Write("\n content.Add(new HostedFileContent(contentValue");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index));
|
||||
this.Write("));");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Write("\n content.Add(new TextContent(contentValue");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index));
|
||||
this.Write("));");
|
||||
|
||||
}
|
||||
}
|
||||
this.Write("\n return content;\n }\n\n private AdditionalPropertiesDictionary? GetMe" +
|
||||
"tadata()\n {");
|
||||
|
||||
EvaluateRecordExpression<object>(this.Model.Metadata, "metadata");
|
||||
this.Write("\n\n if (metadata is null)\n {\n return null; \n }\n" +
|
||||
"\n return new AdditionalPropertiesDictionary(metadata);\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 EvaluateRecordExpression<TValue>(ObjectExpression<RecordDataValue> expression, string targetVariable)
|
||||
{
|
||||
string resultTypeName = $"Dictionary<string, {GetTypeAlias<TValue>()}?>?";
|
||||
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<string>(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<string>(");
|
||||
|
||||
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<string>(");
|
||||
|
||||
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));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <summary>
|
||||
/// Adds a new message to the specified agent conversation
|
||||
/// </summary>
|
||||
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
|
||||
{
|
||||
// <inheritdoc />
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{<#
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); #>
|
||||
if (string.IsNullOrWhiteSpace(conversationId))
|
||||
{
|
||||
throw new DeclarativeActionException($"Conversation identifier must be defined: {this.Id}");
|
||||
}
|
||||
ChatMessage newMessage = new(ChatRole.<#= FormatEnum(this.Model.Role, RoleMap) #>, await this.GetContentAsync(context).ConfigureAwait(false)) { AdditionalProperties = this.GetMetadata() };
|
||||
newMessage = await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);<#
|
||||
AssignVariable(this.Message, "newMessage");
|
||||
#>
|
||||
return default;
|
||||
}
|
||||
|
||||
private async ValueTask<IList<AIContent>> GetContentAsync(IWorkflowContext context)
|
||||
{
|
||||
List<AIContent> content = [];
|
||||
<#
|
||||
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)
|
||||
{#>
|
||||
content.Add(UriContent(contentValue<#= index #>, "image/*"));<#
|
||||
}
|
||||
else if (contentType == AgentMessageContentType.ImageFile)
|
||||
{#>
|
||||
content.Add(new HostedFileContent(contentValue<#= index #>));<#
|
||||
}
|
||||
else
|
||||
{#>
|
||||
content.Add(new TextContent(contentValue<#= index #>));<#
|
||||
}
|
||||
}#>
|
||||
return content;
|
||||
}
|
||||
|
||||
private AdditionalPropertiesDictionary? GetMetadata()
|
||||
{<#
|
||||
EvaluateRecordExpression<object>(this.Model.Metadata, "metadata"); #>
|
||||
|
||||
if (metadata is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new AdditionalPropertiesDictionary(metadata);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Agents.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();
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ClearAllVariablesTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Reset all the state for the targeted variable scope.\n/// </sum" +
|
||||
"mary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
EvaluateEnumExpression<VariablesToClearWrapper, string>(this.Model.Variables, "targetScopeName", ScopeMap, isNullable: true);
|
||||
this.Write("\n await context.QueueClearScopeAsync(targetScopeName).ConfigureAwait(false" +
|
||||
");\n\n return default;\n }\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 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)
|
||||
{
|
||||
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<TValue>(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<TValue>()));
|
||||
|
||||
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<TValue>(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);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
|
||||
<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #>
|
||||
<#@ include file="Snippets/EvaluateEnumExpressionTemplate.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;
|
||||
}
|
||||
}
|
||||
+25
@@ -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.Agents.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,339 @@
|
||||
// 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.Agents.ObjectModel;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen;
|
||||
|
||||
internal abstract class CodeTemplate
|
||||
{
|
||||
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 field ??= new StringBuilder();
|
||||
}
|
||||
set;
|
||||
}
|
||||
/// <summary>
|
||||
/// The error collection for the generation process
|
||||
/// </summary>
|
||||
public CompilerErrorCollection Errors => field ??= [];
|
||||
|
||||
/// <summary>
|
||||
/// A list of the lengths of each indent that was added with PushIndent
|
||||
/// </summary>
|
||||
private List<int> IndentLengths { get => field ??= []; }
|
||||
|
||||
/// <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
|
||||
}
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ConditionGroupTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Conditional branching similar to an if / elseif / elseif / els" +
|
||||
"e chain.\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
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}");
|
||||
this.Write("\n if (condition");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index));
|
||||
this.Write(")\n {\n return \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ConditionGroupExecutor.Steps.Item(this.Model, conditionItem)));
|
||||
this.Write("\";\n }\n ");
|
||||
|
||||
}
|
||||
|
||||
this.Write("\n return \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ConditionGroupExecutor.Steps.Else(this.Model)));
|
||||
this.Write("\";\n }\n}");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
|
||||
void AssignVariable(PropertyPath targetVariable, string valueVariable, bool tightFormat = false)
|
||||
{
|
||||
if (targetVariable is not null)
|
||||
{
|
||||
this.Write("\n await context.QueueStateUpdateAsync(key: \"");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(VariableName(targetVariable)));
|
||||
|
||||
this.Write("\", value: ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(valueVariable));
|
||||
|
||||
this.Write(", scopeName: \"");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(VariableScope(targetVariable)));
|
||||
|
||||
this.Write("\").ConfigureAwait(false);");
|
||||
|
||||
|
||||
if (!tightFormat)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void 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<bool>(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<bool>>(");
|
||||
|
||||
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<bool>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <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)#>";
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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; }
|
||||
}
|
||||
+324
@@ -0,0 +1,324 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using Microsoft.Extensions.AI;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class CopyConversationMessagesTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Copies one or more messages into the specified agent conversat" +
|
||||
"ion.\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExe" +
|
||||
"cutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true);
|
||||
this.Write("\n if (string.IsNullOrWhiteSpace(conversationId))\n {\n thr" +
|
||||
"ow new DeclarativeActionException($\"Conversation identifier must be defined: {th" +
|
||||
"is.Id}\");\n }");
|
||||
|
||||
EvaluateValueExpression<ChatMessage[]>(this.Model.Messages, "messages");
|
||||
|
||||
this.Write(@"
|
||||
if (messages is not null)
|
||||
{
|
||||
foreach (ChatMessage message in messages)
|
||||
{
|
||||
await agentProvider.CreateMessageAsync(conversationId, message, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}");
|
||||
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 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<string>(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<string>(");
|
||||
|
||||
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<string>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void EvaluateValueExpression(ValueExpression expression, string targetVariable) =>
|
||||
EvaluateValueExpression<object>(expression, targetVariable);
|
||||
|
||||
void EvaluateValueExpression<TValue>(ValueExpression expression, string targetVariable)
|
||||
{
|
||||
if (expression is null)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = null;");
|
||||
|
||||
|
||||
}
|
||||
else if (expression.IsLiteral)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.ReadStateAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.EvaluateValueAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.EvaluateValueAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
this.Write(">(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <summary>
|
||||
/// Copies one or more messages into the specified agent conversation.
|
||||
/// </summary>
|
||||
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExecutor(id: "<#= this.Id #>", session)
|
||||
{
|
||||
// <inheritdoc />
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{<#
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true); #>
|
||||
if (string.IsNullOrWhiteSpace(conversationId))
|
||||
{
|
||||
throw new DeclarativeActionException($"Conversation identifier must be defined: {this.Id}");
|
||||
}<#
|
||||
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;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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; }
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class CreateConversationTemplate : ActionTemplate
|
||||
{
|
||||
/// <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/// Creates a new conversation and stores the identifier value to " +
|
||||
"the \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.ConversationId));
|
||||
this.Write("\" variable.\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExe" +
|
||||
"cutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write(@""", session)
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
string conversationId = await agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
AssignVariable(this.ConversationId, "conversationId");
|
||||
this.Write("\n await context.AddEventAsync(new ConversationUpdateEvent(conversationId))" +
|
||||
".ConfigureAwait(false);\n\n return default;\n }\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 ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
|
||||
<#@ include file="Snippets/AssignVariableTemplate.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, ResponseAgentProvider 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");#>
|
||||
await context.AddEventAsync(new ConversationUpdateEvent(conversationId)).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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,40 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </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>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class DefaultTemplate : ActionTemplate, IModeledAction
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\nDelegateExecutor ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
|
||||
this.Write(" = new(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
|
||||
this.Write(".Session");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Action is not null ? $", {this.Action}" : ""));
|
||||
this.Write(");\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate, IModeledAction" visibility="internal" linePragmas="false" #>
|
||||
<#@ 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.Agents.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,51 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class EdgeTemplate : CodeTemplate
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
if (this.Condition is not null)
|
||||
{
|
||||
this.Write("\n builder.AddEdge(");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.SourceId));
|
||||
this.Write(", ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.TargetId));
|
||||
this.Write(", (object? result) => ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Condition));
|
||||
this.Write(");");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Write("\n builder.AddEdge(");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.SourceId));
|
||||
this.Write(", ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.TargetId));
|
||||
this.Write(");");
|
||||
|
||||
}
|
||||
this.Write("\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ 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,37 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class EditTableV2Template : ActionTemplate
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Modify items in a list\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {\n return default;\n }\n}");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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,40 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </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>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class EmptyTemplate : CodeTemplate, IModeledAction
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\nDelegateExecutor ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
|
||||
this.Write(" = new(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
|
||||
this.Write(".Session");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Action is not null ? $", {this.Action}" : ""));
|
||||
this.Write(");\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<#@ template language="C#" inherits="CodeTemplate, IModeledAction" visibility="internal" linePragmas="false" #>
|
||||
<#@ 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; }
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ForeachTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n/// <summary>\n/// Loops over a list assignign the loop variable to \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.Value));
|
||||
this.Write("\" variable.\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write(@""", session)
|
||||
{
|
||||
private int _index;
|
||||
private object[] _values = [];
|
||||
|
||||
public bool HasValue { get; private set; }
|
||||
|
||||
// <inheritdoc />
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
this._index = 0;");
|
||||
|
||||
|
||||
EvaluateValueExpression(this.Model.Items, "evaluatedValue");
|
||||
this.Write(@"
|
||||
|
||||
if (evaluatedValue == null)
|
||||
{
|
||||
this._values = [];
|
||||
this.HasValue = false;
|
||||
}
|
||||
else
|
||||
if (evaluatedValue is IEnumerable evaluatedList)
|
||||
{
|
||||
this._values = [.. evaluatedList];
|
||||
}
|
||||
else
|
||||
{
|
||||
this._values = [evaluatedValue];
|
||||
}
|
||||
|
||||
await this.ResetAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public async ValueTask TakeNextAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken)
|
||||
{
|
||||
if (this.HasValue = this._index < this._values.Length)
|
||||
{
|
||||
object value = this._values[this._index];
|
||||
");
|
||||
|
||||
AssignVariable(this.Value, "value", tightFormat: true);
|
||||
|
||||
if (this.Index is not null)
|
||||
{
|
||||
AssignVariable(this.Index, "this._index", tightFormat: true);
|
||||
}
|
||||
|
||||
this.Write(@"
|
||||
|
||||
this._index++;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask CompleteAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken)
|
||||
{
|
||||
await this.ResetAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async ValueTask ResetAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{");
|
||||
|
||||
AssignVariable(this.Value, "UnassignedValue.Instance", tightFormat: true);
|
||||
|
||||
if (this.Index is not null)
|
||||
{
|
||||
AssignVariable(this.Index, "UnassignedValue.Instance", tightFormat: true);
|
||||
}
|
||||
|
||||
this.Write("\n }\n}");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
|
||||
void AssignVariable(PropertyPath targetVariable, string valueVariable, bool tightFormat = false)
|
||||
{
|
||||
if (targetVariable is not null)
|
||||
{
|
||||
this.Write("\n await context.QueueStateUpdateAsync(key: \"");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(VariableName(targetVariable)));
|
||||
|
||||
this.Write("\", value: ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(valueVariable));
|
||||
|
||||
this.Write(", scopeName: \"");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(VariableScope(targetVariable)));
|
||||
|
||||
this.Write("\").ConfigureAwait(false);");
|
||||
|
||||
|
||||
if (!tightFormat)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void EvaluateValueExpression(ValueExpression expression, string targetVariable) =>
|
||||
EvaluateValueExpression<object>(expression, targetVariable);
|
||||
|
||||
void EvaluateValueExpression<TValue>(ValueExpression expression, string targetVariable)
|
||||
{
|
||||
if (expression is null)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = null;");
|
||||
|
||||
|
||||
}
|
||||
else if (expression.IsLiteral)
|
||||
{
|
||||
this.Write("\n ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.ReadStateAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.EvaluateValueAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
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<TValue>()));
|
||||
|
||||
this.Write("? ");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(targetVariable));
|
||||
|
||||
this.Write(" = await context.EvaluateValueAsync<");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(GetTypeAlias<TValue>()));
|
||||
|
||||
this.Write(">(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
|
||||
<#@ include file="Snippets/AssignVariableTemplate.tt" once="true" #>
|
||||
<#@ include file="Snippets/EvaluateValueExpressionTemplate.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, 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 CompleteAsync(IWorkflowContext context, object? _, CancellationToken cancellationToken)
|
||||
{
|
||||
await this.ResetAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async ValueTask ResetAsync(IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{<#
|
||||
AssignVariable(this.Value, "UnassignedValue.Instance", tightFormat: true);
|
||||
|
||||
if (this.Index is not null)
|
||||
{
|
||||
AssignVariable(this.Index, "UnassignedValue.Instance", tightFormat: true);
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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,38 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class InstanceTemplate : CodeTemplate
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.ExecutorType));
|
||||
this.Write("Executor ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.InstanceVariable));
|
||||
this.Write(" = new(");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootVariable));
|
||||
this.Write(".Session");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.HasProvider ? ", options.AgentProvider" : ""));
|
||||
this.Write(");");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ 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; }
|
||||
}
|
||||
+415
@@ -0,0 +1,415 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class InvokeAzureAgentTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Invokes an agent to process messages and return a response wit" +
|
||||
"hin a conversation context.\n/// </summary>\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 // <inheritdoc />\n protected override async V" +
|
||||
"alueTask<object?> 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<ChatMessage>(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<bool>(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<bool>>(");
|
||||
|
||||
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<bool>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void EvaluateListExpression<TElement>(ValueExpression expression, string targetVariable)
|
||||
{
|
||||
string typeName = GetTypeAlias<TElement>();
|
||||
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<TElement>()));
|
||||
|
||||
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<string>(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<string>(");
|
||||
|
||||
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<string>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <summary>
|
||||
/// Invokes an agent to process messages and return a response within a conversation context.
|
||||
/// </summary>
|
||||
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider 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 DeclarativeActionException($"Agent name must be defined: {this.Id}");
|
||||
}
|
||||
<#
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId", isNullable: true);
|
||||
EvaluateBoolExpression(this.Model.Output?.AutoSend, "autoSend", defaultValue: true);
|
||||
EvaluateListExpression<ChatMessage>(this.Model.Input?.Messages, "inputMessages");#>
|
||||
|
||||
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"); #>
|
||||
return default;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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,99 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ParseValueTemplate : ActionTemplate
|
||||
{
|
||||
/// <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/// Parses a string or untyped value to the provided data type. Wh" +
|
||||
"en the input is a string, it will be treated as JSON.\n/// </summary>\ninternal se" +
|
||||
"aled class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" { \n VariableType targetType = ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.GetVariableType()));
|
||||
this.Write(";");
|
||||
|
||||
if (this.Model.Value.IsVariableReference && this.Model.Value.VariableReference.SegmentCount == 2)
|
||||
{
|
||||
this.Write("\n object? parsedValue = await context.ConvertValueAsync(targetType, key: \"" +
|
||||
"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.Value.VariableReference.VariableName));
|
||||
this.Write("\", scopeName: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.Value.VariableReference.NamespaceAlias));
|
||||
this.Write("\", cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
}
|
||||
else if (this.Model.Value.IsVariableReference)
|
||||
{
|
||||
this.Write("\n object? parsedValue = await context.ConvertValueAsync(targetType, ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(this.Model.Value.VariableReference.ToString())));
|
||||
this.Write(", cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Write("\n object? parsedValue = await context.ConvertValueAsync(targetType, ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(this.Model.Value.ExpressionText)));
|
||||
this.Write(", cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
}
|
||||
AssignVariable(this.Variable, "parsedValue");
|
||||
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 ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
|
||||
<#@ include file="Snippets/AssignVariableTemplate.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;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Linq;
|
||||
using Microsoft.Agents.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,118 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </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>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ProviderTemplate : CodeTemplate
|
||||
{
|
||||
/// <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;
|
||||
");
|
||||
|
||||
if (this.Namespace is not null)
|
||||
{
|
||||
this.Write("\nnamespace ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Namespace));
|
||||
this.Write(";\n");
|
||||
|
||||
}
|
||||
|
||||
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 ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Prefix ?? string.Empty));
|
||||
this.Write("WorkflowProvider\n{");
|
||||
|
||||
foreach (string executor in ByLine(this.Executors, formatGroup: true))
|
||||
{
|
||||
this.Write("\n ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(executor));
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootExecutorType));
|
||||
this.Write("Executor<TInput> ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootInstance));
|
||||
this.Write(" = new(options, inputTransform);");
|
||||
|
||||
|
||||
// Create executor instances
|
||||
foreach (string instance in ByLine(this.Instances))
|
||||
{
|
||||
this.Write("\n ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(instance));
|
||||
|
||||
}
|
||||
this.Write("\n\n // Define the workflow builder\n WorkflowBuilder builder = new(");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.RootInstance));
|
||||
this.Write(");\n\n // Connect executors");
|
||||
|
||||
foreach (string edge in ByLine(this.Edges))
|
||||
{
|
||||
this.Write("\n ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(edge));
|
||||
|
||||
}
|
||||
|
||||
this.Write("\n\n // Build the workflow\n return builder.Build(validateOrphans: fal" +
|
||||
"se);\n }\n}\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<#@ template language="C#" inherits="CodeTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ 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(validateOrphans: false);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class QuestionTemplate : ActionTemplate
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the template output
|
||||
/// </summary>
|
||||
public override string TransformText()
|
||||
{
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Request input.\n/// </summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {\n return default;\n }\n}");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
/// <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.Agents.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,74 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class ResetVariableTemplate : ActionTemplate
|
||||
{
|
||||
/// <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/// Resets the value of the \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Model.Variable));
|
||||
this.Write("\" variable, potentially causing re-evaluation \n/// of the default value, question" +
|
||||
" or action that provides the value to this variable.\n/// </summary>\ninternal sea" +
|
||||
"led class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session) : ActionExecutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n protected override async ValueTask<object?> ExecuteAsync(IWorkf" +
|
||||
"lowContext context, CancellationToken cancellationToken)\n {");
|
||||
|
||||
AssignVariable(this.Variable, "UnassignedValue.Instance");
|
||||
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 ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="Microsoft.Agents.ObjectModel" #>
|
||||
<#@ include file="Snippets/AssignVariableTemplate.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;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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; }
|
||||
}
|
||||
+310
@@ -0,0 +1,310 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class RetrieveConversationMessageTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Retrieves a list of messages from an agent conversation.\n/// <" +
|
||||
"/summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExe" +
|
||||
"cutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
EvaluateStringExpression(this.Model.ConversationId, "conversationId");
|
||||
EvaluateStringExpression(this.Model.MessageId, "messageId");
|
||||
this.Write("\n ChatMessage message = await agentProvider.GetMessageAsync(conversationId" +
|
||||
", messageId, cancellationToken).ConfigureAwait(false);");
|
||||
|
||||
AssignVariable(this.Model.Message, "message");
|
||||
|
||||
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 EvaluateRecordExpression<TValue>(ObjectExpression<RecordDataValue> expression, string targetVariable)
|
||||
{
|
||||
string resultTypeName = $"Dictionary<string, {GetTypeAlias<TValue>()}?>?";
|
||||
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<string>(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<string>(");
|
||||
|
||||
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<string>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <summary>
|
||||
/// Retrieves a list of messages from an agent conversation.
|
||||
/// </summary>
|
||||
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider 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;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.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; }
|
||||
}
|
||||
+584
@@ -0,0 +1,584 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.CodeGen
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class RetrieveConversationMessagesTemplate : ActionTemplate
|
||||
{
|
||||
/// <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");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n");
|
||||
this.Write("\n/// <summary>\n/// Retrieves a specific message from an agent conversation.\n/// <" +
|
||||
"/summary>\ninternal sealed class ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));
|
||||
this.Write("Executor(FormulaSession session, ResponseAgentProvider agentProvider) : ActionExe" +
|
||||
"cutor(id: \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", session)\n{\n // <inheritdoc />\n protected override async ValueTask<object" +
|
||||
"?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)\n " +
|
||||
" {");
|
||||
|
||||
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);
|
||||
this.Write(@"
|
||||
IAsyncEnumerable<ChatMessage> messagesResult =
|
||||
agentProvider.GetMessagesAsync(
|
||||
conversationId,
|
||||
limit,
|
||||
after,
|
||||
before,
|
||||
newestFirst,
|
||||
cancellationToken);
|
||||
List<ChatMessage> messages = [];
|
||||
await foreach (ChatMessage message in messagesResult.ConfigureAwait(false))
|
||||
{
|
||||
messages.Add(message);
|
||||
}");
|
||||
|
||||
AssignVariable(this.Model.Messages, "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 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)
|
||||
{
|
||||
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<TValue>(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<TValue>()));
|
||||
|
||||
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<TValue>(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<int>(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 EvaluateRecordExpression<TValue>(ObjectExpression<RecordDataValue> expression, string targetVariable)
|
||||
{
|
||||
string resultTypeName = $"Dictionary<string, {GetTypeAlias<TValue>()}?>?";
|
||||
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<string>(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<string>(");
|
||||
|
||||
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<string>(");
|
||||
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(FormatStringValue(expression.ExpressionText)));
|
||||
|
||||
this.Write(").ConfigureAwait(false);");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<#@ template language="C#" inherits="ActionTemplate" visibility="internal" linePragmas="false" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ 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" #>
|
||||
/// <summary>
|
||||
/// Retrieves a specific message from an agent conversation.
|
||||
/// </summary>
|
||||
internal sealed class <#= this.Name #>Executor(FormulaSession session, ResponseAgentProvider 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); #>
|
||||
IAsyncEnumerable<ChatMessage> messagesResult =
|
||||
agentProvider.GetMessagesAsync(
|
||||
conversationId,
|
||||
limit,
|
||||
after,
|
||||
before,
|
||||
newestFirst,
|
||||
cancellationToken);
|
||||
List<ChatMessage> messages = [];
|
||||
await foreach (ChatMessage message in messagesResult.ConfigureAwait(false))
|
||||
{
|
||||
messages.Add(message);
|
||||
}<#
|
||||
AssignVariable(this.Model.Messages, "messages");
|
||||
#>
|
||||
return default;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Agents.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,79 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </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.Agents.ObjectModel;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Class to produce the template output
|
||||
/// </summary>
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "18.0.0.0")]
|
||||
internal partial class RootTemplate : CodeTemplate, IModeledAction
|
||||
{
|
||||
/// <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 ");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.TypeName));
|
||||
this.Write("Executor<TInput>(\n DeclarativeWorkflowOptions options,\n Func<TInput, ChatMe" +
|
||||
"ssage> inputTransform) :\n RootExecutor<TInput>(\"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(this.Id));
|
||||
this.Write("\", options, inputTransform)\n where TInput : notnull\n{\n protected override a" +
|
||||
"sync ValueTask ExecuteAsync(TInput message, IWorkflowContext context, Cancellati" +
|
||||
"onToken cancellationToken)\n {");
|
||||
|
||||
if (this.TypeInfo.EnvironmentVariables.Count > 0)
|
||||
{
|
||||
this.Write("\n // Set environment variables\n await this.InitializeEnvironmentAsy" +
|
||||
"nc(\n context,");
|
||||
|
||||
int index = this.TypeInfo.EnvironmentVariables.Count - 1;
|
||||
foreach (string variableName in this.TypeInfo.EnvironmentVariables)
|
||||
{
|
||||
this.Write("\n \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(variableName));
|
||||
this.Write("\"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index > 0 ? "," : ""));
|
||||
|
||||
--index;
|
||||
}
|
||||
this.Write(").ConfigureAwait(false);\n");
|
||||
}
|
||||
|
||||
if (this.TypeInfo.UserVariables.Count > 0)
|
||||
{
|
||||
|
||||
this.Write("\n // Initialize variables");
|
||||
|
||||
foreach (VariableInformationDiagnostic variableInfo in this.TypeInfo.UserVariables)
|
||||
{
|
||||
this.Write("\n await context.QueueStateUpdateAsync(\"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(variableInfo.Path.VariableName));
|
||||
this.Write("\", UnassignedValue.Instance, \"");
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(variableInfo.Path.NamespaceAlias));
|
||||
this.Write("\").ConfigureAwait(false);");
|
||||
|
||||
}
|
||||
}
|
||||
this.Write("\n }\n}\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<#@ template language="C#" inherits="CodeTemplate, IModeledAction" visibility="internal" linePragmas="false" #>
|
||||
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Extensions" #>
|
||||
<#@ import namespace="Microsoft.Agents.AI.Workflows.Declarative.Interpreter" #>
|
||||
<#@ import namespace="Microsoft.Agents.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; }
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user