[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

@@ -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)
{
}
}
}
}