feature(fix?): 在发送消息之前统一检查消息内容是否为空, 不允许发送空消息, 以解决该消息内容不支持查看以及gemini返回<empty content>问题

This commit is contained in:
anka
2025-04-03 11:50:12 +08:00
parent 2fc62efd88
commit 48d07af574
2 changed files with 24 additions and 3 deletions
+21
View File
@@ -72,6 +72,20 @@ class RespondStage(Stage):
# random
return random.uniform(self.interval[0], self.interval[1])
async def _is_empty_message_chain(self, chain):
"""检查消息链是否为空
Args:
chain (MessageChain): 消息链
"""
for comp in chain:
if isinstance(comp, Plain):
if comp.text.strip():
return False
else:
return True
return True
async def process(
self, event: AstrMessageEvent
) -> Union[None, AsyncGenerator[None, None]]:
@@ -82,6 +96,13 @@ class RespondStage(Stage):
if len(result.chain) > 0:
await event._pre_send()
# 检查消息链是否为空
if await self._is_empty_message_chain(result.chain):
logger.info("消息为空,跳过发送阶段")
event.clear_result()
event.stop_event()
return
if self.enable_seg and (
(self.only_llm_result and result.is_llm_result())
or not self.only_llm_result
@@ -127,7 +127,7 @@ class ProviderGoogleGenAI(Provider):
if message["role"] == "user":
if isinstance(message["content"], str):
if not message["content"]:
message["content"] = "<empty_content>"
message["content"] = ""
google_genai_conversation.append(
{"role": "user", "parts": [{"text": message["content"]}]}
@@ -138,7 +138,7 @@ class ProviderGoogleGenAI(Provider):
for part in message["content"]:
if part["type"] == "text":
if not part["text"]:
part["text"] = "<empty_content>"
part["text"] = ""
parts.append({"text": part["text"]})
elif part["type"] == "image_url":
parts.append(
@@ -156,7 +156,7 @@ class ProviderGoogleGenAI(Provider):
elif message["role"] == "assistant":
if "content" in message:
if not message["content"]:
message["content"] = "<empty_content>"
message["content"] = ""
google_genai_conversation.append(
{"role": "model", "parts": [{"text": message["content"]}]}
)