
    Ngs                         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edefd	Zd
edede
fdZ eddd           G d de                      ZdS )    N)AnyIterator)
deprecated)BaseChatLoader)ChatSession)HumanMessagemsgreturnc                    d }| d         d         D ]}|d         }|dk    r|d         }|t           | d         d         D ]}|d         dk    r{|d	         d
         }t          j        |                              d          }t	          j        d          }t	          j        ||          d         }t          |d|i          }|c S t           )NpayloadheadersnameFromvaluepartsmimeTypez
text/plainbodydatazutf-8z\r\nOn .+(\r\n)*wrote:\r\nr   sender)contentadditional_kwargs)
ValueErrorbase64urlsafe_b64decodedecoderecompilesplitr   )	r	   
from_emailvaluesr   partr   patternnewest_responsemessages	            b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_loaders/gmail.py_extract_email_contentr&      s    Ji.+ ) )f~6>>JIw'  
|++<'D+D1188AAD j!>??G hw55a8O"'Hj;Q  G NNN ,     servicer$   c                    |                                                                                      d|d                                                   }t	          |          }d }|d         d         }|D ]}|d         }|dk    r|d         }|t
          |d	         }|                                                                                      d|                                          }	|	d
         }
d }|
D ]1}|d         d         }|D ]}|d         dk    r|d         }||k    r|}2|t
          t	          |          }t          ||g          S )Nmeid)userIdr+   r   r   r   zIn-Reply-Tor   threadIdmessagesz
Message-ID)r.   )usersr.   getexecuter&   r   threadsr   )r(   r$   r	   message_contentin_reply_to
email_datar    r   	thread_idthreadr.   response_email
message_idstarter_contents                 r%   _get_message_datar;   #   ss   
--//
"
"
$
$
(
(
(
G
G
O
O
Q
QC,S11OKY	*J * *f~=   /KJI]]__$$&&**$9*EEMMOOFj!HN - -Y'	2
  	- 	-Ff~--#G_
,,%,N		-
 ,^<<O/ BCCCCr'   z0.0.32z1.0z&langchain_google_community.GMailLoader)sinceremovalalternative_importc            	       N     e Zd ZdZddedededdf fd	Zdee	         fd
Z
 xZS )GMailLoadera*  Load data from `GMail`.

    There are many ways you could want to load data from GMail.
    This loader is currently fairly opinionated in how to do so.
    The way it does it is it first looks for all messages that you have sent.
    It then looks for messages where you are responding to a previous email.
    It then fetches that previous email, and creates a training example
    of that email, followed by your email.

    Note that there are clear limitations here. For example,
    all examples created are only looking at the previous email for context.

    To use:

    - Set up a Google Developer Account:
        Go to the Google Developer Console, create a project,
        and enable the Gmail API for that project.
        This will give you a credentials.json file that you'll need later.
    d   Fcredsnraise_errorr
   Nc                 r    t                                                       || _        || _        || _        d S )N)super__init__rB   rC   rD   )selfrB   rC   rD   	__class__s       r%   rG   zGMailLoader.__init__\   s5    
&r'   c              #     K   ddl m}  |dd| j                  }|                                                                                    ddg| j                                                  }|                    d	g           }|D ]5}	 t          ||          V  # t          $ r}| j        r|Y d }~.d }~ww xY wd S )
Nr   )buildgmailv1)credentialsr*   SENT)r,   labelIds
maxResultsr.   )googleapiclient.discoveryrK   rB   r/   r.   listrC   r1   r0   r;   	ExceptionrD   )rH   rK   r(   resultsr.   r$   es          r%   	lazy_loadzGMailLoader.lazy_loadb   s      333333%4:>>>MMOOXZZTTVTDDWYY	 	 ;;z2.. 	 	G'999999   # GDDDD	 	s   B
B;(	B66B;)rA   F)__name__
__module____qualname____doc__r   intboolrG   r   r   rW   __classcell__)rI   s   @r%   r@   r@   B   s         (' 'c 'c 'd 't ' ' ' ' ' '8K0        r'   r@   )r   r   typingr   r   langchain_core._api.deprecationr   langchain_core.chat_loadersr   langchain_core.chat_sessionsr   langchain_core.messagesr   r&   r;   r@    r'   r%   <module>re      s&    				                 6 6 6 6 6 6 6 6 6 6 6 6 4 4 4 4 4 4 0 0 0 0 0 0     0Ds DS D[ D D D D> 
?  
. . . . .. . . 
. . .r'   