
    Fd1                         d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 de
defd	Zd
ede	defdZde
fdZde
dee
e
f         defdZdS )z Command and Control     N)Dict)Agent)Config)ChatModelResponsevaluereturnc                 H    	 t          |            dS # t          $ r Y dS w xY w)zCheck if the value is a valid integer

    Args:
        value (str): The value to check

    Returns:
        bool: True if the value is a valid integer, False otherwise
    TF)int
ValueError)r   s    ?C:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\app.pyis_valid_intr   
   s9    E


t   uus    
!!assistant_reply_jsonassistant_replyconfigc                    |j         r7|j        dS |j        j        t          j        |j        j                  d| d<   	 d| vrdS t          | t                    sdd|  fS | d         }t          |t                    sdS d	|vrd
S |d	         }|                    di           }||fS # t          j	        j
        $ r Y dS t          $ r}dt          |          fcY d}~S d}~ww xY w)a  Parse the response and return the command name and arguments

    Args:
        assistant_reply_json (dict): The response object from the AI
        assistant_reply (ChatModelResponse): The model response from the AI
        config (Config): The config object

    Returns:
        tuple: The command name and arguments

    Raises:
        json.decoder.JSONDecodeError: If the response is not valid JSON

        Exception: If any other error occurs
    N)Error:z%No 'function_call' in assistant reply)nameargscommand)r   z Missing 'command' object in JSONr   z/The previous message sent was not a dictionary )r   z$'command' object is not a dictionaryr   )r   z(Missing 'name' field in 'command' objectr   )r   zInvalid JSON)openai_functionsfunction_callr   jsonloads	arguments
isinstancedictgetdecoderJSONDecodeError	Exceptionstr)r   r   r   r   command_namer   es          r   get_commandr$      sM   $  
(0DD#16J<FGG+
 +
Y' 000??.55 	XBVXX 
 'y1'4(( 	DCC  GGv KK++	Y&&<' ( ( ('''      Q s<    B) B) "B) B) !B) )C$ 	C$	CC$C$r"   c                 2    g d}|D ]\  }}| |k    r|c S | S )zTakes the original command name given by the AI, and checks if the
    string matches a list of common/known hallucinations
    ))
write_filewrite_to_file)create_filer'   )searchgoogle )r"   synonymsseen_commandactual_command_names       r   map_command_synonymsr/   Q   sL      H
 .6 ' '))<''&&&& (    r   agentc                    	 |j         j                            |           }|r |d	i |d|iS t          |                                           } |j        j        j        D ]N}| |d                                         k    s| |d                                         k    r |d         d	i |c S Od|  dS # t          $ r}dt          |           cY d}~S d}~ww xY w)
a$  Execute the command and return the result

    Args:
        command_name (str): The name of the command to execute
        arguments (dict): The arguments for the command
        agent (Agent): The agent that is executing the command

    Returns:
        str: The result of the command
    r1   labelr   functionzUnknown command 'zl'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format.zError: Nr+   )	command_registrycommandsr   r/   lower	ai_configprompt_generatorr    r!   )r"   r   r1   cmdr   r#   s         r   execute_commandr;   `   s;   "$-11,??  	130000%0000 ,L,>,>,@,@AA
 7@ 	8 	8G 0 6 6 8 88876?#8#8#:#:::*wz*77Y77777 ;   	

  " " "!Q!!!!!!!!"s)   ,B6 A?B6 /B6 6
C CCC)__doc__r   typingr   autogpt.agent.agentr   autogpt.configr   autogpt.llmr   r!   boolr   r$   r/   r   r;   r+   r0   r   <module>rB      s            % % % % % % ! ! ! ! ! ! ) ) ) ) ) )      4 4 1B4 LR4  4  4  4 ns    ("("CH~(" (" (" (" (" (" ("r0   