Skip to content

Commit eb61df2

Browse files
committed
add audioOutput state management that can mute or unmute audio output
1 parent e45ff9e commit eb61df2

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect, useCallback, useRef } from 'react';
2-
import LayercodeClient, { type AgentConfig, type AuthorizeSessionRequest } from '@layercode/js-sdk';
2+
import LayercodeClient, { type AgentConfig, type AuthorizeSessionRequest } from 'layercode-js-sdk';
33

44
/**
55
* Configuration options for the useLayercodeAgent hook.
@@ -20,7 +20,9 @@ interface UseLayercodeAgentOptions {
2020
onAgentSpeakingChange?: (isSpeaking: boolean) => void;
2121

2222
audioInput?: boolean;
23+
audioOutput?: boolean;
2324
onAudioInputChanged?: (audioInput: boolean) => void;
25+
onAudioOutputChanged?: (audioOutput: boolean) => void;
2426
enableAmplitudeMonitoring?: boolean;
2527
}
2628

@@ -50,6 +52,7 @@ const useLayercodeAgent = (
5052
onUserSpeakingChange,
5153
onAgentSpeakingChange,
5254
onAudioInputChanged,
55+
onAudioOutputChanged,
5356
} = options;
5457
const websocketUrlOverride = options['_websocketUrl'];
5558
const enableAmplitudeMonitoring = options.enableAmplitudeMonitoring ?? true;
@@ -60,6 +63,7 @@ const useLayercodeAgent = (
6063
const [userSpeaking, setUserSpeaking] = useState(false);
6164
const [agentSpeaking, setAgentSpeaking] = useState(false);
6265
const [audioInput, _setAudioInput] = useState<boolean>(options.audioInput ?? true);
66+
const [audioOutput, _setAudioOutput] = useState<boolean>(options.audioOutput ?? true);
6367
const [isMuted, setIsMuted] = useState(false);
6468
const [internalConversationId, setInternalConversationId] = useState<string | null | undefined>(conversationId);
6569
const conversationIdRef = useRef<string | undefined>(conversationId);
@@ -92,10 +96,15 @@ const useLayercodeAgent = (
9296
authorizeSessionRequest,
9397
metadata,
9498
audioInput,
99+
audioOutput,
95100
audioInputChanged: (next: boolean) => {
96101
_setAudioInput(next);
97102
onAudioInputChanged?.(next);
98103
},
104+
audioOutputChanged: (next: boolean) => {
105+
_setAudioOutput(next);
106+
onAudioOutputChanged?.(next);
107+
},
99108
onConnect: ({ conversationId, config }: { conversationId: string | null; config?: AgentConfig }) => {
100109
setInternalConversationId((current) => {
101110
if (conversationIdRef.current === undefined) {
@@ -172,7 +181,9 @@ const useLayercodeAgent = (
172181
onAgentSpeakingChange,
173182
onAudioInputChanged,
174183
websocketUrlOverride,
184+
onAudioOutputChanged,
175185
audioInput,
186+
audioOutput,
176187
enableAmplitudeMonitoring,
177188
]
178189
);
@@ -204,6 +215,15 @@ const useLayercodeAgent = (
204215
[_setAudioInput, clientRef, audioInput]
205216
);
206217

218+
const setAudioOutput = useCallback(
219+
(state: React.SetStateAction<boolean>) => {
220+
_setAudioOutput(state);
221+
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioOutput) : state;
222+
clientRef.current?.setAudioOutput(next);
223+
},
224+
[_setAudioOutput, clientRef, audioOutput]
225+
);
226+
207227
const triggerUserTurnStarted = useCallback(() => {
208228
clientRef.current?.triggerUserTurnStarted();
209229
}, []);
@@ -264,6 +284,7 @@ const useLayercodeAgent = (
264284
sendClientResponseText,
265285

266286
setAudioInput,
287+
setAudioOutput,
267288

268289
// State
269290
status,
@@ -274,6 +295,7 @@ const useLayercodeAgent = (
274295
isMuted,
275296
conversationId: internalConversationId,
276297
audioInput,
298+
audioOutput,
277299
};
278300
};
279301

0 commit comments

Comments
 (0)