-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalGPT.xaml.cs
188 lines (160 loc) · 7.88 KB
/
HalGPT.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Configuration;
using System.Windows;
using System.Windows.Input;
namespace HalGpt
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class HalUi
{
private const string WaitingForApi = "Just a moment...";
private const double HorizBorderMargin = 25;
private const double VertBorderMargin = 50;
private string _welcomeSpeech;
private readonly Speech _speech = new();
private readonly Conversation _conversation;
private readonly ConversationOutput _output;
bool _firstTimeOpened = true;
//**********************************************************************************************
// HAL_UI
//**********************************************************************************************
public HalUi()
{
InitializeComponent();
// Set window location (default or saved)
if(Math.Abs((double)Properties.Settings.Default.PropertyValues["WindowTop"].PropertyValue - (-1)) < 0.001)
Top = SystemParameters.PrimaryScreenHeight - Height - VertBorderMargin;
if (Math.Abs((double)Properties.Settings.Default.PropertyValues["WindowLeft"].PropertyValue - (-1)) < 0.001)
Left = HorizBorderMargin;
var appSettings = ConfigurationManager.AppSettings;
string apiKeyOpenAi = appSettings["ApiKeyOpenAI"];
string longDateTimeString = DateTime.Now.ToString("dddd, MMMM dd, yyyy h:mm tt");
string systemMessage = appSettings["SystemMessage"] + $" Today is {longDateTimeString}";
_conversation = new Conversation(apiKeyOpenAi, systemMessage);
_output = new ConversationOutput();
TxtChat.Text = WaitingForApi;
TxtSay.Text = "";
TxtSay.Focus();
if (SpeechEnabled())
_speech.InitSpeech();
}
//**********************************************************************************************
// Window_Activated
//**********************************************************************************************
private async void Window_Activated_Async(object sender, EventArgs e)
{
if (_firstTimeOpened)
{
_firstTimeOpened = false;
var appSettings = ConfigurationManager.AppSettings;
string welcomeRequest = appSettings["WelcomeRequest"];
if (string.IsNullOrEmpty(welcomeRequest))
_welcomeSpeech = "";
else
_welcomeSpeech = await _conversation.ReplyToInput(welcomeRequest, false);
TxtChat.Text = "";
_speech.Speak(_welcomeSpeech);
_ = _output.ReplySlowly(_welcomeSpeech, TxtChat, ScrollConversation);
}
}
//**********************************************************************************************
// TextBox_KeyUp
//**********************************************************************************************
private async void TextBox_KeyUp_Async(object sender, KeyEventArgs e)
{
_output.SpeedUpPendingConversation();
// Process the sentence introduced by the user
if (e.Key == Key.Enter)
{
// We will process meaningful sentences
string inputText = TxtSay.Text.Trim();
if (inputText != "")
{
// Thinking
TxtChat.Text = WaitingForApi;
var answer = await _conversation.ReplyToInput(inputText);
_speech.Speak(answer);
TxtChat.Text = "> " + TxtSay.Text + Environment.NewLine + Environment.NewLine;
TxtSay.Text = "";
await _output.ReplySlowly(answer, TxtChat, ScrollConversation);
}
}
}
//**********************************************************************************************
// Window_Closing
//**********************************************************************************************
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
var appSettings = ConfigurationManager.AppSettings;
string savePath = appSettings["SaveConversationsPath"];
_conversation.SaveAsHtml(savePath);
}
//**********************************************************************************************
// imageHal_MouseLeftButtonDown
//**********************************************************************************************
private void imageHal_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove(); // Allows moving the window
}
//**********************************************************************************************
// TxtChat_OnPreviewMouseDoubleClick
//**********************************************************************************************
private void TxtChat_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
OpenConversationWindow();
e.Handled = true;
}
//**********************************************************************************************
// OpenConversationWindow
//**********************************************************************************************
private void OpenConversationWindow()
{
_output.SpeedUpPendingConversation();
var conversation = new ConversationHistory(_conversation, _speech);
conversation.ShowDialog();
TxtChat.Text = "";
TxtSay.Text = "";
TxtSay.Focus();
}
#region Context menu
//**********************************************************************************************
// SpeechEnabled
//**********************************************************************************************
private static bool SpeechEnabled()
{
return (bool)Properties.Settings.Default.PropertyValues["SpeechEnabled"].PropertyValue;
}
//**********************************************************************************************
// Speech_Checked
//**********************************************************************************************
private void Speech_Checked(object sender, RoutedEventArgs e)
{
_speech.InitSpeech();
}
//**********************************************************************************************
// Speech_Unchecked
//**********************************************************************************************
private void Speech_Unchecked(object sender, RoutedEventArgs e)
{
_speech.Dispose();
}
//**********************************************************************************************
// Conversation_Click
//**********************************************************************************************
private void Conversation_Click(object sender, RoutedEventArgs e)
{
OpenConversationWindow();
}
//**********************************************************************************************
// CloseAnimationAndExit
//**********************************************************************************************
private void Exit_Click(object sender, RoutedEventArgs e)
{
Close();
}
#endregion
}
}