import { z } from "zod";
import { client, agentTask, Sessions } from "anchorbrowser";
client.setConfig({ auth: () => process.env.ANCHORBROWSER_API_KEY });
const Cart = z.object({
product_title: z.string(),
format: z.string(),
unit_price: z.string(),
cart_item_count: z.number(),
cart_subtotal: z.string(),
});
const task = await agentTask(
`Find the book "Atomic Habits" by James Clear in Hardcover. Add one copy to the cart, then open the cart. Return the product title, the format, the unit price, the number of items in the cart and the cart subtotal. Stop at the cart. Do not start checkout, do not enter any personal details and do not enter any payment details.`,
{
taskOptions: {
url: "https://www.barnesandnoble.com/",
outputSchema: z.toJSONSchema(Cart),
},
}
);
try {
if (task.status !== "success") throw new Error(`Task failed: ${task.result}`);
const cart = Cart.parse(JSON.parse(task.data.result));
console.log(cart);
} finally {
await Sessions.deleteSession({ path: { session_id: task.sessionId } });
}