@layercode/js-sdk 2.8.0 → 2.8.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.
@@ -5951,6 +5951,7 @@ registerProcessor('audio_processor', AudioProcessor);
5951
5951
  this._emitAudioInput();
5952
5952
  this.AMPLITUDE_MONITORING_SAMPLE_RATE = 2;
5953
5953
  this._websocketUrl = DEFAULT_WS_URL;
5954
+ this.audioOutputReady = null;
5954
5955
  this.wavRecorder = new WavRecorder({ sampleRate: 8000 }); // TODO should be set my fetched agent config
5955
5956
  this.wavPlayer = new WavStreamPlayer({
5956
5957
  finishedPlayingCallback: this._clientResponseAudioReplayFinished.bind(this),
@@ -6112,12 +6113,19 @@ registerProcessor('audio_processor', AudioProcessor);
6112
6113
  this.status = status;
6113
6114
  this.options.onStatusChange(status);
6114
6115
  }
6116
+ async _waitForAudioOutputReady() {
6117
+ if (!this.audioOutputReady) {
6118
+ return;
6119
+ }
6120
+ await this.audioOutputReady;
6121
+ }
6115
6122
  _setAgentSpeaking(isSpeaking) {
6116
- if (this.agentIsSpeaking === isSpeaking) {
6123
+ const shouldReportSpeaking = this.audioOutput && isSpeaking;
6124
+ if (this.agentIsSpeaking === shouldReportSpeaking) {
6117
6125
  return;
6118
6126
  }
6119
- this.agentIsSpeaking = isSpeaking;
6120
- this.options.onAgentSpeakingChange(isSpeaking);
6127
+ this.agentIsSpeaking = shouldReportSpeaking;
6128
+ this.options.onAgentSpeakingChange(shouldReportSpeaking);
6121
6129
  }
6122
6130
  _setUserSpeaking(isSpeaking) {
6123
6131
  const shouldReportSpeaking = this._shouldCaptureUserAudio() && isSpeaking;
@@ -6185,7 +6193,6 @@ registerProcessor('audio_processor', AudioProcessor);
6185
6193
  if (message.role === 'assistant') {
6186
6194
  // Start tracking new agent turn
6187
6195
  console.debug('Agent turn started, will track new turn ID from audio/text');
6188
- this._setAgentSpeaking(true);
6189
6196
  this._setUserSpeaking(false);
6190
6197
  }
6191
6198
  else if (message.role === 'user' && !this.pushToTalkEnabled) {
@@ -6207,6 +6214,7 @@ registerProcessor('audio_processor', AudioProcessor);
6207
6214
  break;
6208
6215
  }
6209
6216
  case 'response.audio':
6217
+ await this._waitForAudioOutputReady();
6210
6218
  this._setAgentSpeaking(true);
6211
6219
  const audioBuffer = base64ToArrayBuffer(message.content);
6212
6220
  this.wavPlayer.add16BitPCM(audioBuffer, message.turn_id);
@@ -6433,6 +6441,7 @@ registerProcessor('audio_processor', AudioProcessor);
6433
6441
  }
6434
6442
  else {
6435
6443
  this.wavPlayer.mute();
6444
+ this._setAgentSpeaking(false);
6436
6445
  }
6437
6446
  }
6438
6447
  }
@@ -6491,7 +6500,9 @@ registerProcessor('audio_processor', AudioProcessor);
6491
6500
  this.setupVadConfig(config);
6492
6501
  // Bind the websocket message callbacks
6493
6502
  this.bindWebsocketMessageCallbacks(ws, config);
6494
- await this.setupAudioOutput();
6503
+ const audioOutputReady = this.setupAudioOutput();
6504
+ this.audioOutputReady = audioOutputReady;
6505
+ await audioOutputReady;
6495
6506
  }
6496
6507
  catch (error) {
6497
6508
  console.error('Error connecting to Layercode agent:', error);