Skip to content

Commit 6e8d11d

Browse files
committed
enhance: fetch available models on the first-time to use this service instead of at startup
Signed-off-by: leo <longshuang@msn.cn>
1 parent b72704c commit 6e8d11d

8 files changed

Lines changed: 99 additions & 75 deletions

File tree

‎src/AI/Agent.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ 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 chatClient = _service.GetChatClient();
19+
var aiClient = await _service.GetOpenAIClientAsync();
20+
var chatClient = aiClient.GetChatClient(_service.Model);
2021
if (chatClient == null)
2122
throw new Exception("Failed to fetch available models from this service. Please check your configuration and try again.");
2223

‎src/AI/Service.cs‎

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

78
using Azure.AI.OpenAI;
89
using CommunityToolkit.Mvvm.ComponentModel;
910
using OpenAI;
10-
using OpenAI.Chat;
1111

1212
namespace SourceGit.AI
1313
{
@@ -36,29 +36,50 @@ public bool HasValidApiKey
3636
}
3737

3838
[JsonIgnore]
39-
public List<string> AvailableModels
39+
public string CachedApiKey
4040
{
4141
get;
42-
private set;
43-
} = [];
42+
set;
43+
} = string.Empty;
4444

4545
[JsonIgnore]
46-
public string CachedApiKey
46+
public List<string> CachedAvailableModels
4747
{
4848
get;
4949
set;
50-
} = string.Empty;
50+
} = [];
5151

5252
public string Model
5353
{
5454
get => _model;
55-
set => SetProperty(ref _model, value);
55+
set
56+
{
57+
if (SetProperty(ref _model, value))
58+
{
59+
if (!_autoFetchAvailableModels)
60+
{
61+
if (string.IsNullOrEmpty(value))
62+
CachedAvailableModels = [];
63+
else
64+
CachedAvailableModels = [_model];
65+
}
66+
}
67+
}
5668
}
5769

5870
public bool AutoFetchAvailableModels
5971
{
6072
get => _autoFetchAvailableModels;
61-
set => SetProperty(ref _autoFetchAvailableModels, value);
73+
set
74+
{
75+
if (SetProperty(ref _autoFetchAvailableModels, value))
76+
{
77+
if (!value && !string.IsNullOrEmpty(_model))
78+
CachedAvailableModels = [_model];
79+
else
80+
CachedAvailableModels = [];
81+
}
82+
}
6283
}
6384

6485
public string ReasoningEffortLevel
@@ -79,32 +100,9 @@ public string ExtraHeaders
79100
set;
80101
} = string.Empty;
81102

82-
public void FetchAvailableModels()
83-
{
84-
if (!_autoFetchAvailableModels)
85-
{
86-
if (!string.IsNullOrEmpty(Model))
87-
AvailableModels = [Model];
88-
return;
89-
}
90-
91-
var allModels = GetOpenAIClient().GetOpenAIModelClient().GetModels();
92-
AvailableModels = new List<string>();
93-
foreach (var model in allModels.Value)
94-
AvailableModels.Add(model.Id);
95-
96-
if (AvailableModels.Count > 0 && (string.IsNullOrEmpty(Model) || !AvailableModels.Contains(Model)))
97-
Model = AvailableModels[0];
98-
}
99-
100-
public ChatClient GetChatClient()
101-
{
102-
return !string.IsNullOrEmpty(Model) ? GetOpenAIClient().GetChatClient(Model) : null;
103-
}
104-
105-
private OpenAIClient GetOpenAIClient()
103+
public async Task<OpenAIClient> GetOpenAIClientAsync()
106104
{
107-
var credential = GetCredential();
105+
var credential = await GetCredentialAsync();
108106

109107
if (Server.Contains("openai.azure.com/", StringComparison.Ordinal))
110108
{
@@ -122,7 +120,7 @@ private OpenAIClient GetOpenAIClient()
122120
return new OpenAIClient(credential, options);
123121
}
124122

125-
private ApiKeyCredential GetCredential()
123+
private async Task<ApiKeyCredential> GetCredentialAsync()
126124
{
127125
if (!string.IsNullOrEmpty(CachedApiKey))
128126
return new ApiKeyCredential(CachedApiKey);

‎src/App.axaml.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
495495

496496
var pref = ViewModels.Preferences.Instance;
497497
pref.SetCanModify();
498-
pref.UpdateAvailableAIModels();
499498

500499
_launcher = new ViewModels.Launcher(startupRepo);
501500
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };

‎src/ViewModels/AIAssistant.cs‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ public class AIAssistant : ObservableObject
1212
{
1313
public List<string> AvailableModels
1414
{
15-
get => _service.AvailableModels;
15+
get => _service.CachedAvailableModels;
1616
}
1717

18-
public string CurrentModel
18+
public string SelectedModel
1919
{
20-
get => _service.Model;
21-
set => _service.Model = value;
20+
get => _selectedModel;
21+
set
22+
{
23+
if (SetProperty(ref _selectedModel, value))
24+
{
25+
_service.Model = value;
26+
27+
if (!_isFetchingModels)
28+
{
29+
var _ = GenAsync();
30+
}
31+
}
32+
}
2233
}
2334

2435
public bool IsGenerating
@@ -43,6 +54,7 @@ public AIAssistant(Repository repo, AI.Service service, List<Models.Change> chan
4354
{
4455
_repo = repo;
4556
_service = service;
57+
_selectedModel = service.Model;
4658
_cancel = new CancellationTokenSource();
4759

4860
var builder = new StringBuilder();
@@ -60,21 +72,61 @@ public async Task GenAsync()
6072
_cancel.Cancel();
6173
_cancel = new CancellationTokenSource();
6274

63-
var agent = new AI.Agent(_service);
6475
var builder = new StringBuilder();
65-
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
66-
6776
var responseBuilder = new StringBuilder();
6877
var foundResponse = false;
6978
var currentBranchName = _repo.CurrentBranch?.Name ?? "main";
7079

71-
Text = builder.ToString();
80+
Text = string.Empty;
7281
Response = string.Empty;
7382
IsGenerating = true;
7483

7584
try
7685
{
77-
await agent.GenerateCommitMessageAsync(_repo.FullPath, currentBranchName, _changeList, _amendParent, message =>
86+
if (_service.AutoFetchAvailableModels)
87+
{
88+
if (_service.CachedAvailableModels.Count == 0)
89+
{
90+
_isFetchingModels = true;
91+
builder.AppendLine("Fetching available models...");
92+
Text = builder.ToString();
93+
94+
var aiClient = await _service.GetOpenAIClientAsync();
95+
var models = await aiClient.GetOpenAIModelClient().GetModelsAsync();
96+
var availableModels = new List<string>();
97+
foreach (var m in models.Value)
98+
availableModels.Add(m.Id);
99+
100+
if (availableModels.Count == 0)
101+
throw new Exception("No available models found. Please check your OpenAI API key and network connection.");
102+
103+
var lastUsedModel = _selectedModel;
104+
_service.CachedAvailableModels = availableModels;
105+
OnPropertyChanged(nameof(AvailableModels));
106+
107+
if (!availableModels.Contains(lastUsedModel))
108+
{
109+
SelectedModel = availableModels[0];
110+
builder.AppendLine($"The model({lastUsedModel}) last used is not available. Auto change to the first available model.");
111+
Text = builder.ToString();
112+
}
113+
else
114+
{
115+
SelectedModel = null; // Force update views.
116+
SelectedModel = lastUsedModel;
117+
}
118+
_isFetchingModels = false;
119+
}
120+
}
121+
else if (string.IsNullOrEmpty(_selectedModel))
122+
{
123+
throw new Exception("No model is selected and auto-fetching is disabled. Please select a model or enable auto-fetching.");
124+
}
125+
126+
builder.AppendLine("Asking AI to generate commit message...").AppendLine();
127+
Text = builder.ToString();
128+
129+
await new AI.Agent(_service).GenerateCommitMessageAsync(_repo.FullPath, currentBranchName, _changeList, _amendParent, message =>
78130
{
79131
builder.AppendLine(message);
80132

@@ -149,6 +201,8 @@ private void SerializeChange(Models.Change c, StringBuilder builder)
149201
private readonly AI.Service _service = null;
150202
private readonly string _changeList = null;
151203
private readonly string _amendParent = null;
204+
private string _selectedModel = string.Empty;
205+
private bool _isFetchingModels = false;
152206
private CancellationTokenSource _cancel = null;
153207
private bool _isGenerating = false;
154208
private string _text = string.Empty;

‎src/ViewModels/Preferences.cs‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.IO;
44
using System.Text.Json;
55
using System.Text.Json.Serialization;
6-
using System.Threading.Tasks;
6+
77
using Avalonia.Collections;
88
using CommunityToolkit.Mvvm.ComponentModel;
99

@@ -612,24 +612,6 @@ public void AutoRemoveInvalidNode()
612612
RemoveInvalidRepositoriesRecursive(RepositoryNodes);
613613
}
614614

615-
public void UpdateAvailableAIModels()
616-
{
617-
Task.Run(() =>
618-
{
619-
foreach (var service in OpenAIServices)
620-
{
621-
try
622-
{
623-
service.FetchAvailableModels();
624-
}
625-
catch
626-
{
627-
// Ignore errors.
628-
}
629-
}
630-
});
631-
}
632-
633615
public void Save()
634616
{
635617
if (_isLoading || _isReadonly)

‎src/Views/AIAssistant.axaml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
Background="Transparent"
5858
VerticalAlignment="Center"
5959
ItemsSource="{Binding AvailableModels, Mode=OneWay}"
60-
SelectedItem="{Binding CurrentModel, Mode=TwoWay}"
61-
SelectionChanged="OnModelChanged"/>
60+
SelectedItem="{Binding SelectedModel, Mode=TwoWay}"/>
6261

6362
<v:LoadingIcon Grid.Column="2"
6463
Width="14" Height="14"

‎src/Views/AIAssistant.axaml.cs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,6 @@ protected override void OnClosing(WindowClosingEventArgs e)
155155
(DataContext as ViewModels.AIAssistant)?.Cancel();
156156
}
157157

158-
private async void OnModelChanged(object sender, SelectionChangedEventArgs e)
159-
{
160-
if (DataContext is ViewModels.AIAssistant vm && IsLoaded)
161-
await vm.GenAsync();
162-
163-
e.Handled = true;
164-
}
165-
166158
private void OnUseClicked(object sender, RoutedEventArgs e)
167159
{
168160
if (DataContext is ViewModels.AIAssistant vm && !string.IsNullOrEmpty(vm.Response))

‎src/Views/Preferences.axaml.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ protected override async void OnClosing(WindowClosingEventArgs e)
222222
}
223223

224224
var preferences = ViewModels.Preferences.Instance;
225-
preferences.UpdateAvailableAIModels();
226225
preferences.Save();
227226
}
228227

0 commit comments

Comments
 (0)