PasteAIConfiguration.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.Generic; 7 using System.Collections.ObjectModel; 8 using System.ComponentModel; 9 using System.Linq; 10 using System.Runtime.CompilerServices; 11 using System.Text.Json; 12 using System.Text.Json.Serialization; 13 14 namespace Microsoft.PowerToys.Settings.UI.Library 15 { 16 /// <summary> 17 /// Configuration for Paste AI features (custom action transformations like custom prompt processing) 18 /// </summary> 19 public class PasteAIConfiguration : INotifyPropertyChanged 20 { 21 private string _activeProviderId = string.Empty; 22 private ObservableCollection<PasteAIProviderDefinition> _providers = new(); 23 24 public event PropertyChangedEventHandler PropertyChanged; 25 26 [JsonPropertyName("active-provider-id")] 27 public string ActiveProviderId 28 { 29 get => _activeProviderId; 30 set => SetProperty(ref _activeProviderId, value ?? string.Empty); 31 } 32 33 [JsonPropertyName("providers")] 34 public ObservableCollection<PasteAIProviderDefinition> Providers 35 { 36 get => _providers; 37 set => SetProperty(ref _providers, value ?? new ObservableCollection<PasteAIProviderDefinition>()); 38 } 39 40 [JsonIgnore] 41 public PasteAIProviderDefinition ActiveProvider 42 { 43 get 44 { 45 if (_providers is null || _providers.Count == 0) 46 { 47 return null; 48 } 49 50 if (!string.IsNullOrWhiteSpace(_activeProviderId)) 51 { 52 var match = _providers.FirstOrDefault(provider => string.Equals(provider.Id, _activeProviderId, StringComparison.OrdinalIgnoreCase)); 53 if (match is not null) 54 { 55 return match; 56 } 57 } 58 59 return _providers[0]; 60 } 61 } 62 63 [JsonIgnore] 64 public AIServiceType ActiveServiceTypeKind => ActiveProvider?.ServiceTypeKind ?? AIServiceType.OpenAI; 65 66 public override string ToString() 67 => JsonSerializer.Serialize(this, SettingsSerializationContext.Default.PasteAIConfiguration); 68 69 protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) 70 { 71 if (EqualityComparer<T>.Default.Equals(field, value)) 72 { 73 return false; 74 } 75 76 field = value; 77 OnPropertyChanged(propertyName); 78 return true; 79 } 80 81 protected void OnPropertyChanged(string propertyName) 82 { 83 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 84 } 85 } 86 }