fix:修复发送api打字时间,现已成为可选参数。修复可能存在的回复图片问题,为数据库缺失字段提供警告

This commit is contained in:
SengokuCola
2025-06-14 19:21:02 +08:00
parent e4e14550c7
commit 790642afd6
9 changed files with 37 additions and 9 deletions

View File

@@ -107,6 +107,7 @@ class MessageRecv(Message):
self.processed_plain_text = message_dict.get("processed_plain_text", "") self.processed_plain_text = message_dict.get("processed_plain_text", "")
self.detailed_plain_text = message_dict.get("detailed_plain_text", "") self.detailed_plain_text = message_dict.get("detailed_plain_text", "")
self.is_emoji = False self.is_emoji = False
self.is_picid = False
def update_chat_stream(self, chat_stream: "ChatStream"): def update_chat_stream(self, chat_stream: "ChatStream"):
self.chat_stream = chat_stream self.chat_stream = chat_stream
@@ -134,6 +135,7 @@ class MessageRecv(Message):
elif segment.type == "image": elif segment.type == "image":
# 如果是base64图片数据 # 如果是base64图片数据
if isinstance(segment.data, str): if isinstance(segment.data, str):
self.is_picid = True
image_manager = get_image_manager() image_manager = get_image_manager()
# print(f"segment.data: {segment.data}") # print(f"segment.data: {segment.data}")
_, processed_text = await image_manager.process_image(segment.data) _, processed_text = await image_manager.process_image(segment.data)

View File

@@ -199,11 +199,11 @@ class NormalChat:
# 使用信号量控制并发数最多同时处理5个消息 # 使用信号量控制并发数最多同时处理5个消息
semaphore = asyncio.Semaphore(5) semaphore = asyncio.Semaphore(5)
async def limited_process(task): async def limited_process(task, sem):
async with semaphore: async with sem:
await task await task
limited_tasks = [limited_process(task) for task in tasks] limited_tasks = [limited_process(task, semaphore) for task in tasks]
await asyncio.gather(*limited_tasks, return_exceptions=True) await asyncio.gather(*limited_tasks, return_exceptions=True)
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info(f"[{self.stream_name}] 兴趣监控任务被取消") logger.info(f"[{self.stream_name}] 兴趣监控任务被取消")

View File

@@ -40,6 +40,11 @@ class ClassicalWillingManager(BaseWillingManager):
else: else:
is_emoji_not_reply = True is_emoji_not_reply = True
# 处理picid格式消息直接不回复
is_picid_not_reply = False
if willing_info.is_picid:
is_picid_not_reply = True
self.chat_reply_willing[chat_id] = min(current_willing, 3.0) self.chat_reply_willing[chat_id] = min(current_willing, 3.0)
reply_probability = min(max((current_willing - 0.5), 0.01) * 2, 1) reply_probability = min(max((current_willing - 0.5), 0.01) * 2, 1)
@@ -54,6 +59,9 @@ class ClassicalWillingManager(BaseWillingManager):
if is_emoji_not_reply: if is_emoji_not_reply:
reply_probability = 0 reply_probability = 0
if is_picid_not_reply:
reply_probability = 0
return reply_probability return reply_probability
async def before_generate_reply_handle(self, message_id): async def before_generate_reply_handle(self, message_id):

View File

@@ -180,6 +180,9 @@ class MxpWillingManager(BaseWillingManager):
if w_info.is_emoji: if w_info.is_emoji:
probability *= global_config.normal_chat.emoji_response_penalty probability *= global_config.normal_chat.emoji_response_penalty
if w_info.is_picid:
probability = 0 # picid格式消息直接不回复
if w_info.group_info and w_info.group_info.group_id in global_config.normal_chat.talk_frequency_down_groups: if w_info.group_info and w_info.group_info.group_id in global_config.normal_chat.talk_frequency_down_groups:
probability /= global_config.normal_chat.down_frequency_rate probability /= global_config.normal_chat.down_frequency_rate

View File

@@ -61,6 +61,7 @@ class WillingInfo:
group_info: Optional[GroupInfo] group_info: Optional[GroupInfo]
is_mentioned_bot: bool is_mentioned_bot: bool
is_emoji: bool is_emoji: bool
is_picid: bool
interested_rate: float interested_rate: float
# current_mood: float 当前心情? # current_mood: float 当前心情?
@@ -102,6 +103,7 @@ class BaseWillingManager(ABC):
group_info=chat.group_info, group_info=chat.group_info,
is_mentioned_bot=is_mentioned_bot, is_mentioned_bot=is_mentioned_bot,
is_emoji=message.is_emoji, is_emoji=message.is_emoji,
is_picid=message.is_picid,
interested_rate=interested_rate, interested_rate=interested_rate,
) )

View File

@@ -189,7 +189,7 @@ class Images(BaseModel):
emoji_hash = TextField(index=True) # 图像的哈希值 emoji_hash = TextField(index=True) # 图像的哈希值
description = TextField(null=True) # 图像的描述 description = TextField(null=True) # 图像的描述
path = TextField(unique=True) # 图像文件的路径 path = TextField(unique=True) # 图像文件的路径
base64 = TextField(null=True) # 图片的base64编码(可选) # base64 = TextField() # 图片的base64编码
count = IntegerField(default=1) # 图片被引用的次数 count = IntegerField(default=1) # 图片被引用的次数
timestamp = FloatField() # 时间戳 timestamp = FloatField() # 时间戳
type = TextField() # 图像类型,例如 "emoji" type = TextField() # 图像类型,例如 "emoji"
@@ -401,6 +401,10 @@ def initialize_database():
model_fields = set(model._meta.fields.keys()) model_fields = set(model._meta.fields.keys())
# 检查并添加缺失字段(原有逻辑) # 检查并添加缺失字段(原有逻辑)
missing_fields = model_fields - existing_columns
if missing_fields:
logger.warning(f"'{table_name}' 缺失字段: {missing_fields}")
for field_name, field_obj in model._meta.fields.items(): for field_name, field_obj in model._meta.fields.items():
if field_name not in existing_columns: if field_name not in existing_columns:
logger.info(f"'{table_name}' 缺失字段 '{field_name}',正在添加...") logger.info(f"'{table_name}' 缺失字段 '{field_name}',正在添加...")
@@ -427,11 +431,16 @@ def initialize_database():
alter_sql += f" DEFAULT {int(default_value)}" alter_sql += f" DEFAULT {int(default_value)}"
else: else:
alter_sql += f" DEFAULT {default_value}" alter_sql += f" DEFAULT {default_value}"
db.execute_sql(alter_sql) try:
logger.info(f"字段 '{field_name}' 添加成功") db.execute_sql(alter_sql)
logger.info(f"字段 '{field_name}' 添加成功")
except Exception as e:
logger.error(f"添加字段 '{field_name}' 失败: {e}")
# 检查并删除多余字段(新增逻辑) # 检查并删除多余字段(新增逻辑)
extra_fields = existing_columns - model_fields extra_fields = existing_columns - model_fields
if extra_fields:
logger.warning(f"'{table_name}' 存在多余字段: {extra_fields}")
for field_name in extra_fields: for field_name in extra_fields:
try: try:
logger.warning(f"'{table_name}' 存在多余字段 '{field_name}',正在尝试删除...") logger.warning(f"'{table_name}' 存在多余字段 '{field_name}',正在尝试删除...")

View File

@@ -31,6 +31,7 @@ class MessageAPI:
target_id: str, target_id: str,
is_group: bool = True, is_group: bool = True,
display_message: str = "", display_message: str = "",
typing: bool = False,
) -> bool: ) -> bool:
"""直接向指定目标发送消息 """直接向指定目标发送消息
@@ -113,7 +114,7 @@ class MessageAPI:
) )
# 发送消息 # 发送消息
sent_msg = await heart_fc_sender.send_message(bot_message, has_thinking=True, typing=False, set_reply=False) sent_msg = await heart_fc_sender.send_message(bot_message, has_thinking=False, typing=typing, set_reply=False)
if sent_msg: if sent_msg:
logger.info(f"{getattr(self, 'log_prefix', '')} 成功发送消息到 {platform}:{target_id}") logger.info(f"{getattr(self, 'log_prefix', '')} 成功发送消息到 {platform}:{target_id}")

View File

@@ -137,7 +137,7 @@ class BaseAction(ABC):
text=content, user_id=str(chat_stream.user_info.user_id), platform=chat_stream.platform text=content, user_id=str(chat_stream.user_info.user_id), platform=chat_stream.platform
) )
async def send_type(self, type: str, text: str) -> bool: async def send_type(self, type: str, text: str, typing: bool = False) -> bool:
"""发送回复消息 """发送回复消息
Args: Args:
@@ -159,6 +159,7 @@ class BaseAction(ABC):
platform=chat_stream.platform, platform=chat_stream.platform,
target_id=str(chat_stream.group_info.group_id), target_id=str(chat_stream.group_info.group_id),
is_group=True, is_group=True,
typing=typing,
) )
else: else:
# 私聊 # 私聊
@@ -168,6 +169,7 @@ class BaseAction(ABC):
platform=chat_stream.platform, platform=chat_stream.platform,
target_id=str(chat_stream.user_info.user_id), target_id=str(chat_stream.user_info.user_id),
is_group=False, is_group=False,
typing=typing,
) )
async def send_command(self, command_name: str, args: dict = None, display_message: str = None) -> bool: async def send_command(self, command_name: str, args: dict = None, display_message: str = None) -> bool:

View File

@@ -80,7 +80,7 @@ class BaseCommand(ABC):
text=content, user_id=str(chat_stream.user_info.user_id), platform=chat_stream.platform text=content, user_id=str(chat_stream.user_info.user_id), platform=chat_stream.platform
) )
async def send_type(self, message_type: str, content: str, display_message: str = None) -> bool: async def send_type(self, message_type: str, content: str, display_message: str = None, typing: bool = False) -> bool:
"""发送指定类型的回复消息到当前聊天环境 """发送指定类型的回复消息到当前聊天环境
Args: Args:
@@ -103,6 +103,7 @@ class BaseCommand(ABC):
target_id=str(chat_stream.group_info.group_id), target_id=str(chat_stream.group_info.group_id),
is_group=True, is_group=True,
display_message=display_message, display_message=display_message,
typing=typing,
) )
else: else:
# 私聊 # 私聊