
    i                         dde dede fdZy)<   codetime_outreturnc                    ddl ddlddl}ddlddlm dt        ffd}dt        ffd}d}d}d}	  ||       }|j                  } |       t        |j                               g}		 |j                  |	t        |      d	|d
      }
|
j                  j                  dd      }|
j                  j                  dd      }|r'|j                         r	 j!                  |       ||z   S ||z   S # |j                  $ ri}|j                  r|j                  j                  dd      nd}|j                  r|j                  j                  dd      nd}|d| dz  }Y d}~d}~wt        $ r}dt        |       }Y d}~d}~ww xY w# t        $ r}dt        |       }Y d}~d}~ww xY w# t        $ r Y ||z   S w xY w# |r3|j                         r"	 j!                  |       w # t        $ r Y w w xY ww w xY w)z
    Execute code and return std outputs and std error.

    Args:
        code (str): Code to execute.
        time_out (int): time out, in second.

    Returns:
        str: std output and std error
        N)Path_codec                      j                  dd            j                         }|j                  d       j                          d}||z  }| j	                  d      r| n| dz   }|j                  |d       |S )	NTEMP_CODE_DIRtempT)exist_okz.py
utf-8)encoding)getenvresolvemkdiruuid4endswith
write_text)r	   
_workspacefilename	code_pathcode_contentr   osuuids        U/Users/bowang/.openclaw/workspace/ChatDev/functions/function_calling/code_executor.py__write_script_filez)execute_code.<locals>.__write_script_file   s}    "))OV<=EEG
$'jjl^3')	 %t 4u%$,\G<    r   c                  $     j                   xs dS )Npython3)
executable)syss   r   __default_interpreterz+execute_code.<locals>.__default_interpreter   s    ~~**r    TF)cwdcapture_outputtimeoutcheckr   replace)errorsz"
Error: Execution timed out after z	 seconds.zExecution error: zSetup error: )r   r#   
subprocessr   pathlibr   strparentr   runstdoutdecodestderrTimeoutExpired	Exceptionexistsremove)r   r   r,   r   r$   script_pathr1   r3   	workspacecmd	completeder   r   r#   r   s               @@@@r   execute_coder=      s    3 +3 + KFF)$/&&	$&K,?,?,A(BC	2"	N#  ' I %%,,WY,GF%%,,WY,GF ;--/		+& F?6F?! (( 	PCD88QXX__WY_?QSFCD88QXX__WY_?QSF;H:YOOF 	2(Q1F	2  * Q)*  F? ;--/		+&  0;s   4F 1AC8 F6 8FAE+&F +F7F
F 
FF 	F3F.)G	 .F33G	 6	GG	H G/.H /	G;8H :G;;H N)r   )r.   intr=    r   r   <module>r@      s#   As Ac A3 Ar   