@@ -52,9 +52,14 @@ private async Task<bool> EnsureModelsInitialized(IHost host, CancellationToken c
5252 return true ;
5353 }
5454
55- if ( ! PerformSelfcheck ( host ) )
55+ // Skip the self check when host is null.
56+ if ( host is not null )
5657 {
57- return false ;
58+ bool success = await PerformSelfcheck ( host , checkEndpointOnly : true ) ;
59+ if ( ! success )
60+ {
61+ return false ;
62+ }
5863 }
5964
6065 using OllamaApiClient client = new ( Endpoint ) ;
@@ -77,6 +82,7 @@ internal async Task<ICollection<string>> GetAllModels(IHost host = null, Cancell
7782 internal void EnsureModelNameIsValid ( string name )
7883 {
7984 ArgumentException . ThrowIfNullOrEmpty ( name ) ;
85+
8086 if ( ! _availableModels . Contains ( name . AddLatestTagIfNecessery ( ) ) )
8187 {
8288 throw new InvalidOperationException ( $ "A model with the name '{ name } ' doesn't exist. The available models are: [{ string . Join ( ", " , _availableModels ) } ].") ;
@@ -94,7 +100,7 @@ internal void SetSystemPrompt(IHost host, string prompt)
94100 }
95101
96102 private static List < IRenderElement < string > > GetRenderModelElements ( Func < string , bool > isActive ) => [
97- new CustomElement < string > ( label : "Model Name" , m => m ) ,
103+ new CustomElement < string > ( label : "Name" , m => m ) ,
98104 new CustomElement < string > ( label : "Active" , m => isActive ( m ) ? "true" : string . Empty )
99105 ] ;
100106
@@ -165,64 +171,50 @@ internal void ShowOnePreset(IHost host, string name)
165171 ] ) ;
166172 }
167173
168- internal async Task < string > GetActiveModel ( IHost host , CancellationToken cancellationToken = default )
174+ internal async Task < bool > PerformSelfcheck ( IHost host , bool checkEndpointOnly = false )
169175 {
170- if ( _runningConfigChecked is false )
171- {
172- if ( await EnsureModelsInitialized ( host , cancellationToken ) . ConfigureAwait ( false ) )
173- {
174- if ( string . IsNullOrEmpty ( RunningConfig . ModelName ) )
175- {
176- // There is no model set, so use the first one available.
177- if ( _availableModels . Count == 0 )
178- {
179- throw new InvalidOperationException ( $ "No models are available to use from '{ Endpoint } '.") ;
180- }
181-
182- RunningConfig = RunningConfig with { ModelName = _availableModels . First ( ) } ;
183- host . MarkupLine ( $ "No Ollama model is configured. Using the first available model [green]'{ RunningConfig . ModelName } '[/].") ;
184- }
185- else
186- {
187- EnsureModelNameIsValid ( RunningConfig . ModelName ) ;
188- }
189-
190- _runningConfigChecked = true ;
191- }
192- else
193- {
194- throw new InvalidOperationException ( $ "Error initializing models from '{ Endpoint } '.") ;
195- }
196- }
197-
198- return RunningConfig . ModelName ;
199- }
176+ _isRunningLocalHost ??= IsLocalHost ( ) . IsMatch ( new Uri ( Endpoint ) . Host ) ;
200177
201- internal bool PerformSelfcheck ( IHost host )
202- {
203- if ( _isRunningLocalHost is null )
178+ if ( _isRunningLocalHost is true && Process . GetProcessesByName ( "ollama" ) . Length is 0 )
204179 {
205- var endpointUri = new Uri ( Endpoint ) ;
206- _isRunningLocalHost = IsLocalHost ( ) . IsMatch ( endpointUri . Host ) ;
180+ host . WriteErrorLine ( "Please be sure the Ollama is installed and server is running. Check all the prerequisites in the README of this agent are met." ) ;
181+ return false ;
207182 }
208183
209- if ( _isRunningLocalHost is true && Process . GetProcessesByName ( "ollama" ) . Length is 0 )
184+ if ( ! checkEndpointOnly && ! _runningConfigChecked )
210185 {
211- var message = "Please be sure the Ollama is installed and server is running. Check all the prerequisites in the README of this agent are met." ;
212- if ( host is null )
186+ await EnsureModelsInitialized ( host : null ) . ConfigureAwait ( false ) ;
187+ if ( string . IsNullOrEmpty ( RunningConfig . ModelName ) )
213188 {
214- throw new InvalidOperationException ( message ) ;
189+ // There is no model set, so use the first one available.
190+ if ( _availableModels . Count is 0 )
191+ {
192+ host . WriteErrorLine ( $ "No models are available to use from '{ Endpoint } '.") ;
193+ return false ;
194+ }
195+
196+ RunningConfig = RunningConfig with { ModelName = _availableModels . First ( ) } ;
197+ host . MarkupLine ( $ "No Ollama model is configured. Using the first available model [green]'{ RunningConfig . ModelName } '[/].") ;
215198 }
216199 else
217200 {
218- host . WriteErrorLine ( message ) ;
219- return false ;
201+ try
202+ {
203+ EnsureModelNameIsValid ( RunningConfig . ModelName ) ;
204+ }
205+ catch ( InvalidOperationException e )
206+ {
207+ host . WriteErrorLine ( e . Message ) ;
208+ return false ;
209+ }
220210 }
211+
212+ _runningConfigChecked = true ;
221213 }
222214
223215 return true ;
224216 }
225-
217+
226218 /// <summary>
227219 /// Defines a generated regular expression to match localhost addresses
228220 /// "localhost", "127.0.0.1" and "[::1]" with case-insensitivity.
@@ -286,5 +278,6 @@ internal partial class SourceGenerationContext : JsonSerializerContext { }
286278
287279static class TagExtensions
288280{
289- public static string AddLatestTagIfNecessery ( this string model ) => model . IndexOf ( ":" ) == - 1 ? string . Concat ( model , ":latest" ) : model ;
281+ public static string AddLatestTagIfNecessery ( this string model ) =>
282+ model . Contains ( ':' ) ? model : string . Concat ( model , ":latest" ) ;
290283}
0 commit comments