MCPcopy Create free account
hub / github.com/chainlit/chainlit / wrap_user_function

Function wrap_user_function

backend/chainlit/utils.py:17–59  ·  view source on GitHub ↗

Wraps a user-defined function to accept arguments as a dictionary. Args: user_function (Callable): The user-defined function to wrap. Returns: Callable: The wrapped function.

(user_function: Callable, with_task=False)

Source from the content-addressed store, hash-verified

15
16
17def wrap_user_function(user_function: Callable, with_task=False) -> Callable:
18 """
19 Wraps a user-defined function to accept arguments as a dictionary.
20
21 Args:
22 user_function (Callable): The user-defined function to wrap.
23
24 Returns:
25 Callable: The wrapped function.
26 """
27
28 @functools.wraps(user_function)
29 async def wrapper(*args):
30 # Get the parameter names of the user-defined function
31 user_function_params = list(inspect.signature(user_function).parameters.keys())
32
33 # Create a dictionary of parameter names and their corresponding values from *args
34 params_values = {
35 param_name: arg for param_name, arg in zip(user_function_params, args)
36 }
37
38 if with_task:
39 await context.emitter.task_start()
40
41 try:
42 # Call the user-defined function with the arguments
43 if inspect.iscoroutinefunction(user_function):
44 return await user_function(**params_values)
45 else:
46 return user_function(**params_values)
47 except CancelledError:
48 pass
49 except Exception as e:
50 logger.exception(e)
51 if with_task:
52 await ErrorMessage(
53 content=str(e) or e.__class__.__name__, author="Error"
54 ).send()
55 finally:
56 if with_task:
57 await context.emitter.task_end()
58
59 return wrapper
60
61
62def make_module_getattr(registry):

Callers 15

password_auth_callbackFunction · 0.90
header_auth_callbackFunction · 0.90
oauth_callbackFunction · 0.90
on_logoutFunction · 0.90
on_messageFunction · 0.90
on_chat_startFunction · 0.90
on_chat_resumeFunction · 0.90
set_chat_profilesFunction · 0.90
set_startersFunction · 0.90
on_chat_endFunction · 0.90
on_audio_chunkFunction · 0.90
on_audio_endFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected