import * as refs from '../../lib/db/repos/refs.js'; const SYS = { kind: 'system', id: null }; // Karakeep REST: GET /api/v1/bookmarks?cursor=... → { bookmarks:[{id,content:{url,title}}], nextCursor }. export async function importKarakeep({ apiUrl = process.env.KARAKEEP_URL, token = process.env.KARAKEEP_TOKEN, spaceId, dryRun = false, fetchImpl = fetch }) { let cursor = null, created = 0; do { const u = `${apiUrl}/api/v1/bookmarks?limit=100${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`; const res = await fetchImpl(u, { headers: { Authorization: `Bearer ${token}` } }); if (!res.ok) throw new Error(`karakeep ${res.status}`); const body = await res.json(); for (const b of (body.bookmarks || [])) { const url = b.content?.url; if (!url) continue; created++; if (dryRun) continue; await refs.upsertByExternal({ space_id: spaceId, kind: 'url', source_url: url, title: b.content?.title || url, source_kind: 'karakeep', external_id: String(b.id) }, SYS); } cursor = body.nextCursor || null; } while (cursor); return { created }; }