How can we pass some original number or context wh...
# ask-ai
r
How can we pass some original number or context while transferring a call from one livekit agent to new livekit agent
t
Thanks for reaching out, pritesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you’re transferring a call between LiveKit agents and want to keep the original number or context intact, here are some handy ways to do it: • Transferring the Call: You can use the
transfer_sip_participant
method to send the caller to another number. The great part is that the original caller ID and call context stay with the call through the SIP protocol—just make sure your SIP trunk supports call transfers. • Preserving Conversation Context: The
chat_ctx
parameter is your friend here. It lets you pass along conversation history or any metadata when switching agents, so the new agent can pick up right where things left off. • Passing Metadata: You can also include metadata, like the original number, in JSON format when dispatching to an agent. This way, the receiving agent has all the info they need. If you want, I’d be happy to share some detailed examples or help you walk through setting these up. Just let me know! Sources: Transferring calls | LiveKit Docs | examples/warm-transfer/README.md | Workflows | LiveKit Docs | Agent dispatch | LiveKit Docs | agent.py
r
async def call_transfer(_context_: RunContext[ToolState]) -> Dict[str, Any]: """ Transfers the call to the configured phone number using LiveKit's SIP transfer API. Args: context: The RunContext containing the tool state Returns: Dict containing the result of the operation """ # Get function metadata from shared context function_data = get_fn_data_from_ctx(context) if not function_data: return "Function configuration not found." user_data = context.userdata participant = user_data.participant ctx = user_data.call_ctx call_sid = participant.attributes.get("sip.twilio.callSid") config = function_data.get("config", {}) config = json.loads(config) transfer_to_phone = config.get("phone_number") transfer_type = config.get("transfer_type") transfer_message = config.get("transfer_prompt") warm_transfer_message = config.get("warm_transfer_prompt") logger.info(f"Initiating {transfer_type} transfer to {transfer_to_phone}") # Get participant identity and room name participant_identity = participant.identity room_name = ctx.room.name if not transfer_to_phone: logger.error("No transfer_to_phone configured for call_transfer.") return "Transfer number not configured." _try_: session = context.session # Format the phone number (ensure it has + prefix and tel: protocol) if not transfer_to_phone.startswith("+"): transfer_to_phone = f"+{transfer_to_phone.lstrip('+')}" # Add tel: protocol if not present if not transfer_to_phone.startswith("tel:"): transfer_to_phone = f"tel:{transfer_to_phone}" # Inform the caller about the transfer await session.say(transfer_message, _add_to_chat_ctx_=True) # Initialize LiveKit API livekit_api = user_data.lkapi if transfer_type == "cold": # Create transfer request transfer_request = TransferSIPParticipantRequest( _participant_identity_=participant_identity, _room_name_=room_name, _transfer_to_=transfer_to_phone, _play_dialtone_=True, ) logger.debug(f"Cold Transfer request: {transfer_request}") # Transfer caller await livekit_api.sip.transfer_sip_participant(transfer_request) logger.info( f"Successfully transferred participant {participant_identity} to {transfer_to_phone} (cold transfer)" ) return transfer_message 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) summary = None if transfer_type == "warm_summary": chat_history = context.session.history summary = await generate_chat_summary(chat_history) await session.say(summary, _add_to_chat_ctx_=True) await asyncio.sleep(3) # Wait for message to complete elif transfer_type == "warm_message" and 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)" ) if transfer_type == "warm_summary": return summary _else_: return warm_transfer_message except Exception as e: error_msg = f"Failed to transfer call: {e}" logger.error(error_msg) return error_msg This is how I transfer my calls, now let me know what method is good to pass original number
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!