@charivo/stt 0.9.1 → 0.10.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.
@@ -0,0 +1,807 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/gemini-live/index.ts
21
+ var gemini_live_exports = {};
22
+ __export(gemini_live_exports, {
23
+ createGeminiLiveSTTTranscriber: () => createGeminiLiveSTTTranscriber
24
+ });
25
+ module.exports = __toCommonJS(gemini_live_exports);
26
+ var import_core = require("@charivo/core");
27
+
28
+ // src/gemini-live/capture.ts
29
+ var INPUT_SAMPLE_RATE = 16e3;
30
+ var CAPTURE_FRAME_SAMPLES = 1600;
31
+ var CAPTURE_PROCESSOR_NAME = "charivo-gemini-live-stt-capture";
32
+ var FLUSH_TIMEOUT_MS = 250;
33
+ var CAPTURE_WORKLET_SOURCE = `
34
+ class CaptureProcessor extends AudioWorkletProcessor {
35
+ constructor(options) {
36
+ super();
37
+ this.ratio = sampleRate / options.processorOptions.targetRate;
38
+ this.frameSamples = options.processorOptions.frameSamples;
39
+ this.position = 0;
40
+ this.sum = 0;
41
+ this.count = 0;
42
+ this.pending = [];
43
+ // The samples captured after the last full frame are the end of what the
44
+ // user said. Nothing else ever posts them: without this answer they sit in
45
+ // \`pending\` until the context closes and the transcript loses its tail.
46
+ this.port.onmessage = (event) => {
47
+ if (!event.data || event.data.type !== "flush") {
48
+ return;
49
+ }
50
+ if (this.pending.length > 0) {
51
+ this.postFrame(this.pending.splice(0, this.pending.length));
52
+ }
53
+ this.port.postMessage({ type: "flushed" });
54
+ };
55
+ }
56
+
57
+ postFrame(chunk) {
58
+ const frame = new Int16Array(chunk.length);
59
+ for (let i = 0; i < chunk.length; i += 1) {
60
+ const clamped = Math.max(-1, Math.min(1, chunk[i]));
61
+ frame[i] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;
62
+ }
63
+ this.port.postMessage(frame.buffer, [frame.buffer]);
64
+ }
65
+
66
+ process(inputs) {
67
+ const channel = inputs[0] && inputs[0][0];
68
+ if (!channel) {
69
+ return true;
70
+ }
71
+
72
+ // Box-average decimation. Not a proper anti-alias filter, but far better
73
+ // than picking every Nth sample, which folds speech harmonics back down.
74
+ // The position accumulator carries the fractional remainder across groups,
75
+ // so a device rate that is not a whole multiple of the target \u2014 44.1 kHz,
76
+ // say \u2014 alternates group sizes rather than drifting out of step.
77
+ for (let i = 0; i < channel.length; i += 1) {
78
+ this.sum += channel[i];
79
+ this.count += 1;
80
+ this.position += 1;
81
+ if (this.position >= this.ratio) {
82
+ this.position -= this.ratio;
83
+ this.pending.push(this.count > 0 ? this.sum / this.count : 0);
84
+ this.sum = 0;
85
+ this.count = 0;
86
+ }
87
+ }
88
+
89
+ while (this.pending.length >= this.frameSamples) {
90
+ this.postFrame(this.pending.splice(0, this.frameSamples));
91
+ }
92
+
93
+ return true;
94
+ }
95
+ }
96
+
97
+ registerProcessor("${CAPTURE_PROCESSOR_NAME}", CaptureProcessor);
98
+ `;
99
+ async function createCapturePipeline({
100
+ stream,
101
+ onFrame,
102
+ onError
103
+ }) {
104
+ const context = new AudioContext();
105
+ const workletUrl = URL.createObjectURL(
106
+ new Blob([CAPTURE_WORKLET_SOURCE], { type: "application/javascript" })
107
+ );
108
+ try {
109
+ await context.audioWorklet.addModule(workletUrl);
110
+ const source = context.createMediaStreamSource(stream);
111
+ const capture = new AudioWorkletNode(context, CAPTURE_PROCESSOR_NAME, {
112
+ processorOptions: {
113
+ targetRate: INPUT_SAMPLE_RATE,
114
+ frameSamples: CAPTURE_FRAME_SAMPLES
115
+ }
116
+ });
117
+ source.connect(capture);
118
+ let pendingFlush = null;
119
+ const settleFlush = (drained) => {
120
+ const flushing = pendingFlush;
121
+ if (!flushing) {
122
+ return;
123
+ }
124
+ pendingFlush = null;
125
+ clearTimeout(flushing.timer);
126
+ flushing.resolve({ drained });
127
+ };
128
+ capture.port.onmessage = (event) => {
129
+ if (event.data instanceof ArrayBuffer) {
130
+ onFrame(new Uint8Array(event.data));
131
+ return;
132
+ }
133
+ if (event.data?.type === "flushed") {
134
+ settleFlush(true);
135
+ }
136
+ };
137
+ capture.onprocessorerror = () => {
138
+ onError();
139
+ };
140
+ return {
141
+ flush() {
142
+ return new Promise((resolve) => {
143
+ pendingFlush = {
144
+ resolve,
145
+ timer: setTimeout(() => settleFlush(false), FLUSH_TIMEOUT_MS)
146
+ };
147
+ capture.port.postMessage({ type: "flush" });
148
+ });
149
+ },
150
+ stop() {
151
+ capture.port.onmessage = null;
152
+ capture.onprocessorerror = null;
153
+ capture.port.close();
154
+ source.disconnect();
155
+ capture.disconnect();
156
+ void closeContext(context);
157
+ }
158
+ };
159
+ } catch (error) {
160
+ void closeContext(context);
161
+ throw error;
162
+ } finally {
163
+ URL.revokeObjectURL(workletUrl);
164
+ }
165
+ }
166
+ function closeContext(context) {
167
+ return context.close().catch(
168
+ (error) => console.error("Failed to close the capture audio context:", error)
169
+ );
170
+ }
171
+
172
+ // src/gemini-live/index.ts
173
+ var TRANSCRIPTION_MODEL = "gemini-3.5-transcribe-live";
174
+ var OPEN_TIMEOUT_MS = 1e4;
175
+ var BOOTSTRAP_TIMEOUT_MS = 15e3;
176
+ var FINALIZE_TIMEOUT_MS = 5e3;
177
+ var INPUT_MIME_TYPE = "audio/pcm;rate=16000";
178
+ var GeminiLiveSTTTranscriber = class {
179
+ constructor(config) {
180
+ this.config = config;
181
+ }
182
+ mediaStream = null;
183
+ binding = null;
184
+ capture = null;
185
+ // Session epoch. Every per-session callback captures the value from its
186
+ // startRecording() call and ignores events once cleanup() has bumped it, so
187
+ // listeners still attached to a closing socket cannot mutate a newer session.
188
+ generation = 0;
189
+ connecting = false;
190
+ recording = false;
191
+ stopPending = false;
192
+ isCleaningUp = false;
193
+ terminalError = null;
194
+ setupError = null;
195
+ setupReject = null;
196
+ pendingStop = null;
197
+ openGate = null;
198
+ openTimer;
199
+ stopTimer;
200
+ // `snapshot` is what the server last said, `lastEmitted` what subscribers
201
+ // last saw.
202
+ snapshot = "";
203
+ lastEmitted = "";
204
+ activityEnded = false;
205
+ audioSent = false;
206
+ partialSubscribers = [];
207
+ handlePageHide = () => this.cleanup();
208
+ /**
209
+ * Open the Live API session and start streaming microphone audio
210
+ */
211
+ async startRecording(options) {
212
+ if (this.recording || this.connecting) {
213
+ throw new import_core.CharivoStateError("already recording");
214
+ }
215
+ this.connecting = true;
216
+ this.stopPending = false;
217
+ this.terminalError = null;
218
+ this.setupError = null;
219
+ this.pendingStop = null;
220
+ this.snapshot = "";
221
+ this.lastEmitted = "";
222
+ this.activityEnded = false;
223
+ this.audioSent = false;
224
+ const gen = ++this.generation;
225
+ const setupFailure = new Promise((_, reject) => {
226
+ this.setupReject = reject;
227
+ });
228
+ void setupFailure.catch(() => {
229
+ });
230
+ try {
231
+ const micPromise = this.acquireMic();
232
+ void micPromise.then(
233
+ (stream2) => {
234
+ if (gen !== this.generation) {
235
+ stream2.getTracks().forEach((track) => track.stop());
236
+ }
237
+ },
238
+ () => {
239
+ }
240
+ );
241
+ const stream = await Promise.race([micPromise, setupFailure]);
242
+ if (gen !== this.generation) {
243
+ stream.getTracks().forEach((track) => track.stop());
244
+ throw new import_core.CharivoStateError(
245
+ "start canceled because stop was called while connecting"
246
+ );
247
+ }
248
+ this.mediaStream = stream;
249
+ const { url, token } = resolveBootstrap(
250
+ await this.raceBootstrap(
251
+ this.config.bootstrap({
252
+ session: {
253
+ model: TRANSCRIPTION_MODEL,
254
+ language: options?.language
255
+ }
256
+ }),
257
+ setupFailure
258
+ )
259
+ );
260
+ this.openSocket(url, token, gen);
261
+ await Promise.race([this.waitForSetupComplete(gen), setupFailure]);
262
+ if (gen !== this.generation) {
263
+ throw new import_core.CharivoStateError(
264
+ "start canceled because stop was called while connecting"
265
+ );
266
+ }
267
+ this.binding.socket.send(
268
+ JSON.stringify({ realtimeInput: { activityStart: {} } })
269
+ );
270
+ const capturePromise = createCapturePipeline({
271
+ stream,
272
+ onFrame: (frame) => this.sendAudioFrame(frame),
273
+ // Routed like every other failure source in this class: the pipeline is
274
+ // built while the session is still connecting, so a failure inside that
275
+ // window has to fail the in-flight start rather than become a terminal
276
+ // error the start then walks over by going live on a stopped pipeline.
277
+ onError: () => this.failOpenOrTerminal(
278
+ new import_core.CharivoProviderError("capture worklet stopped processing")
279
+ )
280
+ });
281
+ void capturePromise.then(
282
+ (pipeline) => {
283
+ if (gen !== this.generation) {
284
+ pipeline.stop();
285
+ }
286
+ },
287
+ () => {
288
+ }
289
+ );
290
+ const capture = await Promise.race([capturePromise, setupFailure]);
291
+ if (gen !== this.generation) {
292
+ capture.stop();
293
+ throw new import_core.CharivoStateError(
294
+ "start canceled because stop was called while connecting"
295
+ );
296
+ }
297
+ this.capture = capture;
298
+ if (typeof window !== "undefined") {
299
+ window.addEventListener("pagehide", this.handlePageHide);
300
+ }
301
+ this.recording = true;
302
+ } catch (error) {
303
+ const failure = (gen === this.generation ? this.setupError : null) ?? error;
304
+ if (gen === this.generation) {
305
+ this.cleanup();
306
+ this.terminalError = null;
307
+ this.setupError = null;
308
+ }
309
+ throw failure;
310
+ } finally {
311
+ if (gen === this.generation) {
312
+ this.connecting = false;
313
+ this.setupReject = null;
314
+ }
315
+ }
316
+ }
317
+ /**
318
+ * Close the activity and resolve with the transcript of the whole recording
319
+ */
320
+ async stopRecording() {
321
+ if (this.terminalError) {
322
+ const error = this.terminalError;
323
+ this.terminalError = null;
324
+ this.cleanup();
325
+ throw error;
326
+ }
327
+ if (this.stopPending) {
328
+ throw new import_core.CharivoStateError("stop already in progress");
329
+ }
330
+ if (this.connecting) {
331
+ this.cancelConnecting();
332
+ return "";
333
+ }
334
+ if (!this.recording) {
335
+ return "";
336
+ }
337
+ this.stopPending = true;
338
+ const gen = this.generation;
339
+ const stopped = new Promise((resolve, reject) => {
340
+ this.pendingStop = { resolve, reject };
341
+ this.stopTimer = setTimeout(
342
+ () => this.rejectPendingStop(
343
+ new import_core.CharivoTimeoutError(
344
+ `stop timed out after ${FINALIZE_TIMEOUT_MS}ms waiting for the final transcript`
345
+ )
346
+ ),
347
+ FINALIZE_TIMEOUT_MS
348
+ );
349
+ }).then(
350
+ (text) => {
351
+ this.cleanup();
352
+ return text;
353
+ },
354
+ (error) => {
355
+ this.terminalError = null;
356
+ this.cleanup();
357
+ throw error;
358
+ }
359
+ );
360
+ void this.finalizeStop(gen).catch(() => {
361
+ });
362
+ return stopped;
363
+ }
364
+ /**
365
+ * Check if currently recording, including while a session is still
366
+ * connecting: lifecycle owners like Charivo.dispose() gate their stop call
367
+ * on this, so a connecting session must count as active.
368
+ */
369
+ isRecording() {
370
+ return this.recording || this.connecting;
371
+ }
372
+ /**
373
+ * Subscribe to cumulative interim transcript snapshots
374
+ */
375
+ onPartial(callback) {
376
+ this.partialSubscribers.push(callback);
377
+ }
378
+ /**
379
+ * Hand one capture frame to the server, or drop it. The capture pipeline is
380
+ * stopped before the socket is torn down, so a frame can only arrive here
381
+ * while the socket is still open.
382
+ */
383
+ sendAudioFrame(frame) {
384
+ const socket = this.binding?.socket;
385
+ if (!socket || socket.readyState !== WebSocket.OPEN) {
386
+ return;
387
+ }
388
+ socket.send(
389
+ JSON.stringify({
390
+ realtimeInput: {
391
+ audio: { data: bytesToBase64(frame), mimeType: INPUT_MIME_TYPE }
392
+ }
393
+ })
394
+ );
395
+ this.audioSent = true;
396
+ }
397
+ async acquireMic() {
398
+ return navigator.mediaDevices.getUserMedia({
399
+ audio: {
400
+ echoCancellation: true,
401
+ noiseSuppression: true,
402
+ autoGainControl: true
403
+ }
404
+ });
405
+ }
406
+ async raceBootstrap(bootstrapPromise, setupFailure) {
407
+ let timer;
408
+ const timeout = new Promise((_, reject) => {
409
+ timer = setTimeout(
410
+ () => reject(
411
+ new import_core.CharivoTimeoutError(
412
+ `streaming STT bootstrap timed out after ${BOOTSTRAP_TIMEOUT_MS}ms`
413
+ )
414
+ ),
415
+ BOOTSTRAP_TIMEOUT_MS
416
+ );
417
+ });
418
+ try {
419
+ return await Promise.race([bootstrapPromise, setupFailure, timeout]);
420
+ } finally {
421
+ clearTimeout(timer);
422
+ }
423
+ }
424
+ openSocket(url, token, gen) {
425
+ let endpoint;
426
+ try {
427
+ endpoint = new URL(url);
428
+ } catch {
429
+ throw new import_core.CharivoProviderError(
430
+ "bootstrap url is not a valid websocket URL"
431
+ );
432
+ }
433
+ if (endpoint.protocol !== "ws:" && endpoint.protocol !== "wss:") {
434
+ throw new import_core.CharivoProviderError("bootstrap url must use ws: or wss:");
435
+ }
436
+ let socket;
437
+ try {
438
+ endpoint.searchParams.set("access_token", token);
439
+ socket = new WebSocket(endpoint.toString());
440
+ } catch {
441
+ throw new import_core.CharivoProviderError(
442
+ "failed to open the Gemini Live websocket"
443
+ );
444
+ }
445
+ if (gen !== this.generation) {
446
+ socket.close();
447
+ throw new import_core.CharivoStateError(
448
+ "start canceled because stop was called while connecting"
449
+ );
450
+ }
451
+ const binding = { socket, gen, pump: Promise.resolve() };
452
+ this.binding = binding;
453
+ socket.onopen = () => {
454
+ if (gen !== this.generation) {
455
+ return;
456
+ }
457
+ socket.send(
458
+ JSON.stringify({
459
+ setup: {
460
+ model: `models/${TRANSCRIPTION_MODEL}`,
461
+ generationConfig: { responseModalities: ["TEXT"] },
462
+ inputAudioTranscription: { mode: "VERBATIM" },
463
+ realtimeInputConfig: {
464
+ automaticActivityDetection: { disabled: true }
465
+ }
466
+ }
467
+ })
468
+ );
469
+ };
470
+ socket.onmessage = (event) => {
471
+ this.enqueueServerMessage(binding, event.data);
472
+ };
473
+ socket.onerror = () => {
474
+ if (gen !== this.generation || this.isCleaningUp) {
475
+ return;
476
+ }
477
+ this.failOpenOrTerminal(
478
+ new import_core.CharivoProviderError("Gemini Live websocket error")
479
+ );
480
+ };
481
+ socket.onclose = (event) => {
482
+ if (gen !== this.generation || this.isCleaningUp) {
483
+ return;
484
+ }
485
+ this.failOpenOrTerminal(
486
+ new import_core.CharivoProviderError(
487
+ `Gemini Live websocket closed (code ${event.code})`
488
+ )
489
+ );
490
+ };
491
+ }
492
+ enqueueServerMessage(binding, data) {
493
+ if (binding.gen !== this.generation) {
494
+ return;
495
+ }
496
+ const arrivedAfterActivityEnd = this.activityEnded;
497
+ binding.pump = binding.pump.then(async () => {
498
+ let payload;
499
+ try {
500
+ const raw = data instanceof Blob ? await data.text() : data;
501
+ if (binding.gen !== this.generation) {
502
+ return;
503
+ }
504
+ if (typeof raw !== "string") {
505
+ throw new TypeError("unreadable frame");
506
+ }
507
+ payload = JSON.parse(raw);
508
+ } catch {
509
+ this.failOpenOrTerminal(
510
+ new import_core.CharivoProviderError("malformed Gemini Live message")
511
+ );
512
+ return;
513
+ }
514
+ if (typeof payload !== "object" || payload === null) {
515
+ this.failOpenOrTerminal(
516
+ new import_core.CharivoProviderError("malformed Gemini Live message")
517
+ );
518
+ return;
519
+ }
520
+ this.handleServerMessage(
521
+ payload,
522
+ arrivedAfterActivityEnd
523
+ );
524
+ });
525
+ }
526
+ handleServerMessage(message, arrivedAfterActivityEnd) {
527
+ if (message.setupComplete) {
528
+ this.resolveOpenGate();
529
+ return;
530
+ }
531
+ const content = message.serverContent;
532
+ if (!content) {
533
+ return;
534
+ }
535
+ const interim = content.interimInputTranscription;
536
+ if (interim) {
537
+ const text = decodeTranscriptText(interim.text);
538
+ if (text === null) {
539
+ this.failOpenOrTerminal(
540
+ new import_core.CharivoProviderError("malformed Gemini Live message")
541
+ );
542
+ return;
543
+ }
544
+ this.snapshot = text;
545
+ this.emitPartial();
546
+ }
547
+ const final = content.inputTranscription;
548
+ if (final) {
549
+ const text = decodeTranscriptText(final.text);
550
+ if (text === null) {
551
+ this.failOpenOrTerminal(
552
+ new import_core.CharivoProviderError("malformed Gemini Live message")
553
+ );
554
+ return;
555
+ }
556
+ this.snapshot = text;
557
+ this.emitPartial();
558
+ this.maybeResolveStop(arrivedAfterActivityEnd);
559
+ }
560
+ }
561
+ /**
562
+ * Hand the snapshot to subscribers, but only when it changed. The server
563
+ * repeats the interim unchanged while the speaker is silent (8 identical
564
+ * frames across a measured 1.5 s gap), and a subscriber redrawing a draft
565
+ * has nothing to do with those. What is recorded is what was EMITTED, so a
566
+ * final that repeats the text its last interim already showed emits nothing.
567
+ *
568
+ * A shorter snapshot is emitted as given: nothing here enforces
569
+ * monotonicity, because suppressing a revision would leave subscribers
570
+ * holding text the transcript stop returns no longer contains.
571
+ */
572
+ emitPartial() {
573
+ if (this.snapshot === this.lastEmitted) {
574
+ return;
575
+ }
576
+ this.lastEmitted = this.snapshot;
577
+ for (const callback of this.partialSubscribers) {
578
+ callback(this.snapshot);
579
+ }
580
+ }
581
+ /**
582
+ * Settle a stop that is already in flight with the final just applied, but
583
+ * only if that final is eligible: eligibility is having ARRIVED after
584
+ * `activityEnd` went out, because an earlier one cannot be known to cover the
585
+ * audio sent after it. That is the only rule, and the flag is the only place
586
+ * it is read — `activityEnded` says where the session is now, which for a
587
+ * frame the pump held is a different question.
588
+ *
589
+ * The snapshot is trimmed here and nowhere else. Partials stay exactly as the
590
+ * server sent them, and the transcript a caller keeps is the trimmed one —
591
+ * the same split `buildFinal()` makes in `@charivo/stt/openai-realtime`. So
592
+ * the last partial equals what stop resolves MODULO that trim, and nothing
593
+ * else: a final that revises the tail is emitted before it is trimmed, so no
594
+ * subscriber is left holding text the transcript contradicts.
595
+ */
596
+ maybeResolveStop(arrivedAfterActivityEnd) {
597
+ if (!this.stopPending || !this.pendingStop || !arrivedAfterActivityEnd) {
598
+ return;
599
+ }
600
+ this.resolvePendingStop(this.snapshot.trim());
601
+ }
602
+ waitForSetupComplete(gen) {
603
+ return new Promise((resolve, reject) => {
604
+ this.openGate = { resolve, reject, settled: false };
605
+ this.openTimer = setTimeout(() => {
606
+ if (gen !== this.generation) {
607
+ return;
608
+ }
609
+ this.rejectOpenGate(
610
+ new import_core.CharivoTimeoutError(
611
+ `streaming STT session setup did not complete within ${OPEN_TIMEOUT_MS}ms`
612
+ )
613
+ );
614
+ }, OPEN_TIMEOUT_MS);
615
+ });
616
+ }
617
+ resolveOpenGate() {
618
+ clearTimeout(this.openTimer);
619
+ this.openTimer = void 0;
620
+ const gate = this.openGate;
621
+ this.openGate = null;
622
+ if (!gate || gate.settled) {
623
+ return;
624
+ }
625
+ gate.settled = true;
626
+ gate.resolve();
627
+ }
628
+ rejectOpenGate(error) {
629
+ clearTimeout(this.openTimer);
630
+ this.openTimer = void 0;
631
+ const gate = this.openGate;
632
+ this.openGate = null;
633
+ if (!gate || gate.settled) {
634
+ return;
635
+ }
636
+ gate.settled = true;
637
+ gate.reject(error);
638
+ }
639
+ failOpenOrTerminal(error) {
640
+ if (this.openGate && !this.openGate.settled) {
641
+ this.rejectOpenGate(error);
642
+ } else if (this.connecting) {
643
+ this.setupError ??= error;
644
+ this.setupReject?.(error);
645
+ } else {
646
+ this.failTerminal(error);
647
+ }
648
+ }
649
+ failTerminal(error) {
650
+ if (this.terminalError) {
651
+ return;
652
+ }
653
+ this.terminalError = error;
654
+ this.cleanup();
655
+ }
656
+ // Cancels the in-flight startRecording(). Runs cleanup() synchronously so a
657
+ // caller awaiting stopRecording() sees every resource acquired so far (mic,
658
+ // socket, capture, timers, pagehide listener) already released by the time
659
+ // it returns. Every step in startRecording()'s try block re-checks the
660
+ // generation before touching `this` again, so the setup step it is
661
+ // currently awaiting rejects with this cancel message instead of resuming
662
+ // and acquiring or writing one more thing. The open gate is rejected with
663
+ // the cancel reason FIRST, before cleanup() reaches for its own generic
664
+ // "recording ended": cleanup()'s `if (this.openGate && !this.openGate.settled)`
665
+ // is then a no-op, since rejectOpenGate() already settled and cleared it.
666
+ // setupReject is captured before cleanup() runs because cleanup() nulls it
667
+ // as part of tearing this generation down.
668
+ cancelConnecting() {
669
+ const error = new import_core.CharivoStateError(
670
+ "start canceled because stop was called while connecting"
671
+ );
672
+ this.setupError ??= error;
673
+ const reject = this.setupReject;
674
+ this.rejectOpenGate(error);
675
+ this.cleanup();
676
+ reject?.(error);
677
+ }
678
+ /**
679
+ * Drain the capture worklet, then close the activity the recording belongs
680
+ * to. Settles the pending stop itself on every path that cannot get as far
681
+ * as `activityEnd`.
682
+ */
683
+ async finalizeStop(gen) {
684
+ const capture = this.capture;
685
+ const { drained } = await capture.flush();
686
+ if (gen !== this.generation || !this.pendingStop) {
687
+ return;
688
+ }
689
+ this.capture = null;
690
+ capture.stop();
691
+ if (!drained) {
692
+ this.rejectPendingStop(
693
+ new import_core.CharivoProviderError("capture worklet did not drain before stop")
694
+ );
695
+ return;
696
+ }
697
+ if (!this.audioSent) {
698
+ this.resolvePendingStop("");
699
+ return;
700
+ }
701
+ try {
702
+ this.binding.socket.send(
703
+ JSON.stringify({ realtimeInput: { activityEnd: {} } })
704
+ );
705
+ } catch (error) {
706
+ this.failTerminal(
707
+ error instanceof Error ? error : new import_core.CharivoProviderError("failed to end the activity")
708
+ );
709
+ return;
710
+ }
711
+ this.activityEnded = true;
712
+ }
713
+ resolvePendingStop(transcription) {
714
+ const pending = this.pendingStop;
715
+ if (!pending) {
716
+ return;
717
+ }
718
+ clearTimeout(this.stopTimer);
719
+ this.stopTimer = void 0;
720
+ this.pendingStop = null;
721
+ pending.resolve(transcription);
722
+ }
723
+ rejectPendingStop(error) {
724
+ const pending = this.pendingStop;
725
+ if (!pending) {
726
+ return;
727
+ }
728
+ clearTimeout(this.stopTimer);
729
+ this.stopTimer = void 0;
730
+ this.pendingStop = null;
731
+ pending.reject(error);
732
+ }
733
+ cleanup() {
734
+ if (this.isCleaningUp) {
735
+ return;
736
+ }
737
+ this.isCleaningUp = true;
738
+ try {
739
+ clearTimeout(this.stopTimer);
740
+ this.stopTimer = void 0;
741
+ clearTimeout(this.openTimer);
742
+ this.openTimer = void 0;
743
+ if (this.openGate && !this.openGate.settled) {
744
+ const gate = this.openGate;
745
+ gate.settled = true;
746
+ gate.reject(new import_core.CharivoStateError("recording ended"));
747
+ }
748
+ this.openGate = null;
749
+ if (this.pendingStop) {
750
+ this.rejectPendingStop(
751
+ this.terminalError ?? new import_core.CharivoStateError("recording ended")
752
+ );
753
+ }
754
+ const capture = this.capture;
755
+ this.capture = null;
756
+ capture?.stop();
757
+ this.mediaStream?.getTracks().forEach((track) => track.stop());
758
+ this.binding?.socket.close();
759
+ this.binding = null;
760
+ if (typeof window !== "undefined") {
761
+ window.removeEventListener("pagehide", this.handlePageHide);
762
+ }
763
+ this.mediaStream = null;
764
+ this.recording = false;
765
+ this.connecting = false;
766
+ this.setupReject = null;
767
+ this.stopPending = false;
768
+ this.activityEnded = false;
769
+ this.audioSent = false;
770
+ this.snapshot = "";
771
+ this.lastEmitted = "";
772
+ this.generation++;
773
+ } finally {
774
+ this.isCleaningUp = false;
775
+ }
776
+ }
777
+ };
778
+ function bytesToBase64(bytes) {
779
+ let binary = "";
780
+ const stride = 32768;
781
+ for (let offset = 0; offset < bytes.length; offset += stride) {
782
+ binary += String.fromCharCode(...bytes.subarray(offset, offset + stride));
783
+ }
784
+ return btoa(binary);
785
+ }
786
+ function decodeTranscriptText(text) {
787
+ if (text === void 0) {
788
+ return "";
789
+ }
790
+ return typeof text === "string" ? text : null;
791
+ }
792
+ function resolveBootstrap(bootstrap) {
793
+ const { url, token } = bootstrap ?? {};
794
+ if (typeof url !== "string" || !url || typeof token !== "string" || !token) {
795
+ throw new import_core.CharivoProviderError(
796
+ "streaming STT bootstrap is missing its websocket url or token"
797
+ );
798
+ }
799
+ return { url, token };
800
+ }
801
+ function createGeminiLiveSTTTranscriber(config) {
802
+ return new GeminiLiveSTTTranscriber(config);
803
+ }
804
+ // Annotate the CommonJS export names for ESM import in node:
805
+ 0 && (module.exports = {
806
+ createGeminiLiveSTTTranscriber
807
+ });