|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useRef, useState } from 'react'; |
| 4 | + |
| 5 | +type Status = 'idle' | 'recording' | 'processing' | 'speaking'; |
| 6 | + |
| 7 | +export default function Page() { |
| 8 | + const [status, setStatus] = useState<Status>('idle'); |
| 9 | + const [transcript, setTranscript] = useState(''); |
| 10 | + const [reply, setReply] = useState(''); |
| 11 | + const [error, setError] = useState<string | null>(null); |
| 12 | + |
| 13 | + const recorderRef = useRef<MediaRecorder | null>(null); |
| 14 | + const chunksRef = useRef<Blob[]>([]); |
| 15 | + |
| 16 | + const startRecording = async () => { |
| 17 | + if (status !== 'idle') return; |
| 18 | + setError(null); |
| 19 | + |
| 20 | + const stream = await navigator.mediaDevices.getUserMedia({ |
| 21 | + audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true }, |
| 22 | + }); |
| 23 | + |
| 24 | + const recorder = new MediaRecorder(stream, { mimeType: 'audio/webm' }); |
| 25 | + chunksRef.current = []; |
| 26 | + |
| 27 | + recorder.ondataavailable = (e) => { if (e.data.size > 0) chunksRef.current.push(e.data); }; |
| 28 | + recorder.onstop = () => { |
| 29 | + stream.getTracks().forEach((t) => t.stop()); |
| 30 | + runPipeline(new Blob(chunksRef.current, { type: 'audio/webm' })); |
| 31 | + }; |
| 32 | + |
| 33 | + recorder.start(); |
| 34 | + recorderRef.current = recorder; |
| 35 | + setStatus('recording'); |
| 36 | + }; |
| 37 | + |
| 38 | + const stopRecording = () => { |
| 39 | + if (recorderRef.current?.state === 'recording') { |
| 40 | + recorderRef.current.stop(); |
| 41 | + recorderRef.current = null; |
| 42 | + setStatus('processing'); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + const runPipeline = async (blob: Blob) => { |
| 47 | + try { |
| 48 | + const res = await fetch('/api/pipeline', { |
| 49 | + method: 'POST', |
| 50 | + headers: { 'Content-Type': 'audio/webm' }, |
| 51 | + body: blob, |
| 52 | + }); |
| 53 | + |
| 54 | + if (res.status === 204) { |
| 55 | + setError('No speech detected'); |
| 56 | + setStatus('idle'); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (!res.ok) throw new Error(`Pipeline failed: ${res.status}`); |
| 60 | + |
| 61 | + const raw = res.headers.get('X-Transcript'); |
| 62 | + if (raw) setTranscript(decodeURIComponent(raw)); |
| 63 | + |
| 64 | + const rawReply = res.headers.get('X-Reply'); |
| 65 | + if (rawReply) setReply(decodeURIComponent(rawReply)); |
| 66 | + |
| 67 | + const audioBlob = await res.blob(); |
| 68 | + const url = URL.createObjectURL(audioBlob); |
| 69 | + const audio = new Audio(url); |
| 70 | + setStatus('speaking'); |
| 71 | + audio.onended = () => { URL.revokeObjectURL(url); setStatus('idle'); }; |
| 72 | + audio.onerror = () => { URL.revokeObjectURL(url); setStatus('idle'); }; |
| 73 | + audio.play(); |
| 74 | + } catch (err) { |
| 75 | + setError(err instanceof Error ? err.message : String(err)); |
| 76 | + setStatus('idle'); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const busy = status !== 'idle'; |
| 81 | + |
| 82 | + return ( |
| 83 | + <main style={{ maxWidth: 520, margin: '4rem auto', fontFamily: 'sans-serif', padding: '0 1rem' }}> |
| 84 | + <h1 style={{ fontSize: '1.6rem', marginBottom: '0.25rem' }}>MOSS Voice Agent</h1> |
| 85 | + <p style={{ color: '#6b7280', fontSize: '0.85rem', marginBottom: '2rem' }}> |
| 86 | + Deepgram STT · GPT-4.1 Mini · Deepgram TTS · MOSS retrieval |
| 87 | + </p> |
| 88 | + |
| 89 | + <button |
| 90 | + onMouseDown={startRecording} |
| 91 | + onMouseUp={stopRecording} |
| 92 | + onTouchStart={(e) => { e.preventDefault(); startRecording(); }} |
| 93 | + onTouchEnd={(e) => { e.preventDefault(); stopRecording(); }} |
| 94 | + disabled={status === 'processing' || status === 'speaking'} |
| 95 | + style={{ |
| 96 | + padding: '1rem 2.5rem', |
| 97 | + fontSize: '1rem', |
| 98 | + fontWeight: 600, |
| 99 | + borderRadius: 12, |
| 100 | + border: 'none', |
| 101 | + cursor: busy && status !== 'recording' ? 'not-allowed' : 'pointer', |
| 102 | + background: |
| 103 | + status === 'recording' ? '#dc2626' : |
| 104 | + status === 'processing' ? '#6b7280' : |
| 105 | + status === 'speaking' ? '#059669' : '#2563eb', |
| 106 | + color: '#fff', |
| 107 | + userSelect: 'none', |
| 108 | + WebkitUserSelect: 'none', |
| 109 | + transition: 'background 0.15s', |
| 110 | + }} |
| 111 | + > |
| 112 | + {status === 'recording' ? '🎙 Release to send' |
| 113 | + : status === 'processing' ? 'Transcribing…' |
| 114 | + : status === 'speaking' ? '🔊 Speaking…' |
| 115 | + : '● Hold to speak'} |
| 116 | + </button> |
| 117 | + |
| 118 | + <p style={{ marginTop: '0.75rem', fontSize: '0.8rem', color: '#9ca3af' }}> |
| 119 | + {status === 'idle' ? 'Press and hold, speak, then release.' : `Status: ${status}`} |
| 120 | + </p> |
| 121 | + |
| 122 | + {error && ( |
| 123 | + <p style={{ marginTop: '0.75rem', color: '#dc2626', fontSize: '0.82rem' }}>⚠ {error}</p> |
| 124 | + )} |
| 125 | + |
| 126 | + {transcript && ( |
| 127 | + <div style={{ marginTop: '1.5rem', padding: '0.75rem 1rem', background: '#f3f4f6', borderRadius: 8 }}> |
| 128 | + <p style={{ fontSize: '0.72rem', color: '#9ca3af', margin: '0 0 0.25rem' }}>YOU</p> |
| 129 | + <p style={{ margin: 0, fontSize: '0.9rem' }}>{transcript}</p> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + |
| 133 | + {reply && ( |
| 134 | + <div style={{ marginTop: '0.75rem', padding: '0.75rem 1rem', background: '#eff6ff', borderRadius: 8 }}> |
| 135 | + <p style={{ fontSize: '0.72rem', color: '#93c5fd', margin: '0 0 0.25rem' }}>AGENT</p> |
| 136 | + <p style={{ margin: 0, fontSize: '0.9rem' }}>{reply}</p> |
| 137 | + </div> |
| 138 | + )} |
| 139 | + </main> |
| 140 | + ); |
| 141 | +} |
0 commit comments