assemblyai 4.33.3 → 4.34.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.
Files changed (38) hide show
  1. package/dist/assemblyai.streaming.umd.js +1279 -3
  2. package/dist/assemblyai.streaming.umd.min.js +1 -1
  3. package/dist/assemblyai.umd.js +786 -3
  4. package/dist/assemblyai.umd.min.js +1 -1
  5. package/dist/browser.mjs +762 -4
  6. package/dist/bun.mjs +762 -4
  7. package/dist/deno.mjs +762 -4
  8. package/dist/exports/streaming.d.ts +7 -0
  9. package/dist/index.cjs +786 -3
  10. package/dist/index.mjs +778 -4
  11. package/dist/node.cjs +770 -3
  12. package/dist/node.mjs +762 -4
  13. package/dist/services/index.d.ts +2 -2
  14. package/dist/services/streaming/browser/dual-channel-capture.d.ts +66 -0
  15. package/dist/services/streaming/browser/worklets/pcm16-encoder.d.ts +19 -0
  16. package/dist/services/streaming/energy-vad.d.ts +35 -0
  17. package/dist/services/streaming/index.d.ts +4 -0
  18. package/dist/services/streaming/label-mapper.d.ts +44 -0
  19. package/dist/services/streaming/resampler.d.ts +22 -0
  20. package/dist/services/streaming/service.d.ts +69 -1
  21. package/dist/streaming.browser.mjs +1235 -4
  22. package/dist/streaming.cjs +1275 -3
  23. package/dist/streaming.mjs +1264 -4
  24. package/dist/types/streaming/dual-channel.d.ts +48 -0
  25. package/dist/types/streaming/index.d.ts +110 -1
  26. package/dist/workerd.mjs +762 -4
  27. package/package.json +1 -1
  28. package/src/exports/streaming.ts +7 -0
  29. package/src/services/index.ts +20 -1
  30. package/src/services/streaming/browser/dual-channel-capture.ts +177 -0
  31. package/src/services/streaming/browser/worklets/pcm16-encoder.ts +70 -0
  32. package/src/services/streaming/energy-vad.ts +75 -0
  33. package/src/services/streaming/index.ts +4 -0
  34. package/src/services/streaming/label-mapper.ts +128 -0
  35. package/src/services/streaming/resampler.ts +69 -0
  36. package/src/services/streaming/service.ts +385 -2
  37. package/src/types/streaming/dual-channel.ts +57 -0
  38. package/src/types/streaming/index.ts +110 -0
@@ -2,6 +2,19 @@
2
2
 
3
3
  var ws = require('ws');
4
4
 
5
+ /**
6
+ * Thrown when `DualChannelCapture` is constructed in a non-browser environment
7
+ * (no `globalThis.AudioContext`). The helper is intentionally surfaced from the
8
+ * main entrypoint so the import path is uniform across runtimes; the runtime
9
+ * guard moves to construction time.
10
+ */
11
+ class BrowserOnlyError extends Error {
12
+ constructor(message = "DualChannelCapture requires a browser environment (AudioContext is undefined).") {
13
+ super(message);
14
+ this.name = "BrowserOnlyError";
15
+ }
16
+ }
17
+
5
18
  /******************************************************************************
6
19
  Copyright (c) Microsoft Corporation.
7
20
 
@@ -19,6 +32,18 @@ PERFORMANCE OF THIS SOFTWARE.
19
32
  /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
20
33
 
21
34
 
35
+ function __rest(s, e) {
36
+ var t = {};
37
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
38
+ t[p] = s[p];
39
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
40
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
41
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
42
+ t[p[i]] = s[p[i]];
43
+ }
44
+ return t;
45
+ }
46
+
22
47
  function __awaiter(thisArg, _arguments, P, generator) {
23
48
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
49
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -97,9 +122,58 @@ const RealtimeErrorMessages = {
97
122
  class RealtimeError extends Error {
98
123
  }
99
124
 
125
+ const StreamingErrorType = {
126
+ BadSampleRate: 4000,
127
+ AuthFailed: 4001,
128
+ InsufficientFunds: 4002,
129
+ FreeTierUser: 4003,
130
+ NonexistentSessionId: 4004,
131
+ SessionExpired: 4008,
132
+ ClosedSession: 4010,
133
+ RateLimited: 4029,
134
+ UniqueSessionViolation: 4030,
135
+ SessionTimeout: 4031,
136
+ AudioTooShort: 4032,
137
+ AudioTooLong: 4033,
138
+ AudioTooSmallToTranscode: 4034,
139
+ BadSchema: 4101,
140
+ TooManyStreams: 4102,
141
+ Reconnected: 4103,
142
+ ServerError: 3005,
143
+ InputValidationError: 3006,
144
+ AudioChunkDurationViolation: 3007,
145
+ MaxSessionDurationExceeded: 3008,
146
+ ConcurrencyLimitExceeded: 3009,
147
+ };
148
+ const StreamingErrorMessages = {
149
+ [StreamingErrorType.ServerError]: "Server error",
150
+ [StreamingErrorType.InputValidationError]: "Input validation error",
151
+ [StreamingErrorType.AudioChunkDurationViolation]: "Audio chunk duration violation",
152
+ [StreamingErrorType.MaxSessionDurationExceeded]: "Session expired: maximum session duration exceeded",
153
+ [StreamingErrorType.ConcurrencyLimitExceeded]: "Too many concurrent sessions",
154
+ [StreamingErrorType.BadSampleRate]: "Sample rate must be a positive integer",
155
+ [StreamingErrorType.AuthFailed]: "Not Authorized",
156
+ [StreamingErrorType.InsufficientFunds]: "Insufficient funds",
157
+ [StreamingErrorType.FreeTierUser]: "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.",
158
+ [StreamingErrorType.NonexistentSessionId]: "Session ID does not exist",
159
+ [StreamingErrorType.SessionExpired]: "Session has expired",
160
+ [StreamingErrorType.ClosedSession]: "Session is closed",
161
+ [StreamingErrorType.RateLimited]: "Rate limited",
162
+ [StreamingErrorType.UniqueSessionViolation]: "Unique session violation",
163
+ [StreamingErrorType.SessionTimeout]: "Session Timeout",
164
+ [StreamingErrorType.AudioTooShort]: "Audio too short",
165
+ [StreamingErrorType.AudioTooLong]: "Audio too long",
166
+ [StreamingErrorType.AudioTooSmallToTranscode]: "Audio too small to transcode",
167
+ [StreamingErrorType.BadSchema]: "Bad schema",
168
+ [StreamingErrorType.TooManyStreams]: "Too many streams",
169
+ [StreamingErrorType.Reconnected]: "This session has been reconnected. This WebSocket is no longer valid.",
170
+ };
171
+ class StreamingError extends Error {
172
+ }
173
+
100
174
  const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
101
175
  const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
102
- const terminateSessionMessage = `{"terminate_session":true}`;
176
+ const terminateSessionMessage$1 = `{"terminate_session":true}`;
103
177
  /**
104
178
  * RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
105
179
  */
@@ -294,11 +368,11 @@ class RealtimeTranscriber {
294
368
  const sessionTerminatedPromise = new Promise((resolve) => {
295
369
  this.sessionTerminatedResolve = resolve;
296
370
  });
297
- this.socket.send(terminateSessionMessage);
371
+ this.socket.send(terminateSessionMessage$1);
298
372
  yield sessionTerminatedPromise;
299
373
  }
300
374
  else {
301
- this.socket.send(terminateSessionMessage);
375
+ this.socket.send(terminateSessionMessage$1);
302
376
  }
303
377
  }
304
378
  if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners)
@@ -316,5 +390,1203 @@ class RealtimeTranscriber {
316
390
  class RealtimeService extends RealtimeTranscriber {
317
391
  }
318
392
 
393
+ /**
394
+ * Energy-based VAD with adaptive noise-floor tracking and hangover. Pure JS,
395
+ * no dependencies. Suitable for the "which physical channel is speaking" task
396
+ * because the channels are already physically separated at capture — the harder
397
+ * problem (speech vs. non-speech in the wild) is one a customer can swap in a
398
+ * DNN VAD for via the `createVad` parameter.
399
+ *
400
+ * Tuning notes:
401
+ * - thresholdRatio below 2 will treat anything above noise as speech (too sensitive).
402
+ * - thresholdRatio above 6 will miss quiet utterance onsets/offsets.
403
+ * - noiseFloorAlpha above 0.1 makes the floor track quickly (good for non-stationary
404
+ * background) but risks slowly adapting *up* to a sustained low voice.
405
+ */
406
+ class EnergyVad {
407
+ constructor(params = {}) {
408
+ var _a, _b, _c, _d;
409
+ this.hangoverRemaining = 0;
410
+ this.thresholdRatio = (_a = params.thresholdRatio) !== null && _a !== void 0 ? _a : 3.0;
411
+ this.noiseFloorAlpha = (_b = params.noiseFloorAlpha) !== null && _b !== void 0 ? _b : 0.05;
412
+ this.hangoverFrames = (_c = params.hangoverFrames) !== null && _c !== void 0 ? _c : 10;
413
+ this.initialNoiseFloor = (_d = params.initialNoiseFloor) !== null && _d !== void 0 ? _d : 1e-4;
414
+ this.noiseFloor = this.initialNoiseFloor;
415
+ }
416
+ process(frame) {
417
+ let sumSq = 0;
418
+ for (let i = 0; i < frame.length; i++) {
419
+ sumSq += frame[i] * frame[i];
420
+ }
421
+ const rms = frame.length > 0 ? Math.sqrt(sumSq / frame.length) : 0;
422
+ const threshold = this.noiseFloor * this.thresholdRatio;
423
+ let active = rms > threshold;
424
+ if (active) {
425
+ this.hangoverRemaining = this.hangoverFrames;
426
+ }
427
+ else if (this.hangoverRemaining > 0) {
428
+ this.hangoverRemaining--;
429
+ active = true;
430
+ // While in hangover, do not update noise floor — RMS may still reflect tail energy.
431
+ }
432
+ else {
433
+ this.noiseFloor =
434
+ this.noiseFloor * (1 - this.noiseFloorAlpha) +
435
+ rms * this.noiseFloorAlpha;
436
+ }
437
+ return { active, energy: rms };
438
+ }
439
+ reset() {
440
+ this.noiseFloor = this.initialNoiseFloor;
441
+ this.hangoverRemaining = 0;
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Append-only ring buffer of VAD frames in stream-relative ms order.
447
+ * `pushFrame` is O(1) amortized; `framesInWindow` is O(n) over kept frames,
448
+ * which is fine for the per-word lookups we do (a 30 s window at 50 frames/s
449
+ * per channel × 2 channels = 3000 entries, scanned once per word).
450
+ *
451
+ * Runtime-agnostic — no DOM or Web Audio dependencies.
452
+ */
453
+ class VadTimeline {
454
+ constructor(windowMs) {
455
+ this.windowMs = windowMs;
456
+ this.frames = [];
457
+ this.head = 0;
458
+ }
459
+ pushFrame(frame) {
460
+ this.frames.push(frame);
461
+ const cutoff = frame.ts - this.windowMs;
462
+ while (this.head < this.frames.length &&
463
+ this.frames[this.head].ts < cutoff) {
464
+ this.head++;
465
+ }
466
+ if (this.head > 1024 && this.head * 2 > this.frames.length) {
467
+ this.frames = this.frames.slice(this.head);
468
+ this.head = 0;
469
+ }
470
+ }
471
+ framesInWindow(startMs, endMs) {
472
+ const out = [];
473
+ for (let i = this.head; i < this.frames.length; i++) {
474
+ const f = this.frames[i];
475
+ if (f.ts < startMs)
476
+ continue;
477
+ if (f.ts > endMs)
478
+ break;
479
+ out.push(f);
480
+ }
481
+ return out;
482
+ }
483
+ clear() {
484
+ this.frames = [];
485
+ this.head = 0;
486
+ }
487
+ }
488
+ /**
489
+ * Sum per-channel active RMS over a window. Returns a Map from channel name
490
+ * to total score. Channels with zero score are omitted.
491
+ */
492
+ function scoreChannels(frames) {
493
+ var _a;
494
+ const scores = new Map();
495
+ for (const f of frames) {
496
+ if (!f.active)
497
+ continue;
498
+ scores.set(f.channel, ((_a = scores.get(f.channel)) !== null && _a !== void 0 ? _a : 0) + f.rms);
499
+ }
500
+ return scores;
501
+ }
502
+ /**
503
+ * Decide which channel was dominant during a word's `[start, end]` window.
504
+ *
505
+ * - If no channel has any active VAD energy → `"unknown"`.
506
+ * - If the top channel beats the runner-up by at least `dominanceRatio` → top channel.
507
+ * - Else: top channel wins on absolute score; exact ties → `"unknown"`.
508
+ */
509
+ function attributeWord(word, timeline, params) {
510
+ const scores = scoreChannels(timeline.framesInWindow(word.start, word.end));
511
+ if (scores.size === 0)
512
+ return "unknown";
513
+ const sorted = [...scores.entries()].sort((a, b) => b[1] - a[1]);
514
+ if (sorted.length === 1)
515
+ return sorted[0][0];
516
+ const [topName, topScore] = sorted[0];
517
+ const [runnerName, runnerScore] = sorted[1];
518
+ if (topScore >= params.dominanceRatio * runnerScore)
519
+ return topName;
520
+ if (topScore > runnerScore)
521
+ return topName;
522
+ if (runnerScore > topScore)
523
+ return runnerName;
524
+ return "unknown";
525
+ }
526
+ /**
527
+ * Duration-weighted majority of word channels. `"unknown"` if there are no
528
+ * words, every word resolved to `"unknown"`, or two channels tie exactly.
529
+ */
530
+ function rollUpTurnChannel(words) {
531
+ var _a;
532
+ const totals = new Map();
533
+ for (const w of words) {
534
+ if (!w.channel || w.channel === "unknown")
535
+ continue;
536
+ const dur = Math.max(0, w.end - w.start);
537
+ totals.set(w.channel, ((_a = totals.get(w.channel)) !== null && _a !== void 0 ? _a : 0) + dur);
538
+ }
539
+ if (totals.size === 0)
540
+ return "unknown";
541
+ const sorted = [...totals.entries()].sort((a, b) => b[1] - a[1]);
542
+ if (sorted.length === 1)
543
+ return sorted[0][0];
544
+ const [topName, topMs] = sorted[0];
545
+ const [, runnerMs] = sorted[1];
546
+ if (topMs === runnerMs)
547
+ return "unknown";
548
+ return topName;
549
+ }
550
+ /**
551
+ * Mutate `turn` in place: write `turn.words[i].channel` for every word and set
552
+ * `turn.channel` to the duration-weighted rollup.
553
+ *
554
+ * Returns `void` because the transcriber owns the `TurnEvent` ref and forwards
555
+ * the same object to the customer listener — no need to allocate a copy.
556
+ */
557
+ function attributeTurn(turn, timeline, params) {
558
+ for (const w of turn.words) {
559
+ w.channel = attributeWord(w, timeline, params);
560
+ }
561
+ turn.channel = rollUpTurnChannel(turn.words);
562
+ }
563
+
564
+ /**
565
+ * View any `AudioData` (ArrayBuffer / ArrayBufferView / typed array) as a
566
+ * little-endian Int16 sample sequence without copying. Callers must guarantee
567
+ * the underlying byte length is even.
568
+ */
569
+ function toInt16View(audio) {
570
+ // AudioData is ArrayBufferLike per the public type, but in practice callers
571
+ // pass ArrayBuffer or a typed-array view. Handle both without copying.
572
+ if (audio instanceof Int16Array)
573
+ return audio;
574
+ if (ArrayBuffer.isView(audio)) {
575
+ const view = audio;
576
+ return new Int16Array(view.buffer, view.byteOffset, Math.floor(view.byteLength / 2));
577
+ }
578
+ return new Int16Array(audio);
579
+ }
580
+ const defaultStreamingUrl = "wss://streaming.assemblyai.com/v3/ws";
581
+ const terminateSessionMessage = `{"type":"Terminate"}`;
582
+ /**
583
+ * Per-send chunk cap in milliseconds for the dual-channel mixer. The streaming
584
+ * server rejects audio messages longer than 1000 ms (`Input Duration Error`).
585
+ * If a backlog accumulates (e.g. when a browser tab is backgrounded and
586
+ * `setInterval` is throttled to ~1 Hz), `flushMix` loops and emits multiple
587
+ * sends each ≤ this cap until the buffers drain.
588
+ */
589
+ const MAX_CHUNK_MS = 200;
590
+ /**
591
+ * Per-send minimum chunk size in milliseconds. The streaming server also
592
+ * rejects audio messages shorter than 50 ms with the same
593
+ * `Input Duration Error`, so the mixer waits until both per-channel buffers
594
+ * have at least this much accumulated before emitting. Final-flush (close
595
+ * path) bypasses this floor so the trailing partial buffer still gets sent.
596
+ */
597
+ const MIN_CHUNK_MS = 50;
598
+ class StreamingTranscriber {
599
+ constructor(params) {
600
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
601
+ this.listeners = {};
602
+ // Dual-channel mode state (allocated only when params.channels is set).
603
+ this.isDualChannel = false;
604
+ this.vadFrameSamples = 0;
605
+ this.minChunkSamples = 0;
606
+ this.maxChunkSamples = 0;
607
+ this.params = Object.assign(Object.assign({}, params), { websocketBaseUrl: params.websocketBaseUrl || defaultStreamingUrl });
608
+ if ("token" in params && params.token)
609
+ this.token = params.token;
610
+ if ("apiKey" in params && params.apiKey)
611
+ this.apiKey = params.apiKey;
612
+ if (!(this.token || this.apiKey)) {
613
+ throw new Error("API key or temporary token is required.");
614
+ }
615
+ if (params.channels) {
616
+ if (params.channels.length !== 2) {
617
+ throw new Error("StreamingTranscriber.channels must have exactly 2 entries.");
618
+ }
619
+ const names = params.channels.map((c) => c.name);
620
+ if (new Set(names).size !== names.length) {
621
+ throw new Error("StreamingTranscriber.channels names must be unique.");
622
+ }
623
+ this.isDualChannel = true;
624
+ this.channelNames = names;
625
+ const att = (_a = params.channelAttribution) !== null && _a !== void 0 ? _a : {};
626
+ this.attributionParams = {
627
+ dominanceRatio: (_b = att.dominanceRatio) !== null && _b !== void 0 ? _b : 4,
628
+ timelineWindowMs: (_c = att.timelineWindowMs) !== null && _c !== void 0 ? _c : 30000,
629
+ createVad: (_d = att.createVad) !== null && _d !== void 0 ? _d : (() => new EnergyVad()),
630
+ flushIntervalMs: (_e = att.flushIntervalMs) !== null && _e !== void 0 ? _e : 50,
631
+ resolveUnknownChannelsMethod: (_f = att.resolveUnknownChannelsMethod) !== null && _f !== void 0 ? _f : "window",
632
+ resolutionWindowWords: (_g = att.resolutionWindowWords) !== null && _g !== void 0 ? _g : 2,
633
+ speakerHistoryMinRmsEvidence: (_h = att.speakerHistoryMinRmsEvidence) !== null && _h !== void 0 ? _h : 0.5,
634
+ speakerHistoryDominanceRatio: (_j = att.speakerHistoryDominanceRatio) !== null && _j !== void 0 ? _j : 3,
635
+ };
636
+ if (this.attributionParams.resolveUnknownChannelsMethod ===
637
+ "speaker-history") {
638
+ this.speakerHistory = new Map();
639
+ }
640
+ // 20 ms VAD frames at the transcriber's target sample rate.
641
+ this.vadFrameSamples = Math.max(1, Math.round(params.sampleRate * 0.02));
642
+ this.minChunkSamples = Math.max(1, Math.round(params.sampleRate * (MIN_CHUNK_MS / 1000)));
643
+ this.maxChunkSamples = Math.max(this.minChunkSamples, Math.round(params.sampleRate * (MAX_CHUNK_MS / 1000)));
644
+ this.channelBuffers = new Map(names.map((n) => [n, []]));
645
+ this.channelSamplesReceived = new Map(names.map((n) => [n, 0]));
646
+ this.channelVadFloatBuffers = new Map(names.map((n) => [n, new Float32Array(this.vadFrameSamples)]));
647
+ this.channelVadBufferIdx = new Map(names.map((n) => [n, 0]));
648
+ this.channelVads = new Map(names.map((n) => [n, this.attributionParams.createVad(n)]));
649
+ this.timeline = new VadTimeline(this.attributionParams.timelineWindowMs);
650
+ }
651
+ }
652
+ connectionUrl() {
653
+ var _a, _b;
654
+ const url = new URL((_a = this.params.websocketBaseUrl) !== null && _a !== void 0 ? _a : "");
655
+ if (url.protocol !== "wss:") {
656
+ throw new Error("Invalid protocol, must be wss");
657
+ }
658
+ const searchParams = new URLSearchParams();
659
+ if (this.token) {
660
+ searchParams.set("token", this.token);
661
+ }
662
+ searchParams.set("sample_rate", this.params.sampleRate.toString());
663
+ if (this.params.endOfTurnConfidenceThreshold) {
664
+ searchParams.set("end_of_turn_confidence_threshold", this.params.endOfTurnConfidenceThreshold.toString());
665
+ }
666
+ if (this.params.minEndOfTurnSilenceWhenConfident !== undefined) {
667
+ if (this.params.minTurnSilence !== undefined) {
668
+ console.warn("[Deprecation Warning] Both `minEndOfTurnSilenceWhenConfident` and `minTurnSilence` are set. Using `minTurnSilence`; `minEndOfTurnSilenceWhenConfident` is deprecated.");
669
+ }
670
+ else {
671
+ console.warn("[Deprecation Warning] `minEndOfTurnSilenceWhenConfident` is deprecated and will be removed in a future release. Please use `minTurnSilence` instead.");
672
+ }
673
+ }
674
+ const effectiveMinTurnSilence = (_b = this.params.minTurnSilence) !== null && _b !== void 0 ? _b : this.params.minEndOfTurnSilenceWhenConfident;
675
+ if (effectiveMinTurnSilence !== undefined) {
676
+ searchParams.set("min_turn_silence", effectiveMinTurnSilence.toString());
677
+ }
678
+ if (this.params.maxTurnSilence) {
679
+ searchParams.set("max_turn_silence", this.params.maxTurnSilence.toString());
680
+ }
681
+ if (this.params.vadThreshold !== undefined) {
682
+ searchParams.set("vad_threshold", this.params.vadThreshold.toString());
683
+ }
684
+ if (this.params.formatTurns) {
685
+ searchParams.set("format_turns", this.params.formatTurns.toString());
686
+ }
687
+ if (this.params.encoding) {
688
+ searchParams.set("encoding", this.params.encoding.toString());
689
+ }
690
+ if (this.params.keytermsPrompt) {
691
+ searchParams.set("keyterms_prompt", JSON.stringify(this.params.keytermsPrompt));
692
+ }
693
+ else if (this.params.keyterms) {
694
+ console.warn("[Deprecation Warning] `keyterms` is deprecated and will be removed in a future release. Please use `keytermsPrompt` instead.");
695
+ searchParams.set("keyterms_prompt", JSON.stringify(this.params.keyterms));
696
+ }
697
+ if (this.params.prompt) {
698
+ searchParams.set("prompt", this.params.prompt);
699
+ }
700
+ if (this.params.filterProfanity) {
701
+ searchParams.set("filter_profanity", this.params.filterProfanity.toString());
702
+ }
703
+ if (this.params.speechModel === "u3-pro") {
704
+ 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.");
705
+ }
706
+ searchParams.set("speech_model", this.params.speechModel.toString());
707
+ if (this.params.languageDetection !== undefined) {
708
+ searchParams.set("language_detection", this.params.languageDetection.toString());
709
+ }
710
+ if (this.params.domain) {
711
+ searchParams.set("domain", this.params.domain);
712
+ }
713
+ if (this.params.inactivityTimeout !== undefined) {
714
+ searchParams.set("inactivity_timeout", this.params.inactivityTimeout.toString());
715
+ }
716
+ if (this.params.speakerLabels !== undefined) {
717
+ searchParams.set("speaker_labels", this.params.speakerLabels.toString());
718
+ }
719
+ if (this.params.maxSpeakers !== undefined) {
720
+ searchParams.set("max_speakers", this.params.maxSpeakers.toString());
721
+ }
722
+ if (this.params.voiceFocus) {
723
+ searchParams.set("voice_focus", this.params.voiceFocus);
724
+ }
725
+ if (this.params.voiceFocusThreshold !== undefined) {
726
+ searchParams.set("voice_focus_threshold", this.params.voiceFocusThreshold.toString());
727
+ }
728
+ if (this.params.continuousPartials !== undefined) {
729
+ searchParams.set("continuous_partials", this.params.continuousPartials.toString());
730
+ }
731
+ if (this.params.interruptionDelay !== undefined) {
732
+ searchParams.set("interruption_delay", this.params.interruptionDelay.toString());
733
+ }
734
+ if (this.params.turnLeftPadMs !== undefined) {
735
+ searchParams.set("turn_left_pad_ms", this.params.turnLeftPadMs.toString());
736
+ }
737
+ if (this.params.customerSupportAudioCapture) {
738
+ console.warn("`customerSupportAudioCapture=true` will record session audio. Only enable this when explicitly coordinating with AssemblyAI support.");
739
+ // The server's canonical wire name is `_customer_support_audio_capture`
740
+ // (leading underscore = "not officially supported / unstable"). The
741
+ // server also accepts `customer_support_audio_capture` via
742
+ // `populate_by_name=True`, but we send the underscore form to honor
743
+ // the server's stability marker.
744
+ searchParams.set("_customer_support_audio_capture", this.params.customerSupportAudioCapture.toString());
745
+ }
746
+ if (this.params.webhookUrl) {
747
+ searchParams.set("webhook_url", this.params.webhookUrl);
748
+ }
749
+ if (this.params.webhookAuthHeaderName) {
750
+ searchParams.set("webhook_auth_header_name", this.params.webhookAuthHeaderName);
751
+ }
752
+ if (this.params.webhookAuthHeaderValue) {
753
+ searchParams.set("webhook_auth_header_value", this.params.webhookAuthHeaderValue);
754
+ }
755
+ if (this.params.includePartialTurns !== undefined) {
756
+ searchParams.set("include_partial_turns", this.params.includePartialTurns.toString());
757
+ }
758
+ if (this.params.redactPii !== undefined) {
759
+ searchParams.set("redact_pii", this.params.redactPii.toString());
760
+ }
761
+ if (this.params.redactPiiPolicies !== undefined) {
762
+ searchParams.set("redact_pii_policies", JSON.stringify(this.params.redactPiiPolicies));
763
+ }
764
+ if (this.params.redactPiiSub !== undefined) {
765
+ searchParams.set("redact_pii_sub", this.params.redactPiiSub);
766
+ }
767
+ if (this.params.llmGateway !== undefined) {
768
+ searchParams.set("llm_gateway", JSON.stringify(this.params.llmGateway));
769
+ }
770
+ url.search = searchParams.toString();
771
+ return url;
772
+ }
773
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
774
+ on(event, listener) {
775
+ this.listeners[event] = listener;
776
+ }
777
+ connect() {
778
+ return new Promise((resolve) => {
779
+ if (this.socket) {
780
+ throw new Error("Already connected");
781
+ }
782
+ const url = this.connectionUrl();
783
+ if (this.token) {
784
+ this.socket = factory(url.toString());
785
+ }
786
+ else {
787
+ this.socket = factory(url.toString(), {
788
+ headers: { Authorization: this.apiKey },
789
+ });
790
+ }
791
+ this.socket.binaryType = "arraybuffer";
792
+ this.socket.onopen = () => { };
793
+ this.socket.onclose = ({ code, reason }) => {
794
+ var _a, _b;
795
+ if (!reason) {
796
+ if (code in StreamingErrorMessages) {
797
+ reason = StreamingErrorMessages[code];
798
+ }
799
+ }
800
+ // Stop the flush timer when the socket is gone (server-initiated close,
801
+ // network drop, etc.) — otherwise subsequent ticks call send() on a
802
+ // closed socket and spam the error listener.
803
+ if (this.flushTimer) {
804
+ clearInterval(this.flushTimer);
805
+ this.flushTimer = undefined;
806
+ }
807
+ (_b = (_a = this.listeners).close) === null || _b === void 0 ? void 0 : _b.call(_a, code, reason);
808
+ };
809
+ this.socket.onerror = (event) => {
810
+ var _a, _b, _c, _d;
811
+ if (event.error)
812
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, event.error);
813
+ else
814
+ (_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(event.message));
815
+ };
816
+ this.socket.onmessage = ({ data }) => {
817
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
818
+ const message = JSON.parse(data.toString());
819
+ if ("error" in message) {
820
+ const err = new StreamingError(message.error);
821
+ if ("error_code" in message) {
822
+ err.code =
823
+ message.error_code;
824
+ }
825
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, err);
826
+ return;
827
+ }
828
+ switch (message.type) {
829
+ case "Begin": {
830
+ resolve(message);
831
+ (_d = (_c = this.listeners).open) === null || _d === void 0 ? void 0 : _d.call(_c, message);
832
+ break;
833
+ }
834
+ case "Turn": {
835
+ if (this.isDualChannel && this.timeline && this.attributionParams) {
836
+ attributeTurn(message, this.timeline, {
837
+ dominanceRatio: this.attributionParams.dominanceRatio,
838
+ });
839
+ switch (this.attributionParams.resolveUnknownChannelsMethod) {
840
+ case "window":
841
+ this.resolveUnknownChannelsByWindow(message);
842
+ break;
843
+ case "speaker-history":
844
+ this.resolveUnknownChannelsBySpeakerHistory(message);
845
+ break;
846
+ }
847
+ }
848
+ (_f = (_e = this.listeners).turn) === null || _f === void 0 ? void 0 : _f.call(_e, message);
849
+ break;
850
+ }
851
+ case "SpeechStarted": {
852
+ (_h = (_g = this.listeners).speechStarted) === null || _h === void 0 ? void 0 : _h.call(_g, message);
853
+ break;
854
+ }
855
+ case "LLMGatewayResponse": {
856
+ (_k = (_j = this.listeners).llmGatewayResponse) === null || _k === void 0 ? void 0 : _k.call(_j, message);
857
+ break;
858
+ }
859
+ case "Warning": {
860
+ const warning = message;
861
+ console.warn(`Streaming warning (code=${warning.warning_code}): ${warning.warning}`);
862
+ (_m = (_l = this.listeners).warning) === null || _m === void 0 ? void 0 : _m.call(_l, warning);
863
+ break;
864
+ }
865
+ case "Termination": {
866
+ (_o = this.sessionTerminatedResolve) === null || _o === void 0 ? void 0 : _o.call(this);
867
+ break;
868
+ }
869
+ }
870
+ };
871
+ });
872
+ }
873
+ /**
874
+ * Returns a WritableStream that pumps PCM chunks into `sendAudio`. Single-channel
875
+ * only — in dual-channel mode use `sendAudio(pcm, { channel })` directly, since
876
+ * `WritableStream` has no place to carry a channel tag.
877
+ */
878
+ stream() {
879
+ return new WritableStream({
880
+ write: (chunk) => {
881
+ this.sendAudio(chunk);
882
+ },
883
+ });
884
+ }
885
+ /**
886
+ * Send PCM audio.
887
+ *
888
+ * In single-channel mode, `audio` is forwarded directly to the WebSocket and
889
+ * `options` is ignored.
890
+ *
891
+ * In dual-channel mode (when `channels` is configured), `options.channel` is
892
+ * REQUIRED and must match one of the declared channel names. Per-channel PCM is
893
+ * fed into that channel's VAD, accumulated into a per-channel ring buffer, and
894
+ * a scheduled flush (`channelAttribution.flushIntervalMs`, default 50ms) mixes
895
+ * the buffers into mono before sending to the WebSocket.
896
+ */
897
+ sendAudio(audio, options) {
898
+ if (!this.isDualChannel) {
899
+ this.send(audio);
900
+ return;
901
+ }
902
+ if (!(options === null || options === void 0 ? void 0 : options.channel)) {
903
+ throw new Error("StreamingTranscriber is in dual-channel mode; sendAudio requires { channel }.");
904
+ }
905
+ if (!this.channelNames.includes(options.channel)) {
906
+ throw new Error(`Unknown channel "${options.channel}"; declared channels: ${this.channelNames.join(", ")}.`);
907
+ }
908
+ this.ingestChannelAudio(options.channel, audio);
909
+ }
910
+ ingestChannelAudio(name, audio) {
911
+ var _a, _b;
912
+ const samples = toInt16View(audio);
913
+ const buf = this.channelBuffers.get(name);
914
+ const vadBuf = this.channelVadFloatBuffers.get(name);
915
+ let vadIdx = this.channelVadBufferIdx.get(name);
916
+ let received = this.channelSamplesReceived.get(name);
917
+ const vad = this.channelVads.get(name);
918
+ const sampleRate = this.params.sampleRate;
919
+ const frameSize = this.vadFrameSamples;
920
+ for (let i = 0; i < samples.length; i++) {
921
+ const s = samples[i];
922
+ buf.push(s);
923
+ vadBuf[vadIdx++] = s / 0x8000;
924
+ received++;
925
+ if (vadIdx === frameSize) {
926
+ const result = vad.process(vadBuf);
927
+ const frame = {
928
+ ts: (received / sampleRate) * 1000,
929
+ channel: name,
930
+ active: result.active,
931
+ rms: result.energy,
932
+ };
933
+ this.timeline.pushFrame(frame);
934
+ (_b = (_a = this.listeners).vad) === null || _b === void 0 ? void 0 : _b.call(_a, frame);
935
+ vadIdx = 0;
936
+ }
937
+ }
938
+ this.channelVadBufferIdx.set(name, vadIdx);
939
+ this.channelSamplesReceived.set(name, received);
940
+ if (!this.flushTimer)
941
+ this.startFlushTimer();
942
+ }
943
+ startFlushTimer() {
944
+ this.flushTimer = setInterval(() => this.flushMix(), this.attributionParams.flushIntervalMs);
945
+ }
946
+ flushMix(force = false) {
947
+ var _a, _b;
948
+ if (!this.channelNames || !this.channelBuffers)
949
+ return;
950
+ const bufs = this.channelNames.map((n) => this.channelBuffers.get(n));
951
+ const divisor = bufs.length;
952
+ // Loop so a backlog (e.g. accumulated while a browser tab was throttled in
953
+ // the background) drains as multiple sends, each capped at MAX_CHUNK_MS.
954
+ // Without the cap a single message could exceed the server's 1000 ms input
955
+ // duration limit and be rejected with code 3007.
956
+ for (;;) {
957
+ let mixLen = Infinity;
958
+ for (const b of bufs)
959
+ if (b.length < mixLen)
960
+ mixLen = b.length;
961
+ if (!Number.isFinite(mixLen) || mixLen === 0)
962
+ return;
963
+ // The streaming server rejects audio messages shorter than 50 ms with
964
+ // `Input Duration Error`. Wait until both per-channel buffers have at
965
+ // least minChunkSamples worth queued before emitting. The `force` path
966
+ // (final flush on close) bypasses this so the trailing partial buffer
967
+ // still gets through.
968
+ if (!force && mixLen < this.minChunkSamples)
969
+ return;
970
+ if (mixLen > this.maxChunkSamples)
971
+ mixLen = this.maxChunkSamples;
972
+ const out = new Int16Array(mixLen);
973
+ for (let i = 0; i < mixLen; i++) {
974
+ let sum = 0;
975
+ for (let c = 0; c < divisor; c++)
976
+ sum += bufs[c][i];
977
+ const avg = Math.round(sum / divisor);
978
+ out[i] = avg < -32768 ? -32768 : avg > 32767 ? 32767 : avg;
979
+ }
980
+ for (const b of bufs)
981
+ b.splice(0, mixLen);
982
+ try {
983
+ this.send(out.buffer);
984
+ }
985
+ catch (err) {
986
+ (_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, err);
987
+ return;
988
+ }
989
+ }
990
+ }
991
+ /**
992
+ * Fill in words whose per-word VAD attribution was `"unknown"` by looking
993
+ * at the dominant non-`"unknown"` channel among ±N neighbors in the same
994
+ * turn. Words with no non-`"unknown"` neighbors stay `"unknown"`. Confident
995
+ * per-word VAD decisions are never modified.
996
+ *
997
+ * Local temporal heuristic — ignores `speaker_label`, so it works even when
998
+ * AAI's diarization re-uses the same label for two physically distinct
999
+ * voices. Each resolved word gets `channelResolved: true` so downstream
1000
+ * renderers can distinguish inferred channels from directly-measured ones.
1001
+ */
1002
+ resolveUnknownChannelsByWindow(turn) {
1003
+ var _a;
1004
+ if (!this.attributionParams)
1005
+ return;
1006
+ const window = this.attributionParams.resolutionWindowWords;
1007
+ const words = turn.words;
1008
+ let mutated = false;
1009
+ for (let i = 0; i < words.length; i++) {
1010
+ if (words[i].channel !== "unknown")
1011
+ continue;
1012
+ const tally = new Map();
1013
+ const lo = Math.max(0, i - window);
1014
+ const hi = Math.min(words.length - 1, i + window);
1015
+ for (let j = lo; j <= hi; j++) {
1016
+ if (j === i)
1017
+ continue;
1018
+ const ch = words[j].channel;
1019
+ if (!ch || ch === "unknown")
1020
+ continue;
1021
+ tally.set(ch, ((_a = tally.get(ch)) !== null && _a !== void 0 ? _a : 0) + 1);
1022
+ }
1023
+ if (tally.size === 0)
1024
+ continue;
1025
+ // Pick the dominant neighbor channel. Ties → leave `"unknown"` (rare;
1026
+ // would require an equal count of mic and system neighbors).
1027
+ let top;
1028
+ let topCount = 0;
1029
+ let tied = false;
1030
+ for (const [name, count] of tally) {
1031
+ if (count > topCount) {
1032
+ top = name;
1033
+ topCount = count;
1034
+ tied = false;
1035
+ }
1036
+ else if (count === topCount) {
1037
+ tied = true;
1038
+ }
1039
+ }
1040
+ if (top && !tied) {
1041
+ words[i].channel = top;
1042
+ words[i].channelResolved = true;
1043
+ mutated = true;
1044
+ }
1045
+ }
1046
+ // Recompute the rollup only if any per-word channel changed.
1047
+ if (mutated)
1048
+ turn.channel = rollUpTurnChannel(words);
1049
+ }
1050
+ /**
1051
+ * Fill `"unknown"` words by looking up the speaker's session-wide channel
1052
+ * evidence. For each `speaker_label`, sums active VAD frame RMS per channel
1053
+ * across every word the speaker has uttered to date. A speaker is
1054
+ * "resolvable" if their total evidence clears
1055
+ * `speakerHistoryMinRmsEvidence` and their top channel exceeds the
1056
+ * runner-up by `speakerHistoryDominanceRatio`.
1057
+ *
1058
+ * Only touches `"unknown"` words. Confident per-word VAD decisions are
1059
+ * never modified. `speaker_label` is never modified.
1060
+ */
1061
+ resolveUnknownChannelsBySpeakerHistory(turn) {
1062
+ var _a;
1063
+ if (!this.timeline || !this.attributionParams || !this.speakerHistory)
1064
+ return;
1065
+ const minEvidence = this.attributionParams.speakerHistoryMinRmsEvidence;
1066
+ const dominanceRatio = this.attributionParams.speakerHistoryDominanceRatio;
1067
+ // 1. Accumulate evidence from this turn's words.
1068
+ for (const w of turn.words) {
1069
+ if (!w.speaker)
1070
+ continue;
1071
+ const frames = this.timeline.framesInWindow(w.start, w.end);
1072
+ let entry = this.speakerHistory.get(w.speaker);
1073
+ if (!entry) {
1074
+ entry = new Map();
1075
+ this.speakerHistory.set(w.speaker, entry);
1076
+ }
1077
+ for (const f of frames) {
1078
+ if (!f.active)
1079
+ continue;
1080
+ entry.set(f.channel, ((_a = entry.get(f.channel)) !== null && _a !== void 0 ? _a : 0) + f.rms);
1081
+ }
1082
+ }
1083
+ // 2. Fill unknown words whose speakers have dominant evidence.
1084
+ let mutated = false;
1085
+ for (const w of turn.words) {
1086
+ if (w.channel !== "unknown" || !w.speaker)
1087
+ continue;
1088
+ const entry = this.speakerHistory.get(w.speaker);
1089
+ if (!entry || entry.size === 0)
1090
+ continue;
1091
+ let total = 0;
1092
+ let topName;
1093
+ let topScore = 0;
1094
+ let runnerScore = 0;
1095
+ for (const [name, score] of entry) {
1096
+ total += score;
1097
+ if (score > topScore) {
1098
+ runnerScore = topScore;
1099
+ topScore = score;
1100
+ topName = name;
1101
+ }
1102
+ else if (score > runnerScore) {
1103
+ runnerScore = score;
1104
+ }
1105
+ }
1106
+ if (total < minEvidence)
1107
+ continue;
1108
+ if (runnerScore > 0 && topScore < dominanceRatio * runnerScore)
1109
+ continue;
1110
+ if (topName) {
1111
+ w.channel = topName;
1112
+ w.channelResolved = true;
1113
+ mutated = true;
1114
+ }
1115
+ }
1116
+ if (mutated)
1117
+ turn.channel = rollUpTurnChannel(turn.words);
1118
+ }
1119
+ /**
1120
+ * Update the streaming configuration mid-stream.
1121
+ * @param config - The configuration parameters to update
1122
+ */
1123
+ updateConfiguration(config) {
1124
+ const { min_end_of_turn_silence_when_confident, min_turn_silence } = config, rest = __rest(config, ["min_end_of_turn_silence_when_confident", "min_turn_silence"]);
1125
+ if (min_end_of_turn_silence_when_confident !== undefined) {
1126
+ if (min_turn_silence !== undefined) {
1127
+ console.warn("[Deprecation Warning] Both `min_end_of_turn_silence_when_confident` and `min_turn_silence` are set. Using `min_turn_silence`; `min_end_of_turn_silence_when_confident` is deprecated.");
1128
+ }
1129
+ else {
1130
+ console.warn("[Deprecation Warning] `min_end_of_turn_silence_when_confident` is deprecated and will be removed in a future release. Please use `min_turn_silence` instead.");
1131
+ }
1132
+ }
1133
+ const effective = min_turn_silence !== null && min_turn_silence !== void 0 ? min_turn_silence : min_end_of_turn_silence_when_confident;
1134
+ const message = Object.assign(Object.assign({ type: "UpdateConfiguration" }, rest), (effective !== undefined ? { min_turn_silence: effective } : {}));
1135
+ this.send(JSON.stringify(message));
1136
+ }
1137
+ /**
1138
+ * Force the current turn to end immediately.
1139
+ */
1140
+ forceEndpoint() {
1141
+ const message = {
1142
+ type: "ForceEndpoint",
1143
+ };
1144
+ this.send(JSON.stringify(message));
1145
+ }
1146
+ send(data) {
1147
+ if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
1148
+ throw new Error("Socket is not open for communication");
1149
+ }
1150
+ this.socket.send(data);
1151
+ }
1152
+ close() {
1153
+ return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
1154
+ var _a;
1155
+ if (this.flushTimer) {
1156
+ clearInterval(this.flushTimer);
1157
+ this.flushTimer = undefined;
1158
+ // Best-effort: drain any final partial mix so the server gets the tail.
1159
+ // Bypass the 50ms floor here since this is the last flush; if the tail
1160
+ // is <50ms the server will reject that single message, but we'd lose
1161
+ // the audio either way.
1162
+ this.flushMix(true);
1163
+ }
1164
+ if (this.socket) {
1165
+ if (this.socket.readyState === this.socket.OPEN) {
1166
+ if (waitForSessionTermination) {
1167
+ const sessionTerminatedPromise = new Promise((resolve) => {
1168
+ this.sessionTerminatedResolve = resolve;
1169
+ });
1170
+ this.socket.send(terminateSessionMessage);
1171
+ yield sessionTerminatedPromise;
1172
+ }
1173
+ else {
1174
+ this.socket.send(terminateSessionMessage);
1175
+ }
1176
+ }
1177
+ if ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners)
1178
+ this.socket.removeAllListeners();
1179
+ this.socket.close();
1180
+ }
1181
+ this.listeners = {};
1182
+ this.socket = undefined;
1183
+ });
1184
+ }
1185
+ }
1186
+
1187
+ const DEFAULT_FETCH_INIT = {
1188
+ cache: "no-store",
1189
+ };
1190
+
1191
+ const buildUserAgent = (userAgent) => defaultUserAgentString +
1192
+ (userAgent === false
1193
+ ? ""
1194
+ : " AssemblyAI/1.0 (" +
1195
+ Object.entries(Object.assign(Object.assign({}, defaultUserAgent), userAgent))
1196
+ .map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
1197
+ .join(" ") +
1198
+ ")");
1199
+ let defaultUserAgentString = "";
1200
+ if (typeof navigator !== "undefined" && navigator.userAgent) {
1201
+ defaultUserAgentString += navigator.userAgent;
1202
+ }
1203
+ const defaultUserAgent = {
1204
+ sdk: { name: "JavaScript", version: "__SDK_VERSION__" },
1205
+ };
1206
+ if (typeof process !== "undefined") {
1207
+ if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
1208
+ defaultUserAgent.runtime_env = {
1209
+ name: "Node",
1210
+ version: process.versions.node,
1211
+ };
1212
+ }
1213
+ if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
1214
+ defaultUserAgent.runtime_env = {
1215
+ name: "Bun",
1216
+ version: process.versions.bun,
1217
+ };
1218
+ }
1219
+ }
1220
+ if (typeof Deno !== "undefined") {
1221
+ if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
1222
+ defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
1223
+ }
1224
+ }
1225
+
1226
+ /**
1227
+ * Base class for services that communicate with the API.
1228
+ */
1229
+ class BaseService {
1230
+ /**
1231
+ * Create a new service.
1232
+ * @param params - The parameters to use for the service.
1233
+ */
1234
+ constructor(params) {
1235
+ this.params = params;
1236
+ if (params.userAgent === false) {
1237
+ this.userAgent = undefined;
1238
+ }
1239
+ else {
1240
+ this.userAgent = buildUserAgent(params.userAgent || {});
1241
+ }
1242
+ }
1243
+ fetch(input, init) {
1244
+ return __awaiter(this, void 0, void 0, function* () {
1245
+ init = Object.assign(Object.assign({}, DEFAULT_FETCH_INIT), init);
1246
+ let headers = {
1247
+ Authorization: this.params.apiKey,
1248
+ "Content-Type": "application/json",
1249
+ };
1250
+ if (DEFAULT_FETCH_INIT === null || DEFAULT_FETCH_INIT === void 0 ? void 0 : DEFAULT_FETCH_INIT.headers)
1251
+ headers = Object.assign(Object.assign({}, headers), DEFAULT_FETCH_INIT.headers);
1252
+ if (init === null || init === void 0 ? void 0 : init.headers)
1253
+ headers = Object.assign(Object.assign({}, headers), init.headers);
1254
+ if (this.userAgent) {
1255
+ headers["User-Agent"] = this.userAgent;
1256
+ {
1257
+ // chromium browsers have a bug where the user agent can't be modified
1258
+ if (typeof window !== "undefined" && "chrome" in window) {
1259
+ headers["AssemblyAI-Agent"] =
1260
+ this.userAgent;
1261
+ }
1262
+ }
1263
+ }
1264
+ init.headers = headers;
1265
+ if (!input.startsWith("http"))
1266
+ input = this.params.baseUrl + input;
1267
+ const response = yield fetch(input, init);
1268
+ if (response.status >= 400) {
1269
+ let json;
1270
+ const text = yield response.text();
1271
+ if (text) {
1272
+ try {
1273
+ json = JSON.parse(text);
1274
+ }
1275
+ catch (_a) {
1276
+ /* empty */
1277
+ }
1278
+ if (json === null || json === void 0 ? void 0 : json.error)
1279
+ throw new Error(json.error);
1280
+ throw new Error(text);
1281
+ }
1282
+ throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
1283
+ }
1284
+ return response;
1285
+ });
1286
+ }
1287
+ fetchJson(input, init) {
1288
+ return __awaiter(this, void 0, void 0, function* () {
1289
+ const response = yield this.fetch(input, init);
1290
+ return response.json();
1291
+ });
1292
+ }
1293
+ }
1294
+
1295
+ class StreamingTranscriberFactory extends BaseService {
1296
+ constructor(params) {
1297
+ super(params);
1298
+ this.baseServiceParams = params;
1299
+ }
1300
+ transcriber(params) {
1301
+ const serviceParams = Object.assign({}, params);
1302
+ if (!serviceParams.token && !serviceParams.apiKey) {
1303
+ serviceParams.apiKey = this.baseServiceParams.apiKey;
1304
+ }
1305
+ return new StreamingTranscriber(serviceParams);
1306
+ }
1307
+ createTemporaryToken(params) {
1308
+ return __awaiter(this, void 0, void 0, function* () {
1309
+ const searchParams = new URLSearchParams();
1310
+ // Add each param to the search params
1311
+ Object.entries(params).forEach(([key, value]) => {
1312
+ if (value !== undefined && value !== null) {
1313
+ searchParams.append(key, String(value));
1314
+ }
1315
+ });
1316
+ const queryString = searchParams.toString();
1317
+ const url = queryString ? `/v3/token?${queryString}` : "/v3/token";
1318
+ const data = yield this.fetchJson(url, {
1319
+ method: "GET",
1320
+ });
1321
+ return data.token;
1322
+ });
1323
+ }
1324
+ }
1325
+ class StreamingServiceFactory extends StreamingTranscriberFactory {
1326
+ }
1327
+
1328
+ /**
1329
+ * AudioWorklet processor that ingests mono Float32 audio at the AudioContext's
1330
+ * native sample rate, resamples to `targetRate` (linear interpolation, stateful
1331
+ * across `process()` calls), packs to little-endian Int16 PCM, and posts
1332
+ * fixed-size chunks via `port.postMessage` with a running `samplesSent` counter.
1333
+ *
1334
+ * `samplesSent` is in **target-rate samples**, so the main thread can derive a
1335
+ * stream-relative timestamp = `samplesSent / targetRate * 1000` (ms) — the same
1336
+ * frame AAI uses for `StreamingWord.start` / `.end`.
1337
+ *
1338
+ * Defined as a string so it can be registered via a Blob URL — the SDK ships as
1339
+ * a single ESM file, so a separate `.js` worklet asset isn't viable.
1340
+ */
1341
+ const pcm16EncoderWorkletSource = `
1342
+ class Pcm16EncoderProcessor extends AudioWorkletProcessor {
1343
+ constructor(options) {
1344
+ super();
1345
+ const opts = (options && options.processorOptions) || {};
1346
+ this.targetRate = opts.targetRate || 16000;
1347
+ this.chunkMs = opts.chunkMs || 50;
1348
+ this.ratio = sampleRate / this.targetRate;
1349
+ this.chunkSize = Math.round(this.targetRate * this.chunkMs / 1000);
1350
+ this.buffer = new Int16Array(this.chunkSize);
1351
+ this.bufferIdx = 0;
1352
+ this.samplesSent = 0;
1353
+ this.lastSample = 0;
1354
+ this.fractional = 0;
1355
+ }
1356
+
1357
+ process(inputs) {
1358
+ const input = inputs[0];
1359
+ if (!input || input.length === 0 || !input[0] || input[0].length === 0) {
1360
+ return true;
1361
+ }
1362
+ const mono = input[0];
1363
+ let pos = this.fractional;
1364
+ while (pos < mono.length) {
1365
+ const i = Math.floor(pos);
1366
+ const frac = pos - i;
1367
+ const a = i === 0 ? this.lastSample : mono[i - 1];
1368
+ const b = mono[i];
1369
+ const sample = a + (b - a) * frac;
1370
+ const clamped = sample < -1 ? -1 : sample > 1 ? 1 : sample;
1371
+ this.buffer[this.bufferIdx++] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;
1372
+ if (this.bufferIdx === this.chunkSize) {
1373
+ const out = new Int16Array(this.chunkSize);
1374
+ out.set(this.buffer);
1375
+ this.samplesSent += this.chunkSize;
1376
+ this.port.postMessage(
1377
+ { pcm: out.buffer, samplesSent: this.samplesSent },
1378
+ [out.buffer],
1379
+ );
1380
+ this.bufferIdx = 0;
1381
+ }
1382
+ pos += this.ratio;
1383
+ }
1384
+ this.lastSample = mono[mono.length - 1];
1385
+ this.fractional = pos - mono.length;
1386
+ return true;
1387
+ }
1388
+ }
1389
+ registerProcessor("aai-pcm16-encoder", Pcm16EncoderProcessor);
1390
+ `;
1391
+ const PCM16_ENCODER_PROCESSOR_NAME = "aai-pcm16-encoder";
1392
+
1393
+ const DEFAULT_TARGET_RATE = 16000;
1394
+ const DEFAULT_CHUNK_MS = 50;
1395
+ const MIC_CHANNEL = "mic";
1396
+ const SYSTEM_CHANNEL = "system";
1397
+ /**
1398
+ * Browser-only adapter that pumps two `MediaStream`s into a `StreamingTranscriber`
1399
+ * configured for dual-channel mode. Each `MediaStream` runs through its own
1400
+ * `pcm16-encoder` AudioWorklet (resample to `targetSampleRate`, encode to Int16
1401
+ * PCM); each PCM chunk is forwarded via `transcriber.sendAudio(pcm, { channel })`.
1402
+ *
1403
+ * All dual-channel orchestration (mixing, VAD, per-word attribution) lives inside
1404
+ * `StreamingTranscriber` — this class is a pure I/O adapter. Non-browser runtimes
1405
+ * can replicate its job by pushing tagged PCM into `transcriber.sendAudio` directly.
1406
+ *
1407
+ * Caller responsibilities:
1408
+ * - **Echo cancellation** is set at `getUserMedia` time (`audio: { echoCancellation: true }`).
1409
+ * - **System-audio capture** is platform-dependent. Chrome's `getDisplayMedia({ audio: true })`
1410
+ * captures tab audio (and on Windows, full system audio when sharing the whole screen).
1411
+ * macOS requires a virtual loopback driver (e.g. BlackHole) to expose system audio at all.
1412
+ * - **Token auth.** Construct the transcriber with `token` — API-key auth is unsupported in browsers.
1413
+ * - **Stream ownership.** `stop()` tears down the AudioContext but does NOT stop the
1414
+ * `MediaStreamTrack`s passed in — callers own those.
1415
+ */
1416
+ class DualChannelCapture {
1417
+ constructor(params) {
1418
+ var _a;
1419
+ this.running = false;
1420
+ if (typeof globalThis.AudioContext === "undefined") {
1421
+ throw new BrowserOnlyError();
1422
+ }
1423
+ this.params = {
1424
+ micStream: params.micStream,
1425
+ systemStream: params.systemStream,
1426
+ transcriber: params.transcriber,
1427
+ targetSampleRate: (_a = params.targetSampleRate) !== null && _a !== void 0 ? _a : DEFAULT_TARGET_RATE,
1428
+ };
1429
+ }
1430
+ on(event, listener) {
1431
+ if (event === "error")
1432
+ this.errorListener = listener;
1433
+ }
1434
+ /**
1435
+ * Wire the capture pipeline and start pumping tagged PCM into the transcriber.
1436
+ * The transcriber must already be connected. Returns once the worklet is
1437
+ * registered and the audio graph is live.
1438
+ */
1439
+ start() {
1440
+ return __awaiter(this, void 0, void 0, function* () {
1441
+ if (this.running) {
1442
+ throw new Error("DualChannelCapture already started");
1443
+ }
1444
+ this.context = new AudioContext();
1445
+ const blob = new Blob([pcm16EncoderWorkletSource], {
1446
+ type: "application/javascript",
1447
+ });
1448
+ const url = URL.createObjectURL(blob);
1449
+ try {
1450
+ yield this.context.audioWorklet.addModule(url);
1451
+ }
1452
+ finally {
1453
+ URL.revokeObjectURL(url);
1454
+ }
1455
+ this.micSource = this.context.createMediaStreamSource(this.params.micStream);
1456
+ this.sysSource = this.context.createMediaStreamSource(this.params.systemStream);
1457
+ this.micEncoder = this.makeEncoder(MIC_CHANNEL);
1458
+ this.sysEncoder = this.makeEncoder(SYSTEM_CHANNEL);
1459
+ this.micSource.connect(this.micEncoder);
1460
+ this.sysSource.connect(this.sysEncoder);
1461
+ this.running = true;
1462
+ });
1463
+ }
1464
+ makeEncoder(channel) {
1465
+ const node = new AudioWorkletNode(this.context, PCM16_ENCODER_PROCESSOR_NAME, {
1466
+ numberOfInputs: 1,
1467
+ numberOfOutputs: 0,
1468
+ channelCount: 1,
1469
+ channelCountMode: "explicit",
1470
+ channelInterpretation: "speakers",
1471
+ processorOptions: {
1472
+ targetRate: this.params.targetSampleRate,
1473
+ chunkMs: DEFAULT_CHUNK_MS,
1474
+ },
1475
+ });
1476
+ node.port.onmessage = (e) => {
1477
+ var _a;
1478
+ try {
1479
+ this.params.transcriber.sendAudio(e.data.pcm, { channel });
1480
+ }
1481
+ catch (err) {
1482
+ (_a = this.errorListener) === null || _a === void 0 ? void 0 : _a.call(this, err);
1483
+ }
1484
+ };
1485
+ return node;
1486
+ }
1487
+ /**
1488
+ * Tear down internal nodes and close the AudioContext. Does NOT stop the
1489
+ * caller-provided MediaStream tracks — they remain available for preview UI,
1490
+ * recording, etc. Idempotent.
1491
+ */
1492
+ stop() {
1493
+ return __awaiter(this, void 0, void 0, function* () {
1494
+ var _a, _b, _c, _d, _e, _f;
1495
+ if (!this.running)
1496
+ return;
1497
+ this.running = false;
1498
+ try {
1499
+ (_a = this.micEncoder) === null || _a === void 0 ? void 0 : _a.port.close();
1500
+ (_b = this.sysEncoder) === null || _b === void 0 ? void 0 : _b.port.close();
1501
+ (_c = this.micEncoder) === null || _c === void 0 ? void 0 : _c.disconnect();
1502
+ (_d = this.sysEncoder) === null || _d === void 0 ? void 0 : _d.disconnect();
1503
+ (_e = this.micSource) === null || _e === void 0 ? void 0 : _e.disconnect();
1504
+ (_f = this.sysSource) === null || _f === void 0 ? void 0 : _f.disconnect();
1505
+ }
1506
+ catch (_g) {
1507
+ // Disconnecting already-disconnected nodes throws in some browsers; ignore.
1508
+ }
1509
+ if (this.context && this.context.state !== "closed") {
1510
+ yield this.context.close();
1511
+ }
1512
+ this.context = undefined;
1513
+ this.micSource = undefined;
1514
+ this.sysSource = undefined;
1515
+ this.micEncoder = undefined;
1516
+ this.sysEncoder = undefined;
1517
+ });
1518
+ }
1519
+ }
1520
+
1521
+ /**
1522
+ * Linear-interpolation resampler for streaming Float32 audio. Stateful across
1523
+ * `process()` calls so chunk boundaries don't introduce phase discontinuities:
1524
+ * the last input sample and a fractional read position are carried over.
1525
+ *
1526
+ * Linear interpolation is good enough for ASR ingest — the downstream
1527
+ * StreamingTranscriber band-limits at the target rate anyway, and a polyphase
1528
+ * filter would be overkill in the AudioWorklet hot path. If a customer needs
1529
+ * higher quality they can supply their own VadDetector + bypass the encoder.
1530
+ */
1531
+ class LinearResampler {
1532
+ constructor(sourceRate, targetRate) {
1533
+ this.sourceRate = sourceRate;
1534
+ this.targetRate = targetRate;
1535
+ this.lastSample = 0;
1536
+ this.fractional = 0;
1537
+ if (sourceRate <= 0 || targetRate <= 0) {
1538
+ throw new Error("sourceRate and targetRate must be positive");
1539
+ }
1540
+ this.ratio = sourceRate / targetRate;
1541
+ }
1542
+ process(input) {
1543
+ var _a;
1544
+ if (this.sourceRate === this.targetRate) {
1545
+ return input;
1546
+ }
1547
+ // Worst-case output length; we'll slice to actual.
1548
+ const out = new Float32Array(Math.ceil(input.length / this.ratio) + 1);
1549
+ let outIdx = 0;
1550
+ let pos = this.fractional;
1551
+ while (pos < input.length) {
1552
+ const i = Math.floor(pos);
1553
+ const frac = pos - i;
1554
+ const a = i === 0 ? this.lastSample : input[i - 1];
1555
+ const b = input[i];
1556
+ out[outIdx++] = a + (b - a) * frac;
1557
+ pos += this.ratio;
1558
+ }
1559
+ this.lastSample = (_a = input[input.length - 1]) !== null && _a !== void 0 ? _a : this.lastSample;
1560
+ this.fractional = pos - input.length;
1561
+ return out.subarray(0, outIdx);
1562
+ }
1563
+ reset() {
1564
+ this.lastSample = 0;
1565
+ this.fractional = 0;
1566
+ }
1567
+ }
1568
+ /** Convert Float32 PCM (-1..1) to little-endian Int16 PCM. */
1569
+ function float32ToPcm16(input) {
1570
+ const out = new ArrayBuffer(input.length * 2);
1571
+ const view = new DataView(out);
1572
+ for (let i = 0; i < input.length; i++) {
1573
+ const clamped = Math.max(-1, Math.min(1, input[i]));
1574
+ view.setInt16(i * 2, clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff, true);
1575
+ }
1576
+ return out;
1577
+ }
1578
+
1579
+ exports.BrowserOnlyError = BrowserOnlyError;
1580
+ exports.DualChannelCapture = DualChannelCapture;
1581
+ exports.EnergyVad = EnergyVad;
1582
+ exports.LinearResampler = LinearResampler;
319
1583
  exports.RealtimeService = RealtimeService;
320
1584
  exports.RealtimeTranscriber = RealtimeTranscriber;
1585
+ exports.StreamingServiceFactory = StreamingServiceFactory;
1586
+ exports.StreamingTranscriber = StreamingTranscriber;
1587
+ exports.StreamingTranscriberFactory = StreamingTranscriberFactory;
1588
+ exports.VadTimeline = VadTimeline;
1589
+ exports.attributeTurn = attributeTurn;
1590
+ exports.attributeWord = attributeWord;
1591
+ exports.float32ToPcm16 = float32ToPcm16;
1592
+ exports.rollUpTurnChannel = rollUpTurnChannel;