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