PasteAIProviderDefinition.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.ComponentModel; 8 using System.Runtime.CompilerServices; 9 using System.Text.Json.Serialization; 10 11 namespace Microsoft.PowerToys.Settings.UI.Library 12 { 13 /// <summary> 14 /// Represents a single Paste AI provider configuration entry. 15 /// </summary> 16 public class PasteAIProviderDefinition : INotifyPropertyChanged 17 { 18 private string _id = Guid.NewGuid().ToString("N"); 19 private string _serviceType = "OpenAI"; 20 private string _modelName = string.Empty; 21 private string _endpointUrl = string.Empty; 22 private string _apiVersion = string.Empty; 23 private string _deploymentName = string.Empty; 24 private string _modelPath = string.Empty; 25 private string _systemPrompt = string.Empty; 26 private bool _moderationEnabled = true; 27 private bool _isActive; 28 private bool _enableAdvancedAI; 29 private bool _isLocalModel; 30 31 public event PropertyChangedEventHandler PropertyChanged; 32 33 [JsonPropertyName("id")] 34 public string Id 35 { 36 get => _id; 37 set => SetProperty(ref _id, value); 38 } 39 40 [JsonPropertyName("service-type")] 41 public string ServiceType 42 { 43 get => _serviceType; 44 set 45 { 46 if (SetProperty(ref _serviceType, string.IsNullOrWhiteSpace(value) ? "OpenAI" : value)) 47 { 48 OnPropertyChanged(nameof(DisplayName)); 49 } 50 } 51 } 52 53 [JsonIgnore] 54 public AIServiceType ServiceTypeKind 55 { 56 get => ServiceType.ToAIServiceType(); 57 set => ServiceType = value.ToConfigurationString(); 58 } 59 60 [JsonPropertyName("model-name")] 61 public string ModelName 62 { 63 get => _modelName; 64 set 65 { 66 if (SetProperty(ref _modelName, value ?? string.Empty)) 67 { 68 OnPropertyChanged(nameof(DisplayName)); 69 } 70 } 71 } 72 73 [JsonPropertyName("endpoint-url")] 74 public string EndpointUrl 75 { 76 get => _endpointUrl; 77 set => SetProperty(ref _endpointUrl, value ?? string.Empty); 78 } 79 80 [JsonPropertyName("api-version")] 81 public string ApiVersion 82 { 83 get => _apiVersion; 84 set => SetProperty(ref _apiVersion, value ?? string.Empty); 85 } 86 87 [JsonPropertyName("deployment-name")] 88 public string DeploymentName 89 { 90 get => _deploymentName; 91 set => SetProperty(ref _deploymentName, value ?? string.Empty); 92 } 93 94 [JsonPropertyName("model-path")] 95 public string ModelPath 96 { 97 get => _modelPath; 98 set => SetProperty(ref _modelPath, value ?? string.Empty); 99 } 100 101 [JsonPropertyName("system-prompt")] 102 public string SystemPrompt 103 { 104 get => _systemPrompt; 105 set => SetProperty(ref _systemPrompt, value?.Trim() ?? string.Empty); 106 } 107 108 [JsonPropertyName("moderation-enabled")] 109 public bool ModerationEnabled 110 { 111 get => _moderationEnabled; 112 set => SetProperty(ref _moderationEnabled, value); 113 } 114 115 [JsonPropertyName("enable-advanced-ai")] 116 public bool EnableAdvancedAI 117 { 118 get => _enableAdvancedAI; 119 set => SetProperty(ref _enableAdvancedAI, value); 120 } 121 122 [JsonPropertyName("is-local-model")] 123 public bool IsLocalModel 124 { 125 get => _isLocalModel; 126 set => SetProperty(ref _isLocalModel, value); 127 } 128 129 [JsonIgnore] 130 public bool IsActive 131 { 132 get => _isActive; 133 set => SetProperty(ref _isActive, value); 134 } 135 136 [JsonIgnore] 137 public string DisplayName => string.IsNullOrWhiteSpace(ModelName) ? ServiceType : ModelName; 138 139 public PasteAIProviderDefinition Clone() 140 { 141 return new PasteAIProviderDefinition 142 { 143 Id = Id, 144 ServiceType = ServiceType, 145 ModelName = ModelName, 146 EndpointUrl = EndpointUrl, 147 ApiVersion = ApiVersion, 148 DeploymentName = DeploymentName, 149 ModelPath = ModelPath, 150 SystemPrompt = SystemPrompt, 151 ModerationEnabled = ModerationEnabled, 152 EnableAdvancedAI = EnableAdvancedAI, 153 IsLocalModel = IsLocalModel, 154 IsActive = IsActive, 155 }; 156 } 157 158 protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) 159 { 160 if (EqualityComparer<T>.Default.Equals(field, value)) 161 { 162 return false; 163 } 164 165 field = value; 166 OnPropertyChanged(propertyName); 167 return true; 168 } 169 170 protected void OnPropertyChanged(string propertyName) 171 { 172 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 173 } 174 } 175 }