@absolutejs/voice 0.0.22-beta.584 → 0.0.22-beta.586
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 +126 -0
- package/dist/client/htmxBootstrap.js +11 -0
- package/dist/client/index.js +126 -0
- package/dist/core/turnDetection.d.ts +1 -0
- package/dist/core/types.d.ts +4 -0
- package/dist/embed/index.js +11 -0
- package/dist/embed/voice-widget.js +8 -8
- package/dist/index.js +203 -119
- package/dist/react/index.js +126 -0
- package/dist/svelte/index.js +126 -0
- package/dist/testing/index.js +83 -2
- package/dist/vue/index.js +126 -0
- package/package.json +1 -1
package/dist/svelte/index.js
CHANGED
|
@@ -1380,22 +1380,146 @@ var resolveAudioConditioningConfig = (config) => {
|
|
|
1380
1380
|
};
|
|
1381
1381
|
};
|
|
1382
1382
|
|
|
1383
|
+
// src/core/turnDetection.ts
|
|
1384
|
+
var DEFAULT_SILENCE_MS = 700;
|
|
1385
|
+
var DEFAULT_SPEECH_THRESHOLD = 0.015;
|
|
1386
|
+
var DEFAULT_SEMANTIC_VETO_RECHECK_MS = 1200;
|
|
1387
|
+
var toUint8Array = (audio) => {
|
|
1388
|
+
if (audio instanceof ArrayBuffer) {
|
|
1389
|
+
return new Uint8Array(audio);
|
|
1390
|
+
}
|
|
1391
|
+
return new Uint8Array(audio.buffer, audio.byteOffset, audio.byteLength);
|
|
1392
|
+
};
|
|
1393
|
+
var measureAudioLevel = (audio) => {
|
|
1394
|
+
const bytes = toUint8Array(audio);
|
|
1395
|
+
if (bytes.byteLength < 2) {
|
|
1396
|
+
return 0;
|
|
1397
|
+
}
|
|
1398
|
+
const samples = new Int16Array(bytes.buffer, bytes.byteOffset, Math.floor(bytes.byteLength / 2));
|
|
1399
|
+
if (samples.length === 0) {
|
|
1400
|
+
return 0;
|
|
1401
|
+
}
|
|
1402
|
+
let sumSquares = 0;
|
|
1403
|
+
for (const sample of samples) {
|
|
1404
|
+
const normalized = sample / 32768;
|
|
1405
|
+
sumSquares += normalized * normalized;
|
|
1406
|
+
}
|
|
1407
|
+
return Math.sqrt(sumSquares / samples.length);
|
|
1408
|
+
};
|
|
1409
|
+
var normalizeText = (value) => value.trim().replace(/\s+/g, " ");
|
|
1410
|
+
var countWords = (value) => value.length > 0 ? value.split(" ").length : 0;
|
|
1411
|
+
var selectPreferredTranscriptText = (currentText, nextText) => {
|
|
1412
|
+
const current = normalizeText(currentText);
|
|
1413
|
+
const next = normalizeText(nextText);
|
|
1414
|
+
if (!current) {
|
|
1415
|
+
return next;
|
|
1416
|
+
}
|
|
1417
|
+
if (!next) {
|
|
1418
|
+
return current;
|
|
1419
|
+
}
|
|
1420
|
+
if (current === next || current.includes(next)) {
|
|
1421
|
+
return current;
|
|
1422
|
+
}
|
|
1423
|
+
if (next.includes(current)) {
|
|
1424
|
+
return next;
|
|
1425
|
+
}
|
|
1426
|
+
if (countWords(next) > countWords(current)) {
|
|
1427
|
+
return next;
|
|
1428
|
+
}
|
|
1429
|
+
if (countWords(next) === countWords(current) && next.length > current.length) {
|
|
1430
|
+
return next;
|
|
1431
|
+
}
|
|
1432
|
+
return current;
|
|
1433
|
+
};
|
|
1434
|
+
var mergeSequentialTranscriptText = (currentText, nextText) => {
|
|
1435
|
+
const current = normalizeText(currentText);
|
|
1436
|
+
const next = normalizeText(nextText);
|
|
1437
|
+
if (!current) {
|
|
1438
|
+
return next;
|
|
1439
|
+
}
|
|
1440
|
+
if (!next) {
|
|
1441
|
+
return current;
|
|
1442
|
+
}
|
|
1443
|
+
const currentWords = current.split(" ");
|
|
1444
|
+
const nextWords = next.split(" ");
|
|
1445
|
+
const maxOverlap = Math.min(currentWords.length, nextWords.length);
|
|
1446
|
+
for (let overlap = maxOverlap;overlap > 0; overlap -= 1) {
|
|
1447
|
+
const currentSuffix = currentWords.slice(-overlap).join(" ");
|
|
1448
|
+
const nextPrefix = nextWords.slice(0, overlap).join(" ");
|
|
1449
|
+
if (currentSuffix === nextPrefix) {
|
|
1450
|
+
return [...currentWords, ...nextWords.slice(overlap)].join(" ");
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
return `${current} ${next}`.trim();
|
|
1454
|
+
};
|
|
1455
|
+
var countCommonPrefixWords = (currentText, nextText) => {
|
|
1456
|
+
const currentWords = normalizeText(currentText).split(" ").filter(Boolean);
|
|
1457
|
+
const nextWords = normalizeText(nextText).split(" ").filter(Boolean);
|
|
1458
|
+
const maxWords = Math.min(currentWords.length, nextWords.length);
|
|
1459
|
+
let count = 0;
|
|
1460
|
+
for (let index = 0;index < maxWords; index += 1) {
|
|
1461
|
+
if (currentWords[index] !== nextWords[index]) {
|
|
1462
|
+
break;
|
|
1463
|
+
}
|
|
1464
|
+
count += 1;
|
|
1465
|
+
}
|
|
1466
|
+
return count;
|
|
1467
|
+
};
|
|
1468
|
+
var mergeTranscriptTexts = (transcripts) => {
|
|
1469
|
+
const merged = [];
|
|
1470
|
+
for (const transcript of transcripts) {
|
|
1471
|
+
const nextText = normalizeText(transcript.text);
|
|
1472
|
+
if (!nextText) {
|
|
1473
|
+
continue;
|
|
1474
|
+
}
|
|
1475
|
+
const previous = merged.at(-1);
|
|
1476
|
+
if (!previous) {
|
|
1477
|
+
merged.push(nextText);
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
if (nextText === previous || previous.includes(nextText)) {
|
|
1481
|
+
continue;
|
|
1482
|
+
}
|
|
1483
|
+
if (nextText.includes(previous)) {
|
|
1484
|
+
merged[merged.length - 1] = nextText;
|
|
1485
|
+
continue;
|
|
1486
|
+
}
|
|
1487
|
+
merged.push(nextText);
|
|
1488
|
+
}
|
|
1489
|
+
return merged.join(" ").trim();
|
|
1490
|
+
};
|
|
1491
|
+
var buildTurnText = (transcripts, partialText, options = {}) => {
|
|
1492
|
+
const finalText = mergeTranscriptTexts(transcripts);
|
|
1493
|
+
const nextPartial = normalizeText(partialText);
|
|
1494
|
+
const lastFinalEndedAtMs = [...transcripts].reverse().find((transcript) => typeof transcript.endedAtMs === "number")?.endedAtMs;
|
|
1495
|
+
if (finalText && nextPartial && typeof lastFinalEndedAtMs === "number" && typeof options.partialStartedAtMs === "number" && options.partialStartedAtMs - lastFinalEndedAtMs >= 250 && countCommonPrefixWords(finalText, nextPartial) === 0) {
|
|
1496
|
+
return mergeSequentialTranscriptText(finalText, nextPartial);
|
|
1497
|
+
}
|
|
1498
|
+
return selectPreferredTranscriptText(finalText, nextPartial);
|
|
1499
|
+
};
|
|
1500
|
+
|
|
1383
1501
|
// src/core/turnProfiles.ts
|
|
1384
1502
|
var TURN_PROFILE_DEFAULTS = {
|
|
1385
1503
|
balanced: {
|
|
1386
1504
|
qualityProfile: "general",
|
|
1505
|
+
semanticVetoMaxMs: 0,
|
|
1506
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
1387
1507
|
silenceMs: 1400,
|
|
1388
1508
|
speechThreshold: 0.012,
|
|
1389
1509
|
transcriptStabilityMs: 1000
|
|
1390
1510
|
},
|
|
1391
1511
|
fast: {
|
|
1392
1512
|
qualityProfile: "general",
|
|
1513
|
+
semanticVetoMaxMs: 0,
|
|
1514
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
1393
1515
|
silenceMs: 700,
|
|
1394
1516
|
speechThreshold: 0.015,
|
|
1395
1517
|
transcriptStabilityMs: 450
|
|
1396
1518
|
},
|
|
1397
1519
|
"long-form": {
|
|
1398
1520
|
qualityProfile: "general",
|
|
1521
|
+
semanticVetoMaxMs: 0,
|
|
1522
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
1399
1523
|
silenceMs: 2200,
|
|
1400
1524
|
speechThreshold: 0.01,
|
|
1401
1525
|
transcriptStabilityMs: 1500
|
|
@@ -1429,6 +1553,8 @@ var resolveTurnDetectionConfig = (config) => {
|
|
|
1429
1553
|
return {
|
|
1430
1554
|
profile,
|
|
1431
1555
|
qualityProfile,
|
|
1556
|
+
semanticVetoMaxMs: config?.semanticVetoMaxMs ?? preset.semanticVetoMaxMs,
|
|
1557
|
+
semanticVetoRecheckMs: config?.semanticVetoRecheckMs ?? preset.semanticVetoRecheckMs,
|
|
1432
1558
|
silenceMs: config?.silenceMs ?? quality.silenceMs ?? preset.silenceMs,
|
|
1433
1559
|
speechThreshold: config?.speechThreshold ?? quality.speechThreshold ?? preset.speechThreshold,
|
|
1434
1560
|
transcriptStabilityMs: config?.transcriptStabilityMs ?? quality.transcriptStabilityMs ?? preset.transcriptStabilityMs
|
package/dist/testing/index.js
CHANGED
|
@@ -86,6 +86,7 @@ var __require = import.meta.require;
|
|
|
86
86
|
// src/core/turnDetection.ts
|
|
87
87
|
var DEFAULT_SILENCE_MS = 700;
|
|
88
88
|
var DEFAULT_SPEECH_THRESHOLD = 0.015;
|
|
89
|
+
var DEFAULT_SEMANTIC_VETO_RECHECK_MS = 1200;
|
|
89
90
|
var toUint8Array = (audio) => {
|
|
90
91
|
if (audio instanceof ArrayBuffer) {
|
|
91
92
|
return new Uint8Array(audio);
|
|
@@ -3133,18 +3134,24 @@ var resolveAudioConditioningConfig = (config) => {
|
|
|
3133
3134
|
var TURN_PROFILE_DEFAULTS = {
|
|
3134
3135
|
balanced: {
|
|
3135
3136
|
qualityProfile: "general",
|
|
3137
|
+
semanticVetoMaxMs: 0,
|
|
3138
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
3136
3139
|
silenceMs: 1400,
|
|
3137
3140
|
speechThreshold: 0.012,
|
|
3138
3141
|
transcriptStabilityMs: 1000
|
|
3139
3142
|
},
|
|
3140
3143
|
fast: {
|
|
3141
3144
|
qualityProfile: "general",
|
|
3145
|
+
semanticVetoMaxMs: 0,
|
|
3146
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
3142
3147
|
silenceMs: 700,
|
|
3143
3148
|
speechThreshold: 0.015,
|
|
3144
3149
|
transcriptStabilityMs: 450
|
|
3145
3150
|
},
|
|
3146
3151
|
"long-form": {
|
|
3147
3152
|
qualityProfile: "general",
|
|
3153
|
+
semanticVetoMaxMs: 0,
|
|
3154
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
3148
3155
|
silenceMs: 2200,
|
|
3149
3156
|
speechThreshold: 0.01,
|
|
3150
3157
|
transcriptStabilityMs: 1500
|
|
@@ -3178,6 +3185,8 @@ var resolveTurnDetectionConfig = (config) => {
|
|
|
3178
3185
|
return {
|
|
3179
3186
|
profile,
|
|
3180
3187
|
qualityProfile,
|
|
3188
|
+
semanticVetoMaxMs: config?.semanticVetoMaxMs ?? preset.semanticVetoMaxMs,
|
|
3189
|
+
semanticVetoRecheckMs: config?.semanticVetoRecheckMs ?? preset.semanticVetoRecheckMs,
|
|
3181
3190
|
silenceMs: config?.silenceMs ?? quality.silenceMs ?? preset.silenceMs,
|
|
3182
3191
|
speechThreshold: config?.speechThreshold ?? quality.speechThreshold ?? preset.speechThreshold,
|
|
3183
3192
|
transcriptStabilityMs: config?.transcriptStabilityMs ?? quality.transcriptStabilityMs ?? preset.transcriptStabilityMs
|
|
@@ -5910,6 +5919,8 @@ var FALLBACK_CONFIDENCE_SELECTION_DELTA = 0.05;
|
|
|
5910
5919
|
var FALLBACK_WORD_COUNT_SELECTION_MARGIN_RATIO = 0.12;
|
|
5911
5920
|
var EXTENDED_VENDOR_COMMIT_SILENCE_THRESHOLD_MS = 200;
|
|
5912
5921
|
var MAX_VENDOR_COMMIT_GRACE_MS = 1200;
|
|
5922
|
+
var STT_RECONNECT_FLAP_WINDOW_MS = 4000;
|
|
5923
|
+
var MAX_STT_RECONNECTS_IN_FLAP_WINDOW = 3;
|
|
5913
5924
|
var DEFAULT_FORMAT = {
|
|
5914
5925
|
channels: 1,
|
|
5915
5926
|
container: "raw",
|
|
@@ -6105,8 +6116,11 @@ var createVoiceSession = (options) => {
|
|
|
6105
6116
|
const turnDetection = {
|
|
6106
6117
|
silenceMs: options.turnDetection.silenceMs ?? DEFAULT_SILENCE_MS,
|
|
6107
6118
|
speechThreshold: options.turnDetection.speechThreshold ?? DEFAULT_SPEECH_THRESHOLD,
|
|
6108
|
-
transcriptStabilityMs: options.turnDetection.transcriptStabilityMs ?? DEFAULT_TRANSCRIPT_STABILITY_MS
|
|
6119
|
+
transcriptStabilityMs: options.turnDetection.transcriptStabilityMs ?? DEFAULT_TRANSCRIPT_STABILITY_MS,
|
|
6120
|
+
semanticVetoMaxMs: options.turnDetection.semanticVetoMaxMs ?? 0,
|
|
6121
|
+
semanticVetoRecheckMs: options.turnDetection.semanticVetoRecheckMs ?? DEFAULT_SEMANTIC_VETO_RECHECK_MS
|
|
6109
6122
|
};
|
|
6123
|
+
let semanticVetoElapsedMs = 0;
|
|
6110
6124
|
const sttFallback = options.sttFallback ? {
|
|
6111
6125
|
adapter: options.sttFallback.adapter,
|
|
6112
6126
|
completionTimeoutMs: options.sttFallback.completionTimeoutMs ?? DEFAULT_FALLBACK_COMPLETION_TIMEOUT_MS,
|
|
@@ -6147,6 +6161,8 @@ var createVoiceSession = (options) => {
|
|
|
6147
6161
|
let operationQueue = Promise.resolve();
|
|
6148
6162
|
let adapterGenerationCounter = 0;
|
|
6149
6163
|
let activeAdapterGeneration = 0;
|
|
6164
|
+
let sttReconnectCount = 0;
|
|
6165
|
+
let lastSttReconnectAt = 0;
|
|
6150
6166
|
let activeTTSTurnId;
|
|
6151
6167
|
let assistantSpeechEndsAt = 0;
|
|
6152
6168
|
let lastAssistantAudioAt = 0;
|
|
@@ -6621,10 +6637,51 @@ var createVoiceSession = (options) => {
|
|
|
6621
6637
|
silenceTimer = setTimeout(() => {
|
|
6622
6638
|
silenceTimer = null;
|
|
6623
6639
|
pendingCommitReason = null;
|
|
6624
|
-
|
|
6640
|
+
runScheduledCommit(reason);
|
|
6625
6641
|
}, delayMs);
|
|
6626
6642
|
};
|
|
6627
6643
|
const scheduleSilenceCommit = (delayMs = turnDetection.silenceMs, reset = true) => scheduleTurnCommit(delayMs, "silence", reset);
|
|
6644
|
+
const shouldDeferSilenceCommit = async (reason) => {
|
|
6645
|
+
if (reason !== "silence" || turnDetection.semanticVetoMaxMs <= 0 || !options.semanticTurnDetector || semanticVetoElapsedMs >= turnDetection.semanticVetoMaxMs) {
|
|
6646
|
+
return false;
|
|
6647
|
+
}
|
|
6648
|
+
const session = await readSession();
|
|
6649
|
+
const { partialText, transcripts } = session.currentTurn;
|
|
6650
|
+
const userText = buildTurnText(transcripts, partialText, {
|
|
6651
|
+
partialEndedAtMs: session.currentTurn.partialEndedAt,
|
|
6652
|
+
partialStartedAtMs: session.currentTurn.partialStartedAt
|
|
6653
|
+
});
|
|
6654
|
+
if (!userText) {
|
|
6655
|
+
return false;
|
|
6656
|
+
}
|
|
6657
|
+
const silenceMs = session.currentTurn.silenceStartedAt !== undefined ? Date.now() - session.currentTurn.silenceStartedAt : turnDetection.silenceMs;
|
|
6658
|
+
let endOfTurn = true;
|
|
6659
|
+
try {
|
|
6660
|
+
const verdict = await Promise.resolve(options.semanticTurnDetector.evaluate({
|
|
6661
|
+
lastFinalTranscript: transcripts.at(-1),
|
|
6662
|
+
partialText,
|
|
6663
|
+
silenceMs,
|
|
6664
|
+
transcripts
|
|
6665
|
+
}));
|
|
6666
|
+
endOfTurn = verdict.endOfTurn;
|
|
6667
|
+
} catch {
|
|
6668
|
+
return false;
|
|
6669
|
+
}
|
|
6670
|
+
if (endOfTurn !== false) {
|
|
6671
|
+
return false;
|
|
6672
|
+
}
|
|
6673
|
+
const remaining = turnDetection.semanticVetoMaxMs - semanticVetoElapsedMs;
|
|
6674
|
+
const extendMs = Math.max(1, Math.min(turnDetection.semanticVetoRecheckMs, remaining));
|
|
6675
|
+
semanticVetoElapsedMs += extendMs;
|
|
6676
|
+
scheduleTurnCommit(extendMs, reason);
|
|
6677
|
+
return true;
|
|
6678
|
+
};
|
|
6679
|
+
const runScheduledCommit = async (reason) => {
|
|
6680
|
+
if (await shouldDeferSilenceCommit(reason)) {
|
|
6681
|
+
return;
|
|
6682
|
+
}
|
|
6683
|
+
await api.commitTurn(reason);
|
|
6684
|
+
};
|
|
6628
6685
|
const requestTurnCommit = async (reason) => {
|
|
6629
6686
|
const session = await readSession();
|
|
6630
6687
|
const text = buildTurnText(session.currentTurn.transcripts, session.currentTurn.partialText, {
|
|
@@ -6992,6 +7049,27 @@ var createVoiceSession = (options) => {
|
|
|
6992
7049
|
}
|
|
6993
7050
|
};
|
|
6994
7051
|
const handleClose = async (event) => {
|
|
7052
|
+
const session = await readSession();
|
|
7053
|
+
const callLive = session.status !== "completed" && session.status !== "failed";
|
|
7054
|
+
if (callLive && (options.stt || options.realtime)) {
|
|
7055
|
+
const now = Date.now();
|
|
7056
|
+
sttReconnectCount = now - lastSttReconnectAt < STT_RECONNECT_FLAP_WINDOW_MS ? sttReconnectCount + 1 : 1;
|
|
7057
|
+
lastSttReconnectAt = now;
|
|
7058
|
+
if (sttReconnectCount <= MAX_STT_RECONNECTS_IN_FLAP_WINDOW) {
|
|
7059
|
+
await appendTrace({
|
|
7060
|
+
payload: {
|
|
7061
|
+
action: "stt-reconnect",
|
|
7062
|
+
attempt: sttReconnectCount,
|
|
7063
|
+
reason: event.reason ?? "stt stream closed",
|
|
7064
|
+
recoverable: event.recoverable
|
|
7065
|
+
},
|
|
7066
|
+
session,
|
|
7067
|
+
type: "session.error"
|
|
7068
|
+
});
|
|
7069
|
+
await closeAdapter(event.reason ?? "stt stream closed; reconnecting");
|
|
7070
|
+
return;
|
|
7071
|
+
}
|
|
7072
|
+
}
|
|
6995
7073
|
if (event.recoverable === false) {
|
|
6996
7074
|
await failInternal(new Error(event.reason ?? "Speech-to-text session closed"));
|
|
6997
7075
|
return;
|
|
@@ -7316,6 +7394,7 @@ var createVoiceSession = (options) => {
|
|
|
7316
7394
|
});
|
|
7317
7395
|
};
|
|
7318
7396
|
const handleFinal = async (transcript) => {
|
|
7397
|
+
sttReconnectCount = 0;
|
|
7319
7398
|
const session = await writeSession((session2) => {
|
|
7320
7399
|
const alreadyPresent = session2.currentTurn.transcripts.some((existing) => existing.id === transcript.id);
|
|
7321
7400
|
if (!alreadyPresent) {
|
|
@@ -7336,6 +7415,7 @@ var createVoiceSession = (options) => {
|
|
|
7336
7415
|
session2.lastActivityAt = Date.now();
|
|
7337
7416
|
session2.status = "active";
|
|
7338
7417
|
});
|
|
7418
|
+
semanticVetoElapsedMs = 0;
|
|
7339
7419
|
if (silenceTimer && pendingCommitReason === "vendor") {
|
|
7340
7420
|
scheduleTurnCommit(getVendorCommitDelayMs(), "vendor");
|
|
7341
7421
|
}
|
|
@@ -8039,6 +8119,7 @@ var createVoiceSession = (options) => {
|
|
|
8039
8119
|
};
|
|
8040
8120
|
const commitTurnInternal = async (reason = "manual") => {
|
|
8041
8121
|
clearSilenceTimer();
|
|
8122
|
+
semanticVetoElapsedMs = 0;
|
|
8042
8123
|
backchannelDriver?.reset();
|
|
8043
8124
|
amdLastTurnCommitAt = Date.now();
|
|
8044
8125
|
const session = await readSession();
|
package/dist/vue/index.js
CHANGED
|
@@ -11660,22 +11660,146 @@ var resolveAudioConditioningConfig = (config) => {
|
|
|
11660
11660
|
};
|
|
11661
11661
|
};
|
|
11662
11662
|
|
|
11663
|
+
// src/core/turnDetection.ts
|
|
11664
|
+
var DEFAULT_SILENCE_MS = 700;
|
|
11665
|
+
var DEFAULT_SPEECH_THRESHOLD = 0.015;
|
|
11666
|
+
var DEFAULT_SEMANTIC_VETO_RECHECK_MS = 1200;
|
|
11667
|
+
var toUint8Array = (audio) => {
|
|
11668
|
+
if (audio instanceof ArrayBuffer) {
|
|
11669
|
+
return new Uint8Array(audio);
|
|
11670
|
+
}
|
|
11671
|
+
return new Uint8Array(audio.buffer, audio.byteOffset, audio.byteLength);
|
|
11672
|
+
};
|
|
11673
|
+
var measureAudioLevel = (audio) => {
|
|
11674
|
+
const bytes = toUint8Array(audio);
|
|
11675
|
+
if (bytes.byteLength < 2) {
|
|
11676
|
+
return 0;
|
|
11677
|
+
}
|
|
11678
|
+
const samples = new Int16Array(bytes.buffer, bytes.byteOffset, Math.floor(bytes.byteLength / 2));
|
|
11679
|
+
if (samples.length === 0) {
|
|
11680
|
+
return 0;
|
|
11681
|
+
}
|
|
11682
|
+
let sumSquares = 0;
|
|
11683
|
+
for (const sample of samples) {
|
|
11684
|
+
const normalized = sample / 32768;
|
|
11685
|
+
sumSquares += normalized * normalized;
|
|
11686
|
+
}
|
|
11687
|
+
return Math.sqrt(sumSquares / samples.length);
|
|
11688
|
+
};
|
|
11689
|
+
var normalizeText = (value) => value.trim().replace(/\s+/g, " ");
|
|
11690
|
+
var countWords = (value) => value.length > 0 ? value.split(" ").length : 0;
|
|
11691
|
+
var selectPreferredTranscriptText = (currentText, nextText) => {
|
|
11692
|
+
const current = normalizeText(currentText);
|
|
11693
|
+
const next = normalizeText(nextText);
|
|
11694
|
+
if (!current) {
|
|
11695
|
+
return next;
|
|
11696
|
+
}
|
|
11697
|
+
if (!next) {
|
|
11698
|
+
return current;
|
|
11699
|
+
}
|
|
11700
|
+
if (current === next || current.includes(next)) {
|
|
11701
|
+
return current;
|
|
11702
|
+
}
|
|
11703
|
+
if (next.includes(current)) {
|
|
11704
|
+
return next;
|
|
11705
|
+
}
|
|
11706
|
+
if (countWords(next) > countWords(current)) {
|
|
11707
|
+
return next;
|
|
11708
|
+
}
|
|
11709
|
+
if (countWords(next) === countWords(current) && next.length > current.length) {
|
|
11710
|
+
return next;
|
|
11711
|
+
}
|
|
11712
|
+
return current;
|
|
11713
|
+
};
|
|
11714
|
+
var mergeSequentialTranscriptText = (currentText, nextText) => {
|
|
11715
|
+
const current = normalizeText(currentText);
|
|
11716
|
+
const next = normalizeText(nextText);
|
|
11717
|
+
if (!current) {
|
|
11718
|
+
return next;
|
|
11719
|
+
}
|
|
11720
|
+
if (!next) {
|
|
11721
|
+
return current;
|
|
11722
|
+
}
|
|
11723
|
+
const currentWords = current.split(" ");
|
|
11724
|
+
const nextWords = next.split(" ");
|
|
11725
|
+
const maxOverlap = Math.min(currentWords.length, nextWords.length);
|
|
11726
|
+
for (let overlap = maxOverlap;overlap > 0; overlap -= 1) {
|
|
11727
|
+
const currentSuffix = currentWords.slice(-overlap).join(" ");
|
|
11728
|
+
const nextPrefix = nextWords.slice(0, overlap).join(" ");
|
|
11729
|
+
if (currentSuffix === nextPrefix) {
|
|
11730
|
+
return [...currentWords, ...nextWords.slice(overlap)].join(" ");
|
|
11731
|
+
}
|
|
11732
|
+
}
|
|
11733
|
+
return `${current} ${next}`.trim();
|
|
11734
|
+
};
|
|
11735
|
+
var countCommonPrefixWords = (currentText, nextText) => {
|
|
11736
|
+
const currentWords = normalizeText(currentText).split(" ").filter(Boolean);
|
|
11737
|
+
const nextWords = normalizeText(nextText).split(" ").filter(Boolean);
|
|
11738
|
+
const maxWords = Math.min(currentWords.length, nextWords.length);
|
|
11739
|
+
let count = 0;
|
|
11740
|
+
for (let index = 0;index < maxWords; index += 1) {
|
|
11741
|
+
if (currentWords[index] !== nextWords[index]) {
|
|
11742
|
+
break;
|
|
11743
|
+
}
|
|
11744
|
+
count += 1;
|
|
11745
|
+
}
|
|
11746
|
+
return count;
|
|
11747
|
+
};
|
|
11748
|
+
var mergeTranscriptTexts = (transcripts) => {
|
|
11749
|
+
const merged = [];
|
|
11750
|
+
for (const transcript of transcripts) {
|
|
11751
|
+
const nextText = normalizeText(transcript.text);
|
|
11752
|
+
if (!nextText) {
|
|
11753
|
+
continue;
|
|
11754
|
+
}
|
|
11755
|
+
const previous = merged.at(-1);
|
|
11756
|
+
if (!previous) {
|
|
11757
|
+
merged.push(nextText);
|
|
11758
|
+
continue;
|
|
11759
|
+
}
|
|
11760
|
+
if (nextText === previous || previous.includes(nextText)) {
|
|
11761
|
+
continue;
|
|
11762
|
+
}
|
|
11763
|
+
if (nextText.includes(previous)) {
|
|
11764
|
+
merged[merged.length - 1] = nextText;
|
|
11765
|
+
continue;
|
|
11766
|
+
}
|
|
11767
|
+
merged.push(nextText);
|
|
11768
|
+
}
|
|
11769
|
+
return merged.join(" ").trim();
|
|
11770
|
+
};
|
|
11771
|
+
var buildTurnText = (transcripts, partialText, options = {}) => {
|
|
11772
|
+
const finalText = mergeTranscriptTexts(transcripts);
|
|
11773
|
+
const nextPartial = normalizeText(partialText);
|
|
11774
|
+
const lastFinalEndedAtMs = [...transcripts].reverse().find((transcript) => typeof transcript.endedAtMs === "number")?.endedAtMs;
|
|
11775
|
+
if (finalText && nextPartial && typeof lastFinalEndedAtMs === "number" && typeof options.partialStartedAtMs === "number" && options.partialStartedAtMs - lastFinalEndedAtMs >= 250 && countCommonPrefixWords(finalText, nextPartial) === 0) {
|
|
11776
|
+
return mergeSequentialTranscriptText(finalText, nextPartial);
|
|
11777
|
+
}
|
|
11778
|
+
return selectPreferredTranscriptText(finalText, nextPartial);
|
|
11779
|
+
};
|
|
11780
|
+
|
|
11663
11781
|
// src/core/turnProfiles.ts
|
|
11664
11782
|
var TURN_PROFILE_DEFAULTS = {
|
|
11665
11783
|
balanced: {
|
|
11666
11784
|
qualityProfile: "general",
|
|
11785
|
+
semanticVetoMaxMs: 0,
|
|
11786
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
11667
11787
|
silenceMs: 1400,
|
|
11668
11788
|
speechThreshold: 0.012,
|
|
11669
11789
|
transcriptStabilityMs: 1000
|
|
11670
11790
|
},
|
|
11671
11791
|
fast: {
|
|
11672
11792
|
qualityProfile: "general",
|
|
11793
|
+
semanticVetoMaxMs: 0,
|
|
11794
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
11673
11795
|
silenceMs: 700,
|
|
11674
11796
|
speechThreshold: 0.015,
|
|
11675
11797
|
transcriptStabilityMs: 450
|
|
11676
11798
|
},
|
|
11677
11799
|
"long-form": {
|
|
11678
11800
|
qualityProfile: "general",
|
|
11801
|
+
semanticVetoMaxMs: 0,
|
|
11802
|
+
semanticVetoRecheckMs: DEFAULT_SEMANTIC_VETO_RECHECK_MS,
|
|
11679
11803
|
silenceMs: 2200,
|
|
11680
11804
|
speechThreshold: 0.01,
|
|
11681
11805
|
transcriptStabilityMs: 1500
|
|
@@ -11709,6 +11833,8 @@ var resolveTurnDetectionConfig = (config) => {
|
|
|
11709
11833
|
return {
|
|
11710
11834
|
profile,
|
|
11711
11835
|
qualityProfile,
|
|
11836
|
+
semanticVetoMaxMs: config?.semanticVetoMaxMs ?? preset.semanticVetoMaxMs,
|
|
11837
|
+
semanticVetoRecheckMs: config?.semanticVetoRecheckMs ?? preset.semanticVetoRecheckMs,
|
|
11712
11838
|
silenceMs: config?.silenceMs ?? quality.silenceMs ?? preset.silenceMs,
|
|
11713
11839
|
speechThreshold: config?.speechThreshold ?? quality.speechThreshold ?? preset.speechThreshold,
|
|
11714
11840
|
transcriptStabilityMs: config?.transcriptStabilityMs ?? quality.transcriptStabilityMs ?? preset.transcriptStabilityMs
|