"use client"
import * as React from "react"
import { useChat } from "@ai-sdk/react"
import {
isToolUIPart,
lastAssistantMessageIsCompleteWithApprovalResponses,
type UIDataTypes,
type UIMessage,
} from "ai"
import { createChat } from "@/lib/ai"
import {
Example,
ExampleWrapper,
} from "@/registry/bases/base/components/example"
import { Badge } from "@/registry/bases/base/ui/badge"
import { Bubble, BubbleContent } from "@/registry/bases/base/ui/bubble"
import { Button } from "@/registry/bases/base/ui/button"
import {
CodeBlock,
CodeBlockContent,
CodeBlockHeader,
CodeBlockTitle,
} from "@/registry/bases/base/ui/code-block"
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/registry/bases/base/ui/dialog"
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemMedia,
ItemTitle,
} from "@/registry/bases/base/ui/item"
import { Message, MessageContent } from "@/registry/bases/base/ui/message"
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/registry/bases/base/ui/sheet"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/registry/bases/base/ui/trace"
import { IconPlaceholder } from "@/app/(create)/components/icon-placeholder"
const REASONING = `The user wants to know why the checkout deploy is slow, so I should start from the build log instead of guessing.
The install and compile steps finish in under a minute. Image optimization takes almost four, and it runs on every deploy because the cache key includes the commit hash.
Keying the cache on the lockfile and the image directory should let most deploys skip that step entirely.`
export default function TraceExample() {
return (
)
}
type ChatbotMessage = UIMessage<
unknown,
UIDataTypes,
{
getWeather: {
input: { city: string }
output: { location: string; temperature: number; condition: string }
}
searchWikipedia: {
input: { query: string }
output: { results: { title: string; url: string }[] }
}
fetchPage: {
input: { url: string }
output: { status: number; title: string; excerpt: string }
}
readFile: {
input: { path: string }
output: { path: string; content: string }
}
runCommand: {
input: { command: string }
output: { stdout: string }
}
}
>
type ChatbotPart = ChatbotMessage["parts"][number]
type ReasoningPart = Extract
type ToolPart = Extract
type ApprovalResponse = { id: string; approved: boolean }
const README = `# Acme Chat
A chat app built with Next.js and the AI SDK.
## Getting started
pnpm install
pnpm dev
## Chat route
app/api/chat/route.ts streams responses through the AI Gateway.
Set AI_GATEWAY_API_KEY in .env.local.`
const chatbot = createChat()
.user("What's the weather in Lisbon and Tokyo?")
.sleep(800)
.assistant(({ writer }) => {
writer.reasoning(
"The user wants the weather in two cities. I'll look both up at once, then compare them in a sentence."
)
writer
.tool("getWeather", { input: { city: "Lisbon" } })
.sleep(1200)
.output({
location: "Lisbon, Portugal",
temperature: 18,
condition: "Mainly clear",
})
writer
.tool("getWeather", { input: { city: "Tokyo" } })
.sleep(600)
.output({
location: "Tokyo, Japan",
temperature: 26,
condition: "Clear sky",
})
writer.text(
"Lisbon is 18°C and mainly clear. Tokyo is warmer at 26°C under a clear sky."
)
})
.user("Search Wikipedia for the Voyager probes and summarise with sources.")
.sleep(800)
.assistant(({ writer }) => {
writer.reasoning(
"Start with a Wikipedia search. The Voyager 1 article should say where the probe is now."
)
writer
.tool("searchWikipedia", { input: { query: "Voyager probes" } })
.sleep(1000)
.output({
results: [
{
title: "Voyager program",
url: "https://en.wikipedia.org/wiki/Voyager_program",
},
{
title: "Voyager 1",
url: "https://en.wikipedia.org/wiki/Voyager_1",
},
{
title: "Voyager 2",
url: "https://en.wikipedia.org/wiki/Voyager_2",
},
],
})
writer
.tool("fetchPage", {
input: { url: "https://en.wikipedia.org/wiki/Voyager_1" },
})
.sleep(900)
.output({
status: 200,
title: "Voyager 1 - Wikipedia",
excerpt:
"Voyager 1 is a space probe launched by NASA on September 5, 1977. It is the most distant human-made object from Earth.",
})
writer.text(
"NASA launched both Voyager probes in 1977 to study the outer planets. Both have since reached interstellar space, and Voyager 1 is the most distant human-made object from Earth."
)
writer.sourceUrl({
url: "https://en.wikipedia.org/wiki/Voyager_program",
title: "Voyager program",
})
writer.sourceUrl({
url: "https://en.wikipedia.org/wiki/Voyager_1",
title: "Voyager 1",
})
})
.user("Read the README and the changelog, then check the Node version.")
.sleep(800)
.assistant(({ writer }) => {
writer.reasoning(
"Read both files first. Running a command needs the user's approval."
)
writer
.tool("readFile", { input: { path: "README.md" } })
.sleep(700)
.output({ path: "README.md", content: README })
writer
.tool("readFile", { input: { path: "CHANGELOG.md" } })
.sleep(500)
.error("ENOENT: no such file or directory, open 'CHANGELOG.md'")
writer.tool("runCommand", {
input: { command: "node --version" },
needsApproval: true,
output: { stdout: "v24.18.0" },
})
})
.assistant(({ writer, toolCall }) => {
writer.text(
toolCall?.approved
? "The README covers setup and the chat route. There is no changelog. The project runs on Node v24.18.0."
: "The README covers setup and the chat route. There is no changelog. I didn't run the command."
)
})
const chatbotTransport = chatbot.transport({ delayMs: 20 })
function TraceChat() {
const {
messages,
sendMessage,
setMessages,
status,
addToolApprovalResponse,
} = useChat({
messages: chatbot.get(0),
transport: chatbotTransport,
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
})
const nextMessage = chatbot.next(messages)
const isBusy = status === "submitted" || status === "streaming"
const lastMessage = messages.at(-1)
function handleClick() {
if (nextMessage) {
void sendMessage(nextMessage)
return
}
setMessages(chatbot.get(0))
}
return (
)
}
function TraceChainOfThought() {
return (
Looking into the slow deploySearched for the checkout build logvercel.comgithub.comnextjs.orgRead the last twelve deploys
Image optimization takes almost four minutes on every deploy.
The other steps finish in under a minute.
Searching for the cache key
cache key image optimization
)
}
function TraceTask() {
return (
Found the project filesSearched for app/page.tsxRead app/page.tsxScanned 52 filesRead app/layout.tsx
)
}
const SOURCES = [
{
title: "Caching build outputs | example.com",
href: "https://example.com/docs/caching",
},
{
title: "Image optimization reference | example.com",
href: "https://example.com/docs/images",
},
{
title: "Deploy hooks and cache keys | example.com",
href: "https://example.com/docs/deploy-hooks",
},
]
function TraceSources() {
return (
Used {SOURCES.length} sources
{SOURCES.map((source) => (
{source.title}
(opens in a new tab)
))}
)
}
function TraceTurn() {
return (
Worked for 2m 4s, 1 command failedThought for 12 seconds
{REASONING}
Ran 3 commands, 1 failedRan node --version
v24.18.0
Ran mkdir -p distFailed to run pnpm --version
command not found: pnpm
Node is installed and pnpm is not. Reading the docs config.
Read apps/v4/lib/docs.ts
)
}
function ChevronRightGlyph() {
return (
)
}
function ArrowUpRightGlyph() {
return (
)
}
function BrainGlyph() {
return (
)
}
function SearchGlyph() {
return (
)
}
function FileGlyph() {
return (
)
}
function FileSearchGlyph() {
return (
)
}
function TerminalGlyph() {
return (
)
}
function SunGlyph() {
return (
)
}
function BookGlyph() {
return (
)
}
function LinkGlyph() {
return (
)
}
function GlobeGlyph() {
return (
)
}