mirror of https://github.com/Mai-with-u/MaiBot.git
feat:添加结束对话动作
parent
cef094e125
commit
541799b1c2
|
|
@ -181,8 +181,8 @@ class BrainChatting:
|
||||||
should_continue = await self._observe(recent_messages_list=recent_messages_list)
|
should_continue = await self._observe(recent_messages_list=recent_messages_list)
|
||||||
|
|
||||||
if not should_continue:
|
if not should_continue:
|
||||||
# 选择了 complete_talk,停止循环
|
# 选择了 complete_talk,返回 False 表示需要等待新消息
|
||||||
return True
|
return False
|
||||||
|
|
||||||
# 继续下一次迭代(除非选择了 complete_talk)
|
# 继续下一次迭代(除非选择了 complete_talk)
|
||||||
# 短暂等待后再继续,避免过于频繁的循环
|
# 短暂等待后再继续,避免过于频繁的循环
|
||||||
|
|
@ -414,9 +414,13 @@ class BrainChatting:
|
||||||
while self.running:
|
while self.running:
|
||||||
# 主循环
|
# 主循环
|
||||||
success = await self._loopbody()
|
success = await self._loopbody()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
if not success:
|
if not success:
|
||||||
break
|
# 选择了 complete,等待新消息
|
||||||
|
logger.info(f"{self.log_prefix} 选择了 complete,等待新消息...")
|
||||||
|
await self._wait_for_new_message()
|
||||||
|
# 有新消息后继续循环
|
||||||
|
continue
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
# 设置了关闭标志位后被取消是正常流程
|
# 设置了关闭标志位后被取消是正常流程
|
||||||
logger.info(f"{self.log_prefix} 麦麦已关闭聊天")
|
logger.info(f"{self.log_prefix} 麦麦已关闭聊天")
|
||||||
|
|
@ -427,6 +431,33 @@ class BrainChatting:
|
||||||
self._loop_task = asyncio.create_task(self._main_chat_loop())
|
self._loop_task = asyncio.create_task(self._main_chat_loop())
|
||||||
logger.error(f"{self.log_prefix} 结束了当前聊天循环")
|
logger.error(f"{self.log_prefix} 结束了当前聊天循环")
|
||||||
|
|
||||||
|
async def _wait_for_new_message(self):
|
||||||
|
"""等待新消息到达"""
|
||||||
|
last_check_time = self.last_read_time
|
||||||
|
check_interval = 1.0 # 每秒检查一次
|
||||||
|
|
||||||
|
while self.running:
|
||||||
|
# 检查是否有新消息
|
||||||
|
recent_messages_list = message_api.get_messages_by_time_in_chat(
|
||||||
|
chat_id=self.stream_id,
|
||||||
|
start_time=last_check_time,
|
||||||
|
end_time=time.time(),
|
||||||
|
limit=20,
|
||||||
|
limit_mode="latest",
|
||||||
|
filter_mai=True,
|
||||||
|
filter_command=False,
|
||||||
|
filter_intercept_message_level=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 如果有新消息,更新 last_read_time 并返回
|
||||||
|
if len(recent_messages_list) >= 1:
|
||||||
|
self.last_read_time = time.time()
|
||||||
|
logger.info(f"{self.log_prefix} 检测到新消息,恢复循环")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 等待一段时间后再次检查
|
||||||
|
await asyncio.sleep(check_interval)
|
||||||
|
|
||||||
async def _handle_action(
|
async def _handle_action(
|
||||||
self,
|
self,
|
||||||
action: str,
|
action: str,
|
||||||
|
|
|
||||||
|
|
@ -61,11 +61,14 @@ reply
|
||||||
|
|
||||||
wait
|
wait
|
||||||
动作描述:
|
动作描述:
|
||||||
暂时不再发言,等待指定时间后再继续下一次思考。适用于以下情况:
|
暂时不再发言,等待指定时间。适用于以下情况:
|
||||||
- 你已经表达清楚一轮,想给对方留出空间
|
- 你已经表达清楚一轮,想给对方留出空间
|
||||||
- 你感觉对方的话还没说完,或者刚刚发了好几条连续消息
|
- 你感觉对方的话还没说完,或者自己刚刚发了好几条连续消息
|
||||||
|
- 你想要等待一定时间来让对方把话说完,或者等待对方反应
|
||||||
- 你想保持安静,专注"听"而不是马上回复
|
- 你想保持安静,专注"听"而不是马上回复
|
||||||
请你根据上下文来判断要等待多久,请你灵活判断
|
请你根据上下文来判断要等待多久,请你灵活判断:
|
||||||
|
- 如果你们交流间隔时间很短,聊的很频繁,不宜等待太久
|
||||||
|
- 如果你们交流间隔时间很长,聊的很少,可以等待较长时间
|
||||||
{{
|
{{
|
||||||
"action": "wait",
|
"action": "wait",
|
||||||
"target_message_id":"想要作为这次等待依据的消息id(通常是对方的最新消息)",
|
"target_message_id":"想要作为这次等待依据的消息id(通常是对方的最新消息)",
|
||||||
|
|
@ -73,6 +76,20 @@ wait
|
||||||
"reason":"选择等待的原因"
|
"reason":"选择等待的原因"
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
complete_talk
|
||||||
|
动作描述:
|
||||||
|
当前聊天暂时结束了,对方离开,没有更多话题了
|
||||||
|
你可以使用该动作来暂时休息,等待对方有新发言再继续:
|
||||||
|
- 多次wait之后,对方迟迟不回复消息才用
|
||||||
|
- 如果对方只是短暂不回复,应该使用wait而不是complete_talk
|
||||||
|
- 聊天内容显示当前聊天已经结束或者没有新内容时候,选择complete_talk
|
||||||
|
选择此动作后,将不再继续循环思考,直到收到对方的新消息
|
||||||
|
{{
|
||||||
|
"action": "complete_talk",
|
||||||
|
"target_message_id":"触发完成对话的消息id(通常是对方的最新消息)",
|
||||||
|
"reason":"选择完成对话的原因"
|
||||||
|
}}
|
||||||
|
|
||||||
{action_options_text}
|
{action_options_text}
|
||||||
|
|
||||||
请选择合适的action,并说明触发action的消息id和选择该action的原因。消息id格式:m+数字
|
请选择合适的action,并说明触发action的消息id和选择该action的原因。消息id格式:m+数字
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue