
    NgO                         d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ  ej        e          Z G d de          ZdS )	    N)Path)DictIteratorListUnion)BaseChatLoader)ChatSession)	AIMessageHumanMessagec                       e Zd ZdZdeeef         fdZede	e
         defd            Zedej        dede	e         fd	            Zdee         fd
ZdS )SlackChatLoaderz0Load `Slack` conversations from a dump zip file.pathc                     t          |t                    r|nt          |          | _        | j                                        st	          d| j         d          dS )z
        Initialize the chat loader with the path to the exported Slack dump zip file.

        :param path: Path to the exported Slack dump zip file.
        zFile z
 not foundN)
isinstancer   zip_pathexistsFileNotFoundError)selfr   s     b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_loaders/slack.py__init__zSlackChatLoader.__init__   sc     !+4 6 6FDJJ}##%% 	G#$EDM$E$E$EFFF	G 	G    messagesreturnc           
      D   g }d }| D ]	}t          |t                    s|                    dd          }|                    dd          }|                    dd          }|s^t          j        dt          j                  }|                    |          r||k    rB|d         xj        d|z   z  c_        |d         j        d	         	                    d
|i           n+|	                    t          |||d
|igd                     |}t          |          S )Ntext tsuserz<@U\d+> has joined the channel)flagsz

eventsmessage_time)senderr!   )rolecontentadditional_kwargs)r   )r   dictgetrecompile
IGNORECASEmatchr%   r&   appendr   r	   )r   resultsprevious_sendermessager   	timestampr#   skip_patterns           r   _load_single_chat_sessionz)SlackChatLoader._load_single_chat_session   sf   8: 	% 	%Ggt,, ;;vr**DD"--I[[,,F :1  L !!$'' ((##v}4##-h7>>#Y/     # $&,(6	'B&C+ +  	 	 	 %OOG,,,,r   zip_file	file_pathc                     |                      |d          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |t                    st          dt          |                     |S )z"Read JSON data from a zip subfile.rNz#Expected list of dictionaries, got )openjsonloadr   list
ValueErrortype)r4   r5   fdatas       r   
_read_jsonzSlackChatLoader._read_jsonC   s     ]]9c** 	 a9Q<<D	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 $%% 	QO4::OOPPPs   8<<c              #   >  K   t          j        t          | j                  d          5 }|                                D ]D}|                    d          r-|                     ||          }|                     |          V  E	 ddd           dS # 1 swxY w Y   dS )z
        Lazy load the chat sessions from the Slack dump file and yield them
        in the required format.

        :return: Iterator of chat sessions containing messages.
        r7   z.jsonN)zipfileZipFilestrr   namelistendswithr@   r3   )r   r4   r5   r   s       r   	lazy_loadzSlackChatLoader.lazy_loadL   s       _S//55 	C%..00 C C	%%g.. C#xCCH88BBBBBC	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cs   ABBBN)__name__
__module____qualname____doc__r   rD   r   r   staticmethodr   r   r	   r3   rB   rC   r'   r@   r   rG    r   r   r   r      s        ::GCIG G G G !-DJ !-; !- !- !- \!-F W_  d    \C8K0 C C C C C Cr   r   )r9   loggingr)   rB   pathlibr   typingr   r   r   r   langchain_core.chat_loadersr   langchain_core.chat_sessionsr	   langchain_core.messagesr
   r   	getLoggerrH   loggerr   rM   r   r   <module>rV      s      				        . . . . . . . . . . . . 6 6 6 6 6 6 4 4 4 4 4 4 ; ; ; ; ; ; ; ;		8	$	$HC HC HC HC HCn HC HC HC HC HCr   