fix:正常获取Bot自身发送的消息id
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from base64 import b64decode
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING
|
||||
|
||||
from src.chat.utils.utils import process_llm_response
|
||||
from src.common.data_models.message_component_data_model import EmojiComponent, MessageSequence, TextComponent
|
||||
@@ -12,10 +12,12 @@ from src.config.config import global_config
|
||||
from src.core.tooling import ToolExecutionResult
|
||||
|
||||
from ..context_messages import SessionBackedMessage
|
||||
from ..message_adapter import format_speaker_content
|
||||
from ..message_adapter import build_visible_text_from_sequence, clone_message_sequence, format_speaker_content
|
||||
from ..planner_message_utils import build_planner_prefix, build_session_backed_text_message
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.chat.message_receive.message import SessionMessage
|
||||
|
||||
from ..reasoning_engine import MaisakaReasoningEngine
|
||||
from ..runtime import MaisakaHeartFlowChatting
|
||||
|
||||
@@ -136,6 +138,57 @@ class BuiltinToolRuntimeContext:
|
||||
|
||||
return self.engine._get_runtime_manager()
|
||||
|
||||
@staticmethod
|
||||
def _build_visible_text_from_sent_message(message: "SessionMessage") -> str:
|
||||
"""将已发送消息转换为 Maisaka 可见文本。"""
|
||||
|
||||
user_info = message.message_info.user_info
|
||||
speaker_name = user_info.user_cardname or user_info.user_nickname or user_info.user_id
|
||||
visible_message_id = None if message.is_notify else message.message_id
|
||||
legacy_sequence = MessageSequence([])
|
||||
legacy_sequence.text(
|
||||
format_speaker_content(
|
||||
speaker_name,
|
||||
"",
|
||||
message.timestamp,
|
||||
visible_message_id,
|
||||
)
|
||||
)
|
||||
for component in clone_message_sequence(message.raw_message).components:
|
||||
legacy_sequence.components.append(component)
|
||||
return build_visible_text_from_sequence(legacy_sequence).strip()
|
||||
|
||||
def append_sent_message_to_chat_history(
|
||||
self,
|
||||
message: "SessionMessage",
|
||||
*,
|
||||
source_kind: str = "guided_reply",
|
||||
) -> None:
|
||||
"""将真实已发送消息同步到 Maisaka 历史。"""
|
||||
|
||||
user_info = message.message_info.user_info
|
||||
speaker_name = user_info.user_cardname or user_info.user_nickname or user_info.user_id
|
||||
planner_prefix = build_planner_prefix(
|
||||
timestamp=message.timestamp,
|
||||
user_name=speaker_name,
|
||||
group_card=user_info.user_cardname or "",
|
||||
message_id=message.message_id,
|
||||
include_message_id=not message.is_notify and bool(message.message_id),
|
||||
)
|
||||
planner_components = clone_message_sequence(message.raw_message).components
|
||||
if planner_components and isinstance(planner_components[0], TextComponent):
|
||||
planner_components[0].text = f"{planner_prefix}{planner_components[0].text}"
|
||||
else:
|
||||
planner_components.insert(0, TextComponent(planner_prefix))
|
||||
|
||||
history_message = SessionBackedMessage.from_session_message(
|
||||
message,
|
||||
raw_message=MessageSequence(planner_components),
|
||||
visible_text=self._build_visible_text_from_sent_message(message),
|
||||
source_kind=source_kind,
|
||||
)
|
||||
self.runtime._chat_history.append(history_message)
|
||||
|
||||
def append_guided_reply_to_chat_history(self, reply_text: str) -> None:
|
||||
"""将引导回复写回 Maisaka 历史。"""
|
||||
|
||||
|
||||
@@ -144,13 +144,14 @@ async def handle_tool(
|
||||
combined_reply_text = "".join(reply_segments)
|
||||
try:
|
||||
sent = False
|
||||
sent_messages = []
|
||||
if tool_ctx.runtime.chat_stream.platform == CLI_PLATFORM_NAME:
|
||||
for segment in reply_segments:
|
||||
render_cli_message(segment)
|
||||
sent = True
|
||||
else:
|
||||
for index, segment in enumerate(reply_segments):
|
||||
sent = await send_service.text_to_stream(
|
||||
sent_message = await send_service.text_to_stream_with_message(
|
||||
text=segment,
|
||||
stream_id=tool_ctx.runtime.session_id,
|
||||
set_reply=set_quote if index == 0 else False,
|
||||
@@ -158,8 +159,10 @@ async def handle_tool(
|
||||
selected_expressions=reply_result.selected_expression_ids or None,
|
||||
typing=index > 0,
|
||||
)
|
||||
sent = sent_message is not None
|
||||
if not sent:
|
||||
break
|
||||
sent_messages.append(sent_message)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
f"{tool_ctx.runtime.log_prefix} 发送文字消息时发生异常,目标消息编号={target_message_id}"
|
||||
@@ -183,7 +186,11 @@ async def handle_tool(
|
||||
target_user_info = target_message.message_info.user_info
|
||||
target_user_name = target_user_info.user_cardname or target_user_info.user_nickname or target_user_info.user_id
|
||||
|
||||
tool_ctx.append_guided_reply_to_chat_history(combined_reply_text)
|
||||
if tool_ctx.runtime.chat_stream.platform == CLI_PLATFORM_NAME:
|
||||
tool_ctx.append_guided_reply_to_chat_history(combined_reply_text)
|
||||
else:
|
||||
for sent_message in sent_messages:
|
||||
tool_ctx.append_sent_message_to_chat_history(sent_message)
|
||||
return tool_ctx.build_success_result(
|
||||
invocation.tool_name,
|
||||
"回复已生成并发送。",
|
||||
|
||||
@@ -346,10 +346,13 @@ async def handle_tool(
|
||||
f"描述={send_result.description!r} 情绪标签={send_result.emotions} "
|
||||
f"请求情绪={emotion!r} 命中情绪={send_result.matched_emotion!r}"
|
||||
)
|
||||
tool_ctx.append_sent_emoji_to_chat_history(
|
||||
emoji_base64=send_result.emoji_base64,
|
||||
success_message=_EMOJI_SUCCESS_MESSAGE,
|
||||
)
|
||||
if send_result.sent_message is not None:
|
||||
tool_ctx.append_sent_message_to_chat_history(send_result.sent_message)
|
||||
else:
|
||||
tool_ctx.append_sent_emoji_to_chat_history(
|
||||
emoji_base64=send_result.emoji_base64,
|
||||
success_message=_EMOJI_SUCCESS_MESSAGE,
|
||||
)
|
||||
structured_result["success"] = True
|
||||
return tool_ctx.build_success_result(
|
||||
invocation.tool_name,
|
||||
|
||||
Reference in New Issue
Block a user