@layercode/js-sdk 1.0.14 → 1.0.15

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.
@@ -3480,7 +3480,6 @@ registerProcessor('audio_processor', AudioProcessor);
3480
3480
  sessionId: options.sessionId || null,
3481
3481
  authorizeSessionEndpoint: options.authorizeSessionEndpoint,
3482
3482
  metadata: options.metadata || {},
3483
- vadEnabled: options.vadEnabled || true,
3484
3483
  vadResumeDelay: options.vadResumeDelay || 500,
3485
3484
  onConnect: options.onConnect || (() => { }),
3486
3485
  onDisconnect: options.onDisconnect || (() => { }),
@@ -3498,7 +3497,22 @@ registerProcessor('audio_processor', AudioProcessor);
3498
3497
  sampleRate: 16000, // TODO should be set my fetched pipeline config
3499
3498
  });
3500
3499
  this.vad = null;
3501
- if (this.options.vadEnabled) {
3500
+ this.ws = null;
3501
+ this.status = 'disconnected';
3502
+ this.userAudioAmplitude = 0;
3503
+ this.agentAudioAmplitude = 0;
3504
+ this.sessionId = options.sessionId || null;
3505
+ this.pushToTalkActive = false;
3506
+ this.vadPausedPlayer = false;
3507
+ this.pushToTalkEnabled = false;
3508
+ this.canInterrupt = false;
3509
+ // Bind event handlers
3510
+ this._handleWebSocketMessage = this._handleWebSocketMessage.bind(this);
3511
+ this._handleDataAvailable = this._handleDataAvailable.bind(this);
3512
+ }
3513
+ _initializeVAD() {
3514
+ console.log('initializing VAD', { pushToTalkEnabled: this.pushToTalkEnabled, canInterrupt: this.canInterrupt });
3515
+ if (!this.pushToTalkEnabled && this.canInterrupt) {
3502
3516
  dist.MicVAD.new({
3503
3517
  stream: this.wavRecorder.getStream() || undefined,
3504
3518
  model: 'v5',
@@ -3557,16 +3571,6 @@ registerProcessor('audio_processor', AudioProcessor);
3557
3571
  console.error('Error initializing VAD:', error);
3558
3572
  });
3559
3573
  }
3560
- this.ws = null;
3561
- this.status = 'disconnected';
3562
- this.userAudioAmplitude = 0;
3563
- this.agentAudioAmplitude = 0;
3564
- this.sessionId = options.sessionId || null;
3565
- this.pushToTalkActive = false;
3566
- this.vadPausedPlayer = false;
3567
- // Bind event handlers
3568
- this._handleWebSocketMessage = this._handleWebSocketMessage.bind(this);
3569
- this._handleDataAvailable = this._handleDataAvailable.bind(this);
3570
3574
  }
3571
3575
  /**
3572
3576
  * Updates the connection status and triggers the callback
@@ -3626,10 +3630,9 @@ registerProcessor('audio_processor', AudioProcessor);
3626
3630
  // Sent from the server to this client when a new user turn is detected
3627
3631
  console.log('received turn.start from server');
3628
3632
  console.log(message);
3629
- // if (message.role === 'user' && !this.pushToTalkActive) {
3630
- if (message.role === 'user') {
3633
+ if (message.role === 'user' && !this.pushToTalkEnabled && this.canInterrupt) {
3631
3634
  // Interrupt any playing assistant audio if this is a turn trigged by the server (and not push to talk, which will have already called interrupt)
3632
- console.log('interrupting assistant audio, as user turn has started and pushToTalkActive is false');
3635
+ console.log('interrupting assistant audio, as user turn has started and pushToTalkEnabled is false');
3633
3636
  await this._clientInterruptAssistantReplay();
3634
3637
  }
3635
3638
  // if (message.role === 'assistant') {
@@ -3743,6 +3746,19 @@ registerProcessor('audio_processor', AudioProcessor);
3743
3746
  this.ws = new WebSocket(`${this._websocketUrl}?${new URLSearchParams({
3744
3747
  client_session_key: authorizeSessionResponseBody.client_session_key,
3745
3748
  })}`);
3749
+ const config = authorizeSessionResponseBody.config;
3750
+ console.log('config', config);
3751
+ if (config.transcription.trigger === 'push_to_talk') {
3752
+ this.pushToTalkEnabled = true;
3753
+ }
3754
+ else if (config.transcription.trigger === 'automatic') {
3755
+ this.pushToTalkEnabled = false;
3756
+ this.canInterrupt = config.transcription.can_interrupt;
3757
+ }
3758
+ else {
3759
+ throw new Error(`Unknown trigger: ${config.transcription.trigger}`);
3760
+ }
3761
+ this._initializeVAD();
3746
3762
  // Bind the websocket message callbacks
3747
3763
  this.ws.onmessage = this._handleWebSocketMessage;
3748
3764
  this.ws.onopen = () => {