Skip to content

Commit 5a24f89

Browse files
committed
allow toggling mic on/off and disable amplitude updates when inactive; bump JS SDK dependency
1 parent 879f34d commit 5a24f89

File tree

3 files changed

+103
-23
lines changed

3 files changed

+103
-23
lines changed

package-lock.json

Lines changed: 37 additions & 18 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.3.1"
42+
"@layercode/js-sdk": "^2.3.2"
4343
},
4444
"devDependencies": {
4545
"@rollup/plugin-commonjs": "^25.0.8",

src/index.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)