diff --git a/src/chat/message_receive/storage.py b/src/chat/message_receive/storage.py index 23afe6c87..146a4372a 100644 --- a/src/chat/message_receive/storage.py +++ b/src/chat/message_receive/storage.py @@ -142,23 +142,24 @@ class MessageStorage: def replace_image_descriptions(text: str) -> str: """将[图片:描述]替换为[picid:image_id]""" # 先检查文本中是否有图片标记 - pattern = r'\[图片:([^\]]+)\]' + pattern = r"\[图片:([^\]]+)\]" matches = re.findall(pattern, text) - + if not matches: logger.debug("文本中没有图片标记,直接返回原文本") return text + def replace_match(match): description = match.group(1).strip() try: - image_record = (Images.select() - .where(Images.description == description) - .order_by(Images.timestamp.desc()) - .first()) + image_record = ( + Images.select().where(Images.description == description).order_by(Images.timestamp.desc()).first() + ) if image_record: return f"[picid:{image_record.image_id}]" - else: - return match.group(0) # 保持原样 - except Exception as e: + else: + return match.group(0) # 保持原样 + except Exception: return match.group(0) - return re.sub(r'\[图片:([^\]]+)\]', replace_match, text) + + return re.sub(r"\[图片:([^\]]+)\]", replace_match, text) diff --git a/src/chat/utils/utils_image.py b/src/chat/utils/utils_image.py index eed65ad88..17cfb2323 100644 --- a/src/chat/utils/utils_image.py +++ b/src/chat/utils/utils_image.py @@ -187,12 +187,12 @@ class ImageManager: existing_image = Images.get_or_none(Images.emoji_hash == image_hash) if existing_image: # 更新计数 - if hasattr(existing_image, 'count') and existing_image.count is not None: + if hasattr(existing_image, "count") and existing_image.count is not None: existing_image.count += 1 else: existing_image.count = 1 existing_image.save() - + # 如果已有描述,直接返回 if existing_image.description: return f"[图片:{existing_image.description}]" @@ -229,9 +229,9 @@ class ImageManager: existing_image.path = file_path existing_image.description = description existing_image.timestamp = current_timestamp - if not hasattr(existing_image, 'image_id') or not existing_image.image_id: + if not hasattr(existing_image, "image_id") or not existing_image.image_id: existing_image.image_id = str(uuid.uuid4()) - if not hasattr(existing_image, 'vlm_processed') or existing_image.vlm_processed is None: + if not hasattr(existing_image, "vlm_processed") or existing_image.vlm_processed is None: existing_image.vlm_processed = True existing_image.save() else: