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