Skip to content

Commit 0927d8b

Browse files
sangggggclaude
andauthored
Add export-session CLI command for exporting session transcripts to JSON (#84)
This adds a new CLI command `export-session` that exports a specific session to JSON format using the build_session_transcript function. - Make build_session_transcript public in data_collector.rs - Add ExportSession command variant to CLI Commands enum - Implement handle_export_session_command handler in query.rs - Supports output to file (--output) or stdout Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4e81dc2 commit 0927d8b

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/cli/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ pub enum Commands {
7878
session_id: String,
7979
},
8080

81+
/// Export a session transcript to JSON file
82+
ExportSession {
83+
/// Session ID to export
84+
session_id: String,
85+
/// Output file path (prints to stdout if not specified)
86+
#[arg(short, long)]
87+
output: Option<String>,
88+
},
89+
8190
/// Search messages by content
8291
Search {
8392
/// Search query
@@ -300,6 +309,10 @@ impl Cli {
300309
query::handle_session_detail_command(session_id).await
301310
}
302311

312+
Commands::ExportSession { session_id, output } => {
313+
query::handle_export_session_command(session_id, output).await
314+
}
315+
303316
Commands::Search {
304317
query,
305318
limit,

src/cli/query.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use crate::database::DatabaseManager;
1+
use crate::database::{
2+
ChatSessionRepository, DatabaseManager, MessageRepository, ToolOperationRepository,
3+
};
24
use crate::models::Message;
5+
use crate::services::analytics::build_session_transcript;
36
use crate::services::{QueryService, SearchRequest, SessionDetailRequest, SessionsQueryRequest};
47
use crate::utils::time_parser;
58
use anyhow::Result;
@@ -306,6 +309,47 @@ fn is_tool_message(content: &str) -> bool {
306309
|| content.trim().starts_with("[Tool Result]")
307310
}
308311

312+
/// Export a session transcript to JSON
313+
pub async fn handle_export_session_command(
314+
session_id: String,
315+
output: Option<String>,
316+
) -> Result<()> {
317+
let db_path = crate::database::config::get_default_db_path()?;
318+
let db_manager = DatabaseManager::new(&db_path).await?;
319+
320+
// Get repositories
321+
let session_repo = ChatSessionRepository::new(&db_manager);
322+
let message_repo = MessageRepository::new(&db_manager);
323+
let tool_op_repo = ToolOperationRepository::new(&db_manager);
324+
325+
// Parse session_id to UUID
326+
let session_uuid = uuid::Uuid::parse_str(&session_id)
327+
.map_err(|e| anyhow::anyhow!("Invalid session ID format: {e}"))?;
328+
329+
// Get session data
330+
let session = session_repo
331+
.get_by_id(&session_uuid)
332+
.await?
333+
.ok_or_else(|| anyhow::anyhow!("Session not found: {session_id}"))?;
334+
335+
// Get messages and tool operations
336+
let messages = message_repo.get_by_session(&session_uuid).await?;
337+
let tool_operations = tool_op_repo.get_by_session(&session_uuid).await?;
338+
339+
// Build the session transcript
340+
let transcript = build_session_transcript(&messages, &tool_operations, &session)?;
341+
342+
// Output to file or stdout
343+
if let Some(output_path) = output {
344+
std::fs::write(&output_path, &transcript)?;
345+
println!("Session exported to: {output_path}");
346+
} else {
347+
println!("{transcript}");
348+
}
349+
350+
Ok(())
351+
}
352+
309353
#[cfg(test)]
310354
mod tests {
311355
use super::*;

src/services/analytics/data_collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ pub async fn collect_qualitative_data(
5454
// =============================================================================
5555

5656
/// Builds a JSON string representation of the session transcript with embedded tool uses.
57-
fn build_session_transcript(
57+
/// This is the public API for exporting session transcripts.
58+
pub fn build_session_transcript(
5859
messages: &[Message],
5960
tool_operations: &[ToolOperation],
6061
session: &ChatSession,

0 commit comments

Comments
 (0)