Skip to content

Commit

Permalink
Add copy commands, and clean up docs
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 68 deletions.
313 changes: 292 additions & 21 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
async-openai = "0.24.0"
async-openai = "0.25.0"
dotenvy = "0.15.7"
clap = { version = "4", features = ["derive"] }
clap_complete = "4"
Expand All @@ -36,3 +36,4 @@ chrono-tz = "0.10"
surrealdb = { version = "2.0", features = ["kv-rocksdb"] }
async-channel = "2.3"
fern = "0.6"
arboard = "3.4"
49 changes: 41 additions & 8 deletions docs/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ You can also fetch knowledge before running a one-shot query to the AI.
rusty-buddy chat --one-shot "Need help optimizing memory management" --knowledge
```

### Slash Commands in Chat
## Slash Commands in Chat

Within a chat session, enhance your experience with the following slash commands:
Within a chat session, you can enhance your experience with the following slash commands:

#### Renew Context

Refresh the chat context, clearing previous interactions and reloading specified directory files.
Refresh the current chat context to reload any previous interactions or settings:

```
/renew
Expand All @@ -120,14 +120,13 @@ Refresh the chat context, clearing previous interactions and reloading specified

Save code blocks from the assistant's last message to files.

- **Standard Mode:** The users will be prompted for each block.

```
/save-files
```

Options:

- **Interactive Mode:** You'll be prompted for each code block.
- **Greedy Mode:** Quickly save all code blocks without prompts.
- **Greedy Mode:** Quickly save all code blocks without user prompts.

```
/save-files greedy
Expand All @@ -145,7 +144,41 @@ Type the slash command within the chat interface. Use `exit` to end the session,

---

By using these updated options, you can maximize the efficiency and effectiveness of Rusty Buddy's chat functionality. Whether for quick responses or engaging full sessions with varied contexts, these enhancements enable comprehensive interaction with the AI capabilities.
#### Copy Last Message

Copy the last message from the assistant to your clipboard. This command is useful for quickly storing a response that you may want to refer back to or use in another application.

```
/copy-last-message
```

**Key Points:**
- Copies the last assistant message directly to the clipboard using platform-specific clipboard access.
- Useful for efficiently using snippets from the conversation in other contexts or applications.

#### Copy Files

Extract and copy code blocks from the last assistant message to the clipboard.

- **Standard Mode:** Iterate over each code block, presenting the user with an option to copy.

```
/copy-files
```

- **Greedy Mode:** Automatically copies all detected code blocks in the assistant message without prompt.

```
/copy-files greedy
```

**Key Points:**
- Designed to streamline the process of acquiring code snippets from chat sessions.
- Can dramatically speed up the workflow when working on multiple projects or tasks that involve frequent context-switching.

---

Using these new commands, you can create a more efficient interaction loop, allowing Rusty Buddy to assist you with actionable insights and facilitating easy integration into your current workflow. By harnessing these capabilities, you greatly enhance the utility of the chat sessions, accessing and reusing information more effectively.

---

Expand Down
37 changes: 0 additions & 37 deletions extracted_content.txt

This file was deleted.

64 changes: 64 additions & 0 deletions src/chat/commands/copy_files.rs
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()],
);
}
}
47 changes: 47 additions & 0 deletions src/chat/commands/copy_last_answer.rs
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()],
);
}
}
6 changes: 6 additions & 0 deletions src/chat/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
use crate::chat::command::RegisterableCommand;
use crate::chat::command_registry::CommandRegistry;
use crate::chat::commands::copy_files::CopyFilesCommand;
use crate::chat::commands::copy_last_answer::CopyLastMessageCommand;
use crate::chat::commands::refresh::RenewCommand;
use crate::chat::commands::save_files::SaveFilesCommand;
use crate::chat::commands::save_last_answer::SaveLastAnswerCommand;

mod copy_files;
mod copy_last_answer;
mod refresh;
mod save_files;
mod save_last_answer;
Expand All @@ -44,4 +48,6 @@ pub fn initialize_commands(registry: &mut CommandRegistry) {
RenewCommand::register_with_registry(registry);
SaveFilesCommand::register_with_registry(registry);
SaveLastAnswerCommand::register_with_registry(registry);
CopyFilesCommand::register_with_registry(registry);
CopyLastMessageCommand::register_with_registry(registry);
}
1 change: 0 additions & 1 deletion src/chat/commands/save_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ fn save_block(block_content: &str) -> Result<(), Box<dyn Error>> {

fn prompt_and_save_block(index: usize, block_content: &str) -> Result<(), Box<dyn Error>> {
println!("Found code block #{}:", index + 1);
println!("{}", block_content);

if get_user_input("Do you want to save this code block? (y/n): ")?
.trim()
Expand Down

0 comments on commit 80af535

Please sign in to comment.