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