code_style: move some code from agent to service; rename async method with Async suffix; fix typos

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-30 11:53:57 +08:00
parent 66e51ba706
commit ca9f5e179f
3 changed files with 25 additions and 20 deletions

View File

@@ -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<string> 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;
}

View File

@@ -32,7 +32,7 @@ namespace SourceGit.AI
}
""")), false);
public static async Task<ToolChatMessage> Process(ChatToolCall call, Action<string> output)
public static async Task<ToolChatMessage> ProcessAsync(ChatToolCall call, Action<string> output)
{
using var doc = JsonDocument.Parse(call.FunctionArguments);

View File

@@ -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);
}
}
}