zerolaunch_plugin_api/plugin/plugin_trait.rs
1use crate::config::configurable::Configurable;
2use crate::host::plugin_handle::PluginHandle;
3use crate::plugin::types::{PanelInteraction, PluginContext, PluginError, Query, QueryResponse};
4use async_trait::async_trait;
5use std::sync::Arc;
6
7/// 所有插件对象都需实现的核心契约。
8/// 服务于插件生命周期管理、查询处理与动作执行。
9/// 配置管理能力由 Configurable trait 提供。
10#[async_trait]
11pub trait Plugin: Configurable {
12 /// 插件初始化钩子。
13 ///
14 /// `handle` 为宿主注入的平台服务句柄:进程内(内置)插件持有
15 /// `Some(Arc<PluginHandle>)`;远端插件进程无宿主句柄(跨进程不可序列化),
16 /// 收到 `None`,平台能力经 SDK `host()` 的 host/* RPC 访问。
17 async fn init(
18 &self,
19 ctx: &PluginContext,
20 handle: Option<Arc<PluginHandle>>,
21 ) -> Result<(), PluginError>;
22
23 async fn query(&self, ctx: &PluginContext, query: &Query)
24 -> Result<QueryResponse, PluginError>;
25
26 /// 执行插件动作。
27 /// `action_id` 为插件在 `ListItem.actions` / `CustomPanel.actions` / 面板按键
28 /// 绑定中声明的动作标识;`payload` 形状由触发通道决定(插件应两种都兼容):
29 /// - 候选确认通道(搜索栏结果列表 / 行内参数 / 参数面板确认):
30 /// `{"candidate_id": <u64>, "query_text": <str>, "user_args": [<str>]}`;
31 /// - 面板动作通道(`PanelKeyAction::Custom` 面板按键绑定):插件自定义自由 JSON,
32 /// 宿主原样透传。
33 async fn execute_action(
34 &self,
35 ctx: &PluginContext,
36 action_id: &str,
37 payload: serde_json::Value,
38 ) -> Result<(), PluginError>;
39
40 /// 返回插件当前生效的交互策略(防抖延迟、提交行为等)。
41 /// 这是一次快速、同步的调用,不涉及 IO 或网络。
42 /// 默认返回无防抖、Execute 提交的交互策略。
43 fn interaction_policy(&self) -> PanelInteraction {
44 PanelInteraction::default()
45 }
46}