elif transfer_type in ["warm_message", "warm_summary"]:
target_phone_number = transfer_to_phone.replace("tel:", "")
# Warm transfer - add agent to current room
sip_trunk_id =
await get_outbound_trunk_id_from_number(
livekit_api, target_phone_number
)
# Create SIP participant request to add new agent to room
supervisor_identity = f"supervisor-{participant_identity}"
create_request = api.CreateSIPParticipantRequest(
_sip_trunk_id_=sip_trunk_id,
_sip_call_to_=target_phone_number,
_room_name_=room_name,
_participant_identity_=supervisor_identity,
_krisp_enabled_=True,
_wait_until_answered_=True,
)
logger.debug(f"Warm transfer request: {create_request}")
# Add new agent to the room
await livekit_api.sip.create_sip_participant(create_request)
logger.info(
f"Successfully added new agent to room {room_name} ({transfer_type})"
)
# Wait for the supervisor to actually connect to the room
supervisor_connected = False
max_wait_time = 30
# Maximum 30 seconds to wait
wait_interval = 1
# Check every second
for _
in range(max_wait_time):
# Check if supervisor is in the room
room_participants =
await livekit_api.room.list_participants(
api.ListParticipantsRequest(
room=room_name)
)
supervisor_in_room = any(
p.identity == supervisor_identity
for p
in room_participants.participants
)
if supervisor_in_room:
supervisor_connected = True
logger.info(f"Supervisor {supervisor_identity} connected to room")
break
await asyncio.sleep(wait_interval)
if not supervisor_connected:
logger.warning(
f"Supervisor did not connect within {max_wait_time} seconds"
)
return "Supervisor could not be reached. Please try again."
# Get supervisor's audio track SID
participant_info =
await livekit_api.room.get_participant(
api.RoomParticipantIdentity(
room=room_name,
identity=supervisor_identity
)
)
audio_track_sid = None
for track
in participant_info.tracks:
if track.type == api.TrackType.AUDIO:
audio_track_sid = track.sid
break
if not audio_track_sid:
logger.warning(
f"No audio track found for supervisor {supervisor_identity}"
)
return "Failed to mute supervisor: No audio track found."
# Mute the supervisor's audio track
await livekit_api.room.mute_published_track(
api.MuteRoomTrackRequest(
room=room_name,
identity=supervisor_identity,
_track_sid_=audio_track_sid,
muted=True,
)
)
logger.info(f"Supervisor {supervisor_identity} audio track muted")
# Wait a brief moment for supervisor to settle
await asyncio.sleep(2)
# Deliver the warm transfer message
if warm_transfer_message:
await session.say(warm_transfer_message, _add_to_chat_ctx_=True)
await asyncio.sleep(3)
# Wait for message to complete
# Unmute the supervisor's audio track
await livekit_api.room.mute_published_track(
api.MuteRoomTrackRequest(
room=room_name,
identity=supervisor_identity,
_track_sid_=audio_track_sid,
muted=False,
)
)
logger.info(f"Supervisor {supervisor_identity} audio track unmuted")
# Brief pause before agent disconnects
await asyncio.sleep(1)
# Disconnect the current agent
await session.aclose()
logger.info("Current agent disconnected - supervisor taking over")
logger.info(
f"Successfully transferred participant {participant_identity} to {transfer_to_phone} (warm transfer)"
)
return warm_transfer_message
except Exception
as e:
error_msg = f"Failed to transfer call: {e}"
logger.error(error_msg)
return error_msg
How we can play a ringtone while transferring ?