const agent = web.agent({
model,
approval: "ask",
});
const browser = await web.browser.create({
recording: true,
policy: { allowedDomains: ["vendor.example"] },
});
try {
const result = await agent.run({
browserId: browser.id,
startUrl: "https://vendor.example/dashboard",
goal: "Find the latest paid invoice and download the PDF.",
});
if (result.status !== "completed") {
throw new Error(result.error?.message ?? `Run ended with ${result.status}`);
}
const downloads = await browser.downloads.list();
const [invoice] = downloads.data;
if (!invoice) throw new Error("No invoice was downloaded.");
await browser.downloads.save(invoice.id, "./invoice.pdf");
} finally {
await browser.close();
}