"use client"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@/components/ui/command"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { HoverCard, HoverCardContent, HoverCardTrigger, } from "@/components/ui/hover-card"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupTextarea, } from "@/components/ui/input-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { PromptInputFilePart, UploadPolicy } from "@/core/uploads"; import { DEFAULT_UPLOAD_POLICY, splitUnsupportedUploadFiles, useUploadPolicy, validateChatUploadFiles, } from "@/core/uploads"; import { isIMEComposing } from "@/lib/ime"; import { cn } from "@/lib/utils"; import type { ChatStatus } from "ai"; import { ArrowUpIcon, ImageIcon, Loader2Icon, MicIcon, PaperclipIcon, PlusIcon, SquareIcon, UploadIcon, XIcon, } from "lucide-react"; import { nanoid } from "nanoid"; import { type ChangeEvent, type ChangeEventHandler, Children, type ClipboardEventHandler, type ComponentProps, createContext, type FormEvent, type FormEventHandler, type HTMLAttributes, type KeyboardEventHandler, type PropsWithChildren, type ReactNode, type RefObject, useCallback, useContext, useEffect, useMemo, useRef, useState, } from "react"; import { toast } from "sonner"; // ============================================================================ // Provider Context & Types // ============================================================================ export type AttachmentsContext = { files: (PromptInputFilePart & { id: string })[]; add: (files: File[] | FileList) => void; remove: (id: string) => void; clear: () => void; openFileDialog: () => void; fileInputRef: RefObject; }; export type TextInputContext = { value: string; setInput: (v: string) => void; clear: () => void; }; export type PromptInputControllerProps = { textInput: TextInputContext; attachments: AttachmentsContext; /** INTERNAL: Allows PromptInput to register its file textInput + "open" callback */ __registerFileInput: ( ref: RefObject, open: () => void, ) => void; }; const PromptInputController = createContext( null, ); const ProviderAttachmentsContext = createContext( null, ); const PromptInputValidationContext = createContext< ((files: File[] | FileList) => File[]) | null >(null); export const usePromptInputController = () => { const ctx = useContext(PromptInputController); if (!ctx) { throw new Error( "Wrap your component inside to use usePromptInputController().", ); } return ctx; }; // Optional variants (do NOT throw). Useful for dual-mode components and for // Fast Refresh transitions where a page can briefly retain the previous // provider context instance. export const useOptionalPromptInputController = () => useContext(PromptInputController); export const useProviderAttachments = () => { const ctx = useContext(ProviderAttachmentsContext); if (!ctx) { throw new Error( "Wrap your component inside to use useProviderAttachments().", ); } return ctx; }; const useOptionalProviderAttachments = () => useContext(ProviderAttachmentsContext); const usePromptInputValidation = () => useContext(PromptInputValidationContext); export type PromptInputProviderProps = PropsWithChildren<{ initialInput?: string; uploadPolicy?: UploadPolicy; }>; /** * Optional global provider that lifts PromptInput state outside of PromptInput. * If you don't use it, PromptInput stays fully self-managed. */ export function PromptInputProvider({ initialInput: initialTextInput = "", uploadPolicy, children, }: PromptInputProviderProps) { const { data: fetchedUploadPolicy } = useUploadPolicy(); const effectiveUploadPolicy = uploadPolicy ?? fetchedUploadPolicy ?? DEFAULT_UPLOAD_POLICY; // ----- textInput state const [textInput, setTextInput] = useState(initialTextInput); const clearInput = useCallback(() => setTextInput(""), []); // ----- attachments state (global when wrapped) const [attachmentFiles, setAttachmentFiles] = useState< (PromptInputFilePart & { id: string })[] >([]); const attachmentsRef = useRef(attachmentFiles); const fileInputRef = useRef(null); const openRef = useRef<() => void>(() => {}); const add = useCallback((files: File[] | FileList) => { const incoming = Array.from(files); if (incoming.length === 0) { return; } const current = attachmentsRef.current; const existing = current.map((item) => ({ name: item.filename ?? "attachment", size: item.file?.size ?? 0, type: item.mediaType ?? "", })); const validation = validateChatUploadFiles( incoming, existing, effectiveUploadPolicy, ); for (const message of new Set( validation.issues.map((issue) => issue.message), )) { toast.error(message); } if (validation.accepted.length === 0) return; const next = current.concat( validation.accepted.map((file) => ({ id: nanoid(), type: "file" as const, url: URL.createObjectURL(file), mediaType: file.type, filename: file.name, file, })), ); attachmentsRef.current = next; setAttachmentFiles(next); }, [effectiveUploadPolicy]); const remove = useCallback((id: string) => { const current = attachmentsRef.current; const found = current.find((f) => f.id === id); if (found?.url) URL.revokeObjectURL(found.url); const next = current.filter((f) => f.id !== id); attachmentsRef.current = next; setAttachmentFiles(next); }, []); const clear = useCallback(() => { for (const f of attachmentsRef.current) { if (f.url) { URL.revokeObjectURL(f.url); } } attachmentsRef.current = []; setAttachmentFiles([]); }, []); // Keep a ref to attachments for cleanup on unmount (avoids stale closure) attachmentsRef.current = attachmentFiles; // Cleanup blob URLs on unmount to prevent memory leaks useEffect(() => { return () => { for (const f of attachmentsRef.current) { if (f.url) { URL.revokeObjectURL(f.url); } } }; }, []); const openFileDialog = useCallback(() => { openRef.current?.(); }, []); const attachments = useMemo( () => ({ files: attachmentFiles, add, remove, clear, openFileDialog, fileInputRef, }), [attachmentFiles, add, remove, clear, openFileDialog], ); const __registerFileInput = useCallback( (ref: RefObject, open: () => void) => { fileInputRef.current = ref.current; openRef.current = open; }, [], ); const controller = useMemo( () => ({ textInput: { value: textInput, setInput: setTextInput, clear: clearInput, }, attachments, __registerFileInput, }), [textInput, clearInput, attachments, __registerFileInput], ); return ( {children} ); } // ============================================================================ // Component Context & Hooks // ============================================================================ const LocalAttachmentsContext = createContext(null); export const usePromptInputAttachments = () => { // Dual-mode: prefer provider if present, otherwise use local const provider = useOptionalProviderAttachments(); const local = useContext(LocalAttachmentsContext); const context = provider ?? local; if (!context) { throw new Error( "usePromptInputAttachments must be used within a PromptInput or PromptInputProvider", ); } return context; }; export type PromptInputAttachmentProps = HTMLAttributes & { data: PromptInputFilePart & { id: string }; className?: string; }; export function PromptInputAttachment({ data, className, ...props }: PromptInputAttachmentProps) { const attachments = usePromptInputAttachments(); const filename = data.filename || ""; const mediaType = data.mediaType?.startsWith("image/") && data.url ? "image" : "file"; const isImage = mediaType === "image"; const attachmentLabel = filename || (isImage ? "Image" : "Attachment"); return (
{isImage ? ( {filename ) : (
)}
{attachmentLabel}
{isImage && (
{filename
)}

{filename || (isImage ? "Image" : "Attachment")}

{data.mediaType && (

{data.mediaType}

)}
); } export type PromptInputAttachmentsProps = Omit< HTMLAttributes, "children" > & { children: (attachment: PromptInputFilePart & { id: string }) => ReactNode; }; export function PromptInputAttachments({ children, className, ...props }: PromptInputAttachmentsProps) { const attachments = usePromptInputAttachments(); if (!attachments.files.length) { return null; } return (
{attachments.files.map((file) => (
{children(file)}
))}
); } export type PromptInputActionAddAttachmentsProps = ComponentProps< typeof DropdownMenuItem > & { label?: string; }; export const PromptInputActionAddAttachments = ({ label = "Add photos or files", ...props }: PromptInputActionAddAttachmentsProps) => { const attachments = usePromptInputAttachments(); return ( { e.preventDefault(); attachments.openFileDialog(); }} > {label} ); }; export type PromptInputMessage = { text: string; files: PromptInputFilePart[]; }; export type PromptInputProps = Omit< HTMLAttributes, "onSubmit" | "onError" > & { accept?: string; // e.g., "image/*" or leave undefined for any disabled?: boolean; multiple?: boolean; // When true, accepts drops anywhere on document. Default false (opt-in). globalDrop?: boolean; // Render a hidden input with given name and keep it in sync for native form posts. Default false. syncHiddenInput?: boolean; // Minimal constraints maxFiles?: number; maxFileSize?: number; // bytes uploadPolicy?: UploadPolicy; onError?: (err: { code: | "max_files" | "max_file_size" | "max_total_size" | "unsupported_type" | "accept" | "unsupported_package"; message: string; }) => void; onSubmit: ( message: PromptInputMessage, event: FormEvent, ) => void | Promise; }; export const PromptInput = ({ className, accept, disabled, multiple, globalDrop, syncHiddenInput, maxFiles, maxFileSize, uploadPolicy, onError, onSubmit, children, ...props }: PromptInputProps) => { const { data: fetchedUploadPolicy } = useUploadPolicy(); const effectiveUploadPolicy = uploadPolicy ?? fetchedUploadPolicy ?? DEFAULT_UPLOAD_POLICY; const policyAccept = useMemo( () => Object.values(effectiveUploadPolicy.groups) .flatMap((group) => group.extensions) .join(","), [effectiveUploadPolicy], ); // Try to use a provider controller if present const controller = useOptionalPromptInputController(); const usingProvider = !!controller; // Refs const inputRef = useRef(null); const formRef = useRef(null); // ----- Local attachments (only used when no provider) const [items, setItems] = useState<(PromptInputFilePart & { id: string })[]>( [], ); const files = usingProvider ? controller.attachments.files : items; // Keep a ref to files for cleanup on unmount (avoids stale closure) const filesRef = useRef(files); filesRef.current = files; const openFileDialogLocal = useCallback(() => { inputRef.current?.click(); }, []); const matchesAccept = useCallback( (f: File) => { if (!accept || accept.trim() === "") { return true; } const patterns = accept .split(",") .map((s) => s.trim()) .filter(Boolean); return patterns.some((pattern) => { if (pattern.startsWith(".")) { return f.name.toLowerCase().endsWith(pattern.toLowerCase()); } if (pattern.endsWith("/*")) { const prefix = pattern.slice(0, -1); // e.g: image/* -> image/ return f.type.startsWith(prefix); } return f.type === pattern; }); }, [accept], ); const addLocal = useCallback( (fileList: File[] | FileList) => { const incoming = Array.from(fileList); const accepted = incoming.filter((f) => matchesAccept(f)); if (accepted.length < incoming.length) { const error = { code: "accept", message: "部分文件不符合当前输入框允许的格式。", } as const; onError?.(error); if (!onError) toast.error(error.message); } const localPolicy: UploadPolicy = { ...effectiveUploadPolicy, max_files_per_message: typeof maxFiles === "number" ? Math.min(effectiveUploadPolicy.max_files_per_message, maxFiles) : effectiveUploadPolicy.max_files_per_message, groups: Object.fromEntries( Object.entries(effectiveUploadPolicy.groups).map(([key, group]) => [ key, { ...group, max_bytes: typeof maxFileSize === "number" ? Math.min(group.max_bytes, maxFileSize) : group.max_bytes, }, ]), ) as UploadPolicy["groups"], }; const existing = items.map((item) => ({ name: item.filename ?? "attachment", size: item.file?.size ?? 0, type: item.mediaType ?? "", })); const validation = validateChatUploadFiles( accepted, existing, localPolicy, ); for (const issue of Array.from( new Map( validation.issues.map((item) => [item.message, item]), ).values(), )) { onError?.({ code: issue.code, message: issue.message }); if (!onError) toast.error(issue.message); } if (validation.accepted.length === 0) return; setItems((prev) => prev.concat( validation.accepted.map((file) => ({ id: nanoid(), type: "file" as const, url: URL.createObjectURL(file), mediaType: file.type, filename: file.name, file, })), ), ); }, [effectiveUploadPolicy, items, matchesAccept, maxFiles, maxFileSize, onError], ); const removeLocal = useCallback( (id: string) => setItems((prev) => { const found = prev.find((file) => file.id === id); if (found?.url) { URL.revokeObjectURL(found.url); } return prev.filter((file) => file.id !== id); }), [], ); const clearLocal = useCallback( () => setItems((prev) => { for (const file of prev) { if (file.url) { URL.revokeObjectURL(file.url); } } return []; }), [], ); const add = usingProvider ? controller.attachments.add : addLocal; const remove = usingProvider ? controller.attachments.remove : removeLocal; const clear = usingProvider ? controller.attachments.clear : clearLocal; const openFileDialog = usingProvider ? controller.attachments.openFileDialog : openFileDialogLocal; const sanitizeIncomingFiles = useCallback( (fileList: File[] | FileList) => { const { accepted, message } = splitUnsupportedUploadFiles(fileList); if (message) { onError?.({ code: "unsupported_package", message, }); if (!onError) { toast.error(message); } } return accepted; }, [onError], ); // Let provider know about our hidden file input so external menus can call openFileDialog() useEffect(() => { if (!usingProvider) return; controller.__registerFileInput(inputRef, () => inputRef.current?.click()); }, [usingProvider, controller]); // Note: File input cannot be programmatically set for security reasons // The syncHiddenInput prop is no longer functional useEffect(() => { if (syncHiddenInput && inputRef.current && files.length === 0) { inputRef.current.value = ""; } }, [files, syncHiddenInput]); // Attach drop handlers on nearest form and document (opt-in) useEffect(() => { const form = formRef.current; if (!form) return; if (globalDrop) return; // when global drop is on, let the document-level handler own drops const onDragOver = (e: DragEvent) => { if (e.dataTransfer?.types?.includes("Files")) { e.preventDefault(); } }; const onDrop = (e: DragEvent) => { if (e.dataTransfer?.types?.includes("Files")) { e.preventDefault(); } if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) { const accepted = sanitizeIncomingFiles(e.dataTransfer.files); if (accepted.length > 0) { add(accepted); } } }; form.addEventListener("dragover", onDragOver); form.addEventListener("drop", onDrop); return () => { form.removeEventListener("dragover", onDragOver); form.removeEventListener("drop", onDrop); }; }, [add, globalDrop, sanitizeIncomingFiles]); useEffect(() => { if (!globalDrop) return; // Suppress the global chat-upload handler when the drop happens inside an open // modal dialog or popover — those owners (e.g. the skill-install dialog) handle // their own file uploads and should not leak into the active chat thread. const isInsideModal = (target: EventTarget | null): boolean => { if (!(target instanceof Element)) return false; return target.closest('[role="dialog"], [role="alertdialog"]') !== null; }; const onDragOver = (e: DragEvent) => { if (isInsideModal(e.target)) return; if (e.dataTransfer?.types?.includes("Files")) { e.preventDefault(); } }; const onDrop = (e: DragEvent) => { if (isInsideModal(e.target)) return; if (e.dataTransfer?.types?.includes("Files")) { e.preventDefault(); } if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) { const accepted = sanitizeIncomingFiles(e.dataTransfer.files); if (accepted.length > 0) { add(accepted); } } }; document.addEventListener("dragover", onDragOver); document.addEventListener("drop", onDrop); return () => { document.removeEventListener("dragover", onDragOver); document.removeEventListener("drop", onDrop); }; }, [add, globalDrop, sanitizeIncomingFiles]); useEffect( () => () => { if (!usingProvider) { for (const f of filesRef.current) { if (f.url) URL.revokeObjectURL(f.url); } } }, // eslint-disable-next-line react-hooks/exhaustive-deps -- cleanup only on unmount; filesRef always current [usingProvider], ); const handleChange: ChangeEventHandler = (event) => { if (event.currentTarget.files) { const accepted = sanitizeIncomingFiles(event.currentTarget.files); if (accepted.length > 0) { add(accepted); } } // Reset input value to allow selecting files that were previously removed event.currentTarget.value = ""; }; const convertBlobUrlToDataUrl = async ( url: string, ): Promise => { try { const response = await fetch(url); const blob = await response.blob(); return new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve(reader.result as string); reader.onerror = () => resolve(null); reader.readAsDataURL(blob); }); } catch { return null; } }; const ctx = useMemo( () => ({ files: files.map((item) => ({ ...item, id: item.id })), add, remove, clear, openFileDialog, fileInputRef: inputRef, }), [files, add, remove, clear, openFileDialog], ); const handleSubmit: FormEventHandler = (event) => { event.preventDefault(); const form = event.currentTarget; const text = usingProvider ? controller.textInput.value : (() => { const formData = new FormData(form); return (formData.get("message") as string) || ""; })(); // Reset form immediately after capturing text to avoid race condition // where user input during async blob conversion would be lost if (!usingProvider) { form.reset(); } // Convert blob URLs to data URLs asynchronously Promise.all( files.map(async ({ id, ...item }) => { if (item.file instanceof File) { // Downstream upload prep reads the preserved File directly. return item; } if (item.url && item.url.startsWith("blob:")) { const dataUrl = await convertBlobUrlToDataUrl(item.url); // If conversion failed, keep the original blob URL return { ...item, url: dataUrl ?? item.url, }; } return item; }), ) .then((convertedFiles: PromptInputFilePart[]) => { try { const result = onSubmit({ text, files: convertedFiles }, event); // Handle both sync and async onSubmit if (result instanceof Promise) { result .then(() => { clear(); if (usingProvider) { controller.textInput.clear(); } }) .catch(() => { // Don't clear on error - user may want to retry }); } else { // Sync function completed without throwing, clear attachments clear(); if (usingProvider) { controller.textInput.clear(); } } } catch { // Don't clear on error - user may want to retry } }) .catch(() => { // Don't clear on error - user may want to retry }); }; // Render with or without local provider const inner = (
{/* Do not rely on InputGroup's :has(textarea) selector here: Chrome versions before 105 ignore :has(), retain h-9, and clip the whole multi-line composer to a single row. */} {children}
); return usingProvider ? ( inner ) : ( {inner} ); }; export type PromptInputBodyProps = HTMLAttributes; export const PromptInputBody = ({ className, ...props }: PromptInputBodyProps) => (
); export type PromptInputTextareaProps = ComponentProps< typeof InputGroupTextarea >; export const PromptInputTextarea = ({ onChange, onInput, className, placeholder = "What would you like to know?", ...props }: PromptInputTextareaProps) => { const controller = useOptionalPromptInputController(); const attachments = usePromptInputAttachments(); const sanitizeIncomingFiles = usePromptInputValidation(); const [isComposing, setIsComposing] = useState(false); const textareaRef = useRef(null); const resizeForLegacyChrome = useCallback((textarea: HTMLTextAreaElement) => { if ( typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("field-sizing", "content") ) { return; } // Chrome 109 ignores field-sizing: content. Preserve the same 4rem–12rem // autosize range used by min-h-16/max-h-48 instead of leaving the prompt // stuck at its minimum height. textarea.style.height = "auto"; const height = Math.max(64, Math.min(textarea.scrollHeight, 192)); textarea.style.height = `${height}px`; textarea.style.overflowY = textarea.scrollHeight > 192 ? "auto" : "hidden"; }, []); // onInput does not run for a controlled value filled by a suggested question // or restored draft. Resize once after every render in legacy Chrome too. useEffect(() => { const textarea = textareaRef.current; if (!textarea) return; const frame = window.requestAnimationFrame(() => { resizeForLegacyChrome(textarea); }); return () => window.cancelAnimationFrame(frame); }, [controller?.textInput.value, resizeForLegacyChrome]); const handleKeyDown: KeyboardEventHandler = (e) => { if (e.key === "Enter") { if (isIMEComposing(e, isComposing)) { return; } if (e.shiftKey) { return; } e.preventDefault(); // Check if the submit button is disabled before submitting const form = e.currentTarget.form; const submitButton = form?.querySelector( 'button[type="submit"]', ) as HTMLButtonElement | null; if (submitButton?.disabled) { return; } form?.requestSubmit(); } // Remove last attachment when Backspace is pressed and textarea is empty if ( e.key === "Backspace" && e.currentTarget.value === "" && attachments.files.length > 0 ) { e.preventDefault(); const lastAttachment = attachments.files.at(-1); if (lastAttachment) { attachments.remove(lastAttachment.id); } } }; const handlePaste: ClipboardEventHandler = (event) => { const items = event.clipboardData?.items; if (!items) { return; } const files: File[] = []; for (const item of items) { if (item.kind === "file") { const file = item.getAsFile(); if (file) { files.push(file); } } } if (files.length > 0) { event.preventDefault(); const accepted = sanitizeIncomingFiles ? sanitizeIncomingFiles(files) : files; if (accepted.length > 0) { attachments.add(accepted); } } }; const controlledProps = controller ? { value: controller.textInput.value, onChange: (e: ChangeEvent) => { controller.textInput.setInput(e.currentTarget.value); onChange?.(e); }, } : { onChange, }; return ( setIsComposing(false)} onCompositionStart={() => setIsComposing(true)} onKeyDown={handleKeyDown} onInput={(event) => { resizeForLegacyChrome(event.currentTarget); onInput?.(event); }} onPaste={handlePaste} placeholder={placeholder} {...props} {...controlledProps} /> ); }; export type PromptInputHeaderProps = Omit< ComponentProps, "align" >; export const PromptInputHeader = ({ className, ...props }: PromptInputHeaderProps) => ( ); export type PromptInputFooterProps = Omit< ComponentProps, "align" >; export const PromptInputFooter = ({ className, ...props }: PromptInputFooterProps) => ( ); export type PromptInputToolsProps = HTMLAttributes; export const PromptInputTools = ({ className, ...props }: PromptInputToolsProps) => (
); export type PromptInputButtonProps = ComponentProps; export const PromptInputButton = ({ variant = "ghost", className, size, ...props }: PromptInputButtonProps) => { return ( ); }; export type PromptInputActionMenuProps = ComponentProps; export const PromptInputActionMenu = (props: PromptInputActionMenuProps) => ( ); export type PromptInputActionMenuTriggerProps = PromptInputButtonProps; export const PromptInputActionMenuTrigger = ({ className, children, ...props }: PromptInputActionMenuTriggerProps) => ( {children ?? } ); export type PromptInputActionMenuContentProps = ComponentProps< typeof DropdownMenuContent >; export const PromptInputActionMenuContent = ({ className, ...props }: PromptInputActionMenuContentProps) => ( ); export type PromptInputActionMenuItemProps = ComponentProps< typeof DropdownMenuItem >; export const PromptInputActionMenuItem = ({ className, ...props }: PromptInputActionMenuItemProps) => ( ); // Note: Actions that perform side-effects (like opening a file dialog) // are provided in opt-in modules (e.g., prompt-input-attachments). export type PromptInputSubmitProps = ComponentProps & { status?: ChatStatus; }; export const PromptInputSubmit = ({ className, variant = "default", size = "icon-sm", status, children, ...props }: PromptInputSubmitProps) => { let Icon = ; if (status === "submitted") { Icon = ; } else if (status === "streaming") { Icon = ; } else if (status === "error") { Icon = ; } return ( {children ?? Icon} ); }; interface SpeechRecognition extends EventTarget { continuous: boolean; interimResults: boolean; lang: string; start(): void; stop(): void; onstart: ((this: SpeechRecognition, ev: Event) => any) | null; onend: ((this: SpeechRecognition, ev: Event) => any) | null; onresult: | ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => any) | null; onerror: | ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => any) | null; } interface SpeechRecognitionEvent extends Event { results: SpeechRecognitionResultList; resultIndex: number; } type SpeechRecognitionResultList = { readonly length: number; item(index: number): SpeechRecognitionResult; [index: number]: SpeechRecognitionResult; }; type SpeechRecognitionResult = { readonly length: number; item(index: number): SpeechRecognitionAlternative; [index: number]: SpeechRecognitionAlternative; isFinal: boolean; }; type SpeechRecognitionAlternative = { transcript: string; confidence: number; }; interface SpeechRecognitionErrorEvent extends Event { error: string; } declare global { interface Window { SpeechRecognition: { new (): SpeechRecognition; }; webkitSpeechRecognition: { new (): SpeechRecognition; }; } } export type PromptInputSpeechButtonProps = ComponentProps< typeof PromptInputButton > & { textareaRef?: RefObject; onTranscriptionChange?: (text: string) => void; }; export const PromptInputSpeechButton = ({ className, textareaRef, onTranscriptionChange, ...props }: PromptInputSpeechButtonProps) => { const [isListening, setIsListening] = useState(false); const [recognition, setRecognition] = useState( null, ); const recognitionRef = useRef(null); const callbacksRef = useRef({ textareaRef, onTranscriptionChange }); callbacksRef.current = { textareaRef, onTranscriptionChange }; useEffect(() => { if ( typeof window !== "undefined" && ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) ) { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const speechRecognition = new SpeechRecognition(); speechRecognition.continuous = true; speechRecognition.interimResults = true; speechRecognition.lang = "en-US"; speechRecognition.onstart = () => { setIsListening(true); }; speechRecognition.onend = () => { setIsListening(false); }; speechRecognition.onresult = (event) => { let finalTranscript = ""; for (let i = event.resultIndex; i < event.results.length; i++) { const result = event.results[i]; if (result?.isFinal) { finalTranscript += result[0]?.transcript ?? ""; } } const currentTextareaRef = callbacksRef.current.textareaRef; const currentOnTranscriptionChange = callbacksRef.current.onTranscriptionChange; if (finalTranscript && currentTextareaRef?.current) { const textarea = currentTextareaRef.current; const currentValue = textarea.value; const newValue = currentValue + (currentValue ? " " : "") + finalTranscript; textarea.value = newValue; textarea.dispatchEvent(new Event("input", { bubbles: true })); currentOnTranscriptionChange?.(newValue); } }; speechRecognition.onerror = (event) => { console.error("Speech recognition error:", event.error); setIsListening(false); }; recognitionRef.current = speechRecognition; setRecognition(speechRecognition); } return () => { if (recognitionRef.current) { recognitionRef.current.stop(); } }; }, []); const toggleListening = useCallback(() => { if (!recognition) { return; } if (isListening) { recognition.stop(); } else { recognition.start(); } }, [recognition, isListening]); return ( ); }; export type PromptInputSelectProps = ComponentProps; export const PromptInputSelect = (props: PromptInputSelectProps) => (