async def _execute_dynamic_tool(self, context: RunContext, tool_def: dict, args: dict) -> str: function_name = tool_def["function"]["name"] tool_call_id = str(uuid.uuid4())
logger.info(f"Executing dynamic tool '{function_name}' with args: {args}") # Special handling for endCallForUser - shut down the session if function_name == "endCallForUser": await self._announce_tool_message(context, tool_def, "request-start")
logger.info("Hanging up the call due to user request") error_msg = "Hanging up the call due to user request using hangup function." session_ctx = get_job_context() await asyncio.sleep(7) session_ctx.shutdown(reason=error_msg) logger.warning("Session shutdown successfully due to hangup function") await self._announce_tool_message(context, tool_def, "request-complete") return "Call ended successfully." if function_name == "transferCallToUser": await self._announce_tool_message(context, tool_def, "request-start") session_ctx = get_job_context() try: transfer_to = args.get("transfer_to") uui_data = args.get("uui_data") if not transfer_to: raise ValueError("Missing transfer_to number") # Append UUI only if provided if uui_data: hex_data = uui_data.encode("utf-8").hex()
logger.info(f"this is hex data {hex_data}") transfer_to += f"?User-to-User={hex_data};encoding=hex"
logger.info(f"this is transfer to {transfer_to}") room_name = session_ctx.room.name
logger.info(f"this is room name {transfer_to}") identity = session_ctx.room.local_participant.identity
logger.info(f"this is room name {identity}") async with api.LiveKitAPI() as livekit_api: transfer_request = TransferSIPParticipantRequest( participant_identity=identity, room_name=room_name, transfer_to=transfer_to, play_dialtone=False, ) await livekit_api.sip.transfer_sip_participant(transfer_request)
logger.info("SIP participant transferred successfully") #await self._announce_tool_message(context, tool_def, "request-complete") return f"Call transferred successfully to {transfer_to}" except Exception as error: logger.error(f"Error transferring SIP participant: {error}") await self._announce_tool_message(context, tool_def, "request-failed") if hasattr(error, "metadata") and error.metadata: sip_code = error.metadata.get("sip_status_code") sip_msg = error.metadata.get("sip_status") return f"Transfer failed. SIP error {sip_code}: {sip_msg}" return f"Transfer failed: {str(error)}" # Announce request start await self._announce_tool_message(context, tool_def, "request-start") try: if "server" in tool_def and "url" in tool_def["server"]:
logger.info(f"Using Vapi-style server endpoint for {function_name}") result = await execute_vapi_server_tool(tool_def["server"], args, tool_def, tool_call_id) # Announce request completion await self._announce_tool_message(context, tool_def, "request-complete") if isinstance(result, dict): if "text" in result: return result["text"] if "content" in result: return result["content"] return str(result) return str(result) else: raise RuntimeError(f"No server configuration found for tool '{function_name}'") except Exception as e: logger.error(f"Tool '{function_name}' failed: {str(e)}") # Announce request failure await self._announce_tool_message(context, tool_def, "request-failed") return f"Tool '{function_name}' failed: {str(e)}"
for this i am getting an error 2026-02-25 06
1533 [ERROR] voice_agent_dynamic: Error transferring SIP participant: TwirpError(code=invalid_argument, message=twirp error unknown: no SIP session associated with participant, status=400)
though i am calling form phone do you think its happening because i am passing wrong participant id ?