assemblyai 4.26.1 → 4.28.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/README.md +75 -99
- package/dist/assemblyai.umd.js +1 -1
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +1 -1
- package/dist/bun.mjs +1 -1
- package/dist/deno.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/node.cjs +1 -1
- package/dist/node.mjs +1 -1
- package/dist/types/openapi.generated.d.ts +26 -0
- package/dist/workerd.mjs +1 -1
- package/package.json +1 -1
- package/src/types/openapi.generated.ts +29 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# AssemblyAI JavaScript SDK
|
|
14
14
|
|
|
15
15
|
The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API,
|
|
16
|
-
which supports async and streaming transcription
|
|
16
|
+
which supports async and streaming transcription.
|
|
17
17
|
It is written primarily for Node.js in TypeScript with all types exported, but also [compatible with other runtimes](./docs/compat.md).
|
|
18
18
|
|
|
19
19
|
## Documentation
|
|
@@ -46,8 +46,11 @@ Then, import the `assemblyai` module and create an AssemblyAI object with your A
|
|
|
46
46
|
```js
|
|
47
47
|
import { AssemblyAI } from "assemblyai";
|
|
48
48
|
|
|
49
|
+
const baseUrl = "https://api.assemblyai.com";
|
|
50
|
+
|
|
49
51
|
const client = new AssemblyAI({
|
|
50
|
-
apiKey:
|
|
52
|
+
apiKey: "YOUR_API_KEY",
|
|
53
|
+
baseUrl: baseUrl,
|
|
51
54
|
});
|
|
52
55
|
```
|
|
53
56
|
|
|
@@ -96,9 +99,20 @@ When you create a transcript, you can either pass in a URL to an audio file or u
|
|
|
96
99
|
|
|
97
100
|
```js
|
|
98
101
|
// Transcribe file at remote URL
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
const audioFile = "https://assembly.ai/sports_injuries.mp3";
|
|
103
|
+
|
|
104
|
+
const params = {
|
|
105
|
+
audio: audioFile,
|
|
106
|
+
speech_models: ["universal-3-pro", "universal-2"],
|
|
107
|
+
language_detection: true,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const run = async () => {
|
|
111
|
+
const transcript = await client.transcripts.transcribe(params);
|
|
112
|
+
console.log(transcript.text);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
run();
|
|
102
116
|
```
|
|
103
117
|
|
|
104
118
|
> [!NOTE]
|
|
@@ -111,6 +125,8 @@ If you don't want to wait until the transcript is ready, you can use `submit`:
|
|
|
111
125
|
```js
|
|
112
126
|
let transcript = await client.transcripts.submit({
|
|
113
127
|
audio: "https://assembly.ai/espn.m4a",
|
|
128
|
+
speech_models: ["universal-3-pro", "universal-2"],
|
|
129
|
+
language_detection: true,
|
|
114
130
|
});
|
|
115
131
|
```
|
|
116
132
|
|
|
@@ -125,6 +141,8 @@ When you create a transcript, you can either pass in a URL to an audio file or u
|
|
|
125
141
|
// Upload a file via local path and transcribe
|
|
126
142
|
let transcript = await client.transcripts.transcribe({
|
|
127
143
|
audio: "./news.mp4",
|
|
144
|
+
speech_models: ["universal-3-pro", "universal-2"],
|
|
145
|
+
language_detection: true,
|
|
128
146
|
});
|
|
129
147
|
```
|
|
130
148
|
|
|
@@ -138,25 +156,44 @@ If you don't want to wait until the transcript is ready, you can use `submit`:
|
|
|
138
156
|
```js
|
|
139
157
|
let transcript = await client.transcripts.submit({
|
|
140
158
|
audio: "./news.mp4",
|
|
159
|
+
speech_models: ["universal-3-pro", "universal-2"],
|
|
160
|
+
language_detection: true,
|
|
141
161
|
});
|
|
142
162
|
```
|
|
143
163
|
|
|
144
164
|
</details>
|
|
145
165
|
|
|
146
166
|
<details>
|
|
147
|
-
<summary>Enable additional
|
|
167
|
+
<summary>Enable additional Speech Understanding models</summary>
|
|
148
168
|
|
|
149
|
-
You can extract even more insights from the audio by enabling any of our
|
|
150
|
-
For example, here's how to enable [Speaker diarization](https://www.assemblyai.com/docs/
|
|
169
|
+
You can extract even more insights from the audio by enabling any of our Speech Understanding models using _transcription options_.
|
|
170
|
+
For example, here's how to enable [Speaker diarization](https://www.assemblyai.com/docs/pre-recorded-audio/speaker-diarization) model to detect who said what.
|
|
151
171
|
|
|
152
172
|
```js
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
173
|
+
import { AssemblyAI } from "assemblyai";
|
|
174
|
+
|
|
175
|
+
const client = new AssemblyAI({
|
|
176
|
+
apiKey: "<YOUR_API_KEY>",
|
|
156
177
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
178
|
+
|
|
179
|
+
const audioFile = "https://assembly.ai/wildfires.mp3";
|
|
180
|
+
|
|
181
|
+
const params = {
|
|
182
|
+
audio: audioFile,
|
|
183
|
+
speech_models: ["universal-3-pro", "universal-2"],
|
|
184
|
+
language_detection: true,
|
|
185
|
+
speaker_labels: true,
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const run = async () => {
|
|
189
|
+
const transcript = await client.transcripts.transcribe(params);
|
|
190
|
+
|
|
191
|
+
for (const utterance of transcript.utterances!) {
|
|
192
|
+
console.log(`Speaker ${utterance.speaker}: ${utterance.text}`);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
run();
|
|
160
197
|
```
|
|
161
198
|
|
|
162
199
|
</details>
|
|
@@ -187,7 +224,15 @@ const transcript = await client.transcripts.waitUntilReady(transcript.id, {
|
|
|
187
224
|
|
|
188
225
|
```js
|
|
189
226
|
const sentences = await client.transcripts.sentences(transcript.id);
|
|
190
|
-
const paragraphs = await client.transcripts.paragraphs(transcript.id);
|
|
227
|
+
const { paragraphs } = await client.transcripts.paragraphs(transcript.id);
|
|
228
|
+
|
|
229
|
+
for (const paragraph of paragraphs) {
|
|
230
|
+
console.log(paragraph.text);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
for (const sentence of sentences) {
|
|
234
|
+
console.log(sentence.text);
|
|
235
|
+
}
|
|
191
236
|
```
|
|
192
237
|
|
|
193
238
|
</details>
|
|
@@ -240,20 +285,17 @@ const res = await client.transcripts.delete(transcript.id);
|
|
|
240
285
|
|
|
241
286
|
</details>
|
|
242
287
|
|
|
243
|
-
### Transcribe
|
|
288
|
+
### Transcribe streaming audio
|
|
244
289
|
|
|
245
|
-
|
|
290
|
+
Refer to [AssemblyAI's streaming documentation](https://www.assemblyai.com/docs/getting-started/transcribe-streaming-audio) for full code examples.
|
|
246
291
|
|
|
247
|
-
|
|
248
|
-
const rt = client.streaming.transcriber();
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
You can also pass in the following options.
|
|
292
|
+
Create the streaming transcriber.
|
|
252
293
|
|
|
253
294
|
```typescript
|
|
254
|
-
const
|
|
255
|
-
|
|
295
|
+
const transcriber = client.streaming.transcriber({
|
|
296
|
+
speechModel: "u3-rt-pro",
|
|
256
297
|
sampleRate: 16_000,
|
|
298
|
+
formatTurns: true,
|
|
257
299
|
});
|
|
258
300
|
```
|
|
259
301
|
|
|
@@ -275,7 +317,7 @@ const rt = client.streaming.transcriber({
|
|
|
275
317
|
> import { StreamingTranscriber } from "assemblyai";
|
|
276
318
|
> // TODO: implement getToken to retrieve token from server
|
|
277
319
|
> const token = await getToken();
|
|
278
|
-
> const
|
|
320
|
+
> const transcriber = new StreamingTranscriber({
|
|
279
321
|
> token,
|
|
280
322
|
> });
|
|
281
323
|
> ```
|
|
@@ -284,16 +326,16 @@ You can configure the following events.
|
|
|
284
326
|
|
|
285
327
|
<!-- prettier-ignore -->
|
|
286
328
|
```typescript
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
329
|
+
transcriber.on("open", ({ id, expires_at }) => console.log('Session ID:', id, 'Expires at:', expires_at));
|
|
330
|
+
transcriber.on("close", (code: number, reason: string) => console.log('Closed', code, reason));
|
|
331
|
+
transcriber.on("turn", ({ transcript }) => console.log('Transcript:', transcript));
|
|
332
|
+
transcriber.on("error", (error: Error) => console.error('Error', error));
|
|
291
333
|
```
|
|
292
334
|
|
|
293
335
|
After configuring your events, connect to the server.
|
|
294
336
|
|
|
295
337
|
```typescript
|
|
296
|
-
await
|
|
338
|
+
await transcriber.connect();
|
|
297
339
|
```
|
|
298
340
|
|
|
299
341
|
Send audio data via chunks.
|
|
@@ -301,88 +343,22 @@ Send audio data via chunks.
|
|
|
301
343
|
```typescript
|
|
302
344
|
// Pseudo code for getting audio
|
|
303
345
|
getAudio((chunk) => {
|
|
304
|
-
|
|
346
|
+
transcriber.sendAudio(chunk);
|
|
305
347
|
});
|
|
306
348
|
```
|
|
307
349
|
|
|
308
350
|
Or send audio data via a stream:
|
|
309
351
|
|
|
310
352
|
```typescript
|
|
311
|
-
audioStream.pipeTo(
|
|
353
|
+
audioStream.pipeTo(transcriber.stream());
|
|
312
354
|
```
|
|
313
355
|
|
|
314
356
|
Close the connection when you're finished.
|
|
315
357
|
|
|
316
358
|
```typescript
|
|
317
|
-
await
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
## Apply LLMs to your audio with LeMUR
|
|
321
|
-
|
|
322
|
-
Call [LeMUR endpoints](https://www.assemblyai.com/docs/api-reference/lemur) to apply LLMs to your transcript.
|
|
323
|
-
|
|
324
|
-
<details open>
|
|
325
|
-
<summary>Prompt your audio with LeMUR</summary>
|
|
326
|
-
|
|
327
|
-
```js
|
|
328
|
-
const { response } = await client.lemur.task({
|
|
329
|
-
transcript_ids: ["0d295578-8c75-421a-885a-2c487f188927"],
|
|
330
|
-
prompt: "Write a haiku about this conversation.",
|
|
331
|
-
});
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
</details>
|
|
335
|
-
|
|
336
|
-
<details>
|
|
337
|
-
<summary>Summarize with LeMUR</summary>
|
|
338
|
-
|
|
339
|
-
```js
|
|
340
|
-
const { response } = await client.lemur.summary({
|
|
341
|
-
transcript_ids: ["0d295578-8c75-421a-885a-2c487f188927"],
|
|
342
|
-
answer_format: "one sentence",
|
|
343
|
-
context: {
|
|
344
|
-
speakers: ["Alex", "Bob"],
|
|
345
|
-
},
|
|
346
|
-
});
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
</details>
|
|
350
|
-
|
|
351
|
-
<details>
|
|
352
|
-
<summary>Ask questions</summary>
|
|
353
|
-
|
|
354
|
-
```js
|
|
355
|
-
const { response } = await client.lemur.questionAnswer({
|
|
356
|
-
transcript_ids: ["0d295578-8c75-421a-885a-2c487f188927"],
|
|
357
|
-
questions: [
|
|
358
|
-
{
|
|
359
|
-
question: "What are they discussing?",
|
|
360
|
-
answer_format: "text",
|
|
361
|
-
},
|
|
362
|
-
],
|
|
363
|
-
});
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
</details>
|
|
367
|
-
<details>
|
|
368
|
-
<summary>Generate action items</summary>
|
|
369
|
-
|
|
370
|
-
```js
|
|
371
|
-
const { response } = await client.lemur.actionItems({
|
|
372
|
-
transcript_ids: ["0d295578-8c75-421a-885a-2c487f188927"],
|
|
373
|
-
});
|
|
359
|
+
await transcriber.close();
|
|
374
360
|
```
|
|
375
361
|
|
|
376
|
-
</details>
|
|
377
|
-
<details>
|
|
378
|
-
<summary>Delete LeMUR request</summary>
|
|
379
|
-
|
|
380
|
-
```js
|
|
381
|
-
const response = await client.lemur.purgeRequestData(lemurResponse.request_id);
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
</details>
|
|
385
|
-
|
|
386
362
|
## Contributing
|
|
387
363
|
|
|
388
364
|
If you want to contribute to the JavaScript SDK, follow the guidelines in [CONTRIBUTING.md](./CONTRIBUTING.md).
|
package/dist/assemblyai.umd.js
CHANGED
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
defaultUserAgentString += navigator.userAgent;
|
|
66
66
|
}
|
|
67
67
|
const defaultUserAgent = {
|
|
68
|
-
sdk: { name: "JavaScript", version: "4.
|
|
68
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
69
69
|
};
|
|
70
70
|
if (typeof process !== "undefined") {
|
|
71
71
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).assemblyai={})}(this,(function(e){"use strict";function t(e,t,s,i){return new(s||(s=Promise))((function(r,n){function o(e){try{l(i.next(e))}catch(e){n(e)}}function a(e){try{l(i.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const s={cache:"no-store"};let i="";"undefined"!=typeof navigator&&navigator.userAgent&&(i+=navigator.userAgent);const r={sdk:{name:"JavaScript",version:"4.26.1"}};"undefined"!=typeof process&&(process.versions.node&&-1===i.indexOf("Node")&&(r.runtime_env={name:"Node",version:process.versions.node}),process.versions.bun&&-1===i.indexOf("Bun")&&(r.runtime_env={name:"Bun",version:process.versions.bun})),"undefined"!=typeof Deno&&process.versions.bun&&-1===i.indexOf("Deno")&&(r.runtime_env={name:"Deno",version:Deno.version.deno});class n{constructor(e){var t;this.params=e,!1===e.userAgent?this.userAgent=void 0:this.userAgent=(t=e.userAgent||{},i+(!1===t?"":" AssemblyAI/1.0 ("+Object.entries(Object.assign(Object.assign({},r),t)).map((([e,t])=>t?`${e}=${t.name}/${t.version}`:"")).join(" ")+")"))}fetch(e,i){return t(this,void 0,void 0,(function*(){i=Object.assign(Object.assign({},s),i);let t={Authorization:this.params.apiKey,"Content-Type":"application/json"};(null==s?void 0:s.headers)&&(t=Object.assign(Object.assign({},t),s.headers)),(null==i?void 0:i.headers)&&(t=Object.assign(Object.assign({},t),i.headers)),this.userAgent&&(t["User-Agent"]=this.userAgent,"undefined"!=typeof window&&"chrome"in window&&(t["AssemblyAI-Agent"]=this.userAgent)),i.headers=t,e.startsWith("http")||(e=this.params.baseUrl+e);const r=yield fetch(e,i);if(r.status>=400){let e;const t=yield r.text();if(t){try{e=JSON.parse(t)}catch(e){}if(null==e?void 0:e.error)throw new Error(e.error);throw new Error(t)}throw new Error(`HTTP Error: ${r.status} ${r.statusText}`)}return r}))}fetchJson(e,s){return t(this,void 0,void 0,(function*(){return(yield this.fetch(e,s)).json()}))}}class o extends n{summary(e,t){return this.fetchJson("/lemur/v3/generate/summary",{method:"POST",body:JSON.stringify(e),signal:t})}questionAnswer(e,t){return this.fetchJson("/lemur/v3/generate/question-answer",{method:"POST",body:JSON.stringify(e),signal:t})}actionItems(e,t){return this.fetchJson("/lemur/v3/generate/action-items",{method:"POST",body:JSON.stringify(e),signal:t})}task(e,t){return this.fetchJson("/lemur/v3/generate/task",{method:"POST",body:JSON.stringify(e),signal:t})}getResponse(e,t){return this.fetchJson(`/lemur/v3/${e}`,{signal:t})}purgeRequestData(e,t){return this.fetchJson(`/lemur/v3/${e}`,{method:"DELETE",signal:t})}}const{WritableStream:a}="undefined"!=typeof window?window:"undefined"!=typeof global?global:globalThis;var l,c;const d=null!==(c=null!==(l=null!==WebSocket&&void 0!==WebSocket?WebSocket:null===global||void 0===global?void 0:global.WebSocket)&&void 0!==l?l:null===window||void 0===window?void 0:window.WebSocket)&&void 0!==c?c:null===self||void 0===self?void 0:self.WebSocket,h=(e,t)=>t?new d(e,t):new d(e),u={[4e3]:"Sample rate must be a positive integer",[4001]:"Not Authorized",[4002]:"Insufficient funds",[4003]:"This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",[4004]:"Session ID does not exist",[4008]:"Session has expired",[4010]:"Session is closed",[4029]:"Rate limited",[4030]:"Unique session violation",[4031]:"Session Timeout",[4032]:"Audio too short",[4033]:"Audio too long",[4034]:"Audio too small to transcode",[4100]:"Bad JSON",[4101]:"Bad schema",[4102]:"Too many streams",[4103]:"This session has been reconnected. This WebSocket is no longer valid.",[1013]:"Reconnect attempts exhausted",[4104]:"Could not parse word boost parameter"};class p extends Error{}const m={[4e3]:"Sample rate must be a positive integer",[4001]:"Not Authorized",[4002]:"Insufficient funds",[4003]:"This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",[4004]:"Session ID does not exist",[4008]:"Session has expired",[4010]:"Session is closed",[4029]:"Rate limited",[4030]:"Unique session violation",[4031]:"Session Timeout",[4032]:"Audio too short",[4033]:"Audio too long",[4034]:"Audio too small to transcode",[4101]:"Bad schema",[4102]:"Too many streams",[4103]:"This session has been reconnected. This WebSocket is no longer valid."};class f extends Error{}const v='{"terminate_session":true}';class y{constructor(e){var t,s;if(this.listeners={},this.realtimeUrl=null!==(t=e.realtimeUrl)&&void 0!==t?t:"wss://api.assemblyai.com/v2/realtime/ws",this.sampleRate=null!==(s=e.sampleRate)&&void 0!==s?s:16e3,this.wordBoost=e.wordBoost,this.encoding=e.encoding,this.endUtteranceSilenceThreshold=e.endUtteranceSilenceThreshold,this.disablePartialTranscripts=e.disablePartialTranscripts,"token"in e&&e.token&&(this.token=e.token),"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),!this.token&&!this.apiKey)throw new Error("API key or temporary token is required.")}connectionUrl(){const e=new URL(this.realtimeUrl);if("wss:"!==e.protocol)throw new Error("Invalid protocol, must be wss");const t=new URLSearchParams;return this.token&&t.set("token",this.token),t.set("sample_rate",this.sampleRate.toString()),this.wordBoost&&this.wordBoost.length>0&&t.set("word_boost",JSON.stringify(this.wordBoost)),this.encoding&&t.set("encoding",this.encoding),t.set("enable_extra_session_information","true"),this.disablePartialTranscripts&&t.set("disable_partial_transcripts",this.disablePartialTranscripts.toString()),e.search=t.toString(),e}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=h(t.toString()):(console.warn("API key authentication is not supported for the RealtimeTranscriber in browser environment. Use temporary token authentication instead.\nLearn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility."),this.socket=h(t.toString(),{headers:{Authorization:this.apiKey}})),this.socket.binaryType="arraybuffer",this.socket.onopen=()=>{void 0!==this.endUtteranceSilenceThreshold&&null!==this.endUtteranceSilenceThreshold&&this.configureEndUtteranceSilenceThreshold(this.endUtteranceSilenceThreshold)},this.socket.onclose=({code:e,reason:t})=>{var s,i;t||e in u&&(t=u[e]),null===(i=(s=this.listeners).close)||void 0===i||i.call(s,e,t)},this.socket.onerror=e=>{var t,s,i,r;e.error?null===(s=(t=this.listeners).error)||void 0===s||s.call(t,e.error):null===(r=(i=this.listeners).error)||void 0===r||r.call(i,new Error(e.message))},this.socket.onmessage=({data:t})=>{var s,i,r,n,o,a,l,c,d,h,u,m,f,v,y;const g=JSON.parse(t.toString());if("error"in g)null===(i=(s=this.listeners).error)||void 0===i||i.call(s,new p(g.error));else switch(g.message_type){case"SessionBegins":{const t={sessionId:g.session_id,expiresAt:new Date(g.expires_at)};e(t),null===(n=(r=this.listeners).open)||void 0===n||n.call(r,t);break}case"PartialTranscript":g.created=new Date(g.created),null===(a=(o=this.listeners).transcript)||void 0===a||a.call(o,g),null===(c=(l=this.listeners)["transcript.partial"])||void 0===c||c.call(l,g);break;case"FinalTranscript":g.created=new Date(g.created),null===(h=(d=this.listeners).transcript)||void 0===h||h.call(d,g),null===(m=(u=this.listeners)["transcript.final"])||void 0===m||m.call(u,g);break;case"SessionInformation":null===(v=(f=this.listeners).session_information)||void 0===v||v.call(f,g);break;case"SessionTerminated":null===(y=this.sessionTerminatedResolve)||void 0===y||y.call(this)}}}))}sendAudio(e){this.send(e)}stream(){return new a({write:e=>{this.sendAudio(e)}})}forceEndUtterance(){this.send('{"force_end_utterance":true}')}configureEndUtteranceSilenceThreshold(e){this.send(`{"end_utterance_silence_threshold":${e}}`)}send(e){if(!this.socket||this.socket.readyState!==this.socket.OPEN)throw new Error("Socket is not open for communication");this.socket.send(e)}close(){return t(this,arguments,void 0,(function*(e=!0){var t;if(this.socket){if(this.socket.readyState===this.socket.OPEN)if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(v),yield e}else this.socket.send(v);(null===(t=this.socket)||void 0===t?void 0:t.removeAllListeners)&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class g extends n{constructor(e){super(e),this.rtFactoryParams=e}createService(e){return this.transcriber(e)}transcriber(e){const t=Object.assign({},e);return t.token||t.apiKey||(t.apiKey=this.rtFactoryParams.apiKey),new y(t)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){return(yield this.fetchJson("/v2/realtime/token",{method:"POST",body:JSON.stringify(e)})).token}))}}function b(e){return e.startsWith("http")||e.startsWith("https")||e.startsWith("data:")?null:e.startsWith("file://")?e.substring(7):e.startsWith("file:")?e.substring(5):e}class w extends n{constructor(e,t){super(e),this.files=t}transcribe(e,s){return t(this,void 0,void 0,(function*(){const t=yield this.submit(e);return yield this.waitUntilReady(t.id,s)}))}submit(e){return t(this,void 0,void 0,(function*(){let t,s;if("audio"in e){const{audio:i}=e,r=function(e,t){var s={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(s[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(e);r<i.length;r++)t.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(e,i[r])&&(s[i[r]]=e[i[r]])}return s}(e,["audio"]);if("string"==typeof i){const e=b(i);t=null!==e?yield this.files.upload(e):i.startsWith("data:")?yield this.files.upload(i):i}else t=yield this.files.upload(i);s=Object.assign(Object.assign({},r),{audio_url:t})}else s=e;return yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(s)})}))}create(e,s){return t(this,void 0,void 0,(function*(){var t;const i=b(e.audio_url);if(null!==i){const t=yield this.files.upload(i);e.audio_url=t}const r=yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(e)});return null===(t=null==s?void 0:s.poll)||void 0===t||t?yield this.waitUntilReady(r.id,s):r}))}waitUntilReady(e,s){return t(this,void 0,void 0,(function*(){var t,i;const r=null!==(t=null==s?void 0:s.pollingInterval)&&void 0!==t?t:3e3,n=null!==(i=null==s?void 0:s.pollingTimeout)&&void 0!==i?i:-1,o=Date.now();for(;;){const t=yield this.get(e);if("completed"===t.status||"error"===t.status)return t;if(n>0&&Date.now()-o>n)throw new Error("Polling timeout");yield new Promise((e=>setTimeout(e,r)))}}))}get(e){return this.fetchJson(`/v2/transcript/${e}`)}list(e){return t(this,void 0,void 0,(function*(){let t="/v2/transcript";"string"==typeof e?t=e:e&&(t=`${t}?${new URLSearchParams(Object.keys(e).map((t=>{var s;return[t,(null===(s=e[t])||void 0===s?void 0:s.toString())||""]})))}`);const s=yield this.fetchJson(t);for(const e of s.transcripts)e.created=new Date(e.created),e.completed&&(e.completed=new Date(e.completed));return s}))}delete(e){return this.fetchJson(`/v2/transcript/${e}`,{method:"DELETE"})}wordSearch(e,t){const s=new URLSearchParams({words:t.join(",")});return this.fetchJson(`/v2/transcript/${e}/word-search?${s.toString()}`)}sentences(e){return this.fetchJson(`/v2/transcript/${e}/sentences`)}paragraphs(e){return this.fetchJson(`/v2/transcript/${e}/paragraphs`)}subtitles(e){return t(this,arguments,void 0,(function*(e,t="srt",s){let i=`/v2/transcript/${e}/${t}`;if(s){const e=new URLSearchParams;e.set("chars_per_caption",s.toString()),i+=`?${e.toString()}`}const r=yield this.fetch(i);return yield r.text()}))}redactions(e){return this.redactedAudio(e)}redactedAudio(e){return this.fetchJson(`/v2/transcript/${e}/redacted-audio`)}redactedAudioFile(e){return t(this,void 0,void 0,(function*(){const{redacted_audio_url:t,status:s}=yield this.redactedAudio(e);if("redacted_audio_ready"!==s)throw new Error(`Redacted audio status is ${s}`);const i=yield fetch(t);if(!i.ok)throw new Error(`Failed to fetch redacted audio: ${i.statusText}`);return{arrayBuffer:i.arrayBuffer.bind(i),blob:i.blob.bind(i),body:i.body,bodyUsed:i.bodyUsed}}))}}class S extends n{upload(e){return t(this,void 0,void 0,(function*(){let s;s="string"==typeof e?e.startsWith("data:")?function(e){const t=e.split(","),s=t[0].match(/:(.*?);/)[1],i=atob(t[1]);let r=i.length;const n=new Uint8Array(r);for(;r--;)n[r]=i.charCodeAt(r);return new Blob([n],{type:s})}(e):yield function(e){return t(this,void 0,void 0,(function*(){throw new Error("Interacting with the file system is not supported in this environment.")}))}():e;return(yield this.fetchJson("/v2/upload",{method:"POST",body:s,headers:{"Content-Type":"application/octet-stream"},duplex:"half"})).upload_url}))}}const k='{"type":"Terminate"}';class T{constructor(e){if(this.listeners={},this.params=Object.assign(Object.assign({},e),{websocketBaseUrl:e.websocketBaseUrl||"wss://streaming.assemblyai.com/v3/ws"}),"token"in e&&e.token&&(this.token=e.token),"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),!this.token&&!this.apiKey)throw new Error("API key or temporary token is required.")}connectionUrl(){var e;const t=new URL(null!==(e=this.params.websocketBaseUrl)&&void 0!==e?e:"");if("wss:"!==t.protocol)throw new Error("Invalid protocol, must be wss");const s=new URLSearchParams;return this.token&&s.set("token",this.token),s.set("sample_rate",this.params.sampleRate.toString()),this.params.endOfTurnConfidenceThreshold&&s.set("end_of_turn_confidence_threshold",this.params.endOfTurnConfidenceThreshold.toString()),this.params.minTurnSilence?s.set("min_turn_silence",this.params.minTurnSilence.toString()):this.params.minEndOfTurnSilenceWhenConfident&&(console.warn("[Deprecation Warning] `minEndOfTurnSilenceWhenConfident` is deprecated and will be removed in a future release. Please use `minTurnSilence` instead."),s.set("min_end_of_turn_silence_when_confident",this.params.minEndOfTurnSilenceWhenConfident.toString())),this.params.maxTurnSilence&&s.set("max_turn_silence",this.params.maxTurnSilence.toString()),void 0!==this.params.vadThreshold&&s.set("vad_threshold",this.params.vadThreshold.toString()),this.params.formatTurns&&s.set("format_turns",this.params.formatTurns.toString()),this.params.encoding&&s.set("encoding",this.params.encoding.toString()),this.params.keytermsPrompt?s.set("keyterms_prompt",JSON.stringify(this.params.keytermsPrompt)):this.params.keyterms&&(console.warn("[Deprecation Warning] `keyterms` is deprecated and will be removed in a future release. Please use `keytermsPrompt` instead."),s.set("keyterms_prompt",JSON.stringify(this.params.keyterms))),this.params.prompt&&s.set("prompt",this.params.prompt),this.params.filterProfanity&&s.set("filter_profanity",this.params.filterProfanity.toString()),"u3-pro"===this.params.speechModel&&console.warn("[Deprecation Warning] The speech model `u3-pro` is deprecated and will be removed in a future release. Please use `u3-rt-pro` instead."),s.set("speech_model",this.params.speechModel.toString()),void 0!==this.params.languageDetection&&s.set("language_detection",this.params.languageDetection.toString()),void 0!==this.params.inactivityTimeout&&s.set("inactivity_timeout",this.params.inactivityTimeout.toString()),void 0!==this.params.speakerLabels&&s.set("speaker_labels",this.params.speakerLabels.toString()),void 0!==this.params.maxSpeakers&&s.set("max_speakers",this.params.maxSpeakers.toString()),void 0!==this.params.llmGateway&&s.set("llm_gateway",JSON.stringify(this.params.llmGateway)),t.search=s.toString(),t}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=h(t.toString()):(console.warn("API key authentication is not supported for the StreamingTranscriber in browser environment. Use temporary token authentication instead.\nLearn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility."),this.socket=h(t.toString(),{headers:{Authorization:this.apiKey}})),this.socket.binaryType="arraybuffer",this.socket.onopen=()=>{},this.socket.onclose=({code:e,reason:t})=>{var s,i;t||e in m&&(t=m[e]),null===(i=(s=this.listeners).close)||void 0===i||i.call(s,e,t)},this.socket.onerror=e=>{var t,s,i,r;e.error?null===(s=(t=this.listeners).error)||void 0===s||s.call(t,e.error):null===(r=(i=this.listeners).error)||void 0===r||r.call(i,new Error(e.message))},this.socket.onmessage=({data:t})=>{var s,i,r,n,o,a,l,c,d,h,u;const p=JSON.parse(t.toString());if("error"in p)null===(i=(s=this.listeners).error)||void 0===i||i.call(s,new f(p.error));else switch(p.type){case"Begin":e(p),null===(n=(r=this.listeners).open)||void 0===n||n.call(r,p);break;case"Turn":null===(a=(o=this.listeners).turn)||void 0===a||a.call(o,p);break;case"SpeechStarted":null===(c=(l=this.listeners).speechStarted)||void 0===c||c.call(l,p);break;case"LLMGatewayResponse":null===(h=(d=this.listeners).llmGatewayResponse)||void 0===h||h.call(d,p);break;case"Termination":null===(u=this.sessionTerminatedResolve)||void 0===u||u.call(this)}}}))}stream(){return new a({write:e=>{this.sendAudio(e)}})}sendAudio(e){this.send(e)}updateConfiguration(e){const t=Object.assign({type:"UpdateConfiguration"},e);this.send(JSON.stringify(t))}forceEndpoint(){this.send(JSON.stringify({type:"ForceEndpoint"}))}send(e){if(!this.socket||this.socket.readyState!==this.socket.OPEN)throw new Error("Socket is not open for communication");this.socket.send(e)}close(){return t(this,arguments,void 0,(function*(e=!0){var t;if(this.socket){if(this.socket.readyState===this.socket.OPEN)if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(k),yield e}else this.socket.send(k);(null===(t=this.socket)||void 0===t?void 0:t.removeAllListeners)&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class O extends n{constructor(e){super(e),this.baseServiceParams=e}transcriber(e){const t=Object.assign({},e);return t.token||t.apiKey||(t.apiKey=this.baseServiceParams.apiKey),new T(t)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){const t=new URLSearchParams;Object.entries(e).forEach((([e,s])=>{null!=s&&t.append(e,String(s))}));const s=t.toString(),i=s?`/v3/token?${s}`:"/v3/token";return(yield this.fetchJson(i,{method:"GET"})).token}))}}e.AssemblyAI=class{constructor(e){e.baseUrl=e.baseUrl||"https://api.assemblyai.com",e.baseUrl&&e.baseUrl.endsWith("/")&&(e.baseUrl=e.baseUrl.slice(0,-1)),this.files=new S(e),this.transcripts=new w(e,this.files),this.lemur=new o(e),this.realtime=new g(e),this.streaming=new O(Object.assign(Object.assign({},e),{baseUrl:e.streamingBaseUrl||"https://streaming.assemblyai.com"}))}},e.FileService=S,e.LemurService=o,e.RealtimeService=class extends y{},e.RealtimeServiceFactory=class extends g{},e.RealtimeTranscriber=y,e.RealtimeTranscriberFactory=g,e.StreamingTranscriber=T,e.TranscriptService=w}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).assemblyai={})}(this,(function(e){"use strict";function t(e,t,s,i){return new(s||(s=Promise))((function(r,n){function o(e){try{l(i.next(e))}catch(e){n(e)}}function a(e){try{l(i.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const s={cache:"no-store"};let i="";"undefined"!=typeof navigator&&navigator.userAgent&&(i+=navigator.userAgent);const r={sdk:{name:"JavaScript",version:"4.28.0"}};"undefined"!=typeof process&&(process.versions.node&&-1===i.indexOf("Node")&&(r.runtime_env={name:"Node",version:process.versions.node}),process.versions.bun&&-1===i.indexOf("Bun")&&(r.runtime_env={name:"Bun",version:process.versions.bun})),"undefined"!=typeof Deno&&process.versions.bun&&-1===i.indexOf("Deno")&&(r.runtime_env={name:"Deno",version:Deno.version.deno});class n{constructor(e){var t;this.params=e,!1===e.userAgent?this.userAgent=void 0:this.userAgent=(t=e.userAgent||{},i+(!1===t?"":" AssemblyAI/1.0 ("+Object.entries(Object.assign(Object.assign({},r),t)).map((([e,t])=>t?`${e}=${t.name}/${t.version}`:"")).join(" ")+")"))}fetch(e,i){return t(this,void 0,void 0,(function*(){i=Object.assign(Object.assign({},s),i);let t={Authorization:this.params.apiKey,"Content-Type":"application/json"};(null==s?void 0:s.headers)&&(t=Object.assign(Object.assign({},t),s.headers)),(null==i?void 0:i.headers)&&(t=Object.assign(Object.assign({},t),i.headers)),this.userAgent&&(t["User-Agent"]=this.userAgent,"undefined"!=typeof window&&"chrome"in window&&(t["AssemblyAI-Agent"]=this.userAgent)),i.headers=t,e.startsWith("http")||(e=this.params.baseUrl+e);const r=yield fetch(e,i);if(r.status>=400){let e;const t=yield r.text();if(t){try{e=JSON.parse(t)}catch(e){}if(null==e?void 0:e.error)throw new Error(e.error);throw new Error(t)}throw new Error(`HTTP Error: ${r.status} ${r.statusText}`)}return r}))}fetchJson(e,s){return t(this,void 0,void 0,(function*(){return(yield this.fetch(e,s)).json()}))}}class o extends n{summary(e,t){return this.fetchJson("/lemur/v3/generate/summary",{method:"POST",body:JSON.stringify(e),signal:t})}questionAnswer(e,t){return this.fetchJson("/lemur/v3/generate/question-answer",{method:"POST",body:JSON.stringify(e),signal:t})}actionItems(e,t){return this.fetchJson("/lemur/v3/generate/action-items",{method:"POST",body:JSON.stringify(e),signal:t})}task(e,t){return this.fetchJson("/lemur/v3/generate/task",{method:"POST",body:JSON.stringify(e),signal:t})}getResponse(e,t){return this.fetchJson(`/lemur/v3/${e}`,{signal:t})}purgeRequestData(e,t){return this.fetchJson(`/lemur/v3/${e}`,{method:"DELETE",signal:t})}}const{WritableStream:a}="undefined"!=typeof window?window:"undefined"!=typeof global?global:globalThis;var l,c;const d=null!==(c=null!==(l=null!==WebSocket&&void 0!==WebSocket?WebSocket:null===global||void 0===global?void 0:global.WebSocket)&&void 0!==l?l:null===window||void 0===window?void 0:window.WebSocket)&&void 0!==c?c:null===self||void 0===self?void 0:self.WebSocket,h=(e,t)=>t?new d(e,t):new d(e),u={[4e3]:"Sample rate must be a positive integer",[4001]:"Not Authorized",[4002]:"Insufficient funds",[4003]:"This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",[4004]:"Session ID does not exist",[4008]:"Session has expired",[4010]:"Session is closed",[4029]:"Rate limited",[4030]:"Unique session violation",[4031]:"Session Timeout",[4032]:"Audio too short",[4033]:"Audio too long",[4034]:"Audio too small to transcode",[4100]:"Bad JSON",[4101]:"Bad schema",[4102]:"Too many streams",[4103]:"This session has been reconnected. This WebSocket is no longer valid.",[1013]:"Reconnect attempts exhausted",[4104]:"Could not parse word boost parameter"};class p extends Error{}const m={[4e3]:"Sample rate must be a positive integer",[4001]:"Not Authorized",[4002]:"Insufficient funds",[4003]:"This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",[4004]:"Session ID does not exist",[4008]:"Session has expired",[4010]:"Session is closed",[4029]:"Rate limited",[4030]:"Unique session violation",[4031]:"Session Timeout",[4032]:"Audio too short",[4033]:"Audio too long",[4034]:"Audio too small to transcode",[4101]:"Bad schema",[4102]:"Too many streams",[4103]:"This session has been reconnected. This WebSocket is no longer valid."};class f extends Error{}const v='{"terminate_session":true}';class y{constructor(e){var t,s;if(this.listeners={},this.realtimeUrl=null!==(t=e.realtimeUrl)&&void 0!==t?t:"wss://api.assemblyai.com/v2/realtime/ws",this.sampleRate=null!==(s=e.sampleRate)&&void 0!==s?s:16e3,this.wordBoost=e.wordBoost,this.encoding=e.encoding,this.endUtteranceSilenceThreshold=e.endUtteranceSilenceThreshold,this.disablePartialTranscripts=e.disablePartialTranscripts,"token"in e&&e.token&&(this.token=e.token),"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),!this.token&&!this.apiKey)throw new Error("API key or temporary token is required.")}connectionUrl(){const e=new URL(this.realtimeUrl);if("wss:"!==e.protocol)throw new Error("Invalid protocol, must be wss");const t=new URLSearchParams;return this.token&&t.set("token",this.token),t.set("sample_rate",this.sampleRate.toString()),this.wordBoost&&this.wordBoost.length>0&&t.set("word_boost",JSON.stringify(this.wordBoost)),this.encoding&&t.set("encoding",this.encoding),t.set("enable_extra_session_information","true"),this.disablePartialTranscripts&&t.set("disable_partial_transcripts",this.disablePartialTranscripts.toString()),e.search=t.toString(),e}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=h(t.toString()):(console.warn("API key authentication is not supported for the RealtimeTranscriber in browser environment. Use temporary token authentication instead.\nLearn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility."),this.socket=h(t.toString(),{headers:{Authorization:this.apiKey}})),this.socket.binaryType="arraybuffer",this.socket.onopen=()=>{void 0!==this.endUtteranceSilenceThreshold&&null!==this.endUtteranceSilenceThreshold&&this.configureEndUtteranceSilenceThreshold(this.endUtteranceSilenceThreshold)},this.socket.onclose=({code:e,reason:t})=>{var s,i;t||e in u&&(t=u[e]),null===(i=(s=this.listeners).close)||void 0===i||i.call(s,e,t)},this.socket.onerror=e=>{var t,s,i,r;e.error?null===(s=(t=this.listeners).error)||void 0===s||s.call(t,e.error):null===(r=(i=this.listeners).error)||void 0===r||r.call(i,new Error(e.message))},this.socket.onmessage=({data:t})=>{var s,i,r,n,o,a,l,c,d,h,u,m,f,v,y;const g=JSON.parse(t.toString());if("error"in g)null===(i=(s=this.listeners).error)||void 0===i||i.call(s,new p(g.error));else switch(g.message_type){case"SessionBegins":{const t={sessionId:g.session_id,expiresAt:new Date(g.expires_at)};e(t),null===(n=(r=this.listeners).open)||void 0===n||n.call(r,t);break}case"PartialTranscript":g.created=new Date(g.created),null===(a=(o=this.listeners).transcript)||void 0===a||a.call(o,g),null===(c=(l=this.listeners)["transcript.partial"])||void 0===c||c.call(l,g);break;case"FinalTranscript":g.created=new Date(g.created),null===(h=(d=this.listeners).transcript)||void 0===h||h.call(d,g),null===(m=(u=this.listeners)["transcript.final"])||void 0===m||m.call(u,g);break;case"SessionInformation":null===(v=(f=this.listeners).session_information)||void 0===v||v.call(f,g);break;case"SessionTerminated":null===(y=this.sessionTerminatedResolve)||void 0===y||y.call(this)}}}))}sendAudio(e){this.send(e)}stream(){return new a({write:e=>{this.sendAudio(e)}})}forceEndUtterance(){this.send('{"force_end_utterance":true}')}configureEndUtteranceSilenceThreshold(e){this.send(`{"end_utterance_silence_threshold":${e}}`)}send(e){if(!this.socket||this.socket.readyState!==this.socket.OPEN)throw new Error("Socket is not open for communication");this.socket.send(e)}close(){return t(this,arguments,void 0,(function*(e=!0){var t;if(this.socket){if(this.socket.readyState===this.socket.OPEN)if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(v),yield e}else this.socket.send(v);(null===(t=this.socket)||void 0===t?void 0:t.removeAllListeners)&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class g extends n{constructor(e){super(e),this.rtFactoryParams=e}createService(e){return this.transcriber(e)}transcriber(e){const t=Object.assign({},e);return t.token||t.apiKey||(t.apiKey=this.rtFactoryParams.apiKey),new y(t)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){return(yield this.fetchJson("/v2/realtime/token",{method:"POST",body:JSON.stringify(e)})).token}))}}function b(e){return e.startsWith("http")||e.startsWith("https")||e.startsWith("data:")?null:e.startsWith("file://")?e.substring(7):e.startsWith("file:")?e.substring(5):e}class w extends n{constructor(e,t){super(e),this.files=t}transcribe(e,s){return t(this,void 0,void 0,(function*(){const t=yield this.submit(e);return yield this.waitUntilReady(t.id,s)}))}submit(e){return t(this,void 0,void 0,(function*(){let t,s;if("audio"in e){const{audio:i}=e,r=function(e,t){var s={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(s[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(e);r<i.length;r++)t.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(e,i[r])&&(s[i[r]]=e[i[r]])}return s}(e,["audio"]);if("string"==typeof i){const e=b(i);t=null!==e?yield this.files.upload(e):i.startsWith("data:")?yield this.files.upload(i):i}else t=yield this.files.upload(i);s=Object.assign(Object.assign({},r),{audio_url:t})}else s=e;return yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(s)})}))}create(e,s){return t(this,void 0,void 0,(function*(){var t;const i=b(e.audio_url);if(null!==i){const t=yield this.files.upload(i);e.audio_url=t}const r=yield this.fetchJson("/v2/transcript",{method:"POST",body:JSON.stringify(e)});return null===(t=null==s?void 0:s.poll)||void 0===t||t?yield this.waitUntilReady(r.id,s):r}))}waitUntilReady(e,s){return t(this,void 0,void 0,(function*(){var t,i;const r=null!==(t=null==s?void 0:s.pollingInterval)&&void 0!==t?t:3e3,n=null!==(i=null==s?void 0:s.pollingTimeout)&&void 0!==i?i:-1,o=Date.now();for(;;){const t=yield this.get(e);if("completed"===t.status||"error"===t.status)return t;if(n>0&&Date.now()-o>n)throw new Error("Polling timeout");yield new Promise((e=>setTimeout(e,r)))}}))}get(e){return this.fetchJson(`/v2/transcript/${e}`)}list(e){return t(this,void 0,void 0,(function*(){let t="/v2/transcript";"string"==typeof e?t=e:e&&(t=`${t}?${new URLSearchParams(Object.keys(e).map((t=>{var s;return[t,(null===(s=e[t])||void 0===s?void 0:s.toString())||""]})))}`);const s=yield this.fetchJson(t);for(const e of s.transcripts)e.created=new Date(e.created),e.completed&&(e.completed=new Date(e.completed));return s}))}delete(e){return this.fetchJson(`/v2/transcript/${e}`,{method:"DELETE"})}wordSearch(e,t){const s=new URLSearchParams({words:t.join(",")});return this.fetchJson(`/v2/transcript/${e}/word-search?${s.toString()}`)}sentences(e){return this.fetchJson(`/v2/transcript/${e}/sentences`)}paragraphs(e){return this.fetchJson(`/v2/transcript/${e}/paragraphs`)}subtitles(e){return t(this,arguments,void 0,(function*(e,t="srt",s){let i=`/v2/transcript/${e}/${t}`;if(s){const e=new URLSearchParams;e.set("chars_per_caption",s.toString()),i+=`?${e.toString()}`}const r=yield this.fetch(i);return yield r.text()}))}redactions(e){return this.redactedAudio(e)}redactedAudio(e){return this.fetchJson(`/v2/transcript/${e}/redacted-audio`)}redactedAudioFile(e){return t(this,void 0,void 0,(function*(){const{redacted_audio_url:t,status:s}=yield this.redactedAudio(e);if("redacted_audio_ready"!==s)throw new Error(`Redacted audio status is ${s}`);const i=yield fetch(t);if(!i.ok)throw new Error(`Failed to fetch redacted audio: ${i.statusText}`);return{arrayBuffer:i.arrayBuffer.bind(i),blob:i.blob.bind(i),body:i.body,bodyUsed:i.bodyUsed}}))}}class S extends n{upload(e){return t(this,void 0,void 0,(function*(){let s;s="string"==typeof e?e.startsWith("data:")?function(e){const t=e.split(","),s=t[0].match(/:(.*?);/)[1],i=atob(t[1]);let r=i.length;const n=new Uint8Array(r);for(;r--;)n[r]=i.charCodeAt(r);return new Blob([n],{type:s})}(e):yield function(e){return t(this,void 0,void 0,(function*(){throw new Error("Interacting with the file system is not supported in this environment.")}))}():e;return(yield this.fetchJson("/v2/upload",{method:"POST",body:s,headers:{"Content-Type":"application/octet-stream"},duplex:"half"})).upload_url}))}}const k='{"type":"Terminate"}';class T{constructor(e){if(this.listeners={},this.params=Object.assign(Object.assign({},e),{websocketBaseUrl:e.websocketBaseUrl||"wss://streaming.assemblyai.com/v3/ws"}),"token"in e&&e.token&&(this.token=e.token),"apiKey"in e&&e.apiKey&&(this.apiKey=e.apiKey),!this.token&&!this.apiKey)throw new Error("API key or temporary token is required.")}connectionUrl(){var e;const t=new URL(null!==(e=this.params.websocketBaseUrl)&&void 0!==e?e:"");if("wss:"!==t.protocol)throw new Error("Invalid protocol, must be wss");const s=new URLSearchParams;return this.token&&s.set("token",this.token),s.set("sample_rate",this.params.sampleRate.toString()),this.params.endOfTurnConfidenceThreshold&&s.set("end_of_turn_confidence_threshold",this.params.endOfTurnConfidenceThreshold.toString()),this.params.minTurnSilence?s.set("min_turn_silence",this.params.minTurnSilence.toString()):this.params.minEndOfTurnSilenceWhenConfident&&(console.warn("[Deprecation Warning] `minEndOfTurnSilenceWhenConfident` is deprecated and will be removed in a future release. Please use `minTurnSilence` instead."),s.set("min_end_of_turn_silence_when_confident",this.params.minEndOfTurnSilenceWhenConfident.toString())),this.params.maxTurnSilence&&s.set("max_turn_silence",this.params.maxTurnSilence.toString()),void 0!==this.params.vadThreshold&&s.set("vad_threshold",this.params.vadThreshold.toString()),this.params.formatTurns&&s.set("format_turns",this.params.formatTurns.toString()),this.params.encoding&&s.set("encoding",this.params.encoding.toString()),this.params.keytermsPrompt?s.set("keyterms_prompt",JSON.stringify(this.params.keytermsPrompt)):this.params.keyterms&&(console.warn("[Deprecation Warning] `keyterms` is deprecated and will be removed in a future release. Please use `keytermsPrompt` instead."),s.set("keyterms_prompt",JSON.stringify(this.params.keyterms))),this.params.prompt&&s.set("prompt",this.params.prompt),this.params.filterProfanity&&s.set("filter_profanity",this.params.filterProfanity.toString()),"u3-pro"===this.params.speechModel&&console.warn("[Deprecation Warning] The speech model `u3-pro` is deprecated and will be removed in a future release. Please use `u3-rt-pro` instead."),s.set("speech_model",this.params.speechModel.toString()),void 0!==this.params.languageDetection&&s.set("language_detection",this.params.languageDetection.toString()),void 0!==this.params.inactivityTimeout&&s.set("inactivity_timeout",this.params.inactivityTimeout.toString()),void 0!==this.params.speakerLabels&&s.set("speaker_labels",this.params.speakerLabels.toString()),void 0!==this.params.maxSpeakers&&s.set("max_speakers",this.params.maxSpeakers.toString()),void 0!==this.params.llmGateway&&s.set("llm_gateway",JSON.stringify(this.params.llmGateway)),t.search=s.toString(),t}on(e,t){this.listeners[e]=t}connect(){return new Promise((e=>{if(this.socket)throw new Error("Already connected");const t=this.connectionUrl();this.token?this.socket=h(t.toString()):(console.warn("API key authentication is not supported for the StreamingTranscriber in browser environment. Use temporary token authentication instead.\nLearn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility."),this.socket=h(t.toString(),{headers:{Authorization:this.apiKey}})),this.socket.binaryType="arraybuffer",this.socket.onopen=()=>{},this.socket.onclose=({code:e,reason:t})=>{var s,i;t||e in m&&(t=m[e]),null===(i=(s=this.listeners).close)||void 0===i||i.call(s,e,t)},this.socket.onerror=e=>{var t,s,i,r;e.error?null===(s=(t=this.listeners).error)||void 0===s||s.call(t,e.error):null===(r=(i=this.listeners).error)||void 0===r||r.call(i,new Error(e.message))},this.socket.onmessage=({data:t})=>{var s,i,r,n,o,a,l,c,d,h,u;const p=JSON.parse(t.toString());if("error"in p)null===(i=(s=this.listeners).error)||void 0===i||i.call(s,new f(p.error));else switch(p.type){case"Begin":e(p),null===(n=(r=this.listeners).open)||void 0===n||n.call(r,p);break;case"Turn":null===(a=(o=this.listeners).turn)||void 0===a||a.call(o,p);break;case"SpeechStarted":null===(c=(l=this.listeners).speechStarted)||void 0===c||c.call(l,p);break;case"LLMGatewayResponse":null===(h=(d=this.listeners).llmGatewayResponse)||void 0===h||h.call(d,p);break;case"Termination":null===(u=this.sessionTerminatedResolve)||void 0===u||u.call(this)}}}))}stream(){return new a({write:e=>{this.sendAudio(e)}})}sendAudio(e){this.send(e)}updateConfiguration(e){const t=Object.assign({type:"UpdateConfiguration"},e);this.send(JSON.stringify(t))}forceEndpoint(){this.send(JSON.stringify({type:"ForceEndpoint"}))}send(e){if(!this.socket||this.socket.readyState!==this.socket.OPEN)throw new Error("Socket is not open for communication");this.socket.send(e)}close(){return t(this,arguments,void 0,(function*(e=!0){var t;if(this.socket){if(this.socket.readyState===this.socket.OPEN)if(e){const e=new Promise((e=>{this.sessionTerminatedResolve=e}));this.socket.send(k),yield e}else this.socket.send(k);(null===(t=this.socket)||void 0===t?void 0:t.removeAllListeners)&&this.socket.removeAllListeners(),this.socket.close()}this.listeners={},this.socket=void 0}))}}class O extends n{constructor(e){super(e),this.baseServiceParams=e}transcriber(e){const t=Object.assign({},e);return t.token||t.apiKey||(t.apiKey=this.baseServiceParams.apiKey),new T(t)}createTemporaryToken(e){return t(this,void 0,void 0,(function*(){const t=new URLSearchParams;Object.entries(e).forEach((([e,s])=>{null!=s&&t.append(e,String(s))}));const s=t.toString(),i=s?`/v3/token?${s}`:"/v3/token";return(yield this.fetchJson(i,{method:"GET"})).token}))}}e.AssemblyAI=class{constructor(e){e.baseUrl=e.baseUrl||"https://api.assemblyai.com",e.baseUrl&&e.baseUrl.endsWith("/")&&(e.baseUrl=e.baseUrl.slice(0,-1)),this.files=new S(e),this.transcripts=new w(e,this.files),this.lemur=new o(e),this.realtime=new g(e),this.streaming=new O(Object.assign(Object.assign({},e),{baseUrl:e.streamingBaseUrl||"https://streaming.assemblyai.com"}))}},e.FileService=S,e.LemurService=o,e.RealtimeService=class extends y{},e.RealtimeServiceFactory=class extends g{},e.RealtimeTranscriber=y,e.RealtimeTranscriberFactory=g,e.StreamingTranscriber=T,e.TranscriptService=w}));
|
package/dist/browser.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.28.0" },
|
|
19
19
|
};
|
|
20
20
|
if (typeof process !== "undefined") {
|
|
21
21
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/bun.mjs
CHANGED
|
@@ -17,7 +17,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
17
17
|
defaultUserAgentString += navigator.userAgent;
|
|
18
18
|
}
|
|
19
19
|
const defaultUserAgent = {
|
|
20
|
-
sdk: { name: "JavaScript", version: "4.
|
|
20
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
21
21
|
};
|
|
22
22
|
if (typeof process !== "undefined") {
|
|
23
23
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/deno.mjs
CHANGED
|
@@ -17,7 +17,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
17
17
|
defaultUserAgentString += navigator.userAgent;
|
|
18
18
|
}
|
|
19
19
|
const defaultUserAgent = {
|
|
20
|
-
sdk: { name: "JavaScript", version: "4.
|
|
20
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
21
21
|
};
|
|
22
22
|
if (typeof process !== "undefined") {
|
|
23
23
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/index.cjs
CHANGED
|
@@ -63,7 +63,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
63
63
|
defaultUserAgentString += navigator.userAgent;
|
|
64
64
|
}
|
|
65
65
|
const defaultUserAgent = {
|
|
66
|
-
sdk: { name: "JavaScript", version: "4.
|
|
66
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
67
67
|
};
|
|
68
68
|
if (typeof process !== "undefined") {
|
|
69
69
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/index.mjs
CHANGED
|
@@ -61,7 +61,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
61
61
|
defaultUserAgentString += navigator.userAgent;
|
|
62
62
|
}
|
|
63
63
|
const defaultUserAgent = {
|
|
64
|
-
sdk: { name: "JavaScript", version: "4.
|
|
64
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
65
65
|
};
|
|
66
66
|
if (typeof process !== "undefined") {
|
|
67
67
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/node.cjs
CHANGED
|
@@ -22,7 +22,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
22
22
|
defaultUserAgentString += navigator.userAgent;
|
|
23
23
|
}
|
|
24
24
|
const defaultUserAgent = {
|
|
25
|
-
sdk: { name: "JavaScript", version: "4.
|
|
25
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
26
26
|
};
|
|
27
27
|
if (typeof process !== "undefined") {
|
|
28
28
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/dist/node.mjs
CHANGED
|
@@ -20,7 +20,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
20
20
|
defaultUserAgentString += navigator.userAgent;
|
|
21
21
|
}
|
|
22
22
|
const defaultUserAgent = {
|
|
23
|
-
sdk: { name: "JavaScript", version: "4.
|
|
23
|
+
sdk: { name: "JavaScript", version: "4.28.0" },
|
|
24
24
|
};
|
|
25
25
|
if (typeof process !== "undefined") {
|
|
26
26
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -9,6 +9,12 @@ type OneOf<T extends any[]> = T extends [infer Only] ? Only : T extends [infer A
|
|
|
9
9
|
* Either success, or unavailable in the rare case that the model failed
|
|
10
10
|
*/
|
|
11
11
|
export type AudioIntelligenceModelStatus = "success" | "unavailable";
|
|
12
|
+
export interface TranscriptMetadata {
|
|
13
|
+
/** The domain that was actually used for the transcription. */
|
|
14
|
+
domain_used?: string | null;
|
|
15
|
+
/** An optional warning message, if applicable. */
|
|
16
|
+
warning?: string | null;
|
|
17
|
+
}
|
|
12
18
|
/**
|
|
13
19
|
* @example
|
|
14
20
|
* ```js
|
|
@@ -2698,6 +2704,10 @@ export type Transcript = {
|
|
|
2698
2704
|
/**
|
|
2699
2705
|
* Change how deterministic the response is, with 0 being the most deterministic and 1 being the least deterministic.
|
|
2700
2706
|
*/
|
|
2707
|
+
remove_audio_tags?: string | null;
|
|
2708
|
+
/**
|
|
2709
|
+
* When set to 'all', removes all bracketed audio/speaker tags (e.g. [MUSIC], [Speaker: A]) from the transcript. Only supported for Universal-3 Pro.
|
|
2710
|
+
*/
|
|
2701
2711
|
text?: string | null;
|
|
2702
2712
|
/**
|
|
2703
2713
|
* True while a request is throttled and false when a request is no longer throttled
|
|
@@ -2747,6 +2757,14 @@ export type Transcript = {
|
|
|
2747
2757
|
* Translations of the full transcript text when translation is enabled
|
|
2748
2758
|
*/
|
|
2749
2759
|
translated_texts?: Record<string, string>;
|
|
2760
|
+
/**
|
|
2761
|
+
* The domain used for the transcription.
|
|
2762
|
+
*/
|
|
2763
|
+
domain?: string | null;
|
|
2764
|
+
/**
|
|
2765
|
+
* Metadata returned by the transcription API.
|
|
2766
|
+
*/
|
|
2767
|
+
metadata?: TranscriptMetadata | null;
|
|
2750
2768
|
};
|
|
2751
2769
|
/**
|
|
2752
2770
|
* How much to boost specified words
|
|
@@ -3131,6 +3149,10 @@ export type TranscriptOptionalParams = {
|
|
|
3131
3149
|
/**
|
|
3132
3150
|
* Change how deterministic the response is, with 0 being the most deterministic and 1 being the least deterministic.
|
|
3133
3151
|
*/
|
|
3152
|
+
remove_audio_tags?: string | null;
|
|
3153
|
+
/**
|
|
3154
|
+
* When set to 'all', removes all bracketed audio/speaker tags (e.g. [MUSIC], [Speaker: A]) from the transcript. Only supported for Universal-3 Pro.
|
|
3155
|
+
*/
|
|
3134
3156
|
topics?: string[];
|
|
3135
3157
|
/**
|
|
3136
3158
|
* The header name to be sent with the transcript completed or failed webhook requests
|
|
@@ -3156,6 +3178,10 @@ export type TranscriptOptionalParams = {
|
|
|
3156
3178
|
* Speech understanding configuration/response for LLM Gateway features
|
|
3157
3179
|
*/
|
|
3158
3180
|
speech_understanding?: SpeechUnderstandingRequest | SpeechUnderstandingResponse;
|
|
3181
|
+
/**
|
|
3182
|
+
* The domain to use for the transcription (e.g. 'medical-v1').
|
|
3183
|
+
*/
|
|
3184
|
+
domain?: string | null;
|
|
3159
3185
|
};
|
|
3160
3186
|
/**
|
|
3161
3187
|
* @example
|
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.28.0" },
|
|
19
19
|
};
|
|
20
20
|
if (typeof process !== "undefined") {
|
|
21
21
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblyai",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.28.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"
|
|
@@ -21,6 +21,13 @@ type OneOf<T extends any[]> = T extends [infer Only]
|
|
|
21
21
|
*/
|
|
22
22
|
export type AudioIntelligenceModelStatus = "success" | "unavailable";
|
|
23
23
|
|
|
24
|
+
export interface TranscriptMetadata {
|
|
25
|
+
/** The domain that was actually used for the transcription. */
|
|
26
|
+
domain_used?: string | null;
|
|
27
|
+
/** An optional warning message, if applicable. */
|
|
28
|
+
warning?: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
24
31
|
/**
|
|
25
32
|
* @example
|
|
26
33
|
* ```js
|
|
@@ -2889,6 +2896,11 @@ export type Transcript = {
|
|
|
2889
2896
|
* Change how deterministic the response is, with 0 being the most deterministic and 1 being the least deterministic.
|
|
2890
2897
|
*/
|
|
2891
2898
|
|
|
2899
|
+
remove_audio_tags?: string | null;
|
|
2900
|
+
/**
|
|
2901
|
+
* When set to 'all', removes all bracketed audio/speaker tags (e.g. [MUSIC], [Speaker: A]) from the transcript. Only supported for Universal-3 Pro.
|
|
2902
|
+
*/
|
|
2903
|
+
|
|
2892
2904
|
text?: string | null;
|
|
2893
2905
|
/**
|
|
2894
2906
|
* True while a request is throttled and false when a request is no longer throttled
|
|
@@ -2938,6 +2950,14 @@ export type Transcript = {
|
|
|
2938
2950
|
* Translations of the full transcript text when translation is enabled
|
|
2939
2951
|
*/
|
|
2940
2952
|
translated_texts?: Record<string, string>;
|
|
2953
|
+
/**
|
|
2954
|
+
* The domain used for the transcription.
|
|
2955
|
+
*/
|
|
2956
|
+
domain?: string | null;
|
|
2957
|
+
/**
|
|
2958
|
+
* Metadata returned by the transcription API.
|
|
2959
|
+
*/
|
|
2960
|
+
metadata?: TranscriptMetadata | null;
|
|
2941
2961
|
};
|
|
2942
2962
|
|
|
2943
2963
|
/**
|
|
@@ -3433,6 +3453,11 @@ export type TranscriptOptionalParams = {
|
|
|
3433
3453
|
* Change how deterministic the response is, with 0 being the most deterministic and 1 being the least deterministic.
|
|
3434
3454
|
*/
|
|
3435
3455
|
|
|
3456
|
+
remove_audio_tags?: string | null;
|
|
3457
|
+
/**
|
|
3458
|
+
* When set to 'all', removes all bracketed audio/speaker tags (e.g. [MUSIC], [Speaker: A]) from the transcript. Only supported for Universal-3 Pro.
|
|
3459
|
+
*/
|
|
3460
|
+
|
|
3436
3461
|
topics?: string[];
|
|
3437
3462
|
/**
|
|
3438
3463
|
* The header name to be sent with the transcript completed or failed webhook requests
|
|
@@ -3460,6 +3485,10 @@ export type TranscriptOptionalParams = {
|
|
|
3460
3485
|
speech_understanding?:
|
|
3461
3486
|
| SpeechUnderstandingRequest
|
|
3462
3487
|
| SpeechUnderstandingResponse;
|
|
3488
|
+
/**
|
|
3489
|
+
* The domain to use for the transcription (e.g. 'medical-v1').
|
|
3490
|
+
*/
|
|
3491
|
+
domain?: string | null;
|
|
3463
3492
|
};
|
|
3464
3493
|
|
|
3465
3494
|
/**
|