Skip to content
This repository was archived by the owner on May 19, 2026. It is now read-only.

Commit 82fc702

Browse files
committed
Disable response interruption, reorder AI prompts and add sysprompt command
1 parent 17584ff commit 82fc702

6 files changed

Lines changed: 81 additions & 39 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sllama"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
edition = "2024"
55

66
[dependencies]

changelog.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@
22

33
## 0.1.5 - 2025-06-08
44

5-
_Use [rustyline](https://github.com/kkawakam/rustyline) for user input handling_
5+
_Integrate [rustyline](https://github.com/kkawakam/rustyline) for input handling, add `sysprompt` command, order prompts
6+
and disable interruption_
7+
8+
### Interruption
9+
10+
- Crossterm and rustyline do not play nice, disabled AI response interruption to keep input consistent.
11+
12+
### Prompts
13+
14+
- Use a more traditional ordering with:
15+
1. System prompt
16+
2. Context file
17+
3. History file
18+
4. Current user prompt
19+
20+
### Commands
21+
22+
- Enable modifying system prompt for the current session.
623

724
## 0.1.4 - 2025-06-08
825

readme.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ A command-line interface for interacting with Ollama AI models.
77
- Store conversations as files
88
- Add context to a session with `-f/--file` flag
99
- Prompts are built in the following way:
10-
1. History file (including the current user prompt)
10+
1. System prompt
1111
2. Context file
12-
3. System prompt
12+
3. History file
13+
4. Current user prompt
1314

1415
## Installation
1516

@@ -70,6 +71,12 @@ Exit the current chat.
7071

7172
`:q`
7273

74+
#### Sysprompt
75+
76+
Update the system prompt for this session. Does not modify any configurations.
77+
78+
`:sysprompt Enter the new system prompt here`
79+
7380
## Configuration
7481

7582
You can configure your sllama by creating and modifying TOML configuration located at `~/.sllama.toml`/

src/main.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ fn main() -> io::Result<()> {
5454
};
5555

5656
let mut history = HistoryFile::new(filename.clone(), config.sllama_dir.clone())?;
57-
let ollama_client = OllamaClient::new(config.model, config.system_prompt);
58-
57+
println!("{}", history.get_content());
58+
println!("You're conversing with {} model", &config.model);
59+
let mut ollama_client = OllamaClient::new(config.model, config.system_prompt);
5960
println!("Press Enter during AI generation to interrupt the response.");
6061

6162
// Main conversation loop
6263
loop {
6364
// Prompt the user for input
64-
println!("\nEnter your prompt (or type ':q' to end):");
65+
println!(
66+
"\nEnter your prompt or a command (type ':q' to end or ':help' for other commands)"
67+
);
6568
let mut rl = match rustyline::DefaultEditor::new() {
6669
Ok(r) => r,
6770
Err(e) => {
@@ -106,17 +109,24 @@ fn main() -> io::Result<()> {
106109
}
107110
continue;
108111
}
112+
":sysprompt" => {
113+
ollama_client.update_system_prompt(args.join(" "));
114+
continue;
115+
}
109116
_ => {
110117
println!("Unknown command '{}'", command_string);
111118
continue;
112119
}
113120
}
114121
}
115122

116-
history.append_user_input(&user_prompt)?;
123+
let (ollama_response, was_interrupted) = ollama_client.generate_response(
124+
history.get_content(),
125+
&user_prompt,
126+
input_file_content.as_deref(),
127+
)?;
117128

118-
let (ollama_response, was_interrupted) = ollama_client
119-
.generate_response(history.get_content(), input_file_content.as_deref())?;
129+
history.append_user_input(&user_prompt)?;
120130

121131
history.append_ai_response(&ollama_response, was_interrupted)?;
122132
}

src/ollama_client.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crossterm::event;
2-
use crossterm::event::{Event, KeyCode};
31
use std::io::{BufReader, Read, Write};
42
use std::process::{Child, Command, Stdio};
53
use 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\nAdditional 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\nPrevious conversation: ")?;
49+
stdin.write_all(history_content.as_bytes())?;
50+
51+
// Finally, add the user prompt
52+
stdin.write_all(b"\n\nCurrent 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!("\nAI 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

8492
fn 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

Comments
 (0)