-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add Live to Firebase AI sample app #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ permissions: | |
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| - master | ||
| paths: | ||
| - 'ai/ai-react-app/**' | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| .liveViewContainer { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| height: 100%; | ||
| padding: 24px; | ||
| text-align: center; | ||
| gap: 20px; | ||
| } | ||
|
|
||
| .title { | ||
| font-size: 1.5rem; | ||
| font-weight: 400; | ||
| color: var(--color-text-primary); | ||
| margin: 0; | ||
| } | ||
|
|
||
| .instructions { | ||
| max-width: 500px; | ||
| color: var(--color-text-secondary); | ||
| font-size: 0.875rem; | ||
| line-height: 1.5; | ||
| } | ||
|
|
||
| .statusContainer { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 12px; | ||
| min-height: 24px; | ||
| } | ||
|
|
||
| .statusIndicator { | ||
| width: 12px; | ||
| height: 12px; | ||
| border-radius: 50%; | ||
| background-color: var(--color-text-placeholder); | ||
| transition: background-color 0.3s ease; | ||
| } | ||
|
|
||
| .statusIndicator.active { | ||
| background-color: var(--brand-google-cloud-green); | ||
| animation: pulse 2s infinite; | ||
| } | ||
|
|
||
| .statusText { | ||
| font-size: 1rem; | ||
| font-weight: 500; | ||
| color: var(--color-text-secondary); | ||
| } | ||
|
|
||
| .controlButton { | ||
| background-color: var(--color-surface-interactive); | ||
| color: var(--color-text-on-interactive); | ||
| border: none; | ||
| padding: 12px 24px; | ||
| border-radius: 24px; | ||
| cursor: pointer; | ||
| font-weight: 500; | ||
| font-size: 1rem; | ||
| transition: background-color 0.15s ease; | ||
| min-width: 200px; | ||
| } | ||
| .controlButton:hover:not(:disabled) { | ||
| background-color: var(--color-surface-interactive-hover); | ||
| } | ||
| .controlButton.stop { | ||
| background-color: var(--brand-firebase-red); | ||
| } | ||
| .controlButton.stop:hover:not(:disabled) { | ||
| background-color: #b71c1c; | ||
| } | ||
| .controlButton:disabled { | ||
| background-color: var(--color-surface-tertiary); | ||
| color: var(--color-text-disabled); | ||
| cursor: not-allowed; | ||
| } | ||
|
|
||
| .errorMessage { | ||
| background-color: var(--color-error-bg); | ||
| color: var(--color-error-text); | ||
| border: 1px solid var(--color-error-border); | ||
| padding: 10px 16px; | ||
| border-radius: 4px; | ||
| margin-top: 20px; | ||
| max-width: 500px; | ||
| white-space: pre-wrap; | ||
| } | ||
|
|
||
| @keyframes pulse { | ||
| 0% { | ||
| box-shadow: 0 0 0 0 rgba(52, 168, 83, 0.7); | ||
| } | ||
| 70% { | ||
| box-shadow: 0 0 0 10px rgba(52, 168, 83, 0); | ||
| } | ||
| 100% { | ||
| box-shadow: 0 0 0 0 rgba(52, 168, 83, 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import React, { useState, useEffect, useCallback } from "react"; | ||
| import styles from "./LiveView.module.css"; | ||
| import { | ||
| AI, | ||
| getLiveGenerativeModel, | ||
| startAudioConversation, | ||
| AudioConversationController, | ||
| AIError, | ||
| ResponseModality, | ||
| } from "firebase/ai"; | ||
| import { LIVE_MODELS } from "../services/firebaseAIService"; | ||
|
|
||
| interface LiveViewProps { | ||
| aiInstance: AI; | ||
| } | ||
|
|
||
| type ConversationState = "idle" | "active" | "error"; | ||
dlarocque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const LiveView: React.FC<LiveViewProps> = ({ aiInstance }) => { | ||
| const [conversationState, setConversationState] = | ||
| useState<ConversationState>("idle"); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [controller, setController] = | ||
| useState<AudioConversationController | null>(null); | ||
|
|
||
| const handleStartConversation = useCallback(async () => { | ||
| setError(null); | ||
| setConversationState("active"); | ||
|
|
||
| try { | ||
| const modelName = LIVE_MODELS.get(aiInstance.backend.backendType)!; | ||
| console.log(`[LiveView] Getting live model: ${modelName}`); | ||
| const model = getLiveGenerativeModel(aiInstance, { | ||
dlarocque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| model: modelName, | ||
| generationConfig: { | ||
| responseModalities: [ResponseModality.AUDIO] | ||
| } | ||
| }); | ||
|
|
||
| console.log("[LiveView] Connecting to live session..."); | ||
| const liveSession = await model.connect(); | ||
|
|
||
| console.log( | ||
| "[LiveView] Starting audio conversation. This will request microphone permissions.", | ||
| ); | ||
|
|
||
| const newController = await startAudioConversation(liveSession); | ||
|
|
||
| setController(newController); | ||
| console.log("[LiveView] Audio conversation started successfully."); | ||
| } catch (err: unknown) { | ||
| console.error("[LiveView] Failed to start conversation:", err); | ||
| let errorMessage = "An unknown error occurred."; | ||
| if (err instanceof AIError) { | ||
| errorMessage = `Error (${err.code}): ${err.message}`; | ||
| } else if (err instanceof Error) { | ||
| errorMessage = err.message; | ||
| } | ||
| setError(errorMessage); | ||
| setConversationState("error"); | ||
| setController(null); // Ensure controller is cleared on error | ||
| } | ||
| }, [aiInstance]); | ||
|
|
||
| const handleStopConversation = useCallback(async () => { | ||
| if (!controller) return; | ||
|
|
||
| console.log("[LiveView] Stopping audio conversation..."); | ||
| await controller.stop(); | ||
| setController(null); | ||
| setConversationState("idle"); | ||
| console.log("[LiveView] Audio conversation stopped."); | ||
| }, [controller]); | ||
|
|
||
| // Cleanup effect to stop the conversation if the component unmounts | ||
| useEffect(() => { | ||
| return () => { | ||
| if (controller) { | ||
| console.log( | ||
| "[LiveView] Component unmounting, stopping active conversation.", | ||
| ); | ||
| controller.stop(); | ||
| } | ||
| }; | ||
| }, [controller]); | ||
|
|
||
| const getStatusText = () => { | ||
| switch (conversationState) { | ||
| case "idle": | ||
| return "Ready"; | ||
| case "active": | ||
| return "In Conversation"; | ||
| case "error": | ||
| return "Error"; | ||
| default: | ||
| return "Unknown"; | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={styles.liveViewContainer}> | ||
| <h2 className={styles.title}>Live Conversation</h2> | ||
| <p className={styles.instructions}> | ||
| Click the button below to start a real-time voice conversation with the | ||
| model. Your browser will ask for microphone permissions. | ||
| </p> | ||
|
|
||
| <div className={styles.statusContainer}> | ||
| <div | ||
| className={`${styles.statusIndicator} ${ | ||
| conversationState === "active" ? styles.active : "" | ||
| }`} | ||
| /> | ||
| <span className={styles.statusText}>Status: {getStatusText()}</span> | ||
| </div> | ||
|
|
||
| <button | ||
| className={`${styles.controlButton} ${ | ||
| conversationState === "active" ? styles.stop : "" | ||
| }`} | ||
| onClick={ | ||
| conversationState === "active" | ||
| ? handleStopConversation | ||
| : handleStartConversation | ||
| } | ||
| disabled={false} // The button is never truly disabled, it just toggles state | ||
| > | ||
| {conversationState === "active" | ||
| ? "Stop Conversation" | ||
| : "Start Conversation"} | ||
| </button> | ||
|
|
||
| {error && <div className={styles.errorMessage}>{error}</div>} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default LiveView; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.