|
| 1 | +import { Stagehand } from "@/dist"; |
| 2 | +import StagehandConfig from "@/stagehand.config"; |
| 3 | +import { CerebrasClient } from "../lib/llm/CerebrasClient"; |
| 4 | +import chalk from "chalk"; |
| 5 | + |
| 6 | +async function formFillingSensible() { |
| 7 | + const stagehand = new Stagehand({ |
| 8 | + ...StagehandConfig, |
| 9 | + env: "LOCAL", |
| 10 | + llmClient: new CerebrasClient({ |
| 11 | + modelName: "cerebras-llama-3.3-70b", |
| 12 | + clientOptions: { |
| 13 | + apiKey: process.env.CEREBRAS_API_KEY, |
| 14 | + }, |
| 15 | + logger: console.log, |
| 16 | + }), |
| 17 | + }); |
| 18 | + await stagehand.init(); |
| 19 | + |
| 20 | + // Block manifest worker to prevent PWA installation popup. |
| 21 | + // This is necessary because the website prompts the user to install the PWA and prevents form filling. |
| 22 | + await stagehand.page.route("**/manifest.json", (route) => route.abort()); |
| 23 | + |
| 24 | + // Go to the website and wait for it to load |
| 25 | + await stagehand.page.goto("https://file.1040.com/estimate/", { |
| 26 | + waitUntil: "networkidle", |
| 27 | + timeout: 30000, |
| 28 | + }); |
| 29 | + |
| 30 | + // Observe the form fields with suggested actions |
| 31 | + const observed = await stagehand.page.observe({ |
| 32 | + instruction: |
| 33 | + "fill all the form fields in the page with mock data. In the description inlcude the field name", |
| 34 | + returnAction: true, |
| 35 | + }); |
| 36 | + |
| 37 | + // Uncomment the following snippet to see the stagehand candidate suggestions (initial) |
| 38 | + console.log( |
| 39 | + `${chalk.green("Observe:")} Form fields found:\n${observed |
| 40 | + .map((r) => `${chalk.yellow(r.description)} -> ${chalk.gray(r.selector)}`) |
| 41 | + .join("\n")}`, |
| 42 | + ); |
| 43 | + |
| 44 | + // Create a mapping of 1+ keywords in the form fields to standardize field names |
| 45 | + const mapping = (description: string): string | null => { |
| 46 | + const keywords: { [key: string]: string[] } = { |
| 47 | + age: ["old"], |
| 48 | + dependentsUnder17: ["under age 17", "child", "minor"], |
| 49 | + dependents17to23: ["17-23", "school", "student"], |
| 50 | + wages: ["wages", "W-2 Box 1"], |
| 51 | + federalTax: ["federal tax", "Box 2"], |
| 52 | + stateTax: ["state tax", "Box 17"], |
| 53 | + }; |
| 54 | + |
| 55 | + for (const [key, terms] of Object.entries(keywords)) { |
| 56 | + if (terms.some((term) => description.toLowerCase().includes(term))) { |
| 57 | + return key; |
| 58 | + } |
| 59 | + } |
| 60 | + return null; |
| 61 | + }; |
| 62 | + |
| 63 | + // Fill the form fields with sensible data. This data will only be used in your session and not be shared with LLM providers/external APIs. |
| 64 | + const userInputs: { [key: string]: string } = { |
| 65 | + age: "26", |
| 66 | + dependentsUnder17: "1", |
| 67 | + wages: "54321", |
| 68 | + federalTax: "8345", |
| 69 | + stateTax: "2222", |
| 70 | + }; |
| 71 | + |
| 72 | + const updatedFields = observed.map((candidate) => { |
| 73 | + const key = mapping(candidate.description); |
| 74 | + if (key && userInputs[key]) { |
| 75 | + candidate.arguments = [userInputs[key]]; |
| 76 | + } |
| 77 | + return candidate; |
| 78 | + }); |
| 79 | + // List of sensible-data candidates |
| 80 | + console.log( |
| 81 | + `\n${chalk.green("Sensible Data form inputs:")} Form fields to be filled:\n${updatedFields |
| 82 | + .map( |
| 83 | + (r) => |
| 84 | + `${chalk.yellow(r.description)} -> ${chalk.blue(r.arguments?.[0] || "no value")}`, |
| 85 | + ) |
| 86 | + .join("\n")}`, |
| 87 | + ); |
| 88 | + |
| 89 | + // Fill all the form fields with the sensible candidates |
| 90 | + for (const candidate of updatedFields) { |
| 91 | + await stagehand.page.act(candidate); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +(async () => { |
| 96 | + await formFillingSensible(); |
| 97 | +})(); |
0 commit comments