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
reader-service.ts · 9.9 KB · 208 lines
| 1 | /** |
| 2 | * AI Reader, main-process side: read the page, open the reader tab, write the TL;DR. |
| 3 | * |
| 4 | * Privacy rules (the point of the design): |
| 5 | * - Nothing is sent anywhere unless the user clicks Reader (or an automation client explicitly passes |
| 6 | * `acknowledge: true`). Page text goes to the AI provider ONLY for the TL;DR; the reader view itself |
| 7 | * is built locally. |
| 8 | * - The first summary per provider each session asks first, naming the provider, the model, the host |
| 9 | * and how much text. (DBB_AI_CONSENT=1 pre-answers yes, for scripted demos.) |
| 10 | * - Pages with a visible password field are never read. |
| 11 | * - Every provider call is announced as an `ai-summary` event (toast + event log) with the host, how |
| 12 | * many characters were sent, and what it cost — never silent, like every other spend in the browser. |
| 13 | * - API keys stay in this process; the UI only ever receives provider and model names. |
| 14 | */ |
| 15 | |
| 16 | import { readFileSync } from "node:fs"; |
| 17 | import { dirname, join } from "node:path"; |
| 18 | import { fileURLToPath } from "node:url"; |
| 19 | import { dialog, webContents, type BrowserWindow, type WebContents } from "electron"; |
| 20 | import { AiError, summarize, type SummaryResult } from "./ai.js"; |
| 21 | import { redact, type AiConfig, type Provider } from "./ai-config.js"; |
| 22 | import { blocksToText, buildReaderHtml, footFor, tldrUpdateScript, validateExtraction, type Extracted } from "./reader.js"; |
| 23 | import { readerUrl, type ReaderPages } from "./reader-pages.js"; |
| 24 | import { safeExternalUrl } from "./urls.js"; |
| 25 | |
| 26 | const here = dirname(fileURLToPath(import.meta.url)); |
| 27 | let assets: { readability: string; extractor: string } | null = null; |
| 28 | function loadAssets() { |
| 29 | assets ??= { |
| 30 | readability: readFileSync(join(here, "Readability.js"), "utf8"), |
| 31 | extractor: readFileSync(join(here, "reader-extractor.js"), "utf8"), |
| 32 | }; |
| 33 | return assets; |
| 34 | } |
| 35 | |
| 36 | const LABEL: Record<Provider, string> = { anthropic: "Anthropic", openai: "OpenAI" }; |
| 37 | |
| 38 | export interface ReaderDeps { |
| 39 | getWindow(): BrowserWindow | null; |
| 40 | askChrome(op: string, args: Record<string, unknown>): Promise<unknown>; |
| 41 | emit(payload: Record<string, unknown>): void; |
| 42 | getAi(): AiConfig | null; |
| 43 | /** Where reader views are served from (and updated when a summary arrives). */ |
| 44 | pages: ReaderPages; |
| 45 | } |
| 46 | |
| 47 | export interface OpenOptions { |
| 48 | wcId: number; |
| 49 | /** A human clicked: ask for consent with a dialog. False for automation, which must pass `acknowledge`. */ |
| 50 | interactive: boolean; |
| 51 | /** false = open the reader with no AI summary at all. */ |
| 52 | withAi?: boolean; |
| 53 | acknowledge?: boolean; |
| 54 | } |
| 55 | |
| 56 | export interface ReaderResult { |
| 57 | ok: boolean; |
| 58 | reason?: "not-web" | "login" | "unreadable" | "is-reader" | "no-tab" | "error"; |
| 59 | message?: string; |
| 60 | tab?: { id: number; wcId: number }; |
| 61 | /** What happened with the summary: pending (still running), or the final state. */ |
| 62 | ai?: { state: "pending" | "none" | "declined"; provider?: Provider; model?: string }; |
| 63 | /** Resolves when the summary has been written (or failed). Not serialized. */ |
| 64 | done?: Promise<AiOutcome>; |
| 65 | } |
| 66 | |
| 67 | export type AiOutcome = |
| 68 | | { ok: true; provider: Provider; model: string; tldr: string; points: string[]; kind: string; ms: number; costUsd: number | null; usage: { inputTokens: number; outputTokens: number }; sentChars: number; truncated: boolean } |
| 69 | | { ok: false; code: string; message: string } |
| 70 | | { ok: false; code: "skipped"; message: string }; |
| 71 | |
| 72 | export function createReader(deps: ReaderDeps) { |
| 73 | const consented = new Set<Provider>(process.env.DBB_AI_CONSENT === "1" ? ["anthropic", "openai"] : []); |
| 74 | const win = () => deps.getWindow(); |
| 75 | |
| 76 | const isOurGuest = (wc: WebContents | undefined): wc is WebContents => { |
| 77 | const w = win(); |
| 78 | return !!wc && !wc.isDestroyed() && wc.getType() === "webview" && !!w && wc.hostWebContents === w.webContents; |
| 79 | }; |
| 80 | |
| 81 | async function extract(wc: WebContents): Promise<unknown> { |
| 82 | const a = loadAssets(); |
| 83 | const full = `(() => {\n${a.readability}\n;return (${a.extractor})(typeof Readability === "function" ? Readability : null);\n})()`; |
| 84 | try { |
| 85 | return await wc.executeJavaScript(full, false); |
| 86 | } catch { |
| 87 | // A page whose CSP/Trusted Types blocks Readability's DOM work: fall back to visible text only. |
| 88 | return await wc.executeJavaScript(`(${a.extractor})(null)`, false); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | async function askConsent(cfg: AiConfig, ex: Extracted, chars: number): Promise<boolean> { |
| 93 | if (consented.has(cfg.provider)) return true; |
| 94 | const w = win(); |
| 95 | if (!w) return false; |
| 96 | const label = LABEL[cfg.provider]; |
| 97 | const host = new URL(ex.url).host; |
| 98 | const { response, checkboxChecked } = await dialog.showMessageBox(w, { |
| 99 | type: "question", |
| 100 | buttons: ["Summarize", "Read without a summary"], |
| 101 | defaultId: 0, |
| 102 | cancelId: 1, |
| 103 | title: "Send this page to an AI provider?", |
| 104 | message: `Send the text of this page to ${label} (${cfg.model}) to write a TL;DR?`, |
| 105 | detail: |
| 106 | `About ${chars.toLocaleString("en-US")} characters of text from ${host} will be sent to ${label}. ` + |
| 107 | `Pages with a login form are never read. Usage is billed to your ${label} account.\n\n` + |
| 108 | `The reader view itself is built on this computer; only the summary comes from ${label}.`, |
| 109 | checkboxLabel: `Don't ask again this session for ${label}`, |
| 110 | }); |
| 111 | if (response !== 0) return false; |
| 112 | if (checkboxChecked) consented.add(cfg.provider); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | async function open(o: OpenOptions): Promise<ReaderResult> { |
| 117 | const wc = webContents.fromId(o.wcId); |
| 118 | if (!isOurGuest(wc)) return { ok: false, reason: "no-tab", message: "No such tab." }; |
| 119 | const url = wc.getURL(); |
| 120 | if (url.startsWith("data:text/html")) return { ok: false, reason: "is-reader", message: "This tab is already a reader view." }; |
| 121 | if (!safeExternalUrl(url)) return { ok: false, reason: "not-web", message: "Only web pages can be opened in the AI Reader." }; |
| 122 | |
| 123 | let raw: unknown; |
| 124 | try { |
| 125 | raw = await extract(wc); |
| 126 | } catch { |
| 127 | return { ok: false, reason: "unreadable", message: "Couldn't read this page." }; |
| 128 | } |
| 129 | if (raw && typeof raw === "object" && (raw as { error?: string }).error === "login") { |
| 130 | return { ok: false, reason: "login", message: "This page has a login form, so it isn't read by the AI Reader." }; |
| 131 | } |
| 132 | const ex = validateExtraction(raw); |
| 133 | if (!ex) return { ok: false, reason: "unreadable", message: "There's no readable text on this page." }; |
| 134 | const text = blocksToText(ex.blocks); |
| 135 | |
| 136 | // ---- decide whether anything may leave this machine ---- |
| 137 | const cfg = deps.getAi(); |
| 138 | let state: "pending" | "none" | "declined" = "none"; |
| 139 | if (cfg && o.withAi !== false) { |
| 140 | const allowed = o.interactive ? await askConsent(cfg, ex, Math.min(text.length, 60_000)) : !!o.acknowledge || consented.has(cfg.provider); |
| 141 | state = allowed ? "pending" : "declined"; |
| 142 | } else if (cfg) state = "declined"; |
| 143 | |
| 144 | // ---- open the reader tab (built locally from validated data) ---- |
| 145 | const html = buildReaderHtml(ex, { |
| 146 | ai: state === "pending" && cfg ? { state: "pending", provider: LABEL[cfg.provider], model: cfg.model } : { state: state === "pending" ? "none" : state }, |
| 147 | }); |
| 148 | const pageId = deps.pages.put(html); |
| 149 | let tab: { id: number; wcId: number }; |
| 150 | try { |
| 151 | tab = (await deps.askChrome("tab.new", { url: readerUrl(pageId), activate: true, wait: true, reader: ex.url })) as { id: number; wcId: number }; |
| 152 | } catch (e) { |
| 153 | return { ok: false, reason: "error", message: `Couldn't open the reader tab: ${(e as Error).message}` }; |
| 154 | } |
| 155 | |
| 156 | if (state !== "pending" || !cfg) { |
| 157 | return { ok: true, tab, ai: { state } }; |
| 158 | } |
| 159 | |
| 160 | // ---- the TL;DR, written in the background ---- |
| 161 | const done = (async (): Promise<AiOutcome> => { |
| 162 | const target = () => { |
| 163 | const t = webContents.fromId(tab.wcId); |
| 164 | return t && !t.isDestroyed() ? t : null; |
| 165 | }; |
| 166 | deps.emit({ kind: "reader-busy", wcId: tab.wcId, busy: true, provider: LABEL[cfg.provider], model: cfg.model }); |
| 167 | try { |
| 168 | const r: SummaryResult = await summarize(cfg, { title: ex.title, url: ex.url, text, lang: ex.lang }); |
| 169 | // Update the stored page FIRST so that any load from now on (Back, Forward, reload) shows the finished view… |
| 170 | const foot = footFor(r); |
| 171 | deps.pages.update(pageId, buildReaderHtml(ex, { ai: { state: "done", tldr: r.tldr, points: r.points, foot } })); |
| 172 | // …then fill in the page that is open right now. |
| 173 | const t = target(); |
| 174 | if (t) await t.executeJavaScript(tldrUpdateScript({ state: "done", tldr: r.tldr, points: r.points, foot })); |
| 175 | // Never silent: every provider call is an event, with what was sent and what it cost. |
| 176 | deps.emit({ |
| 177 | kind: "ai-summary", |
| 178 | provider: r.provider, |
| 179 | model: r.model, |
| 180 | host: new URL(ex.url).host, |
| 181 | sentChars: r.sentChars, |
| 182 | truncated: r.truncated, |
| 183 | ms: r.ms, |
| 184 | inputTokens: r.usage.inputTokens, |
| 185 | outputTokens: r.usage.outputTokens, |
| 186 | costUsd: r.costUsd, |
| 187 | }); |
| 188 | return { ok: true, provider: r.provider, model: r.model, tldr: r.tldr, points: r.points, kind: r.kind, ms: r.ms, costUsd: r.costUsd, usage: r.usage, sentChars: r.sentChars, truncated: r.truncated }; |
| 189 | } catch (e) { |
| 190 | const err = e instanceof AiError ? e : new AiError("provider", String((e as Error)?.message ?? e)); |
| 191 | const message = redact(err.message, cfg); |
| 192 | deps.pages.update(pageId, buildReaderHtml(ex, { ai: { state: "error", message } })); |
| 193 | const t = target(); |
| 194 | if (t) await t.executeJavaScript(tldrUpdateScript({ state: "error", tldr: `The summary could not be written: ${message}`, points: [], foot: "" })).catch(() => {}); |
| 195 | deps.emit({ kind: "ai-error", provider: cfg.provider, model: cfg.model, code: err.code, message }); |
| 196 | return { ok: false, code: err.code, message }; |
| 197 | } finally { |
| 198 | deps.emit({ kind: "reader-busy", wcId: tab.wcId, busy: false }); |
| 199 | } |
| 200 | })(); |
| 201 | |
| 202 | return { ok: true, tab, ai: { state: "pending", provider: cfg.provider, model: cfg.model }, done }; |
| 203 | } |
| 204 | |
| 205 | return { open }; |
| 206 | } |
| 207 | |
| 208 | export type ReaderService = ReturnType<typeof createReader>; |