Skip to content

Commit b81a182

Browse files
committed
feat: add local track state back in to useConversationWith
1 parent c8a023d commit b81a182

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

packages/react/src/hooks/useConversationWith.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type TypedEventEmitter from 'typed-emitter';
2-
import { Room, RoomEvent, ConnectionState, TrackPublishOptions, Track } from 'livekit-client';
2+
import { Room, RoomEvent, ConnectionState, TrackPublishOptions, Track, LocalParticipant } from 'livekit-client';
33
import { EventEmitter } from 'events';
44
import { useCallback, useEffect, useMemo, useState } from 'react';
55

66
import { ConnectionCredentials } from '../utils/ConnectionCredentialsProvider';
77
import { useMaybeRoomContext } from '../context';
88
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';
99
import { useAgent } from './useAgent';
10-
import { TrackReferencePlaceholder } from '@livekit/components-core';
10+
import { TrackReferenceOrPlaceholder, TrackReferencePlaceholder } from '@livekit/components-core';
11+
import { useLocalParticipant } from './useLocalParticipant';
1112

1213
/** State representing the current connection status to the server hosted agent */
1314
// FIXME: maybe just make this ConnectionState?
@@ -75,18 +76,33 @@ type ConversationStateConnecting = ConversationStateCommon & {
7576
connectionState: "connecting";
7677
isConnected: false;
7778
isReconnecting: false;
79+
80+
local: {
81+
camera: TrackReferencePlaceholder<Track.Source.Camera, LocalParticipant>;
82+
microphone: TrackReferencePlaceholder<Track.Source.Microphone, LocalParticipant>;
83+
};
7884
};
7985

8086
type ConversationStateConnected = ConversationStateCommon & {
8187
connectionState: "connected" | "reconnecting" | "signalReconnecting";
8288
isConnected: true;
8389
isReconnecting: boolean;
90+
91+
local: {
92+
camera: TrackReferenceOrPlaceholder<Track.Source.Camera, LocalParticipant>,
93+
microphone: TrackReferenceOrPlaceholder<Track.Source.Microphone, LocalParticipant>,
94+
};
8495
};
8596

8697
type ConversationStateDisconnected = ConversationStateCommon & {
8798
connectionState: "disconnected";
8899
isConnected: false;
89100
isReconnecting: false;
101+
102+
local: {
103+
camera: TrackReferencePlaceholder<Track.Source.Camera, LocalParticipant>;
104+
microphone: TrackReferencePlaceholder<Track.Source.Microphone, LocalParticipant>;
105+
};
90106
};
91107

92108
type ConversationActions = {
@@ -166,6 +182,14 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch,
166182
};
167183
}, [room, emitter]);
168184

185+
const { localParticipant, cameraTrack, microphoneTrack } = useLocalParticipant({ room });
186+
const localCamera = useMemo(() => {
187+
return cameraTrack ? { source: Track.Source.Camera as const, participant: localParticipant, publication: microphoneTrack } : null;
188+
}, [localParticipant, microphoneTrack]);
189+
const localMicrophone = useMemo(() => {
190+
return microphoneTrack ? { source: Track.Source.Microphone as const, participant: localParticipant, publication: microphoneTrack } : null;
191+
}, [localParticipant, microphoneTrack]);
192+
169193
const conversationState = useMemo((): ConversationStateConnecting | ConversationStateConnected | ConversationStateDisconnected => {
170194
const common: ConversationStateCommon = {
171195
[Symbol.toStringTag]: "AgentSessionInstance",
@@ -185,6 +209,11 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch,
185209

186210
connectionState: 'connecting',
187211
...generateDerivedConnectionStateValues('connecting'),
212+
213+
local: {
214+
camera: { participant: localParticipant, source: Track.Source.Camera },
215+
microphone: { participant: localParticipant, source: Track.Source.Microphone },
216+
},
188217
};
189218

190219
case ConnectionState.Connected:
@@ -195,6 +224,11 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch,
195224

196225
connectionState: roomConnectionState,
197226
...generateDerivedConnectionStateValues(roomConnectionState),
227+
228+
local: {
229+
camera: localCamera ?? { participant: localParticipant, source: Track.Source.Camera },
230+
microphone: localMicrophone ?? { participant: localParticipant, source: Track.Source.Microphone },
231+
},
198232
};
199233

200234
case ConnectionState.Disconnected:
@@ -203,9 +237,14 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch,
203237

204238
connectionState: ConnectionState.Disconnected,
205239
...generateDerivedConnectionStateValues(ConnectionState.Disconnected),
240+
241+
local: {
242+
camera: { participant: localParticipant, source: Track.Source.Camera },
243+
microphone: { participant: localParticipant, source: Track.Source.Microphone },
244+
},
206245
};
207246
}
208-
}, [options.credentials, room, emitter, roomConnectionState]);
247+
}, [options.credentials, room, emitter, roomConnectionState, localParticipant]);
209248
useEffect(() => {
210249
emitter.emit(ConversationEvent.ConnectionStateChanged, conversationState.connectionState);
211250
}, [emitter, conversationState.connectionState]);

0 commit comments

Comments
 (0)