Skip to main content

kitchen_tools/plugin/
mod.rs

1//! Plugins for the neuro-kitchen apps, as separate programs (lumen's model
2//! plugins, dsp-kitchen's sorters). The app never links a plugin, so a plugin never adds weight,
3//! licences or a runtime to the app, and can be installed, updated or removed on its own.
4//!
5//! - [`protocol`]: requests and events, JSON lines over the program's stdin and stdout.
6//! - [`PluginInfo`]: what a plugin offers (its manifest): name, version, the protocol it speaks,
7//!   its licence, its capabilities, whether its runtime is ready, and the app's own fields.
8//!   [`PluginInfo::compatible`] is the handshake.
9//! - [`PluginKind`]: an app's plugins (programs named `<prefix><name>`) and where they are
10//!   found ([`PluginKind::discover`]).
11//! - [`PluginProcess`]: a plugin in server mode, kept alive between requests.
12//! - [`Manager`]: install (from a file or a verified download), update, remove, disk use.
13//!
14//! The manager's Settings page is kitchen-shell's (`plugins_view::PluginsPage`): the runtime
15//! draws nothing.
16
17pub mod client;
18pub mod discover;
19pub mod manager;
20pub mod protocol;
21
22pub use client::PluginProcess;
23pub use discover::PluginKind;
24pub use manager::{Installed, Manager};
25pub use protocol::{CoreRequest, Event, PROTOCOL};
26
27use serde::{Deserialize, Serialize};
28
29/// Whether a plugin's runtime can run here, and why not.
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct RuntimeStatus {
32    /// Ready to run.
33    pub ready: bool,
34    /// What runs it (`ONNX Runtime 1.22 (CUDA, CPU)`), or what is missing and how to get it.
35    pub detail: String,
36}
37
38fn protocol_one() -> u32 {
39    1
40}
41
42/// A plugin program and what it offers (its answer to `info`).
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub struct PluginInfo {
45    /// Its name.
46    pub name: String,
47    /// Its version.
48    pub version: String,
49    /// The protocol version it speaks (1 when it does not say).
50    #[serde(default = "protocol_one")]
51    pub protocol: u32,
52    /// Its licence (it may differ from the app's).
53    #[serde(default)]
54    pub licence: String,
55    /// What it can do (`segment`, `instances`, `sort`): the app offers what these name.
56    #[serde(default)]
57    pub capabilities: Vec<String>,
58    /// Whether its runtime is ready.
59    #[serde(default)]
60    pub runtime: Option<RuntimeStatus>,
61    /// Where it was found (set by discovery, not by the plugin).
62    #[serde(default)]
63    pub program: String,
64    /// The app's own fields (lumen's `models`).
65    #[serde(flatten)]
66    pub extra: serde_json::Map<String, serde_json::Value>,
67}
68
69impl PluginInfo {
70    /// The handshake: whether this app can talk to it (the same protocol version).
71    pub fn compatible(&self) -> Result<(), String> {
72        match self.protocol {
73            PROTOCOL => Ok(()),
74            p if p > PROTOCOL => {
75                Err(format!("{} speaks protocol {p}; this app speaks {PROTOCOL}: update the app", self.name))
76            }
77            p => Err(format!("{} speaks protocol {p}; this app speaks {PROTOCOL}: update the plugin", self.name)),
78        }
79    }
80
81    /// Whether it can do `capability`.
82    pub fn can(&self, capability: &str) -> bool {
83        self.capabilities.iter().any(|c| c == capability)
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn manifests_keep_the_apps_fields_and_check_the_protocol() {
93        let info: PluginInfo = serde_json::from_str(r#"{"name":"lumen-ml-sam","version":"0.1.0","licence":"Apache-2.0","capabilities":["segment"],"models":[{"id":"sam2-tiny"}]}"#).unwrap();
94        assert_eq!(info.protocol, 1, "a plugin that does not say speaks 1");
95        assert!(info.compatible().is_ok() && info.can("segment") && !info.can("sort"));
96        assert_eq!(info.extra["models"][0]["id"], "sam2-tiny");
97        let newer = PluginInfo { protocol: 2, ..info.clone() };
98        assert!(newer.compatible().unwrap_err().contains("update the app"));
99        let back: PluginInfo = serde_json::from_str(&serde_json::to_string(&info).unwrap()).unwrap();
100        assert_eq!(back, info);
101    }
102}