更新本地测试页的合并逻辑:前缀继续增长时接上新内容;快照滚动时按文本重叠更新尾部;定稿到达后仍用定稿替换临时文本

main
Bifang 2026-10-10 13:39:19 +08:00
parent 86ebb44be4
commit b6bf60ca1c
1 changed files with 54 additions and 6 deletions

View File

@ -4160,6 +4160,59 @@ ${transcript}
return groups;
}
function getLivePartialMatchView(text) {
const source = String(text || "");
let normalized = "";
const sourceOffsets = [];
// Ignore punctuation while matching snapshots because partial ASR often revises it.
for (let offset = 0; offset < source.length;) {
const character = String.fromCodePoint(source.codePointAt(offset));
if (!/[\s\p{P}\p{S}]/u.test(character)) {
normalized += character;
sourceOffsets.push(offset);
}
offset += character.length;
}
return { normalized, sourceOffsets };
}
function mergeLivePartialSnapshot(currentText, nextText) {
const current = String(currentText || "").trim();
const next = String(nextText || "").trim();
if (!current) return next;
if (!next) return current;
const currentView = getLivePartialMatchView(current);
const nextView = getLivePartialMatchView(next);
const currentNormalized = currentView.normalized;
const nextNormalized = nextView.normalized;
if (!currentNormalized || !nextNormalized) return next.startsWith(current) ? next : current;
// FunASR's online model emits new chunks that its client appends. Qwen sends
// revisable rolling snapshots, so accept growth directly and merge their tail overlap.
if (nextNormalized.startsWith(currentNormalized)) return next;
if (currentNormalized.startsWith(nextNormalized)) return current;
const maxOverlap = Math.min(currentNormalized.length, nextNormalized.length, 128);
for (let overlapLength = maxOverlap; overlapLength >= 2; overlapLength -= 1) {
const overlap = currentNormalized.slice(-overlapLength);
const nextMatchIndex = nextNormalized.lastIndexOf(overlap);
if (nextMatchIndex < 0) continue;
const currentMatchIndex = currentNormalized.length - overlapLength;
const currentTextOffset = currentView.sourceOffsets[currentMatchIndex];
const nextTextOffset = nextView.sourceOffsets[nextMatchIndex];
return `${current.slice(0, currentTextOffset)}${next.slice(nextTextOffset)}`;
}
// Without a textual overlap there is no safe way to distinguish new text from a rewrite.
// Keep the short startup hypothesis replaceable until the model has produced context.
if (currentNormalized.length <= 16) return next;
return current;
}
function renderLiveTranscript() {
const items = groupLiveTranscriptItems(state.liveTranscript);
if (!items.length) {
@ -4283,12 +4336,7 @@ ${transcript}
if (existingPartialIndex >= 0) {
const existingPartial = state.liveTranscript[existingPartialIndex];
existingPartial.label = label;
// The server sends a full, revisable partial snapshot. Only expose its
// newly appended suffix while streaming; keep the visible draft intact
// on rollback/revision, then let the final sentence replace it below.
if (safeText.startsWith(existingPartial.text)) {
existingPartial.text = safeText;
}
existingPartial.text = mergeLivePartialSnapshot(existingPartial.text, safeText);
existingPartial.speaker = speaker;
existingPartial.speakerPending = speakerPending;
existingPartial.segmentIndex = segmentIndex;