Skip to content

Commit 886b242

Browse files
committed
enhance: added cancellation logic to the OpenAI chat response API call.
1 parent f7f549f commit 886b242

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Commands/GenerateCommitMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private string GenerateChangeSummary(Models.Change change)
7878
prompt.AppendLine("- Simply describe the MAIN GOAL of the changes.");
7979
prompt.AppendLine("- Output directly the summary in plain text.`");
8080

81-
var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here is the `git diff` output: {diff}");
81+
var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here is the `git diff` output: {diff}", _cancelToken);
8282
if (rsp != null && rsp.Choices.Count > 0)
8383
return rsp.Choices[0].Message.Content;
8484

@@ -104,7 +104,7 @@ private string GenerateSubject(string summary)
104104
prompt.AppendLine("- Output directly only one commit message in plain text with the next format: {type}: {commit_message}.");
105105
prompt.AppendLine("- Be as concise as possible, keep the message under 50 characters.");
106106

107-
var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here are the summaries changes: {summary}");
107+
var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here are the summaries changes: {summary}", _cancelToken);
108108
if (rsp != null && rsp.Choices.Count > 0)
109109
return rsp.Choices[0].Message.Content;
110110

src/Models/OpenAI.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net.Http;
44
using System.Text.Json;
55
using System.Text.Json.Serialization;
6+
using System.Threading;
67

78
namespace SourceGit.Models
89
{
@@ -97,7 +98,7 @@ public static bool IsValid
9798
get => !string.IsNullOrEmpty(Server) && !string.IsNullOrEmpty(ApiKey) && !string.IsNullOrEmpty(Model);
9899
}
99100

100-
public static OpenAIChatResponse Chat(string prompt, string question)
101+
public static OpenAIChatResponse Chat(string prompt, string question, CancellationToken cancellation)
101102
{
102103
var chat = new OpenAIChatRequest() { Model = Model };
103104
chat.AddMessage("system", prompt);
@@ -107,17 +108,27 @@ public static OpenAIChatResponse Chat(string prompt, string question)
107108
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}");
108109

109110
var req = new StringContent(JsonSerializer.Serialize(chat, JsonCodeGen.Default.OpenAIChatRequest));
110-
var task = client.PostAsync(Server, req);
111-
task.Wait();
112-
113-
var rsp = task.Result;
114-
if (!rsp.IsSuccessStatusCode)
115-
throw new Exception($"AI service returns error code {rsp.StatusCode}");
116-
117-
var reader = rsp.Content.ReadAsStringAsync();
118-
reader.Wait();
119-
120-
return JsonSerializer.Deserialize(reader.Result, JsonCodeGen.Default.OpenAIChatResponse);
111+
try
112+
{
113+
var task = client.PostAsync(Server, req, cancellation);
114+
task.Wait();
115+
116+
var rsp = task.Result;
117+
if (!rsp.IsSuccessStatusCode)
118+
throw new Exception($"AI service returns error code {rsp.StatusCode}");
119+
120+
var reader = rsp.Content.ReadAsStringAsync(cancellation);
121+
reader.Wait();
122+
123+
return JsonSerializer.Deserialize(reader.Result, JsonCodeGen.Default.OpenAIChatResponse);
124+
}
125+
catch
126+
{
127+
if (cancellation.IsCancellationRequested)
128+
return null;
129+
130+
throw;
131+
}
121132
}
122133
}
123134
}

0 commit comments

Comments
 (0)