Discussions

Ask a Question
Back to all

Avatar manual interruption

Hi,

I would like to know how to configure LiveKit/LiveAvatar SDK for manual interruptions from user.

The case is as follows: the kid is talking to AI, and the kid does constant barge-ins (“WAIT! ALSO! AND…”), which interrupt Avatar so it does not complete any phrase.

I want to listen to the kid's speech, queue it, wait for the avatar to complete talking, and push that queue to the avatar.

I try to leverage these events:

session.on(AgentEventsEnum.AVATAR_SPEAK_STARTED...)
session.on(AgentEventsEnum.AVATAR_SPEAK_ENDED...)
session.on(AgentEventsEnum.USER_TRANSCRIPTION...)

session.on(AgentEventsEnum.AVATAR_SPEAK_STARTED, () => {
  console.log('🎅 AVATAR started speaking');

  setIsAvatarTalking(true);
});

session.on(AgentEventsEnum.AVATAR_SPEAK_ENDED, () => {
  console.log('🎅 AVATAR stopped speaking');
  
	const queuedMessages = [...interruptionQueueRef.current];
  const combinedMessage = queuedMessages.join(' and ');

  setChatHistory(prev => [...prev, { role: 'user', text: combinedMessage }]);
  setIsAvatarTalking(false);
});

session.on(AgentEventsEnum.USER_TRANSCRIPTION, onUserTrans); // below

And do this:

const onUserTrans = (e) => {
  console.log('👤 USER SAID:', e.text);

  // Add to overall queue
  userMessageQueueRef.current.push(e.text);
  console.log('📝 USER MESSAGE QUEUE:', userMessageQueueRef.current);

  // Check if avatar is talking
  if (isAvatarTalking) {
    // Avatar is talking - queue the message instead of sending immediately
    console.log('⏸️  INTERRUPTION DETECTED - Queuing message');
    interruptionQueueRef.current.push(e.text);
  } else {
    // Avatar not talking - normal flow
    setChatHistory(prev => [...prev, { role: 'user', text: e.text }]);
  }
};

However, USER_TRANSCRIPTION does not happen if I do not interrupt the avatar.

Any suggestions?