Skip to main content

zerolaunch_plugin_host/
info.rs

1//! 插件展示条目 —— 插件管理页 / CLI 的插件级视图。
2//!
3//! 字段权威是 `PluginMetadata`(第三方取自清单,内置由代码声明);本类型只追加
4//! 运行态三件事:进程状态、启用态、已注册组件 id。构造必须经 `from_metadata`,
5//! 不再允许各处自行拼装,避免同一行数据出现多份字段来源。
6
7use serde::Serialize;
8use zerolaunch_plugin_api::plugin::{PluginKind, PluginMetadata, PluginMode};
9
10use crate::manager::PluginRuntimeState;
11
12/// 插件列表 / 详情共用的插件级条目(跨 IPC 序列化,键名为 camelCase)。
13#[derive(Debug, Clone, Serialize)]
14pub struct PluginInfo {
15    /// 插件 id。序列化键名 `pluginId`。
16    #[serde(rename = "pluginId")]
17    pub plugin_id: String,
18    #[serde(rename = "name")]
19    pub name: String,
20    #[serde(rename = "version")]
21    pub version: String,
22    #[serde(rename = "description")]
23    pub description: String,
24    #[serde(rename = "author")]
25    pub author: String,
26    /// 运行状态:已加载取进程状态,未加载固定为 error。
27    #[serde(rename = "state")]
28    pub state: PluginRuntimeState,
29    /// 是否启用:已加载看组件启用态,未加载恒为 false。
30    #[serde(rename = "enabled")]
31    pub enabled: bool,
32    /// 插件种类:内置或第三方。
33    #[serde(rename = "kind", default)]
34    pub kind: PluginKind,
35    #[serde(rename = "priority")]
36    pub priority: u32,
37    /// 已注册组件 id(前端据此关联 ConfigManager 中的组件配置;未加载为空)。
38    #[serde(rename = "componentIds", default)]
39    pub component_ids: Vec<String>,
40    /// 全局唤醒快捷键(如 "Ctrl+E"),可空。
41    #[serde(rename = "hotkey", default)]
42    pub hotkey: Option<String>,
43    /// 插件显示图标(data URL),可空表示无图标。
44    #[serde(rename = "icon", default)]
45    pub icon: Option<String>,
46    /// 插件形态:inline = 行内插件;panel = 独立插件(唯一权威依据)。
47    #[serde(rename = "mode", default)]
48    pub mode: PluginMode,
49}
50
51impl PluginInfo {
52    /// 由插件元数据投影为展示条目。
53    /// 参数:meta - 插件级元数据(字段唯一来源);state - 进程状态;
54    /// component_ids - 已注册组件 id;enabled - 启用态。
55    /// 返回:IPC 展示条目。
56    pub fn from_metadata(
57        meta: &PluginMetadata,
58        state: PluginRuntimeState,
59        component_ids: Vec<String>,
60        enabled: bool,
61    ) -> Self {
62        Self {
63            plugin_id: meta.id.clone(),
64            name: meta.name.clone(),
65            version: meta.version.clone(),
66            description: meta.description.clone(),
67            author: meta.author.clone(),
68            state,
69            enabled,
70            kind: meta.kind,
71            priority: meta.priority,
72            component_ids,
73            hotkey: meta.hotkey.clone(),
74            icon: meta.icon.clone(),
75            mode: meta.mode,
76        }
77    }
78}