Merge branch 'master' into better-stream

This commit is contained in:
渡鸦95676
2025-04-13 16:47:56 +08:00
committed by GitHub
5 changed files with 30 additions and 9 deletions
+2
View File
@@ -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
@@ -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:
+1
View File
@@ -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(
@@ -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()
+6 -5
View File
@@ -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):