assemblyai 4.35.3 → 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 +24 -0
- package/README.md +98 -0
- package/dist/assemblyai.streaming.umd.js +26 -11
- package/dist/assemblyai.streaming.umd.min.js +1 -1
- package/dist/assemblyai.umd.js +486 -83
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +430 -80
- package/dist/bun.mjs +420 -69
- package/dist/deno.mjs +420 -69
- package/dist/index.cjs +479 -76
- package/dist/index.mjs +477 -77
- package/dist/node.cjs +418 -64
- package/dist/node.mjs +416 -65
- 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 +24 -11
- package/dist/streaming.cjs +25 -10
- package/dist/streaming.mjs +25 -10
- package/dist/types/index.d.ts +1 -0
- package/dist/types/services/index.d.ts +1 -0
- package/dist/types/streaming/index.d.ts +6 -1
- 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 +424 -73
- package/package.json +1 -1
- package/src/services/base.ts +18 -8
- package/src/services/index.ts +19 -0
- package/src/services/streaming/service.ts +17 -6
- 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/streaming/index.ts +6 -1
- 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/index.cjs
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
var ws = require('ws');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* The default speech model for synchronous transcription.
|
|
7
|
+
*/
|
|
8
|
+
const defaultSyncSpeechModel = "universal-3-5-pro";
|
|
9
|
+
|
|
5
10
|
/**
|
|
6
11
|
* Thrown when `DualChannelCapture` is constructed in a non-browser environment
|
|
7
12
|
* (no `globalThis.AudioContext`). The helper is intentionally surfaced from the
|
|
@@ -54,11 +59,39 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
54
59
|
});
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
function __values(o) {
|
|
63
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
64
|
+
if (m) return m.call(o);
|
|
65
|
+
if (o && typeof o.length === "number") return {
|
|
66
|
+
next: function () {
|
|
67
|
+
if (o && i >= o.length) o = void 0;
|
|
68
|
+
return { value: o && o[i++], done: !o };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function __asyncValues(o) {
|
|
75
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
76
|
+
var m = o[Symbol.asyncIterator], i;
|
|
77
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
78
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
79
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
80
|
+
}
|
|
81
|
+
|
|
57
82
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
58
83
|
var e = new Error(message);
|
|
59
84
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
60
85
|
};
|
|
61
86
|
|
|
87
|
+
const readFile = function (
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
89
|
+
path) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
throw new Error("Interacting with the file system is not supported in this environment.");
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
62
95
|
const DEFAULT_FETCH_INIT = {
|
|
63
96
|
cache: "no-store",
|
|
64
97
|
};
|
|
@@ -76,7 +109,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
76
109
|
defaultUserAgentString += navigator.userAgent;
|
|
77
110
|
}
|
|
78
111
|
const defaultUserAgent = {
|
|
79
|
-
sdk: { name: "JavaScript", version: "4.
|
|
112
|
+
sdk: { name: "JavaScript", version: "4.36.3" },
|
|
80
113
|
};
|
|
81
114
|
if (typeof process !== "undefined") {
|
|
82
115
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -115,13 +148,16 @@ class BaseService {
|
|
|
115
148
|
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
116
149
|
}
|
|
117
150
|
}
|
|
118
|
-
|
|
151
|
+
fetchResponse(input, init) {
|
|
119
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
153
|
init = Object.assign(Object.assign({}, DEFAULT_FETCH_INIT), init);
|
|
121
154
|
let headers = {
|
|
122
155
|
Authorization: this.params.apiKey,
|
|
123
|
-
"Content-Type": "application/json",
|
|
124
156
|
};
|
|
157
|
+
// FormData bodies must let fetch set the multipart boundary itself.
|
|
158
|
+
if (!(init.body instanceof FormData)) {
|
|
159
|
+
headers["Content-Type"] = "application/json";
|
|
160
|
+
}
|
|
125
161
|
if (DEFAULT_FETCH_INIT === null || DEFAULT_FETCH_INIT === void 0 ? void 0 : DEFAULT_FETCH_INIT.headers)
|
|
126
162
|
headers = Object.assign(Object.assign({}, headers), DEFAULT_FETCH_INIT.headers);
|
|
127
163
|
if (init === null || init === void 0 ? void 0 : init.headers)
|
|
@@ -131,15 +167,19 @@ class BaseService {
|
|
|
131
167
|
{
|
|
132
168
|
// chromium browsers have a bug where the user agent can't be modified
|
|
133
169
|
if (typeof window !== "undefined" && "chrome" in window) {
|
|
134
|
-
headers["AssemblyAI-Agent"] =
|
|
135
|
-
this.userAgent;
|
|
170
|
+
headers["AssemblyAI-Agent"] = this.userAgent;
|
|
136
171
|
}
|
|
137
172
|
}
|
|
138
173
|
}
|
|
139
174
|
init.headers = headers;
|
|
140
175
|
if (!input.startsWith("http"))
|
|
141
176
|
input = this.params.baseUrl + input;
|
|
142
|
-
|
|
177
|
+
return yield fetch(input, init);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
fetch(input, init) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
const response = yield this.fetchResponse(input, init);
|
|
143
183
|
if (response.status >= 400) {
|
|
144
184
|
let json;
|
|
145
185
|
const text = yield response.text();
|
|
@@ -167,58 +207,373 @@ class BaseService {
|
|
|
167
207
|
}
|
|
168
208
|
}
|
|
169
209
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Error thrown when a synchronous transcription request fails.
|
|
212
|
+
*/
|
|
213
|
+
class SyncTranscriptError extends Error {
|
|
214
|
+
/**
|
|
215
|
+
* Create a new SyncTranscriptError.
|
|
216
|
+
* @param message - The human-readable error message.
|
|
217
|
+
* @param status - The HTTP status code of the failed request.
|
|
218
|
+
* @param errorCode - Machine-readable code — the snake_cased
|
|
219
|
+
* problem-details `title` from the server (e.g. `bad_audio`,
|
|
220
|
+
* `audio_too_large`, `capacity_exceeded`, `inference_timeout`).
|
|
221
|
+
* @param retryAfter - Seconds to wait before retrying, from the
|
|
222
|
+
* `Retry-After` header on 429/503 responses.
|
|
223
|
+
*/
|
|
224
|
+
constructor(message, status, errorCode, retryAfter) {
|
|
225
|
+
super(message);
|
|
226
|
+
this.status = status;
|
|
227
|
+
this.errorCode = errorCode;
|
|
228
|
+
this.retryAfter = retryAfter;
|
|
229
|
+
this.name = "SyncTranscriptError";
|
|
184
230
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function getPath(path) {
|
|
234
|
+
if (path.startsWith("http"))
|
|
235
|
+
return null;
|
|
236
|
+
if (path.startsWith("https"))
|
|
237
|
+
return null;
|
|
238
|
+
if (path.startsWith("data:"))
|
|
239
|
+
return null;
|
|
240
|
+
if (path.startsWith("file://"))
|
|
241
|
+
return path.substring(7);
|
|
242
|
+
if (path.startsWith("file:"))
|
|
243
|
+
return path.substring(5);
|
|
244
|
+
return path;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Canonical paths since the sync API gained a /v1 prefix (#18103); the
|
|
248
|
+
// unprefixed routes remain served for SDK versions that predate it.
|
|
249
|
+
const transcribeEndpoint = "/v1/transcribe";
|
|
250
|
+
const warmEndpoint = "/v1/warm";
|
|
251
|
+
const modelHeader = "X-AAI-Model";
|
|
252
|
+
// Kept above the server's 30 s deadline so the client doesn't race it.
|
|
253
|
+
const defaultTimeoutMs = 60000;
|
|
254
|
+
const warmTimeoutMs = 10000;
|
|
255
|
+
const maxPromptLength = 4096;
|
|
256
|
+
const maxKeytermsPromptLength = 2048;
|
|
257
|
+
const maxContextTurns = 100;
|
|
258
|
+
const maxContextLength = 4096;
|
|
259
|
+
// Extensions that signal raw S16LE PCM rather than a WAV container.
|
|
260
|
+
const pcmSuffixes = [".pcm", ".raw"];
|
|
261
|
+
/**
|
|
262
|
+
* The synchronous transcription service: audio in, transcript out,
|
|
263
|
+
* one request.
|
|
264
|
+
*
|
|
265
|
+
* Unlike `client.transcripts` (which submits a job to the async API and
|
|
266
|
+
* polls for completion), `SyncTranscriber` posts the audio to the sync
|
|
267
|
+
* API and returns the finished transcript in the HTTP response. There is no
|
|
268
|
+
* job id or status to poll. Accepts a local file path, raw audio bytes, a
|
|
269
|
+
* Blob, or a readable stream — but not a URL.
|
|
270
|
+
*/
|
|
271
|
+
class SyncTranscriber extends BaseService {
|
|
272
|
+
/**
|
|
273
|
+
* Create a new synchronous transcription service.
|
|
274
|
+
* @param params - The parameters to use for the service.
|
|
275
|
+
*/
|
|
276
|
+
constructor(params) {
|
|
277
|
+
super(params);
|
|
191
278
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
279
|
+
/**
|
|
280
|
+
* Transcribe audio and return the finished transcript in one request.
|
|
281
|
+
* @param audio - A local file path, raw audio bytes, a Blob, or a readable
|
|
282
|
+
* stream. Raw PCM also requires `sample_rate` and `channels` on the config.
|
|
283
|
+
* @param config - Options for this transcription request.
|
|
284
|
+
* @param options - Client-side options, such as the request timeout.
|
|
285
|
+
* @returns A promise that resolves to the finished transcript.
|
|
286
|
+
* @throws SyncTranscriptError when the request fails.
|
|
287
|
+
*/
|
|
288
|
+
transcribe(audio_1) {
|
|
289
|
+
return __awaiter(this, arguments, void 0, function* (audio, config = {}, options = {}) {
|
|
290
|
+
var _a, _b;
|
|
291
|
+
const { bytes, filename, contentType } = yield resolveAudio(audio, config);
|
|
292
|
+
const body = new FormData();
|
|
293
|
+
body.append("audio", new Blob([bytes], { type: contentType }), filename);
|
|
294
|
+
const configJson = buildConfigJson(config);
|
|
295
|
+
if (configJson) {
|
|
296
|
+
body.append("config", new Blob([JSON.stringify(configJson)], { type: "application/json" }));
|
|
297
|
+
}
|
|
298
|
+
const response = yield this.fetchResponse(transcribeEndpoint, {
|
|
299
|
+
method: "POST",
|
|
300
|
+
body,
|
|
301
|
+
headers: { [modelHeader]: (_a = config.model) !== null && _a !== void 0 ? _a : defaultSyncSpeechModel },
|
|
302
|
+
signal: AbortSignal.timeout((_b = options.timeout) !== null && _b !== void 0 ? _b : defaultTimeoutMs),
|
|
303
|
+
});
|
|
304
|
+
if (response.status !== 200)
|
|
305
|
+
throw yield errorFromResponse(response);
|
|
306
|
+
return (yield response.json());
|
|
197
307
|
});
|
|
198
308
|
}
|
|
199
|
-
getResponse(id, signal) {
|
|
200
|
-
return this.fetchJson(`/lemur/v3/${id}`, { signal });
|
|
201
|
-
}
|
|
202
309
|
/**
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
310
|
+
* Open the connection to the sync API ahead of time.
|
|
311
|
+
*
|
|
312
|
+
* The sync API is a single request/response, so a `transcribe()` that
|
|
313
|
+
* opens its connection on demand pays the full DNS + TCP + TLS handshake
|
|
314
|
+
* on the critical path. Call `warm()` as soon as you know audio is coming —
|
|
315
|
+
* typically while the clip is still being recorded — so the next
|
|
316
|
+
* `transcribe()` reuses the already-open connection. `warm()` is idempotent
|
|
317
|
+
* and cheap; call it shortly before `transcribe()` so the pooled connection
|
|
318
|
+
* hasn't idled out.
|
|
319
|
+
* @param params - Optionally the model to route the probe to, so the warmed
|
|
320
|
+
* connection lands on the same backend as the eventual transcription.
|
|
321
|
+
* @returns A promise that resolves to `true` once the connection is open
|
|
322
|
+
* (any HTTP response — even a non-200 — means the socket is
|
|
323
|
+
* established), or `false` if the connection could not be opened.
|
|
206
324
|
*/
|
|
207
|
-
|
|
208
|
-
return this
|
|
209
|
-
|
|
210
|
-
|
|
325
|
+
warm(params) {
|
|
326
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
var _a;
|
|
328
|
+
try {
|
|
329
|
+
yield this.fetchResponse(warmEndpoint, {
|
|
330
|
+
method: "GET",
|
|
331
|
+
headers: { [modelHeader]: (_a = params === null || params === void 0 ? void 0 : params.model) !== null && _a !== void 0 ? _a : defaultSyncSpeechModel },
|
|
332
|
+
signal: AbortSignal.timeout(warmTimeoutMs),
|
|
333
|
+
});
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
catch (_b) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
211
339
|
});
|
|
212
340
|
}
|
|
213
341
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Read the audio input into bytes and decide its multipart content type.
|
|
344
|
+
*
|
|
345
|
+
* PCM is selected when the source has a `.pcm`/`.raw` extension or when
|
|
346
|
+
* `sample_rate`/`channels` are set on the config (the fields the sync API
|
|
347
|
+
* requires only for raw PCM) — and both must then be present. Everything
|
|
348
|
+
* else is treated as a WAV container. URLs are rejected — the sync API has
|
|
349
|
+
* no URL ingestion.
|
|
350
|
+
*/
|
|
351
|
+
function resolveAudio(input, config) {
|
|
352
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
353
|
+
var _a;
|
|
354
|
+
let bytes;
|
|
355
|
+
let filename;
|
|
356
|
+
let suffix = "";
|
|
357
|
+
if (typeof input === "string") {
|
|
358
|
+
if (/^https?:\/\//i.test(input)) {
|
|
359
|
+
throw new Error("SyncTranscriber does not accept URLs. Pass a local file path or " +
|
|
360
|
+
"audio bytes, or use client.transcripts for URL/async transcription.");
|
|
361
|
+
}
|
|
362
|
+
if (input.startsWith("data:")) {
|
|
363
|
+
bytes = dataUrlToBytes(input);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const path = (_a = getPath(input)) !== null && _a !== void 0 ? _a : input;
|
|
367
|
+
bytes = yield readStream(yield readFile());
|
|
368
|
+
filename = basename(path);
|
|
369
|
+
suffix = extname(filename);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
else if (input instanceof Uint8Array) {
|
|
373
|
+
bytes = input;
|
|
374
|
+
}
|
|
375
|
+
else if (input instanceof ArrayBuffer) {
|
|
376
|
+
bytes = new Uint8Array(input);
|
|
377
|
+
}
|
|
378
|
+
else if (input instanceof Blob) {
|
|
379
|
+
bytes = new Uint8Array(yield input.arrayBuffer());
|
|
380
|
+
// File instances carry a name; the File global itself needs Node >= 20.
|
|
381
|
+
const name = input.name;
|
|
382
|
+
if (name) {
|
|
383
|
+
filename = basename(name);
|
|
384
|
+
suffix = extname(filename);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
else if (isWebReadableStream(input)) {
|
|
388
|
+
bytes = yield readStream(input);
|
|
389
|
+
}
|
|
390
|
+
else if (isAsyncIterable(input)) {
|
|
391
|
+
bytes = yield readAsyncIterable(input);
|
|
392
|
+
// fs.ReadStream carries the path it was opened from.
|
|
393
|
+
const path = input.path;
|
|
394
|
+
if (typeof path === "string") {
|
|
395
|
+
filename = basename(path);
|
|
396
|
+
suffix = extname(filename);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
throw new TypeError("unsupported audio input type");
|
|
401
|
+
}
|
|
402
|
+
const wantsPcm = config.sample_rate !== undefined || config.channels !== undefined;
|
|
403
|
+
const isPcm = pcmSuffixes.includes(suffix) || wantsPcm;
|
|
404
|
+
if (isPcm &&
|
|
405
|
+
(config.sample_rate === undefined || config.channels === undefined)) {
|
|
406
|
+
throw new Error("raw PCM audio requires both sample_rate and channels in the config");
|
|
407
|
+
}
|
|
408
|
+
const contentType = isPcm ? "audio/pcm" : "audio/wav";
|
|
409
|
+
if (!filename)
|
|
410
|
+
filename = isPcm ? "audio.pcm" : "audio.wav";
|
|
411
|
+
return { bytes, filename, contentType };
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Serialize the config to the JSON `config` part, validating and normalizing
|
|
416
|
+
* field values to match the server's caps. The routing `model` is never
|
|
417
|
+
* included — it travels in the `X-AAI-Model` header. Returns `undefined`
|
|
418
|
+
* when there is nothing to send, so the part can be omitted entirely.
|
|
419
|
+
*/
|
|
420
|
+
function buildConfigJson(config) {
|
|
421
|
+
if (config.prompt !== undefined && config.prompt.length > maxPromptLength) {
|
|
422
|
+
throw new Error(`prompt exceeds ${maxPromptLength} characters (got ${config.prompt.length})`);
|
|
423
|
+
}
|
|
424
|
+
const json = {};
|
|
425
|
+
if (config.prompt !== undefined)
|
|
426
|
+
json["prompt"] = config.prompt;
|
|
427
|
+
const keytermsPrompt = normalizeKeytermsPrompt(config.keyterms_prompt);
|
|
428
|
+
if (keytermsPrompt)
|
|
429
|
+
json["keyterms_prompt"] = keytermsPrompt;
|
|
430
|
+
const context = normalizeConversationContext(config.conversation_context);
|
|
431
|
+
if (context)
|
|
432
|
+
json["conversation_context"] = context;
|
|
433
|
+
if (config.language_codes !== undefined)
|
|
434
|
+
json["language_codes"] = config.language_codes;
|
|
435
|
+
if (config.sample_rate !== undefined)
|
|
436
|
+
json["sample_rate"] = config.sample_rate;
|
|
437
|
+
if (config.channels !== undefined)
|
|
438
|
+
json["channels"] = config.channels;
|
|
439
|
+
if (config.timestamps !== undefined)
|
|
440
|
+
json["timestamps"] = config.timestamps;
|
|
441
|
+
return Object.keys(json).length > 0 ? json : undefined;
|
|
442
|
+
}
|
|
443
|
+
function normalizeKeytermsPrompt(keytermsPrompt) {
|
|
444
|
+
if (!keytermsPrompt)
|
|
445
|
+
return undefined;
|
|
446
|
+
const terms = keytermsPrompt
|
|
447
|
+
.map((term) => term.trim())
|
|
448
|
+
.filter((term) => term.length > 0);
|
|
449
|
+
const total = terms.reduce((sum, term) => sum + term.length, 0);
|
|
450
|
+
if (total > maxKeytermsPromptLength) {
|
|
451
|
+
throw new Error(`keyterms_prompt exceeds ${maxKeytermsPromptLength} characters (got ${total})`);
|
|
452
|
+
}
|
|
453
|
+
return terms.length > 0 ? terms : undefined;
|
|
454
|
+
}
|
|
455
|
+
function normalizeConversationContext(context) {
|
|
456
|
+
if (context === undefined)
|
|
457
|
+
return undefined;
|
|
458
|
+
let turns = (typeof context === "string" ? [context] : context)
|
|
459
|
+
.map((turn) => turn.trim())
|
|
460
|
+
.filter((turn) => turn.length > 0);
|
|
461
|
+
let total = turns.reduce((sum, turn) => sum + turn.length, 0);
|
|
462
|
+
// Over-cap context is trimmed oldest-first, never rejected.
|
|
463
|
+
while (turns.length > 0 &&
|
|
464
|
+
(turns.length > maxContextTurns || total > maxContextLength)) {
|
|
465
|
+
total -= turns[0].length;
|
|
466
|
+
turns = turns.slice(1);
|
|
467
|
+
}
|
|
468
|
+
return turns.length > 0 ? turns : undefined;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Build a SyncTranscriptError from a non-200 response. The primary format
|
|
472
|
+
* is an RFC 9457 problem-details body (`status`/`title`/`detail`); legacy
|
|
473
|
+
* `{error_code, message}` and `{detail}`-only bodies are also accepted.
|
|
474
|
+
*/
|
|
475
|
+
function errorFromResponse(response) {
|
|
476
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
477
|
+
let errorCode;
|
|
478
|
+
let message;
|
|
479
|
+
const text = yield response.text();
|
|
480
|
+
try {
|
|
481
|
+
const body = JSON.parse(text);
|
|
482
|
+
if (body && typeof body === "object" && !Array.isArray(body)) {
|
|
483
|
+
if (typeof body.error_code === "string")
|
|
484
|
+
errorCode = body.error_code;
|
|
485
|
+
if (errorCode === undefined && typeof body.title === "string") {
|
|
486
|
+
errorCode = body.title.toLowerCase().replace(/ /g, "_");
|
|
487
|
+
}
|
|
488
|
+
if (typeof body.detail === "string")
|
|
489
|
+
message = body.detail;
|
|
490
|
+
else if (typeof body.message === "string")
|
|
491
|
+
message = body.message;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
catch (_a) {
|
|
495
|
+
if (text)
|
|
496
|
+
message = text;
|
|
497
|
+
}
|
|
498
|
+
if (!message) {
|
|
499
|
+
message = `sync transcription failed with status ${response.status}`;
|
|
500
|
+
}
|
|
501
|
+
const retryHeader = response.headers.get("retry-after");
|
|
502
|
+
const retryAfter = retryHeader && /^\d+$/.test(retryHeader)
|
|
503
|
+
? parseInt(retryHeader, 10)
|
|
504
|
+
: undefined;
|
|
505
|
+
return new SyncTranscriptError(message, response.status, errorCode, retryAfter);
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
function basename(path) {
|
|
509
|
+
var _a;
|
|
510
|
+
return (_a = path.split(/[\\/]/).pop()) !== null && _a !== void 0 ? _a : path;
|
|
511
|
+
}
|
|
512
|
+
function extname(filename) {
|
|
513
|
+
const dotIndex = filename.lastIndexOf(".");
|
|
514
|
+
return dotIndex > 0 ? filename.slice(dotIndex).toLowerCase() : "";
|
|
515
|
+
}
|
|
516
|
+
function dataUrlToBytes(dataUrl) {
|
|
517
|
+
const base64 = dataUrl.split(",")[1];
|
|
518
|
+
const binary = atob(base64);
|
|
519
|
+
const bytes = new Uint8Array(binary.length);
|
|
520
|
+
for (let i = 0; i < binary.length; i++)
|
|
521
|
+
bytes[i] = binary.charCodeAt(i);
|
|
522
|
+
return bytes;
|
|
523
|
+
}
|
|
524
|
+
function isWebReadableStream(input) {
|
|
525
|
+
return typeof (input === null || input === void 0 ? void 0 : input.getReader) === "function";
|
|
526
|
+
}
|
|
527
|
+
function isAsyncIterable(input) {
|
|
528
|
+
return (typeof (input === null || input === void 0 ? void 0 : input[Symbol.asyncIterator]) ===
|
|
529
|
+
"function");
|
|
530
|
+
}
|
|
531
|
+
function readStream(stream) {
|
|
532
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
533
|
+
const chunks = [];
|
|
534
|
+
const reader = stream.getReader();
|
|
535
|
+
for (;;) {
|
|
536
|
+
const { done, value } = yield reader.read();
|
|
537
|
+
if (done)
|
|
538
|
+
break;
|
|
539
|
+
chunks.push(value);
|
|
540
|
+
}
|
|
541
|
+
return concatChunks(chunks);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
function readAsyncIterable(iterable) {
|
|
545
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
546
|
+
var _a, iterable_1, iterable_1_1;
|
|
547
|
+
var _b, e_1, _c, _d;
|
|
548
|
+
const chunks = [];
|
|
549
|
+
try {
|
|
550
|
+
for (_a = true, iterable_1 = __asyncValues(iterable); iterable_1_1 = yield iterable_1.next(), _b = iterable_1_1.done, !_b; _a = true) {
|
|
551
|
+
_d = iterable_1_1.value;
|
|
552
|
+
_a = false;
|
|
553
|
+
const chunk = _d;
|
|
554
|
+
chunks.push(chunk);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
558
|
+
finally {
|
|
559
|
+
try {
|
|
560
|
+
if (!_a && !_b && (_c = iterable_1.return)) yield _c.call(iterable_1);
|
|
561
|
+
}
|
|
562
|
+
finally { if (e_1) throw e_1.error; }
|
|
563
|
+
}
|
|
564
|
+
return concatChunks(chunks);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
function concatChunks(chunks) {
|
|
568
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
569
|
+
const bytes = new Uint8Array(total);
|
|
570
|
+
let offset = 0;
|
|
571
|
+
for (const chunk of chunks) {
|
|
572
|
+
bytes.set(chunk, offset);
|
|
573
|
+
offset += chunk.length;
|
|
574
|
+
}
|
|
575
|
+
return bytes;
|
|
576
|
+
}
|
|
222
577
|
|
|
223
578
|
const RealtimeErrorType = {
|
|
224
579
|
BadSampleRate: 4000,
|
|
@@ -324,6 +679,59 @@ const StreamingErrorMessages = {
|
|
|
324
679
|
class StreamingError extends Error {
|
|
325
680
|
}
|
|
326
681
|
|
|
682
|
+
class LemurService extends BaseService {
|
|
683
|
+
summary(params, signal) {
|
|
684
|
+
return this.fetchJson("/lemur/v3/generate/summary", {
|
|
685
|
+
method: "POST",
|
|
686
|
+
body: JSON.stringify(params),
|
|
687
|
+
signal,
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
questionAnswer(params, signal) {
|
|
691
|
+
return this.fetchJson("/lemur/v3/generate/question-answer", {
|
|
692
|
+
method: "POST",
|
|
693
|
+
body: JSON.stringify(params),
|
|
694
|
+
signal,
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
actionItems(params, signal) {
|
|
698
|
+
return this.fetchJson("/lemur/v3/generate/action-items", {
|
|
699
|
+
method: "POST",
|
|
700
|
+
body: JSON.stringify(params),
|
|
701
|
+
signal,
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
task(params, signal) {
|
|
705
|
+
return this.fetchJson("/lemur/v3/generate/task", {
|
|
706
|
+
method: "POST",
|
|
707
|
+
body: JSON.stringify(params),
|
|
708
|
+
signal,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
getResponse(id, signal) {
|
|
712
|
+
return this.fetchJson(`/lemur/v3/${id}`, { signal });
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Delete the data for a previously submitted LeMUR request.
|
|
716
|
+
* @param id - ID of the LeMUR request
|
|
717
|
+
* @param signal - Optional AbortSignal to cancel the request
|
|
718
|
+
*/
|
|
719
|
+
purgeRequestData(id, signal) {
|
|
720
|
+
return this.fetchJson(`/lemur/v3/${id}`, {
|
|
721
|
+
method: "DELETE",
|
|
722
|
+
signal,
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
728
|
+
? window
|
|
729
|
+
: typeof global !== "undefined"
|
|
730
|
+
? global
|
|
731
|
+
: globalThis;
|
|
732
|
+
|
|
733
|
+
const factory = (url, params) => new ws(url, params);
|
|
734
|
+
|
|
327
735
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
328
736
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
329
737
|
const terminateSessionMessage$1 = `{"terminate_session":true}`;
|
|
@@ -577,20 +985,6 @@ class RealtimeTranscriberFactory extends BaseService {
|
|
|
577
985
|
class RealtimeServiceFactory extends RealtimeTranscriberFactory {
|
|
578
986
|
}
|
|
579
987
|
|
|
580
|
-
function getPath(path) {
|
|
581
|
-
if (path.startsWith("http"))
|
|
582
|
-
return null;
|
|
583
|
-
if (path.startsWith("https"))
|
|
584
|
-
return null;
|
|
585
|
-
if (path.startsWith("data:"))
|
|
586
|
-
return null;
|
|
587
|
-
if (path.startsWith("file://"))
|
|
588
|
-
return path.substring(7);
|
|
589
|
-
if (path.startsWith("file:"))
|
|
590
|
-
return path.substring(5);
|
|
591
|
-
return path;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
988
|
class TranscriptService extends BaseService {
|
|
595
989
|
constructor(params, files) {
|
|
596
990
|
super(params);
|
|
@@ -837,14 +1231,6 @@ class TranscriptService extends BaseService {
|
|
|
837
1231
|
}
|
|
838
1232
|
}
|
|
839
1233
|
|
|
840
|
-
const readFile = function (
|
|
841
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
842
|
-
path) {
|
|
843
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
844
|
-
throw new Error("Interacting with the file system is not supported in this environment.");
|
|
845
|
-
});
|
|
846
|
-
};
|
|
847
|
-
|
|
848
1234
|
class FileService extends BaseService {
|
|
849
1235
|
/**
|
|
850
1236
|
* Upload a local file to AssemblyAI.
|
|
@@ -1128,6 +1514,10 @@ class StreamingTranscriber {
|
|
|
1128
1514
|
if (!(this.token || this.apiKey)) {
|
|
1129
1515
|
throw new Error("API key or temporary token is required.");
|
|
1130
1516
|
}
|
|
1517
|
+
const isOpus = params.encoding === "opus" || params.encoding === "ogg_opus";
|
|
1518
|
+
if (params.sampleRate === undefined && (!isOpus || params.channels)) {
|
|
1519
|
+
throw new Error('`sampleRate` is required; it may only be omitted when `encoding` is "opus" or "ogg_opus" (the Opus stream is self-describing) and dual-channel mode is not used.');
|
|
1520
|
+
}
|
|
1131
1521
|
if (params.channels) {
|
|
1132
1522
|
if (params.channels.length !== 2) {
|
|
1133
1523
|
throw new Error("StreamingTranscriber.channels must have exactly 2 entries.");
|
|
@@ -1153,10 +1543,12 @@ class StreamingTranscriber {
|
|
|
1153
1543
|
"speaker-history") {
|
|
1154
1544
|
this.speakerHistory = new Map();
|
|
1155
1545
|
}
|
|
1156
|
-
// 20 ms VAD frames at the transcriber's target sample rate.
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
this.
|
|
1546
|
+
// 20 ms VAD frames at the transcriber's target sample rate. The
|
|
1547
|
+
// constructor check above guarantees sampleRate in dual-channel mode.
|
|
1548
|
+
const sampleRate = params.sampleRate;
|
|
1549
|
+
this.vadFrameSamples = Math.max(1, Math.round(sampleRate * 0.02));
|
|
1550
|
+
this.minChunkSamples = Math.max(1, Math.round(sampleRate * (MIN_CHUNK_MS / 1000)));
|
|
1551
|
+
this.maxChunkSamples = Math.max(this.minChunkSamples, Math.round(sampleRate * (MAX_CHUNK_MS / 1000)));
|
|
1160
1552
|
this.channelBuffers = new Map(names.map((n) => [n, []]));
|
|
1161
1553
|
this.channelSamplesReceived = new Map(names.map((n) => [n, 0]));
|
|
1162
1554
|
this.channelVadFloatBuffers = new Map(names.map((n) => [n, new Float32Array(this.vadFrameSamples)]));
|
|
@@ -1175,7 +1567,9 @@ class StreamingTranscriber {
|
|
|
1175
1567
|
if (this.token) {
|
|
1176
1568
|
searchParams.set("token", this.token);
|
|
1177
1569
|
}
|
|
1178
|
-
|
|
1570
|
+
if (this.params.sampleRate !== undefined) {
|
|
1571
|
+
searchParams.set("sample_rate", this.params.sampleRate.toString());
|
|
1572
|
+
}
|
|
1179
1573
|
if (this.params.endOfTurnConfidenceThreshold) {
|
|
1180
1574
|
searchParams.set("end_of_turn_confidence_threshold", this.params.endOfTurnConfidenceThreshold.toString());
|
|
1181
1575
|
}
|
|
@@ -2121,6 +2515,7 @@ function float32ToPcm16(input) {
|
|
|
2121
2515
|
|
|
2122
2516
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
2123
2517
|
const defaultStreamingUrl = "https://streaming.assemblyai.com";
|
|
2518
|
+
const defaultSyncUrl = "https://sync.assemblyai.com";
|
|
2124
2519
|
class AssemblyAI {
|
|
2125
2520
|
/**
|
|
2126
2521
|
* Create a new AssemblyAI client.
|
|
@@ -2136,6 +2531,11 @@ class AssemblyAI {
|
|
|
2136
2531
|
this.lemur = new LemurService(params);
|
|
2137
2532
|
this.realtime = new RealtimeTranscriberFactory(params);
|
|
2138
2533
|
this.streaming = new StreamingTranscriberFactory(Object.assign(Object.assign({}, params), { baseUrl: params.streamingBaseUrl || defaultStreamingUrl }));
|
|
2534
|
+
let syncBaseUrl = params.syncBaseUrl || defaultSyncUrl;
|
|
2535
|
+
if (syncBaseUrl.endsWith("/")) {
|
|
2536
|
+
syncBaseUrl = syncBaseUrl.slice(0, -1);
|
|
2537
|
+
}
|
|
2538
|
+
this.sync = new SyncTranscriber(Object.assign(Object.assign({}, params), { baseUrl: syncBaseUrl }));
|
|
2139
2539
|
}
|
|
2140
2540
|
}
|
|
2141
2541
|
|
|
@@ -2151,9 +2551,12 @@ exports.RealtimeServiceFactory = RealtimeServiceFactory;
|
|
|
2151
2551
|
exports.RealtimeTranscriber = RealtimeTranscriber;
|
|
2152
2552
|
exports.RealtimeTranscriberFactory = RealtimeTranscriberFactory;
|
|
2153
2553
|
exports.StreamingTranscriber = StreamingTranscriber;
|
|
2554
|
+
exports.SyncTranscriber = SyncTranscriber;
|
|
2555
|
+
exports.SyncTranscriptError = SyncTranscriptError;
|
|
2154
2556
|
exports.TranscriptService = TranscriptService;
|
|
2155
2557
|
exports.VadTimeline = VadTimeline;
|
|
2156
2558
|
exports.attributeTurn = attributeTurn;
|
|
2157
2559
|
exports.attributeWord = attributeWord;
|
|
2560
|
+
exports.defaultSyncSpeechModel = defaultSyncSpeechModel;
|
|
2158
2561
|
exports.float32ToPcm16 = float32ToPcm16;
|
|
2159
2562
|
exports.rollUpTurnChannel = rollUpTurnChannel;
|