Skip to content

Commit c2585d4

Browse files
authored
Merge pull request #12 from layercodedev/add-audio-output-state
add audioOutput state management that can mute or unmute audio output
2 parents 92e770a + f45a746 commit c2585d4

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"access": "public"
4040
},
4141
"dependencies": {
42-
"@layercode/js-sdk": "^2.4.0"
42+
"@layercode/js-sdk": "^2.5.0"
4343
},
4444
"devDependencies": {
4545
"@rollup/plugin-commonjs": "^25.0.8",

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}, []);
@@ -268,6 +288,7 @@ const useLayercodeAgent = (
268288
sendClientResponseData,
269289

270290
setAudioInput,
291+
setAudioOutput,
271292

272293
// State
273294
status,
@@ -278,6 +299,7 @@ const useLayercodeAgent = (
278299
isMuted,
279300
conversationId: internalConversationId,
280301
audioInput,
302+
audioOutput,
281303
};
282304
};
283305

0 commit comments

Comments
 (0)