2525logger = get_logger (__name__ )
2626
2727
28- def run_code (code : str , language : str , tool_context : ToolContext ) -> str :
28+ def run_code (
29+ code : str , language : str , tool_context : ToolContext , timeout : int = 30
30+ ) -> str :
2931 """Run code in a code sandbox and return the output.
3032 For C++ code, don't execute it directly, compile and execute via Python; write sources and object files to /tmp.
3133
3234 Args:
3335 code (str): The code to run.
3436 language (str): The programming language of the code. Language must be one of the supported languages: python3.
37+ timeout (int, optional): The timeout in seconds for the code execution. Defaults to 30.
3538
3639 Returns:
3740 str: The output of the code execution.
@@ -55,7 +58,7 @@ def run_code(code: str, language: str, tool_context: ToolContext) -> str:
5558 logger .debug (f"tool_user_session_id: { tool_user_session_id } " )
5659
5760 logger .debug (
58- f"Running code in language: { language } , session_id={ session_id } , code={ code } , tool_id={ tool_id } , host={ host } , service={ service } , region={ region } "
61+ f"Running code in language: { language } , session_id={ session_id } , code={ code } , tool_id={ tool_id } , host={ host } , service={ service } , region={ region } , timeout= { timeout } "
5962 )
6063
6164 ak = tool_context .state .get ("VOLCENGINE_ACCESS_KEY" )
@@ -87,7 +90,7 @@ def run_code(code: str, language: str, tool_context: ToolContext) -> str:
8790 "OperationPayload" : json .dumps (
8891 {
8992 "code" : code ,
90- "timeout" : 30 ,
93+ "timeout" : timeout ,
9194 "kernel_name" : language ,
9295 }
9396 ),
0 commit comments