askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
bookmarks-service.ts · 13.0 KB · 295 lines
| 1 | /** |
| 2 | * Bookmarks are the person's saved links on a connected service (askrobots), read and saved over its MCP connection. |
| 3 | * |
| 4 | * - The list is kept in a local copy, so it shows at once and when offline, and is fetched from the service once when the |
| 5 | * browser opens (if a sign-in is saved), when the person opens the list, right after saving, and right after signing in. |
| 6 | * - Whether the page you are on is saved is decided from that copy first. The copy holds only the newest links, so the browser |
| 7 | * also asks the person's own account about the site they are on (once per site every ten minutes). That is a setting, shown in |
| 8 | * the bookmarks list, and can be turned off; with it off, the addresses you visit are never sent. |
| 9 | * - Reading never reaches the keychain on its own: until the connection has been used in this session, only the local copy |
| 10 | * is shown (on macOS, unlocking a saved sign-in can raise a system password prompt). |
| 11 | * - Saving the page you are on is one click with no dialog after it (the star fills in); it goes where the server's entry says |
| 12 | * saved links go (fixed arguments in mcp-servers.json, such as a project), and is private. |
| 13 | */ |
| 14 | |
| 15 | export interface Bookmark { |
| 16 | id: string; |
| 17 | url: string; |
| 18 | title: string; |
| 19 | tags: string[]; |
| 20 | createdAt: string | null; |
| 21 | } |
| 22 | |
| 23 | export interface BookmarksState { |
| 24 | items: Bookmark[]; |
| 25 | /** Older bookmarks the browser has seen in searches and page checks: not in the list, but known to be saved (for ☆). */ |
| 26 | known: Bookmark[]; |
| 27 | /** Pages you visit are checked against the service (sends the site's name to your own account). */ |
| 28 | checkPages: boolean; |
| 29 | /** When the list last came from the service (epoch ms), or null if it never has. */ |
| 30 | updatedAt: number | null; |
| 31 | /** Where `items` came from right now. */ |
| 32 | source: "service" | "copy" | "none"; |
| 33 | connected: boolean; |
| 34 | /** The service returns at most this many, newest first. */ |
| 35 | limit: number; |
| 36 | error?: string; |
| 37 | } |
| 38 | |
| 39 | export interface BookmarksDeps { |
| 40 | /** Call a tool on the bookmark service. `fixed`: apply the server entry's fixed arguments (for saving). */ |
| 41 | call(tool: string, args: Record<string, unknown>, opts: { fixed: boolean }): Promise<string>; |
| 42 | /** Signed in to the service. May read the saved sign-in (and so the keychain): only call it when that is fine. */ |
| 43 | connected(): boolean; |
| 44 | /** True once the saved sign-in has been read this session (so checking costs nothing). */ |
| 45 | ready(): boolean; |
| 46 | loadCopy(): string | null; |
| 47 | saveCopy(text: string): void; |
| 48 | changed(state: BookmarksState): void; |
| 49 | now?(): number; |
| 50 | } |
| 51 | |
| 52 | export const LIMIT = 100; |
| 53 | /** How many older bookmarks seen in searches are remembered. */ |
| 54 | const MAX_KNOWN = 5000; |
| 55 | /** A site is checked again after this long (links saved elsewhere in the meantime). */ |
| 56 | const CHECK_AGAIN_MS = 10 * 60_000; |
| 57 | |
| 58 | /** One address in a comparable form: no fragment, lower-case host, no trailing slash on the path. */ |
| 59 | /** Query parameters that only track where a click came from: the same page with or without them is the same page. */ |
| 60 | const TRACKING = /^(utm_[a-z]+|fbclid|gclid|dclid|msclkid|mc_cid|mc_eid|ref_src|guccounter|guce_referrer|guce_referrer_sig|_ga|igshid|si)$/i; |
| 61 | |
| 62 | export function normalizeUrl(u: string): string { |
| 63 | try { |
| 64 | const x = new URL(u); |
| 65 | x.hash = ""; |
| 66 | for (const k of [...x.searchParams.keys()]) if (TRACKING.test(k)) x.searchParams.delete(k); |
| 67 | x.hostname = x.hostname.toLowerCase(); |
| 68 | let s = x.href; |
| 69 | if (x.pathname !== "/" && s.endsWith("/") && !x.search) s = s.slice(0, -1); |
| 70 | if (x.pathname === "/" && !x.search) s = `${x.protocol}//${x.host}`; |
| 71 | return s; |
| 72 | } catch { |
| 73 | return u.trim(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const str = (v: unknown): string => (typeof v === "string" ? v : ""); |
| 78 | |
| 79 | /** The service's answer, as bookmarks. Accepts a list, or an object holding one; drops anything that is not a web address. */ |
| 80 | export function parseLinks(text: string): Bookmark[] { |
| 81 | let j: unknown; |
| 82 | try { |
| 83 | j = JSON.parse(text); |
| 84 | } catch { |
| 85 | return []; |
| 86 | } |
| 87 | const list = Array.isArray(j) ? j : j && typeof j === "object" ? ((j as Record<string, unknown>).links ?? (j as Record<string, unknown>).results ?? []) : []; |
| 88 | if (!Array.isArray(list)) return []; |
| 89 | const out: Bookmark[] = []; |
| 90 | for (const raw of list as Array<Record<string, unknown>>) { |
| 91 | const url = str(raw?.url); |
| 92 | if (!/^https?:\/\//i.test(url)) continue; |
| 93 | let host = url; |
| 94 | try { |
| 95 | host = new URL(url).host; |
| 96 | } catch { |
| 97 | /* keep the address */ |
| 98 | } |
| 99 | const tags = Array.isArray(raw.tags) ? raw.tags.map(str).filter(Boolean) : str(raw.tags).split(",").map((t) => t.trim()).filter(Boolean); |
| 100 | out.push({ id: str(raw.id) || url, url, title: str(raw.title).trim().slice(0, 300) || host, tags, createdAt: str(raw.created_at) || null }); |
| 101 | } |
| 102 | return out; |
| 103 | } |
| 104 | |
| 105 | export class BookmarksService { |
| 106 | private items: Bookmark[] = []; |
| 107 | private known: Bookmark[] = []; |
| 108 | private checkOn = true; |
| 109 | /** Sites checked this session, and when: a site is asked about again after CHECK_AGAIN_MS. */ |
| 110 | private checkedHosts = new Map<string, number>(); |
| 111 | private fullHosts = new Set<string>(); |
| 112 | private checkedPages = new Set<string>(); |
| 113 | private updatedAt: number | null = null; |
| 114 | private source: BookmarksState["source"] = "none"; |
| 115 | private error: string | undefined; |
| 116 | private inFlight: Promise<BookmarksState> | null = null; |
| 117 | private keys = new Set<string>(); |
| 118 | |
| 119 | constructor(private d: BookmarksDeps) { |
| 120 | try { |
| 121 | const t = d.loadCopy(); |
| 122 | const j = t ? (JSON.parse(t) as { items?: unknown; updatedAt?: unknown }) : null; |
| 123 | if (j && Array.isArray(j.items)) { |
| 124 | this.items = parseLinks(JSON.stringify((j.items as Array<Record<string, unknown>>).map((b) => ({ ...b, created_at: b.createdAt, tags: b.tags })))); |
| 125 | this.updatedAt = typeof j.updatedAt === "number" ? j.updatedAt : null; |
| 126 | this.source = this.items.length || this.updatedAt ? "copy" : "none"; |
| 127 | if ((j as { checkPages?: unknown }).checkPages === false) this.checkOn = false; |
| 128 | const k = (j as { known?: unknown }).known; |
| 129 | if (Array.isArray(k)) this.known = parseLinks(JSON.stringify((k as Array<Record<string, unknown>>).map((b) => ({ ...b, created_at: b.createdAt })))); |
| 130 | } |
| 131 | } catch { |
| 132 | /* no copy */ |
| 133 | } |
| 134 | this.reindex(); |
| 135 | } |
| 136 | |
| 137 | private now() { |
| 138 | return (this.d.now ?? Date.now)(); |
| 139 | } |
| 140 | private reindex() { |
| 141 | this.keys = new Set([...this.items, ...this.known].map((b) => normalizeUrl(b.url))); |
| 142 | } |
| 143 | private persistCopy() { |
| 144 | this.d.saveCopy(JSON.stringify({ updatedAt: this.updatedAt, items: this.items, known: this.known, checkPages: this.checkOn })); |
| 145 | } |
| 146 | |
| 147 | /** What to show now. Never reaches the service (or the keychain). */ |
| 148 | state(): BookmarksState { |
| 149 | return { items: this.items, known: this.known, checkPages: this.checkOn, updatedAt: this.updatedAt, source: this.source, connected: this.d.ready() ? this.d.connected() : this.source === "service", limit: LIMIT, ...(this.error ? { error: this.error } : {}) }; |
| 150 | } |
| 151 | |
| 152 | has(url: string): boolean { |
| 153 | return this.keys.has(normalizeUrl(url)); |
| 154 | } |
| 155 | |
| 156 | /** Fetch the list from the service. One at a time; a second call while one is running gets the same answer. */ |
| 157 | refresh(): Promise<BookmarksState> { |
| 158 | if (this.inFlight) return this.inFlight; |
| 159 | this.inFlight = (async () => { |
| 160 | try { |
| 161 | if (!this.d.connected()) { |
| 162 | this.error = undefined; |
| 163 | return this.state(); |
| 164 | } |
| 165 | const text = await this.d.call("list_links", { limit: LIMIT }, { fixed: false }); |
| 166 | const next = parseLinks(text); |
| 167 | const before = JSON.stringify(this.items.map((b) => [b.id, b.url, b.title, b.tags])); |
| 168 | this.items = next; |
| 169 | this.updatedAt = this.now(); |
| 170 | this.source = "service"; |
| 171 | this.error = undefined; |
| 172 | this.reindex(); |
| 173 | this.persistCopy(); |
| 174 | // Always tell the window after a successful load, even when the list is the same: it may still think it is not |
| 175 | // connected (it starts from the local copy), and then it would never check the page you are on. |
| 176 | void before; |
| 177 | this.d.changed(this.state()); |
| 178 | return this.state(); |
| 179 | } catch (e) { |
| 180 | this.error = e instanceof Error ? e.message : String(e); |
| 181 | return this.state(); |
| 182 | } finally { |
| 183 | this.inFlight = null; |
| 184 | } |
| 185 | })(); |
| 186 | return this.inFlight; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Search all of the person's links on the service (the local list holds only the newest ones). Only what the person typed |
| 191 | * into the bookmarks search is sent. Results are not added to the local list. |
| 192 | */ |
| 193 | async search(query: string): Promise<{ items: Bookmark[]; error?: string }> { |
| 194 | const q = query.trim().slice(0, 200); |
| 195 | if (q.length < 2) return { items: [] }; |
| 196 | if (!this.d.connected()) return { items: [], error: "Connect askrobots.com to search all your bookmarks." }; |
| 197 | try { |
| 198 | const found = parseLinks(await this.d.call("list_links", { search: q, limit: LIMIT }, { fixed: false })); |
| 199 | this.remember(found); |
| 200 | return { items: found }; |
| 201 | } catch (e) { |
| 202 | return { items: [], error: e instanceof Error ? e.message : String(e) }; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** Remember bookmarks seen outside the list (searches, page checks), so ☆ knows those pages are saved. */ |
| 207 | private remember(found: Bookmark[]): void { |
| 208 | const inList = new Set(this.items.map((b) => normalizeUrl(b.url))); |
| 209 | const have = new Set(this.known.map((b) => normalizeUrl(b.url))); |
| 210 | const before = this.known.length; |
| 211 | for (const b of found) { |
| 212 | const k = normalizeUrl(b.url); |
| 213 | if (!inList.has(k) && !have.has(k)) { |
| 214 | this.known.push(b); |
| 215 | have.add(k); |
| 216 | } |
| 217 | } |
| 218 | if (this.known.length > MAX_KNOWN) this.known = this.known.slice(-MAX_KNOWN); |
| 219 | if (this.known.length !== before) { |
| 220 | this.reindex(); |
| 221 | this.persistCopy(); |
| 222 | this.d.changed(this.state()); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** Whether pages the person visits are checked against the service (a setting; on unless they turned it off). */ |
| 227 | get checkPages(): boolean { |
| 228 | return this.checkOn; |
| 229 | } |
| 230 | setCheckPages(on: boolean): BookmarksState { |
| 231 | this.checkOn = on; |
| 232 | this.persistCopy(); |
| 233 | return this.state(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Is the page you are on saved, beyond what the local list knows? Asks the service about the page's site (and, if the site |
| 238 | * has more saved pages than one answer holds, about the page itself), once per site every ten minutes. Sends the site's name and |
| 239 | * possibly the page address to the person's own account; never when the setting is off or the connection is not open yet. |
| 240 | */ |
| 241 | async checkPage(url: string): Promise<{ checked: boolean; saved: boolean }> { |
| 242 | if (!this.checkOn || !this.d.ready() || !/^https?:\/\//i.test(url)) return { checked: false, saved: this.has(url) }; |
| 243 | if (this.has(url)) return { checked: false, saved: true }; |
| 244 | let host: string; |
| 245 | try { |
| 246 | host = new URL(url).hostname.replace(/^www\./, ""); |
| 247 | } catch { |
| 248 | return { checked: false, saved: false }; |
| 249 | } |
| 250 | try { |
| 251 | const at = this.checkedHosts.get(host); |
| 252 | if (at === undefined || this.now() - at > CHECK_AGAIN_MS) { |
| 253 | if (!this.d.connected()) return { checked: false, saved: false }; |
| 254 | this.checkedHosts.set(host, this.now()); |
| 255 | this.fullHosts.delete(host); |
| 256 | const onSite = parseLinks(await this.d.call("list_links", { search: host, limit: LIMIT }, { fixed: false })).filter((b) => { |
| 257 | try { |
| 258 | return new URL(b.url).hostname.replace(/^www\./, "") === host; |
| 259 | } catch { |
| 260 | return false; |
| 261 | } |
| 262 | }); |
| 263 | this.remember(onSite); |
| 264 | if (onSite.length < LIMIT) this.fullHosts.add(host); // the answer held every saved page on this site |
| 265 | } |
| 266 | if (!this.has(url) && !this.fullHosts.has(host) && !this.checkedPages.has(normalizeUrl(url))) { |
| 267 | this.checkedPages.add(normalizeUrl(url)); |
| 268 | const page = url.replace(/^https?:\/\//i, "").split("#")[0]!.slice(0, 200); |
| 269 | this.remember(parseLinks(await this.d.call("list_links", { search: page, limit: 10 }, { fixed: false })).filter((b) => normalizeUrl(b.url) === normalizeUrl(url))); |
| 270 | } |
| 271 | } catch (e) { |
| 272 | // A failed check is no answer: forget that the site was checked, so the next page there tries again, and say why. |
| 273 | this.checkedHosts.delete(host); |
| 274 | this.checkedPages.delete(normalizeUrl(url)); |
| 275 | this.error = `could not check ${host}: ${e instanceof Error ? e.message : String(e)}`; |
| 276 | return { checked: false, saved: this.has(url) }; |
| 277 | } |
| 278 | return { checked: true, saved: this.has(url) }; |
| 279 | } |
| 280 | |
| 281 | /** Save an address as a bookmark (the person's own click), then refresh. */ |
| 282 | async add(url: string, title: string): Promise<{ ok: boolean; already?: boolean; message?: string }> { |
| 283 | if (!/^https?:\/\//i.test(url)) return { ok: false, message: "only web pages can be saved" }; |
| 284 | if (!this.d.connected()) return { ok: false, message: "Connect askrobots.com in the assistant panel first." }; |
| 285 | if (this.source !== "service") await this.refresh(); // never save twice because the list had not been loaded yet |
| 286 | if (this.has(url)) return { ok: true, already: true }; |
| 287 | try { |
| 288 | await this.d.call("create_link", { url, title: title.slice(0, 255) || url }, { fixed: true }); |
| 289 | } catch (e) { |
| 290 | return { ok: false, message: e instanceof Error ? e.message : String(e) }; |
| 291 | } |
| 292 | await this.refresh(); |
| 293 | return { ok: true }; |
| 294 | } |
| 295 | } |