From 9d95a63ed2ef17f0cbcf32ed2ea2b597d4ca1e37 Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Thu, 27 Mar 2025 14:05:10 +0100 Subject: [PATCH] first commit --- export_cursor_chats_to_md.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 export_cursor_chats_to_md.py diff --git a/export_cursor_chats_to_md.py b/export_cursor_chats_to_md.py new file mode 100644 index 0000000..5a9227f --- /dev/null +++ b/export_cursor_chats_to_md.py @@ -0,0 +1,49 @@ +import os +import json +from datetime import datetime + +# === Config === +CURSOR_CHAT_DIR = os.path.expanduser("~/.cursor/chat/") +OUTPUT_FILE = "cursor_history.md" + +# === Initialisation du contenu === +md_output = "" + +# === Chargement des discussions Cursor === +for filename in sorted(os.listdir(CURSOR_CHAT_DIR)): + if not filename.endswith(".json"): + continue + + filepath = os.path.join(CURSOR_CHAT_DIR, filename) + + with open(filepath, "r", encoding="utf-8") as f: + try: + chat_data = json.load(f) + except json.JSONDecodeError: + continue # Fichier corrompu ou non lisible + + created_at_raw = chat_data.get("createdAt", "") + try: + created_at = datetime.fromisoformat(created_at_raw.replace("Z", "")) + except ValueError: + created_at = datetime.now() + + formatted_time = created_at.strftime("%Y-%m-%d %H:%M:%S") + md_output += f"\n---\n\n## Session du {formatted_time}\n\n" + + for msg in chat_data.get("messages", []): + role = msg.get("role", "") + content = msg.get("content", "").strip() + if not content: + continue + + if role == "user": + md_output += f"** Utilisateur :**\n{content}\n\n" + elif role == "assistant": + md_output += f"** Assistant :**\n{content}\n\n" + +# === Écriture / ajout dans le fichier final === +with open(OUTPUT_FILE, "a", encoding="utf-8") as output_file: + output_file.write(md_output) + +print(f" Export terminé ! Discussions ajoutées à : {OUTPUT_FILE}")