A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
chat.mjs · 13.0 KB · 152 lines
1// The in-browser chat, end to end, against a scripted model on the demo server (see /v1/chat/completions there).
2export default async function (ctx) {
3 const { call: T, check, base, sleep } = ctx;
4 const ev = async (since) => T("chat.events", { since });
5 /** Wait until an event matching `pred` shows up after `since`. */
6 async function waitEvent(since, pred, what) {
7 for (let i = 0; i < 200; i++) {
8 const { events } = await ev(since);
9 const hit = events.find(pred);
10 if (hit) return hit;
11 await sleep(50);
12 }
13 throw new Error(`timed out waiting for ${what}`);
14 }
15 const value = async (name) => (await T("page.eval", { js: `document.querySelector('[name=${name}]').value` })).value;
16
17 const st = await T("chat.state");
18 check(st.available && st.provider === "openai" && st.endpoint === new URL(base).host && st.mode === "ask" && !st.running, "the chat is available with the model endpoint named (host only), and starts in ask mode", JSON.stringify({ ...st, transcript: undefined }));
19 check(!JSON.stringify(st).includes("e2e-fake-key"), "no key appears in the chat state");
20
21 // the model picker: only models of providers that are set up are offered, and nothing else can be picked
22 // (an Anthropic key in this machine's ~/.dbbasic/env adds Anthropic's models; CI has none)
23 const host = new URL(base).host;
24 check(st.models.some((m) => m.id === "openai/fake-model" && m.label === `fake-model (${host})`) && st.models.every((m) => /^(anthropic|openai)\//.test(m.id) && (m.id.startsWith("anthropic/") || m.id === "openai/fake-model")) && st.modelId === "openai/fake-model", "the assistant offers only the models it is set up for, the stand-in one named with its machine", JSON.stringify(st.models));
25 const badPick = await T("chat.model", { model: "openai/gpt-not-offered" });
26 const goodPick = await T("chat.model", { model: "openai/fake-model" });
27 check(badPick.ok === false && goodPick.ok === true && goodPick.model === "fake-model", "a model that is not on offer cannot be picked", JSON.stringify({ badPick, goodPick }));
28
29 await T("page.goto", { url: base + "/forms" });
30
31 // reading
32 const r1 = await T("chat.send", { text: "read this page" });
33 check(r1.ok && r1.ended === "answered" && /^The page says: .*Sign up/.test(r1.reply), "the assistant reads the page with a tool and answers from it", r1.reply);
34 check(r1.events.some((e) => e.kind === "tool-start" && e.name === "read_page") && !r1.events.some((e) => e.kind === "confirm"), "reading is shown as a step and needs no approval");
35
36 const log = await (await fetch(base + "/v1/_log")).json();
37 const lastReq = JSON.stringify(log.at(-1));
38 const tm = log.at(-1).messages.at(-1);
39 const word = /<untrusted-([0-9a-f]{12})>/.exec(log.at(-1).messages[0].content)?.[1];
40 check(tm.role === "tool" && /^\[Content from a web page/.test(tm.content) && !!word && tm.content.includes(`<untrusted-${word}>`) && tm.content.endsWith(`</untrusted-${word}>`), "what a tool returned goes to the model with a line saying it is not from the person, between tags that carry this conversation's random word (the same word as in the system prompt)", word);
41 check(/UNTRUSTED/.test(log.at(-1).messages[0].content) && log.at(-1).tools.some((t) => t.function.name === "type_text"), "the model is told the data is untrusted and is given the tools");
42 check(!/hunter2|4111111111111111|123456|csrf-secret-value/.test(JSON.stringify(log)), "none of the secret values on the page ever reached the model");
43
44 // filling in ask mode: each action waits for a yes
45 let since = (await ev(0)).next;
46 const run = T("chat.send", { text: "fill the form" });
47 const c1 = await waitEvent(since, (e) => e.kind === "confirm", "the first approval");
48 check(c1.name === "list_fields" ? false : /Type “Grace” into “First name”/.test(c1.summary) && c1.danger === false, "the first action is shown in words and is not marked dangerous", c1.summary);
49 check((await value("first")) === "Ada", "nothing has changed on the page before the person says yes");
50 await T("chat.confirm", { id: c1.id, ok: true });
51 const c2 = await waitEvent(c1.seq, (e) => e.kind === "confirm", "the second approval");
52 check(/Choose “United States” in “Country”/.test(c2.summary), "the next action waits for its own yes", c2.summary);
53 await T("chat.confirm", { id: c2.id, ok: true });
54 const r2 = await run;
55 check(r2.ended === "answered" && (await value("first")) === "Grace" && (await value("country")) === "us", "after the yeses the name and the dropdown are filled in", JSON.stringify({ first: await value("first"), country: await value("country") }));
56 check((await T("page.eval", { js: "window.submitted === true" })).value === false, "filling in did not submit the form");
57
58 // a password field is refused outright, with no question asked
59 since = (await ev(0)).next;
60 const r3 = await T("chat.send", { text: "password please" });
61 check(r3.reply === "Result: refused" && !r3.events.some((e) => e.kind === "confirm") && r3.events.some((e) => e.kind === "tool-denied"), "typing into a password field is refused without asking", r3.reply);
62 check((await T("page.eval", { js: "document.querySelector('[name=pw]').value" })).value === "hunter2-secret", "the password field is unchanged");
63
64 // submitting is a dangerous click: asked about, and a no really means no
65 since = (await ev(0)).next;
66 const run4 = T("chat.send", { text: "submit it" });
67 const c4 = await waitEvent(since, (e) => e.kind === "confirm", "the submit approval");
68 check(c4.danger === true && /submits the form/.test(c4.summary), "clicking a submit button is marked dangerous and says what it does", c4.summary);
69 await T("chat.confirm", { id: c4.id, ok: false });
70 const r4 = await run4;
71 check(r4.reply === "Result: declined" && (await T("page.eval", { js: "window.submitted === true" })).value === false, "a no stops the click, and the assistant is told not to retry");
72 since = (await ev(0)).next;
73 const run4b = T("chat.send", { text: "submit it" });
74 const c4b = await waitEvent(since, (e) => e.kind === "confirm", "the second submit approval");
75 await T("chat.confirm", { id: c4b.id, ok: true });
76 await run4b;
77 check((await T("page.eval", { js: "window.submitted === true" })).value === true, "a yes lets the click through");
78
79 // auto mode: ordinary steps run unasked, dangerous ones still ask
80 await T("page.goto", { url: base + "/forms" });
81 await T("chat.mode", { mode: "auto" });
82 const r5 = await T("chat.send", { text: "fill the form" });
83 check(r5.ended === "answered" && !r5.events.some((e) => e.kind === "confirm") && (await value("first")) === "Grace", "in auto mode filling in runs without asking", r5.reply);
84 since = (await ev(0)).next;
85 const run6 = T("chat.send", { text: "submit it" });
86 const c6 = await waitEvent(since, (e) => e.kind === "confirm", "the submit approval in auto mode");
87 check(c6.danger === true, "in auto mode a submit is still asked about");
88 await T("chat.confirm", { id: c6.id, ok: false });
89 await run6;
90 since = (await ev(0)).next;
91 const urlBefore = (await T("tabs.list")).find((t) => t.active).url;
92 const run7 = T("chat.send", { text: "leave this site" });
93 const c7 = await waitEvent(since, (e) => e.kind === "confirm", "the approval to open another site");
94 check(c7.danger === true && /localhost/.test(c7.summary), "in auto mode going to a site that is not open is still asked about", c7.summary);
95 await T("chat.confirm", { id: c7.id, ok: false });
96 const r7 = await run7;
97 check(r7.reply === "Result: declined" && (await T("tabs.list")).find((t) => t.active).url === urlBefore, "and a no leaves the tab where it was");
98 await T("chat.mode", { mode: "ask" });
99
100 // never silent, and the person can clear it
101 const evs = await T("x402.events", { since: 0 });
102 check(evs.events.some((e) => e.kind === "ai-chat" && e.modelCalls >= 1 && e.provider === "openai" && e.where === new URL(base).host), "every finished run is announced in the event log with the model and how much it used");
103 await T("chat.clear");
104 const cleared = await T("chat.state");
105 check(cleared.transcript.length === 1 && cleared.transcript[0].kind === "cleared", "clearing empties the conversation");
106 const afterClear = await T("chat.send", { text: "hello" });
107 const lastLog = (await (await fetch(base + "/v1/_log")).json()).at(-1);
108 check(afterClear.ok && lastLog.messages.filter((m) => m.role === "user").length === 1, "after clearing, the model sees a fresh conversation");
109
110
111 // the panel (the chrome UI): open it, type, approve with a click, see the answer
112 const c = await ctx.chrome();
113 const q = (js) => c.ev(`(() => { ${js} })()`);
114 await T("chat.clear");
115 await T("page.goto", { url: base + "/forms" });
116 await c.ev(`setChat(false)`); // earlier questions from the scripts opened it by themselves
117 await c.ev(`toggleChat()`);
118 await sleep(300);
119 check(await q(`return document.getElementById('chat').classList.contains('open') && (/@/.test(document.getElementById('chatSub').textContent) || (!document.getElementById('chatModel').hidden && document.getElementById('chatModel').selectedOptions[0].textContent.includes('${new URL(base).host}'))) && /go to/.test(document.getElementById('chatPriv').textContent)`), "the panel opens and names the model and where your words go", await q(`return JSON.stringify([document.getElementById('chat').className, document.getElementById('chatSub').textContent, document.getElementById('chatPriv').textContent])`));
120 const fits = await q(`const p = document.getElementById('chat').getBoundingClientRect(); return JSON.stringify(['chatModel', 'chatMode', 'chatClear', 'chatClose'].map((id) => { const r = document.getElementById(id).getBoundingClientRect(); return [id, Math.round(r.left), Math.round(r.right), Math.round(p.right)]; }))`);
121 check(JSON.parse(fits).every(([id, l, r, pr]) => (id === 'chatModel' && r === 0) || (r <= pr + 1 && r > l)), "the panel's header fits: the model picker, the mode, New and close are all inside it", fits);
122 check(await q(`return !!document.getElementById('chatEmpty')`), "an empty conversation shows a short hint");
123 await c.ev(`(() => { const i = document.getElementById('chatIn'); i.value = 'fill the form'; i.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true })); })()`);
124 const waitDom = async (js, what) => { for (let i = 0; i < 150; i++) { if (await q(js)) return true; await sleep(50); } throw new Error("timed out waiting for " + what); };
125 await waitDom(`return document.querySelectorAll('#chatLog .ask:not(.done)').length === 1`, "an approval card");
126 check(await q(`const a = document.querySelector('#chatLog .ask'); return /Type “Grace” into “First name”/.test(a.querySelector('.what').textContent) && !a.classList.contains('danger') && document.getElementById('chatSend').textContent === 'Stop'`), "typing a message shows the action as a card with Do it / Skip, and the button becomes Stop");
127 await q(`document.querySelector('#chatLog .ask:not(.done) .sbtn').click()`);
128 await waitDom(`return document.querySelectorAll('#chatLog .ask:not(.done)').length === 1 && /Choose/.test(document.querySelector('#chatLog .ask:not(.done) .what').textContent)`, "the second card");
129 await q(`document.querySelector('#chatLog .ask:not(.done) .sbtn.quiet').click()`); // Skip
130 await waitDom(`return document.getElementById('chatSend').textContent === 'Send' && [...document.querySelectorAll('#chatLog .m.a')].some((m) => /Filled in/.test(m.textContent))`, "the answer");
131 check(await q(`return [...document.querySelectorAll('#chatLog .ask')].map((a) => a.querySelector('.tag').textContent).join('|') === 'Approved|Skipped'`), "the cards end up marked Approved and Skipped");
132 check((await value("first")) === "Grace" && (await value("country")) === "ca", "a click on Do it ran the action; a click on Skip did not (the country is still Canada)");
133 check(await q(`return !document.getElementById('chatLog').innerHTML.includes('<script')`), "text from the model is shown as text, not as markup");
134 await c.ev(`toggleChat()`);
135 check(await q(`return !document.getElementById('chat').classList.contains('open')`), "the panel closes");
136
137 // a question from a script opens the panel by itself, so the person sees it
138 since = (await ev(0)).next;
139 const run8 = T("chat.send", { text: "submit it" });
140 await waitEvent(since, (e) => e.kind === "confirm", "the approval for the panel check");
141 await waitDom(`return document.getElementById('chat').classList.contains('open') && document.querySelectorAll('#chatLog .ask.danger:not(.done)').length === 1`, "the panel to open itself");
142 check(true, "a question that needs an answer opens the panel by itself, marked risky");
143 await q(`document.querySelector('#chatLog .ask.danger .sbtn.quiet').click()`);
144 await run8;
145
146 // off while the caller network is on
147 await T("net.provider", { enabled: true, userAgent: "E2E-Chat/1.0 (+https://example.invalid/about)" });
148 const off = await T("chat.send", { text: "read this page" });
149 const offState = await T("chat.state");
150 await T("net.provider", { enabled: false });
151 check(off.ok === false && off.reason === "caller-network-on" && offState.available === false, "the chat is switched off while the caller network is on", JSON.stringify(off));
152}