diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index 7a6284a83..781dfafca 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -193,6 +193,7 @@ class Record(BaseMessageComponent): bs64_data = file_to_base64(self.file) else: raise Exception(f"not a valid file: {self.file}") + bs64_data = bs64_data.removeprefix("base64://") return bs64_data @@ -397,6 +398,7 @@ class Image(BaseMessageComponent): bs64_data = file_to_base64(url) else: raise Exception(f"not a valid file: {url}") + bs64_data = bs64_data.removeprefix("base64://") return bs64_data diff --git a/astrbot/core/pipeline/process_stage/method/llm_request.py b/astrbot/core/pipeline/process_stage/method/llm_request.py index 617e458c8..fd70275d8 100644 --- a/astrbot/core/pipeline/process_stage/method/llm_request.py +++ b/astrbot/core/pipeline/process_stage/method/llm_request.py @@ -146,8 +146,12 @@ class LLMRequestSubStage(Stage): ): logger.debug("上下文长度超过限制,将截断。") req.contexts = req.contexts[ - -(self.max_context_length - self.dequeue_context_length) * 2 : + -(self.max_context_length - self.dequeue_context_length + 1) * 2 : ] + # 找到第一个role 为 user 的索引,确保上下文格式正确 + index = next((i for i, item in enumerate(req.contexts) if item.get("role") == "user"), None) + if index is not None and index > 0: + req.contexts = req.contexts[index:] # session_id if not req.session_id: diff --git a/astrbot/core/pipeline/respond/stage.py b/astrbot/core/pipeline/respond/stage.py index 4f8db2623..be13370f4 100644 --- a/astrbot/core/pipeline/respond/stage.py +++ b/astrbot/core/pipeline/respond/stage.py @@ -202,6 +202,7 @@ class RespondStage(Stage): try: await event.send(result) except Exception as e: + logger.error(traceback.format_exc()) logger.error(f"发送消息失败: {e} chain: {result.chain}") await event._post_send() logger.info( diff --git a/astrbot/core/platform/sources/lark/lark_event.py b/astrbot/core/platform/sources/lark/lark_event.py index ae0d55ab7..48c7c8d14 100644 --- a/astrbot/core/platform/sources/lark/lark_event.py +++ b/astrbot/core/platform/sources/lark/lark_event.py @@ -2,7 +2,10 @@ import asyncio import json import re import uuid +import base64 import lark_oapi as lark + +from io import BytesIO from typing import List, AsyncGenerator from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.message_components import Plain, Image as AstrBotImage, At @@ -29,22 +32,32 @@ class LarkMessageEvent(AstrMessageEvent): _stage.append({"tag": "at", "user_id": comp.qq, "style": []}) elif isinstance(comp, AstrBotImage): file_path = "" + image_file = None + if comp.file and comp.file.startswith("file:///"): file_path = comp.file.replace("file:///", "") elif comp.file and comp.file.startswith("http"): image_file_path = await download_image_by_url(comp.file) file_path = image_file_path elif comp.file and comp.file.startswith("base64://"): - pass + base64_str = comp.file.removeprefix("base64://") + image_data = base64.b64decode(base64_str) + # save as temp file + file_path = f"data/temp/{uuid.uuid4()}_test.jpg" + with open(file_path, "wb") as f: + f.write(BytesIO(image_data).getvalue()) else: file_path = comp.file + if image_file is None: + image_file = open(file_path, "rb") + request = ( CreateImageRequest.builder() .request_body( CreateImageRequestBody.builder() .image_type("message") - .image(open(file_path, "rb")) + .image(image_file) .build() ) .build() @@ -53,7 +66,7 @@ class LarkMessageEvent(AstrMessageEvent): if not response.success(): logger.error(f"无法上传飞书图片({response.code}): {response.msg}") image_key = response.data.image_key - print(image_key) + logger.debug(image_key) ret.append(_stage) ret.append([{"tag": "img", "image_key": image_key}]) _stage.clear() diff --git a/astrbot/core/utils/shared_preferences.py b/astrbot/core/utils/shared_preferences.py index fc0bf7434..33a681419 100644 --- a/astrbot/core/utils/shared_preferences.py +++ b/astrbot/core/utils/shared_preferences.py @@ -8,11 +8,12 @@ class SharedPreferences: self._data = self._load_preferences() def _load_preferences(self): - try: - with open(self.path, "r") as f: - return json.load(f) - except json.JSONDecodeError: - os.remove(self.path) + if os.path.exists(self.path): + try: + with open(self.path, "r") as f: + return json.load(f) + except json.JSONDecodeError: + os.remove(self.path) return {} def _save_preferences(self):