"use client"; import { AttachmentPrimitive, ComposerPrimitive, MessagePrimitive, useAssistantApi, useAssistantState, } from "@assistant-ui/react"; import { FileText, PlusIcon, XIcon } from "lucide-react"; import Image from "next/image"; import { type FC, type PropsWithChildren, useEffect, useState } from "react"; import { useShallow } from "zustand/shallow"; import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; const useFileSrc = (file: File | undefined) => { const [src, setSrc] = useState(undefined); useEffect(() => { if (!file) { setSrc(undefined); return; } const objectUrl = URL.createObjectURL(file); setSrc(objectUrl); return () => { URL.revokeObjectURL(objectUrl); }; }, [file]); return src; }; const useAttachmentSrc = () => { const { file, src } = useAssistantState( useShallow(({ attachment }): { file?: File; src?: string } => { if (attachment.type !== "image") return {}; if (attachment.file) return { file: attachment.file }; const src = attachment.content?.filter((c) => c.type === "image")[0]?.image; if (!src) return {}; return { src }; }) ); return useFileSrc(file) ?? src; }; type AttachmentPreviewProps = { src: string; }; const AttachmentPreview: FC = ({ src }) => { const [isLoaded, setIsLoaded] = useState(false); return ( Image Preview setIsLoaded(true)} priority={false} /> ); }; const AttachmentPreviewDialog: FC = ({ children }) => { const src = useAttachmentSrc(); if (!src) return children; return ( {children} Image Attachment Preview
); }; const AttachmentThumb: FC = () => { const isImage = useAssistantState(({ attachment }) => attachment.type === "image"); const src = useAttachmentSrc(); return ( ); }; const AttachmentUI: FC = () => { const api = useAssistantApi(); const isComposer = api.attachment.source === "composer"; const isImage = useAssistantState(({ attachment }) => attachment.type === "image"); const typeLabel = useAssistantState(({ attachment }) => { const type = attachment.type; switch (type) { case "image": return "Image"; case "document": return "Document"; case "file": return "File"; default: { const _exhaustiveCheck: never = type; throw new Error(`Unknown attachment type: ${_exhaustiveCheck}`); } } }); return ( #attachment-tile]:size-24" )} >
{isComposer && }
); }; const AttachmentRemove: FC = () => { return ( ); }; export const UserMessageAttachments: FC = () => { return (
); }; export const ComposerAttachments: FC = () => { return (
); }; export const ComposerAddAttachment: FC = () => { return ( ); };