[GPO][AdvPaste]Online AI models GPO (#33045)

* [Advanced Paste] AI gpo

* address PR comments

* XAML format

* Fix showing Enable Paste with AI with module disabled

* Rename variable in ViewModel for clarity

* Update adml revision

* Move policy registry key around

* Update src/modules/AdvancedPaste/AdvancedPaste/Strings/en-us/Resources.resw

Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>

---------

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>
This commit is contained in:
Stefan Markovic
2024-05-26 13:22:50 +02:00
committed by GitHub
parent 8bb5a33572
commit d0d2f3cd9c
11 changed files with 126 additions and 38 deletions

View File

@@ -45,7 +45,7 @@
Severity="Informational" />
<controls:SettingsGroup x:Uid="AdvancedPaste_EnableAISettingsGroup" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<tkcontrols:SettingsCard x:Uid="AdvancedPaste_EnableAISettingsCard">
<tkcontrols:SettingsCard x:Uid="AdvancedPaste_EnableAISettingsCard" IsEnabled="{x:Bind ViewModel.IsOnlineAIModelsDisallowedByGPO, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
<tkcontrols:SettingsCard.HeaderIcon>
<PathIcon Data="M128 766q0-42 24-77t65-48l178-57q32-11 61-30t52-42q50-50 71-114l58-179q13-40 48-65t78-26q42 0 77 24t50 65l58 177q21 66 72 117 49 50 117 72l176 58q43 14 69 48t26 80q0 41-25 76t-64 49l-178 58q-66 21-117 72-32 32-51 73t-33 84-26 83-30 73-45 51-71 20q-42 0-77-24t-49-65l-58-178q-8-25-19-47t-28-43q-34-43-77-68t-89-41-89-27-78-29-55-45-21-75zm1149 7q-76-29-145-53t-129-60-104-88-73-138l-57-176-67 176q-18 48-42 89t-60 78q-34 34-76 61t-89 43l-177 57q75 29 144 53t127 60 103 89 73 137l57 176 67-176q37-97 103-168t168-103l177-57zm-125 759q0-31 20-57t49-36l99-32q34-11 53-34t30-51 20-59 20-54 33-41 58-16q32 0 59 19t38 50q6 20 11 40t13 40 17 38 25 34q16 17 39 26t48 18 49 16 44 20 31 32 12 50q0 33-18 60t-51 38q-19 6-39 11t-41 13-39 17-34 25q-24 25-35 62t-24 73-35 61-68 25q-32 0-59-19t-38-50q-6-18-11-39t-13-41-17-40-24-33q-18-17-41-27t-47-17-49-15-43-20-30-33-12-54zm583 4q-43-13-74-30t-55-41-40-55-32-74q-12 41-29 72t-42 55-55 42-71 31q81 23 128 71t71 129q15-43 31-74t40-54 53-40 75-32z" />
</tkcontrols:SettingsCard.HeaderIcon>
@@ -73,6 +73,12 @@
IsEnabled="{x:Bind ViewModel.IsOpenAIEnabled, Mode=OneWay}">
<ToggleSwitch IsOn="{x:Bind ViewModel.ShowCustomPreview, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<InfoBar
x:Uid="GPO_SettingIsManaged"
IsClosable="False"
IsOpen="{x:Bind ViewModel.ShowOnlineAIModelsGpoConfiguredInfoBar, Mode=OneWay}"
IsTabStop="{x:Bind ViewModel.ShowOnlineAIModelsGpoConfiguredInfoBar, Mode=OneWay}"
Severity="Informational" />
</controls:SettingsGroup>
<controls:SettingsGroup x:Uid="AdvancedPaste_ClipboardHistorySettingsGroup" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">

View File

@@ -32,6 +32,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private GpoRuleConfigured _onlineAIModelsGpoRuleConfiguration;
private bool _onlineAIModelsDisallowedByGPO;
private bool _isEnabled;
private Func<string, int> SendConfigMSG { get; }
@@ -80,6 +82,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
_isEnabled = GeneralSettingsConfig.Enabled.AdvancedPaste;
}
_onlineAIModelsGpoRuleConfiguration = GPOWrapper.GetAllowedAdvancedPasteOnlineAIModelsValue();
if (_onlineAIModelsGpoRuleConfiguration == GpoRuleConfigured.Disabled)
{
_onlineAIModelsDisallowedByGPO = true;
// disable AI if it was enabled
DisableAI();
}
}
public bool IsEnabled
@@ -124,13 +135,23 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
return cred is not null;
}
public bool IsOpenAIEnabled => OpenAIKeyExists();
public bool IsOpenAIEnabled => OpenAIKeyExists() && !IsOnlineAIModelsDisallowedByGPO;
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
}
public bool IsOnlineAIModelsDisallowedByGPO
{
get => _onlineAIModelsDisallowedByGPO || _enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled;
}
public bool ShowOnlineAIModelsGpoConfiguredInfoBar
{
get => _onlineAIModelsDisallowedByGPO && _enabledGpoRuleConfiguration != GpoRuleConfigured.Disabled;
}
private bool IsClipboardHistoryEnabled()
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\";
@@ -334,18 +355,30 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
internal void DisableAI()
{
PasswordVault vault = new PasswordVault();
PasswordCredential cred = vault.Retrieve("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey");
vault.Remove(cred);
OnPropertyChanged(nameof(IsOpenAIEnabled));
try
{
PasswordVault vault = new PasswordVault();
PasswordCredential cred = vault.Retrieve("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey");
vault.Remove(cred);
OnPropertyChanged(nameof(IsOpenAIEnabled));
}
catch (Exception)
{
}
}
internal void EnableAI(string password)
{
PasswordVault vault = new PasswordVault();
PasswordCredential cred = new PasswordCredential("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey", password);
vault.Add(cred);
OnPropertyChanged(nameof(IsOpenAIEnabled));
try
{
PasswordVault vault = new PasswordVault();
PasswordCredential cred = new PasswordCredential("https://platform.openai.com/api-keys", "PowerToys_AdvancedPaste_OpenAIKey", password);
vault.Add(cred);
OnPropertyChanged(nameof(IsOpenAIEnabled));
}
catch (Exception)
{
}
}
}
}