Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sllama"
version = "0.1.9"
version = "0.1.10"
edition = "2024"

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.10 - 2025-06-14

_Implement basic command completion and hinting with rustyline_

## 0.1.9 - 2025-06-12

_Add support for rustyline modes_
Expand Down
44 changes: 17 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,39 @@ Update the system prompt for this session. Does not modify any configurations.
You can configure your sllama by creating and modifying TOML configuration located at `~/.sllama.toml`/
`%USERPROFILE%\.sllama.toml`.

### Options

#### rustyline

##### mode

Switch rustyline input mode between `Emacs` and `Vi`.

Default: `Emacs`

#### model

Ollama model used
An example toml populated with the default values.

Default: `gemma3:12b`
```toml
# Ollama model used.
model = "gemma3:12b"

#### sllama_dir
# Path to the sllama directory. This will hold new history files by default.
# ~ is expanded to the user's home directory based on `$HOME` or `%USERPROFILE%`. (not verified on windows)
sllama_dir = "~/sllama"

Path to the sllama directory. This will hold new history files by default.

Default: `~/sllama`

#### system_prompt

System prompt that configures the AI assistant's behavior.

Default:

```
# System prompt that configures the AI assistant's behavior.
system_prompt = """
You are an AI assistant receiving input from a command-line
application called silent-llama (sllama). The user may include additional context from another file.
This supplementary content appears after the system prompt and before the history file content.
Your responses are displayed in the terminal and saved to the history file.
Keep your answers helpful, concise, and relevant to both the user's direct query and any file context provided.
You can tell where you have previously responded by --- AI Response --- (added automatically).
"""

[rustyline]
# Switch rustyline input mode between `Emacs` and `Vi`.
mode = "emacs"
```

## TODO

- [x] Clarify how the prompt is formed
- [x] Add a configuration file
- [x] Integrate rustyline
- [ ] Implement completions with rustyline (commands and files)
- [ ] Implement completions with rustyline
- [x] Commands
- [ ] Files
- [ ] Support multiline input with shift + enter (using rustyline)
- [ ] Use `ollama server` and API calls instead
- [ ] Allow changing the context file during a chat
Expand Down
278 changes: 278 additions & 0 deletions src/command_complete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/*
* Copyright © 2025 Mitja Leino
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
use rustyline::completion::{Completer, Pair};
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::validate::{ValidationContext, ValidationResult, Validator};
use rustyline::{Context, Helper};
use std::borrow::Cow;

pub(crate) struct CommandHelper {
commands: Vec<String>,
file_commands: Vec<String>,
}

impl CommandHelper {
pub(crate) fn new(commands: Vec<&str>) -> Self {
CommandHelper {
commands: commands.iter().map(|s| s.to_string()).collect(),
file_commands: vec![],
}
}
}

impl Completer for CommandHelper {
type Candidate = Pair;

fn complete(
&self,
line: &str,
pos: usize,
_ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
if line.starts_with(":") {
let parts: Vec<&str> = line.split(' ').collect();
let command = parts[0];

if line.contains(" ") {
// Handle args
Ok((pos, vec![]))
} else {
// Handle command completion
let word_start = 1;
let word = &line[word_start..pos];

let matches: Vec<Pair> = self
.commands
.iter()
.filter(|cmd| cmd.starts_with(word) && cmd.len() > word.len())
.map(|cmd| Pair {
display: cmd.clone(),
replacement: cmd.clone(),
})
.collect();

Ok((word_start, matches))
}
} else {
Ok((pos, vec![]))
}
}
}

impl Hinter for CommandHelper {
type Hint = String;

fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<Self::Hint> {
// Simple hinting - can be expanded as needed
if line.starts_with(":") && !line.contains(" ") && pos == line.len() {
let command = &line[1..];
// Only show hints at the end of the line
for cmd in &self.commands {
if cmd.starts_with(command) && cmd != command && cmd.len() > command.len() {
return Some((&cmd[command.len()..]).to_string());
}
}
}
None
}
}

impl Highlighter for CommandHelper {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
Cow::Borrowed(line)
}

fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
Cow::Borrowed(hint)
}
}

impl Validator for CommandHelper {
fn validate(&self, _ctx: &mut ValidationContext) -> rustyline::Result<ValidationResult> {
Ok(ValidationResult::Valid(None))
}
}

// This is the key trait that combines all the above functionality
impl Helper for CommandHelper {}

#[cfg(test)]
mod tests {
use super::*;
use rustyline::history::DefaultHistory;
use rustyline::Context;

#[test]
fn test_command_helper_new() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);

assert_eq!(helper.commands, vec!["help", "quit", "save"]);
assert!(helper.file_commands.is_empty());
}

#[test]
fn test_command_with_arguments() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let (pos, matches) = helper.complete(":save file.txt", 12, &ctx).unwrap();
assert_eq!(pos, 12);
assert!(matches.is_empty());
}

#[test]
fn test_no_matches() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let (pos, matches) = helper.complete(":xyz", 4, &ctx).unwrap();
assert_eq!(pos, 1);
assert!(matches.is_empty());
}

#[test]
fn test_complete_empty_line() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let (pos, matches) = helper.complete("", 0, &ctx).unwrap();
assert_eq!(pos, 0);
assert!(matches.is_empty());
}

#[test]
fn test_complete_non_command_line() {
let commands = vec!["help", "quit", "save", "hey"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let (pos, matches) = helper.complete("Hey there", 0, &ctx).unwrap();
assert_eq!(pos, 0);
assert!(matches.is_empty());
}

#[test]
fn test_hint_no_match() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let hint = helper.hint(":xyz", 4, &ctx);
assert_eq!(hint, None);
}

#[test]
fn test_hint_with_space() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let hint = helper.hint(":help ", 6, &ctx);
assert_eq!(hint, None);
}

#[test]
fn test_empty_commands_list() {
let helper = CommandHelper::new(vec![]);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

// Try to complete with an empty command list
let (pos, matches) = helper.complete(":h", 2, &ctx).unwrap();
assert_eq!(pos, 1);
assert!(matches.is_empty());

// Try to get a hint with an empty command list
let hint = helper.hint(":h", 2, &ctx);
assert_eq!(hint, None);
}

#[test]
fn test_complete_command() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

// Partial command
let (pos, matches) = helper.complete(":h", 2, &ctx).unwrap();
assert_eq!(pos, 1);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].display, "help");

// Complete command
let (pos, matches) = helper.complete(":help", 5, &ctx).unwrap();
assert_eq!(pos, 1);
assert!(matches.is_empty());
}

#[test]
fn multiple_matches() {
let commands = vec!["help", "quit", "switch", "sysprompt"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

let (pos, matches) = helper.complete(":s", 2, &ctx).unwrap();
assert_eq!(pos, 1);
assert_eq!(matches.len(), 2);

let match_strings: Vec<String> = matches.iter().map(|m| m.display.clone()).collect();
assert!(match_strings.contains(&"switch".to_string()));
assert!(match_strings.contains(&"sysprompt".to_string()));
}

#[test]
fn test_hint() {
let commands = vec!["help", "quit", "save"];
let helper = CommandHelper::new(commands);
let history = DefaultHistory::new();
let ctx = Context::new(&history);

// Partial command
let hint = helper.hint(":h", 2, &ctx);
assert_eq!(hint, Some("elp".to_string()));

// Complete command
let hint = helper.hint(":help", 5, &ctx);
assert_eq!(hint, None);
}

#[test]
fn test_highlighter() {
let helper = CommandHelper::new(vec!["help"]);

// Test line highlighting (currently returns unchanged)
let highlighted = helper.highlight("test line", 4);
assert_eq!(highlighted, "test line");

// Test hint highlighting (currently returns unchanged)
let highlighted_hint = helper.highlight_hint("hint text");
assert_eq!(highlighted_hint, "hint text");
}
}
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*
*/

use crate::command_complete::CommandHelper;
use rustyline::history::{DefaultHistory, FileHistory};
use rustyline::Editor;
use serde::Deserialize;
use std::path::PathBuf;
use std::{fs, io};
Expand Down Expand Up @@ -97,6 +100,16 @@ impl Config {
}
}

pub fn create_editor(&self) -> rustyline::Result<Editor<CommandHelper, DefaultHistory>> {
let config = self.create_rustyline_config();
let commands = vec!["q", "help", "list", "switch", "edit", "sysprompt"];
let helper = CommandHelper::new(commands);
let mut editor = Editor::with_config(config)?;
editor.set_helper(Some(helper));

Ok(editor)
}

pub fn load() -> io::Result<Self> {
let config_path = match get_config_path() {
Ok(path) => path,
Expand Down
Loading