@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.
@@ -3474,7 +3474,6 @@ class LayercodeClient {
3474
3474
  sessionId: options.sessionId || null,
3475
3475
  authorizeSessionEndpoint: options.authorizeSessionEndpoint,
3476
3476
  metadata: options.metadata || {},
3477
- vadEnabled: options.vadEnabled || true,
3478
3477
  vadResumeDelay: options.vadResumeDelay || 500,
3479
3478
  onConnect: options.onConnect || (() => { }),
3480
3479
  onDisconnect: options.onDisconnect || (() => { }),
@@ -3492,7 +3491,22 @@ class LayercodeClient {
3492
3491
  sampleRate: 16000, // TODO should be set my fetched pipeline config
3493
3492
  });
3494
3493
  this.vad = null;
3495
- if (this.options.vadEnabled) {
3494
+ this.ws = null;
3495
+ this.status = 'disconnected';
3496
+ this.userAudioAmplitude = 0;
3497
+ this.agentAudioAmplitude = 0;
3498
+ this.sessionId = options.sessionId || null;
3499
+ this.pushToTalkActive = false;
3500
+ this.vadPausedPlayer = false;
3501
+ this.pushToTalkEnabled = false;
3502
+ this.canInterrupt = false;
3503
+ // Bind event handlers
3504
+ this._handleWebSocketMessage = this._handleWebSocketMessage.bind(this);
3505
+ this._handleDataAvailable = this._handleDataAvailable.bind(this);
3506
+ }
3507
+ _initializeVAD() {
3508
+ console.log('initializing VAD', { pushToTalkEnabled: this.pushToTalkEnabled, canInterrupt: this.canInterrupt });
3509
+ if (!this.pushToTalkEnabled && this.canInterrupt) {
3496
3510
  dist.MicVAD.new({
3497
3511
  stream: this.wavRecorder.getStream() || undefined,
3498
3512
  model: 'v5',
@@ -3551,16 +3565,6 @@ class LayercodeClient {
3551
3565
  console.error('Error initializing VAD:', error);
3552
3566
  });
3553
3567
  }
3554
- this.ws = null;
3555
- this.status = 'disconnected';
3556
- this.userAudioAmplitude = 0;
3557
- this.agentAudioAmplitude = 0;
3558
- this.sessionId = options.sessionId || null;
3559
- this.pushToTalkActive = false;
3560
- this.vadPausedPlayer = false;
3561
- // Bind event handlers
3562
- this._handleWebSocketMessage = this._handleWebSocketMessage.bind(this);
3563
- this._handleDataAvailable = this._handleDataAvailable.bind(this);
3564
3568
  }
3565
3569
  /**
3566
3570
  * Updates the connection status and triggers the callback
@@ -3620,10 +3624,9 @@ class LayercodeClient {
3620
3624
  // Sent from the server to this client when a new user turn is detected
3621
3625
  console.log('received turn.start from server');
3622
3626
  console.log(message);
3623
- // if (message.role === 'user' && !this.pushToTalkActive) {
3624
- if (message.role === 'user') {
3627
+ if (message.role === 'user' && !this.pushToTalkEnabled && this.canInterrupt) {
3625
3628
  // 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)
3626
- console.log('interrupting assistant audio, as user turn has started and pushToTalkActive is false');
3629
+ console.log('interrupting assistant audio, as user turn has started and pushToTalkEnabled is false');
3627
3630
  await this._clientInterruptAssistantReplay();
3628
3631
  }
3629
3632
  // if (message.role === 'assistant') {
@@ -3737,6 +3740,19 @@ class LayercodeClient {
3737
3740
  this.ws = new WebSocket(`${this._websocketUrl}?${new URLSearchParams({
3738
3741
  client_session_key: authorizeSessionResponseBody.client_session_key,
3739
3742
  })}`);
3743
+ const config = authorizeSessionResponseBody.config;
3744
+ console.log('config', config);
3745
+ if (config.transcription.trigger === 'push_to_talk') {
3746
+ this.pushToTalkEnabled = true;
3747
+ }
3748
+ else if (config.transcription.trigger === 'automatic') {
3749
+ this.pushToTalkEnabled = false;
3750
+ this.canInterrupt = config.transcription.can_interrupt;
3751
+ }
3752
+ else {
3753
+ throw new Error(`Unknown trigger: ${config.transcription.trigger}`);
3754
+ }
3755
+ this._initializeVAD();
3740
3756
  // Bind the websocket message callbacks
3741
3757
  this.ws.onmessage = this._handleWebSocketMessage;
3742
3758
  this.ws.onopen = () => {