Skip to content

Commit 6bf4749

Browse files
committed
decorator
1 parent 2c3ba73 commit 6bf4749

File tree

3 files changed

+39
-52
lines changed

3 files changed

+39
-52
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ This feature is especially powerful when used with [Agents](https://docs.livekit
140140
The participant who implements the method and will receive its calls must first register support:
141141

142142
```python
143-
@room.local_participant.rpc_method("greet")
143+
@room.local_participant.register_rpc_method("greet")
144144
async def handle_greet(request_id: str, caller_identity: str, payload: str, response_timeout: float):
145145
print(f"Received greeting from {caller_identity}: {payload}")
146146
return f"Hello, {caller_identity}!"

examples/rpc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ async def main():
8989

9090

9191
def register_receiver_methods(greeters_room: rtc.Room, math_genius_room: rtc.Room):
92-
@greeters_room.local_participant.rpc_method("arrival")
92+
@greeters_room.local_participant.register_rpc_method("arrival")
9393
async def arrival_method(
9494
data: RpcInvocationData,
9595
):
9696
print(f'[Greeter] Oh {data.caller_identity} arrived and said "{data.payload}"')
9797
await asyncio.sleep(2)
9898
return "Welcome and have a wonderful day!"
9999

100-
@math_genius_room.local_participant.rpc_method("square-root")
100+
@math_genius_room.local_participant.register_rpc_method("square-root")
101101
async def square_root_method(
102102
data: RpcInvocationData,
103103
):
@@ -114,7 +114,7 @@ async def square_root_method(
114114
print(f"[Math Genius] Aha! It's {result}")
115115
return json.dumps({"result": result})
116116

117-
@math_genius_room.local_participant.rpc_method("divide")
117+
@math_genius_room.local_participant.register_rpc_method("divide")
118118
async def divide_method(
119119
data: RpcInvocationData,
120120
):
@@ -128,7 +128,7 @@ async def divide_method(
128128
result = dividend / divisor
129129
return json.dumps({"result": result})
130130

131-
@math_genius_room.local_participant.rpc_method("long-calculation")
131+
@math_genius_room.local_participant.register_rpc_method("long-calculation")
132132
async def long_calculation_method(
133133
data: RpcInvocationData,
134134
):

livekit-rtc/livekit/rtc/participant.py

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -292,70 +292,57 @@ async def perform_rpc(
292292

293293
def register_rpc_method(
294294
self,
295-
method: str,
296-
handler: Callable[[RpcInvocationData], Union[Awaitable[str], str]],
297-
) -> None:
295+
method_name: str,
296+
handler: Optional[Callable[[RpcInvocationData], Union[Awaitable[str], str]]] = None,
297+
) -> Union[None, Callable]:
298298
"""
299299
Establishes the participant as a receiver for calls of the specified RPC method.
300-
Will overwrite any existing callback for the same method.
300+
Can be used either as a decorator or a regular method.
301+
302+
The handler will recieve one argument of type `RpcInvocationData` and should return a string response which will be forwarded back to the caller.
303+
304+
The handler may be synchronous or asynchronous.
305+
306+
If unable to respond within `response_timeout`, the caller will hang up and receive an error on their side.
307+
308+
You may raise errors of type `RpcError` in the handler, and they will be forwarded to the caller.
309+
310+
Other errors raised in your handler will be caught and forwarded to the caller as "1500 Application Error".
301311
302312
Args:
303-
method (str): The name of the indicated RPC method
304-
handler (Callable): Will be invoked when an RPC request for this method is received
313+
method_name (str): The name of the indicated RPC method.
314+
handler (Optional[Callable]): Handler to be invoked whenever an RPC request for this method is received. Omit this argument to use the decorator syntax.
305315
306316
Returns:
307-
None
308-
309-
Raises:
310-
RpcError: On failure. Details in `message`.
317+
None (when used as a decorator it returns the decorator function)
311318
312319
Example:
320+
# As a decorator:
321+
@room.local_participant.register_rpc_method("greet")
313322
async def greet_handler(data: RpcInvocationData) -> str:
314323
print(f"Received greeting from {data.caller_identity}: {data.payload}")
315324
return f"Hello, {data.caller_identity}!"
316-
317-
await room.local_participant.register_rpc_method('greet', greet_handler)
318-
319-
The handler should return a string or a coroutine that resolves to a string.
320-
321-
If unable to respond within `response_timeout`, the caller will hang up and receive an error on their side.
322-
323-
You may raise errors of type `RpcError` with a string `message` in the handler,
324-
and they will be received on the caller's side with the message intact.
325-
Other errors raised in your handler will not be transmitted as-is, and will instead arrive to the caller as `1500` ("Application Error").
326-
"""
327-
self._rpc_handlers[method] = handler
328-
329-
req = proto_ffi.FfiRequest()
330-
req.register_rpc_method.local_participant_handle = self._ffi_handle.handle
331-
req.register_rpc_method.method = method
332-
333-
FfiClient.instance.request(req)
334-
335-
def rpc_method(self, method: str):
336-
"""
337-
Decorator form of `register_rpc_method`
338-
339-
Args:
340-
method (str): The name of the indicated RPC method
341-
342-
Example:
343-
@local_participant.rpc_method("greet")
325+
326+
# As a regular method:
344327
async def greet_handler(data: RpcInvocationData) -> str:
345328
print(f"Received greeting from {data.caller_identity}: {data.payload}")
346-
return f"Hello, {params.caller_identity}!"
329+
return f"Hello, {data.caller_identity}!"
347330
348-
See Also:
349-
`register_rpc_method` for more details
331+
room.local_participant.register_rpc_method('greet', greet_handler)
350332
"""
351333

352-
def decorator(
353-
handler: Callable[[RpcInvocationData], Union[Awaitable[str], str]],
354-
):
355-
self.register_rpc_method(method, handler)
356-
return handler
334+
def register(handler_func):
335+
self._rpc_handlers[method_name] = handler_func
336+
req = proto_ffi.FfiRequest()
337+
req.register_rpc_method.local_participant_handle = self._ffi_handle.handle
338+
req.register_rpc_method.method = method_name
339+
FfiClient.instance.request(req)
357340

358-
return decorator
341+
if handler is not None:
342+
register(handler)
343+
else:
344+
# Called as a decorator
345+
return register
359346

360347
def unregister_rpc_method(self, method: str) -> None:
361348
"""

0 commit comments

Comments
 (0)