Skip to content

Commit ed5fb44

Browse files
committed
Add more comments and make some additional updates
1 parent db22595 commit ed5fb44

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

shell/agents/AIShell.Ollama.Agent/Command.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void ListPresetAction(string name)
5959

6060
settings.ShowOnePreset(host, name);
6161
}
62-
catch (InvalidOperationException ex)
62+
catch (Exception ex)
6363
{
6464
string availablePresetNames = PresetNamesAsString();
6565
host.WriteErrorLine($"{ex.Message} Available preset(s): {availablePresetNames}.");
@@ -98,7 +98,7 @@ private async Task UsePresetAction(string name)
9898
await setting.UsePreset(host, chosenPreset);
9999
host.MarkupLine($"Using the preset [green]{chosenPreset.Name}[/]:");
100100
}
101-
catch (InvalidOperationException ex)
101+
catch (Exception ex)
102102
{
103103
string availablePresetNames = PresetNamesAsString();
104104
host.WriteErrorLine($"{ex.Message} Available presets: {availablePresetNames}.");
@@ -153,7 +153,7 @@ private void ShowSystemPromptAction()
153153
{
154154
settings.ShowSystemPrompt(host);
155155
}
156-
catch (InvalidOperationException ex)
156+
catch (Exception ex)
157157
{
158158
host.WriteErrorLine(ex.Message);
159159
}
@@ -179,7 +179,7 @@ private void SetSystemPromptAction(string prompt)
179179
{
180180
settings.SetSystemPrompt(host, prompt);
181181
}
182-
catch (InvalidOperationException ex)
182+
catch (Exception ex)
183183
{
184184
host.WriteErrorLine(ex.Message);
185185
}
@@ -239,7 +239,7 @@ private async Task ListModelAction(string name)
239239

240240
await settings.ShowOneModel(host, name);
241241
}
242-
catch (InvalidOperationException ex)
242+
catch (Exception ex)
243243
{
244244
host.WriteErrorLine(ex.Message);
245245
}
@@ -282,14 +282,28 @@ private async Task UseModelAction(string name)
282282
CancellationToken.None);
283283
}
284284

285-
await settings.UseModel(host: null, name);
285+
await settings.UseModel(host, name);
286286
host.MarkupLine($"Using the model [green]{name}[/]");
287287
}
288-
catch (InvalidOperationException ex)
288+
catch (Exception ex)
289289
{
290290
host.WriteErrorLine(ex.Message);
291291
}
292292
}
293293

294-
private IEnumerable<string> ModelNameCompleter(CompletionContext context) => _agnet.Settings?.GetAllModels().GetAwaiter().GetResult() ?? [];
294+
private IEnumerable<string> ModelNameCompleter(CompletionContext context)
295+
{
296+
try
297+
{
298+
// Model retrieval may throw.
299+
var results = _agnet.Settings?.GetAllModels().Result;
300+
if (results is not null)
301+
{
302+
return results;
303+
}
304+
}
305+
catch (Exception) { }
306+
307+
return [];
308+
}
295309
}

shell/agents/AIShell.Ollama.Agent/Settings.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,23 @@ public Settings(ConfigData configData)
4545
}
4646
}
4747

48+
/// <summary>
49+
/// Retrieve available models from the Ollama endpoint.
50+
/// </summary>
51+
/// <param name="host">Used for writing error to host when it's a local endpoint but the Ollama server is not started. The endpoint check will be skipped if a `null` is specified.</param>
52+
/// <param name="cancellationToken">Used for cancel the operation.</param>
53+
/// <returns></returns>
4854
private async Task<bool> EnsureModelsInitialized(IHost host, CancellationToken cancellationToken = default)
4955
{
5056
if (_initialized)
5157
{
5258
return true;
5359
}
5460

55-
// Skip the self check when host is null.
61+
// The endpoint check is supposed to be interactive and can be skipped in some cases, such as when
62+
// the `PerformSelfcheck` method was already called right before entering this method.
63+
// So, we will simply skip the endpoint check when the passed-in host is null. If there's something
64+
// wrong with the endpoint, the subsequent calls to retrieve models will fail and throw anyway.
5665
if (host is not null)
5766
{
5867
bool success = await PerformSelfcheck(host, checkEndpointOnly: true);
@@ -183,6 +192,7 @@ internal async Task<bool> PerformSelfcheck(IHost host, bool checkEndpointOnly =
183192

184193
if (!checkEndpointOnly && !_runningConfigChecked)
185194
{
195+
// Skip the endpoint check in 'EnsureModelsInitialized' as we already did it.
186196
await EnsureModelsInitialized(host: null).ConfigureAwait(false);
187197
if (string.IsNullOrEmpty(RunningConfig.ModelName))
188198
{

0 commit comments

Comments
 (0)