From c6df820164e045175dfa2cb22d64877c261b326c Mon Sep 17 00:00:00 2001 From: Raila23 <3271405327@qq.com> Date: Sat, 12 Apr 2025 15:34:35 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E7=9A=84=E6=B6=88=E6=81=AF=EF=BC=8C=E6=AF=94?= =?UTF-8?q?=E5=AE=9E=E9=99=85=E4=B8=8A=E6=9C=9F=E6=9C=9B=E7=9A=84=E5=A4=9A?= =?UTF-8?q?1=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/pipeline/process_stage/method/llm_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astrbot/core/pipeline/process_stage/method/llm_request.py b/astrbot/core/pipeline/process_stage/method/llm_request.py index 617e458c8..90c7d6256 100644 --- a/astrbot/core/pipeline/process_stage/method/llm_request.py +++ b/astrbot/core/pipeline/process_stage/method/llm_request.py @@ -146,7 +146,7 @@ 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 : ] # session_id From 2fe1f2060a61a22c5565cab3c08d1e2828e873f5 Mon Sep 17 00:00:00 2001 From: Raila23 <3271405327@qq.com> Date: Sat, 12 Apr 2025 16:26:02 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=B7=A5=E5=85=B7=E6=88=96=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E6=9C=AA=E7=9F=A5=E6=83=85=E5=86=B5=EF=BC=8C=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=AF=BC=E8=87=B4400=20BadRequestError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/pipeline/process_stage/method/llm_request.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/astrbot/core/pipeline/process_stage/method/llm_request.py b/astrbot/core/pipeline/process_stage/method/llm_request.py index 90c7d6256..fd70275d8 100644 --- a/astrbot/core/pipeline/process_stage/method/llm_request.py +++ b/astrbot/core/pipeline/process_stage/method/llm_request.py @@ -148,6 +148,10 @@ class LLMRequestSubStage(Stage): req.contexts = req.contexts[ -(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: From c9079b929936bd9f0a6be46e84223a535d3e290d Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Sun, 13 Apr 2025 06:06:02 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E9=A3=9E?= =?UTF-8?q?=E4=B9=A6=E9=80=82=E9=85=8D=E5=99=A8=E8=BD=AC=E6=8D=A2=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=BF=87=E7=A8=8B=E4=B8=AD=E6=97=A0=E6=B3=95=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=BD=AC=E5=8C=96Base64=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/sources/lark/lark_event.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/astrbot/core/platform/sources/lark/lark_event.py b/astrbot/core/platform/sources/lark/lark_event.py index 544a7a5be..fdb873ce3 100644 --- a/astrbot/core/platform/sources/lark/lark_event.py +++ b/astrbot/core/platform/sources/lark/lark_event.py @@ -1,6 +1,8 @@ import json import uuid +import base64 import lark_oapi as lark +from io import BytesIO from typing import List from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.message_components import Plain, Image as AstrBotImage, At @@ -27,22 +29,29 @@ 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[9:] + image_data = base64.b64decode(base64_str) + image_file = 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() @@ -51,7 +60,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() From 9470ca6bc503d719ea8fccd4a1bb98a9b318197f Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Sun, 13 Apr 2025 11:36:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/utils/shared_preferences.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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): From 83745f83a52e834690a40390e5ff4ee06bfe5781 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 13 Apr 2025 15:29:56 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E5=AF=B9=E9=A3=9E?= =?UTF-8?q?=E4=B9=A6=E9=80=82=E9=85=8D=E5=99=A8=20base64=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=95=B0=E6=8D=AE=E5=85=88=E4=BF=9D=E5=AD=98=E5=88=B0?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/message/components.py | 2 ++ astrbot/core/pipeline/respond/stage.py | 1 + astrbot/core/platform/sources/lark/lark_event.py | 7 +++++-- 3 files changed, 8 insertions(+), 2 deletions(-) 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/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 fdb873ce3..b1aee548e 100644 --- a/astrbot/core/platform/sources/lark/lark_event.py +++ b/astrbot/core/platform/sources/lark/lark_event.py @@ -37,9 +37,12 @@ class LarkMessageEvent(AstrMessageEvent): image_file_path = await download_image_by_url(comp.file) file_path = image_file_path elif comp.file and comp.file.startswith("base64://"): - base64_str = comp.file[9:] + base64_str = comp.file.removeprefix("base64://") image_data = base64.b64decode(base64_str) - image_file = BytesIO(image_data).getvalue() + # 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