Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): handle missing sources gracefully in ChatSources component

Steven Enamakela594bdcbcde6
diff --git a/app/src/features/conversations/components/aui/ChatSources.tsx b/app/src/features/conversations/components/aui/ChatSources.tsx
index cbe0bae689..5d603d1fd5 100644
--- a/app/src/features/conversations/components/aui/ChatSources.tsx
+++ b/app/src/features/conversations/components/aui/ChatSources.tsx
@@ -9,11 +9,21 @@
* the turn's `citations` (memory retrieval) for `document`. `Thread` groups
* the run of source parts and hands them here through its `SourceGroup` slot.
*
- * Renders through the vendored `sources.aui` element's per-part `Sources`
- * component (a `SourceMessagePartComponent`) rather than the old collapsible
- * `components/ai-elements/Sources.tsx` disclosure — every source shows as a
- * badge/link inline, nothing hidden behind a click.
+ * A short list shows every source as a badge inline. Past
+ * `MAX_INLINE_SOURCES` the row is grouped behind one summary toggle — the
+ * first few sites' favicons stacked, then "N sources" — because a research
+ * turn cites twenty-plus pages and a flat wrap of that many badges buried the
+ * answer under half a screen of chips. Opening the toggle shows the full row.
+ *
+ * The favicon comes from the source's real site, not its URL host: grounded
+ * answers (Gemini) cite through `vertexaisearch.cloud.google.com` redirect
+ * URLs titled with the target domain, so keying the icon on the URL drew the
+ * same Google "G" on every badge. `sourceDomain` prefers a hostname-shaped
+ * title for that reason.
*/
+import { ChevronRightIcon } from 'lucide-react';
+import { useId, useState } from 'react';
+
import { Badge } from '../../../../components/assistant-ui/badge';
import {
DocumentSourceIcon,
@@ -21,8 +31,62 @@ import {
SourceIcon,
SourceTitle,
} from '../../../../components/assistant-ui/elements/sources.aui';
-import type { SourceItemPart } from '../../../../components/assistant-ui/thread';
+import { cn } from '../../../../components/assistant-ui/lib/utils';
+import type { SourceItemPart, SourceUrlPart } from '../../../../components/assistant-ui/thread';
import { useT } from '../../../../lib/i18n/I18nContext';
+import { fillPlaceholders } from '../../tools/toolPhrases';
+
+/** Up to this many sources render inline; more collapse behind the summary toggle. */
+export const MAX_INLINE_SOURCES = 4;
+/** Favicons stacked in the collapsed summary. */
+const SUMMARY_ICONS = 4;
+
+const HOSTNAME = /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i;
+
+/**
+ * The site a `url` source belongs to: a hostname-shaped title (a grounding
+ * redirect's target domain) wins over the URL's own host.
+ */
+export function sourceDomain(source: SourceUrlPart): string {
+ const title = source.title?.trim().replace(/^www\./i, '');
+ if (title && HOSTNAME.test(title)) return title.toLowerCase();
+ try {
+ return new URL(source.url).hostname.replace(/^www\./, '');
+ } catch {
+ return source.url;
+ }
+}
+
+/** Distinct sites across the url sources, in first-seen order. */
+function distinctDomains(sources: readonly SourceItemPart[]): string[] {
+ const seen = new Set<string>();
+ for (const source of sources) {
+ if (source.sourceType === 'url') seen.add(sourceDomain(source));
+ }
+ return [...seen];
+}
+
+function SourceBadge({ source }: { source: SourceItemPart }) {
+ const { t } = useT();
+ if (source.sourceType === 'url') {
+ return (
+ <Source href={source.url} data-testid="agent-source-row">
+ <SourceIcon url={`https://${sourceDomain(source)}`} />
+ <SourceTitle>{source.title || source.url}</SourceTitle>
+ </Source>
+ );
+ }
+ return (
+ <Badge variant="secondary" data-testid="agent-memory-source-row">
+ <span className="inline-flex items-center gap-1.5">
+ <DocumentSourceIcon />
+ <SourceTitle>
+ {source.title ?? t('conversations.agentTaskInsights.memoryCitationFallbackTitle')}
+ </SourceTitle>
+ </span>
+ </Badge>
+ );
+}
/**
* Composes the vendored `sources.aui` primitives (`Source`/`SourceIcon`/
@@ -34,30 +98,59 @@ import { useT } from '../../../../lib/i18n/I18nContext';
*/
export function ChatSources({ sources }: { sources: readonly SourceItemPart[] }) {
const { t } = useT();
+ const [open, setOpen] = useState(false);
+ const listId = useId();
if (sources.length === 0) return null;
+ const grouped = sources.length > MAX_INLINE_SOURCES;
+ const badges = (
+ <div
+ id={listId}
+ data-testid="turn-sources-list"
+ className="flex flex-wrap items-center gap-1.5">
+ {sources.map(source => (
+ <SourceBadge key={source.id} source={source} />
+ ))}
+ </div>
+ );
+
return (
<section
data-testid="turn-sources"
+ data-grouped={grouped ? 'true' : undefined}
aria-label={t('conversations.agentTaskInsights.sourcesHeading')}
- className="mt-1 flex flex-wrap items-center gap-1.5">
- {sources.map(source =>
- source.sourceType === 'url' ? (
- <Source key={source.id} href={source.url} data-testid="agent-source-row">
- <SourceIcon url={source.url} />
- <SourceTitle>{source.title || source.url}</SourceTitle>
- </Source>
- ) : (
- <Badge key={source.id} variant="secondary" data-testid="agent-memory-source-row">
- <span className="inline-flex items-center gap-1.5">
- <DocumentSourceIcon />
- <SourceTitle>
- {source.title ?? t('conversations.agentTaskInsights.memoryCitationFallbackTitle')}
- </SourceTitle>
- </span>
- </Badge>
- )
- )}
+ className="mt-1 flex flex-col items-start gap-1.5">
+ {grouped ? (
+ <button
+ type="button"
+ data-testid="turn-sources-toggle"
+ aria-expanded={open}
+ aria-controls={open ? listId : undefined}
+ onClick={() => setOpen(value => !value)}
+ className="border-input text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:ring-ring/50 inline-flex items-center gap-1.5 rounded-md border px-2 py-1 text-xs font-medium transition-colors outline-none focus-visible:ring-1">
+ <span className="flex items-center -space-x-1" aria-hidden="true">
+ {distinctDomains(sources)
+ .slice(0, SUMMARY_ICONS)
+ .map(domain => (
+ <SourceIcon
+ key={domain}
+ url={`https://${domain}`}
+ className="ring-background bg-background size-3.5 rounded-full ring-1"
+ />
+ ))}
+ </span>
+ <span>
+ {fillPlaceholders(t('conversations.tools.search.sources.other', '{count} sources'), {
+ count: String(sources.length),
+ })}
+ </span>
+ <ChevronRightIcon
+ className={cn('size-3 transition-transform', open && 'rotate-90')}
+ aria-hidden="true"
+ />
+ </button>
+ ) : null}
+ {!grouped || open ? badges : null}
</section>
);
}