chore: format code and remove redundant blank lines
This commit applies automated code formatting across the project. The changes primarily involve removing unnecessary blank lines and ensuring consistent code style, improving readability and maintainability without altering functionality.
This commit is contained in:
@@ -36,27 +36,19 @@ def get_context_groups(chat_id: str) -> Optional[List[List[str]]]:
|
||||
# 检查当前聊天的ID和类型是否在组的chat_ids中
|
||||
if [current_type, str(current_chat_raw_id)] in group.chat_ids:
|
||||
# 返回组内其他聊天的 [type, id] 列表
|
||||
return [
|
||||
chat_info
|
||||
for chat_info in group.chat_ids
|
||||
if chat_info != [current_type, str(current_chat_raw_id)]
|
||||
]
|
||||
return [chat_info for chat_info in group.chat_ids if chat_info != [current_type, str(current_chat_raw_id)]]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def build_cross_context_normal(
|
||||
chat_stream: ChatStream, other_chat_infos: List[List[str]]
|
||||
) -> str:
|
||||
async def build_cross_context_normal(chat_stream: ChatStream, other_chat_infos: List[List[str]]) -> str:
|
||||
"""
|
||||
构建跨群聊/私聊上下文 (Normal模式)
|
||||
"""
|
||||
cross_context_messages = []
|
||||
for chat_type, chat_raw_id in other_chat_infos:
|
||||
is_group = chat_type == "group"
|
||||
stream_id = get_chat_manager().get_stream_id(
|
||||
chat_stream.platform, chat_raw_id, is_group=is_group
|
||||
)
|
||||
stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group)
|
||||
if not stream_id:
|
||||
continue
|
||||
|
||||
@@ -68,9 +60,7 @@ async def build_cross_context_normal(
|
||||
)
|
||||
if messages:
|
||||
chat_name = get_chat_manager().get_stream_name(stream_id) or chat_raw_id
|
||||
formatted_messages, _ = build_readable_messages_with_id(
|
||||
messages, timestamp_mode="relative"
|
||||
)
|
||||
formatted_messages, _ = build_readable_messages_with_id(messages, timestamp_mode="relative")
|
||||
cross_context_messages.append(f'[以下是来自"{chat_name}"的近期消息]\n{formatted_messages}')
|
||||
except Exception as e:
|
||||
logger.error(f"获取聊天 {chat_raw_id} 的消息失败: {e}")
|
||||
@@ -97,9 +87,7 @@ async def build_cross_context_s4u(
|
||||
if user_id:
|
||||
for chat_type, chat_raw_id in other_chat_infos:
|
||||
is_group = chat_type == "group"
|
||||
stream_id = get_chat_manager().get_stream_id(
|
||||
chat_stream.platform, chat_raw_id, is_group=is_group
|
||||
)
|
||||
stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group)
|
||||
if not stream_id:
|
||||
continue
|
||||
|
||||
@@ -114,9 +102,7 @@ async def build_cross_context_s4u(
|
||||
if user_messages:
|
||||
chat_name = get_chat_manager().get_stream_name(stream_id) or chat_raw_id
|
||||
user_name = (
|
||||
target_user_info.get("person_name")
|
||||
or target_user_info.get("user_nickname")
|
||||
or user_id
|
||||
target_user_info.get("person_name") or target_user_info.get("user_nickname") or user_id
|
||||
)
|
||||
formatted_messages, _ = build_readable_messages_with_id(
|
||||
user_messages, timestamp_mode="relative"
|
||||
@@ -182,9 +168,7 @@ async def get_chat_history_by_group_name(group_name: str) -> str:
|
||||
)
|
||||
if messages:
|
||||
chat_name = get_chat_manager().get_stream_name(stream_id) or chat_raw_id
|
||||
formatted_messages, _ = build_readable_messages_with_id(
|
||||
messages, timestamp_mode="relative"
|
||||
)
|
||||
formatted_messages, _ = build_readable_messages_with_id(messages, timestamp_mode="relative")
|
||||
cross_context_messages.append(f'[以下是来自"{chat_name}"的近期消息]\n{formatted_messages}')
|
||||
except Exception as e:
|
||||
logger.error(f"获取聊天 {chat_raw_id} 的消息失败: {e}")
|
||||
@@ -193,4 +177,4 @@ async def get_chat_history_by_group_name(group_name: str) -> str:
|
||||
if not cross_context_messages:
|
||||
return f"无法从互通组 {group_name} 中获取任何聊天记录。"
|
||||
|
||||
return "# 跨上下文参考\n" + "\n\n".join(cross_context_messages) + "\n"
|
||||
return "# 跨上下文参考\n" + "\n\n".join(cross_context_messages) + "\n"
|
||||
|
||||
@@ -107,9 +107,7 @@ async def generate_reply(
|
||||
"""
|
||||
try:
|
||||
# 获取回复器
|
||||
replyer = get_replyer(
|
||||
chat_stream, chat_id, request_type=request_type
|
||||
)
|
||||
replyer = get_replyer(chat_stream, chat_id, request_type=request_type)
|
||||
if not replyer:
|
||||
logger.error("[GeneratorAPI] 无法获取回复器")
|
||||
return False, [], None
|
||||
|
||||
@@ -51,6 +51,7 @@ logger = get_logger("send_api")
|
||||
# 适配器命令响应等待池
|
||||
_adapter_response_pool: Dict[str, asyncio.Future] = {}
|
||||
|
||||
|
||||
def message_dict_to_message_recv(message_dict: Dict[str, Any]) -> Optional[MessageRecv]:
|
||||
"""查找要回复的消息
|
||||
|
||||
@@ -97,10 +98,11 @@ def message_dict_to_message_recv(message_dict: Dict[str, Any]) -> Optional[Messa
|
||||
}
|
||||
|
||||
message_recv = MessageRecv(message_dict)
|
||||
|
||||
|
||||
logger.info(f"[SendAPI] 找到匹配的回复消息,发送者: {message_dict.get('user_nickname', '')}")
|
||||
return message_recv
|
||||
|
||||
|
||||
def put_adapter_response(request_id: str, response_data: dict) -> None:
|
||||
"""将适配器响应放入响应池"""
|
||||
if request_id in _adapter_response_pool:
|
||||
@@ -192,7 +194,7 @@ async def _send_to_target(
|
||||
anchor_message.update_chat_stream(target_stream)
|
||||
reply_to_platform_id = (
|
||||
f"{anchor_message.message_info.platform}:{anchor_message.message_info.user_info.user_id}"
|
||||
)
|
||||
)
|
||||
else:
|
||||
anchor_message = None
|
||||
reply_to_platform_id = None
|
||||
@@ -234,7 +236,6 @@ async def _send_to_target(
|
||||
return False
|
||||
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 公共API函数 - 预定义类型的发送函数
|
||||
# =============================================================================
|
||||
@@ -274,7 +275,9 @@ async def text_to_stream(
|
||||
)
|
||||
|
||||
|
||||
async def emoji_to_stream(emoji_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False) -> bool:
|
||||
async def emoji_to_stream(
|
||||
emoji_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False
|
||||
) -> bool:
|
||||
"""向指定流发送表情包
|
||||
|
||||
Args:
|
||||
@@ -285,10 +288,14 @@ async def emoji_to_stream(emoji_base64: str, stream_id: str, storage_message: bo
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
return await _send_to_target("emoji", emoji_base64, stream_id, "", typing=False, storage_message=storage_message, set_reply=set_reply)
|
||||
return await _send_to_target(
|
||||
"emoji", emoji_base64, stream_id, "", typing=False, storage_message=storage_message, set_reply=set_reply
|
||||
)
|
||||
|
||||
|
||||
async def image_to_stream(image_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False) -> bool:
|
||||
async def image_to_stream(
|
||||
image_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False
|
||||
) -> bool:
|
||||
"""向指定流发送图片
|
||||
|
||||
Args:
|
||||
@@ -299,11 +306,17 @@ async def image_to_stream(image_base64: str, stream_id: str, storage_message: bo
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
return await _send_to_target("image", image_base64, stream_id, "", typing=False, storage_message=storage_message, set_reply=set_reply)
|
||||
return await _send_to_target(
|
||||
"image", image_base64, stream_id, "", typing=False, storage_message=storage_message, set_reply=set_reply
|
||||
)
|
||||
|
||||
|
||||
async def command_to_stream(
|
||||
command: Union[str, dict], stream_id: str, storage_message: bool = True, display_message: str = "", set_reply: bool = False
|
||||
command: Union[str, dict],
|
||||
stream_id: str,
|
||||
storage_message: bool = True,
|
||||
display_message: str = "",
|
||||
set_reply: bool = False,
|
||||
) -> bool:
|
||||
"""向指定流发送命令
|
||||
|
||||
|
||||
Reference in New Issue
Block a user