Sources
Displays the links the agent used for a reply.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For, createMemo } from "solid-js"
import {
useDemoChat,
type UIMessage,
createChat,
getMessageText
} from "~/lib/ai-demo-chat"
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(componentProps: { sources: SourcePart[] }) {
const content = createMemo(() => {
const { sources } = componentProps
return (
<Trace>
<TraceCollapsible defaultOpen>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20" />
<path d="M2 12h20" />
</svg>
</TraceIcon>
<TraceTitle>Used {sources.length} sources</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<Trace>
{
<For each={sources}>
{(source) => (
<a href={source.url} target="_blank" rel="noreferrer">
<TraceLine>
<TraceTitle>
{source.title ?? new URL(source.url).hostname}
<span class="sr-only"> (opens in a new tab)</span>
</TraceTitle>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M7 7h10v10" />
<path d="M7 17 17 7" />
</svg>
</TraceIcon>
</TraceLine>
</a>
)}
</For>
}
</Trace>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
)
})
return <>{content()}</>
}
export default function SourcesDemo() {
const { messages, sendMessage, setMessages, status } = useDemoChat({
messages: transcript,
transport
})
const busy = () => status() === "submitted" || status() === "streaming"
return (
<div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
{
<For each={messages()}>
{(message) => {
const content = createMemo(() => {
if (message.role === "user") {
return (
<Message 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>
<MessageContent class="gap-4">
{text && <p>{text}</p>}
{sources.length > 0 && <Sources sources={sources} />}
</MessageContent>
</Message>
)
})
return <>{content()}</>
}}
</For>
}
{!busy() && (
<Button
variant="outline"
size="sm"
class="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.
Install the required Solid UI dependencies:
Copy and paste the following code into your project.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { ComponentProps, JSX } from "@solidjs/web"
import { merge, omit } from "solid-js"
import { cn } from "~/lib/utils"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
export function Trace(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="trace"
class={cn(
"gap-2 *:has-[>[data-slot=trace-line]]:rounded-md *:has-[>[data-slot=trace-line]]:focus-visible:ring-3 *:has-[>[data-slot=trace-line]]:focus-visible:ring-ring/50 flex w-full min-w-0 flex-col *: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]]:text-left *:has-[>[data-slot=trace-line]]:outline-none",
props.class
)}
/>
)
}
export function TraceLine(
props: Omit<ComponentProps<"span">, "class"> & {
class?: string
status?: "running" | "error" | "pending" | undefined
}
) {
const rest = omit(props, "class", "status")
return (
<span
{...rest}
data-slot="trace-line"
data-status={props.status ?? undefined}
class={cn(
"gap-2 text-sm flex w-fit max-w-full min-w-0 items-center 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",
props.class
)}
/>
)
}
export function TraceIcon(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-icon"
class={cn(
"size-4 [&_svg:not([class*='size-'])]:size-4 [[data-slot=trace-title]~&]:-ml-1 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none",
props.class
)}
/>
)
}
export function TraceTitle(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-title"
class={cn(
"min-w-0 truncate motion-reduce:shimmer-none",
props.class
)}
/>
)
}
export function TraceDescription(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-description"
class={cn(
"shrink-0 text-muted-foreground/70 tabular-nums",
props.class
)}
/>
)
}
export function TraceCollapsible(props: ComponentProps<typeof Collapsible>) {
const rest = omit(props, "class", "render")
return (
<Collapsible
{...rest}
class={cn(
"flex w-full min-w-0 flex-col",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, { "data-slot": "trace-collapsible" })
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
/>
)
}
export function TraceCollapsibleTrigger(
props: ComponentProps<typeof CollapsibleTrigger>
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleTrigger
{...rest}
class={cn(
"gap-1 rounded-md focus-visible:ring-3 focus-visible:ring-ring/50 group/trace-collapsible-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center text-left text-muted-foreground transition-colors outline-none hover:text-foreground has-[[data-slot=trace-line][data-status=error]]:text-destructive",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-trigger"
})
return props.render ? (
props.render(forwarded, state)
) : (
<button {...forwarded} />
)
}}
>
{props.children}
<TraceIcon class="transition-transform group-aria-expanded/trace-collapsible-trigger:rotate-90">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="m9 18 6-6-6-6" />
</svg>
</TraceIcon>
</CollapsibleTrigger>
)
}
export function TraceCollapsibleContent(
props: Omit<ComponentProps<typeof CollapsibleContent>, "children"> & {
children?: JSX.Element
}
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleContent
{...rest}
class="duration-200 h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] data-ending-style:h-0 data-starting-style:h-0"
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-content"
})
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
>
<div
data-slot="trace-collapsible-content-inner"
class={cn(
"mt-2 mb-1 ml-[calc(--spacing(2)-1px)] border-l pr-1 pl-4 leading-relaxed min-w-0 wrap-break-word text-muted-foreground",
props.class
)}
>
{props.children}
</div>
</CollapsibleContent>
)
}Update the import paths to match your project setup.
Usage
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"import { For } from "solid-js"
;<Trace>
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<GlobeIcon />
</TraceIcon>
<TraceTitle>Used {sources.length} sources</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<Trace>
{
<For each={sources}>
{(source) => (
<a href={source.url} target="_blank" rel="noreferrer">
<TraceLine>
<TraceIcon>
<GlobeIcon />
</TraceIcon>
<TraceTitle>
{source.title ?? new URL(source.url).hostname}
<span class="sr-only"> (opens in a new tab)</span>
</TraceTitle>
<TraceIcon>
<ArrowUpRightIcon />
</TraceIcon>
</TraceLine>
</a>
)}
</For>
}
</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 Solid 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 solidui-cli@latest add trace
2. Read https://www.solid-ui.com/docs/ai-elements/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.