Skip to main content

mouser_mode_api/
services.rs

1//! Host services a plugin may call. The host implements these traits and
2//! answers [`crate::host_api::Call::Service`] requests with them, checking the
3//! permission named on each method first; the plugin SDK offers the same
4//! methods as RPC clients. Model calls go through [`AgentService`], so a
5//! plugin never sees an API key.
6
7use serde_json::Value;
8
9pub type Result<T> = std::result::Result<T, String>;
10
11/// Files on the shell's remote host (`remote.read`).
12pub trait RemoteService {
13    /// `user@host` and directory of the shell, if it is remote.
14    fn location(&self) -> Option<(String, String)>;
15    /// List a remote directory as `mouser_core::commander::Entry` values.
16    fn list(&self, dir: &str) -> Result<Value>;
17}
18
19/// Preferences (`prefs.write` for `set`): the host validates values and
20/// writes the file keeping its comments.
21pub trait PrefsService {
22    fn schema(&self) -> Value;
23    fn get(&self, key: &str) -> Result<Value>;
24    fn set(&self, key: &str, value: Value) -> Result<()>;
25}
26
27/// Models and the agent (`agent.use`). Tools run in the host after a
28/// host-drawn approval; replies stream back as `agent.stream` chunks.
29pub trait AgentService {
30    fn models(&self) -> Result<Value>;
31    /// Start a turn; returns a stream id.
32    fn send(&self, conversation: &str, text: &str) -> Result<u64>;
33    fn cancel(&self, stream: u64);
34}
35
36/// What this plugin was granted, and asking for more (host-drawn popup).
37pub trait PermissionService {
38    fn granted(&self) -> Vec<String>;
39    fn request(&self, permission: &str, why: &str) -> Result<bool>;
40}
41
42/// HTTP(S) fetches made by the host (`net.fetch`).
43pub trait FetchService {
44    fn fetch(&self, url: &str) -> Result<Vec<u8>>;
45}
46
47/// Method names and the permission each needs.
48pub const METHODS: &[(&str, Option<&str>)] = &[
49    ("remote.location", Some("remote.read")),
50    ("remote.list", Some("remote.read")),
51    ("remote.read", Some("remote.read")),
52    ("prefs.schema", None),
53    ("prefs.get", None),
54    ("prefs.set", Some("prefs.write")),
55    ("prefs.reset", Some("prefs.write")),
56    ("prefs.secret_prompt", Some("prefs.write")),
57    ("agent.models", Some("agent.use")),
58    ("agent.set_default", Some("agent.use")),
59    ("agent.send", Some("agent.use")),
60    ("agent.cancel", Some("agent.use")),
61    ("agent.clear", Some("agent.use")),
62    ("agent.revoke_all", Some("agent.use")),
63    ("agent.state", Some("agent.use")),
64    // Hugging Face models for the local engine (search, details, a
65    // consented install that Mouser downloads; progress as `models.download`).
66    ("models.hf_search", Some("agent.use")),
67    ("models.hf_details", Some("agent.use")),
68    ("models.hf_install", Some("agent.use")),
69    ("models.hf_cancel", Some("agent.use")),
70    ("permissions.granted", None),
71    ("permissions.request", None),
72    ("fetch", Some("net.fetch")),
73];
74
75/// Permission a method needs, if any. `None` means unrestricted or unknown.
76pub fn permission_for(method: &str) -> Option<&'static str> {
77    METHODS.iter().find(|(m, _)| *m == method).and_then(|(_, p)| *p)
78}
79
80/// Whether `granted` allows calling `method`; unknown methods are refused.
81pub fn allowed(method: &str, granted: &[String]) -> bool {
82    match METHODS.iter().find(|(m, _)| *m == method) {
83        Some((_, None)) => true,
84        Some((_, Some(p))) => granted.iter().any(|g| g == p),
85        None => false,
86    }
87}