assemblyai 4.33.3 → 4.34.4

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