fix(cli): insert voice transcription at cursor position instead of ap… (#26287)

Co-authored-by: Zheyuan <zlin252@emory.edu>
This commit is contained in:
Zheyuan Lin
2026-05-01 12:41:17 -04:00
committed by GitHub
parent d9f273e440
commit 7213822e84
2 changed files with 59 additions and 28 deletions
+19 -20
View File
@@ -51,6 +51,7 @@ export function useVoiceMode({
const recorderRef = useRef<AudioRecorder | null>(null);
const transcriptionServiceRef = useRef<TranscriptionProvider | null>(null);
const turnBaselineRef = useRef<string | null>(null);
const turnBaselineCursorOffsetRef = useRef<number>(0);
const pttStateRef = useRef<'idle' | 'possible-hold' | 'recording'>('idle');
const pttTimerRef = useRef<NodeJS.Timeout | null>(null);
@@ -112,6 +113,7 @@ export function useVoiceMode({
recordingInProgressRef.current = true;
turnBaselineRef.current = bufferRef.current.text;
turnBaselineCursorOffsetRef.current = bufferRef.current.getOffset();
setIsConnecting(true);
setIsRecording(true);
@@ -193,29 +195,23 @@ export function useVoiceMode({
}
if (text) {
const currentBufferText = bufferRef.current.text;
const previousTranscription = liveTranscriptionRef.current;
const baseline = turnBaselineRef.current ?? '';
const insertOffset = turnBaselineCursorOffsetRef.current;
const textBefore = baseline.slice(0, insertOffset);
const textAfter = baseline.slice(insertOffset);
let newTotalText = currentBufferText;
const prefix =
textBefore.length > 0 && !/\s$/.test(textBefore)
? textBefore + ' '
: textBefore;
if (
previousTranscription &&
currentBufferText.endsWith(previousTranscription)
) {
newTotalText = currentBufferText.slice(
0,
-previousTranscription.length,
);
} else if (
currentBufferText &&
!currentBufferText.endsWith(' ') &&
!currentBufferText.endsWith('\n')
) {
newTotalText += ' ';
}
const suffix =
text.length > 0 && textAfter.length > 0 && !/^\s/.test(textAfter)
? ' '
: '';
newTotalText += text;
bufferRef.current.setText(newTotalText, 'end');
const newTotalText = prefix + text + suffix + textAfter;
bufferRef.current.setText(newTotalText, prefix.length + text.length);
}
liveTranscriptionRef.current = text;
});
@@ -226,6 +222,9 @@ export function useVoiceMode({
stopRequestedRef.current
)
return;
// Advance the baseline so subsequent turns append after this turn's text
turnBaselineRef.current = bufferRef.current.text;
turnBaselineCursorOffsetRef.current = bufferRef.current.getOffset();
liveTranscriptionRef.current = '';
});