Skip to content

Commit 2f3b499

Browse files
authored
Merge pull request #1 from layercodedev/add-mute-functionality
add mute functionality
2 parents e7f785a + 808bfdd commit 2f3b499

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 UseLayercodeAgentOptions {
1313
onDisconnect?: () => void;
1414
onError?: (error: Error) => void;
1515
onDataMessage?: (data: any) => void;
16+
onMuteStateChange?: (isMuted: boolean) => void;
1617
onMessage?: (data: any) => void;
1718
}
1819

@@ -27,11 +28,12 @@ const useLayercodeAgent = (
2728
options: UseLayercodeAgentOptions & Record<string, any>
2829
) => {
2930
// Extract public options
30-
const { agentId, conversationId, authorizeSessionEndpoint, metadata = {}, onConnect, onDisconnect, onError, onDataMessage, onMessage } = options;
31+
const { agentId, conversationId, authorizeSessionEndpoint, metadata = {}, onConnect, onDisconnect, onError, onDataMessage, onMessage, onMuteStateChange } = options;
3132

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

@@ -68,13 +70,20 @@ const useLayercodeAgent = (
6870
onAgentAmplitudeChange: (amplitude: number) => {
6971
setAgentAudioAmplitude(amplitude);
7072
},
73+
onMuteStateChange: (muted: boolean) => {
74+
setIsMuted(muted);
75+
onMuteStateChange?.(muted);
76+
},
7177
});
7278

7379
// Pass the override websocket URL if provided. Use for local development.
7480
if (options['_websocketUrl']) {
7581
clientRef.current._websocketUrl = options['_websocketUrl'];
7682
}
7783

84+
// Set initial mute state from JS SDK
85+
setIsMuted(clientRef.current.isMuted);
86+
7887
// Connect to the agent
7988
clientRef.current.connect().catch((error: Error) => {
8089
console.error('Failed to connect to agent:', error);
@@ -91,10 +100,14 @@ const useLayercodeAgent = (
91100
}, [agentId, conversationId, authorizeSessionEndpoint]); // Make sure metadata isn't causing unnecessary re-renders if it changes often
92101

93102
// Class methods
94-
// TODO: Mic mute
95-
// const setMuteMic = useCallback((mute: boolean) => {
96-
// // clientRef.current?.setMuteMic(mute);
97-
// }, []);
103+
const mute = useCallback(() => {
104+
clientRef.current?.mute();
105+
}, []);
106+
107+
const unmute = useCallback(() => {
108+
clientRef.current?.unmute();
109+
}, []);
110+
98111
const triggerUserTurnStarted = useCallback(() => {
99112
clientRef.current?.triggerUserTurnStarted();
100113
}, []);
@@ -115,11 +128,14 @@ const useLayercodeAgent = (
115128
triggerUserTurnFinished,
116129
connect,
117130
disconnect,
131+
mute,
132+
unmute,
118133

119134
// State
120135
status,
121136
userAudioAmplitude,
122137
agentAudioAmplitude,
138+
isMuted,
123139
};
124140
};
125141

0 commit comments

Comments
 (0)