feat:优化工具调用的得到的信息调用和缓存
This commit is contained in:
@@ -31,19 +31,21 @@ class Message(MessageBase):
|
||||
def __init__(
|
||||
self,
|
||||
message_id: str,
|
||||
timestamp: float,
|
||||
chat_stream: ChatStream,
|
||||
user_info: UserInfo,
|
||||
message_segment: Optional[Seg] = None,
|
||||
timestamp: Optional[float] = None,
|
||||
reply: Optional["MessageRecv"] = None,
|
||||
detailed_plain_text: str = "",
|
||||
processed_plain_text: str = "",
|
||||
):
|
||||
# 使用传入的时间戳或当前时间
|
||||
current_timestamp = timestamp if timestamp is not None else round(time.time(), 3)
|
||||
# 构造基础消息信息
|
||||
message_info = BaseMessageInfo(
|
||||
platform=chat_stream.platform,
|
||||
message_id=message_id,
|
||||
time=timestamp,
|
||||
time=current_timestamp,
|
||||
group_info=chat_stream.group_info,
|
||||
user_info=user_info,
|
||||
)
|
||||
@@ -164,11 +166,12 @@ class MessageProcessBase(Message):
|
||||
message_segment: Optional[Seg] = None,
|
||||
reply: Optional["MessageRecv"] = None,
|
||||
thinking_start_time: float = 0,
|
||||
timestamp: Optional[float] = None,
|
||||
):
|
||||
# 调用父类初始化
|
||||
# 调用父类初始化,传递时间戳
|
||||
super().__init__(
|
||||
message_id=message_id,
|
||||
timestamp=round(time.time(), 3), # 保留3位小数
|
||||
timestamp=timestamp,
|
||||
chat_stream=chat_stream,
|
||||
user_info=bot_user_info,
|
||||
message_segment=message_segment,
|
||||
@@ -238,8 +241,9 @@ class MessageThinking(MessageProcessBase):
|
||||
bot_user_info: UserInfo,
|
||||
reply: Optional["MessageRecv"] = None,
|
||||
thinking_start_time: float = 0,
|
||||
timestamp: Optional[float] = None,
|
||||
):
|
||||
# 调用父类初始化
|
||||
# 调用父类初始化,传递时间戳
|
||||
super().__init__(
|
||||
message_id=message_id,
|
||||
chat_stream=chat_stream,
|
||||
@@ -247,6 +251,7 @@ class MessageThinking(MessageProcessBase):
|
||||
message_segment=None, # 思考状态不需要消息段
|
||||
reply=reply,
|
||||
thinking_start_time=thinking_start_time,
|
||||
timestamp=timestamp,
|
||||
)
|
||||
|
||||
# 思考状态特有属性
|
||||
|
||||
@@ -107,53 +107,6 @@ async def get_embedding(text, request_type="embedding"):
|
||||
return embedding
|
||||
|
||||
|
||||
async def get_recent_group_messages(chat_id: str, limit: int = 12) -> list:
|
||||
"""从数据库获取群组最近的消息记录
|
||||
|
||||
Args:
|
||||
chat_id: 群组ID
|
||||
limit: 获取消息数量,默认12条
|
||||
|
||||
Returns:
|
||||
list: Message对象列表,按时间正序排列
|
||||
"""
|
||||
|
||||
# 从数据库获取最近消息
|
||||
recent_messages = list(
|
||||
db.messages.find(
|
||||
{"chat_id": chat_id},
|
||||
)
|
||||
.sort("time", -1)
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
if not recent_messages:
|
||||
return []
|
||||
|
||||
# 转换为 Message对象列表
|
||||
message_objects = []
|
||||
for msg_data in recent_messages:
|
||||
try:
|
||||
chat_info = msg_data.get("chat_info", {})
|
||||
chat_stream = ChatStream.from_dict(chat_info)
|
||||
user_info = msg_data.get("user_info", {})
|
||||
user_info = UserInfo.from_dict(user_info)
|
||||
msg = Message(
|
||||
message_id=msg_data["message_id"],
|
||||
chat_stream=chat_stream,
|
||||
timestamp=msg_data["time"],
|
||||
user_info=user_info,
|
||||
processed_plain_text=msg_data.get("processed_text", ""),
|
||||
detailed_plain_text=msg_data.get("detailed_plain_text", ""),
|
||||
)
|
||||
message_objects.append(msg)
|
||||
except KeyError:
|
||||
logger.warning("数据库中存在无效的消息")
|
||||
continue
|
||||
|
||||
# 按时间正序排列
|
||||
message_objects.reverse()
|
||||
return message_objects
|
||||
|
||||
|
||||
def get_recent_group_detailed_plain_text(chat_stream_id: str, limit: int = 12, combine=False):
|
||||
|
||||
@@ -264,9 +264,6 @@ class HeartFChatting:
|
||||
logger.error(f"[HFC:{self.stream_id}] 获取ChatStream时出错 in _initialize: {e}")
|
||||
return False
|
||||
|
||||
logger.debug(
|
||||
f"{self.log_prefix} HeartFChatting initialized: is_group={self.is_group_chat}, target_info={self.chat_target_info}"
|
||||
)
|
||||
# --- End using utility function ---
|
||||
|
||||
self._initialized = True
|
||||
@@ -861,7 +858,7 @@ class HeartFChatting:
|
||||
cycle_history=self._cycle_history, # <-- Pass HFC state
|
||||
observed_messages_str=observed_messages_str, # <-- Pass local variable
|
||||
current_mind=current_mind, # <-- Pass argument
|
||||
structured_info=self.sub_mind.structured_info, # <-- Pass SubMind info
|
||||
structured_info=self.sub_mind.structured_info_str, # <-- Pass SubMind info
|
||||
current_available_actions=current_available_actions, # <-- Pass determined actions
|
||||
)
|
||||
|
||||
@@ -1301,7 +1298,7 @@ class HeartFChatting:
|
||||
# Focus specific args:
|
||||
reason=reason,
|
||||
current_mind_info=self.sub_mind.current_mind,
|
||||
structured_info=self.sub_mind.structured_info,
|
||||
structured_info=self.sub_mind.structured_info_str,
|
||||
sender_name=sender_name_for_prompt, # Pass determined name
|
||||
# Normal specific args (not used in focus mode):
|
||||
# message_txt="",
|
||||
|
||||
@@ -63,15 +63,12 @@ class NormalChat:
|
||||
self.is_group_chat, self.chat_target_info = await get_chat_type_and_target_info(self.stream_id)
|
||||
# Update stream_name again after potential async call in util func
|
||||
self.stream_name = chat_manager.get_stream_name(self.stream_id) or self.stream_id
|
||||
logger.debug(
|
||||
f"[{self.stream_name}] NormalChat initialized: is_group={self.is_group_chat}, target_info={self.chat_target_info}"
|
||||
)
|
||||
# --- End using utility function ---
|
||||
self._initialized = True
|
||||
logger.info(f"[{self.stream_name}] NormalChat 实例 initialize 完成 (异步部分)。")
|
||||
|
||||
# 改为实例方法
|
||||
async def _create_thinking_message(self, message: MessageRecv) -> str:
|
||||
async def _create_thinking_message(self, message: MessageRecv, timestamp: Optional[float] = None) -> str:
|
||||
"""创建思考消息"""
|
||||
messageinfo = message.message_info
|
||||
|
||||
@@ -85,15 +82,15 @@ class NormalChat:
|
||||
thinking_id = "mt" + str(thinking_time_point)
|
||||
thinking_message = MessageThinking(
|
||||
message_id=thinking_id,
|
||||
chat_stream=self.chat_stream, # 使用 self.chat_stream
|
||||
chat_stream=self.chat_stream,
|
||||
bot_user_info=bot_user_info,
|
||||
reply=message,
|
||||
thinking_start_time=thinking_time_point,
|
||||
timestamp=timestamp if timestamp is not None else None
|
||||
)
|
||||
|
||||
await message_manager.add_message(thinking_message)
|
||||
return thinking_id
|
||||
|
||||
# 改为实例方法
|
||||
async def _add_messages_to_manager(
|
||||
self, message: MessageRecv, response_set: List[str], thinking_id
|
||||
@@ -209,7 +206,7 @@ class NormalChat:
|
||||
try:
|
||||
# 处理消息
|
||||
await self.normal_response(
|
||||
message=message, is_mentioned=is_mentioned, interested_rate=interest_value
|
||||
message=message, is_mentioned=is_mentioned, interested_rate=interest_value, rewind_response = False
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 处理兴趣消息{msg_id}时出错: {e}\n{traceback.format_exc()}")
|
||||
@@ -217,7 +214,7 @@ class NormalChat:
|
||||
self.interest_dict.pop(msg_id, None)
|
||||
|
||||
# 改为实例方法, 移除 chat 参数
|
||||
async def normal_response(self, message: MessageRecv, is_mentioned: bool, interested_rate: float) -> None:
|
||||
async def normal_response(self, message: MessageRecv, is_mentioned: bool, interested_rate: float, rewind_response: bool = False) -> None:
|
||||
# 检查收到的消息是否属于当前实例处理的 chat stream
|
||||
if message.chat_stream.stream_id != self.stream_id:
|
||||
logger.error(
|
||||
@@ -264,7 +261,10 @@ class NormalChat:
|
||||
await willing_manager.before_generate_reply_handle(message.message_info.message_id)
|
||||
|
||||
with Timer("创建思考消息", timing_results):
|
||||
thinking_id = await self._create_thinking_message(message)
|
||||
if rewind_response:
|
||||
thinking_id = await self._create_thinking_message(message, message.message_info.time)
|
||||
else:
|
||||
thinking_id = await self._create_thinking_message(message)
|
||||
|
||||
logger.debug(f"[{self.stream_name}] 创建捕捉器,thinking_id:{thinking_id}")
|
||||
|
||||
@@ -393,7 +393,7 @@ class NormalChat:
|
||||
|
||||
try:
|
||||
logger.info(f"[{self.stream_name}] 处理初始高兴趣消息 {msg_id} (兴趣值: {interest_value:.2f})")
|
||||
await self.normal_response(message=message, is_mentioned=is_mentioned, interested_rate=interest_value)
|
||||
await self.normal_response(message=message, is_mentioned=is_mentioned, interested_rate=interest_value, rewind_response = True)
|
||||
processed_count += 1
|
||||
except Exception as e:
|
||||
logger.error(f"[{self.stream_name}] 处理初始兴趣消息 {msg_id} 时出错: {e}\\n{traceback.format_exc()}")
|
||||
|
||||
Reference in New Issue
Block a user