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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
history-copy.ts · 7.3 KB · 210 lines
| 1 | /** |
| 2 | * A copy of your askrobots history on this machine, so the address bar can suggest pages you visited, on any of your |
| 3 | * computers, without sending what you type anywhere. Electron-free (main.ts gives it the connection and a file), so it is |
| 4 | * unit-tested. |
| 5 | * |
| 6 | * - Only addresses, titles and times: never the pages' text. |
| 7 | * - Kept up to date with the account's list_history: each answer gives a cursor for the next read, the visits added since |
| 8 | * and the ids of visits forgotten since (on any computer). "full_copy" means start again from nothing. |
| 9 | * - Signing out, or signing in again (perhaps as someone else), deletes it. |
| 10 | * - At most MAX_VISITS visits; the oldest go first. |
| 11 | */ |
| 12 | |
| 13 | export const MAX_VISITS = 20_000; |
| 14 | const PAGE = 500; |
| 15 | /** Pages read in one go; the rest come on the next read. */ |
| 16 | const MAX_PAGES = 40; |
| 17 | |
| 18 | export interface CopyVisit { |
| 19 | id: string; |
| 20 | url: string; |
| 21 | title: string; |
| 22 | /** When it was visited, in ms. */ |
| 23 | at: number; |
| 24 | } |
| 25 | |
| 26 | /** A page, all its visits together: what the address bar suggests. */ |
| 27 | export interface HistoryPage { |
| 28 | url: string; |
| 29 | title: string; |
| 30 | visits: number; |
| 31 | last: number; |
| 32 | } |
| 33 | |
| 34 | export interface HistoryCopyDeps { |
| 35 | call(tool: string, args: Record<string, unknown>): Promise<string>; |
| 36 | /** Signed in, with the history permission. */ |
| 37 | connected(): boolean; |
| 38 | /** The saved copy, or null. */ |
| 39 | load(): string | null; |
| 40 | /** Save the copy; null deletes it. */ |
| 41 | save(text: string | null): void; |
| 42 | now(): number; |
| 43 | } |
| 44 | |
| 45 | interface Saved { |
| 46 | v: 1; |
| 47 | cursor?: string; |
| 48 | visits: CopyVisit[]; |
| 49 | } |
| 50 | |
| 51 | export class HistoryCopy { |
| 52 | private visits = new Map<string, CopyVisit>(); |
| 53 | private cursor: string | undefined; |
| 54 | private loaded = false; |
| 55 | private syncing: Promise<void> | null = null; |
| 56 | private pagesCache: HistoryPage[] | null = null; |
| 57 | private pagesAt = 0; |
| 58 | private lastError: string | undefined; |
| 59 | private lastSync: number | undefined; |
| 60 | |
| 61 | constructor(private d: HistoryCopyDeps) {} |
| 62 | |
| 63 | private ensureLoaded(): void { |
| 64 | if (this.loaded) return; |
| 65 | this.loaded = true; |
| 66 | try { |
| 67 | const raw = this.d.load(); |
| 68 | if (!raw) return; |
| 69 | const s = JSON.parse(raw) as Saved; |
| 70 | if (s.v !== 1 || !Array.isArray(s.visits)) return; |
| 71 | for (const v of s.visits) if (v && typeof v.id === "string" && typeof v.url === "string" && typeof v.at === "number") this.visits.set(v.id, { id: v.id, url: v.url, title: String(v.title ?? ""), at: v.at }); |
| 72 | this.cursor = typeof s.cursor === "string" ? s.cursor : undefined; |
| 73 | } catch { |
| 74 | this.visits.clear(); |
| 75 | this.cursor = undefined; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private persist(): void { |
| 80 | const s: Saved = { v: 1, ...(this.cursor ? { cursor: this.cursor } : {}), visits: [...this.visits.values()] }; |
| 81 | this.d.save(JSON.stringify(s)); |
| 82 | } |
| 83 | |
| 84 | state(): { visits: number; pages: number; lastSync?: number; error?: string } { |
| 85 | this.ensureLoaded(); |
| 86 | return { visits: this.visits.size, pages: this.pages().length, ...(this.lastSync ? { lastSync: this.lastSync } : {}), ...(this.lastError ? { error: this.lastError } : {}) }; |
| 87 | } |
| 88 | |
| 89 | /** Signed out, or signed in again: the copy belongs to the account it came from, so it goes. */ |
| 90 | clear(): void { |
| 91 | this.loaded = true; |
| 92 | this.visits.clear(); |
| 93 | this.cursor = undefined; |
| 94 | this.pagesCache = null; |
| 95 | this.lastError = undefined; |
| 96 | this.lastSync = undefined; |
| 97 | this.d.save(null); |
| 98 | } |
| 99 | |
| 100 | /** Take visits out here at once (a forget), before the account's list says so too. */ |
| 101 | remove(gone: (v: CopyVisit) => boolean): void { |
| 102 | this.ensureLoaded(); |
| 103 | let any = false; |
| 104 | for (const [id, v] of this.visits) if (gone(v)) (this.visits.delete(id), (any = true)); |
| 105 | if (any) { |
| 106 | this.pagesCache = null; |
| 107 | this.persist(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** Read what changed on the account since the last read. One at a time; a second call waits for the first. */ |
| 112 | sync(): Promise<void> { |
| 113 | if (this.syncing) return this.syncing; |
| 114 | this.syncing = this.pull().finally(() => { |
| 115 | this.syncing = null; |
| 116 | }); |
| 117 | return this.syncing; |
| 118 | } |
| 119 | |
| 120 | private async pull(): Promise<void> { |
| 121 | this.ensureLoaded(); |
| 122 | if (!this.d.connected()) return; |
| 123 | let restarted = false; |
| 124 | let changed = false; |
| 125 | try { |
| 126 | for (let page = 0; page < MAX_PAGES; page++) { |
| 127 | const res = JSON.parse(await this.d.call("list_history", { ...(this.cursor ? { cursor: this.cursor } : {}), limit: PAGE })) as { |
| 128 | visits?: Array<{ id?: unknown; url?: unknown; title?: unknown; visited_at?: unknown }>; |
| 129 | forgotten?: unknown[]; |
| 130 | full_copy?: boolean; |
| 131 | cursor?: unknown; |
| 132 | has_more?: boolean; |
| 133 | error?: unknown; |
| 134 | }; |
| 135 | if (res.error) { |
| 136 | // a cursor the account no longer knows: start again from nothing, once |
| 137 | if (this.cursor && !restarted) { |
| 138 | restarted = true; |
| 139 | this.visits.clear(); |
| 140 | this.cursor = undefined; |
| 141 | changed = true; |
| 142 | page = -1; |
| 143 | continue; |
| 144 | } |
| 145 | throw new Error(String(res.error)); |
| 146 | } |
| 147 | if (res.full_copy && this.cursor && !restarted) { |
| 148 | restarted = true; |
| 149 | this.visits.clear(); |
| 150 | this.cursor = undefined; |
| 151 | changed = true; |
| 152 | page = -1; |
| 153 | continue; |
| 154 | } |
| 155 | for (const id of res.forgotten ?? []) if (this.visits.delete(String(id))) changed = true; |
| 156 | for (const v of res.visits ?? []) { |
| 157 | if (typeof v.id !== "string" && typeof v.id !== "number") continue; |
| 158 | if (typeof v.url !== "string") continue; |
| 159 | const at = Date.parse(String(v.visited_at ?? "")); |
| 160 | this.visits.set(String(v.id), { id: String(v.id), url: v.url, title: typeof v.title === "string" ? v.title.slice(0, 300) : "", at: Number.isFinite(at) ? at : 0 }); |
| 161 | changed = true; |
| 162 | } |
| 163 | if (typeof res.cursor === "string") this.cursor = res.cursor; |
| 164 | if (!res.has_more) break; |
| 165 | } |
| 166 | this.lastError = undefined; |
| 167 | this.lastSync = this.d.now(); |
| 168 | } catch (e) { |
| 169 | this.lastError = e instanceof Error ? e.message.slice(0, 200) : String(e); |
| 170 | } |
| 171 | if (this.visits.size > MAX_VISITS) { |
| 172 | const oldest = [...this.visits.values()].sort((a, b) => a.at - b.at).slice(0, this.visits.size - MAX_VISITS); |
| 173 | for (const v of oldest) this.visits.delete(v.id); |
| 174 | changed = true; |
| 175 | } |
| 176 | if (changed) { |
| 177 | this.pagesCache = null; |
| 178 | this.persist(); |
| 179 | } else if (this.cursor) this.persist(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Every page, the ones you visit most and most lately first: how often (up to 20 visits counted), weighted by how long ago |
| 184 | * the last visit was. |
| 185 | */ |
| 186 | pages(): HistoryPage[] { |
| 187 | this.ensureLoaded(); |
| 188 | if (this.pagesCache && this.d.now() - this.pagesAt < 60_000) return this.pagesCache; |
| 189 | const by = new Map<string, HistoryPage>(); |
| 190 | for (const v of this.visits.values()) { |
| 191 | const p = by.get(v.url); |
| 192 | if (!p) by.set(v.url, { url: v.url, title: v.title, visits: 1, last: v.at }); |
| 193 | else { |
| 194 | p.visits++; |
| 195 | if (v.at >= p.last) { |
| 196 | p.last = v.at; |
| 197 | if (v.title) p.title = v.title; |
| 198 | } else if (!p.title && v.title) p.title = v.title; |
| 199 | } |
| 200 | } |
| 201 | const now = this.d.now(); |
| 202 | const weight = (p: HistoryPage) => { |
| 203 | const days = (now - p.last) / 86_400_000; |
| 204 | return Math.min(p.visits, 20) * (days < 1 ? 4 : days < 7 ? 2 : days < 30 ? 1 : 0.5); |
| 205 | }; |
| 206 | this.pagesAt = now; |
| 207 | this.pagesCache = [...by.values()].sort((a, b) => weight(b) - weight(a) || b.last - a.last); |
| 208 | return this.pagesCache; |
| 209 | } |
| 210 | } |