bulky-yak-50122
08/06/2025, 11:08 AMtall-airline-16756
08/06/2025, 11:08 AMtall-airline-16756
08/06/2025, 11:09 AM@function_tool()
decorator that performs an RPC call using room.local_participant.perform_rpc()
.
◦ Example: @function_tool
async def send_url_to_frontend(self, context: RunContext[ShopifyUserData], url: str):
return await self._send_url_to_frontend_internal(context.userdata, url)
async def _send_url_to_frontend_internal(self, userdata: ShopifyUserData, url: str):
payload = {"url": url}
await room.local_participant.perform_rpc(
destination_identity=client_participant.identity,
method="client.navigate",
payload=json.dumps(payload)
)
1. Frontend Implementation:
◦ Register an RPC handler on the frontend to receive and process the data.
◦ Example: room.localParticipant.registerRpcMethod("client.navigate", (data) => {
const url = JSON.parse(data).url;
window.location.href = url;
});
1. Data Sharing Alternatives:
◦ Byte Streams: For sending files or binary data.
◦ Data Packets: For low-level data transfer.
◦ Text Streams: For real-time text updates.
Let me know if you need further details or assistance with implementation!
Sources: complex-agents/nova-sonic/form_agent.py | Remote method calls (RPC) | LiveKit Docs | Web and mobile frontends | LiveKit Docsbulky-yak-50122
08/06/2025, 12:31 PMtall-airline-16756
08/06/2025, 12:31 PMroom
object for await room.local_participant.perform_rpc
, here’s a simple way to do it:
1. Import Necessary Modules: Start by importing what you need from the LiveKit library.
2. Initialize the Room: Create an instance of the Room
class.
3. Connect to the Room: Use the connect
method on your room instance, and provide your LiveKit URL and token.
4. Access Local Participant: Once you’re connected, you can get the local_participant
from the room object.
5. Perform RPC Call: Finally, call perform_rpc
on the local participant to send your RPC.
Here’s a quick example to guide you:
from livekit import rtc
import asyncio
async def main():
room = rtc.Room()
# Connect to the room
URL = "YOUR_LIVEKIT_URL"
TOKEN = "YOUR_ACCESS_TOKEN"
await room.connect(URL, TOKEN)
try:
# Perform an RPC call using the local participant
response = await room.local_participant.perform_rpc(
destination_identity="recipient-identity",
method="greet",
payload="Hello from RPC!"
)
print(f"RPC response: {response}")
except Exception as e:
print(f"RPC call failed: {e}")
asyncio.run(main())
Just swap out YOUR_LIVEKIT_URL
and YOUR_ACCESS_TOKEN
with your actual details. If you hit any bumps or want to chat more about this, I’m here to help!bulky-yak-50122
08/06/2025, 12:34 PMtall-airline-16756
08/06/2025, 12:34 PMfrom livekit.agents import function_tool, get_job_context
import json
@function_tool
async def my_function_tool(context):
try:
# Access the Room instance
room = get_job_context().room
if not room:
raise Exception("Room not available")
# Define the destination participant identity
participant_identity = next(iter(room.remote_participants.keys()))
# Perform the RPC call
response = await room.local_participant.perform_rpc(
destination_identity=participant_identity,
method="my_rpc_method",
payload=json.dumps({"key": "value"}),
response_timeout=5.0 # Optional timeout
)
return response
except Exception as e:
raise ToolError(f"RPC call failed: {e}")
Here are a few key points to keep in mind:
• get_job_context().room
helps you access the room object inside your function tool.
• room.local_participant.perform_rpc()
is the way to send your RPC call.
• Just make sure the room is connected properly and the participant identity you’re targeting is valid.
If anything’s unclear or you want to dive deeper, just let me know—I’m here to help!
Sources: Tool definition and use | LiveKit Docs | avatars/tavus/tavus.py