use serde_json::Value;
pub type Result<T> = std::result::Result<T, String>;
pub trait RemoteService {
fn location(&self) -> Option<(String, String)>;
fn list(&self, dir: &str) -> Result<Value>;
}
pub trait PrefsService {
fn schema(&self) -> Value;
fn get(&self, key: &str) -> Result<Value>;
fn set(&self, key: &str, value: Value) -> Result<()>;
}
pub trait AgentService {
fn models(&self) -> Result<Value>;
fn send(&self, conversation: &str, text: &str) -> Result<u64>;
fn cancel(&self, stream: u64);
}
pub trait PermissionService {
fn granted(&self) -> Vec<String>;
fn request(&self, permission: &str, why: &str) -> Result<bool>;
}
pub trait FetchService {
fn fetch(&self, url: &str) -> Result<Vec<u8>>;
}
pub const METHODS: &[(&str, Option<&str>)] = &[
("remote.location", Some("remote.read")),
("remote.list", Some("remote.read")),
("remote.read", Some("remote.read")),
("prefs.schema", None),
("prefs.get", None),
("prefs.set", Some("prefs.write")),
("prefs.reset", Some("prefs.write")),
("prefs.secret_prompt", Some("prefs.write")),
("agent.models", Some("agent.use")),
("agent.set_default", Some("agent.use")),
("agent.send", Some("agent.use")),
("agent.cancel", Some("agent.use")),
("agent.clear", Some("agent.use")),
("agent.revoke_all", Some("agent.use")),
("agent.state", Some("agent.use")),
("models.hf_search", Some("agent.use")),
("models.hf_details", Some("agent.use")),
("models.hf_install", Some("agent.use")),
("models.hf_cancel", Some("agent.use")),
("permissions.granted", None),
("permissions.request", None),
("fetch", Some("net.fetch")),
];
pub fn permission_for(method: &str) -> Option<&'static str> {
METHODS.iter().find(|(m, _)| *m == method).and_then(|(_, p)| *p)
}
pub fn allowed(method: &str, granted: &[String]) -> bool {
match METHODS.iter().find(|(m, _)| *m == method) {
Some((_, None)) => true,
Some((_, Some(p))) => granted.iter().any(|g| g == p),
None => false,
}
}