#pragma once /** * @file plugin_manager.h * @brief 宿主侧插件管理器 / 能力注册表(C++ 便利 API,不跨 ABI 边界) * * 分工: * - vde_plugin_api.h:跨 ABI 的纯 C 契约(插件可见); * - plugin_manager.h:宿主/内核内部使用的 C++ 管理 API(加载、发现、能力查询、格式分派)。 * * 插件不链接内核;管理器把宿主服务表(VdePluginHost)在 init 时传给插件。 * 宿主应用需先调用 set_host_services(通常由 vde::plugin::make_geometry_host_services() 提供)。 */ #include #include #include #include #include #include #include "vde/plugin/vde_plugin_api.h" #include "vde/plugin/plugin_security.h" namespace vde::plugin { /// 插件种类(与 C ABI VdePluginKind 对齐) enum class PluginKind : std::uint32_t { Format = VDE_PLUGIN_KIND_FORMAT, GeomOp = VDE_PLUGIN_KIND_GEOM_OP, CaeSolver = VDE_PLUGIN_KIND_CAE_SOLVER, CamStrategy = VDE_PLUGIN_KIND_CAM_STRATEGY, Ui = VDE_PLUGIN_KIND_UI, Binding = VDE_PLUGIN_KIND_BINDING, GcsSolver = VDE_PLUGIN_KIND_GCS_SOLVER, Mesher = VDE_PLUGIN_KIND_MESHER, Optimizer = VDE_PLUGIN_KIND_OPTIMIZER, IoBatch = VDE_PLUGIN_KIND_IO_BATCH }; const char* plugin_kind_name(PluginKind kind); std::optional plugin_kind_from_string(const std::string& text); /// 语义化版本(major.minor.patch;解析失败时 valid=false) struct SemanticVersion { int major = 0; int minor = 0; int patch = 0; bool valid = false; }; SemanticVersion parse_semantic_version(const std::string& text); /// 插件清单(vde-plugin.json 的规范化结果) struct PluginManifest { std::string id; ///< 反域名唯一 ID(必填) std::string name; ///< 显示名(缺省用 id) SemanticVersion version; ///< 插件版本(必填) PluginKind kind = PluginKind::Format; std::string entry; ///< 动态库文件名(相对清单目录;可含子目录) int abi_major = 1; ///< 插件要求/构建时的宿主 ABI major int abi_minor = 0; ///< 插件构建时的 ABI minor std::vector capabilities; std::vector dependencies; std::string license; std::string signature; ///< 可选 Ed25519 签名(P1 校验) std::string manifest_path; ///< 发现时填充 std::string library_path; ///< 发现时按平台 entry 解析 }; /// 解析清单 JSON(独立函数,便于单测;失败返回 false 并写 error) bool parse_plugin_manifest(const std::string& json_text, const std::string& manifest_path, PluginManifest* out, std::string* error); /// 每插件生命周期统计(P4-49;PluginManager 线程安全维护) struct PluginStats { std::string plugin_id; ///< 插件 id std::string kind; ///< 清单 kind(format/ui/...) std::string version; ///< 清单版本字符串 bool loaded = false; ///< 当前是否已加载 std::uint64_t load_count = 0; ///< 成功加载次数(物理 load) std::uint64_t reload_count = 0; ///< 成功 reload 次数 std::uint64_t unload_count = 0; ///< 成功卸载次数(物理 unload) std::uint64_t error_count = 0; ///< 失败操作次数 double last_load_ms = 0.0; ///< 最近一次成功加载耗时(毫秒,steady_clock) std::string last_error; ///< 最近一次失败原因 }; /// 已注册格式插件信息 struct FormatPluginInfo { std::string plugin_id; std::string format_id; std::string extensions; int probe_priority = 0; ///< 预留:显式优先级 }; /// 格式导入结果(句柄由宿主服务创建,调用方负责按宿主约定销毁/转交) struct FormatImportResult { bool ok = false; std::string format_id; std::vector bodies; std::string error; }; /// 已注册 CAE 求解器信息(P2 / ABI 1.2) struct CaeSolverInfo { std::string plugin_id; std::string solver_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册 CAM 策略信息(P2 / ABI 1.2) struct CamStrategyInfo { std::string plugin_id; std::string strategy_id; std::string postprocessor_id; ///< 可为空 std::string machine_id; ///< 可为空 }; /// 已注册 GCS 约束求解器信息(P3 / ABI 1.3) struct GcsSolverInfo { std::string plugin_id; std::string solver_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册网格生成器信息(P4 / ABI 1.4) struct MesherInfo { std::string plugin_id; std::string mesher_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册优化器信息(P4 / ABI 1.5) struct OptimizerInfo { std::string plugin_id; std::string optimizer_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册批量导入导出处理器信息(P4 / ABI 1.5) struct IoBatchInfo { std::string plugin_id; std::string batch_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册几何算子信息(P4 / ABI 1.7) struct GeomOpInfo { std::string plugin_id; std::string op_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册 UI 主题插件信息(P4 / ABI 1.8) struct UiPluginInfo { std::string plugin_id; std::string ui_id; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 已注册语言绑定插件信息(P4 / ABI 1.8) struct BindingPluginInfo { std::string plugin_id; std::string binding_id; std::string language; std::string capabilities_json; ///< 插件声明的能力描述(JSON,可为空) }; /// 进程外服务 OS 级资源限制(清单 limits 字段,P3-4) /// /// 全为 0/false 表示不限制(保持 P2-4 行为);声明后由宿主用 Windows Job Object /// 或 POSIX setrlimit/setsid 强制。语义见 docs/design/vde-service-protocol.md §7。 struct ExternalServiceLimits { int max_memory_mb = 0; ///< 内存上限 MB(Windows JobMemoryLimit / POSIX RLIMIT_AS) int max_cpu_seconds = 0; ///< CPU 时间上限秒(Windows 每进程用户态时间 / POSIX RLIMIT_CPU) int max_processes = 0; ///< 作业内最大进程数(Windows ActiveProcessLimit / POSIX RLIMIT_NPROC 尽力而为) bool kill_on_close = false; ///< 宿主关闭作业/调用结束时杀死残留子进程(Windows KILL_ON_JOB_CLOSE) bool empty() const { return max_memory_mb <= 0 && max_cpu_seconds <= 0 && max_processes <= 0 && !kill_on_close; } }; /// 进程外服务沙箱策略(清单 sandbox 扩展字段,P3-14b) /// /// 语义:声明即契约——宿主必须强制,无法强制时默认拒绝调用(返回 unsupported_sandbox); /// 只有 allow_unsandboxed=true 时才允许显式降级(记录 sandbox_downgraded/warnings)。 /// 兼容性:旧的 sandbox.network=false/true(bool)与 filesystem="request-dir" /// 保持“声明性元数据”语义,不触发强制/拒绝(declared=false),避免破坏既有清单。 struct ExternalServiceSandboxPolicy { std::string network = "inherit"; ///< "inherit" | "none" std::string filesystem = "read_write";///< "read_write" | "read_only" std::string isolation = "process"; ///< "process" | "none" bool allow_unsandboxed = false; ///< 显式逃生:无法强制时允许降级运行(默认 false) bool declared = false; ///< 是否使用新风格策略字段(决定是否强制) }; /// 进程外服务容器运行时规格(清单 sandbox.container,P3-14c) /// /// 声明后服务在 docker/podman 容器内运行(真实隔离路径);宿主只挂载工作目录 /// (workdir -> /vde/work),不注入任意宿主机路径;无可用 runtime 时按 P3-14b /// 语义返回 unsupported_sandbox,allow_unsandboxed 可降级为直接进程执行。 struct ExternalServiceContainerSpec { bool enabled = false; ///< 清单是否声明 container std::string runtime = "auto"; ///< "auto" | "docker" | "podman" std::string image; ///< 镜像名(必填、非空) std::string network = "none"; ///< "none"(--network none)| "inherit"(不传 network 参数) bool read_only = true; ///< 容器根文件系统只读(--read-only + --tmpfs /tmp) }; /// 进程外服务清单(vde-service.json 的规范化结果,P2-4) /// /// 进程外服务(external service)与进程内插件(VdePluginRegistration)并列: /// 清单发现、请求/响应文件交换、超时/退出码映射,见 docs/design/vde-service-protocol.md。 struct ExternalServiceInfo { std::string id; ///< 反域名唯一 ID(必填) std::string name; ///< 显示名(缺省 = id) SemanticVersion version; ///< 服务版本(必填,严格 x.y.z) std::string protocol_name; ///< 协议名(应为 vde-service) std::string protocol_version; ///< 协议版本(应为 0.1) std::string executable; ///< 按平台解析后的可执行文件路径(相对清单目录解析) std::string manifest_path; ///< 清单路径 std::string directory; ///< 清单所在目录 std::vector capabilities; ///< 能力 ID(与插件能力同规范) int priority = 0; ///< 清单 priority(P3-14;越大越优先,默认 0) int request_timeout_ms = 30000; ///< 清单 timeouts.request_ms std::vector env_allow; ///< sandbox.env_allow:允许透传给服务的环境变量名 bool allow_network = false; ///< 清单声明(P2 仅记录,执行层强制留 P3) ExternalServiceLimits limits; ///< 清单 limits(P3-4 OS 级沙箱;默认不限制) ExternalServiceSandboxPolicy sandbox_policy; ///< 清单 sandbox 策略(P3-14b) ExternalServiceContainerSpec container; ///< 清单 sandbox.container(P3-14c) std::string executable_entry; ///< 清单 executable 原始值(容器内命令用) std::string license; ///< SPDX / 商业许可说明 }; /// 解析进程外服务清单(独立函数,便于单测;失败返回 false 并写 error) bool parse_service_manifest(const std::string& json_text, const std::string& manifest_path, ExternalServiceInfo* out, std::string* error); /// 进程外服务调用选项(P2-4) struct ExternalServiceInvokeOptions { int timeout_ms = 0; ///< 0 = 清单 timeouts.request_ms(宿主硬上限 300s) std::string work_root; ///< 自动工作目录的父目录(空 = 沙箱根/系统临时目录) std::string work_dir; ///< 显式工作目录(必须在沙箱白名单内;空 = 自动生成) std::string request_path; ///< 非空:从文件读取请求(优先于 request_json) std::string response_path; ///< 非空:指定响应文件(必须在 work_dir 内) bool keep_work_dir = true; ///< false:返回前删除工作目录(默认保留供调用方取 outputs) }; /// 进程外服务调用结果(失败隔离:服务崩溃/坏响应只置标志,宿主不抛出) struct ExternalServiceResult { bool ok = false; ///< 端到端成功(响应 status=ok 且退出码 0 且路径校验通过) bool started = false; ///< 子进程是否成功创建 bool timed_out = false; ///< 超时并已强制终止服务进程 bool protocol_error = false; ///< 清单/响应缺失/坏 JSON/status=error/输出越界/request_id 不匹配 bool crashed = false; ///< 服务异常退出(非预期退出码,或声明 ok 但退出码非 0) bool limit_exceeded = false; ///< 是否因 OS 级沙箱限制被终止(内存/CPU;P3-4) bool sandbox_unsupported = false; ///< 声明了无法强制的沙箱策略且未允许逃生(P3-14b) bool sandbox_downgraded = false; ///< 声明策略无法强制但 allow_unsandboxed=true,降级运行 bool sandbox_enforced = false; ///< 声明的沙箱策略全部按声明强制 std::vector sandbox_warnings; ///< 沙箱相关诊断(降级原因等) bool container_used = false; ///< 本次调用是否经容器 runtime 执行(P3-14c) std::string container_runtime; ///< 实际使用的容器 runtime 名(docker/podman) std::string limit_kind; ///< 命中的限制类型:memory / cpu / processes(可为空) int exit_code = -1; ///< 服务进程退出码(-1 = 未启动/被信号终止) int duration_ms = 0; ///< 墙钟耗时 int timeout_ms = 0; ///< 本次生效超时 std::string service_id; ///< 服务 ID std::string request_id; ///< 宿主生成/注入并校验回显的 request_id std::string status; ///< 响应 status(ok/error;缺失为空) std::string error_code; ///< 响应 error.code(status=error 时) std::string error; ///< 宿主侧错误描述(含 stderr 摘要) std::string response_json; ///< 原始响应文本(成功/失败均保留,便于诊断) std::string work_dir; ///< 本次工作目录(keep_work_dir=false 时可能已删除) std::string response_path; ///< 响应文件路径 std::vector outputs; ///< 响应 outputs[].path(解析为绝对路径) std::vector diagnostics; ///< 响应 diagnostics[] std::string stdout_excerpt; ///< 服务 stdout(截断到 service_output_limit) std::string stderr_excerpt; ///< 服务 stderr(截断到 service_output_limit) }; /** * @brief 能力注册表:capability ID → 提供者插件 ID * * 插件之间不直接链接,通过此表按能力 ID 互相发现(跨插件通信由宿主中介)。 */ class CapabilityRegistry { public: CapabilityRegistry(); ~CapabilityRegistry(); CapabilityRegistry(const CapabilityRegistry&) = delete; CapabilityRegistry& operator=(const CapabilityRegistry&) = delete; void add(const std::string& capability, const std::string& plugin_id); void remove_provider(const std::string& plugin_id); bool has(const std::string& capability) const; std::optional provider(const std::string& capability) const; std::vector capabilities() const; std::vector providers() const; void clear(); std::size_t size() const; /// 重复能力注册的冲突记录(按"首次提供者为准"忽略后来的提供者;供诊断/测试) std::vector conflicts() const; private: struct Impl; std::unique_ptr impl_; }; /** * @brief 插件管理器(进程级单例) * * 典型用法: * auto& pm = PluginManager::instance(); * pm.set_host_services(vde::plugin::make_geometry_host_services()); * pm.add_search_path("plugins"); * pm.scan_search_paths(); * for (auto& m : pm.plugins()) pm.load(m.id); * auto fmt = pm.formats(); * FormatImportResult r; * pm.import_file("a.vbox", &r); */ class PluginManager { public: static PluginManager& instance(); ~PluginManager(); PluginManager(const PluginManager&) = delete; PluginManager& operator=(const PluginManager&) = delete; // ── 宿主服务 ── void set_host_services(const VdePluginHost& host); /// 清除宿主服务表(此后 load()/import_file()/export_file() 会以"未配置宿主服务"失败) void clear_host_services(); bool has_host_services() const; const VdePluginHost& host_services() const; // ── 发现 ── void add_search_path(const std::string& dir); void clear_search_paths(); /// 递归扫描目录(最多 2 层)找 vde-plugin.json;返回新增清单数 std::size_t scan_directory(const std::string& dir, std::string* error = nullptr); /// 扫描全部搜索路径;返回新增清单数 std::size_t scan_search_paths(std::string* error = nullptr); // ── 加载/卸载 ── bool load(const std::string& plugin_id, std::string* error = nullptr); // ── P3-1 安全策略(宿主可选启用;默认关闭保持既有行为)── /// 设置安全策略(require_signature/allow_unsigned/publisher 白名单/权限)。 /// 设置后 load() 在 dlopen 前校验清单签名/发布者/权限;未设置则不做安全校验。 void set_security_policy(const PluginSecurityPolicy& policy); /// 注入验签器与 32B Ed25519 公钥(宿主可用 vde_license/KMS 实现) void set_security_verifier(SignatureVerifier* verifier, std::vector public_key = {}); /// 关闭安全校验(恢复 P3-1 之前行为;已加载插件不受影响) void clear_security(); /// 是否启用安全校验 bool security_enabled() const; /// 最近一次安全校验报告(plugin_id 未校验过 → code=Ok 且 message 为空) PluginSecurityReport security_report(const std::string& plugin_id) const; /** * @brief 卸载插件(依赖感知):若仍有已加载插件依赖 plugin_id,默认拒绝并列出 * 依赖者;无依赖时行为不变;未加载时幂等成功。 * @param error [out] 失败原因(依赖者列表/未找到等) */ bool unload(const std::string& plugin_id, std::string* error = nullptr); /// 显式级联卸载:依赖者先卸载(先依赖者后被依赖),未加载时幂等成功。 bool unload_cascade(const std::string& plugin_id, std::string* error = nullptr); /// `unload(plugin_id, cascade=true, error)` 便捷重载(cascade=true 等价 unload_cascade) bool unload(const std::string& plugin_id, bool cascade, std::string* error = nullptr); /** * @brief 热重载插件:已加载时先卸载自身及其已加载依赖者,再按依赖顺序重新加载; * 新版本加载失败时回滚到旧版本(重新加载原插件及其依赖者)并返回错误; * 未加载时等价 load(plugin_id)。重复 reload 幂等。 */ bool reload(const std::string& plugin_id, std::string* error = nullptr); bool is_loaded(const std::string& plugin_id) const; // ── 可观测性(P4-49)── /// 查询单个插件统计;未知插件(未发现且无统计)返回 nullopt std::optional plugin_stats(const std::string& plugin_id) const; /// 所有已发现/有统计插件(按 plugin_id 升序) std::vector plugin_stats() const; /// 确定性诊断 JSON 数组(字段顺序固定、插件按 id 升序;空 → "[]") std::string diagnostics_json() const; // ── 查询 ── std::vector plugins() const; std::optional manifest(const std::string& plugin_id) const; std::vector formats() const; // ── CAE / CAM 扩展点(P2 / ABI 1.2)── /// 已加载并注册的 CAE 求解器(按加载顺序;同一 solver_id 首次注册为准) std::vector cae_solvers() const; /// 已加载并注册的 CAM 策略/后处理 std::vector cam_strategies() const; /** * @brief 调用已注册 CAE 求解器(失败隔离:插件异常/负错误码/空结果均返回 false+error) * @param solver_id 求解器 ID(cae_solvers() 中) * @param model_data VDE 序列化模型数据(VDE-TEXT/二进制;插件用 body_from_serialized 还原) * @param model_size 数据字节数 * @param params_json 求解参数 JSON(可为空串) * @param result_json [out] 插件返回的 UTF-8 JSON */ bool solve_cae(const std::string& solver_id, const void* model_data, std::size_t model_size, const std::string& params_json, std::string* result_json, std::string* error = nullptr); /** * @brief 调用已注册 CAM 策略(G-code/JSON 输出;失败隔离同上) */ bool invoke_cam(const std::string& strategy_id, const void* model_data, std::size_t model_size, const std::string& args_json, std::string* output, std::string* error = nullptr); // ── GCS 约束求解器扩展点(P3 / ABI 1.3)── /// 已加载并注册的 GCS 求解器(按加载顺序;同一 solver_id 首次注册为准) std::vector gcs_solvers() const; /** * @brief 调用已注册 GCS 求解器(失败隔离:未知 id/坏草图 JSON/插件异常/空结果 → * false + error,不影响后续调用) * @param solver_id 求解器 ID(gcs_solvers() 中) * @param sketch_data 草图 JSON(UTF-8 文本;字节数 sketch_size) * @param sketch_size 草图 JSON 字节数 * @param params_json 求解参数 JSON(可为空串) * @param result_json [out] 插件返回的 UTF-8 JSON(positions/constraint_status/dof) */ bool solve_gcs(const std::string& solver_id, const void* sketch_data, std::size_t sketch_size, const std::string& params_json, std::string* result_json, std::string* error = nullptr); // ── Mesher 网格生成扩展点(P4 / ABI 1.4)── /// 已加载并注册的网格生成器(按加载顺序;同一 mesher_id 首次注册为准) std::vector meshers() const; /** * @brief 调用已注册网格生成器(失败隔离:未知 id/空模型/插件异常/非 OK 状态 → * false + error,不影响后续调用) * @param mesher_id 网格器 ID(meshers() 中) * @param model_data VDE 序列化模型数据(VDE-TEXT/二进制) * @param model_size 数据字节数 * @param options_json 网格参数 JSON(可空;支持 element_size/tolerance/element_type) * @param mesh_json [out] 插件返回的 UTF-8 网格 JSON(nodes/elements/diagnostics) */ bool invoke_mesher(const std::string& mesher_id, const void* model_data, std::size_t model_size, const std::string& options_json, std::string* mesh_json, std::string* error = nullptr); // ── Optimizer / IO_BATCH 扩展点(P4 / ABI 1.5)── /// 已加载并注册的优化器(按加载顺序;同一 optimizer_id 首次注册为准) std::vector optimizers() const; /** * @brief 调用已注册优化器(失败隔离:未知 id/空输入/插件异常/非 OK 状态 → * false + error,不影响后续调用) * @param optimizer_id 优化器 ID(optimizers() 中) * @param options_json 优化参数 JSON(可空;max_iterations/tolerance/flags) * @param input_json 优化问题 JSON(UTF-8;字节数 input_size) * @param input_size input_json 字节数 * @param result_json [out] 插件返回的 UTF-8 JSON(objective/iterations/solution) */ bool invoke_optimizer(const std::string& optimizer_id, const std::string& options_json, const void* input_json, std::size_t input_size, std::string* result_json, std::string* error = nullptr); /// 已加载并注册的批量导入导出处理器 std::vector io_batches() const; /** * @brief 调用已注册批量处理器(失败隔离同上) * @param batch_id 批处理器 ID(io_batches() 中) * @param options_json 参数 JSON(可空;format/flags) * @param request_json 批量请求 JSON(UTF-8;字节数 request_size) * @param response_json [out] 插件返回的 UTF-8 JSON(processed/failed/items) */ bool invoke_io_batch(const std::string& batch_id, const std::string& options_json, const void* request_json, std::size_t request_size, std::string* response_json, std::string* error = nullptr); // ── GEOM_OP 几何算子扩展点(P4 / ABI 1.7)── /// 已加载并注册的几何算子(按加载顺序;同一 op_id 首次注册为准) std::vector geom_ops() const; /** * @brief 调用已注册几何算子(失败隔离:未知 id/空模型/插件异常/空结果 → * false + error,不影响后续调用) * @param op_id 算子 ID(geom_ops() 中) * @param model_data VDE 序列化模型数据(VDE-TEXT/二进制) * @param model_size 数据字节数 * @param params_json 算子参数 JSON(可空;如 {"op":"volume"}) * @param result_json [out] 插件返回的 UTF-8 JSON */ bool invoke_geometry_op(const std::string& op_id, const void* model_data, std::size_t model_size, const std::string& params_json, std::string* result_json, std::string* error = nullptr); // ── UI / BINDING 扩展点(P4 / ABI 1.8)── /// 已加载并注册的 UI 主题插件 std::vector ui_plugins() const; /// 调用 UI 插件生成主题 JSON(options_json 可空) bool ui_theme_json(const std::string& ui_id, const std::string& options_json, std::string* result_json, std::string* error = nullptr); /// 已加载并注册的语言绑定插件 std::vector binding_plugins() const; /// 调用绑定插件生成元信息 JSON(ABI 1.8 兼容入口;等价 options_json 为空) bool binding_manifest_json(const std::string& binding_id, std::string* result_json, std::string* error = nullptr); /** * @brief 带 options 的绑定元信息调用(P4-60 / ABI 1.9) * @param options_json 绑定选项 JSON(如 {"language":"python"};可空) * 优先调用 VdeBindingPlugin.get_manifest_json_ex;旧 1.8 插件回退 get_manifest_json。 */ bool binding_manifest_json(const std::string& binding_id, const std::string& options_json, std::string* result_json, std::string* error = nullptr); // ── 进程外服务(vde-service,P2-4)── void add_service_search_path(const std::string& dir); void clear_service_search_paths(); /// 扫描目录(最多 2 层)找 vde-service.json;返回新增服务数 std::size_t scan_service_directory(const std::string& dir, std::string* error = nullptr); /// 扫描全部服务搜索路径;返回新增服务数 std::size_t scan_service_search_paths(std::string* error = nullptr); void clear_external_services(); std::vector external_services() const; std::optional external_service(const std::string& service_id) const; /// 沙箱工作目录白名单根(空 = 系统临时目录;所有 work_dir 必须位于其下) void set_service_sandbox_root(const std::string& dir); std::string service_sandbox_root() const; /// 子进程 stdout/stderr 捕获上限(字节,超出截断并追加标记;默认 256 KiB) void set_service_output_limit(std::size_t bytes); std::size_t service_output_limit() const; /** * @brief 调用进程外服务(P2-4):写 request.json → 沙箱内启动服务可执行文件 → * 超时强制终止 → 读取 response.json 并校验协议/输出路径白名单/request_id。 * * 失败隔离:服务崩溃、坏响应、超时都只写 result(ok=false + 细分标志),不抛异常; * 旧插件行为不受影响。工作目录默认保留(result.work_dir)供调用方取 outputs。 */ bool invoke_external_service(const std::string& service_id, const std::string& request_json, ExternalServiceResult* result, const std::string& work_root = std::string(), int timeout_ms = 0, std::string* error = nullptr); /// 便捷重载:默认选项(自动工作目录 + 清单超时),失败原因写 error bool invoke_external_service(const std::string& service_id, const std::string& request_json, ExternalServiceResult* result, std::string* error); /// 带完整选项(request_path/response_path/work_dir/timeout/keep_work_dir)的版本 bool invoke_external_service(const std::string& service_id, const std::string& request_json, const ExternalServiceInvokeOptions& options, ExternalServiceResult* result, std::string* error = nullptr); // ── 进程外服务能力路由(P3-14)── /// 返回声明了该 capability 的外部服务;确定性顺序:priority 降序,再 service id 升序 std::vector services_with_capability(const std::string& capability) const; /// 选择首个候选(与 services_with_capability 同序);无候选返回 nullopt std::optional select_external_service(const std::string& capability) const; /** * @brief 按 capability 调用外部服务(P3-14):候选按 priority 降序/id 升序依次尝试, * 失败(协议错误/超时/崩溃/沙箱限制)自动回退下一个候选;成功时写 selected_service_id。 * * 无候选 → false + 明确错误;全部失败 → false + 聚合错误,result 为最后一次尝试结果。 * 失败隔离与 invoke_external_service 一致,宿主不抛出。 */ bool invoke_external_service_by_capability(const std::string& capability, const std::string& request_json, ExternalServiceResult* result, std::string* selected_service_id = nullptr, std::string* error = nullptr); /// 带完整调用选项的按能力调用(options 对每个候选生效) bool invoke_external_service_by_capability(const std::string& capability, const std::string& request_json, const ExternalServiceInvokeOptions& options, ExternalServiceResult* result, std::string* selected_service_id = nullptr, std::string* error = nullptr); CapabilityRegistry& capabilities(); const CapabilityRegistry& capabilities() const; const std::string& last_error() const; // ── 格式分派(P0) ── /// 按格式插件 probe/扩展名选择插件并导入;bodies 所有权归调用方(宿主服务句柄) bool import_file(const std::string& path, FormatImportResult* out); /// 按插件写出(format_id 为空时按扩展名/首个写插件选择) bool export_file(const std::string& path, const std::vector& bodies, const std::string& format_id = std::string()); /// 清空全部已发现/已加载插件(卸载动态库、清注册表;测试用) void clear(); private: PluginManager(); struct Impl; std::unique_ptr impl_; }; } // namespace vde::plugin