|
| 1 | +use clap::Args; |
| 2 | +use crossterm::execute; |
| 3 | +use crossterm::style::{self, Color}; |
| 4 | + |
| 5 | +use super::editor::open_editor; |
| 6 | +use crate::cli::chat::{ChatError, ChatSession, ChatState}; |
| 7 | + |
| 8 | +/// Arguments to the `/reply` command. |
| 9 | +#[deny(missing_docs)] |
| 10 | +#[derive(Debug, PartialEq, Args)] |
| 11 | +pub struct ReplyArgs {} |
| 12 | + |
| 13 | +impl ReplyArgs { |
| 14 | + pub async fn execute(self, session: &mut ChatSession) -> Result<ChatState, ChatError> { |
| 15 | + // Get the most recent assistant message from transcript |
| 16 | + let last_assistant_message = session |
| 17 | + .conversation |
| 18 | + .transcript |
| 19 | + .iter() |
| 20 | + .rev() |
| 21 | + .find(|msg| !msg.starts_with("> ")) |
| 22 | + .cloned(); |
| 23 | + |
| 24 | + let initial_text = match last_assistant_message { |
| 25 | + Some(msg) => { |
| 26 | + // Format with > prefix for each line |
| 27 | + msg.lines() |
| 28 | + .map(|line| format!("> {}", line)) |
| 29 | + .collect::<Vec<_>>() |
| 30 | + .join("\n") |
| 31 | + }, |
| 32 | + None => { |
| 33 | + execute!( |
| 34 | + session.stderr, |
| 35 | + style::SetForegroundColor(Color::Yellow), |
| 36 | + style::Print("\nNo assistant message found to reply to.\n\n"), |
| 37 | + style::SetForegroundColor(Color::Reset) |
| 38 | + )?; |
| 39 | + |
| 40 | + return Ok(ChatState::PromptUser { |
| 41 | + skip_printing_tools: true, |
| 42 | + }); |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + let content = match open_editor(Some(initial_text.clone())) { |
| 47 | + Ok(content) => content, |
| 48 | + Err(err) => { |
| 49 | + execute!( |
| 50 | + session.stderr, |
| 51 | + style::SetForegroundColor(Color::Red), |
| 52 | + style::Print(format!("\nError opening editor: {}\n\n", err)), |
| 53 | + style::SetForegroundColor(Color::Reset) |
| 54 | + )?; |
| 55 | + |
| 56 | + return Ok(ChatState::PromptUser { |
| 57 | + skip_printing_tools: true, |
| 58 | + }); |
| 59 | + }, |
| 60 | + }; |
| 61 | + |
| 62 | + Ok( |
| 63 | + match content.trim().is_empty() || content.trim() == initial_text.trim() { |
| 64 | + true => { |
| 65 | + execute!( |
| 66 | + session.stderr, |
| 67 | + style::SetForegroundColor(Color::Yellow), |
| 68 | + style::Print("\nNo changes made in editor, not submitting.\n\n"), |
| 69 | + style::SetForegroundColor(Color::Reset) |
| 70 | + )?; |
| 71 | + |
| 72 | + ChatState::PromptUser { |
| 73 | + skip_printing_tools: true, |
| 74 | + } |
| 75 | + }, |
| 76 | + false => { |
| 77 | + execute!( |
| 78 | + session.stderr, |
| 79 | + style::SetForegroundColor(Color::Green), |
| 80 | + style::Print("\nContent loaded from editor. Submitting prompt...\n\n"), |
| 81 | + style::SetForegroundColor(Color::Reset) |
| 82 | + )?; |
| 83 | + |
| 84 | + // Display the content as if the user typed it |
| 85 | + execute!( |
| 86 | + session.stderr, |
| 87 | + style::SetAttribute(style::Attribute::Reset), |
| 88 | + style::SetForegroundColor(Color::Magenta), |
| 89 | + style::Print("> "), |
| 90 | + style::SetAttribute(style::Attribute::Reset), |
| 91 | + style::Print(&content), |
| 92 | + style::Print("\n") |
| 93 | + )?; |
| 94 | + |
| 95 | + // Process the content as user input |
| 96 | + ChatState::HandleInput { input: content } |
| 97 | + }, |
| 98 | + }, |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
0 commit comments