Skip to content

Commit

Permalink
Allow enterprises to disable Terminal Chat or specific LMs (#18095)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
Adds registry keys to allow enterprises to disable Terminal Chat or only
enable certain LMs

Notes:
- If the policy is not set at all, all LM providers are allowed
- If the policy is set, we look at the provided allow list to determine
which LMs (if any) should be allowed
- Only the allowed LMs show up in the AI Settings page to allow for
configuration
- If no LMs are allowed, the Terminal Chat action is not shown in the
new tab dropdown nor the command palette and existing keybindings to
open Terminal Chat are not handled
- Regardless of the policy, any keybindings/modifications to a user's
toggle terminal chat action are preserved

## Validation Steps Performed
Toggling the policy/updating the allow list updates the settings page,
dropdown and command palette appropriately

## PR Checklist
- [x] Closes #16401

Co-authored-by: Heiko <[email protected]>
  • Loading branch information
PankajBhojwani and htcfreek authored Oct 29, 2024
1 parent b2524f9 commit 438621f
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 106 deletions.
8 changes: 8 additions & 0 deletions policies/WindowsTerminal.admx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<supportedOn>
<definitions>
<definition name="SUPPORTED_WindowsTerminal_1_21" displayName="$(string.SUPPORTED_WindowsTerminal_1_21)" />
<definition name="SUPPORTED_WindowsTerminalCanary_1_23" displayName="$(string.SUPPORTED_WindowsTerminalCanary_1_23)" />
</definitions>
</supportedOn>
<categories>
Expand All @@ -24,5 +25,12 @@
<multiText id="DisabledProfileSources" valueName="DisabledProfileSources" required="true" />
</elements>
</policy>
<policy name="EnabledLMProviders" class="Both" displayName="$(string.EnabledLMProviders)" explainText="$(string.EnabledLMProvidersText)" presentation="$(presentation.EnabledLMProviders)" key="Software\Policies\Microsoft\Windows Terminal">
<parentCategory ref="WindowsTerminal" />
<supportedOn ref="SUPPORTED_WindowsTerminalCanary_1_23" />
<elements>
<multiText id="EnabledLMProviders" valueName="EnabledLMProviders" required="false" />
</elements>
</policy>
</policies>
</policyDefinitions>
15 changes: 15 additions & 0 deletions policies/en-US/WindowsTerminal.adml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<stringTable>
<string id="WindowsTerminal">Windows Terminal</string>
<string id="SUPPORTED_WindowsTerminal_1_21">At least Windows Terminal 1.21</string>
<string id="SUPPORTED_WindowsTerminalCanary_1_23">At least Windows Terminal Canary 1.23</string>
<string id="DisabledProfileSources">Disabled Profile Sources</string>
<string id="DisabledProfileSourcesText">Profiles will not be generated from any sources listed here. Source names can be arbitrary strings. Potential candidates can be found as the "source" property on profile definitions in Windows Terminal's settings.json file.

Expand All @@ -18,11 +19,25 @@ Common sources are:
For instance, setting this policy to Windows.Terminal.Wsl will disable the builtin WSL integration of Windows Terminal.

Note: Existing profiles will disappear from Windows Terminal after adding their source to this policy.</string>
<string id="EnabledLMProviders">Enabled Language Model/AI Providers</string>
<string id="EnabledLMProvidersText">The listed Language Models/AI Providers will be available for use in Terminal Chat.

Enabling the policy but leaving the list empty disallows all providers and therefore disables the Terminal Chat feature completely.

Common providers are:
- AzureOpenAI
- OpenAI
- GitHubCopilot

For instance, setting this policy to GitHubCopilot will allow the use of GitHubCopilot in Terminal Chat.</string>
</stringTable>
<presentationTable>
<presentation id="DisabledProfileSources">
<multiTextBox refId="DisabledProfileSources">List of disabled sources (one per line)</multiTextBox>
</presentation>
<presentation id="EnabledLMProviders">
<multiTextBox refId="EnabledLMProviders">List of enabled Language Model/AI Providers (one per line)</multiTextBox>
</presentation>
</presentationTable>
</resources>
</policyDefinitionResources>
12 changes: 6 additions & 6 deletions src/cascadia/QueryExtension/ExtensionPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
_lmProvider = lmProvider;
_clearAndInitializeMessages(nullptr, nullptr);

const auto brandingData = _lmProvider.BrandingData();
const auto headerIconPath = brandingData.HeaderIconPath().empty() ? terminalChatLogoPath : brandingData.HeaderIconPath();
const auto brandingData = _lmProvider ? _lmProvider.BrandingData() : nullptr;
const auto headerIconPath = (!brandingData || brandingData.HeaderIconPath().empty()) ? terminalChatLogoPath : brandingData.HeaderIconPath();
Windows::Foundation::Uri headerImageSourceUri{ headerIconPath };
Media::Imaging::BitmapImage headerImageSource{ headerImageSourceUri };
HeaderIcon().Source(headerImageSource);

const auto headerText = brandingData.HeaderText().empty() ? RS_(L"IntroText/Text") : brandingData.HeaderText();
const auto headerText = (!brandingData || brandingData.HeaderText().empty()) ? RS_(L"IntroText/Text") : brandingData.HeaderText();
QueryIntro().Text(headerText);

const auto subheaderText = brandingData.SubheaderText().empty() ? RS_(L"TitleSubheader/Text") : brandingData.SubheaderText();
const auto subheaderText = (!brandingData || brandingData.SubheaderText().empty()) ? RS_(L"TitleSubheader/Text") : brandingData.SubheaderText();
TitleSubheader().Text(subheaderText);
}

Expand Down Expand Up @@ -234,9 +234,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
}
}

const auto brandingData = _lmProvider.BrandingData();
const auto brandingData = _lmProvider ? _lmProvider.BrandingData() : nullptr;
const auto responseAttribution = response.ResponseAttribution().empty() ? _ProfileName : response.ResponseAttribution();
const auto badgeUriPath = _lmProvider ? brandingData.BadgeIconPath() : winrt::hstring{};
const auto badgeUriPath = brandingData ? brandingData.BadgeIconPath() : L"";
const auto responseGroupedMessages = winrt::make<GroupedChatMessages>(time, false, winrt::single_threaded_vector(std::move(messageParts)), responseAttribution, badgeUriPath);
_messages.Append(responseGroupedMessages);

Expand Down
25 changes: 15 additions & 10 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,18 +662,23 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::_HandleToggleAIChat(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (ExtensionPresenter().Visibility() == Visibility::Collapsed)
{
_loadQueryExtension();
ExtensionPresenter().Visibility(Visibility::Visible);
_extensionPalette.Visibility(Visibility::Visible);
}
else
args.Handled(false);
// only handle this if the feature is allowed
if (WI_IsAnyFlagSet(AIConfig::AllowedLMProviders(), EnabledLMProviders::All))
{
_extensionPalette.Visibility(Visibility::Collapsed);
ExtensionPresenter().Visibility(Visibility::Collapsed);
if (ExtensionPresenter().Visibility() == Visibility::Collapsed)
{
_loadQueryExtension();
ExtensionPresenter().Visibility(Visibility::Visible);
_extensionPalette.Visibility(Visibility::Visible);
}
else
{
_extensionPalette.Visibility(Visibility::Collapsed);
ExtensionPresenter().Visibility(Visibility::Collapsed);
}
args.Handled(true);
}
args.Handled(true);
}

void TerminalPage::_HandleSetColorScheme(const IInspectable& /*sender*/,
Expand Down
144 changes: 75 additions & 69 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,58 +989,61 @@ namespace winrt::TerminalApp::implementation
_SetAcceleratorForMenuItem(commandPaletteFlyout, commandPaletteKeyChord);
}

// Create the AI chat button.
auto AIChatFlyout = WUX::Controls::MenuFlyoutItem{};
AIChatFlyout.Text(RS_(L"AIChatMenuItem"));
const auto AIChatToolTip = RS_(L"AIChatToolTip");

WUX::Controls::ToolTipService::SetToolTip(AIChatFlyout, box_value(AIChatToolTip));
Automation::AutomationProperties::SetHelpText(AIChatFlyout, AIChatToolTip);

// BODGY
// Manually load this icon from an SVG path; it is ironically much more humane this way.
// The XAML resource loader can't resolve theme-light/theme-dark for us, for... well, reasons.
// But also, you can't load a PathIcon with a *string* using the WinRT API... well. Reasons.
{
static constexpr wil::zwstring_view pathSVG{
L"m11.799 0c1.4358 0 2.5997 1.1639 2.5997 2.5997"
"v4.6161c-0.3705-0.2371-0.7731-0.42843-1.1998-0.56618"
"v-2.2501h-11.999v7.3991c0 0.7731 0.62673 1.3999 1.3998 1.3999"
"h4.0503c0.06775 0.2097 0.14838 0.4137 0.24109 0.6109l-0.17934 0.5889"
"h-4.1121c-1.4358 0-2.5997-1.1639-2.5997-2.5997"
"v-9.1989c0-1.4358 1.1639-2.5997 2.5997-2.5997"
"h9.1989zm0 1.1999h-9.1989c-0.77311 0-1.3998 0.62673-1.3998 1.3998"
"v0.59993h11.999v-0.59993c0-0.77311-0.6267-1.3998-1.3999-1.3998"
"zm1.3999 6.2987c0.4385 0.1711 0.8428 0.41052 1.1998 0.70512 0.9782 "
"0.80711 1.6017 2.0287 1.6017 3.3959 0 2.4304-1.9702 4.4005-4.4005 "
"4.4005-0.7739 0-1.5013-0.1998-2.1332-0.5508l-1.7496 0.5325c-0.30612 "
"0.0931-0.59233-0.1931-0.49914-0.4993l0.53258-1.749c-0.35108-0.6321-0.55106-1.3596-0.55106-2.1339 "
"0-2.3834 1.8949-4.3243 4.2604-4.3983 0.0395-0.0012 0.0792-0.00192 "
"0.1191-0.00208 0.0069-8e-5 0.0139-8e-5 0.0208-8e-5 0.5641 0 1.1034 "
"0.10607 1.599 0.2994zm0.0012 3.701c0.2209 0 0.4-0.1791 0.4-0.4 "
"0-0.221-0.1791-0.4001-0.4-0.4001h-3.2003c-0.22094 0-0.40003 0.1791-0.40003 "
"0.4001 0 0.2209 0.17909 0.4 0.40003 0.4h3.2003zm-3.2003 1.6001h1.6001c0.221 "
"0 0.4001-0.1791 0.4001-0.4s-0.1791-0.4-0.4001-0.4h-1.6001c-0.22094 0-0.40003 "
"0.1791-0.40003 0.4s0.17909 0.4 0.40003 0.4z"
};
try
// Create the AI chat button if AI features are allowed
if (WI_IsAnyFlagSet(_settings.GlobalSettings().AIInfo().AllowedLMProviders(), EnabledLMProviders::All))
{
auto AIChatFlyout = WUX::Controls::MenuFlyoutItem{};
AIChatFlyout.Text(RS_(L"AIChatMenuItem"));
const auto AIChatToolTip = RS_(L"AIChatToolTip");

WUX::Controls::ToolTipService::SetToolTip(AIChatFlyout, box_value(AIChatToolTip));
Automation::AutomationProperties::SetHelpText(AIChatFlyout, AIChatToolTip);

// BODGY
// Manually load this icon from an SVG path; it is ironically much more humane this way.
// The XAML resource loader can't resolve theme-light/theme-dark for us, for... well, reasons.
// But also, you can't load a PathIcon with a *string* using the WinRT API... well. Reasons.
{
hstring hsPathSVG{ pathSVG };
auto geometry = Markup::XamlBindingHelper::ConvertValue(winrt::xaml_typename<WUX::Media::Geometry>(), winrt::box_value(hsPathSVG));
WUX::Controls::PathIcon pathIcon;
pathIcon.Data(geometry.try_as<WUX::Media::Geometry>());
AIChatFlyout.Icon(pathIcon);
static constexpr wil::zwstring_view pathSVG{
L"m11.799 0c1.4358 0 2.5997 1.1639 2.5997 2.5997"
"v4.6161c-0.3705-0.2371-0.7731-0.42843-1.1998-0.56618"
"v-2.2501h-11.999v7.3991c0 0.7731 0.62673 1.3999 1.3998 1.3999"
"h4.0503c0.06775 0.2097 0.14838 0.4137 0.24109 0.6109l-0.17934 0.5889"
"h-4.1121c-1.4358 0-2.5997-1.1639-2.5997-2.5997"
"v-9.1989c0-1.4358 1.1639-2.5997 2.5997-2.5997"
"h9.1989zm0 1.1999h-9.1989c-0.77311 0-1.3998 0.62673-1.3998 1.3998"
"v0.59993h11.999v-0.59993c0-0.77311-0.6267-1.3998-1.3999-1.3998"
"zm1.3999 6.2987c0.4385 0.1711 0.8428 0.41052 1.1998 0.70512 0.9782 "
"0.80711 1.6017 2.0287 1.6017 3.3959 0 2.4304-1.9702 4.4005-4.4005 "
"4.4005-0.7739 0-1.5013-0.1998-2.1332-0.5508l-1.7496 0.5325c-0.30612 "
"0.0931-0.59233-0.1931-0.49914-0.4993l0.53258-1.749c-0.35108-0.6321-0.55106-1.3596-0.55106-2.1339 "
"0-2.3834 1.8949-4.3243 4.2604-4.3983 0.0395-0.0012 0.0792-0.00192 "
"0.1191-0.00208 0.0069-8e-5 0.0139-8e-5 0.0208-8e-5 0.5641 0 1.1034 "
"0.10607 1.599 0.2994zm0.0012 3.701c0.2209 0 0.4-0.1791 0.4-0.4 "
"0-0.221-0.1791-0.4001-0.4-0.4001h-3.2003c-0.22094 0-0.40003 0.1791-0.40003 "
"0.4001 0 0.2209 0.17909 0.4 0.40003 0.4h3.2003zm-3.2003 1.6001h1.6001c0.221 "
"0 0.4001-0.1791 0.4001-0.4s-0.1791-0.4-0.4001-0.4h-1.6001c-0.22094 0-0.40003 "
"0.1791-0.40003 0.4s0.17909 0.4 0.40003 0.4z"
};
try
{
hstring hsPathSVG{ pathSVG };
auto geometry = Markup::XamlBindingHelper::ConvertValue(winrt::xaml_typename<WUX::Media::Geometry>(), winrt::box_value(hsPathSVG));
WUX::Controls::PathIcon pathIcon;
pathIcon.Data(geometry.try_as<WUX::Media::Geometry>());
AIChatFlyout.Icon(pathIcon);
}
CATCH_LOG();
}
CATCH_LOG();
}

AIChatFlyout.Click({ this, &TerminalPage::_AIChatButtonOnClick });
newTabFlyout.Items().Append(AIChatFlyout);
AIChatFlyout.Click({ this, &TerminalPage::_AIChatButtonOnClick });
newTabFlyout.Items().Append(AIChatFlyout);

const auto AIChatKeyChord{ actionMap.GetKeyBindingForAction(L"Terminal.OpenTerminalChat") };
if (AIChatKeyChord)
{
_SetAcceleratorForMenuItem(AIChatFlyout, AIChatKeyChord);
const auto AIChatKeyChord{ actionMap.GetKeyBindingForAction(L"Terminal.OpenTerminalChat") };
if (AIChatKeyChord)
{
_SetAcceleratorForMenuItem(AIChatFlyout, AIChatKeyChord);
}
}

// Create the about button.
Expand Down Expand Up @@ -5755,31 +5758,34 @@ namespace winrt::TerminalApp::implementation
}
}

// we now have a provider of the correct type, update that
winrt::hstring newAuthValues = authValuesString;
if (newAuthValues.empty())
if (_lmProvider)
{
Windows::Data::Json::JsonObject authValuesJson;
const auto settingsAIInfo = _settings.GlobalSettings().AIInfo();
switch (providerType)
// we now have a provider of the correct type, update that
winrt::hstring newAuthValues = authValuesString;
if (newAuthValues.empty())
{
case LLMProvider::AzureOpenAI:
authValuesJson.SetNamedValue(L"endpoint", WDJ::JsonValue::CreateStringValue(settingsAIInfo.AzureOpenAIEndpoint()));
authValuesJson.SetNamedValue(L"key", WDJ::JsonValue::CreateStringValue(settingsAIInfo.AzureOpenAIKey()));
newAuthValues = authValuesJson.ToString();
break;
case LLMProvider::OpenAI:
authValuesJson.SetNamedValue(L"key", WDJ::JsonValue::CreateStringValue(settingsAIInfo.OpenAIKey()));
newAuthValues = authValuesJson.ToString();
break;
case LLMProvider::GithubCopilot:
newAuthValues = settingsAIInfo.GithubCopilotAuthValues();
break;
default:
break;
Windows::Data::Json::JsonObject authValuesJson;
const auto settingsAIInfo = _settings.GlobalSettings().AIInfo();
switch (providerType)
{
case LLMProvider::AzureOpenAI:
authValuesJson.SetNamedValue(L"endpoint", WDJ::JsonValue::CreateStringValue(settingsAIInfo.AzureOpenAIEndpoint()));
authValuesJson.SetNamedValue(L"key", WDJ::JsonValue::CreateStringValue(settingsAIInfo.AzureOpenAIKey()));
newAuthValues = authValuesJson.ToString();
break;
case LLMProvider::OpenAI:
authValuesJson.SetNamedValue(L"key", WDJ::JsonValue::CreateStringValue(settingsAIInfo.OpenAIKey()));
newAuthValues = authValuesJson.ToString();
break;
case LLMProvider::GithubCopilot:
newAuthValues = settingsAIInfo.GithubCopilotAuthValues();
break;
default:
break;
}
}
_lmProvider.SetAuthentication(newAuthValues);
}
_lmProvider.SetAuthentication(newAuthValues);

if (_extensionPalette)
{
Expand Down
9 changes: 7 additions & 2 deletions src/cascadia/TerminalSettingsEditor/AISettings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
Style="{StaticResource TextBlockSubHeaderStyle}" />
<!-- GitHub Copilot -->
<local:SettingContainer x:Uid="AISettings_GithubCopilot"
Style="{StaticResource ExpanderSettingContainerStyle}"
Visibility="{x:Bind ViewModel.GithubCopilotFeatureEnabled}">
CurrentValue="{x:Bind ViewModel.GithubCopilotStatus, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.GithubCopilotAllowed}"
Style="{StaticResource ExpanderSettingContainerStyle}">
<StackPanel>
<Grid Visibility="{x:Bind ViewModel.AreGithubCopilotTokensSet, Mode=OneWay}">
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -137,6 +138,8 @@
</local:SettingContainer>
<!-- Azure OpenAI -->
<local:SettingContainer x:Uid="AISettings_AzureOpenAIKeyAndEndpoint"
CurrentValue="{x:Bind ViewModel.AzureOpenAIStatus, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.AzureOpenAIAllowed}"
Style="{StaticResource ExpanderSettingContainerStyle}">
<StackPanel>
<Grid Visibility="{x:Bind ViewModel.AreAzureOpenAIKeyAndEndpointSet, Mode=OneWay}">
Expand Down Expand Up @@ -261,6 +264,8 @@
</local:SettingContainer>
<!-- OpenAI -->
<local:SettingContainer x:Uid="AISettings_OpenAIKey"
CurrentValue="{x:Bind ViewModel.OpenAIStatus, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.OpenAIAllowed}"
Style="{StaticResource ExpanderSettingContainerStyle}">
<StackPanel>
<Grid Visibility="{x:Bind ViewModel.IsOpenAIKeySet, Mode=OneWay}">
Expand Down
Loading

0 comments on commit 438621f

Please sign in to comment.