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
+23 -13
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()