Skip to content

Commit 3e54bbc

Browse files
Gabry848claude
andcommitted
fix(voice): upsert trascrizioni per item_id invece di append
Il server ora invia aggiornamenti incrementali del transcript con item_id. Il client sostituisce il transcript esistente con lo stesso item_id invece di aggiungere duplicati parziali, mostrando sempre il testo completo. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1a864f5 commit 3e54bbc

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/hooks/useVoiceChat.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface ServerStatus {
3131
export interface VoiceTranscript {
3232
role: 'user' | 'assistant';
3333
content: string;
34+
item_id?: string;
3435
}
3536

3637
/**
@@ -123,7 +124,7 @@ export function useVoiceChat() {
123124
onConnectionClose: (...args) => websocketCallbacksRef.current.onConnectionClose?.(...args),
124125
onStatus: (...args) => websocketCallbacksRef.current.onStatus?.(...args),
125126
onAudioChunk: (...args) => websocketCallbacksRef.current.onAudioChunk?.(...args),
126-
onTranscript: (...args) => websocketCallbacksRef.current.onTranscript?.(...args),
127+
onTranscript: (role, content, itemId) => websocketCallbacksRef.current.onTranscript?.(role, content, itemId),
127128
onToolCall: (...args) => websocketCallbacksRef.current.onToolCall?.(...args),
128129
onToolOutput: (...args) => websocketCallbacksRef.current.onToolOutput?.(...args),
129130
onDone: (...args) => websocketCallbacksRef.current.onDone?.(...args),
@@ -443,9 +444,20 @@ export function useVoiceChat() {
443444
}
444445
},
445446

446-
onTranscript: (role: 'user' | 'assistant', content: string) => {
447+
onTranscript: (role: 'user' | 'assistant', content: string, itemId?: string) => {
447448
if (!isMountedRef.current) return;
448-
setTranscripts(prev => [...prev, { role, content }]);
449+
setTranscripts(prev => {
450+
if (itemId) {
451+
const idx = prev.findLastIndex(t => t.item_id === itemId);
452+
if (idx !== -1) {
453+
// Aggiorna il transcript esistente con il testo più completo
454+
const updated = [...prev];
455+
updated[idx] = { role, content, item_id: itemId };
456+
return updated;
457+
}
458+
}
459+
return [...prev, { role, content, item_id: itemId }];
460+
});
449461
},
450462

451463
onToolCall: (toolName: string, args: string) => {

src/services/voiceBotService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface VoiceAudioResponse {
6060
export interface VoiceTranscriptResponse {
6161
type: 'transcript';
6262
role: 'user' | 'assistant';
63+
item_id?: string;
6364
content: string;
6465
}
6566

@@ -111,7 +112,7 @@ export enum WebSocketAuthState {
111112
export interface VoiceChatCallbacks {
112113
onStatus?: (phase: VoiceServerPhase, message: string) => void | Promise<void>;
113114
onAudioChunk?: (audioData: string, chunkIndex: number) => void;
114-
onTranscript?: (role: 'user' | 'assistant', content: string) => void;
115+
onTranscript?: (role: 'user' | 'assistant', content: string, itemId?: string) => void;
115116
onToolCall?: (toolName: string, args: string) => void;
116117
onToolOutput?: (toolName: string, output: string) => void;
117118
onError?: (error: string) => void;
@@ -459,7 +460,7 @@ export class VoiceBotWebSocket {
459460
* Gestisce risposta di trascrizione
460461
*/
461462
private handleTranscriptResponse(response: VoiceTranscriptResponse): void {
462-
this.callbacks.onTranscript?.(response.role, response.content);
463+
this.callbacks.onTranscript?.(response.role, response.content, response.item_id);
463464
}
464465

465466
/**

0 commit comments

Comments
 (0)