U
    hO                     @   s   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eZG dd deZdS )	    N)Path)DictIteratorListUnion)BaseChatLoader)ChatSession)	AIMessageHumanMessagec                   @   sl   e Zd ZdZeeef dddZee	e
 edddZeejee	e dd	d
Zee dddZdS )SlackChatLoaderz0Load `Slack` conversations from a dump zip file.)pathc                 C   s8   t |tr|nt|| _| j s4t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    r   J/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/chat_loaders/slack.py__init__   s    	
zSlackChatLoader.__init__)messagesreturnc              	   C   s   g }d }| D ]}t |tsq|dd}|dd}|dd}|sFqtjdtjd}||rbq||kr|d  jd| 7  _|d jd	 	d
|i n |	t
|||d
|igdd |}qt|dS )Ntext tsuserz<@U\d+> has joined the channel)flagsz

eventsZmessage_time)senderr   )rolecontentadditional_kwargs)r   )r   dictgetrecompile
IGNORECASEmatchr    r!   appendr
   r   )r   resultsZprevious_sendermessager   	timestampr   Zskip_patternr   r   r   _load_single_chat_session   s@    
 

z)SlackChatLoader._load_single_chat_session)zip_file	file_pathr   c              	   C   sB   |  |d}t|}W 5 Q R X t|ts>tdt| |S )z"Read JSON data from a zip subfile.rz#Expected list of dictionaries, got )openjsonloadr   list
ValueErrortype)r-   r.   fdatar   r   r   
_read_jsonC   s
    
zSlackChatLoader._read_json)r   c              	   c   sR   t t| jd6}| D ]&}|dr| ||}| |V  qW 5 Q R X 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.
        r/   z.jsonN)zipfileZipFilestrr   namelistendswithr8   r,   )r   r-   r.   r   r   r   r   	lazy_loadL   s
    
zSlackChatLoader.lazy_loadN)__name__
__module____qualname____doc__r   r;   r   r   staticmethodr   r   r   r,   r9   r:   r"   r8   r   r>   r   r   r   r   r      s   
#r   )r1   loggingr$   r9   pathlibr   typingr   r   r   r   Zlangchain_core.chat_loadersr   Zlangchain_core.chat_sessionsr   Zlangchain_core.messagesr	   r
   	getLoggerr?   loggerr   r   r   r   r   <module>   s   
