chore(core.platform): 🚨 修正platform基类和webchat的错误Lint

This commit is contained in:
Dt8333
2025-10-31 20:14:51 +08:00
parent 339f9042b7
commit 11eb0b92ee
8 changed files with 35 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ import hashlib
import re
import uuid
from collections.abc import AsyncGenerator
from typing import Any
from typing import Any, cast
from astrbot import logger
from astrbot.core.db.po import Conversation
@@ -52,7 +52,7 @@ class AstrMessageEvent(abc.ABC):
"""是否是 At 机器人或者带有唤醒词或者是私聊(插件注册的事件监听器会让 is_wake 设为 True, 但是不会让这个属性置为 True)"""
self._extras: dict[str, Any] = {}
self.session = MessageSesion(
platform_name=platform_meta.id,
platform_name=cast(str, platform_meta.id),
message_type=message_obj.type,
session_id=session_id,
)
@@ -152,7 +152,9 @@ class AstrMessageEvent(abc.ABC):
def get_sender_name(self) -> str:
"""获取消息发送者的名称。(可能会返回空字符串)"""
return self.message_obj.sender.nickname
if isinstance(self.message_obj.sender.nickname, str):
return self.message_obj.sender.nickname
return ""
def set_extra(self, key, value):
"""设置额外的信息。"""
@@ -269,7 +271,7 @@ class AstrMessageEvent(abc.ABC):
"""
self.call_llm = call_llm
def get_result(self) -> MessageEventResult:
def get_result(self) -> MessageEventResult | None:
"""获取消息事件的结果。"""
return self._result
@@ -319,11 +321,11 @@ class AstrMessageEvent(abc.ABC):
self,
prompt: str,
func_tool_manager=None,
session_id: str = None,
session_id: str = "",
image_urls: list[str] = [],
contexts: list = [],
system_prompt: str = "",
conversation: Conversation = None,
conversation: Conversation | None = None,
) -> ProviderRequest:
"""创建一个 LLM 请求。
@@ -389,7 +391,7 @@ class AstrMessageEvent(abc.ABC):
"""
await self.send(MessageChain([Plain(emoji)]))
async def get_group(self, group_id: str = None, **kwargs) -> Group | None:
async def get_group(self, group_id: str | None = None, **kwargs) -> Group | None:
"""获取一个群聊的数据, 如果不填写 group_id: 如果是私聊消息,返回 None。如果是群聊消息返回当前群聊的数据。
适配情况:

View File

@@ -9,7 +9,7 @@ from .message_type import MessageType
@dataclass
class MessageMember:
user_id: str # 发送者id
nickname: str = None
nickname: str | None = None
def __str__(self):
# 使用 f-string 来构建返回的字符串表示形式
@@ -23,15 +23,15 @@ class MessageMember:
class Group:
group_id: str
"""群号"""
group_name: str = None
group_name: str | None = None
"""群名称"""
group_avatar: str = None
group_avatar: str | None = None
"""群头像"""
group_owner: str = None
group_owner: str | None = None
"""群主 id"""
group_admins: list[str] = None
group_admins: list[str] | None = None
"""群管理员 id"""
members: list[MessageMember] = None
members: list[MessageMember] | None = None
"""所有群成员"""
def __str__(self):
@@ -54,7 +54,7 @@ class AstrBotMessage:
self_id: str # 机器人的识别id
session_id: str # 会话id。取决于 unique_session 的设置。
message_id: str # 消息id
group: Group # 群组
group: Group | None # 群组
sender: MessageMember # 发送者
message: list[BaseMessageComponent] # 消息链使用 Nakuru 的消息链格式
message_str: str # 最直观的纯文本消息字符串

View File

@@ -13,7 +13,7 @@ class MessageSession:
"""平台适配器实例的唯一标识符。自 AstrBot v4.0.0 起,该字段实际为 platform_id。"""
message_type: MessageType
session_id: str
platform_id: str = None
platform_id: str | None = None
def __str__(self):
return f"{self.platform_id}:{self.message_type.value}:{self.session_id}"

View File

@@ -1,7 +1,7 @@
import abc
import uuid
from asyncio import Queue
from collections.abc import Awaitable
from collections.abc import Coroutine
from typing import Any
from astrbot.core.message.message_event_result import MessageChain
@@ -20,7 +20,7 @@ class Platform(abc.ABC):
self.client_self_id = uuid.uuid4().hex
@abc.abstractmethod
def run(self) -> Awaitable[Any]:
def run(self) -> Coroutine[Any, Any, None]:
"""得到一个平台的运行实例,需要返回一个协程对象。"""
raise NotImplementedError
@@ -36,7 +36,7 @@ class Platform(abc.ABC):
self,
session: MessageSesion,
message_chain: MessageChain,
) -> Awaitable[Any]:
) -> None:
"""通过会话发送消息。该方法旨在让插件能够直接通过**可持久化的会话数据**发送消息,而不需要保存 event 对象。
异步方法。

View File

@@ -7,12 +7,12 @@ class PlatformMetadata:
"""平台的名称,即平台的类型,如 aiocqhttp, discord, slack"""
description: str
"""平台的描述"""
id: str = None
id: str | None = None
"""平台的唯一标识符,用于配置中识别特定平台"""
default_config_tmpl: dict = None
default_config_tmpl: dict | None = None
"""平台的默认配置模板"""
adapter_display_name: str = None
adapter_display_name: str | None = None
"""显示在 WebUI 配置页中的平台名称,如空则是 name"""
logo_path: str = None
logo_path: str | None = None
"""平台适配器的 logo 文件路径(相对于插件目录)"""

View File

@@ -11,9 +11,9 @@ platform_cls_map: dict[str, type] = {}
def register_platform_adapter(
adapter_name: str,
desc: str,
default_config_tmpl: dict = None,
adapter_display_name: str = None,
logo_path: str = None,
default_config_tmpl: dict | None = None,
adapter_display_name: str | None = None,
logo_path: str | None = None,
):
"""用于注册平台适配器的带参装饰器。

View File

@@ -2,7 +2,7 @@ import asyncio
import os
import time
import uuid
from collections.abc import Awaitable, Callable
from collections.abc import Callable, Coroutine
from typing import Any
from astrbot import logger
@@ -141,7 +141,7 @@ class WebChatAdapter(Platform):
abm.raw_message = data
return abm
def run(self) -> Awaitable[Any]:
def run(self) -> Coroutine[Any, Any, None]:
async def callback(data: tuple):
abm = await self.convert_message(data)
await self.handle_msg(abm)

View File

@@ -49,7 +49,9 @@ class WebChatMessageEvent(AstrMessageEvent):
# save image to local
filename = str(uuid.uuid4()) + ".jpg"
path = os.path.join(imgs_dir, filename)
if comp.file and comp.file.startswith("file:///"):
if comp.file is None:
raise ValueError("Image file is None")
elif comp.file.startswith("file:///"):
ph = comp.file[8:]
with open(path, "wb") as f:
with open(ph, "rb") as f2:
@@ -78,7 +80,9 @@ class WebChatMessageEvent(AstrMessageEvent):
# save record to local
filename = str(uuid.uuid4()) + ".wav"
path = os.path.join(imgs_dir, filename)
if comp.file and comp.file.startswith("file:///"):
if comp.file is None:
raise ValueError("Record file is None")
elif comp.file.startswith("file:///"):
ph = comp.file[8:]
with open(path, "wb") as f:
with open(ph, "rb") as f2: