1818
1919from veadk .tools .builtin_tools ._agentkit import (
2020 get_agentkit_endpoint_config ,
21+ invoke_agentkit_exec_bash ,
2122 invoke_agentkit_run_code ,
2223 resolve_agentkit_tool_id ,
2324)
2728
2829
2930def run_code (
30- code : str , language : str , tool_context : ToolContext , timeout : int = 30
31+ code : str ,
32+ language : str ,
33+ tool_context : ToolContext ,
34+ timeout : int = 30 ,
35+ exec_dir : str = "/tmp" ,
36+ env : dict [str , str ] | None = None ,
37+ hard_timeout : int = 300 ,
38+ max_output_length : int = 30000 ,
3139) -> str :
3240 """Run code in a code sandbox and return the output.
3341 For C++ code, don't execute it directly, compile and execute via Python; write sources and object files to /tmp.
3442
3543 Args:
3644 code (str): The code to run.
37- language (str): The programming language of the code. Language must be one of the supported languages: python3 .
45+ language (str): The execution language. Use ``python3`` for code or ``bash`` for shell scripts .
3846 timeout (int, optional): The timeout in seconds for the code execution. Defaults to 30.
47+ exec_dir (str, optional): Working directory for Bash execution. Defaults to ``/tmp``.
48+ env (dict[str, str], optional): Environment variables for Bash execution.
49+ hard_timeout (int, optional): Hard timeout for Bash execution. Defaults to 300 seconds.
50+ max_output_length (int, optional): Maximum Bash output length. Defaults to 30000.
3951
4052 Returns:
4153 str: The output of the code execution.
@@ -55,19 +67,35 @@ def run_code(
5567 f"Running code in language: { language } , session_id={ session_id } , code={ code } , tool_id={ tool_id } , host={ host } , service={ service } , region={ region } , timeout={ timeout } "
5668 )
5769
58- res = invoke_agentkit_run_code (
59- tool_id = tool_id ,
60- tool_user_session_id = tool_user_session_id ,
61- code = code ,
62- timeout = timeout ,
63- kernel_name = language ,
64- tool_state = tool_context .state if tool_context else None ,
65- ttl = int (os .getenv ("AGENTKIT_TOOL_TTL" , "1800" )),
66- )
70+ tool_state = tool_context .state if tool_context else None
71+ ttl = int (os .getenv ("AGENTKIT_TOOL_TTL" , "1800" ))
72+ if language .lower () in {"bash" , "shell" }:
73+ res = invoke_agentkit_exec_bash (
74+ tool_id = tool_id ,
75+ tool_user_session_id = tool_user_session_id ,
76+ command = code ,
77+ exec_dir = exec_dir ,
78+ env = env ,
79+ timeout = timeout ,
80+ hard_timeout = hard_timeout ,
81+ max_output_length = max_output_length ,
82+ tool_state = tool_state ,
83+ ttl = ttl ,
84+ )
85+ else :
86+ res = invoke_agentkit_run_code (
87+ tool_id = tool_id ,
88+ tool_user_session_id = tool_user_session_id ,
89+ code = code ,
90+ timeout = timeout ,
91+ kernel_name = language ,
92+ tool_state = tool_state ,
93+ ttl = ttl ,
94+ )
6795 logger .debug (f"Invoke run code response: { res } " )
6896
6997 try :
7098 return res ["Result" ]["Result" ]
71- except KeyError as e :
99+ except ( KeyError , TypeError ) as e :
72100 logger .error (f"Error occurred while running code: { e } , response is { res } " )
73101 return res
0 commit comments