Files
insistence 2a055ba648 feat: 新增插件系统 (#112)
* feat: 初始化插件系统

* refactor: 收口插件系统运行时重构

* perf: 优化插件系统类型提示

* fix&perf: 修复和优化插件系统

* fix: 修复gitignore规则误忽略插件文件的问题

* fix: 修复运行时插件根路径算错的问题

* fix: 加强插件发现和路由注册的防护措施

* revert: 回滚定时任务白名单

* fix: 移除未使用的应用路由注册探测

* revert: 恢复部分代码

* perf: 优化插件系统

* docs: 新增插件开发文档

* perf: 优化插件管理模块

* perf: 提升插件系统核心能力

* refactor: 重构生命周期 step runner

* fix: 修复lint错误

* test: 清理测试用例

* test: 调整测试目录名称

* fix: 修复前后端目录硬编码的问题

* fix: 修复插件系统安全性缺口

* refactor: 重新设计插件生命周期 Migration 事务与回滚

* perf: 优化插件系统边界问题

* refactor: 重构当前插件系统的依赖体系设计

* perf: 优化代码

* perf: 优化代码

* fix: 修复代码合并问题

* fix: 修复bug

* perf: 优化代码

* perf&fix: 优化代码和修复bug

* docs: 优化文档格式

* feat: 适配Vue2版本

* docs: 更新README文档

* fix: 修复ruff lint错误

* chore: 更新后端依赖文件
2026-07-28 20:35:18 +08:00

109 lines
3.6 KiB
Python

import sys
from pathlib import Path
from subprocess import CompletedProcess
from cli.runtime.plugin.service import CliPluginRuntimeService
from plugins.core.environment import PluginRuntimeEnvironmentService
from plugins.core.validation.dependencies import (
NpmDependencyInspector,
PluginDependencyChecker,
PythonDependencyInspector,
)
EXPECTED_FRONTEND_BUILD_TIMEOUT = 300
class FakeRuntimeEnvironment:
"""
测试用插件 CLI 运行时环境服务。
"""
def __init__(self, backend_dir: Path, frontend_dir: Path | None = None) -> None:
"""初始化测试用插件 CLI 运行时环境服务。"""
self.backend_dir = backend_dir
self.frontend_dir = frontend_dir or Path(
PluginRuntimeEnvironmentService(backend_root=backend_dir).get_frontend_dir()
)
def get_backend_dir(self) -> str:
"""获取后端项目根目录。"""
return str(self.backend_dir)
def get_backend_plugins_dir(self) -> str:
"""获取后端插件根目录。"""
return str(self.backend_dir / 'plugins')
def get_frontend_dir(self) -> str:
"""获取前端项目根目录。"""
return str(self.frontend_dir)
def get_frontend_plugins_dir(self) -> str:
"""获取前端插件根目录。"""
return str(self.frontend_dir / 'plugins')
@staticmethod
def get_frontend_mode() -> str:
"""获取测试用前端运行模式。"""
return 'dev'
@staticmethod
def get_backend_runtime_mode() -> str:
"""获取测试用后端运行模式。"""
return 'dev'
@staticmethod
def get_python_executable() -> str:
"""获取测试用 Python 解释器。"""
return sys.executable
class FakePluginRuntimeGateway:
"""
测试用插件 CLI 运行时适配器。
"""
def __init__(self) -> None:
"""初始化测试用插件 CLI 运行时适配器。"""
self.completed_process = CompletedProcess(args=[], returncode=0, stdout='1 passed\n', stderr='')
self.commands: list[tuple[list[str], str, int | None]] = []
def run_command(
self,
command: list[str],
workdir: str,
*,
timeout: int | None = None,
) -> CompletedProcess[str]:
"""记录测试用系统命令。"""
self.commands.append((command, workdir, timeout))
return self.completed_process
def build_runtime(backend_root: Path, frontend_root: Path | None = None) -> CliPluginRuntimeService:
"""构建测试用插件 CLI 运行时服务。"""
return CliPluginRuntimeService(
runtime_environment=FakeRuntimeEnvironment(backend_root, frontend_root),
dependency_checker=PluginDependencyChecker(
python_inspector=PythonDependencyInspector(installed_packages={'openai': '2.17.0'}),
npm_inspector=NpmDependencyInspector(installed_packages={'vue': '3.5.26'}),
),
)
def build_runtime_with_gateway(
backend_root: Path,
gateway: FakePluginRuntimeGateway,
frontend_root: Path | None = None,
) -> CliPluginRuntimeService:
"""构建带测试运行时适配器的插件 CLI 运行时服务。"""
return CliPluginRuntimeService(
runtime_environment=FakeRuntimeEnvironment(backend_root, frontend_root),
dependency_checker=PluginDependencyChecker(
python_inspector=PythonDependencyInspector(installed_packages={'openai': '2.17.0'}),
npm_inspector=NpmDependencyInspector(installed_packages={'vue': '3.5.26'}),
),
management_gateway=gateway,
model_gateway=gateway,
command_gateway=gateway,
)