use std::rc::Rc;
pub use agent_provider_config::management::{ProviderAdmin, ProviderSnapshot};
pub use agent_provider_config::protocol::*;
#[derive(Clone, Default)]
pub struct ProviderAdministration(Option<std::sync::Arc<dyn ProviderAdmin>>);
impl ProviderAdministration {
pub fn new(backend: std::sync::Arc<dyn ProviderAdmin>) -> Self {
Self(Some(backend))
}
pub fn is_available(&self) -> bool {
self.0.is_some()
}
pub fn get(&self) -> Result<std::sync::Arc<dyn ProviderAdmin>, ModelError> {
self.0
.clone()
.ok_or_else(|| ModelError::new(ErrorKind::Unavailable))
}
#[cfg(feature = "native")]
pub fn from_launcher() -> Result<Self, ModelError> {
tilo_model_client::runtime_client::provider_admin_from_launcher().map(Self::new)
}
#[cfg(not(feature = "native"))]
pub fn from_launcher() -> Result<Self, ModelError> {
Err(ModelError::new(ErrorKind::Unavailable))
}
}
#[derive(Clone, Default)]
pub struct Models(Option<Rc<dyn ModelBackend>>);
#[cfg(test)]
mod tests;
impl Models {
pub fn new(backend: Rc<dyn ModelBackend>) -> Self {
Self(Some(backend))
}
#[cfg(feature = "native")]
pub fn from_launcher() -> Result<Self, ModelError> {
tilo_model_client::runtime_client::from_launcher().map(Self::new)
}
#[cfg(not(feature = "native"))]
pub fn from_launcher() -> Result<Self, ModelError> {
Err(ModelError::new(ErrorKind::Unavailable))
}
pub fn stream<'a>(
&'a self,
request: &'a ModelRequest,
sink: StreamSink<'a>,
) -> CompletionFuture<'a> {
match &self.0 {
Some(backend) => backend.stream(request, sink),
None => self.complete(request),
}
}
pub fn is_available(&self) -> bool {
self.0.is_some()
}
pub fn complete<'a>(&'a self, request: &'a ModelRequest) -> CompletionFuture<'a> {
match &self.0 {
Some(backend) => backend.complete(request),
None => Box::pin(async {
Err(ModelError {
kind: ErrorKind::Unavailable,
message: "model access is unavailable from this process".into(),
})
}),
}
}
}