Sources
The pages and documents the agent used, as a collapsed list under its answer.
Installation
pnpm dlx shadcn@latest add @silkware/ai-sourcesTo copy the pattern by hand, install the components it composes:
pnpm dlx shadcn@latest add @silkware/bubble @silkware/button @silkware/message @silkware/traceThen install the packages it imports and copy the pattern into components/ai-elements/sources.tsx:
pnpm add ai @ai-sdk/react @shadcn/helpers lucide-reactThe pattern also imports @/lib/ai, which holds the scripted streams.
"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 defaultExpanded> <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" onPress={() => { setMessages([]) sendMessage(question) }} > Replay </Button> )} </div> )}Usage
import { ArrowUpRightIcon, GlobeIcon } from "lucide-react"
import { Trace, TraceCollapsible, TraceCollapsibleContent, TraceCollapsibleTrigger, TraceIcon, TraceLine, TraceTitle,} from "@/components/ui/trace"<Trace> <TraceCollapsible defaultExpanded> <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>Each source is a link wrapped around a TraceLine, so it takes the muted colour and the hover state of the trace. Fall back to the hostname when a source has no title. The pattern removes duplicates by URL before it counts them, so "Used 3 sources" is the number of different pages.
Examples
Web pages
The demo streams an answer, then four source-url parts, one of them repeated. The list shows three.
Documents
The agent's sources are often files, not pages. Without a link to open, show the title and the file type with a TraceDescription, and keep the same summary line.
AI SDK
The AI SDK sends sources as message parts: source-url has url, title and a sourceId, and source-document has a title, a mediaType and an optional filename. Collect them from the message, drop duplicates, and render the list after the text.
const sources = message.parts .filter((part) => part.type === "source-url") .filter( (part, index, all) => all.findIndex((other) => other.url === part.url) === index )Sources are off by default on the stream. Turn them on in the route:
return result.toUIMessageStreamResponse({ sendSources: true })Whether a model returns sources depends on the model and its tools, such as a web search tool. If none arrive, render nothing: do not show an empty "Used 0 sources" line.
Offline, createChat adds a source with writer.sourceUrl({ url, title }), and the demo streams four of them. Use it to design the list before a search tool exists.
Accessibility
| Key | Action |
|---|---|
| Enter | Opens or closes the list when the summary has focus, or follows the focused link. |
| Space | Opens or closes the list when the summary has focus. |
| Tab | Moves between the summary and the links in the open list. |
The composed components provide:
- A disclosure with
aria-expandedand a visible focus ring. - Native links: they can be opened in a new tab, copied and read by screen readers as links.
You provide:
- A text for each link that names the page. A hostname alone is the fallback, not the aim.
- A note that a link opens in a new tab, as the pattern does with visually hidden text.
rel="noreferrer"on external links, which the pattern sets.
Guidelines
When to use
- Whenever the answer rests on pages or documents the person could open and check.
- Under the reply, close to the claim it supports. Pair it with Inline Citation when the reply makes several separate claims.
When not to use
- For the agent's actions, such as files it changed. Use Chain of Thought or Tool.
- For links the person asked for. Write them in the reply.
Content
- Show the page title, not the raw URL.
- List each source once.
- Say "Used 3 sources", not "Sources". A count tells the person how much the answer rests on.
Human in the loop
Sources let the person verify before they act. Link only to what the agent actually read, never to pages that look relevant. If a source cannot be opened, say so instead of hiding it.
List only the pages the agent read, with the title of each.
Don’t add sources the agent did not use to make the answer look better supported.
Composition
| Component | What it does here |
|---|---|
| Trace | The summary line, the nested list and each source line, with the collapsible. |
| Message and Bubble | The turn around the list in the demo. |
| Button | The Replay button in the demo. |
The pattern exports one component, SourcesDemo. Sources is internal to the file: copy it to reuse the list.