From 499dc9bb7a4727ef53a224530e4a096d1e5a7af3 Mon Sep 17 00:00:00 2001 From: Ani <115020168+drawbyperpetual@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:16:20 +0200 Subject: [PATCH] [AdvancedPaste]Check "Paste with AI" enabled state for enabling custom actions (#35026) * [AdvancedPaste] Check OpenAI enabled state for custom actions * Add some more explanations to the expected exception * Add description saying that it requires Paste with AI to be enabled * Check openAI enabled only if we have custom actions --------- Co-authored-by: Jaime Bernardo --- .../ViewModels/OptionsViewModel.cs | 3 +- .../AdvancedPasteModuleInterface/dllmain.cpp | 49 ++++++++++++++++--- .../SettingsXAML/Views/AdvancedPaste.xaml | 6 ++- .../Settings.UI/Strings/en-us/Resources.resw | 5 +- .../ViewModels/AdvancedPasteViewModel.cs | 2 + 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index def4d89d11..141561543e 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -94,6 +94,7 @@ namespace AdvancedPaste.ViewModels ClipboardHistoryEnabled = IsClipboardHistoryEnabled(); ReadClipboard(); + UpdateAllowedByGPO(); _clipboardTimer = new() { Interval = TimeSpan.FromSeconds(1) }; _clipboardTimer.Tick += ClipboardTimer_Tick; _clipboardTimer.Start(); @@ -462,7 +463,7 @@ namespace AdvancedPaste.ViewModels { Logger.LogTrace(); - if (string.IsNullOrWhiteSpace(inputInstructions)) + if (string.IsNullOrWhiteSpace(inputInstructions) || !IsCustomAIEnabled) { return string.Empty; } diff --git a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp index 261fa43932..eae8fc0b85 100644 --- a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp +++ b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp @@ -13,7 +13,9 @@ #include #include #include +#include +#include #include #include #include @@ -54,6 +56,9 @@ namespace const wchar_t JSON_KEY_PASTE_AS_JSON_HOTKEY[] = L"paste-as-json-hotkey"; const wchar_t JSON_KEY_SHOW_CUSTOM_PREVIEW[] = L"ShowCustomPreview"; const wchar_t JSON_KEY_VALUE[] = L"value"; + + const wchar_t OPENAI_VAULT_RESOURCE[] = L"https://platform.openai.com/api-keys"; + const wchar_t OPENAI_VAULT_USERNAME[] = L"PowerToys_AdvancedPaste_OpenAIKey"; } class AdvancedPaste : public PowertoyModuleIface @@ -133,6 +138,34 @@ private: return jsonObject; } + static bool open_ai_key_exists() + { + try + { + winrt::Windows::Security::Credentials::PasswordVault vault; + return vault.Retrieve(OPENAI_VAULT_RESOURCE, OPENAI_VAULT_USERNAME) != nullptr; + } + catch (const winrt::hresult_error& ex) + { + // Looks like the only way to access the PasswordVault is through the an API that throws an exception in case the resource doesn't exist. + // If the compiler breaks here when you're debugging, just continue. + // If you want to disable breaking here in a more permanent way, just add a condition in Visual Studio's Exception Settings to not break on win::hresult_error, but that might make you not hit other exceptions you might want to catch. + if (ex.code() == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) + { + return false; // Credential doesn't exist. + } + Logger::error("Unexpected error while retrieving OpenAI key from vault: {}", winrt::to_string(ex.message())); + return false; + } + } + + bool is_open_ai_enabled() + { + return gpo_policy_enabled_configuration() != powertoys_gpo::gpo_rule_configured_disabled && + powertoys_gpo::getAllowedAdvancedPasteOnlineAIModelsValue() != powertoys_gpo::gpo_rule_configured_disabled && + open_ai_key_exists(); + } + bool migrate_data_and_remove_data_file(Hotkey& old_paste_as_plain_hotkey) { const wchar_t OLD_JSON_KEY_ACTIVATION_SHORTCUT[] = L"ActivationShortcut"; @@ -216,15 +249,17 @@ private: if (propertiesObject.HasKey(JSON_KEY_CUSTOM_ACTIONS)) { const auto customActions = propertiesObject.GetNamedObject(JSON_KEY_CUSTOM_ACTIONS).GetNamedArray(JSON_KEY_VALUE); - - for (const auto& customAction : customActions) + if (customActions.Size() > 0 && is_open_ai_enabled()) { - const auto object = customAction.GetObjectW(); - - if (object.GetNamedBoolean(JSON_KEY_IS_SHOWN, false)) + for (const auto& customAction : customActions) { - m_custom_action_hotkeys.push_back(parse_single_hotkey(object.GetNamedObject(JSON_KEY_SHORTCUT))); - m_custom_action_ids.push_back(static_cast(object.GetNamedNumber(JSON_KEY_ID))); + const auto object = customAction.GetObjectW(); + + if (object.GetNamedBoolean(JSON_KEY_IS_SHOWN, false)) + { + m_custom_action_hotkeys.push_back(parse_single_hotkey(object.GetNamedObject(JSON_KEY_SHORTCUT))); + m_custom_action_ids.push_back(static_cast(object.GetNamedNumber(JSON_KEY_ID))); + } } } } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml index ed7a634388..1598beecc8 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml @@ -102,7 +102,10 @@ - +