The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
'use client';
/** assistant-ui registry thread list, shared by the live sidebar and demo.
* Host adapters supply list data and callbacks; optional row slots add product metadata.
*/
import { cn } from '@/components/assistant-ui/lib/utils';
import { Button } from '@/components/assistant-ui/ui/button';
import { Input } from '@/components/assistant-ui/ui/input';
import { Skeleton } from '@/components/assistant-ui/ui/skeleton';
import { useT } from '@/lib/i18n/I18nContext';
import {
AuiIf,
ThreadListItemMorePrimitive,
ThreadListItemPrimitive,
ThreadListPrimitive,
useAui,
useAuiState,
} from '@assistant-ui/react';
import {
ArchiveIcon,
Loader2Icon,
MoreHorizontalIcon,
PencilIcon,
PlusIcon,
SearchIcon,
TrashIcon,
} from 'lucide-react';
import {
type ComponentPropsWithoutRef,
type FC,
forwardRef,
Fragment,
type ReactNode,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
export const ThreadList: FC = () => {
const [search, setSearch] = useState('');
const hasThreads = useAuiState(s => s.threads.threadIds.length > 0);
return (
<ThreadListRoot>
<ThreadListNew />
{hasThreads && <ThreadListSearch value={search} onValueChange={setSearch} />}
<ThreadListItems searchQuery={hasThreads ? search : ''} />
</ThreadListRoot>
);
};
export const ThreadListSearch = forwardRef<
HTMLInputElement,
Omit<ComponentPropsWithoutRef<typeof Input>, 'value' | 'onChange'> & {
value: string;
onValueChange: (value: string) => void;
}
>(({ className, value, onValueChange, ...props }, ref) => {
const { t } = useT();
const searchThreadsLabel = t('assistantUi.threadList.searchThreads', 'Search threads');
return (
<div data-slot="aui_thread-list-search" className="relative px-0.5 py-1">
<SearchIcon
data-slot="aui_thread-list-search-icon"
className="text-muted-foreground pointer-events-none absolute start-3 top-1/2 size-4 -translate-y-1/2"
/>
<Input
ref={ref}
type="search"
value={value}
onChange={event => onValueChange(event.target.value)}
aria-label={searchThreadsLabel}
placeholder={searchThreadsLabel}
className={cn('h-8 ps-8 text-sm', className)}
{...props}
/>
</div>
);
});
ThreadListSearch.displayName = 'ThreadListSearch';
export const ThreadListRoot: FC<ComponentPropsWithoutRef<typeof ThreadListPrimitive.Root>> = ({
className,
...props
}) => {
return (
<ThreadListPrimitive.Root
data-slot="aui_thread-list-root"
className={cn('flex flex-col gap-0.5', className)}
{...props}
/>
);
};
export const ThreadListItems: FC<ComponentPropsWithoutRef<'div'> & { searchQuery?: string }> = ({
className,
searchQuery = '',
...props
}) => {
return (
<div
data-slot="aui_thread-list-items"
className={cn('flex flex-col gap-0.5', className)}
{...props}>
<AuiIf condition={s => s.threads.isLoading}>
<ThreadListSkeleton />
</AuiIf>
<AuiIf condition={s => !s.threads.isLoading}>
<ThreadListItemGroups searchQuery={searchQuery} />
</AuiIf>
</div>
);
};
const DAY_IN_MS = 86_400_000;
const dateGroupLabel = (date: Date | undefined, startOfToday: number): string => {
if (!date || date.getTime() >= startOfToday) return 'Today';
if (date.getTime() >= startOfToday - DAY_IN_MS) return 'Yesterday';
return 'Earlier';
};
type ThreadListGroup = { label: string; indices: number[] };
const ThreadListItemGroups: FC<{ searchQuery?: string }> = ({ searchQuery = '' }) => {
const { t } = useT();
const threadIds = useAuiState(s => s.threads.threadIds);
const threadItems = useAuiState(s => s.threads.threadItems);
const query = searchQuery.trim().toLowerCase();
const { filteredIndices, groups } = useMemo(() => {
const itemsById = new Map(threadItems.map(item => [item.id, item]));
const dates = threadIds.map(id => itemsById.get(id)?.lastMessageAt);
const filteredIndices = threadIds
.map((id, index) => ({ id, index }))
.filter(
({ id }) => !query || (itemsById.get(id)?.title || 'New Chat').toLowerCase().includes(query)
)
.map(({ index }) => index);
if (!filteredIndices.some(index => dates[index])) {
return { filteredIndices, groups: null };
}
const now = new Date();
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
const time = (index: number) => dates[index]?.getTime() ?? Number.MAX_SAFE_INTEGER;
const sorted = [...filteredIndices].sort((a, b) => time(b) - time(a));
const result: ThreadListGroup[] = [];
for (const index of sorted) {
const label = dateGroupLabel(dates[index], startOfToday);
const lastGroup = result[result.length - 1];
if (lastGroup?.label === label) {
lastGroup.indices.push(index);
} else {
result.push({ label, indices: [index] });
}
}
return { filteredIndices, groups: result };
}, [threadIds, threadItems, query]);
if (query && filteredIndices.length === 0) {
return (
<div data-slot="aui_thread-list-empty" className="text-muted-foreground px-2.5 py-4 text-sm">
{t('assistantUi.threadList.noThreadsFound', 'No threads found')}
</div>
);
}
if (!groups) {
return filteredIndices.map(index => (
<ThreadListPrimitive.ItemByIndex
key={threadIds[index]}
index={index}
components={{ ThreadListItem }}
/>
));
}
return groups.map(group => (
<Fragment key={group.label}>
<div
data-slot="aui_thread-list-group-label"
className="text-muted-foreground px-2.5 pt-3 pb-1 text-xs font-medium">
{group.label}
</div>
{group.indices.map(index => (
<ThreadListPrimitive.ItemByIndex
key={threadIds[index]}
index={index}
components={{ ThreadListItem }}
/>
))}
</Fragment>
));
};
export const ThreadListNew = forwardRef<
HTMLButtonElement,
ComponentPropsWithoutRef<typeof Button> & { labelClassName?: string; label?: string }
>(({ className, labelClassName, label, children, ...props }, ref) => {
const { t } = useT();
return (
<ThreadListPrimitive.New
render={
<Button
ref={ref}
variant="ghost"
data-slot="aui_thread-list-new"
className={cn(
'hover:bg-muted data-active:bg-muted h-8 justify-start gap-2 rounded-md px-2.5 text-sm font-normal',
className
)}
{...props}
/>
}>
{children ?? (
<>
<PlusIcon data-slot="aui_thread-list-new-icon" className="size-4 shrink-0" />
<span
data-slot="aui_thread-list-new-label"
className={cn('whitespace-nowrap', labelClassName)}>
{label ?? t('assistantUi.threadList.newThread', 'New Thread')}
</span>
</>
)}
</ThreadListPrimitive.New>
);
});
ThreadListNew.displayName = 'ThreadListNew';
const ThreadListSkeleton: FC = () => {
const { t } = useT();
const loadingThreadsLabel = t('assistantUi.threadList.loadingThreads', 'Loading threads');
return (
<div className="flex flex-col gap-0.5">
{Array.from({ length: 5 }, (_, i) => (
<div
key={i}
role="status"
aria-label={loadingThreadsLabel}
data-slot="aui_thread-list-skeleton-wrapper"
className="flex h-8 items-center px-2.5">
<Skeleton data-slot="aui_thread-list-skeleton" className="h-3.5 w-full" />
</div>
))}
</div>
);
};
export const ThreadListItem: FC<{
className?: string;
running?: boolean;
trailing?: ReactNode;
menuExtras?: ReactNode;
showArchive?: boolean;
triggerProps?: ComponentPropsWithoutRef<'button'>;
}> = ({ className, running, trailing, menuExtras, showArchive = true, triggerProps }) => {
const { t } = useT();
const runtimeRunning = useAuiState(s => s.threadListItem.isRunning);
const isRunning = running ?? runtimeRunning;
const [isRenaming, setIsRenaming] = useState(false);
const triggerRef = useRef<HTMLButtonElement>(null);
const restoreFocusRef = useRef(false);
useEffect(() => {
if (isRenaming || !restoreFocusRef.current) return;
restoreFocusRef.current = false;
triggerRef.current?.focus();
}, [isRenaming]);
return (
<ThreadListItemPrimitive.Root
data-slot="aui_thread-list-item"
className={cn(
'group hover:bg-muted focus-visible:bg-muted data-active:bg-muted has-focus-visible:bg-muted has-data-[state=open]:bg-muted relative flex h-8 items-center rounded-md transition-colors focus-visible:outline-none',
className
)}>
{isRenaming ? (
<ThreadListItemRename
onDone={restoreFocus => {
restoreFocusRef.current = restoreFocus;
setIsRenaming(false);
}}
/>
) : (
<ThreadListItemPrimitive.Trigger
ref={triggerRef}
{...triggerProps}
data-slot="aui_thread-list-item-trigger"
className="focus-visible:ring-ring/50 flex h-full min-w-0 flex-1 items-center rounded-md px-2.5 text-start text-sm outline-none group-hover:pe-9 group-has-focus-visible:pe-9 group-has-data-[state=open]:pe-9 group-data-active:pe-9 focus-visible:ring-1">
<span
data-slot="aui_thread-list-item-title"
data-running={isRunning ? 'true' : undefined}
aria-busy={isRunning || undefined}
className="min-w-0 flex-1 truncate">
<ThreadListItemPrimitive.Title fallback={t('chat.untitledThread')} />
</span>
{trailing}
{isRunning && (
<>
<Loader2Icon
data-slot="aui_thread-list-item-running"
aria-hidden
className="ms-2 size-3.5 shrink-0 animate-spin motion-reduce:animate-none text-muted-foreground"
/>
<span className="sr-only">
{t('conversations.backgroundTasks.statusRunning', 'Running')}
</span>
</>
)}
</ThreadListItemPrimitive.Trigger>
)}
<ThreadListItemMore
isRenaming={isRenaming}
onRename={() => setIsRenaming(true)}
showArchive={showArchive}
extras={menuExtras}
/>
</ThreadListItemPrimitive.Root>
);
};
const ThreadListItemRename: FC<{ onDone: (restoreFocus: boolean) => void }> = ({ onDone }) => {
const { t } = useT();
const aui = useAui();
const title = useAuiState(s => s.threadListItem.title) ?? '';
const [value, setValue] = useState(title);
const inputRef = useRef<HTMLInputElement>(null);
const settledRef = useRef(false);
useEffect(() => {
inputRef.current?.select();
}, []);
const commit = (restoreFocus: boolean) => {
if (settledRef.current) return;
settledRef.current = true;
const next = value.trim();
if (!next || next === title) {
onDone(restoreFocus);
return;
}
// Deferred so a synchronous throw lands on the rejection path too.
Promise.resolve()
.then(() => aui.threadListItem.rename(next))
.then(
() => onDone(restoreFocus),
() => {
settledRef.current = false;
if (restoreFocus) inputRef.current?.focus();
}
);
};
const cancel = () => {
if (settledRef.current) return;
settledRef.current = true;
onDone(true);
};
return (
<Input
ref={inputRef}
autoFocus
data-slot="aui_thread-list-item-rename"
data-testid={`thread-title-input-${aui.threadListItem.getState().id}`}
aria-label={t('assistantUi.threadList.renameThread', 'Rename thread')}
value={value}
className="h-7 min-w-0 flex-1 ps-2.5 pe-9 text-sm"
onChange={event => setValue(event.target.value)}
onBlur={() => commit(false)}
onKeyDown={event => {
if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return;
if (event.key === 'Enter') {
event.preventDefault();
commit(true);
} else if (event.key === 'Escape') {
event.preventDefault();
cancel();
}
}}
/>
);
};
const ThreadListItemMore: FC<{
isRenaming: boolean;
onRename: () => void;
showArchive: boolean;
extras?: ReactNode;
}> = ({ isRenaming, onRename, showArchive, extras }) => {
const { t } = useT();
return (
<ThreadListItemMorePrimitive.Root sharedFocusGroup>
<ThreadListItemMorePrimitive.Trigger
render={
<Button
variant="ghost"
size="icon"
data-slot="aui_thread-list-item-more"
className="data-[state=open]:bg-accent absolute end-1.5 top-1/2 size-6 -translate-y-1/2 p-0 opacity-0 group-hover:opacity-100 group-has-focus-visible:opacity-100 group-data-active:opacity-100 data-[state=open]:opacity-100"
/>
}>
<MoreHorizontalIcon className="size-3.5" />
<span className="sr-only">{t('assistantUi.threadList.moreOptions', 'More options')}</span>
</ThreadListItemMorePrimitive.Trigger>
<ThreadListItemMorePrimitive.Content
// The closed menu remains mounted during its exit animation. It must
// not move focus away from the editor and commit a cancelled rename.
onEscapeKeyDown={event => {
if (isRenaming) event.preventDefault();
}}
onCloseAutoFocus={event => {
if (isRenaming) event.preventDefault();
}}
side="right"
align="start"
sideOffset={6}
data-slot="aui_thread-list-item-more-content"
className="bg-popover text-popover-foreground data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=open]:animate-in data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:animate-out data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 min-w-32 overflow-hidden rounded-xl border p-1.5">
<ThreadListItemMorePrimitive.Item
data-slot="aui_thread-list-item-more-item"
className="hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground flex cursor-pointer items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm outline-none select-none"
onSelect={onRename}>
<PencilIcon className="size-4" />
{t('assistantUi.threadList.rename', 'Rename')}
</ThreadListItemMorePrimitive.Item>
{extras}
{showArchive && (
<ThreadListItemPrimitive.Archive
render={
<ThreadListItemMorePrimitive.Item
data-slot="aui_thread-list-item-more-item"
className="hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground flex cursor-pointer items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm outline-none select-none"
/>
}>
<ArchiveIcon className="size-4" />
{t('assistantUi.threadList.archive', 'Archive')}
</ThreadListItemPrimitive.Archive>
)}
<ThreadListItemPrimitive.Delete
render={
<ThreadListItemMorePrimitive.Item
data-slot="aui_thread-list-item-more-item"
className="text-destructive hover:bg-destructive/10 hover:text-destructive focus:bg-destructive/10 focus:text-destructive flex cursor-pointer items-center gap-2 rounded-lg px-2.5 py-1.5 text-sm outline-none select-none"
/>
}>
<TrashIcon className="size-4" />
{t('common.delete', 'Delete')}
</ThreadListItemPrimitive.Delete>
</ThreadListItemMorePrimitive.Content>
</ThreadListItemMorePrimitive.Root>
);
};