o
    i8                     @   s  U d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ d	Zd
ZdZee jd d d ZdZdZeeZe	eef ed< e Z de!fddZ"de#e$ de!fddZ%dede#e$ de#e$ fddZ&de$ddfddZ'dS )    )defaultdictN)Path)Lock)DefaultDict)context)logger)ChatRequestGenerationConfigLLMMessage(   2   zdeepseek-v4-flashllmpromptszmemory_point_gen_prompt.mdzhttps://api.deepseek.com   _group_message_counterreturnc                   C   s   t jdd S )Nzutf-8)encoding)MEMORY_PROMPT_PATH	read_textstrip r   r   7/home/dore/onebot/Bot_Ero/core/group_memory_pipeline.py_load_memory_prompt   s   r   recordsc                 C   s&   dd | D }d dd |D  S )Nc                 S   s   g | ]}t |qS r   )runtime_contextformat_chat_record).0recordr   r   r   
<listcomp>   s    z*_build_chat_plain_text.<locals>.<listcomp>
c                 s   s    | ]}|r|V  qd S )Nr   )r   liner   r   r   	<genexpr>   s    z)_build_chat_plain_text.<locals>.<genexpr>)joinr   )r   linesr   r   r   _build_chat_plain_text   s   r%   group_idc                 C   s  |sg S t dd }|std g S zt }W n ty5 } ztd| | g W  Y d}~S d}~ww t|}|s>g S | d| }t	d| | t
|td}td	|d
g}ttdd}	d}
tdtd D ]a}z|t||	d}W n ty } ztd| |t| W Y d}~qhd}~ww |j }
t	d| ||
 zt|
}t|tr|W   S td| |t W qh tjy   td| |t Y qhw td| |
 g S )u9   提取群聊记忆点，返回解析后的 JSON 数组。DEEPSEEK_API_KEY u?   群聊记忆提取跳过：缺少环境变量 SIFLOW_API_KEY。u;   群 %s 记忆提取失败：读取 prompt 文件异常：%sNz

u   群 %s 记忆提取 prompt:
%s)api_keybase_urluser)rolecontenti   )model
max_tokens   )messagesconfigu>   群 %s 记忆提取调用失败，准备重试（%s/%s）：%su'   群 %s 记忆提取结果(第%s次):
%suD   群 %s 记忆提取返回非数组 JSON，准备重试（%s/%s）。uB   群 %s 记忆提取 JSON 解析失败，准备重试（%s/%s）。uB   群 %s 记忆提取失败：超过重试次数。最后返回：%s)osgetenvr   r   warningr   	Exceptionerrorr%   infor
   MEMORY_LLM_BASE_URLr   r	   MEMORY_LLM_MODELrangeMEMORY_MAX_RETRIESchatr   messagejsonloads
isinstancelistJSONDecodeError)r&   r   r)   prompt_prefixexc	chat_textpromptr   r1   r2   	last_textattemptresponseparsedr   r   r   extract_group_memory_points"   sf   


	


rL   r   c                 C   s   |  ddkr	dS |  ddkrdS |  d}|du rdS t|}d}t t|  d7  < t| t d	kr8d
}W d   n1 sBw   Y  |rWtj|td}t|| dS dS )u   
    插件执行后调用：
    - 仅处理群聊 message 事件
    - 每累计 40 条消息触发一次回调
    - 回调入参为：该群最新 50 条消息
    	post_typer>   Nmessage_typegroupr&   Fr0   r   T)limit)	getint_counter_lockr   MEMORY_BATCH_SIZEr   get_recent_chat_recordsMEMORY_CONTEXT_SIZErL   )r   r&   gidshould_triggerpayloadr   r   r   after_pluginsY   s&   
rZ   )(collectionsr   r?   r3   pathlibr   	threadingr   typingr   corer   r   core.loggerr   core.llm.llmr   r	   r
   r   rT   rV   r:   __file__resolveparentr   r9   r<   rR   r   __annotations__rS   strr   rB   dictr%   rL   rZ   r   r   r   r   <module>   s,    7