/** * Agent Transcription Display * * Shows agent speech transcriptions in a simple overlay. * Displays final transcriptions after the agent finishes speaking. */ import { useEffect, useState } from 'react'; import { useRoomContext } from '@livekit/components-react'; // import styles from './TranscriptionDisplay.module.css'; // Commented out while UI is hidden interface TranscriptionMessage { id: string; text: string; timestamp: number; } export function TranscriptionDisplay() { const room = useRoomContext(); const [messages, setMessages] = useState([]); useEffect(() => { if (!room) return; console.log('[TranscriptionDisplay] Setting up transcription handler'); let unregister: (() => void) | undefined; try { // Register handler for transcription text streams unregister = room.registerTextStreamHandler( 'lk.transcription', async (reader, participantInfo) => { try { const text = await reader.readAll(); const attributes = reader.info.attributes || {}; const isFinal = attributes['lk.transcription_final'] === 'true'; const transcribedTrackId = attributes['lk.transcribed_track_id']; // Only process agent transcriptions (not customer) and only final ones if (!transcribedTrackId || !isFinal) return; console.log('[TranscriptionDisplay] Final transcription:', text); // Add transcription to display const newMessage: TranscriptionMessage = { id: crypto.randomUUID(), text, timestamp: Date.now(), }; setMessages((prev) => [...prev.slice(-9), newMessage]); // Keep last 10 messages } catch (error) { console.error('[TranscriptionDisplay] Error processing transcription:', error); } } ); } catch (error) { // Ignore "already registered" errors from React StrictMode double-mounting if (error instanceof Error && error.message.includes('already been set')) { console.log('[TranscriptionDisplay] Handler already registered (React StrictMode), skipping'); return; // Exit early, no cleanup needed } console.error('[TranscriptionDisplay] Error registering handler:', error); return; } return () => { console.log('[TranscriptionDisplay] Cleaning up transcription handler'); if (unregister) { try { unregister(); } catch (e) { // Ignore cleanup errors } } }; }, [room]); // UI display commented out - transcriptions still processed and logged // Uncomment to re-enable visual display /* if (messages.length === 0) return null; return (
🤖 AI Agent
{messages.map((message) => (
{message.text}
))}
); */ // Return null to hide UI while keeping handler active return null; }