Skip to content

Commit ca9f5e1

Browse files
committed
code_style: move some code from agent to service; rename async method with Async suffix; fix typos
Signed-off-by: leo <longshuang@msn.cn>
1 parent 66e51ba commit ca9f5e1

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/AI/Agent.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Text;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using Azure.AI.OpenAI;
7-
using OpenAI;
86
using OpenAI.Chat;
97

108
namespace SourceGit.AI
@@ -18,21 +16,16 @@ public Agent(Service service)
1816

1917
public async Task GenerateCommitMessageAsync(string repo, string changeList, Action<string> onUpdate, CancellationToken cancellation)
2018
{
21-
var endPoint = new Uri(_service.Server);
22-
var client = _service.Server.Contains("openai.azure.com/", StringComparison.Ordinal)
23-
? new AzureOpenAIClient(endPoint, _service.Credential)
24-
: new OpenAIClient(_service.Credential, new() { Endpoint = endPoint });
25-
26-
var chatClient = client.GetChatClient(_service.Model);
19+
var chatClient = _service.GetChatClient();
2720
var options = new ChatCompletionOptions() { Tools = { ChatTools.GetDetailChangesInFile } };
2821

2922
var userMessageBuilder = new StringBuilder();
3023
userMessageBuilder
3124
.AppendLine("Generate a commit message (follow the rule of conventional commit message) for given git repository.")
3225
.AppendLine("- Read all given changed files before generating. Only binary files (such as images, audios ...) can be skipped.")
3326
.AppendLine("- Output the conventional commit message (with detail changes in list) directly. Do not explain your output nor introduce your answer.")
34-
.AppendLine(string.IsNullOrEmpty(_service.AdditionalPrompt) ? string.Empty : _service.AdditionalPrompt)
35-
.Append("Reposiory path: ").AppendLine(repo.Quoted())
27+
.AppendLine(_service.AdditionalPrompt)
28+
.Append("Repository path: ").AppendLine(repo.Quoted())
3629
.AppendLine("Changed files ('A' means added, 'M' means modified, 'D' means deleted, 'T' means type changed, 'R' means renamed, 'C' means copied): ")
3730
.Append(changeList);
3831

@@ -65,15 +58,15 @@ public async Task GenerateCommitMessageAsync(string repo, string changeList, Act
6558

6659
foreach (var call in completion.ToolCalls)
6760
{
68-
var result = await ChatTools.Process(call, onUpdate);
61+
var result = await ChatTools.ProcessAsync(call, onUpdate);
6962
messages.Add(result);
7063
}
7164

7265
inProgress = true;
7366
break;
7467
}
7568
case ChatFinishReason.ContentFilter:
76-
throw new Exception("Ommitted content due to a content filter flag");
69+
throw new Exception("Omitted content due to a content filter flag");
7770
default:
7871
break;
7972
}

src/AI/ChatTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static class ChatTools
3232
}
3333
""")), false);
3434

35-
public static async Task<ToolChatMessage> Process(ChatToolCall call, Action<string> output)
35+
public static async Task<ToolChatMessage> ProcessAsync(ChatToolCall call, Action<string> output)
3636
{
3737
using var doc = JsonDocument.Parse(call.FunctionArguments);
3838

src/AI/Service.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
using System;
22
using System.ClientModel;
3+
using Azure.AI.OpenAI;
4+
using OpenAI;
5+
using OpenAI.Chat;
36

47
namespace SourceGit.AI
58
{
69
public class Service
710
{
8-
public string Name { get; set; }
9-
public string Server { get; set; }
10-
public string Model { get; set; }
11-
public string ApiKey { get; set; }
12-
public bool ReadApiKeyFromEnv { get; set; }
13-
public string AdditionalPrompt { get; set; }
14-
public ApiKeyCredential Credential => new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey);
11+
public string Name { get; set; } = string.Empty;
12+
public string Server { get; set; } = string.Empty;
13+
public string Model { get; set; } = string.Empty;
14+
public string ApiKey { get; set; } = string.Empty;
15+
public bool ReadApiKeyFromEnv { get; set; } = false;
16+
public string AdditionalPrompt { get; set; } = string.Empty;
17+
18+
public ChatClient GetChatClient()
19+
{
20+
var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey);
21+
var client = Server.Contains("openai.azure.com/", StringComparison.Ordinal)
22+
? new AzureOpenAIClient(new Uri(Server), credential)
23+
: new OpenAIClient(credential, new() { Endpoint = new Uri(Server) });
24+
25+
return client.GetChatClient(Model);
26+
}
1527
}
1628
}

0 commit comments

Comments
 (0)