import { z } from "zod";
import { client, agentTask, Sessions } from "anchorbrowser";
client.setConfig({ auth: () => process.env.ANCHORBROWSER_API_KEY });
const Order = z.object({
custname: z.string(),
custtel: z.string(),
custemail: z.string(),
size: z.string(),
topping: z.array(z.string()),
delivery: z.string(),
comments: z.string(),
});
const task = await agentTask(
`Fill in the order form with these values and submit it. Customer name: Ada Lovelace. Telephone: 555-0100. E-mail: ada@example.com. Pizza size: Medium. Toppings: Bacon and Extra Cheese. Preferred delivery time: 19:30. Delivery instructions: Ring the bell. After you submit, the page shows the submitted values as JSON. Return those values.`,
{
taskOptions: {
url: "https://httpbin.org/forms/post",
outputSchema: z.toJSONSchema(Order),
},
}
);
try {
if (task.status !== "success") throw new Error(`Task failed: ${task.result}`);
const order = Order.parse(JSON.parse(task.data.result));
console.log(order);
} finally {
await Sessions.deleteSession({ path: { session_id: task.sessionId } });
}