-
Notifications
You must be signed in to change notification settings - Fork 139
Update examples with useSession/useAgent hooks #1242
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
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f514318
Use session
pblazej b5b3cde
Use agent
pblazej de906a0
Token generation
pblazej 55854f7
Catch
pblazej 6a3a037
Lint
pblazej 0745049
Keep side effects
pblazej 8629563
Fix hydration errors
pblazej d9dd885
Revert "Fix hydration errors"
pblazej bfc77d5
Use session.isConnected
pblazej 6de8642
Change
pblazej f8375f0
Consistency
pblazej dc0e3a6
Prettier
pblazej f35d5a1
API
pblazej 63d3da7
fix: address stretched header image on index page
1egoman e5573e5
fix: migrate all session.internal event emitter usage to useEvents
1egoman 95bad4f
refactor: rename VoiceAssistantExample => AgentExample
1egoman 29d13fd
refactor: move TokenSource outside of example components
1egoman 238b090
refactor: remove noop externally managed room
1egoman 1383d77
refactor: refurbish ai agent example to use more of agent sdk
1egoman 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@livekit/component-example-next': patch | ||
| '@livekit/components-react': patch | ||
| --- | ||
|
|
||
| Update nextjs examples with useSession/useAgent hooks |
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,105 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| useAgent, | ||
| BarVisualizer, | ||
| RoomAudioRenderer, | ||
| VoiceAssistantControlBar, | ||
| SessionProvider, | ||
| useSession, | ||
| SessionEvent, | ||
| useEvents, | ||
| } from '@livekit/components-react'; | ||
| import type { NextPage } from 'next'; | ||
| import { useMemo, useState, useEffect } from 'react'; | ||
| import { MediaDeviceFailure, TokenSource } from 'livekit-client'; | ||
| import styles from '../styles/VoiceAssistant.module.scss'; | ||
| import { generateRandomUserId } from '../lib/helper'; | ||
|
|
||
| function SimpleAgent() { | ||
| const agent = useAgent(); | ||
|
|
||
| useEffect(() => { | ||
| if (agent.state === 'failed') { | ||
| alert(`Agent error: ${agent.failureReasons.join(', ')}`); | ||
| } | ||
| }, [agent.state, agent.failureReasons]); | ||
|
|
||
| return ( | ||
| <BarVisualizer | ||
| state={agent.state} | ||
| barCount={7} | ||
| track={agent.microphoneTrack} | ||
| style={{ width: '75vw', height: '300px' }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const tokenSource = TokenSource.endpoint(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT!); | ||
|
|
||
| const AgentExample: NextPage = () => { | ||
| const params = useMemo( | ||
| () => (typeof window !== 'undefined' ? new URLSearchParams(location.search) : null), | ||
| [], | ||
| ); | ||
| const roomName = useMemo( | ||
| () => params?.get('room') ?? 'test-room-' + Math.random().toFixed(5), | ||
| [params], | ||
| ); | ||
| const [userIdentity] = useState(() => params?.get('user') ?? generateRandomUserId()); | ||
|
|
||
| const session = useSession(tokenSource, { | ||
| roomName, | ||
| participantIdentity: userIdentity, | ||
| }); | ||
|
|
||
| const [started, setStarted] = useState(false); | ||
| useEffect(() => { | ||
| if (started) { | ||
| session.start().catch((err) => { | ||
| console.error('Failed to start session:', err); | ||
| }); | ||
| } else { | ||
| session.end().catch((err) => { | ||
| console.error('Failed to end session:', err); | ||
| }); | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [started, session.start, session.end]); | ||
|
|
||
| useEffect(() => { | ||
| if (session.connectionState === 'disconnected') { | ||
| setStarted(false); | ||
| } | ||
| }, [session.connectionState]); | ||
|
|
||
| useEvents(session, SessionEvent.MediaDevicesError, (error) => { | ||
| const failure = MediaDeviceFailure.getFailure(error); | ||
| console.error(failure); | ||
| alert( | ||
| 'Error acquiring camera or microphone permissions. Please make sure you grant the necessary permissions in your browser and reload the tab', | ||
| ); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <main data-lk-theme="default" className={styles.main}> | ||
| <SessionProvider session={session}> | ||
| <div className={styles.room}> | ||
| <div className={styles.inner}> | ||
| {started ? ( | ||
| <SimpleAgent /> | ||
| ) : ( | ||
| <button className="lk-button" onClick={() => setStarted(true)}> | ||
| Connect | ||
| </button> | ||
| )} | ||
| </div> | ||
| <VoiceAssistantControlBar /> | ||
| <RoomAudioRenderer /> | ||
| </div> | ||
| </SessionProvider> | ||
| </main> | ||
| ); | ||
| }; | ||
|
|
||
| export default AgentExample; | ||
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
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.