tilo-process-services 0.5.1

The services an app borrows from its process.
Documentation
//! Asynchronous model access explicitly supplied by an authenticated process.
//!
//! Unlike presentation services this capability has no process default. A process
//! binds its authenticated session to a backend before passing it to an app.
use std::rc::Rc;

pub use agent_provider_config::management::{ProviderAdmin, ProviderSnapshot};
pub use agent_provider_config::protocol::*;

/// Process-scoped authority to administer providers, independent of inference access.
#[derive(Clone, Default)]
pub struct ProviderAdministration(Option<std::sync::Arc<dyn ProviderAdmin>>);

impl ProviderAdministration {
    /// Bind a backend whose session the process has explicitly authorized.
    pub fn new(backend: std::sync::Arc<dyn ProviderAdmin>) -> Self {
        Self(Some(backend))
    }

    pub fn is_available(&self) -> bool {
        self.0.is_some()
    }

    /// Obtain this process's authority, never an ambient or another mount's session.
    pub fn get(&self) -> Result<std::sync::Arc<dyn ProviderAdmin>, ModelError> {
        self.0
            .clone()
            .ok_or_else(|| ModelError::new(ErrorKind::Unavailable))
    }

    /// Capture the launcher's separately authorized administration capability.
    #[cfg(feature = "native")]
    pub fn from_launcher() -> Result<Self, ModelError> {
        tilo_model_client::runtime_client::provider_admin_from_launcher().map(Self::new)
    }

    /// Launcher access requires native process services.
    ///
    /// # Errors
    /// Always returns unavailable; an embedding process may use [`Self::new`].
    #[cfg(not(feature = "native"))]
    pub fn from_launcher() -> Result<Self, ModelError> {
        Err(ModelError::new(ErrorKind::Unavailable))
    }
}

/// A cloneable model capability. Clones retain the originating process session;
/// constructing another process does not replace it. Browser backends need not be Send.
#[derive(Clone, Default)]
pub struct Models(Option<Rc<dyn ModelBackend>>);

#[cfg(test)]
mod tests;

impl Models {
    /// The process must authenticate and authorize this backend before injection.
    pub fn new(backend: Rc<dyn ModelBackend>) -> Self {
        Self(Some(backend))
    }

    /// Capture the launcher's authenticated capability and deliberate selection.
    #[cfg(feature = "native")]
    pub fn from_launcher() -> Result<Self, ModelError> {
        tilo_model_client::runtime_client::from_launcher().map(Self::new)
    }

    /// Launcher access requires native process services.
    ///
    /// # Errors
    /// Always returns unavailable; processes can inject a backend with [`Self::new`].
    #[cfg(not(feature = "native"))]
    pub fn from_launcher() -> Result<Self, ModelError> {
        Err(ModelError::new(ErrorKind::Unavailable))
    }

    /// Stream presentation updates using only this process's capability.
    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()
    }

    /// Complete without blocking the UI. No backend means unavailable, never
    /// an ambient client, provider key lookup or fallback to another session.
    /// The future is intentionally local: browser process capabilities are not `Send`.
    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(),
                })
            }),
        }
    }
}