-
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.
Add copy commands, and clean up docs
Add new dependencies to the Cargo.lock file and update the versions of existing ones to ensure compatibility and incorporate the latest features. Introduce two new commands to the chat system: `/copy-last-message` to copy the assistant's last response to the clipboard and `/copy-files` to copy code blocks from the assistant’s message. Enhance documentation for clarity and remove outdated content. These changes improve the functionality of the Rusty Buddy chat application by allowing users to efficiently copy previous messages or code snippets, thus streamlining their workflow. Additionally, documentation has been refined to better guide users on utilizing new features, whereas the previous implementation lacked these specific commands and had less organized documentation content.
- Loading branch information
Christian Stolz
committed
Oct 12, 2024
1 parent
5a378d5
commit 80af535
Showing
8 changed files
with
452 additions
and
68 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,64 @@ | ||
use crate::chat::command::{ChatCommand, RegisterableCommand}; | ||
use crate::chat::command_registry::CommandRegistry; | ||
use crate::chat::message_helpers::find_last_assistant_message; | ||
use crate::chat::service::ChatService; | ||
use crate::cli::editor::get_user_input; | ||
use arboard::Clipboard; | ||
use regex::Regex; | ||
use std::error::Error; | ||
|
||
pub struct CopyFilesCommand; | ||
|
||
impl CopyFilesCommand { | ||
pub fn new() -> Self { | ||
CopyFilesCommand {} | ||
} | ||
|
||
fn copy_to_clipboard(content: &str) -> Result<(), Box<dyn Error>> { | ||
let mut clipboard = Clipboard::new()?; | ||
clipboard.set_text(content.to_string())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl ChatCommand for CopyFilesCommand { | ||
fn execute(&self, args: &[&str], chat_service: &mut ChatService) -> Result<(), Box<dyn Error>> { | ||
let assistant_answer = | ||
find_last_assistant_message(chat_service).ok_or("No assistant message found.")?; | ||
|
||
let greedy_mode = args.contains(&"greedy"); | ||
let re = Regex::new(r"```(?s)(.*?)```")?; | ||
|
||
if greedy_mode { | ||
if let Some(start) = assistant_answer.find("```") { | ||
if let Some(end) = assistant_answer.rfind("```") { | ||
if start < end { | ||
let block_content = &assistant_answer[start + 3..end].trim(); | ||
Self::copy_to_clipboard(block_content)?; | ||
} | ||
} | ||
} | ||
} else { | ||
let mut counter = 1; | ||
for cap in re.captures_iter(&assistant_answer) { | ||
let block_content = &cap[1]; | ||
Self::copy_to_clipboard(block_content)?; | ||
println!("Copied code block {}", counter); | ||
get_user_input("Press enter for next block: ")?; | ||
counter += 1; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl RegisterableCommand for CopyFilesCommand { | ||
fn register_with_registry(registry: &mut CommandRegistry) { | ||
let command = CopyFilesCommand::new(); | ||
registry.register_command( | ||
"/copy-files", | ||
Box::new(command), | ||
vec!["copy-files".to_string(), "copy-files greedy".to_string()], | ||
); | ||
} | ||
} |
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,47 @@ | ||
use crate::chat::command::{ChatCommand, RegisterableCommand}; | ||
use crate::chat::command_registry::CommandRegistry; | ||
use crate::chat::message_helpers::find_last_assistant_message; | ||
use crate::chat::service::ChatService; | ||
use arboard::Clipboard; | ||
use std::error::Error; | ||
|
||
pub struct CopyLastMessageCommand; | ||
|
||
impl CopyLastMessageCommand { | ||
pub fn new() -> Self { | ||
CopyLastMessageCommand {} | ||
} | ||
|
||
fn copy_to_clipboard(content: &str) -> Result<(), Box<dyn Error>> { | ||
let mut clipboard = Clipboard::new()?; | ||
clipboard.set_text(content.to_string())?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl ChatCommand for CopyLastMessageCommand { | ||
fn execute( | ||
&self, | ||
_args: &[&str], | ||
chat_service: &mut ChatService, | ||
) -> Result<(), Box<dyn Error>> { | ||
if let Some(last_message) = find_last_assistant_message(chat_service) { | ||
Self::copy_to_clipboard(&last_message)?; | ||
println!("Last assistant message copied to clipboard."); | ||
} else { | ||
println!("No assistant message to copy."); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl RegisterableCommand for CopyLastMessageCommand { | ||
fn register_with_registry(registry: &mut CommandRegistry) { | ||
let command = CopyLastMessageCommand::new(); | ||
registry.register_command( | ||
"/copy-last-message", | ||
Box::new(command), | ||
vec!["copy-last-message".to_string()], | ||
); | ||
} | ||
} |
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