2024-10-24 16:35:51 -04:00
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-09-08 16:56:10 +02:00
|
|
|
import type { LanguageModelUsage, UIMessage } from 'ai';
|
2024-11-15 12:18:17 -05:00
|
|
|
import {
|
2024-10-11 18:00:22 +05:30
|
|
|
useRef,
|
|
|
|
|
useEffect,
|
|
|
|
|
useState,
|
|
|
|
|
useCallback,
|
2024-11-15 12:18:17 -05:00
|
|
|
type Dispatch,
|
|
|
|
|
type SetStateAction,
|
|
|
|
|
type ChangeEvent,
|
2024-12-03 17:49:38 +03:00
|
|
|
memo,
|
2025-09-08 16:56:10 +02:00
|
|
|
useMemo,
|
2024-10-24 16:35:51 -04:00
|
|
|
} from 'react';
|
|
|
|
|
import { toast } from 'sonner';
|
2024-11-01 15:31:54 +05:30
|
|
|
import { useLocalStorage, useWindowSize } from 'usehooks-ts';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-09-09 23:58:40 +01:00
|
|
|
import { ArrowUpIcon, PaperclipIcon, CpuIcon, StopIcon, ChevronDownIcon } from './icons';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { PreviewAttachment } from './preview-attachment';
|
2024-11-15 10:14:25 -05:00
|
|
|
import { Button } from './ui/button';
|
2024-12-03 17:49:38 +03:00
|
|
|
import { SuggestedActions } from './suggested-actions';
|
2025-08-28 14:15:36 +01:00
|
|
|
import {
|
|
|
|
|
PromptInput,
|
|
|
|
|
PromptInputTextarea,
|
|
|
|
|
PromptInputToolbar,
|
|
|
|
|
PromptInputTools,
|
|
|
|
|
PromptInputSubmit,
|
2025-09-01 11:07:07 +01:00
|
|
|
PromptInputModelSelect,
|
|
|
|
|
PromptInputModelSelectContent,
|
2025-08-28 14:15:36 +01:00
|
|
|
} from './elements/prompt-input';
|
2025-09-09 15:44:07 -04:00
|
|
|
import { SelectItem } from '@/components/ui/select';
|
2025-09-09 23:58:40 +01:00
|
|
|
import * as SelectPrimitive from '@radix-ui/react-select';
|
2024-12-04 17:27:48 +03:00
|
|
|
import equal from 'fast-deep-equal';
|
2025-04-03 01:05:07 -07:00
|
|
|
import type { UseChatHelpers } from '@ai-sdk/react';
|
2025-05-01 02:28:24 -07:00
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
|
|
|
|
import { ArrowDown } from 'lucide-react';
|
|
|
|
|
import { useScrollToBottom } from '@/hooks/use-scroll-to-bottom';
|
2025-05-01 17:47:48 -07:00
|
|
|
import type { VisibilityType } from './visibility-selector';
|
2025-07-03 02:26:34 -07:00
|
|
|
import type { Attachment, ChatMessage } from '@/lib/types';
|
2025-09-01 11:07:07 +01:00
|
|
|
import { chatModels } from '@/lib/ai/models';
|
|
|
|
|
import { saveChatModelAsCookie } from '@/app/(chat)/actions';
|
|
|
|
|
import { startTransition } from 'react';
|
2025-09-08 16:56:10 +02:00
|
|
|
import { getContextWindow, normalizeUsage } from 'tokenlens';
|
|
|
|
|
import { Context } from './elements/context';
|
|
|
|
|
import { myProvider } from '@/lib/ai/providers';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
function PureMultimodalInput({
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId,
|
2024-10-11 18:00:22 +05:30
|
|
|
input,
|
|
|
|
|
setInput,
|
2025-03-11 15:33:18 -07:00
|
|
|
status,
|
2024-10-11 18:00:22 +05:30
|
|
|
stop,
|
|
|
|
|
attachments,
|
|
|
|
|
setAttachments,
|
|
|
|
|
messages,
|
2024-10-30 16:01:24 +05:30
|
|
|
setMessages,
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage,
|
2024-10-30 16:01:24 +05:30
|
|
|
className,
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType,
|
2025-09-01 11:07:07 +01:00
|
|
|
selectedModelId,
|
2025-09-08 16:56:10 +02:00
|
|
|
usage,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId: string;
|
2025-07-03 02:26:34 -07:00
|
|
|
input: string;
|
|
|
|
|
setInput: Dispatch<SetStateAction<string>>;
|
|
|
|
|
status: UseChatHelpers<ChatMessage>['status'];
|
2024-10-11 18:00:22 +05:30
|
|
|
stop: () => void;
|
|
|
|
|
attachments: Array<Attachment>;
|
|
|
|
|
setAttachments: Dispatch<SetStateAction<Array<Attachment>>>;
|
2025-03-20 14:10:45 -07:00
|
|
|
messages: Array<UIMessage>;
|
2025-07-03 02:26:34 -07:00
|
|
|
setMessages: UseChatHelpers<ChatMessage>['setMessages'];
|
|
|
|
|
sendMessage: UseChatHelpers<ChatMessage>['sendMessage'];
|
2024-10-30 16:01:24 +05:30
|
|
|
className?: string;
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType: VisibilityType;
|
2025-09-01 11:07:07 +01:00
|
|
|
selectedModelId: string;
|
2025-09-08 16:56:10 +02:00
|
|
|
usage?: LanguageModelUsage;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) {
|
|
|
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
2024-10-14 23:01:46 +05:30
|
|
|
const { width } = useWindowSize();
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (textareaRef.current) {
|
|
|
|
|
adjustHeight();
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const adjustHeight = () => {
|
|
|
|
|
if (textareaRef.current) {
|
2025-09-07 00:04:51 +01:00
|
|
|
textareaRef.current.style.height = '44px';
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-08 17:00:38 +05:30
|
|
|
const resetHeight = () => {
|
|
|
|
|
if (textareaRef.current) {
|
2025-09-07 00:04:51 +01:00
|
|
|
textareaRef.current.style.height = '44px';
|
2025-01-08 17:00:38 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-31 21:02:05 +05:30
|
|
|
const [localStorageInput, setLocalStorageInput] = useLocalStorage(
|
|
|
|
|
'input',
|
2024-11-15 13:00:15 -05:00
|
|
|
'',
|
2024-10-31 21:02:05 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (textareaRef.current) {
|
|
|
|
|
const domValue = textareaRef.current.value;
|
|
|
|
|
// Prefer DOM value over localStorage to handle hydration
|
|
|
|
|
const finalValue = domValue || localStorageInput || '';
|
|
|
|
|
setInput(finalValue);
|
|
|
|
|
adjustHeight();
|
|
|
|
|
}
|
|
|
|
|
// Only run once after hydration
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setLocalStorageInput(input);
|
|
|
|
|
}, [input, setLocalStorageInput]);
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
const handleInput = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
setInput(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const [uploadQueue, setUploadQueue] = useState<Array<string>>([]);
|
|
|
|
|
|
|
|
|
|
const submitForm = useCallback(() => {
|
2024-11-05 17:15:51 +03:00
|
|
|
window.history.replaceState({}, '', `/chat/${chatId}`);
|
|
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage({
|
|
|
|
|
role: 'user',
|
|
|
|
|
parts: [
|
|
|
|
|
...attachments.map((attachment) => ({
|
|
|
|
|
type: 'file' as const,
|
|
|
|
|
url: attachment.url,
|
|
|
|
|
name: attachment.name,
|
|
|
|
|
mediaType: attachment.contentType,
|
|
|
|
|
})),
|
|
|
|
|
{
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: input,
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-10-11 18:00:22 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setAttachments([]);
|
2024-10-31 21:02:05 +05:30
|
|
|
setLocalStorageInput('');
|
2025-01-08 17:00:38 +05:30
|
|
|
resetHeight();
|
2025-07-03 02:26:34 -07:00
|
|
|
setInput('');
|
2024-10-14 23:01:46 +05:30
|
|
|
|
|
|
|
|
if (width && width > 768) {
|
|
|
|
|
textareaRef.current?.focus();
|
|
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
}, [
|
2025-07-03 02:26:34 -07:00
|
|
|
input,
|
|
|
|
|
setInput,
|
2024-11-05 17:15:51 +03:00
|
|
|
attachments,
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage,
|
2024-11-05 17:15:51 +03:00
|
|
|
setAttachments,
|
|
|
|
|
setLocalStorageInput,
|
|
|
|
|
width,
|
|
|
|
|
chatId,
|
|
|
|
|
]);
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
const uploadFile = async (file: File) => {
|
|
|
|
|
const formData = new FormData();
|
2024-10-24 16:35:51 -04:00
|
|
|
formData.append('file', file);
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
try {
|
2024-11-15 12:18:17 -05:00
|
|
|
const response = await fetch('/api/files/upload', {
|
2024-10-24 16:35:51 -04:00
|
|
|
method: 'POST',
|
2024-10-11 18:00:22 +05:30
|
|
|
body: formData,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
const { url, pathname, contentType } = data;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
url,
|
|
|
|
|
name: pathname,
|
|
|
|
|
contentType: contentType,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-11-15 12:18:17 -05:00
|
|
|
const { error } = await response.json();
|
|
|
|
|
toast.error(error);
|
2024-10-11 18:00:22 +05:30
|
|
|
} catch (error) {
|
2024-10-24 16:35:51 -04:00
|
|
|
toast.error('Failed to upload file, please try again!');
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-08 16:56:10 +02:00
|
|
|
const modelResolver = useMemo(() => {
|
|
|
|
|
return myProvider.languageModel(selectedModelId);
|
|
|
|
|
}, [selectedModelId]);
|
|
|
|
|
|
|
|
|
|
const contextMax = useMemo(() => {
|
|
|
|
|
// Resolve from selected model; stable across chunks.
|
|
|
|
|
const cw = getContextWindow(modelResolver.modelId);
|
|
|
|
|
return cw.combinedMax ?? cw.inputMax ?? 0;
|
|
|
|
|
}, [modelResolver]);
|
|
|
|
|
|
|
|
|
|
const usedTokens = useMemo(() => {
|
|
|
|
|
// Prefer explicit usage data part captured via onData
|
|
|
|
|
if (!usage) return 0; // update only when final usage arrives
|
|
|
|
|
const n = normalizeUsage(usage);
|
|
|
|
|
return typeof n.total === 'number'
|
|
|
|
|
? n.total
|
|
|
|
|
: (n.input ?? 0) + (n.output ?? 0);
|
|
|
|
|
}, [usage]);
|
|
|
|
|
|
|
|
|
|
const contextProps = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
maxTokens: contextMax,
|
|
|
|
|
usedTokens,
|
|
|
|
|
usage,
|
|
|
|
|
modelId: modelResolver.modelId,
|
|
|
|
|
}),
|
|
|
|
|
[contextMax, usedTokens, usage, modelResolver],
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
const handleFileChange = useCallback(
|
|
|
|
|
async (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const files = Array.from(event.target.files || []);
|
|
|
|
|
|
|
|
|
|
setUploadQueue(files.map((file) => file.name));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const uploadPromises = files.map((file) => uploadFile(file));
|
|
|
|
|
const uploadedAttachments = await Promise.all(uploadPromises);
|
|
|
|
|
const successfullyUploadedAttachments = uploadedAttachments.filter(
|
2024-11-15 13:00:15 -05:00
|
|
|
(attachment) => attachment !== undefined,
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setAttachments((currentAttachments) => [
|
|
|
|
|
...currentAttachments,
|
|
|
|
|
...successfullyUploadedAttachments,
|
|
|
|
|
]);
|
|
|
|
|
} catch (error) {
|
2024-10-24 16:35:51 -04:00
|
|
|
console.error('Error uploading files!', error);
|
2024-10-11 18:00:22 +05:30
|
|
|
} finally {
|
|
|
|
|
setUploadQueue([]);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-11-15 13:00:15 -05:00
|
|
|
[setAttachments],
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
|
2025-05-01 02:28:24 -07:00
|
|
|
const { isAtBottom, scrollToBottom } = useScrollToBottom();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (status === 'submitted') {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
}, [status, scrollToBottom]);
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
2025-09-10 20:23:41 +01:00
|
|
|
<div className='flex relative flex-col gap-4 w-full'>
|
2025-05-01 02:28:24 -07:00
|
|
|
<AnimatePresence>
|
|
|
|
|
{!isAtBottom && (
|
|
|
|
|
<motion.div
|
|
|
|
|
initial={{ opacity: 0, y: 10 }}
|
|
|
|
|
animate={{ opacity: 1, y: 0 }}
|
|
|
|
|
exit={{ opacity: 0, y: 10 }}
|
|
|
|
|
transition={{ type: 'spring', stiffness: 300, damping: 20 }}
|
2025-09-10 20:23:41 +01:00
|
|
|
className='absolute -top-12 left-1/2 z-50 -translate-x-1/2'
|
2025-05-01 02:28:24 -07:00
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
data-testid="scroll-to-bottom-button"
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
size="icon"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<ArrowDown />
|
|
|
|
|
</Button>
|
|
|
|
|
</motion.div>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
{messages.length === 0 &&
|
|
|
|
|
attachments.length === 0 &&
|
|
|
|
|
uploadQueue.length === 0 && (
|
2025-05-01 17:47:48 -07:00
|
|
|
<SuggestedActions
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage={sendMessage}
|
2025-05-01 17:47:48 -07:00
|
|
|
chatId={chatId}
|
|
|
|
|
selectedVisibilityType={selectedVisibilityType}
|
|
|
|
|
/>
|
2024-10-11 18:00:22 +05:30
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<input
|
|
|
|
|
type="file"
|
2025-09-09 15:44:07 -04:00
|
|
|
className="-top-4 -left-4 pointer-events-none fixed size-0.5 opacity-0"
|
2024-10-11 18:00:22 +05:30
|
|
|
ref={fileInputRef}
|
|
|
|
|
multiple
|
|
|
|
|
onChange={handleFileChange}
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-08-28 14:15:36 +01:00
|
|
|
<PromptInput
|
2025-09-10 20:23:41 +01:00
|
|
|
className='p-3 rounded-xl border transition-all duration-200 border-border bg-background shadow-xs focus-within:border-border hover:border-muted-foreground/50'
|
2025-08-28 14:15:36 +01:00
|
|
|
onSubmit={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (status !== 'ready') {
|
|
|
|
|
toast.error('Please wait for the model to finish its response!');
|
|
|
|
|
} else {
|
|
|
|
|
submitForm();
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
}}
|
2025-08-28 14:15:36 +01:00
|
|
|
>
|
|
|
|
|
{(attachments.length > 0 || uploadQueue.length > 0) && (
|
|
|
|
|
<div
|
|
|
|
|
data-testid="attachments-preview"
|
2025-09-10 20:23:41 +01:00
|
|
|
className='flex overflow-x-scroll flex-row gap-2 items-end'
|
2025-08-28 14:15:36 +01:00
|
|
|
>
|
|
|
|
|
{attachments.map((attachment) => (
|
|
|
|
|
<PreviewAttachment
|
|
|
|
|
key={attachment.url}
|
|
|
|
|
attachment={attachment}
|
|
|
|
|
onRemove={() => {
|
|
|
|
|
setAttachments((currentAttachments) =>
|
|
|
|
|
currentAttachments.filter((a) => a.url !== attachment.url),
|
|
|
|
|
);
|
|
|
|
|
if (fileInputRef.current) {
|
|
|
|
|
fileInputRef.current.value = '';
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
{uploadQueue.map((filename) => (
|
|
|
|
|
<PreviewAttachment
|
|
|
|
|
key={filename}
|
|
|
|
|
attachment={{
|
|
|
|
|
url: '',
|
|
|
|
|
name: filename,
|
|
|
|
|
contentType: '',
|
|
|
|
|
}}
|
|
|
|
|
isUploading={true}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2024-12-17 15:36:51 +05:30
|
|
|
)}
|
2025-09-10 20:23:41 +01:00
|
|
|
<div className='flex flex-row gap-1 items-start sm:gap-2'>
|
2025-09-09 00:29:04 +02:00
|
|
|
<PromptInputTextarea
|
|
|
|
|
data-testid="multimodal-input"
|
|
|
|
|
ref={textareaRef}
|
|
|
|
|
placeholder="Send a message..."
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
minHeight={44}
|
|
|
|
|
maxHeight={200}
|
|
|
|
|
disableAutoResize={true}
|
2025-09-10 20:23:41 +01:00
|
|
|
className='grow resize-none border-0! p-2 border-none! bg-transparent text-sm outline-none ring-0 [-ms-overflow-style:none] [scrollbar-width:none] placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0 [&::-webkit-scrollbar]:hidden'
|
2025-09-09 00:29:04 +02:00
|
|
|
rows={1}
|
|
|
|
|
autoFocus
|
|
|
|
|
/>{' '}
|
2025-09-10 20:23:41 +01:00
|
|
|
<Context {...contextProps} />
|
2025-09-09 00:29:04 +02:00
|
|
|
</div>
|
2025-09-10 20:23:41 +01:00
|
|
|
<PromptInputToolbar className='!border-top-0 border-t-0! p-0 shadow-none dark:border-0 dark:border-transparent!'>
|
2025-09-09 23:58:40 +01:00
|
|
|
<PromptInputTools className="gap-0 sm:gap-0.5">
|
2025-09-08 16:56:10 +02:00
|
|
|
<AttachmentsButton
|
|
|
|
|
fileInputRef={fileInputRef}
|
|
|
|
|
status={status}
|
|
|
|
|
selectedModelId={selectedModelId}
|
|
|
|
|
/>
|
2025-09-01 11:07:07 +01:00
|
|
|
<ModelSelectorCompact selectedModelId={selectedModelId} />
|
2025-08-28 14:15:36 +01:00
|
|
|
</PromptInputTools>
|
2025-09-09 00:29:04 +02:00
|
|
|
|
2025-08-28 14:15:36 +01:00
|
|
|
{status === 'submitted' ? (
|
|
|
|
|
<StopButton stop={stop} setMessages={setMessages} />
|
|
|
|
|
) : (
|
|
|
|
|
<PromptInputSubmit
|
|
|
|
|
status={status}
|
|
|
|
|
disabled={!input.trim() || uploadQueue.length > 0}
|
2025-09-10 20:23:41 +01:00
|
|
|
className="rounded-full transition-colors duration-200 size-8 bg-primary text-primary-foreground hover:bg-primary/90 disabled:bg-muted disabled:text-muted-foreground"
|
2025-09-01 16:36:28 +01:00
|
|
|
>
|
2025-09-09 22:19:34 +01:00
|
|
|
<ArrowUpIcon size={14} />
|
2025-09-01 16:36:28 +01:00
|
|
|
</PromptInputSubmit>
|
2025-08-28 14:15:36 +01:00
|
|
|
)}
|
|
|
|
|
</PromptInputToolbar>
|
|
|
|
|
</PromptInput>
|
2024-10-11 18:00:22 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-12-03 17:49:38 +03:00
|
|
|
|
|
|
|
|
export const MultimodalInput = memo(
|
|
|
|
|
PureMultimodalInput,
|
2024-12-04 17:27:48 +03:00
|
|
|
(prevProps, nextProps) => {
|
|
|
|
|
if (prevProps.input !== nextProps.input) return false;
|
2025-03-11 15:33:18 -07:00
|
|
|
if (prevProps.status !== nextProps.status) return false;
|
2024-12-04 17:27:48 +03:00
|
|
|
if (!equal(prevProps.attachments, nextProps.attachments)) return false;
|
2025-05-01 17:47:48 -07:00
|
|
|
if (prevProps.selectedVisibilityType !== nextProps.selectedVisibilityType)
|
|
|
|
|
return false;
|
2025-09-01 11:07:07 +01:00
|
|
|
if (prevProps.selectedModelId !== nextProps.selectedModelId) return false;
|
2024-12-03 17:49:38 +03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-12-10 17:54:10 +05:30
|
|
|
|
|
|
|
|
function PureAttachmentsButton({
|
|
|
|
|
fileInputRef,
|
2025-03-11 15:33:18 -07:00
|
|
|
status,
|
2025-09-08 11:08:45 +01:00
|
|
|
selectedModelId,
|
2024-12-10 17:54:10 +05:30
|
|
|
}: {
|
|
|
|
|
fileInputRef: React.MutableRefObject<HTMLInputElement | null>;
|
2025-07-03 02:26:34 -07:00
|
|
|
status: UseChatHelpers<ChatMessage>['status'];
|
2025-09-08 11:08:45 +01:00
|
|
|
selectedModelId: string;
|
2024-12-10 17:54:10 +05:30
|
|
|
}) {
|
2025-09-08 11:08:45 +01:00
|
|
|
const isReasoningModel = selectedModelId === 'chat-model-reasoning';
|
2025-09-08 16:56:10 +02:00
|
|
|
|
2024-12-10 17:54:10 +05:30
|
|
|
return (
|
|
|
|
|
<Button
|
2025-03-04 17:25:46 -08:00
|
|
|
data-testid="attachments-button"
|
2025-09-10 20:23:41 +01:00
|
|
|
className='p-1 h-8 rounded-lg transition-colors aspect-square hover:bg-accent'
|
2024-12-10 17:54:10 +05:30
|
|
|
onClick={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
fileInputRef.current?.click();
|
|
|
|
|
}}
|
2025-09-08 11:08:45 +01:00
|
|
|
disabled={status !== 'ready' || isReasoningModel}
|
2024-12-17 15:36:51 +05:30
|
|
|
variant="ghost"
|
2024-12-10 17:54:10 +05:30
|
|
|
>
|
2025-09-09 22:19:34 +01:00
|
|
|
<PaperclipIcon size={14} style={{ width: 14, height: 14 }} />
|
2024-12-10 17:54:10 +05:30
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AttachmentsButton = memo(PureAttachmentsButton);
|
|
|
|
|
|
2025-09-01 11:07:07 +01:00
|
|
|
function PureModelSelectorCompact({
|
|
|
|
|
selectedModelId,
|
|
|
|
|
}: {
|
|
|
|
|
selectedModelId: string;
|
|
|
|
|
}) {
|
|
|
|
|
const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId);
|
|
|
|
|
|
2025-09-01 16:36:28 +01:00
|
|
|
const selectedModel = chatModels.find(
|
|
|
|
|
(model) => model.id === optimisticModelId,
|
|
|
|
|
);
|
2025-09-01 11:07:07 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<PromptInputModelSelect
|
|
|
|
|
value={selectedModel?.name}
|
|
|
|
|
onValueChange={(modelName) => {
|
2025-09-01 16:36:28 +01:00
|
|
|
const model = chatModels.find((m) => m.name === modelName);
|
2025-09-01 11:07:07 +01:00
|
|
|
if (model) {
|
|
|
|
|
setOptimisticModelId(model.id);
|
|
|
|
|
startTransition(() => {
|
|
|
|
|
saveChatModelAsCookie(model.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-09-09 23:58:40 +01:00
|
|
|
<SelectPrimitive.Trigger
|
2025-09-01 11:07:07 +01:00
|
|
|
type="button"
|
2025-09-10 20:23:41 +01:00
|
|
|
className='flex gap-2 items-center px-2 h-8 rounded-lg border-0 shadow-none transition-colors bg-background text-foreground hover:bg-accent focus:outline-none focus:ring-0 focus-visible:ring-0 focus-visible:ring-offset-0'
|
2025-09-01 11:07:07 +01:00
|
|
|
>
|
2025-09-09 23:58:40 +01:00
|
|
|
<CpuIcon size={16} />
|
2025-09-10 20:23:41 +01:00
|
|
|
<span className="hidden text-xs font-medium sm:block">{selectedModel?.name}</span>
|
2025-09-09 23:58:40 +01:00
|
|
|
<ChevronDownIcon size={16} />
|
|
|
|
|
</SelectPrimitive.Trigger>
|
|
|
|
|
<PromptInputModelSelectContent className="min-w-[260px] p-0">
|
2025-09-10 20:23:41 +01:00
|
|
|
<div className="flex flex-col gap-px">
|
2025-09-01 11:07:07 +01:00
|
|
|
{chatModels.map((model) => (
|
2025-09-09 23:58:40 +01:00
|
|
|
<SelectItem key={model.id} value={model.name} className="px-3 py-2 text-xs">
|
2025-09-10 20:23:41 +01:00
|
|
|
<div className="flex flex-col flex-1 gap-1 min-w-0">
|
|
|
|
|
<div className="text-xs font-medium truncate">
|
2025-09-09 23:58:40 +01:00
|
|
|
{model.name}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-[10px] text-muted-foreground truncate leading-tight">
|
2025-09-01 16:36:28 +01:00
|
|
|
{model.description}
|
|
|
|
|
</div>
|
2025-09-01 11:07:07 +01:00
|
|
|
</div>
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
2025-09-10 20:23:41 +01:00
|
|
|
</div>
|
2025-09-01 11:07:07 +01:00
|
|
|
</PromptInputModelSelectContent>
|
|
|
|
|
</PromptInputModelSelect>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ModelSelectorCompact = memo(PureModelSelectorCompact);
|
|
|
|
|
|
2024-12-10 17:54:10 +05:30
|
|
|
function PureStopButton({
|
|
|
|
|
stop,
|
|
|
|
|
setMessages,
|
|
|
|
|
}: {
|
|
|
|
|
stop: () => void;
|
2025-07-03 02:26:34 -07:00
|
|
|
setMessages: UseChatHelpers<ChatMessage>['setMessages'];
|
2024-12-10 17:54:10 +05:30
|
|
|
}) {
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
2025-03-04 17:25:46 -08:00
|
|
|
data-testid="stop-button"
|
2025-09-10 20:23:41 +01:00
|
|
|
className="p-1 rounded-full transition-colors duration-200 size-7 bg-foreground text-background hover:bg-foreground/90 disabled:bg-muted disabled:text-muted-foreground"
|
2024-12-10 17:54:10 +05:30
|
|
|
onClick={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
stop();
|
2025-03-16 18:42:29 -07:00
|
|
|
setMessages((messages) => messages);
|
2024-12-10 17:54:10 +05:30
|
|
|
}}
|
|
|
|
|
>
|
2025-09-09 22:19:34 +01:00
|
|
|
<StopIcon size={14} />
|
2024-12-10 17:54:10 +05:30
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 20:23:41 +01:00
|
|
|
const StopButton = memo(PureStopButton);
|