nutritious-policeman-86688
08/04/2025, 1:34 AMtall-airline-16756
08/04/2025, 1:34 AMtall-airline-16756
08/04/2025, 1:34 AMdelete_room API. Here's an example implementation:
from livekit import api
from livekit.agents import get_job_context
from livekit.agents.llm import function_tool
import asyncio
async def hangup_call():
ctx = get_job_context()
if ctx is None:
# Not running in a job context
return
# Let the agent finish speaking if there's ongoing speech
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
# Delete the room to end the call
await ctx.api.room.delete_room(
api.DeleteRoomRequest(
room=ctx.room.name,
)
)
class MyAgent(Agent):
@function_tool
async def end_call(self, ctx: RunContext):
"""Called when the user wants to end the call"""
# Let the agent finish speaking
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
await hangup_call()
Key Points:
• The hangup_call function handles the deletion of the room to terminate the call.
• The end_call method is decorated with @function_tool, making it accessible as a tool for the LLM.
• It ensures any ongoing speech finishes before hanging up the call.
This approach ensures a clean termination of the call for all participants.nutritious-policeman-86688
08/04/2025, 1:35 AMtall-airline-16756
08/04/2025, 1:36 AMfrom livekit import api
from livekit.agents.llm import function_tool
async def hangup_call(ctx):
if ctx is None:
# Not running in a job context
return
# Let the agent finish speaking if there's ongoing speech
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
# Delete the room to end the call
await ctx.api.room.delete_room(
api.DeleteRoomRequest(
room=ctx.room.name,
)
)
class MyAgent(Agent):
@function_tool
async def end_call(self, ctx):
"""Called when the user wants to end the call"""
# Let the agent finish speaking
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
await hangup_call(ctx)
By passing ctx explicitly like this, your function becomes more modular and way easier to test or reuse in different situations. Hope this helps!nutritious-policeman-86688
08/04/2025, 1:50 AMtall-airline-16756
08/04/2025, 1:51 AM