1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! 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(),
})
}),
}
}
}