@absolutejs/voice 0.0.22-beta.319 → 0.0.22-beta.320
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.
- package/dist/angular/index.js +413 -0
- package/dist/browserMediaRoutes.d.ts +61 -0
- package/dist/client/browserMedia.d.ts +8 -0
- package/dist/client/htmxBootstrap.js +172 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +414 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +587 -404
- package/dist/react/index.js +413 -0
- package/dist/svelte/index.js +413 -0
- package/dist/testing/index.js +441 -28
- package/dist/trace.d.ts +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/vue/index.js +413 -0
- package/package.json +1 -1
package/dist/vue/index.js
CHANGED
|
@@ -4741,6 +4741,412 @@ var serverMessageToAction = (message) => {
|
|
|
4741
4741
|
}
|
|
4742
4742
|
};
|
|
4743
4743
|
|
|
4744
|
+
// node_modules/@absolutejs/media/dist/index.js
|
|
4745
|
+
var formatLabel = (format) => `${format.container}/${format.encoding}/${String(format.sampleRateHz)}hz/${String(format.channels)}ch`;
|
|
4746
|
+
var formatMatches = (actual, expected) => actual.container === expected.container && actual.encoding === expected.encoding && actual.sampleRateHz === expected.sampleRateHz && actual.channels === expected.channels;
|
|
4747
|
+
var pushIssue = (issues, severity, code, message) => {
|
|
4748
|
+
issues.push({ code, message, severity });
|
|
4749
|
+
};
|
|
4750
|
+
var numericMetadata = (frame, key) => {
|
|
4751
|
+
const value = frame.metadata?.[key];
|
|
4752
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
4753
|
+
};
|
|
4754
|
+
var average = (values) => values.length === 0 ? undefined : values.reduce((total, value) => total + value, 0) / values.length;
|
|
4755
|
+
var max = (values) => values.length === 0 ? undefined : Math.max(...values);
|
|
4756
|
+
var min = (values) => values.length === 0 ? undefined : Math.min(...values);
|
|
4757
|
+
var numericStat = (stat, key) => {
|
|
4758
|
+
const value = stat[key];
|
|
4759
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
4760
|
+
};
|
|
4761
|
+
var booleanStat = (stat, key) => {
|
|
4762
|
+
const value = stat[key];
|
|
4763
|
+
return typeof value === "boolean" ? value : undefined;
|
|
4764
|
+
};
|
|
4765
|
+
var stringStat = (stat, key) => {
|
|
4766
|
+
const value = stat[key];
|
|
4767
|
+
return typeof value === "string" ? value : undefined;
|
|
4768
|
+
};
|
|
4769
|
+
var secondsToMs = (value) => value === undefined ? undefined : value * 1000;
|
|
4770
|
+
var normalizeWebRTCStat = (stat) => {
|
|
4771
|
+
const sample = {};
|
|
4772
|
+
for (const [key, value] of Object.entries(stat)) {
|
|
4773
|
+
if (value === null || typeof value === "boolean" || typeof value === "number" || typeof value === "string") {
|
|
4774
|
+
sample[key] = value;
|
|
4775
|
+
}
|
|
4776
|
+
}
|
|
4777
|
+
return sample;
|
|
4778
|
+
};
|
|
4779
|
+
var buildMediaResamplingPlan = (input) => {
|
|
4780
|
+
const required = !formatMatches(input.inputFormat, input.outputFormat);
|
|
4781
|
+
return {
|
|
4782
|
+
inputFormat: input.inputFormat,
|
|
4783
|
+
outputFormat: input.outputFormat,
|
|
4784
|
+
ratio: input.outputFormat.sampleRateHz / input.inputFormat.sampleRateHz,
|
|
4785
|
+
required,
|
|
4786
|
+
status: input.inputFormat.container === input.outputFormat.container && input.inputFormat.encoding === input.outputFormat.encoding && input.inputFormat.channels === input.outputFormat.channels ? "pass" : "warn"
|
|
4787
|
+
};
|
|
4788
|
+
};
|
|
4789
|
+
var speechProbability = (frame) => {
|
|
4790
|
+
if (frame.metadata?.isSpeech === true) {
|
|
4791
|
+
return 1;
|
|
4792
|
+
}
|
|
4793
|
+
if (frame.metadata?.isSpeech === false) {
|
|
4794
|
+
return 0;
|
|
4795
|
+
}
|
|
4796
|
+
for (const key of ["speechProbability", "voiceProbability", "rms", "energy"]) {
|
|
4797
|
+
const value = numericMetadata(frame, key);
|
|
4798
|
+
if (value !== undefined) {
|
|
4799
|
+
return value;
|
|
4800
|
+
}
|
|
4801
|
+
}
|
|
4802
|
+
return 0;
|
|
4803
|
+
};
|
|
4804
|
+
var buildMediaVadReport = (input = {}) => {
|
|
4805
|
+
const frames = (input.frames ?? []).filter((frame) => frame.kind === "input-audio");
|
|
4806
|
+
const speechStartThreshold = input.speechStartThreshold ?? 0.6;
|
|
4807
|
+
const speechEndThreshold = input.speechEndThreshold ?? 0.35;
|
|
4808
|
+
const minSpeechFrames = input.minSpeechFrames ?? 1;
|
|
4809
|
+
const maxSilenceFrames = input.maxSilenceFrames ?? 1;
|
|
4810
|
+
const segments = [];
|
|
4811
|
+
let activeFrames = [];
|
|
4812
|
+
let silenceFrames = 0;
|
|
4813
|
+
const closeSegment = () => {
|
|
4814
|
+
if (activeFrames.length < minSpeechFrames) {
|
|
4815
|
+
activeFrames = [];
|
|
4816
|
+
silenceFrames = 0;
|
|
4817
|
+
return;
|
|
4818
|
+
}
|
|
4819
|
+
const first = activeFrames[0];
|
|
4820
|
+
const last = activeFrames.at(-1);
|
|
4821
|
+
if (!first) {
|
|
4822
|
+
return;
|
|
4823
|
+
}
|
|
4824
|
+
segments.push({
|
|
4825
|
+
durationMs: first.at !== undefined && last?.at !== undefined ? last.at - first.at + (last.durationMs ?? 0) : undefined,
|
|
4826
|
+
endAt: last?.at !== undefined ? last.at + (last.durationMs ?? 0) : undefined,
|
|
4827
|
+
frameCount: activeFrames.length,
|
|
4828
|
+
segmentId: `vad:${String(segments.length + 1)}`,
|
|
4829
|
+
sessionId: first.sessionId,
|
|
4830
|
+
startAt: first.at,
|
|
4831
|
+
turnId: first.turnId
|
|
4832
|
+
});
|
|
4833
|
+
activeFrames = [];
|
|
4834
|
+
silenceFrames = 0;
|
|
4835
|
+
};
|
|
4836
|
+
for (const frame of frames) {
|
|
4837
|
+
const probability = speechProbability(frame);
|
|
4838
|
+
if (activeFrames.length === 0) {
|
|
4839
|
+
if (probability >= speechStartThreshold) {
|
|
4840
|
+
activeFrames.push(frame);
|
|
4841
|
+
}
|
|
4842
|
+
continue;
|
|
4843
|
+
}
|
|
4844
|
+
activeFrames.push(frame);
|
|
4845
|
+
if (probability <= speechEndThreshold) {
|
|
4846
|
+
silenceFrames += 1;
|
|
4847
|
+
} else {
|
|
4848
|
+
silenceFrames = 0;
|
|
4849
|
+
}
|
|
4850
|
+
if (silenceFrames > maxSilenceFrames) {
|
|
4851
|
+
closeSegment();
|
|
4852
|
+
}
|
|
4853
|
+
}
|
|
4854
|
+
closeSegment();
|
|
4855
|
+
return {
|
|
4856
|
+
checkedAt: Date.now(),
|
|
4857
|
+
inputAudioFrames: frames.length,
|
|
4858
|
+
segments,
|
|
4859
|
+
status: frames.length === 0 ? "warn" : "pass"
|
|
4860
|
+
};
|
|
4861
|
+
};
|
|
4862
|
+
var buildMediaInterruptionReport = (input = {}) => {
|
|
4863
|
+
const issues = [];
|
|
4864
|
+
const interruptionFrames = (input.frames ?? []).filter((frame) => frame.kind === "interruption");
|
|
4865
|
+
const latenciesMs = interruptionFrames.map((frame) => frame.latencyMs).filter((latency) => typeof latency === "number");
|
|
4866
|
+
const maxInterruptionLatencyMs = input.maxInterruptionLatencyMs;
|
|
4867
|
+
if (interruptionFrames.length === 0) {
|
|
4868
|
+
pushIssue(issues, "warning", "media.interruption_missing", "No interruption frame was observed.");
|
|
4869
|
+
}
|
|
4870
|
+
if (maxInterruptionLatencyMs !== undefined && latenciesMs.some((latency) => latency > maxInterruptionLatencyMs)) {
|
|
4871
|
+
pushIssue(issues, "error", "media.interruption_latency", `Interruption latency exceeded ${String(maxInterruptionLatencyMs)}ms.`);
|
|
4872
|
+
}
|
|
4873
|
+
return {
|
|
4874
|
+
checkedAt: Date.now(),
|
|
4875
|
+
interruptionFrames: interruptionFrames.length,
|
|
4876
|
+
issues,
|
|
4877
|
+
latenciesMs,
|
|
4878
|
+
status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass"
|
|
4879
|
+
};
|
|
4880
|
+
};
|
|
4881
|
+
var buildMediaQualityReport = (input = {}) => {
|
|
4882
|
+
const frames = [...input.frames ?? []].sort((a, b) => (a.at ?? 0) - (b.at ?? 0));
|
|
4883
|
+
const audioFrames = frames.filter((frame) => frame.kind === "input-audio" || frame.kind === "assistant-audio");
|
|
4884
|
+
const inputAudioFrames = frames.filter((frame) => frame.kind === "input-audio");
|
|
4885
|
+
const assistantAudioFrames = frames.filter((frame) => frame.kind === "assistant-audio");
|
|
4886
|
+
const issues = [];
|
|
4887
|
+
const gapsMs = [];
|
|
4888
|
+
for (const [index, frame] of audioFrames.entries()) {
|
|
4889
|
+
const previous = audioFrames[index - 1];
|
|
4890
|
+
if (previous?.at === undefined || frame.at === undefined || previous.durationMs === undefined) {
|
|
4891
|
+
continue;
|
|
4892
|
+
}
|
|
4893
|
+
const gap = frame.at - (previous.at + previous.durationMs);
|
|
4894
|
+
if (gap > 0) {
|
|
4895
|
+
gapsMs.push(gap);
|
|
4896
|
+
}
|
|
4897
|
+
}
|
|
4898
|
+
const jitterMs = audioFrames.map((frame) => numericMetadata(frame, "jitterMs")).filter((value) => value !== undefined).at(-1) ?? max(gapsMs);
|
|
4899
|
+
const first = audioFrames.find((frame) => frame.at !== undefined);
|
|
4900
|
+
const last = audioFrames.toReversed().find((frame) => frame.at !== undefined);
|
|
4901
|
+
const durationMs = first?.at !== undefined && last?.at !== undefined ? last.at - first.at + (last.durationMs ?? 0) : undefined;
|
|
4902
|
+
const expectedDurationMs = audioFrames.length > 0 ? audioFrames.reduce((total, frame) => total + (frame.durationMs ?? 0), 0) : undefined;
|
|
4903
|
+
const timestampDriftMs = durationMs !== undefined && expectedDurationMs !== undefined ? Math.max(0, durationMs - expectedDurationMs) : undefined;
|
|
4904
|
+
const speechScores = inputAudioFrames.map(speechProbability);
|
|
4905
|
+
const speechFrames = speechScores.filter((score) => score >= 0.6).length;
|
|
4906
|
+
const silenceFrames = speechScores.filter((score) => score <= 0.35).length;
|
|
4907
|
+
const unknownSpeechFrames = Math.max(0, inputAudioFrames.length - speechFrames - silenceFrames);
|
|
4908
|
+
const speechRatio = inputAudioFrames.length === 0 ? 0 : speechFrames / inputAudioFrames.length;
|
|
4909
|
+
const silenceRatio = inputAudioFrames.length === 0 ? 0 : silenceFrames / inputAudioFrames.length;
|
|
4910
|
+
const levels = audioFrames.map((frame) => numericMetadata(frame, "level") ?? numericMetadata(frame, "rms") ?? numericMetadata(frame, "energy")).filter((value) => value !== undefined);
|
|
4911
|
+
const backpressureEvents = input.transport?.backpressureEvents ?? 0;
|
|
4912
|
+
const maxGapMs = input.maxGapMs;
|
|
4913
|
+
if (maxGapMs !== undefined && gapsMs.some((gap) => gap > maxGapMs)) {
|
|
4914
|
+
pushIssue(issues, "warning", "media.quality_gap", `Observed media gap above ${String(maxGapMs)}ms.`);
|
|
4915
|
+
}
|
|
4916
|
+
if (input.maxJitterMs !== undefined && jitterMs !== undefined && jitterMs > input.maxJitterMs) {
|
|
4917
|
+
pushIssue(issues, "warning", "media.quality_jitter", `Observed jitter ${String(jitterMs)}ms above ${String(input.maxJitterMs)}ms.`);
|
|
4918
|
+
}
|
|
4919
|
+
if (input.maxTimestampDriftMs !== undefined && timestampDriftMs !== undefined && timestampDriftMs > input.maxTimestampDriftMs) {
|
|
4920
|
+
pushIssue(issues, "warning", "media.quality_timestamp_drift", `Observed timestamp drift ${String(timestampDriftMs)}ms above ${String(input.maxTimestampDriftMs)}ms.`);
|
|
4921
|
+
}
|
|
4922
|
+
if (input.minSpeechRatio !== undefined && inputAudioFrames.length > 0 && speechRatio < input.minSpeechRatio) {
|
|
4923
|
+
pushIssue(issues, "warning", "media.quality_speech_ratio", `Observed speech ratio ${String(speechRatio)} below ${String(input.minSpeechRatio)}.`);
|
|
4924
|
+
}
|
|
4925
|
+
if (input.maxBackpressureEvents !== undefined && backpressureEvents > input.maxBackpressureEvents) {
|
|
4926
|
+
pushIssue(issues, "warning", "media.quality_backpressure", `Observed ${String(backpressureEvents)} backpressure event(s), above ${String(input.maxBackpressureEvents)}.`);
|
|
4927
|
+
}
|
|
4928
|
+
return {
|
|
4929
|
+
assistantAudioFrames: assistantAudioFrames.length,
|
|
4930
|
+
backpressureEvents,
|
|
4931
|
+
checkedAt: Date.now(),
|
|
4932
|
+
durationMs,
|
|
4933
|
+
gapCount: gapsMs.length,
|
|
4934
|
+
gapsMs,
|
|
4935
|
+
inputAudioFrames: inputAudioFrames.length,
|
|
4936
|
+
issues,
|
|
4937
|
+
jitterMs,
|
|
4938
|
+
levelAverage: average(levels),
|
|
4939
|
+
levelMax: max(levels),
|
|
4940
|
+
levelMin: min(levels),
|
|
4941
|
+
silenceFrames,
|
|
4942
|
+
silenceRatio,
|
|
4943
|
+
speechFrames,
|
|
4944
|
+
speechRatio,
|
|
4945
|
+
status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
|
|
4946
|
+
timestampDriftMs,
|
|
4947
|
+
totalFrames: frames.length,
|
|
4948
|
+
unknownSpeechFrames
|
|
4949
|
+
};
|
|
4950
|
+
};
|
|
4951
|
+
var buildMediaWebRTCStatsReport = (input = {}) => {
|
|
4952
|
+
const stats = input.stats ?? [];
|
|
4953
|
+
const issues = [];
|
|
4954
|
+
const inbound = stats.filter((stat) => stat.type === "inbound-rtp" && stringStat(stat, "kind") !== "video");
|
|
4955
|
+
const outbound = stats.filter((stat) => stat.type === "outbound-rtp" && stringStat(stat, "kind") !== "video");
|
|
4956
|
+
const candidatePairs = stats.filter((stat) => stat.type === "candidate-pair");
|
|
4957
|
+
const audioTracks = stats.filter((stat) => (stat.type === "track" || stat.type === "media-source") && stringStat(stat, "kind") === "audio");
|
|
4958
|
+
const activeCandidatePairs = candidatePairs.filter((stat) => booleanStat(stat, "selected") === true || booleanStat(stat, "nominated") === true || stringStat(stat, "state") === "succeeded").length;
|
|
4959
|
+
const liveAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") !== "ended" && stringStat(stat, "trackState") !== "ended" && booleanStat(stat, "ended") !== true).length;
|
|
4960
|
+
const endedAudioTracks = audioTracks.filter((stat) => stringStat(stat, "readyState") === "ended" || stringStat(stat, "trackState") === "ended" || booleanStat(stat, "ended") === true).length;
|
|
4961
|
+
const inboundPackets = inbound.reduce((total, stat) => total + (numericStat(stat, "packetsReceived") ?? 0), 0);
|
|
4962
|
+
const outboundPackets = outbound.reduce((total, stat) => total + (numericStat(stat, "packetsSent") ?? 0), 0);
|
|
4963
|
+
const packetsLost = [...inbound, ...outbound].reduce((total, stat) => total + Math.max(0, numericStat(stat, "packetsLost") ?? 0), 0);
|
|
4964
|
+
const packetLossDenominator = inboundPackets + packetsLost;
|
|
4965
|
+
const packetLossRatio = packetLossDenominator === 0 ? 0 : packetsLost / packetLossDenominator;
|
|
4966
|
+
const bytesReceived = inbound.reduce((total, stat) => total + (numericStat(stat, "bytesReceived") ?? 0), 0);
|
|
4967
|
+
const bytesSent = outbound.reduce((total, stat) => total + (numericStat(stat, "bytesSent") ?? 0), 0);
|
|
4968
|
+
const roundTripTimeMs = max(candidatePairs.map((stat) => secondsToMs(numericStat(stat, "currentRoundTripTime") ?? numericStat(stat, "roundTripTime"))).filter((value) => value !== undefined));
|
|
4969
|
+
const jitterMs = max([...inbound, ...outbound].map((stat) => secondsToMs(numericStat(stat, "jitter"))).filter((value) => value !== undefined));
|
|
4970
|
+
const jitterBufferDelayMs = max(inbound.map((stat) => {
|
|
4971
|
+
const delay = numericStat(stat, "jitterBufferDelay");
|
|
4972
|
+
const emitted = numericStat(stat, "jitterBufferEmittedCount");
|
|
4973
|
+
return delay !== undefined && emitted !== undefined && emitted > 0 ? delay / emitted * 1000 : undefined;
|
|
4974
|
+
}).filter((value) => value !== undefined));
|
|
4975
|
+
const audioLevels = audioTracks.map((stat) => numericStat(stat, "audioLevel")).filter((value) => value !== undefined);
|
|
4976
|
+
if (input.requireConnectedCandidatePair && candidatePairs.length > 0 && activeCandidatePairs === 0) {
|
|
4977
|
+
pushIssue(issues, "error", "media.webrtc_candidate_pair_missing", "No active WebRTC candidate pair was observed.");
|
|
4978
|
+
}
|
|
4979
|
+
if (input.requireLiveAudioTrack && liveAudioTracks === 0) {
|
|
4980
|
+
pushIssue(issues, "error", "media.webrtc_audio_track_missing", "No live WebRTC audio track was observed.");
|
|
4981
|
+
}
|
|
4982
|
+
if (input.maxPacketLossRatio !== undefined && packetLossRatio > input.maxPacketLossRatio) {
|
|
4983
|
+
pushIssue(issues, "warning", "media.webrtc_packet_loss", `Observed WebRTC packet loss ratio ${String(packetLossRatio)} above ${String(input.maxPacketLossRatio)}.`);
|
|
4984
|
+
}
|
|
4985
|
+
if (input.maxRoundTripTimeMs !== undefined && roundTripTimeMs !== undefined && roundTripTimeMs > input.maxRoundTripTimeMs) {
|
|
4986
|
+
pushIssue(issues, "warning", "media.webrtc_round_trip_time", `Observed WebRTC RTT ${String(roundTripTimeMs)}ms above ${String(input.maxRoundTripTimeMs)}ms.`);
|
|
4987
|
+
}
|
|
4988
|
+
if (input.maxJitterMs !== undefined && jitterMs !== undefined && jitterMs > input.maxJitterMs) {
|
|
4989
|
+
pushIssue(issues, "warning", "media.webrtc_jitter", `Observed WebRTC jitter ${String(jitterMs)}ms above ${String(input.maxJitterMs)}ms.`);
|
|
4990
|
+
}
|
|
4991
|
+
return {
|
|
4992
|
+
activeCandidatePairs,
|
|
4993
|
+
audioLevelAverage: average(audioLevels),
|
|
4994
|
+
bytesReceived,
|
|
4995
|
+
bytesSent,
|
|
4996
|
+
checkedAt: Date.now(),
|
|
4997
|
+
endedAudioTracks,
|
|
4998
|
+
inboundPackets,
|
|
4999
|
+
issues,
|
|
5000
|
+
jitterBufferDelayMs,
|
|
5001
|
+
jitterMs,
|
|
5002
|
+
liveAudioTracks,
|
|
5003
|
+
outboundPackets,
|
|
5004
|
+
packetLossRatio,
|
|
5005
|
+
packetsLost,
|
|
5006
|
+
roundTripTimeMs,
|
|
5007
|
+
status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
|
|
5008
|
+
totalStats: stats.length
|
|
5009
|
+
};
|
|
5010
|
+
};
|
|
5011
|
+
var collectMediaWebRTCStats = async (input) => {
|
|
5012
|
+
const report = await input.peerConnection.getStats(input.selector ?? null);
|
|
5013
|
+
return [...report.values()].map(normalizeWebRTCStat);
|
|
5014
|
+
};
|
|
5015
|
+
var collectMediaWebRTCStatsReport = async (input) => {
|
|
5016
|
+
const stats = await collectMediaWebRTCStats(input);
|
|
5017
|
+
return buildMediaWebRTCStatsReport({
|
|
5018
|
+
...input,
|
|
5019
|
+
stats
|
|
5020
|
+
});
|
|
5021
|
+
};
|
|
5022
|
+
var buildMediaPipelineCalibrationReport = (input = {}) => {
|
|
5023
|
+
const frames = input.frames ?? [];
|
|
5024
|
+
const issues = [];
|
|
5025
|
+
const inputFrames = frames.filter((frame) => frame.kind === "input-audio");
|
|
5026
|
+
const assistantFrames = frames.filter((frame) => frame.kind === "assistant-audio");
|
|
5027
|
+
const turnCommitFrames = frames.filter((frame) => frame.kind === "turn-commit");
|
|
5028
|
+
const interruptionFrameRecords = frames.filter((frame) => frame.kind === "interruption");
|
|
5029
|
+
const traceLinkedFrames = frames.filter((frame) => frame.traceEventId).length;
|
|
5030
|
+
const backpressureFrames = frames.filter((frame) => Boolean(frame.metadata?.backpressure)).length;
|
|
5031
|
+
const audioLatencies = assistantFrames.map((frame) => frame.latencyMs).filter((latency) => typeof latency === "number");
|
|
5032
|
+
const firstAudioLatencyMs = audioLatencies.length > 0 ? Math.min(...audioLatencies) : undefined;
|
|
5033
|
+
const jitterValues = frames.map((frame) => numericMetadata(frame, "jitterMs")).filter((value) => value !== undefined);
|
|
5034
|
+
const jitterMs = jitterValues.length > 0 ? Math.max(...jitterValues) : undefined;
|
|
5035
|
+
const inputFormat = input.inputFormat ?? inputFrames.find((frame) => frame.format)?.format;
|
|
5036
|
+
const outputFormat = input.outputFormat ?? assistantFrames.find((frame) => frame.format)?.format;
|
|
5037
|
+
const resamplingRequired = Boolean(input.expectedInputFormat && inputFormat && inputFormat.sampleRateHz !== input.expectedInputFormat.sampleRateHz) || Boolean(input.expectedOutputFormat && outputFormat && outputFormat.sampleRateHz !== input.expectedOutputFormat.sampleRateHz);
|
|
5038
|
+
const resamplingTargetHz = resamplingRequired && input.expectedInputFormat ? input.expectedInputFormat.sampleRateHz : resamplingRequired ? input.expectedOutputFormat?.sampleRateHz : undefined;
|
|
5039
|
+
if (inputFrames.length === 0) {
|
|
5040
|
+
pushIssue(issues, "warning", "media.input_audio_missing", "No input audio frames were observed.");
|
|
5041
|
+
}
|
|
5042
|
+
if (assistantFrames.length === 0) {
|
|
5043
|
+
pushIssue(issues, "warning", "media.assistant_audio_missing", "No assistant audio frames were observed.");
|
|
5044
|
+
}
|
|
5045
|
+
if (input.expectedInputFormat && inputFormat && !formatMatches(inputFormat, input.expectedInputFormat)) {
|
|
5046
|
+
pushIssue(issues, inputFormat.sampleRateHz === input.expectedInputFormat.sampleRateHz ? "warning" : "error", "media.input_format_mismatch", `Input format ${formatLabel(inputFormat)} does not match expected ${formatLabel(input.expectedInputFormat)}.`);
|
|
5047
|
+
}
|
|
5048
|
+
if (input.expectedOutputFormat && outputFormat && !formatMatches(outputFormat, input.expectedOutputFormat)) {
|
|
5049
|
+
pushIssue(issues, outputFormat.sampleRateHz === input.expectedOutputFormat.sampleRateHz ? "warning" : "error", "media.output_format_mismatch", `Output format ${formatLabel(outputFormat)} does not match expected ${formatLabel(input.expectedOutputFormat)}.`);
|
|
5050
|
+
}
|
|
5051
|
+
if (firstAudioLatencyMs !== undefined && input.maxFirstAudioLatencyMs !== undefined && firstAudioLatencyMs > input.maxFirstAudioLatencyMs) {
|
|
5052
|
+
pushIssue(issues, "error", "media.first_audio_latency", `First audio latency ${String(firstAudioLatencyMs)}ms exceeds budget ${String(input.maxFirstAudioLatencyMs)}ms.`);
|
|
5053
|
+
}
|
|
5054
|
+
if (jitterMs !== undefined && input.maxJitterMs !== undefined && jitterMs > input.maxJitterMs) {
|
|
5055
|
+
pushIssue(issues, "warning", "media.jitter", `Media jitter ${String(jitterMs)}ms exceeds budget ${String(input.maxJitterMs)}ms.`);
|
|
5056
|
+
}
|
|
5057
|
+
if (input.maxBackpressureFrames !== undefined && backpressureFrames > input.maxBackpressureFrames) {
|
|
5058
|
+
pushIssue(issues, "warning", "media.backpressure", `Backpressure frame count ${String(backpressureFrames)} exceeds budget ${String(input.maxBackpressureFrames)}.`);
|
|
5059
|
+
}
|
|
5060
|
+
if (input.requireInterruptionFrame && interruptionFrameRecords.length === 0) {
|
|
5061
|
+
pushIssue(issues, "warning", "media.interruption_missing", "No interruption frame was observed.");
|
|
5062
|
+
}
|
|
5063
|
+
if (input.requireTraceEvidence && traceLinkedFrames === 0) {
|
|
5064
|
+
pushIssue(issues, "warning", "media.trace_evidence_missing", "No media frames were linked to trace evidence.");
|
|
5065
|
+
}
|
|
5066
|
+
return {
|
|
5067
|
+
assistantAudioFrames: assistantFrames.length,
|
|
5068
|
+
backpressureFrames,
|
|
5069
|
+
checkedAt: Date.now(),
|
|
5070
|
+
firstAudioLatencyMs,
|
|
5071
|
+
inputAudioFrames: inputFrames.length,
|
|
5072
|
+
inputFormat,
|
|
5073
|
+
interruptionFrames: interruptionFrameRecords.length,
|
|
5074
|
+
issues,
|
|
5075
|
+
jitterMs,
|
|
5076
|
+
outputFormat,
|
|
5077
|
+
resamplingRequired,
|
|
5078
|
+
resamplingTargetHz,
|
|
5079
|
+
status: issues.some((issue) => issue.severity === "error") ? "fail" : issues.length > 0 ? "warn" : "pass",
|
|
5080
|
+
surface: input.surface ?? "media-pipeline",
|
|
5081
|
+
traceLinkedFrames,
|
|
5082
|
+
turnCommitFrames: turnCommitFrames.length
|
|
5083
|
+
};
|
|
5084
|
+
};
|
|
5085
|
+
|
|
5086
|
+
// src/client/browserMedia.ts
|
|
5087
|
+
var DEFAULT_BROWSER_MEDIA_PATH = "/api/voice/browser-media";
|
|
5088
|
+
var DEFAULT_BROWSER_MEDIA_INTERVAL_MS = 5000;
|
|
5089
|
+
var resolvePeerConnection = async (options) => options.peerConnection ?? await options.getPeerConnection?.() ?? null;
|
|
5090
|
+
var postBrowserMediaReport = async (payload, options) => {
|
|
5091
|
+
const requestFetch = options.fetch ?? globalThis.fetch;
|
|
5092
|
+
if (!requestFetch) {
|
|
5093
|
+
return;
|
|
5094
|
+
}
|
|
5095
|
+
await requestFetch(options.path ?? DEFAULT_BROWSER_MEDIA_PATH, {
|
|
5096
|
+
body: JSON.stringify(payload),
|
|
5097
|
+
headers: {
|
|
5098
|
+
"Content-Type": "application/json"
|
|
5099
|
+
},
|
|
5100
|
+
keepalive: true,
|
|
5101
|
+
method: "POST"
|
|
5102
|
+
});
|
|
5103
|
+
};
|
|
5104
|
+
var createVoiceBrowserMediaReporter = (options) => {
|
|
5105
|
+
let interval = null;
|
|
5106
|
+
const reportOnce = async () => {
|
|
5107
|
+
const peerConnection = await resolvePeerConnection(options);
|
|
5108
|
+
if (!peerConnection) {
|
|
5109
|
+
return;
|
|
5110
|
+
}
|
|
5111
|
+
const report = await collectMediaWebRTCStatsReport({
|
|
5112
|
+
...options,
|
|
5113
|
+
peerConnection
|
|
5114
|
+
});
|
|
5115
|
+
const payload = {
|
|
5116
|
+
at: Date.now(),
|
|
5117
|
+
report,
|
|
5118
|
+
scenarioId: options.getScenarioId?.() ?? null,
|
|
5119
|
+
sessionId: options.getSessionId?.() ?? null
|
|
5120
|
+
};
|
|
5121
|
+
options.onReport?.(payload);
|
|
5122
|
+
await postBrowserMediaReport(payload, options);
|
|
5123
|
+
return payload;
|
|
5124
|
+
};
|
|
5125
|
+
const run = () => {
|
|
5126
|
+
reportOnce().catch((error) => {
|
|
5127
|
+
options.onError?.(error);
|
|
5128
|
+
});
|
|
5129
|
+
};
|
|
5130
|
+
const stop = () => {
|
|
5131
|
+
if (interval) {
|
|
5132
|
+
clearInterval(interval);
|
|
5133
|
+
interval = null;
|
|
5134
|
+
}
|
|
5135
|
+
};
|
|
5136
|
+
return {
|
|
5137
|
+
close: stop,
|
|
5138
|
+
reportOnce,
|
|
5139
|
+
start: () => {
|
|
5140
|
+
if (interval) {
|
|
5141
|
+
return;
|
|
5142
|
+
}
|
|
5143
|
+
run();
|
|
5144
|
+
interval = setInterval(run, options.intervalMs ?? DEFAULT_BROWSER_MEDIA_INTERVAL_MS);
|
|
5145
|
+
},
|
|
5146
|
+
stop
|
|
5147
|
+
};
|
|
5148
|
+
};
|
|
5149
|
+
|
|
4744
5150
|
// src/client/connection.ts
|
|
4745
5151
|
var WS_OPEN = 1;
|
|
4746
5152
|
var WS_CLOSED = 3;
|
|
@@ -5173,12 +5579,18 @@ var createVoiceStreamStore = () => {
|
|
|
5173
5579
|
var createVoiceStream = (path, options = {}) => {
|
|
5174
5580
|
const connection = createVoiceConnection(path, options);
|
|
5175
5581
|
const store = createVoiceStreamStore();
|
|
5582
|
+
const browserMediaReporter = options.browserMedia && typeof window !== "undefined" ? createVoiceBrowserMediaReporter({
|
|
5583
|
+
...options.browserMedia,
|
|
5584
|
+
getScenarioId: () => options.browserMedia ? options.browserMedia.getScenarioId?.() ?? connection.getScenarioId() : connection.getScenarioId(),
|
|
5585
|
+
getSessionId: () => options.browserMedia ? options.browserMedia.getSessionId?.() ?? connection.getSessionId() : connection.getSessionId()
|
|
5586
|
+
}) : null;
|
|
5176
5587
|
const subscribers = new Set;
|
|
5177
5588
|
const start = (input) => Promise.resolve().then(() => {
|
|
5178
5589
|
if (!input?.sessionId && !input?.scenarioId) {
|
|
5179
5590
|
return;
|
|
5180
5591
|
}
|
|
5181
5592
|
connection.start(input);
|
|
5593
|
+
browserMediaReporter?.start();
|
|
5182
5594
|
});
|
|
5183
5595
|
const notify = () => {
|
|
5184
5596
|
subscribers.forEach((subscriber) => subscriber());
|
|
@@ -5220,6 +5632,7 @@ var createVoiceStream = (path, options = {}) => {
|
|
|
5220
5632
|
},
|
|
5221
5633
|
close() {
|
|
5222
5634
|
unsubscribeConnection();
|
|
5635
|
+
browserMediaReporter?.close();
|
|
5223
5636
|
connection.close();
|
|
5224
5637
|
store.dispatch({ type: "disconnected" });
|
|
5225
5638
|
notify();
|