-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
101 lines (81 loc) · 2.83 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
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using DuckDuckGo;
namespace DDGChatAPI
{
public partial class MainWindow : Window
{
// Create api instance
ChatAPI api = new ChatAPI();
// Create markdown engine
MdXaml.Markdown md = new MdXaml.Markdown();
public MainWindow()
{
InitializeComponent();
}
public void clearMarkdownRenderer()
{
// Clear markdown renderer
FlowDocument flowDoc = md.Transform("");
mdViewer.Document = flowDoc;
}
private async void mainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Initialize Conversation
int init = await api.initConversation();
if (init != 0)
{
MessageBox.Show("Init returned: " + init.ToString());
}
// Enable controls after initialization
chatContentBox.IsEnabled = true;
sendBTN.IsEnabled = true;
// Default prompt
string intro = await api.PromptAsync(api.DefaultPrompt);
FlowDocument flowDoc = md.Transform(intro);
mdViewer.Document = flowDoc;
}
public async Task sendMessage()
{
// Get question from chatbox
string prompt = chatContentBox.Text;
// Check if prompt is empty
if (prompt == null || prompt == "" || prompt == " ")
{
return;
}
// Add current token to title
this.Title = "Duck.AI | " + api.token;
// Clear markdown renderer
clearMarkdownRenderer();
// Show loading animation
loadingAnimation.Visibility = Visibility.Visible;
// Clear chatbox
chatContentBox.Clear();
// Send question to api and wait for response
var answer = await api.PromptAsync(prompt);
// Create a FlowDocument for the markdown renderer
FlowDocument flowDoc = md.Transform(answer.ToString());
// Hide loading animation
loadingAnimation.Visibility = Visibility.Hidden;
// Render the FlowDocument
mdViewer.Document = flowDoc;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
await sendMessage();
}
public async void chatBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
await sendMessage();
}
}
private async void Button_Click_2(object sender, RoutedEventArgs e)
{
await sendMessage();
}
}
}