Skip to content

Commit 109d3a2

Browse files
committed
add mute functionality
1 parent f0143a3 commit 109d3a2

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface UseLayercodePipelineOptions {
1313
onDisconnect?: () => void;
1414
onError?: (error: Error) => void;
1515
onDataMessage?: (data: any) => void;
16+
onMuteStateChange?: (isMuted: boolean) => void;
1617
}
1718

1819
/**
@@ -26,11 +27,12 @@ const useLayercodePipeline = (
2627
options: UseLayercodePipelineOptions & Record<string, any>
2728
) => {
2829
// Extract public options
29-
const { pipelineId, sessionId, authorizeSessionEndpoint, metadata = {}, onConnect, onDisconnect, onError, onDataMessage } = options;
30+
const { pipelineId, sessionId, authorizeSessionEndpoint, metadata = {}, onConnect, onDisconnect, onError, onDataMessage, onMuteStateChange } = options;
3031

3132
const [status, setStatus] = useState('initializing');
3233
const [userAudioAmplitude, setUserAudioAmplitude] = useState(0);
3334
const [agentAudioAmplitude, setAgentAudioAmplitude] = useState(0);
35+
const [isMuted, setIsMuted] = useState(false);
3436
// Reference to the LayercodeClient instance
3537
const clientRef = useRef<LayercodeClient | null>(null);
3638

@@ -64,13 +66,20 @@ const useLayercodePipeline = (
6466
onAgentAmplitudeChange: (amplitude: number) => {
6567
setAgentAudioAmplitude(amplitude);
6668
},
69+
onMuteStateChange: (muted: boolean) => {
70+
setIsMuted(muted);
71+
onMuteStateChange?.(muted);
72+
},
6773
});
6874

6975
// Pass the override websocket URL if provided. Use for local development.
7076
if (options['_websocketUrl']) {
7177
clientRef.current._websocketUrl = options['_websocketUrl'];
7278
}
7379

80+
// Set initial mute state from JS SDK
81+
setIsMuted(clientRef.current.isMuted);
82+
7483
// Connect to the pipeline
7584
clientRef.current.connect().catch((error: Error) => {
7685
console.error('Failed to connect to pipeline:', error);
@@ -87,10 +96,14 @@ const useLayercodePipeline = (
8796
}, [pipelineId, sessionId, authorizeSessionEndpoint]); // Make sure metadata isn't causing unnecessary re-renders if it changes often
8897

8998
// Class methods
90-
// TODO: Mic mute
91-
// const setMuteMic = useCallback((mute: boolean) => {
92-
// // clientRef.current?.setMuteMic(mute);
93-
// }, []);
99+
const mute = useCallback(() => {
100+
clientRef.current?.mute();
101+
}, []);
102+
103+
const unmute = useCallback(() => {
104+
clientRef.current?.unmute();
105+
}, []);
106+
94107
const triggerUserTurnStarted = useCallback(() => {
95108
clientRef.current?.triggerUserTurnStarted();
96109
}, []);
@@ -111,11 +124,14 @@ const useLayercodePipeline = (
111124
triggerUserTurnFinished,
112125
connect,
113126
disconnect,
127+
mute,
128+
unmute,
114129

115130
// State
116131
status,
117132
userAudioAmplitude,
118133
agentAudioAmplitude,
134+
isMuted,
119135
};
120136
};
121137

0 commit comments

Comments
 (0)