From c6afa970606fecd8c37c299f1bb1d0ec28492ade Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Fri, 6 Mar 2026 12:17:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=A8=E6=96=B0=E7=89=88=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugin_runtime/host/event_dispatcher.py | 15 +++++++++------ src/plugin_runtime/host/rpc_server.py | 4 ++-- src/plugin_runtime/runner/runner_main.py | 9 +++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/plugin_runtime/host/event_dispatcher.py b/src/plugin_runtime/host/event_dispatcher.py index c6a577f5..db117daf 100644 --- a/src/plugin_runtime/host/event_dispatcher.py +++ b/src/plugin_runtime/host/event_dispatcher.py @@ -7,7 +7,7 @@ 4. 事件结果历史记录 """ -from typing import Any, Optional +from typing import Any, Awaitable, Callable import asyncio import logging @@ -16,6 +16,9 @@ from src.plugin_runtime.host.component_registry import ComponentRegistry, Regist logger = logging.getLogger("plugin_runtime.host.event_dispatcher") +# invoke_fn 类型: async (plugin_id, component_name, args) -> response_payload dict +InvokeFn = Callable[[str, str, dict[str, Any]], Awaitable[dict[str, Any]]] + class EventResult: """单个 EventHandler 的执行结果""" @@ -44,8 +47,8 @@ class EventDispatcher: 再通过提供的 invoke_fn 回调 RPC 到 Runner 执行。 """ - def __init__(self, registry: ComponentRegistry): - self._registry = registry + def __init__(self, registry: ComponentRegistry) -> None: + self._registry: ComponentRegistry = registry self._result_history: dict[str, list[EventResult]] = {} self._history_enabled: set[str] = set() @@ -63,10 +66,10 @@ class EventDispatcher: async def dispatch_event( self, event_type: str, - invoke_fn, # async (plugin_id, component_name, args) -> dict — Supervisor.invoke_plugin wrapper + invoke_fn: InvokeFn, message: dict[str, Any] | None = None, extra_args: dict[str, Any] | None = None, - ) -> tuple[bool, Optional[dict[str, Any]]]: + ) -> tuple[bool, dict[str, Any] | None]: """分发事件到所有对应 handler。 Args: @@ -117,7 +120,7 @@ class EventDispatcher: async def _invoke_handler( self, - invoke_fn, + invoke_fn: InvokeFn, handler: RegisteredComponent, args: dict[str, Any], event_type: str, diff --git a/src/plugin_runtime/host/rpc_server.py b/src/plugin_runtime/host/rpc_server.py index 0f42f0fc..2bc7fea7 100644 --- a/src/plugin_runtime/host/rpc_server.py +++ b/src/plugin_runtime/host/rpc_server.py @@ -63,10 +63,10 @@ class RPCServer: self._pending_requests: dict[int, asyncio.Future] = {} # 发送队列(背压控制) - self._send_queue: asyncio.Queue | None = None + self._send_queue: asyncio.Queue[bytes] | None = None # 运行状态 - self._running = False + self._running: bool = False self._tasks: list[asyncio.Task] = [] @property diff --git a/src/plugin_runtime/runner/runner_main.py b/src/plugin_runtime/runner/runner_main.py index 5c458bf2..e6017121 100644 --- a/src/plugin_runtime/runner/runner_main.py +++ b/src/plugin_runtime/runner/runner_main.py @@ -9,8 +9,6 @@ 6. 转发插件的能力调用到 Host """ -from typing import Any - import asyncio import contextlib import logging @@ -26,10 +24,9 @@ from src.plugin_runtime.protocol.envelope import ( InvokePayload, InvokeResultPayload, RegisterComponentsPayload, - ShutdownPayload, ) from src.plugin_runtime.protocol.errors import ErrorCode -from src.plugin_runtime.runner.plugin_loader import PluginLoader +from src.plugin_runtime.runner.plugin_loader import PluginLoader, PluginMeta from src.plugin_runtime.runner.rpc_client import RPCClient logger = logging.getLogger("plugin_runtime.runner.main") @@ -113,7 +110,7 @@ class PluginRunner: self._rpc_client.register_method("plugin.shutdown", self._handle_shutdown) self._rpc_client.register_method("plugin.config_updated", self._handle_config_updated) - async def _register_plugin(self, meta) -> None: + async def _register_plugin(self, meta: PluginMeta) -> None: """向 Host 注册单个插件""" # 收集插件组件声明 components = [] @@ -139,7 +136,7 @@ class PluginRunner: ) try: - resp = await self._rpc_client.send_request( + _resp = await self._rpc_client.send_request( "plugin.register_components", plugin_id=meta.plugin_id, payload=reg_payload.model_dump(),