The Vercel AI SDK · Chapter 10 of 21
Generative UI
By the end of this chapter Mali's answers come with cards: a card for each plant a search found, with its price, a pet-safe badge, an out-of-stock label and an Add to cart button; a card for a delivery check; a card for an order. The cards are drawn from the tools' outputs, not from the model's words. You will build PlantCard and DeliveryCard, and show them in 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
Ask chapter 9's chat for pet-safe plants and Mali answers in a sentence:
I found 9, here are the top 3, all safe for pets: Areca Palm (₹799), Spider Plant (₹299) and Calathea (₹549).
Ananya wants more than a sentence. A customer choosing a plant wants to see all nine, compare prices, notice which are out of stock, and add one to the cart with a tap, not by typing "add one Calathea to my cart". The search already found all nine: they are in the tool's output, in the same message, as data. The page only has to draw them.
Deepa has a second reason. A price or a stock level in the model's words is the model's retelling of the data; a price on a card is the data itself. A card cannot misquote ₹549.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
PlantCard.tsx: a plant card
Export default function PlantCard({ plant, onAdd }) from PlantCard.tsx (plant: HariBhari's PlantCard type from ../../shared/haribhari.ts, onAdd: (plant) => void). An <article aria-label={plant.name}> showing the name, the price as ₹ with toLocaleString('en-IN') (₹1,299), a Pet-safe badge only when plant.petSafe, Out of stock only when the plant is not inStock, and a button Add to cart that calls onAdd(plant), disabled when out of stock.
The Check draws four plants from the catalogue, one of them out of stock and one over ₹999.
- Task 2
DeliveryCard.tsx: a delivery card
Export default function DeliveryCard({ delivery }) from DeliveryCard.tsx (delivery: Delivery | { error: string }). For { error } show the error's text. Not deliverable: an <article aria-label="Delivery"> saying We don't deliver to AREA (PINCODE) yet. Deliverable: an <article aria-label="Delivery"> with Delivery to AREA (PINCODE), then Free delivery when the fee is 0 or Delivery fee ₹FEE, then Next slot: and the first slot.
The Check draws three areas with different fees, one HariBhari does not reach, and an error.
- Task 3
Chat.tsx: cards in the chat
Chapter 9's typed chat in Chat.tsx (MaliMessage, useChat<MaliMessage>, the given Composer), drawing each part: a text part's text; a tool-searchPlants part in state output-available as a PlantCard per plant in part.output (key plant.id), whose onAdd sends Add 1 NAME (ID) to my cart with sendMessage; tool-checkDelivery output-available as <DeliveryCard delivery={part.output} />; tool-getOrder output-available as the given <OrderCard order={part.output} />; any other tool part as the given <ToolStatus part={part} />.
The Check asks for plants (one card per plant the tool found, not only the three the text names), presses Add to cart on a card, then asks about a delivery and an order in one chat.
What you will learn
- Generative UI: components chosen and filled by the model's tool calls.
- A card per tool: PlantCard, DeliveryCard, OrderCard, drawn from part.output.
- Typed outputs: why part.output is a list of plants once you check part.type and part.state.
- A card that acts: the Add to cart button sends a message.
- Cards and text together, in the order the parts came.
More in The chat UI
Build "Generative UI" 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.