cursor-helper 0.2.2

CLI helper for Cursor IDE operations not exposed in the UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Export chat command - Export chat history to readable formats
//!
//! # Disclaimer
//!
//! This tool reads chat history from local SQLite databases stored on your machine
//! by the Cursor IDE. It accesses **your own data** for personal use, backup, and
//! data portability purposes.
//!
//! This tool does NOT:
//! - Reverse engineer, decompile, or modify Cursor's source code
//! - Access Cursor's cloud services or APIs
//! - Scrape data from Cursor's servers
//! - Create derivative works of Cursor itself
//!
//! The exported data belongs to you (the user). Please respect others' privacy
//! and do not share exported conversations without consent from all participants.
//!
//! This tool is not affiliated with or endorsed by Anysphere, Inc. (Cursor).

use anyhow::{bail, Context, Result};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

use super::utils;

/// Output format for chat export
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportFormat {
    Markdown,
    Json,
}

impl ExportFormat {
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "md" | "markdown" => Some(Self::Markdown),
            "json" => Some(Self::Json),
            _ => None,
        }
    }
}

/// Export options
#[derive(Debug, Clone, Default)]
pub struct ExportOptions {
    /// Include thinking/reasoning blocks
    pub with_thinking: bool,
    /// Include tool calls
    pub with_tools: bool,
    /// Include model info and token counts
    pub with_stats: bool,
    /// Include archived chat sessions
    pub include_archived: bool,
    /// Exclude sessions with no messages
    pub exclude_blank: bool,
}

/// Tool call information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    /// Tool name (e.g., "read_file_v2", "edit_file")
    pub name: String,
    /// Tool parameters
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<String>,
    /// Tool result (truncated for large outputs)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<String>,
    /// Status: completed, failed, etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// A single message in a chat conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    /// Role: "user", "assistant", "tool", or "thinking"
    pub role: String,
    /// Message content
    pub content: String,
    /// Timestamp if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<i64>,
    /// Thinking duration in ms (for thinking messages)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_duration_ms: Option<i64>,
    /// Tool call info (for tool messages)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call: Option<ToolCall>,
    /// Model used (for assistant messages)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Token count
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokens: Option<TokenCount>,
}

/// Token usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenCount {
    pub input: i64,
    pub output: i64,
}

/// A chat session/conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatSession {
    /// Session UUID
    pub id: String,
    /// Session title if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Messages in the conversation
    pub messages: Vec<ChatMessage>,
    /// When the session was created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<i64>,
    /// When the session was last updated
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<i64>,
}

/// Export result containing all chat sessions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatExport {
    /// Project path
    pub project_path: String,
    /// Export timestamp
    pub exported_at: i64,
    /// All chat sessions
    pub sessions: Vec<ChatSession>,
}

/// Execute the export-chat command
pub fn execute(
    project_path: &str,
    format: ExportFormat,
    output: Option<&str>,
    options: &ExportOptions,
    split: bool,
) -> Result<()> {
    let project_path = PathBuf::from(project_path);

    // Try to canonicalize for local paths, but allow remote paths that don't exist locally
    let (project_path, is_remote) = if project_path.exists() {
        let canonical = project_path
            .canonicalize()
            .with_context(|| format!("Failed to resolve: {}", project_path.display()))?;
        // On Windows, canonicalize() returns \\?\ prefix which we need to strip
        let canonical = utils::strip_windows_prefix(&canonical);
        (canonical, false)
    } else {
        // Path doesn't exist locally - might be a remote path
        // Make it absolute if it's relative
        let abs_path = if project_path.is_absolute() {
            project_path
        } else {
            std::env::current_dir()?.join(&project_path)
        };
        (abs_path, true)
    };

    // Find workspace storage for this project
    let workspace_dir = utils::find_workspace_dir(&project_path)?;

    let Some(workspace_dir) = workspace_dir else {
        if is_remote {
            bail!(
                "No Cursor workspace data found for remote path: {}\n\
                 Hint: For remote sessions, use the exact path as shown in Cursor\n\
                 (e.g., /home/user/project for SSH/tunnel connections)",
                project_path.display()
            );
        } else {
            bail!(
                "No Cursor workspace data found for: {}",
                project_path.display()
            );
        }
    };

    // Extract chat sessions
    let mut sessions = extract_chat_sessions(&workspace_dir, options)?;

    // Filter blank sessions if requested
    if options.exclude_blank {
        let before = sessions.len();
        sessions.retain(|s| !s.messages.is_empty());
        let filtered = before - sessions.len();
        if filtered > 0 {
            println!("Filtered {} blank session(s)", filtered);
        }
    }

    if sessions.is_empty() {
        println!("No chat sessions found for this project.");
        return Ok(());
    }

    println!("Found {} chat session(s)", sessions.len());

    let project_path_str = project_path.to_string_lossy().to_string();
    let exported_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);

    // Handle split output
    if split {
        let output_dir = output.ok_or_else(|| {
            anyhow::anyhow!("--split requires --output to specify the output directory")
        })?;

        write_split_output(
            &sessions,
            output_dir,
            format,
            &project_path_str,
            exported_at,
        )?;
    } else {
        // Build single export
        let export = ChatExport {
            project_path: project_path_str,
            exported_at,
            sessions,
        };

        // Format output
        let content = match format {
            ExportFormat::Markdown => format_as_markdown(&export),
            ExportFormat::Json => serde_json::to_string_pretty(&export)?,
        };

        // Write or print
        if let Some(output_path) = output {
            fs::write(output_path, &content)
                .with_context(|| format!("Failed to write: {}", output_path))?;
            println!("Exported to: {}", output_path);
        } else {
            println!("{}", content);
        }
    }

    Ok(())
}

/// Execute the export-chat command using workspace ID directly
///
/// This is useful for remote sessions where the path doesn't exist locally.
/// Use `cursor-helper list` to find workspace IDs.
pub fn execute_by_id(
    workspace_id: &str,
    format: ExportFormat,
    output: Option<&str>,
    options: &ExportOptions,
    split: bool,
) -> Result<()> {
    let workspace_storage_dir = crate::config::workspace_storage_dir()?;
    let workspace_dir = workspace_storage_dir.join(workspace_id);

    if !workspace_dir.exists() {
        bail!(
            "Workspace not found: {}\n\
             Hint: Use 'cursor-helper list' to see available workspaces",
            workspace_id
        );
    }

    // Try to read the project path from workspace.json
    let project_path = {
        let workspace_json = workspace_dir.join("workspace.json");
        if workspace_json.exists() {
            let content = fs::read_to_string(&workspace_json)?;
            let ws: serde_json::Value = serde_json::from_str(&content)?;
            ws.get("folder")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .unwrap_or_else(|| format!("workspace:{}", workspace_id))
        } else {
            format!("workspace:{}", workspace_id)
        }
    };

    // Extract chat sessions
    let mut sessions = extract_chat_sessions(&workspace_dir, options)?;

    // Filter blank sessions if requested
    if options.exclude_blank {
        let before = sessions.len();
        sessions.retain(|s| !s.messages.is_empty());
        let filtered = before - sessions.len();
        if filtered > 0 {
            println!("Filtered {} blank session(s)", filtered);
        }
    }

    if sessions.is_empty() {
        println!("No chat sessions found for this workspace.");
        return Ok(());
    }

    println!("Found {} chat session(s)", sessions.len());

    let exported_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);

    // Handle split output
    if split {
        let output_dir = output.ok_or_else(|| {
            anyhow::anyhow!("--split requires --output to specify the output directory")
        })?;

        write_split_output(&sessions, output_dir, format, &project_path, exported_at)?;
    } else {
        // Build single export
        let export = ChatExport {
            project_path,
            exported_at,
            sessions,
        };

        // Format output
        let content = match format {
            ExportFormat::Markdown => format_as_markdown(&export),
            ExportFormat::Json => serde_json::to_string_pretty(&export)?,
        };

        // Write or print
        if let Some(output_path) = output {
            fs::write(output_path, &content)
                .with_context(|| format!("Failed to write: {}", output_path))?;
            println!("Exported to: {}", output_path);
        } else {
            println!("{}", content);
        }
    }

    Ok(())
}

/// Extract chat sessions from a workspace directory
fn extract_chat_sessions(
    workspace_dir: &Path,
    options: &ExportOptions,
) -> Result<Vec<ChatSession>> {
    let db_path = workspace_dir.join("state.vscdb");

    if !db_path.exists() {
        return Ok(vec![]);
    }

    let conn = Connection::open_with_flags(
        &db_path,
        rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
    )
    .with_context(|| format!("Failed to open database: {}", db_path.display()))?;

    // Query composer metadata from workspace storage
    // QueryReturnedNoRows means no chat sessions exist, not an error
    let composer_data: Option<String> = match conn.query_row(
        "SELECT value FROM ItemTable WHERE key = 'composer.composerData'",
        [],
        |row| row.get(0),
    ) {
        Ok(data) => Some(data),
        Err(rusqlite::Error::QueryReturnedNoRows) => None,
        Err(e) => {
            return Err(e).with_context(|| {
                format!("Failed to query chat metadata from: {}", db_path.display())
            })
        }
    };

    // Parse composer metadata to get session info
    let composers: Vec<ComposerInfo> = composer_data
        .as_ref()
        .and_then(|data| parse_composer_data(data, options.include_archived))
        .unwrap_or_default();

    if composers.is_empty() {
        return Ok(vec![]);
    }

    // Open global storage for bubble content (optional - may not exist on all setups)
    let global_db_path = crate::config::global_storage_dir()
        .ok()
        .map(|d| d.join("state.vscdb"))
        .filter(|p| p.exists());

    let global_conn = global_db_path.and_then(|path| {
        Connection::open_with_flags(
            &path,
            rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
        )
        .ok() // Global storage is optional - proceed without messages if unavailable
    });

    // Build sessions with messages from global storage
    let mut sessions = Vec::new();

    for composer in composers {
        let messages = if let Some(ref gconn) = global_conn {
            fetch_session_messages(gconn, &composer.composer_id, options).unwrap_or_default()
        } else {
            vec![]
        };

        sessions.push(ChatSession {
            id: composer.composer_id.clone(),
            title: Some(composer.name.clone()),
            messages,
            created_at: Some(composer.created_at / 1000),
            updated_at: Some(composer.last_updated_at / 1000),
        });
    }

    // Sort by creation time (newest first)
    sessions.sort_by(|a, b| b.created_at.cmp(&a.created_at));

    Ok(sessions)
}

/// Fetch messages for a session from global storage
fn fetch_session_messages(
    conn: &Connection,
    composer_id: &str,
    options: &ExportOptions,
) -> Result<Vec<ChatMessage>> {
    let composer_key = format!("composerData:{}", composer_id);

    // Get composer data (stored as TEXT in cursorDiskKV)
    let composer_str: String = match conn.query_row(
        "SELECT value FROM cursorDiskKV WHERE key = ?1",
        rusqlite::params![&composer_key],
        |row| row.get::<_, String>(0),
    ) {
        Ok(s) => s,
        Err(_) => return Ok(vec![]), // Session not found in global storage
    };

    let composer_data: serde_json::Value = serde_json::from_str(&composer_str)?;

    // Get bubble headers (bubbleId + type)
    let Some(headers) = composer_data
        .get("fullConversationHeadersOnly")
        .and_then(|v| v.as_array())
    else {
        return Ok(vec![]);
    };

    let mut messages = Vec::new();

    for header in headers {
        let Some(bubble_id) = header.get("bubbleId").and_then(|v| v.as_str()) else {
            continue;
        };
        let bubble_type = header.get("type").and_then(|v| v.as_i64()).unwrap_or(0);

        // Fetch bubble content
        let bubble_key = format!("bubbleId:{}:{}", composer_id, bubble_id);
        let bubble_str: Option<String> = conn
            .query_row(
                "SELECT value FROM cursorDiskKV WHERE key = ?",
                [&bubble_key],
                |row| row.get(0),
            )
            .ok();

        if let Some(json_str) = bubble_str {
            if let Ok(bubble) = serde_json::from_str::<serde_json::Value>(&json_str) {
                // Parse timestamp from ISO string
                let timestamp = bubble
                    .get("createdAt")
                    .and_then(|v| v.as_str())
                    .and_then(parse_iso_timestamp);

                // Check for thinking block (capabilityType=30 with thinking field)
                if options.with_thinking {
                    if let Some(thinking) = bubble.get("thinking").and_then(|t| t.as_object()) {
                        if let Some(thinking_text) = thinking.get("text").and_then(|v| v.as_str()) {
                            if !thinking_text.is_empty() {
                                let thinking_duration =
                                    bubble.get("thinkingDurationMs").and_then(|v| v.as_i64());

                                messages.push(ChatMessage {
                                    role: "thinking".to_string(),
                                    content: thinking_text.to_string(),
                                    timestamp,
                                    thinking_duration_ms: thinking_duration,
                                    tool_call: None,
                                    model: None,
                                    tokens: None,
                                });
                            }
                        }
                    }
                }

                // Check for tool call (capabilityType=15 with toolFormerData)
                if options.with_tools {
                    if let Some(tool_data) =
                        bubble.get("toolFormerData").and_then(|t| t.as_object())
                    {
                        let tool_name = tool_data
                            .get("name")
                            .and_then(|v| v.as_str())
                            .unwrap_or("unknown")
                            .to_string();

                        let params = tool_data
                            .get("params")
                            .and_then(|v| v.as_str())
                            .map(|s| truncate_str(s, 500));

                        let result = tool_data
                            .get("result")
                            .and_then(|v| v.as_str())
                            .map(|s| truncate_str(s, 1000));

                        let status = tool_data
                            .get("status")
                            .and_then(|v| v.as_str())
                            .map(|s| s.to_string());

                        messages.push(ChatMessage {
                            role: "tool".to_string(),
                            content: format!("[{}]", tool_name),
                            timestamp,
                            thinking_duration_ms: None,
                            tool_call: Some(ToolCall {
                                name: tool_name,
                                params,
                                result,
                                status,
                            }),
                            model: None,
                            tokens: None,
                        });

                        continue; // Tool calls don't have regular text content
                    }
                }

                // Regular message content
                let text = bubble
                    .get("text")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();

                if !text.is_empty() {
                    let role = match bubble_type {
                        1 => "user",
                        2 => "assistant",
                        _ => "unknown",
                    };

                    // Extract model info and tokens if requested
                    let model = if options.with_stats {
                        bubble
                            .get("modelInfo")
                            .and_then(|m| m.get("modelName"))
                            .and_then(|v| v.as_str())
                            .map(|s| s.to_string())
                    } else {
                        None
                    };

                    let tokens = if options.with_stats {
                        bubble.get("tokenCount").and_then(|tc| {
                            let input = tc.get("inputTokens").and_then(|v| v.as_i64())?;
                            let output = tc.get("outputTokens").and_then(|v| v.as_i64())?;
                            if input > 0 || output > 0 {
                                Some(TokenCount { input, output })
                            } else {
                                None
                            }
                        })
                    } else {
                        None
                    };

                    messages.push(ChatMessage {
                        role: role.to_string(),
                        content: text,
                        timestamp,
                        thinking_duration_ms: None,
                        tool_call: None,
                        model,
                        tokens,
                    });
                }
            }
        }
    }

    Ok(messages)
}

/// Parse ISO 8601 timestamp to Unix timestamp
fn parse_iso_timestamp(s: &str) -> Option<i64> {
    // Simple parsing for "2026-01-19T04:31:31.394Z" format
    chrono::DateTime::parse_from_rfc3339(s)
        .ok()
        .map(|dt| dt.timestamp())
}

/// Truncate string to max length (char-safe)
fn truncate_str(s: &str, max_chars: usize) -> String {
    let char_count = s.chars().count();
    if char_count <= max_chars {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max_chars).collect();
        format!("{}...[truncated]", truncated)
    }
}

/// Composer metadata from composer.composerData
#[derive(Debug, Clone)]
struct ComposerInfo {
    composer_id: String,
    name: String,
    created_at: i64,
    last_updated_at: i64,
}

/// Parse composer.composerData JSON
fn parse_composer_data(data: &str, include_archived: bool) -> Option<Vec<ComposerInfo>> {
    let json: serde_json::Value = serde_json::from_str(data).ok()?;
    let composers = json.get("allComposers")?.as_array()?;

    let mut result = Vec::new();
    for c in composers {
        // Skip archived composers unless explicitly included
        let is_archived = c
            .get("isArchived")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        if is_archived && !include_archived {
            continue;
        }

        // Try to parse each composer, skip if any required field is missing
        let Some(composer_id) = c.get("composerId").and_then(|v| v.as_str()) else {
            continue;
        };
        let name = c
            .get("name")
            .and_then(|n| n.as_str())
            .unwrap_or("Untitled")
            .to_string();
        let Some(created_at) = c.get("createdAt").and_then(|v| v.as_i64()) else {
            continue;
        };
        let last_updated_at = c
            .get("lastUpdatedAt")
            .and_then(|v| v.as_i64())
            .unwrap_or(created_at);

        result.push(ComposerInfo {
            composer_id: composer_id.to_string(),
            name,
            created_at,
            last_updated_at,
        });
    }

    Some(result)
}

/// Format a single message as markdown
///
/// `heading` is the markdown heading prefix (e.g., "##" or "###")
fn format_message_as_markdown(msg: &ChatMessage, heading: &str) -> String {
    let mut md = String::new();

    match msg.role.as_str() {
        "thinking" => {
            md.push_str(&format!("{} 💭 **Thinking**", heading));
            if let Some(duration) = msg.thinking_duration_ms {
                md.push_str(&format!(" _{:.1}s_", duration as f64 / 1000.0));
            }
            md.push_str("\n\n");
            md.push_str("<details>\n<summary>Click to expand thinking...</summary>\n\n");
            md.push_str(&msg.content);
            md.push_str("\n\n</details>\n\n");
        }
        "tool" => {
            if let Some(ref tc) = msg.tool_call {
                md.push_str(&format!("{} 🔧 **Tool: {}**", heading, tc.name));
                if let Some(ref status) = tc.status {
                    md.push_str(&format!(" [{}]", status));
                }
                md.push_str("\n\n");

                if let Some(ref params) = tc.params {
                    md.push_str("<details>\n<summary>Parameters</summary>\n\n```json\n");
                    md.push_str(params);
                    md.push_str("\n```\n\n</details>\n\n");
                }

                if let Some(ref result) = tc.result {
                    md.push_str("<details>\n<summary>Result</summary>\n\n```\n");
                    md.push_str(result);
                    md.push_str("\n```\n\n</details>\n\n");
                }
            }
        }
        _ => {
            let role_display = match msg.role.as_str() {
                "user" => "**User**",
                "assistant" => "**Assistant**",
                "system" => "**System**",
                other => other,
            };

            md.push_str(&format!("{} {}", heading, role_display));

            // Add model info if present
            if let Some(ref model) = msg.model {
                md.push_str(&format!(" _{}_", model));
            }

            // Add token count if present
            if let Some(ref tokens) = msg.tokens {
                if tokens.input > 0 || tokens.output > 0 {
                    md.push_str(&format!(" ({}{}↑)", tokens.input, tokens.output));
                }
            }

            md.push_str("\n\n");
            md.push_str(&msg.content);
            md.push_str("\n\n");
        }
    }

    md
}

/// Format export as markdown
fn format_as_markdown(export: &ChatExport) -> String {
    let mut md = String::new();

    md.push_str(&format!("# Chat Export: {}\n\n", export.project_path));
    md.push_str(&format!(
        "_Exported: {}_\n\n",
        format_timestamp(export.exported_at)
    ));
    md.push_str("---\n\n");

    for (i, session) in export.sessions.iter().enumerate() {
        let title = session.title.as_deref().unwrap_or("Untitled Session");

        md.push_str(&format!("## Session {}: {}\n\n", i + 1, title));

        if let Some(created) = session.created_at {
            md.push_str(&format!("_Created: {}_\n\n", format_timestamp(created)));
        }

        for msg in &session.messages {
            md.push_str(&format_message_as_markdown(msg, "###"));
        }

        md.push_str("---\n\n");
    }

    md
}

/// Write sessions to separate files in a directory
fn write_split_output(
    sessions: &[ChatSession],
    output_dir: &str,
    format: ExportFormat,
    project_path: &str,
    exported_at: i64,
) -> Result<()> {
    // Create output directory
    fs::create_dir_all(output_dir)
        .with_context(|| format!("Failed to create directory: {}", output_dir))?;

    let ext = match format {
        ExportFormat::Markdown => "md",
        ExportFormat::Json => "json",
    };

    for (i, session) in sessions.iter().enumerate() {
        let title = session.title.as_deref().unwrap_or("Untitled");
        let safe_title = sanitize_filename(title);
        let filename = format!("{:03}-{}.{}", i + 1, safe_title, ext);
        let file_path = Path::new(output_dir).join(&filename);

        let content = match format {
            ExportFormat::Markdown => format_single_session_as_markdown(session, i + 1),
            ExportFormat::Json => {
                let single_export = ChatExport {
                    project_path: project_path.to_string(),
                    exported_at,
                    sessions: vec![session.clone()],
                };
                serde_json::to_string_pretty(&single_export)?
            }
        };

        fs::write(&file_path, &content)
            .with_context(|| format!("Failed to write: {}", file_path.display()))?;
    }

    println!(
        "Exported {} sessions to directory: {}",
        sessions.len(),
        output_dir
    );

    Ok(())
}

/// Sanitize a string for use as a filename
fn sanitize_filename(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
            c if c.is_control() => '_',
            c => c,
        })
        .take(50) // Limit filename length
        .collect::<String>()
        .trim()
        .to_string()
}

/// Format a single session as markdown (for split output)
fn format_single_session_as_markdown(session: &ChatSession, index: usize) -> String {
    let mut md = String::new();

    let title = session.title.as_deref().unwrap_or("Untitled Session");
    md.push_str(&format!("# Session {}: {}\n\n", index, title));

    if let Some(created) = session.created_at {
        md.push_str(&format!("_Created: {}_\n\n", format_timestamp(created)));
    }

    md.push_str("---\n\n");

    for msg in &session.messages {
        md.push_str(&format_message_as_markdown(msg, "##"));
    }

    md
}

/// Format unix timestamp as human-readable string
fn format_timestamp(ts: i64) -> String {
    chrono::DateTime::from_timestamp(ts, 0)
        .map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
        .unwrap_or_else(|| ts.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_export_format() {
        assert_eq!(ExportFormat::from_str("md"), Some(ExportFormat::Markdown));
        assert_eq!(
            ExportFormat::from_str("markdown"),
            Some(ExportFormat::Markdown)
        );
        assert_eq!(ExportFormat::from_str("json"), Some(ExportFormat::Json));
        assert_eq!(ExportFormat::from_str("xml"), None);
    }

    #[test]
    fn test_export_format_case_insensitive() {
        assert_eq!(ExportFormat::from_str("MD"), Some(ExportFormat::Markdown));
        assert_eq!(ExportFormat::from_str("JSON"), Some(ExportFormat::Json));
        assert_eq!(
            ExportFormat::from_str("Markdown"),
            Some(ExportFormat::Markdown)
        );
    }

    #[test]
    fn test_truncate_str_short() {
        // String shorter than limit should be unchanged
        assert_eq!(truncate_str("hello", 10), "hello");
    }

    #[test]
    fn test_truncate_str_exact() {
        // String exactly at limit should be unchanged
        assert_eq!(truncate_str("hello", 5), "hello");
    }

    #[test]
    fn test_truncate_str_long() {
        // String longer than limit should be truncated
        let result = truncate_str("hello world", 5);
        assert!(result.starts_with("hello"));
        assert!(result.ends_with("...[truncated]"));
    }

    #[test]
    fn test_truncate_str_unicode() {
        // Unicode characters should be handled correctly (char-safe)
        let result = truncate_str("你好世界", 2);
        assert!(result.starts_with("你好"));
        assert!(result.ends_with("...[truncated]"));
    }

    #[test]
    fn test_sanitize_filename_basic() {
        assert_eq!(sanitize_filename("hello world"), "hello world");
    }

    #[test]
    fn test_sanitize_filename_special_chars() {
        // Special characters should be replaced with underscore
        assert_eq!(
            sanitize_filename("file/with:bad*chars"),
            "file_with_bad_chars"
        );
        assert_eq!(sanitize_filename("test<>pipe|"), "test__pipe_");
    }

    #[test]
    fn test_sanitize_filename_length_limit() {
        // Long filenames should be truncated to 50 chars
        let long_name = "a".repeat(100);
        let result = sanitize_filename(&long_name);
        assert_eq!(result.len(), 50);
    }

    #[test]
    fn test_format_timestamp() {
        // Known timestamp: 2024-01-01 00:00:00 UTC
        let ts = 1704067200;
        let result = format_timestamp(ts);
        assert!(result.contains("2024-01-01"));
        assert!(result.contains("UTC"));
    }

    #[test]
    fn test_format_timestamp_zero() {
        // Unix epoch
        let result = format_timestamp(0);
        assert!(result.contains("1970-01-01"));
    }

    #[test]
    fn test_format_message_user() {
        let msg = ChatMessage {
            role: "user".to_string(),
            content: "Hello, world!".to_string(),
            timestamp: None,
            thinking_duration_ms: None,
            tool_call: None,
            model: None,
            tokens: None,
        };
        let result = format_message_as_markdown(&msg, "##");
        assert!(result.contains("## **User**"));
        assert!(result.contains("Hello, world!"));
    }

    #[test]
    fn test_format_message_assistant_with_model() {
        let msg = ChatMessage {
            role: "assistant".to_string(),
            content: "I can help with that.".to_string(),
            timestamp: None,
            thinking_duration_ms: None,
            tool_call: None,
            model: Some("gpt-4".to_string()),
            tokens: Some(TokenCount {
                input: 100,
                output: 50,
            }),
        };
        let result = format_message_as_markdown(&msg, "###");
        assert!(result.contains("### **Assistant**"));
        assert!(result.contains("_gpt-4_"));
        assert!(result.contains("100↓"));
        assert!(result.contains("50↑"));
    }

    #[test]
    fn test_format_message_thinking() {
        let msg = ChatMessage {
            role: "thinking".to_string(),
            content: "Let me think about this...".to_string(),
            timestamp: None,
            thinking_duration_ms: Some(5000),
            tool_call: None,
            model: None,
            tokens: None,
        };
        let result = format_message_as_markdown(&msg, "##");
        assert!(result.contains("## 💭 **Thinking**"));
        assert!(result.contains("_5.0s_"));
        assert!(result.contains("<details>"));
        assert!(result.contains("Let me think about this..."));
    }

    #[test]
    fn test_format_message_tool() {
        let msg = ChatMessage {
            role: "tool".to_string(),
            content: "[read_file]".to_string(),
            timestamp: None,
            thinking_duration_ms: None,
            tool_call: Some(ToolCall {
                name: "read_file".to_string(),
                params: Some(r#"{"path": "/test.rs"}"#.to_string()),
                result: Some("file contents...".to_string()),
                status: Some("completed".to_string()),
            }),
            model: None,
            tokens: None,
        };
        let result = format_message_as_markdown(&msg, "###");
        assert!(result.contains("### 🔧 **Tool: read_file**"));
        assert!(result.contains("[completed]"));
        assert!(result.contains("Parameters"));
        assert!(result.contains("Result"));
    }
}