@anganyai/voice-sdk 0.0.7 → 0.0.8

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/index.cjs CHANGED
@@ -16429,7 +16429,10 @@ var SipManager = class extends EventEmitter {
16429
16429
  const statusCode = response.message.statusCode;
16430
16430
  const reasonPhrase = response.message.reasonPhrase;
16431
16431
  this.logger.warn("Call rejected", { statusCode, reasonPhrase });
16432
- rejectionReason = { code: statusCode, reason: this.mapSipErrorToMessage(statusCode, reasonPhrase) };
16432
+ rejectionReason = {
16433
+ code: statusCode,
16434
+ reason: this.mapSipErrorToMessage(statusCode, reasonPhrase)
16435
+ };
16433
16436
  }
16434
16437
  };
16435
16438
  inviter.stateChange.addListener((state) => {
@@ -16968,8 +16971,18 @@ var TranscriptionService = class extends EventEmitter {
16968
16971
  this.maxReconnectAttempts = 5;
16969
16972
  this.reconnectDelay = 1e3;
16970
16973
  this.isIntentionallyClosed = false;
16974
+ this.useEventSource = false;
16971
16975
  this.apiUrl = apiUrl;
16972
16976
  }
16977
+ /**
16978
+ * Set EventSource factory for React Native environments
16979
+ * Call this before start() to use EventSource instead of fetch streams
16980
+ */
16981
+ setEventSourceFactory(factory) {
16982
+ this.eventSourceFactory = factory;
16983
+ this.useEventSource = true;
16984
+ this.logger.debug("EventSource factory set - will use EventSource mode");
16985
+ }
16973
16986
  /**
16974
16987
  * Set token refresh callback for handling expired tokens
16975
16988
  */
@@ -16999,7 +17012,10 @@ var TranscriptionService = class extends EventEmitter {
16999
17012
  */
17000
17013
  async connectToSSE(accessToken, isRetry = false) {
17001
17014
  const sseUrl = `${this.apiUrl}/api/v1/events?event_types=transcription,call_event`;
17002
- this.logger.debug("Connecting to SSE endpoint", { url: sseUrl, isRetry });
17015
+ this.logger.debug("Connecting to SSE endpoint", { url: sseUrl, isRetry, useEventSource: this.useEventSource });
17016
+ if (this.useEventSource && this.eventSourceFactory) {
17017
+ return this.connectWithEventSource(sseUrl, accessToken);
17018
+ }
17003
17019
  const response = await fetch(sseUrl, {
17004
17020
  method: "GET",
17005
17021
  headers: {
@@ -17037,6 +17053,65 @@ var TranscriptionService = class extends EventEmitter {
17037
17053
  this.reader = response.body.getReader();
17038
17054
  this.processStream();
17039
17055
  }
17056
+ /**
17057
+ * Connect using EventSource (for React Native)
17058
+ */
17059
+ connectWithEventSource(sseUrl, accessToken) {
17060
+ if (!this.eventSourceFactory) {
17061
+ throw new Error("EventSource factory not set");
17062
+ }
17063
+ this.logger.debug("Connecting with EventSource", { url: sseUrl });
17064
+ if (this.eventSource) {
17065
+ this.eventSource.removeAllEventListeners();
17066
+ this.eventSource.close();
17067
+ }
17068
+ this.eventSource = this.eventSourceFactory(sseUrl, {
17069
+ headers: {
17070
+ Authorization: `Bearer ${accessToken}`,
17071
+ Accept: "text/event-stream",
17072
+ "Cache-Control": "no-cache"
17073
+ }
17074
+ });
17075
+ this.eventSource.addEventListener("open", () => {
17076
+ this.logger.info("EventSource SSE connection established");
17077
+ this.reconnectAttempts = 0;
17078
+ this.emit("connected");
17079
+ });
17080
+ this.eventSource.addEventListener("message", (event) => {
17081
+ this.logger.debug("EventSource message received", { data: event.data });
17082
+ try {
17083
+ const data = JSON.parse(event.data);
17084
+ this.handleMessage(data);
17085
+ } catch (error) {
17086
+ this.logger.error("Error parsing EventSource message", { error, data: event.data });
17087
+ }
17088
+ });
17089
+ this.eventSource.addEventListener("error", (event) => {
17090
+ this.logger.error("EventSource error", { event });
17091
+ if (!this.isIntentionallyClosed) {
17092
+ this.emit("disconnected");
17093
+ this.handleReconnection();
17094
+ }
17095
+ });
17096
+ this.eventSource.addEventListener("transcription", (event) => {
17097
+ this.logger.debug("EventSource transcription event", { data: event.data });
17098
+ try {
17099
+ const data = JSON.parse(event.data);
17100
+ this.handleMessage({ ...data, type: "transcription" });
17101
+ } catch (error) {
17102
+ this.logger.error("Error parsing transcription event", { error, data: event.data });
17103
+ }
17104
+ });
17105
+ this.eventSource.addEventListener("call_event", (event) => {
17106
+ this.logger.debug("EventSource call_event", { data: event.data });
17107
+ try {
17108
+ const data = JSON.parse(event.data);
17109
+ this.handleMessage(data);
17110
+ } catch (error) {
17111
+ this.logger.error("Error parsing call_event", { error, data: event.data });
17112
+ }
17113
+ });
17114
+ }
17040
17115
  /**
17041
17116
  * Stop the SSE connection
17042
17117
  */
@@ -17047,6 +17122,11 @@ var TranscriptionService = class extends EventEmitter {
17047
17122
  clearTimeout(this.reconnectTimeout);
17048
17123
  delete this.reconnectTimeout;
17049
17124
  }
17125
+ if (this.eventSource) {
17126
+ this.eventSource.removeAllEventListeners();
17127
+ this.eventSource.close();
17128
+ delete this.eventSource;
17129
+ }
17050
17130
  if (this.reader) {
17051
17131
  this.reader.cancel();
17052
17132
  delete this.reader;
@@ -17460,6 +17540,14 @@ var Conversation = class extends EventEmitter {
17460
17540
  sipUrl: urls.sipUrl || "will be derived"
17461
17541
  });
17462
17542
  }
17543
+ /**
17544
+ * Set EventSource factory for React Native environments
17545
+ * Call this before initialize() to use EventSource instead of fetch streams for SSE
17546
+ */
17547
+ setEventSourceFactory(factory) {
17548
+ this.transcriptionService.setEventSourceFactory(factory);
17549
+ this.logger.debug("EventSource factory configured for transcription service");
17550
+ }
17463
17551
  /**
17464
17552
  * Initialize and start the conversation
17465
17553
  */
@@ -18157,6 +18245,10 @@ var AnganyVoice = class extends EventEmitter {
18157
18245
  this.conversations.delete(conversationId);
18158
18246
  this.emit("conversationEnded", conversationId);
18159
18247
  });
18248
+ if (this.config.eventSourceFactory) {
18249
+ this.logger.debug("Setting EventSource factory for conversation");
18250
+ conversation.setEventSourceFactory(this.config.eventSourceFactory);
18251
+ }
18160
18252
  try {
18161
18253
  this.logger.debug("Initializing conversation...");
18162
18254
  await conversation.initialize();