每次收到同一分段的新 partial 快照,都会直接更新这段可修订草稿;该分段定稿后,再用最终文本替换草稿。

main
Bifang 2026-10-10 14:27:55 +08:00
parent b6bf60ca1c
commit ca1ec42ea8
1 changed files with 4 additions and 54 deletions

View File

@ -4160,59 +4160,6 @@ ${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) {
@ -4336,7 +4283,10 @@ ${transcript}
if (existingPartialIndex >= 0) {
const existingPartial = state.liveTranscript[existingPartialIndex];
existingPartial.label = label;
existingPartial.text = mergeLivePartialSnapshot(existingPartial.text, safeText);
// Backend partial events are complete, revisable snapshots for this segment.
// Replace the draft wholesale so a changed hypothesis cannot leave stale text
// frozen in the bubble; the matching final event replaces this same segment.
existingPartial.text = safeText;
existingPartial.speaker = speaker;
existingPartial.speakerPending = speakerPending;
existingPartial.segmentIndex = segmentIndex;