Files
pipecat/examples/word-wrangler-gemini-live/client/src/hooks/useWordDetection.ts
2025-04-24 14:31:48 -04:00

63 lines
1.5 KiB
TypeScript

import { useRef } from 'react';
import { useRTVIClientEvent } from '@pipecat-ai/client-react';
import { RTVIEvent } from '@pipecat-ai/client-js';
import { detectWordGuess } from '@/utils/wordDetection';
import { GAME_STATES, GameState } from '@/constants/gameConstants';
interface UseWordDetectionProps {
gameState: GameState;
currentWord: string;
onCorrectGuess: () => void;
onIncorrectGuess: () => void;
}
export function useWordDetection({
gameState,
currentWord,
onCorrectGuess,
onIncorrectGuess,
}: UseWordDetectionProps) {
const lastProcessedMessageRef = useRef('');
// Reset the last processed message
const resetLastProcessedMessage = () => {
lastProcessedMessageRef.current = '';
};
// Listen for bot transcripts to detect correct answers
useRTVIClientEvent(RTVIEvent.BotTranscript, (data) => {
if (gameState !== GAME_STATES.ACTIVE) {
return;
}
if (!currentWord) {
return;
}
if (!data.text) {
return;
}
// Skip if this is a repeat of the same transcript
if (data.text === lastProcessedMessageRef.current) {
return;
}
lastProcessedMessageRef.current = data.text;
// Use the utility function to detect word guesses
const result = detectWordGuess(data.text, currentWord);
if (result.isCorrect) {
onCorrectGuess();
} else if (result.isExplicitGuess) {
onIncorrectGuess();
} else {
}
});
return {
resetLastProcessedMessage,
};
}