feat: Enhance plugin runtime with new component registry and workflow executor
- Introduced `ComponentRegistry` for managing plugin components with support for registration, enabling/disabling, and querying by type and plugin. - Added `EventDispatcher` to handle event distribution to registered event handlers, supporting both blocking and non-blocking execution. - Implemented `WorkflowExecutor` to manage a linear workflow execution across multiple stages, including command routing and error handling. - Created `ManifestValidator` for validating plugin manifests against required fields and version compatibility. - Updated `RPCClient` to use `MsgPackCodec` for message encoding. - Enhanced `PluginRunner` to support lifecycle hooks for plugins, including `on_load` and `on_unload`. - Added sys.path isolation to restrict plugin access to only necessary directories.
This commit is contained in:
@@ -1 +1 @@
|
||||
# Protocol 层 - RPC 消息模型、编解码、错误码
|
||||
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
"""MsgPack / JSON 编解码器
|
||||
|
||||
提供统一的消息编解码接口,生产环境默认使用 MsgPack,
|
||||
开发调试模式可切换为 JSON(仅编解码切换,传输层不变)。
|
||||
"""
|
||||
"""MsgPack 编解码器"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import json
|
||||
|
||||
import msgpack
|
||||
|
||||
from .envelope import Envelope
|
||||
@@ -30,7 +24,7 @@ class Codec:
|
||||
|
||||
|
||||
class MsgPackCodec(Codec):
|
||||
"""MsgPack 编解码器(生产默认)"""
|
||||
"""MsgPack 编解码器"""
|
||||
|
||||
def encode(self, obj: dict[str, Any]) -> bytes:
|
||||
return msgpack.packb(obj, use_bin_type=True)
|
||||
@@ -47,34 +41,3 @@ class MsgPackCodec(Codec):
|
||||
def decode_envelope(self, data: bytes) -> Envelope:
|
||||
raw = self.decode(data)
|
||||
return Envelope.model_validate(raw)
|
||||
|
||||
|
||||
class JsonCodec(Codec):
|
||||
"""JSON 编解码器(开发调试用)"""
|
||||
|
||||
def encode(self, obj: dict[str, Any]) -> bytes:
|
||||
return json.dumps(obj, ensure_ascii=False).encode("utf-8")
|
||||
|
||||
def decode(self, data: bytes) -> dict[str, Any]:
|
||||
result = json.loads(data.decode("utf-8"))
|
||||
if not isinstance(result, dict):
|
||||
raise ValueError(f"期望解码为 dict,实际为 {type(result)}")
|
||||
return result
|
||||
|
||||
def encode_envelope(self, envelope: Envelope) -> bytes:
|
||||
return self.encode(envelope.model_dump())
|
||||
|
||||
def decode_envelope(self, data: bytes) -> Envelope:
|
||||
raw = self.decode(data)
|
||||
return Envelope.model_validate(raw)
|
||||
|
||||
|
||||
def create_codec(use_json: bool = False) -> Codec:
|
||||
"""创建编解码器实例
|
||||
|
||||
Args:
|
||||
use_json: 是否使用 JSON(开发模式)。默认使用 MsgPack。
|
||||
"""
|
||||
if use_json:
|
||||
return JsonCodec()
|
||||
return MsgPackCodec()
|
||||
|
||||
Reference in New Issue
Block a user