pub mod cli;
use serde_json::Value;
pub use cli::CliTransport;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransportErrorKind {
Unavailable,
Refused,
Failed,
NotFound,
Timeout,
Protocol,
Unauthorized,
Unsupported,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TransportError {
pub kind: TransportErrorKind,
pub message: String,
pub code: Option<i32>,
pub body: Option<Value>,
}
impl TransportError {
pub fn new(kind: TransportErrorKind, message: impl Into<String>) -> TransportError {
TransportError {
kind,
message: message.into(),
code: None,
body: None,
}
}
pub fn unavailable(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Unavailable, message)
}
pub fn refused(message: impl Into<String>, body: Option<Value>) -> TransportError {
TransportError {
body,
..TransportError::new(TransportErrorKind::Refused, message)
}
}
pub fn failed(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Failed, message)
}
pub fn not_found(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::NotFound, message)
}
pub fn protocol(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Protocol, message)
}
pub fn unsupported(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Unsupported, message)
}
pub fn with_code(mut self, code: i32) -> TransportError {
self.code = Some(code);
self
}
pub fn with_body(mut self, body: Value) -> TransportError {
self.body = Some(body);
self
}
pub fn is_refused(&self) -> bool {
self.kind == TransportErrorKind::Refused
}
pub fn headline(&self) -> &'static str {
match self.kind {
TransportErrorKind::Unavailable => "unavailable",
TransportErrorKind::Refused => "refused",
TransportErrorKind::Failed => "failed",
TransportErrorKind::NotFound => "not found",
TransportErrorKind::Timeout => "timed out",
TransportErrorKind::Protocol => "unexpected answer",
TransportErrorKind::Unauthorized => "not authorized",
TransportErrorKind::Unsupported => "not available here",
}
}
pub fn reasons(&self) -> Vec<String> {
let Some(body) = &self.body else {
return Vec::new();
};
let mut out: Vec<String> = body
.get("delete_blockers")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(Value::as_str)
.map(str::to_string)
.collect()
})
.unwrap_or_default();
if let Some(e) = body.get("error").and_then(Value::as_str) {
if !e.is_empty() && !out.iter().any(|o| o == e) {
out.push(e.to_string());
}
}
out
}
}
impl std::fmt::Display for TransportError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.headline(), self.message)
}
}
impl std::error::Error for TransportError {}
pub trait ConsoleTransport: Send + Sync {
fn host_profile(&self) -> Result<Value, TransportError>;
fn engines_status(&self, probe: bool) -> Result<Value, TransportError>;
fn models_catalog(
&self,
q: &str,
engine: Option<&str>,
fits_only: bool,
) -> Result<Value, TransportError>;
fn models_installed(&self, provider: Option<&str>) -> Result<Value, TransportError>;
fn start_download(
&self,
provider: &str,
artifact: &str,
expected_bytes: Option<u64>,
) -> Result<Value, TransportError>;
fn delete_model(
&self,
provider: &str,
artifact: &str,
force: bool,
) -> Result<Value, TransportError>;
fn engine_install(&self, id: &str, dry_run: bool) -> Result<Value, TransportError>;
fn job(&self, id: &str) -> Result<Value, TransportError>;
fn cancel_job(&self, id: &str) -> Result<Value, TransportError>;
fn host_label(&self) -> String {
"this machine".to_string()
}
fn cancel_download(&self, id: &str) -> Result<Value, TransportError> {
self.cancel_job(id)
}
fn download_job(&self, id: &str) -> Result<Value, TransportError> {
self.job(id)
}
fn capabilities(&self) -> TransportCaps {
TransportCaps::default()
}
fn models_catalog_hub(
&self,
q: &str,
engine: Option<&str>,
fits_only: bool,
) -> Result<Value, TransportError> {
let _ = (q, engine, fits_only);
Err(TransportError::unsupported(
"this backend cannot search Hugging Face",
))
}
fn download_jobs(&self) -> Result<Value, TransportError> {
Err(TransportError::unsupported(
"this backend cannot list its downloads",
))
}
fn capability_defaults(&self) -> Result<Value, TransportError> {
Err(TransportError::unsupported(
"this backend cannot read the capability defaults",
))
}
fn set_text_default(&self, provider: &str, model: &str) -> Result<Value, TransportError> {
let _ = (provider, model);
Err(TransportError::unsupported(
"this backend cannot change the default text model",
))
}
fn engine_install_at(
&self,
id: &str,
dry_run: bool,
location: InstallLocation,
) -> Result<Value, TransportError> {
match location {
InstallLocation::Auto => self.engine_install(id, dry_run),
other => Err(TransportError::unsupported(format!(
"this backend cannot choose where an engine is installed ({})",
other.as_str()
))),
}
}
fn engine_job_continue(
&self,
job_id: &str,
action: Option<&str>,
) -> Result<Value, TransportError> {
let _ = (job_id, action);
Err(TransportError::unsupported(
"this backend cannot continue a paused install",
))
}
fn engine_server(&self, id: &str, action: ServerAction) -> Result<Value, TransportError> {
let _ = (id, action);
Err(TransportError::unsupported(
"this backend cannot start or stop engine servers",
))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TransportCaps {
pub hub_search: bool,
pub text_default: bool,
pub downloads_feed: bool,
pub engine_continue: bool,
pub engine_server: bool,
pub install_location: bool,
}
impl TransportCaps {
pub const ALL: TransportCaps = TransportCaps {
hub_search: true,
text_default: true,
downloads_feed: true,
engine_continue: true,
engine_server: true,
install_location: true,
};
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum InstallLocation {
#[default]
Auto,
User,
System,
}
impl InstallLocation {
pub fn as_str(self) -> &'static str {
match self {
InstallLocation::Auto => "auto",
InstallLocation::User => "user",
InstallLocation::System => "system",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ServerAction {
Start,
Stop,
}
impl ServerAction {
pub fn as_str(self) -> &'static str {
match self {
ServerAction::Start => "start",
ServerAction::Stop => "stop",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn reasons_collect_blockers_and_error() {
let e = TransportError::refused(
"model is loaded",
Some(json!({"delete_blockers": ["loaded"], "error": "unload it first"})),
);
assert!(e.is_refused());
assert_eq!(e.reasons(), vec!["loaded", "unload it first"]);
assert_eq!(e.to_string(), "refused: model is loaded");
assert!(TransportError::failed("x").reasons().is_empty());
}
struct Bare;
impl ConsoleTransport for Bare {
fn host_profile(&self) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn engines_status(&self, _p: bool) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn models_catalog(
&self,
_q: &str,
_e: Option<&str>,
_f: bool,
) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn models_installed(&self, _p: Option<&str>) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn start_download(
&self,
_p: &str,
_a: &str,
_b: Option<u64>,
) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn delete_model(&self, _p: &str, _a: &str, _f: bool) -> Result<Value, TransportError> {
Ok(json!({}))
}
fn engine_install(&self, id: &str, _d: bool) -> Result<Value, TransportError> {
Ok(json!({"engine": id}))
}
fn job(&self, id: &str) -> Result<Value, TransportError> {
Ok(json!({"job_id": id, "via": "job"}))
}
fn cancel_job(&self, id: &str) -> Result<Value, TransportError> {
Ok(json!({"job_id": id, "via": "cancel_job"}))
}
}
#[test]
fn optional_verbs_default_to_unsupported_never_a_fake() {
let t = Bare;
assert_eq!(t.capabilities(), TransportCaps::default());
for e in [
t.models_catalog_hub("q", None, false).unwrap_err(),
t.download_jobs().unwrap_err(),
t.capability_defaults().unwrap_err(),
t.set_text_default("p", "m").unwrap_err(),
t.engine_job_continue("j", None).unwrap_err(),
t.engine_server("ollama", ServerAction::Start).unwrap_err(),
t.engine_install_at("ollama", false, InstallLocation::User)
.unwrap_err(),
] {
assert_eq!(e.kind, TransportErrorKind::Unsupported, "{e}");
assert_eq!(e.headline(), "not available here");
}
assert_eq!(
t.engine_install_at("ollama", false, InstallLocation::Auto)
.unwrap()["engine"],
"ollama"
);
assert_eq!(t.download_job("dl_1").unwrap()["via"], "job");
assert_eq!(t.cancel_download("dl_1").unwrap()["via"], "cancel_job");
}
}