assemblyai 4.5.0 → 4.6.1
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 +12 -0
- package/dist/assemblyai.streaming.umd.js +29 -0
- package/dist/assemblyai.umd.js +30 -11
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +30 -11
- package/dist/bun.mjs +30 -11
- package/dist/deno.mjs +30 -11
- package/dist/index.cjs +30 -11
- package/dist/index.mjs +30 -11
- package/dist/node.cjs +30 -11
- package/dist/node.mjs +30 -11
- package/dist/services/realtime/service.d.ts +60 -0
- package/dist/streaming.browser.mjs +29 -0
- package/dist/streaming.cjs +29 -0
- package/dist/streaming.mjs +29 -0
- package/dist/types/openapi.generated.d.ts +41 -28
- package/dist/workerd.mjs +30 -11
- package/package.json +27 -15
- package/src/services/realtime/service.ts +65 -0
- package/src/services/transcripts/index.ts +0 -13
- package/src/types/openapi.generated.ts +53 -33
package/dist/streaming.mjs
CHANGED
|
@@ -84,7 +84,14 @@ class RealtimeError extends Error {
|
|
|
84
84
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
85
85
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
86
86
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
87
|
+
/**
|
|
88
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
89
|
+
*/
|
|
87
90
|
class RealtimeTranscriber {
|
|
91
|
+
/**
|
|
92
|
+
* Create a new RealtimeTranscriber.
|
|
93
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
94
|
+
*/
|
|
88
95
|
constructor(params) {
|
|
89
96
|
var _a, _b;
|
|
90
97
|
this.listeners = {};
|
|
@@ -125,10 +132,19 @@ class RealtimeTranscriber {
|
|
|
125
132
|
url.search = searchParams.toString();
|
|
126
133
|
return url;
|
|
127
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Add a listener for an event.
|
|
137
|
+
* @param event - The event to listen for.
|
|
138
|
+
* @param listener - The function to call when the event is emitted.
|
|
139
|
+
*/
|
|
128
140
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
141
|
on(event, listener) {
|
|
130
142
|
this.listeners[event] = listener;
|
|
131
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Connect to the server and begin a new session.
|
|
146
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
147
|
+
*/
|
|
132
148
|
connect() {
|
|
133
149
|
return new Promise((resolve) => {
|
|
134
150
|
if (this.socket) {
|
|
@@ -210,9 +226,17 @@ class RealtimeTranscriber {
|
|
|
210
226
|
};
|
|
211
227
|
});
|
|
212
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Send audio data to the server.
|
|
231
|
+
* @param audio - The audio data to send to the server.
|
|
232
|
+
*/
|
|
213
233
|
sendAudio(audio) {
|
|
214
234
|
this.send(audio);
|
|
215
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
238
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
239
|
+
*/
|
|
216
240
|
stream() {
|
|
217
241
|
return new WritableStream({
|
|
218
242
|
write: (chunk) => {
|
|
@@ -240,6 +264,11 @@ class RealtimeTranscriber {
|
|
|
240
264
|
}
|
|
241
265
|
this.socket.send(data);
|
|
242
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Close the connection to the server.
|
|
269
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
270
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
271
|
+
*/
|
|
243
272
|
close() {
|
|
244
273
|
return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
|
|
245
274
|
var _a;
|
|
@@ -529,12 +529,7 @@ export type LemurActionItemsParams = LemurBaseParams & {
|
|
|
529
529
|
* }
|
|
530
530
|
* ```
|
|
531
531
|
*/
|
|
532
|
-
export type LemurActionItemsResponse =
|
|
533
|
-
/**
|
|
534
|
-
* The response generated by LeMUR
|
|
535
|
-
*/
|
|
536
|
-
response: string;
|
|
537
|
-
};
|
|
532
|
+
export type LemurActionItemsResponse = LemurStringResponse;
|
|
538
533
|
/**
|
|
539
534
|
* @example
|
|
540
535
|
* ```js
|
|
@@ -562,7 +557,6 @@ export type LemurBaseParams = {
|
|
|
562
557
|
]>;
|
|
563
558
|
/**
|
|
564
559
|
* The model that is used for the final prompt after compression is performed.
|
|
565
|
-
* Defaults to "default".
|
|
566
560
|
*
|
|
567
561
|
* @defaultValue "default
|
|
568
562
|
*/
|
|
@@ -614,7 +608,7 @@ export type LemurBaseResponse = {
|
|
|
614
608
|
* The model that is used for the final prompt after compression is performed.
|
|
615
609
|
*
|
|
616
610
|
*/
|
|
617
|
-
export type LemurModel = "
|
|
611
|
+
export type LemurModel = "anthropic/claude-3-5-sonnet" | "anthropic/claude-3-opus" | "anthropic/claude-3-haiku" | "anthropic/claude-3-sonnet" | "anthropic/claude-2-1" | "anthropic/claude-2" | "default" | "anthropic/claude-instant-1-2" | "basic" | "assemblyai/mistral-7b";
|
|
618
612
|
/**
|
|
619
613
|
* @example
|
|
620
614
|
* ```js
|
|
@@ -732,7 +726,26 @@ export type LemurQuestionAnswerResponse = LemurBaseResponse & {
|
|
|
732
726
|
*/
|
|
733
727
|
response: LemurQuestionAnswer[];
|
|
734
728
|
};
|
|
735
|
-
export type LemurResponse =
|
|
729
|
+
export type LemurResponse = LemurStringResponse | LemurQuestionAnswerResponse;
|
|
730
|
+
/**
|
|
731
|
+
* @example
|
|
732
|
+
* ```js
|
|
733
|
+
* {
|
|
734
|
+
* "request_id": "5e1b27c2-691f-4414-8bc5-f14678442f9e",
|
|
735
|
+
* "response": "Based on the transcript, the following locations were mentioned as being affected by wildfire smoke from Canada:\n\n- Maine\n- Maryland\n- Minnesota\n- Mid Atlantic region\n- Northeast region\n- New York City\n- Baltimore\n",
|
|
736
|
+
* "usage": {
|
|
737
|
+
* "input_tokens": 27,
|
|
738
|
+
* "output_tokens": 3
|
|
739
|
+
* }
|
|
740
|
+
* }
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
export type LemurStringResponse = {
|
|
744
|
+
/**
|
|
745
|
+
* The response generated by LeMUR.
|
|
746
|
+
*/
|
|
747
|
+
response: string;
|
|
748
|
+
} & LemurBaseResponse;
|
|
736
749
|
/**
|
|
737
750
|
* @example
|
|
738
751
|
* ```js
|
|
@@ -766,12 +779,7 @@ export type LemurSummaryParams = LemurBaseParams & {
|
|
|
766
779
|
* }
|
|
767
780
|
* ```
|
|
768
781
|
*/
|
|
769
|
-
export type LemurSummaryResponse =
|
|
770
|
-
/**
|
|
771
|
-
* The response generated by LeMUR
|
|
772
|
-
*/
|
|
773
|
-
response: string;
|
|
774
|
-
};
|
|
782
|
+
export type LemurSummaryResponse = LemurStringResponse;
|
|
775
783
|
/**
|
|
776
784
|
* @example
|
|
777
785
|
* ```js
|
|
@@ -806,12 +814,7 @@ export type LemurTaskParams = {
|
|
|
806
814
|
* }
|
|
807
815
|
* ```
|
|
808
816
|
*/
|
|
809
|
-
export type LemurTaskResponse =
|
|
810
|
-
/**
|
|
811
|
-
* The response generated by LeMUR.
|
|
812
|
-
*/
|
|
813
|
-
response: string;
|
|
814
|
-
} & LemurBaseResponse;
|
|
817
|
+
export type LemurTaskResponse = LemurStringResponse;
|
|
815
818
|
/**
|
|
816
819
|
* The usage numbers for the LeMUR request
|
|
817
820
|
*/
|
|
@@ -1025,6 +1028,10 @@ export type RealtimeTemporaryTokenResponse = {
|
|
|
1025
1028
|
*/
|
|
1026
1029
|
token: string;
|
|
1027
1030
|
};
|
|
1031
|
+
/**
|
|
1032
|
+
* The notification when the redacted audio is ready.
|
|
1033
|
+
*/
|
|
1034
|
+
export type RedactedAudioNotification = RedactedAudioResponse;
|
|
1028
1035
|
/**
|
|
1029
1036
|
* @example
|
|
1030
1037
|
* ```js
|
|
@@ -1219,7 +1226,7 @@ export type SeverityScoreSummary = {
|
|
|
1219
1226
|
/**
|
|
1220
1227
|
* The speech model to use for the transcription.
|
|
1221
1228
|
*/
|
|
1222
|
-
export type SpeechModel = "best" | "nano"
|
|
1229
|
+
export type SpeechModel = "best" | "nano";
|
|
1223
1230
|
/**
|
|
1224
1231
|
* The replacement logic for detected PII, can be "entity_name" or "hash". See {@link https://www.assemblyai.com/docs/models/pii-redaction | PII redaction } for more details.
|
|
1225
1232
|
*/
|
|
@@ -2343,15 +2350,17 @@ export type Transcript = {
|
|
|
2343
2350
|
*/
|
|
2344
2351
|
webhook_auth: boolean;
|
|
2345
2352
|
/**
|
|
2346
|
-
* The header name
|
|
2353
|
+
* The header name to be sent with the transcript completed or failed webhook requests
|
|
2347
2354
|
*/
|
|
2348
2355
|
webhook_auth_header_name?: string | null;
|
|
2349
2356
|
/**
|
|
2350
|
-
* The status code we received from your server when delivering
|
|
2357
|
+
* The status code we received from your server when delivering the transcript completed or failed webhook request, if a webhook URL was provided
|
|
2351
2358
|
*/
|
|
2352
2359
|
webhook_status_code?: number | null;
|
|
2353
2360
|
/**
|
|
2354
|
-
* The URL to which we send
|
|
2361
|
+
* The URL to which we send webhook requests.
|
|
2362
|
+
* We sends two different types of webhook requests.
|
|
2363
|
+
* One request when a transcript is completed or failed, and one request when the redacted audio is ready if redact_pii_audio is enabled.
|
|
2355
2364
|
*/
|
|
2356
2365
|
webhook_url?: string | null;
|
|
2357
2366
|
/**
|
|
@@ -2660,17 +2669,17 @@ export type TranscriptOptionalParams = {
|
|
|
2660
2669
|
*/
|
|
2661
2670
|
topics?: string[];
|
|
2662
2671
|
/**
|
|
2663
|
-
* The header name
|
|
2672
|
+
* The header name to be sent with the transcript completed or failed webhook requests
|
|
2664
2673
|
* @defaultValue null
|
|
2665
2674
|
*/
|
|
2666
2675
|
webhook_auth_header_name?: string | null;
|
|
2667
2676
|
/**
|
|
2668
|
-
*
|
|
2677
|
+
* The header value to send back with the transcript completed or failed webhook requests for added security
|
|
2669
2678
|
* @defaultValue null
|
|
2670
2679
|
*/
|
|
2671
2680
|
webhook_auth_header_value?: string | null;
|
|
2672
2681
|
/**
|
|
2673
|
-
* The URL to which
|
|
2682
|
+
* The URL to which we send webhook requests. We sends two different types of webhook requests. One request when a transcript is completed or failed, and one request when the redacted audio is ready if redact_pii_audio is enabled.
|
|
2674
2683
|
*/
|
|
2675
2684
|
webhook_url?: string;
|
|
2676
2685
|
/**
|
|
@@ -3013,6 +3022,10 @@ export type TranscriptUtterance = {
|
|
|
3013
3022
|
*/
|
|
3014
3023
|
words: TranscriptWord[];
|
|
3015
3024
|
};
|
|
3025
|
+
/**
|
|
3026
|
+
* The notifications sent to the webhook URL.
|
|
3027
|
+
*/
|
|
3028
|
+
export type TranscriptWebhookNotification = TranscriptReadyNotification | RedactedAudioNotification;
|
|
3016
3029
|
/**
|
|
3017
3030
|
* @example
|
|
3018
3031
|
* ```js
|
package/dist/workerd.mjs
CHANGED
|
@@ -15,7 +15,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
15
15
|
defaultUserAgentString += navigator.userAgent;
|
|
16
16
|
}
|
|
17
17
|
const defaultUserAgent = {
|
|
18
|
-
sdk: { name: "JavaScript", version: "4.
|
|
18
|
+
sdk: { name: "JavaScript", version: "4.6.1" },
|
|
19
19
|
};
|
|
20
20
|
if (typeof process !== "undefined") {
|
|
21
21
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -191,7 +191,14 @@ class RealtimeError extends Error {
|
|
|
191
191
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
192
192
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
193
193
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
194
|
+
/**
|
|
195
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
196
|
+
*/
|
|
194
197
|
class RealtimeTranscriber {
|
|
198
|
+
/**
|
|
199
|
+
* Create a new RealtimeTranscriber.
|
|
200
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
201
|
+
*/
|
|
195
202
|
constructor(params) {
|
|
196
203
|
this.listeners = {};
|
|
197
204
|
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
|
|
@@ -231,10 +238,19 @@ class RealtimeTranscriber {
|
|
|
231
238
|
url.search = searchParams.toString();
|
|
232
239
|
return url;
|
|
233
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Add a listener for an event.
|
|
243
|
+
* @param event - The event to listen for.
|
|
244
|
+
* @param listener - The function to call when the event is emitted.
|
|
245
|
+
*/
|
|
234
246
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
235
247
|
on(event, listener) {
|
|
236
248
|
this.listeners[event] = listener;
|
|
237
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Connect to the server and begin a new session.
|
|
252
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
253
|
+
*/
|
|
238
254
|
connect() {
|
|
239
255
|
return new Promise((resolve) => {
|
|
240
256
|
if (this.socket) {
|
|
@@ -313,9 +329,17 @@ class RealtimeTranscriber {
|
|
|
313
329
|
};
|
|
314
330
|
});
|
|
315
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* Send audio data to the server.
|
|
334
|
+
* @param audio - The audio data to send to the server.
|
|
335
|
+
*/
|
|
316
336
|
sendAudio(audio) {
|
|
317
337
|
this.send(audio);
|
|
318
338
|
}
|
|
339
|
+
/**
|
|
340
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
341
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
342
|
+
*/
|
|
319
343
|
stream() {
|
|
320
344
|
return new WritableStream({
|
|
321
345
|
write: (chunk) => {
|
|
@@ -343,6 +367,11 @@ class RealtimeTranscriber {
|
|
|
343
367
|
}
|
|
344
368
|
this.socket.send(data);
|
|
345
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Close the connection to the server.
|
|
372
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
373
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
374
|
+
*/
|
|
346
375
|
async close(waitForSessionTermination = true) {
|
|
347
376
|
if (this.socket) {
|
|
348
377
|
if (this.socket.readyState === this.socket.OPEN) {
|
|
@@ -429,7 +458,6 @@ class TranscriptService extends BaseService {
|
|
|
429
458
|
* @returns A promise that resolves to the transcript. The transcript status is "completed" or "error".
|
|
430
459
|
*/
|
|
431
460
|
async transcribe(params, options) {
|
|
432
|
-
deprecateConformer2(params);
|
|
433
461
|
const transcript = await this.submit(params);
|
|
434
462
|
return await this.waitUntilReady(transcript.id, options);
|
|
435
463
|
}
|
|
@@ -439,7 +467,6 @@ class TranscriptService extends BaseService {
|
|
|
439
467
|
* @returns A promise that resolves to the queued transcript.
|
|
440
468
|
*/
|
|
441
469
|
async submit(params) {
|
|
442
|
-
deprecateConformer2(params);
|
|
443
470
|
let audioUrl;
|
|
444
471
|
let transcriptParams = undefined;
|
|
445
472
|
if ("audio" in params) {
|
|
@@ -483,7 +510,6 @@ class TranscriptService extends BaseService {
|
|
|
483
510
|
* @deprecated Use `transcribe` instead to transcribe a audio file that includes polling, or `submit` to transcribe a audio file without polling.
|
|
484
511
|
*/
|
|
485
512
|
async create(params, options) {
|
|
486
|
-
deprecateConformer2(params);
|
|
487
513
|
const path = getPath(params.audio_url);
|
|
488
514
|
if (path !== null) {
|
|
489
515
|
const uploadUrl = await this.files.upload(path);
|
|
@@ -646,13 +672,6 @@ class TranscriptService extends BaseService {
|
|
|
646
672
|
};
|
|
647
673
|
}
|
|
648
674
|
}
|
|
649
|
-
function deprecateConformer2(params) {
|
|
650
|
-
if (!params)
|
|
651
|
-
return;
|
|
652
|
-
if (params.speech_model === "conformer-2") {
|
|
653
|
-
console.warn("The speech_model conformer-2 option is deprecated and will stop working in the near future. Use best or nano instead.");
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
675
|
|
|
657
676
|
const readFile = async function (
|
|
658
677
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblyai",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.1",
|
|
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"
|
|
@@ -115,39 +115,51 @@
|
|
|
115
115
|
"docs"
|
|
116
116
|
],
|
|
117
117
|
"devDependencies": {
|
|
118
|
-
"@babel/preset-env": "^7.24.
|
|
119
|
-
"@babel/preset-typescript": "^7.24.
|
|
118
|
+
"@babel/preset-env": "^7.24.7",
|
|
119
|
+
"@babel/preset-typescript": "^7.24.7",
|
|
120
120
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
121
|
-
"@rollup/plugin-replace": "^5.0.
|
|
121
|
+
"@rollup/plugin-replace": "^5.0.7",
|
|
122
122
|
"@rollup/plugin-terser": "^0.4.4",
|
|
123
123
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
124
124
|
"@types/jest": "^29.5.12",
|
|
125
|
-
"@types/node": "^18.19.
|
|
125
|
+
"@types/node": "^18.19.38",
|
|
126
126
|
"@types/websocket": "^1.0.10",
|
|
127
127
|
"@types/ws": "^8.5.10",
|
|
128
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
128
|
+
"@typescript-eslint/eslint-plugin": "^7.13.1",
|
|
129
129
|
"dotenv": "^16.4.5",
|
|
130
130
|
"eslint": "^8.57.0",
|
|
131
|
-
"eslint-plugin-tsdoc": "^0.
|
|
131
|
+
"eslint-plugin-tsdoc": "^0.3.0",
|
|
132
132
|
"jest": "^29.7.0",
|
|
133
133
|
"jest-cli": "^29.7.0",
|
|
134
134
|
"jest-fetch-mock": "^3.0.3",
|
|
135
135
|
"jest-junit": "^16.0.0",
|
|
136
136
|
"jest-websocket-mock": "^2.5.0",
|
|
137
|
-
"jsr": "^0.12.4",
|
|
138
137
|
"mock-socket": "^9.3.1",
|
|
139
138
|
"openapi-typescript": "^6.7.5",
|
|
140
|
-
"prettier": "^3.2
|
|
141
|
-
"publint": "^0.2.
|
|
142
|
-
"rimraf": "^5.0.
|
|
143
|
-
"rollup": "^4.
|
|
144
|
-
"ts-jest": "^29.1.
|
|
145
|
-
"tslib": "^2.
|
|
139
|
+
"prettier": "^3.3.2",
|
|
140
|
+
"publint": "^0.2.8",
|
|
141
|
+
"rimraf": "^5.0.7",
|
|
142
|
+
"rollup": "^4.18.0",
|
|
143
|
+
"ts-jest": "^29.1.5",
|
|
144
|
+
"tslib": "^2.6.3",
|
|
146
145
|
"typedoc": "^0.25.13",
|
|
147
146
|
"typedoc-plugin-extras": "^3.0.0",
|
|
148
147
|
"typescript": "^5.4.5"
|
|
149
148
|
},
|
|
150
149
|
"dependencies": {
|
|
151
|
-
"ws": "^8.17.
|
|
150
|
+
"ws": "^8.17.1"
|
|
151
|
+
},
|
|
152
|
+
"pnpm": {
|
|
153
|
+
"packageExtensions": {
|
|
154
|
+
"ws": {
|
|
155
|
+
"peerDependencies": {
|
|
156
|
+
"@types/ws": "^8.5.10"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
"overrides": {
|
|
161
|
+
"undici@<5.28.4": ">=5.28.4",
|
|
162
|
+
"braces@<3.0.3": ">=3.0.3"
|
|
163
|
+
}
|
|
152
164
|
}
|
|
153
165
|
}
|
|
@@ -45,6 +45,9 @@ type BufferLike =
|
|
|
45
45
|
| { valueOf(): string }
|
|
46
46
|
| { [Symbol.toPrimitive](hint: string): string };
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
50
|
+
*/
|
|
48
51
|
export class RealtimeTranscriber {
|
|
49
52
|
private realtimeUrl: string;
|
|
50
53
|
private sampleRate: number;
|
|
@@ -59,6 +62,10 @@ export class RealtimeTranscriber {
|
|
|
59
62
|
private listeners: RealtimeListeners = {};
|
|
60
63
|
private sessionTerminatedResolve?: () => void;
|
|
61
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Create a new RealtimeTranscriber.
|
|
67
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
68
|
+
*/
|
|
62
69
|
constructor(params: RealtimeTranscriberParams) {
|
|
63
70
|
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
|
|
64
71
|
this.sampleRate = params.sampleRate ?? 16_000;
|
|
@@ -106,30 +113,75 @@ export class RealtimeTranscriber {
|
|
|
106
113
|
return url;
|
|
107
114
|
}
|
|
108
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Listen for the open event which is emitted when the connection is established and the session begins.
|
|
118
|
+
* @param event - The open event.
|
|
119
|
+
* @param listener - The function to call when the event is emitted.
|
|
120
|
+
*/
|
|
109
121
|
on(event: "open", listener: (event: SessionBeginsEventData) => void): void;
|
|
122
|
+
/**
|
|
123
|
+
* Listen for the transcript event which is emitted when a partian or final transcript is received.
|
|
124
|
+
* @param event - The transcript event.
|
|
125
|
+
* @param listener - The function to call when the event is emitted.
|
|
126
|
+
*/
|
|
110
127
|
on(
|
|
111
128
|
event: "transcript",
|
|
112
129
|
listener: (transcript: RealtimeTranscript) => void,
|
|
113
130
|
): void;
|
|
131
|
+
/**
|
|
132
|
+
* Listen for the partial transcript event which is emitted when a partial transcript is received.
|
|
133
|
+
* @param event - The partial transcript event.
|
|
134
|
+
* @param listener - The function to call when the event is emitted.
|
|
135
|
+
*/
|
|
114
136
|
on(
|
|
115
137
|
event: "transcript.partial",
|
|
116
138
|
listener: (transcript: PartialTranscript) => void,
|
|
117
139
|
): void;
|
|
140
|
+
/**
|
|
141
|
+
* Listen for the final transcript event which is emitted when a final transcript is received.
|
|
142
|
+
* @param event - The final transcript event.
|
|
143
|
+
* @param listener - The function to call when the event is emitted.
|
|
144
|
+
*/
|
|
118
145
|
on(
|
|
119
146
|
event: "transcript.final",
|
|
120
147
|
listener: (transcript: FinalTranscript) => void,
|
|
121
148
|
): void;
|
|
149
|
+
/**
|
|
150
|
+
* Listen for the session information event which is emitted when session information is received.
|
|
151
|
+
* The session information is sent right before the session is terminated.
|
|
152
|
+
* @param event - The session information event.
|
|
153
|
+
* @param listener - The function to call when the event is emitted.
|
|
154
|
+
*/
|
|
122
155
|
on(
|
|
123
156
|
event: "session_information",
|
|
124
157
|
listener: (info: SessionInformation) => void,
|
|
125
158
|
): void;
|
|
159
|
+
/**
|
|
160
|
+
* Listen for the error event which is emitted when an error occurs.
|
|
161
|
+
* @param event - The error event.
|
|
162
|
+
* @param listener - The function to call when the event is emitted.
|
|
163
|
+
*/
|
|
126
164
|
on(event: "error", listener: (error: Error) => void): void;
|
|
165
|
+
/**
|
|
166
|
+
* Listen for the close event which is emitted when the connection is closed.
|
|
167
|
+
* @param event - The close event.
|
|
168
|
+
* @param listener - The function to call when the event is emitted.
|
|
169
|
+
*/
|
|
127
170
|
on(event: "close", listener: (code: number, reason: string) => void): void;
|
|
171
|
+
/**
|
|
172
|
+
* Add a listener for an event.
|
|
173
|
+
* @param event - The event to listen for.
|
|
174
|
+
* @param listener - The function to call when the event is emitted.
|
|
175
|
+
*/
|
|
128
176
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
177
|
on(event: RealtimeEvents, listener: (...args: any[]) => void) {
|
|
130
178
|
this.listeners[event] = listener;
|
|
131
179
|
}
|
|
132
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Connect to the server and begin a new session.
|
|
183
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
184
|
+
*/
|
|
133
185
|
connect() {
|
|
134
186
|
return new Promise<SessionBeginsEventData>((resolve) => {
|
|
135
187
|
if (this.socket) {
|
|
@@ -216,10 +268,18 @@ export class RealtimeTranscriber {
|
|
|
216
268
|
});
|
|
217
269
|
}
|
|
218
270
|
|
|
271
|
+
/**
|
|
272
|
+
* Send audio data to the server.
|
|
273
|
+
* @param audio - The audio data to send to the server.
|
|
274
|
+
*/
|
|
219
275
|
sendAudio(audio: AudioData) {
|
|
220
276
|
this.send(audio);
|
|
221
277
|
}
|
|
222
278
|
|
|
279
|
+
/**
|
|
280
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
281
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
282
|
+
*/
|
|
223
283
|
stream(): WritableStream<AudioData> {
|
|
224
284
|
return new WritableStream<AudioData>({
|
|
225
285
|
write: (chunk: AudioData) => {
|
|
@@ -251,6 +311,11 @@ export class RealtimeTranscriber {
|
|
|
251
311
|
this.socket.send(data);
|
|
252
312
|
}
|
|
253
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Close the connection to the server.
|
|
316
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
317
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
318
|
+
*/
|
|
254
319
|
async close(waitForSessionTermination = true) {
|
|
255
320
|
if (this.socket) {
|
|
256
321
|
if (this.socket.readyState === this.socket.OPEN) {
|
|
@@ -15,7 +15,6 @@ import {
|
|
|
15
15
|
TranscribeParams,
|
|
16
16
|
TranscribeOptions,
|
|
17
17
|
SubmitParams,
|
|
18
|
-
SpeechModel,
|
|
19
18
|
RedactedAudioFile,
|
|
20
19
|
} from "../..";
|
|
21
20
|
import { FileService } from "../files";
|
|
@@ -39,7 +38,6 @@ export class TranscriptService extends BaseService {
|
|
|
39
38
|
params: TranscribeParams,
|
|
40
39
|
options?: TranscribeOptions,
|
|
41
40
|
): Promise<Transcript> {
|
|
42
|
-
deprecateConformer2(params);
|
|
43
41
|
const transcript = await this.submit(params);
|
|
44
42
|
return await this.waitUntilReady(transcript.id, options);
|
|
45
43
|
}
|
|
@@ -50,7 +48,6 @@ export class TranscriptService extends BaseService {
|
|
|
50
48
|
* @returns A promise that resolves to the queued transcript.
|
|
51
49
|
*/
|
|
52
50
|
async submit(params: SubmitParams): Promise<Transcript> {
|
|
53
|
-
deprecateConformer2(params);
|
|
54
51
|
let audioUrl;
|
|
55
52
|
let transcriptParams: TranscriptParams | undefined = undefined;
|
|
56
53
|
if ("audio" in params) {
|
|
@@ -95,7 +92,6 @@ export class TranscriptService extends BaseService {
|
|
|
95
92
|
params: TranscriptParams,
|
|
96
93
|
options?: CreateTranscriptOptions,
|
|
97
94
|
): Promise<Transcript> {
|
|
98
|
-
deprecateConformer2(params);
|
|
99
95
|
const path = getPath(params.audio_url);
|
|
100
96
|
if (path !== null) {
|
|
101
97
|
const uploadUrl = await this.files.upload(path);
|
|
@@ -287,12 +283,3 @@ export class TranscriptService extends BaseService {
|
|
|
287
283
|
};
|
|
288
284
|
}
|
|
289
285
|
}
|
|
290
|
-
|
|
291
|
-
function deprecateConformer2(params: { speech_model?: SpeechModel | null }) {
|
|
292
|
-
if (!params) return;
|
|
293
|
-
if (params.speech_model === "conformer-2") {
|
|
294
|
-
console.warn(
|
|
295
|
-
"The speech_model conformer-2 option is deprecated and will stop working in the near future. Use best or nano instead.",
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
}
|