// SPDX-License-Identifier: MIT // Copyright contributors to the openassistant project import React, { useCallback, useEffect, useState } from 'react'; import { Button, Tooltip, ScrollShadow, Badge } from '@heroui/react'; import { Icon } from '@iconify/react'; import PromptInput from './prompt-input'; import VoiceChatButton from './voice-chat-button'; type PromptInputWithBottomActionsProps = { ideas?: { title: string; description: string; icon?: string; context?: string; callback?: () => void; }[]; /** * Callback function triggered to refresh the list of ideas. */ onRefreshIdeas?: () => void; onSendMessage: (message: string) => void; onVoiceMessage: (voice: Blob) => Promise; enableVoice?: boolean; enableScreenCapture?: boolean; onScreenshotClick?: () => void; onRemoveScreenshot?: () => void; enableAttachFile?: boolean; screenCaptured?: string; defaultPromptText?: string; status?: 'success' | 'failed' | 'pending'; onStopChat?: () => void; onRestartChat?: () => void; fontSize?: string; }; export default function Component({ ideas, onSendMessage, onVoiceMessage, enableVoice, enableScreenCapture, onScreenshotClick, onRemoveScreenshot, enableAttachFile, screenCaptured, defaultPromptText, status, onStopChat, onRestartChat, fontSize, onRefreshIdeas, }: PromptInputWithBottomActionsProps) { const [prompt, setPrompt] = useState(''); const onSendClick = useCallback(() => { onSendMessage(prompt); setPrompt(''); }, [prompt, onSendMessage]); const onStopClick = () => { onStopChat?.(); }; const onRestartChatClick = () => { onRestartChat?.(); }; const onKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter' && event.shiftKey) { if (prompt.length > 0) { onSendClick(); } // prevent new line event.preventDefault(); } }; const onClickIdea = (index: number) => { const idea = ideas?.[index]; if (idea) { const content = `${idea.description}`; setPrompt(content); if (idea.callback) { idea.callback(); } } }; const onRecordingComplete = async (voiceBlob: Blob) => { const voice = await onVoiceMessage(voiceBlob); setPrompt(voice); }; // if screenCaptured is not empty and defaultPromptText is empty, send it useEffect(() => { if ( screenCaptured && screenCaptured.length > 0 && defaultPromptText && defaultPromptText.length > 0 ) { onSendMessage(defaultPromptText); onRemoveScreenshot?.(); } }, [ screenCaptured, defaultPromptText, onSendClick, onSendMessage, onRemoveScreenshot, ]); return (
{ideas?.map(({ title, description, icon, context }, index) => ( ))} {ideas && ideas.length > 0 && ( )}
{screenCaptured && screenCaptured.length > 0 && ( } > )}
} minRows={3} radius="lg" value={prompt} variant="flat" onValueChange={setPrompt} onKeyDown={onKeyDown} disabled={status === 'pending'} />
{enableScreenCapture && ( )} {enableVoice && ( )} {enableAttachFile && ( <> )}

{prompt.length}/2000

); }