|
| 1 | +import React, { useState, useEffect, useCallback } from "react"; |
| 2 | +import styles from "./LiveView.module.css"; |
| 3 | +import { |
| 4 | + AI, |
| 5 | + getLiveGenerativeModel, |
| 6 | + startAudioConversation, |
| 7 | + AudioConversationController, |
| 8 | + AIError, |
| 9 | + ResponseModality, |
| 10 | +} from "firebase/ai"; |
| 11 | +import { LIVE_MODELS } from "../services/firebaseAIService"; |
| 12 | + |
| 13 | +interface LiveViewProps { |
| 14 | + aiInstance: AI; |
| 15 | +} |
| 16 | + |
| 17 | +type ConversationState = "idle" | "active" | "error"; |
| 18 | + |
| 19 | +const LiveView: React.FC<LiveViewProps> = ({ aiInstance }) => { |
| 20 | + const [conversationState, setConversationState] = |
| 21 | + useState<ConversationState>("idle"); |
| 22 | + const [error, setError] = useState<string | null>(null); |
| 23 | + const [controller, setController] = |
| 24 | + useState<AudioConversationController | null>(null); |
| 25 | + |
| 26 | + const handleStartConversation = useCallback(async () => { |
| 27 | + setError(null); |
| 28 | + setConversationState("active"); |
| 29 | + |
| 30 | + try { |
| 31 | + const modelName = LIVE_MODELS.get(aiInstance.backend.backendType)!; |
| 32 | + console.log(`[LiveView] Getting live model: ${modelName}`); |
| 33 | + const model = getLiveGenerativeModel(aiInstance, { |
| 34 | + model: modelName, |
| 35 | + generationConfig: { |
| 36 | + responseModalities: [ResponseModality.AUDIO] |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + console.log("[LiveView] Connecting to live session..."); |
| 41 | + const liveSession = await model.connect(); |
| 42 | + |
| 43 | + console.log( |
| 44 | + "[LiveView] Starting audio conversation. This will request microphone permissions.", |
| 45 | + ); |
| 46 | + |
| 47 | + const newController = await startAudioConversation(liveSession); |
| 48 | + |
| 49 | + setController(newController); |
| 50 | + console.log("[LiveView] Audio conversation started successfully."); |
| 51 | + } catch (err: unknown) { |
| 52 | + console.error("[LiveView] Failed to start conversation:", err); |
| 53 | + let errorMessage = "An unknown error occurred."; |
| 54 | + if (err instanceof AIError) { |
| 55 | + errorMessage = `Error (${err.code}): ${err.message}`; |
| 56 | + } else if (err instanceof Error) { |
| 57 | + errorMessage = err.message; |
| 58 | + } |
| 59 | + setError(errorMessage); |
| 60 | + setConversationState("error"); |
| 61 | + setController(null); // Ensure controller is cleared on error |
| 62 | + } |
| 63 | + }, [aiInstance]); |
| 64 | + |
| 65 | + const handleStopConversation = useCallback(async () => { |
| 66 | + if (!controller) return; |
| 67 | + |
| 68 | + console.log("[LiveView] Stopping audio conversation..."); |
| 69 | + await controller.stop(); |
| 70 | + setController(null); |
| 71 | + setConversationState("idle"); |
| 72 | + console.log("[LiveView] Audio conversation stopped."); |
| 73 | + }, [controller]); |
| 74 | + |
| 75 | + // Cleanup effect to stop the conversation if the component unmounts |
| 76 | + useEffect(() => { |
| 77 | + return () => { |
| 78 | + if (controller) { |
| 79 | + console.log( |
| 80 | + "[LiveView] Component unmounting, stopping active conversation.", |
| 81 | + ); |
| 82 | + controller.stop(); |
| 83 | + } |
| 84 | + }; |
| 85 | + }, [controller]); |
| 86 | + |
| 87 | + const getStatusText = () => { |
| 88 | + switch (conversationState) { |
| 89 | + case "idle": |
| 90 | + return "Ready"; |
| 91 | + case "active": |
| 92 | + return "In Conversation"; |
| 93 | + case "error": |
| 94 | + return "Error"; |
| 95 | + default: |
| 96 | + return "Unknown"; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + return ( |
| 101 | + <div className={styles.liveViewContainer}> |
| 102 | + <h2 className={styles.title}>Live Conversation</h2> |
| 103 | + <p className={styles.instructions}> |
| 104 | + Click the button below to start a real-time voice conversation with the |
| 105 | + model. Your browser will ask for microphone permissions. |
| 106 | + </p> |
| 107 | + |
| 108 | + <div className={styles.statusContainer}> |
| 109 | + <div |
| 110 | + className={`${styles.statusIndicator} ${ |
| 111 | + conversationState === "active" ? styles.active : "" |
| 112 | + }`} |
| 113 | + /> |
| 114 | + <span className={styles.statusText}>Status: {getStatusText()}</span> |
| 115 | + </div> |
| 116 | + |
| 117 | + <button |
| 118 | + className={`${styles.controlButton} ${ |
| 119 | + conversationState === "active" ? styles.stop : "" |
| 120 | + }`} |
| 121 | + onClick={ |
| 122 | + conversationState === "active" |
| 123 | + ? handleStopConversation |
| 124 | + : handleStartConversation |
| 125 | + } |
| 126 | + disabled={false} // The button is never truly disabled, it just toggles state |
| 127 | + > |
| 128 | + {conversationState === "active" |
| 129 | + ? "Stop Conversation" |
| 130 | + : "Start Conversation"} |
| 131 | + </button> |
| 132 | + |
| 133 | + {error && <div className={styles.errorMessage}>{error}</div>} |
| 134 | + </div> |
| 135 | + ); |
| 136 | +}; |
| 137 | + |
| 138 | +export default LiveView; |
0 commit comments