Skip to content

Commit fdd28f2

Browse files
committed
feat: add session context and use it throughout new agents sdk components
1 parent b3918f9 commit fdd28f2

File tree

5 files changed

+15
-42
lines changed

5 files changed

+15
-42
lines changed

packages/react/src/components/RoomProvider.tsx

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/react/src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ export {
3131
ChatEntry,
3232
formatChatMessageLinks,
3333
} from '../components/ChatEntry';
34-
export * from './RoomProvider';
34+
export * from './SessionProvider';

packages/react/src/context/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {
1616
} from './participant-context';
1717
export {} from './pin-context';
1818
export { RoomContext, useEnsureRoom, useMaybeRoomContext, useRoomContext } from './room-context';
19+
export { SessionContext, useEnsureSession, useMaybeSessionContext, useSessionContext } from './session-context';
1920
export {
2021
TrackRefContext,
2122
useEnsureTrackRef,

packages/react/src/hooks/useAgent.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ParticipantAgentAttributes, TrackReference } from '@livekit/components-
1616
import { useParticipantTracks } from './useParticipantTracks';
1717
import { useRemoteParticipants } from './useRemoteParticipants';
1818
import { UseSessionReturn } from './useSession';
19+
import { useMaybeSessionContext } from '../context';
1920

2021
// FIXME: make this 10 seconds once room dispatch booting info is discoverable
2122
const DEFAULT_AGENT_CONNECT_TIMEOUT_MILLISECONDS = 20_000;
@@ -265,7 +266,15 @@ type SessionStub = Pick<UseSessionReturn, 'connectionState' | 'room' | 'internal
265266
* useAgent encapculates all agent state, normalizing some quirks around how LiveKit Agents work.
266267
* @public
267268
*/
268-
export function useAgent(session: SessionStub): UseAgentReturn {
269+
export function useAgent(session?: SessionStub): UseAgentReturn {
270+
const sessionFromContext = useMaybeSessionContext();
271+
session = session ?? sessionFromContext;
272+
if (!session) {
273+
throw new Error(
274+
'No session provided, make sure you are inside a Session context or pass the session explicitly',
275+
);
276+
}
277+
269278
const {
270279
room,
271280
internal: { agentConnectTimeoutMilliseconds },

packages/react/src/hooks/useSessionMessages.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useAgent } from './useAgent';
1414
import { useTranscriptions } from './useTranscriptions';
1515
import { useChat } from './useChat';
1616
import { UseSessionReturn } from './useSession';
17+
import { useEnsureSession } from '../context';
1718

1819
/** @public */
1920
export type UseSessionMessagesReturn = {
@@ -42,8 +43,8 @@ export type MessagesCallbacks = {
4243
};
4344

4445
/** @public */
45-
export function useSessionMessages(session: UseSessionReturn): UseSessionMessagesReturn {
46-
const { room } = session;
46+
export function useSessionMessages(session?: UseSessionReturn): UseSessionMessagesReturn {
47+
const { room } = useEnsureSession(session);
4748

4849
const agent = useAgent(session);
4950

0 commit comments

Comments
 (0)