@absolutejs/voice 0.0.22-beta.463 → 0.0.22-beta.465

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.
@@ -2008,669 +2008,12 @@ var serverMessageToAction = (message) => {
2008
2008
  }
2009
2009
  };
2010
2010
 
2011
- // node_modules/@absolutejs/media/dist/index.js
2012
- var formatLabel = (format) => `${format.container}/${format.encoding}/${String(format.sampleRateHz)}hz/${String(format.channels)}ch`;
2013
- var formatMatches = (actual, expected) => actual.container === expected.container && actual.encoding === expected.encoding && actual.sampleRateHz === expected.sampleRateHz && actual.channels === expected.channels;
2014
- var pushIssue = (issues, severity, code, message) => {
2015
- issues.push({ code, message, severity });
2016
- };
2017
- var numericMetadata = (frame, key) => {
2018
- const value = frame.metadata?.[key];
2019
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
2020
- };
2021
- var average = (values) => values.length === 0 ? undefined : values.reduce((total, value) => total + value, 0) / values.length;
2022
- var max = (values) => values.length === 0 ? undefined : Math.max(...values);
2023
- var min = (values) => values.length === 0 ? undefined : Math.min(...values);
2024
- var numericStat = (stat, key) => {
2025
- const value = stat[key];
2026
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
2027
- };
2028
- var booleanStat = (stat, key) => {
2029
- const value = stat[key];
2030
- return typeof value === "boolean" ? value : undefined;
2031
- };
2032
- var stringStat = (stat, key) => {
2033
- const value = stat[key];
2034
- return typeof value === "string" ? value : undefined;
2035
- };
2036
- var statKey = (stat) => String(stat.id ?? stringStat(stat, "ssrc") ?? numericStat(stat, "ssrc") ?? stringStat(stat, "trackIdentifier") ?? stringStat(stat, "mid") ?? "unknown");
2037
- var secondsToMs = (value) => value === undefined ? undefined : value * 1000;
2038
- var DEFAULT_TELEPHONY_FORMAT = {
2039
- channels: 1,
2040
- container: "raw",
2041
- encoding: "mulaw",
2042
- sampleRateHz: 8000
2043
- };
2044
- var bytesToBase64 = (audio) => {
2045
- const bytes = audio instanceof ArrayBuffer ? new Uint8Array(audio) : new Uint8Array(audio.buffer, audio.byteOffset, audio.byteLength);
2046
- return Buffer.from(bytes).toString("base64");
2047
- };
2048
- var base64ToBytes = (value) => new Uint8Array(Buffer.from(value, "base64"));
2049
- var unknownRecord = (value) => value && typeof value === "object" ? value : {};
2050
- var firstString = (records, keys) => {
2051
- for (const record of records) {
2052
- for (const key of keys) {
2053
- const value = record[key];
2054
- if (typeof value === "string" && value.length > 0) {
2055
- return value;
2056
- }
2057
- if (typeof value === "number" && Number.isFinite(value)) {
2058
- return String(value);
2059
- }
2060
- }
2061
- }
2062
- return;
2063
- };
2064
- var firstNumber = (records, keys) => {
2065
- for (const record of records) {
2066
- for (const key of keys) {
2067
- const value = record[key];
2068
- if (typeof value === "number" && Number.isFinite(value)) {
2069
- return value;
2070
- }
2071
- if (typeof value === "string") {
2072
- const parsed = Number(value);
2073
- if (Number.isFinite(parsed)) {
2074
- return parsed;
2075
- }
2076
- }
2077
- }
2078
- }
2079
- return;
2080
- };
2081
- var telephonyDirection = (track) => {
2082
- const normalized = track?.toLowerCase();
2083
- if (!normalized) {
2084
- return "unknown";
2085
- }
2086
- if (normalized.includes("inbound") || normalized.includes("caller") || normalized.includes("in")) {
2087
- return "inbound";
2088
- }
2089
- if (normalized.includes("outbound") || normalized.includes("assistant") || normalized.includes("out")) {
2090
- return "outbound";
2091
- }
2092
- return "unknown";
2093
- };
2094
- var telephonyFrameKind = (direction) => direction === "outbound" ? "assistant-audio" : "input-audio";
2095
- var telephonyEventKind = (envelope) => {
2096
- const raw = firstString([envelope], ["event", "type", "eventType"]) ?? firstString([unknownRecord(envelope.message)], ["event", "type"]);
2097
- const normalized = raw?.toLowerCase().replace(/[_\s-]+/g, "-");
2098
- if (!normalized) {
2099
- return "unknown";
2100
- }
2101
- if (normalized.includes("connected")) {
2102
- return "connected";
2103
- }
2104
- if (normalized.includes("start")) {
2105
- return "start";
2106
- }
2107
- if (normalized.includes("media")) {
2108
- return "media";
2109
- }
2110
- if (normalized.includes("stop") || normalized.includes("closed")) {
2111
- return "stop";
2112
- }
2113
- if (normalized.includes("error") || normalized.includes("failed")) {
2114
- return "error";
2115
- }
2116
- return "unknown";
2117
- };
2118
- var normalizeWebRTCStat = (stat) => {
2119
- const sample = {};
2120
- for (const [key, value] of Object.entries(stat)) {
2121
- if (value === null || typeof value === "boolean" || typeof value === "number" || typeof value === "string") {
2122
- sample[key] = value;
2123
- }
2124
- }
2125
- return sample;
2126
- };
2127
- var parseTelephonyMediaFrame = (input) => {
2128
- const envelope = input.envelope;
2129
- const media = unknownRecord(envelope.media);
2130
- const payload = firstString([media, envelope], ["payload", "audio", "data"]) ?? firstString([unknownRecord(envelope.message)], ["payload"]);
2131
- if (!payload) {
2132
- return;
2133
- }
2134
- const carrier = input.carrier ?? firstString([envelope], ["provider"]) ?? "telephony";
2135
- const streamId = firstString([media, envelope], ["streamSid", "stream_id", "streamId", "streamId", "callSid", "call_id"]);
2136
- const sequenceNumber = firstString([media, envelope], ["sequenceNumber", "sequence_number", "chunk"]);
2137
- const track = firstString([media, envelope], ["track", "direction"]);
2138
- const direction = telephonyDirection(track);
2139
- const timestamp = firstNumber([media, envelope], ["timestamp", "time", "startedAt"]);
2140
- return {
2141
- at: timestamp,
2142
- audio: base64ToBytes(payload),
2143
- format: input.format ?? DEFAULT_TELEPHONY_FORMAT,
2144
- id: [
2145
- carrier,
2146
- streamId ?? input.sessionId ?? "stream",
2147
- sequenceNumber ?? timestamp ?? Date.now()
2148
- ].join(":"),
2149
- kind: telephonyFrameKind(direction),
2150
- metadata: {
2151
- carrier,
2152
- direction,
2153
- event: firstString([envelope], ["event", "type"]),
2154
- sequenceNumber,
2155
- streamId,
2156
- track
2157
- },
2158
- sessionId: input.sessionId ?? streamId,
2159
- source: "telephony"
2160
- };
2161
- };
2162
- var serializeTelephonyMediaFrame = (input) => {
2163
- const carrier = input.carrier ?? input.frame.metadata?.carrier ?? "telephony";
2164
- const streamId = input.streamId ?? (typeof input.frame.metadata?.streamId === "string" ? input.frame.metadata.streamId : input.frame.sessionId);
2165
- const sequenceNumber = input.sequenceNumber ?? (typeof input.frame.metadata?.sequenceNumber === "string" || typeof input.frame.metadata?.sequenceNumber === "number" ? input.frame.metadata.sequenceNumber : undefined);
2166
- const direction = input.frame.kind === "assistant-audio" ? "outbound" : "inbound";
2167
- const payload = input.frame.audio ? bytesToBase64(input.frame.audio) : "";
2168
- if (carrier === "twilio") {
2169
- return {
2170
- event: "media",
2171
- sequenceNumber,
2172
- streamSid: streamId,
2173
- media: {
2174
- payload,
2175
- timestamp: input.frame.at,
2176
- track: direction
2177
- }
2178
- };
2179
- }
2180
- if (carrier === "telnyx") {
2181
- return {
2182
- event: "media",
2183
- stream_id: streamId,
2184
- sequence_number: sequenceNumber,
2185
- media: {
2186
- payload,
2187
- timestamp: input.frame.at,
2188
- track: direction
2189
- }
2190
- };
2191
- }
2192
- if (carrier === "plivo") {
2193
- return {
2194
- event: "media",
2195
- streamId,
2196
- sequenceNumber,
2197
- media: {
2198
- payload,
2199
- timestamp: input.frame.at,
2200
- track: direction
2201
- }
2202
- };
2203
- }
2204
- return {
2205
- event: "media",
2206
- provider: carrier,
2207
- sequenceNumber,
2208
- streamId,
2209
- media: {
2210
- payload,
2211
- timestamp: input.frame.at,
2212
- track: direction
2213
- }
2214
- };
2215
- };
2216
- var createTelephonyMediaSerializer = (input) => {
2217
- const format = input.format ?? DEFAULT_TELEPHONY_FORMAT;
2218
- return {
2219
- carrier: input.carrier,
2220
- format,
2221
- parse: (envelope) => parseTelephonyMediaFrame({
2222
- carrier: input.carrier,
2223
- envelope,
2224
- format,
2225
- sessionId: input.sessionId ?? input.streamId
2226
- }),
2227
- serialize: (frame) => serializeTelephonyMediaFrame({
2228
- carrier: input.carrier,
2229
- frame,
2230
- streamId: input.streamId
2231
- })
2232
- };
2233
- };
2234
- var parseTelephonyStreamEvent = (input) => {
2235
- const envelope = input.envelope;
2236
- const media = unknownRecord(envelope.media);
2237
- const start = unknownRecord(envelope.start);
2238
- const stop = unknownRecord(envelope.stop);
2239
- const errorRecord = unknownRecord(envelope.error);
2240
- const kind = telephonyEventKind(envelope);
2241
- const carrier = input.carrier ?? firstString([envelope], ["provider", "carrier"]) ?? "telephony";
2242
- const frame = kind === "media" ? parseTelephonyMediaFrame({
2243
- carrier,
2244
- envelope,
2245
- format: input.format,
2246
- sessionId: input.sessionId
2247
- }) : undefined;
2248
- const streamId = firstString([media, start, stop, envelope], ["streamSid", "stream_id", "streamId", "callSid", "call_id"]) ?? input.sessionId;
2249
- const sequenceNumber = firstString([media, envelope], ["sequenceNumber", "sequence_number", "chunk"]);
2250
- const track = firstString([media, envelope], ["track", "direction"]);
2251
- return {
2252
- audioBytes: frame?.audio ? frame.audio instanceof ArrayBuffer ? frame.audio.byteLength : frame.audio.byteLength : 0,
2253
- at: frame?.at ?? firstNumber([media, start, stop, envelope], ["timestamp", "time", "startedAt"]),
2254
- carrier,
2255
- direction: telephonyDirection(track),
2256
- error: firstString([errorRecord, envelope], ["message", "error", "reason"]),
2257
- kind,
2258
- sequenceNumber,
2259
- streamId
2260
- };
2261
- };
2262
- var buildMediaTelephonyStreamLifecycleReport = (input = {}) => {
2263
- const envelopes = input.envelopes ?? [];
2264
- const events = envelopes.map((envelope) => parseTelephonyStreamEvent({
2265
- carrier: input.carrier,
2266
- envelope
2267
- }));
2268
- const issues = [];
2269
- const startedIndex = events.findIndex((event) => event.kind === "start");
2270
- const firstMediaIndex = events.findIndex((event) => event.kind === "media");
2271
- const stoppedIndex = events.findIndex((event) => event.kind === "stop");
2272
- const started = startedIndex >= 0;
2273
- const stopped = stoppedIndex >= 0;
2274
- const mediaEvents = events.filter((event) => event.kind === "media");
2275
- const audioBytes = events.reduce((total, event) => total + event.audioBytes, 0);
2276
- const minAudioBytes = input.minAudioBytes ?? 1;
2277
- const streamIds = Array.from(new Set(events.map((event) => event.streamId).filter(Boolean)));
2278
- if ((input.requireStart ?? true) && !started) {
2279
- pushIssue(issues, "error", "media.telephony_missing_start", "Telephony media stream did not include a start event.");
2280
- }
2281
- if ((input.requireMedia ?? true) && mediaEvents.length === 0) {
2282
- pushIssue(issues, "error", "media.telephony_missing_media", "Telephony media stream did not include media payload events.");
2283
- }
2284
- if ((input.requireStop ?? true) && !stopped) {
2285
- pushIssue(issues, input.maxMissingStop === false ? "warning" : "error", "media.telephony_missing_stop", "Telephony media stream did not include a stop event.");
2286
- }
2287
- if (started && firstMediaIndex >= 0 && firstMediaIndex < startedIndex) {
2288
- pushIssue(issues, "error", "media.telephony_media_before_start", "Telephony media payload arrived before the stream start event.");
2289
- }
2290
- if (stopped && firstMediaIndex >= 0 && stoppedIndex < firstMediaIndex) {
2291
- pushIssue(issues, "error", "media.telephony_stop_before_media", "Telephony media stream stopped before any media payload arrived.");
2292
- }
2293
- if (mediaEvents.length > 0 && audioBytes < minAudioBytes) {
2294
- pushIssue(issues, "error", "media.telephony_no_audio_bytes", `Telephony media stream parsed ${String(audioBytes)} audio byte(s), below required ${String(minAudioBytes)}.`);
2295
- }
2296
- for (const event of events) {
2297
- if (event.kind === "error") {
2298
- pushIssue(issues, "error", "media.telephony_stream_error", event.error ?? "Telephony media stream emitted an error event.");
2299
- }
2300
- }
2301
- return {
2302
- audioBytes,
2303
- carrier: input.carrier,
2304
- checkedAt: Date.now(),
2305
- events,
2306
- issues,
2307
- mediaEvents: mediaEvents.length,
2308
- started,
2309
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
2310
- stopped,
2311
- streamIds
2312
- };
2313
- };
2314
- var buildMediaResamplingPlan = (input) => {
2315
- const required = !formatMatches(input.inputFormat, input.outputFormat);
2316
- return {
2317
- inputFormat: input.inputFormat,
2318
- outputFormat: input.outputFormat,
2319
- ratio: input.outputFormat.sampleRateHz / input.inputFormat.sampleRateHz,
2320
- required,
2321
- status: input.inputFormat.container === input.outputFormat.container && input.inputFormat.encoding === input.outputFormat.encoding && input.inputFormat.channels === input.outputFormat.channels ? "pass" : "warn"
2322
- };
2323
- };
2324
- var speechProbability = (frame) => {
2325
- if (frame.metadata?.isSpeech === true) {
2326
- return 1;
2327
- }
2328
- if (frame.metadata?.isSpeech === false) {
2329
- return 0;
2330
- }
2331
- for (const key of ["speechProbability", "voiceProbability", "rms", "energy"]) {
2332
- const value = numericMetadata(frame, key);
2333
- if (value !== undefined) {
2334
- return value;
2335
- }
2336
- }
2337
- return 0;
2338
- };
2339
- var buildMediaVadReport = (input = {}) => {
2340
- const frames = (input.frames ?? []).filter((frame) => frame.kind === "input-audio");
2341
- const speechStartThreshold = input.speechStartThreshold ?? 0.6;
2342
- const speechEndThreshold = input.speechEndThreshold ?? 0.35;
2343
- const minSpeechFrames = input.minSpeechFrames ?? 1;
2344
- const maxSilenceFrames = input.maxSilenceFrames ?? 1;
2345
- const segments = [];
2346
- let activeFrames = [];
2347
- let silenceFrames = 0;
2348
- const closeSegment = () => {
2349
- if (activeFrames.length < minSpeechFrames) {
2350
- activeFrames = [];
2351
- silenceFrames = 0;
2352
- return;
2353
- }
2354
- const first = activeFrames[0];
2355
- const last = activeFrames.at(-1);
2356
- if (!first) {
2357
- return;
2358
- }
2359
- segments.push({
2360
- durationMs: first.at !== undefined && last?.at !== undefined ? last.at - first.at + (last.durationMs ?? 0) : undefined,
2361
- endAt: last?.at !== undefined ? last.at + (last.durationMs ?? 0) : undefined,
2362
- frameCount: activeFrames.length,
2363
- segmentId: `vad:${String(segments.length + 1)}`,
2364
- sessionId: first.sessionId,
2365
- startAt: first.at,
2366
- turnId: first.turnId
2367
- });
2368
- activeFrames = [];
2369
- silenceFrames = 0;
2370
- };
2371
- for (const frame of frames) {
2372
- const probability = speechProbability(frame);
2373
- if (activeFrames.length === 0) {
2374
- if (probability >= speechStartThreshold) {
2375
- activeFrames.push(frame);
2376
- }
2377
- continue;
2378
- }
2379
- activeFrames.push(frame);
2380
- if (probability <= speechEndThreshold) {
2381
- silenceFrames += 1;
2382
- } else {
2383
- silenceFrames = 0;
2384
- }
2385
- if (silenceFrames > maxSilenceFrames) {
2386
- closeSegment();
2387
- }
2388
- }
2389
- closeSegment();
2390
- return {
2391
- checkedAt: Date.now(),
2392
- inputAudioFrames: frames.length,
2393
- segments,
2394
- status: frames.length === 0 ? "warn" : "pass"
2395
- };
2396
- };
2397
- var buildMediaInterruptionReport = (input = {}) => {
2398
- const issues = [];
2399
- const interruptionFrames = (input.frames ?? []).filter((frame) => frame.kind === "interruption");
2400
- const latenciesMs = interruptionFrames.map((frame) => frame.latencyMs).filter((latency) => typeof latency === "number");
2401
- const maxInterruptionLatencyMs = input.maxInterruptionLatencyMs;
2402
- if (interruptionFrames.length === 0) {
2403
- pushIssue(issues, "warning", "media.interruption_missing", "No interruption frame was observed.");
2404
- }
2405
- if (maxInterruptionLatencyMs !== undefined && latenciesMs.some((latency) => latency > maxInterruptionLatencyMs)) {
2406
- pushIssue(issues, "error", "media.interruption_latency", `Interruption latency exceeded ${String(maxInterruptionLatencyMs)}ms.`);
2407
- }
2408
- return {
2409
- checkedAt: Date.now(),
2410
- interruptionFrames: interruptionFrames.length,
2411
- issues,
2412
- latenciesMs,
2413
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass"
2414
- };
2415
- };
2416
- var buildMediaQualityReport = (input = {}) => {
2417
- const frames = [...input.frames ?? []].sort((a, b) => (a.at ?? 0) - (b.at ?? 0));
2418
- const audioFrames = frames.filter((frame) => frame.kind === "input-audio" || frame.kind === "assistant-audio");
2419
- const inputAudioFrames = frames.filter((frame) => frame.kind === "input-audio");
2420
- const assistantAudioFrames = frames.filter((frame) => frame.kind === "assistant-audio");
2421
- const issues = [];
2422
- const gapsMs = [];
2423
- for (const [index, frame] of audioFrames.entries()) {
2424
- const previous = audioFrames[index - 1];
2425
- if (previous?.at === undefined || frame.at === undefined || previous.durationMs === undefined) {
2426
- continue;
2427
- }
2428
- const gap = frame.at - (previous.at + previous.durationMs);
2429
- if (gap > 0) {
2430
- gapsMs.push(gap);
2431
- }
2432
- }
2433
- const jitterMs = audioFrames.map((frame) => numericMetadata(frame, "jitterMs")).filter((value) => value !== undefined).at(-1) ?? max(gapsMs);
2434
- const first = audioFrames.find((frame) => frame.at !== undefined);
2435
- const last = audioFrames.toReversed().find((frame) => frame.at !== undefined);
2436
- const durationMs = first?.at !== undefined && last?.at !== undefined ? last.at - first.at + (last.durationMs ?? 0) : undefined;
2437
- const expectedDurationMs = audioFrames.length > 0 ? audioFrames.reduce((total, frame) => total + (frame.durationMs ?? 0), 0) : undefined;
2438
- const timestampDriftMs = durationMs !== undefined && expectedDurationMs !== undefined ? Math.max(0, durationMs - expectedDurationMs) : undefined;
2439
- const speechScores = inputAudioFrames.map(speechProbability);
2440
- const speechFrames = speechScores.filter((score) => score >= 0.6).length;
2441
- const silenceFrames = speechScores.filter((score) => score <= 0.35).length;
2442
- const unknownSpeechFrames = Math.max(0, inputAudioFrames.length - speechFrames - silenceFrames);
2443
- const speechRatio = inputAudioFrames.length === 0 ? 0 : speechFrames / inputAudioFrames.length;
2444
- const silenceRatio = inputAudioFrames.length === 0 ? 0 : silenceFrames / inputAudioFrames.length;
2445
- const levels = audioFrames.map((frame) => numericMetadata(frame, "level") ?? numericMetadata(frame, "rms") ?? numericMetadata(frame, "energy")).filter((value) => value !== undefined);
2446
- const backpressureEvents = input.transport?.backpressureEvents ?? 0;
2447
- const maxGapMs = input.maxGapMs;
2448
- if (maxGapMs !== undefined && gapsMs.some((gap) => gap > maxGapMs)) {
2449
- pushIssue(issues, "warning", "media.quality_gap", `Observed media gap above ${String(maxGapMs)}ms.`);
2450
- }
2451
- if (input.maxJitterMs !== undefined && jitterMs !== undefined && jitterMs > input.maxJitterMs) {
2452
- pushIssue(issues, "warning", "media.quality_jitter", `Observed jitter ${String(jitterMs)}ms above ${String(input.maxJitterMs)}ms.`);
2453
- }
2454
- if (input.maxTimestampDriftMs !== undefined && timestampDriftMs !== undefined && timestampDriftMs > input.maxTimestampDriftMs) {
2455
- pushIssue(issues, "warning", "media.quality_timestamp_drift", `Observed timestamp drift ${String(timestampDriftMs)}ms above ${String(input.maxTimestampDriftMs)}ms.`);
2456
- }
2457
- if (input.minSpeechRatio !== undefined && inputAudioFrames.length > 0 && speechRatio < input.minSpeechRatio) {
2458
- pushIssue(issues, "warning", "media.quality_speech_ratio", `Observed speech ratio ${String(speechRatio)} below ${String(input.minSpeechRatio)}.`);
2459
- }
2460
- if (input.maxBackpressureEvents !== undefined && backpressureEvents > input.maxBackpressureEvents) {
2461
- pushIssue(issues, "warning", "media.quality_backpressure", `Observed ${String(backpressureEvents)} backpressure event(s), above ${String(input.maxBackpressureEvents)}.`);
2462
- }
2463
- return {
2464
- assistantAudioFrames: assistantAudioFrames.length,
2465
- backpressureEvents,
2466
- checkedAt: Date.now(),
2467
- durationMs,
2468
- gapCount: gapsMs.length,
2469
- gapsMs,
2470
- inputAudioFrames: inputAudioFrames.length,
2471
- issues,
2472
- jitterMs,
2473
- levelAverage: average(levels),
2474
- levelMax: max(levels),
2475
- levelMin: min(levels),
2476
- silenceFrames,
2477
- silenceRatio,
2478
- speechFrames,
2479
- speechRatio,
2480
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
2481
- timestampDriftMs,
2482
- totalFrames: frames.length,
2483
- unknownSpeechFrames
2484
- };
2485
- };
2486
- var buildMediaWebRTCStatsReport = (input = {}) => {
2487
- const stats = input.stats ?? [];
2488
- const issues = [];
2489
- const inbound = stats.filter((stat) => stat.type === "inbound-rtp" && stringStat(stat, "kind") !== "video");
2490
- const outbound = stats.filter((stat) => stat.type === "outbound-rtp" && stringStat(stat, "kind") !== "video");
2491
- const candidatePairs = stats.filter((stat) => stat.type === "candidate-pair");
2492
- const audioTracks = stats.filter((stat) => (stat.type === "track" || stat.type === "media-source") && stringStat(stat, "kind") === "audio");
2493
- const activeCandidatePairs = candidatePairs.filter((stat) => booleanStat(stat, "selected") === true || booleanStat(stat, "nominated") === true || stringStat(stat, "state") === "succeeded").length;
2494
- const liveAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") !== "ended" && stringStat(stat, "trackState") !== "ended" && booleanStat(stat, "ended") !== true).length;
2495
- const endedAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") === "ended" || stringStat(stat, "trackState") === "ended" || booleanStat(stat, "ended") === true).length;
2496
- const inboundPackets = inbound.reduce((total, stat) => total + (numericStat(stat, "packetsReceived") ?? 0), 0);
2497
- const outboundPackets = outbound.reduce((total, stat) => total + (numericStat(stat, "packetsSent") ?? 0), 0);
2498
- const packetsLost = [...inbound, ...outbound].reduce((total, stat) => total + Math.max(0, numericStat(stat, "packetsLost") ?? 0), 0);
2499
- const packetLossDenominator = inboundPackets + packetsLost;
2500
- const packetLossRatio = packetLossDenominator === 0 ? 0 : packetsLost / packetLossDenominator;
2501
- const bytesReceived = inbound.reduce((total, stat) => total + (numericStat(stat, "bytesReceived") ?? 0), 0);
2502
- const bytesSent = outbound.reduce((total, stat) => total + (numericStat(stat, "bytesSent") ?? 0), 0);
2503
- const roundTripTimeMs = max(candidatePairs.map((stat) => secondsToMs(numericStat(stat, "currentRoundTripTime") ?? numericStat(stat, "roundTripTime"))).filter((value) => value !== undefined));
2504
- const jitterMs = max([...inbound, ...outbound].map((stat) => secondsToMs(numericStat(stat, "jitter"))).filter((value) => value !== undefined));
2505
- const jitterBufferDelayMs = max(inbound.map((stat) => {
2506
- const delay = numericStat(stat, "jitterBufferDelay");
2507
- const emitted = numericStat(stat, "jitterBufferEmittedCount");
2508
- return delay !== undefined && emitted !== undefined && emitted > 0 ? delay / emitted * 1000 : undefined;
2509
- }).filter((value) => value !== undefined));
2510
- const audioLevels = audioTracks.map((stat) => numericStat(stat, "audioLevel")).filter((value) => value !== undefined);
2511
- if (input.requireConnectedCandidatePair && candidatePairs.length > 0 && activeCandidatePairs === 0) {
2512
- pushIssue(issues, "error", "media.webrtc_candidate_pair_missing", "No active WebRTC candidate pair was observed.");
2513
- }
2514
- if (input.requireLiveAudioTrack && liveAudioTracks === 0) {
2515
- pushIssue(issues, "error", "media.webrtc_audio_track_missing", "No live WebRTC audio track was observed.");
2516
- }
2517
- if (input.maxPacketLossRatio !== undefined && packetLossRatio > input.maxPacketLossRatio) {
2518
- pushIssue(issues, "warning", "media.webrtc_packet_loss", `Observed WebRTC packet loss ratio ${String(packetLossRatio)} above ${String(input.maxPacketLossRatio)}.`);
2519
- }
2520
- if (input.maxRoundTripTimeMs !== undefined && roundTripTimeMs !== undefined && roundTripTimeMs > input.maxRoundTripTimeMs) {
2521
- pushIssue(issues, "warning", "media.webrtc_round_trip_time", `Observed WebRTC RTT ${String(roundTripTimeMs)}ms above ${String(input.maxRoundTripTimeMs)}ms.`);
2522
- }
2523
- if (input.maxJitterMs !== undefined && jitterMs !== undefined && jitterMs > input.maxJitterMs) {
2524
- pushIssue(issues, "warning", "media.webrtc_jitter", `Observed WebRTC jitter ${String(jitterMs)}ms above ${String(input.maxJitterMs)}ms.`);
2525
- }
2526
- return {
2527
- activeCandidatePairs,
2528
- audioLevelAverage: average(audioLevels),
2529
- bytesReceived,
2530
- bytesSent,
2531
- checkedAt: Date.now(),
2532
- endedAudioTracks,
2533
- inboundPackets,
2534
- issues,
2535
- jitterBufferDelayMs,
2536
- jitterMs,
2537
- liveAudioTracks,
2538
- outboundPackets,
2539
- packetLossRatio,
2540
- packetsLost,
2541
- roundTripTimeMs,
2542
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
2543
- totalStats: stats.length
2544
- };
2545
- };
2546
- var collectMediaWebRTCStats = async (input) => {
2547
- const report = await input.peerConnection.getStats(input.selector ?? null);
2548
- return [...report.values()].map(normalizeWebRTCStat);
2549
- };
2550
- var buildMediaWebRTCStreamContinuityReport = (input = {}) => {
2551
- const stats = input.stats ?? [];
2552
- const previousStats = input.previousStats ?? [];
2553
- const issues = [];
2554
- const previousByKey = new Map(previousStats.map((stat) => [statKey(stat), stat]));
2555
- const audioRtp = stats.filter((stat) => (stat.type === "inbound-rtp" || stat.type === "outbound-rtp") && stringStat(stat, "kind") !== "video" && stringStat(stat, "mediaType") !== "video");
2556
- const streams = audioRtp.map((stat) => {
2557
- const direction = stat.type === "outbound-rtp" ? "outbound" : "inbound";
2558
- const packetsKey = direction === "outbound" ? "packetsSent" : "packetsReceived";
2559
- const bytesKey = direction === "outbound" ? "bytesSent" : "bytesReceived";
2560
- const previous = previousByKey.get(statKey(stat));
2561
- const currentPackets = numericStat(stat, packetsKey);
2562
- const previousPackets = previous ? numericStat(previous, packetsKey) : undefined;
2563
- const currentBytes = numericStat(stat, bytesKey);
2564
- const previousBytes = previous ? numericStat(previous, bytesKey) : undefined;
2565
- const timeDeltaMs = stat.timestamp !== undefined && previous?.timestamp !== undefined ? stat.timestamp - previous.timestamp : undefined;
2566
- return {
2567
- bytesDelta: currentBytes !== undefined && previousBytes !== undefined ? currentBytes - previousBytes : undefined,
2568
- currentPackets,
2569
- direction,
2570
- id: statKey(stat),
2571
- packetDelta: currentPackets !== undefined && previousPackets !== undefined ? currentPackets - previousPackets : undefined,
2572
- previousPackets,
2573
- timeDeltaMs
2574
- };
2575
- });
2576
- const inbound = streams.filter((stream) => stream.direction === "inbound");
2577
- const outbound = streams.filter((stream) => stream.direction === "outbound");
2578
- const maxObservedGapMs = max(streams.map((stream) => stream.timeDeltaMs).filter((value) => value !== undefined));
2579
- const stalledInboundStreams = inbound.filter((stream) => input.maxInboundPacketStallMs !== undefined && stream.timeDeltaMs !== undefined && stream.timeDeltaMs >= input.maxInboundPacketStallMs && stream.packetDelta !== undefined && stream.packetDelta <= 0).length;
2580
- const stalledOutboundStreams = outbound.filter((stream) => input.maxOutboundPacketStallMs !== undefined && stream.timeDeltaMs !== undefined && stream.timeDeltaMs >= input.maxOutboundPacketStallMs && stream.packetDelta !== undefined && stream.packetDelta <= 0).length;
2581
- if (input.requireInboundAudio && inbound.length === 0) {
2582
- pushIssue(issues, "error", "media.webrtc_inbound_audio_missing", "No inbound WebRTC audio RTP stream was observed.");
2583
- }
2584
- if (input.requireOutboundAudio && outbound.length === 0) {
2585
- pushIssue(issues, "error", "media.webrtc_outbound_audio_missing", "No outbound WebRTC audio RTP stream was observed.");
2586
- }
2587
- if (input.maxGapMs !== undefined && maxObservedGapMs !== undefined && maxObservedGapMs > input.maxGapMs) {
2588
- pushIssue(issues, "warning", "media.webrtc_stream_gap", `Observed WebRTC stream sample gap ${String(maxObservedGapMs)}ms above ${String(input.maxGapMs)}ms.`);
2589
- }
2590
- if (stalledInboundStreams > 0) {
2591
- pushIssue(issues, "error", "media.webrtc_inbound_stalled", `${String(stalledInboundStreams)} inbound WebRTC audio stream(s) stopped receiving packets.`);
2592
- }
2593
- if (stalledOutboundStreams > 0) {
2594
- pushIssue(issues, "error", "media.webrtc_outbound_stalled", `${String(stalledOutboundStreams)} outbound WebRTC audio stream(s) stopped sending packets.`);
2595
- }
2596
- return {
2597
- checkedAt: Date.now(),
2598
- inboundAudioStreams: inbound.length,
2599
- issues,
2600
- maxObservedGapMs,
2601
- outboundAudioStreams: outbound.length,
2602
- stalledInboundStreams,
2603
- stalledOutboundStreams,
2604
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
2605
- streams,
2606
- totalStats: stats.length
2607
- };
2608
- };
2609
- var buildMediaPipelineCalibrationReport = (input = {}) => {
2610
- const frames = input.frames ?? [];
2611
- const issues = [];
2612
- const inputFrames = frames.filter((frame) => frame.kind === "input-audio");
2613
- const assistantFrames = frames.filter((frame) => frame.kind === "assistant-audio");
2614
- const turnCommitFrames = frames.filter((frame) => frame.kind === "turn-commit");
2615
- const interruptionFrameRecords = frames.filter((frame) => frame.kind === "interruption");
2616
- const traceLinkedFrames = frames.filter((frame) => frame.traceEventId).length;
2617
- const backpressureFrames = frames.filter((frame) => Boolean(frame.metadata?.backpressure)).length;
2618
- const audioLatencies = assistantFrames.map((frame) => frame.latencyMs).filter((latency) => typeof latency === "number");
2619
- const firstAudioLatencyMs = audioLatencies.length > 0 ? Math.min(...audioLatencies) : undefined;
2620
- const jitterValues = frames.map((frame) => numericMetadata(frame, "jitterMs")).filter((value) => value !== undefined);
2621
- const jitterMs = jitterValues.length > 0 ? Math.max(...jitterValues) : undefined;
2622
- const inputFormat = input.inputFormat ?? inputFrames.find((frame) => frame.format)?.format;
2623
- const outputFormat = input.outputFormat ?? assistantFrames.find((frame) => frame.format)?.format;
2624
- const resamplingRequired = Boolean(input.expectedInputFormat && inputFormat && inputFormat.sampleRateHz !== input.expectedInputFormat.sampleRateHz) || Boolean(input.expectedOutputFormat && outputFormat && outputFormat.sampleRateHz !== input.expectedOutputFormat.sampleRateHz);
2625
- const resamplingTargetHz = resamplingRequired && input.expectedInputFormat ? input.expectedInputFormat.sampleRateHz : resamplingRequired ? input.expectedOutputFormat?.sampleRateHz : undefined;
2626
- if (inputFrames.length === 0) {
2627
- pushIssue(issues, "warning", "media.input_audio_missing", "No input audio frames were observed.");
2628
- }
2629
- if (assistantFrames.length === 0) {
2630
- pushIssue(issues, "warning", "media.assistant_audio_missing", "No assistant audio frames were observed.");
2631
- }
2632
- if (input.expectedInputFormat && inputFormat && !formatMatches(inputFormat, input.expectedInputFormat)) {
2633
- pushIssue(issues, inputFormat.sampleRateHz === input.expectedInputFormat.sampleRateHz ? "warning" : "error", "media.input_format_mismatch", `Input format ${formatLabel(inputFormat)} does not match expected ${formatLabel(input.expectedInputFormat)}.`);
2634
- }
2635
- if (input.expectedOutputFormat && outputFormat && !formatMatches(outputFormat, input.expectedOutputFormat)) {
2636
- pushIssue(issues, outputFormat.sampleRateHz === input.expectedOutputFormat.sampleRateHz ? "warning" : "error", "media.output_format_mismatch", `Output format ${formatLabel(outputFormat)} does not match expected ${formatLabel(input.expectedOutputFormat)}.`);
2637
- }
2638
- if (firstAudioLatencyMs !== undefined && input.maxFirstAudioLatencyMs !== undefined && firstAudioLatencyMs > input.maxFirstAudioLatencyMs) {
2639
- pushIssue(issues, "error", "media.first_audio_latency", `First audio latency ${String(firstAudioLatencyMs)}ms exceeds budget ${String(input.maxFirstAudioLatencyMs)}ms.`);
2640
- }
2641
- if (jitterMs !== undefined && input.maxJitterMs !== undefined && jitterMs > input.maxJitterMs) {
2642
- pushIssue(issues, "warning", "media.jitter", `Media jitter ${String(jitterMs)}ms exceeds budget ${String(input.maxJitterMs)}ms.`);
2643
- }
2644
- if (input.maxBackpressureFrames !== undefined && backpressureFrames > input.maxBackpressureFrames) {
2645
- pushIssue(issues, "warning", "media.backpressure", `Backpressure frame count ${String(backpressureFrames)} exceeds budget ${String(input.maxBackpressureFrames)}.`);
2646
- }
2647
- if (input.requireInterruptionFrame && interruptionFrameRecords.length === 0) {
2648
- pushIssue(issues, "warning", "media.interruption_missing", "No interruption frame was observed.");
2649
- }
2650
- if (input.requireTraceEvidence && traceLinkedFrames === 0) {
2651
- pushIssue(issues, "warning", "media.trace_evidence_missing", "No media frames were linked to trace evidence.");
2652
- }
2653
- return {
2654
- assistantAudioFrames: assistantFrames.length,
2655
- backpressureFrames,
2656
- checkedAt: Date.now(),
2657
- firstAudioLatencyMs,
2658
- inputAudioFrames: inputFrames.length,
2659
- inputFormat,
2660
- interruptionFrames: interruptionFrameRecords.length,
2661
- issues,
2662
- jitterMs,
2663
- outputFormat,
2664
- resamplingRequired,
2665
- resamplingTargetHz,
2666
- status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
2667
- surface: input.surface ?? "media-pipeline",
2668
- traceLinkedFrames,
2669
- turnCommitFrames: turnCommitFrames.length
2670
- };
2671
- };
2672
-
2673
2011
  // src/client/browserMedia.ts
2012
+ import {
2013
+ buildMediaWebRTCStatsReport,
2014
+ buildMediaWebRTCStreamContinuityReport,
2015
+ collectMediaWebRTCStats
2016
+ } from "@absolutejs/media";
2674
2017
  var DEFAULT_BROWSER_MEDIA_PATH = "/api/voice/browser-media";
2675
2018
  var DEFAULT_BROWSER_MEDIA_INTERVAL_MS = 5000;
2676
2019
  var resolvePeerConnection = async (options) => options.peerConnection ?? await options.getPeerConnection?.() ?? null;