补全新版插件系统的类型注解
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
4. 事件结果历史记录
|
4. 事件结果历史记录
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any, Optional
|
from typing import Any, Awaitable, Callable
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
@@ -16,6 +16,9 @@ from src.plugin_runtime.host.component_registry import ComponentRegistry, Regist
|
|||||||
|
|
||||||
logger = logging.getLogger("plugin_runtime.host.event_dispatcher")
|
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:
|
class EventResult:
|
||||||
"""单个 EventHandler 的执行结果"""
|
"""单个 EventHandler 的执行结果"""
|
||||||
@@ -44,8 +47,8 @@ class EventDispatcher:
|
|||||||
再通过提供的 invoke_fn 回调 RPC 到 Runner 执行。
|
再通过提供的 invoke_fn 回调 RPC 到 Runner 执行。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, registry: ComponentRegistry):
|
def __init__(self, registry: ComponentRegistry) -> None:
|
||||||
self._registry = registry
|
self._registry: ComponentRegistry = registry
|
||||||
self._result_history: dict[str, list[EventResult]] = {}
|
self._result_history: dict[str, list[EventResult]] = {}
|
||||||
self._history_enabled: set[str] = set()
|
self._history_enabled: set[str] = set()
|
||||||
|
|
||||||
@@ -63,10 +66,10 @@ class EventDispatcher:
|
|||||||
async def dispatch_event(
|
async def dispatch_event(
|
||||||
self,
|
self,
|
||||||
event_type: str,
|
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,
|
message: dict[str, Any] | None = None,
|
||||||
extra_args: 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。
|
"""分发事件到所有对应 handler。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -117,7 +120,7 @@ class EventDispatcher:
|
|||||||
|
|
||||||
async def _invoke_handler(
|
async def _invoke_handler(
|
||||||
self,
|
self,
|
||||||
invoke_fn,
|
invoke_fn: InvokeFn,
|
||||||
handler: RegisteredComponent,
|
handler: RegisteredComponent,
|
||||||
args: dict[str, Any],
|
args: dict[str, Any],
|
||||||
event_type: str,
|
event_type: str,
|
||||||
|
|||||||
@@ -63,10 +63,10 @@ class RPCServer:
|
|||||||
self._pending_requests: dict[int, asyncio.Future] = {}
|
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] = []
|
self._tasks: list[asyncio.Task] = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -9,8 +9,6 @@
|
|||||||
6. 转发插件的能力调用到 Host
|
6. 转发插件的能力调用到 Host
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
@@ -26,10 +24,9 @@ from src.plugin_runtime.protocol.envelope import (
|
|||||||
InvokePayload,
|
InvokePayload,
|
||||||
InvokeResultPayload,
|
InvokeResultPayload,
|
||||||
RegisterComponentsPayload,
|
RegisterComponentsPayload,
|
||||||
ShutdownPayload,
|
|
||||||
)
|
)
|
||||||
from src.plugin_runtime.protocol.errors import ErrorCode
|
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
|
from src.plugin_runtime.runner.rpc_client import RPCClient
|
||||||
|
|
||||||
logger = logging.getLogger("plugin_runtime.runner.main")
|
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.shutdown", self._handle_shutdown)
|
||||||
self._rpc_client.register_method("plugin.config_updated", self._handle_config_updated)
|
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 注册单个插件"""
|
"""向 Host 注册单个插件"""
|
||||||
# 收集插件组件声明
|
# 收集插件组件声明
|
||||||
components = []
|
components = []
|
||||||
@@ -139,7 +136,7 @@ class PluginRunner:
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = await self._rpc_client.send_request(
|
_resp = await self._rpc_client.send_request(
|
||||||
"plugin.register_components",
|
"plugin.register_components",
|
||||||
plugin_id=meta.plugin_id,
|
plugin_id=meta.plugin_id,
|
||||||
payload=reg_payload.model_dump(),
|
payload=reg_payload.model_dump(),
|
||||||
|
|||||||
Reference in New Issue
Block a user