assemblyai 4.5.0-beta.0 → 4.6.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +53 -9
  3. package/dist/assemblyai.streaming.umd.js +317 -0
  4. package/dist/assemblyai.streaming.umd.min.js +1 -0
  5. package/dist/assemblyai.umd.js +160 -12
  6. package/dist/assemblyai.umd.min.js +1 -1
  7. package/dist/browser.mjs +154 -11
  8. package/dist/bun.mjs +154 -11
  9. package/dist/deno.mjs +154 -11
  10. package/dist/exports/index.d.ts +2 -0
  11. package/dist/exports/streaming.d.ts +4 -0
  12. package/dist/index.cjs +160 -12
  13. package/dist/index.mjs +160 -12
  14. package/dist/node.cjs +154 -11
  15. package/dist/node.mjs +154 -11
  16. package/dist/polyfills/fetch/default.d.ts +1 -0
  17. package/dist/polyfills/fetch/workerd.d.ts +1 -0
  18. package/dist/services/base.d.ts +1 -0
  19. package/dist/services/lemur/index.d.ts +8 -1
  20. package/dist/services/realtime/service.d.ts +60 -0
  21. package/dist/services/transcripts/index.d.ts +16 -3
  22. package/dist/streaming.browser.mjs +268 -0
  23. package/dist/streaming.cjs +306 -0
  24. package/dist/streaming.mjs +303 -0
  25. package/dist/types/index.d.ts +7 -0
  26. package/dist/types/openapi.generated.d.ts +75 -29
  27. package/dist/types/services/index.d.ts +7 -0
  28. package/dist/types/transcripts/index.d.ts +9 -0
  29. package/dist/utils/userAgent.d.ts +2 -0
  30. package/dist/workerd.mjs +751 -0
  31. package/docs/reference-types-from-js.md +27 -0
  32. package/package.json +57 -25
  33. package/src/exports/index.ts +2 -0
  34. package/src/exports/streaming.ts +4 -0
  35. package/src/polyfills/fetch/default.ts +3 -0
  36. package/src/polyfills/fetch/workerd.ts +1 -0
  37. package/src/services/base.ts +27 -7
  38. package/src/services/files/index.ts +19 -2
  39. package/src/services/index.ts +2 -1
  40. package/src/services/lemur/index.ts +12 -0
  41. package/src/services/realtime/service.ts +65 -0
  42. package/src/services/transcripts/index.ts +41 -4
  43. package/src/types/index.ts +9 -0
  44. package/src/types/openapi.generated.ts +224 -48
  45. package/src/types/services/index.ts +8 -0
  46. package/src/types/transcripts/index.ts +10 -0
  47. package/src/utils/path.ts +1 -0
  48. package/src/utils/userAgent.ts +51 -0
@@ -1,5 +1,5 @@
1
1
  import { BaseService } from "../base";
2
- import { ParagraphsResponse, SentencesResponse, Transcript, TranscriptList, TranscriptParams, CreateTranscriptOptions, SubtitleFormat, RedactedAudioResponse, ListTranscriptParams, WordSearchResponse, BaseServiceParams, PollingOptions, TranscribeParams, TranscribeOptions, SubmitParams } from "../..";
2
+ import { ParagraphsResponse, SentencesResponse, Transcript, TranscriptList, TranscriptParams, CreateTranscriptOptions, SubtitleFormat, RedactedAudioResponse, ListTranscriptParams, WordSearchResponse, BaseServiceParams, PollingOptions, TranscribeParams, TranscribeOptions, SubmitParams, RedactedAudioFile } from "../..";
3
3
  import { FileService } from "../files";
4
4
  export declare class TranscriptService extends BaseService {
5
5
  private files;
@@ -78,9 +78,22 @@ export declare class TranscriptService extends BaseService {
78
78
  */
79
79
  subtitles(id: string, format?: SubtitleFormat, chars_per_caption?: number): Promise<string>;
80
80
  /**
81
- * Retrieve redactions of a transcript.
81
+ * Retrieve the redacted audio URL of a transcript.
82
82
  * @param id - The identifier of the transcript.
83
- * @returns A promise that resolves to the subtitles text.
83
+ * @returns A promise that resolves to the details of the redacted audio.
84
+ * @deprecated Use `redactedAudio` instead.
84
85
  */
85
86
  redactions(id: string): Promise<RedactedAudioResponse>;
87
+ /**
88
+ * Retrieve the redacted audio URL of a transcript.
89
+ * @param id - The identifier of the transcript.
90
+ * @returns A promise that resolves to the details of the redacted audio.
91
+ */
92
+ redactedAudio(id: string): Promise<RedactedAudioResponse>;
93
+ /**
94
+ * Retrieve the redacted audio file of a transcript.
95
+ * @param id - The identifier of the transcript.
96
+ * @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
97
+ */
98
+ redactedAudioFile(id: string): Promise<RedactedAudioFile>;
86
99
  }
@@ -0,0 +1,268 @@
1
+ const { WritableStream } = typeof window !== "undefined"
2
+ ? window
3
+ : typeof global !== "undefined"
4
+ ? global
5
+ : globalThis;
6
+
7
+ const PolyfillWebSocket = WebSocket ?? global?.WebSocket ?? window?.WebSocket ?? self?.WebSocket;
8
+ const factory = (url, params) => {
9
+ if (params) {
10
+ return new PolyfillWebSocket(url, params);
11
+ }
12
+ return new PolyfillWebSocket(url);
13
+ };
14
+
15
+ var RealtimeErrorType;
16
+ (function (RealtimeErrorType) {
17
+ RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
18
+ RealtimeErrorType[RealtimeErrorType["AuthFailed"] = 4001] = "AuthFailed";
19
+ // Both InsufficientFunds and FreeAccount error use 4002
20
+ RealtimeErrorType[RealtimeErrorType["InsufficientFundsOrFreeAccount"] = 4002] = "InsufficientFundsOrFreeAccount";
21
+ RealtimeErrorType[RealtimeErrorType["NonexistentSessionId"] = 4004] = "NonexistentSessionId";
22
+ RealtimeErrorType[RealtimeErrorType["SessionExpired"] = 4008] = "SessionExpired";
23
+ RealtimeErrorType[RealtimeErrorType["ClosedSession"] = 4010] = "ClosedSession";
24
+ RealtimeErrorType[RealtimeErrorType["RateLimited"] = 4029] = "RateLimited";
25
+ RealtimeErrorType[RealtimeErrorType["UniqueSessionViolation"] = 4030] = "UniqueSessionViolation";
26
+ RealtimeErrorType[RealtimeErrorType["SessionTimeout"] = 4031] = "SessionTimeout";
27
+ RealtimeErrorType[RealtimeErrorType["AudioTooShort"] = 4032] = "AudioTooShort";
28
+ RealtimeErrorType[RealtimeErrorType["AudioTooLong"] = 4033] = "AudioTooLong";
29
+ RealtimeErrorType[RealtimeErrorType["BadJson"] = 4100] = "BadJson";
30
+ RealtimeErrorType[RealtimeErrorType["BadSchema"] = 4101] = "BadSchema";
31
+ RealtimeErrorType[RealtimeErrorType["TooManyStreams"] = 4102] = "TooManyStreams";
32
+ RealtimeErrorType[RealtimeErrorType["Reconnected"] = 4103] = "Reconnected";
33
+ RealtimeErrorType[RealtimeErrorType["ReconnectAttemptsExhausted"] = 1013] = "ReconnectAttemptsExhausted";
34
+ })(RealtimeErrorType || (RealtimeErrorType = {}));
35
+ const RealtimeErrorMessages = {
36
+ [RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
37
+ [RealtimeErrorType.AuthFailed]: "Not Authorized",
38
+ [RealtimeErrorType.InsufficientFundsOrFreeAccount]: "Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",
39
+ [RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
40
+ [RealtimeErrorType.SessionExpired]: "Session has expired",
41
+ [RealtimeErrorType.ClosedSession]: "Session is closed",
42
+ [RealtimeErrorType.RateLimited]: "Rate limited",
43
+ [RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
44
+ [RealtimeErrorType.SessionTimeout]: "Session Timeout",
45
+ [RealtimeErrorType.AudioTooShort]: "Audio too short",
46
+ [RealtimeErrorType.AudioTooLong]: "Audio too long",
47
+ [RealtimeErrorType.BadJson]: "Bad JSON",
48
+ [RealtimeErrorType.BadSchema]: "Bad schema",
49
+ [RealtimeErrorType.TooManyStreams]: "Too many streams",
50
+ [RealtimeErrorType.Reconnected]: "Reconnected",
51
+ [RealtimeErrorType.ReconnectAttemptsExhausted]: "Reconnect attempts exhausted",
52
+ };
53
+ class RealtimeError extends Error {
54
+ }
55
+
56
+ const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
57
+ const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
58
+ const terminateSessionMessage = `{"terminate_session":true}`;
59
+ /**
60
+ * RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
61
+ */
62
+ class RealtimeTranscriber {
63
+ /**
64
+ * Create a new RealtimeTranscriber.
65
+ * @param params - Parameters to configure the RealtimeTranscriber
66
+ */
67
+ constructor(params) {
68
+ this.listeners = {};
69
+ this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
70
+ this.sampleRate = params.sampleRate ?? 16_000;
71
+ this.wordBoost = params.wordBoost;
72
+ this.encoding = params.encoding;
73
+ this.endUtteranceSilenceThreshold = params.endUtteranceSilenceThreshold;
74
+ this.disablePartialTranscripts = params.disablePartialTranscripts;
75
+ if ("token" in params && params.token)
76
+ this.token = params.token;
77
+ if ("apiKey" in params && params.apiKey)
78
+ this.apiKey = params.apiKey;
79
+ if (!(this.token || this.apiKey)) {
80
+ throw new Error("API key or temporary token is required.");
81
+ }
82
+ }
83
+ connectionUrl() {
84
+ const url = new URL(this.realtimeUrl);
85
+ if (url.protocol !== "wss:") {
86
+ throw new Error("Invalid protocol, must be wss");
87
+ }
88
+ const searchParams = new URLSearchParams();
89
+ if (this.token) {
90
+ searchParams.set("token", this.token);
91
+ }
92
+ searchParams.set("sample_rate", this.sampleRate.toString());
93
+ if (this.wordBoost && this.wordBoost.length > 0) {
94
+ searchParams.set("word_boost", JSON.stringify(this.wordBoost));
95
+ }
96
+ if (this.encoding) {
97
+ searchParams.set("encoding", this.encoding);
98
+ }
99
+ searchParams.set("enable_extra_session_information", "true");
100
+ if (this.disablePartialTranscripts) {
101
+ searchParams.set("disable_partial_transcripts", this.disablePartialTranscripts.toString());
102
+ }
103
+ url.search = searchParams.toString();
104
+ return url;
105
+ }
106
+ /**
107
+ * Add a listener for an event.
108
+ * @param event - The event to listen for.
109
+ * @param listener - The function to call when the event is emitted.
110
+ */
111
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
+ on(event, listener) {
113
+ this.listeners[event] = listener;
114
+ }
115
+ /**
116
+ * Connect to the server and begin a new session.
117
+ * @returns A promise that resolves when the connection is established and the session begins.
118
+ */
119
+ connect() {
120
+ return new Promise((resolve) => {
121
+ if (this.socket) {
122
+ throw new Error("Already connected");
123
+ }
124
+ const url = this.connectionUrl();
125
+ if (this.token) {
126
+ this.socket = factory(url.toString());
127
+ }
128
+ else {
129
+ this.socket = factory(url.toString(), {
130
+ headers: { Authorization: this.apiKey },
131
+ });
132
+ }
133
+ this.socket.binaryType = "arraybuffer";
134
+ this.socket.onopen = () => {
135
+ if (this.endUtteranceSilenceThreshold === undefined ||
136
+ this.endUtteranceSilenceThreshold === null) {
137
+ return;
138
+ }
139
+ this.configureEndUtteranceSilenceThreshold(this.endUtteranceSilenceThreshold);
140
+ };
141
+ this.socket.onclose = ({ code, reason }) => {
142
+ if (!reason) {
143
+ if (code in RealtimeErrorType) {
144
+ reason = RealtimeErrorMessages[code];
145
+ }
146
+ }
147
+ this.listeners.close?.(code, reason);
148
+ };
149
+ this.socket.onerror = (event) => {
150
+ if (event.error)
151
+ this.listeners.error?.(event.error);
152
+ else
153
+ this.listeners.error?.(new Error(event.message));
154
+ };
155
+ this.socket.onmessage = ({ data }) => {
156
+ const message = JSON.parse(data.toString());
157
+ if ("error" in message) {
158
+ this.listeners.error?.(new RealtimeError(message.error));
159
+ return;
160
+ }
161
+ switch (message.message_type) {
162
+ case "SessionBegins": {
163
+ const openObject = {
164
+ sessionId: message.session_id,
165
+ expiresAt: new Date(message.expires_at),
166
+ };
167
+ resolve(openObject);
168
+ this.listeners.open?.(openObject);
169
+ break;
170
+ }
171
+ case "PartialTranscript": {
172
+ // message.created is actually a string when coming from the socket
173
+ message.created = new Date(message.created);
174
+ this.listeners.transcript?.(message);
175
+ this.listeners["transcript.partial"]?.(message);
176
+ break;
177
+ }
178
+ case "FinalTranscript": {
179
+ // message.created is actually a string when coming from the socket
180
+ message.created = new Date(message.created);
181
+ this.listeners.transcript?.(message);
182
+ this.listeners["transcript.final"]?.(message);
183
+ break;
184
+ }
185
+ case "SessionInformation": {
186
+ this.listeners.session_information?.(message);
187
+ break;
188
+ }
189
+ case "SessionTerminated": {
190
+ this.sessionTerminatedResolve?.();
191
+ break;
192
+ }
193
+ }
194
+ };
195
+ });
196
+ }
197
+ /**
198
+ * Send audio data to the server.
199
+ * @param audio - The audio data to send to the server.
200
+ */
201
+ sendAudio(audio) {
202
+ this.send(audio);
203
+ }
204
+ /**
205
+ * Create a writable stream that can be used to send audio data to the server.
206
+ * @returns A writable stream that can be used to send audio data to the server.
207
+ */
208
+ stream() {
209
+ return new WritableStream({
210
+ write: (chunk) => {
211
+ this.sendAudio(chunk);
212
+ },
213
+ });
214
+ }
215
+ /**
216
+ * Manually end an utterance
217
+ */
218
+ forceEndUtterance() {
219
+ this.send(forceEndOfUtteranceMessage);
220
+ }
221
+ /**
222
+ * Configure the threshold for how long to wait before ending an utterance. Default is 700ms.
223
+ * @param threshold - The duration of the end utterance silence threshold in milliseconds.
224
+ * This value must be an integer between 0 and 20_000.
225
+ */
226
+ configureEndUtteranceSilenceThreshold(threshold) {
227
+ this.send(`{"end_utterance_silence_threshold":${threshold}}`);
228
+ }
229
+ send(data) {
230
+ if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
231
+ throw new Error("Socket is not open for communication");
232
+ }
233
+ this.socket.send(data);
234
+ }
235
+ /**
236
+ * Close the connection to the server.
237
+ * @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
238
+ * While waiting for the session to be terminated, you will receive the final transcript and session information.
239
+ */
240
+ async close(waitForSessionTermination = true) {
241
+ if (this.socket) {
242
+ if (this.socket.readyState === this.socket.OPEN) {
243
+ if (waitForSessionTermination) {
244
+ const sessionTerminatedPromise = new Promise((resolve) => {
245
+ this.sessionTerminatedResolve = resolve;
246
+ });
247
+ this.socket.send(terminateSessionMessage);
248
+ await sessionTerminatedPromise;
249
+ }
250
+ else {
251
+ this.socket.send(terminateSessionMessage);
252
+ }
253
+ }
254
+ if (this.socket?.removeAllListeners)
255
+ this.socket.removeAllListeners();
256
+ this.socket.close();
257
+ }
258
+ this.listeners = {};
259
+ this.socket = undefined;
260
+ }
261
+ }
262
+ /**
263
+ * @deprecated Use RealtimeTranscriber instead
264
+ */
265
+ class RealtimeService extends RealtimeTranscriber {
266
+ }
267
+
268
+ export { RealtimeService, RealtimeTranscriber };
@@ -0,0 +1,306 @@
1
+ 'use strict';
2
+
3
+ var ws = require('ws');
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol */
20
+
21
+
22
+ function __awaiter(thisArg, _arguments, P, generator) {
23
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
+ return new (P || (P = Promise))(function (resolve, reject) {
25
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
29
+ });
30
+ }
31
+
32
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
33
+ var e = new Error(message);
34
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
35
+ };
36
+
37
+ const { WritableStream } = typeof window !== "undefined"
38
+ ? window
39
+ : typeof global !== "undefined"
40
+ ? global
41
+ : globalThis;
42
+
43
+ const factory = (url, params) => new ws(url, params);
44
+
45
+ var RealtimeErrorType;
46
+ (function (RealtimeErrorType) {
47
+ RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
48
+ RealtimeErrorType[RealtimeErrorType["AuthFailed"] = 4001] = "AuthFailed";
49
+ // Both InsufficientFunds and FreeAccount error use 4002
50
+ RealtimeErrorType[RealtimeErrorType["InsufficientFundsOrFreeAccount"] = 4002] = "InsufficientFundsOrFreeAccount";
51
+ RealtimeErrorType[RealtimeErrorType["NonexistentSessionId"] = 4004] = "NonexistentSessionId";
52
+ RealtimeErrorType[RealtimeErrorType["SessionExpired"] = 4008] = "SessionExpired";
53
+ RealtimeErrorType[RealtimeErrorType["ClosedSession"] = 4010] = "ClosedSession";
54
+ RealtimeErrorType[RealtimeErrorType["RateLimited"] = 4029] = "RateLimited";
55
+ RealtimeErrorType[RealtimeErrorType["UniqueSessionViolation"] = 4030] = "UniqueSessionViolation";
56
+ RealtimeErrorType[RealtimeErrorType["SessionTimeout"] = 4031] = "SessionTimeout";
57
+ RealtimeErrorType[RealtimeErrorType["AudioTooShort"] = 4032] = "AudioTooShort";
58
+ RealtimeErrorType[RealtimeErrorType["AudioTooLong"] = 4033] = "AudioTooLong";
59
+ RealtimeErrorType[RealtimeErrorType["BadJson"] = 4100] = "BadJson";
60
+ RealtimeErrorType[RealtimeErrorType["BadSchema"] = 4101] = "BadSchema";
61
+ RealtimeErrorType[RealtimeErrorType["TooManyStreams"] = 4102] = "TooManyStreams";
62
+ RealtimeErrorType[RealtimeErrorType["Reconnected"] = 4103] = "Reconnected";
63
+ RealtimeErrorType[RealtimeErrorType["ReconnectAttemptsExhausted"] = 1013] = "ReconnectAttemptsExhausted";
64
+ })(RealtimeErrorType || (RealtimeErrorType = {}));
65
+ const RealtimeErrorMessages = {
66
+ [RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
67
+ [RealtimeErrorType.AuthFailed]: "Not Authorized",
68
+ [RealtimeErrorType.InsufficientFundsOrFreeAccount]: "Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",
69
+ [RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
70
+ [RealtimeErrorType.SessionExpired]: "Session has expired",
71
+ [RealtimeErrorType.ClosedSession]: "Session is closed",
72
+ [RealtimeErrorType.RateLimited]: "Rate limited",
73
+ [RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
74
+ [RealtimeErrorType.SessionTimeout]: "Session Timeout",
75
+ [RealtimeErrorType.AudioTooShort]: "Audio too short",
76
+ [RealtimeErrorType.AudioTooLong]: "Audio too long",
77
+ [RealtimeErrorType.BadJson]: "Bad JSON",
78
+ [RealtimeErrorType.BadSchema]: "Bad schema",
79
+ [RealtimeErrorType.TooManyStreams]: "Too many streams",
80
+ [RealtimeErrorType.Reconnected]: "Reconnected",
81
+ [RealtimeErrorType.ReconnectAttemptsExhausted]: "Reconnect attempts exhausted",
82
+ };
83
+ class RealtimeError extends Error {
84
+ }
85
+
86
+ const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
87
+ const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
88
+ const terminateSessionMessage = `{"terminate_session":true}`;
89
+ /**
90
+ * RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
91
+ */
92
+ class RealtimeTranscriber {
93
+ /**
94
+ * Create a new RealtimeTranscriber.
95
+ * @param params - Parameters to configure the RealtimeTranscriber
96
+ */
97
+ constructor(params) {
98
+ var _a, _b;
99
+ this.listeners = {};
100
+ this.realtimeUrl = (_a = params.realtimeUrl) !== null && _a !== void 0 ? _a : defaultRealtimeUrl;
101
+ this.sampleRate = (_b = params.sampleRate) !== null && _b !== void 0 ? _b : 16000;
102
+ this.wordBoost = params.wordBoost;
103
+ this.encoding = params.encoding;
104
+ this.endUtteranceSilenceThreshold = params.endUtteranceSilenceThreshold;
105
+ this.disablePartialTranscripts = params.disablePartialTranscripts;
106
+ if ("token" in params && params.token)
107
+ this.token = params.token;
108
+ if ("apiKey" in params && params.apiKey)
109
+ this.apiKey = params.apiKey;
110
+ if (!(this.token || this.apiKey)) {
111
+ throw new Error("API key or temporary token is required.");
112
+ }
113
+ }
114
+ connectionUrl() {
115
+ const url = new URL(this.realtimeUrl);
116
+ if (url.protocol !== "wss:") {
117
+ throw new Error("Invalid protocol, must be wss");
118
+ }
119
+ const searchParams = new URLSearchParams();
120
+ if (this.token) {
121
+ searchParams.set("token", this.token);
122
+ }
123
+ searchParams.set("sample_rate", this.sampleRate.toString());
124
+ if (this.wordBoost && this.wordBoost.length > 0) {
125
+ searchParams.set("word_boost", JSON.stringify(this.wordBoost));
126
+ }
127
+ if (this.encoding) {
128
+ searchParams.set("encoding", this.encoding);
129
+ }
130
+ searchParams.set("enable_extra_session_information", "true");
131
+ if (this.disablePartialTranscripts) {
132
+ searchParams.set("disable_partial_transcripts", this.disablePartialTranscripts.toString());
133
+ }
134
+ url.search = searchParams.toString();
135
+ return url;
136
+ }
137
+ /**
138
+ * Add a listener for an event.
139
+ * @param event - The event to listen for.
140
+ * @param listener - The function to call when the event is emitted.
141
+ */
142
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
+ on(event, listener) {
144
+ this.listeners[event] = listener;
145
+ }
146
+ /**
147
+ * Connect to the server and begin a new session.
148
+ * @returns A promise that resolves when the connection is established and the session begins.
149
+ */
150
+ connect() {
151
+ return new Promise((resolve) => {
152
+ if (this.socket) {
153
+ throw new Error("Already connected");
154
+ }
155
+ const url = this.connectionUrl();
156
+ if (this.token) {
157
+ this.socket = factory(url.toString());
158
+ }
159
+ else {
160
+ this.socket = factory(url.toString(), {
161
+ headers: { Authorization: this.apiKey },
162
+ });
163
+ }
164
+ this.socket.binaryType = "arraybuffer";
165
+ this.socket.onopen = () => {
166
+ if (this.endUtteranceSilenceThreshold === undefined ||
167
+ this.endUtteranceSilenceThreshold === null) {
168
+ return;
169
+ }
170
+ this.configureEndUtteranceSilenceThreshold(this.endUtteranceSilenceThreshold);
171
+ };
172
+ this.socket.onclose = ({ code, reason }) => {
173
+ var _a, _b;
174
+ if (!reason) {
175
+ if (code in RealtimeErrorType) {
176
+ reason = RealtimeErrorMessages[code];
177
+ }
178
+ }
179
+ (_b = (_a = this.listeners).close) === null || _b === void 0 ? void 0 : _b.call(_a, code, reason);
180
+ };
181
+ this.socket.onerror = (event) => {
182
+ var _a, _b, _c, _d;
183
+ if (event.error)
184
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, event.error);
185
+ else
186
+ (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(event.message));
187
+ };
188
+ this.socket.onmessage = ({ data }) => {
189
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
190
+ const message = JSON.parse(data.toString());
191
+ if ("error" in message) {
192
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, new RealtimeError(message.error));
193
+ return;
194
+ }
195
+ switch (message.message_type) {
196
+ case "SessionBegins": {
197
+ const openObject = {
198
+ sessionId: message.session_id,
199
+ expiresAt: new Date(message.expires_at),
200
+ };
201
+ resolve(openObject);
202
+ (_d = (_c = this.listeners).open) === null || _d === void 0 ? void 0 : _d.call(_c, openObject);
203
+ break;
204
+ }
205
+ case "PartialTranscript": {
206
+ // message.created is actually a string when coming from the socket
207
+ message.created = new Date(message.created);
208
+ (_f = (_e = this.listeners).transcript) === null || _f === void 0 ? void 0 : _f.call(_e, message);
209
+ (_h = (_g = this.listeners)["transcript.partial"]) === null || _h === void 0 ? void 0 : _h.call(_g, message);
210
+ break;
211
+ }
212
+ case "FinalTranscript": {
213
+ // message.created is actually a string when coming from the socket
214
+ message.created = new Date(message.created);
215
+ (_k = (_j = this.listeners).transcript) === null || _k === void 0 ? void 0 : _k.call(_j, message);
216
+ (_m = (_l = this.listeners)["transcript.final"]) === null || _m === void 0 ? void 0 : _m.call(_l, message);
217
+ break;
218
+ }
219
+ case "SessionInformation": {
220
+ (_p = (_o = this.listeners).session_information) === null || _p === void 0 ? void 0 : _p.call(_o, message);
221
+ break;
222
+ }
223
+ case "SessionTerminated": {
224
+ (_q = this.sessionTerminatedResolve) === null || _q === void 0 ? void 0 : _q.call(this);
225
+ break;
226
+ }
227
+ }
228
+ };
229
+ });
230
+ }
231
+ /**
232
+ * Send audio data to the server.
233
+ * @param audio - The audio data to send to the server.
234
+ */
235
+ sendAudio(audio) {
236
+ this.send(audio);
237
+ }
238
+ /**
239
+ * Create a writable stream that can be used to send audio data to the server.
240
+ * @returns A writable stream that can be used to send audio data to the server.
241
+ */
242
+ stream() {
243
+ return new WritableStream({
244
+ write: (chunk) => {
245
+ this.sendAudio(chunk);
246
+ },
247
+ });
248
+ }
249
+ /**
250
+ * Manually end an utterance
251
+ */
252
+ forceEndUtterance() {
253
+ this.send(forceEndOfUtteranceMessage);
254
+ }
255
+ /**
256
+ * Configure the threshold for how long to wait before ending an utterance. Default is 700ms.
257
+ * @param threshold - The duration of the end utterance silence threshold in milliseconds.
258
+ * This value must be an integer between 0 and 20_000.
259
+ */
260
+ configureEndUtteranceSilenceThreshold(threshold) {
261
+ this.send(`{"end_utterance_silence_threshold":${threshold}}`);
262
+ }
263
+ send(data) {
264
+ if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
265
+ throw new Error("Socket is not open for communication");
266
+ }
267
+ this.socket.send(data);
268
+ }
269
+ /**
270
+ * Close the connection to the server.
271
+ * @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
272
+ * While waiting for the session to be terminated, you will receive the final transcript and session information.
273
+ */
274
+ close() {
275
+ return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
276
+ var _a;
277
+ if (this.socket) {
278
+ if (this.socket.readyState === this.socket.OPEN) {
279
+ if (waitForSessionTermination) {
280
+ const sessionTerminatedPromise = new Promise((resolve) => {
281
+ this.sessionTerminatedResolve = resolve;
282
+ });
283
+ this.socket.send(terminateSessionMessage);
284
+ yield sessionTerminatedPromise;
285
+ }
286
+ else {
287
+ this.socket.send(terminateSessionMessage);
288
+ }
289
+ }
290
+ if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners)
291
+ this.socket.removeAllListeners();
292
+ this.socket.close();
293
+ }
294
+ this.listeners = {};
295
+ this.socket = undefined;
296
+ });
297
+ }
298
+ }
299
+ /**
300
+ * @deprecated Use RealtimeTranscriber instead
301
+ */
302
+ class RealtimeService extends RealtimeTranscriber {
303
+ }
304
+
305
+ exports.RealtimeService = RealtimeService;
306
+ exports.RealtimeTranscriber = RealtimeTranscriber;