assemblyai 4.4.0 → 4.5.0-beta.0

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.
@@ -1,5 +1,8 @@
1
1
  import { WritableStream } from "#streams";
2
- import WebSocket from "#ws";
2
+ import {
3
+ PolyfillWebSocket,
4
+ factory as polyfillWebSocketFactory,
5
+ } from "#websocket";
3
6
  import { ErrorEvent, MessageEvent, CloseEvent } from "ws";
4
7
  import {
5
8
  RealtimeEvents,
@@ -50,10 +53,9 @@ export class RealtimeTranscriber {
50
53
  private apiKey?: string;
51
54
  private token?: string;
52
55
  private endUtteranceSilenceThreshold?: number;
53
- private enableExtraSessionInformation?: boolean;
54
56
  private disablePartialTranscripts?: boolean;
55
57
 
56
- private socket?: WebSocket;
58
+ private socket?: PolyfillWebSocket;
57
59
  private listeners: RealtimeListeners = {};
58
60
  private sessionTerminatedResolve?: () => void;
59
61
 
@@ -63,7 +65,6 @@ export class RealtimeTranscriber {
63
65
  this.wordBoost = params.wordBoost;
64
66
  this.encoding = params.encoding;
65
67
  this.endUtteranceSilenceThreshold = params.endUtteranceSilenceThreshold;
66
- this.enableExtraSessionInformation = params.enableExtraSessionInformation;
67
68
  this.disablePartialTranscripts = params.disablePartialTranscripts;
68
69
  if ("token" in params && params.token) this.token = params.token;
69
70
  if ("apiKey" in params && params.apiKey) this.apiKey = params.apiKey;
@@ -91,12 +92,9 @@ export class RealtimeTranscriber {
91
92
  if (this.encoding) {
92
93
  searchParams.set("encoding", this.encoding);
93
94
  }
94
- if (this.enableExtraSessionInformation) {
95
- searchParams.set(
96
- "enable_extra_session_information",
97
- this.enableExtraSessionInformation.toString(),
98
- );
99
- }
95
+
96
+ searchParams.set("enable_extra_session_information", "true");
97
+
100
98
  if (this.disablePartialTranscripts) {
101
99
  searchParams.set(
102
100
  "disable_partial_transcripts",
@@ -141,15 +139,15 @@ export class RealtimeTranscriber {
141
139
  const url = this.connectionUrl();
142
140
 
143
141
  if (this.token) {
144
- this.socket = new WebSocket(url.toString());
142
+ this.socket = polyfillWebSocketFactory(url.toString());
145
143
  } else {
146
- this.socket = new WebSocket(url.toString(), {
144
+ this.socket = polyfillWebSocketFactory(url.toString(), {
147
145
  headers: { Authorization: this.apiKey },
148
146
  });
149
147
  }
150
- this.socket.binaryType = "arraybuffer";
148
+ this.socket!.binaryType = "arraybuffer";
151
149
 
152
- this.socket.onopen = () => {
150
+ this.socket!.onopen = () => {
153
151
  if (
154
152
  this.endUtteranceSilenceThreshold === undefined ||
155
153
  this.endUtteranceSilenceThreshold === null
@@ -161,7 +159,7 @@ export class RealtimeTranscriber {
161
159
  );
162
160
  };
163
161
 
164
- this.socket.onclose = ({ code, reason }: CloseEvent) => {
162
+ this.socket!.onclose = ({ code, reason }: CloseEvent) => {
165
163
  if (!reason) {
166
164
  if (code in RealtimeErrorType) {
167
165
  reason = RealtimeErrorMessages[code as RealtimeErrorType];
@@ -170,12 +168,12 @@ export class RealtimeTranscriber {
170
168
  this.listeners.close?.(code, reason);
171
169
  };
172
170
 
173
- this.socket.onerror = (event: ErrorEvent) => {
171
+ this.socket!.onerror = (event: ErrorEvent) => {
174
172
  if (event.error) this.listeners.error?.(event.error as Error);
175
173
  else this.listeners.error?.(new Error(event.message));
176
174
  };
177
175
 
178
- this.socket.onmessage = ({ data }: MessageEvent) => {
176
+ this.socket!.onmessage = ({ data }: MessageEvent) => {
179
177
  const message = JSON.parse(data.toString()) as RealtimeMessage;
180
178
  if ("error" in message) {
181
179
  this.listeners.error?.(new RealtimeError(message.error));
@@ -247,7 +245,7 @@ export class RealtimeTranscriber {
247
245
  }
248
246
 
249
247
  private send(data: BufferLike) {
250
- if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
248
+ if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
251
249
  throw new Error("Socket is not open for communication");
252
250
  }
253
251
  this.socket.send(data);
@@ -255,7 +253,7 @@ export class RealtimeTranscriber {
255
253
 
256
254
  async close(waitForSessionTermination = true) {
257
255
  if (this.socket) {
258
- if (this.socket.readyState === WebSocket.OPEN) {
256
+ if (this.socket.readyState === this.socket.OPEN) {
259
257
  if (waitForSessionTermination) {
260
258
  const sessionTerminatedPromise = new Promise<void>((resolve) => {
261
259
  this.sessionTerminatedResolve = resolve;
@@ -266,7 +264,7 @@ export class RealtimeTranscriber {
266
264
  this.socket.send(terminateSessionMessage);
267
265
  }
268
266
  }
269
- if ("removeAllListeners" in this.socket) this.socket.removeAllListeners();
267
+ if (this.socket?.removeAllListeners) this.socket.removeAllListeners();
270
268
  this.socket.close();
271
269
  }
272
270
 
@@ -37,7 +37,8 @@ type CreateRealtimeTranscriberParams = {
37
37
  /**
38
38
  * Enable extra session information.
39
39
  * Set to `true` to receive the `session_information` message before the session ends. Defaults to `false`.
40
- * @defaultValue false
40
+ * @defaultValue true
41
+ * @deprecated This parameter is now ignored and will be removed. Session information will always be sent.
41
42
  */
42
43
  enableExtraSessionInformation?: boolean;
43
44
  } & (
@@ -91,7 +92,8 @@ type RealtimeTranscriberParams = {
91
92
  /**
92
93
  * Enable extra session information.
93
94
  * Set to `true` to receive the `session_information` message before the session ends. Defaults to `false`.
94
- * @defaultValue false
95
+ * @defaultValue true
96
+ * @deprecated This parameter is now ignored and will be removed. Session information will always be sent.
95
97
  */
96
98
  enableExtraSessionInformation?: boolean;
97
99
  } & (
@@ -1,15 +0,0 @@
1
- var ws = null;
2
-
3
- if (typeof WebSocket !== "undefined") {
4
- ws = WebSocket;
5
- } else if (typeof MozWebSocket !== "undefined") {
6
- ws = MozWebSocket;
7
- } else if (typeof global !== "undefined") {
8
- ws = global.WebSocket || global.MozWebSocket;
9
- } else if (typeof window !== "undefined") {
10
- ws = window.WebSocket || window.MozWebSocket;
11
- } else if (typeof self !== "undefined") {
12
- ws = self.WebSocket || self.MozWebSocket;
13
- }
14
-
15
- export default ws;
@@ -1 +0,0 @@
1
- module.exports = require("ws");
@@ -1,2 +0,0 @@
1
- import ws from "ws";
2
- export default ws;
@@ -1,2 +0,0 @@
1
- import ws from "ws";
2
- export default ws;