Skip to main content

tilo_process_services/
models.rs

1//! Asynchronous model access explicitly supplied by an authenticated process.
2//!
3//! Unlike presentation services this capability has no process default. A process
4//! binds its authenticated session to a backend before passing it to an app.
5use std::rc::Rc;
6
7pub use agent_provider_config::management::{ProviderAdmin, ProviderSnapshot};
8pub use agent_provider_config::protocol::*;
9
10/// Process-scoped authority to administer providers, independent of inference access.
11#[derive(Clone, Default)]
12pub struct ProviderAdministration(Option<std::sync::Arc<dyn ProviderAdmin>>);
13
14impl ProviderAdministration {
15    /// Bind a backend whose session the process has explicitly authorized.
16    pub fn new(backend: std::sync::Arc<dyn ProviderAdmin>) -> Self {
17        Self(Some(backend))
18    }
19
20    pub fn is_available(&self) -> bool {
21        self.0.is_some()
22    }
23
24    /// Obtain this process's authority, never an ambient or another mount's session.
25    pub fn get(&self) -> Result<std::sync::Arc<dyn ProviderAdmin>, ModelError> {
26        self.0
27            .clone()
28            .ok_or_else(|| ModelError::new(ErrorKind::Unavailable))
29    }
30
31    /// Capture the launcher's separately authorized administration capability.
32    #[cfg(feature = "native")]
33    pub fn from_launcher() -> Result<Self, ModelError> {
34        tilo_model_client::runtime_client::provider_admin_from_launcher().map(Self::new)
35    }
36
37    /// Launcher access requires native process services.
38    ///
39    /// # Errors
40    /// Always returns unavailable; an embedding process may use [`Self::new`].
41    #[cfg(not(feature = "native"))]
42    pub fn from_launcher() -> Result<Self, ModelError> {
43        Err(ModelError::new(ErrorKind::Unavailable))
44    }
45}
46
47/// A cloneable model capability. Clones retain the originating process session;
48/// constructing another process does not replace it. Browser backends need not be Send.
49#[derive(Clone, Default)]
50pub struct Models(Option<Rc<dyn ModelBackend>>);
51
52#[cfg(test)]
53mod tests;
54
55impl Models {
56    /// The process must authenticate and authorize this backend before injection.
57    pub fn new(backend: Rc<dyn ModelBackend>) -> Self {
58        Self(Some(backend))
59    }
60
61    /// Capture the launcher's authenticated capability and deliberate selection.
62    #[cfg(feature = "native")]
63    pub fn from_launcher() -> Result<Self, ModelError> {
64        tilo_model_client::runtime_client::from_launcher().map(Self::new)
65    }
66
67    /// Launcher access requires native process services.
68    ///
69    /// # Errors
70    /// Always returns unavailable; processes can inject a backend with [`Self::new`].
71    #[cfg(not(feature = "native"))]
72    pub fn from_launcher() -> Result<Self, ModelError> {
73        Err(ModelError::new(ErrorKind::Unavailable))
74    }
75
76    /// Stream presentation updates using only this process's capability.
77    pub fn stream<'a>(
78        &'a self,
79        request: &'a ModelRequest,
80        sink: StreamSink<'a>,
81    ) -> CompletionFuture<'a> {
82        match &self.0 {
83            Some(backend) => backend.stream(request, sink),
84            None => self.complete(request),
85        }
86    }
87
88    pub fn is_available(&self) -> bool {
89        self.0.is_some()
90    }
91
92    /// Complete without blocking the UI. No backend means unavailable, never
93    /// an ambient client, provider key lookup or fallback to another session.
94    /// The future is intentionally local: browser process capabilities are not `Send`.
95    pub fn complete<'a>(&'a self, request: &'a ModelRequest) -> CompletionFuture<'a> {
96        match &self.0 {
97            Some(backend) => backend.complete(request),
98            None => Box::pin(async {
99                Err(ModelError {
100                    kind: ErrorKind::Unavailable,
101                    message: "model access is unavailable from this process".into(),
102                })
103            }),
104        }
105    }
106}