@unith-ai/core-client 2.0.5-beta.1 → 2.0.6

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.
package/dist/lib.web.js CHANGED
@@ -598,6 +598,17 @@ var Connection = class _Connection {
598
598
  });
599
599
  });
600
600
  this.socket.addEventListener("close", (event) => {
601
+ if (this.onMessageCallback) {
602
+ const parsedEvent = {
603
+ event: "timeout" /* TIME_OUT */,
604
+ user_id: "",
605
+ username: ""
606
+ };
607
+ if (isValidSocketEvent(parsedEvent)) {
608
+ this.onMessageCallback(parsedEvent);
609
+ return;
610
+ }
611
+ }
601
612
  this.disconnect({
602
613
  reason: "error",
603
614
  message: event.reason || "The connection was closed by the server.",
@@ -702,6 +713,10 @@ var Connection = class _Connection {
702
713
  this.socket.close();
703
714
  }
704
715
  sendMessage(message) {
716
+ if (this.socket.readyState !== WebSocket.OPEN) {
717
+ console.warn("Attempted to send a message on a closed socket.");
718
+ return;
719
+ }
705
720
  this.socket.send(JSON.stringify(message));
706
721
  }
707
722
  sendPingEvent(message) {
@@ -709,6 +724,7 @@ var Connection = class _Connection {
709
724
  console.warn("Ping event sent without a callback set.");
710
725
  return;
711
726
  }
727
+ if (this.socket.readyState !== WebSocket.OPEN) return;
712
728
  this.socket.send(JSON.stringify(message));
713
729
  }
714
730
  onPingPong(callback) {
@@ -755,11 +771,11 @@ var IdleVideo = class _IdleVideo {
755
771
  );
756
772
  }
757
773
  } else {
758
- const errorMessage = await response.text();
774
+ const errorMessage = await response.json();
759
775
  const error = new Error(
760
- `An error occurred retrieving idle video: ${response.status} ${response.statusText}. Response: ${errorMessage}`
776
+ errorMessage.detail || "An error occurred retrieving your digital human."
761
777
  );
762
- error.name = "IdleVideoError";
778
+ error.name = "headError";
763
779
  throw error;
764
780
  }
765
781
  }
@@ -776,11 +792,11 @@ var IdleVideo = class _IdleVideo {
776
792
  );
777
793
  }
778
794
  } else {
779
- const errorMessage = await response.text();
795
+ const errorMessage = await response.json();
780
796
  const error = new Error(
781
- `An error occurred retrieving avatar: ${response.status} ${response.statusText}. Response: ${errorMessage}`
797
+ errorMessage.detail || "An error occurred retrieving your digital human."
782
798
  );
783
- error.name = "AvatarError";
799
+ error.name = "headError";
784
800
  throw error;
785
801
  }
786
802
  }
@@ -797,11 +813,11 @@ var IdleVideo = class _IdleVideo {
797
813
  );
798
814
  }
799
815
  } else {
800
- const errorMessage = await response.text();
816
+ const errorMessage = await response.json();
801
817
  const error = new Error(
802
- `An error occurred retrieving idle video: ${response.status} ${response.statusText}. Response: ${errorMessage}`
818
+ errorMessage.detail || "An error occurred retrieving your digital human."
803
819
  );
804
- error.name = "IdleVideoError";
820
+ error.name = "headError";
805
821
  throw error;
806
822
  }
807
823
  }
@@ -25449,13 +25465,13 @@ function getLanguageCode(languageTag) {
25449
25465
 
25450
25466
  // src/modules/microphone.ts
25451
25467
  var Microphone = class _Microphone {
25452
- constructor(provider, options, elevenLabsOptions, user, headInfo, microphoneAccess, token, sendMessage) {
25468
+ constructor(provider, options, elevenLabsOptions, user, headInfo, microphoneAccess, token, voiceInterruptions) {
25453
25469
  this.provider = provider;
25454
25470
  this.options = options;
25455
25471
  this.elevenLabsOptions = elevenLabsOptions;
25456
25472
  this.user = user;
25457
25473
  this.headInfo = headInfo;
25458
- this.sendMessage = sendMessage;
25474
+ this.voiceInterruptions = voiceInterruptions;
25459
25475
  this.microphoneStatus = "OFF";
25460
25476
  this.microphoneAccess = false;
25461
25477
  this.recognizer = null;
@@ -25488,7 +25504,7 @@ var Microphone = class _Microphone {
25488
25504
  * @param sendMessage
25489
25505
  * @returns
25490
25506
  */
25491
- static async initializeMicrophone(options, provider, elevenLabsOptions, user, headInfo, microphoneAccess, sendMessage) {
25507
+ static async initializeMicrophone(options, provider, elevenLabsOptions, user, headInfo, microphoneAccess, voiceInterruptions) {
25492
25508
  let token = {
25493
25509
  token: "",
25494
25510
  generatedAt: null,
@@ -25510,7 +25526,7 @@ var Microphone = class _Microphone {
25510
25526
  headInfo,
25511
25527
  microphoneAccess,
25512
25528
  token,
25513
- sendMessage
25529
+ voiceInterruptions
25514
25530
  );
25515
25531
  }
25516
25532
  updateMicrophoneStatus(status) {
@@ -25590,7 +25606,7 @@ var Microphone = class _Microphone {
25590
25606
  }
25591
25607
  };
25592
25608
  this.recognizer.recognizing = (_3, event) => {
25593
- if (event.result.text.length < 2) return;
25609
+ if (!this.voiceInterruptions || event.result.text.length < 2) return;
25594
25610
  this.options.onMicrophonePartialSpeechRecognitionResult({
25595
25611
  transcript: event.result.text
25596
25612
  });
@@ -25661,7 +25677,8 @@ var Microphone = class _Microphone {
25661
25677
  this.handleRecognitionResult(data.text);
25662
25678
  });
25663
25679
  this.connection.on(P.PARTIAL_TRANSCRIPT, (data) => {
25664
- if (data.text.length < 2 || data.text[0] === "(") return;
25680
+ if (!this.voiceInterruptions || data.text.length < 2 || data.text[0] === "(")
25681
+ return;
25665
25682
  this.options.onMicrophonePartialSpeechRecognitionResult({
25666
25683
  transcript: data.text
25667
25684
  });
@@ -26774,20 +26791,8 @@ var Conversation = class _Conversation {
26774
26791
  return;
26775
26792
  }
26776
26793
  if (errorType === "resource_exhausted") {
26777
- if (this.avController.isPlaying) {
26778
- this.options.onError({
26779
- message: "We are experiencing heavy traffic and we can't deliver the response, try again",
26780
- endConversation: false,
26781
- type: "toast"
26782
- });
26783
- } else {
26784
- this.options.onError({
26785
- message: "The system is experiencing heavy traffic. Please try again later.",
26786
- endConversation: true,
26787
- type: "toast"
26788
- });
26789
- return;
26790
- }
26794
+ this.options.onHighDemand();
26795
+ return;
26791
26796
  } else if (errorType === "deadline_exceeded") {
26792
26797
  this.options.onError({
26793
26798
  message: "We are experiencing heavy traffic and we can't deliver the response, try again",
@@ -27044,6 +27049,8 @@ var Conversation = class _Conversation {
27044
27049
  },
27045
27050
  onError: () => {
27046
27051
  },
27052
+ onHighDemand: () => {
27053
+ },
27047
27054
  ...partialOptions
27048
27055
  };
27049
27056
  }
@@ -27256,7 +27263,7 @@ var Conversation = class _Conversation {
27256
27263
  return this.volume;
27257
27264
  }
27258
27265
  async startSession() {
27259
- var _a;
27266
+ var _a, _b;
27260
27267
  this.sessionStarted = true;
27261
27268
  if (this.audioOutput.context.state === "suspended") {
27262
27269
  await this.audioOutput.context.resume();
@@ -27276,13 +27283,13 @@ var Conversation = class _Conversation {
27276
27283
  this.user,
27277
27284
  this.headInfo,
27278
27285
  this.microphoneAccess,
27279
- this.sendMessage
27286
+ (_b = this.options.voiceInterruptions) != null ? _b : false
27280
27287
  );
27281
27288
  }
27282
27289
  return this.connection;
27283
27290
  }
27284
27291
  async toggleMicrophone() {
27285
- var _a;
27292
+ var _a, _b;
27286
27293
  if (this.options.microphoneProvider === "custom") {
27287
27294
  throw new Error("Cannot toggle microphone for custom provider.");
27288
27295
  }
@@ -27294,7 +27301,7 @@ var Conversation = class _Conversation {
27294
27301
  this.user,
27295
27302
  this.headInfo,
27296
27303
  this.microphoneAccess,
27297
- this.sendMessage
27304
+ (_b = this.options.voiceInterruptions) != null ? _b : false
27298
27305
  );
27299
27306
  }
27300
27307
  if (!this.microphoneAccess && this.microphone.status() === "OFF") {
@@ -27352,6 +27359,7 @@ export {
27352
27359
  EventType,
27353
27360
  StreamingEventType,
27354
27361
  VideoTransitionType,
27362
+ defaultElevenLabsOptions,
27355
27363
  isBinaryEvent,
27356
27364
  isConversationEndEvent,
27357
27365
  isJoinEvent,