o
    ZhJ                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ G dd	 d	eeZG d
d deZG dd deZdS )    N)Enum)AnyDictListOptionalType)CallbackManagerForToolRun)	BaseModelField)GmailBaseTool)clean_email_bodyc                   @   s   e Zd ZdZdZdZdS )Resourcez"Enumerator of Resources to search.threadsmessagesN)__name__
__module____qualname____doc__THREADSMESSAGES r   r   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_community/gmail/search.pyr      s    r   c                   @   sP   e Zd ZU dZedddZeed< eej	ddZ
eed< ed	d
dZeed< dS )SearchArgsSchemazInput for SearchGmailTool..a  The Gmail query. Example filters include from:sender, to:recipient, subject:subject, -filtered_term, in:folder, is:important|read|starred, after:year/mo/date, before:year/mo/date, label:label_name "exact phrase". Search newer/older than using d (day), m (month), and y (year): newer_than:2d, older_than:1y. Attachments with extension example: filename:pdf. Multiple term matching example: from:amy OR from:david.)descriptionqueryz*Whether to search for threads or messages.)defaultr   resource
   z(The maximum number of results to return.max_resultsN)r   r   r   r   r
   r   str__annotations__r   r   r   r   intr   r   r   r   r      s   
 r   c                   @   s   e Zd ZU dZdZeed< dZeed< eZ	e
e ed< deeeef  deeeef  fd	d
Zdeeeef  deeeef  fddZejddfdedededee deeeef  f
ddZdS )GmailSearchz4Tool that searches for messages or threads in Gmail.Zsearch_gmailnamezUse this tool to search for email messages or threads. The input must be a valid Gmail query. The output is a JSON list of the requested resource.r   args_schemar   returnc           	      C   sz   g }|D ]6}|d }| j   jd|d }|d }g |d< |D ]}|d }|d ||d d q"|| q|S )Nidme)userIdr&   r   snippet)r)   r&   )api_resourceusersr   getexecuteappend)	selfr   resultsthread	thread_idZthread_datar   messager)   r   r   r   _parse_threads9   s    zGmailSearch._parse_threadsr   c              
   C   s   g }|D ]}|d }| j   jdd|d }t|d }t|}|d }|d }	d}
|	 rn|
 D ]5}| }t|d}|d	krld
|vrlz|jddd}
W n tyi   |jddd}
Y nw  nq7n	|jddd}
t|
}||d |d |d |||	d q|S )Nr&   r'   raw)r(   formatr&   SubjectFrom zContent-Dispositionz
text/plain
attachmentT)decodezutf-8zlatin-1threadIdr)   )r&   r<   r)   bodysubjectsender)r*   r+   r   r,   r-   base64urlsafe_b64decodeemailmessage_from_bytesis_multipartwalkget_content_typer   get_payloadr;   UnicodeDecodeErrorr   r.   )r/   r   r0   r3   Z
message_idZmessage_dataZraw_messageZ	email_msgr>   r?   message_bodypartctypeZcdispor=   r   r   r   _parse_messagesM   sP   

zGmailSearch._parse_messagesr   Nr   r   r   run_managerc                 C   s   |t jkr| j  jd||d |jg }| 	|S |t j
kr<| j  jd||d |jg }| |S td| d)zRun the tool.r'   )r(   qZ
maxResultszResource of type z not implemented.)r   r   r*   r+   r   listr-   r,   valuerL   r   r   r4   NotImplementedError)r/   r   r   r   rM   r0   r   r   r   _run}   s&   





zGmailSearch._run)r   r   r   r   r#   r   r    r   r   r$   r   r   r   r   r4   rL   r   r   r!   r   r   rR   r   r   r   r   r"   .   s,   
 
**3r"   )r@   rB   enumr   typingr   r   r   r   r   Zlangchain_core.callbacksr   Zpydanticr	   r
   Z%langchain_google_community.gmail.baser   Z&langchain_google_community.gmail.utilsr   r   r   r   r"   r   r   r   r   <module>   s    