-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slash-command): Add filename prompt to /save-last-answer
- 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
Showing
3 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
); | ||
} | ||
} |