'use client';
import { useEffect, useRef, type FC } from 'react';
import {
ActionBarPrimitive,
AssistantIf,
BranchPickerPrimitive,
ComposerPrimitive,
MessagePrimitive,
ThreadPrimitive,
useAssistantState,
useThreadViewport,
useThread,
} from '@assistant-ui/react';
import {
ArrowUpIcon,
BookOpenIcon,
CheckIcon,
ChevronDownIcon,
ChevronLeftIcon,
ChevronRightIcon,
CopyIcon,
PencilIcon,
RefreshCwIcon,
SquareIcon,
Trash2Icon,
} from 'lucide-react';
import { MarkdownText } from './MarkdownText';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { cn } from '@/utils/tailwind';
import type { ScoredChunk } from '@/services/ai/types';
interface ThreadProps {
sources?: ScoredChunk[];
onClear?: () => void;
onResetIndex?: () => void;
isLoadingHistory?: boolean;
hasActiveConversation?: boolean;
}
const LoadingOverlay: FC<{ isVisible: boolean }> = ({ isVisible }) => {
return (
);
};
const ScrollToBottomButton: FC = () => {
const isAtBottom = useThreadViewport((v) => v.isAtBottom);
const lastMessageRole = useThread((t) => t.messages.at(-1)?.role);
const isRunning = useThread((t) => t.isRunning);
// Don't show button if last message is user with no AI response yet
if (lastMessageRole === 'user' && !isRunning) return null;
return (
);
};
export const Thread: FC = ({
sources = [],
onClear,
onResetIndex,
isLoadingHistory = false,
hasActiveConversation = false,
}) => {
const viewportRef = useRef(null);
const isInitialMount = useRef(true);
const messageCount = useThread((t) => t.messages.length);
const lastMessageRole = useThread((t) => t.messages.at(-1)?.role);
const isRunning = useThread((t) => t.isRunning);
const showLoading = isLoadingHistory && hasActiveConversation;
useEffect(() => {
if (isInitialMount.current && messageCount > 0 && viewportRef.current) {
isInitialMount.current = false;
requestAnimationFrame(() => {
const viewport = viewportRef.current;
if (viewport) {
viewport.scrollTop = viewport.scrollHeight;
}
});
}
}, [messageCount]);
useEffect(() => {
if (lastMessageRole === 'user' && viewportRef.current && !isInitialMount.current) {
requestAnimationFrame(() => {
const viewport = viewportRef.current;
if (!viewport) return;
const messages = viewport.querySelectorAll('[data-message-role="user"]');
const lastUserMessage = messages[messages.length - 1];
if (lastUserMessage) {
lastUserMessage.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}
});
}
}, [messageCount, lastMessageRole]);
const getSpacerHeight = () => {
if (lastMessageRole === 'user' && !isRunning) {
return 'min-h-8';
}
if (isRunning) {
return 'min-h-[50vh]';
}
if (lastMessageRole === 'assistant') {
return 'min-h-4';
}
return 'min-h-4';
};
return (
{!hasActiveConversation && (
Ask about this book
Get answers based on the book content
)}
s.thread.isEmpty === false}>
,
}}
/>
AI can make mistakes. Verify with the book.
);
};
interface ComposerProps {
onClear?: () => void;
onResetIndex?: () => void;
}
const Composer: FC = ({ onClear, onResetIndex }) => {
const isEmpty = useAssistantState((s) => s.composer.isEmpty);
const isRunning = useAssistantState((s) => s.thread.isRunning);
return (
{onClear && (
)}
{onResetIndex && (
)}
{/* Placeholder when empty and not running */}
);
};
interface AssistantMessageProps {
sources?: ScoredChunk[];
}
const AssistantMessage: FC = ({ sources = [] }) => {
return (
s.message.status?.type !== 'running'}>
{sources.length > 0 && (
Sources from book
{sources.map((source, i) => (
{source.chapterTitle || `Section ${source.sectionIndex + 1}`}
{source.text}
))}
)}
message.isCopied}>
!message.isCopied}>
);
};
const UserMessage: FC = () => {
return (
message.isCopied}>
!message.isCopied}>
);
};
const EditComposer: FC = () => {
return (
);
};
const BranchPicker: FC<{ className?: string }> = ({ className }) => {
return (
/
);
};