From 26ab0a7529ff502e375baa42a6c7322c22d8b428 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 2 Apr 2026 10:35:49 +0800 Subject: [PATCH] refactor: use blank `User-Agent` header when communicating with open-ai compatible service (#2216) Signed-off-by: leo --- src/AI/Service.cs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/AI/Service.cs b/src/AI/Service.cs index 0bd3bf84..f7eb25e6 100644 --- a/src/AI/Service.cs +++ b/src/AI/Service.cs @@ -57,12 +57,7 @@ namespace SourceGit.AI public async Task> FetchAvailableModelsAsync() { - 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) }); - - var allModels = client.GetOpenAIModelClient().GetModels(); + var allModels = GetOpenAIClient().GetOpenAIModelClient().GetModels(); AvailableModels = new List(); foreach (var model in allModels.Value) AvailableModels.Add(model.Id); @@ -82,15 +77,15 @@ namespace SourceGit.AI public ChatClient GetChatClient() { - if (string.IsNullOrEmpty(Model)) - return null; + return !string.IsNullOrEmpty(Model) ? GetOpenAIClient().GetChatClient(Model) : null; + } + private OpenAIClient GetOpenAIClient() + { 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); + return Server.Contains("openai.azure.com/", StringComparison.Ordinal) + ? new AzureOpenAIClient(new Uri(Server), credential, new AzureOpenAIClientOptions() { UserAgentApplicationId = string.Empty }) + : new OpenAIClient(credential, new() { Endpoint = new Uri(Server), UserAgentApplicationId = string.Empty }); } private string _name = string.Empty;