assemblyai 4.4.1 → 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.
- package/CHANGELOG.md +6 -0
- package/dist/assemblyai.umd.js +14 -20
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +614 -0
- package/dist/bun.mjs +8 -6
- package/dist/deno.mjs +8 -6
- package/dist/index.cjs +9 -6
- package/dist/index.mjs +9 -6
- package/dist/node.cjs +8 -6
- package/dist/node.mjs +8 -6
- package/dist/polyfills/websocket/browser.d.ts +3 -0
- package/dist/polyfills/websocket/default.d.ts +3 -0
- package/dist/polyfills/websocket/index.d.ts +27 -0
- package/package.json +8 -12
- package/src/polyfills/websocket/browser.ts +18 -0
- package/src/polyfills/websocket/default.ts +8 -0
- package/src/polyfills/websocket/index.ts +41 -0
- package/src/services/realtime/service.ts +15 -12
- package/src/polyfills/ws/browser.mjs +0 -15
- package/src/polyfills/ws/index.cjs +0 -1
- package/src/polyfills/ws/index.d.ts +0 -2
- package/src/polyfills/ws/index.mjs +0 -2
package/dist/deno.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ws from 'ws';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Base class for services that communicate with the API.
|
|
@@ -88,6 +88,8 @@ const { WritableStream } = typeof window !== "undefined"
|
|
|
88
88
|
? global
|
|
89
89
|
: globalThis;
|
|
90
90
|
|
|
91
|
+
const factory = (url, params) => new ws(url, params);
|
|
92
|
+
|
|
91
93
|
var RealtimeErrorType;
|
|
92
94
|
(function (RealtimeErrorType) {
|
|
93
95
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -183,10 +185,10 @@ class RealtimeTranscriber {
|
|
|
183
185
|
}
|
|
184
186
|
const url = this.connectionUrl();
|
|
185
187
|
if (this.token) {
|
|
186
|
-
this.socket =
|
|
188
|
+
this.socket = factory(url.toString());
|
|
187
189
|
}
|
|
188
190
|
else {
|
|
189
|
-
this.socket =
|
|
191
|
+
this.socket = factory(url.toString(), {
|
|
190
192
|
headers: { Authorization: this.apiKey },
|
|
191
193
|
});
|
|
192
194
|
}
|
|
@@ -279,14 +281,14 @@ class RealtimeTranscriber {
|
|
|
279
281
|
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
280
282
|
}
|
|
281
283
|
send(data) {
|
|
282
|
-
if (!this.socket || this.socket.readyState !==
|
|
284
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
283
285
|
throw new Error("Socket is not open for communication");
|
|
284
286
|
}
|
|
285
287
|
this.socket.send(data);
|
|
286
288
|
}
|
|
287
289
|
async close(waitForSessionTermination = true) {
|
|
288
290
|
if (this.socket) {
|
|
289
|
-
if (this.socket.readyState ===
|
|
291
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
290
292
|
if (waitForSessionTermination) {
|
|
291
293
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
292
294
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -298,7 +300,7 @@ class RealtimeTranscriber {
|
|
|
298
300
|
this.socket.send(terminateSessionMessage);
|
|
299
301
|
}
|
|
300
302
|
}
|
|
301
|
-
if (
|
|
303
|
+
if (this.socket?.removeAllListeners)
|
|
302
304
|
this.socket.removeAllListeners();
|
|
303
305
|
this.socket.close();
|
|
304
306
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var ws = require('ws');
|
|
4
4
|
|
|
5
5
|
/******************************************************************************
|
|
6
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -135,6 +135,8 @@ const { WritableStream } = typeof window !== "undefined"
|
|
|
135
135
|
? global
|
|
136
136
|
: globalThis;
|
|
137
137
|
|
|
138
|
+
const factory = (url, params) => new ws(url, params);
|
|
139
|
+
|
|
138
140
|
var RealtimeErrorType;
|
|
139
141
|
(function (RealtimeErrorType) {
|
|
140
142
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -231,10 +233,10 @@ class RealtimeTranscriber {
|
|
|
231
233
|
}
|
|
232
234
|
const url = this.connectionUrl();
|
|
233
235
|
if (this.token) {
|
|
234
|
-
this.socket =
|
|
236
|
+
this.socket = factory(url.toString());
|
|
235
237
|
}
|
|
236
238
|
else {
|
|
237
|
-
this.socket =
|
|
239
|
+
this.socket = factory(url.toString(), {
|
|
238
240
|
headers: { Authorization: this.apiKey },
|
|
239
241
|
});
|
|
240
242
|
}
|
|
@@ -330,15 +332,16 @@ class RealtimeTranscriber {
|
|
|
330
332
|
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
331
333
|
}
|
|
332
334
|
send(data) {
|
|
333
|
-
if (!this.socket || this.socket.readyState !==
|
|
335
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
334
336
|
throw new Error("Socket is not open for communication");
|
|
335
337
|
}
|
|
336
338
|
this.socket.send(data);
|
|
337
339
|
}
|
|
338
340
|
close() {
|
|
339
341
|
return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
|
|
342
|
+
var _a;
|
|
340
343
|
if (this.socket) {
|
|
341
|
-
if (this.socket.readyState ===
|
|
344
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
342
345
|
if (waitForSessionTermination) {
|
|
343
346
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
344
347
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -350,7 +353,7 @@ class RealtimeTranscriber {
|
|
|
350
353
|
this.socket.send(terminateSessionMessage);
|
|
351
354
|
}
|
|
352
355
|
}
|
|
353
|
-
if (
|
|
356
|
+
if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners)
|
|
354
357
|
this.socket.removeAllListeners();
|
|
355
358
|
this.socket.close();
|
|
356
359
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ws from 'ws';
|
|
2
2
|
|
|
3
3
|
/******************************************************************************
|
|
4
4
|
Copyright (c) Microsoft Corporation.
|
|
@@ -133,6 +133,8 @@ const { WritableStream } = typeof window !== "undefined"
|
|
|
133
133
|
? global
|
|
134
134
|
: globalThis;
|
|
135
135
|
|
|
136
|
+
const factory = (url, params) => new ws(url, params);
|
|
137
|
+
|
|
136
138
|
var RealtimeErrorType;
|
|
137
139
|
(function (RealtimeErrorType) {
|
|
138
140
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -229,10 +231,10 @@ class RealtimeTranscriber {
|
|
|
229
231
|
}
|
|
230
232
|
const url = this.connectionUrl();
|
|
231
233
|
if (this.token) {
|
|
232
|
-
this.socket =
|
|
234
|
+
this.socket = factory(url.toString());
|
|
233
235
|
}
|
|
234
236
|
else {
|
|
235
|
-
this.socket =
|
|
237
|
+
this.socket = factory(url.toString(), {
|
|
236
238
|
headers: { Authorization: this.apiKey },
|
|
237
239
|
});
|
|
238
240
|
}
|
|
@@ -328,15 +330,16 @@ class RealtimeTranscriber {
|
|
|
328
330
|
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
329
331
|
}
|
|
330
332
|
send(data) {
|
|
331
|
-
if (!this.socket || this.socket.readyState !==
|
|
333
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
332
334
|
throw new Error("Socket is not open for communication");
|
|
333
335
|
}
|
|
334
336
|
this.socket.send(data);
|
|
335
337
|
}
|
|
336
338
|
close() {
|
|
337
339
|
return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
|
|
340
|
+
var _a;
|
|
338
341
|
if (this.socket) {
|
|
339
|
-
if (this.socket.readyState ===
|
|
342
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
340
343
|
if (waitForSessionTermination) {
|
|
341
344
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
342
345
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -348,7 +351,7 @@ class RealtimeTranscriber {
|
|
|
348
351
|
this.socket.send(terminateSessionMessage);
|
|
349
352
|
}
|
|
350
353
|
}
|
|
351
|
-
if (
|
|
354
|
+
if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners)
|
|
352
355
|
this.socket.removeAllListeners();
|
|
353
356
|
this.socket.close();
|
|
354
357
|
}
|
package/dist/node.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var web = require('stream/web');
|
|
4
|
-
var
|
|
4
|
+
var ws = require('ws');
|
|
5
5
|
var fs = require('fs');
|
|
6
6
|
var stream = require('stream');
|
|
7
7
|
|
|
@@ -87,6 +87,8 @@ class LemurService extends BaseService {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
const factory = (url, params) => new ws(url, params);
|
|
91
|
+
|
|
90
92
|
var RealtimeErrorType;
|
|
91
93
|
(function (RealtimeErrorType) {
|
|
92
94
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -182,10 +184,10 @@ class RealtimeTranscriber {
|
|
|
182
184
|
}
|
|
183
185
|
const url = this.connectionUrl();
|
|
184
186
|
if (this.token) {
|
|
185
|
-
this.socket =
|
|
187
|
+
this.socket = factory(url.toString());
|
|
186
188
|
}
|
|
187
189
|
else {
|
|
188
|
-
this.socket =
|
|
190
|
+
this.socket = factory(url.toString(), {
|
|
189
191
|
headers: { Authorization: this.apiKey },
|
|
190
192
|
});
|
|
191
193
|
}
|
|
@@ -278,14 +280,14 @@ class RealtimeTranscriber {
|
|
|
278
280
|
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
279
281
|
}
|
|
280
282
|
send(data) {
|
|
281
|
-
if (!this.socket || this.socket.readyState !==
|
|
283
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
282
284
|
throw new Error("Socket is not open for communication");
|
|
283
285
|
}
|
|
284
286
|
this.socket.send(data);
|
|
285
287
|
}
|
|
286
288
|
async close(waitForSessionTermination = true) {
|
|
287
289
|
if (this.socket) {
|
|
288
|
-
if (this.socket.readyState ===
|
|
290
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
289
291
|
if (waitForSessionTermination) {
|
|
290
292
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
291
293
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -297,7 +299,7 @@ class RealtimeTranscriber {
|
|
|
297
299
|
this.socket.send(terminateSessionMessage);
|
|
298
300
|
}
|
|
299
301
|
}
|
|
300
|
-
if (
|
|
302
|
+
if (this.socket?.removeAllListeners)
|
|
301
303
|
this.socket.removeAllListeners();
|
|
302
304
|
this.socket.close();
|
|
303
305
|
}
|
package/dist/node.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WritableStream } from 'stream/web';
|
|
2
|
-
import
|
|
2
|
+
import ws from 'ws';
|
|
3
3
|
import { createReadStream } from 'fs';
|
|
4
4
|
import { Readable } from 'stream';
|
|
5
5
|
|
|
@@ -85,6 +85,8 @@ class LemurService extends BaseService {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
const factory = (url, params) => new ws(url, params);
|
|
89
|
+
|
|
88
90
|
var RealtimeErrorType;
|
|
89
91
|
(function (RealtimeErrorType) {
|
|
90
92
|
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
@@ -180,10 +182,10 @@ class RealtimeTranscriber {
|
|
|
180
182
|
}
|
|
181
183
|
const url = this.connectionUrl();
|
|
182
184
|
if (this.token) {
|
|
183
|
-
this.socket =
|
|
185
|
+
this.socket = factory(url.toString());
|
|
184
186
|
}
|
|
185
187
|
else {
|
|
186
|
-
this.socket =
|
|
188
|
+
this.socket = factory(url.toString(), {
|
|
187
189
|
headers: { Authorization: this.apiKey },
|
|
188
190
|
});
|
|
189
191
|
}
|
|
@@ -276,14 +278,14 @@ class RealtimeTranscriber {
|
|
|
276
278
|
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
277
279
|
}
|
|
278
280
|
send(data) {
|
|
279
|
-
if (!this.socket || this.socket.readyState !==
|
|
281
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
280
282
|
throw new Error("Socket is not open for communication");
|
|
281
283
|
}
|
|
282
284
|
this.socket.send(data);
|
|
283
285
|
}
|
|
284
286
|
async close(waitForSessionTermination = true) {
|
|
285
287
|
if (this.socket) {
|
|
286
|
-
if (this.socket.readyState ===
|
|
288
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
287
289
|
if (waitForSessionTermination) {
|
|
288
290
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
289
291
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -295,7 +297,7 @@ class RealtimeTranscriber {
|
|
|
295
297
|
this.socket.send(terminateSessionMessage);
|
|
296
298
|
}
|
|
297
299
|
}
|
|
298
|
-
if (
|
|
300
|
+
if (this.socket?.removeAllListeners)
|
|
299
301
|
this.socket.removeAllListeners();
|
|
300
302
|
this.socket.close();
|
|
301
303
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import ws, { Event, ErrorEvent, CloseEvent, MessageEvent } from "ws";
|
|
3
|
+
export type PolyfillWebSocket = {
|
|
4
|
+
OPEN: typeof ws.OPEN;
|
|
5
|
+
binaryType: string;
|
|
6
|
+
onopen: ((event: Event) => void) | null;
|
|
7
|
+
onerror: ((event: ErrorEvent) => void) | null;
|
|
8
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
9
|
+
onmessage: ((event: MessageEvent) => void) | null;
|
|
10
|
+
readonly readyState: typeof ws.CONNECTING | typeof ws.OPEN | typeof ws.CLOSING | typeof ws.CLOSED;
|
|
11
|
+
removeAllListeners?: () => void;
|
|
12
|
+
send(data: string | number | Buffer | DataView | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly unknown[] | readonly number[] | {
|
|
13
|
+
valueOf(): ArrayBuffer;
|
|
14
|
+
} | {
|
|
15
|
+
valueOf(): SharedArrayBuffer;
|
|
16
|
+
} | {
|
|
17
|
+
valueOf(): Uint8Array;
|
|
18
|
+
} | {
|
|
19
|
+
valueOf(): readonly number[];
|
|
20
|
+
} | {
|
|
21
|
+
valueOf(): string;
|
|
22
|
+
} | {
|
|
23
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
24
|
+
}): unknown;
|
|
25
|
+
close(): unknown;
|
|
26
|
+
};
|
|
27
|
+
export type PolyfillWebSocketFactory = (url: string, params?: unknown) => PolyfillWebSocket;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblyai",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0-beta.0",
|
|
4
4
|
"description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18"
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"default": "./dist/deno.mjs"
|
|
18
18
|
},
|
|
19
19
|
"workerd": "./dist/index.mjs",
|
|
20
|
-
"browser": "./dist/
|
|
20
|
+
"browser": "./dist/browser.mjs",
|
|
21
21
|
"node": {
|
|
22
22
|
"types": "./dist/index.d.ts",
|
|
23
23
|
"import": "./dist/node.mjs",
|
|
@@ -40,14 +40,10 @@
|
|
|
40
40
|
"node": "./src/polyfills/streams/node.ts",
|
|
41
41
|
"default": "./src/polyfills/streams/index.ts"
|
|
42
42
|
},
|
|
43
|
-
"#
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"default":
|
|
47
|
-
"types": "./src/polyfills/ws/index.d.ts",
|
|
48
|
-
"import": "./src/polyfills/ws/index.mjs",
|
|
49
|
-
"require": "./src/polyfills/ws/index.cjs"
|
|
50
|
-
}
|
|
43
|
+
"#websocket": {
|
|
44
|
+
"browser": "./src/polyfills/websocket/browser.ts",
|
|
45
|
+
"node": "./src/polyfills/websocket/default.ts",
|
|
46
|
+
"default": "./src/polyfills/websocket/default.ts"
|
|
51
47
|
}
|
|
52
48
|
},
|
|
53
49
|
"type": "commonjs",
|
|
@@ -61,7 +57,7 @@
|
|
|
61
57
|
"url": "git+https://github.com/AssemblyAI/assemblyai-node-sdk.git"
|
|
62
58
|
},
|
|
63
59
|
"publishConfig": {
|
|
64
|
-
"tag": "
|
|
60
|
+
"tag": "beta",
|
|
65
61
|
"access": "public",
|
|
66
62
|
"registry": "https://registry.npmjs.org/"
|
|
67
63
|
},
|
|
@@ -70,7 +66,7 @@
|
|
|
70
66
|
"clean": "rimraf dist/* && rimraf temp/* && rimraf temp-docs/*",
|
|
71
67
|
"lint": "eslint -c .eslintrc.json '{src,tests}/**/*.{js,ts}' && publint && tsc --noEmit -p tsconfig.json",
|
|
72
68
|
"test": "pnpm run test:unit && pnpm run test:integration",
|
|
73
|
-
"test:unit": "jest --config jest.unit.config.js",
|
|
69
|
+
"test:unit": "jest --config jest.unit.config.js --testTimeout 1000",
|
|
74
70
|
"test:integration": "jest --config jest.integration.config.js --testTimeout 360000",
|
|
75
71
|
"format": "prettier '**/*' --write",
|
|
76
72
|
"generate:types": "tsx ./scripts/generate-types.ts && prettier 'src/types/*.generated.ts' --write",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PolyfillWebSocketFactory, PolyfillWebSocket } from ".";
|
|
2
|
+
export { PolyfillWebSocket } from ".";
|
|
3
|
+
|
|
4
|
+
const PolyfillWebSocket =
|
|
5
|
+
WebSocket ?? global?.WebSocket ?? window?.WebSocket ?? self?.WebSocket;
|
|
6
|
+
|
|
7
|
+
export const factory: PolyfillWebSocketFactory = (
|
|
8
|
+
url: string,
|
|
9
|
+
params?: unknown,
|
|
10
|
+
) => {
|
|
11
|
+
if (params) {
|
|
12
|
+
return new PolyfillWebSocket(
|
|
13
|
+
url,
|
|
14
|
+
params as string | string[],
|
|
15
|
+
) as unknown as PolyfillWebSocket;
|
|
16
|
+
}
|
|
17
|
+
return new PolyfillWebSocket(url) as unknown as PolyfillWebSocket;
|
|
18
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import ws from "ws";
|
|
2
|
+
import { PolyfillWebSocket, PolyfillWebSocketFactory } from ".";
|
|
3
|
+
export { PolyfillWebSocket } from ".";
|
|
4
|
+
|
|
5
|
+
export const factory: PolyfillWebSocketFactory = (
|
|
6
|
+
url: string,
|
|
7
|
+
params?: unknown,
|
|
8
|
+
) => new ws(url, params as ws.ClientOptions) as unknown as PolyfillWebSocket;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import ws, { Event, ErrorEvent, CloseEvent, MessageEvent } from "ws";
|
|
2
|
+
|
|
3
|
+
export type PolyfillWebSocket = {
|
|
4
|
+
OPEN: typeof ws.OPEN;
|
|
5
|
+
binaryType: string;
|
|
6
|
+
onopen: ((event: Event) => void) | null;
|
|
7
|
+
onerror: ((event: ErrorEvent) => void) | null;
|
|
8
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
9
|
+
onmessage: ((event: MessageEvent) => void) | null;
|
|
10
|
+
readonly readyState:
|
|
11
|
+
| typeof ws.CONNECTING
|
|
12
|
+
| typeof ws.OPEN
|
|
13
|
+
| typeof ws.CLOSING
|
|
14
|
+
| typeof ws.CLOSED;
|
|
15
|
+
removeAllListeners?: () => void;
|
|
16
|
+
send(
|
|
17
|
+
data:
|
|
18
|
+
| string
|
|
19
|
+
| number
|
|
20
|
+
| Buffer
|
|
21
|
+
| DataView
|
|
22
|
+
| ArrayBufferView
|
|
23
|
+
| Uint8Array
|
|
24
|
+
| ArrayBuffer
|
|
25
|
+
| SharedArrayBuffer
|
|
26
|
+
| readonly unknown[]
|
|
27
|
+
| readonly number[]
|
|
28
|
+
| { valueOf(): ArrayBuffer }
|
|
29
|
+
| { valueOf(): SharedArrayBuffer }
|
|
30
|
+
| { valueOf(): Uint8Array }
|
|
31
|
+
| { valueOf(): readonly number[] }
|
|
32
|
+
| { valueOf(): string }
|
|
33
|
+
| { [Symbol.toPrimitive](hint: string): string },
|
|
34
|
+
): unknown;
|
|
35
|
+
close(): unknown;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type PolyfillWebSocketFactory = (
|
|
39
|
+
url: string,
|
|
40
|
+
params?: unknown,
|
|
41
|
+
) => PolyfillWebSocket;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { WritableStream } from "#streams";
|
|
2
|
-
import
|
|
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,
|
|
@@ -52,7 +55,7 @@ export class RealtimeTranscriber {
|
|
|
52
55
|
private endUtteranceSilenceThreshold?: number;
|
|
53
56
|
private disablePartialTranscripts?: boolean;
|
|
54
57
|
|
|
55
|
-
private socket?:
|
|
58
|
+
private socket?: PolyfillWebSocket;
|
|
56
59
|
private listeners: RealtimeListeners = {};
|
|
57
60
|
private sessionTerminatedResolve?: () => void;
|
|
58
61
|
|
|
@@ -136,15 +139,15 @@ export class RealtimeTranscriber {
|
|
|
136
139
|
const url = this.connectionUrl();
|
|
137
140
|
|
|
138
141
|
if (this.token) {
|
|
139
|
-
this.socket =
|
|
142
|
+
this.socket = polyfillWebSocketFactory(url.toString());
|
|
140
143
|
} else {
|
|
141
|
-
this.socket =
|
|
144
|
+
this.socket = polyfillWebSocketFactory(url.toString(), {
|
|
142
145
|
headers: { Authorization: this.apiKey },
|
|
143
146
|
});
|
|
144
147
|
}
|
|
145
|
-
this.socket
|
|
148
|
+
this.socket!.binaryType = "arraybuffer";
|
|
146
149
|
|
|
147
|
-
this.socket
|
|
150
|
+
this.socket!.onopen = () => {
|
|
148
151
|
if (
|
|
149
152
|
this.endUtteranceSilenceThreshold === undefined ||
|
|
150
153
|
this.endUtteranceSilenceThreshold === null
|
|
@@ -156,7 +159,7 @@ export class RealtimeTranscriber {
|
|
|
156
159
|
);
|
|
157
160
|
};
|
|
158
161
|
|
|
159
|
-
this.socket
|
|
162
|
+
this.socket!.onclose = ({ code, reason }: CloseEvent) => {
|
|
160
163
|
if (!reason) {
|
|
161
164
|
if (code in RealtimeErrorType) {
|
|
162
165
|
reason = RealtimeErrorMessages[code as RealtimeErrorType];
|
|
@@ -165,12 +168,12 @@ export class RealtimeTranscriber {
|
|
|
165
168
|
this.listeners.close?.(code, reason);
|
|
166
169
|
};
|
|
167
170
|
|
|
168
|
-
this.socket
|
|
171
|
+
this.socket!.onerror = (event: ErrorEvent) => {
|
|
169
172
|
if (event.error) this.listeners.error?.(event.error as Error);
|
|
170
173
|
else this.listeners.error?.(new Error(event.message));
|
|
171
174
|
};
|
|
172
175
|
|
|
173
|
-
this.socket
|
|
176
|
+
this.socket!.onmessage = ({ data }: MessageEvent) => {
|
|
174
177
|
const message = JSON.parse(data.toString()) as RealtimeMessage;
|
|
175
178
|
if ("error" in message) {
|
|
176
179
|
this.listeners.error?.(new RealtimeError(message.error));
|
|
@@ -242,7 +245,7 @@ export class RealtimeTranscriber {
|
|
|
242
245
|
}
|
|
243
246
|
|
|
244
247
|
private send(data: BufferLike) {
|
|
245
|
-
if (!this.socket || this.socket.readyState !==
|
|
248
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
246
249
|
throw new Error("Socket is not open for communication");
|
|
247
250
|
}
|
|
248
251
|
this.socket.send(data);
|
|
@@ -250,7 +253,7 @@ export class RealtimeTranscriber {
|
|
|
250
253
|
|
|
251
254
|
async close(waitForSessionTermination = true) {
|
|
252
255
|
if (this.socket) {
|
|
253
|
-
if (this.socket.readyState ===
|
|
256
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
254
257
|
if (waitForSessionTermination) {
|
|
255
258
|
const sessionTerminatedPromise = new Promise<void>((resolve) => {
|
|
256
259
|
this.sessionTerminatedResolve = resolve;
|
|
@@ -261,7 +264,7 @@ export class RealtimeTranscriber {
|
|
|
261
264
|
this.socket.send(terminateSessionMessage);
|
|
262
265
|
}
|
|
263
266
|
}
|
|
264
|
-
if (
|
|
267
|
+
if (this.socket?.removeAllListeners) this.socket.removeAllListeners();
|
|
265
268
|
this.socket.close();
|
|
266
269
|
}
|
|
267
270
|
|
|
@@ -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");
|