forked from somogyijanos/cursor-chat-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
executable file
·220 lines (183 loc) · 8.61 KB
/
chat.py
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
#!/usr/bin/env python
import os
import sys
import typer
from src.vscdb import VSCDBQuery
from src.export import ChatExporter, MarkdownChatFormatter, MarkdownFileSaver
from rich.console import Console
from rich.markdown import Markdown
from loguru import logger
import json
import yaml
import platform
from pathlib import Path
logger.remove()
logger.add(sys.stderr, level="INFO")
app = typer.Typer()
console = Console()
@app.command()
def export(
db_path: str = typer.Argument(None, help="The path to the SQLite database file. If not provided, the latest workspace will be used."),
output_dir: str = typer.Option(None, help="The directory where the output markdown files will be saved. If not provided, prints to command line."),
latest_tab: bool = typer.Option(False, "--latest-tab", help="Export only the latest tab. If not set, all tabs will be exported.")
):
"""
Export chat data from the database to markdown files or print it to the command line.
"""
if not db_path:
db_path = get_latest_workspace_db_path()
image_dir = None
try:
# Query the AI chat data from the database
db_query = VSCDBQuery(db_path)
chat_data = db_query.query_aichat_data()
if "error" in chat_data:
error_message = f"Error querying chat data: {chat_data['error']}"
logger.error(error_message)
raise typer.Exit(code=1)
# Convert the chat data from JSON string to dictionary
chat_data_dict = json.loads(chat_data[0])
if latest_tab:
# Get the latest tab by timestamp
latest_tab = max(chat_data_dict['tabs'], key=lambda tab: tab.get('timestamp', 0))
chat_data_dict['tabs'] = [latest_tab]
# Check if there are any images in the chat data
has_images = any('image' in bubble for tab in chat_data_dict['tabs'] for bubble in tab.get('bubbles', []))
if has_images and output_dir:
image_dir = os.path.join(output_dir, 'images')
# Format the chat data
formatter = MarkdownChatFormatter()
formatted_chats = formatter.format(chat_data_dict, image_dir)
if output_dir:
# Save the chat data
saver = MarkdownFileSaver()
exporter = ChatExporter(formatter, saver)
exporter.export(chat_data_dict, output_dir, image_dir)
success_message = f"Chat data has been successfully exported to {output_dir}"
logger.info(success_message)
else:
# Print the chat data to the command line using markdown
for formatted_data in formatted_chats:
console.print(Markdown(formatted_data))
logger.info("Chat data has been successfully printed to the command line")
except KeyError as e:
error_message = f"KeyError: {e}. The chat data structure is not as expected. Please check the database content."
logger.error(error_message)
raise typer.Exit(code=1)
except json.JSONDecodeError as e:
error_message = f"JSON decode error: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
except FileNotFoundError as e:
error_message = f"File not found: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
except Exception as e:
error_message = f"Failed to export chat data: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
def get_cursor_workspace_path() -> Path:
config_path = Path("config.yml")
logger.debug(f"Looking for configuration file at: {config_path}")
if not config_path.exists():
error_message = f"Configuration file not found: {config_path}"
logger.error(error_message)
raise FileNotFoundError(error_message)
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
logger.debug("Configuration file loaded successfully")
system = platform.system()
logger.debug(f"Detected operating system: {system}")
if system not in config["default_vscdb_dir_paths"]:
error_message = f"Unsupported operating system: {system}"
logger.error(error_message)
raise ValueError(error_message)
base_path = Path(os.path.expandvars(config["default_vscdb_dir_paths"][system])).expanduser()
logger.debug(f"Resolved base path: {base_path}")
if not base_path.exists():
error_message = f"Cursor workspace storage directory not found: {base_path}"
logger.error(error_message)
raise FileNotFoundError(error_message)
logger.info(f"Cursor workspace storage directory found: {base_path}")
return base_path
def get_latest_workspace_db_path() -> str:
base_path = get_cursor_workspace_path()
workspace_folder = max(base_path.glob("*"), key=os.path.getmtime)
db_path = workspace_folder / "state.vscdb"
if not db_path.exists():
raise FileNotFoundError(f"state.vscdb not found in {workspace_folder}")
return str(db_path)
@app.command()
def discover(
directory: str = typer.Argument(None, help="The directory to search for state.vscdb files. If not provided, the default Cursor workspace storage directory will be used."),
limit: int = typer.Option(None, help="The maximum number of state.vscdb files to process. Defaults to 10 if search_text is not provided, else -1."),
search_text: str = typer.Option(None, help="The text to search for in the chat history.")
):
"""
Discover all state.vscdb files in a directory and its subdirectories, and print a few lines of dialogue.
"""
if not directory:
directory = str(get_cursor_workspace_path())
if limit is None:
limit = -1 if search_text else 10
try:
state_files = []
for root, _, files in os.walk(directory):
if 'state.vscdb' in files:
db_path = os.path.join(root, 'state.vscdb')
state_files.append((db_path, os.path.getmtime(db_path)))
# Sort files by modification time (newest first)
state_files.sort(key=lambda x: x[1], reverse=True)
# Only process the newest files up to the specified limit, unless limit is -1
if limit != -1:
state_files = state_files[:limit]
results = []
# Process the files
for db_path, _ in state_files:
db_query = VSCDBQuery(db_path)
chat_data = db_query.query_aichat_data()
if "error" in chat_data:
error_message = f"Error querying chat data from {db_path}: {chat_data['error']}"
logger.error(error_message)
elif not chat_data:
logger.debug(f"No chat data found in {db_path}")
else:
chat_data_dict = json.loads(chat_data[0])
formatter = MarkdownChatFormatter()
formatted_chats = formatter.format(chat_data_dict, image_dir=None)
if search_text:
# Filter the formatted data to include only lines containing the search text
for formatted_data in formatted_chats:
filtered_lines = [line for line in formatted_data.splitlines() if search_text.lower() in line.lower()]
if filtered_lines:
results.append((db_path, "\n".join(formatted_data.splitlines()[:10]) + "\n..."))
if not filtered_lines:
logger.debug(f"No chat entries containing '{search_text}' found in {db_path}")
else:
# Collect the first few lines of the formatted chat data
for formatted_data in formatted_chats:
results.append((db_path, "\n".join(formatted_data.splitlines()[:10]) + "\n..."))
# Print all results at the end
console.print('\n\n')
if results:
for db_path, result in results:
console.print(Markdown("---"))
console.print(f"DATABASE: {os.path.join(os.path.basename(os.path.dirname(db_path)), os.path.basename(db_path))}\n")
console.print(Markdown(result))
console.print('\n\n')
else:
console.print("No results found.")
except FileNotFoundError as e:
error_message = f"File not found: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
except json.JSONDecodeError as e:
error_message = f"JSON decode error: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
except Exception as e:
error_message = f"Failed to discover and print chat data: {e}"
logger.error(error_message)
raise typer.Exit(code=1)
if __name__ == "__main__":
app()