-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
148 lines (138 loc) · 5.15 KB
/
MainWindow.xaml.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
using Microsoft.UI.Xaml;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.UI.Text;
using Microsoft.Windows.AppNotifications.Builder;
using WK.Libraries.SharpClipboardNS;
using Microsoft.Windows.AppNotifications;
using System;
using Windows.Storage;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
using Windows.ApplicationModel.DataTransfer;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Cheatboard;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
private readonly SharpClipboard _clipboard = new();
private readonly HttpClient _httpClient = new();
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
private string LastAnswer { get; set; } = "";
private readonly string _defaultPrompt =
"Without any explanation, Write directly the correct answer(s) of the following question(s)";
public MainWindow()
{
InitializeComponent();
_clipboard.ClipboardChanged += ClipboardChanged;
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var storedPrompt = (string)(_localSettings.Values["Prompt"] ??= _defaultPrompt);
var storedOpenAiApiKey = (string)_localSettings.Values["OpenAiApiKey"];
PromptEditBox.Document.SetText(TextSetOptions.None, storedPrompt);
OpenAiApiKeyEditBox.Document.SetText(TextSetOptions.None, storedOpenAiApiKey);
}
private async void ClipboardChanged(object sender, SharpClipboard.ClipboardChangedEventArgs e)
{
if (!IsRunningSwitch.IsOn) return;
if (e.ContentType != SharpClipboard.ContentTypes.Text)
return;
var text = _clipboard.ClipboardText;
if (LastAnswer == text)
return;
XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGlyph);
XmlElement badgeElement = badgeXml.SelectSingleNode("/badge") as XmlElement;
badgeElement.SetAttribute("value", "available");
BadgeNotification badge = new BadgeNotification(badgeXml);
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
badgeUpdater.Update(badge);
OpenAiApiKeyEditBox.Document.GetText(TextGetOptions.None, out var openAiKey);
PromptEditBox.Document.GetText(TextGetOptions.None, out var prompt);
if (string.IsNullOrWhiteSpace(openAiKey))
{
var missingApiKeyError = new AppNotificationBuilder()
.MuteAudio()
.AddText("Error: OpenAI API Key is missing")
.BuildNotification();
AppNotificationManager.Default.Show(missingApiKeyError);
}
if (string.IsNullOrWhiteSpace(prompt))
{
prompt = _defaultPrompt;
}
openAiKey = openAiKey.Replace("\r", string.Empty).Replace("\n", string.Empty);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
openAiKey);
if (text == null) return;
const int maxTokens = 256;
const double temperature = 0.2;
const string model = "gpt-3.5-turbo";
try
{
var response = await _httpClient.PostAsync("https://api.openai.com/v1/chat/completions",
new StringContent(JsonSerializer.Serialize(new
{
messages = new[]
{
new
{
role = "system",
content = prompt
},
new
{
role = "user",
content = text
}
},
max_tokens = maxTokens,
temperature,
model
}), Encoding.UTF8, "application/json"));
var responseContent = await response.Content.ReadAsStringAsync();
var completionResponse = JsonSerializer.Deserialize<CompletionResponse>(responseContent);
var completion = completionResponse?.choices[0].message.content;
if (completion == null) return;
var notification = new AppNotificationBuilder()
.MuteAudio()
.AddText(completion)
.BuildNotification();
AppNotificationManager.Default.Show(notification);
LastAnswer = completion;
var package = new DataPackage();
package.SetText(completion);
Clipboard.SetContent(package);
}
catch (Exception)
{
var error = new AppNotificationBuilder()
.MuteAudio()
.AddText("Error: Check your API key and internet connection")
.BuildNotification();
AppNotificationManager.Default.Show(error);
}
finally
{
badgeUpdater.Clear();
}
}
private void PromptEditBox_TextChanged(object sender, RoutedEventArgs e)
{
PromptEditBox.Document.GetText(TextGetOptions.None, out var value);
value = value.Replace("\r", string.Empty).Replace("\n", string.Empty);
_localSettings.Values["Prompt"] = value;
}
private void OpenAiApiKeyEditBox_TextChanged(object sender, RoutedEventArgs e)
{
OpenAiApiKeyEditBox.Document.GetText(TextGetOptions.None, out var value);
value = value.Replace("\r", string.Empty).Replace("\n", string.Empty);
_localSettings.Values["OpenAiApiKey"] = value;
}
}