@@ -3,11 +3,32 @@ use std::path::Path;
33use std:: net:: TcpStream ;
44use std:: time:: Duration ;
55use serde_json:: json;
6- use regex:: Regex ;
76
87#[ cfg( target_os = "windows" ) ]
98use std:: os:: windows:: process:: CommandExt ;
109
10+ // Default Ollama port
11+ const DEFAULT_OLLAMA_PORT : u16 = 11434 ;
12+
13+ // Helper function to get Ollama port from environment or use default
14+ fn get_ollama_port ( ) -> u16 {
15+ std:: env:: var ( "OLLAMA_PORT" )
16+ . ok ( )
17+ . and_then ( |s| s. parse :: < u16 > ( ) . ok ( ) )
18+ . unwrap_or ( DEFAULT_OLLAMA_PORT )
19+ }
20+
21+ // Helper function to get Ollama base URL
22+ fn get_ollama_base_url ( ) -> String {
23+ let port = get_ollama_port ( ) ;
24+ format ! ( "http://localhost:{}" , port)
25+ }
26+
27+ // Helper function to get Ollama API endpoint
28+ fn get_ollama_api_url ( endpoint : & str ) -> String {
29+ format ! ( "{}/api/{}" , get_ollama_base_url( ) , endpoint)
30+ }
31+
1132// Helper function to generate extended PATH based on platform
1233fn get_extended_path ( ) -> String {
1334 let current_path = std:: env:: var ( "PATH" ) . unwrap_or_default ( ) ;
@@ -53,11 +74,33 @@ fn get_platform() -> String {
5374 }
5475}
5576
77+ #[ tauri:: command]
78+ fn get_ollama_port_config ( ) -> u16 {
79+ get_ollama_port ( )
80+ }
81+
82+ #[ tauri:: command]
83+ fn set_ollama_port_config ( port : u16 ) -> Result < String , String > {
84+ if port < 1024 || port > 65535 {
85+ return Err ( "Port must be between 1024 and 65535" . to_string ( ) ) ;
86+ }
87+
88+ std:: env:: set_var ( "OLLAMA_PORT" , port. to_string ( ) ) ;
89+ Ok ( format ! ( "Ollama port set to {}" , port) )
90+ }
91+
92+ #[ tauri:: command]
93+ fn get_ollama_url ( ) -> String {
94+ get_ollama_base_url ( )
95+ }
96+
5697#[ tauri:: command]
5798fn check_ollama_service_running ( ) -> bool {
58- // Check if Ollama service is running by attempting to connect to port 11434
99+ // Check if Ollama service is running by attempting to connect to the configured port
100+ let port = get_ollama_port ( ) ;
101+ let address = format ! ( "127.0.0.1:{}" , port) ;
59102 TcpStream :: connect_timeout (
60- & "127.0.0.1:11434" . parse ( ) . unwrap ( ) ,
103+ & address . parse ( ) . unwrap ( ) ,
61104 Duration :: from_millis ( 1000 )
62105 ) . is_ok ( )
63106}
@@ -366,8 +409,9 @@ async fn list_installed_models() -> Result<Vec<String>, String> {
366409 // If ollama list fails, try to use the API directly
367410 if cfg ! ( target_os = "windows" ) {
368411 // On Windows, try to use curl to call the API
412+ let api_url = get_ollama_api_url ( "tags" ) ;
369413 let api_result = Command :: new ( "curl" )
370- . args ( [ "-s" , "http://localhost:11434/api/tags" ] )
414+ . args ( [ "-s" , & api_url ] )
371415 . output ( ) ;
372416
373417 match api_result {
@@ -603,10 +647,11 @@ async fn stop_ollama_service() -> Result<String, String> {
603647#[ tauri:: command]
604648async fn load_ollama_model ( model_name : String ) -> Result < String , String > {
605649 // Load a model by making a simple request to it
650+ let api_url = get_ollama_api_url ( "generate" ) ;
606651 let output = Command :: new ( "curl" )
607652 . args ( [
608653 "-X" , "POST" ,
609- "http://localhost:11434/api/generate" ,
654+ & api_url ,
610655 "-H" , "Content-Type: application/json" ,
611656 "-d" , & format ! ( r#"{{"model": "{}", "prompt": "hello", "stream": false}}"# , model_name)
612657 ] )
@@ -620,7 +665,7 @@ async fn load_ollama_model(model_name: String) -> Result<String, String> {
620665 if stdout. contains ( "error" ) {
621666 Err ( format ! ( "Failed to load model '{}': {}" , model_name, stdout) )
622667 } else {
623- Ok ( format ! ( "Model '{}' loaded successfully. API Response received. Command: curl -X POST http://localhost:11434/api/generate " , model_name) )
668+ Ok ( format ! ( "Model '{}' loaded successfully. API Response received. Command: curl -X POST {} " , model_name, api_url ) )
624669 }
625670 } else {
626671 let error = String :: from_utf8_lossy ( & output. stderr ) ;
@@ -732,18 +777,19 @@ async fn scan_for_models() -> Result<Vec<String>, String> {
732777
733778#[ tauri:: command]
734779fn check_ollama_service_status ( ) -> Result < bool , String > {
735- // Primary method: Check if Ollama service is running by testing TCP connection to port 11434
780+ // Primary method: Check if Ollama service is running by testing TCP connection to the configured port
736781 if check_ollama_service_running ( ) {
737782 return Ok ( true ) ;
738783 }
739784
740785 // Secondary method: Try to call the API directly
786+ let api_url = get_ollama_api_url ( "tags" ) ;
741787 let output = Command :: new ( "curl" )
742788 . args ( [
743789 "-s" ,
744790 "--connect-timeout" , "2" ,
745791 "--max-time" , "3" ,
746- "http://localhost:11434/api/tags"
792+ & api_url
747793 ] )
748794 . output ( ) ;
749795
@@ -884,7 +930,7 @@ async fn fix_windows_ollama_service() -> Result<String, String> {
884930 attempts += 1 ;
885931 }
886932
887- fix_info. push_str ( " ⚠ Service was started but is not responding on port 11434 \n " ) ;
933+ fix_info. push_str ( & format ! ( " ⚠ Service was started but is not responding on port {} \n " , get_ollama_port ( ) ) ) ;
888934 fix_info. push_str ( " Try restarting the app or running 'ollama serve' manually\n " ) ;
889935
890936 Err ( fix_info)
@@ -1451,7 +1497,10 @@ pub fn run() {
14511497 diagnose_windows_ollama_issues,
14521498 fix_windows_ollama_service,
14531499 ask_ollama_verbose,
1454- search_web
1500+ search_web,
1501+ get_ollama_port_config,
1502+ set_ollama_port_config,
1503+ get_ollama_url
14551504 ] )
14561505 . setup ( |app| {
14571506 if cfg ! ( debug_assertions) {
0 commit comments