Spaces:
Sleeping
Sleeping
Deploy v1 — single-Docker FastAPI + Next.js + RAG + voice + faithfulness
Browse files- frontend/src/app/page.tsx +33 -1
frontend/src/app/page.tsx
CHANGED
|
@@ -57,6 +57,9 @@ export default function Page() {
|
|
| 57 |
const [sessionId, setSessionId] = useState<string | undefined>();
|
| 58 |
const [uploadStatus, setUploadStatus] = useState<string | null>(null);
|
| 59 |
const [handsFree, setHandsFree] = useState(false); // VAD auto-cutoff mode
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
| 62 |
const audioChunksRef = useRef<Blob[]>([]);
|
|
@@ -83,6 +86,19 @@ export default function Page() {
|
|
| 83 |
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" });
|
| 84 |
}, [messages]);
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
function pushUser(text: string) {
|
| 87 |
setMessages((m) => [...m, { id: `u_${Date.now()}`, role: "user", content: text }]);
|
| 88 |
}
|
|
@@ -115,7 +131,23 @@ export default function Page() {
|
|
| 115 |
});
|
| 116 |
if (audioUrl) {
|
| 117 |
const audio = new Audio(audioUrl);
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
} catch (e: unknown) {
|
| 121 |
pushAssistant(`Sorry — backend error: ${e instanceof Error ? e.message : String(e)}`);
|
|
|
|
| 57 |
const [sessionId, setSessionId] = useState<string | undefined>();
|
| 58 |
const [uploadStatus, setUploadStatus] = useState<string | null>(null);
|
| 59 |
const [handsFree, setHandsFree] = useState(false); // VAD auto-cutoff mode
|
| 60 |
+
// Live ref of handsFree so async TTS-ended callbacks read the latest value
|
| 61 |
+
// (closure captured at audio.play() time would otherwise be stale)
|
| 62 |
+
const handsFreeRef = useRef(false);
|
| 63 |
|
| 64 |
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
| 65 |
const audioChunksRef = useRef<Blob[]>([]);
|
|
|
|
| 86 |
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" });
|
| 87 |
}, [messages]);
|
| 88 |
|
| 89 |
+
// Hands-free continuous loop: when toggled ON, immediately open mic. When
|
| 90 |
+
// toggled OFF, close any in-progress recording. The send() function takes
|
| 91 |
+
// care of re-opening the mic after each assistant TTS finishes.
|
| 92 |
+
useEffect(() => {
|
| 93 |
+
handsFreeRef.current = handsFree;
|
| 94 |
+
if (handsFree && !recording && !busy) {
|
| 95 |
+
startRecording();
|
| 96 |
+
} else if (!handsFree && recording) {
|
| 97 |
+
stopRecording();
|
| 98 |
+
}
|
| 99 |
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
| 100 |
+
}, [handsFree]);
|
| 101 |
+
|
| 102 |
function pushUser(text: string) {
|
| 103 |
setMessages((m) => [...m, { id: `u_${Date.now()}`, role: "user", content: text }]);
|
| 104 |
}
|
|
|
|
| 131 |
});
|
| 132 |
if (audioUrl) {
|
| 133 |
const audio = new Audio(audioUrl);
|
| 134 |
+
// In hands-free mode, re-open the mic AFTER the assistant's TTS reply
|
| 135 |
+
// finishes playing — that's how a Siri/Alexa loop feels natural.
|
| 136 |
+
audio.addEventListener("ended", () => {
|
| 137 |
+
if (handsFreeRef.current) {
|
| 138 |
+
setTimeout(() => { if (handsFreeRef.current) startRecording(); }, 250);
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
audio.play().catch(() => {
|
| 142 |
+
// Audio playback failed (e.g. browser blocked autoplay) — still
|
| 143 |
+
// continue the hands-free loop if enabled.
|
| 144 |
+
if (handsFreeRef.current) {
|
| 145 |
+
setTimeout(() => { if (handsFreeRef.current) startRecording(); }, 250);
|
| 146 |
+
}
|
| 147 |
+
});
|
| 148 |
+
} else if (handsFreeRef.current) {
|
| 149 |
+
// No audio reply (voice toggle off) — restart mic after a short pause
|
| 150 |
+
setTimeout(() => { if (handsFreeRef.current) startRecording(); }, 500);
|
| 151 |
}
|
| 152 |
} catch (e: unknown) {
|
| 153 |
pushAssistant(`Sorry — backend error: ${e instanceof Error ? e.message : String(e)}`);
|