-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.cs
More file actions
203 lines (175 loc) · 7.33 KB
/
Copy pathGlobalExceptionHandler.cs
File metadata and controls
203 lines (175 loc) · 7.33 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Activities;
using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.Presentation.View;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
namespace GlobalExceptionHandler
{
[DisplayName("Global Exception Handler")]
public class GlobalExceptionHandlerActivity : NativeActivity
{
private readonly HttpClient _httpClient;
public GlobalExceptionHandlerActivity()
{
_httpClient = new HttpClient();
}
public GlobalExceptionHandlerActivity(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
[Category("Input")]
[DisplayName("Exception")]
[Description("The exception to handle")]
public InArgument<Exception> Exception { get; set; }
[Category("Input")]
[DisplayName("OpenAI API Key")]
[Description("Your OpenAI API key")]
public InArgument<string> OpenAIApiKey { get; set; }
[Category("Input")]
[DisplayName("Derive Mail Values")]
[Description("Enable to generate mail subject and body")]
public InArgument<bool> DeriveMailValues { get; set; }
[Category("Output")]
[DisplayName("Mail Subject")]
[Description("AI-generated mail subject")]
public OutArgument<string> MailSubject { get; set; }
[Category("Output")]
[DisplayName("Mail Body")]
[Description("AI-generated mail body containing error details and suggestions")]
public OutArgument<string> MailBody { get; set; }
protected override void Execute(NativeActivityContext context)
{
var exception = Exception.Get(context);
var apiKey = OpenAIApiKey.Get(context);
var deriveMailValues = DeriveMailValues.Get(context);
if (exception == null)
{
throw new ArgumentNullException(nameof(exception), "Exception cannot be null");
}
if (string.IsNullOrEmpty(apiKey))
{
throw new ArgumentNullException(nameof(apiKey), "OpenAI API Key cannot be null or empty");
}
// Log the exception details
System.Diagnostics.Debug.WriteLine($"Exception Type: {exception.GetType().Name}");
System.Diagnostics.Debug.WriteLine($"Message: {exception.Message}");
System.Diagnostics.Debug.WriteLine($"Stack Trace: {exception.StackTrace}");
// Call OpenAI API to analyze the exception
var analysisResult = AnalyzeExceptionWithOpenAI(exception, apiKey, deriveMailValues).Result;
if (deriveMailValues && analysisResult != null)
{
MailSubject.Set(context, analysisResult.MailSubject);
MailBody.Set(context, analysisResult.MailBody);
}
}
private async Task<AnalysisResult> AnalyzeExceptionWithOpenAI(Exception exception, string apiKey, bool deriveMailValues)
{
try
{
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var prompt = $"Analyze this UiPath workflow exception and provide a summary and suggested fix:\n\n" +
$"Exception Type: {exception.GetType().Name}\n" +
$"Message: {exception.Message}\n" +
$"Stack Trace: {exception.StackTrace}";
if (deriveMailValues)
{
prompt += "\n\nPlease also provide:\n" +
"1. A concise mail subject line (max 100 characters)\n" +
"2. A detailed mail body that includes:\n" +
" - The original error details\n" +
" - A summary of the issue\n" +
" - Suggested fixes\n" +
"Format the response as JSON with 'subject' and 'body' fields.";
}
var requestBody = new
{
model = "gpt-3.5-turbo",
messages = new[]
{
new
{
role = "system",
content = "You are an expert in UiPath workflow automation and error handling. Analyze the provided exception and suggest a fix."
},
new
{
role = "user",
content = prompt
}
},
temperature = 0.7
};
var response = await _httpClient.PostAsync(
"https://api.openai.com/v1/chat/completions",
new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json")
);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var openAIResponse = JsonConvert.DeserializeObject<OpenAIResponse>(result);
// Log the OpenAI analysis
System.Diagnostics.Debug.WriteLine("OpenAI Analysis:");
System.Diagnostics.Debug.WriteLine(openAIResponse.Choices[0].Message.Content);
if (deriveMailValues)
{
try
{
var mailContent = JsonConvert.DeserializeObject<MailContent>(openAIResponse.Choices[0].Message.Content);
return new AnalysisResult
{
MailSubject = mailContent.Subject,
MailBody = mailContent.Body
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error parsing mail content: {ex.Message}");
}
}
}
else
{
System.Diagnostics.Debug.WriteLine($"Error calling OpenAI API: {response.StatusCode}");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in OpenAI analysis: {ex.Message}");
}
return null;
}
}
public class OpenAIResponse
{
public List<Choice> Choices { get; set; }
}
public class Choice
{
public Message Message { get; set; }
}
public class Message
{
public string Content { get; set; }
}
public class MailContent
{
public string Subject { get; set; }
public string Body { get; set; }
}
public class AnalysisResult
{
public string MailSubject { get; set; }
public string MailBody { get; set; }
}
}