Skip to content

Adding command to export conversation to markdown #2284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions crates/chat-cli/src/cli/chat/cli/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::path::PathBuf;

use clap::Parser;
use crossterm::execute;
use crossterm::style::{
self,
Attribute,
Color,
};

use crate::cli::chat::{
ChatError,
ChatSession,
ChatState,
};
use crate::os::Os;

/// Export the conversation transcript to a markdown file
#[derive(Debug, PartialEq, Parser)]
pub struct ExportArgs {
/// Path to save the markdown file
#[arg(required = true)]
pub path: PathBuf,

/// Force overwrite if file exists
#[arg(short, long)]
pub force: bool,
}

impl ExportArgs {
pub async fn execute(&self, os: &mut Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
let transcript = self.get_markdown_transcript(session);

if os.fs.exists(&self.path) && !self.force {
execute!(
session.stderr,
style::SetForegroundColor(Color::Red),
style::Print(format!(
"\nFile at {} already exists. To overwrite, use -f or --force\n\n",
&self.path.display()
)),
style::SetAttribute(Attribute::Reset)
)?;

return Ok(ChatState::PromptUser {
skip_printing_tools: false,
});
}

match os.fs.write(&self.path, transcript).await {
Ok(_) => {
execute!(
session.stderr,
style::SetForegroundColor(Color::Green),
style::Print(format!("\n✔ Conversation exported to {}\n\n", self.path.display())),
style::SetAttribute(Attribute::Reset)
)?;
},
Err(err) => {
return Err(ChatError::Custom(
format!("Failed to export conversation: {}", err).into(),
));
},
}

Ok(ChatState::PromptUser {
skip_printing_tools: false,
})
}

fn get_markdown_transcript(&self, session: &ChatSession) -> String {
let mut markdown = String::from("# Amazon Q Conversation\n\n");

for message in &session.conversation.transcript {
if message.starts_with("> ") {
markdown.push_str(&format!("## User\n\n{}\n\n", message));
} else {
markdown.push_str(&format!("## Assistant\n\n{}\n\n", message));
}
}

markdown
}
}
5 changes: 5 additions & 0 deletions crates/chat-cli/src/cli/chat/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod clear;
pub mod compact;
pub mod context;
pub mod editor;
pub mod export;
pub mod hooks;
pub mod knowledge;
pub mod mcp;
Expand All @@ -18,6 +19,7 @@ use clear::ClearArgs;
use compact::CompactArgs;
use context::ContextSubcommand;
use editor::EditorArgs;
use export::ExportArgs;
use hooks::HooksArgs;
use knowledge::KnowledgeSubcommand;
use mcp::McpArgs;
Expand Down Expand Up @@ -76,6 +78,8 @@ pub enum SlashCommand {
Mcp(McpArgs),
/// Select a model for the current conversation session
Model(ModelArgs),
/// Export the conversation transcript to a markdown file
Export(ExportArgs),
/// Upgrade to a Q Developer Pro subscription for increased query limits
Subscribe(SubscribeArgs),
#[command(flatten)]
Expand Down Expand Up @@ -109,6 +113,7 @@ impl SlashCommand {
Self::Usage(args) => args.execute(os, session).await,
Self::Mcp(args) => args.execute(session).await,
Self::Model(args) => args.execute(session).await,
Self::Export(args) => args.execute(os, session).await,
Self::Subscribe(args) => args.execute(os, session).await,
Self::Persist(subcommand) => subcommand.execute(os, session).await,
// Self::Root(subcommand) => {
Expand Down
1 change: 1 addition & 0 deletions crates/chat-cli/src/cli/chat/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub const COMMANDS: &[&str] = &[
"/compact",
"/compact help",
"/usage",
"/export",
"/save",
"/load",
"/subscribe",
Expand Down