1- use crossterm:: event;
2- use crossterm:: event:: { Event , KeyCode } ;
31use std:: io:: { BufReader , Read , Write } ;
42use std:: process:: { Child , Command , Stdio } ;
53use std:: sync:: mpsc;
@@ -19,11 +17,12 @@ impl OllamaClient {
1917 system_prompt,
2018 }
2119 }
22-
20+
2321 pub ( crate ) fn generate_response (
2422 & self ,
2523 history_content : & str ,
26- context_content : Option < & str >
24+ user_prompt : & str ,
25+ context_content : Option < & str > ,
2726 ) -> io:: Result < ( String , bool ) > {
2827 // Create the ollama command with stdout piped
2928 let mut cmd = Command :: new ( "ollama" )
@@ -35,18 +34,23 @@ impl OllamaClient {
3534
3635 // Create the input for the ollama process
3736 if let Some ( mut stdin) = cmd. stdin . take ( ) {
38- // Use the full history for context (including current user input)
39- stdin. write_all ( history_content. as_bytes ( ) ) ?;
37+ // First, add the system prompt
38+ stdin. write_all ( b"Here is the system prompt: " ) ?;
39+ stdin. write_all ( self . system_prompt . as_bytes ( ) ) ?;
4040
41- // Include the context file content if available
41+ // Then add the context file content if available
4242 if let Some ( ref content) = context_content {
4343 stdin. write_all ( b"\n \n Additional context from file: " ) ?;
4444 stdin. write_all ( content. as_bytes ( ) ) ?;
4545 }
4646
47- // Finally, add the system prompt
48- stdin. write_all ( b"\n \n " ) ?;
49- stdin. write_all ( self . system_prompt . as_bytes ( ) ) ?;
47+ // Then include the full history file for context
48+ stdin. write_all ( b"\n \n Previous conversation: " ) ?;
49+ stdin. write_all ( history_content. as_bytes ( ) ) ?;
50+
51+ // Finally, add the user prompt
52+ stdin. write_all ( b"\n \n Current user prompt: " ) ?;
53+ stdin. write_all ( user_prompt. as_bytes ( ) ) ?;
5054 }
5155
5256 // Get stdout stream from the child process
@@ -56,35 +60,39 @@ impl OllamaClient {
5660 // Set up channel for interrupt signal
5761 let ( interrupt_tx, interrupt_rx) = mpsc:: channel ( ) ;
5862
59- thread:: spawn ( move || {
60- println ! ( "\n AI is responding... (Press Enter to interrupt)\n " ) ;
61- loop {
62- if event:: poll ( Duration :: from_millis ( 100 ) ) . unwrap ( ) {
63- if let Event :: Key ( key) = event:: read ( ) . unwrap ( ) {
64- if key. code == KeyCode :: Enter {
65- let _ = interrupt_tx. send ( ( ) ) ;
66- break ;
67- }
68- }
69- }
70- }
71- } ) ;
63+ // thread::spawn(move || {
64+ // println!("\nAI is responding... (Press Enter to interrupt)\n");
65+ // loop {
66+ // if event::poll(Duration::from_millis(100)).unwrap() {
67+ // if let Event::Key(key) = event::read().unwrap() {
68+ // if key.code == KeyCode::Enter {
69+ // let _ = interrupt_tx.send(());
70+ // break;
71+ // }
72+ // }
73+ // }
74+ // }
75+ // });
7276
7377 // Read response while checking for interrupt
7478 let ( full_response, was_interrupted) =
7579 read_process_output_with_interrupt ( & mut reader, & interrupt_rx, & mut cmd)
7680 . expect ( "error reading process output" ) ;
7781
7882 let ollama_response = String :: from_utf8_lossy ( & full_response) . to_string ( ) ;
79-
83+
8084 Ok ( ( ollama_response, was_interrupted) )
8185 }
86+
87+ pub ( crate ) fn update_system_prompt ( & mut self , new_system_prompt : String ) {
88+ self . system_prompt = new_system_prompt;
89+ }
8290}
8391
8492fn read_process_output_with_interrupt (
8593 reader : & mut BufReader < impl Read > ,
8694 interrupt_rx : & Receiver < ( ) > ,
87- cmd : & mut Child
95+ cmd : & mut Child ,
8896) -> io:: Result < ( Vec < u8 > , bool ) > {
8997 let mut buffer = [ 0 ; 1024 ] ;
9098 let mut full_response = Vec :: new ( ) ;
@@ -135,7 +143,7 @@ fn read_process_output_with_interrupt(
135143 Ok ( 0 ) => break ,
136144 Ok ( bytes_read) => {
137145 full_response. extend_from_slice ( & buffer[ ..bytes_read] ) ;
138- } ,
146+ }
139147 Err ( _) => break ,
140148 }
141149 }
@@ -152,9 +160,9 @@ mod tests {
152160 fn test_ollama_client_creation ( ) {
153161 let model = "llama2" . to_string ( ) ;
154162 let system_prompt = "You are a helpful assistant." . to_string ( ) ;
155-
163+
156164 let client = OllamaClient :: new ( model. clone ( ) , system_prompt. clone ( ) ) ;
157-
165+
158166 assert_eq ! ( client. model, model) ;
159167 assert_eq ! ( client. system_prompt, system_prompt) ;
160168 }
0 commit comments