🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-07-01 16:22:13 +00:00
parent bb2a95e388
commit b369d12c90
4 changed files with 36 additions and 33 deletions

View File

@@ -18,12 +18,12 @@ from src.mais4u.mais4u_chat.s4u_msg_processor import S4UMessageProcessor
# 定义日志配置
# 获取项目根目录假设本文件在src/chat/message_receive/下,根目录为上上上级目录)
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
ENABLE_S4U_CHAT = os.path.isfile(os.path.join(PROJECT_ROOT, 's4u.s4u'))
ENABLE_S4U_CHAT = os.path.isfile(os.path.join(PROJECT_ROOT, "s4u.s4u"))
if ENABLE_S4U_CHAT:
print('''\nS4U私聊模式已开启\n!!!!!!!!!!!!!!!!!\n''')
print("""\nS4U私聊模式已开启\n!!!!!!!!!!!!!!!!!\n""")
# 仅内部开启
# 配置主程序日志格式

View File

@@ -156,7 +156,7 @@ class S4UChat:
self._normal_queue = asyncio.PriorityQueue()
self._entry_counter = 0 # 保证FIFO的全局计数器
self._new_message_event = asyncio.Event() # 用于唤醒处理器
self._new_message_event = asyncio.Event() # 用于唤醒处理器
self._processing_task = asyncio.create_task(self._message_processor())
self._current_generation_task: Optional[asyncio.Task] = None
@@ -194,7 +194,7 @@ class S4UChat:
# 规则VIP从不被打断
if current_queue == "vip":
pass # Do nothing
pass # Do nothing
# 规则:普通消息可以被打断
elif current_queue == "normal":
@@ -217,7 +217,9 @@ class S4UChat:
if should_interrupt:
if self.gpt.partial_response:
logger.warning(f"[{self.stream_name}] Interrupting reply. Already generated: '{self.gpt.partial_response}'")
logger.warning(
f"[{self.stream_name}] Interrupting reply. Already generated: '{self.gpt.partial_response}'"
)
self._current_generation_task.cancel()
# 将消息放入对应的队列
@@ -229,7 +231,7 @@ class S4UChat:
await self._normal_queue.put(item)
self._entry_counter += 1
self._new_message_event.set() # 唤醒处理器
self._new_message_event.set() # 唤醒处理器
async def _message_processor(self):
"""调度器优先处理VIP队列然后处理普通队列。"""
@@ -248,12 +250,14 @@ class S4UChat:
priority, entry_count, timestamp, message = self._normal_queue.get_nowait()
# 检查普通消息是否超时
if time.time() - timestamp > self._MESSAGE_TIMEOUT_SECONDS:
logger.info(f"[{self.stream_name}] Discarding stale normal message: {message.processed_plain_text[:20]}...")
logger.info(
f"[{self.stream_name}] Discarding stale normal message: {message.processed_plain_text[:20]}..."
)
self._normal_queue.task_done()
continue # 处理下一条
continue # 处理下一条
queue_name = "normal"
else:
continue # 没有消息了,回去等事件
continue # 没有消息了,回去等事件
self._current_message_being_replied = (queue_name, priority, entry_count, message)
self._current_generation_task = asyncio.create_task(self._generate_and_send(message))
@@ -261,7 +265,9 @@ class S4UChat:
try:
await self._current_generation_task
except asyncio.CancelledError:
logger.info(f"[{self.stream_name}] Reply generation was interrupted externally for {queue_name} message. The message will be discarded.")
logger.info(
f"[{self.stream_name}] Reply generation was interrupted externally for {queue_name} message. The message will be discarded."
)
# 被中断的消息应该被丢弃,而不是重新排队,以响应最新的用户输入。
# 旧的重新入队逻辑会导致所有中断的消息最终都被回复。
@@ -271,7 +277,7 @@ class S4UChat:
self._current_generation_task = None
self._current_message_being_replied = None
# 标记任务完成
if queue_name == 'vip':
if queue_name == "vip":
self._vip_queue.task_done()
else:
self._normal_queue.task_done()

View File

@@ -1,6 +1,5 @@
from src.config.config import global_config
from src.common.logger import get_logger
from src.individuality.individuality import get_individuality
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
import time
@@ -105,7 +104,9 @@ class PromptBuilder:
)
relation_info = "".join(relation_info_list)
if relation_info:
relation_prompt = await global_prompt_manager.format_prompt("relation_prompt", relation_info=relation_info)
relation_prompt = await global_prompt_manager.format_prompt(
"relation_prompt", relation_info=relation_info
)
return relation_prompt
async def build_memory_block(self, text: str) -> str:
@@ -199,7 +200,6 @@ class PromptBuilder:
return core_msg_str, background_dialogue_prompt
async def build_prompt_normal(
self,
message,
@@ -207,11 +207,8 @@ class PromptBuilder:
message_txt: str,
sender_name: str = "某人",
) -> str:
identity_block, relation_info_block, memory_block = await asyncio.gather(
self.build_identity_block(),
self.build_relation_info(chat_stream),
self.build_memory_block(message_txt)
self.build_identity_block(), self.build_relation_info(chat_stream), self.build_memory_block(message_txt)
)
core_dialogue_prompt, background_dialogue_prompt = self.build_chat_history_prompts(chat_stream, message)