
    Fd4                        d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)  G d d          Z*dS )    N)datetime)ForeStyle)Config)AIConfig)extract_json_from_responsevalidate_json)chat_with_ai)OPEN_AI_CHAT_MODELS)count_string_tokens)FULL_MESSAGE_HISTORY_FILE_NAMENEXT_ACTION_FILE_NAMEUSER_INPUT_FILE_NAMELogCycleHandler)loggerprint_assistant_thoughts)MessageHistory)VectorMemory)CommandRegistry)say_text)Spinner)clean_input)	Workspacec                   J    e Zd ZdZdededededededed	ed
e	fdZ
d Zd ZdS )Agenta  Agent class for interacting with Auto-GPT.

    Attributes:
        ai_name: The name of the agent.
        memory: The memory object to use.
        next_action_count: The number of actions to execute.
        system_prompt: The system prompt is the initial prompt that defines everything
          the AI needs to know to achieve its task successfully.
        Currently, the dynamic and customizable information in the system prompt are
          ai_name, description and goals.

        triggering_prompt: The last sentence the AI will see before answering.
            For Auto-GPT, this prompt is:
            Determine exactly one command to use, and respond using the format specified
              above:
            The triggering prompt is not part of the system prompt because between the
              system prompt and the triggering
            prompt we have contextual information that can distract the AI and make it
              forget that its goal is to find the next task to achieve.
            SYSTEM PROMPT
            CONTEXTUAL INFORMATION (memory, previous conversations, anything relevant)
            TRIGGERING PROMPT

        The triggering prompt reminds the AI about its short term meta task
        (defining the next task)
    ai_namememorynext_action_countcommand_registry	ai_configsystem_prompttriggering_promptworkspace_directoryconfigc
                    || _         || _        t          |           | _        || _        || _        |	| _        || _        || _        || _	        t          ||	j                  | _        t          j                                        d          | _        d| _        t%                      | _        t)          j        |	j                  j        | _        d S )Nz%Y%m%d_%H%M%Sr   )r   r   r   historyr   r   r$   r    r!   r"   r   restrict_to_workspace	workspacer   nowstrftime
created_atcycle_countr   log_cycle_handlerr   getfast_llm_model
max_tokensfast_token_limit)
selfr   r   r   r   r    r!   r"   r#   r$   s
             GC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\agent\agent.py__init__zAgent.__init__:   s     %d++!2 0"*!2"#68TUU",..11/BB!0!2!2 3 7!!
 !

 	    c                 P    ddl m}m} d _        d }d }d} fd}t	          j        t          j        |           	  xj        dz  c_        d j        _         j                             j	        j
         j         j        d  j        D             t                      j        j        rR j        j        dk    rB j         j        j        k    r-t#          j        dt&          j         j        j                    d S t+          d	 j        j        
          5  t/           j          j         j         j         j        j                  }d d d            n# 1 swxY w Y   	 t9          |j                  }t=          | j                   n5# t>          j         $ r#}	t#          j!        d|	            i }Y d }	~	nd }	~	ww xY w j        j"        D ],}
|
#                                s|
$                    |          }-|i k    r	 tK           j
        | j                    ||| j                  \  }} j        j&        rtO          d|             (                    |          }n9# tR          $ r,}	t#          j!        dtU          |	                     Y d }	~	nd }	~	ww xY w j                             j	        j
         j         j        |tV                     t#          j        d           t#          j        dt&          j,        dt&          j,         | tZ          j.         dt&          j,         | tZ          j.                     j        j        s6 j/        dk    r*d _0        t#          j1        d j
         d           	  j        j2        rtg           j        d          }n/tg           j        t&          j4        dz   tZ          j.        z             }|5                                6                                 j        j7        k    rd}n8|5                                6                                dk    rt#          j8        d           |5                                9                     j        j7         d          rd	 tu          tw          |<                    d          d                              _/        d}n%# tz          $ r t#          j8        d           Y \w xY wna|5                                 j        j>        k    rd}n<|}d} j                             j	        j
         j         j        |t~                     	 |dk    r!t#          j        dt&          j4        d           ne|dk    rt#          j1        d           d S nHt#          j        d           t#          j        t&          j,         dtZ          j.          j/                    |.|5                                9                    d           rd!| }n1|dk    rd"| }n$ j        j"        D ]0}
|
@                                s|
A                    ||          \  }}1 ||| #          }d$| d%| }t          tU          |           j        j                  }t          tU           j        C                                           j        j                  }||z   d&z    j        k    rd'| d(} j        j"        D ]-}
|
D                                s|
E                    ||          }. j/        dk    r xj/        dz  c_/        |= j        F                    d)|d*           t#          j        d+t&          j        |           n< j        F                    d)d,d*           t#          j        d+t&          j        d,           l)-Nr   )execute_commandget_command c                     j         dk    rt          j                     d S t          t          j        dz   t          j        z              d_         d S )Nr   zAInterrupt signal received. Stopping continuous command execution.)r   sysexitprintr   REDr   	RESET_ALL)signumframer2   s     r3   signal_handlerz4Agent.start_interaction_loop.<locals>.signal_handlerb   sZ    %**




HYZo&  
 *+&&&r5   T   c                 6    g | ]}|                                 S  )raw).0ms     r3   
<listcomp>z0Agent.start_interaction_loop.<locals>.<listcomp>w   s     ///Q///r5   zContinuous Limit Reached: zThinking... )plain_outputz1Exception while validating assistant reply JSON: zI want to execute zError: 

zNEXT ACTION: z
COMMAND = z  ARGUMENTS = zEnter 'y' to authorise command, 'y -N' to run N continuous commands, 's' to run self-feedback commands, 'n' to exit program, or enter feedback for z...zWaiting for your response...zInput:zGENERATE NEXT COMMAND JSONzInvalid input format.z - zTInvalid input format. Please enter 'y -n' where n is the number of continuous tasks.EXIThuman_feedbackz8-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=z
Exiting...zAUTHORISED COMMANDS LEFT: errorzCould not execute command: zHuman feedback: )command_name	argumentsagentzCommand z returned: iX  zFailure: command zm returned too much output.                         Do not execute this command again with the same arguments.systemaction_resultzSYSTEM: zUnable to execute command)Gautogpt.appr7   r8   r,   signalSIGINTr-   log_count_within_cycle	log_cycler    r   r+   r&   r   r$   continuous_modecontinuous_limitr   typewriter_logr   YELLOWr   rJ   r
   r!   r"   r1   r/   r   contentr	   jsonJSONDecodeErrorrO   pluginscan_handle_post_planningpost_planningr   
speak_moder   _resolve_pathlike_command_args	Exceptionstrr   CYANr   r?   r   
user_inputinfochat_messages_enabledr   MAGENTAlowerstripauthorise_keywarn
startswithabsintsplit
ValueErrorexit_keyr   can_handle_pre_commandpre_commandr   summary_messagecan_handle_post_commandpost_commandadd)r2   r7   r8   rP   rQ   ri   rB   assistant_replyassistant_reply_jsonepluginconsole_inputresultcommand_resultresult_tlengthmemory_tlengths   `               r3   start_interaction_loopzAgent.start_interaction_loopW   s#	   <<<<<<<< 	
		+ 		+ 		+ 		+ 		+ 	fm^444F	!<=D"9",,& //$,///.   +
K0144$t{'CCC%0K{35  
 dk6NOOO  ".K&*)K.# #              *'A#+( ($ 2DK@@@@' * * *TQRTTUUU')$$$$$$* +- R R6688 '-';';<P'Q'Q$$ $r))6,&:DK   /:k,ot{/ /+L) {- F !Dl!D!DEEE $ C CI N NII  6 6 6Lc!ff555555556",,& $%   !$'''!	GTY G Geo G G#yG*3G5:_G G   ;. F43IQ3N3N #%)|) ) )  
+{8 (3 K)G) ) )4 K)@5?)R) ) %**,,22448QQQ%A
&,,..4466"<<$;<<< &,,..99;4888  
%58 #M$7$7$<$<Q$? @ @6 6D2 *FJJ) % % %"K!C   %H% &,,..$+2FFF%+
%2
'7.88 N2 O ,&0   !===)R   
  6))K---E *
 %d+++%yeeEOeTMcee  
 'L,>,>,@,@,K,KG,T,T'ByBB!1118J88"k1  F!88:: ! .4.@.@$i/ /+L)) "1!-'" " "
 RLQQQQ!4'')C" " "54466779S" " "N2S84;PPPT T T TF #k1 G GF!99;; ! #00vFFFF)A--**a/** !  6?CCC%j$+vFFFF  +FXXX%-H  IF	sU   3EEE")F F>F99F>A#I% %
J/"JJ:<R7 7SSc                     d|v r'|d         dv rt          | j        j                  |d<   n9dD ]6}||v r0t          | j                            ||                             ||<   7|S )N	directory>   r9   /)filenamer   
clone_path)rg   r(   rootget_path)r2   command_argspathlikes      r3   re   z$Agent._resolve_pathlike_command_args7  s    ,&&<+D	+Q+Q(+DN,?(@(@L%%C  |++-0//X0FGG. .L* r5   N)__name__
__module____qualname____doc__rg   r   rs   r   r   r   r4   r   re   rE   r5   r3   r   r      s         6  	
 *    !    :^ ^ ^@	 	 	 	 	r5   r   )+r_   rV   r;   r   coloramar   r   autogpt.configr   autogpt.config.ai_configr   autogpt.json_utils.utilitiesr   r	   autogpt.llm.chatr
   autogpt.llm.providers.openair   autogpt.llm.utilsr   autogpt.log_cycle.log_cycler   r   r   r   autogpt.logsr   r   autogpt.memory.message_historyr   autogpt.memory.vectorr   autogpt.models.command_registryr   autogpt.speechr   autogpt.spinnerr   autogpt.utilsr   autogpt.workspacer   r   rE   r5   r3   <module>r      s     



                       ! ! ! ! ! ! - - - - - - R R R R R R R R ) ) ) ) ) ) < < < < < < 1 1 1 1 1 1            : 9 9 9 9 9 9 9 9 9 9 9 9 9 . . . . . . ; ; ; ; ; ; # # # # # # # # # # # # % % % % % % ' ' ' ' ' 'b b b b b b b b b br5   