Read the cart from each answer
PassonResult to buy.turn. It gets the whole answer once for each call, after the call’s links went out and before the model reads it. You wrap no tool and parse no JSON.
chat.ts
result.cart is the newest cart, and result.carts lists every cart on the table, oldest first. A shopper buying from more than one store has one cart for each store, and each cart is its own order that the shopper confirms on its own. Show every cart in carts, so the shopper hears about each one.
To read the carts after the turn ends, use turn.results. It holds the whole answer of each call the turn made, in order:
chat.ts
onResult passed to buyTool hears every call the tool makes. Pass it to buy.turn to hear only the answers of one message, so one message’s cart never lands beside another message’s reply. A refusal the tool turns into an answer, such as the Vault link for a shopper with no card, reaches onResult too. A call that throws calls nothing, and an onResult that throws costs only that call to it.
Render each field
Every amount is in the minor unit of the cart’scurrency, the store’s ISO 4217 code, so 1331 is \$13.31 in USD and ¥1,331 in JPY.
A short formatter turns minor units into text for any currency:
money.ts
Show an estimate as an estimate
WhentotalIsEstimate is true, the total covers the items and the known fees only. Show it as an estimate, such as “about $18.41 before tax and delivery”. DoorDash prices a cart once the shopper’s own account holds it, so its first total is an estimate.
Show the ceiling beside the total
A store that adds shipping and tax only as it places the order asks the shopper to approve more than the total. ShowapprovedCeilingCents whenever the cart carries one. A shopper who sees only the total approves less than the card can be held for.
Show a hold line for a booking
A flight or a hotel places a hold on the shopper’s card, and the shopper sees it on their statement. Readhold.reason: show a line such as “Card held for $353.01” when it is flight or hotel. A checkout_estimate is the room a store needs for the tax and shipping it adds at checkout, and the card releases what the store does not use, so show it beside the total and leave the hold line out.
Take discounts off the total
Each entry indiscounts has a label, a negative amountCents, and a kind. Show it as money off, such as “DashPass discount −$3.00”. The total already includes it, so never subtract it again. The same line also appears in merchantFeeLines, so a panel that shows discounts on their own leaves the negative fee lines out. A coupon names the code the store accepted in code, and a discount has none.
Mark the products that are in the cart
A line’sref is the id of the product card it is. A product in result.products whose ref matches is in the cart, so your product cards can show an In cart badge.
Show a cart the store is still building
Some stores build the cart in their own checkout, which takes about 30 seconds. The answer then hascart_building set, with the store’s name and when it started, and carries no cart yet. Show the shopper that the total is on its way, and do not offer a confirm.
A cart already on the table whose store prices it again reads pricing: true, and several carts can read it at once. Show “Pricing” in place of that cart’s total until an answer brings it back as false.
Agentcard sends the cart in a purchase.message when the store’s checkout finishes. Post text as it is.
The message carries text and facts, not the cart, so your panel keeps reading “Pricing” until it reads the cart again. When a cart_ready or cart_failed message arrives, read the shopper’s conversation, with the conversation_id the answer that started the cart named, and show its carts. A cart reads pricing: true there only while its store still prices it:
refresh-cart.ts
The model’s description tells it to confirm a cart that is still building only after the shopper says yes to the total in the message Agentcard sends next. A shopper message sent while the cart builds waits for the cart, and the shopper reads a line saying the store is still building the cart.
A cart that sat without being sent for approval produces a
cart_idle message. Its facts.nudgeNumber counts the nudges, 1 for the first and 2 for the second.
Check it in the sandbox
- Send “two packs of AA batteries from Amazon” as a test shopper, and tap a product card.
- Check that your panel shows the line with its name, quantity, and price, the fees, and the total, and that the numbers match
result.cart. - Send “make it two”. Check that the panel updates and the cart’s
hashchanges. - Ask for an item at a second store. Check that the panel shows two carts.
- Send “start over”. Check that the panel clears.