Skip to content

Commit 1838763

Browse files
committed
code_style: code simplify
Signed-off-by: leo <longshuang@msn.cn>
1 parent 6e8d11d commit 1838763

3 files changed

Lines changed: 6 additions & 16 deletions

File tree

‎src/AI/Agent.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public Agent(Service service)
1616

1717
public async Task GenerateCommitMessageAsync(string repo, string currentBranch, string changeList, string amendParent, Action<string> onUpdate, CancellationToken cancellation)
1818
{
19-
var aiClient = await _service.GetOpenAIClientAsync();
20-
var chatClient = aiClient.GetChatClient(_service.Model);
19+
var chatClient = _service.GetOpenAIClient().GetChatClient(_service.Model);
2120
if (chatClient == null)
2221
throw new Exception("Failed to fetch available models from this service. Please check your configuration and try again.");
2322

‎src/AI/Service.cs‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.ClientModel.Primitives;
44
using System.Collections.Generic;
55
using System.Text.Json.Serialization;
6-
using System.Threading.Tasks;
76

87
using Azure.AI.OpenAI;
98
using CommunityToolkit.Mvvm.ComponentModel;
@@ -100,10 +99,12 @@ public string ExtraHeaders
10099
set;
101100
} = string.Empty;
102101

103-
public async Task<OpenAIClient> GetOpenAIClientAsync()
102+
public OpenAIClient GetOpenAIClient()
104103
{
105-
var credential = await GetCredentialAsync();
104+
if (string.IsNullOrEmpty(CachedApiKey))
105+
CachedApiKey = ApiKeyManager.Get(Server)?.GetRawApiKey() ?? string.Empty;
106106

107+
var credential = new ApiKeyCredential(CachedApiKey);
107108
if (Server.Contains("openai.azure.com/", StringComparison.Ordinal))
108109
{
109110
if (string.IsNullOrEmpty(ExtraHeaders))
@@ -120,15 +121,6 @@ public async Task<OpenAIClient> GetOpenAIClientAsync()
120121
return new OpenAIClient(credential, options);
121122
}
122123

123-
private async Task<ApiKeyCredential> GetCredentialAsync()
124-
{
125-
if (!string.IsNullOrEmpty(CachedApiKey))
126-
return new ApiKeyCredential(CachedApiKey);
127-
128-
CachedApiKey = ApiKeyManager.Get(Server)?.GetRawApiKey() ?? string.Empty;
129-
return new ApiKeyCredential(CachedApiKey);
130-
}
131-
132124
private string _name = string.Empty;
133125
private string _server = string.Empty;
134126
private string _model = string.Empty;

‎src/ViewModels/AIAssistant.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public async Task GenAsync()
9191
builder.AppendLine("Fetching available models...");
9292
Text = builder.ToString();
9393

94-
var aiClient = await _service.GetOpenAIClientAsync();
95-
var models = await aiClient.GetOpenAIModelClient().GetModelsAsync();
94+
var models = await _service.GetOpenAIClient().GetOpenAIModelClient().GetModelsAsync();
9695
var availableModels = new List<string>();
9796
foreach (var m in models.Value)
9897
availableModels.Add(m.Id);

0 commit comments

Comments
 (0)