o
    ZhO                     @   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                   @   sr   e Zd ZdZdeeef fddZede	e
 defddZed	ejd
ede	e fddZdee fddZdS )SlackChatLoaderz0Load `Slack` conversations from a dump zip file.pathc                 C   s8   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    r   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_loaders/slack.py__init__   s   	
zSlackChatLoader.__init__messagesreturnc              	   C   s   g }d }| D ]Y}t |tsq|dd}|dd}|dd}|s#qtjdtjd}||r1q||krM|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   )Z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_pathc                 C   sV   |  |d}t|}W d   n1 s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)r,   r-   fdatar   r   r   
_read_jsonC   s   
zSlackChatLoader._read_jsonc                 c   sj    t t| jd!}| D ]}|dr"| ||}| |V  qW d   dS 1 s.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.
        r.   z.jsonN)zipfileZipFilestrr   namelistendswithr7   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+   r8   r9   r!   r7   r   r=   r   r   r   r   r      s    

#r   )r0   loggingr#   r8   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    
