/ src / settings-ui / Settings.UI.Library / AdvancedPasteMigrationHelper.cs
AdvancedPasteMigrationHelper.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Collections.ObjectModel;
  7  using System.Linq;
  8  
  9  namespace Microsoft.PowerToys.Settings.UI.Library
 10  {
 11      /// <summary>
 12      /// Helper methods for migrating legacy Advanced Paste settings to the updated schema.
 13      /// </summary>
 14      public static class AdvancedPasteMigrationHelper
 15      {
 16          /// <summary>
 17          /// Ensures an OpenAI provider exists in the configuration, creating one if necessary.
 18          /// </summary>
 19          /// <param name="configuration">The configuration instance.</param>
 20          /// <returns>The ensured provider and a flag indicating whether changes were made.</returns>
 21          public static (PasteAIProviderDefinition Provider, bool Updated) EnsureOpenAIProvider(PasteAIConfiguration configuration)
 22          {
 23              if (configuration is null)
 24              {
 25                  return (null, false);
 26              }
 27  
 28              configuration.Providers ??= new ObservableCollection<PasteAIProviderDefinition>();
 29  
 30              const string serviceTypeKey = "OpenAI";
 31              var existingProvider = configuration.Providers.FirstOrDefault(provider => string.Equals(provider.ServiceType, serviceTypeKey, StringComparison.OrdinalIgnoreCase));
 32              bool updated = false;
 33  
 34              if (existingProvider is null)
 35              {
 36                  existingProvider = CreateProvider(serviceTypeKey);
 37                  configuration.Providers.Add(existingProvider);
 38                  updated = true;
 39              }
 40  
 41              updated |= EnsureActiveProviderIsValid(configuration, existingProvider);
 42  
 43              return (existingProvider, updated);
 44          }
 45  
 46          /// <summary>
 47          /// Creates a provider with default values for the requested service type.
 48          /// </summary>
 49          private static PasteAIProviderDefinition CreateProvider(string serviceTypeKey)
 50          {
 51              var serviceType = serviceTypeKey.ToAIServiceType();
 52              var metadata = AIServiceTypeRegistry.GetMetadata(serviceType);
 53              var provider = new PasteAIProviderDefinition
 54              {
 55                  ServiceType = serviceTypeKey,
 56                  ModelName = PasteAIProviderDefaults.GetDefaultModelName(serviceType),
 57                  EndpointUrl = string.Empty,
 58                  ApiVersion = string.Empty,
 59                  DeploymentName = string.Empty,
 60                  ModelPath = string.Empty,
 61                  SystemPrompt = string.Empty,
 62                  ModerationEnabled = serviceType == AIServiceType.OpenAI,
 63                  IsLocalModel = metadata.IsLocalModel,
 64              };
 65  
 66              return provider;
 67          }
 68  
 69          private static bool EnsureActiveProviderIsValid(PasteAIConfiguration configuration, PasteAIProviderDefinition preferredProvider = null)
 70          {
 71              if (configuration?.Providers is null || configuration.Providers.Count == 0)
 72              {
 73                  if (!string.IsNullOrWhiteSpace(configuration?.ActiveProviderId))
 74                  {
 75                      configuration.ActiveProviderId = string.Empty;
 76                      return true;
 77                  }
 78  
 79                  return false;
 80              }
 81  
 82              bool updated = false;
 83  
 84              var activeProvider = configuration.Providers.FirstOrDefault(provider => string.Equals(provider.Id, configuration.ActiveProviderId, StringComparison.OrdinalIgnoreCase));
 85              if (activeProvider is null)
 86              {
 87                  activeProvider = preferredProvider ?? configuration.Providers.First();
 88                  configuration.ActiveProviderId = activeProvider.Id;
 89                  updated = true;
 90              }
 91  
 92              foreach (var provider in configuration.Providers)
 93              {
 94                  bool shouldBeActive = string.Equals(provider.Id, configuration.ActiveProviderId, StringComparison.OrdinalIgnoreCase);
 95                  if (provider.IsActive != shouldBeActive)
 96                  {
 97                      provider.IsActive = shouldBeActive;
 98                      updated = true;
 99                  }
100              }
101  
102              return updated;
103          }
104      }
105  }