@@ -16,6 +16,12 @@ interface UseLayercodeAgentOptions {
1616 onDataMessage ?: ( data : any ) => void ;
1717 onMuteStateChange ?: ( isMuted : boolean ) => void ;
1818 onMessage ?: ( data : any ) => void ;
19+ onUserSpeakingChange ?: ( isSpeaking : boolean ) => void ;
20+ onAgentSpeakingChange ?: ( isSpeaking : boolean ) => void ;
21+
22+ audioInput ?: boolean ;
23+ onAudioInputChanged ?: ( audioInput : boolean ) => void ;
24+ enableAmplitudeMonitoring ?: boolean ;
1925}
2026
2127/**
@@ -41,12 +47,19 @@ const useLayercodeAgent = (
4147 onDataMessage,
4248 onMessage,
4349 onMuteStateChange,
50+ onUserSpeakingChange,
51+ onAgentSpeakingChange,
52+ onAudioInputChanged,
4453 } = options ;
4554 const websocketUrlOverride = options [ '_websocketUrl' ] ;
55+ const enableAmplitudeMonitoring = options . enableAmplitudeMonitoring ?? true ;
4656
4757 const [ status , setStatus ] = useState ( 'initializing' ) ;
4858 const [ userAudioAmplitude , setUserAudioAmplitude ] = useState ( 0 ) ;
4959 const [ agentAudioAmplitude , setAgentAudioAmplitude ] = useState ( 0 ) ;
60+ const [ userSpeaking , setUserSpeaking ] = useState ( false ) ;
61+ const [ agentSpeaking , setAgentSpeaking ] = useState ( false ) ;
62+ const [ audioInput , _setAudioInput ] = useState < boolean > ( options . audioInput ?? true ) ;
5063 const [ isMuted , setIsMuted ] = useState ( false ) ;
5164 const [ internalConversationId , setInternalConversationId ] = useState < string | null | undefined > ( conversationId ) ;
5265 const conversationIdRef = useRef < string | undefined > ( conversationId ) ;
@@ -62,6 +75,13 @@ const useLayercodeAgent = (
6275 }
6376 } , [ conversationId ] ) ;
6477
78+ useEffect ( ( ) => {
79+ if ( ! enableAmplitudeMonitoring ) {
80+ setUserAudioAmplitude ( 0 ) ;
81+ setAgentAudioAmplitude ( 0 ) ;
82+ }
83+ } , [ enableAmplitudeMonitoring ] ) ;
84+
6585 const createClient = useCallback (
6686 ( initialConversationId : string | null ) => {
6787 console . log ( 'Creating LayercodeClient instance' ) ;
@@ -71,6 +91,11 @@ const useLayercodeAgent = (
7191 authorizeSessionEndpoint,
7292 authorizeSessionRequest,
7393 metadata,
94+ audioInput,
95+ audioInputChanged : ( next : boolean ) => {
96+ _setAudioInput ( next ) ;
97+ onAudioInputChanged ?.( next ) ;
98+ } ,
7499 onConnect : ( { conversationId, config } : { conversationId : string | null ; config ?: AgentConfig } ) => {
75100 setInternalConversationId ( ( current ) => {
76101 if ( conversationIdRef . current === undefined ) {
@@ -95,22 +120,39 @@ const useLayercodeAgent = (
95120 onStatusChange : ( newStatus : string ) => {
96121 setStatus ( newStatus ) ;
97122 } ,
98- onUserAmplitudeChange : ( amplitude : number ) => {
99- setUserAudioAmplitude ( amplitude ) ;
123+ onUserAmplitudeChange : enableAmplitudeMonitoring
124+ ? ( amplitude : number ) => {
125+ setUserAudioAmplitude ( amplitude ) ;
126+ }
127+ : undefined ,
128+ onAgentAmplitudeChange : enableAmplitudeMonitoring
129+ ? ( amplitude : number ) => {
130+ setAgentAudioAmplitude ( amplitude ) ;
131+ }
132+ : undefined ,
133+ onUserIsSpeakingChange : ( isSpeaking : boolean ) => {
134+ setUserSpeaking ( isSpeaking ) ;
135+ onUserSpeakingChange ?.( isSpeaking ) ;
100136 } ,
101- onAgentAmplitudeChange : ( amplitude : number ) => {
102- setAgentAudioAmplitude ( amplitude ) ;
137+ onAgentSpeakingChange : ( isSpeaking : boolean ) => {
138+ setAgentSpeaking ( isSpeaking ) ;
139+ onAgentSpeakingChange ?.( isSpeaking ) ;
103140 } ,
104141 onMuteStateChange : ( muted : boolean ) => {
105142 setIsMuted ( muted ) ;
106143 onMuteStateChange ?.( muted ) ;
107144 } ,
145+ enableAmplitudeMonitoring,
108146 } ) ;
109147
110148 if ( websocketUrlOverride ) {
111149 client . _websocketUrl = websocketUrlOverride ;
112150 }
113151
152+ setUserSpeaking ( false ) ;
153+ onUserSpeakingChange ?.( false ) ;
154+ setAgentSpeaking ( false ) ;
155+ onAgentSpeakingChange ?.( false ) ;
114156 setIsMuted ( client . isMuted ) ;
115157 clientRef . current = client ;
116158 return client ;
@@ -126,7 +168,12 @@ const useLayercodeAgent = (
126168 onError ,
127169 onMessage ,
128170 onMuteStateChange ,
171+ onUserSpeakingChange ,
172+ onAgentSpeakingChange ,
173+ onAudioInputChanged ,
129174 websocketUrlOverride ,
175+ audioInput ,
176+ enableAmplitudeMonitoring ,
130177 ]
131178 ) ;
132179
@@ -148,6 +195,15 @@ const useLayercodeAgent = (
148195 clientRef . current ?. unmute ( ) ;
149196 } , [ ] ) ;
150197
198+ const setAudioInput = useCallback (
199+ ( state : React . SetStateAction < boolean > ) => {
200+ _setAudioInput ( state ) ;
201+ const next = typeof state === 'function' ? ( state as ( prev : boolean ) => boolean ) ( audioInput ) : state ;
202+ clientRef . current ?. setAudioInput ( next ) ;
203+ } ,
204+ [ _setAudioInput , clientRef , audioInput ]
205+ ) ;
206+
151207 const triggerUserTurnStarted = useCallback ( ( ) => {
152208 clientRef . current ?. triggerUserTurnStarted ( ) ;
153209 } , [ ] ) ;
@@ -207,12 +263,17 @@ const useLayercodeAgent = (
207263 unmute,
208264 sendClientResponseText,
209265
266+ setAudioInput,
267+
210268 // State
211269 status,
212270 userAudioAmplitude,
213271 agentAudioAmplitude,
272+ userSpeaking,
273+ agentSpeaking,
214274 isMuted,
215275 conversationId : internalConversationId ,
276+ audioInput,
216277 } ;
217278} ;
218279
0 commit comments