I want to send data to fronted on tool execution h...
# ask-ai
b
I want to send data to fronted on tool execution how to do that?
t
Thanks for reaching out, Madhav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To send data to the frontend upon tool execution in LiveKit, you can use Remote Procedure Calls (RPC). Here’s how: 1. Use RPC to Forward Tool Calls: ◦ Implement an agent function with the
@function_tool()
decorator that performs an RPC call using
room.local_participant.perform_rpc()
. ◦ Example:
Copy code
@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:
Copy code
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 Docs
b
how to get room object in await room.local_participant.perform_rpc
t
To grab the
room
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:
Copy code
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!
b
I need to do this in function tool function right? can you share implementation for that
t
Absolutely, you can implement this within a function tool! Here’s a friendly example showing how to grab the room object and make an RPC call inside a function tool function:
Copy code
from 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