import { memo } from 'react'; import type { BlockKind } from './block'; import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from './icons'; import { toast } from 'sonner'; import { useBlock } from '@/hooks/use-block'; const getActionText = ( type: 'create' | 'update' | 'request-suggestions', tense: 'present' | 'past', ) => { switch (type) { case 'create': return tense === 'present' ? 'Creating' : 'Created'; case 'update': return tense === 'present' ? 'Updating' : 'Updated'; case 'request-suggestions': return tense === 'present' ? 'Adding suggestions' : 'Added suggestions to'; default: return null; } }; interface DocumentToolResultProps { type: 'create' | 'update' | 'request-suggestions'; result: { id: string; title: string; kind: BlockKind }; isReadonly: boolean; } function PureDocumentToolResult({ type, result, isReadonly, }: DocumentToolResultProps) { const { setBlock } = useBlock(); return ( { if (isReadonly) { toast.error( 'Viewing files in shared chats is currently not supported.', ); return; } const rect = event.currentTarget.getBoundingClientRect(); const boundingBox = { top: rect.top, left: rect.left, width: rect.width, height: rect.height, }; setBlock({ documentId: result.id, kind: result.kind, content: '', title: result.title, isVisible: true, status: 'idle', boundingBox, }); }} > {type === 'create' ? ( ) : type === 'update' ? ( ) : type === 'request-suggestions' ? ( ) : null} {`${getActionText(type, 'past')} "${result.title}"`} ); } export const DocumentToolResult = memo(PureDocumentToolResult, () => true); interface DocumentToolCallProps { type: 'create' | 'update' | 'request-suggestions'; args: { title: string }; isReadonly: boolean; } function PureDocumentToolCall({ type, args, isReadonly, }: DocumentToolCallProps) { const { setBlock } = useBlock(); return ( { if (isReadonly) { toast.error( 'Viewing files in shared chats is currently not supported.', ); return; } const rect = event.currentTarget.getBoundingClientRect(); const boundingBox = { top: rect.top, left: rect.left, width: rect.width, height: rect.height, }; setBlock((currentBlock) => ({ ...currentBlock, isVisible: true, boundingBox, })); }} > {type === 'create' ? ( ) : type === 'update' ? ( ) : type === 'request-suggestions' ? ( ) : null} {`${getActionText(type, 'present')} ${args.title ? `"${args.title}"` : ''}`} {} ); } export const DocumentToolCall = memo(PureDocumentToolCall, () => true);