Skip to content

Commit ca0e26d

Browse files
committed
state management setup
1 parent 879f34d commit ca0e26d

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/index.ts

Lines changed: 47 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.
@@ -16,6 +16,11 @@ interface UseLayercodeAgentOptions {
1616
onDataMessage?: (data: any) => void;
1717
onMuteStateChange?: (isMuted: boolean) => void;
1818
onMessage?: (data: any) => void;
19+
20+
audioInput?: boolean;
21+
audioOutput?: boolean;
22+
onAudioInputChanged?: (audioInput: boolean) => void;
23+
onAudioOutputChanged?: (audioOutput: boolean) => void;
1924
}
2025

2126
/**
@@ -41,12 +46,16 @@ const useLayercodeAgent = (
4146
onDataMessage,
4247
onMessage,
4348
onMuteStateChange,
49+
onAudioInputChanged,
50+
onAudioOutputChanged,
4451
} = options;
4552
const websocketUrlOverride = options['_websocketUrl'];
4653

4754
const [status, setStatus] = useState('initializing');
4855
const [userAudioAmplitude, setUserAudioAmplitude] = useState(0);
4956
const [agentAudioAmplitude, setAgentAudioAmplitude] = useState(0);
57+
const [audioInput, _setAudioInput] = useState<boolean>(options.audioInput ?? true);
58+
const [audioOutput, _setAudioOutput] = useState<boolean>(options.audioOutput ?? true);
5059
const [isMuted, setIsMuted] = useState(false);
5160
const [internalConversationId, setInternalConversationId] = useState<string | null | undefined>(conversationId);
5261
const conversationIdRef = useRef<string | undefined>(conversationId);
@@ -71,6 +80,16 @@ const useLayercodeAgent = (
7180
authorizeSessionEndpoint,
7281
authorizeSessionRequest,
7382
metadata,
83+
audioInput,
84+
audioOutput,
85+
audioInputChanged: (next: boolean) => {
86+
_setAudioInput(next);
87+
onAudioInputChanged?.(next);
88+
},
89+
audioOutputChanged: (next: boolean) => {
90+
_setAudioOutput(next);
91+
onAudioOutputChanged?.(next);
92+
},
7493
onConnect: ({ conversationId, config }: { conversationId: string | null; config?: AgentConfig }) => {
7594
setInternalConversationId((current) => {
7695
if (conversationIdRef.current === undefined) {
@@ -126,7 +145,11 @@ const useLayercodeAgent = (
126145
onError,
127146
onMessage,
128147
onMuteStateChange,
148+
onAudioInputChanged,
149+
onAudioOutputChanged,
129150
websocketUrlOverride,
151+
audioInput,
152+
audioOutput,
130153
]
131154
);
132155

@@ -148,6 +171,24 @@ const useLayercodeAgent = (
148171
clientRef.current?.unmute();
149172
}, []);
150173

174+
const setAudioInput = useCallback(
175+
(state: React.SetStateAction<boolean>) => {
176+
_setAudioInput(state);
177+
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioInput) : state;
178+
clientRef.current?.setAudioInput(next);
179+
},
180+
[_setAudioInput, clientRef, audioInput]
181+
);
182+
183+
const setAudioOutput = useCallback(
184+
(state: React.SetStateAction<boolean>) => {
185+
_setAudioOutput(state);
186+
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioOutput) : state;
187+
clientRef.current?.setAudioOutput(next);
188+
},
189+
[_setAudioOutput, clientRef, audioOutput]
190+
);
191+
151192
const triggerUserTurnStarted = useCallback(() => {
152193
clientRef.current?.triggerUserTurnStarted();
153194
}, []);
@@ -207,12 +248,17 @@ const useLayercodeAgent = (
207248
unmute,
208249
sendClientResponseText,
209250

251+
setAudioInput,
252+
setAudioOutput,
253+
210254
// State
211255
status,
212256
userAudioAmplitude,
213257
agentAudioAmplitude,
214258
isMuted,
215259
conversationId: internalConversationId,
260+
audioInput,
261+
audioOutput,
216262
};
217263
};
218264

0 commit comments

Comments
 (0)