Skip to content
WebJs
Menu

Displays the links the agent used for a reply.

How do I start an Astro project with pnpm?

Run pnpm create astro@latest and follow the prompts. It scaffolds the project and installs the dependencies.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { bubbleClass, bubbleContentClass } from '#components/ui/bubble.ts';
import { buttonClass } from '#components/ui/button.ts';
import { messageClass, messageContentClass } from '#components/ui/message.ts';
import {
  traceClass, traceLineClass, traceIconClass, traceTitleClass,
  traceCollapsibleClass, traceCollapsibleTriggerClass, traceCollapsibleContentClass,
} from '#components/ui/trace.ts';
import { ArrowUpRightIcon, ChevronRightIcon, GlobeIcon } from '#lib/ui/lucide.ts';

// The agent cited the Astro page twice, so the list is deduped by url.
const sources = [
  { url: 'https://docs.astro.build/en/install-and-setup/', title: 'Install Astro | docs.astro.build' },
  { url: 'https://www.npmjs.com/package/create-astro', title: 'create-astro | npmjs.com' },
  { url: 'https://docs.astro.build/en/install-and-setup/', title: 'Install Astro | docs.astro.build' },
  { url: 'https://pnpm.io/cli/create', title: 'pnpm create | pnpm.io' },
].filter((source, index, all) => all.findIndex((other) => other.url === source.url) === index);

export default function SourcesDemo() {
  return html`
    <div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
      <div data-slot="message" data-align="end" class=${messageClass()}>
        <div data-slot="message-content" class=${messageContentClass()}>
          <div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
            <div data-slot="bubble-content" class=${bubbleContentClass()}>How do I start an Astro project with pnpm?</div>
          </div>
        </div>
      </div>
      <div data-slot="message" data-align="start" class=${messageClass()}>
        <div data-slot="message-content" class=${cn(messageContentClass(), 'gap-4')}>
          <p>Run pnpm create astro@latest and follow the prompts. It scaffolds the project and installs the dependencies.</p>
          <div data-slot="trace" class=${traceClass()}>
            <details data-slot="trace-collapsible" open class=${cn(traceCollapsibleClass(), 'group/trace-collapsible')}>
              <summary data-slot="trace-collapsible-trigger" class=${traceCollapsibleTriggerClass()}>
                <span data-slot="trace-line" class=${traceLineClass()}>
                  <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${GlobeIcon()}</span>
                  <span data-slot="trace-title" class=${traceTitleClass()}>Used ${sources.length} sources</span>
                </span>
                <span data-slot="trace-icon" aria-hidden="true" class=${cn(traceIconClass(), 'transition-transform group-open/trace-collapsible:rotate-90')}>${ChevronRightIcon()}</span>
              </summary>
              <div data-slot="trace-collapsible-content" class=${traceCollapsibleContentClass()}>
                <div data-slot="trace" class=${traceClass()}>
                  ${sources.map((source) => html`
                    <a href=${source.url} target="_blank" rel="noreferrer">
                      <span data-slot="trace-line" class=${traceLineClass()}>
                        <span data-slot="trace-title" class=${traceTitleClass()}>
                          ${source.title}
                          <span class="sr-only"> (opens in a new tab)</span>
                        </span>
                        <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${ArrowUpRightIcon()}</span>
                      </span>
                    </a>
                  `)}
                </div>
              </div>
            </details>
          </div>
        </div>
      </div>
      <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${cn(buttonClass({ variant: 'outline', size: 'sm' }), 'mt-auto self-start')}>
        Replay
      </button>
    </div>
  `;
}
View Code

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 a trace, with a trace collapsible for the list.

Installation

Sources is built with the Trace component.

pnpm dlx @webjsdev/ui add trace

Usage

import {
  traceClass,
  traceCollapsibleClass,
  traceCollapsibleContentClass,
  traceCollapsibleTriggerClass,
  traceIconClass,
  traceLineClass,
  traceTitleClass,
} from '#components/ui/trace.ts';
<div data-slot="trace" class=${traceClass()}>
  <details data-slot="trace-collapsible" class=${cn(traceCollapsibleClass(), 'group/trace-collapsible')}>
    <summary data-slot="trace-collapsible-trigger" class=${traceCollapsibleTriggerClass()}>
      <span data-slot="trace-line" class=${traceLineClass()}>
        <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>
          ${GlobeIcon()}
        </span>
        <span data-slot="trace-title" class=${traceTitleClass()}>Used ${sources.length} sources</span>
      </span>
      <span data-slot="trace-icon" aria-hidden="true" class=${cn(traceIconClass(), 'transition-transform group-open/trace-collapsible:rotate-90')}>
        ${ChevronRightIcon()}
      </span>
    </summary>
    <div data-slot="trace-collapsible-content" class=${traceCollapsibleContentClass()}>
      <div data-slot="trace" class=${traceClass()}>
        ${sources.map((source) => html`
          <a href=${source.url} target="_blank" rel="noreferrer">
            <span data-slot="trace-line" class=${traceLineClass()}>
              <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>
                ${GlobeIcon()}
              </span>
              <span data-slot="trace-title" class=${traceTitleClass()}>
                ${source.title ?? new URL(source.url).hostname}
                <span class="sr-only"> (opens in a new tab)</span>
              </span>
              <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>
                ${ArrowUpRightIcon()}
              </span>
            </span>
          </a>
        `)}
      </div>
    </div>
  </details>
</div>

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. See AI SDK for streaming the parts from a server action.

Accessibility

Each source is a plain link inside the list.

  • Links open in a new tab. Add sr-only text, 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 used Sources from AI SDK Elements, its wrapper lives in components/ai-elements/sources.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
SourcesA trace with a trace collapsible.
SourcesTriggerThe collapsible's <summary>, holding a trace line with a trace icon and a trace title that uses the count, like "Used 3 sources".
SourcesContentThe trace collapsible content, holding a trace for the links.
SourceA link that wraps a trace line, with a trace icon and a trace title for its title, and "(opens in a new tab)" in an sr-only span.

API Reference

See Trace.