Files
agent-framework/dotnet/src/Microsoft.Agents.Workflows.Declarative/PowerFx/SystemScope.cs
T
Chris 74879489a4 .NET Workflow - Integrated updated CPS Object Model (#681)
* Checkpoint

* Update workflows/DeepResearch.yaml

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

* Comment

* Fix comment

* Update package version

* Fix nuget haxx

* Checkpoint

* Code complete

* Testing

* Message content workaround

* Add sequential flow

* Checkpoint

* Integration test project

* Checkpoint

* Checkpoint cleanup

* Complete

* Update package

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-09-15 20:41:07 +00:00

116 lines
5.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.Agents.Workflows.Declarative.Extensions;
using Microsoft.Agents.Workflows.Declarative.Interpreter;
using Microsoft.Bot.ObjectModel;
using Microsoft.Bot.ObjectModel.SystemVariables;
using Microsoft.Extensions.AI;
using Microsoft.PowerFx.Types;
namespace Microsoft.Agents.Workflows.Declarative.PowerFx;
internal static class SystemScope
{
public static class Names
{
public const string Activity = nameof(Activity);
public const string Bot = nameof(Bot);
public const string Conversation = nameof(Conversation);
public const string ConversationId = nameof(SystemVariables.ConversationId);
public const string InternalId = nameof(InternalId);
public const string LastMessage = nameof(LastMessage);
public const string LastMessageId = nameof(SystemVariables.LastMessageId);
public const string LastMessageText = nameof(SystemVariables.LastMessageText);
public const string Recognizer = nameof(Recognizer);
public const string User = nameof(User);
public const string UserLanguage = nameof(UserLanguage);
}
public static ImmutableHashSet<string> AllNames { get; } = GetNames().ToImmutableHashSet();
public static IEnumerable<string> GetNames()
{
yield return Names.Activity;
yield return Names.Bot;
yield return Names.Conversation;
yield return Names.ConversationId;
yield return Names.InternalId;
yield return Names.LastMessage;
yield return Names.LastMessageId;
yield return Names.LastMessageText;
yield return Names.Recognizer;
yield return Names.User;
yield return Names.UserLanguage;
}
public static void InitializeSystem(this WorkflowScopes scopes)
{
scopes.Set(Names.Activity, RecordValue.Empty(), VariableScopeNames.System);
scopes.Set(Names.Bot, RecordValue.Empty(), VariableScopeNames.System);
scopes.Set(Names.LastMessage, FormulaType.String.NewBlank(), VariableScopeNames.System);
Set(Names.LastMessageId);
Set(Names.LastMessageText);
scopes.Set(
Names.Conversation,
RecordValue.NewRecordFromFields(
new NamedValue("Id", FormulaType.String.NewBlank()),
new NamedValue("LocalTimeZone", FormulaValue.New(TimeZoneInfo.Local.StandardName)),
new NamedValue("LocalTimeZoneOffset", FormulaValue.New(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow))),
new NamedValue("InTestMode", FormulaValue.New(false))),
VariableScopeNames.System);
scopes.Set(Names.ConversationId, FormulaType.String.NewBlank(), VariableScopeNames.System);
scopes.Set(Names.InternalId, FormulaType.String.NewBlank(), VariableScopeNames.System);
scopes.Set(
Names.Recognizer,
RecordValue.NewRecordFromFields(
new NamedValue("Id", FormulaType.String.NewBlank()),
new NamedValue("Text", FormulaType.String.NewBlank())),
VariableScopeNames.System);
scopes.Set(
Names.User,
RecordValue.NewRecordFromFields(
new NamedValue("Language", StringValue.New(CultureInfo.CurrentCulture.TwoLetterISOLanguageName))),
VariableScopeNames.System);
scopes.Set(Names.UserLanguage, StringValue.New(CultureInfo.CurrentCulture.TwoLetterISOLanguageName), VariableScopeNames.System);
void Set(string key, string? value = null)
{
if (string.IsNullOrEmpty(value))
{
scopes.Set(key, FormulaType.String.NewBlank(), VariableScopeNames.System);
}
else
{
scopes.Set(key, FormulaValue.New(value), VariableScopeNames.System);
}
}
}
public static FormulaValue GetConversationId(this DeclarativeWorkflowState state) =>
state.Get(VariableScopeNames.System, Names.ConversationId);
public static async ValueTask SetConversationIdAsync(this DeclarativeWorkflowState state, IWorkflowContext context, string conversationId)
{
RecordValue conversation = (RecordValue)state.Get(VariableScopeNames.System, Names.Conversation);
conversation.UpdateField("Id", FormulaValue.New(conversationId));
await state.SetAsync(VariableScopeNames.System, Names.Conversation, conversation, context).ConfigureAwait(false);
await state.SetAsync(VariableScopeNames.System, Names.ConversationId, FormulaValue.New(conversationId), context).ConfigureAwait(false);
}
public static async ValueTask SetLastMessageAsync(this DeclarativeWorkflowState state, IWorkflowContext context, ChatMessage message)
{
await state.SetAsync(VariableScopeNames.System, Names.LastMessage, message.ToRecord(), context).ConfigureAwait(false);
await state.SetAsync(VariableScopeNames.System, Names.LastMessageId, message.MessageId is null ? FormulaValue.NewBlank(FormulaType.String) : FormulaValue.New(message.MessageId), context).ConfigureAwait(false);
await state.SetAsync(VariableScopeNames.System, Names.LastMessageText, FormulaValue.New(message.Text), context).ConfigureAwait(false);
}
}