'use client'; import { useChat } from '@ai-sdk/react'; import { Container, ScrollArea, Paper, Group, TextInput, Button, Stack, Text, Box } from '@mantine/core'; import { useEffect, useRef, useState } from 'react'; import { MicrophoneRecorder } from './MicrophoneRecorder'; export function ChatInterface() { const viewport = useRef(null); const [input, setInput] = useState(''); const { messages, sendMessage, status, } = useChat(); // Auto-scroll to bottom when new messages arrive useEffect(() => { if (viewport.current) { viewport.current.scrollTo({ top: viewport.current.scrollHeight, behavior: 'smooth', }); } }, [messages]); return ( {/* Chat messages area */} {messages.length === 0 && ( Start a conversation by typing or speaking... )} {messages.map((message) => ( {/* Extract text from parts */} {('parts' in message && Array.isArray((message as any).parts)) ? (message as any).parts.find((p: any) => p.type === 'text')?.text || '' : (message as any).content || ''} ))} {/* Input area */}
{ e.preventDefault(); if (!input.trim() || status === 'submitted' || status === 'streaming') return; sendMessage({ text: input }); setInput(''); }}> setInput(e.currentTarget.value)} placeholder="Speak or type your thoughts..." style={{ flex: 1 }} variant="unstyled" disabled={status === 'submitted' || status === 'streaming'} /> {/* Microphone Recorder */} { // Update the input field in real-time setInput(transcript); }} onTranscriptFinalized={(transcript) => { // Set the input and submit setInput(transcript); // Trigger form submission setTimeout(() => { const form = document.querySelector('form'); if (form) { form.requestSubmit(); } }, 100); }} />
); }