11import axios from 'axios' ;
2+ import { Registry } from '@remix-project/remix-lib' ;
23
34// Helper function to track events using MatomoManager instance
45function trackMatomoEvent ( category : string , action : string , name ?: string ) {
@@ -14,15 +15,47 @@ function trackMatomoEvent(category: string, action: string, name?: string) {
1415// default Ollama ports to check (11434 is the legacy/standard port)
1516const OLLAMA_PORTS = [ 11434 , 11435 , 11436 ] ;
1617const OLLAMA_BASE_HOST = 'http://localhost' ;
18+ const DEFAULT_OLLAMA_HOST = 'http://localhost:11434' ;
1719
1820let discoveredOllamaHost : string | null = null ;
1921
22+ function getConfiguredOllamaEndpoint ( ) : string | null {
23+ try {
24+ const config = Registry . getInstance ( ) . get ( 'config' ) . api ;
25+ const configuredEndpoint = config . get ( 'settings/ollama-endpoint' ) ;
26+ if ( configuredEndpoint && configuredEndpoint !== DEFAULT_OLLAMA_HOST ) {
27+ _paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'ollama_using_configured_endpoint' , configuredEndpoint ] ) ;
28+ return configuredEndpoint ;
29+ }
30+ } catch ( error ) {
31+ _paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'ollama_config_access_failed' , error . message || 'unknown' ] ) ;
32+ }
33+ return null ;
34+ }
35+
2036export async function discoverOllamaHost ( ) : Promise < string | null > {
2137 if ( discoveredOllamaHost ) {
2238 trackMatomoEvent ( 'ai' , 'remixAI' , `ollama_host_cache_hit:${ discoveredOllamaHost } ` ) ;
2339 return discoveredOllamaHost ;
2440 }
2541
42+ // First, try to use the configured endpoint from settings
43+ const configuredEndpoint = getConfiguredOllamaEndpoint ( ) ;
44+ if ( configuredEndpoint ) {
45+ try {
46+ const res = await axios . get ( `${ configuredEndpoint } /api/tags` , { timeout : 2000 } ) ;
47+ if ( res . status === 200 ) {
48+ discoveredOllamaHost = configuredEndpoint ;
49+ _paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'ollama_configured_endpoint_success' , configuredEndpoint ] ) ;
50+ return configuredEndpoint ;
51+ }
52+ } catch ( error ) {
53+ _paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'ollama_configured_endpoint_failed' , `${ configuredEndpoint } :${ error . message || 'unknown' } ` ] ) ;
54+ // Fall back to discovery if configured endpoint fails
55+ }
56+ }
57+
58+ // Fall back to port discovery if no configured endpoint or it failed
2659 for ( const port of OLLAMA_PORTS ) {
2760 const host = `${ OLLAMA_BASE_HOST } :${ port } ` ;
2861 trackMatomoEvent ( 'ai' , 'remixAI' , `ollama_port_check:${ port } ` ) ;
@@ -75,6 +108,12 @@ export function resetOllamaHost(): void {
75108 discoveredOllamaHost = null ;
76109}
77110
111+ export function resetOllamaHostOnSettingsChange ( ) : void {
112+ // This function should be called when Ollama settings are updated
113+ resetOllamaHost ( ) ;
114+ _paq . push ( [ 'trackEvent' , 'ai' , 'remixAI' , 'ollama_reset_on_settings_change' ] ) ;
115+ }
116+
78117export async function pullModel ( modelName : string ) : Promise < void > {
79118 // in case the user wants to pull a model from registry
80119 trackMatomoEvent ( 'ai' , 'remixAI' , `ollama_pull_model_start:${ modelName } ` ) ;
0 commit comments