async def entrypoint(_ctx_: JobContext):
"""Main entrypoint for the LiveKit agent"""
_try_:
# Connect to the job context
await ctx.connect()
logger.info("Connected to LiveKit job context")
# Check if room is connected before proceeding
if not ctx.room.isconnected():
logger.warning("Room is not connected, waiting for connection...")
# Wait a bit for connection to establish
await asyncio.sleep(1)
if not ctx.room.isconnected():
logger.error("Room connection failed")
return
logger.info(f"Room connected: {ctx.room.name}")
# Initialize restaurant agent
await restaurant_agent.initialize()
# Wait for participant to join with timeout
_try_:
await asyncio.wait_for(ctx.wait_for_participant(),
timeout=60.0)
except asyncio.TimeoutError:
logger.warning("No participant joined within 60 seconds")
return
# Check if we have participants
if not ctx.room.remote_participants:
logger.warning("No remote participants found")
return
# Get the first participant
participant = ctx.room.remote_participants[0]
logger.info(f"Participant joined: {participant.identity}")
# Extract phone number from participant identity if available
phone_number = None
if hasattr(participant, 'identity') and participant.identity:
# Try to extract phone number from identity (format: "phone:+1234567890")
if participant.identity.startswith('phone:'):
phone_number = participant.identity[6:]
# Remove "phone:" prefix
# Check for spam
if restaurant_agent.is_spam_call(
phone=phone_number, _room_name_=ctx.room.name):
await restaurant_agent.handle_spam_call(
phone=phone_number, _room_name_=ctx.room.name)
return
# Record this call attempt
restaurant_agent.record_call_attempt(
phone=phone_number, _room_name_=ctx.room.name)
# Reset activity timer
restaurant_agent.last_customer_activity = time.time()
restaurant_agent.conversation_start_time = time.time()
# Create agent session with specific models
session = AgentSession(
llm=openai.LLM(
model="gpt-4o"),
tts=inworld.TTS(
model="Ashley"),
stt=deepgram.STT(
model="nova-3"),
)
# Create the assistant agent with function tools
assistant = Agent(
_entrypoint_fnc_=restaurant_agent.get_system_prompt(),
tools=[
restaurant_agent.create_reservation,
restaurant_agent.cancel_reservation,
restaurant_agent.update_reservation,
restaurant_agent.create_order,
restaurant_agent.get_customer_info,
restaurant_agent.check_availability,
restaurant_agent.end_call,
restaurant_agent.thank_you_and_end,
]
)
# Start auto-hangup monitoring task
async def monitor_activity():
while True:
_try_:
await asyncio.sleep(5)
# Check every 5 seconds
# Check if room is still connected
if not ctx.room.isconnected():
logger.info("Room disconnected, stopping monitoring")
break
# Check if we should auto hangup
if await restaurant_agent.check_and_hangup_if_needed():
break
# Check if conversation has been going too long
conversation_duration = time.time() - restaurant_agent.conversation_start_time
if conversation_duration > settings.max_conversation_duration:
logger.info("Conversation timeout reached, hanging up")
await restaurant_agent.end_call_with_thanks()
break
except asyncio.CancelledError:
break
except Exception
as e:
logger.error(f"Error in activity monitor: {e}")
# Start the assistant
await session.start(
agent=assistant,
room=ctx.room)
# Start monitoring task
monitor_task = asyncio.create_task(monitor_activity())
_try_:
# Keep running until participant leaves or auto-hangup
await ctx.wait_for_disconnect()
_finally_:
# Clean up monitoring task
monitor_task.cancel()
_try_:
await monitor_task
except asyncio.CancelledError:
pass
# Properly shutdown the job context
await ctx.shutdown()
logger.info("Job context shutdown completed")
except Exception
as e:
logger.error(f"Error in entrypoint: {e}")
# Ensure shutdown even on error
_try_:
await ctx.shutdown()
except Exception
as shutdown_error:
logger.error(f"Error during shutdown: {shutdown_error}")
raise
ERROR
main Error during shutdown: object NoneType can't be used in 'await' expression