fix: accumulate transcript chunks into single messages per turn
All checks were successful
Build & Push / build-and-push (push) Successful in 1m23s
All checks were successful
Build & Push / build-and-push (push) Successful in 1m23s
Streaming transcription words now merge into one bubble per agent/user turn instead of creating separate entries for each chunk. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -132,6 +132,7 @@ export default function VoiceAgentProvider({ locale, children }: VoiceAgentProvi
|
|||||||
const [completedBrief, setCompletedBrief] = useState<string | null>(null);
|
const [completedBrief, setCompletedBrief] = useState<string | null>(null);
|
||||||
const [completedFormData, setCompletedFormData] = useState<WizardFormData | null>(null);
|
const [completedFormData, setCompletedFormData] = useState<WizardFormData | null>(null);
|
||||||
|
|
||||||
|
const turnCompleteRef = useRef(true);
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const mediaStreamRef = useRef<MediaStream | null>(null);
|
const mediaStreamRef = useRef<MediaStream | null>(null);
|
||||||
const audioContextRef = useRef<AudioContext | null>(null);
|
const audioContextRef = useRef<AudioContext | null>(null);
|
||||||
@@ -141,7 +142,15 @@ export default function VoiceAgentProvider({ locale, children }: VoiceAgentProvi
|
|||||||
const animFrameRef = useRef<number>(0);
|
const animFrameRef = useRef<number>(0);
|
||||||
|
|
||||||
const addTranscript = useCallback((role: 'user' | 'agent', text: string) => {
|
const addTranscript = useCallback((role: 'user' | 'agent', text: string) => {
|
||||||
setTranscript((prev) => [...prev, { role, text, timestamp: Date.now() }]);
|
setTranscript((prev) => {
|
||||||
|
const last = prev[prev.length - 1];
|
||||||
|
// Append to last entry if same role and turn is still ongoing
|
||||||
|
if (last && last.role === role && !turnCompleteRef.current) {
|
||||||
|
return [...prev.slice(0, -1), { ...last, text: last.text + text }];
|
||||||
|
}
|
||||||
|
turnCompleteRef.current = false;
|
||||||
|
return [...prev, { role, text, timestamp: Date.now() }];
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const trackAmplitude = useCallback(() => {
|
const trackAmplitude = useCallback(() => {
|
||||||
@@ -369,6 +378,10 @@ export default function VoiceAgentProvider({ locale, children }: VoiceAgentProvi
|
|||||||
if (msg.serverContent.outputTranscription?.text) {
|
if (msg.serverContent.outputTranscription?.text) {
|
||||||
addTranscript('agent', msg.serverContent.outputTranscription.text);
|
addTranscript('agent', msg.serverContent.outputTranscription.text);
|
||||||
}
|
}
|
||||||
|
// Turn complete — next output starts a new transcript entry
|
||||||
|
if (msg.serverContent.turnComplete || msg.serverContent.generationComplete) {
|
||||||
|
turnCompleteRef.current = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool call
|
// Tool call
|
||||||
|
|||||||
Reference in New Issue
Block a user