Part 1
The route handler on the AI SDK
Use streamText with the Day 7 tool registry; useChat consumes the UI message stream it produces. The Day 6 provider loop stays in the terminal.
Study first
- The AI SDK's chatbot guide:
useChat, UI messages and their parts, and the route handler shape. streamTextwithtoolsandstopWhen, andconvertToModelMessagesfrom UI messages.
Build
pnpm add ai @ai-sdk/react @ai-sdk/anthropic @ai-sdk/openai.src/lib/chat.ts:chatModel()returns the configured provider's default model from the Day 6 list, or the id inCHAT_MODEL, through that provider's AI SDK adapter;chatToolsmaps the registry's tools into the SDK'stool()shape, schema andexecutefrom the same definitions.src/app/api/chat/route.ts: validate the body, thensafeValidateUIMessagesagainst the tools; stream with the system prompt, the tools andstopWhen: isStepCount(8); answer withcreateUIMessageStreamResponse.- A unit test with the SDK's
MockLanguageModelV4for the stream's shape, plus a body without an id and a message without parts, both 400; and a live test that asks the real small model for a calculation and finds the calculator call in the stream.
Done when
curl -Nagainst/api/chatwith one user message prints a UI message stream, andpnpm test:liveshows the tool call.
Technical notes
- Validate the envelope with Zod and the messages with
safeValidateUIMessages, which knows the tool parts' shapes. A bad body is a 400 with the failure, not a stream that fails halfway. isStepCount(8)matches the 8 rounds of the Day 7 loop.- The model id comes from the provider list in
src/lib/providers.ts, so the web and the terminal agree on the default;CHAT_MODELoverrides it, which the live suite uses to run on the small model.

