
    3fi                         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	ee      Z G d
 de      Z G d de      Zy)    N)Enum)AnyDictListOptionalType)CallbackManagerForToolRun)	BaseModelField)GmailBaseTool)clean_email_bodyc                       e Zd ZdZdZdZy)Resourcez"Enumerator of Resources to search.threadsmessagesN)__name__
__module____qualname____doc__THREADSMESSAGES     e/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/gmail/search.pyr   r      s    ,GHr   r   c                       e Zd ZU dZ edd      Zeed<    eej                  d      Z
eed<    ed	d
      Zeed<   y)SearchArgsSchemaz#Input schema 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      sX    - 5E3  !!@Hh 
 >K r   r   c                       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	Zd
eeeef      deeeef      fdZej"                  ddfdedededee   deeeef      f
dZy)GmailSearchz4Tool that searches for messages or threads in Gmail.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                 2   g }|D ]  }|d   }| j                   j                         j                         j                  d|      j	                         }|d   }g |d<   |D ]!  }|d   }|d   j                  ||d   d       # |j                  |        |S )Nidme)userIdr-   r   snippet)r0   r-   )api_resourceusersr   getexecuteappend)	selfr   resultsthread	thread_idthread_datar   messager0   s	            r   _parse_threadszGmailSearch._parse_threads=   s     	#FtI!!'')DY/	  #:.H!#F:# U!),z"))gWT]*STU NN6"	# r   r   c           
         g }|D ]s  }|d   }| j                   j                         j                         j                  dd|      j	                         }t        j                  |d         }t        j                  |      }|j                  dd      }|j                  dd      }	d}
|j                         rp|j                         D ]\  }|j                         }t        |j                  d            }|d	k(  s3d
|vs8	 |j                  d      j                  dd      }
 n& n$	 |j                  d      j                  dd      }
t!        |
      }|j#                  |d   |d   |d   |||	d       v |S # t        $ r& |j                  d      j                  dd      }
Y w xY w# t        $ r& |j                  d      j                  dd      }
Y w xY w)Nr-   r.   raw)r/   formatr-   Subject FromzContent-Dispositionz
text/plain
attachmentT)decodezutf-8replace)errorszlatin-1threadIdr0   )r-   rG   r0   bodysubjectsender)r1   r2   r   r3   r4   base64urlsafe_b64decodeemailmessage_from_bytesis_multipartwalkget_content_typer#   get_payloadrD   UnicodeDecodeErrorr   r5   )r6   r   r7   r;   
message_idmessage_dataraw_message	email_msgrI   rJ   message_bodypartctypecdisporH   s                  r   _parse_messageszGmailSearch._parse_messagesQ   s    4	G J!!'')D:>	  !22<3FGK00=ImmIr2G]]62.FL%%'%NN, D 113E *?!@AF,V1K+/+;+;4+;+H+O+O '	 ,P ,L #,#8#8#8#E#L#L	 $M $L $L1DNN!$- ,Z 8+I6 &$	W4	j 7  2 +/+;+;4+;+H+O+O )) ,P ,L * #,#8#8#8#E#L#L!) $M $Ls$   #E>*#F0>,F-,F-0,GGr!   Nr   r    r"   run_managerc                 8   |t         j                  k(  rs| j                  j                         j	                         j                  d||      j                         j                  |j                  g       }| j                  |      S |t         j                  k(  rs| j                  j                         j                         j                  d||      j                         j                  |j                  g       }| j                  |      S t        d| d      )zRun the tool.r.   )r/   q
maxResultszResource of type z not implemented.)r   r   r1   r2   r   listr4   r3   valuer\   r   r   r<   NotImplementedError)r6   r   r    r"   r]   r7   s         r   _runzGmailSearch._run   s     x(((!!'')TU{CX^^R(  ''00)))!!'')TU{CX^^R(  &&w//%(9(CT&UVVr   )r   r   r   r   r)   r#   r$   r   r   r*   r   r   r   r   r<   r\   r   r   r%   r   r	   rd   r   r   r   r'   r'   0   s    >D#	@   +;K&':d4S>&: tDcN?S (7T#s(^(< 7d3PS8nAU 7x &..;?WW W 	W
 78W 
d38n	Wr   r'   )rK   rM   enumr   typingr   r   r   r   r   langchain_core.callbacksr	   pydanticr
   r   %langchain_google_community.gmail.baser   &langchain_google_community.gmail.utilsr   r#   r   r   r'   r   r   r   <module>rk      sJ       2 2 > % ? CsD y 8uW- uWr   