@anganyai/voice-sdk 0.0.7 → 0.0.9

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,14 @@ 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", {
17016
+ url: sseUrl,
17017
+ isRetry,
17018
+ useEventSource: this.useEventSource
17019
+ });
17020
+ if (this.useEventSource && this.eventSourceFactory) {
17021
+ return this.connectWithEventSource(sseUrl, accessToken);
17022
+ }
17003
17023
  const response = await fetch(sseUrl, {
17004
17024
  method: "GET",
17005
17025
  headers: {
@@ -17037,6 +17057,62 @@ var TranscriptionService = class extends EventEmitter {
17037
17057
  this.reader = response.body.getReader();
17038
17058
  this.processStream();
17039
17059
  }
17060
+ /**
17061
+ * Connect using EventSource (for React Native)
17062
+ */
17063
+ connectWithEventSource(sseUrl, accessToken) {
17064
+ if (!this.eventSourceFactory) {
17065
+ throw new Error("EventSource factory not set");
17066
+ }
17067
+ this.logger.debug("Connecting with EventSource", { url: sseUrl });
17068
+ if (this.eventSource) {
17069
+ this.eventSource.removeAllEventListeners();
17070
+ this.eventSource.close();
17071
+ }
17072
+ this.eventSource = this.eventSourceFactory(sseUrl, {
17073
+ headers: {
17074
+ Authorization: `Bearer ${accessToken}`,
17075
+ Accept: "text/event-stream",
17076
+ "Cache-Control": "no-cache"
17077
+ }
17078
+ });
17079
+ const handleSSEEvent = (eventType, event) => {
17080
+ this.logger.debug(`EventSource [${eventType}] received`, { data: event.data, type: event.type });
17081
+ if (!event.data) {
17082
+ this.logger.debug(`EventSource [${eventType}] has no data, skipping`);
17083
+ return;
17084
+ }
17085
+ try {
17086
+ const data = JSON.parse(event.data);
17087
+ if (!data.type && eventType !== "message") {
17088
+ data.type = eventType;
17089
+ }
17090
+ this.handleMessage(data);
17091
+ } catch (error) {
17092
+ this.logger.error(`Error parsing EventSource [${eventType}]`, { error, data: event.data });
17093
+ }
17094
+ };
17095
+ this.eventSource.addEventListener("open", () => {
17096
+ this.logger.info("EventSource SSE connection established");
17097
+ this.reconnectAttempts = 0;
17098
+ this.emit("connected");
17099
+ });
17100
+ this.eventSource.addEventListener("error", (event) => {
17101
+ this.logger.error("EventSource error", { event });
17102
+ if (!this.isIntentionallyClosed) {
17103
+ this.emit("disconnected");
17104
+ this.handleReconnection();
17105
+ }
17106
+ });
17107
+ this.eventSource.addEventListener("message", (event) => handleSSEEvent("message", event));
17108
+ this.eventSource.addEventListener("transcription", (event) => handleSSEEvent("transcription", event));
17109
+ this.eventSource.addEventListener("call_event", (event) => handleSSEEvent("call_event", event));
17110
+ this.eventSource.addEventListener("call_started", (event) => handleSSEEvent("call_started", event));
17111
+ this.eventSource.addEventListener("call_ended", (event) => handleSSEEvent("call_ended", event));
17112
+ this.eventSource.addEventListener("connected", (event) => handleSSEEvent("connected", event));
17113
+ this.eventSource.addEventListener("welcome", (event) => handleSSEEvent("welcome", event));
17114
+ this.eventSource.addEventListener("heartbeat", (event) => handleSSEEvent("heartbeat", event));
17115
+ }
17040
17116
  /**
17041
17117
  * Stop the SSE connection
17042
17118
  */
@@ -17047,6 +17123,11 @@ var TranscriptionService = class extends EventEmitter {
17047
17123
  clearTimeout(this.reconnectTimeout);
17048
17124
  delete this.reconnectTimeout;
17049
17125
  }
17126
+ if (this.eventSource) {
17127
+ this.eventSource.removeAllEventListeners();
17128
+ this.eventSource.close();
17129
+ delete this.eventSource;
17130
+ }
17050
17131
  if (this.reader) {
17051
17132
  this.reader.cancel();
17052
17133
  delete this.reader;
@@ -17460,6 +17541,14 @@ var Conversation = class extends EventEmitter {
17460
17541
  sipUrl: urls.sipUrl || "will be derived"
17461
17542
  });
17462
17543
  }
17544
+ /**
17545
+ * Set EventSource factory for React Native environments
17546
+ * Call this before initialize() to use EventSource instead of fetch streams for SSE
17547
+ */
17548
+ setEventSourceFactory(factory) {
17549
+ this.transcriptionService.setEventSourceFactory(factory);
17550
+ this.logger.debug("EventSource factory configured for transcription service");
17551
+ }
17463
17552
  /**
17464
17553
  * Initialize and start the conversation
17465
17554
  */
@@ -18157,6 +18246,10 @@ var AnganyVoice = class extends EventEmitter {
18157
18246
  this.conversations.delete(conversationId);
18158
18247
  this.emit("conversationEnded", conversationId);
18159
18248
  });
18249
+ if (this.config.eventSourceFactory) {
18250
+ this.logger.debug("Setting EventSource factory for conversation");
18251
+ conversation.setEventSourceFactory(this.config.eventSourceFactory);
18252
+ }
18160
18253
  try {
18161
18254
  this.logger.debug("Initializing conversation...");
18162
18255
  await conversation.initialize();