@@ -78,6 +78,25 @@ def output_return_object(obj, context: Context):
7878 else :
7979 context .finish (error = f"return object needs to be a dictionary, but get type: { type (obj )} " )
8080
81+
82+ def get_call_args (fn , context : Context ) -> tuple :
83+ """
84+ Determine the arguments to pass to a block function based on its signature.
85+ Returns a tuple of positional arguments.
86+ """
87+ signature = inspect .signature (fn )
88+ params_count = len (signature .parameters )
89+
90+ if params_count == 0 :
91+ return ()
92+ elif params_count == 1 :
93+ first_param = list (signature .parameters .values ())[0 ]
94+ if first_param .annotation is Context :
95+ return (context ,)
96+ return (context .inputs ,)
97+ else :
98+ return (context .inputs , context )
99+
81100logger = logging .getLogger (EXECUTOR_NAME )
82101
83102async def run_block (message , mainframe : Mainframe , session_dir : str , tmp_dir : str , package_name : str , pkg_dir : str ):
@@ -145,28 +164,15 @@ async def run_block(message, mainframe: Mainframe, session_dir: str, tmp_dir: st
145164 return
146165
147166 try :
148- signature = inspect .signature (fn )
149- params_count = len (signature .parameters )
167+ args = get_call_args (fn , context )
150168 result = None
151169 traceback_str = None
152170
153171 try :
154172 if inspect .iscoroutinefunction (fn ):
155- if params_count == 0 :
156- result = await fn ()
157- elif params_count == 1 :
158- only_context_param = list (signature .parameters .values ())[0 ].annotation is Context
159- result = await fn (context ) if only_context_param else await fn (context .inputs )
160- else :
161- result = await fn (context .inputs , context )
173+ result = await fn (* args )
162174 else :
163- if params_count == 0 :
164- result = fn ()
165- elif params_count == 1 :
166- only_context_param = list (signature .parameters .values ())[0 ].annotation is Context
167- result = fn (context ) if only_context_param else fn (context .inputs )
168- else :
169- result = fn (context .inputs , context )
175+ result = fn (* args )
170176 except ExitFunctionException as e :
171177 if e .args [0 ] is not None :
172178 context .finish (error = "block call exit with message: " + str (e .args [0 ]))
0 commit comments