Skip to main content

haloforge_plugin_api/
manifest.rs

1use serde::{Deserialize, Serialize};
2use crate::permissions::Permission;
3
4/// Full parsed plugin manifest (from manifest.json inside .hfpkg).
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct PluginManifest {
7    pub id: String,
8    pub name: String,
9    pub version: String,
10    pub description: String,
11    #[serde(default)]
12    pub long_description: Option<String>,
13    pub author: String,
14    #[serde(default)]
15    pub author_url: Option<String>,
16    #[serde(default)]
17    pub homepage: Option<String>,
18    #[serde(default)]
19    pub license: Option<String>,
20    #[serde(default)]
21    pub keywords: Vec<String>,
22    #[serde(default)]
23    pub icon: Option<String>,
24
25    pub compatibility: CompatibilitySpec,
26
27    /// Which capability levels this plugin uses (e.g. [1, 4]).
28    pub capability_levels: Vec<CapabilityLevel>,
29
30    /// Per-level integration configuration.
31    #[serde(default)]
32    pub integration: IntegrationConfig,
33
34    /// Declarative host window policy for plugin routes and file/resource handlers.
35    #[serde(default)]
36    pub window: WindowPolicyConfig,
37
38    /// Entry points for native library and frontend bundle.
39    #[serde(default)]
40    pub entry: EntryConfig,
41
42    /// Other plugin IDs this plugin depends on.
43    #[serde(default)]
44    pub dependencies: Vec<PluginDependency>,
45
46    /// Declared permissions (checked at install time and enforced at runtime).
47    #[serde(default)]
48    pub permissions: Vec<Permission>,
49
50    /// Declarative access to stable host-side capability groups.
51    /// These should match the host hooks used from `@haloforge/plugin-sdk`.
52    #[serde(default)]
53    pub host_capabilities: Vec<HostCapability>,
54
55    /// JSON Schema for plugin settings (auto-rendered in Plugin Manager).
56    #[serde(default)]
57    pub settings_schema: Option<serde_json::Value>,
58
59    /// IPC commands this plugin registers (informational, for documentation).
60    #[serde(default)]
61    pub commands: Vec<CommandDeclaration>,
62
63    /// SHA-256 checksum of the .hfpkg file. Required for published plugins.
64    #[serde(default)]
65    pub checksum: Option<String>,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct CompatibilitySpec {
70    pub min_app_version: String,
71    #[serde(default)]
72    pub min_host_api_version: Option<String>,
73    #[serde(default)]
74    pub max_app_version: Option<String>,
75    #[serde(default = "all_platforms")]
76    pub platforms: Vec<String>,
77}
78
79fn all_platforms() -> Vec<String> {
80    vec!["windows".into(), "macos".into(), "linux".into()]
81}
82
83/// Capability level integer constants (matching the design doc).
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
85#[serde(from = "u8", into = "u8")]
86pub enum CapabilityLevel {
87    /// Level 0 — Top-level module (same tier as DevKit/AIChat).
88    Module = 0,
89    /// Level 1 — Feature inside an existing module.
90    ModuleFeature = 1,
91    /// Level 2 — UI slot injection / extension.
92    UiExtension = 2,
93    /// Level 3 — AI assistant registration.
94    AiAssistant = 3,
95    /// Level 4 — Headless service / backend extension.
96    Service = 4,
97}
98
99/// Stable, documented host capability groups for black-box-compatible plugins.
100#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
101#[serde(rename_all = "snake_case")]
102pub enum HostCapability {
103    Navigation,
104    AppState,
105    FileIntents,
106    FileDialogs,
107    #[serde(rename = "aichat")]
108    AiChat,
109    EnterpriseGateway,
110    DeepLinks,
111    ThemeRead,
112    EventSubscribe,
113}
114
115impl HostCapability {
116    pub fn as_str(&self) -> &'static str {
117        match self {
118            Self::Navigation => "navigation",
119            Self::AppState => "app_state",
120            Self::FileIntents => "file_intents",
121            Self::FileDialogs => "file_dialogs",
122            Self::AiChat => "aichat",
123            Self::EnterpriseGateway => "enterprise_gateway",
124            Self::DeepLinks => "deep_links",
125            Self::ThemeRead => "theme_read",
126            Self::EventSubscribe => "event_subscribe",
127        }
128    }
129}
130
131impl From<u8> for CapabilityLevel {
132    fn from(v: u8) -> Self {
133        match v {
134            0 => Self::Module,
135            1 => Self::ModuleFeature,
136            2 => Self::UiExtension,
137            3 => Self::AiAssistant,
138            4 => Self::Service,
139            _ => Self::Service,
140        }
141    }
142}
143
144impl From<CapabilityLevel> for u8 {
145    fn from(l: CapabilityLevel) -> u8 {
146        l as u8
147    }
148}
149
150/// Integration configuration block — one sub-block per declared level.
151#[derive(Debug, Clone, Default, Serialize, Deserialize)]
152pub struct IntegrationConfig {
153    #[serde(default)]
154    pub level0: Option<Level0Config>,
155    #[serde(default)]
156    pub level1: Option<Level1Config>,
157    #[serde(default)]
158    pub level2: Option<Level2Config>,
159    #[serde(default)]
160    pub level3: Option<Level3Config>,
161    #[serde(default)]
162    pub level4: Option<Level4Config>,
163}
164
165/// Declarative window behavior requested by a plugin.
166///
167/// The host owns actual window creation and decides whether a request is allowed,
168/// but these fields let a plugin describe how its routes and resources should be
169/// opened when invoked from menus, deeplinks, file associations, or other plugins.
170#[derive(Debug, Clone, Default, Serialize, Deserialize)]
171pub struct WindowPolicyConfig {
172    /// Default open behavior for plugin routes: "smart", "current", "new_window",
173    /// "reuse_existing", or "reuse_or_new".
174    #[serde(default)]
175    pub default_open_mode: Option<String>,
176    /// Default reuse key: "plugin", "route", "resource", or "none".
177    #[serde(default)]
178    pub reuse_key: Option<String>,
179    /// Whether this plugin may have more than one window at the same time.
180    /// Omitted means the host default is used.
181    #[serde(default)]
182    pub allow_multiple: Option<bool>,
183    /// File/resource handlers declared by this plugin.
184    #[serde(default)]
185    pub document_handlers: Vec<DocumentHandlerConfig>,
186}
187
188/// File or resource entry point that can be routed into a plugin panel.
189#[derive(Debug, Clone, Default, Serialize, Deserialize)]
190pub struct DocumentHandlerConfig {
191    /// Stable handler id, unique within the plugin. Defaults to the route when omitted.
192    #[serde(default)]
193    pub id: Option<String>,
194    /// User-facing handler label, used in menus and open-with UI.
195    #[serde(default)]
196    pub label: Option<String>,
197    /// Lowercase extensions including the leading dot, for example [".md"].
198    #[serde(default)]
199    pub extensions: Vec<String>,
200    /// MIME types handled by this plugin.
201    #[serde(default)]
202    pub mime_types: Vec<String>,
203    /// Plugin route opened for this resource.
204    pub route: String,
205    /// Query parameter that receives the resource path/URI. Defaults to "path".
206    #[serde(default)]
207    pub resource_param: Option<String>,
208    /// Handler-specific open behavior; falls back to the plugin window policy.
209    #[serde(default)]
210    pub open_mode: Option<String>,
211    /// Handler-specific reuse key; falls back to the plugin window policy.
212    #[serde(default)]
213    pub reuse_key: Option<String>,
214    /// Handler-specific multiple-window allowance; falls back to the plugin policy.
215    #[serde(default)]
216    pub allow_multiple: Option<bool>,
217}
218
219/// Level 0 — The plugin adds a new top-level module to the sidebar.
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct Level0Config {
222    /// Unique module ID (must not collide with "devkit", "aichat", "settings").
223    pub module_id: String,
224    pub module_label: String,
225    /// Lucide icon name.
226    pub module_icon: String,
227    /// "main" = above the settings divider; "bottom" = below it.
228    #[serde(default = "default_sidebar_position")]
229    pub sidebar_position: String,
230    /// Lower = higher up. Defaults to 100.
231    #[serde(default = "default_sidebar_order")]
232    pub sidebar_order: u32,
233    /// Path inside the package to the JS bundle for this module's panel.
234    pub panel_entry: String,
235}
236
237fn default_sidebar_position() -> String { "main".into() }
238fn default_sidebar_order() -> u32 { 100 }
239
240/// Level 1 — The plugin adds a feature tab to an existing module.
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct Level1Config {
243    /// Target module: "devkit", "aichat", or a plugin module_id.
244    pub parent_module: String,
245    /// Unique tab ID within the parent module.
246    pub tab_id: String,
247    pub tab_label: String,
248    /// Lucide icon name.
249    pub tab_icon: String,
250    /// "after:snippet" | "before:summary" | "index:5"
251    #[serde(default)]
252    pub tab_position: Option<String>,
253    /// Path inside the package to the JS bundle for this tab's panel.
254    pub panel_entry: String,
255}
256
257/// Level 2 — The plugin injects into UI slots.
258#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct Level2Config {
260    /// Which slots the plugin injects into (see UI Slot Reference in the design doc).
261    pub slots: Vec<String>,
262}
263
264/// Level 3 — The plugin registers an AI assistant.
265#[derive(Debug, Clone, Serialize, Deserialize)]
266pub struct Level3Config {
267    pub assistant_id: String,
268    pub assistant_name: String,
269    #[serde(default)]
270    pub assistant_icon: Option<String>,
271    #[serde(default)]
272    pub assistant_description: Option<String>,
273    /// Path inside the package to the system prompt markdown file.
274    pub system_prompt_file: String,
275    /// Optional: auto-select a specific model_config_id for this assistant.
276    #[serde(default)]
277    pub preferred_model: Option<String>,
278}
279
280/// Level 4 — The plugin registers backend services / workflow step types.
281#[derive(Debug, Clone, Default, Serialize, Deserialize)]
282pub struct Level4Config {
283    /// Step type IDs this plugin registers (e.g. ["p4_sync", "p4_submit"]).
284    #[serde(default)]
285    pub workflow_step_types: Vec<String>,
286}
287
288/// Native library paths per platform.
289#[derive(Debug, Clone, Default, Serialize, Deserialize)]
290pub struct EntryConfig {
291    #[serde(default)]
292    pub native: Option<NativeEntry>,
293    #[serde(default)]
294    pub frontend: Option<String>,
295    #[serde(default)]
296    pub frontend_styles: Option<String>,
297}
298
299#[derive(Debug, Clone, Default, Serialize, Deserialize)]
300pub struct NativeEntry {
301    #[serde(default)]
302    pub macos_arm64: Option<String>,
303    #[serde(default)]
304    pub macos_x64: Option<String>,
305    #[serde(default)]
306    pub windows_x64: Option<String>,
307    #[serde(default)]
308    pub windows_arm64: Option<String>,
309    #[serde(default)]
310    pub linux_x64: Option<String>,
311    #[serde(default)]
312    pub linux_arm64: Option<String>,
313}
314
315impl NativeEntry {
316    /// Return the library path for the current platform/arch, if present.
317    pub fn for_current_platform(&self) -> Option<&str> {
318        #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
319        return self.macos_arm64.as_deref();
320
321        #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
322        return self.macos_x64.as_deref();
323
324        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
325        return self.windows_x64.as_deref();
326
327        #[cfg(all(target_os = "windows", target_arch = "aarch64"))]
328        return self.windows_arm64.as_deref();
329
330        #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
331        return self.linux_x64.as_deref();
332
333        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
334        return self.linux_arm64.as_deref();
335
336        #[allow(unreachable_code)]
337        None
338    }
339}
340
341#[derive(Debug, Clone, Serialize, Deserialize)]
342pub struct PluginDependency {
343    pub id: String,
344    /// SemVer requirement string, e.g. ">=1.0.0".
345    pub version: String,
346}
347
348#[derive(Debug, Clone, Serialize, Deserialize)]
349pub struct CommandDeclaration {
350    pub id: String,
351    #[serde(default)]
352    pub description: Option<String>,
353}
354
355#[cfg(test)]
356mod tests {
357    use super::{HostCapability, PluginManifest};
358
359    #[test]
360    fn manifest_supports_public_host_api_fields() {
361        let manifest: PluginManifest = serde_json::from_value(serde_json::json!({
362            "id": "dev.haloforge.example",
363            "name": "Example",
364            "version": "0.1.0",
365            "description": "Example plugin",
366            "author": "HaloForge Team",
367            "compatibility": {
368                "min_app_version": "0.1.0",
369                "min_host_api_version": "0.1.0",
370                "platforms": ["windows"]
371            },
372            "capability_levels": [2],
373            "host_capabilities": ["navigation", "aichat"],
374            "integration": {
375                "level2": { "slots": ["devkit.toolbar"] }
376            }
377        }))
378        .expect("manifest should deserialize");
379
380        assert_eq!(
381            manifest.compatibility.min_host_api_version.as_deref(),
382            Some("0.1.0")
383        );
384        assert_eq!(
385            manifest.host_capabilities,
386            vec![HostCapability::Navigation, HostCapability::AiChat]
387        );
388    }
389
390    #[test]
391    fn manifest_supports_window_policy_document_handlers() {
392        let manifest: PluginManifest = serde_json::from_value(serde_json::json!({
393            "id": "dev.haloforge.markdown",
394            "name": "Markdown",
395            "version": "0.2.13",
396            "description": "Markdown plugin",
397            "author": "HaloForge Team",
398            "compatibility": {
399                "min_app_version": "0.8.0",
400                "min_host_api_version": "0.2.16",
401                "platforms": ["windows", "macos", "linux"]
402            },
403            "capability_levels": [0],
404            "host_capabilities": ["navigation", "file_intents"],
405            "integration": {
406                "level0": {
407                    "module_id": "markdown",
408                    "module_label": "Markdown",
409                    "module_icon": "FileText",
410                    "panel_entry": "app/dist/index.js"
411                }
412            },
413            "window": {
414                "default_open_mode": "reuse_or_new",
415                "reuse_key": "resource",
416                "allow_multiple": true,
417                "document_handlers": [{
418                    "id": "markdown",
419                    "label": "Markdown",
420                    "extensions": [".md", ".markdown"],
421                    "mime_types": ["text/markdown"],
422                    "route": "/document",
423                    "resource_param": "path"
424                }]
425            }
426        }))
427        .expect("manifest should deserialize");
428
429        assert_eq!(manifest.window.default_open_mode.as_deref(), Some("reuse_or_new"));
430        assert_eq!(manifest.window.reuse_key.as_deref(), Some("resource"));
431        assert_eq!(manifest.window.allow_multiple, Some(true));
432        let handler = manifest
433            .window
434            .document_handlers
435            .first()
436            .expect("document handler should deserialize");
437        assert_eq!(handler.extensions, vec![".md", ".markdown"]);
438        assert_eq!(handler.mime_types, vec!["text/markdown"]);
439        assert_eq!(handler.route, "/document");
440        assert_eq!(handler.resource_param.as_deref(), Some("path"));
441    }
442
443    #[test]
444    fn host_capability_names_are_stable() {
445        assert_eq!(HostCapability::FileIntents.as_str(), "file_intents");
446        assert_eq!(HostCapability::FileDialogs.as_str(), "file_dialogs");
447        assert_eq!(HostCapability::DeepLinks.as_str(), "deep_links");
448        assert_eq!(HostCapability::ThemeRead.as_str(), "theme_read");
449    }
450}