
    7|hO                         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y)	    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y)SlackChatLoaderz0Load `Slack` conversations from a dump zip file.pathc                     t        |t              r|n
t        |      | _        | j                  j                         st	        d| j                   d      y)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     e/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/chat_loaders/slack.py__init__zSlackChatLoader.__init__   sG     !+4 6DJ}}##%#eDMM?*$EFF &    messagesreturnc           
         g }d }| D ]  }t        |t              s|j                  dd      }|j                  dd      }|j                  dd      }|sMt        j                  dt        j
                        }|j                  |      r||k(  r?|d   xj                  d|z   z  c_        |d   j                  d	   j                  d
|i       n#|j                  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   s   8: 	%Ggt,;;vr*DD"-I[[,F::1L !!$'(##v}4#--h7>>#Y/  # $&,(6	'B&C+	 %O;	%< G,,r   zip_file	file_pathc                     | j                  |d      5 }t        j                  |      }ddd       t        t              st        dt        |             |S # 1 sw Y   2xY w)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99Q<D	 $%B4:,OPP		  	 s   AA#c              #   "  K   t        j                  t        | j                        d      5 }|j	                         D ]9  }|j                  d      s| j                  ||      }| j                  |       ; 	 ddd       y# 1 sw Y   yxY ww)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 	C%..0 C	%%g.#xCH88BBC	C 	C 	Cs"   *B%B'B:	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 !-DJ !-; !- !-F W__  d  C8K0 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      sE      	   . . 6 4 ;			8	$HCn HCr   