The Vercel AI SDK · Chapter 11 of 21
Asking before acting
By the end of this chapter Mali asks before it changes anything: when the model wants to add to a customer's cart, the route stops and the page shows the question with Approve and Deny. The cart changes only after a yes, and each chat has its own cart. You will add approval to the route, build the question's component, and wire the answer back into the chat. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The chat UI
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.
Why you need this
Since chapter 10, a plant card's Add button sends "Add 1 Calathea (P07) to my cart", and Mali adds it at once. That is fine when the customer pressed the button. But the model can call addToCart from any message: "what would 2 ZZ plants cost?", a misread "two", a follow-up about something else. Deepa has handled the calls that follow: "I never ordered that." A model that reads tools from text will sometimes pick one nobody meant, and for tools that only look (stock, delivery, orders) that is harmless. For a tool that changes something (a cart, an order, a refund), the customer should say yes first.
There is a second problem in the route. Chapter 9's route makes maliTools() with a new Cart on every request. A chat is many requests, so the cart forgets what it held after every message. The cart has to live as long as the chat.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
server.ts: ask before adding to the cart
Chapter 9's route in server.ts, with two changes. One cart per chat: answer 400 when body.id is not a non-empty string; keep a Map from chat id to Cart (from ../../shared/haribhari.ts) at the top of the file, and pass the chat's cart (new on its first message) to maliTools(cart). Ask first: toolApproval: { addToCart: ({ productId, qty }) => ({ type: 'user-approval', reason }) } in streamText, with the reason Add QTY x NAME to your cart? (the name from product(productId)).
The Check asks to add plants: a tool-approval-request with that reason and no tool output or text; the round trip approved (the cart total) and denied ("No problem..."); a second add in the same chat adds to the same cart; a new chat starts with an empty one; and 400 with no id.
- Task 2
Approval.tsx: ask the customer
Export default function Approval({ part, onRespond }) from Approval.tsx (part: ToolUIPart<InferUITools<MaliTools>>, onRespond: (approved: boolean) => void). For state 'approval-requested': a <div role="group" aria-label="Approval"> with part.approval.requestReason (or Mali would like to go ahead. Is that OK? when there is none), a button Approve calling onRespond(true) and a button Deny calling onRespond(false). For 'approval-responded': Approved. Mali is on it... or Denied. (part.approval.approved). For 'output-denied': Not added to your cart. Anything else: nothing.
- Task 3
Chat.tsx: the chat asks, then carries on
Change the given chapter 10 chat in Chat.tsx: take addToolApprovalResponse from useChat, and add sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses (from 'ai') to its options. In show(), before the other tool parts, draw a 'tool-addToCart' part whose state is in ASKING with <Approval part={part} onRespond={...} />; onRespond calls addToolApprovalResponse({ id: part.approval.id, approved }) when the part is 'approval-requested'.
The Check uses its own approving route: the question shows and nothing is added before an answer; Approve posts once by itself, adds once and shows the total; Deny posts once, adds nothing, and shows "No problem..." and Not added to your cart.
What you will learn
- Tool approval: toolApproval on streamText, and what the stream sends instead of running the tool.
- A reason for the question, computed on the server.
- The approval round trip: approval-requested, addToolApprovalResponse, sendAutomaticallyWhen, then the tool runs (or not).
- Denied calls: output-denied, and what the model is told.
- State per chat: one Cart per chat id, kept on the server.
More in The chat UI
Build "Asking before acting" yourself
HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.