@layercode/js-sdk 2.8.4 → 2.8.5

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.
@@ -6208,27 +6208,34 @@ registerProcessor('audio_processor', AudioProcessor);
6208
6208
  * @param {ArrayBuffer} data - The audio data buffer
6209
6209
  */
6210
6210
  _handleDataAvailable(data) {
6211
- var _a, _b, _c;
6211
+ var _a, _b, _c, _d;
6212
6212
  try {
6213
- const base64 = arrayBufferToBase64(data.mono);
6214
- // Don't send audio if muted
6213
+ // Don't send or buffer audio if muted. Also clear any stale buffer so we
6214
+ // don't accidentally flush old audio after unmute.
6215
6215
  if (this.isMuted) {
6216
+ this.audioBuffer = [];
6216
6217
  return;
6217
6218
  }
6218
6219
  // Determine if we should gate audio based on VAD configuration
6219
- const shouldGateAudio = ((_a = this.vadConfig) === null || _a === void 0 ? void 0 : _a.gate_audio) !== false; // Default to true if not specified
6220
- const bufferFrames = (_c = (_b = this.vadConfig) === null || _b === void 0 ? void 0 : _b.buffer_frames) !== null && _c !== void 0 ? _c : 10; // Default to 10 if not specified
6220
+ const shouldGateAudio = ((_a = this.vadConfig) === null || _a === void 0 ? void 0 : _a.gate_audio) !== false; // default true
6221
+ const bufferFrames = (_c = (_b = this.vadConfig) === null || _b === void 0 ? void 0 : _b.buffer_frames) !== null && _c !== void 0 ? _c : 10; // default 10
6222
+ // If VAD is disabled or failed to init, gating would deadlock (userIsSpeaking never flips true).
6223
+ // Only gate if we actually have a running VAD instance.
6224
+ const vadEnabledByConfig = ((_d = this.vadConfig) === null || _d === void 0 ? void 0 : _d.enabled) !== false; // default true
6225
+ const vadAvailable = vadEnabledByConfig && !!this.vad && !this.pushToTalkEnabled;
6221
6226
  let sendAudio;
6222
6227
  if (this.pushToTalkEnabled) {
6223
6228
  sendAudio = this.pushToTalkActive;
6224
6229
  }
6225
6230
  else if (shouldGateAudio) {
6226
- sendAudio = this.userIsSpeaking;
6231
+ // Key fix: if VAD isn't available, don't gate — send audio.
6232
+ sendAudio = vadAvailable ? this.userIsSpeaking : true;
6227
6233
  }
6228
6234
  else {
6229
6235
  // If gate_audio is false, always send audio
6230
6236
  sendAudio = true;
6231
6237
  }
6238
+ const base64 = arrayBufferToBase64(data.mono);
6232
6239
  if (sendAudio) {
6233
6240
  // If we have buffered audio and we're gating, send it first
6234
6241
  if (shouldGateAudio && this.audioBuffer.length > 0) {
@@ -6401,13 +6408,17 @@ registerProcessor('audio_processor', AudioProcessor);
6401
6408
  this.recorderStarted = true;
6402
6409
  this._sendReadyIfNeeded();
6403
6410
  }
6404
- const reportedDeviceId = (_a = this.activeDeviceId) !== null && _a !== void 0 ? _a : (this.useSystemDefaultDevice ? 'default' : ((_b = this.deviceId) !== null && _b !== void 0 ? _b : 'default'));
6411
+ const reportedDeviceId = (_a = this.activeDeviceId) !== null && _a !== void 0 ? _a : (this.useSystemDefaultDevice ? 'default' : (_b = this.deviceId) !== null && _b !== void 0 ? _b : 'default');
6405
6412
  if (reportedDeviceId !== this.lastReportedDeviceId) {
6406
6413
  this.lastReportedDeviceId = reportedDeviceId;
6407
6414
  if (this.options.onDeviceSwitched) {
6408
6415
  this.options.onDeviceSwitched(reportedDeviceId);
6409
6416
  }
6410
6417
  }
6418
+ // Ensure automatic mode has a VAD instance once the recorder stream is live
6419
+ if (!this.vad && !this.pushToTalkEnabled) {
6420
+ await this._initializeVAD();
6421
+ }
6411
6422
  console.debug('Recorder started successfully with device:', reportedDeviceId);
6412
6423
  }
6413
6424
  catch (error) {
@@ -6734,7 +6745,7 @@ registerProcessor('audio_processor', AudioProcessor);
6734
6745
  const newStream = this.wavRecorder.getStream();
6735
6746
  await this._reinitializeVAD(newStream);
6736
6747
  }
6737
- const reportedDeviceId = (_c = (_b = this.lastReportedDeviceId) !== null && _b !== void 0 ? _b : this.activeDeviceId) !== null && _c !== void 0 ? _c : (this.useSystemDefaultDevice ? 'default' : (normalizedDeviceId !== null && normalizedDeviceId !== void 0 ? normalizedDeviceId : 'default'));
6748
+ const reportedDeviceId = (_c = (_b = this.lastReportedDeviceId) !== null && _b !== void 0 ? _b : this.activeDeviceId) !== null && _c !== void 0 ? _c : (this.useSystemDefaultDevice ? 'default' : normalizedDeviceId !== null && normalizedDeviceId !== void 0 ? normalizedDeviceId : 'default');
6738
6749
  console.debug(`Successfully switched to input device: ${reportedDeviceId}`);
6739
6750
  }
6740
6751
  catch (error) {
@@ -6788,7 +6799,7 @@ registerProcessor('audio_processor', AudioProcessor);
6788
6799
  this.recorderStarted = true;
6789
6800
  this._sendReadyIfNeeded();
6790
6801
  }
6791
- const reportedDeviceId = (_a = this.activeDeviceId) !== null && _a !== void 0 ? _a : (this.useSystemDefaultDevice ? 'default' : ((_b = this.deviceId) !== null && _b !== void 0 ? _b : 'default'));
6802
+ const reportedDeviceId = (_a = this.activeDeviceId) !== null && _a !== void 0 ? _a : (this.useSystemDefaultDevice ? 'default' : (_b = this.deviceId) !== null && _b !== void 0 ? _b : 'default');
6792
6803
  if (reportedDeviceId !== previousReportedDeviceId) {
6793
6804
  this.lastReportedDeviceId = reportedDeviceId;
6794
6805
  if (this.options.onDeviceSwitched) {
@@ -7033,11 +7044,7 @@ registerProcessor('audio_processor', AudioProcessor);
7033
7044
  async _shouldWarnAudioDevicesRequireUserGesture(error) {
7034
7045
  const e = error;
7035
7046
  const name = typeof (e === null || e === void 0 ? void 0 : e.name) === 'string' ? e.name : '';
7036
- const msg = typeof (e === null || e === void 0 ? void 0 : e.message) === 'string'
7037
- ? e.message
7038
- : typeof error === 'string'
7039
- ? error
7040
- : '';
7047
+ const msg = typeof (e === null || e === void 0 ? void 0 : e.message) === 'string' ? e.message : typeof error === 'string' ? error : '';
7041
7048
  const isPermissionLike = name === 'NotAllowedError' || name === 'SecurityError' || name === 'PermissionDeniedError';
7042
7049
  if (!isPermissionLike)
7043
7050
  return false;