provide example of LiveKit agent event handling. E...
# ask-ai
t
provide example of LiveKit agent event handling. Event handlers should be members of agent class.
t
Thanks for reaching out, sergey. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here is an example of LiveKit agent event handling where event handlers are implemented as member methods of an agent class: Event handlers are registered using the
.on()
method to connect events to their respective handler methods. For example:
Copy code
self._human_input.on("start_of_speech", _on_start_of_speech)
self._human_input.on("vad_inference_done", _on_vad_inference_done)
self._human_input.on("end_of_speech", _on_end_of_speech)
self._human_input.on("interim_transcript", _on_interim_transcript)
self._human_input.on("final_transcript", _on_final_transcript)
Another example for agent playout:
Copy code
agent_playout.on("playout_started", _on_playout_started)
agent_playout.on("playout_stopped", _on_playout_stopped)
For real-time sessions, event handlers can be registered like this:
Copy code
self._rt_session.on("generation_created", self._on_generation_created)
self._rt_session.on("input_speech_started", self._on_input_speech_started)
self._rt_session.on("input_speech_stopped", self._on_input_speech_stopped)
self._rt_session.on("input_audio_transcription_completed", self._on_input_audio_transcription_completed)
self._rt_session.on("metrics_collected", self._on_metrics_collected)
self._rt_session.on("error", self._on_error)
The handler methods, such as
_on_input_speech_stopped
,
_on_input_audio_transcription_completed
, and
_on_generation_created
, are defined as member methods of the class. Let me know if you need further clarification or additional examples! Sources: GitHub::livekit::agents | livekit-agents/livekit/agents/voice/agent_activit...
t
provide example for agent_state_changed
t
Hey there! Let me walk you through handling the agent_state_changed event in LiveKit – it's actually pretty straightforward.
Copy code
agent_state_events: list[AgentStateChangedEvent] = []
session.on("agent_state_changed", agent_state_events.append)
Here's what's happening in this little code snippet: • We're creating an empty list to keep track of those state change events. • Using
session.on("agent_state_changed", callback)
to catch whenever the state shifts. • Each time the event is triggered, we're adding it to our list – super easy! Wanna see how you can check out those state transitions? Check this out:
Copy code
assert agent_state_events[0].old_state == "initializing"
assert agent_state_events[0].new_state == "listening"
assert agent_state_events[1].new_state == "thinking"
Pro tip: These events even come with timestamps, so you can really dig into the timing if you want. Need me to break anything down further or show you another example? I'm totally here to help! Sources: GitHub::livekit::agents | tests/test_agent_session.py