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, 56 minutes ago
History
reader.mjs · 7.8 KB · 95 lines
| 1 | // The AI Reader. The default run sends NOTHING anywhere (it never acknowledges); the checks that call a real |
| 2 | // AI provider only run with E2E_LIVE_AI=1 and a configured key. |
| 3 | const KEY_SHAPE = /sk-(?:ant-|proj-|svcacct-)?[A-Za-z0-9_-]{20,}/; |
| 4 | |
| 5 | export default async function (ctx) { |
| 6 | const { call: T, check, base, sleep } = ctx; |
| 7 | const seen = []; |
| 8 | const rec = (v) => (seen.push(JSON.stringify(v)), v); |
| 9 | const textOf = async (tab) => (await T("page.text", { tab })).text; |
| 10 | const firstTab = async () => (await T("tabs.list"))[0].id; |
| 11 | |
| 12 | const st = rec(await T("ai.status")); |
| 13 | check(typeof st.configured === "boolean" && !KEY_SHAPE.test(JSON.stringify(st)), "ai.status reports provider/model and contains no key material", JSON.stringify({ provider: st.provider, model: st.model })); |
| 14 | |
| 15 | // ---- without acknowledge: the reader opens, nothing is sent ---- |
| 16 | await T("page.goto", { url: base + "/readable" }); |
| 17 | const ev0 = (await T("x402.events", { since: 0 })).next; |
| 18 | let r = rec(await T("reader.open", { acknowledge: false })); |
| 19 | check(r.ok && (r.state.state === "declined" || r.state.state === "none") && !r.summary, "without acknowledge: the reader opens and makes no AI call", JSON.stringify(r.state)); |
| 20 | await sleep(500); |
| 21 | check(!(await T("x402.events", { since: ev0 })).events.some((e) => e.kind === "ai-summary"), "…and no ai-summary event exists (nothing left the machine)"); |
| 22 | const rtab = r.tab.id; |
| 23 | const t = await textOf(rtab); |
| 24 | check(/The Keeper of the Light/.test(t) && /forty years/.test(t) && /Forty years of notebooks/.test(t), "the reader shows the article: title, headings, body"); |
| 25 | check(!/BUY CHEAP WATCHES/.test(t) && !/Trending/.test(t) && !/Ten celebrity diets/.test(t) && !/Subscribe now/.test(t) && !/Privacy/.test(t), "navigation, ad, sidebar and footer junk is stripped"); |
| 26 | const dom = JSON.parse((await T("page.eval", { tab: rtab, js: `JSON.stringify({ scripts: document.querySelectorAll('script').length, handlers: [...document.querySelectorAll('*')].filter(e => [...e.attributes].some(a => /^on/i.test(a.name))).length, jsLinks: [...document.querySelectorAll('a')].filter(a => /^javascript:/i.test(a.getAttribute('href') || '')).length, title: document.title, csp: document.querySelector('meta[http-equiv=Content-Security-Policy]')?.content })` })).value); |
| 27 | check(dom.scripts === 0 && dom.handlers === 0 && dom.jsLinks === 0, "the reader page has no <script>, no on* attributes, no javascript: links", JSON.stringify({ s: dom.scripts, h: dom.handlers, j: dom.jsLinks })); |
| 28 | check(/Keeper/.test(dom.title) && !/HACKED/.test(dom.title), "the page's planted onerror payload never ran in the reader (title intact)", dom.title); |
| 29 | check(/default-src 'none'/.test(dom.csp), "a strict CSP is in force in the reader"); |
| 30 | check((await T("tabs.list")).find((x) => x.id === rtab).url === "", "the reader tab's URL is not a giant data: URL"); |
| 31 | const c = await ctx.chrome(); |
| 32 | check((await T("page.eval", { tab: rtab, js: "location.href" })).value.startsWith("dbb-reader://"), "the reader is served from a short dbb-reader:// address"); |
| 33 | check((await c.ev(`tabs.find((t) => t.wcId === ${r.tab.wcId}).wv.canGoBack()`)) === false, "a new reader tab has nothing to go Back to (no blank about:blank entry)"); |
| 34 | await T("page.reload", { tab: rtab }); |
| 35 | check(/The Keeper of the Light/.test(await textOf(rtab)) && !/expired/i.test(await textOf(rtab)), "reloading the reader tab shows the page again, straight from the store"); |
| 36 | const plain = await T("tabs.new", { url: base + "/free", wait: true }); |
| 37 | check((await c.ev(`tabs.find((t) => t.id === ${plain.id}).wv.canGoBack()`)) === false, "ANY new tab starts with nothing to go Back to (not just readers)"); |
| 38 | await T("tabs.close", { tab: plain.id }); |
| 39 | await T("tabs.close", { tab: rtab }); |
| 40 | await T("tabs.activate", { tab: await firstTab() }); |
| 41 | |
| 42 | // ---- refusals ---- |
| 43 | await T("page.goto", { url: base + "/login" }); |
| 44 | r = rec(await T("reader.open", { acknowledge: true })); |
| 45 | check(!r.ok && r.reason === "login", "a page with a password field is refused: never read, never sent", r.message); |
| 46 | await T("page.goto", { url: "about:blank" }).catch(() => {}); |
| 47 | r = rec(await T("reader.open", { acknowledge: true }).catch((e) => ({ ok: false, reason: e.code }))); |
| 48 | check(!r.ok, "a non-web page is refused", r.reason); |
| 49 | await T("ai.provider", { provider: "nonsense" }).then(() => check(false, "a bad provider was accepted"), (e) => check(e.code === "bad-params", "an unknown provider is rejected")); |
| 50 | |
| 51 | // ---- live: real provider calls (opt-in) ---- |
| 52 | if (ctx.liveAi && st.configured) { |
| 53 | await T("page.goto", { url: base + "/readable" }); |
| 54 | const evA = (await T("x402.events", { since: 0 })).next; |
| 55 | r = rec(await T("reader.open", { acknowledge: true, wait: true })); |
| 56 | check(r.ok && r.summary?.ok, `acknowledged: a summary was written by ${r.summary?.provider}/${r.summary?.model}`, `${r.summary?.ms} ms, $${r.summary?.costUsd?.toFixed?.(5)}`); |
| 57 | check(!/^\s*pwned\s*$/i.test(r.summary?.tldr || ""), "an injection planted in the article was not obeyed"); |
| 58 | check(!/[-ヿ一-鿿]/.test(r.summary?.tldr || ""), "the summary of an English page is in English"); |
| 59 | const body = await textOf(r.tab.id); |
| 60 | check(body.includes((r.summary?.tldr || "x").slice(0, 40)) && /Summarized by /.test(body), "the TL;DR and the provider/model/cost footer appear in the reader page"); |
| 61 | const evs = (await T("x402.events", { since: evA })).events; |
| 62 | const ai = evs.find((e) => e.kind === "ai-summary"); |
| 63 | check(!!ai && ai.host === new URL(base).host && ai.sentChars > 500, "an ai-summary event records the host, characters sent and cost", `sent=${ai?.sentChars}`); |
| 64 | check(evs.some((e) => e.kind === "reader-busy" && e.busy) && evs.some((e) => e.kind === "reader-busy" && !e.busy), "the tab showed the 'summarizing' state while working"); |
| 65 | |
| 66 | // Back / Forward must show the FINISHED view from the store, not the pending snapshot, and cost nothing. |
| 67 | const tldrStart = (r.summary?.tldr || "x").slice(0, 40); |
| 68 | const calls = () => T("x402.events", { since: 0 }).then((x) => x.events.filter((e) => e.kind === "ai-summary").length); |
| 69 | const callsBefore = await calls(); |
| 70 | await T("page.click", { tab: r.tab.id, selector: "header .meta a" }); // "original": leaves the reader for the source page |
| 71 | await T("page.waitFor", { tab: r.tab.id, url: "/readable", timeoutMs: 8000 }); |
| 72 | await T("page.back", { tab: r.tab.id }); |
| 73 | const afterBack = await textOf(r.tab.id); |
| 74 | check(afterBack.includes(tldrStart) && !/Summarizing with/.test(afterBack), "Back from the original returns to the reader WITH the summary (not the 'Summarizing…' skeleton)"); |
| 75 | await T("page.forward", { tab: r.tab.id }); |
| 76 | await T("page.back", { tab: r.tab.id }); |
| 77 | check((await textOf(r.tab.id)).includes(tldrStart), "…and again after Forward then Back"); |
| 78 | check((await calls()) === callsBefore, "going Back and Forward made no new AI call (it came from the stored page)"); |
| 79 | await T("tabs.close", { tab: r.tab.id }); |
| 80 | await T("tabs.activate", { tab: await firstTab() }); |
| 81 | if (st.available.includes("openai") && st.available.includes("anthropic")) { |
| 82 | const other = st.provider === "anthropic" ? "openai" : "anthropic"; |
| 83 | rec(await T("ai.provider", { provider: other })); |
| 84 | r = rec(await T("reader.open", { acknowledge: true, wait: true })); |
| 85 | check(r.ok && r.summary?.ok && r.summary.provider === other, `switching provider works: ${other} wrote a summary`, `${r.summary?.model}, ${r.summary?.ms} ms`); |
| 86 | await T("tabs.close", { tab: r.tab.id }); |
| 87 | rec(await T("ai.provider", { provider: st.provider })); |
| 88 | } |
| 89 | } else { |
| 90 | console.log(" · live AI checks skipped (set E2E_LIVE_AI=1 with a configured key)"); |
| 91 | } |
| 92 | |
| 93 | // ---- no key material anywhere we looked, nor in the app's own log ---- |
| 94 | check(!KEY_SHAPE.test(seen.join("\n") + "\n" + ctx.appLog()), "no API key appears in any API response or in the app's log"); |
| 95 | } |