🤖 自动格式化代码 [skip ci]
This commit is contained in:
@@ -293,7 +293,15 @@ block_and_ignore: 更加极端的结束对话方式,直接结束对话并在
|
|||||||
reason = result.get("reason", "LLM未提供原因,默认等待")
|
reason = result.get("reason", "LLM未提供原因,默认等待")
|
||||||
|
|
||||||
# 验证action类型
|
# 验证action类型
|
||||||
valid_actions = ["direct_reply", "fetch_knowledge", "wait", "listening", "rethink_goal", "end_conversation", "block_and_ignore"]
|
valid_actions = [
|
||||||
|
"direct_reply",
|
||||||
|
"fetch_knowledge",
|
||||||
|
"wait",
|
||||||
|
"listening",
|
||||||
|
"rethink_goal",
|
||||||
|
"end_conversation",
|
||||||
|
"block_and_ignore",
|
||||||
|
]
|
||||||
if action not in valid_actions:
|
if action not in valid_actions:
|
||||||
logger.warning(f"LLM返回了未知的行动类型: '{action}',强制改为 wait")
|
logger.warning(f"LLM返回了未知的行动类型: '{action}',强制改为 wait")
|
||||||
reason = f"(原始行动'{action}'无效,已强制改为wait) {reason}"
|
reason = f"(原始行动'{action}'无效,已强制改为wait) {reason}"
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import time
|
import time
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
# from .message_storage import MongoDBMessageStorage
|
# from .message_storage import MongoDBMessageStorage
|
||||||
from src.plugins.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
|
from src.plugins.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
|
||||||
|
|
||||||
# from ...config.config import global_config
|
# from ...config.config import global_config
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional
|
||||||
from ..chat.message import Message
|
from ..chat.message import Message
|
||||||
@@ -135,17 +137,17 @@ class Conversation:
|
|||||||
"""思考步,PFC核心循环模块"""
|
"""思考步,PFC核心循环模块"""
|
||||||
while self.should_continue:
|
while self.should_continue:
|
||||||
if self.ignore_until_timestamp and time.time() < self.ignore_until_timestamp:
|
if self.ignore_until_timestamp and time.time() < self.ignore_until_timestamp:
|
||||||
# 仍在忽略期间,等待下次检查
|
# 仍在忽略期间,等待下次检查
|
||||||
await asyncio.sleep(30) # 每 30 秒检查一次
|
await asyncio.sleep(30) # 每 30 秒检查一次
|
||||||
continue # 跳过本轮循环的剩余部分
|
continue # 跳过本轮循环的剩余部分
|
||||||
elif self.ignore_until_timestamp and time.time() >= self.ignore_until_timestamp:
|
elif self.ignore_until_timestamp and time.time() >= self.ignore_until_timestamp:
|
||||||
# 忽略期结束,现在正常地结束对话
|
# 忽略期结束,现在正常地结束对话
|
||||||
logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。")
|
logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。")
|
||||||
self.ignore_until_timestamp = None # 清除时间戳
|
self.ignore_until_timestamp = None # 清除时间戳
|
||||||
self.should_continue = False # 现在停止循环
|
self.should_continue = False # 现在停止循环
|
||||||
# (可选)在这里记录一个 'end_conversation' 动作
|
# (可选)在这里记录一个 'end_conversation' 动作
|
||||||
# 或者确保管理器会基于 should_continue 为 False 来清理它
|
# 或者确保管理器会基于 should_continue 为 False 来清理它
|
||||||
continue # 跳过本轮循环的剩余部分,让它终止
|
continue # 跳过本轮循环的剩余部分,让它终止
|
||||||
try:
|
try:
|
||||||
# --- 在规划前记录当前新消息数量 ---
|
# --- 在规划前记录当前新消息数量 ---
|
||||||
initial_new_message_count = 0
|
initial_new_message_count = 0
|
||||||
@@ -424,19 +426,19 @@ class Conversation:
|
|||||||
# 这里不需要 return,主循环会在下一轮检查 should_continue
|
# 这里不需要 return,主循环会在下一轮检查 should_continue
|
||||||
|
|
||||||
elif action == "block_and_ignore":
|
elif action == "block_and_ignore":
|
||||||
logger.info("不想再理你了...")
|
logger.info("不想再理你了...")
|
||||||
# 1. 标记对话为暂时忽略
|
# 1. 标记对话为暂时忽略
|
||||||
ignore_duration_seconds = 10 * 60 # 10 分钟
|
ignore_duration_seconds = 10 * 60 # 10 分钟
|
||||||
self.ignore_until_timestamp = time.time() + ignore_duration_seconds
|
self.ignore_until_timestamp = time.time() + ignore_duration_seconds
|
||||||
logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}")
|
logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}")
|
||||||
conversation_info.done_action[action_index].update(
|
conversation_info.done_action[action_index].update(
|
||||||
{
|
{
|
||||||
"status": "done", # 或者一个自定义状态,比如 "ignored"
|
"status": "done", # 或者一个自定义状态,比如 "ignored"
|
||||||
"final_reason": "Detected potential harassment, ignoring temporarily.", # 检测到潜在骚扰,暂时忽略
|
"final_reason": "Detected potential harassment, ignoring temporarily.", # 检测到潜在骚扰,暂时忽略
|
||||||
"time": datetime.datetime.now().strftime("%H:%M:%S"),
|
"time": datetime.datetime.now().strftime("%H:%M:%S"),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self.state = ConversationState.IGNORED
|
self.state = ConversationState.IGNORED
|
||||||
|
|
||||||
else: # 对应 'wait' 动作
|
else: # 对应 'wait' 动作
|
||||||
self.state = ConversationState.WAITING
|
self.state = ConversationState.WAITING
|
||||||
|
|||||||
@@ -47,18 +47,20 @@ class PFCManager:
|
|||||||
return self._instances[stream_id]
|
return self._instances[stream_id]
|
||||||
if stream_id in self._instances:
|
if stream_id in self._instances:
|
||||||
instance = self._instances[stream_id]
|
instance = self._instances[stream_id]
|
||||||
if hasattr(instance, 'ignore_until_timestamp') and \
|
if (
|
||||||
instance.ignore_until_timestamp and \
|
hasattr(instance, "ignore_until_timestamp")
|
||||||
time.time() < instance.ignore_until_timestamp:
|
and instance.ignore_until_timestamp
|
||||||
|
and time.time() < instance.ignore_until_timestamp
|
||||||
|
):
|
||||||
logger.debug(f"会话实例当前处于忽略状态: {stream_id}")
|
logger.debug(f"会话实例当前处于忽略状态: {stream_id}")
|
||||||
# 返回 None 阻止交互。或者可以返回实例但标记它被忽略了喵?
|
# 返回 None 阻止交互。或者可以返回实例但标记它被忽略了喵?
|
||||||
# 还是返回 None 吧喵。
|
# 还是返回 None 吧喵。
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 检查 should_continue 状态
|
# 检查 should_continue 状态
|
||||||
if instance.should_continue:
|
if instance.should_continue:
|
||||||
logger.debug(f"使用现有会话实例: {stream_id}")
|
logger.debug(f"使用现有会话实例: {stream_id}")
|
||||||
return instance
|
return instance
|
||||||
# else: 实例存在但不应继续
|
# else: 实例存在但不应继续
|
||||||
try:
|
try:
|
||||||
# 创建新实例
|
# 创建新实例
|
||||||
|
|||||||
Reference in New Issue
Block a user