Skip to content

Commit 37182c3

Browse files
authored
Pull audio track from avatar worker in useVoiceAssistant (#1130)
1 parent 7828026 commit 37182c3

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

.changeset/tender-eyes-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/components-react": patch
3+
---
4+
5+
Pull audio track from avatar worker in useVoiceAssistant

packages/core/etc/components-core.api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ export type ChatOptions = {
8686
};
8787

8888
// @public (undocumented)
89-
export function computeMenuPosition(button: HTMLElement, menu: HTMLElement): Promise<{
90-
x: number;
91-
y: number;
92-
}>;
89+
export function computeMenuPosition(button: HTMLElement, menu: HTMLElement, onUpdate?: (x: number, y: number) => void): () => void;
9390

9491
// @public (undocumented)
9592
export function connectedParticipantObserver(room: Room, identity: string, options?: ConnectedParticipantObserverOptions): Observable<RemoteParticipant | undefined>;

packages/react/etc/components-react.api.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,16 +1256,12 @@ export interface VideoTrackProps extends React_2.VideoHTMLAttributes<HTMLVideoEl
12561256

12571257
// @beta (undocumented)
12581258
export interface VoiceAssistant {
1259-
// (undocumented)
12601259
agent: RemoteParticipant | undefined;
1261-
// (undocumented)
12621260
agentAttributes: RemoteParticipant['attributes'] | undefined;
1263-
// (undocumented)
12641261
agentTranscriptions: ReceivedTranscriptionSegment[];
1265-
// (undocumented)
12661262
audioTrack: TrackReference_3 | undefined;
1267-
// (undocumented)
12681263
state: AgentState;
1264+
videoTrack: TrackReference_3 | undefined;
12691265
}
12701266

12711267
// @beta (undocumented)

packages/react/src/hooks/useVoiceAssistant.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,29 @@ export type AgentState =
2323
* @beta
2424
*/
2525
export interface VoiceAssistant {
26+
/**
27+
* The agent participant.
28+
*/
2629
agent: RemoteParticipant | undefined;
30+
/**
31+
* The current state of the agent.
32+
*/
2733
state: AgentState;
34+
/**
35+
* The microphone track published by the agent or associated avatar worker (if any).
36+
*/
2837
audioTrack: TrackReference | undefined;
38+
/**
39+
* The camera track published by the agent or associated avatar worker (if any).
40+
*/
41+
videoTrack: TrackReference | undefined;
42+
/**
43+
* The transcriptions of the agent's microphone track (if any).
44+
*/
2945
agentTranscriptions: ReceivedTranscriptionSegment[];
46+
/**
47+
* The agent's participant attributes.
48+
*/
3049
agentAttributes: RemoteParticipant['attributes'] | undefined;
3150
}
3251

@@ -42,8 +61,28 @@ const state_attribute = 'lk.agent.state';
4261
* @beta
4362
*/
4463
export function useVoiceAssistant(): VoiceAssistant {
45-
const agent = useRemoteParticipants().find((p) => p.kind === ParticipantKind.AGENT);
46-
const audioTrack = useParticipantTracks([Track.Source.Microphone], agent?.identity)[0];
64+
const remoteParticipants = useRemoteParticipants();
65+
const agent = remoteParticipants.find(
66+
(p) => p.kind === ParticipantKind.AGENT && !('lk.publish_on_behalf' in p.attributes),
67+
);
68+
const worker = remoteParticipants.find(
69+
(p) =>
70+
p.kind === ParticipantKind.AGENT && p.attributes['lk.publish_on_behalf'] === agent?.identity,
71+
);
72+
const agentTracks = useParticipantTracks(
73+
[Track.Source.Microphone, Track.Source.Camera],
74+
agent?.identity,
75+
);
76+
const workerTracks = useParticipantTracks(
77+
[Track.Source.Microphone, Track.Source.Camera],
78+
worker?.identity,
79+
);
80+
const audioTrack =
81+
agentTracks.find((t) => t.source === Track.Source.Microphone) ??
82+
workerTracks.find((t) => t.source === Track.Source.Microphone);
83+
const videoTrack =
84+
agentTracks.find((t) => t.source === Track.Source.Camera) ??
85+
workerTracks.find((t) => t.source === Track.Source.Camera);
4786
const { segments: agentTranscriptions } = useTrackTranscription(audioTrack);
4887
const connectionState = useConnectionState();
4988
const { attributes } = useParticipantAttributes({ participant: agent });
@@ -66,6 +105,7 @@ export function useVoiceAssistant(): VoiceAssistant {
66105
agent,
67106
state,
68107
audioTrack,
108+
videoTrack,
69109
agentTranscriptions,
70110
agentAttributes: attributes,
71111
};

0 commit comments

Comments
 (0)