|
| 1 | +// Dev only: a Clerk that asks the owner to sign in for it, in the fake |
| 2 | +// preview (spec 21.10). The sign-in page is drawn on a canvas and answers |
| 3 | +// the owner's clicks and keys, so taking the browser can be tried end to end. |
| 4 | + |
| 5 | +import type { FakeBotloft } from "../lib/fake"; |
| 6 | +import type { BrowserInput, CrewId } from "../lib/protocol.gen"; |
| 7 | + |
| 8 | +const W = 1280; |
| 9 | +const H = 800; |
| 10 | +const FIELDS = { user: 300, password: 390 } as const; |
| 11 | +type Field = keyof typeof FIELDS; |
| 12 | + |
| 13 | +interface Form { |
| 14 | + user: string; |
| 15 | + password: string; |
| 16 | + field: Field | null; |
| 17 | + signedIn: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +function draw(form: Form): string { |
| 21 | + const canvas = document.createElement("canvas"); |
| 22 | + canvas.width = W; |
| 23 | + canvas.height = H; |
| 24 | + const g = canvas.getContext("2d"); |
| 25 | + if (!g) { |
| 26 | + return ""; |
| 27 | + } |
| 28 | + g.fillStyle = "#f7f4ee"; |
| 29 | + g.fillRect(0, 0, W, H); |
| 30 | + g.fillStyle = "#3b2a1a"; |
| 31 | + g.font = "600 36px Georgia, serif"; |
| 32 | + if (form.signedIn) { |
| 33 | + g.fillText(`Welcome back, ${form.user || "baker"}`, 440, 220); |
| 34 | + g.font = "20px system-ui, sans-serif"; |
| 35 | + const orders = ["#1042 · 25 kg flour", "#1043 · 10 kg rye", "#1044 · 5 kg semolina"]; |
| 36 | + for (const [row, line] of orders.entries()) { |
| 37 | + g.fillText(`Order ${line}`, 440, 300 + row * 48); |
| 38 | + } |
| 39 | + return canvas.toDataURL("image/jpeg", 0.7).split(",")[1] ?? ""; |
| 40 | + } |
| 41 | + g.fillText("Flour Co. · Sign in", 440, 220); |
| 42 | + for (const name of ["user", "password"] as const) { |
| 43 | + const y = FIELDS[name]; |
| 44 | + g.fillStyle = "#ffffff"; |
| 45 | + g.strokeStyle = form.field === name ? "#c2410c" : "#b5a896"; |
| 46 | + g.lineWidth = form.field === name ? 3 : 2; |
| 47 | + g.beginPath(); |
| 48 | + g.roundRect(440, y, 400, 52, 12); |
| 49 | + g.fill(); |
| 50 | + g.stroke(); |
| 51 | + const value = name === "password" ? "•".repeat(form.password.length) : form.user; |
| 52 | + g.fillStyle = value ? "#1f1409" : "#9a8c7a"; |
| 53 | + g.font = "20px system-ui, sans-serif"; |
| 54 | + g.fillText(value || (name === "user" ? "E-mail" : "Password"), 460, y + 33); |
| 55 | + } |
| 56 | + g.fillStyle = "#c2410c"; |
| 57 | + g.beginPath(); |
| 58 | + g.roundRect(440, 480, 400, 52, 26); |
| 59 | + g.fill(); |
| 60 | + g.fillStyle = "#ffffff"; |
| 61 | + g.font = "600 20px system-ui, sans-serif"; |
| 62 | + g.fillText("Sign in", 606, 513); |
| 63 | + return canvas.toDataURL("image/jpeg", 0.7).split(",")[1] ?? ""; |
| 64 | +} |
| 65 | + |
| 66 | +/** The page's answer to one of the owner's events; true once they sign in. */ |
| 67 | +function handle(form: Form, input: BrowserInput): boolean { |
| 68 | + if (input.kind === "mouse" && input.action === "down") { |
| 69 | + const inside = (top: number) => |
| 70 | + input.x >= 440 && input.x <= 840 && input.y >= top && input.y <= top + 52; |
| 71 | + form.field = inside(FIELDS.user) ? "user" : inside(FIELDS.password) ? "password" : null; |
| 72 | + return inside(480); |
| 73 | + } |
| 74 | + const field = form.field; |
| 75 | + if (input.kind === "text" && field) { |
| 76 | + form[field] += input.text; |
| 77 | + } else if (input.kind === "key" && input.key === "Enter") { |
| 78 | + return true; |
| 79 | + } else if (input.kind === "key" && input.key === "Tab") { |
| 80 | + form.field = form.field === "user" ? "password" : "user"; |
| 81 | + } else if (input.kind === "key" && input.key === "Backspace" && field) { |
| 82 | + form[field] = form[field].slice(0, -1); |
| 83 | + } else if (input.kind === "key" && [...input.key].length === 1 && field && input.modifiers < 2) { |
| 84 | + form[field] += input.key; |
| 85 | + } |
| 86 | + return false; |
| 87 | +} |
| 88 | + |
| 89 | +export function seedHands(fake: FakeBotloft, crewId: CrewId): void { |
| 90 | + const clerk = fake.addBot(crewId, "Clerk", "Places the bakery's orders"); |
| 91 | + fake.setBotState(clerk.id, "needs_approval", 2); |
| 92 | + const form: Form = { user: "", password: "", field: null, signedIn: false }; |
| 93 | + const login = "https://flour.example/login"; |
| 94 | + fake.browser.open(clerk.id, login, "Sign in · Flour Co."); |
| 95 | + fake.browser.frame(clerk.id, draw(form)); |
| 96 | + fake.chat.tool(clerk.id, "mcp__botloft__browser_open", { summary: login, status: "done" }); |
| 97 | + const task = "Sign in to Flour Co. with the bakery's account"; |
| 98 | + fake.chat.tool(clerk.id, "mcp__botloft__browser_ask_owner", { summary: task, status: "running" }); |
| 99 | + fake.browser.ask(clerk.id, task); |
| 100 | + fake.browser.onInput(clerk.id, (input) => { |
| 101 | + if (form.signedIn) { |
| 102 | + return; |
| 103 | + } |
| 104 | + if (handle(form, input)) { |
| 105 | + form.signedIn = true; |
| 106 | + fake.browser.open(clerk.id, "https://flour.example/orders", "Orders · Flour Co."); |
| 107 | + } |
| 108 | + fake.browser.frame(clerk.id, draw(form)); |
| 109 | + }); |
| 110 | +} |
0 commit comments