hallowed-book-84502
09/01/2025, 7:55 AMfrom dotenv import load_dotenv
import os
from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions, ConversationItemAddedEvent
from livekit.plugins import openai, noise_cancellation
from datetime import datetime, timezone
from supabase import create_client, Client
import asyncio
load_dotenv()
# Supabase Client initialisieren
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
class Assistant(Agent):
def __init__(self) -> None:
super().__init__(
instructions="You are a helpful voice AI assistant. Always respond in English only, regardless of the language the user speaks in."
)
async def entrypoint(ctx: agents.JobContext):
session = AgentSession(
llm=openai.realtime.RealtimeModel(voice="coral")
)
await session.start(
room=ctx.room,
agent=Assistant(),
room_input_options=RoomInputOptions(
noise_cancellation=noise_cancellation.BVC(),
),
)
import asyncio
async def insert_log_async(data):
await asyncio.to_thread(lambda: supabase.table("test-speech-interaction").insert(data).execute())
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
ts = datetime.now(timezone.utc)
data = {
"room": ctx.room.name,
"role": event.item.role,
"message": event.item.text_content,
"interrupted": event.item.interrupted,
"ts": ts.isoformat()
}
asyncio.create_task(insert_log_async(data))
await session.generate_reply(
instructions="Greet the user and offer your assistance in English."
)
if __name__ == "__main__":
agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))tall-airline-16756
09/01/2025, 7:55 AMtall-airline-16756
09/01/2025, 7:56 AMCreateRoom method. This way, you get to customize the room settings right from the start.
• Implicit creation upon participant join: If you prefer, rooms get created automatically the moment the first participant joins. The room name is included in the connection token, which also contains the participant’s identity and permissions.
In your code, you’re probably passing the room name through the ctx.room object. This would have been set earlier on—maybe during token generation or when making API calls.
Sources: Rooms, participants, and tracks | LiveKit Docs | packages/livekit-server-sdk/src/RoomServiceClient.ts | livekit-api/livekit/api/room_service.py | Connecting to LiveKit | LiveKit Docshallowed-book-84502
09/01/2025, 8:12 AMtall-airline-16756
09/01/2025, 8:13 AMJobContext class doesn’t have a direct participant attribute itself. But no worries—you can still work with participants using methods like wait_for_participant() and add_participant_entrypoint(). If you’re looking to get participant details, try using these methods or check out ctx.job.participant if that fits your situation. Hope this helps!
Sources: lib/livekit/proto/livekit_agent_pb.rb | livekit-agents/livekit/agents/job.pyhallowed-book-84502
09/01/2025, 8:53 AMtall-airline-16756
09/01/2025, 8:54 AMDUPLICATE_IDENTITY reason. It’s kind of like making sure there’s only one “you” in the room at a time.
• If you disconnect and then reconnect, that’s totally fine! Just keep in mind that each connection starts fresh, so any tracks or subscriptions you had before won’t automatically come back—you’ll need to set those up again.
This way, LiveKit keeps things running smoothly by making sure only one instance of each participant identity is in the room at once.
Sources: Connecting to LiveKit | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | examples/multiple_connections.py | test/singlenode_test.go