@layercode/js-sdk 1.0.17 → 1.0.19

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.
@@ -3501,6 +3501,9 @@ class LayercodeClient {
3501
3501
  this.pushToTalkEnabled = false;
3502
3502
  this.canInterrupt = false;
3503
3503
  this.userIsSpeaking = false;
3504
+ this.endUserTurn = false;
3505
+ this.recorderStarted = false;
3506
+ this.readySent = false;
3504
3507
  // Bind event handlers
3505
3508
  this._handleWebSocketMessage = this._handleWebSocketMessage.bind(this);
3506
3509
  this._handleDataAvailable = this._handleDataAvailable.bind(this);
@@ -3534,11 +3537,7 @@ class LayercodeClient {
3534
3537
  this.userIsSpeaking = false;
3535
3538
  },
3536
3539
  onSpeechEnd: () => {
3537
- this.userIsSpeaking = false;
3538
- this._wsSend({
3539
- type: 'vad_events',
3540
- event: 'vad_end',
3541
- });
3540
+ this.endUserTurn = true; // Set flag to indicate that the user turn has ended, so we can send a vad_end event to the server
3542
3541
  },
3543
3542
  })
3544
3543
  .then((vad) => {
@@ -3714,6 +3713,14 @@ class LayercodeClient {
3714
3713
  type: 'client.audio',
3715
3714
  content: base64,
3716
3715
  });
3716
+ if (this.endUserTurn) {
3717
+ this.endUserTurn = false;
3718
+ this.userIsSpeaking = false; // Reset userIsSpeaking to false so we don't send any more audio to the server
3719
+ this._wsSend({
3720
+ type: 'vad_events',
3721
+ event: 'vad_end',
3722
+ });
3723
+ }
3717
3724
  }
3718
3725
  }
3719
3726
  catch (error) {
@@ -3731,6 +3738,13 @@ class LayercodeClient {
3731
3738
  this.ws.send(messageString);
3732
3739
  }
3733
3740
  }
3741
+ _sendReadyIfNeeded() {
3742
+ var _a;
3743
+ if (this.recorderStarted && ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN && !this.readySent) {
3744
+ this._wsSend({ type: 'client.ready' });
3745
+ this.readySent = true;
3746
+ }
3747
+ }
3734
3748
  /**
3735
3749
  * Sets up amplitude monitoring for a given audio source.
3736
3750
  * @param {WavRecorder | WavStreamPlayer} source - The audio source (recorder or player).
@@ -3806,6 +3820,8 @@ class LayercodeClient {
3806
3820
  console.log('WebSocket connection established');
3807
3821
  this._setStatus('connected');
3808
3822
  this.options.onConnect({ sessionId: this.sessionId });
3823
+ // Attempt to send ready message if recorder already started
3824
+ this._sendReadyIfNeeded();
3809
3825
  };
3810
3826
  this.ws.onclose = () => {
3811
3827
  console.log('WebSocket connection closed');
@@ -3826,6 +3842,9 @@ class LayercodeClient {
3826
3842
  await this.wavPlayer.connect();
3827
3843
  // Set up audio player amplitude monitoring
3828
3844
  this._setupAmplitudeMonitoring(this.wavPlayer, this.options.onAgentAmplitudeChange, (amp) => (this.agentAudioAmplitude = amp));
3845
+ // Mark recorder as started and attempt to notify server
3846
+ this.recorderStarted = true;
3847
+ this._sendReadyIfNeeded();
3829
3848
  }
3830
3849
  catch (error) {
3831
3850
  console.error('Error connecting to Layercode pipeline:', error);