import { mkdirSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { DatabaseSync } from "node:sqlite"; export type AtlasDatabase = DatabaseSync; export function openDatabase(path: string): AtlasDatabase { const absolutePath = path === ":memory:" ? path : resolve(path); if (absolutePath !== ":memory:") { mkdirSync(dirname(absolutePath), { recursive: true }); } const db = new DatabaseSync(absolutePath); db.exec("PRAGMA foreign_keys = ON"); db.exec("PRAGMA journal_mode = WAL"); db.exec("PRAGMA busy_timeout = 5000"); migrate(db); return db; } function migrate(db: AtlasDatabase): void { db.exec(` CREATE TABLE IF NOT EXISTS schema_meta ( key TEXT PRIMARY KEY, value TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS integrations ( id INTEGER PRIMARY KEY, provider TEXT NOT NULL, account TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'active', created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE(provider, account) ); CREATE TABLE IF NOT EXISTS resources ( id INTEGER PRIMARY KEY, canonical_url TEXT NOT NULL UNIQUE, resource_type TEXT NOT NULL, title TEXT NOT NULL, author TEXT, description TEXT, language TEXT, availability_status TEXT NOT NULL DEFAULT 'available', created_at TEXT NOT NULL, updated_at TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS saves ( id INTEGER PRIMARY KEY, integration_id INTEGER NOT NULL REFERENCES integrations(id), provider_external_id TEXT NOT NULL, resource_id INTEGER NOT NULL REFERENCES resources(id), saved_at TEXT, unsaved_at TEXT, provider_metadata TEXT NOT NULL DEFAULT '{}', created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE(integration_id, provider_external_id) ); CREATE TABLE IF NOT EXISTS github_repositories ( resource_id INTEGER PRIMARY KEY REFERENCES resources(id) ON DELETE CASCADE, github_id INTEGER NOT NULL UNIQUE, node_id TEXT NOT NULL UNIQUE, owner TEXT NOT NULL, name TEXT NOT NULL, full_name TEXT NOT NULL UNIQUE, default_branch TEXT, license_spdx TEXT, stars INTEGER NOT NULL DEFAULT 0, forks INTEGER NOT NULL DEFAULT 0, open_issues INTEGER NOT NULL DEFAULT 0, topics TEXT NOT NULL DEFAULT '[]', archived INTEGER NOT NULL DEFAULT 0, pushed_at TEXT, github_updated_at TEXT ); CREATE TABLE IF NOT EXISTS x_posts ( resource_id INTEGER PRIMARY KEY REFERENCES resources(id) ON DELETE CASCADE, x_post_id TEXT NOT NULL UNIQUE, author_id TEXT, author_handle TEXT, author_name TEXT, post_created_at TEXT, conversation_id TEXT, media TEXT NOT NULL DEFAULT '[]', outbound_urls TEXT NOT NULL DEFAULT '[]', raw_json TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS x_media ( resource_id INTEGER NOT NULL REFERENCES resources(id) ON DELETE CASCADE, media_key TEXT NOT NULL, type TEXT NOT NULL, source TEXT, article_id TEXT, position INTEGER NOT NULL DEFAULT 0, url TEXT, thumbnail_url TEXT, width INTEGER, height INTEGER, duration_millis INTEGER, local_path TEXT, content_type TEXT, byte_size INTEGER, sha256 TEXT, download_state TEXT, PRIMARY KEY(resource_id, media_key) ); CREATE TABLE IF NOT EXISTS sync_checkpoints ( integration_id INTEGER PRIMARY KEY REFERENCES integrations(id), etag TEXT, is_complete INTEGER NOT NULL DEFAULT 0, high_watermark TEXT, last_checked_at TEXT, last_success_at TEXT, last_reconciled_at TEXT, last_error TEXT ); CREATE TABLE IF NOT EXISTS sync_runs ( id INTEGER PRIMARY KEY, integration_id INTEGER NOT NULL REFERENCES integrations(id), status TEXT NOT NULL, started_at TEXT NOT NULL, completed_at TEXT, imported_count INTEGER NOT NULL DEFAULT 0, updated_count INTEGER NOT NULL DEFAULT 0, removed_count INTEGER NOT NULL DEFAULT 0, error_message TEXT ); CREATE TABLE IF NOT EXISTS captures ( id INTEGER PRIMARY KEY, resource_id INTEGER NOT NULL REFERENCES resources(id) ON DELETE CASCADE, kind TEXT NOT NULL, source_url TEXT NOT NULL, fetched_at TEXT NOT NULL, content_hash TEXT NOT NULL, normalized_content TEXT NOT NULL, UNIQUE(resource_id, kind, content_hash) ); CREATE TABLE IF NOT EXISTS chunks ( id INTEGER PRIMARY KEY, capture_id INTEGER NOT NULL REFERENCES captures(id) ON DELETE CASCADE, ordinal INTEGER NOT NULL, text TEXT NOT NULL, token_count INTEGER NOT NULL, content_hash TEXT NOT NULL, UNIQUE(capture_id, ordinal) ); CREATE TABLE IF NOT EXISTS resource_fetch_state ( resource_id INTEGER NOT NULL REFERENCES resources(id) ON DELETE CASCADE, kind TEXT NOT NULL, etag TEXT, status TEXT NOT NULL, last_checked_at TEXT NOT NULL, last_success_at TEXT, error_message TEXT, PRIMARY KEY(resource_id, kind) ); CREATE TABLE IF NOT EXISTS resource_notes ( resource_id INTEGER PRIMARY KEY REFERENCES resources(id) ON DELETE CASCADE, context TEXT NOT NULL, updated_at TEXT NOT NULL ); -- The one preview image the palette shows for a bookmark: a repository's -- first real image in its README, a page's og:image, an X post's first -- photo. Kept out of x_media because most of that table is X-specific -- (media keys, article ids, download state) and none of it applies here. -- One row per resource: this is the picture that stands for the bookmark, -- not an archive of every image it ever mentioned. CREATE TABLE IF NOT EXISTS resource_images ( resource_id INTEGER PRIMARY KEY REFERENCES resources(id) ON DELETE CASCADE, source_url TEXT NOT NULL, local_path TEXT NOT NULL, content_type TEXT NOT NULL, byte_size INTEGER NOT NULL, fetched_at TEXT NOT NULL ); CREATE VIRTUAL TABLE IF NOT EXISTS resources_fts USING fts5( resource_id UNINDEXED, title, description, topics, language, content, tokenize = 'porter unicode61 remove_diacritics 2' ); `); // The on-device embedding tables went with the semantic-search feature. Drop // them so an existing database does not keep thousands of orphaned vectors; // IF EXISTS makes this a no-op on every open after the first. db.exec("DROP TABLE IF EXISTS chunk_embeddings"); db.exec("DROP TABLE IF EXISTS embedding_cache"); const checkpointColumns = db .prepare("PRAGMA table_info(sync_checkpoints)") .all() as Array<{ name: string }>; if (!checkpointColumns.some((column) => column.name === "is_complete")) { db.exec("ALTER TABLE sync_checkpoints ADD COLUMN is_complete INTEGER NOT NULL DEFAULT 0"); } const ftsColumns = db .prepare("PRAGMA table_info(resources_fts)") .all() as Array<{ name: string }>; if (!ftsColumns.some((column) => column.name === "content")) { db.exec("DROP TABLE resources_fts"); db.exec(` CREATE VIRTUAL TABLE resources_fts USING fts5( resource_id UNINDEXED, title, description, topics, language, content, tokenize = 'porter unicode61 remove_diacritics 2' ) `); rebuildMissingFtsRows(db); } // Porter stemming, so "designing" finds "design" and "designs". FTS5 records the // tokenizer in the table's own SQL, so an older one is detected without needing a // schema version. The vocabulary table is built from resources_fts, so it has to // be dropped with it and recreated below. const ftsSql = ( db .prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'resources_fts'") .get() as { sql: string } | undefined )?.sql ?? ""; if (!ftsSql.includes("porter")) { db.exec("DROP TABLE IF EXISTS resources_vocab"); db.exec("DROP TABLE resources_fts"); db.exec(` CREATE VIRTUAL TABLE resources_fts USING fts5( resource_id UNINDEXED, title, description, topics, language, content, tokenize = 'porter unicode61 remove_diacritics 2' ) `); db.exec("BEGIN IMMEDIATE"); try { for (const row of db.prepare("SELECT id FROM resources").all() as Array<{ id: number }>) { refreshResourceFts(db, row.id); } db.exec("COMMIT"); } catch (error) { db.exec("ROLLBACK"); throw error; } } // Both of these are gone: IDF counts through the index itself, and usage // statistics are no longer tracked at all. db.exec("DROP TABLE IF EXISTS resources_vocab"); db.exec("DROP TABLE IF EXISTS bookmark_usage"); // Chunk-level index (external content over `chunks`). Triggers keep it in // sync; the rebuild runs only the first time the table is created. const chunksFtsSql = ( db .prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'chunks_fts'") .get() as { sql: string } | undefined )?.sql; const chunksNeedsRebuild = chunksFtsSql === undefined || !chunksFtsSql.includes("porter"); if (chunksFtsSql !== undefined && !chunksFtsSql.includes("porter")) { db.exec("DROP TABLE chunks_fts"); } db.exec(` CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5( text, content = 'chunks', content_rowid = 'id', tokenize = 'porter unicode61 remove_diacritics 2' ); CREATE TRIGGER IF NOT EXISTS chunks_fts_insert AFTER INSERT ON chunks BEGIN INSERT INTO chunks_fts (rowid, text) VALUES (new.id, new.text); END; CREATE TRIGGER IF NOT EXISTS chunks_fts_delete AFTER DELETE ON chunks BEGIN INSERT INTO chunks_fts (chunks_fts, rowid, text) VALUES ('delete', old.id, old.text); END; CREATE TRIGGER IF NOT EXISTS chunks_fts_update AFTER UPDATE ON chunks BEGIN INSERT INTO chunks_fts (chunks_fts, rowid, text) VALUES ('delete', old.id, old.text); INSERT INTO chunks_fts (rowid, text) VALUES (new.id, new.text); END; `); if (chunksNeedsRebuild) { db.exec("INSERT INTO chunks_fts (chunks_fts) VALUES ('rebuild')"); } } function rebuildMissingFtsRows(db: AtlasDatabase): void { const rows = db.prepare(` SELECT r.id FROM resources r LEFT JOIN resources_fts f ON f.resource_id = r.id WHERE f.resource_id IS NULL `).all() as Array<{ id: number }>; for (const row of rows) refreshResourceFts(db, row.id); } export function refreshResourceFts(db: AtlasDatabase, resourceId: number): void { const resource = db.prepare(` SELECT r.title, r.description, r.language, COALESCE(g.topics, '[]') AS topics, COALESCE(n.context, '') AS note, COALESCE(( SELECT group_concat(part, char(10) || char(10)) FROM ( SELECT ( SELECT c2.normalized_content FROM captures c2 WHERE c2.resource_id = r.id AND c2.kind = c.kind ORDER BY c2.fetched_at DESC, c2.id DESC LIMIT 1 ) AS part FROM captures c WHERE c.resource_id = r.id GROUP BY c.kind ) WHERE part IS NOT NULL AND length(trim(part)) > 0 ), '') AS content FROM resources r LEFT JOIN github_repositories g ON g.resource_id = r.id LEFT JOIN resource_notes n ON n.resource_id = r.id WHERE r.id = ? `).get(resourceId) as | { title: string; description: string | null; language: string | null; topics: string; note: string; content: string; } | undefined; if (!resource) return; let topics = ""; try { const parsed = JSON.parse(resource.topics) as unknown; topics = Array.isArray(parsed) ? parsed.join(" ") : ""; } catch { topics = ""; } db.prepare("DELETE FROM resources_fts WHERE resource_id = ?").run(resourceId); db.prepare(` INSERT INTO resources_fts ( resource_id, title, description, topics, language, content ) VALUES (?, ?, ?, ?, ?, ?) `).run( resourceId, resource.title, resource.description ?? "", topics, resource.language ?? "", [resource.note, resource.content].filter((part) => part.trim()).join("\n\n"), ); } export type PruneResult = { pruned: number }; /** * Delete resources that no longer have an active save. Reconciliation removes a * bookmark by setting saves.unsaved_at and never deletes, so unstarred repos and * unbookmarked posts pile up as rows nothing can see. Everything hanging off the * resource cascades, but `saves` has no ON DELETE CASCADE and the full-text row * is not linked by foreign key at all, so both are cleared explicitly. */ export function pruneOrphanResources(db: AtlasDatabase, options: { dryRun?: boolean } = {}): PruneResult { const ids = (db.prepare(` SELECT r.id FROM resources r WHERE NOT EXISTS (SELECT 1 FROM saves s WHERE s.resource_id = r.id AND s.unsaved_at IS NULL) `).all() as Array<{ id: number }>).map((row) => row.id); if (options.dryRun || ids.length === 0) return { pruned: ids.length }; const deleteFts = db.prepare("DELETE FROM resources_fts WHERE resource_id = ?"); const deleteSaves = db.prepare("DELETE FROM saves WHERE resource_id = ?"); const deleteResource = db.prepare("DELETE FROM resources WHERE id = ?"); db.exec("BEGIN IMMEDIATE"); try { for (const id of ids) { deleteFts.run(id); deleteSaves.run(id); deleteResource.run(id); } db.exec("COMMIT"); } catch (error) { db.exec("ROLLBACK"); throw error; } return { pruned: ids.length }; } /** * Per-user directory holding the database and other derived files. * * Deliberately not the checkout: installed as a package this module lives under * node_modules, and writing a database inside an installed package is both * surprising and broken on a read-only install. Resolving per user also means * the CLI and the palette agree no matter which directory each is started from. * * Mirrored in extensions/bookmark-atlas/index.ts, which cannot import this * module when it is symlinked into ~/.pi/agent/extensions. Keep the two in sync; * test/db.test.ts fails if they drift apart. */ export function atlasDataDir(): string { const override = process.env.BOOKMARK_ATLAS_DATA_DIR; if (override) return resolve(override); const home = homedir(); if (process.platform === "darwin") { return join(home, "Library", "Application Support", "bookmark-atlas"); } if (process.platform === "win32") { return join(process.env.LOCALAPPDATA ?? join(home, "AppData", "Local"), "bookmark-atlas"); } return join(process.env.XDG_DATA_HOME ?? join(home, ".local", "share"), "bookmark-atlas"); } export function databasePath(): string { return process.env.BOOKMARK_ATLAS_DB ?? join(atlasDataDir(), "bookmarks.db"); }