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