yt-chat-components 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yt-chat-components",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "main": "build/static/js/bundle.min.js",
5
5
  "module": "build/static/js/bundle.min.js",
6
6
  "types": "build/static/js/index.d.ts",
@@ -44,6 +44,8 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
44
44
  const isSpeakingRef = useRef<boolean>(false);
45
45
  const SILENCE_THRESHOLD = 35; // 静音阈值,可根据环境调整
46
46
  const SPEECH_DELAY = 1500; // 停止说话多久后认为一段话结束(毫秒)
47
+ // 发送信息后持有这个id,当id不为null放弃处理后续说话
48
+ const msgIdRef = useRef<string | null>(null);
47
49
 
48
50
  // WebRTC相关状态和引用
49
51
  const peerConnectionRef = useRef<RTCPeerConnection | null>(null);
@@ -91,17 +93,17 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
91
93
 
92
94
  // 监听用户打断
93
95
  audio.onplay = () => {
94
- // 设置打断检测
96
+ // 设置打断检测,3秒后才启动
95
97
  const interruptionCheck = setInterval(() => {
96
98
  if (isSpeakingRef.current) {
97
99
  // 用户开始说话,打断播放
98
- console.log('用户打断播放');
100
+ // console.log('用户打断播放');
99
101
  audio.pause();
100
102
  URL.revokeObjectURL(audioUrl);
101
103
  clearInterval(interruptionCheck);
102
104
  setWorkStatus('正在聆听');
103
105
  }
104
- }, 100);
106
+ }, 3000);
105
107
 
106
108
  // 播放结束时清除检测
107
109
  audio.onended = () => {
@@ -134,7 +136,7 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
134
136
  const {code} = userInfo;
135
137
  const opeartorId = code || sessionId;
136
138
  // 创建WebSocket连接
137
- console.log('开始连接WebSocket', flowId, sessionId, api_key);
139
+ console.log('开始连接WebSocket');
138
140
  const ws = new WebSocket(`${hostUrl.replace('http', 'ws')}/api/v1/ws/webrtc?flow_id=${flowId}&session_id=${sessionId}&api_key=${api_key}&operator_id=${opeartorId}`);
139
141
  wsRef.current = ws;
140
142
 
@@ -199,7 +201,14 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
199
201
  vadProcessorRef.current = vadProcessor;
200
202
 
201
203
  vadProcessor.onaudioprocess = (e) => {
202
- if (workStatus === '正在回复' || workStatus === '正在思考') return; // 正在听对方说话 or 思考,不处理
204
+ // 正在听对方说话 or 思考,不处理
205
+ if (workStatus === '正在回复'){
206
+ console.log("--------------------- 正在回复")
207
+ return
208
+ }else if(msgIdRef.current !== null){
209
+ console.log("--------------------- 正在思考")
210
+ return;
211
+ }
203
212
 
204
213
  analyser.getByteFrequencyData(dataArray);
205
214
 
@@ -216,7 +225,6 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
216
225
  if (isSpeakingNow && !isSpeakingRef.current) {
217
226
  setIsSpeakingNow(true)
218
227
  // 开始说话
219
- console.log('开始说话', isSpeakingRef.current);
220
228
  isSpeakingRef.current = true;
221
229
 
222
230
  // 开始录制
@@ -255,12 +263,10 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
255
263
  }
256
264
  }
257
265
  else if (!isSpeakingNow && isSpeakingRef.current) {
258
- console.log('准备停止说话');
259
266
  // 可能停止说话,设置超时
260
267
  if (!speechTimeoutRef.current) {
261
268
  speechTimeoutRef.current = setTimeout(() => {
262
269
  // 确认停止说话
263
- console.log('停止说话');
264
270
  isSpeakingRef.current = false;
265
271
 
266
272
  // 停止录制
@@ -272,15 +278,15 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
272
278
  reader.readAsDataURL(blob);
273
279
  reader.onloadend = () => {
274
280
  const base64Audio = reader.result as string;
275
- console.log('准备发送');
276
281
  // 发送完整的语音片段到服务器
277
282
  if (wsRef.current) {
278
- console.log('发送数据');
283
+ msgIdRef.current = new Date().getTime() + ""
279
284
  sendWebSocketMessage('complete-audio', {
280
285
  audioData: base64Audio.split(',')[1], // 移除data URL前缀
281
286
  userMessage: true,
282
287
  audioFormat:"mp3",
283
- sampleRate:16000
288
+ sampleRate:16000,
289
+ msgId: msgIdRef.current,
284
290
  });
285
291
  console.log('发送完毕');
286
292
 
@@ -342,11 +348,12 @@ const CallInterface: React.FC<CallInterfaceProps> = ({
342
348
  }
343
349
  } else if (message.type === 'complete-audio') {
344
350
  // 处理从后端返回的完整音频响应
351
+ msgIdRef.current = null
345
352
  if (message.audioData) {
346
353
  handleAudioData(message.audioData);
354
+ }else {
355
+ setWorkStatus('正在聆听');
347
356
  }
348
- } else if (message.type === 'busy') {
349
- message.info('对方正在思考,请稍等');
350
357
  }
351
358
  };
352
359