@absolutejs/voice 0.0.22-beta.514 → 0.0.22-beta.516

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,1707 @@
1
+ // src/client/htmx.ts
2
+ var DEFAULT_EVENT_NAME = "voice-refresh";
3
+ var DEFAULT_QUERY_PARAM = "sessionId";
4
+ var resolveElement = (input) => {
5
+ if (typeof input !== "string") {
6
+ return input;
7
+ }
8
+ return document.querySelector(input);
9
+ };
10
+ var buildRoute = (element, route, queryParam, sessionId) => {
11
+ const baseRoute = route ?? element.getAttribute("hx-get") ?? "";
12
+ if (!baseRoute) {
13
+ return "";
14
+ }
15
+ const url = new URL(baseRoute, window.location.origin);
16
+ if (sessionId) {
17
+ url.searchParams.set(queryParam, sessionId);
18
+ } else {
19
+ url.searchParams.delete(queryParam);
20
+ }
21
+ return `${url.pathname}${url.search}${url.hash}`;
22
+ };
23
+ var bindVoiceHTMX = (stream, options) => {
24
+ if (typeof window === "undefined" || typeof document === "undefined") {
25
+ return () => {};
26
+ }
27
+ const element = resolveElement(options.element);
28
+ if (!element) {
29
+ return () => {};
30
+ }
31
+ const eventName = options.eventName ?? DEFAULT_EVENT_NAME;
32
+ const queryParam = options.sessionQueryParam ?? DEFAULT_QUERY_PARAM;
33
+ const sync = () => {
34
+ const htmxWindow = window;
35
+ const nextRoute = buildRoute(element, options.route, queryParam, stream.sessionId);
36
+ if (nextRoute) {
37
+ element.setAttribute("hx-get", nextRoute);
38
+ }
39
+ htmxWindow.htmx?.process?.(element);
40
+ htmxWindow.htmx?.trigger?.(element, eventName);
41
+ };
42
+ const unsubscribe = stream.subscribe(sync);
43
+ sync();
44
+ return () => {
45
+ unsubscribe();
46
+ };
47
+ };
48
+
49
+ // src/client/microphone.ts
50
+ var clampSample = (value) => Math.max(-1, Math.min(1, value));
51
+ var floatTo16BitPCM = (input) => {
52
+ const output = new Int16Array(input.length);
53
+ for (let index = 0;index < input.length; index += 1) {
54
+ const sample = clampSample(input[index] ?? 0);
55
+ output[index] = sample < 0 ? sample * 32768 : sample * 32767;
56
+ }
57
+ return new Uint8Array(output.buffer);
58
+ };
59
+ var getPcmLevel = (audio) => {
60
+ const bytes = audio instanceof Uint8Array ? audio : new Uint8Array(audio);
61
+ if (bytes.byteLength < 2) {
62
+ return 0;
63
+ }
64
+ const samples = new Int16Array(bytes.buffer, bytes.byteOffset, Math.floor(bytes.byteLength / 2));
65
+ if (samples.length === 0) {
66
+ return 0;
67
+ }
68
+ let sumSquares = 0;
69
+ for (const sample of samples) {
70
+ const normalized = sample / 32768;
71
+ sumSquares += normalized * normalized;
72
+ }
73
+ return Math.min(1, Math.max(0, Math.sqrt(sumSquares / samples.length) * 5.5));
74
+ };
75
+ var downsampleBuffer = (input, sourceRate, targetRate) => {
76
+ if (sourceRate === targetRate) {
77
+ return input;
78
+ }
79
+ const ratio = sourceRate / targetRate;
80
+ const length = Math.round(input.length / ratio);
81
+ const output = new Float32Array(length);
82
+ let offsetResult = 0;
83
+ let offsetBuffer = 0;
84
+ while (offsetResult < output.length) {
85
+ const nextOffsetBuffer = Math.round((offsetResult + 1) * ratio);
86
+ let accum = 0;
87
+ let count = 0;
88
+ for (let index = offsetBuffer;index < nextOffsetBuffer && index < input.length; index += 1) {
89
+ accum += input[index] ?? 0;
90
+ count += 1;
91
+ }
92
+ output[offsetResult] = count > 0 ? accum / count : 0;
93
+ offsetResult += 1;
94
+ offsetBuffer = nextOffsetBuffer;
95
+ }
96
+ return output;
97
+ };
98
+ var createMicrophoneCapture = (options) => {
99
+ let audioContext = null;
100
+ let sourceNode = null;
101
+ let processorNode = null;
102
+ let mediaStream = null;
103
+ const start = async () => {
104
+ if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia) {
105
+ throw new Error("Browser microphone capture requires navigator.mediaDevices.getUserMedia.");
106
+ }
107
+ const AudioContextCtor = (typeof window !== "undefined" ? window.AudioContext ?? window.webkitAudioContext : undefined) ?? AudioContext;
108
+ if (!AudioContextCtor) {
109
+ throw new Error("Browser microphone capture requires AudioContext support.");
110
+ }
111
+ mediaStream = await navigator.mediaDevices.getUserMedia({
112
+ audio: {
113
+ channelCount: options.channelCount ?? 1
114
+ }
115
+ });
116
+ audioContext = new AudioContextCtor;
117
+ sourceNode = audioContext.createMediaStreamSource(mediaStream);
118
+ processorNode = audioContext.createScriptProcessor(4096, 1, 1);
119
+ processorNode.onaudioprocess = (event) => {
120
+ const channel = event.inputBuffer.getChannelData(0);
121
+ const downsampled = downsampleBuffer(channel, audioContext?.sampleRate ?? 48000, options.sampleRateHz ?? 16000);
122
+ const pcm = floatTo16BitPCM(downsampled);
123
+ options.onLevel?.(getPcmLevel(pcm));
124
+ options.onAudio(pcm);
125
+ };
126
+ sourceNode.connect(processorNode);
127
+ processorNode.connect(audioContext.destination);
128
+ };
129
+ const stop = () => {
130
+ processorNode?.disconnect();
131
+ sourceNode?.disconnect();
132
+ mediaStream?.getTracks().forEach((track) => track.stop());
133
+ audioContext?.close();
134
+ options.onLevel?.(0);
135
+ audioContext = null;
136
+ mediaStream = null;
137
+ processorNode = null;
138
+ sourceNode = null;
139
+ };
140
+ return { start, stop };
141
+ };
142
+
143
+ // src/client/actions.ts
144
+ var normalizeErrorMessage = (value) => {
145
+ if (typeof value === "string" && value.trim()) {
146
+ return value;
147
+ }
148
+ if (value instanceof Error && value.message.trim()) {
149
+ return value.message;
150
+ }
151
+ if (value && typeof value === "object") {
152
+ const record = value;
153
+ for (const key of ["message", "reason", "description"]) {
154
+ const candidate = record[key];
155
+ if (typeof candidate === "string" && candidate.trim()) {
156
+ return candidate;
157
+ }
158
+ }
159
+ if ("error" in record) {
160
+ return normalizeErrorMessage(record.error);
161
+ }
162
+ if ("cause" in record) {
163
+ return normalizeErrorMessage(record.cause);
164
+ }
165
+ try {
166
+ return JSON.stringify(value);
167
+ } catch {}
168
+ }
169
+ return "Unexpected error";
170
+ };
171
+ var serverMessageToAction = (message) => {
172
+ switch (message.type) {
173
+ case "audio":
174
+ return {
175
+ chunk: Uint8Array.from(atob(message.chunkBase64), (char) => char.charCodeAt(0)),
176
+ format: message.format,
177
+ receivedAt: message.receivedAt,
178
+ turnId: message.turnId,
179
+ type: "audio"
180
+ };
181
+ case "assistant":
182
+ return {
183
+ text: message.text,
184
+ type: "assistant"
185
+ };
186
+ case "complete":
187
+ return {
188
+ sessionId: message.sessionId,
189
+ type: "complete"
190
+ };
191
+ case "connection":
192
+ return {
193
+ reconnect: message.reconnect,
194
+ type: "connection"
195
+ };
196
+ case "call_lifecycle":
197
+ return {
198
+ event: message.event,
199
+ sessionId: message.sessionId,
200
+ type: "call_lifecycle"
201
+ };
202
+ case "error":
203
+ return {
204
+ message: normalizeErrorMessage(message.message),
205
+ type: "error"
206
+ };
207
+ case "final":
208
+ return {
209
+ transcript: message.transcript,
210
+ type: "final"
211
+ };
212
+ case "partial":
213
+ return {
214
+ transcript: message.transcript,
215
+ type: "partial"
216
+ };
217
+ case "replay":
218
+ return {
219
+ assistantTexts: message.assistantTexts,
220
+ call: message.call,
221
+ partial: message.partial,
222
+ scenarioId: message.scenarioId,
223
+ sessionId: message.sessionId,
224
+ sessionMetadata: message.sessionMetadata,
225
+ status: message.status,
226
+ turns: message.turns,
227
+ type: "replay"
228
+ };
229
+ case "session":
230
+ return {
231
+ sessionId: message.sessionId,
232
+ sessionMetadata: message.sessionMetadata,
233
+ scenarioId: message.scenarioId,
234
+ status: message.status,
235
+ type: "session"
236
+ };
237
+ case "turn":
238
+ return {
239
+ turn: message.turn,
240
+ type: "turn"
241
+ };
242
+ default:
243
+ return null;
244
+ }
245
+ };
246
+
247
+ // node_modules/@absolutejs/media/dist/index.js
248
+ var pushIssue = (issues, severity, code, message) => {
249
+ issues.push({ code, message, severity });
250
+ };
251
+ var average = (values) => values.length === 0 ? undefined : values.reduce((total, value) => total + value, 0) / values.length;
252
+ var max = (values) => values.length === 0 ? undefined : Math.max(...values);
253
+ var numericStat = (stat, key) => {
254
+ const value = stat[key];
255
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
256
+ };
257
+ var booleanStat = (stat, key) => {
258
+ const value = stat[key];
259
+ return typeof value === "boolean" ? value : undefined;
260
+ };
261
+ var stringStat = (stat, key) => {
262
+ const value = stat[key];
263
+ return typeof value === "string" ? value : undefined;
264
+ };
265
+ var statKey = (stat) => String(stat.id ?? stringStat(stat, "ssrc") ?? numericStat(stat, "ssrc") ?? stringStat(stat, "trackIdentifier") ?? stringStat(stat, "mid") ?? "unknown");
266
+ var secondsToMs = (value) => value === undefined ? undefined : value * 1000;
267
+ var normalizeWebRTCStat = (stat) => {
268
+ const sample = {};
269
+ for (const [key, value] of Object.entries(stat)) {
270
+ if (value === null || typeof value === "boolean" || typeof value === "number" || typeof value === "string") {
271
+ sample[key] = value;
272
+ }
273
+ }
274
+ return sample;
275
+ };
276
+ var buildMediaWebRTCStatsReport = (input = {}) => {
277
+ const stats = input.stats ?? [];
278
+ const issues = [];
279
+ const inbound = stats.filter((stat) => stat.type === "inbound-rtp" && stringStat(stat, "kind") !== "video");
280
+ const outbound = stats.filter((stat) => stat.type === "outbound-rtp" && stringStat(stat, "kind") !== "video");
281
+ const candidatePairs = stats.filter((stat) => stat.type === "candidate-pair");
282
+ const audioTracks = stats.filter((stat) => (stat.type === "track" || stat.type === "media-source") && stringStat(stat, "kind") === "audio");
283
+ const activeCandidatePairs = candidatePairs.filter((stat) => booleanStat(stat, "selected") === true || booleanStat(stat, "nominated") === true || stringStat(stat, "state") === "succeeded").length;
284
+ const liveAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") !== "ended" && stringStat(stat, "trackState") !== "ended" && booleanStat(stat, "ended") !== true).length;
285
+ const endedAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") === "ended" || stringStat(stat, "trackState") === "ended" || booleanStat(stat, "ended") === true).length;
286
+ const inboundPackets = inbound.reduce((total, stat) => total + (numericStat(stat, "packetsReceived") ?? 0), 0);
287
+ const outboundPackets = outbound.reduce((total, stat) => total + (numericStat(stat, "packetsSent") ?? 0), 0);
288
+ const packetsLost = [...inbound, ...outbound].reduce((total, stat) => total + Math.max(0, numericStat(stat, "packetsLost") ?? 0), 0);
289
+ const packetLossDenominator = inboundPackets + packetsLost;
290
+ const packetLossRatio = packetLossDenominator === 0 ? 0 : packetsLost / packetLossDenominator;
291
+ const bytesReceived = inbound.reduce((total, stat) => total + (numericStat(stat, "bytesReceived") ?? 0), 0);
292
+ const bytesSent = outbound.reduce((total, stat) => total + (numericStat(stat, "bytesSent") ?? 0), 0);
293
+ const roundTripTimeMs = max(candidatePairs.map((stat) => secondsToMs(numericStat(stat, "currentRoundTripTime") ?? numericStat(stat, "roundTripTime"))).filter((value) => value !== undefined));
294
+ const jitterMs = max([...inbound, ...outbound].map((stat) => secondsToMs(numericStat(stat, "jitter"))).filter((value) => value !== undefined));
295
+ const jitterBufferDelayMs = max(inbound.map((stat) => {
296
+ const delay = numericStat(stat, "jitterBufferDelay");
297
+ const emitted = numericStat(stat, "jitterBufferEmittedCount");
298
+ return delay !== undefined && emitted !== undefined && emitted > 0 ? delay / emitted * 1000 : undefined;
299
+ }).filter((value) => value !== undefined));
300
+ const audioLevels = audioTracks.map((stat) => numericStat(stat, "audioLevel")).filter((value) => value !== undefined);
301
+ if (input.requireConnectedCandidatePair && candidatePairs.length > 0 && activeCandidatePairs === 0) {
302
+ pushIssue(issues, "error", "media.webrtc_candidate_pair_missing", "No active WebRTC candidate pair was observed.");
303
+ }
304
+ if (input.requireLiveAudioTrack && liveAudioTracks === 0) {
305
+ pushIssue(issues, "error", "media.webrtc_audio_track_missing", "No live WebRTC audio track was observed.");
306
+ }
307
+ if (input.maxPacketLossRatio !== undefined && packetLossRatio > input.maxPacketLossRatio) {
308
+ pushIssue(issues, "warning", "media.webrtc_packet_loss", `Observed WebRTC packet loss ratio ${String(packetLossRatio)} above ${String(input.maxPacketLossRatio)}.`);
309
+ }
310
+ if (input.maxRoundTripTimeMs !== undefined && roundTripTimeMs !== undefined && roundTripTimeMs > input.maxRoundTripTimeMs) {
311
+ pushIssue(issues, "warning", "media.webrtc_round_trip_time", `Observed WebRTC RTT ${String(roundTripTimeMs)}ms above ${String(input.maxRoundTripTimeMs)}ms.`);
312
+ }
313
+ if (input.maxJitterMs !== undefined && jitterMs !== undefined && jitterMs > input.maxJitterMs) {
314
+ pushIssue(issues, "warning", "media.webrtc_jitter", `Observed WebRTC jitter ${String(jitterMs)}ms above ${String(input.maxJitterMs)}ms.`);
315
+ }
316
+ return {
317
+ activeCandidatePairs,
318
+ audioLevelAverage: average(audioLevels),
319
+ bytesReceived,
320
+ bytesSent,
321
+ checkedAt: Date.now(),
322
+ endedAudioTracks,
323
+ inboundPackets,
324
+ issues,
325
+ jitterBufferDelayMs,
326
+ jitterMs,
327
+ liveAudioTracks,
328
+ outboundPackets,
329
+ packetLossRatio,
330
+ packetsLost,
331
+ roundTripTimeMs,
332
+ status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
333
+ totalStats: stats.length
334
+ };
335
+ };
336
+ var collectMediaWebRTCStats = async (input) => {
337
+ const report = await input.peerConnection.getStats(input.selector ?? null);
338
+ return [...report.values()].map(normalizeWebRTCStat);
339
+ };
340
+ var buildMediaWebRTCStreamContinuityReport = (input = {}) => {
341
+ const stats = input.stats ?? [];
342
+ const previousStats = input.previousStats ?? [];
343
+ const issues = [];
344
+ const previousByKey = new Map(previousStats.map((stat) => [statKey(stat), stat]));
345
+ const audioRtp = stats.filter((stat) => (stat.type === "inbound-rtp" || stat.type === "outbound-rtp") && stringStat(stat, "kind") !== "video" && stringStat(stat, "mediaType") !== "video");
346
+ const streams = audioRtp.map((stat) => {
347
+ const direction = stat.type === "outbound-rtp" ? "outbound" : "inbound";
348
+ const packetsKey = direction === "outbound" ? "packetsSent" : "packetsReceived";
349
+ const bytesKey = direction === "outbound" ? "bytesSent" : "bytesReceived";
350
+ const previous = previousByKey.get(statKey(stat));
351
+ const currentPackets = numericStat(stat, packetsKey);
352
+ const previousPackets = previous ? numericStat(previous, packetsKey) : undefined;
353
+ const currentBytes = numericStat(stat, bytesKey);
354
+ const previousBytes = previous ? numericStat(previous, bytesKey) : undefined;
355
+ const timeDeltaMs = stat.timestamp !== undefined && previous?.timestamp !== undefined ? stat.timestamp - previous.timestamp : undefined;
356
+ return {
357
+ bytesDelta: currentBytes !== undefined && previousBytes !== undefined ? currentBytes - previousBytes : undefined,
358
+ currentPackets,
359
+ direction,
360
+ id: statKey(stat),
361
+ packetDelta: currentPackets !== undefined && previousPackets !== undefined ? currentPackets - previousPackets : undefined,
362
+ previousPackets,
363
+ timeDeltaMs
364
+ };
365
+ });
366
+ const inbound = streams.filter((stream) => stream.direction === "inbound");
367
+ const outbound = streams.filter((stream) => stream.direction === "outbound");
368
+ const maxObservedGapMs = max(streams.map((stream) => stream.timeDeltaMs).filter((value) => value !== undefined));
369
+ const stalledInboundStreams = inbound.filter((stream) => input.maxInboundPacketStallMs !== undefined && stream.timeDeltaMs !== undefined && stream.timeDeltaMs >= input.maxInboundPacketStallMs && stream.packetDelta !== undefined && stream.packetDelta <= 0).length;
370
+ const stalledOutboundStreams = outbound.filter((stream) => input.maxOutboundPacketStallMs !== undefined && stream.timeDeltaMs !== undefined && stream.timeDeltaMs >= input.maxOutboundPacketStallMs && stream.packetDelta !== undefined && stream.packetDelta <= 0).length;
371
+ if (input.requireInboundAudio && inbound.length === 0) {
372
+ pushIssue(issues, "error", "media.webrtc_inbound_audio_missing", "No inbound WebRTC audio RTP stream was observed.");
373
+ }
374
+ if (input.requireOutboundAudio && outbound.length === 0) {
375
+ pushIssue(issues, "error", "media.webrtc_outbound_audio_missing", "No outbound WebRTC audio RTP stream was observed.");
376
+ }
377
+ if (input.maxGapMs !== undefined && maxObservedGapMs !== undefined && maxObservedGapMs > input.maxGapMs) {
378
+ pushIssue(issues, "warning", "media.webrtc_stream_gap", `Observed WebRTC stream sample gap ${String(maxObservedGapMs)}ms above ${String(input.maxGapMs)}ms.`);
379
+ }
380
+ if (stalledInboundStreams > 0) {
381
+ pushIssue(issues, "error", "media.webrtc_inbound_stalled", `${String(stalledInboundStreams)} inbound WebRTC audio stream(s) stopped receiving packets.`);
382
+ }
383
+ if (stalledOutboundStreams > 0) {
384
+ pushIssue(issues, "error", "media.webrtc_outbound_stalled", `${String(stalledOutboundStreams)} outbound WebRTC audio stream(s) stopped sending packets.`);
385
+ }
386
+ return {
387
+ checkedAt: Date.now(),
388
+ inboundAudioStreams: inbound.length,
389
+ issues,
390
+ maxObservedGapMs,
391
+ outboundAudioStreams: outbound.length,
392
+ stalledInboundStreams,
393
+ stalledOutboundStreams,
394
+ status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
395
+ streams,
396
+ totalStats: stats.length
397
+ };
398
+ };
399
+
400
+ // src/client/browserMedia.ts
401
+ var DEFAULT_BROWSER_MEDIA_PATH = "/api/voice/browser-media";
402
+ var DEFAULT_BROWSER_MEDIA_INTERVAL_MS = 5000;
403
+ var resolvePeerConnection = async (options) => options.peerConnection ?? await options.getPeerConnection?.() ?? null;
404
+ var postBrowserMediaReport = async (payload, options) => {
405
+ const requestFetch = options.fetch ?? globalThis.fetch;
406
+ if (!requestFetch) {
407
+ return;
408
+ }
409
+ await requestFetch(options.path ?? DEFAULT_BROWSER_MEDIA_PATH, {
410
+ body: JSON.stringify(payload),
411
+ headers: {
412
+ "Content-Type": "application/json"
413
+ },
414
+ keepalive: true,
415
+ method: "POST"
416
+ });
417
+ };
418
+ var createVoiceBrowserMediaReporter = (options) => {
419
+ let interval = null;
420
+ let previousStats = [];
421
+ const reportOnce = async () => {
422
+ const peerConnection = await resolvePeerConnection(options);
423
+ if (!peerConnection) {
424
+ return;
425
+ }
426
+ const stats = await collectMediaWebRTCStats({ peerConnection });
427
+ const report = buildMediaWebRTCStatsReport({
428
+ ...options,
429
+ stats
430
+ });
431
+ const continuity = options.continuity === false ? undefined : buildMediaWebRTCStreamContinuityReport({
432
+ ...options.continuity,
433
+ previousStats,
434
+ stats
435
+ });
436
+ const payload = {
437
+ at: Date.now(),
438
+ continuity,
439
+ report,
440
+ scenarioId: options.getScenarioId?.() ?? null,
441
+ sessionId: options.getSessionId?.() ?? null
442
+ };
443
+ previousStats = stats;
444
+ options.onReport?.(payload);
445
+ await postBrowserMediaReport(payload, options);
446
+ return payload;
447
+ };
448
+ const run = () => {
449
+ reportOnce().catch((error) => {
450
+ options.onError?.(error);
451
+ });
452
+ };
453
+ const stop = () => {
454
+ if (interval) {
455
+ clearInterval(interval);
456
+ interval = null;
457
+ }
458
+ };
459
+ return {
460
+ close: stop,
461
+ reportOnce,
462
+ start: () => {
463
+ if (interval) {
464
+ return;
465
+ }
466
+ run();
467
+ interval = setInterval(run, options.intervalMs ?? DEFAULT_BROWSER_MEDIA_INTERVAL_MS);
468
+ },
469
+ stop
470
+ };
471
+ };
472
+
473
+ // src/client/connection.ts
474
+ var WS_OPEN = 1;
475
+ var WS_CLOSED = 3;
476
+ var WS_NORMAL_CLOSURE = 1000;
477
+ var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
478
+ var DEFAULT_PING_INTERVAL = 30000;
479
+ var RECONNECT_DELAY_MS = 500;
480
+ var DEFAULT_SCENARIO_QUERY_PARAM = "scenarioId";
481
+ var noop = () => {};
482
+ var noopUnsubscribe = () => noop;
483
+ var NOOP_CONNECTION = {
484
+ callControl: noop,
485
+ close: noop,
486
+ endTurn: noop,
487
+ getReadyState: () => WS_CLOSED,
488
+ getScenarioId: () => "",
489
+ getSessionId: () => "",
490
+ send: noop,
491
+ sendAudio: noop,
492
+ simulateDisconnect: noop,
493
+ start: () => {},
494
+ subscribe: noopUnsubscribe
495
+ };
496
+ var createSessionId = () => crypto.randomUUID();
497
+ var buildWsUrl = (path, sessionId, scenarioId) => {
498
+ const { hostname, port, protocol } = window.location;
499
+ const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
500
+ const portSuffix = port ? `:${port}` : "";
501
+ const url = new URL(`${wsProtocol}//${hostname}${portSuffix}${path}`);
502
+ url.searchParams.set("sessionId", sessionId);
503
+ if (scenarioId) {
504
+ url.searchParams.set(DEFAULT_SCENARIO_QUERY_PARAM, scenarioId);
505
+ }
506
+ return url.toString();
507
+ };
508
+ var isVoiceServerMessage = (value) => {
509
+ if (!value || typeof value !== "object" || !("type" in value)) {
510
+ return false;
511
+ }
512
+ switch (value.type) {
513
+ case "audio":
514
+ case "assistant":
515
+ case "call_lifecycle":
516
+ case "complete":
517
+ case "connection":
518
+ case "error":
519
+ case "final":
520
+ case "partial":
521
+ case "pong":
522
+ case "replay":
523
+ case "session":
524
+ case "turn":
525
+ return true;
526
+ default:
527
+ return false;
528
+ }
529
+ };
530
+ var parseServerMessage = (event) => {
531
+ if (typeof event.data !== "string") {
532
+ return null;
533
+ }
534
+ try {
535
+ const parsed = JSON.parse(event.data);
536
+ return isVoiceServerMessage(parsed) ? parsed : null;
537
+ } catch {
538
+ return null;
539
+ }
540
+ };
541
+ var createVoiceConnection = (path, options = {}) => {
542
+ if (typeof window === "undefined") {
543
+ return NOOP_CONNECTION;
544
+ }
545
+ const listeners = new Set;
546
+ const shouldReconnect = options.reconnect !== false;
547
+ const maxReconnectAttempts = options.maxReconnectAttempts ?? DEFAULT_MAX_RECONNECT_ATTEMPTS;
548
+ const pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
549
+ const state = {
550
+ isConnected: false,
551
+ pendingMessages: [],
552
+ scenarioId: options.scenarioId ?? null,
553
+ pingInterval: null,
554
+ reconnectAttempts: 0,
555
+ reconnectTimeout: null,
556
+ sessionId: options.sessionId ?? createSessionId(),
557
+ ws: null
558
+ };
559
+ const emitConnection = (reconnect) => {
560
+ listeners.forEach((listener) => listener(reconnect));
561
+ };
562
+ const clearTimers = () => {
563
+ if (state.pingInterval) {
564
+ clearInterval(state.pingInterval);
565
+ state.pingInterval = null;
566
+ }
567
+ if (state.reconnectTimeout) {
568
+ clearTimeout(state.reconnectTimeout);
569
+ state.reconnectTimeout = null;
570
+ }
571
+ };
572
+ const flushPendingMessages = () => {
573
+ if (state.ws?.readyState !== WS_OPEN) {
574
+ return;
575
+ }
576
+ while (state.pendingMessages.length > 0) {
577
+ const next = state.pendingMessages.shift();
578
+ if (next !== undefined) {
579
+ state.ws.send(next);
580
+ }
581
+ }
582
+ };
583
+ const scheduleReconnect = () => {
584
+ const nextAttemptAt = Date.now() + RECONNECT_DELAY_MS;
585
+ state.reconnectAttempts += 1;
586
+ emitConnection({
587
+ reconnect: {
588
+ attempts: state.reconnectAttempts,
589
+ lastDisconnectAt: Date.now(),
590
+ maxAttempts: maxReconnectAttempts,
591
+ nextAttemptAt,
592
+ status: "reconnecting"
593
+ },
594
+ type: "connection"
595
+ });
596
+ state.reconnectTimeout = setTimeout(() => {
597
+ if (state.reconnectAttempts > maxReconnectAttempts) {
598
+ emitConnection({
599
+ reconnect: {
600
+ attempts: state.reconnectAttempts,
601
+ maxAttempts: maxReconnectAttempts,
602
+ status: "exhausted"
603
+ },
604
+ type: "connection"
605
+ });
606
+ return;
607
+ }
608
+ connect();
609
+ }, RECONNECT_DELAY_MS);
610
+ };
611
+ const connect = () => {
612
+ const ws = new WebSocket(buildWsUrl(path, state.sessionId, state.scenarioId));
613
+ ws.binaryType = "arraybuffer";
614
+ ws.onopen = () => {
615
+ const wasReconnecting = state.reconnectAttempts > 0;
616
+ state.isConnected = true;
617
+ flushPendingMessages();
618
+ if (wasReconnecting) {
619
+ emitConnection({
620
+ reconnect: {
621
+ attempts: state.reconnectAttempts,
622
+ lastResumedAt: Date.now(),
623
+ maxAttempts: maxReconnectAttempts,
624
+ status: "resumed"
625
+ },
626
+ type: "connection"
627
+ });
628
+ state.reconnectAttempts = 0;
629
+ }
630
+ listeners.forEach((listener) => listener({
631
+ scenarioId: state.scenarioId ?? undefined,
632
+ sessionId: state.sessionId,
633
+ status: "active",
634
+ type: "session"
635
+ }));
636
+ state.pingInterval = setInterval(() => {
637
+ if (ws.readyState === WS_OPEN) {
638
+ ws.send(JSON.stringify({ type: "ping" }));
639
+ }
640
+ }, pingInterval);
641
+ };
642
+ ws.onmessage = (event) => {
643
+ const parsed = parseServerMessage(event);
644
+ if (!parsed) {
645
+ return;
646
+ }
647
+ if (parsed.type === "session") {
648
+ state.sessionId = parsed.sessionId;
649
+ state.scenarioId = parsed.scenarioId ?? state.scenarioId;
650
+ }
651
+ listeners.forEach((listener) => listener(parsed));
652
+ };
653
+ ws.onclose = (event) => {
654
+ state.isConnected = false;
655
+ clearTimers();
656
+ const reconnectable = shouldReconnect && event.code !== WS_NORMAL_CLOSURE && state.reconnectAttempts < maxReconnectAttempts;
657
+ if (reconnectable) {
658
+ scheduleReconnect();
659
+ } else if (shouldReconnect && event.code !== WS_NORMAL_CLOSURE) {
660
+ emitConnection({
661
+ reconnect: {
662
+ attempts: state.reconnectAttempts,
663
+ lastDisconnectAt: Date.now(),
664
+ maxAttempts: maxReconnectAttempts,
665
+ status: "exhausted"
666
+ },
667
+ type: "connection"
668
+ });
669
+ }
670
+ };
671
+ state.ws = ws;
672
+ };
673
+ const sendSerialized = (value) => {
674
+ if (state.ws?.readyState === WS_OPEN) {
675
+ state.ws.send(value);
676
+ return;
677
+ }
678
+ state.pendingMessages.push(value);
679
+ };
680
+ const send = (message) => {
681
+ sendSerialized(JSON.stringify(message));
682
+ };
683
+ const start = (input = {}) => {
684
+ if (input.sessionId) {
685
+ state.sessionId = input.sessionId;
686
+ }
687
+ if (input.scenarioId) {
688
+ state.scenarioId = input.scenarioId;
689
+ }
690
+ send({
691
+ type: "start",
692
+ sessionId: state.sessionId,
693
+ scenarioId: state.scenarioId ?? undefined
694
+ });
695
+ };
696
+ const sendAudio = (audio) => {
697
+ sendSerialized(audio);
698
+ };
699
+ const endTurn = () => {
700
+ send({ type: "end_turn" });
701
+ };
702
+ const callControl = (message) => {
703
+ send({
704
+ ...message,
705
+ type: "call_control"
706
+ });
707
+ };
708
+ const close = () => {
709
+ clearTimers();
710
+ if (state.ws) {
711
+ state.ws.close(WS_NORMAL_CLOSURE);
712
+ state.ws = null;
713
+ }
714
+ state.isConnected = false;
715
+ listeners.clear();
716
+ };
717
+ const simulateDisconnect = () => {
718
+ if (state.ws?.readyState === WS_OPEN) {
719
+ state.ws.close(4000, "absolutejs-voice-reconnect-proof");
720
+ }
721
+ };
722
+ const subscribe = (callback) => {
723
+ listeners.add(callback);
724
+ return () => {
725
+ listeners.delete(callback);
726
+ };
727
+ };
728
+ connect();
729
+ return {
730
+ callControl,
731
+ close,
732
+ endTurn,
733
+ getReadyState: () => state.ws?.readyState ?? WS_CLOSED,
734
+ getScenarioId: () => state.scenarioId ?? "",
735
+ getSessionId: () => state.sessionId,
736
+ send,
737
+ sendAudio,
738
+ simulateDisconnect,
739
+ start,
740
+ subscribe
741
+ };
742
+ };
743
+
744
+ // src/client/store.ts
745
+ var createInitialReconnectState = () => ({
746
+ attempts: 0,
747
+ maxAttempts: 0,
748
+ status: "idle"
749
+ });
750
+ var createInitialState = () => ({
751
+ assistantAudio: [],
752
+ assistantTexts: [],
753
+ call: null,
754
+ error: null,
755
+ isConnected: false,
756
+ sessionMetadata: null,
757
+ scenarioId: null,
758
+ partial: "",
759
+ reconnect: createInitialReconnectState(),
760
+ sessionId: null,
761
+ status: "idle",
762
+ turns: []
763
+ });
764
+ var createVoiceStreamStore = () => {
765
+ let state = createInitialState();
766
+ const subscribers = new Set;
767
+ const notify = () => {
768
+ subscribers.forEach((subscriber) => subscriber());
769
+ };
770
+ const dispatch = (action) => {
771
+ switch (action.type) {
772
+ case "audio":
773
+ state = {
774
+ ...state,
775
+ assistantAudio: [
776
+ ...state.assistantAudio,
777
+ {
778
+ chunk: action.chunk,
779
+ format: action.format,
780
+ receivedAt: action.receivedAt,
781
+ turnId: action.turnId
782
+ }
783
+ ]
784
+ };
785
+ break;
786
+ case "assistant":
787
+ state = {
788
+ ...state,
789
+ assistantTexts: [...state.assistantTexts, action.text]
790
+ };
791
+ break;
792
+ case "complete":
793
+ state = {
794
+ ...state,
795
+ sessionId: action.sessionId,
796
+ status: "completed"
797
+ };
798
+ break;
799
+ case "call_lifecycle":
800
+ state = {
801
+ ...state,
802
+ call: {
803
+ ...state.call,
804
+ disposition: action.event.type === "end" ? action.event.disposition : state.call?.disposition,
805
+ endedAt: action.event.type === "end" ? action.event.at : state.call?.endedAt,
806
+ events: [...state.call?.events ?? [], action.event],
807
+ lastEventAt: action.event.at,
808
+ startedAt: state.call?.startedAt ?? action.event.at
809
+ },
810
+ sessionId: action.sessionId
811
+ };
812
+ break;
813
+ case "connected":
814
+ state = {
815
+ ...state,
816
+ isConnected: true,
817
+ reconnect: state.reconnect.status === "reconnecting" ? {
818
+ ...state.reconnect,
819
+ lastResumedAt: Date.now(),
820
+ nextAttemptAt: undefined,
821
+ status: "resumed"
822
+ } : state.reconnect
823
+ };
824
+ break;
825
+ case "connection":
826
+ state = {
827
+ ...state,
828
+ reconnect: action.reconnect
829
+ };
830
+ break;
831
+ case "disconnected":
832
+ state = {
833
+ ...state,
834
+ isConnected: false
835
+ };
836
+ break;
837
+ case "error":
838
+ state = {
839
+ ...state,
840
+ error: action.message
841
+ };
842
+ break;
843
+ case "final":
844
+ state = {
845
+ ...state,
846
+ partial: action.transcript.text,
847
+ turns: state.turns.map((turn) => turn)
848
+ };
849
+ break;
850
+ case "partial":
851
+ state = {
852
+ ...state,
853
+ partial: action.transcript.text
854
+ };
855
+ break;
856
+ case "replay":
857
+ state = {
858
+ ...state,
859
+ assistantTexts: [...action.assistantTexts],
860
+ call: action.call ?? null,
861
+ error: null,
862
+ isConnected: action.status === "active",
863
+ partial: action.partial,
864
+ reconnect: state.reconnect.status === "reconnecting" ? {
865
+ ...state.reconnect,
866
+ lastResumedAt: Date.now(),
867
+ nextAttemptAt: undefined,
868
+ status: "resumed"
869
+ } : state.reconnect,
870
+ scenarioId: action.scenarioId ?? state.scenarioId,
871
+ sessionId: action.sessionId,
872
+ sessionMetadata: action.sessionMetadata ?? state.sessionMetadata,
873
+ status: action.status,
874
+ turns: [...action.turns]
875
+ };
876
+ break;
877
+ case "session":
878
+ state = {
879
+ ...state,
880
+ error: null,
881
+ scenarioId: action.scenarioId ?? state.scenarioId,
882
+ isConnected: action.status === "active",
883
+ sessionId: action.sessionId,
884
+ sessionMetadata: action.sessionMetadata ?? state.sessionMetadata,
885
+ status: action.status
886
+ };
887
+ break;
888
+ case "turn":
889
+ state = {
890
+ ...state,
891
+ partial: "",
892
+ turns: [...state.turns, action.turn]
893
+ };
894
+ break;
895
+ }
896
+ notify();
897
+ };
898
+ return {
899
+ dispatch,
900
+ getServerSnapshot: () => state,
901
+ getSnapshot: () => state,
902
+ subscribe: (subscriber) => {
903
+ subscribers.add(subscriber);
904
+ return () => {
905
+ subscribers.delete(subscriber);
906
+ };
907
+ }
908
+ };
909
+ };
910
+
911
+ // src/client/createVoiceStream.ts
912
+ var createVoiceStream = (path, options = {}) => {
913
+ const connection = createVoiceConnection(path, options);
914
+ const store = createVoiceStreamStore();
915
+ const browserMediaReporter = options.browserMedia && typeof window !== "undefined" ? createVoiceBrowserMediaReporter({
916
+ ...options.browserMedia,
917
+ getScenarioId: () => options.browserMedia ? options.browserMedia.getScenarioId?.() ?? connection.getScenarioId() : connection.getScenarioId(),
918
+ getSessionId: () => options.browserMedia ? options.browserMedia.getSessionId?.() ?? connection.getSessionId() : connection.getSessionId()
919
+ }) : null;
920
+ const subscribers = new Set;
921
+ const start = (input) => Promise.resolve().then(() => {
922
+ if (!input?.sessionId && !input?.scenarioId) {
923
+ return;
924
+ }
925
+ connection.start(input);
926
+ browserMediaReporter?.start();
927
+ });
928
+ const notify = () => {
929
+ subscribers.forEach((subscriber) => subscriber());
930
+ };
931
+ const reportReconnect = () => {
932
+ if (!options.reconnectReportPath || typeof fetch === "undefined") {
933
+ return;
934
+ }
935
+ const snapshot = store.getSnapshot();
936
+ const body = JSON.stringify({
937
+ at: Date.now(),
938
+ reconnect: snapshot.reconnect,
939
+ scenarioId: snapshot.scenarioId,
940
+ sessionId: connection.getSessionId(),
941
+ turnIds: snapshot.turns.map((turn) => turn.id)
942
+ });
943
+ fetch(options.reconnectReportPath, {
944
+ body,
945
+ headers: {
946
+ "Content-Type": "application/json"
947
+ },
948
+ keepalive: true,
949
+ method: "POST"
950
+ }).catch(() => {});
951
+ };
952
+ const unsubscribeConnection = connection.subscribe((message) => {
953
+ const action = serverMessageToAction(message);
954
+ if (action) {
955
+ store.dispatch(action);
956
+ if (message.type === "connection") {
957
+ reportReconnect();
958
+ }
959
+ notify();
960
+ }
961
+ });
962
+ return {
963
+ callControl(message) {
964
+ connection.callControl(message);
965
+ },
966
+ close() {
967
+ unsubscribeConnection();
968
+ browserMediaReporter?.close();
969
+ connection.close();
970
+ store.dispatch({ type: "disconnected" });
971
+ notify();
972
+ },
973
+ endTurn() {
974
+ connection.endTurn();
975
+ },
976
+ get error() {
977
+ return store.getSnapshot().error;
978
+ },
979
+ getServerSnapshot() {
980
+ return store.getServerSnapshot();
981
+ },
982
+ getSnapshot() {
983
+ return store.getSnapshot();
984
+ },
985
+ get isConnected() {
986
+ return store.getSnapshot().isConnected;
987
+ },
988
+ get scenarioId() {
989
+ return store.getSnapshot().scenarioId;
990
+ },
991
+ get sessionMetadata() {
992
+ return store.getSnapshot().sessionMetadata;
993
+ },
994
+ start,
995
+ get partial() {
996
+ return store.getSnapshot().partial;
997
+ },
998
+ get reconnect() {
999
+ return store.getSnapshot().reconnect;
1000
+ },
1001
+ get sessionId() {
1002
+ return connection.getSessionId();
1003
+ },
1004
+ get status() {
1005
+ return store.getSnapshot().status;
1006
+ },
1007
+ get turns() {
1008
+ return store.getSnapshot().turns;
1009
+ },
1010
+ get assistantTexts() {
1011
+ return store.getSnapshot().assistantTexts;
1012
+ },
1013
+ get assistantAudio() {
1014
+ return store.getSnapshot().assistantAudio;
1015
+ },
1016
+ get call() {
1017
+ return store.getSnapshot().call;
1018
+ },
1019
+ sendAudio(audio) {
1020
+ connection.sendAudio(audio);
1021
+ },
1022
+ simulateDisconnect() {
1023
+ connection.simulateDisconnect();
1024
+ },
1025
+ subscribe(subscriber) {
1026
+ subscribers.add(subscriber);
1027
+ return () => {
1028
+ subscribers.delete(subscriber);
1029
+ };
1030
+ }
1031
+ };
1032
+ };
1033
+
1034
+ // src/audioConditioning.ts
1035
+ var DEFAULT_TARGET_LEVEL = 0.08;
1036
+ var DEFAULT_MAX_GAIN = 3;
1037
+ var DEFAULT_NOISE_GATE_THRESHOLD = 0.006;
1038
+ var DEFAULT_NOISE_GATE_ATTENUATION = 0.15;
1039
+ var resolveAudioConditioningConfig = (config) => {
1040
+ if (!config || config.enabled === false) {
1041
+ return;
1042
+ }
1043
+ return {
1044
+ enabled: true,
1045
+ maxGain: config.maxGain ?? DEFAULT_MAX_GAIN,
1046
+ noiseGateAttenuation: config.noiseGateAttenuation ?? DEFAULT_NOISE_GATE_ATTENUATION,
1047
+ noiseGateThreshold: config.noiseGateThreshold ?? DEFAULT_NOISE_GATE_THRESHOLD,
1048
+ targetLevel: config.targetLevel ?? DEFAULT_TARGET_LEVEL
1049
+ };
1050
+ };
1051
+
1052
+ // src/turnProfiles.ts
1053
+ var TURN_PROFILE_DEFAULTS = {
1054
+ balanced: {
1055
+ qualityProfile: "general",
1056
+ silenceMs: 1400,
1057
+ speechThreshold: 0.012,
1058
+ transcriptStabilityMs: 1000
1059
+ },
1060
+ fast: {
1061
+ qualityProfile: "general",
1062
+ silenceMs: 700,
1063
+ speechThreshold: 0.015,
1064
+ transcriptStabilityMs: 450
1065
+ },
1066
+ "long-form": {
1067
+ qualityProfile: "general",
1068
+ silenceMs: 2200,
1069
+ speechThreshold: 0.01,
1070
+ transcriptStabilityMs: 1500
1071
+ }
1072
+ };
1073
+ var QUALITY_PROFILE_DEFAULTS = {
1074
+ general: {},
1075
+ "accent-heavy": {
1076
+ silenceMs: 1200,
1077
+ speechThreshold: 0.01,
1078
+ transcriptStabilityMs: 1200
1079
+ },
1080
+ "noisy-room": {
1081
+ silenceMs: 2000,
1082
+ speechThreshold: 0.02,
1083
+ transcriptStabilityMs: 1600
1084
+ },
1085
+ "short-command": {
1086
+ silenceMs: 500,
1087
+ speechThreshold: 0.016,
1088
+ transcriptStabilityMs: 420
1089
+ }
1090
+ };
1091
+ var DEFAULT_TURN_PROFILE = "fast";
1092
+ var DEFAULT_QUALITY_PROFILE = "general";
1093
+ var resolveTurnDetectionConfig = (config) => {
1094
+ const profile = config?.profile ?? DEFAULT_TURN_PROFILE;
1095
+ const qualityProfile = config?.qualityProfile ?? DEFAULT_QUALITY_PROFILE;
1096
+ const preset = TURN_PROFILE_DEFAULTS[profile];
1097
+ const quality = QUALITY_PROFILE_DEFAULTS[qualityProfile];
1098
+ return {
1099
+ profile,
1100
+ qualityProfile,
1101
+ silenceMs: config?.silenceMs ?? quality.silenceMs ?? preset.silenceMs,
1102
+ speechThreshold: config?.speechThreshold ?? quality.speechThreshold ?? preset.speechThreshold,
1103
+ transcriptStabilityMs: config?.transcriptStabilityMs ?? quality.transcriptStabilityMs ?? preset.transcriptStabilityMs
1104
+ };
1105
+ };
1106
+
1107
+ // src/presets.ts
1108
+ var PRESET_INPUTS = {
1109
+ chat: {
1110
+ audioConditioning: {
1111
+ enabled: true,
1112
+ maxGain: 2.5,
1113
+ noiseGateAttenuation: 0,
1114
+ noiseGateThreshold: 0.004,
1115
+ targetLevel: 0.08
1116
+ },
1117
+ capture: {
1118
+ channelCount: 1,
1119
+ sampleRateHz: 16000
1120
+ },
1121
+ connection: {
1122
+ maxReconnectAttempts: 10,
1123
+ pingInterval: 30000,
1124
+ reconnect: true
1125
+ },
1126
+ sttLifecycle: "continuous",
1127
+ turnDetection: {
1128
+ qualityProfile: "short-command",
1129
+ profile: "balanced"
1130
+ }
1131
+ },
1132
+ default: {
1133
+ capture: {
1134
+ channelCount: 1,
1135
+ sampleRateHz: 16000
1136
+ },
1137
+ connection: {
1138
+ maxReconnectAttempts: 10,
1139
+ pingInterval: 30000,
1140
+ reconnect: true
1141
+ },
1142
+ sttLifecycle: "continuous",
1143
+ turnDetection: {
1144
+ qualityProfile: "general",
1145
+ profile: "fast"
1146
+ }
1147
+ },
1148
+ dictation: {
1149
+ audioConditioning: {
1150
+ enabled: true,
1151
+ maxGain: 2.25,
1152
+ noiseGateAttenuation: 0.05,
1153
+ noiseGateThreshold: 0.003,
1154
+ targetLevel: 0.08
1155
+ },
1156
+ capture: {
1157
+ channelCount: 1,
1158
+ sampleRateHz: 16000
1159
+ },
1160
+ connection: {
1161
+ maxReconnectAttempts: 12,
1162
+ pingInterval: 30000,
1163
+ reconnect: true
1164
+ },
1165
+ sttLifecycle: "continuous",
1166
+ turnDetection: {
1167
+ qualityProfile: "accent-heavy",
1168
+ profile: "long-form"
1169
+ }
1170
+ },
1171
+ "guided-intake": {
1172
+ audioConditioning: {
1173
+ enabled: true,
1174
+ maxGain: 2.5,
1175
+ noiseGateAttenuation: 0,
1176
+ noiseGateThreshold: 0.004,
1177
+ targetLevel: 0.08
1178
+ },
1179
+ capture: {
1180
+ channelCount: 1,
1181
+ sampleRateHz: 16000
1182
+ },
1183
+ connection: {
1184
+ maxReconnectAttempts: 12,
1185
+ pingInterval: 30000,
1186
+ reconnect: true
1187
+ },
1188
+ sttLifecycle: "turn-scoped",
1189
+ turnDetection: {
1190
+ qualityProfile: "accent-heavy",
1191
+ profile: "long-form"
1192
+ }
1193
+ },
1194
+ "noisy-room": {
1195
+ audioConditioning: {
1196
+ enabled: true,
1197
+ maxGain: 3,
1198
+ noiseGateAttenuation: 0.12,
1199
+ noiseGateThreshold: 0.006,
1200
+ targetLevel: 0.085
1201
+ },
1202
+ capture: {
1203
+ channelCount: 1,
1204
+ sampleRateHz: 16000
1205
+ },
1206
+ connection: {
1207
+ maxReconnectAttempts: 14,
1208
+ pingInterval: 45000,
1209
+ reconnect: true
1210
+ },
1211
+ sttLifecycle: "continuous",
1212
+ turnDetection: {
1213
+ qualityProfile: "noisy-room",
1214
+ profile: "long-form",
1215
+ silenceMs: 2100,
1216
+ speechThreshold: 0.02,
1217
+ transcriptStabilityMs: 1650
1218
+ }
1219
+ },
1220
+ "pstn-balanced": {
1221
+ audioConditioning: {
1222
+ enabled: true,
1223
+ maxGain: 2.8,
1224
+ noiseGateAttenuation: 0.07,
1225
+ noiseGateThreshold: 0.005,
1226
+ targetLevel: 0.08
1227
+ },
1228
+ capture: {
1229
+ channelCount: 1,
1230
+ sampleRateHz: 16000
1231
+ },
1232
+ connection: {
1233
+ maxReconnectAttempts: 14,
1234
+ pingInterval: 45000,
1235
+ reconnect: true
1236
+ },
1237
+ sttLifecycle: "continuous",
1238
+ turnDetection: {
1239
+ qualityProfile: "noisy-room",
1240
+ profile: "long-form",
1241
+ silenceMs: 660,
1242
+ speechThreshold: 0.012,
1243
+ transcriptStabilityMs: 300
1244
+ }
1245
+ },
1246
+ "pstn-fast": {
1247
+ audioConditioning: {
1248
+ enabled: true,
1249
+ maxGain: 2.75,
1250
+ noiseGateAttenuation: 0.06,
1251
+ noiseGateThreshold: 0.005,
1252
+ targetLevel: 0.08
1253
+ },
1254
+ capture: {
1255
+ channelCount: 1,
1256
+ sampleRateHz: 16000
1257
+ },
1258
+ connection: {
1259
+ maxReconnectAttempts: 14,
1260
+ pingInterval: 45000,
1261
+ reconnect: true
1262
+ },
1263
+ sttLifecycle: "continuous",
1264
+ turnDetection: {
1265
+ qualityProfile: "noisy-room",
1266
+ profile: "long-form",
1267
+ silenceMs: 620,
1268
+ speechThreshold: 0.012,
1269
+ transcriptStabilityMs: 280
1270
+ }
1271
+ },
1272
+ reliability: {
1273
+ audioConditioning: {
1274
+ enabled: true,
1275
+ maxGain: 2.9,
1276
+ noiseGateAttenuation: 0.08,
1277
+ noiseGateThreshold: 0.005,
1278
+ targetLevel: 0.08
1279
+ },
1280
+ capture: {
1281
+ channelCount: 1,
1282
+ sampleRateHz: 16000
1283
+ },
1284
+ connection: {
1285
+ maxReconnectAttempts: 14,
1286
+ pingInterval: 45000,
1287
+ reconnect: true
1288
+ },
1289
+ sttLifecycle: "continuous",
1290
+ turnDetection: {
1291
+ qualityProfile: "noisy-room",
1292
+ profile: "long-form"
1293
+ }
1294
+ }
1295
+ };
1296
+ var resolveVoiceRuntimePreset = (name = "default") => {
1297
+ const preset = PRESET_INPUTS[name];
1298
+ return {
1299
+ audioConditioning: resolveAudioConditioningConfig(preset.audioConditioning),
1300
+ capture: {
1301
+ channelCount: preset.capture?.channelCount ?? 1,
1302
+ sampleRateHz: preset.capture?.sampleRateHz ?? 16000
1303
+ },
1304
+ connection: {
1305
+ ...preset.connection
1306
+ },
1307
+ name,
1308
+ sttLifecycle: preset.sttLifecycle ?? "continuous",
1309
+ turnDetection: resolveTurnDetectionConfig(preset.turnDetection)
1310
+ };
1311
+ };
1312
+
1313
+ // src/client/controller.ts
1314
+ var createInitialState2 = (stream) => ({
1315
+ assistantAudio: [...stream.assistantAudio],
1316
+ assistantTexts: [...stream.assistantTexts],
1317
+ call: stream.call,
1318
+ error: stream.error,
1319
+ isConnected: stream.isConnected,
1320
+ isRecording: false,
1321
+ partial: stream.partial,
1322
+ reconnect: stream.reconnect,
1323
+ recordingError: null,
1324
+ sessionId: stream.sessionId,
1325
+ sessionMetadata: stream.sessionMetadata,
1326
+ scenarioId: stream.scenarioId,
1327
+ status: stream.status,
1328
+ turns: [...stream.turns]
1329
+ });
1330
+ var createVoiceController = (path, options = {}) => {
1331
+ const preset = resolveVoiceRuntimePreset(options.preset);
1332
+ const stream = createVoiceStream(path, {
1333
+ ...preset.connection,
1334
+ ...options.connection
1335
+ });
1336
+ let capture = null;
1337
+ let state = createInitialState2(stream);
1338
+ const subscribers = new Set;
1339
+ const notify = () => {
1340
+ for (const subscriber of subscribers) {
1341
+ subscriber();
1342
+ }
1343
+ };
1344
+ const sync = () => {
1345
+ state = {
1346
+ ...state,
1347
+ assistantAudio: [...stream.assistantAudio],
1348
+ assistantTexts: [...stream.assistantTexts],
1349
+ call: stream.call,
1350
+ error: stream.error,
1351
+ isConnected: stream.isConnected,
1352
+ partial: stream.partial,
1353
+ reconnect: stream.reconnect,
1354
+ sessionId: stream.sessionId,
1355
+ sessionMetadata: stream.sessionMetadata,
1356
+ scenarioId: stream.scenarioId,
1357
+ status: stream.status,
1358
+ turns: [...stream.turns]
1359
+ };
1360
+ if (options.autoStopOnComplete !== false && state.status === "completed" && state.isRecording) {
1361
+ capture?.stop();
1362
+ capture = null;
1363
+ state = {
1364
+ ...state,
1365
+ isRecording: false
1366
+ };
1367
+ }
1368
+ notify();
1369
+ };
1370
+ const unsubscribeStream = stream.subscribe(sync);
1371
+ sync();
1372
+ const ensureCapture = () => {
1373
+ if (capture) {
1374
+ return capture;
1375
+ }
1376
+ capture = createMicrophoneCapture({
1377
+ channelCount: options.capture?.channelCount ?? preset.capture.channelCount,
1378
+ onLevel: options.capture?.onLevel,
1379
+ onAudio: (audio) => {
1380
+ if (options.capture?.onAudio) {
1381
+ options.capture.onAudio(audio, stream.sendAudio);
1382
+ return;
1383
+ }
1384
+ stream.sendAudio(audio);
1385
+ },
1386
+ sampleRateHz: options.capture?.sampleRateHz ?? preset.capture.sampleRateHz
1387
+ });
1388
+ return capture;
1389
+ };
1390
+ const stopRecording = () => {
1391
+ capture?.stop();
1392
+ capture = null;
1393
+ state = {
1394
+ ...state,
1395
+ isRecording: false
1396
+ };
1397
+ notify();
1398
+ };
1399
+ const startRecording = async () => {
1400
+ if (state.isRecording) {
1401
+ return;
1402
+ }
1403
+ try {
1404
+ state = {
1405
+ ...state,
1406
+ recordingError: null
1407
+ };
1408
+ notify();
1409
+ await ensureCapture().start();
1410
+ state = {
1411
+ ...state,
1412
+ isRecording: true
1413
+ };
1414
+ notify();
1415
+ } catch (error) {
1416
+ capture = null;
1417
+ state = {
1418
+ ...state,
1419
+ isRecording: false,
1420
+ recordingError: error instanceof Error ? error.message : String(error)
1421
+ };
1422
+ notify();
1423
+ throw error;
1424
+ }
1425
+ };
1426
+ const close = () => {
1427
+ unsubscribeStream();
1428
+ stopRecording();
1429
+ stream.close();
1430
+ };
1431
+ return {
1432
+ bindHTMX(bindingOptions) {
1433
+ return bindVoiceHTMX(stream, bindingOptions);
1434
+ },
1435
+ callControl: (message) => stream.callControl(message),
1436
+ close,
1437
+ endTurn: () => stream.endTurn(),
1438
+ get error() {
1439
+ return state.error;
1440
+ },
1441
+ getServerSnapshot: () => state,
1442
+ getSnapshot: () => state,
1443
+ get isConnected() {
1444
+ return state.isConnected;
1445
+ },
1446
+ get isRecording() {
1447
+ return state.isRecording;
1448
+ },
1449
+ get partial() {
1450
+ return state.partial;
1451
+ },
1452
+ get recordingError() {
1453
+ return state.recordingError;
1454
+ },
1455
+ get reconnect() {
1456
+ return state.reconnect;
1457
+ },
1458
+ sendAudio: (audio) => stream.sendAudio(audio),
1459
+ simulateDisconnect: () => stream.simulateDisconnect(),
1460
+ get sessionId() {
1461
+ return state.sessionId;
1462
+ },
1463
+ get sessionMetadata() {
1464
+ return state.sessionMetadata;
1465
+ },
1466
+ get scenarioId() {
1467
+ return state.scenarioId;
1468
+ },
1469
+ startRecording,
1470
+ get status() {
1471
+ return state.status;
1472
+ },
1473
+ stopRecording,
1474
+ subscribe: (subscriber) => {
1475
+ subscribers.add(subscriber);
1476
+ return () => {
1477
+ subscribers.delete(subscriber);
1478
+ };
1479
+ },
1480
+ toggleRecording: async () => {
1481
+ if (state.isRecording) {
1482
+ stopRecording();
1483
+ return;
1484
+ }
1485
+ await startRecording();
1486
+ },
1487
+ get turns() {
1488
+ return state.turns;
1489
+ },
1490
+ get assistantTexts() {
1491
+ return state.assistantTexts;
1492
+ },
1493
+ get assistantAudio() {
1494
+ return state.assistantAudio;
1495
+ },
1496
+ get call() {
1497
+ return state.call;
1498
+ }
1499
+ };
1500
+ };
1501
+
1502
+ // src/agentState.ts
1503
+ var deriveVoiceAgentUIState = (input) => {
1504
+ if (!input.isConnected) {
1505
+ return "idle";
1506
+ }
1507
+ if (input.isPlaying) {
1508
+ return "speaking";
1509
+ }
1510
+ if (input.isRecording && input.hasActivePartial) {
1511
+ return "listening";
1512
+ }
1513
+ if (input.isRecording) {
1514
+ return "listening";
1515
+ }
1516
+ if (input.lastTranscriptAt && !input.lastAssistantAt) {
1517
+ return "thinking";
1518
+ }
1519
+ if (input.lastTranscriptAt && input.lastAssistantAt && input.lastTranscriptAt > input.lastAssistantAt) {
1520
+ return "thinking";
1521
+ }
1522
+ return "idle";
1523
+ };
1524
+
1525
+ // src/client/voiceWidgetView.ts
1526
+ var DEFAULT_VOICE_WIDGET_THEME = {
1527
+ accent: "#3b82f6",
1528
+ background: "#0f172a",
1529
+ errorAccent: "#ef4444",
1530
+ fontFamily: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
1531
+ foreground: "#f8fafc",
1532
+ radius: 16
1533
+ };
1534
+ var DEFAULT_VOICE_WIDGET_LABELS = {
1535
+ callEnded: "Call ended",
1536
+ connecting: "Connecting…",
1537
+ endCall: "End call",
1538
+ idle: "Idle",
1539
+ listening: "Listening",
1540
+ mute: "Mute",
1541
+ speaking: "Speaking",
1542
+ startCall: "Start call",
1543
+ thinking: "Thinking",
1544
+ unmute: "Unmute"
1545
+ };
1546
+ var stateLabel = (state, labels) => {
1547
+ switch (state) {
1548
+ case "listening":
1549
+ return labels.listening;
1550
+ case "speaking":
1551
+ return labels.speaking;
1552
+ case "thinking":
1553
+ return labels.thinking;
1554
+ case "idle":
1555
+ return labels.idle;
1556
+ }
1557
+ };
1558
+ var createVoiceWidgetViewModel = (input) => {
1559
+ const theme = { ...DEFAULT_VOICE_WIDGET_THEME, ...input.theme };
1560
+ const labels = { ...DEFAULT_VOICE_WIDGET_LABELS, ...input.labels };
1561
+ const lastAssistantAt = input.state.assistantAudio.at(-1)?.receivedAt;
1562
+ const lastTranscriptAt = input.state.turns.at(-1)?.committedAt;
1563
+ const agentState = deriveVoiceAgentUIState({
1564
+ hasActivePartial: input.state.partial.length > 0,
1565
+ isConnected: input.state.isConnected,
1566
+ isPlaying: false,
1567
+ isRecording: input.state.isRecording,
1568
+ lastAssistantAt,
1569
+ lastTranscriptAt
1570
+ });
1571
+ const connecting = !input.state.isConnected && input.state.status !== "idle" && !input.state.error;
1572
+ const statusLabel = input.state.error ? "Error" : connecting ? labels.connecting : input.state.status === "completed" ? labels.callEnded : stateLabel(agentState, labels);
1573
+ return {
1574
+ agentState,
1575
+ classes: {
1576
+ container: `absolute-voice-widget absolute-voice-widget--${agentState}`,
1577
+ dot: `absolute-voice-widget__dot${input.state.error ? " absolute-voice-widget__dot--error" : ""}`
1578
+ },
1579
+ controls: {
1580
+ canEnd: input.state.isConnected,
1581
+ canMute: input.state.isRecording,
1582
+ canStart: !input.state.isRecording && input.state.status !== "completed"
1583
+ },
1584
+ errorMessage: input.state.error ?? undefined,
1585
+ labels,
1586
+ partial: input.state.partial || undefined,
1587
+ statusLabel,
1588
+ theme,
1589
+ title: input.title ?? "Voice"
1590
+ };
1591
+ };
1592
+ var escapeHtml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#39;");
1593
+ var resolveRadius = (radius) => typeof radius === "number" ? `${radius}px` : radius;
1594
+ var renderVoiceWidgetHTML = (model) => {
1595
+ const t = model.theme;
1596
+ const containerStyle = `background:${t.background};border-radius:${resolveRadius(t.radius)};color:${t.foreground};font-family:${t.fontFamily};min-width:240px;padding:20px 22px;`;
1597
+ const dotStyle = `background:${model.errorMessage ? t.errorAccent : model.agentState === "idle" ? "rgba(148,163,184,0.6)" : t.accent};border-radius:50%;height:10px;width:10px;`;
1598
+ const buttons = [];
1599
+ if (model.controls.canStart) {
1600
+ buttons.push(`<button type="button" data-action="start" style="background:${t.accent};border:none;border-radius:12px;color:${t.foreground};cursor:pointer;font-size:14px;font-weight:500;padding:10px 14px;">${escapeHtml(model.labels.startCall)}</button>`);
1601
+ }
1602
+ if (model.controls.canMute) {
1603
+ buttons.push(`<button type="button" data-action="mute" style="background:transparent;border:1px solid rgba(255,255,255,0.18);border-radius:12px;color:${t.foreground};cursor:pointer;font-size:14px;font-weight:500;padding:10px 14px;">${escapeHtml(model.labels.mute)}</button>`);
1604
+ }
1605
+ if (model.controls.canEnd) {
1606
+ buttons.push(`<button type="button" data-action="end" style="background:${t.errorAccent};border:none;border-radius:12px;color:${t.foreground};cursor:pointer;font-size:14px;font-weight:500;padding:10px 14px;">${escapeHtml(model.labels.endCall)}</button>`);
1607
+ }
1608
+ return `<div role="region" aria-live="polite" data-agent-state="${model.agentState}" class="${escapeHtml(model.classes.container)}" style="${containerStyle}">
1609
+ <div style="align-items:center;display:flex;gap:10px;margin-bottom:12px;">
1610
+ <span aria-hidden="true" class="${escapeHtml(model.classes.dot)}" style="${dotStyle}"></span>
1611
+ <strong style="font-size:15px;">${escapeHtml(model.title)}</strong>
1612
+ <span style="font-size:13px;margin-left:auto;opacity:0.7;">${escapeHtml(model.statusLabel)}</span>
1613
+ </div>
1614
+ ${model.partial ? `<p style="font-size:13px;margin:8px 0 12px;opacity:0.85;word-break:break-word;">“${escapeHtml(model.partial)}”</p>` : ""}
1615
+ <div style="display:flex;gap:10px;">${buttons.join("")}</div>
1616
+ ${model.errorMessage ? `<p style="color:${t.errorAccent};font-size:12px;margin-top:12px;">${escapeHtml(model.errorMessage)}</p>` : ""}
1617
+ </div>`;
1618
+ };
1619
+
1620
+ // src/embed/index.ts
1621
+ var resolveTarget = (target) => {
1622
+ if (typeof target !== "string")
1623
+ return target;
1624
+ const el = document.querySelector(target);
1625
+ if (!el) {
1626
+ throw new Error(`AbsoluteVoice.mount: no element matches "${target}"`);
1627
+ }
1628
+ return el;
1629
+ };
1630
+ var mount = (target, options = {}) => {
1631
+ const host = resolveTarget(target);
1632
+ const controller = createVoiceController(options.path ?? "/voice", options.controllerOptions);
1633
+ let lastError = null;
1634
+ let lastStatus = null;
1635
+ const render = () => {
1636
+ const model = createVoiceWidgetViewModel({
1637
+ ...options.labels !== undefined ? { labels: options.labels } : {},
1638
+ state: {
1639
+ assistantAudio: controller.assistantAudio,
1640
+ error: controller.error,
1641
+ isConnected: controller.isConnected,
1642
+ isRecording: controller.isRecording,
1643
+ partial: controller.partial,
1644
+ status: controller.status,
1645
+ turns: controller.turns
1646
+ },
1647
+ ...options.theme !== undefined ? { theme: options.theme } : {},
1648
+ ...options.title !== undefined ? { title: options.title } : {}
1649
+ });
1650
+ host.innerHTML = renderVoiceWidgetHTML(model);
1651
+ for (const button of host.querySelectorAll("button[data-action]")) {
1652
+ const action = button.dataset.action;
1653
+ button.addEventListener("click", () => {
1654
+ if (action === "start")
1655
+ controller.startRecording();
1656
+ else if (action === "mute")
1657
+ controller.stopRecording();
1658
+ else if (action === "end")
1659
+ controller.close();
1660
+ });
1661
+ }
1662
+ if (controller.error && controller.error !== lastError) {
1663
+ lastError = controller.error;
1664
+ options.onError?.(controller.error);
1665
+ }
1666
+ if (controller.status !== lastStatus) {
1667
+ lastStatus = controller.status;
1668
+ options.onStatusChange?.(controller.status);
1669
+ }
1670
+ };
1671
+ const unsubscribe = controller.subscribe(render);
1672
+ render();
1673
+ if (options.autoStart) {
1674
+ controller.startRecording();
1675
+ }
1676
+ return {
1677
+ controller,
1678
+ async end() {
1679
+ await controller.close();
1680
+ },
1681
+ mute() {
1682
+ controller.stopRecording();
1683
+ },
1684
+ async start() {
1685
+ await controller.startRecording();
1686
+ },
1687
+ unmount() {
1688
+ unsubscribe();
1689
+ controller.close();
1690
+ host.innerHTML = "";
1691
+ }
1692
+ };
1693
+ };
1694
+ var VOICE_EMBED_VERSION = "0.0.22-beta.516";
1695
+ var globalApi = {
1696
+ mount,
1697
+ version: VOICE_EMBED_VERSION
1698
+ };
1699
+ if (typeof globalThis !== "undefined") {
1700
+ globalThis.AbsoluteVoice = globalApi;
1701
+ }
1702
+ var embed_default = globalApi;
1703
+ export {
1704
+ mount,
1705
+ embed_default as default,
1706
+ VOICE_EMBED_VERSION
1707
+ };