From ca9f5e179f25ea51946e58257dacfd21259af55a Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 30 Mar 2026 11:53:57 +0800 Subject: [PATCH] code_style: move some code from agent to service; rename async method with `Async` suffix; fix typos Signed-off-by: leo --- src/AI/Agent.cs | 17 +++++------------ src/AI/ChatTools.cs | 2 +- src/AI/Service.cs | 26 +++++++++++++++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/AI/Agent.cs b/src/AI/Agent.cs index 8431293a..bd51babb 100644 --- a/src/AI/Agent.cs +++ b/src/AI/Agent.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; -using Azure.AI.OpenAI; -using OpenAI; using OpenAI.Chat; namespace SourceGit.AI @@ -18,12 +16,7 @@ namespace SourceGit.AI public async Task GenerateCommitMessageAsync(string repo, string changeList, Action onUpdate, CancellationToken cancellation) { - var endPoint = new Uri(_service.Server); - var client = _service.Server.Contains("openai.azure.com/", StringComparison.Ordinal) - ? new AzureOpenAIClient(endPoint, _service.Credential) - : new OpenAIClient(_service.Credential, new() { Endpoint = endPoint }); - - var chatClient = client.GetChatClient(_service.Model); + var chatClient = _service.GetChatClient(); var options = new ChatCompletionOptions() { Tools = { ChatTools.GetDetailChangesInFile } }; var userMessageBuilder = new StringBuilder(); @@ -31,8 +24,8 @@ namespace SourceGit.AI .AppendLine("Generate a commit message (follow the rule of conventional commit message) for given git repository.") .AppendLine("- Read all given changed files before generating. Only binary files (such as images, audios ...) can be skipped.") .AppendLine("- Output the conventional commit message (with detail changes in list) directly. Do not explain your output nor introduce your answer.") - .AppendLine(string.IsNullOrEmpty(_service.AdditionalPrompt) ? string.Empty : _service.AdditionalPrompt) - .Append("Reposiory path: ").AppendLine(repo.Quoted()) + .AppendLine(_service.AdditionalPrompt) + .Append("Repository path: ").AppendLine(repo.Quoted()) .AppendLine("Changed files ('A' means added, 'M' means modified, 'D' means deleted, 'T' means type changed, 'R' means renamed, 'C' means copied): ") .Append(changeList); @@ -65,7 +58,7 @@ namespace SourceGit.AI foreach (var call in completion.ToolCalls) { - var result = await ChatTools.Process(call, onUpdate); + var result = await ChatTools.ProcessAsync(call, onUpdate); messages.Add(result); } @@ -73,7 +66,7 @@ namespace SourceGit.AI break; } case ChatFinishReason.ContentFilter: - throw new Exception("Ommitted content due to a content filter flag"); + throw new Exception("Omitted content due to a content filter flag"); default: break; } diff --git a/src/AI/ChatTools.cs b/src/AI/ChatTools.cs index afc2cca0..b9e2f80c 100644 --- a/src/AI/ChatTools.cs +++ b/src/AI/ChatTools.cs @@ -32,7 +32,7 @@ namespace SourceGit.AI } """)), false); - public static async Task Process(ChatToolCall call, Action output) + public static async Task ProcessAsync(ChatToolCall call, Action output) { using var doc = JsonDocument.Parse(call.FunctionArguments); diff --git a/src/AI/Service.cs b/src/AI/Service.cs index 2482a8f8..91709cfe 100644 --- a/src/AI/Service.cs +++ b/src/AI/Service.cs @@ -1,16 +1,28 @@ using System; using System.ClientModel; +using Azure.AI.OpenAI; +using OpenAI; +using OpenAI.Chat; namespace SourceGit.AI { public class Service { - public string Name { get; set; } - public string Server { get; set; } - public string Model { get; set; } - public string ApiKey { get; set; } - public bool ReadApiKeyFromEnv { get; set; } - public string AdditionalPrompt { get; set; } - public ApiKeyCredential Credential => new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey); + public string Name { get; set; } = string.Empty; + public string Server { get; set; } = string.Empty; + public string Model { get; set; } = string.Empty; + public string ApiKey { get; set; } = string.Empty; + public bool ReadApiKeyFromEnv { get; set; } = false; + public string AdditionalPrompt { get; set; } = string.Empty; + + public ChatClient GetChatClient() + { + var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey); + var client = Server.Contains("openai.azure.com/", StringComparison.Ordinal) + ? new AzureOpenAIClient(new Uri(Server), credential) + : new OpenAIClient(credential, new() { Endpoint = new Uri(Server) }); + + return client.GetChatClient(Model); + } } }