style: introduce ruff as linter and formatter (#1356)

* style: remove necessary imports

* style: fix F841

* style: fix F401

* style: fix F811

* style: fix E402

* style: fix E721

* style: fix E722

* style: fix E722

* style: fix F541

* style: ruff format

* style: all passed

* style: add ruff in deps

* style: more ignores in ruff.toml

* style: add pre-commit
This commit is contained in:
Junyan Qin (Chin)
2025-04-29 17:24:07 +08:00
committed by GitHub
parent 09e70d70e9
commit 209f16af76
240 changed files with 5307 additions and 4689 deletions

View File

@@ -3,7 +3,6 @@ from __future__ import annotations
import typing
import pkgutil
import importlib
import os
import traceback
from .. import loader, events, context, models
@@ -11,7 +10,6 @@ from ...core import entities as core_entities
from ...provider.tools import entities as tools_entities
from ...utils import funcschema
from ...discover import engine as discover_engine
from ...utils import pkgmgr
class PluginLoader(loader.PluginLoader):
@@ -36,17 +34,17 @@ class PluginLoader(loader.PluginLoader):
"""初始化"""
def register(
self,
name: str,
description: str,
version: str,
author: str
) -> typing.Callable[[typing.Type[context.BasePlugin]], typing.Type[context.BasePlugin]]:
self, name: str, description: str, version: str, author: str
) -> typing.Callable[
[typing.Type[context.BasePlugin]], typing.Type[context.BasePlugin]
]:
self.ap.logger.debug(f'注册插件 {name} {version} by {author}')
container = context.RuntimeContainer(
plugin_name=name,
plugin_label=discover_engine.I18nString(en_US=name, zh_CN=name),
plugin_description=discover_engine.I18nString(en_US=description, zh_CN=description),
plugin_description=discover_engine.I18nString(
en_US=description, zh_CN=description
),
plugin_version=version,
plugin_author=author,
plugin_repository='',
@@ -61,20 +59,21 @@ class PluginLoader(loader.PluginLoader):
def wrapper(cls: context.BasePlugin) -> typing.Type[context.BasePlugin]:
container.plugin_class = cls
return cls
return wrapper
# 过时
# 最早将于 v3.4 版本移除
def on(
self,
event: typing.Type[events.BaseEventModel]
self, event: typing.Type[events.BaseEventModel]
) -> typing.Callable[[typing.Callable], typing.Callable]:
"""注册过时的事件处理器"""
self.ap.logger.debug(f'注册事件处理器 {event.__name__}')
def wrapper(func: typing.Callable) -> typing.Callable:
async def handler(plugin: context.BasePlugin, ctx: context.EventContext) -> None:
async def handler(
plugin: context.BasePlugin, ctx: context.EventContext
) -> None:
args = {
'host': ctx.host,
'event': ctx,
@@ -82,12 +81,12 @@ class PluginLoader(loader.PluginLoader):
# 把 ctx.event 所有的属性都放到 args 里
# for k, v in ctx.event.dict().items():
# args[k] = v
# args[k] = v
for attr_name in ctx.event.__dict__.keys():
args[attr_name] = getattr(ctx.event, attr_name)
func(plugin, **args)
self._current_container.event_handlers[event] = handler
return func
@@ -98,20 +97,21 @@ class PluginLoader(loader.PluginLoader):
# 最早将于 v3.4 版本移除
def func(
self,
name: str=None,
name: str = None,
) -> typing.Callable:
"""注册过时的内容函数"""
self.ap.logger.debug(f'注册内容函数 {name}')
def wrapper(func: typing.Callable) -> typing.Callable:
function_schema = funcschema.get_func_schema(func)
function_name = self._current_container.plugin_name + '-' + (func.__name__ if name is None else name)
function_name = (
self._current_container.plugin_name
+ '-'
+ (func.__name__ if name is None else name)
)
async def handler(
plugin: context.BasePlugin,
query: core_entities.Query,
*args,
**kwargs
plugin: context.BasePlugin, query: core_entities.Query, *args, **kwargs
):
return func(*args, **kwargs)
@@ -126,18 +126,19 @@ class PluginLoader(loader.PluginLoader):
self._current_container.tools.append(llm_function)
return func
return wrapper
def handler(
self,
event: typing.Type[events.BaseEventModel]
self, event: typing.Type[events.BaseEventModel]
) -> typing.Callable[[typing.Callable], typing.Callable]:
"""注册事件处理器"""
self.ap.logger.debug(f'注册事件处理器 {event.__name__}')
def wrapper(func: typing.Callable) -> typing.Callable:
if self._current_container is None: # None indicates this plugin is registered through manifest, so ignore it here
def wrapper(func: typing.Callable) -> typing.Callable:
if (
self._current_container is None
): # None indicates this plugin is registered through manifest, so ignore it here
return func
self._current_container.event_handlers[event] = func
@@ -148,17 +149,23 @@ class PluginLoader(loader.PluginLoader):
def llm_func(
self,
name: str=None,
name: str = None,
) -> typing.Callable:
"""注册内容函数"""
self.ap.logger.debug(f'注册内容函数 {name}')
def wrapper(func: typing.Callable) -> typing.Callable:
if self._current_container is None: # None indicates this plugin is registered through manifest, so ignore it here
if (
self._current_container is None
): # None indicates this plugin is registered through manifest, so ignore it here
return func
function_schema = funcschema.get_func_schema(func)
function_name = self._current_container.plugin_name + '-' + (func.__name__ if name is None else name)
function_name = (
self._current_container.plugin_name
+ '-'
+ (func.__name__ if name is None else name)
)
llm_function = tools_entities.LLMFunction(
name=function_name,
@@ -171,43 +178,40 @@ class PluginLoader(loader.PluginLoader):
self._current_container.tools.append(llm_function)
return func
return wrapper
async def _walk_plugin_path(
self,
module,
prefix='',
path_prefix=''
):
"""遍历插件路径
"""
async def _walk_plugin_path(self, module, prefix='', path_prefix=''):
"""遍历插件路径"""
for item in pkgutil.iter_modules(module.__path__):
if item.ispkg:
await self._walk_plugin_path(
__import__(module.__name__ + "." + item.name, fromlist=[""]),
prefix + item.name + ".",
path_prefix + item.name + "/",
__import__(module.__name__ + '.' + item.name, fromlist=['']),
prefix + item.name + '.',
path_prefix + item.name + '/',
)
else:
try:
self._current_pkg_path = "plugins/" + path_prefix
self._current_module_path = "plugins/" + path_prefix + item.name + ".py"
self._current_pkg_path = 'plugins/' + path_prefix
self._current_module_path = (
'plugins/' + path_prefix + item.name + '.py'
)
self._current_container = None
importlib.import_module(module.__name__ + "." + item.name)
importlib.import_module(module.__name__ + '.' + item.name)
if self._current_container is not None:
self.plugins.append(self._current_container)
self.ap.logger.debug(f'插件 {self._current_container} 已加载')
except:
self.ap.logger.error(f'加载插件模块 {prefix + item.name} 时发生错误')
except Exception:
self.ap.logger.error(
f'加载插件模块 {prefix + item.name} 时发生错误'
)
traceback.print_exc()
async def load_plugins(self):
"""加载插件
"""
"""加载插件"""
setattr(models, 'register', self.register)
setattr(models, 'on', self.on)
setattr(models, 'func', self.func)
@@ -215,4 +219,4 @@ class PluginLoader(loader.PluginLoader):
setattr(context, 'register', self.register)
setattr(context, 'handler', self.handler)
setattr(context, 'llm_func', self.llm_func)
await self._walk_plugin_path(__import__("plugins", fromlist=[""]))
await self._walk_plugin_path(__import__('plugins', fromlist=['']))

View File

@@ -1,12 +1,11 @@
from __future__ import annotations
import typing
import abc
import os
import traceback
from ...core import app
from .. import context, events, models
from .. import context, events
from .. import loader
from ...utils import funcschema
from ...provider.tools import entities as tools_entities
@@ -21,13 +20,12 @@ class PluginManifestLoader(loader.PluginLoader):
super().__init__(ap)
def handler(
self,
event: typing.Type[events.BaseEventModel]
self, event: typing.Type[events.BaseEventModel]
) -> typing.Callable[[typing.Callable], typing.Callable]:
"""注册事件处理器"""
self.ap.logger.debug(f'注册事件处理器 {event.__name__}')
def wrapper(func: typing.Callable) -> typing.Callable:
self._current_container.event_handlers[event] = func
return func
@@ -36,14 +34,18 @@ class PluginManifestLoader(loader.PluginLoader):
def llm_func(
self,
name: str=None,
name: str = None,
) -> typing.Callable:
"""注册内容函数"""
self.ap.logger.debug(f'注册内容函数 {name}')
def wrapper(func: typing.Callable) -> typing.Callable:
function_schema = funcschema.get_func_schema(func)
function_name = self._current_container.plugin_name + '-' + (func.__name__ if name is None else name)
function_name = (
self._current_container.plugin_name
+ '-'
+ (func.__name__ if name is None else name)
)
llm_function = tools_entities.LLMFunction(
name=function_name,
@@ -56,7 +58,7 @@ class PluginManifestLoader(loader.PluginLoader):
self._current_container.tools.append(llm_function)
return func
return wrapper
async def load_plugins(self):
@@ -68,7 +70,11 @@ class PluginManifestLoader(loader.PluginLoader):
for plugin_manifest in plugin_manifests:
try:
config_schema = plugin_manifest.spec['config'] if 'config' in plugin_manifest.spec else []
config_schema = (
plugin_manifest.spec['config']
if 'config' in plugin_manifest.spec
else []
)
current_plugin_container = context.RuntimeContainer(
plugin_name=plugin_manifest.metadata.name,
@@ -77,7 +83,9 @@ class PluginManifestLoader(loader.PluginLoader):
plugin_version=plugin_manifest.metadata.version,
plugin_author=plugin_manifest.metadata.author,
plugin_repository=plugin_manifest.metadata.repository,
main_file=os.path.join(plugin_manifest.rel_dir, plugin_manifest.execution.python.path),
main_file=os.path.join(
plugin_manifest.rel_dir, plugin_manifest.execution.python.path
),
pkg_path=plugin_manifest.rel_dir,
config_schema=config_schema,
event_handlers={},
@@ -95,6 +103,8 @@ class PluginManifestLoader(loader.PluginLoader):
# TODO load component extensions
self.plugins.append(current_plugin_container)
except Exception as e:
self.ap.logger.error(f'加载插件 {plugin_manifest.metadata.name} 时发生错误')
except Exception:
self.ap.logger.error(
f'加载插件 {plugin_manifest.metadata.name} 时发生错误'
)
traceback.print_exc()