From 8dfa3ae8fa0d6e83f320ac5cc9eb3a425e07d31b Mon Sep 17 00:00:00 2001 From: "Shawn Yuan (from Dev Box)" Date: Fri, 14 Nov 2025 15:01:44 +0800 Subject: [PATCH] remove unused property Signed-off-by: Shawn Yuan (from Dev Box) --- .../AdvancedPaste/Helpers/UserSettings.cs | 10 +- .../AIProviderConfigurationSnapshot.cs | 29 +--- .../AdvancedPasteMigrationHelper.cs | 136 +++--------------- .../PasteAIConfiguration.cs | 19 +-- .../ViewModels/AdvancedPasteViewModel.cs | 26 +--- 5 files changed, 25 insertions(+), 195 deletions(-) diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs index b43944b5eb..5eaae4dd3e 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs @@ -176,25 +176,19 @@ namespace AdvancedPaste.Settings properties.PasteAIConfiguration = configuration; } - bool hasLegacyProviders = configuration.LegacyProviderConfigurations is { Count: > 0 }; bool legacyAdvancedAIConsumed = properties.TryConsumeLegacyAdvancedAIEnabled(out var advancedFlag); bool legacyAdvancedAIEnabled = legacyAdvancedAIConsumed && advancedFlag; PasswordCredential legacyCredential = TryGetLegacyOpenAICredential(); - if (!hasLegacyProviders && legacyCredential is null && !legacyAdvancedAIConsumed) + if (legacyCredential is null && !legacyAdvancedAIConsumed) { return false; } bool configurationUpdated = false; - if (hasLegacyProviders) - { - configurationUpdated |= AdvancedPasteMigrationHelper.MigrateLegacyProviderConfigurations(configuration); - } - PasteAIProviderDefinition openAIProvider = null; - if (legacyCredential is not null || hasLegacyProviders || legacyAdvancedAIConsumed) + if (legacyCredential is not null || legacyAdvancedAIConsumed) { var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration); openAIProvider = ensureResult.Provider; diff --git a/src/settings-ui/Settings.UI.Library/AIProviderConfigurationSnapshot.cs b/src/settings-ui/Settings.UI.Library/AIProviderConfigurationSnapshot.cs index 456632545f..7619c72ccd 100644 --- a/src/settings-ui/Settings.UI.Library/AIProviderConfigurationSnapshot.cs +++ b/src/settings-ui/Settings.UI.Library/AIProviderConfigurationSnapshot.cs @@ -2,34 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Text.Json.Serialization; - namespace Microsoft.PowerToys.Settings.UI.Library { - /// - /// Stores provider-specific configuration overrides so each AI service can keep distinct settings. - /// - public class AIProviderConfigurationSnapshot - { - [JsonPropertyName("model-name")] - public string ModelName { get; set; } = string.Empty; - - [JsonPropertyName("endpoint-url")] - public string EndpointUrl { get; set; } = string.Empty; - - [JsonPropertyName("api-version")] - public string ApiVersion { get; set; } = string.Empty; - - [JsonPropertyName("deployment-name")] - public string DeploymentName { get; set; } = string.Empty; - - [JsonPropertyName("model-path")] - public string ModelPath { get; set; } = string.Empty; - - [JsonPropertyName("system-prompt")] - public string SystemPrompt { get; set; } = string.Empty; - - [JsonPropertyName("moderation-enabled")] - public bool ModerationEnabled { get; set; } = true; - } + // Intentionally left empty after removing legacy provider snapshots. } diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPasteMigrationHelper.cs b/src/settings-ui/Settings.UI.Library/AdvancedPasteMigrationHelper.cs index 8a606ad83a..b61f83dd3d 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPasteMigrationHelper.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPasteMigrationHelper.cs @@ -13,56 +13,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library /// public static class AdvancedPasteMigrationHelper { - /// - /// Moves legacy provider configuration snapshots into the strongly-typed providers collection. - /// - /// The configuration instance to migrate. - /// True if the configuration was modified. - public static bool MigrateLegacyProviderConfigurations(PasteAIConfiguration configuration) - { - if (configuration is null) - { - return false; - } - - configuration.Providers ??= new ObservableCollection(); - - bool configurationUpdated = false; - - if (configuration.LegacyProviderConfigurations is { Count: > 0 }) - { - foreach (var entry in configuration.LegacyProviderConfigurations) - { - var result = EnsureProvider(configuration, entry.Key, entry.Value); - configurationUpdated |= result.Updated; - } - - configuration.LegacyProviderConfigurations = null; - } - - configurationUpdated |= EnsureActiveProviderIsValid(configuration); - - return configurationUpdated; - } - /// /// Ensures an OpenAI provider exists in the configuration, creating one if necessary. /// /// The configuration instance. /// The ensured provider and a flag indicating whether changes were made. public static (PasteAIProviderDefinition Provider, bool Updated) EnsureOpenAIProvider(PasteAIConfiguration configuration) - { - return EnsureProvider(configuration, AIServiceType.OpenAI.ToConfigurationString(), null); - } - - /// - /// Ensures a provider for the supplied service type exists, optionally applying a legacy snapshot. - /// - /// The configuration instance. - /// The persisted service type key. - /// An optional snapshot containing legacy values. - /// The ensured provider and whether the configuration was updated. - public static (PasteAIProviderDefinition Provider, bool Updated) EnsureProvider(PasteAIConfiguration configuration, string serviceTypeKey, AIProviderConfigurationSnapshot snapshot) { if (configuration is null) { @@ -71,101 +27,45 @@ namespace Microsoft.PowerToys.Settings.UI.Library configuration.Providers ??= new ObservableCollection(); - var normalizedServiceType = NormalizeServiceType(serviceTypeKey); - var existingProvider = configuration.Providers.FirstOrDefault(provider => string.Equals(provider.ServiceType, normalizedServiceType, StringComparison.OrdinalIgnoreCase)); - bool configurationUpdated = false; + const string serviceTypeKey = "OpenAI"; + var existingProvider = configuration.Providers.FirstOrDefault(provider => string.Equals(provider.ServiceType, serviceTypeKey, StringComparison.OrdinalIgnoreCase)); + bool updated = false; if (existingProvider is null) { - existingProvider = CreateProvider(normalizedServiceType, snapshot); + existingProvider = CreateProvider(serviceTypeKey); configuration.Providers.Add(existingProvider); - configurationUpdated = true; - } - else if (snapshot is not null) - { - configurationUpdated |= ApplySnapshot(existingProvider, snapshot); + updated = true; } - configurationUpdated |= EnsureActiveProviderIsValid(configuration, existingProvider); + updated |= EnsureActiveProviderIsValid(configuration, existingProvider); - return (existingProvider, configurationUpdated); + return (existingProvider, updated); } - private static string NormalizeServiceType(string serviceTypeKey) - { - var serviceType = serviceTypeKey.ToAIServiceType(); - return serviceType.ToConfigurationString(); - } - - private static PasteAIProviderDefinition CreateProvider(string serviceTypeKey, AIProviderConfigurationSnapshot snapshot) + /// + /// Creates a provider with default values for the requested service type. + /// + private static PasteAIProviderDefinition CreateProvider(string serviceTypeKey) { var serviceType = serviceTypeKey.ToAIServiceType(); var metadata = AIServiceTypeRegistry.GetMetadata(serviceType); var provider = new PasteAIProviderDefinition { ServiceType = serviceTypeKey, - ModelName = !string.IsNullOrWhiteSpace(snapshot?.ModelName) ? snapshot.ModelName : PasteAIProviderDefaults.GetDefaultModelName(serviceType), - EndpointUrl = snapshot?.EndpointUrl ?? string.Empty, - ApiVersion = snapshot?.ApiVersion ?? string.Empty, - DeploymentName = snapshot?.DeploymentName ?? string.Empty, - ModelPath = snapshot?.ModelPath ?? string.Empty, - SystemPrompt = snapshot?.SystemPrompt ?? string.Empty, - ModerationEnabled = snapshot?.ModerationEnabled ?? true, + ModelName = PasteAIProviderDefaults.GetDefaultModelName(serviceType), + EndpointUrl = string.Empty, + ApiVersion = string.Empty, + DeploymentName = string.Empty, + ModelPath = string.Empty, + SystemPrompt = string.Empty, + ModerationEnabled = serviceType == AIServiceType.OpenAI, IsLocalModel = metadata.IsLocalModel, }; return provider; } - private static bool ApplySnapshot(PasteAIProviderDefinition provider, AIProviderConfigurationSnapshot snapshot) - { - bool updated = false; - - if (!string.IsNullOrWhiteSpace(snapshot.ModelName) && !string.Equals(provider.ModelName, snapshot.ModelName, StringComparison.Ordinal)) - { - provider.ModelName = snapshot.ModelName; - updated = true; - } - - if (!string.IsNullOrWhiteSpace(snapshot.EndpointUrl) && !string.Equals(provider.EndpointUrl, snapshot.EndpointUrl, StringComparison.Ordinal)) - { - provider.EndpointUrl = snapshot.EndpointUrl; - updated = true; - } - - if (!string.IsNullOrWhiteSpace(snapshot.ApiVersion) && !string.Equals(provider.ApiVersion, snapshot.ApiVersion, StringComparison.Ordinal)) - { - provider.ApiVersion = snapshot.ApiVersion; - updated = true; - } - - if (!string.IsNullOrWhiteSpace(snapshot.DeploymentName) && !string.Equals(provider.DeploymentName, snapshot.DeploymentName, StringComparison.Ordinal)) - { - provider.DeploymentName = snapshot.DeploymentName; - updated = true; - } - - if (!string.IsNullOrWhiteSpace(snapshot.ModelPath) && !string.Equals(provider.ModelPath, snapshot.ModelPath, StringComparison.Ordinal)) - { - provider.ModelPath = snapshot.ModelPath; - updated = true; - } - - if (!string.IsNullOrWhiteSpace(snapshot.SystemPrompt) && !string.Equals(provider.SystemPrompt, snapshot.SystemPrompt, StringComparison.Ordinal)) - { - provider.SystemPrompt = snapshot.SystemPrompt; - updated = true; - } - - if (provider.ModerationEnabled != snapshot.ModerationEnabled) - { - provider.ModerationEnabled = snapshot.ModerationEnabled; - updated = true; - } - - return updated; - } - private static bool EnsureActiveProviderIsValid(PasteAIConfiguration configuration, PasteAIProviderDefinition preferredProvider = null) { if (configuration?.Providers is null || configuration.Providers.Count == 0) diff --git a/src/settings-ui/Settings.UI.Library/PasteAIConfiguration.cs b/src/settings-ui/Settings.UI.Library/PasteAIConfiguration.cs index 3ed0937280..0075a53b8d 100644 --- a/src/settings-ui/Settings.UI.Library/PasteAIConfiguration.cs +++ b/src/settings-ui/Settings.UI.Library/PasteAIConfiguration.cs @@ -20,8 +20,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library { private string _activeProviderId = string.Empty; private ObservableCollection _providers = new(); - private bool _useSharedCredentials = true; - private Dictionary _legacyProviderConfigurations; public event PropertyChangedEventHandler PropertyChanged; @@ -38,22 +36,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library get => _providers; set => SetProperty(ref _providers, value ?? new ObservableCollection()); } - - [JsonPropertyName("use-shared-credentials")] - public bool UseSharedCredentials - { - get => _useSharedCredentials; - set => SetProperty(ref _useSharedCredentials, value); - } - - [JsonPropertyName("provider-configurations")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public Dictionary LegacyProviderConfigurations - { - get => _legacyProviderConfigurations; - set => _legacyProviderConfigurations = value; - } - + [JsonIgnore] public PasteAIProviderDefinition ActiveProvider { diff --git a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs index d10bd77178..4a4f5e9bab 100644 --- a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs @@ -198,23 +198,17 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels properties.PasteAIConfiguration = configuration; } - bool hasLegacyProviders = configuration.LegacyProviderConfigurations is { Count: > 0 }; PasswordCredential legacyCredential = TryGetLegacyOpenAICredential(); - if (!hasLegacyProviders && legacyCredential is null && !legacyAdvancedAIConsumed) + if (legacyCredential is null && !legacyAdvancedAIConsumed) { return; } bool configurationUpdated = false; - if (hasLegacyProviders) - { - configurationUpdated |= AdvancedPasteMigrationHelper.MigrateLegacyProviderConfigurations(configuration); - } - PasteAIProviderDefinition openAIProvider = null; - if (legacyCredential is not null || hasLegacyProviders || legacyAdvancedAIConsumed) + if (legacyCredential is not null || legacyAdvancedAIConsumed) { var ensureResult = AdvancedPasteMigrationHelper.EnsureOpenAIProvider(configuration); openAIProvider = ensureResult.Provider; @@ -1248,11 +1242,6 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels return true; } - if (current.UseSharedCredentials != incoming.UseSharedCredentials) - { - return true; - } - var currentProviders = current.Providers ?? new ObservableCollection(); var incomingProviders = incoming.Providers ?? new ObservableCollection(); @@ -1408,8 +1397,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels return; } - if (string.Equals(e.PropertyName, nameof(PasteAIConfiguration.ActiveProviderId), StringComparison.Ordinal) - || string.Equals(e.PropertyName, nameof(PasteAIConfiguration.UseSharedCredentials), StringComparison.Ordinal)) + if (string.Equals(e.PropertyName, nameof(PasteAIConfiguration.ActiveProviderId), StringComparison.Ordinal)) { SaveAndNotifySettings(); } @@ -1426,15 +1414,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels pasteConfig.Providers ??= new ObservableCollection(); - bool configurationUpdated = AdvancedPasteMigrationHelper.MigrateLegacyProviderConfigurations(pasteConfig); - SubscribeToPasteAIProviders(pasteConfig); - - if (configurationUpdated) - { - SaveAndNotifySettings(); - OnPropertyChanged(nameof(PasteAIConfiguration)); - } } private static string RetrieveCredentialValue(string credentialResource, string credentialUserName)