Skip to content

Commit

Permalink
feat(slash-command): Add filename prompt to /save-last-answer
Browse files Browse the repository at this point in the history
- Updated the /save-last-answer slash command to prompt the user for a filename when saving the last assistant's message.
- If no filename is provided by the user, defaults to 'last_answer.txt'.
- Ensures more flexibility and customization by allowing users to specify their desired file location.
- Utilizes the get_user_input function for capturing user input efficiently.%
  • Loading branch information
Christian Stolz committed Oct 4, 2024
1 parent 464c298 commit a6c2014
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ This script will download and install the Rusty Buddy binaries onto your system.
```plaintext
OPENAI_KEY=your_openai_api_key
```

## Setup

The `init` command is a setup utility designed to streamline the initial configuration process for the Rusty Buddy CLI. It ensures that your environment is correctly set up with the necessary credentials and configurations before using the tool. This command is particularly useful for first-time users or when setting up on a new system.
Expand Down Expand Up @@ -234,18 +235,20 @@ The following slash commands are currently supported:
```
/save-files
```
This command allows you to interactively extract code snippets from AI responses and save them. Options include:
- **y**: Save this block.
- **n**: Skip this block.
For a quick save of all available code between first and last triple backticks, use:
Options include **y** to save block or **n** to skip. For a quick save of all available code between first and last triple backticks, use:
```
/save-files greedy
```
To execute a slash command, type it within the chat interface. For example, to save files, you would enter:
- **Save Last Answer**: Save the complete last response from the assistant to a file.
```
/save-last-answer
```
This command will store the last assistant's message into a file named `last_answer.txt`.

To execute a slash command, type it within the chat interface. For example, to save the last answer, you would enter:
```
/save-files
/save-last-answer
```

When you're done with the chat session, typing `exit` will allow you to exit, optionally saving the chat session under a specified name.
Expand Down Expand Up @@ -416,4 +419,4 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file

## Contact

For inquiries or support, please contact hg8496(mailto:[email protected]).
For inquiries or support, please contact hg8496(mailto:[email protected]).
3 changes: 3 additions & 0 deletions src/chat/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use crate::chat::command::RegisterableCommand;
use crate::chat::command_registry::CommandRegistry;
use crate::chat::commands::refresh::RenewCommand;
use crate::chat::commands::save_files::SaveFilesCommand;
use crate::chat::commands::save_last_answer::SaveLastAnswerCommand;

mod refresh;
mod save_files;
mod save_last_answer;

pub fn initialize_commands(registry: &mut CommandRegistry) {
// Each command registers itself
RenewCommand::register_with_registry(registry);
SaveFilesCommand::register_with_registry(registry);
SaveLastAnswerCommand::register_with_registry(registry);
}
69 changes: 69 additions & 0 deletions src/chat/commands/save_last_answer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::chat::command::{ChatCommand, RegisterableCommand};
use crate::chat::command_registry::CommandRegistry;
use crate::chat::file_storage::DirectoryChatStorage;
use crate::chat::interface::{ChatBackend, ChatStorage, MessageRole};
use crate::chat::service::ChatService;
use crate::cli::editor::get_filename_input;
use crate::openai_api::openai_interface::OpenAIInterface;
use std::error::Error;
use std::fs;

pub struct SaveLastAnswerCommand;

impl SaveLastAnswerCommand {
pub fn new() -> Self {
SaveLastAnswerCommand {}
}

fn find_last_assistant_message<B: ChatBackend, S: ChatStorage>(
chat_service: &ChatService<B, S>,
) -> Option<String> {
let mut last_assistant_message = None;
chat_service.process_messages(|msg| {
if msg.role == MessageRole::Assistant {
last_assistant_message = Some(msg.content.clone());
}
});
last_assistant_message
}
}

impl ChatCommand for SaveLastAnswerCommand {
fn execute(
&self,
_args: &[&str],
chat_service: &mut ChatService<OpenAIInterface, DirectoryChatStorage>,
) -> Result<(), Box<dyn Error>> {
if let Some(last_message) = Self::find_last_assistant_message(chat_service) {
let default_file_name = "last_answer.txt";
let user_file_path = get_filename_input(&format!(
"Enter file path to save the last answer (default: {}). Use <Tab> for filename autocompletion: ",
default_file_name
))?;

let file_path = if user_file_path.trim().is_empty() {
default_file_name.to_string()
} else {
user_file_path
};

fs::write(&file_path, last_message)?;
println!("Last assistant answer saved to '{}'.", file_path);
} else {
println!("No assistant answer to save.");
}

Ok(())
}
}

impl RegisterableCommand for SaveLastAnswerCommand {
fn register_with_registry(registry: &mut CommandRegistry) {
let command = SaveLastAnswerCommand::new();
registry.register_command(
"/save-last-answer",
Box::new(command),
vec!["save-last-answer".to_string()],
);
}
}

0 comments on commit a6c2014

Please sign in to comment.