|
| 1 | +import { asRecord } from "./coerce.js"; |
| 2 | +import { openReadOnlySqlite } from "./open-sqlite.js"; |
| 3 | + |
| 4 | +// The Cursor CLI agent (`~/.cursor` / `~/.cursor-nightly`) stores each chat as |
| 5 | +// its own content-addressed SQLite store, distinct from the GUI's single |
| 6 | +// `state.vscdb`. The `meta` table holds one row whose `value` is hex-encoded |
| 7 | +// JSON (the latest root blob id + last-used model); the `blobs` table maps a |
| 8 | +// sha256 id to either a message (JSON: `{ role, content }`) or the binary root |
| 9 | +// manifest. The manifest is a protobuf-style flat list of `0x0a 0x20` followed |
| 10 | +// by a 32-byte blob id, giving the conversation's messages in order. |
| 11 | + |
| 12 | +export interface CursorCliMessage { |
| 13 | + readonly role: string; |
| 14 | + readonly content: unknown; |
| 15 | +} |
| 16 | + |
| 17 | +export interface CursorCliStore { |
| 18 | + readonly lastUsedModel: string | null; |
| 19 | + readonly messages: CursorCliMessage[]; |
| 20 | +} |
| 21 | + |
| 22 | +const MANIFEST_RECORD_TAG = 0x0a; |
| 23 | +const MANIFEST_ID_LENGTH = 0x20; |
| 24 | +const MANIFEST_RECORD_LENGTH = 2 + MANIFEST_ID_LENGTH; |
| 25 | + |
| 26 | +/** |
| 27 | + * The conversation's message blob ids, in order, read from the leading run of |
| 28 | + * `[0x0a, 0x20, <32-byte id>]` records. Trailing protobuf fields after the run |
| 29 | + * are ignored; a manifest that doesn't start with the run yields `[]`. |
| 30 | + */ |
| 31 | +const parseManifestBlobIds = (manifest: Buffer): string[] => { |
| 32 | + const ids: string[] = []; |
| 33 | + let offset = 0; |
| 34 | + while ( |
| 35 | + offset + MANIFEST_RECORD_LENGTH <= manifest.length && |
| 36 | + manifest[offset] === MANIFEST_RECORD_TAG && |
| 37 | + manifest[offset + 1] === MANIFEST_ID_LENGTH |
| 38 | + ) { |
| 39 | + ids.push(manifest.subarray(offset + 2, offset + MANIFEST_RECORD_LENGTH).toString("hex")); |
| 40 | + offset += MANIFEST_RECORD_LENGTH; |
| 41 | + } |
| 42 | + return ids; |
| 43 | +}; |
| 44 | + |
| 45 | +/** blobs.data is a BLOB (Uint8Array); meta.value is hex-encoded TEXT. */ |
| 46 | +const toBuffer = (value: unknown): Buffer | null => { |
| 47 | + if (value instanceof Uint8Array) return Buffer.from(value); |
| 48 | + if (typeof value === "string") return Buffer.from(value, "hex"); |
| 49 | + return null; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Read a Cursor CLI per-session `store.db`: the last-used model and every |
| 54 | + * conversation message in order. Returns `null` when the store can't be opened |
| 55 | + * (older Node without `node:sqlite`, or an unreadable/locked file) or has no |
| 56 | + * usable `meta` row; the messages array is empty when the manifest is missing. |
| 57 | + */ |
| 58 | +export const readCursorCliStore = (storeDbPath: string): CursorCliStore | null => { |
| 59 | + const database = openReadOnlySqlite(storeDbPath); |
| 60 | + if (!database) return null; |
| 61 | + try { |
| 62 | + const metaRow = asRecord(database.prepare("SELECT value FROM meta LIMIT 1").get()); |
| 63 | + const metaValue = metaRow && typeof metaRow.value === "string" ? metaRow.value : null; |
| 64 | + if (!metaValue) return null; |
| 65 | + let meta: Record<string, unknown> | undefined; |
| 66 | + try { |
| 67 | + meta = asRecord(JSON.parse(Buffer.from(metaValue, "hex").toString("utf8"))); |
| 68 | + } catch { |
| 69 | + return null; |
| 70 | + } |
| 71 | + if (!meta) return null; |
| 72 | + |
| 73 | + const lastUsedModel = typeof meta.lastUsedModel === "string" ? meta.lastUsedModel : null; |
| 74 | + const latestRootBlobId = |
| 75 | + typeof meta.latestRootBlobId === "string" ? meta.latestRootBlobId : null; |
| 76 | + if (!latestRootBlobId) return { lastUsedModel, messages: [] }; |
| 77 | + |
| 78 | + const blobStatement = database.prepare("SELECT data FROM blobs WHERE id = ?"); |
| 79 | + const blobBuffer = (id: string): Buffer | null => { |
| 80 | + const row = asRecord(blobStatement.get(id)); |
| 81 | + return row ? toBuffer(row.data) : null; |
| 82 | + }; |
| 83 | + |
| 84 | + const manifest = blobBuffer(latestRootBlobId); |
| 85 | + if (!manifest) return { lastUsedModel, messages: [] }; |
| 86 | + |
| 87 | + const messages: CursorCliMessage[] = []; |
| 88 | + for (const blobId of parseManifestBlobIds(manifest)) { |
| 89 | + const raw = blobBuffer(blobId); |
| 90 | + if (!raw) continue; |
| 91 | + const text = raw.toString("utf8"); |
| 92 | + if (!text.startsWith("{")) continue; |
| 93 | + let message: Record<string, unknown> | undefined; |
| 94 | + try { |
| 95 | + message = asRecord(JSON.parse(text)); |
| 96 | + } catch { |
| 97 | + continue; |
| 98 | + } |
| 99 | + if (message && typeof message.role === "string") { |
| 100 | + messages.push({ role: message.role, content: message.content }); |
| 101 | + } |
| 102 | + } |
| 103 | + return { lastUsedModel, messages }; |
| 104 | + } catch { |
| 105 | + // A locked or unreadable store can throw mid-read; skip it rather than |
| 106 | + // sinking the whole stats run. |
| 107 | + return null; |
| 108 | + } finally { |
| 109 | + try { |
| 110 | + database.close(); |
| 111 | + } catch { |
| 112 | + // Already closed or never fully opened — nothing to release. |
| 113 | + } |
| 114 | + } |
| 115 | +}; |
0 commit comments