- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Trace
- Typography
Run pnpm create astro@latest and follow the prompts. It scaffolds the project and installs the dependencies.
"use client"
import { useChat } from "@ai-sdk/react""use client"
import { useChat } from "@ai-sdk/react"
import type { UIMessage } from "ai"
import { ArrowUpRightIcon, GlobeIcon } from "lucide-react"
import { createChat, getMessageText } from "@/lib/ai"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { Message, MessageContent } from "@/components/ui/message"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace"
const chat = createChat()
.user("How do I start an Astro project with pnpm?")
.assistant(({ writer }) => {
writer.text(
"Run pnpm create astro@latest and follow the prompts. It scaffolds the project and installs the dependencies."
)
writer.sourceUrl({
url: "https://docs.astro.build/en/install-and-setup/",
title: "Install Astro | docs.astro.build",
})
writer.sourceUrl({
url: "https://www.npmjs.com/package/create-astro",
title: "create-astro | npmjs.com",
})
writer.sourceUrl({
url: "https://docs.astro.build/en/install-and-setup/",
title: "Install Astro | docs.astro.build",
})
writer.sourceUrl({
url: "https://pnpm.io/cli/create",
title: "pnpm create | pnpm.io",
})
})
const transcript = chat.get()
const [question] = chat.get(1)
const transport = chat.transport({ delayMs: 20 })
type SourcePart = Extract<UIMessage["parts"][number], { type: "source-url" }>
function Sources({ sources }: { sources: SourcePart[] }) {
return (
<Trace>
<TraceCollapsible defaultOpen>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<GlobeIcon />
</TraceIcon>
<TraceTitle>Used {sources.length} sources</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<Trace>
{sources.map((source) => (
<a
key={source.url}
href={source.url}
target="_blank"
rel="noreferrer"
>
<TraceLine>
<TraceTitle>
{source.title ?? new URL(source.url).hostname}
<span className="sr-only"> (opens in a new tab)</span>
</TraceTitle>
<TraceIcon>
<ArrowUpRightIcon />
</TraceIcon>
</TraceLine>
</a>
))}
</Trace>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
)
}
export function SourcesDemo() {
const { messages, sendMessage, setMessages, status } = useChat({
messages: transcript,
transport,
})
const busy = status === "submitted" || status === "streaming"
return (
<div className="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
{messages.map((message) => {
if (message.role === "user") {
return (
<Message key={message.id} align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>{getMessageText(message)}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
)
}
const sources = message.parts
.filter((part) => part.type === "source-url")
.filter(
(part, index, all) =>
all.findIndex((other) => other.url === part.url) === index
)
const text = getMessageText(message)
return (
<Message key={message.id}>
<MessageContent className="gap-4">
{text && <p>{text}</p>}
{sources.length > 0 && <Sources sources={sources} />}
</MessageContent>
</Message>
)
})}
{!busy && (
<Button
variant="outline"
size="sm"
className="mt-auto self-start"
onClick={() => {
setMessages([])
sendMessage(question)
}}
>
Replay
</Button>
)}
</div>
)
}
Sources is a line that lists the links the agent used for a reply. It says how many there are, like "Used 3 sources", and opens into a link for each one.
Sources is built with Trace with a TraceCollapsible for the list.
Installation#
Sources is built with the Trace component.
pnpm dlx shadcn@latest add trace
Install the required shadcn/ui dependencies:
pnpm dlx shadcn@latest add collapsible
Copy and paste the following code into your project.
import * as React from "react"
import { cn } from "cn"
import { ChevronRightIcon } from "lucide-react"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible"
function Trace({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="trace"
className={cn(
"flex w-full min-w-0 flex-col gap-2 *:has-[>[data-slot=trace-line]]:flex *:has-[>[data-slot=trace-line]]:w-fit *:has-[>[data-slot=trace-line]]:max-w-full *:has-[>[data-slot=trace-line]]:min-w-0 *:has-[>[data-slot=trace-line]]:rounded-lg *:has-[>[data-slot=trace-line]]:text-left *:has-[>[data-slot=trace-line]]:outline-none *:has-[>[data-slot=trace-line]]:focus-visible:ring-3 *:has-[>[data-slot=trace-line]]:focus-visible:ring-ring/30",
className
)}
{...props}
/>
)
}
function TraceLine({
className,
status,
...props
}: React.ComponentProps<"span"> & {
status?: "running" | "error" | "pending"
}) {
return (
<span
data-slot="trace-line"
data-status={status}
className={cn(
"flex w-fit max-w-full min-w-0 items-center gap-2 text-sm text-muted-foreground transition-colors in-[a,button]:cursor-pointer in-[a:hover,button:hover]:text-foreground data-[status=error]:text-destructive data-[status=error]:in-[a:hover,button:hover]:text-destructive data-[status=pending]:opacity-60 data-[status=running]:*:data-[slot=trace-title]:shimmer",
className
)}
{...props}
/>
)
}
function TraceIcon({ className, ...props }: React.ComponentProps<"span">) {
return (
<span
data-slot="trace-icon"
aria-hidden="true"
className={cn(
"flex size-4 shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [[data-slot=trace-title]~&]:-ml-1",
className
)}
{...props}
/>
)
}
function TraceTitle({ className, ...props }: React.ComponentProps<"span">) {
return (
<span
data-slot="trace-title"
className={cn("min-w-0 truncate motion-reduce:shimmer-none", className)}
{...props}
/>
)
}
function TraceDescription({
className,
...props
}: React.ComponentProps<"span">) {
return (
<span
data-slot="trace-description"
className={cn(
"shrink-0 text-muted-foreground/70 tabular-nums",
className
)}
{...props}
/>
)
}
function TraceCollapsible({
className,
...props
}: Omit<React.ComponentProps<typeof Collapsible>, "open"> &
(
| { open?: undefined }
| {
open: boolean
onOpenChange: NonNullable<
React.ComponentProps<typeof Collapsible>["onOpenChange"]
>
}
)) {
return (
<Collapsible
data-slot="trace-collapsible"
className={cn("flex w-full min-w-0 flex-col", className)}
{...props}
/>
)
}
function TraceCollapsibleTrigger({
className,
children,
...props
}: React.ComponentProps<typeof CollapsibleTrigger>) {
return (
<CollapsibleTrigger
data-slot="trace-collapsible-trigger"
className={cn(
"group/trace-collapsible-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center gap-1 rounded-lg text-left text-muted-foreground transition-colors outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/30 has-[[data-status=error]]:text-destructive",
className
)}
{...props}
>
{children}
<TraceIcon className="transition-transform group-aria-expanded/trace-collapsible-trigger:rotate-90">
<ChevronRightIcon />
</TraceIcon>
</CollapsibleTrigger>
)
}
function TraceCollapsibleContent({
className,
children,
...props
}: React.ComponentProps<typeof CollapsibleContent>) {
return (
<CollapsibleContent
data-slot="trace-collapsible-content"
className="h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] duration-200 data-ending-style:h-0 data-starting-style:h-0"
{...props}
>
<div
data-slot="trace-collapsible-content-inner"
className={cn(
"mt-2 mb-1 ml-[calc(--spacing(2)-1px)] min-w-0 border-l pr-1 pl-4 leading-relaxed wrap-break-word text-muted-foreground",
className
)}
>
{children}
</div>
</CollapsibleContent>
)
}
export {
Trace,
TraceLine,
TraceIcon,
TraceTitle,
TraceDescription,
TraceCollapsible,
TraceCollapsibleTrigger,
TraceCollapsibleContent,
}
Update the import paths to match your project setup.
Usage#
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace"<Trace>
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<GlobeIcon />
</TraceIcon>
<TraceTitle>Used {sources.length} sources</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<Trace>
{sources.map((source) => (
<a
key={source.url}
href={source.url}
target="_blank"
rel="noreferrer"
>
<TraceLine>
<TraceIcon>
<GlobeIcon />
</TraceIcon>
<TraceTitle>
{source.title ?? new URL(source.url).hostname}
<span className="sr-only"> (opens in a new tab)</span>
</TraceTitle>
<TraceIcon>
<ArrowUpRightIcon />
</TraceIcon>
</TraceLine>
</a>
))}
</Trace>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>AI SDK#
Collect the source-url parts of a message and render them after the text. The agent can cite the same page more than once, so dedupe them by url.
const sources = message.parts
.filter((part) => part.type === "source-url")
.filter(
(part, index, all) =>
all.findIndex((other) => other.url === part.url) === index
)title is optional on a source-url part. Fall back to the hostname of the URL.
Accessibility#
Each source is a plain link inside the list.
- Links open in a new tab. Add
sr-onlytext, like "(opens in a new tab)", so screen readers announce it. - The arrow icon is decorative. The title is the name of the link.
- The count in the trigger, like "Used 3 sources", tells the person what opens before they open it.
Migrating from AI SDK Elements#
If you added Sources from AI SDK Elements, your project has a components/ai-elements/sources.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Trace. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/sources.tsx from AI SDK Elements to the shadcn/ui Trace component.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install Trace: npx shadcn@latest add trace
2. Read https://ui.shadcn.com/docs/ai-elements/base/sources.md and build Sources the same way.
3. Rebuild each part:
- Sources renders a Trace with a TraceCollapsible.
- SourcesTrigger renders a TraceCollapsibleTrigger with a TraceLine, a TraceIcon and a TraceTitle that uses count, like "Used 3 sources".
- SourcesContent renders a TraceCollapsibleContent with a Trace for the links.
- Source renders a link that wraps a TraceLine, with a TraceIcon and a TraceTitle for its title. Add "(opens in a new tab)" in an sr-only span.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.API Reference#
See Trace.