feat: 添加插件运行时配置类,增强会话令牌管理和插件目录支持
This commit is contained in:
@@ -16,6 +16,8 @@ import os
|
||||
import sys
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
from src.plugin_runtime import ENV_IPC_ADDRESS, ENV_PLUGIN_DIRS, ENV_SESSION_TOKEN
|
||||
from src.plugin_runtime.host.capability_service import CapabilityService
|
||||
from src.plugin_runtime.host.component_registry import ComponentRegistry
|
||||
from src.plugin_runtime.host.event_dispatcher import EventDispatcher
|
||||
@@ -93,10 +95,14 @@ class PluginSupervisor:
|
||||
self,
|
||||
plugin_dirs: Optional[List[str]] = None,
|
||||
socket_path: Optional[str] = None,
|
||||
health_check_interval_sec: float = 30.0,
|
||||
health_check_interval_sec: Optional[float] = None,
|
||||
max_restart_attempts: Optional[int] = None,
|
||||
runner_spawn_timeout_sec: Optional[float] = None,
|
||||
):
|
||||
_cfg = global_config.plugin_runtime
|
||||
self._plugin_dirs = plugin_dirs or []
|
||||
self._health_interval = health_check_interval_sec
|
||||
self._health_interval = health_check_interval_sec if health_check_interval_sec is not None else _cfg.health_check_interval_sec
|
||||
self._runner_spawn_timeout = runner_spawn_timeout_sec if runner_spawn_timeout_sec is not None else _cfg.runner_spawn_timeout_sec
|
||||
|
||||
# 基础设施
|
||||
self._transport = create_transport_server(socket_path=socket_path)
|
||||
@@ -118,7 +124,7 @@ class PluginSupervisor:
|
||||
# Runner 子进程
|
||||
self._runner_process: Optional[asyncio.subprocess.Process] = None
|
||||
self._runner_generation: int = 0
|
||||
self._max_restart_attempts: int = 3
|
||||
self._max_restart_attempts: int = max_restart_attempts if max_restart_attempts is not None else _cfg.max_restart_attempts
|
||||
self._restart_count: int = 0
|
||||
|
||||
# 已注册的插件组件信息
|
||||
@@ -230,10 +236,10 @@ class PluginSupervisor:
|
||||
|
||||
# 等待 Runner 完成连接,避免 start() 返回时 Runner 尚未就绪
|
||||
try:
|
||||
await self._wait_for_runner_generation(expected_generation, timeout_sec=30.0)
|
||||
await self._wait_for_runner_generation(expected_generation, timeout_sec=self._runner_spawn_timeout)
|
||||
except TimeoutError:
|
||||
if not self._rpc_server.is_connected:
|
||||
logger.warning("Runner 未在 30s 内完成连接,后续操作可能失败")
|
||||
logger.warning(f"Runner 未在 {self._runner_spawn_timeout}s 内完成连接,后续操作可能失败")
|
||||
|
||||
# 启动健康检查
|
||||
self._health_task = asyncio.create_task(self._health_check_loop())
|
||||
@@ -305,7 +311,7 @@ class PluginSupervisor:
|
||||
# 拉起新 Runner
|
||||
try:
|
||||
await self._spawn_runner()
|
||||
await self._wait_for_runner_generation(expected_generation, timeout_sec=30.0)
|
||||
await self._wait_for_runner_generation(expected_generation, timeout_sec=self._runner_spawn_timeout)
|
||||
resp = await self._rpc_server.send_request("plugin.health", timeout_ms=5000)
|
||||
health = HealthPayload.model_validate(resp.payload)
|
||||
if not health.healthy:
|
||||
@@ -393,9 +399,9 @@ class PluginSupervisor:
|
||||
token = self._rpc_server.session_token
|
||||
|
||||
env = os.environ.copy()
|
||||
env["MAIBOT_IPC_ADDRESS"] = address
|
||||
env["MAIBOT_SESSION_TOKEN"] = token
|
||||
env["MAIBOT_PLUGIN_DIRS"] = os.pathsep.join(self._plugin_dirs)
|
||||
env[ENV_IPC_ADDRESS] = address
|
||||
env[ENV_SESSION_TOKEN] = token
|
||||
env[ENV_PLUGIN_DIRS] = os.pathsep.join(self._plugin_dirs)
|
||||
|
||||
self._runner_process = await asyncio.create_subprocess_exec(
|
||||
sys.executable, "-m", runner_module,
|
||||
|
||||
@@ -24,6 +24,7 @@ import time
|
||||
import uuid
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
from src.plugin_runtime.host.component_registry import ComponentRegistry, RegisteredComponent
|
||||
|
||||
logger = get_logger("plugin_runtime.host.workflow_executor")
|
||||
@@ -44,7 +45,9 @@ HOOK_SKIP_STAGE = "skip_stage"
|
||||
HOOK_ABORT = "abort"
|
||||
|
||||
# blocking hook 全局最大超时(秒):即使 hook 声明 timeout_ms=0 也不会无限等待
|
||||
GLOBAL_BLOCKING_TIMEOUT_SEC = 120.0
|
||||
# 从配置文件读取,允许用户调整
|
||||
def _get_blocking_timeout() -> float:
|
||||
return global_config.plugin_runtime.workflow_blocking_timeout_sec
|
||||
|
||||
|
||||
class ModificationRecord:
|
||||
@@ -300,7 +303,7 @@ class WorkflowExecutor:
|
||||
"""
|
||||
timeout_ms = step.metadata.get("timeout_ms", 0)
|
||||
# 使用 hook 声明的超时,但不超过全局安全阀
|
||||
timeout_sec = timeout_ms / 1000 if timeout_ms > 0 else GLOBAL_BLOCKING_TIMEOUT_SEC
|
||||
timeout_sec = timeout_ms / 1000 if timeout_ms > 0 else _get_blocking_timeout()
|
||||
step_key = f"{stage}:{step.full_name}"
|
||||
step_start = time.perf_counter()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user