assemblyai 4.35.4 → 4.36.3
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 +20 -0
- package/README.md +98 -0
- package/dist/assemblyai.streaming.umd.js +13 -6
- package/dist/assemblyai.streaming.umd.min.js +1 -1
- package/dist/assemblyai.umd.js +473 -78
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +417 -75
- package/dist/bun.mjs +407 -64
- package/dist/deno.mjs +407 -64
- package/dist/index.cjs +466 -71
- package/dist/index.mjs +464 -72
- package/dist/node.cjs +405 -59
- package/dist/node.mjs +403 -60
- package/dist/services/base.d.ts +1 -0
- package/dist/services/index.d.ts +7 -1
- package/dist/services/sync/index.d.ts +1 -0
- package/dist/services/sync/service.d.ts +48 -0
- package/dist/streaming.browser.mjs +11 -6
- package/dist/streaming.cjs +12 -5
- package/dist/streaming.mjs +12 -5
- package/dist/types/index.d.ts +1 -0
- package/dist/types/services/index.d.ts +1 -0
- package/dist/types/sync/index.d.ts +133 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/sync.d.ts +20 -0
- package/dist/workerd.mjs +411 -68
- package/package.json +1 -1
- package/src/services/base.ts +18 -8
- package/src/services/index.ts +19 -0
- package/src/services/sync/index.ts +1 -0
- package/src/services/sync/service.ts +369 -0
- package/src/types/index.ts +1 -0
- package/src/types/services/index.ts +1 -0
- package/src/types/sync/index.ts +145 -0
- package/src/utils/errors/index.ts +2 -0
- package/src/utils/errors/sync.ts +25 -0
package/dist/browser.mjs
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default speech model for synchronous transcription.
|
|
3
|
+
*/
|
|
4
|
+
const defaultSyncSpeechModel = "universal-3-5-pro";
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* Thrown when `DualChannelCapture` is constructed in a non-browser environment
|
|
3
8
|
* (no `globalThis.AudioContext`). The helper is intentionally surfaced from the
|
|
@@ -11,6 +16,12 @@ class BrowserOnlyError extends Error {
|
|
|
11
16
|
}
|
|
12
17
|
}
|
|
13
18
|
|
|
19
|
+
const readFile = async function (
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
+
path) {
|
|
22
|
+
throw new Error("Interacting with the file system is not supported in this environment.");
|
|
23
|
+
};
|
|
24
|
+
|
|
14
25
|
const DEFAULT_FETCH_INIT = {
|
|
15
26
|
cache: "no-store",
|
|
16
27
|
};
|
|
@@ -28,7 +39,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
28
39
|
defaultUserAgentString += navigator.userAgent;
|
|
29
40
|
}
|
|
30
41
|
const defaultUserAgent = {
|
|
31
|
-
sdk: { name: "JavaScript", version: "4.
|
|
42
|
+
sdk: { name: "JavaScript", version: "4.36.3" },
|
|
32
43
|
};
|
|
33
44
|
if (typeof process !== "undefined") {
|
|
34
45
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -67,12 +78,15 @@ class BaseService {
|
|
|
67
78
|
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
68
79
|
}
|
|
69
80
|
}
|
|
70
|
-
async
|
|
81
|
+
async fetchResponse(input, init) {
|
|
71
82
|
init = { ...DEFAULT_FETCH_INIT, ...init };
|
|
72
83
|
let headers = {
|
|
73
84
|
Authorization: this.params.apiKey,
|
|
74
|
-
"Content-Type": "application/json",
|
|
75
85
|
};
|
|
86
|
+
// FormData bodies must let fetch set the multipart boundary itself.
|
|
87
|
+
if (!(init.body instanceof FormData)) {
|
|
88
|
+
headers["Content-Type"] = "application/json";
|
|
89
|
+
}
|
|
76
90
|
if (DEFAULT_FETCH_INIT?.headers)
|
|
77
91
|
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
|
|
78
92
|
if (init?.headers)
|
|
@@ -82,15 +96,17 @@ class BaseService {
|
|
|
82
96
|
{
|
|
83
97
|
// chromium browsers have a bug where the user agent can't be modified
|
|
84
98
|
if (typeof window !== "undefined" && "chrome" in window) {
|
|
85
|
-
headers["AssemblyAI-Agent"] =
|
|
86
|
-
this.userAgent;
|
|
99
|
+
headers["AssemblyAI-Agent"] = this.userAgent;
|
|
87
100
|
}
|
|
88
101
|
}
|
|
89
102
|
}
|
|
90
103
|
init.headers = headers;
|
|
91
104
|
if (!input.startsWith("http"))
|
|
92
105
|
input = this.params.baseUrl + input;
|
|
93
|
-
|
|
106
|
+
return await fetch(input, init);
|
|
107
|
+
}
|
|
108
|
+
async fetch(input, init) {
|
|
109
|
+
const response = await this.fetchResponse(input, init);
|
|
94
110
|
if (response.status >= 400) {
|
|
95
111
|
let json;
|
|
96
112
|
const text = await response.text();
|
|
@@ -115,64 +131,342 @@ class BaseService {
|
|
|
115
131
|
}
|
|
116
132
|
}
|
|
117
133
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Error thrown when a synchronous transcription request fails.
|
|
136
|
+
*/
|
|
137
|
+
class SyncTranscriptError extends Error {
|
|
138
|
+
/**
|
|
139
|
+
* Create a new SyncTranscriptError.
|
|
140
|
+
* @param message - The human-readable error message.
|
|
141
|
+
* @param status - The HTTP status code of the failed request.
|
|
142
|
+
* @param errorCode - Machine-readable code — the snake_cased
|
|
143
|
+
* problem-details `title` from the server (e.g. `bad_audio`,
|
|
144
|
+
* `audio_too_large`, `capacity_exceeded`, `inference_timeout`).
|
|
145
|
+
* @param retryAfter - Seconds to wait before retrying, from the
|
|
146
|
+
* `Retry-After` header on 429/503 responses.
|
|
147
|
+
*/
|
|
148
|
+
constructor(message, status, errorCode, retryAfter) {
|
|
149
|
+
super(message);
|
|
150
|
+
this.status = status;
|
|
151
|
+
this.errorCode = errorCode;
|
|
152
|
+
this.retryAfter = retryAfter;
|
|
153
|
+
this.name = "SyncTranscriptError";
|
|
132
154
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function getPath(path) {
|
|
158
|
+
if (path.startsWith("http"))
|
|
159
|
+
return null;
|
|
160
|
+
if (path.startsWith("https"))
|
|
161
|
+
return null;
|
|
162
|
+
if (path.startsWith("data:"))
|
|
163
|
+
return null;
|
|
164
|
+
if (path.startsWith("file://"))
|
|
165
|
+
return path.substring(7);
|
|
166
|
+
if (path.startsWith("file:"))
|
|
167
|
+
return path.substring(5);
|
|
168
|
+
return path;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Canonical paths since the sync API gained a /v1 prefix (#18103); the
|
|
172
|
+
// unprefixed routes remain served for SDK versions that predate it.
|
|
173
|
+
const transcribeEndpoint = "/v1/transcribe";
|
|
174
|
+
const warmEndpoint = "/v1/warm";
|
|
175
|
+
const modelHeader = "X-AAI-Model";
|
|
176
|
+
// Kept above the server's 30 s deadline so the client doesn't race it.
|
|
177
|
+
const defaultTimeoutMs = 60_000;
|
|
178
|
+
const warmTimeoutMs = 10_000;
|
|
179
|
+
const maxPromptLength = 4096;
|
|
180
|
+
const maxKeytermsPromptLength = 2048;
|
|
181
|
+
const maxContextTurns = 100;
|
|
182
|
+
const maxContextLength = 4096;
|
|
183
|
+
// Extensions that signal raw S16LE PCM rather than a WAV container.
|
|
184
|
+
const pcmSuffixes = [".pcm", ".raw"];
|
|
185
|
+
/**
|
|
186
|
+
* The synchronous transcription service: audio in, transcript out,
|
|
187
|
+
* one request.
|
|
188
|
+
*
|
|
189
|
+
* Unlike `client.transcripts` (which submits a job to the async API and
|
|
190
|
+
* polls for completion), `SyncTranscriber` posts the audio to the sync
|
|
191
|
+
* API and returns the finished transcript in the HTTP response. There is no
|
|
192
|
+
* job id or status to poll. Accepts a local file path, raw audio bytes, a
|
|
193
|
+
* Blob, or a readable stream — but not a URL.
|
|
194
|
+
*/
|
|
195
|
+
class SyncTranscriber extends BaseService {
|
|
196
|
+
/**
|
|
197
|
+
* Create a new synchronous transcription service.
|
|
198
|
+
* @param params - The parameters to use for the service.
|
|
199
|
+
*/
|
|
200
|
+
constructor(params) {
|
|
201
|
+
super(params);
|
|
139
202
|
}
|
|
140
|
-
|
|
141
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Transcribe audio and return the finished transcript in one request.
|
|
205
|
+
* @param audio - A local file path, raw audio bytes, a Blob, or a readable
|
|
206
|
+
* stream. Raw PCM also requires `sample_rate` and `channels` on the config.
|
|
207
|
+
* @param config - Options for this transcription request.
|
|
208
|
+
* @param options - Client-side options, such as the request timeout.
|
|
209
|
+
* @returns A promise that resolves to the finished transcript.
|
|
210
|
+
* @throws SyncTranscriptError when the request fails.
|
|
211
|
+
*/
|
|
212
|
+
async transcribe(audio, config = {}, options = {}) {
|
|
213
|
+
const { bytes, filename, contentType } = await resolveAudio(audio, config);
|
|
214
|
+
const body = new FormData();
|
|
215
|
+
body.append("audio", new Blob([bytes], { type: contentType }), filename);
|
|
216
|
+
const configJson = buildConfigJson(config);
|
|
217
|
+
if (configJson) {
|
|
218
|
+
body.append("config", new Blob([JSON.stringify(configJson)], { type: "application/json" }));
|
|
219
|
+
}
|
|
220
|
+
const response = await this.fetchResponse(transcribeEndpoint, {
|
|
142
221
|
method: "POST",
|
|
143
|
-
body
|
|
144
|
-
|
|
222
|
+
body,
|
|
223
|
+
headers: { [modelHeader]: config.model ?? defaultSyncSpeechModel },
|
|
224
|
+
signal: AbortSignal.timeout(options.timeout ?? defaultTimeoutMs),
|
|
145
225
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return
|
|
226
|
+
if (response.status !== 200)
|
|
227
|
+
throw await errorFromResponse(response);
|
|
228
|
+
return (await response.json());
|
|
149
229
|
}
|
|
150
230
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
231
|
+
* Open the connection to the sync API ahead of time.
|
|
232
|
+
*
|
|
233
|
+
* The sync API is a single request/response, so a `transcribe()` that
|
|
234
|
+
* opens its connection on demand pays the full DNS + TCP + TLS handshake
|
|
235
|
+
* on the critical path. Call `warm()` as soon as you know audio is coming —
|
|
236
|
+
* typically while the clip is still being recorded — so the next
|
|
237
|
+
* `transcribe()` reuses the already-open connection. `warm()` is idempotent
|
|
238
|
+
* and cheap; call it shortly before `transcribe()` so the pooled connection
|
|
239
|
+
* hasn't idled out.
|
|
240
|
+
* @param params - Optionally the model to route the probe to, so the warmed
|
|
241
|
+
* connection lands on the same backend as the eventual transcription.
|
|
242
|
+
* @returns A promise that resolves to `true` once the connection is open
|
|
243
|
+
* (any HTTP response — even a non-200 — means the socket is
|
|
244
|
+
* established), or `false` if the connection could not be opened.
|
|
154
245
|
*/
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
246
|
+
async warm(params) {
|
|
247
|
+
try {
|
|
248
|
+
await this.fetchResponse(warmEndpoint, {
|
|
249
|
+
method: "GET",
|
|
250
|
+
headers: { [modelHeader]: params?.model ?? defaultSyncSpeechModel },
|
|
251
|
+
signal: AbortSignal.timeout(warmTimeoutMs),
|
|
252
|
+
});
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
160
258
|
}
|
|
161
259
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Read the audio input into bytes and decide its multipart content type.
|
|
262
|
+
*
|
|
263
|
+
* PCM is selected when the source has a `.pcm`/`.raw` extension or when
|
|
264
|
+
* `sample_rate`/`channels` are set on the config (the fields the sync API
|
|
265
|
+
* requires only for raw PCM) — and both must then be present. Everything
|
|
266
|
+
* else is treated as a WAV container. URLs are rejected — the sync API has
|
|
267
|
+
* no URL ingestion.
|
|
268
|
+
*/
|
|
269
|
+
async function resolveAudio(input, config) {
|
|
270
|
+
let bytes;
|
|
271
|
+
let filename;
|
|
272
|
+
let suffix = "";
|
|
273
|
+
if (typeof input === "string") {
|
|
274
|
+
if (/^https?:\/\//i.test(input)) {
|
|
275
|
+
throw new Error("SyncTranscriber does not accept URLs. Pass a local file path or " +
|
|
276
|
+
"audio bytes, or use client.transcripts for URL/async transcription.");
|
|
277
|
+
}
|
|
278
|
+
if (input.startsWith("data:")) {
|
|
279
|
+
bytes = dataUrlToBytes(input);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const path = getPath(input) ?? input;
|
|
283
|
+
bytes = await readStream(await readFile());
|
|
284
|
+
filename = basename(path);
|
|
285
|
+
suffix = extname(filename);
|
|
286
|
+
}
|
|
173
287
|
}
|
|
174
|
-
|
|
175
|
-
|
|
288
|
+
else if (input instanceof Uint8Array) {
|
|
289
|
+
bytes = input;
|
|
290
|
+
}
|
|
291
|
+
else if (input instanceof ArrayBuffer) {
|
|
292
|
+
bytes = new Uint8Array(input);
|
|
293
|
+
}
|
|
294
|
+
else if (input instanceof Blob) {
|
|
295
|
+
bytes = new Uint8Array(await input.arrayBuffer());
|
|
296
|
+
// File instances carry a name; the File global itself needs Node >= 20.
|
|
297
|
+
const name = input.name;
|
|
298
|
+
if (name) {
|
|
299
|
+
filename = basename(name);
|
|
300
|
+
suffix = extname(filename);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else if (isWebReadableStream(input)) {
|
|
304
|
+
bytes = await readStream(input);
|
|
305
|
+
}
|
|
306
|
+
else if (isAsyncIterable(input)) {
|
|
307
|
+
bytes = await readAsyncIterable(input);
|
|
308
|
+
// fs.ReadStream carries the path it was opened from.
|
|
309
|
+
const path = input.path;
|
|
310
|
+
if (typeof path === "string") {
|
|
311
|
+
filename = basename(path);
|
|
312
|
+
suffix = extname(filename);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
throw new TypeError("unsupported audio input type");
|
|
317
|
+
}
|
|
318
|
+
const wantsPcm = config.sample_rate !== undefined || config.channels !== undefined;
|
|
319
|
+
const isPcm = pcmSuffixes.includes(suffix) || wantsPcm;
|
|
320
|
+
if (isPcm &&
|
|
321
|
+
(config.sample_rate === undefined || config.channels === undefined)) {
|
|
322
|
+
throw new Error("raw PCM audio requires both sample_rate and channels in the config");
|
|
323
|
+
}
|
|
324
|
+
const contentType = isPcm ? "audio/pcm" : "audio/wav";
|
|
325
|
+
if (!filename)
|
|
326
|
+
filename = isPcm ? "audio.pcm" : "audio.wav";
|
|
327
|
+
return { bytes, filename, contentType };
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Serialize the config to the JSON `config` part, validating and normalizing
|
|
331
|
+
* field values to match the server's caps. The routing `model` is never
|
|
332
|
+
* included — it travels in the `X-AAI-Model` header. Returns `undefined`
|
|
333
|
+
* when there is nothing to send, so the part can be omitted entirely.
|
|
334
|
+
*/
|
|
335
|
+
function buildConfigJson(config) {
|
|
336
|
+
if (config.prompt !== undefined && config.prompt.length > maxPromptLength) {
|
|
337
|
+
throw new Error(`prompt exceeds ${maxPromptLength} characters (got ${config.prompt.length})`);
|
|
338
|
+
}
|
|
339
|
+
const json = {};
|
|
340
|
+
if (config.prompt !== undefined)
|
|
341
|
+
json["prompt"] = config.prompt;
|
|
342
|
+
const keytermsPrompt = normalizeKeytermsPrompt(config.keyterms_prompt);
|
|
343
|
+
if (keytermsPrompt)
|
|
344
|
+
json["keyterms_prompt"] = keytermsPrompt;
|
|
345
|
+
const context = normalizeConversationContext(config.conversation_context);
|
|
346
|
+
if (context)
|
|
347
|
+
json["conversation_context"] = context;
|
|
348
|
+
if (config.language_codes !== undefined)
|
|
349
|
+
json["language_codes"] = config.language_codes;
|
|
350
|
+
if (config.sample_rate !== undefined)
|
|
351
|
+
json["sample_rate"] = config.sample_rate;
|
|
352
|
+
if (config.channels !== undefined)
|
|
353
|
+
json["channels"] = config.channels;
|
|
354
|
+
if (config.timestamps !== undefined)
|
|
355
|
+
json["timestamps"] = config.timestamps;
|
|
356
|
+
return Object.keys(json).length > 0 ? json : undefined;
|
|
357
|
+
}
|
|
358
|
+
function normalizeKeytermsPrompt(keytermsPrompt) {
|
|
359
|
+
if (!keytermsPrompt)
|
|
360
|
+
return undefined;
|
|
361
|
+
const terms = keytermsPrompt
|
|
362
|
+
.map((term) => term.trim())
|
|
363
|
+
.filter((term) => term.length > 0);
|
|
364
|
+
const total = terms.reduce((sum, term) => sum + term.length, 0);
|
|
365
|
+
if (total > maxKeytermsPromptLength) {
|
|
366
|
+
throw new Error(`keyterms_prompt exceeds ${maxKeytermsPromptLength} characters (got ${total})`);
|
|
367
|
+
}
|
|
368
|
+
return terms.length > 0 ? terms : undefined;
|
|
369
|
+
}
|
|
370
|
+
function normalizeConversationContext(context) {
|
|
371
|
+
if (context === undefined)
|
|
372
|
+
return undefined;
|
|
373
|
+
let turns = (typeof context === "string" ? [context] : context)
|
|
374
|
+
.map((turn) => turn.trim())
|
|
375
|
+
.filter((turn) => turn.length > 0);
|
|
376
|
+
let total = turns.reduce((sum, turn) => sum + turn.length, 0);
|
|
377
|
+
// Over-cap context is trimmed oldest-first, never rejected.
|
|
378
|
+
while (turns.length > 0 &&
|
|
379
|
+
(turns.length > maxContextTurns || total > maxContextLength)) {
|
|
380
|
+
total -= turns[0].length;
|
|
381
|
+
turns = turns.slice(1);
|
|
382
|
+
}
|
|
383
|
+
return turns.length > 0 ? turns : undefined;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Build a SyncTranscriptError from a non-200 response. The primary format
|
|
387
|
+
* is an RFC 9457 problem-details body (`status`/`title`/`detail`); legacy
|
|
388
|
+
* `{error_code, message}` and `{detail}`-only bodies are also accepted.
|
|
389
|
+
*/
|
|
390
|
+
async function errorFromResponse(response) {
|
|
391
|
+
let errorCode;
|
|
392
|
+
let message;
|
|
393
|
+
const text = await response.text();
|
|
394
|
+
try {
|
|
395
|
+
const body = JSON.parse(text);
|
|
396
|
+
if (body && typeof body === "object" && !Array.isArray(body)) {
|
|
397
|
+
if (typeof body.error_code === "string")
|
|
398
|
+
errorCode = body.error_code;
|
|
399
|
+
if (errorCode === undefined && typeof body.title === "string") {
|
|
400
|
+
errorCode = body.title.toLowerCase().replace(/ /g, "_");
|
|
401
|
+
}
|
|
402
|
+
if (typeof body.detail === "string")
|
|
403
|
+
message = body.detail;
|
|
404
|
+
else if (typeof body.message === "string")
|
|
405
|
+
message = body.message;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
if (text)
|
|
410
|
+
message = text;
|
|
411
|
+
}
|
|
412
|
+
if (!message) {
|
|
413
|
+
message = `sync transcription failed with status ${response.status}`;
|
|
414
|
+
}
|
|
415
|
+
const retryHeader = response.headers.get("retry-after");
|
|
416
|
+
const retryAfter = retryHeader && /^\d+$/.test(retryHeader)
|
|
417
|
+
? parseInt(retryHeader, 10)
|
|
418
|
+
: undefined;
|
|
419
|
+
return new SyncTranscriptError(message, response.status, errorCode, retryAfter);
|
|
420
|
+
}
|
|
421
|
+
function basename(path) {
|
|
422
|
+
return path.split(/[\\/]/).pop() ?? path;
|
|
423
|
+
}
|
|
424
|
+
function extname(filename) {
|
|
425
|
+
const dotIndex = filename.lastIndexOf(".");
|
|
426
|
+
return dotIndex > 0 ? filename.slice(dotIndex).toLowerCase() : "";
|
|
427
|
+
}
|
|
428
|
+
function dataUrlToBytes(dataUrl) {
|
|
429
|
+
const base64 = dataUrl.split(",")[1];
|
|
430
|
+
const binary = atob(base64);
|
|
431
|
+
const bytes = new Uint8Array(binary.length);
|
|
432
|
+
for (let i = 0; i < binary.length; i++)
|
|
433
|
+
bytes[i] = binary.charCodeAt(i);
|
|
434
|
+
return bytes;
|
|
435
|
+
}
|
|
436
|
+
function isWebReadableStream(input) {
|
|
437
|
+
return typeof input?.getReader === "function";
|
|
438
|
+
}
|
|
439
|
+
function isAsyncIterable(input) {
|
|
440
|
+
return (typeof input?.[Symbol.asyncIterator] ===
|
|
441
|
+
"function");
|
|
442
|
+
}
|
|
443
|
+
async function readStream(stream) {
|
|
444
|
+
const chunks = [];
|
|
445
|
+
const reader = stream.getReader();
|
|
446
|
+
for (;;) {
|
|
447
|
+
const { done, value } = await reader.read();
|
|
448
|
+
if (done)
|
|
449
|
+
break;
|
|
450
|
+
chunks.push(value);
|
|
451
|
+
}
|
|
452
|
+
return concatChunks(chunks);
|
|
453
|
+
}
|
|
454
|
+
async function readAsyncIterable(iterable) {
|
|
455
|
+
const chunks = [];
|
|
456
|
+
for await (const chunk of iterable)
|
|
457
|
+
chunks.push(chunk);
|
|
458
|
+
return concatChunks(chunks);
|
|
459
|
+
}
|
|
460
|
+
function concatChunks(chunks) {
|
|
461
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
462
|
+
const bytes = new Uint8Array(total);
|
|
463
|
+
let offset = 0;
|
|
464
|
+
for (const chunk of chunks) {
|
|
465
|
+
bytes.set(chunk, offset);
|
|
466
|
+
offset += chunk.length;
|
|
467
|
+
}
|
|
468
|
+
return bytes;
|
|
469
|
+
}
|
|
176
470
|
|
|
177
471
|
const RealtimeErrorType = {
|
|
178
472
|
BadSampleRate: 4000,
|
|
@@ -278,6 +572,65 @@ const StreamingErrorMessages = {
|
|
|
278
572
|
class StreamingError extends Error {
|
|
279
573
|
}
|
|
280
574
|
|
|
575
|
+
class LemurService extends BaseService {
|
|
576
|
+
summary(params, signal) {
|
|
577
|
+
return this.fetchJson("/lemur/v3/generate/summary", {
|
|
578
|
+
method: "POST",
|
|
579
|
+
body: JSON.stringify(params),
|
|
580
|
+
signal,
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
questionAnswer(params, signal) {
|
|
584
|
+
return this.fetchJson("/lemur/v3/generate/question-answer", {
|
|
585
|
+
method: "POST",
|
|
586
|
+
body: JSON.stringify(params),
|
|
587
|
+
signal,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
actionItems(params, signal) {
|
|
591
|
+
return this.fetchJson("/lemur/v3/generate/action-items", {
|
|
592
|
+
method: "POST",
|
|
593
|
+
body: JSON.stringify(params),
|
|
594
|
+
signal,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
task(params, signal) {
|
|
598
|
+
return this.fetchJson("/lemur/v3/generate/task", {
|
|
599
|
+
method: "POST",
|
|
600
|
+
body: JSON.stringify(params),
|
|
601
|
+
signal,
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
getResponse(id, signal) {
|
|
605
|
+
return this.fetchJson(`/lemur/v3/${id}`, { signal });
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Delete the data for a previously submitted LeMUR request.
|
|
609
|
+
* @param id - ID of the LeMUR request
|
|
610
|
+
* @param signal - Optional AbortSignal to cancel the request
|
|
611
|
+
*/
|
|
612
|
+
purgeRequestData(id, signal) {
|
|
613
|
+
return this.fetchJson(`/lemur/v3/${id}`, {
|
|
614
|
+
method: "DELETE",
|
|
615
|
+
signal,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
621
|
+
? window
|
|
622
|
+
: typeof global !== "undefined"
|
|
623
|
+
? global
|
|
624
|
+
: globalThis;
|
|
625
|
+
|
|
626
|
+
const PolyfillWebSocket = WebSocket ?? global?.WebSocket ?? window?.WebSocket ?? self?.WebSocket;
|
|
627
|
+
const factory = (url, params) => {
|
|
628
|
+
if (params) {
|
|
629
|
+
return new PolyfillWebSocket(url, params);
|
|
630
|
+
}
|
|
631
|
+
return new PolyfillWebSocket(url);
|
|
632
|
+
};
|
|
633
|
+
|
|
281
634
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
282
635
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
283
636
|
const terminateSessionMessage$1 = `{"terminate_session":true}`;
|
|
@@ -526,20 +879,6 @@ class RealtimeTranscriberFactory extends BaseService {
|
|
|
526
879
|
class RealtimeServiceFactory extends RealtimeTranscriberFactory {
|
|
527
880
|
}
|
|
528
881
|
|
|
529
|
-
function getPath(path) {
|
|
530
|
-
if (path.startsWith("http"))
|
|
531
|
-
return null;
|
|
532
|
-
if (path.startsWith("https"))
|
|
533
|
-
return null;
|
|
534
|
-
if (path.startsWith("data:"))
|
|
535
|
-
return null;
|
|
536
|
-
if (path.startsWith("file://"))
|
|
537
|
-
return path.substring(7);
|
|
538
|
-
if (path.startsWith("file:"))
|
|
539
|
-
return path.substring(5);
|
|
540
|
-
return path;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
882
|
class TranscriptService extends BaseService {
|
|
544
883
|
constructor(params, files) {
|
|
545
884
|
super(params);
|
|
@@ -767,12 +1106,6 @@ class TranscriptService extends BaseService {
|
|
|
767
1106
|
}
|
|
768
1107
|
}
|
|
769
1108
|
|
|
770
|
-
const readFile = async function (
|
|
771
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
772
|
-
path) {
|
|
773
|
-
throw new Error("Interacting with the file system is not supported in this environment.");
|
|
774
|
-
};
|
|
775
|
-
|
|
776
1109
|
class FileService extends BaseService {
|
|
777
1110
|
/**
|
|
778
1111
|
* Upload a local file to AssemblyAI.
|
|
@@ -2038,6 +2371,7 @@ function float32ToPcm16(input) {
|
|
|
2038
2371
|
|
|
2039
2372
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
2040
2373
|
const defaultStreamingUrl = "https://streaming.assemblyai.com";
|
|
2374
|
+
const defaultSyncUrl = "https://sync.assemblyai.com";
|
|
2041
2375
|
class AssemblyAI {
|
|
2042
2376
|
/**
|
|
2043
2377
|
* Create a new AssemblyAI client.
|
|
@@ -2056,7 +2390,15 @@ class AssemblyAI {
|
|
|
2056
2390
|
...params,
|
|
2057
2391
|
baseUrl: params.streamingBaseUrl || defaultStreamingUrl,
|
|
2058
2392
|
});
|
|
2393
|
+
let syncBaseUrl = params.syncBaseUrl || defaultSyncUrl;
|
|
2394
|
+
if (syncBaseUrl.endsWith("/")) {
|
|
2395
|
+
syncBaseUrl = syncBaseUrl.slice(0, -1);
|
|
2396
|
+
}
|
|
2397
|
+
this.sync = new SyncTranscriber({
|
|
2398
|
+
...params,
|
|
2399
|
+
baseUrl: syncBaseUrl,
|
|
2400
|
+
});
|
|
2059
2401
|
}
|
|
2060
2402
|
}
|
|
2061
2403
|
|
|
2062
|
-
export { AssemblyAI, BrowserOnlyError, DualChannelCapture, EnergyVad, FileService, LemurService, LinearResampler, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, StreamingTranscriber, TranscriptService, VadTimeline, attributeTurn, attributeWord, float32ToPcm16, rollUpTurnChannel };
|
|
2404
|
+
export { AssemblyAI, BrowserOnlyError, DualChannelCapture, EnergyVad, FileService, LemurService, LinearResampler, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, StreamingTranscriber, SyncTranscriber, SyncTranscriptError, TranscriptService, VadTimeline, attributeTurn, attributeWord, defaultSyncSpeechModel, float32ToPcm16, rollUpTurnChannel };
|