Skip to content

Pull audio track from avatar worker in useVoiceAssistant #1130

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 6 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-eyes-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-react": patch
---

Pull audio track from avatar worker in useVoiceAssistant
5 changes: 1 addition & 4 deletions packages/core/etc/components-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ export type ChatOptions = {
};

// @public (undocumented)
export function computeMenuPosition(button: HTMLElement, menu: HTMLElement): Promise<{
x: number;
y: number;
}>;
export function computeMenuPosition(button: HTMLElement, menu: HTMLElement, onUpdate?: (x: number, y: number) => void): () => void;

// @public (undocumented)
export function connectedParticipantObserver(room: Room, identity: string, options?: ConnectedParticipantObserverOptions): Observable<RemoteParticipant | undefined>;
Expand Down
6 changes: 1 addition & 5 deletions packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1256,16 +1256,12 @@ export interface VideoTrackProps extends React_2.VideoHTMLAttributes<HTMLVideoEl

// @beta (undocumented)
export interface VoiceAssistant {
// (undocumented)
agent: RemoteParticipant | undefined;
// (undocumented)
agentAttributes: RemoteParticipant['attributes'] | undefined;
// (undocumented)
agentTranscriptions: ReceivedTranscriptionSegment[];
// (undocumented)
audioTrack: TrackReference_3 | undefined;
// (undocumented)
state: AgentState;
videoTrack: TrackReference_3 | undefined;
}

// @beta (undocumented)
Expand Down
44 changes: 42 additions & 2 deletions packages/react/src/hooks/useVoiceAssistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,29 @@ export type AgentState =
* @beta
*/
export interface VoiceAssistant {
/**
* The agent participant.
*/
agent: RemoteParticipant | undefined;
/**
* The current state of the agent.
*/
state: AgentState;
/**
* The microphone track published by the agent or associated avatar worker (if any).
*/
audioTrack: TrackReference | undefined;
/**
* The camera track published by the agent or associated avatar worker (if any).
*/
videoTrack: TrackReference | undefined;
/**
* The transcriptions of the agent's microphone track (if any).
*/
agentTranscriptions: ReceivedTranscriptionSegment[];
/**
* The agent's participant attributes.
*/
agentAttributes: RemoteParticipant['attributes'] | undefined;
}

Expand All @@ -42,8 +61,28 @@ const state_attribute = 'lk.agent.state';
* @beta
*/
export function useVoiceAssistant(): VoiceAssistant {
const agent = useRemoteParticipants().find((p) => p.kind === ParticipantKind.AGENT);
const audioTrack = useParticipantTracks([Track.Source.Microphone], agent?.identity)[0];
const remoteParticipants = useRemoteParticipants();
const agent = remoteParticipants.find(
(p) => p.kind === ParticipantKind.AGENT && !('lk.publish_on_behalf' in p.attributes),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are we documenting these attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably need a full reference of special attributes but for now this is going into the avatar integration docs https://github.com/livekit/web/pull/1165/files#diff-42c10a5d6ec7f1e0d9ade709732ba1d4c3697b6d411523ecdc5a2ac75a9457efR46

);
const worker = remoteParticipants.find(
(p) =>
p.kind === ParticipantKind.AGENT && p.attributes['lk.publish_on_behalf'] === agent?.identity,
);
const agentTracks = useParticipantTracks(
[Track.Source.Microphone, Track.Source.Camera],
agent?.identity,
);
const workerTracks = useParticipantTracks(
[Track.Source.Microphone, Track.Source.Camera],
worker?.identity,
);
const audioTrack =
agentTracks.find((t) => t.source === Track.Source.Microphone) ??
workerTracks.find((t) => t.source === Track.Source.Microphone);
const videoTrack =
agentTracks.find((t) => t.source === Track.Source.Camera) ??
workerTracks.find((t) => t.source === Track.Source.Camera);
const { segments: agentTranscriptions } = useTrackTranscription(audioTrack);
const connectionState = useConnectionState();
const { attributes } = useParticipantAttributes({ participant: agent });
Expand All @@ -66,6 +105,7 @@ export function useVoiceAssistant(): VoiceAssistant {
agent,
state,
audioTrack,
videoTrack,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't we simply want to return all tracks that the agent publishes?
not a fan of audioTrack in the first place, but adding videoTrack doesn't make it any better.
Generally we have microphoneTrack and cameraTrack in our glossary of constructs, but not sure how well that works for agents

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i don't have a strong opinion really. this seemed least disruptive and i think if you really want all the tracks you can get them from agent anyways

agentTranscriptions,
agentAttributes: attributes,
};
Expand Down