The Vercel AI SDK · Chapter 21 of 21

Capstone: Ask Mali, end to end

By the end of this chapter Ask Mali is whole: one chat route that does everything this lab taught, one chat page that shows all of it, and a scorecard that says whether it is ready. You will put the pieces together yourself, run Deepa's twelve test conversations through them, and answer the questions a team asks before a launch. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for customers
  • 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

It is Monday of Diwali week. Ananya wants Ask Mali on the website by Wednesday. Over twenty chapters you built each piece on its own: tools, streaming, cards, approval, photos, guardrails, a fallback, costs, tests. Each worked in its chapter. A customer does not meet them one at a time. One message can need a search, a card, a guard and a cost, all in the same stream, and a mistake in how the pieces fit is a mistake the customer sees.

Deepa has the last word: twelve conversations, the ones her team answers most often, and Mali goes live when all twelve are right. Vikram has his own line: no chat above ₹1.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    server.ts: Ask Mali, the whole route

    Export SORRY, chatRoute(model) and POST = chatRoute(withFallback(lab('lab-chat'), lab('lab-chat-mini'))). chatRoute wraps the model with [redactPhones, couponGuard] (./guards.ts) and returns POST(req), which: answers 400 to a body that is not JSON, to messages safeValidateUIMessages refuses, and to a file part that is not a PNG, JPEG or WebP photo under 2 MB; streams with MALI_INSTRUCTIONS, the converted messages, maliTools(cart) with one Cart per chat id, toolApproval: { addToCart: 'user-approval' }, isStepCount(5), abortSignal: req.signal; and answers through toUIMessageStream with originalMessages, onError: () => SORRY, messageMetadata { model: 'lab-chat', tokens, rupees } at the finish part, and onEnd saving the chat with saveChat(body.id, messages).

  2. Task 2

    Chat.tsx: Ask Mali, the whole page

    Starting from the given chat, draw every part: file parts as an <img>; searchPlants' output as a PlantCard per plant (./Cards.tsx) whose onAdd sends Add 1 NAME to my cart; checkDelivery's as a DeliveryCard; getOrder's as an OrderCard; a waiting line while a tool runs; an addToCart in approval-requested as Approve and Deny buttons calling addToolApprovalResponse (with sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses); the cost under an answer as ₹ and metadata.rupees with 4 decimals; and a photo picker whose files go with sendMessage({ text, files }).

    The Check runs Deepa's twelve conversations through your page and your route.

  3. Task 3 · interview question

    Interview: scorecard.ts, Deepa's twelve

    Run CONVERSATIONS (./deepa.ts) through POST (./server.ts): for each, a chat id new on every run; for each turn, post { id, messages: [...loadChat(id), message] } with the photo as a file part on the first turn; read the answer with readAnswer. Score the last answer (says matches, never does not, waits is approval-requested) and print pass NAME or FAIL NAME: what was wrong, then N of 12 right, ₹X a chat (every answer's metadata.rupees added up, over 12, 4 decimals).

    Deepa will ask: twelve pass. What would you add to these twelve before Mali goes live, and how often would you run them?

What you will learn

  • Composition: how the pieces stack in one route, and in which order.
  • The whole page: text, cards, approval, photos, errors and cost in one component.
  • Acceptance tests: Deepa's twelve conversations as a scorecard.
  • Launch questions: what to check before customers arrive, and after.

More in Ready for customers

Build "Capstone: Ask Mali, end to end" 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.