Repository navigation
Expand file tree
/
Copy pathextract_cursor.py
More file actions
executable file
·625 lines (502 loc) · 24.9 KB
/
Copy pathextract_cursor.py
File metadata and controls
executable file
·625 lines (502 loc) · 24.9 KB
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
#!/usr/bin/env python3
"""
ULTIMATE Cursor extraction - EVERY VERSION, EVERY FORMAT:
1. Chat mode (workspace ItemTable - workbench.panel.aichat)
2. aiService (OLD format - pre-v0.43 prompts/generations)
3. Workspace composers (composer.composerData in workspace ItemTable)
4. Global composers inline (composerData.conversation[] in global cursorDiskKV)
5. Global composers separate (bubbleId:{composer}:{bubble} in global cursorDiskKV)
This gets EVERYTHING from v0.2 through v2.0+
LIMITATIONS:
- aiService (old format): Assistant responses are not persisted, only summaries
- Model information: Only available for conversations with modelConfig stored
in composerData. Older conversations may not have model information.
"""
import json
import sqlite3
from pathlib import Path
from datetime import datetime
import platform
import os
from collections import defaultdict
def find_cursor_installations():
"""Find all Cursor installation directories"""
system = platform.system()
home = Path.home()
locations = []
if system == "Darwin": # macOS
base_dirs = [
home / "Library/Application Support",
home / ".config"
]
elif system == "Linux":
base_dirs = [
home / ".config",
home / ".local/share"
]
elif system == "Windows":
base_dirs = [
Path(os.environ.get('APPDATA', home / 'AppData/Roaming')),
Path(os.environ.get('LOCALAPPDATA', home / 'AppData/Local'))
]
else:
base_dirs = [home / ".config"]
for base_dir in base_dirs:
if not base_dir.exists():
continue
cursor_dir = base_dir / 'Cursor'
if cursor_dir.exists():
locations.append(cursor_dir)
return list(set(locations))
def extract_aiservice_conversations(db_path, workspace_id):
"""Extract OLD Cursor format (pre-v0.43) aiService prompts and generations"""
conversations = []
try:
conn = sqlite3.connect(Path(db_path).resolve().as_uri() + '?mode=ro', uri=True)
cursor = conn.cursor()
# Get prompts
cursor.execute("SELECT value FROM ItemTable WHERE key = 'aiService.prompts'")
prompts_result = cursor.fetchone()
# Get generations
cursor.execute("SELECT value FROM ItemTable WHERE key = 'aiService.generations'")
gens_result = cursor.fetchone()
if prompts_result or gens_result:
prompts = json.loads(prompts_result[0]) if prompts_result else []
generations = json.loads(gens_result[0]) if gens_result else []
# Pair prompts with generations
max_len = max(len(prompts), len(generations))
for i in range(max_len):
messages = []
if i < len(prompts):
prompt = prompts[i]
messages.append({
'role': 'user',
'content': prompt.get('text', ''),
'command_type': prompt.get('commandType')
})
if i < len(generations):
gen = generations[i]
msg = {
'role': 'assistant',
'content': gen.get('text', gen.get('message', '')),
}
# Extract model if available
if 'model' in gen:
msg['model'] = gen['model']
elif 'modelId' in gen:
msg['model'] = gen['modelId']
messages.append(msg)
if messages:
# Try to get model from generation data
model = None
if i < len(generations):
gen = generations[i]
if isinstance(gen, dict):
model = gen.get('model') or gen.get('modelId') or gen.get('modelName')
# Add model to assistant messages
for msg in messages:
if isinstance(msg, dict) and msg.get('role') == 'assistant':
msg['model'] = model
conv = {
'messages': messages,
'source': 'cursor-aiservice',
'workspace_id': workspace_id
}
if model:
conv['model'] = model
conversations.append(conv)
conn.close()
except Exception as e:
pass
return conversations
def extract_workspace_composers(db_path, workspace_id):
"""Extract workspace-specific composer data (pre-migration to global storage)"""
conversations = []
try:
conn = sqlite3.connect(Path(db_path).resolve().as_uri() + '?mode=ro', uri=True)
cursor = conn.cursor()
cursor.execute("SELECT value FROM ItemTable WHERE key = 'composer.composerData'")
result = cursor.fetchone()
if result:
data = json.loads(result[0])
if isinstance(data, dict) and 'allComposers' in data:
all_composers = data['allComposers']
if isinstance(all_composers, list):
for composer_data in all_composers:
if not isinstance(composer_data, dict):
continue
messages = []
code_contexts = []
diffs = []
conversation = composer_data.get('conversation', [])
for bubble in conversation:
bubble_type = bubble.get('type')
text = bubble.get('text', '')
if bubble_type == 1: # User
msg = {
'role': 'user',
'content': text
}
context = bubble.get('context', {})
if context and 'selections' in context:
ctx = []
for sel in context['selections']:
if 'uri' in sel and 'fsPath' in sel['uri']:
ctx.append({
'file': sel['uri']['fsPath'],
'code': sel.get('text', sel.get('rawText', '')),
'range': sel.get('range')
})
if ctx:
msg['code_context'] = ctx
code_contexts.extend(ctx)
messages.append(msg)
elif bubble_type == 2: # AI
msg = {
'role': 'assistant',
'content': text
}
# Extract model name if available
if 'modelId' in bubble:
msg['model'] = bubble['modelId']
elif 'model' in bubble:
msg['model'] = bubble['model']
elif 'modelName' in bubble:
msg['model'] = bubble['modelName']
if 'codeBlocks' in bubble and bubble['codeBlocks']:
msg['code_blocks'] = bubble['codeBlocks']
if 'suggestedCodeBlocks' in bubble and bubble['suggestedCodeBlocks']:
msg['suggested_code_blocks'] = bubble['suggestedCodeBlocks']
diffs.extend(bubble['suggestedCodeBlocks'])
if 'diffHistories' in bubble and bubble['diffHistories']:
msg['diff_histories'] = bubble['diffHistories']
diffs.extend(bubble['diffHistories'])
messages.append(msg)
if messages:
# Get model from modelConfig
model = composer_data.get('modelConfig', {}).get('modelName')
# Add model to all assistant messages
for msg in messages:
if isinstance(msg, dict) and msg.get('role') == 'assistant':
msg['model'] = model
conversations.append({
'messages': messages,
'source': 'cursor-workspace-composer',
'composer_id': composer_data.get('composerId'),
'name': composer_data.get('name', 'Untitled'),
'workspace_id': workspace_id,
'model': model,
'has_code_context': len(code_contexts) > 0,
'has_diffs': len(diffs) > 0
})
conn.close()
except Exception as e:
pass
return conversations
def extract_chat_mode(db_path, workspace_id):
"""Extract Chat mode conversations"""
conversations = []
try:
conn = sqlite3.connect(Path(db_path).resolve().as_uri() + '?mode=ro', uri=True)
cursor = conn.cursor()
cursor.execute("SELECT value FROM ItemTable WHERE key = 'workbench.panel.aichat.view.aichat.chatdata'")
result = cursor.fetchone()
if result:
data = json.loads(result[0])
if 'tabs' in data:
for tab in data['tabs']:
if 'bubbles' in tab and len(tab['bubbles']) > 0:
messages = []
code_context = []
suggested_diffs = []
for bubble in tab['bubbles']:
bubble_type = bubble.get('type')
content = bubble.get('rawText', bubble.get('text', ''))
msg = {
'role': 'user' if bubble_type == 'user' else 'assistant',
'content': content
}
if 'selections' in bubble and bubble['selections']:
ctx = []
for sel in bubble['selections']:
if 'uri' in sel and 'fsPath' in sel['uri']:
ctx.append({
'file': sel['uri']['fsPath'],
'code': sel.get('text', sel.get('rawText', '')),
'range': sel.get('range')
})
if ctx:
msg['code_context'] = ctx
code_context.extend(ctx)
if 'suggestedDiffs' in bubble and bubble['suggestedDiffs']:
msg['suggested_diffs'] = bubble['suggestedDiffs']
suggested_diffs.extend(bubble['suggestedDiffs'])
messages.append(msg)
if messages:
conversations.append({
'messages': messages,
'source': 'cursor-chat',
'chat_title': tab.get('chatTitle'),
'tab_id': tab.get('tabId'),
'workspace_id': workspace_id,
'has_code_context': len(code_context) > 0,
'has_diffs': len(suggested_diffs) > 0
})
conn.close()
except Exception as e:
pass
return conversations
def extract_bubbles_for_composer(cursor, composer_id):
"""Extract separate bubble storage for a composer"""
bubbles = []
try:
cursor.execute(
"SELECT key, value FROM cursorDiskKV WHERE key >= ? AND key < ? ORDER BY rowid",
(f'bubbleId:{composer_id}:', f'bubbleId:{composer_id};')
)
for key, value in cursor.fetchall():
if not value:
continue
try:
bubble_data = json.loads(value)
bubble_type = bubble_data.get('type')
text = bubble_data.get('text', '')
msg = {
'role': 'user' if bubble_type == 1 else 'assistant',
'content': text,
'bubble_id': key.split(':')[2]
}
if bubble_type == 1:
if 'selections' in bubble_data:
ctx = []
for sel in bubble_data.get('selections', []):
if 'uri' in sel and 'fsPath' in sel.get('uri', {}):
ctx.append({
'file': sel['uri']['fsPath'],
'code': sel.get('text', sel.get('rawText', '')),
'range': sel.get('range')
})
if ctx:
msg['code_context'] = ctx
elif bubble_type == 2:
msg = {
'role': 'assistant',
'content': text,
'bubble_id': key.split(':')[2]
}
# Extract model name if available
if 'modelId' in bubble_data:
msg['model'] = bubble_data['modelId']
elif 'model' in bubble_data:
msg['model'] = bubble_data['model']
elif 'modelName' in bubble_data:
msg['model'] = bubble_data['modelName']
if 'codeBlocks' in bubble_data and bubble_data['codeBlocks']:
msg['code_blocks'] = bubble_data['codeBlocks']
if 'suggestedCodeBlocks' in bubble_data and bubble_data['suggestedCodeBlocks']:
msg['suggested_code_blocks'] = bubble_data['suggestedCodeBlocks']
if 'diffHistories' in bubble_data and bubble_data['diffHistories']:
msg['diff_histories'] = bubble_data['diffHistories']
if 'toolResults' in bubble_data and bubble_data['toolResults']:
msg['tool_results'] = bubble_data['toolResults']
bubbles.append(msg)
except json.JSONDecodeError:
continue
except Exception as e:
pass
return bubbles
def extract_global_composers(global_db_path):
"""Extract global composer data (both inline and separate storage)"""
conversations = []
try:
conn = sqlite3.connect(Path(global_db_path).resolve().as_uri() + '?mode=ro', uri=True)
cursor = conn.cursor()
cursor.execute("SELECT key, value FROM cursorDiskKV WHERE key LIKE 'composerData:%'")
results = cursor.fetchall()
for key, value in results:
if not value:
continue
try:
data = json.loads(value)
composer_id = data.get('composerId', key.split(':')[1])
messages = []
code_contexts = []
diffs = []
inline_conversation = data.get('conversation', [])
if inline_conversation and len(inline_conversation) > 0:
# INLINE STORAGE
for bubble in inline_conversation:
bubble_type = bubble.get('type')
text = bubble.get('text', '')
if bubble_type == 1:
msg = {
'role': 'user',
'content': text
}
context = bubble.get('context', {})
if context and 'selections' in context:
ctx = []
for sel in context['selections']:
if 'uri' in sel and 'fsPath' in sel['uri']:
ctx.append({
'file': sel['uri']['fsPath'],
'code': sel.get('text', sel.get('rawText', '')),
'range': sel.get('range')
})
if ctx:
msg['code_context'] = ctx
code_contexts.extend(ctx)
messages.append(msg)
elif bubble_type == 2:
msg = {
'role': 'assistant',
'content': text
}
# Extract model name if available
if 'modelId' in bubble:
msg['model'] = bubble['modelId']
elif 'model' in bubble:
msg['model'] = bubble['model']
elif 'modelName' in bubble:
msg['model'] = bubble['modelName']
if 'codeBlocks' in bubble and bubble['codeBlocks']:
msg['code_blocks'] = bubble['codeBlocks']
if 'suggestedCodeBlocks' in bubble and bubble['suggestedCodeBlocks']:
msg['suggested_code_blocks'] = bubble['suggestedCodeBlocks']
diffs.extend(bubble['suggestedCodeBlocks'])
if 'diffHistories' in bubble and bubble['diffHistories']:
msg['diff_histories'] = bubble['diffHistories']
diffs.extend(bubble['diffHistories'])
messages.append(msg)
else:
# SEPARATE STORAGE
messages = extract_bubbles_for_composer(cursor, composer_id)
for msg in messages:
if 'code_context' in msg:
code_contexts.extend(msg['code_context'])
if 'suggested_code_blocks' in msg:
diffs.extend(msg['suggested_code_blocks'])
if 'diff_histories' in msg:
diffs.extend(msg['diff_histories'])
if messages:
# Get model from modelConfig
model = data.get('modelConfig', {}).get('modelName')
# Add model to all assistant messages
for msg in messages:
if isinstance(msg, dict) and msg.get('role') == 'assistant':
msg['model'] = model
conversations.append({
'messages': messages,
'source': 'cursor-global-composer',
'composer_id': composer_id,
'name': data.get('name', 'Untitled'),
'status': data.get('status'),
'unified_mode': data.get('unifiedMode'),
'created_at': data.get('createdAt'),
'updated_at': data.get('lastUpdatedAt'),
'model': model,
'has_code_context': len(code_contexts) > 0,
'has_diffs': len(diffs) > 0,
'storage_type': 'inline' if inline_conversation else 'separate'
})
except (json.JSONDecodeError, KeyError) as e:
continue
conn.close()
except Exception as e:
print(f"Error extracting global composers: {e}")
return conversations
def main():
print("="*80)
print("CURSOR ULTIMATE EXTRACTION - ALL VERSIONS (v0.2 - v2.0+)")
print("="*80)
print()
installations = find_cursor_installations()
if not installations:
print("❌ No Cursor installations found!")
return
print(f"✅ Found {len(installations)} installation(s):")
for inst in installations:
print(f" - {inst}")
print()
all_conversations = []
stats = defaultdict(int)
for installation in installations:
print(f"📂 Processing: {installation}")
# Extract from ALL workspace databases
workspace_storage = installation / 'User/workspaceStorage'
if workspace_storage.exists():
aiservice_count = 0
workspace_composer_count = 0
chat_count = 0
for workspace in workspace_storage.iterdir():
if workspace.is_dir() and workspace.name != 'ext-dev':
db_file = workspace / 'state.vscdb'
if db_file.exists():
# Extract aiService (old format)
convs = extract_aiservice_conversations(db_file, workspace.name)
all_conversations.extend(convs)
aiservice_count += len(convs)
# Extract workspace composers
convs = extract_workspace_composers(db_file, workspace.name)
all_conversations.extend(convs)
workspace_composer_count += len(convs)
# Extract Chat mode
convs = extract_chat_mode(db_file, workspace.name)
all_conversations.extend(convs)
chat_count += len(convs)
print(f" ✅ aiService (old format): {aiservice_count} conversations")
print(f" ✅ Workspace composers: {workspace_composer_count} conversations")
print(f" ✅ Chat mode: {chat_count} conversations")
stats['aiservice'] += aiservice_count
stats['workspace_composer'] += workspace_composer_count
stats['chat'] += chat_count
# Extract global composers
global_storage = installation / 'User/globalStorage/state.vscdb'
if global_storage.exists():
convs = extract_global_composers(global_storage)
all_conversations.extend(convs)
inline_count = sum(1 for c in convs if c.get('storage_type') == 'inline')
separate_count = sum(1 for c in convs if c.get('storage_type') == 'separate')
print(f" ✅ Global composers: {len(convs)} conversations")
print(f" - Inline storage: {inline_count}")
print(f" - Separate storage: {separate_count}")
stats['global_composer'] += len(convs)
else:
print(f" ⚠️ No global storage found")
print()
print("="*80)
print("ULTIMATE EXTRACTION COMPLETE")
print("="*80)
print(f"Total conversations: {len(all_conversations):,}")
print(f" aiService (v0.2-v0.43): {stats['aiservice']:,}")
print(f" Workspace composers (v0.43-v1.x): {stats['workspace_composer']:,}")
print(f" Global composers (v1.x-v2.0+): {stats['global_composer']:,}")
print(f" Chat mode (v0.x-v1.x): {stats['chat']:,}")
if not all_conversations:
print("No conversations found!")
return
# Statistics
total_messages = sum(len(c['messages']) for c in all_conversations)
with_code = sum(1 for c in all_conversations if c.get('has_code_context'))
with_diffs = sum(1 for c in all_conversations if c.get('has_diffs'))
complete = sum(1 for c in all_conversations
if any(m['role'] == 'assistant' for m in c['messages']))
print(f"\nComplete conversations: {complete:,}")
print(f"Total messages: {total_messages:,}")
print(f"With code context: {with_code:,}")
print(f"With diffs: {with_diffs:,}")
print()
# Save to organized JSONL
output_dir = Path('extracted_data')
output_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_file = output_dir / f'cursor_ultimate_{timestamp}.jsonl'
with open(output_file, 'w') as f:
for conv in all_conversations:
f.write(json.dumps(conv, ensure_ascii=False) + '\n')
file_size = output_file.stat().st_size / 1024 / 1024
print(f"✅ Saved to: {output_file}")
print(f" Size: {file_size:.2f} MB")
print(f" Format: JSONL (one conversation per line)")
if __name__ == '__main__':
main()