
    Fd%              	         d Z ddlmZ ddlZddlZddl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mZ ed         Zd@dZdAdZdBdZ	 dCdDdZ	 dCdEd!Z ed"d#dd$d%d&d'i          dFd(            ZdGd+Z ed,d-d$d.d&d'd$d/d&d'd0          dHd1            Z ed2d3d$d.d&d'd$d/d&d'd0          	 dIdJd5            Z  ed6d7dd$d8d&d'i          dFd9            Z! ed:d;d<d$d=d&d'i          dKd?            Z"dS )LzFile operations for AutoGPT    )annotationsN)	GeneratorLiteral)Config)Agent)command)read_textual_file)logger)
MemoryItemVectorMemory)writeappenddeletetextstrreturnc                t    t          j        |                     d                                                    S )z(Get the hex checksum for the given text.utf-8)hashlibmd5encode	hexdigest)r   s    TC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\commands\file_operations.pytext_checksumr      s*    ;t{{7++,,66888    log_path8Generator[tuple[Operation, str, str | None], None, None]c              #  V  K   	 t          | dd          }n# t          $ r Y dS w xY w|D ]}|                    dd                                          }|s-|                    dd	          \  }}|                                }|d
v rl	 d |                    dd	          D             \  }}n># t          $ r1 t          j        d| d           |                                d}}Y nw xY w|||fV  |dk    r||                                dfV  |	                                 dS )zKParse the file operations log and return a tuple containing the log entriesrr   encodingNzFile Operation Logger :    )maxsplitr   r   c              3  >   K   | ]}|                                 V  d S N)strip).0xs     r   	<genexpr>z&operations_from_log.<locals>.<genexpr>+   s*      !S!S!''))!S!S!S!S!S!Sr    #z File log entry lacks checksum: ''r   )
openFileNotFoundErrorreplacer)   splitrsplit
ValueErrorr
   warnclose)r   logline	operationtailpathchecksums          r   operations_from_logr=      s~     8S7333     2 2||3R88>>@@ 	**TA*66	4OO%%	+++4!S!ST[[PQ[5R5R!S!S!Shh 4 4 4FtFFFGGG!%th4 dH-----(""djjllD1111IIKKKKKs    
%%
$B//8C*)C*dict[str, str]c                \    i }t          |           D ]\  }}}|dv r|||<   |dk    r||= |S )a  Iterates over the operations log and returns the expected state.

    Parses a log file at config.file_logger_path to construct a dictionary that maps
    each file path written or appended to its checksum. Deleted files are removed
    from the dictionary.

    Returns:
        A dictionary mapping file paths to their checksums.

    Raises:
        FileNotFoundError: If config.file_logger_path is not found.
        ValueError: If the log file content is not in the expected format.
    r&   r   )r=   )r   stater9   r;   r<   s        r   file_operations_staterA   6   sV     E%8%B%B  !	4+++"E$KK(""dLr   r9   	Operationfilenameconfigr   r<   
str | Noneboolc                    t          |j                  }| dk    r||vrdS | dk    r|                    |          |k    rdS dS )aT  Check if the operation has already been performed

    Args:
        operation: The operation to check for
        filename: The name of the file to check for
        config: The agent config
        checksum: The checksum of the contents to be written

    Returns:
        True if the operation has already been performed on the file
    r   Tr   F)rA   file_logger_pathget)r9   rC   rD   r<   r@   s        r   is_duplicate_operationrJ   M   sY     "&"9::EH!6!6tG		( 3 3x ? ?t5r   agentr   Nonec                    |  d| }||d| z  }t          j        d|            t          |j        j        | d|d           dS )zLog the file operation to the file_logger.txt

    Args:
        operation: The operation to log
        filename: The name of the file the operation was performed on
        checksum: The checksum of the contents to be written
    r#   Nr-   zLogging file operation: 
F)
should_log)r
   debugappend_to_filerD   rH   )r9   rC   rK   r<   	log_entrys        r   log_operationrS   c   s~     ****I_(__$	
L7I77888%)'7'7'75     r   	read_filezRead an existing filestringzThe path of the file to readT)typedescriptionrequiredc                    	 t          | t                    }t          j        || |j                  }t          |j                  dk    r|j        S |S # t          $ r}dt          |           cY d}~S d}~ww xY w)zRead a file and return the contents

    Args:
        filename (str): The name of the file to read

    Returns:
        str: The contents of the file
    r$   Error: N)
r	   r
   r   from_text_filerD   lenchunkssummary	Exceptionr   )rC   rK   contentfile_memoryes        r   rT   rT   v   s    (
"#Hf55 !/5<PP{!""Q&&&& " " "!Q!!!!!!!!"s$   AA A 
A9A4.A94A9memoryr   c                   	 t          j        d|             t          |           }t          j        ||           }t          j        d|                    d                      |                    |           t          j        dt          |j	                   d|             dS # t          $ r%}t          j        d|  d|            Y d}~dS d}~ww xY w)	a/  
    Ingest a file by reading its content, splitting it into chunks with a specified
    maximum length and overlap, and adding the chunks to the memory storage.

    Args:
        filename: The name of the file to ingest
        memory: An object with an add() method to store the chunks in memory
    zIngesting file zCreated memory: Tz	Ingested z chunks from zError while ingesting file 'z': N)r
   inforT   r   r[   rP   dumpaddr\   e_chunksr_   r5   )rC   rc   r`   ra   errs        r   ingest_filerj      s   G0h00111H%% !/BB@(8(8(>(>@@AAA

;RK$8 9 9RRRRSSSSS G G GE8EEEEFFFFFFFFFGs   B&B* *
C4CCwrite_to_filezWrites to a filez The name of the file to write tozThe text to write to the file)rC   r   c                   t          |          }t          d| |j        |          rdS 	 t          j                            |           }t          j        |d           t          | dd          5 }|                    |           ddd           n# 1 swxY w Y   t          d| ||           d	S # t          $ r}d
| cY d}~S d}~ww xY w)zWrite text to a file

    Args:
        filename (str): The name of the file to write to
        text (str): The text to write to the file

    Returns:
        str: A message indicating success or failure
    r   z%Error: File has already been updated.Texist_okwr   r    NzFile written to successfully.rZ   )r   rJ   rD   osr;   dirnamemakedirsr/   r   rS   r_   )rC   r   rK   r<   	directoryfri   s          r   rk   rk      s&   4 T""HgxxHH 766GOOH--	
I----(C'222 	aGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	gx999..   sB   AB2 1BB2 BB2 BB2 2
C<C CCrQ   zAppends to a filerO   c                   	 t           j                            |           }t          j        |d           t	          | dd          5 }|                    |           ddd           n# 1 swxY w Y   |r^t	          | dd          5 }t          |                                          }ddd           n# 1 swxY w Y   t          d| ||	           d
S # t          $ r}d| cY d}~S d}~ww xY w)a  Append text to a file

    Args:
        filename (str): The name of the file to append to
        text (str): The text to append to the file
        should_log (bool): Should log output

    Returns:
        str: A message indicating success or failure
    Trm   ar   r    Nr   r   )r<   zText appended successfully.rZ   )
rp   r;   rq   rr   r/   r   r   readrS   r_   )rC   r   rK   rO   rs   rt   r<   ri   s           r   rQ   rQ      s   :GOOH--	
I----(C'222 	aGGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	Hhg666 3!(223 3 3 3 3 3 3 3 3 3 3 3 3 3 3(HehGGGG,,   sf   AC 	A+C +A//C 2A/3C 
"B8,C 8B<<C ?B< C 
C1"C,&C1,C1delete_filezDeletes a filezThe name of the file to deletec                    t          d| |j                  rdS 	 t          j        |            t	          d| |           dS # t
          $ r}d| cY d}~S d}~ww xY w)zDelete a file

    Args:
        filename (str): The name of the file to delete

    Returns:
        str: A message indicating success or failure
    r   z%Error: File has already been deleted.zFile deleted successfully.rZ   N)rJ   rD   rp   removerS   r_   )rC   rK   ri   s      r   rx   rx     s    ( h%,?? 766
	(h%000++   s   %A 
AAAA
list_fileszLists Files in a Directoryrs   zThe directory to list files in	list[str]c                0   g }t          j        |           D ]~\  }}}|D ]u}|                    d          rt           j                            t           j                            ||          |j        j                  }|                    |           v|S )zlists files in a directory recursively

    Args:
        directory (str): The directory to search in

    Returns:
        list[str]: A list of files found in the directory
    .)	rp   walk
startswithr;   relpathjoinrD   workspace_pathr   )rs   rK   found_filesroot_filesfilerelative_paths           r   r{   r{   $  s    ( K'),, . .a 	. 	.Ds## GOOT4((%,*E M }----	. r   )r   r   r   r   )r   r   r   r   )r   r   r   r>   r(   )
r9   rB   rC   r   rD   r   r<   rE   r   rF   )
r9   r   rC   r   rK   r   r<   rE   r   rL   )rC   r   rK   r   r   r   )rC   r   rc   r   r   rL   )rC   r   r   r   rK   r   r   r   )T)
rC   r   r   r   rK   r   rO   rF   r   r   )rs   r   rK   r   r   r|   )#__doc__
__future__r   r   rp   os.pathtypingr   r   
confectionr   autogpt.agent.agentr   autogpt.command_decoratorr   &autogpt.commands.file_operations_utilsr	   autogpt.configautogpt.logsr
   autogpt.memory.vectorr   r   rB   r   r=   rA   rJ   rS   rT   rj   rk   rQ   rx   r{    r   r   <module>r      sK   ! ! " " " " " "  				  % % % % % % % %       % % % % % % - - - - - - D D D D D D ! ! ! ! ! !       : : : : : : : :/0	9 9 9 9
   8   0 QU    . IM    & 	9
 

 
" " "
 
",G G G G4 	 =
 
 :
 
     !  0 	 =
 
 :
 
  " @D   !  : 	;
 

 
  
 
& 	 ;
 

 
  
 
  r   